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

Contents of /climacs/file-commands.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.19 - (show annotations)
Thu Jun 1 22:51:40 2006 UTC (7 years, 10 months ago) by thenriksen
Branch: MAIN
Changes since 1.18: +50 -42 lines
Use truenames (if available) when comparing pathnames in `find-file'.
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 (or pathname (parse-namestring string)) 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 (defun evaluate-attributes (buffer options)
133 "Evaluate the attributes `options' and modify `buffer' as
134 appropriate. `Options' should be an alist mapping option names
135 to their values."
136 ;; First, check whether we need to change the syntax (via the SYNTAX
137 ;; option). MODE is an alias for SYNTAX for compatibility with
138 ;; Emacs. If there is more than one option with one of these names,
139 ;; only the first will be acted upon.
140 (let ((specified-syntax
141 (syntax-from-name
142 (second (find-if #'(lambda (name)
143 (or (string-equal name "SYNTAX")
144 (string-equal name "MODE")))
145 options
146 :key #'first)))))
147 (when specified-syntax
148 (setf (syntax buffer)
149 (make-instance specified-syntax
150 :buffer buffer))))
151 ;; Now we iterate through the options (discarding SYNTAX and MODE
152 ;; options).
153 (loop for (name value) in options
154 unless (or (string-equal name "SYNTAX")
155 (string-equal name "MODE"))
156 do (eval-option (syntax buffer) name value)))
157
158 (defun split-attribute (string char)
159 (let (pairs)
160 (loop with start = 0
161 for ch across string
162 for i from 0
163 when (eql ch char)
164 do (push (string-trim '(#\Space #\Tab) (subseq string start i))
165 pairs)
166 (setf start (1+ i))
167 finally (unless (>= start i)
168 (push (string-trim '(#\Space #\Tab) (subseq string start))
169 pairs)))
170 (nreverse pairs)))
171
172 (defun split-attribute-line (line)
173 (mapcar (lambda (pair) (split-attribute pair #\:))
174 (split-attribute line #\;)))
175
176 (defun get-attribute-line (buffer)
177 (let ((scan (beginning-of-buffer (clone-mark (point buffer)))))
178 ;; skip the leading whitespace
179 (loop until (end-of-buffer-p scan)
180 until (not (whitespacep (object-after scan)))
181 do (forward-object scan))
182 ;; stop looking if we're already 1,000 objects into the buffer
183 (unless (> (offset scan) 1000)
184 (let ((start-found
185 (loop with newlines = 0
186 when (end-of-buffer-p scan)
187 do (return nil)
188 when (eql (object-after scan) #\Newline)
189 do (incf newlines)
190 when (> newlines 1)
191 do (return nil)
192 do (forward-object scan)
193 until (looking-at scan "-*-")
194 finally (return t))))
195 (when start-found
196 (let ((line (buffer-substring buffer
197 (offset scan)
198 (offset (end-of-line (clone-mark scan))))))
199 (when (>= (length line) 6)
200 (let ((end (search "-*-" line :from-end t :start2 3)))
201 (when end
202 (string-trim '(#\Space #\Tab) (subseq line 3 end)))))))))))
203
204 (defun evaluate-attributes-line (buffer)
205 (evaluate-attributes
206 buffer
207 (split-attribute-line (get-attribute-line buffer))))
208
209 (define-command (com-reparse-attribute-list :name t :command-table buffer-table) ()
210 "Reparse the current buffer's attribute list.
211 An attribute list is a line of keyword-value pairs, each keyword separated
212 from the corresponding value by a colon. If another keyword-value pair
213 follows, the value should be terminated by a colon. The attribute list
214 is surrounded by '-*-' sequences, but the opening '-*-' need not be at the
215 beginning of the line. Climacs looks for the attribute list
216 on the first or second non-blank line of the file.
217
218 An example attribute-list is:
219
220 ;; -*- Syntax: Lisp; Base: 10 -*- "
221 (evaluate-attributes-line (buffer (current-window))))
222
223 ;; Adapted from cl-fad/PCL
224 (defun directory-pathname-p (pathspec)
225 "Returns NIL if PATHSPEC does not designate a directory."
226 (let ((name (pathname-name pathspec))
227 (type (pathname-type pathspec)))
228 (and (or (null name) (eql name :unspecific))
229 (or (null type) (eql type :unspecific)))))
230
231 (defun find-file (filepath &optional readonlyp)
232 (cond ((null filepath)
233 (display-message "No file name given.")
234 (beep))
235 ((directory-pathname-p filepath)
236 (display-message "~A is a directory name." filepath)
237 (beep))
238 (t
239 (flet ((usable-pathname (pathname)
240 (if (probe-file pathname)
241 (truename pathname)
242 pathname)))
243 (let ((existing-buffer (find filepath (buffers *application-frame*)
244 :key #'filepath
245 :test #'(lambda (fp1 fp2)
246 (and fp1 fp2
247 (equal (usable-pathname fp1)
248 (usable-pathname fp2)))))))
249 (if (and existing-buffer (if readonlyp (read-only-p existing-buffer) t))
250 (switch-to-buffer existing-buffer)
251 (progn
252 (when readonlyp
253 (unless (probe-file filepath)
254 (beep)
255 (display-message "No such file: ~A" filepath)
256 (return-from find-file nil)))
257 (let ((buffer (make-buffer))
258 (pane (current-window)))
259 ;; Clear the pane's cache; otherwise residue from the
260 ;; previously displayed buffer may under certain
261 ;; circumstances be displayed.
262 (clear-cache pane)
263 (setf (syntax buffer) nil)
264 (setf (offset (point (buffer pane))) (offset (point pane)))
265 (setf (buffer (current-window)) buffer)
266 ;; Don't want to create the file if it doesn't exist.
267 (when (probe-file filepath)
268 (with-open-file (stream filepath :direction :input)
269 (input-from-stream stream buffer 0))
270 (setf (file-write-time buffer) (file-write-date filepath))
271 ;; A file! That means we may have a local options
272 ;; line to parse.
273 (evaluate-attributes-line buffer))
274 ;; If the local options line didn't set a syntax, do
275 ;; it now.
276 (when (null (syntax buffer))
277 (setf (syntax buffer)
278 (make-instance (syntax-class-name-for-filepath filepath)
279 :buffer buffer)))
280 (setf (filepath buffer) filepath
281 (name buffer) (filepath-filename filepath)
282 (needs-saving buffer) nil
283 (read-only-p buffer) readonlyp)
284 (beginning-of-buffer (point pane))
285 (update-syntax buffer (syntax buffer))
286 (clear-modify buffer)
287 buffer))))))))
288
289 (defun directory-of-buffer (buffer)
290 "Extract the directory part of the filepath to the file in BUFFER.
291 If BUFFER does not have a filepath, the path to the user's home
292 directory will be returned."
293 (make-pathname
294 :directory
295 (pathname-directory
296 (or (filepath buffer)
297 (user-homedir-pathname)))))
298
299 (define-command (com-find-file :name t :command-table buffer-table)
300 ((filepath 'pathname
301 :prompt "Find File"
302 :default (directory-of-buffer (buffer (current-window)))
303 :default-type 'pathname
304 :insert-default t))
305 "Prompt for a filename then edit that file.
306 If a buffer is already visiting that file, switch to that buffer. Does not create a file if the filename given does not name an existing file."
307 (find-file filepath))
308
309 (set-key `(com-find-file ,*unsupplied-argument-marker*)
310 'buffer-table
311 '((#\x :control) (#\f :control)))
312
313 (define-command (com-find-file-read-only :name t :command-table buffer-table)
314 ((filepath 'pathname :Prompt "Find file read only"
315 :default (directory-of-buffer (buffer (current-window)))
316 :default-type 'pathname
317 :insert-default t))
318 "Prompt for a filename then open that file readonly.
319 If a buffer is already visiting that file, switch to that buffer. If the filename given does not name an existing file, signal an error."
320 (find-file filepath t))
321
322 (set-key `(com-find-file-read-only ,*unsupplied-argument-marker*)
323 'buffer-table
324 '((#\x :control) (#\r :control)))
325
326 (define-command (com-read-only :name t :command-table buffer-table) ()
327 "Toggle the readonly status of the current buffer.
328 When a buffer is readonly, attempts to change the contents of the buffer signal an error."
329 (let ((buffer (buffer (current-window))))
330 (setf (read-only-p buffer) (not (read-only-p buffer)))))
331
332 (set-key 'com-read-only
333 'buffer-table
334 '((#\x :control) (#\q :control)))
335
336 (defun set-visited-file-name (filename buffer)
337 (setf (filepath buffer) filename
338 (file-saved-p buffer) nil
339 (file-write-time buffer) nil
340 (name buffer) (filepath-filename filename)
341 (needs-saving buffer) t))
342
343 (define-command (com-set-visited-file-name :name t :command-table buffer-table)
344 ((filename 'pathname :prompt "New file name"
345 :default (directory-of-buffer (buffer (current-window)))
346 :default-type 'pathname
347 :insert-default t))
348 "Prompt for a new filename for the current buffer.
349 The next time the buffer is saved it will be saved to a file with that filename."
350 (set-visited-file-name filename (buffer (current-window))))
351
352 (define-command (com-insert-file :name t :command-table buffer-table)
353 ((filename 'pathname :prompt "Insert File"
354 :default (directory-of-buffer (buffer (current-window)))
355 :default-type 'pathname
356 :insert-default t))
357 "Prompt for a filename and insert its contents at point.
358 Leaves mark after the inserted contents."
359 (let ((pane (current-window)))
360 (when (probe-file filename)
361 (setf (mark pane) (clone-mark (point pane) :left))
362 (with-open-file (stream filename :direction :input)
363 (input-from-stream stream
364 (buffer pane)
365 (offset (point pane))))
366 (psetf (offset (mark pane)) (offset (point pane))
367 (offset (point pane)) (offset (mark pane))))
368 (redisplay-frame-panes *application-frame*)))
369
370 (set-key `(com-insert-file ,*unsupplied-argument-marker*)
371 'buffer-table
372 '((#\x :control) (#\i :control)))
373
374 (define-command (com-revert-buffer :name t :command-table buffer-table) ()
375 "Replace the contents of the current buffer with the visited file.
376 Signals an error if the file does not exist."
377 (let* ((pane (current-window))
378 (buffer (buffer pane))
379 (filepath (filepath buffer))
380 (save (offset (point pane))))
381 (when (accept 'boolean :prompt (format nil "Revert buffer from file ~A?"
382 (filepath buffer)))
383 (cond ((directory-pathname-p filepath)
384 (display-message "~A is a directory name." filepath)
385 (beep))
386 ((probe-file filepath)
387 (unless (check-file-times buffer filepath "Revert" "reverted")
388 (return-from com-revert-buffer))
389 (erase-buffer buffer)
390 (with-open-file (stream filepath :direction :input)
391 (input-from-stream stream buffer 0))
392 (setf (offset (point pane)) (min (size buffer) save)
393 (file-saved-p buffer) nil))
394 (t
395 (display-message "No file ~A" filepath)
396 (beep))))))
397
398 (defun extract-version-number (pathname)
399 "Extracts the emacs-style version-number from a pathname."
400 (let* ((type (pathname-type pathname))
401 (length (length type)))
402 (when (and (> length 2) (char= (char type (1- length)) #\~))
403 (let ((tilde (position #\~ type :from-end t :end (- length 2))))
404 (when tilde
405 (parse-integer type :start (1+ tilde) :junk-allowed t))))))
406
407 (defun version-number (pathname)
408 "Return the number of the highest versioned backup of PATHNAME
409 or 0 if there is no versioned backup. Looks for name.type~X~,
410 returns highest X."
411 (let* ((wildpath (merge-pathnames (make-pathname :type :wild) pathname))
412 (possibilities (directory wildpath)))
413 (loop for possibility in possibilities
414 for version = (extract-version-number possibility)
415 if (numberp version)
416 maximize version into max
417 finally (return max))))
418
419 (defun check-file-times (buffer filepath question answer)
420 "Return NIL if filepath newer than buffer and user doesn't want to overwrite"
421 (let ((f-w-d (file-write-date filepath))
422 (f-w-t (file-write-time buffer)))
423 (if (and f-w-d f-w-t (> f-w-d f-w-t))
424 (if (accept 'boolean
425 :prompt (format nil "File has changed on disk. ~a anyway?"
426 question))
427 t
428 (progn (display-message "~a not ~a" filepath answer)
429 nil))
430 t)))
431
432 (defun save-buffer (buffer)
433 (let ((filepath (or (filepath buffer)
434 (accept 'pathname :prompt "Save Buffer to File"))))
435 (cond
436 ((directory-pathname-p filepath)
437 (display-message "~A is a directory." filepath)
438 (beep))
439 (t
440 (unless (check-file-times buffer filepath "Overwrite" "written")
441 (return-from save-buffer))
442 (when (and (probe-file filepath) (not (file-saved-p buffer)))
443 (let ((backup-name (pathname-name filepath))
444 (backup-type (format nil "~A~~~D~~"
445 (pathname-type filepath)
446 (1+ (version-number filepath)))))
447 (rename-file filepath (make-pathname :name backup-name
448 :type backup-type)))
449 (setf (file-saved-p buffer) t))
450 (with-open-file (stream filepath :direction :output :if-exists :supersede)
451 (output-to-stream stream buffer 0 (size buffer)))
452 (setf (filepath buffer) filepath
453 (file-write-time buffer) (file-write-date filepath)
454 (name buffer) (filepath-filename filepath))
455 (display-message "Wrote: ~a" filepath)
456 (setf (needs-saving buffer) nil)))))
457
458 (define-command (com-save-buffer :name t :command-table buffer-table) ()
459 "Write the contents of the buffer to a file.
460 If there is filename associated with the buffer, write to that file, replacing its contents. If not, prompt for a filename."
461 (let ((buffer (buffer (current-window))))
462 (if (or (null (filepath buffer))
463 (needs-saving buffer))
464 (save-buffer buffer)
465 (display-message "No changes need to be saved from ~a" (name buffer)))))
466
467 (set-key 'com-save-buffer
468 'buffer-table
469 '((#\x :control) (#\s :control)))
470
471 (defmethod frame-exit :around ((frame climacs) #-mcclim &key)
472 (loop for buffer in (buffers frame)
473 when (and (needs-saving buffer)
474 (filepath buffer)
475 (handler-case (accept 'boolean
476 :prompt (format nil "Save buffer: ~a ?" (name buffer)))
477 (error () (progn (beep)
478 (display-message "Invalid answer")
479 (return-from frame-exit nil)))))
480 do (save-buffer buffer))
481 (when (or (notany #'(lambda (buffer) (and (needs-saving buffer) (filepath buffer)))
482 (buffers frame))
483 (handler-case (accept 'boolean :prompt "Modified buffers exist. Quit anyway?")
484 (error () (progn (beep)
485 (display-message "Invalid answer")
486 (return-from frame-exit nil)))))
487 (call-next-method)))
488
489 (define-command (com-write-buffer :name t :command-table buffer-table)
490 ((filepath 'pathname :prompt "Write Buffer to File"
491 :default (directory-of-buffer (buffer (current-window)))
492 :default-type 'pathname
493 :insert-default t))
494 "Prompt for a filename and write the current buffer to it.
495 Changes the file visted by the buffer to the given file."
496 (let ((buffer (buffer (current-window))))
497 (cond
498 ((directory-pathname-p filepath)
499 (display-message "~A is a directory name." filepath))
500 (t
501 (with-open-file (stream filepath :direction :output :if-exists :supersede)
502 (output-to-stream stream buffer 0 (size buffer)))
503 (setf (filepath buffer) filepath
504 (name buffer) (filepath-filename filepath)
505 (needs-saving buffer) nil)
506 (display-message "Wrote: ~a" (filepath buffer))))))
507
508 (set-key `(com-write-buffer ,*unsupplied-argument-marker*)
509 'buffer-table
510 '((#\x :control) (#\w :control)))
511

  ViewVC Help
Powered by ViewVC 1.1.5