]> gitweb.factorcode.org Git - factor.git/blob - misc/fuel/fuel-edit.el
Do not use deprecated marker api
[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 (require 'xref)
21
22 (require 'etags)
23
24 \f
25 ;;; Customization
26
27 (defcustom fuel-edit-word-method nil
28   "How the new buffer is opened when invoking `fuel-edit-word-at-point'."
29   :group 'fuel
30   :type '(choice (const :tag "Other window" window)
31                  (const :tag "Other frame" frame)
32                  (const :tag "Current window" nil)))
33
34 \f
35 ;;; Auxiliar functions:
36
37 (defun fuel-edit--visit-file (file method)
38   (cond ((eq method 'window) (find-file-other-window file))
39         ((eq method 'frame) (find-file-other-frame file))
40         (t (find-file file))))
41
42 (defun fuel-edit--try-edit (ret)
43   (let* ((err (fuel-eval--retort-error ret))
44          (loc (fuel-eval--retort-result ret)))
45     (when (or err (not loc) (not (listp loc)) (not (stringp (car loc))))
46       (error "Couldn't find edit location"))
47     (unless (file-readable-p (car loc))
48       (error "Couldn't open '%s' for read" (car loc)))
49     (fuel-edit--visit-file (car loc) fuel-edit-word-method)
50     (goto-char (point-min))
51     (forward-line (1- (if (numberp (cadr loc)) (cadr loc) 1)))))
52
53 (defun fuel-edit--edit-article (name)
54   (let ((cmd `(:fuel* (,name fuel-get-article-location) "fuel" t)))
55     (fuel-edit--try-edit (fuel-eval--send/wait cmd))))
56
57 \f
58 ;;; Editing commands:
59
60 (defvar fuel-edit--word-history nil)
61
62 ;;;###autoload
63 (defun fuel-edit-vocabulary (&optional refresh vocab)
64   "Visits vocabulary file in Emacs.
65 When called interactively, asks for vocabulary with completion.
66 With prefix argument, refreshes cached vocabulary list."
67   (interactive "P")
68   (let* ((vocab (or vocab (fuel-completion--read-vocab refresh)))
69          (cmd `(:fuel* (,vocab fuel-get-vocab-location) "fuel" t)))
70     (fuel-edit--try-edit (fuel-eval--send/wait cmd))))
71
72 ;;;###autoload
73 (defun fuel-edit-word-at-point (&optional arg)
74   "Opens a new window visiting the definition of the word at point.
75 With prefix, asks for the word to edit."
76   (interactive "P")
77   (let* ((word (or (and (not arg) (factor-symbol-at-point))
78                    (fuel-completion--read-word "Edit word: ")))
79          (cmd `(:fuel* ((:quote ,word) fuel-get-word-location)))
80          (marker (and (not arg) (point-marker))))
81     (if (and (not arg) (factor-on-vocab))
82         (fuel-edit-vocabulary nil word)
83       (fuel-edit--try-edit (fuel-eval--send/wait cmd)))
84     (when marker (xref-push-marker-stack marker))))
85
86 (defun fuel-edit-word-doc-at-point (&optional arg word)
87   "Opens a new window visiting the documentation file for the word at point.
88 With prefix, asks for the word to edit."
89   (interactive "P")
90   (let* ((word (or word
91                    (and (not arg) (factor-symbol-at-point))
92                    (fuel-completion--read-word "Edit word: ")))
93          (cmd `(:fuel* ((:quote ,word) fuel-get-doc-location)))
94          (marker (and (not arg) (point-marker))))
95     (condition-case nil
96         (fuel-edit--try-edit (fuel-eval--send/wait cmd))
97       (error
98        (message "Documentation for '%s' not found" word)
99        (when (and (eq major-mode 'factor-mode)
100                   (y-or-n-p (concat "No documentation found. "
101                                     "Do you want to open the vocab's "
102                                     "doc file? ")))
103          (when marker (xref-push-marker-stack marker))
104          (find-file-other-window
105           (format "%s-docs.factor"
106                   (file-name-sans-extension (buffer-file-name)))))))))
107
108 (defun fuel-edit-pop-edit-word-stack ()
109   "Pop back to where \\[fuel-edit-word-at-point] or \\[fuel-edit-word-doc-at-point]
110 was last invoked."
111   (interactive)
112   (condition-case nil
113       (pop-tag-mark)
114     (error "No previous location for find word or vocab invocation")))
115
116 (defvar fuel-edit--buffer-history nil)
117
118 (defun fuel-switch-to-buffer (&optional method)
119   "Switch to any of the existing Factor buffers, with completion."
120   (interactive)
121   (let ((buffer (completing-read "Factor buffer: "
122                                  (remove (buffer-name)
123                                          (mapcar 'buffer-name (buffer-list)))
124                                  #'(lambda (s) (string-match "\\.factor$" s))
125                                  t
126                                  nil
127                                  fuel-edit--buffer-history)))
128     (cond ((eq method 'window) (switch-to-buffer-other-window buffer))
129           ((eq method 'frame) (switch-to-buffer-other-frame buffer))
130           (t (switch-to-buffer buffer)))))
131
132 (defun fuel-switch-to-buffer-other-window ()
133   "Switch to any of the existing Factor buffers, in other window."
134   (interactive)
135   (fuel-switch-to-buffer 'window))
136
137 (defun fuel-switch-to-buffer-other-frame ()
138   "Switch to any of the existing Factor buffers, in other frame."
139   (interactive)
140   (fuel-switch-to-buffer 'frame))
141
142 \f
143 (provide 'fuel-edit)
144 ;;; fuel-edit.el ends here