]> 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 (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--try-edit (ret)
37   (let* ((err (fuel-eval--retort-error ret))
38          (loc (fuel-eval--retort-result ret)))
39     (when (or err (not loc) (not (listp loc)) (not (stringp (car loc))))
40       (error "Couldn't find edit location"))
41     (unless (file-readable-p (car loc))
42       (error "Couldn't open '%s' for read" (car loc)))
43     (cond ((eq fuel-edit-word-method 'window) (find-file-other-window (car loc)))
44           ((eq fuel-edit-word-method 'frame) (find-file-other-frame (car loc)))
45           (t (find-file (car loc))))
46     (goto-line (if (numberp (cadr loc)) (cadr loc) 1))))
47
48 (defun fuel-edit--read-vocabulary-name (refresh)
49   (let* ((vocabs (fuel-completion--vocabs refresh))
50          (prompt "Vocabulary name: "))
51     (if vocabs
52         (completing-read prompt vocabs nil nil nil fuel-edit--vocab-history)
53       (read-string prompt nil fuel-edit--vocab-history))))
54
55 (defun fuel-edit--edit-article (name)
56   (let ((cmd `(:fuel* (,name fuel-get-article-location) "fuel" t)))
57     (fuel-edit--try-edit (fuel-eval--send/wait cmd))))
58
59 \f
60 ;;; Editing commands:
61
62 (defvar fuel-edit--word-history nil)
63 (defvar fuel-edit--vocab-history nil)
64 (defvar fuel-edit--previous-location nil)
65
66 (defun fuel-edit-vocabulary (&optional refresh vocab)
67   "Visits vocabulary file in Emacs.
68 When called interactively, asks for vocabulary with completion.
69 With prefix argument, refreshes cached vocabulary list."
70   (interactive "P")
71   (let* ((vocab (or vocab (fuel-edit--read-vocabulary-name refresh)))
72          (cmd `(:fuel* (,vocab fuel-get-vocab-location) "fuel" t)))
73     (fuel-edit--try-edit (fuel-eval--send/wait cmd))))
74
75 (defun fuel-edit-word (&optional arg)
76   "Asks for a word to edit, with completion.
77 With prefix, only words visible in the current vocabulary are
78 offered."
79   (interactive "P")
80   (let* ((word (fuel-completion--read-word "Edit word: "
81                                            nil
82                                            fuel-edit--word-history
83                                            arg))
84          (cmd `(:fuel* ((:quote ,word) fuel-get-edit-location))))
85     (fuel-edit--try-edit (fuel-eval--send/wait cmd))))
86
87 (defun fuel-edit-word-at-point (&optional arg)
88   "Opens a new window visiting the definition of the word at point.
89 With prefix, asks for the word to edit."
90   (interactive "P")
91   (let* ((word (or (and (not arg) (fuel-syntax-symbol-at-point))
92                    (fuel-completion--read-word "Edit word: ")))
93          (cmd `(:fuel* ((:quote ,word) fuel-get-edit-location)))
94          (marker (and (not arg) (point-marker))))
95     (condition-case nil
96         (fuel-edit--try-edit (fuel-eval--send/wait cmd))
97       (error (fuel-edit-vocabulary nil word)))
98     (when marker (ring-insert find-tag-marker-ring marker))))
99
100 (defun fuel-edit-word-doc-at-point (&optional arg word)
101   "Opens a new window visiting the documentation file for the word at point.
102 With prefix, asks for the word to edit."
103   (interactive "P")
104   (let* ((word (or word
105                    (and (not arg) (fuel-syntax-symbol-at-point))
106                    (fuel-completion--read-word "Edit word: ")))
107          (cmd `(:fuel* ((:quote ,word) fuel-get-doc-location)))
108          (marker (and (not arg) (point-marker))))
109     (condition-case nil
110         (fuel-edit--try-edit (fuel-eval--send/wait cmd))
111       (error
112        (message "Documentation for '%s' not found" word)
113        (when (and (eq major-mode 'factor-mode)
114                   (y-or-n-p (concat "No documentation found. "
115                                     "Do you want to open the vocab's "
116                                     "doc file? ")))
117          (when marker (ring-insert find-tag-marker-ring marker))
118          (find-file-other-window
119           (format "%s-docs.factor"
120                   (file-name-sans-extension (buffer-file-name)))))))))
121
122 (defun fuel-edit-pop-edit-word-stack ()
123   "Pop back to where \\[fuel-edit-word-at-point] or \\[fuel-edit-word-doc-at-point]
124 was last invoked."
125   (interactive)
126   (condition-case nil
127       (pop-tag-mark)
128     (error "No previous location for find word or vocab invokation")))
129
130 \f
131 (provide 'fuel-edit)
132 ;;; fuel-edit.el ends here