Debian Codegraph @

lib/text/external-editor.scm

raw
1 ;;;
2 ;;; text/external-editor.scm - invoke external editor
3 ;;;
4 ;;; Copyright (c) 2015-2024 Shiro Kawai <[email protected]>
5 ;;;
6 ;;; Redistribution and use in source and binary forms, with or without
7 ;;; modification, are permitted provided that the following conditions
8 ;;; are met:
9 ;;;
10 ;;; 1. Redistributions of source code must retain the above copyright
11 ;;; notice, this list of conditions and the following disclaimer.
12 ;;;
13 ;;; 2. Redistributions in binary form must reproduce the above copyright
14 ;;; notice, this list of conditions and the following disclaimer in the
15 ;;; documentation and/or other materials provided with the distribution.
16 ;;;
17 ;;; 3. Neither the name of the authors nor the names of its contributors
18 ;;; may be used to endorse or promote products derived from this
19 ;;; software without specific prior written permission.
20 ;;;
21 ;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 ;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 ;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 ;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 ;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 ;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 ;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 ;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 ;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 ;;;
33
34 ;; Used to be gauche.interactive.ed.
35
36 (define-module text.external-editor
37 (use gauche.process)
38 (use file.util)
39 (export ed ed-string ed-pick-file))
40 (select-module text.external-editor)
41
42 ;; To remember editor name user typed in
43 (define *user-entered-editor* #f)
44
45 ;; API
46 ;; (ed path-or-proc :key editor load-after)
47 ;; Starts an external editor. The editor program path is determined
48 ;; in the following order:
49 ;;
50 ;; - The :editor keyword argument, if it's a string.
51 ;; - The value of *editor* in the user module, if defined.
52 ;; - The value of the environment variable GAUCHE_EDITOR
53 ;; - The value of the environment variable EDITOR
54 ;; - If none works, the behavior depends on the value of :editor keyword
55 ;; argument:
56 ;; #f - just returns #f.
57 ;; error - throws an error
58 ;; ask - ask the user
59 ;; message - print message and return #f
60 ;;
61 ;; Returns #t for normal termination.
62 ;;
63 ;; The editor may be called in one of the following way:
64 ;;
65 ;; EDITOR filename
66 ;; EDITOR +lineno filename
67 ;;
68 ;; The latter expects to locate the cursor on the specified line in the
69 ;; filename.
70 ;;
71 ;; The file to open is determined by a generic function ed-pick-file.
72 ;; It should return (<filename> <lineno>), or #f to indicate that it
73 ;; couldn't determine the file to edit.
74 ;;
75 ;; NB: Common Lisp's ed can be invoked without argument. For our usage,
76 ;; though, that feature doesn't seem too useful---it's more likely that
77 ;; repl IS inside an editor so we only need to open a specific file in
78 ;; a buffer. Emacsclient requires filename, so it further complicates things.
79 ;; For now we just require one argument.
80
81 (define (ed path-or-proc :key (editor 'ask) (load-after 'ask))
82 (if-let1 target (ed-pick-file path-or-proc)
83 (let* ([filename (car target)]
84 [lineno (cadr target)]
85 [sig0 (file-signature filename)])
86 (and-let1 e (pick-editor editor)
87 (begin (invoke-editor e (car target) (cadr target))
88 (when (and (not (equal? (file-signature filename) sig0))
89 (file-exists? filename)
90 (load-after? load-after filename))
91 (load (car target)))
92 #t)))
93 (error "Don't know how to edit:" path-or-proc)))
94
95 ;; Returns (<size> <mtime>) of a named file, to check if the file is updated
96 ;; by the editor. Returns #f if filename doesn't exit.
97 (define (file-signature filename)
98 (and (file-exists? filename)
99 (list (file-size filename :follow-link? #t)
100 (file-mtime filename :follow-link? #t))))
101
102 ;; Customize how to find a file to edit. The method must return
103 ;; a list (<filename> <line-numer>).
104 (define-generic ed-pick-file)
105 (define-method ed-pick-file ((fn <string>)) (list fn 1))
106 (define-method ed-pick-file ((fn <procedure>))
107 (let1 loc (source-location fn)
108 ;; If the recorded file does not exist, it is either (1) removed
109 ;; after loaded, (2) cwd has been changed, or (3) it's a pseudo filename
110 ;; such as "(standard input)". In either case, opening such file isn't
111 ;; much useful since that won't contain the definition. So we ignore it.
112 (and (file-exists? (car loc)) loc)))
113 (define-method ed-pick-file ((fn <top>)) #f)
114
115 ;; API
116 ;; (ed-string string :key (editor #f)
117 ;; Allow user to edit STRING with the editor. Returns result string.
118 ;; Need to create a temporary file
119 (define (ed-string string :key (editor #f))
120 (call-with-temporary-file
121 (^[port name]
122 (and-let1 e (pick-editor editor)
123 (begin
124 (display string port)
125 (close-output-port port)
126 (invoke-editor e name 1)
127 (file->string name))))))
128
129 ;; internal
130 ;; NB: If specified editor isn't actually name an executable, we let
131 ;; run-process to throw an error rather than doing some clever things.
132 (define (pick-editor editor)
133 (or (and (string? editor) editor)
134 (module-binding-ref 'user '*editor* #f)
135 (sys-getenv "GAUCHE_EDITOR")
136 (sys-getenv "EDITOR")
137 (case editor
138 [(#f) #f] ;just give up
139 [(error) (error "Don't know which editor to use.")]
140 [(message) (print "Don't know which editor to use.") #f]
141 [(ask)
142 (if *user-entered-editor*
143 (format #t "Editor name [~a]: " *user-entered-editor*)
144 (format #t "Editor name (or just return to abort): "))
145 (flush)
146 (let1 ans (read-line)
147 (if (#/^\s*$/ ans)
148 *user-entered-editor*
149 (begin (set! *user-entered-editor* ans) ans)))]
150 [else (error "Editor argument must be either one of #f, error, \
151 message, or ask, but got" editor)])))
152
153 ;; internal
154 ;; Determine whether we should reload the file if it's updated.
155 (define (load-after? load-after name)
156 (ecase load-after
157 [(#t) #t]
158 [(#f) #f]
159 [(ask)
160 (let loop ()
161 (format #t "Reload ~s? [y/N]: " name)
162 (flush)
163 (rxmatch-case (read-line)
164 [#/^y/i () #t]
165 [#/^n/i () #f]
166 [else (loop)]))]))
167
168 ;; internal
169 ;; Invoke external editor; won't return until the editor exits.
170 (define (invoke-editor editor filename lineno)
171 (run-process `(,editor ,#"+~lineno" ,filename) :wait #t))