]> gitweb.factorcode.org Git - factor.git/blob - misc/fuel/fuel-edit.el
756f06550fbb3cc133673c2d479d95ac4f8df052
[factor.git] / misc / fuel / fuel-edit.el
1 ;;; fuel-edit.el -- utilities for file editing
2
3 ;; Copyright (C) 2009 Jose Antonio Ortega Ruiz
4 ;; See http://factorcode.org/license.txt for BSD license.
5
6 ;; Author: Jose Antonio Ortega Ruiz <jao@gnu.org>
7 ;; Keywords: languages, fuel, factor
8 ;; Start date: Mon Jan 05, 2009 21:16
9
10 ;;; Comentary:
11
12 ;; Locating and opening factor source and documentation files.
13
14 ;;; Code:
15
16 (require 'fuel-completion)
17 (require 'fuel-eval)
18 (require 'fuel-base)
19 (require 'factor-mode)
20
21 (require 'etags)
22
23 \f
24 ;;; Customization
25
26 (defcustom fuel-edit-word-method nil
27   "How the new buffer is opened when invoking `fuel-edit-word-at-point'."
28   :group 'fuel
29   :type '(choice (const :tag "Other window" window)
30                  (const :tag "Other frame" frame)
31                  (const :tag "Current window" nil)))
32
33 \f
34 ;;; Auxiliar functions:
35
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))))
40
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)))))
51
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))))
55
56 \f
57 ;;; Editing commands:
58
59 (defvar fuel-edit--word-history nil)
60
61 ;;;###autoload
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."
66   (interactive "P")
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))))
70
71 ;;;###autoload
72 (defun fuel-edit-word (&optional arg)
73   "Asks for a word to edit, with completion.
74 With prefix, only words visible in the current vocabulary are
75 offered."
76   (interactive "P")
77   (let* ((word (fuel-completion--read-word "Edit word: "
78                                            nil
79                                            fuel-edit--word-history
80                                            arg))
81          (cmd `(:fuel* ((:quote ,word) fuel-get-word-location))))
82     (fuel-edit--try-edit (fuel-eval--send/wait cmd))))
83
84 (defun fuel-edit-word-at-point (&optional arg)
85   "Opens a new window visiting the definition of the word at point.
86 With prefix, asks for the word to edit."
87   (interactive "P")
88   (let* ((word (or (and (not arg) (factor-symbol-at-point))
89                    (fuel-completion--read-word "Edit word: ")))
90          (cmd `(:fuel* ((:quote ,word) fuel-get-word-location)))
91          (marker (and (not arg) (point-marker))))
92     (if (and (not arg) (factor-on-vocab))
93         (fuel-edit-vocabulary nil word)
94       (fuel-edit--try-edit (fuel-eval--send/wait cmd)))
95     (when marker (ring-insert find-tag-marker-ring marker))))
96
97 (defun fuel-edit-word-doc-at-point (&optional arg word)
98   "Opens a new window visiting the documentation file for the word at point.
99 With prefix, asks for the word to edit."
100   (interactive "P")
101   (let* ((word (or word
102                    (and (not arg) (factor-symbol-at-point))
103                    (fuel-completion--read-word "Edit word: ")))
104          (cmd `(:fuel* ((:quote ,word) fuel-get-doc-location)))
105          (marker (and (not arg) (point-marker))))
106     (condition-case nil
107         (fuel-edit--try-edit (fuel-eval--send/wait cmd))
108       (error
109        (message "Documentation for '%s' not found" word)
110        (when (and (eq major-mode 'factor-mode)
111                   (y-or-n-p (concat "No documentation found. "
112                                     "Do you want to open the vocab's "
113                                     "doc file? ")))
114          (when marker (ring-insert find-tag-marker-ring marker))
115          (find-file-other-window
116           (format "%s-docs.factor"
117                   (file-name-sans-extension (buffer-file-name)))))))))
118
119 (defun fuel-edit-pop-edit-word-stack ()
120   "Pop back to where \\[fuel-edit-word-at-point] or \\[fuel-edit-word-doc-at-point]
121 was last invoked."
122   (interactive)
123   (condition-case nil
124       (pop-tag-mark)
125     (error "No previous location for find word or vocab invocation")))
126
127 (defvar fuel-edit--buffer-history nil)
128
129 (defun fuel-switch-to-buffer (&optional method)
130   "Switch to any of the existing Factor buffers, with completion."
131   (interactive)
132   (let ((buffer (completing-read "Factor buffer: "
133                                  (remove (buffer-name)
134                                          (mapcar 'buffer-name (buffer-list)))
135                                  #'(lambda (s) (string-match "\\.factor$" s))
136                                  t
137                                  nil
138                                  fuel-edit--buffer-history)))
139     (cond ((eq method 'window) (switch-to-buffer-other-window buffer))
140           ((eq method 'frame) (switch-to-buffer-other-frame buffer))
141           (t (switch-to-buffer buffer)))))
142
143 (defun fuel-switch-to-buffer-other-window ()
144   "Switch to any of the existing Factor buffers, in other window."
145   (interactive)
146   (fuel-switch-to-buffer 'window))
147
148 (defun fuel-switch-to-buffer-other-frame ()
149   "Switch to any of the existing Factor buffers, in other frame."
150   (interactive)
151   (fuel-switch-to-buffer 'frame))
152
153 \f
154 (provide 'fuel-edit)
155 ;;; fuel-edit.el ends here