]> gitweb.factorcode.org Git - factor.git/blob - misc/fuel/fuel-edit.el
Use lexical scoping in all fuel sources
[factor.git] / misc / fuel / fuel-edit.el
1 ;;; fuel-edit.el -- utilities for file editing -*- lexical-binding: t -*-
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-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."
75   (interactive "P")
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))))
84
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."
88   (interactive "P")
89   (let* ((word (or word
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))))
94     (condition-case nil
95         (fuel-edit--try-edit (fuel-eval--send/wait cmd))
96       (error
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 "
101                                     "doc file? ")))
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)))))))))
106
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]
109 was last invoked."
110   (interactive)
111   (condition-case nil
112       (pop-tag-mark)
113     (error "No previous location for find word or vocab invocation")))
114
115 (defvar fuel-edit--buffer-history nil)
116
117 (defun fuel-switch-to-buffer (&optional method)
118   "Switch to any of the existing Factor buffers, with completion."
119   (interactive)
120   (let ((buffer (completing-read "Factor buffer: "
121                                  (remove (buffer-name)
122                                          (mapcar 'buffer-name (buffer-list)))
123                                  #'(lambda (s) (string-match "\\.factor$" s))
124                                  t
125                                  nil
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)))))
130
131 (defun fuel-switch-to-buffer-other-window ()
132   "Switch to any of the existing Factor buffers, in other window."
133   (interactive)
134   (fuel-switch-to-buffer 'window))
135
136 (defun fuel-switch-to-buffer-other-frame ()
137   "Switch to any of the existing Factor buffers, in other frame."
138   (interactive)
139   (fuel-switch-to-buffer 'frame))
140
141 \f
142 (provide 'fuel-edit)
143 ;;; fuel-edit.el ends here