1 ;;; fuel-edit.el -- utilities for file editing
3 ;; Copyright (C) 2009 Jose Antonio Ortega Ruiz
4 ;; See http://factorcode.org/license.txt for BSD license.
6 ;; Author: Jose Antonio Ortega Ruiz <jao@gnu.org>
7 ;; Keywords: languages, fuel, factor
8 ;; Start date: Mon Jan 05, 2009 21:16
12 ;; Locating and opening factor source and documentation files.
16 (require 'fuel-completion)
19 (require 'factor-mode)
26 (defcustom fuel-edit-word-method nil
27 "How the new buffer is opened when invoking `fuel-edit-word-at-point'."
29 :type '(choice (const :tag "Other window" window)
30 (const :tag "Other frame" frame)
31 (const :tag "Current window" nil)))
34 ;;; Auxiliar functions:
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))))
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)))))
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))))
59 (defvar fuel-edit--word-history nil)
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."
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))))
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
77 (let* ((word (fuel-completion--read-word "Edit word: "
79 fuel-edit--word-history
81 (cmd `(:fuel* ((:quote ,word) fuel-get-word-location))))
82 (fuel-edit--try-edit (fuel-eval--send/wait cmd))))
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."
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))))
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."
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))))
107 (fuel-edit--try-edit (fuel-eval--send/wait cmd))
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 "
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)))))))))
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]
125 (error "No previous location for find word or vocab invocation")))
127 (defvar fuel-edit--buffer-history nil)
129 (defun fuel-switch-to-buffer (&optional method)
130 "Switch to any of the existing Factor buffers, with completion."
132 (let ((buffer (completing-read "Factor buffer: "
133 (remove (buffer-name)
134 (mapcar 'buffer-name (buffer-list)))
135 #'(lambda (s) (string-match "\\.factor$" s))
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)))))
143 (defun fuel-switch-to-buffer-other-window ()
144 "Switch to any of the existing Factor buffers, in other window."
146 (fuel-switch-to-buffer 'window))
148 (defun fuel-switch-to-buffer-other-frame ()
149 "Switch to any of the existing Factor buffers, in other frame."
151 (fuel-switch-to-buffer 'frame))
155 ;;; fuel-edit.el ends here