]> 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 \f
21 ;;; Auxiliar functions:
22
23 (defun fuel-edit--try-edit (ret)
24   (let* ((err (fuel-eval--retort-error ret))
25          (loc (fuel-eval--retort-result ret)))
26     (when (or err (not loc) (not (listp loc)) (not (stringp (car loc))))
27       (error "Couldn't find edit location"))
28     (unless (file-readable-p (car loc))
29       (error "Couldn't open '%s' for read" (car loc)))
30     (find-file-other-window (car loc))
31     (goto-line (if (numberp (cadr loc)) (cadr loc) 1))))
32
33 (defun fuel-edit--read-vocabulary-name (refresh)
34   (let* ((vocabs (fuel-completion--vocabs refresh))
35          (prompt "Vocabulary name: "))
36     (if vocabs
37         (completing-read prompt vocabs nil nil nil fuel-edit--vocab-history)
38       (read-string prompt nil fuel-edit--vocab-history))))
39
40 (defun fuel-edit--edit-article (name)
41   (let ((cmd `(:fuel* (,name fuel-get-article-location) "fuel" t)))
42     (fuel-edit--try-edit (fuel-eval--send/wait cmd))))
43
44 \f
45 ;;; Editing commands:
46
47 (defvar fuel-edit--word-history nil)
48 (defvar fuel-edit--vocab-history nil)
49
50 (defun fuel-edit-vocabulary (&optional refresh vocab)
51   "Visits vocabulary file in Emacs.
52 When called interactively, asks for vocabulary with completion.
53 With prefix argument, refreshes cached vocabulary list."
54   (interactive "P")
55   (let* ((vocab (or vocab (fuel-edit--read-vocabulary-name refresh)))
56          (cmd `(:fuel* (,vocab fuel-get-vocab-location) "fuel" t)))
57     (fuel-edit--try-edit (fuel-eval--send/wait cmd))))
58
59 (defun fuel-edit-word (&optional arg)
60   "Asks for a word to edit, with completion.
61 With prefix, only words visible in the current vocabulary are
62 offered."
63   (interactive "P")
64   (let* ((word (fuel-completion--read-word "Edit word: "
65                                            nil
66                                            fuel-edit--word-history
67                                            arg))
68          (cmd `(:fuel* ((:quote ,word) fuel-get-edit-location))))
69     (fuel-edit--try-edit (fuel-eval--send/wait cmd))))
70
71 (defun fuel-edit-word-at-point (&optional arg)
72   "Opens a new window visiting the definition of the word at point.
73 With prefix, asks for the word to edit."
74   (interactive "P")
75   (let* ((word (or (and (not arg) (fuel-syntax-symbol-at-point))
76                    (fuel-completion--read-word "Edit word: ")))
77          (cmd `(:fuel* ((:quote ,word) fuel-get-edit-location))))
78     (condition-case nil
79         (fuel-edit--try-edit (fuel-eval--send/wait cmd))
80       (error (fuel-edit-vocabulary nil word)))))
81
82 (defun fuel-edit-word-doc-at-point (&optional arg word)
83   "Opens a new window visiting the documentation file for the word at point.
84 With prefix, asks for the word to edit."
85   (interactive "P")
86   (let* ((word (or word
87                    (and (not arg) (fuel-syntax-symbol-at-point))
88                    (fuel-completion--read-word "Edit word: ")))
89          (cmd `(:fuel* ((:quote ,word) fuel-get-doc-location))))
90     (condition-case nil
91         (fuel-edit--try-edit (fuel-eval--send/wait cmd))
92       (error
93        (message "Documentation for '%s' not found" word)
94        (when (and (eq major-mode 'factor-mode)
95                   (y-or-n-p (concat "No documentation found. "
96                                     "Do you want to open the vocab's "
97                                     "doc file? ")))
98          (find-file-other-window
99           (format "%s-docs.factor"
100                   (file-name-sans-extension (buffer-file-name)))))))))
101
102 \f
103 (provide 'fuel-edit)
104 ;;; fuel-edit.el ends here