1 ;;; fuel-edit.el -- utilities for file editing -*- lexical-binding: t -*-
3 ;; Copyright (C) 2009 Jose Antonio Ortega Ruiz
4 ;; See http://factorcode.org/license.txt for BSD license.
6 ;; Author: Jose Antonio Ortega Ruiz <jao@gnu.org>
7 ;; Keywords: languages, fuel, factor
8 ;; Start date: Mon Jan 05, 2009 21:16
12 ;; Locating and opening factor source and documentation files.
16 (require 'fuel-completion)
19 (require 'factor-mode)
26 (defcustom fuel-edit-word-method nil
27 "How the new buffer is opened when invoking `fuel-edit-word-at-point'."
29 :type '(choice (const :tag "Other window" window)
30 (const :tag "Other frame" frame)
31 (const :tag "Current window" nil)))
34 ;;; Auxiliar functions:
36 (defun fuel-edit--visit-file (file method)
37 (cond ((eq method 'window) (find-file-other-window file))
38 ((eq method 'frame) (find-file-other-frame file))
39 (t (find-file file))))
41 (defun fuel-edit--try-edit (ret)
42 (let* ((err (fuel-eval--retort-error ret))
43 (loc (fuel-eval--retort-result ret)))
44 (when (or err (not loc) (not (listp loc)) (not (stringp (car loc))))
45 (error "Couldn't find edit location"))
46 (unless (file-readable-p (car loc))
47 (error "Couldn't open '%s' for read" (car loc)))
48 (fuel-edit--visit-file (car loc) fuel-edit-word-method)
49 (goto-char (point-min))
50 (forward-line (1- (if (numberp (cadr loc)) (cadr loc) 1)))))
52 (defun fuel-edit--edit-article (name)
53 (let ((cmd `(:fuel* (,name fuel-get-article-location) "fuel" t)))
54 (fuel-edit--try-edit (fuel-eval--send/wait cmd))))
59 (defvar fuel-edit--word-history nil)
62 (defun fuel-edit-vocabulary (&optional refresh vocab)
63 "Visits vocabulary file in Emacs.
64 When called interactively, asks for vocabulary with completion.
65 With prefix argument, refreshes cached vocabulary list."
67 (let* ((vocab (or vocab (fuel-completion--read-vocab refresh)))
68 (cmd `(:fuel* (,vocab fuel-get-vocab-location) "fuel" t)))
69 (fuel-edit--try-edit (fuel-eval--send/wait cmd))))
72 (defun fuel-edit-word-at-point (&optional arg)
73 "Opens a new window visiting the definition of the word at point.
74 With prefix, asks for the word to edit."
76 (let* ((word (or (and (not arg) (factor-symbol-at-point))
77 (fuel-completion--read-word "Edit word: ")))
78 (cmd `(:fuel* ((:quote ,word) fuel-get-word-location)))
79 (marker (and (not arg) (point-marker))))
80 (if (and (not arg) (factor-on-vocab))
81 (fuel-edit-vocabulary nil word)
82 (fuel-edit--try-edit (fuel-eval--send/wait cmd)))
83 (when marker (ring-insert find-tag-marker-ring marker))))
85 (defun fuel-edit-word-doc-at-point (&optional arg word)
86 "Opens a new window visiting the documentation file for the word at point.
87 With prefix, asks for the word to edit."
90 (and (not arg) (factor-symbol-at-point))
91 (fuel-completion--read-word "Edit word: ")))
92 (cmd `(:fuel* ((:quote ,word) fuel-get-doc-location)))
93 (marker (and (not arg) (point-marker))))
95 (fuel-edit--try-edit (fuel-eval--send/wait cmd))
97 (message "Documentation for '%s' not found" word)
98 (when (and (eq major-mode 'factor-mode)
99 (y-or-n-p (concat "No documentation found. "
100 "Do you want to open the vocab's "
102 (when marker (ring-insert find-tag-marker-ring marker))
103 (find-file-other-window
104 (format "%s-docs.factor"
105 (file-name-sans-extension (buffer-file-name)))))))))
107 (defun fuel-edit-pop-edit-word-stack ()
108 "Pop back to where \\[fuel-edit-word-at-point] or \\[fuel-edit-word-doc-at-point]
113 (error "No previous location for find word or vocab invocation")))
115 (defvar fuel-edit--buffer-history nil)
117 (defun fuel-switch-to-buffer (&optional method)
118 "Switch to any of the existing Factor buffers, with completion."
120 (let ((buffer (completing-read "Factor buffer: "
121 (remove (buffer-name)
122 (mapcar 'buffer-name (buffer-list)))
123 #'(lambda (s) (string-match "\\.factor$" s))
126 fuel-edit--buffer-history)))
127 (cond ((eq method 'window) (switch-to-buffer-other-window buffer))
128 ((eq method 'frame) (switch-to-buffer-other-frame buffer))
129 (t (switch-to-buffer buffer)))))
131 (defun fuel-switch-to-buffer-other-window ()
132 "Switch to any of the existing Factor buffers, in other window."
134 (fuel-switch-to-buffer 'window))
136 (defun fuel-switch-to-buffer-other-frame ()
137 "Switch to any of the existing Factor buffers, in other frame."
139 (fuel-switch-to-buffer 'frame))
143 ;;; fuel-edit.el ends here