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

  ViewVC Help
Powered by ViewVC 1.1.5