/[cmucl]/src/hemlock/filecoms.lisp
ViewVC logotype

Contents of /src/hemlock/filecoms.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1.1.4 - (hide annotations) (vendor branch)
Fri Feb 8 16:34:26 1991 UTC (23 years, 2 months ago) by ram
Changes since 1.1.1.3: +8 -5 lines
Added new header with RCS FILE-COMMENT.
1 ram 1.1 ;;; -*- Package: Hemlock; Log: hemlock.log -*-
2     ;;;
3     ;;; **********************************************************************
4 ram 1.1.1.4 ;;; This code was written as part of the CMU Common Lisp project at
5     ;;; Carnegie Mellon University, and has been placed in the public domain.
6     ;;; If you want to use this code or any part of CMU Common Lisp, please contact
7     ;;; Scott Fahlman or slisp-group@cs.cmu.edu.
8     ;;;
9     (ext:file-comment
10     "$Header: /tiger/var/lib/cvsroots/cmucl/src/hemlock/filecoms.lisp,v 1.1.1.4 1991/02/08 16:34:26 ram Exp $")
11     ;;;
12 ram 1.1 ;;; **********************************************************************
13     ;;;
14     ;;; This file contains file/buffer manipulating commands.
15     ;;;
16     (in-package "HEMLOCK")
17    
18    
19    
20     ;;;; PROCESS-FILE-OPTIONS.
21    
22     (defvar *mode-option-handlers* ()
23     "Do not modify this; use Define-File-Option instead.")
24    
25     (defvar *file-type-hooks* ()
26     "Do not modify this; use Define-File-Type-Hook instead.")
27    
28     (defun trim-subseq (string start end)
29     (declare (simple-string string))
30     (string-trim '(#\Space #\Tab) (subseq string start end)))
31    
32     ;;; PROCESS-FILE-OPTIONS checks the first line of buffer for the file options
33     ;;; indicator "-*-". IF it finds this, then it enters a do-file-options block.
34     ;;; If any parsing errors occur while picking out options, we return from this
35     ;;; block. Staying inside this function at this point, allows us to still set
36     ;;; a major mode if no file option specified one.
37     ;;;
38     ;;; We also cater to old style mode comments:
39     ;;; -*- Lisp -*-
40     ;;; -*- Text -*-
41     ;;; This kicks in if we find no colon on the file options line.
42     ;;;
43     (defun process-file-options (buffer &optional
44     (pathname (buffer-pathname buffer)))
45     "Checks for file options and invokes handlers if there are any. If no
46     \"Mode\" mode option is specified, then this tries to invoke the appropriate
47     file type hook."
48     (let* ((string
49     (line-string (mark-line (buffer-start-mark buffer))))
50     (found (search "-*-" string))
51     (no-major-mode t)
52     (type (if pathname (pathname-type pathname))))
53     (declare (simple-string string))
54     (when found
55     (block do-file-options
56     (let* ((start (+ found 3))
57     (end (search "-*-" string :start2 start)))
58     (unless end
59     (loud-message "No closing \"-*-\". Aborting file options.")
60     (return-from do-file-options))
61     (cond
62     ((find #\: string :start start :end end)
63     (do ((opt-start start (1+ semi)) colon semi)
64     (nil)
65     (setq colon (position #\: string :start opt-start :end end))
66     (unless colon
67     (loud-message "Missing \":\". Aborting file options.")
68     (return-from do-file-options))
69     (setq semi (or (position #\; string :start colon :end end) end))
70     (let* ((option (nstring-downcase
71     (trim-subseq string opt-start colon)))
72     (handler (assoc option *mode-option-handlers*
73     :test #'string=)))
74     (declare (simple-string option))
75     (cond
76     (handler
77     (let ((result (funcall (cdr handler) buffer
78     (trim-subseq string (1+ colon) semi))))
79     (when (string= option "mode")
80     (setq no-major-mode (not result)))))
81     (t (message "Unknown file option: ~S" option)))
82     (when (= semi end) (return nil)))))
83     (t
84     ;; Old style mode comment.
85     (setq no-major-mode nil)
86     (funcall (cdr (assoc "mode" *mode-option-handlers* :test #'string=))
87     buffer (trim-subseq string start end)))))))
88     (when (and no-major-mode type)
89     (let ((hook (assoc (string-downcase type) *file-type-hooks*
90     :test #'string=)))
91     (when hook (funcall (cdr hook) buffer type))))))
92    
93    
94    
95     ;;;; File options and file type hooks.
96    
97     (defmacro define-file-option (name lambda-list &body body)
98     "Define-File-Option Name (Buffer Value) {Form}*
99     Defines a new file option to be user in the -*- line at the top of a file.
100     The body is evaluated with Buffer bound to the buffer the file has been read
101     into and Value to the string argument to the option."
102     (let ((name (string-downcase name)))
103     `(setf (cdr (or (assoc ,name *mode-option-handlers* :test #'string=)
104     (car (push (cons ,name nil) *mode-option-handlers*))))
105     #'(lambda ,lambda-list ,@body))))
106    
107     (define-file-option "Mode" (buffer str)
108     (let ((seen-major-mode-p nil)
109     (lastpos 0))
110     (loop
111     (let* ((pos (position #\, str :start lastpos))
112     (substr (trim-subseq str lastpos pos)))
113     (cond ((getstring substr *mode-names*)
114     (cond ((mode-major-p substr)
115     (when seen-major-mode-p
116     (loud-message
117     "Major mode already processed. Using ~S now."
118     substr))
119     (setf seen-major-mode-p t)
120     (setf (buffer-major-mode buffer) substr))
121     (t
122     (setf (buffer-minor-mode buffer substr) t))))
123     (t
124     (loud-message "~S is not a defined mode -- ignored." substr)))
125     (unless pos
126     (return seen-major-mode-p))
127     (setf lastpos (1+ pos))))))
128    
129    
130     (defmacro define-file-type-hook (type-list (buffer type) &body body)
131     "Define-File-Type-Hook ({Type}*) (Buffer Type) {Form}*
132     Define some code to be evaluated when a file having one of the specified
133     Types is read by a file command. Buffer is bound to the buffer the
134     file is in, and Type is the actual type read."
135     (let ((fun (gensym)) (str (gensym)))
136     `(flet ((,fun (,buffer ,type) ,@body))
137     (dolist (,str ',(mapcar #'string-downcase type-list))
138     (setf (cdr (or (assoc ,str *file-type-hooks* :test #'string=)
139     (car (push (cons ,str nil) *file-type-hooks*))))
140     #',fun)))))
141    
142     (define-file-type-hook ("pas" "pasmac" "macro" "defs" "spc" "bdy")
143     (buffer type)
144     (declare (ignore type))
145     (setf (buffer-major-mode buffer) "Pascal"))
146    
147     (define-file-type-hook ("lisp" "slisp" "l" "lsp" "mcl") (buffer type)
148     (declare (ignore type))
149     (setf (buffer-major-mode buffer) "Lisp"))
150    
151     (define-file-type-hook ("txt" "text" "tx") (buffer type)
152     (declare (ignore type))
153     (setf (buffer-major-mode buffer) "Text"))
154    
155    
156    
157     ;;;; Support for file hacking commands:
158    
159     (defhvar "Pathname Defaults"
160     "This variable contains a pathname which is used to supply defaults
161     when we don't have anything better."
162     :value (pathname "gazonk.del"))
163    
164     (defhvar "Last Resort Pathname Defaults"
165     "This variable contains a pathname which is used to supply defaults when
166     we don't have anything better, but unlike \"Pathname Defaults\", this is
167     never set to some buffer's pathname."
168     :value (pathname "gazonk"))
169    
170     (defhvar "Last Resort Pathname Defaults Function"
171     "This variable contains a function that is called when a default pathname is
172     needed, the buffer has no pathname, and the buffer's name is not entirely
173     composed of alphanumerics. The default value is a function that simply
174     returns \"Last Resort Pathname Defaults\". The function must take a buffer
175     as an argument, and it must return some pathname."
176     :value #'(lambda (buffer)
177     (declare (ignore buffer))
178     (merge-pathnames (value last-resort-pathname-defaults)
179     (value pathname-defaults))))
180    
181     (defun buffer-default-pathname (buffer)
182     "Returns \"Buffer Pathname\" if it is bound. If it is not, and buffer's name
183     is composed solely of alphnumeric characters, then return a pathname formed
184     from the buffer's name. If the buffer's name has other characters in it,
185     then return the value of \"Last Resort Pathname Defaults Function\" called
186     on buffer."
187     (or (buffer-pathname buffer)
188     (if (every #'alphanumericp (the simple-string (buffer-name buffer)))
189     (merge-pathnames (make-pathname :name (buffer-name buffer))
190     (value pathname-defaults))
191     (funcall (value last-resort-pathname-defaults-function) buffer))))
192    
193    
194     (defun pathname-to-buffer-name (pathname)
195     "Returns a simple-string using components from pathname."
196     (let ((pathname (pathname pathname)))
197     (concatenate 'simple-string
198     (file-namestring pathname)
199     " "
200     (directory-namestring pathname))))
201    
202    
203    
204     ;;;; File hacking commands.
205    
206     (defcommand "Process File Options" (p)
207     "Reprocess this buffer's file options."
208     "Reprocess this buffer's file options."
209     (declare (ignore p))
210     (process-file-options (current-buffer)))
211    
212     (defcommand "Insert File" (p &optional pathname (buffer (current-buffer)))
213     "Inserts a file which is prompted for into the current buffer at the point.
214     The prefix argument is ignored."
215     "Inserts the file named by Pathname into Buffer at the point."
216     (declare (ignore p))
217     (let* ((pn (or pathname
218     (prompt-for-file :default (buffer-default-pathname buffer)
219     :prompt "Insert File: "
220     :help "Name of file to insert")))
221     (point (buffer-point buffer))
222     ;; start and end will be deleted by undo stuff
223     (start (copy-mark point :right-inserting))
224     (end (copy-mark point :left-inserting))
225     (region (region start end)))
226     (setv pathname-defaults pn)
227     (push-buffer-mark (copy-mark end))
228     (read-file pn end)
229     (make-region-undo :delete "Insert File" region)))
230    
231     (defcommand "Write Region" (p &optional pathname)
232     "Writes the current region to a file. "
233     "Writes the current region to a file. "
234     (declare (ignore p))
235     (let ((region (current-region))
236     (pn (or pathname
237     (prompt-for-file :prompt "File to Write: "
238     :help "The name of the file to write the region to. "
239     :default (buffer-default-pathname
240     (current-buffer))
241     :must-exist nil))))
242     (write-file region pn)
243     (message "~A written." (namestring (truename pn)))))
244    
245    
246    
247     ;;;; Visiting and reverting files.
248    
249     (defcommand "Visit File" (p &optional pathname (buffer (current-buffer)))
250     "Replaces the contents of Buffer with the file Pathname. The prefix
251     argument is ignored. The buffer is set to be writable, so its region
252     can be deleted."
253     "Replaces the contents of the current buffer with the text in the file
254     which is prompted for. The prefix argument is, of course, ignored p times."
255     (declare (ignore p))
256     (when (and (buffer-modified buffer)
257     (prompt-for-y-or-n :prompt "Buffer is modified, save it? "))
258     (save-file-command () buffer))
259     (let ((pn (or pathname
260     (prompt-for-file :prompt "Visit File: "
261     :must-exist nil
262     :help "Name of file to visit."
263     :default (buffer-default-pathname buffer)))))
264     (setf (buffer-writable buffer) t)
265     (read-buffer-file pn buffer)
266     (let ((n (pathname-to-buffer-name (buffer-pathname buffer))))
267     (unless (getstring n *buffer-names*)
268     (setf (buffer-name buffer) n))
269     (warn-about-visit-file-buffers buffer))))
270    
271     (defun warn-about-visit-file-buffers (buffer)
272     (let ((buffer-pn (buffer-pathname buffer)))
273     (dolist (b *buffer-list*)
274     (unless (eq b buffer)
275     (let ((bpn (buffer-pathname b)))
276     (when (equal bpn buffer-pn)
277     (message "Buffer ~A also contains ~A."
278     (buffer-name b) (namestring buffer-pn))
279     (return)))))))
280    
281    
282     (defhvar "Revert File Confirm"
283     "If this is true, Revert File will prompt before reverting."
284     :value t)
285    
286     (defcommand "Revert File" (p)
287     "Unless in Save Mode, reads in the last saved version of the file in
288     the current buffer. When in Save Mode, reads in the last checkpoint or
289     the last saved version, whichever is more recent. An argument will always
290     force Revert File to use the last saved version. In either case, if the
291     buffer has been modified and \"Revert File Confirm\" is true, then Revert
292     File will ask for confirmation beforehand. An attempt is made to maintain
293     the point's relative position."
294     "With an argument reverts to the last saved version of the file in the
295     current buffer. Without, reverts to the last checkpoint or last saved
296     version, whichever is more recent."
297     (let* ((buffer (current-buffer))
298     (buffer-pn (buffer-pathname buffer))
299     (point (current-point))
300     (lines (1- (count-lines (region (buffer-start-mark buffer) point)))))
301     (multiple-value-bind (revert-pn used-checkpoint)
302     (if p buffer-pn (revert-pathname buffer))
303     (unless revert-pn
304     (editor-error "No file associated with buffer to revert to!"))
305     (when (or (not (value revert-file-confirm))
306     (not (buffer-modified buffer))
307     (prompt-for-y-or-n
308     :prompt
309     "Buffer contains changes, are you sure you want to revert? "
310     :help (list
311     "Reverting the file will undo any changes by reading in the last ~
312     ~:[saved version~;checkpoint file~]." used-checkpoint)
313     :default t))
314     (read-buffer-file revert-pn buffer)
315     (when used-checkpoint
316     (setf (buffer-modified buffer) t)
317     (setf (buffer-pathname buffer) buffer-pn)
318     (message "Reverted to checkpoint file ~A." (namestring revert-pn)))
319     (unless (line-offset point lines)
320     (buffer-end point))))))
321    
322     ;;; REVERT-PATHNAME -- Internal
323     ;;;
324     ;;; If in Save Mode, return either the checkpoint pathname or the buffer
325     ;;; pathname whichever is more recent. Otherwise return the buffer-pathname
326     ;;; if it exists. If neither file exists, return NIL.
327     ;;;
328     (defun revert-pathname (buffer)
329     (let* ((buffer-pn (buffer-pathname buffer))
330     (buffer-pn-date (file-write-date buffer-pn))
331     (checkpoint-pn (get-checkpoint-pathname buffer))
332     (checkpoint-pn-date (file-write-date checkpoint-pn)))
333     (cond (checkpoint-pn-date
334     (if (> checkpoint-pn-date (or buffer-pn-date 0))
335     (values checkpoint-pn t)
336     (values buffer-pn nil)))
337     (buffer-pn-date (values buffer-pn nil))
338     (t (values nil nil)))))
339    
340    
341    
342     ;;;; Find file.
343    
344     (defcommand "Find File" (p &optional pathname)
345     "Visit a file in its own buffer.
346     If the file is already in some buffer, select that buffer,
347     otherwise make a new buffer with the same name as the file and
348     read the file into it."
349     "Make a buffer containing the file Pathname current, creating a buffer
350     if necessary. The buffer is returned."
351     (declare (ignore p))
352     (let* ((pn (or pathname
353     (prompt-for-file
354     :prompt "Find File: "
355     :must-exist nil
356     :help "Name of file to read into its own buffer."
357     :default (buffer-default-pathname (current-buffer)))))
358     (buffer (find-file-buffer pn)))
359     (change-to-buffer buffer)
360     buffer))
361    
362     (defun find-file-buffer (pathname)
363     "Return a buffer assoicated with the file Pathname, reading the file into a
364     new buffer if necessary. The second value is T if we created a buffer, NIL
365     otherwise. If the file has already been read, we check to see if the file
366     has been modified on disk since it was read, giving the user various
367     recovery options."
368     (let* ((pathname (pathname pathname))
369     (trial-pathname (or (probe-file pathname)
370     (merge-pathnames pathname (default-directory))))
371     (found (find trial-pathname (the list *buffer-list*)
372     :key #'buffer-pathname :test #'equal)))
373     (cond ((not found)
374     (let* ((name (pathname-to-buffer-name trial-pathname))
375 ram 1.1.1.1 (found (getstring name *buffer-names*))
376     (use (if found
377 ram 1.1 (prompt-for-buffer
378     :prompt "Buffer to use: "
379     :help
380     "Buffer name in use; give another buffer name, or confirm to reuse."
381 ram 1.1.1.1 :default found :must-exist nil)
382 ram 1.1 (make-buffer name)))
383     (buffer (if (stringp use) (make-buffer use) use)))
384     (when (and (buffer-modified buffer)
385     (prompt-for-y-or-n :prompt
386     "Buffer is modified, save it? "))
387     (save-file-command () buffer))
388     (read-buffer-file pathname buffer)
389     (values buffer (stringp use))))
390     ((check-disk-version-consistent pathname found)
391     (values found nil))
392     (t
393     (read-buffer-file pathname found)
394     (values found nil)))))
395    
396     ;;; Check-Disk-Version-Consistent -- Internal
397     ;;;
398     ;;; Check that Buffer contains a valid version of the file Pathname,
399     ;;; harrassing the user if not. We return true if the buffer is O.K., and
400     ;;; false if the file should be read.
401     ;;;
402     (defun check-disk-version-consistent (pathname buffer)
403     (let ((ndate (file-write-date pathname))
404     (odate (buffer-write-date buffer)))
405     (cond ((not (and ndate odate (/= ndate odate)))
406     t)
407     ((buffer-modified buffer)
408     (beep)
409     (clear-input)
410     (command-case (:prompt (list
411     "File has been changed on disk since it was read and you have made changes too!~
412     ~%Read in the disk version of ~A? [Y] " (namestring pathname))
413     :help
414     "The file in disk has been changed since Hemlock last saved it, meaning that
415     someone else has probably overwritten it. Since the version read into Hemlock
416     has been changed as well, the two versions may have inconsistent changes. If
417     this is the case, it would be a good idea to save your changes in another file
418     and compare the two versions.
419    
420     Type one of the following commands:")
421     ((:confirm :yes)
422     "Prompt for a file to write the buffer out to, then read in the disk version."
423     (write-buffer-file
424     buffer
425     (prompt-for-file
426     :prompt "File to save changes in: "
427     :help (list "Save buffer ~S to this file before reading ~A."
428     (buffer-name buffer) (namestring pathname))
429     :must-exist nil
430     :default (buffer-default-pathname buffer)))
431     nil)
432     (:no
433     "Change to the buffer without reading the new version."
434     t)
435 ram 1.1.1.2 (#\r
436 ram 1.1 "Read in the new version, clobbering the changes in the buffer."
437     nil)))
438     (t
439     (not (prompt-for-yes-or-no :prompt
440     (list
441     "File has been changed on disk since it was read.~
442     ~%Read in the disk version of ~A? "
443     (namestring pathname))
444     :help
445     "Type Y to read in the new version or N to just switch to the buffer."
446     :default t))))))
447    
448    
449     (defhvar "Read File Hook"
450     "These functions are called when a file is read into a buffer. Each function
451     must take two arguments -- the buffer the file was read into and whether the
452     file existed (non-nil) or not (nil).")
453    
454     (defun read-buffer-file (pathname buffer)
455     "Delete the buffer's region, and uses READ-FILE to read pathname into it.
456     If the file exists, set the buffer's write date to the file's; otherwise,
457     MESSAGE that this is a new file and set the buffer's write date to nil.
458     Move buffer's point to the beginning, set the buffer unmodified. If the
459     file exists, set the buffer's pathname to the probed pathname; else, set it
460     to pathname merged with DEFAULT-DIRECTORY. Set \"Pathname Defaults\" to the
461     same thing. Process the file options, and then invoke \"Read File Hook\"."
462     (delete-region (buffer-region buffer))
463     (let* ((pathname (pathname pathname))
464     (probed-pathname (probe-file pathname)))
465     (cond (probed-pathname
466     (read-file probed-pathname (buffer-point buffer))
467     (setf (buffer-write-date buffer) (file-write-date probed-pathname)))
468     (t
469     (message "(New File)")
470     (setf (buffer-write-date buffer) nil)))
471     (buffer-start (buffer-point buffer))
472     (setf (buffer-modified buffer) nil)
473     (let ((stored-pathname (or probed-pathname
474     (merge-pathnames pathname (default-directory)))))
475     (setf (buffer-pathname buffer) stored-pathname)
476     (setf (value pathname-defaults) stored-pathname)
477     (process-file-options buffer stored-pathname)
478     (invoke-hook read-file-hook buffer probed-pathname))))
479    
480    
481    
482     ;;;; File writing.
483    
484     (defhvar "Add Newline at EOF on Writing File"
485     "This controls whether WRITE-BUFFER-FILE adds a newline at the end of the
486     file when it ends at the end of a non-empty line. When set, this may be
487     :ask-user and WRITE-BUFFER-FILE will prompt; otherwise, just add one and
488     inform the user. When nil, never add one and don't ask."
489     :value :ask-user)
490    
491     (defhvar "Keep Backup Files"
492     "When set, .BAK files will be saved upon file writing. This defaults to nil."
493     :value nil)
494    
495     (defhvar "Write File Hook"
496     "These functions are called when a buffer has been written. Each function
497     must take the buffer as an argument.")
498    
499     (defun write-buffer-file (buffer pathname)
500     "Write's buffer to pathname. This assumes pathname is somehow related to
501     the buffer's pathname, and if the buffer's write date is not the same as
502     pathname's, then this prompts the user for confirmation before overwriting
503     the file. This consults \"Add Newline at EOF on Writing File\" and
504     interacts with the user if necessary. This sets \"Pathname Defaults\", and
505     the buffer is marked unmodified. The buffer's pathname and write date are
506     updated, and the buffer is renamed according to the new pathname if possible.
507     This invokes \"Write File Hook\"."
508     (let ((buffer-pn (buffer-pathname buffer)))
509     (let ((date (buffer-write-date buffer))
510     (file-date (when (probe-file pathname) (file-write-date pathname))))
511     (when (and buffer-pn date file-date
512     (equal (make-pathname :version nil :defaults buffer-pn)
513     (make-pathname :version nil :defaults pathname))
514     (/= date file-date))
515     (unless (prompt-for-yes-or-no :prompt (list
516     "File has been changed on disk since it was read.~%Overwrite ~A anyway? "
517     (namestring buffer-pn))
518     :help
519     "Type No to abort writing the file or Yes to overwrite the disk version."
520     :default nil)
521     (editor-error "Write aborted."))))
522     (let ((val (value add-newline-at-eof-on-writing-file)))
523     (when val
524     (let ((end (buffer-end-mark buffer)))
525     (unless (start-line-p end)
526     (when (if (eq val :ask-user)
527     (prompt-for-y-or-n
528     :prompt
529     (list "~A~%File does not have a newline at EOF, add one? "
530     (buffer-name buffer))
531     :default t)
532     t)
533     (insert-character end #\newline)
534     (message "Added newline at EOF."))))))
535     (setv pathname-defaults pathname)
536     (write-file (buffer-region buffer) pathname)
537     (let ((tn (truename pathname)))
538     (message "~A written." (namestring tn))
539     (setf (buffer-modified buffer) nil)
540     (unless (equal tn buffer-pn)
541     (setf (buffer-pathname buffer) tn))
542     (setf (buffer-write-date buffer) (file-write-date tn))
543     (let ((name (pathname-to-buffer-name tn)))
544     (unless (getstring name *buffer-names*)
545     (setf (buffer-name buffer) name)))))
546     (invoke-hook write-file-hook buffer))
547    
548     (defcommand "Write File" (p &optional pathname (buffer (current-buffer)))
549     "Writes the contents of Buffer, which defaults to the current buffer to
550     the file named by Pathname. The prefix argument is ignored."
551     "Prompts for a file to write the contents of the current Buffer to.
552     The prefix argument is ignored."
553     (declare (ignore p))
554     (write-buffer-file
555     buffer
556     (or pathname
557     (prompt-for-file :prompt "Write File: "
558     :must-exist nil
559     :help "Name of file to write to"
560     :default (buffer-default-pathname buffer)))))
561    
562     (defcommand "Save File" (p &optional (buffer (current-buffer)))
563     "Writes the contents of the current buffer to the associated file. If there
564     is no associated file, once is prompted for."
565     "Writes the contents of the current buffer to the associated file."
566     (declare (ignore p))
567     (when (or (buffer-modified buffer)
568     (prompt-for-y-or-n
569     :prompt "Buffer is unmodified, write it anyway? "
570     :default t))
571     (write-buffer-file
572     buffer
573     (or (buffer-pathname buffer)
574     (prompt-for-file :prompt "Save File: "
575     :help "Name of file to write to"
576     :default (buffer-default-pathname buffer)
577     :must-exist nil)))))
578    
579     (defhvar "Save All Files Confirm"
580     "When non-nil, prompts for confirmation before writing each modified buffer."
581     :value t)
582    
583     (defcommand "Save All Files" (p)
584     "Saves all modified buffers in their associated files.
585     If a buffer has no associated file it is ignored even if it is modified.."
586     "Saves each modified buffer that has a file."
587     (declare (ignore p))
588     (let ((saved-count 0))
589     (dolist (b *buffer-list*)
590     (let ((pn (buffer-pathname b))
591     (name (buffer-name b)))
592     (when
593     (and (buffer-modified b)
594     pn
595     (or (not (value save-all-files-confirm))
596     (prompt-for-y-or-n
597     :prompt (list
598     "Write ~:[buffer ~A as file ~S~;file ~*~S~], ~
599     Y or N: "
600     (string= (pathname-to-buffer-name pn) name)
601     name (namestring pn))
602     :default t)))
603     (write-buffer-file b pn)
604     (incf saved-count))))
605     (if (zerop saved-count)
606     (message "No files were saved.")
607     (message "Saved ~S file~:P." saved-count))))
608    
609     (defcommand "Save All Files and Exit" (p)
610     "Save all modified buffers in their associated files and exit;
611     a combination of \"Save All Files\" and \"Exit Hemlock\"."
612     "Do a save-all-files-command and then an exit-hemlock."
613     (declare (ignore p))
614     (save-all-files-command ())
615     (exit-hemlock))
616    
617     (defcommand "Backup File" (p)
618     "Write the buffer to a file without changing the associated name."
619     "Write the buffer to a file without changing the associated name."
620     (declare (ignore p))
621     (let ((file (prompt-for-file :prompt "Backup to File: "
622     :help
623     "Name of a file to backup the current buffer in."
624     :default (buffer-default-pathname (current-buffer))
625     :must-exist nil)))
626     (write-file (buffer-region (current-buffer)) file)
627     (message "~A written." (namestring (truename file)))))
628    
629    
630    
631     ;;;; Buffer hacking commands:
632    
633     (defvar *buffer-history* ()
634     "A list of buffers, in order from most recently to least recently selected.")
635    
636     (defun previous-buffer ()
637     "Returns some previously selected buffer that is not the current buffer.
638     Returns nil if no such buffer exists."
639     (let ((b (car *buffer-history*)))
640     (or (if (eq b (current-buffer)) (cadr *buffer-history*) b)
641     (find-if-not #'(lambda (x)
642     (or (eq x (current-buffer))
643     (eq x *echo-area-buffer*)))
644     (the list *buffer-list*)))))
645    
646     ;;; ADD-BUFFER-HISTORY-HOOK makes sure every buffer will be visited by
647     ;;; "Circulate Buffers" even if it has never been before.
648     ;;;
649     (defun add-buffer-history-hook (buffer)
650     (let ((ele (last *buffer-history*))
651     (new-stuff (list buffer)))
652     (if ele
653     (setf (cdr ele) new-stuff)
654     (setf *buffer-history* new-stuff))))
655     ;;;
656     (add-hook make-buffer-hook 'add-buffer-history-hook)
657    
658     ;;; DELETE-BUFFER-HISTORY-HOOK makes sure we never end up in a dead buffer.
659     ;;;
660     (defun delete-buffer-history-hook (buffer)
661     (setq *buffer-history* (delq buffer *buffer-history*)))
662     ;;;
663     (add-hook delete-buffer-hook 'delete-buffer-history-hook)
664    
665     (defun change-to-buffer (buffer)
666     "Switches to buffer in the current window maintaining *buffer-history*."
667     (setq *buffer-history*
668     (cons (current-buffer) (delq (current-buffer) *buffer-history*)))
669     (setf (current-buffer) buffer)
670     (setf (window-buffer (current-window)) buffer))
671    
672     (defun delete-buffer-if-possible (buffer)
673     "Deletes a buffer if at all possible. If buffer is the only buffer, other
674     than the echo area, signals an error. Otherwise, find some recently current
675     buffer, and make all of buffer's windows display this recent buffer. If
676     buffer is current, set the current buffer to be this recently current
677     buffer."
678     (let ((new-buf (flet ((frob (b)
679     (or (eq b buffer) (eq b *echo-area-buffer*))))
680     (or (find-if-not #'frob (the list *buffer-history*))
681     (find-if-not #'frob (the list *buffer-list*))))))
682     (unless new-buf
683     (error "Cannot delete only buffer ~S." buffer))
684     (dolist (w (buffer-windows buffer))
685     (setf (window-buffer w) new-buf))
686     (when (eq buffer (current-buffer))
687     (setf (current-buffer) new-buf)))
688     (delete-buffer buffer))
689    
690    
691     (defvar *create-buffer-count* 0)
692    
693     (defcommand "Create Buffer" (p &optional buffer-name)
694     "Create a new buffer. If a buffer with the specified name already exists,
695     then go to it."
696     "Create or go to the buffer with the specifed name."
697     (declare (ignore p))
698     (let ((name (or buffer-name
699     (prompt-for-buffer :prompt "Create Buffer: "
700     :default-string
701     (format nil "Buffer ~D"
702     (incf *create-buffer-count*))
703     :must-exist nil))))
704     (if (bufferp name)
705     (change-to-buffer name)
706     (change-to-buffer (or (getstring name *buffer-names*)
707     (make-buffer name))))))
708    
709     (defcommand "Select Buffer" (p)
710     "Select a different buffer.
711     The buffer to go to is prompted for."
712     "Select a different buffer.
713     The buffer to go to is prompted for."
714     (declare (ignore p))
715     (let ((buf (prompt-for-buffer :prompt "Select Buffer: "
716     :default (previous-buffer))))
717     (when (eq buf *echo-area-buffer*)
718     (editor-error "Cannot select Echo Area buffer."))
719     (change-to-buffer buf)))
720    
721    
722     (defvar *buffer-history-ptr* ()
723     "The successively previous buffer to the current buffer.")
724    
725     (defcommand "Select Previous Buffer" (p)
726     "Select the buffer selected before this one. If called repeatedly
727     with an argument, select the successively previous buffer to the
728     current one leaving the buffer history as it is."
729     "Select the buffer selected before this one."
730     (if p
731     (circulate-buffers-command nil)
732     (let ((b (previous-buffer)))
733     (unless b (editor-error "No previous buffer."))
734     (change-to-buffer b)
735     ;;
736     ;; If the pointer goes to nil, then "Circulate Buffers" will keep doing
737     ;; "Select Previous Buffer".
738     (setf *buffer-history-ptr* (cddr *buffer-history*))
739     (setf (last-command-type) :previous-buffer))))
740    
741     (defcommand "Circulate Buffers" (p)
742     "Advance through buffer history, selecting successively previous buffer."
743     "Advance through buffer history, selecting successively previous buffer."
744     (declare (ignore p))
745     (if (and (eq (last-command-type) :previous-buffer)
746     *buffer-history-ptr*) ;Possibly nil if never CHANGE-TO-BUFFER.
747     (let ((b (pop *buffer-history-ptr*)))
748     (when (eq b (current-buffer))
749     (setf b (pop *buffer-history-ptr*)))
750     (unless b
751     (setf *buffer-history-ptr*
752     (or (cdr *buffer-history*) *buffer-history*))
753     (setf b (car *buffer-history*)))
754     (setf (current-buffer) b)
755     (setf (window-buffer (current-window)) b)
756     (setf (last-command-type) :previous-buffer))
757     (select-previous-buffer-command nil)))
758    
759    
760     (defcommand "Buffer Not Modified" (p)
761     "Make the current buffer not modified."
762     "Make the current buffer not modified."
763     (declare (ignore p))
764     (setf (buffer-modified (current-buffer)) nil)
765     (message "Buffer marked as unmodified."))
766    
767     (defcommand "Check Buffer Modified" (p)
768     "Say whether the buffer is modified or not."
769     "Say whether the current buffer is modified or not."
770     (declare (ignore p))
771     (clear-echo-area)
772     (message "Buffer ~S ~:[is not~;is~] modified."
773     (buffer-name (current-buffer)) (buffer-modified (current-buffer))))
774    
775     (defcommand "Set Buffer Read-Only" (p)
776     "Toggles the read-only flag for the current buffer."
777     "Toggles the read-only flag for the current buffer."
778     (declare (ignore p))
779     (let ((buffer (current-buffer)))
780     (message "Buffer ~S is now ~:[read-only~;writable~]."
781     (buffer-name buffer)
782     (setf (buffer-writable buffer) (not (buffer-writable buffer))))))
783    
784     (defcommand "Kill Buffer" (p &optional buffer-name)
785     "Prompts for a buffer to delete.
786     If the buffer is modified, then let the user save the file before doing so.
787     When deleting the current buffer, prompts for a new buffer to select. If
788     a buffer other than the current one is deleted then any windows into it
789     are deleted."
790     "Delete buffer Buffer-Name, doing sensible things if the buffer is displayed
791     or current."
792     (declare (ignore p))
793     (let ((buffer (if buffer-name
794     (getstring buffer-name *buffer-names*)
795     (prompt-for-buffer :prompt "Kill Buffer: "
796     :default (current-buffer)))))
797     (if (not buffer)
798     (editor-error "No buffer named ~S" buffer-name))
799     (if (and (buffer-modified buffer)
800     (prompt-for-y-or-n :prompt "Save it first? "))
801     (save-file-command () buffer))
802     (if (eq buffer (current-buffer))
803     (let ((new (prompt-for-buffer :prompt "New Buffer: "
804     :default (previous-buffer)
805     :help "Buffer to change to after the current one is killed.")))
806     (when (eq new buffer)
807     (editor-error "You must select a different buffer."))
808     (dolist (w (buffer-windows buffer))
809     (setf (window-buffer w) new))
810     (setf (current-buffer) new))
811     (dolist (w (buffer-windows buffer))
812     (delete-window w)))
813     (delete-buffer buffer)))
814    
815     (defcommand "Rename Buffer" (p)
816     "Change the current buffer's name.
817     The name, which is prompted for, defaults to the name of the associated
818     file."
819     "Change the name of the current buffer."
820     (declare (ignore p))
821     (let* ((buf (current-buffer))
822     (pn (buffer-pathname buf))
823     (name (if pn (pathname-to-buffer-name pn) (buffer-name buf)))
824     (new (prompt-for-string :prompt "New Name: "
825     :help "Give a new name for the current buffer"
826     :default name)))
827     (multiple-value-bind (entry foundp) (getstring new *buffer-names*)
828     (cond ((or (not foundp) (eq entry buf))
829     (setf (buffer-name buf) new))
830     (t (editor-error "Name ~S already in use." new))))))
831    
832    
833     (defcommand "Insert Buffer" (p)
834     "Insert the contents of a buffer.
835     The name of the buffer to insert is prompted for."
836     "Prompt for a buffer to insert at the point."
837     (declare (ignore p))
838     (let ((point (current-point))
839     (region (buffer-region (prompt-for-buffer
840     :default (previous-buffer)
841     :help
842     "Type the name of a buffer to insert."))))
843     ;;
844     ;; start and end will be deleted by undo stuff
845     (let ((save (region (copy-mark point :right-inserting)
846     (copy-mark point :left-inserting))))
847     (push-buffer-mark (copy-mark point))
848     (insert-region point region)
849     (make-region-undo :delete "Insert Buffer" save))))
850    
851    
852    
853     ;;;; File utility commands:
854    
855     (defcommand "Directory" (p)
856     "Do a directory into a pop-up window. If an argument is supplied, then
857     dot files are listed too (as with ls -a). Prompts for a pathname which
858     may contain wildcards in the name and type."
859     "Do a directory into a pop-up window."
860     (let* ((dpn (value pathname-defaults))
861     (pn (prompt-for-file
862     :prompt "Directory: "
863     :help "Pathname to do directory on."
864     :default (make-pathname :device (pathname-device dpn)
865     :directory (pathname-directory dpn))
866     :must-exist nil)))
867     (setf (value pathname-defaults) (merge-pathnames pn dpn))
868     (with-pop-up-display (s)
869     (print-directory pn s :all p))))
870    
871     (defcommand "Verbose Directory" (p)
872     "Do a directory into a pop-up window. If an argument is supplied, then
873     dot files are listed too (as with ls -a). Prompts for a pathname which
874     may contain wildcards in the name and type."
875     "Do a directory into a pop-up window."
876     (let* ((dpn (value pathname-defaults))
877     (pn (prompt-for-file
878     :prompt "Verbose Directory: "
879     :help "Pathname to do directory on."
880     :default (make-pathname :device (pathname-device dpn)
881     :directory (pathname-directory dpn))
882     :must-exist nil)))
883     (setf (value pathname-defaults) (merge-pathnames pn dpn))
884     (with-pop-up-display (s)
885     (print-directory pn s :verbose t :all p))))
886    
887    
888    
889     ;;;; Change log stuff:
890    
891     (define-file-option "Log" (buffer value)
892     (defhvar "Log File Name"
893     "The name of the file for the change log for the file in this buffer."
894     :buffer buffer :value value))
895    
896     (defhvar "Log Entry Template"
897     "The format string used to generate the template for a change-log entry.
898     Three arguments are given: the file, the date (create if available, now
899     otherwise) and the file author, or NIL if not available. The last \"@\"
900     is deleted and the point placed where it was."
901     :value "~A, ~A, Edit by ~:[???~;~:*~:(~A~)~].~% @~2%")
902    
903     (defmode "Log"
904     :major-p t
905     :setup-function
906     #'(lambda (buffer)
907     (setf (buffer-minor-mode buffer "Fill") t))
908     :cleanup-function
909     #'(lambda (buffer)
910     (setf (buffer-minor-mode buffer "Fill") nil)))
911    
912     (defhvar "Fill Prefix" "The fill prefix in Log mode."
913     :value " " :mode "Log")
914    
915     (define-file-type-hook ("log") (buffer type)
916     (declare (ignore type))
917     (setf (buffer-major-mode buffer) "Log"))
918    
919     (defun universal-time-to-string (ut)
920     (multiple-value-bind (sec min hour day month year)
921     (decode-universal-time ut)
922     (format nil "~2,'0D-~A-~2,'0D ~2,'0D:~2,'0D:~2,'0D"
923     day (svref '#("Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug"
924     "Sep" "Oct" "Nov" "Dec")
925     (1- month))
926     (rem year 100)
927     hour min sec)))
928    
929     (defvar *back-to-@-pattern* (new-search-pattern :character :backward #\@))
930     (defcommand "Log Change" (p)
931     "Make an entry in the change-log file for this buffer.
932     Saves the file in the current buffer if it is modified, then finds the file
933     specified in the \"Log\" file option, adds the template for a change-log
934     entry at the beginning, then does a recursive edit, saving the log file on
935     exit."
936     "Find the change-log file as specified by \"Log File Name\" and edit it."
937     (declare (ignore p))
938     (unless (hemlock-bound-p 'log-file-name)
939     (editor-error "No log file defined."))
940     (let* ((buffer (current-buffer))
941     (pathname (buffer-pathname buffer)))
942     (when (or (buffer-modified buffer) (null pathname))
943     (save-file-command ()))
944     (unwind-protect
945     (progn
946     (find-file-command nil (merge-pathnames
947     (value log-file-name)
948     (buffer-default-pathname buffer)))
949     (let ((point (current-point)))
950     (buffer-start point)
951     (with-output-to-mark (s point :full)
952     (format s (value log-entry-template)
953     (namestring pathname)
954     (universal-time-to-string
955     (or (file-write-date pathname)
956     (get-universal-time)))
957     (file-author pathname)))
958     (when (find-pattern point *back-to-@-pattern*)
959     (delete-characters point 1)))
960     (do-recursive-edit)
961     (when (buffer-modified (current-buffer)) (save-file-command ())))
962     (if (member buffer *buffer-list* :test #'eq)
963     (change-to-buffer buffer)
964     (editor-error "Old buffer has been deleted.")))))
965    
966    
967    
968     ;;;; Window hacking commands:
969    
970     (defcommand "Next Window" (p)
971     "Change the current window to be the next window and the current buffer
972     to be it's buffer."
973     "Go to the next window.
974     If the next window is the bottom window then wrap around to the top window."
975     (declare (ignore p))
976     (let* ((next (next-window (current-window)))
977     (buffer (window-buffer next)))
978     (setf (current-buffer) buffer (current-window) next)))
979    
980     (defcommand "Previous Window" (p)
981     "Change the current window to be the previous window and the current buffer
982     to be it's buffer."
983     "Go to the previous window.
984     If the Previous window is the top window then wrap around to the bottom."
985     (declare (ignore p))
986     (let* ((previous (previous-window (current-window)))
987     (buffer (window-buffer previous)))
988     (setf (current-buffer) buffer (current-window) previous)))
989    
990     (defcommand "Split Window" (p)
991     "Make a new window by splitting the current window.
992     The new window is made the current window and displays starting at
993     the same place as the current window."
994     "Create a new window which displays starting at the same place
995     as the current window."
996     (declare (ignore p))
997     (let ((new (make-window (window-display-start (current-window)))))
998     (unless new (editor-error "Could not make a new window."))
999     (setf (current-window) new)))
1000    
1001     (defcommand "New Window" (p)
1002     "Make a new window and go to it.
1003     The window will display the same buffer as the current one."
1004     "Create a new window which displays starting at the same place
1005     as the current window."
1006     (declare (ignore p))
1007     (let ((new (make-window (window-display-start (current-window))
1008     :ask-user t)))
1009     (unless new (editor-error "Could not make a new window."))
1010     (setf (current-window) new)))
1011    
1012     (defcommand "Delete Window" (p)
1013     "Delete the current window, going to the previous window."
1014     "Delete the window we are in, going to the previous window."
1015     (declare (ignore p))
1016 ram 1.1.1.3 (when (= (length *window-list*) 2)
1017     (editor-error "Cannot delete only window."))
1018 ram 1.1 (let ((window (current-window)))
1019 ram 1.1.1.3 (previous-window-command nil)
1020 ram 1.1 (delete-window window)))
1021    
1022     (defcommand "Line to Top of Window" (p)
1023     "Move current line to top of window."
1024     "Move current line to top of window."
1025     (declare (ignore p))
1026     (with-mark ((mark (current-point)))
1027     (move-mark (window-display-start (current-window)) (line-start mark))))
1028    
1029     (defcommand "Delete Next Window" (p)
1030     "Deletes the next window on display."
1031     "Deletes then next window on display."
1032     (declare (ignore p))
1033 ram 1.1.1.3 (if (<= (length *window-list*) 2)
1034 ram 1.1 (editor-error "Cannot delete only window")
1035     (delete-window (next-window (current-window)))))
1036 ram 1.1.1.2
1037     (defcommand "Go to One Window" (p)
1038     "Deletes all windows leaving one with the \"Default Initial Window X\",
1039     \"Default Initial Window Y\", \"Default Initial Window Width\", and
1040     \"Default Initial Window Height\"."
1041     "Deletes all windows leaving one with the \"Default Initial Window X\",
1042     \"Default Initial Window Y\", \"Default Initial Window Width\", and
1043     \"Default Initial Window Height\"."
1044     (declare (ignore p))
1045     (let ((win (make-window (window-display-start (current-window))
1046     :ask-user t
1047     :x (value default-initial-window-x)
1048     :y (value default-initial-window-y)
1049     :width (value default-initial-window-width)
1050     :height (value default-initial-window-height))))
1051     (setf (current-window) win)
1052     (dolist (w *window-list*)
1053     (unless (or (eq w win)
1054     (eq w *echo-area-window*))
1055     (delete-window w)))))
1056 ram 1.1
1057     (defcommand "Line to Center of Window" (p)
1058     "Moves current line to the center of the window."
1059     "Moves current line to the center of the window."
1060     (declare (ignore p))
1061     (center-window (current-window) (current-point)))

  ViewVC Help
Powered by ViewVC 1.1.5