]> gitweb.factorcode.org Git - factor.git/blob - misc/fuel/fuel-edit.el
Merge branch 'master' of git://factorcode.org/git/factor
[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
20 (require 'etags)
21
22 \f
23 ;;; Customization
24
25 (defmacro fuel-edit--define-custom-visit (var group doc)
26   `(defcustom ,var nil
27      ,doc
28      :group ',group
29      :type '(choice (const :tag "Other window" window)
30                     (const :tag "Other frame" frame)
31                     (const :tag "Current window" nil))))
32
33 (fuel-edit--define-custom-visit
34  fuel-edit-word-method fuel
35  "How the new buffer is opened when invoking \\[fuel-edit-word-at-point]")
36
37 \f
38 ;;; Auxiliar functions:
39
40 (defun fuel-edit--visit-file (file method)
41   (cond ((eq method 'window) (find-file-other-window file))
42         ((eq method 'frame) (find-file-other-frame file))
43         (t (find-file file))))
44
45 (defun fuel-edit--looking-at-vocab ()
46   (save-excursion
47     (fuel-syntax--beginning-of-defun)
48     (looking-at "USING:\\|USE:\\|IN:")))
49
50 (defun fuel-edit--try-edit (ret)
51   (let* ((err (fuel-eval--retort-error ret))
52          (loc (fuel-eval--retort-result ret)))
53     (when (or err (not loc) (not (listp loc)) (not (stringp (car loc))))
54       (error "Couldn't find edit location"))
55     (unless (file-readable-p (car loc))
56       (error "Couldn't open '%s' for read" (car loc)))
57     (fuel-edit--visit-file (car loc) fuel-edit-word-method)
58     (goto-line (if (numberp (cadr loc)) (cadr loc) 1))))
59
60 (defun fuel-edit--edit-article (name)
61   (let ((cmd `(:fuel* (,name fuel-get-article-location) "fuel" t)))
62     (fuel-edit--try-edit (fuel-eval--send/wait cmd))))
63
64 \f
65 ;;; Editing commands:
66
67 (defvar fuel-edit--word-history nil)
68
69 (defun fuel-edit-vocabulary (&optional refresh vocab)
70   "Visits vocabulary file in Emacs.
71 When called interactively, asks for vocabulary with completion.
72 With prefix argument, refreshes cached vocabulary list."
73   (interactive "P")
74   (let* ((vocab (or vocab (fuel-completion--read-vocab refresh)))
75          (cmd `(:fuel* (,vocab fuel-get-vocab-location) "fuel" t)))
76     (fuel-edit--try-edit (fuel-eval--send/wait cmd))))
77
78 (defun fuel-edit-word (&optional arg)
79   "Asks for a word to edit, with completion.
80 With prefix, only words visible in the current vocabulary are
81 offered."
82   (interactive "P")
83   (let* ((word (fuel-completion--read-word "Edit word: "
84                                            nil
85                                            fuel-edit--word-history
86                                            arg))
87          (cmd `(:fuel* ((:quote ,word) fuel-get-word-location))))
88     (fuel-edit--try-edit (fuel-eval--send/wait cmd))))
89
90 (defun fuel-edit-word-at-point (&optional arg)
91   "Opens a new window visiting the definition of the word at point.
92 With prefix, asks for the word to edit."
93   (interactive "P")
94   (let* ((word (or (and (not arg) (fuel-syntax-symbol-at-point))
95                    (fuel-completion--read-word "Edit word: ")))
96          (cmd `(:fuel* ((:quote ,word) fuel-get-word-location)))
97          (marker (and (not arg) (point-marker))))
98     (if (and (not arg) (fuel-edit--looking-at-vocab))
99         (fuel-edit-vocabulary nil word)
100       (fuel-edit--try-edit (fuel-eval--send/wait cmd)))
101     (when marker (ring-insert find-tag-marker-ring marker))))
102
103 (defun fuel-edit-word-doc-at-point (&optional arg word)
104   "Opens a new window visiting the documentation file for the word at point.
105 With prefix, asks for the word to edit."
106   (interactive "P")
107   (let* ((word (or word
108                    (and (not arg) (fuel-syntax-symbol-at-point))
109                    (fuel-completion--read-word "Edit word: ")))
110          (cmd `(:fuel* ((:quote ,word) fuel-get-doc-location)))
111          (marker (and (not arg) (point-marker))))
112     (condition-case nil
113         (fuel-edit--try-edit (fuel-eval--send/wait cmd))
114       (error
115        (message "Documentation for '%s' not found" word)
116        (when (and (eq major-mode 'factor-mode)
117                   (y-or-n-p (concat "No documentation found. "
118                                     "Do you want to open the vocab's "
119                                     "doc file? ")))
120          (when marker (ring-insert find-tag-marker-ring marker))
121          (find-file-other-window
122           (format "%s-docs.factor"
123                   (file-name-sans-extension (buffer-file-name)))))))))
124
125 (defun fuel-edit-pop-edit-word-stack ()
126   "Pop back to where \\[fuel-edit-word-at-point] or \\[fuel-edit-word-doc-at-point]
127 was last invoked."
128   (interactive)
129   (condition-case nil
130       (pop-tag-mark)
131     (error "No previous location for find word or vocab invocation")))
132
133 (defvar fuel-edit--buffer-history nil)
134
135 (defun fuel-switch-to-buffer (&optional method)
136   "Switch to any of the existing Factor buffers, with completion."
137   (interactive)
138   (let ((buffer (completing-read "Factor buffer: "
139                                  (remove (buffer-name)
140                                          (mapcar 'buffer-name (buffer-list)))
141                                  '(lambda (s) (string-match "\\.factor$" s))
142                                  t
143                                  nil
144                                  fuel-edit--buffer-history)))
145     (cond ((eq method 'window) (switch-to-buffer-other-window buffer))
146           ((eq method 'frame) (switch-to-buffer-other-frame buffer))
147           (t (switch-to-buffer buffer)))))
148
149 (defun fuel-switch-to-buffer-other-window ()
150   "Switch to any of the existing Factor buffers, in other window."
151   (interactive)
152   (fuel-switch-to-buffer 'window))
153
154 (defun fuel-switch-to-buffer-other-frame ()
155   "Switch to any of the existing Factor buffers, in other frame."
156   (interactive)
157   (fuel-switch-to-buffer 'frame))
158
159 \f
160 (provide 'fuel-edit)
161 ;;; fuel-edit.el ends here