/[climacs]/climacs/file-commands.lisp
ViewVC logotype

Contents of /climacs/file-commands.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (show annotations)
Sat Nov 12 09:38:32 2005 UTC (8 years, 5 months ago) by dmurray
Branch: MAIN
The new files. developer-commands.lisp contains commands used
in developing climacs. file-commands, search-commands,
unicode-commands and window-commands.lisp contain what you would
expect. misc-commands contains everything else, except that stuff
kept in gui.lisp, which is the gui stuff (no, really), some low-level
stuff, and the buffer-handling.
1 ;;; -*- Mode: Lisp; Package: CLIMACS-GUI -*-
2
3 ;;; (c) copyright 2004-2005 by
4 ;;; Robert Strandh (strandh@labri.fr)
5 ;;; (c) copyright 2004-2005 by
6 ;;; Elliott Johnson (ejohnson@fasl.info)
7 ;;; (c) copyright 2005 by
8 ;;; Matthieu Villeneuve (matthieu.villeneuve@free.fr)
9 ;;; (c) copyright 2005 by
10 ;;; Aleksandar Bakic (a_bakic@yahoo.com)
11
12 ;;; This library is free software; you can redistribute it and/or
13 ;;; modify it under the terms of the GNU Library General Public
14 ;;; License as published by the Free Software Foundation; either
15 ;;; version 2 of the License, or (at your option) any later version.
16 ;;;
17 ;;; This library is distributed in the hope that it will be useful,
18 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 ;;; Library General Public License for more details.
21 ;;;
22 ;;; You should have received a copy of the GNU Library General Public
23 ;;; License along with this library; if not, write to the
24 ;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;;; Boston, MA 02111-1307 USA.
26
27 ;;; File commands for the Climacs editor.
28
29 (in-package :climacs-gui)
30
31 (defun filename-completer (so-far mode)
32 (flet ((remove-trail (s)
33 (subseq s 0 (let ((pos (position #\/ s :from-end t)))
34 (if pos (1+ pos) 0)))))
35 (let* ((directory-prefix
36 (if (and (plusp (length so-far)) (eql (aref so-far 0) #\/))
37 ""
38 (namestring #+sbcl *default-pathname-defaults*
39 #+cmu (ext:default-directory)
40 #-(or sbcl cmu) *default-pathname-defaults*)))
41 (full-so-far (concatenate 'string directory-prefix so-far))
42 (pathnames
43 (loop with length = (length full-so-far)
44 and wildcard = (concatenate 'string (remove-trail so-far) "*.*")
45 for path in
46 #+(or sbcl cmu lispworks) (directory wildcard)
47 #+openmcl (directory wildcard :directories t)
48 #+allegro (directory wildcard :directories-are-files nil)
49 #+cormanlisp (nconc (directory wildcard)
50 (cl::directory-subdirs dirname))
51 #-(or sbcl cmu lispworks openmcl allegro cormanlisp)
52 (directory wildcard)
53 when (let ((mismatch (mismatch (namestring path) full-so-far)))
54 (or (null mismatch) (= mismatch length)))
55 collect path))
56 (strings (mapcar #'namestring pathnames))
57 (first-string (car strings))
58 (length-common-prefix nil)
59 (completed-string nil)
60 (full-completed-string nil))
61 (unless (null pathnames)
62 (setf length-common-prefix
63 (loop with length = (length first-string)
64 for string in (cdr strings)
65 do (setf length (min length (or (mismatch string first-string) length)))
66 finally (return length))))
67 (unless (null pathnames)
68 (setf completed-string
69 (subseq first-string (length directory-prefix)
70 (if (null (cdr pathnames)) nil length-common-prefix)))
71 (setf full-completed-string
72 (concatenate 'string directory-prefix completed-string)))
73 (case mode
74 ((:complete-limited :complete-maximal)
75 (cond ((null pathnames)
76 (values so-far nil nil 0 nil))
77 ((null (cdr pathnames))
78 (values completed-string t (car pathnames) 1 nil))
79 (t
80 (values completed-string nil nil (length pathnames) nil))))
81 (:complete
82 (cond ((null pathnames)
83 (values so-far t so-far 1 nil))
84 ((null (cdr pathnames))
85 (values completed-string t (car pathnames) 1 nil))
86 ((find full-completed-string strings :test #'string-equal)
87 (let ((pos (position full-completed-string strings :test #'string-equal)))
88 (values completed-string
89 t (elt pathnames pos) (length pathnames) nil)))
90 (t
91 (values completed-string nil nil (length pathnames) nil))))
92 (:possibilities
93 (values nil nil nil (length pathnames)
94 (loop with length = (length directory-prefix)
95 for name in pathnames
96 collect (list (subseq (namestring name) length nil)
97 name))))))))
98
99 (define-presentation-method present (object (type pathname)
100 stream (view climacs-textual-view) &key)
101 (princ (namestring object) stream))
102
103 (define-presentation-method accept ((type pathname) stream (view climacs-textual-view)
104 &key (default nil defaultp) (default-type type))
105 (multiple-value-bind (pathname success string)
106 (complete-input stream
107 #'filename-completer
108 :allow-any-input t)
109 (cond (success
110 (values pathname type))
111 ((and (zerop (length string))
112 defaultp)
113 (values default default-type))
114 (t (values string 'string)))))
115
116 (defun filepath-filename (pathname)
117 (if (null (pathname-type pathname))
118 (pathname-name pathname)
119 (concatenate 'string (pathname-name pathname)
120 "." (pathname-type pathname))))
121
122 (defun syntax-class-name-for-filepath (filepath)
123 (or (climacs-syntax::syntax-description-class-name
124 (find (or (pathname-type filepath)
125 (pathname-name filepath))
126 climacs-syntax::*syntaxes*
127 :test (lambda (x y)
128 (member x y :test #'string-equal))
129 :key #'climacs-syntax::syntax-description-pathname-types))
130 'basic-syntax))
131
132 ;; Adapted from cl-fad/PCL
133 (defun directory-pathname-p (pathspec)
134 "Returns NIL if PATHSPEC does not designate a directory."
135 (let ((name (pathname-name pathspec))
136 (type (pathname-type pathspec)))
137 (and (or (null name) (eql name :unspecific))
138 (or (null type) (eql type :unspecific)))))
139
140 (defun find-file (filepath)
141 (cond ((null filepath)
142 (display-message "No file name given.")
143 (beep))
144 ((directory-pathname-p filepath)
145 (display-message "~A is a directory name." filepath)
146 (beep))
147 (t
148 (let ((existing-buffer (find filepath (buffers *application-frame*)
149 :key #'filepath :test #'equal)))
150 (if existing-buffer
151 (switch-to-buffer existing-buffer)
152 (let ((buffer (make-buffer))
153 (pane (current-window)))
154 (setf (offset (point (buffer pane))) (offset (point pane)))
155 (setf (buffer (current-window)) buffer)
156 (setf (syntax buffer)
157 (make-instance (syntax-class-name-for-filepath filepath)
158 :buffer (buffer (point pane))))
159 ;; Don't want to create the file if it doesn't exist.
160 (when (probe-file filepath)
161 (with-open-file (stream filepath :direction :input)
162 (input-from-stream stream buffer 0)))
163 (setf (filepath buffer) filepath
164 (name buffer) (filepath-filename filepath)
165 (needs-saving buffer) nil)
166 (beginning-of-buffer (point pane))
167 ;; this one is needed so that the buffer modification protocol
168 ;; resets the low and high marks after redisplay
169 (redisplay-frame-panes *application-frame*)
170 buffer))))))
171
172 (define-command (com-find-file :name t :command-table buffer-table) ()
173 (let* ((filepath (accept 'pathname :prompt "Find File")))
174 (find-file filepath)))
175
176 (set-key 'com-find-file
177 'buffer-table
178 '((#\x :control) (#\f :control)))
179
180 (defun find-file-read-only (filepath)
181 (cond ((null filepath)
182 (display-message "No file name given.")
183 (beep))
184 ((directory-pathname-p filepath)
185 (display-message "~A is a directory name." filepath)
186 (beep))
187 (t
188 (let ((existing-buffer (find filepath (buffers *application-frame*)
189 :key #'filepath :test #'equal)))
190 (if (and existing-buffer (read-only-p existing-buffer))
191 (switch-to-buffer existing-buffer)
192 (if (probe-file filepath)
193 (let ((buffer (make-buffer))
194 (pane (current-window)))
195 (setf (offset (point (buffer pane))) (offset (point pane)))
196 (setf (buffer (current-window)) buffer)
197 (setf (syntax buffer)
198 (make-instance (syntax-class-name-for-filepath filepath)
199 :buffer (buffer (point pane))))
200 (with-open-file (stream filepath :direction :input)
201 (input-from-stream stream buffer 0))
202 (setf (filepath buffer) filepath
203 (name buffer) (filepath-filename filepath)
204 (needs-saving buffer) nil
205 (read-only-p buffer) t)
206 (beginning-of-buffer (point pane))
207 ;; this one is needed so that the buffer modification protocol
208 ;; resets the low and high marks after redisplay
209 (redisplay-frame-panes *application-frame*)
210 buffer)
211 (progn
212 (display-message "No such file: ~A" filepath)
213 (beep)
214 nil)))))))
215
216 (define-command (com-find-file-read-only :name t :command-table buffer-table) ()
217 (let ((filepath (accept 'pathname :Prompt "Find file read only")))
218 (find-file-read-only filepath)))
219
220 (set-key 'com-find-file-read-only
221 'buffer-table
222 '((#\x :control) (#\r :control)))
223
224 (define-command (com-read-only :name t :command-table buffer-table) ()
225 (let ((buffer (buffer (current-window))))
226 (setf (read-only-p buffer) (not (read-only-p buffer)))))
227
228 (set-key 'com-read-only
229 'buffer-table
230 '((#\x :control) (#\q :control)))
231
232 (defun set-visited-file-name (filename buffer)
233 (setf (filepath buffer) filename
234 (name buffer) (filepath-filename filename)
235 (needs-saving buffer) t))
236
237 (define-command (com-set-visited-file-name :name t :command-table buffer-table) ()
238 (let ((filename (accept 'pathname :prompt "New file name")))
239 (set-visited-file-name filename (buffer (current-window)))))
240
241 (define-command (com-insert-file :name t :command-table buffer-table) ()
242 (let ((filename (accept 'pathname :prompt "Insert File"))
243 (pane (current-window)))
244 (when (probe-file filename)
245 (setf (mark pane) (clone-mark (point pane) :left))
246 (with-open-file (stream filename :direction :input)
247 (input-from-stream stream
248 (buffer pane)
249 (offset (point pane))))
250 (psetf (offset (mark pane)) (offset (point pane))
251 (offset (point pane)) (offset (mark pane))))
252 (redisplay-frame-panes *application-frame*)))
253
254 (set-key 'com-insert-file
255 'buffer-table
256 '((#\x :control) (#\i :control)))
257
258 (define-command (com-revert-buffer :name t :command-table buffer-table) ()
259 (let* ((pane (current-window))
260 (buffer (buffer pane))
261 (filepath (filepath buffer))
262 (save (offset (point pane))))
263 (when (accept 'boolean :prompt (format nil "Revert buffer from file ~A?"
264 (filepath buffer)))
265 (cond ((directory-pathname-p filepath)
266 (display-message "~A is a directory name." filepath)
267 (beep))
268 ((probe-file filepath)
269 (erase-buffer buffer)
270 (with-open-file (stream filepath :direction :input)
271 (input-from-stream stream buffer 0))
272 (setf (offset (point pane))
273 (min (size buffer) save)))
274 (t
275 (display-message "No file ~A" filepath)
276 (beep))))))
277
278 (defun save-buffer (buffer)
279 (let ((filepath (or (filepath buffer)
280 (accept 'pathname :prompt "Save Buffer to File"))))
281 (cond
282 ((directory-pathname-p filepath)
283 (display-message "~A is a directory." filepath)
284 (beep))
285 (t
286 (when (probe-file filepath)
287 (let ((backup-name (pathname-name filepath))
288 (backup-type (concatenate 'string (pathname-type filepath) "~")))
289 (rename-file filepath (make-pathname :name backup-name
290 :type backup-type))))
291 (with-open-file (stream filepath :direction :output :if-exists :supersede)
292 (output-to-stream stream buffer 0 (size buffer)))
293 (setf (filepath buffer) filepath
294 (name buffer) (filepath-filename filepath))
295 (display-message "Wrote: ~a" (filepath buffer))
296 (setf (needs-saving buffer) nil)))))
297
298 (define-command (com-save-buffer :name t :command-table buffer-table) ()
299 (let ((buffer (buffer (current-window))))
300 (if (or (null (filepath buffer))
301 (needs-saving buffer))
302 (save-buffer buffer)
303 (display-message "No changes need to be saved from ~a" (name buffer)))))
304
305 (set-key 'com-save-buffer
306 'buffer-table
307 '((#\x :control) (#\s :control)))
308
309 (defmethod frame-exit :around ((frame climacs))
310 (loop for buffer in (buffers frame)
311 when (and (needs-saving buffer)
312 (filepath buffer)
313 (handler-case (accept 'boolean
314 :prompt (format nil "Save buffer: ~a ?" (name buffer)))
315 (error () (progn (beep)
316 (display-message "Invalid answer")
317 (return-from frame-exit nil)))))
318 do (save-buffer buffer))
319 (when (or (notany #'(lambda (buffer) (and (needs-saving buffer) (filepath buffer)))
320 (buffers frame))
321 (handler-case (accept 'boolean :prompt "Modified buffers exist. Quit anyway?")
322 (error () (progn (beep)
323 (display-message "Invalid answer")
324 (return-from frame-exit nil)))))
325 (call-next-method)))
326
327 (define-command (com-write-buffer :name t :command-table buffer-table) ()
328 (let ((filepath (accept 'pathname :prompt "Write Buffer to File"))
329 (buffer (buffer (current-window))))
330 (cond
331 ((directory-pathname-p filepath)
332 (display-message "~A is a directory name." filepath))
333 (t
334 (with-open-file (stream filepath :direction :output :if-exists :supersede)
335 (output-to-stream stream buffer 0 (size buffer)))
336 (setf (filepath buffer) filepath
337 (name buffer) (filepath-filename filepath)
338 (needs-saving buffer) nil)
339 (display-message "Wrote: ~a" (filepath buffer))))))
340
341 (set-key 'com-write-buffer
342 'buffer-table
343 '((#\x :control) (#\w :control)))
344

  ViewVC Help
Powered by ViewVC 1.1.5