1 ;;; fuel-edit.el -- utilities for file editing -*- lexical-binding: t -*-
3 ;; Copyright (C) 2009 Jose Antonio Ortega Ruiz
4 ;; See https://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)
27 (defcustom fuel-edit-word-method nil
28 "How the new buffer is opened when invoking `fuel-edit-word-at-point'."
30 :type '(choice (const :tag "Other window" window)
31 (const :tag "Other frame" frame)
32 (const :tag "Current window" nil)))
35 ;;; Auxiliar functions:
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))))
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)))))
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))))
60 (defvar fuel-edit--word-history nil)
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."
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))))
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."
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))))
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."
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))))
96 (fuel-edit--try-edit (fuel-eval--send/wait cmd))
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 "
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)))))))))
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]
114 (error "No previous location for find word or vocab invocation")))
116 (defvar fuel-edit--buffer-history nil)
118 (defun fuel-switch-to-buffer (&optional method)
119 "Switch to any of the existing Factor buffers, with completion."
121 (let ((buffer (completing-read "Factor buffer: "
122 (remove (buffer-name)
123 (mapcar 'buffer-name (buffer-list)))
124 #'(lambda (s) (string-match "\\.factor$" s))
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)))))
132 (defun fuel-switch-to-buffer-other-window ()
133 "Switch to any of the existing Factor buffers, in other window."
135 (fuel-switch-to-buffer 'window))
137 (defun fuel-switch-to-buffer-other-frame ()
138 "Switch to any of the existing Factor buffers, in other frame."
140 (fuel-switch-to-buffer 'frame))
144 ;;; fuel-edit.el ends here