]> gitweb.factorcode.org Git - factor.git/blob - misc/fuel/fuel-edit.el
FUEL: Correctly detect vocabs when M-. in a USING:/USE: form.
[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 (defcustom fuel-edit-word-method nil
26   "How the new buffer is opened when invoking
27 \\[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--looking-at-vocab ()
37   (save-excursion
38     (fuel-syntax--beginning-of-defun)
39     (looking-at "USING:\\|USE:")))
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     (cond ((eq fuel-edit-word-method 'window) (find-file-other-window (car loc)))
49           ((eq fuel-edit-word-method 'frame) (find-file-other-frame (car loc)))
50           (t (find-file (car loc))))
51     (goto-line (if (numberp (cadr loc)) (cadr loc) 1))))
52
53 (defun fuel-edit--read-vocabulary-name (refresh)
54   (let* ((vocabs (fuel-completion--vocabs refresh))
55          (prompt "Vocabulary name: "))
56     (if vocabs
57         (completing-read prompt vocabs nil nil nil fuel-edit--vocab-history)
58       (read-string prompt nil fuel-edit--vocab-history))))
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 (defvar fuel-edit--vocab-history nil)
69 (defvar fuel-edit--previous-location nil)
70
71 (defun fuel-edit-vocabulary (&optional refresh vocab)
72   "Visits vocabulary file in Emacs.
73 When called interactively, asks for vocabulary with completion.
74 With prefix argument, refreshes cached vocabulary list."
75   (interactive "P")
76   (let* ((vocab (or vocab (fuel-edit--read-vocabulary-name refresh)))
77          (cmd `(:fuel* (,vocab fuel-get-vocab-location) "fuel" t)))
78     (fuel-edit--try-edit (fuel-eval--send/wait cmd))))
79
80 (defun fuel-edit-word (&optional arg)
81   "Asks for a word to edit, with completion.
82 With prefix, only words visible in the current vocabulary are
83 offered."
84   (interactive "P")
85   (let* ((word (fuel-completion--read-word "Edit word: "
86                                            nil
87                                            fuel-edit--word-history
88                                            arg))
89          (cmd `(:fuel* ((:quote ,word) fuel-get-edit-location))))
90     (fuel-edit--try-edit (fuel-eval--send/wait cmd))))
91
92 (defun fuel-edit-word-at-point (&optional arg)
93   "Opens a new window visiting the definition of the word at point.
94 With prefix, asks for the word to edit."
95   (interactive "P")
96   (let* ((word (or (and (not arg) (fuel-syntax-symbol-at-point))
97                    (fuel-completion--read-word "Edit word: ")))
98          (cmd `(:fuel* ((:quote ,word) fuel-get-edit-location)))
99          (marker (and (not arg) (point-marker))))
100     (if (and (not arg) (fuel-edit--looking-at-vocab))
101         (fuel-edit-vocabulary nil word)
102       (fuel-edit--try-edit (fuel-eval--send/wait cmd)))
103     (when marker (ring-insert find-tag-marker-ring marker))))
104
105 (defun fuel-edit-word-doc-at-point (&optional arg word)
106   "Opens a new window visiting the documentation file for the word at point.
107 With prefix, asks for the word to edit."
108   (interactive "P")
109   (let* ((word (or word
110                    (and (not arg) (fuel-syntax-symbol-at-point))
111                    (fuel-completion--read-word "Edit word: ")))
112          (cmd `(:fuel* ((:quote ,word) fuel-get-doc-location)))
113          (marker (and (not arg) (point-marker))))
114     (condition-case nil
115         (fuel-edit--try-edit (fuel-eval--send/wait cmd))
116       (error
117        (message "Documentation for '%s' not found" word)
118        (when (and (eq major-mode 'factor-mode)
119                   (y-or-n-p (concat "No documentation found. "
120                                     "Do you want to open the vocab's "
121                                     "doc file? ")))
122          (when marker (ring-insert find-tag-marker-ring marker))
123          (find-file-other-window
124           (format "%s-docs.factor"
125                   (file-name-sans-extension (buffer-file-name)))))))))
126
127 (defun fuel-edit-pop-edit-word-stack ()
128   "Pop back to where \\[fuel-edit-word-at-point] or \\[fuel-edit-word-doc-at-point]
129 was last invoked."
130   (interactive)
131   (condition-case nil
132       (pop-tag-mark)
133     (error "No previous location for find word or vocab invokation")))
134
135 \f
136 (provide 'fuel-edit)
137 ;;; fuel-edit.el ends here