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)
25 (defcustom fuel-edit-word-method nil
26 "How the new buffer is opened when invoking
27 \\[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--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))))
48 (defun fuel-edit--read-vocabulary-name (refresh)
49 (let* ((vocabs (fuel-completion--vocabs refresh))
50 (prompt "Vocabulary name: "))
52 (completing-read prompt vocabs nil nil nil fuel-edit--vocab-history)
53 (read-string prompt nil fuel-edit--vocab-history))))
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))))
62 (defvar fuel-edit--word-history nil)
63 (defvar fuel-edit--vocab-history nil)
64 (defvar fuel-edit--previous-location nil)
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."
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))))
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
80 (let* ((word (fuel-completion--read-word "Edit word: "
82 fuel-edit--word-history
84 (cmd `(:fuel* ((:quote ,word) fuel-get-edit-location))))
85 (fuel-edit--try-edit (fuel-eval--send/wait cmd))))
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."
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))))
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))))
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."
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))))
110 (fuel-edit--try-edit (fuel-eval--send/wait cmd))
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 "
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)))))))))
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]
128 (error "No previous location for find word or vocab invokation")))
132 ;;; fuel-edit.el ends here