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

Contents of /src/hemlock/filecoms.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (show annotations)
Fri Feb 11 21:53:02 1994 UTC (20 years, 2 months ago) by ram
Branch: MAIN
Changes since 1.3: +37 -23 lines
Copy new_compiler branch to trunk.
1 ;;; -*- Package: Hemlock; Log: hemlock.log -*-
2 ;;;
3 ;;; **********************************************************************
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.4 1994/02/11 21:53:02 ram Exp $")
11 ;;;
12 ;;; **********************************************************************
13 ;;;
14 ;;; This file contains file/buffer manipulating commands.
15 ;;;
16
17 (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 (loud-message "Buffer ~A also contains ~A."
279 (buffer-name b) (namestring buffer-pn))
280 (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 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 "With an argument reverts to the last saved version of the file in the
296 current buffer. Without, reverts to the last checkpoint or last saved
297 version, whichever is more recent."
298 (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 (and checkpoint-pn
334 (file-write-date checkpoint-pn))))
335 (cond (checkpoint-pn-date
336 (if (> checkpoint-pn-date (or buffer-pn-date 0))
337 (values checkpoint-pn t)
338 (values buffer-pn nil)))
339 (buffer-pn-date (values buffer-pn nil))
340 (t (values nil nil)))))
341
342
343
344 ;;;; Find file.
345
346 (defcommand "Find File" (p &optional pathname)
347 "Visit a file in its own buffer.
348 If the file is already in some buffer, select that buffer,
349 otherwise make a new buffer with the same name as the file and
350 read the file into it."
351 "Make a buffer containing the file Pathname current, creating a buffer
352 if necessary. The buffer is returned."
353 (declare (ignore p))
354 (let* ((pn (or pathname
355 (prompt-for-file
356 :prompt "Find File: "
357 :must-exist nil
358 :help "Name of file to read into its own buffer."
359 :default (buffer-default-pathname (current-buffer)))))
360 (buffer (find-file-buffer pn)))
361 (change-to-buffer buffer)
362 buffer))
363
364 (defun find-file-buffer (pathname)
365 "Return a buffer assoicated with the file Pathname, reading the file into a
366 new buffer if necessary. The second value is T if we created a buffer, NIL
367 otherwise. If the file has already been read, we check to see if the file
368 has been modified on disk since it was read, giving the user various
369 recovery options."
370 (let* ((pathname (pathname pathname))
371 (trial-pathname (or (probe-file pathname)
372 (merge-pathnames pathname (default-directory))))
373 (found (find trial-pathname (the list *buffer-list*)
374 :key #'buffer-pathname :test #'equal)))
375 (cond ((not found)
376 (let* ((name (pathname-to-buffer-name trial-pathname))
377 (found (getstring name *buffer-names*))
378 (use (if found
379 (prompt-for-buffer
380 :prompt "Buffer to use: "
381 :help
382 "Buffer name in use; give another buffer name, or confirm to reuse."
383 :default found :must-exist nil)
384 (make-buffer name)))
385 (buffer (if (stringp use) (make-buffer use) use)))
386 (when (and (buffer-modified buffer)
387 (prompt-for-y-or-n :prompt
388 "Buffer is modified, save it? "))
389 (save-file-command () buffer))
390 (read-buffer-file pathname buffer)
391 (values buffer (stringp use))))
392 ((check-disk-version-consistent pathname found)
393 (values found nil))
394 (t
395 (read-buffer-file pathname found)
396 (values found nil)))))
397
398 ;;; Check-Disk-Version-Consistent -- Internal
399 ;;;
400 ;;; Check that Buffer contains a valid version of the file Pathname,
401 ;;; harrassing the user if not. We return true if the buffer is O.K., and
402 ;;; false if the file should be read.
403 ;;;
404 (defun check-disk-version-consistent (pathname buffer)
405 (let ((ndate (file-write-date pathname))
406 (odate (buffer-write-date buffer)))
407 (cond ((not (and ndate odate (/= ndate odate)))
408 t)
409 ((buffer-modified buffer)
410 (beep)
411 (clear-input)
412 (command-case (:prompt (list
413 "File has been changed on disk since it was read and you have made changes too!~
414 ~%Read in the disk version of ~A? [Y] " (namestring pathname))
415 :help
416 "The file in disk has been changed since Hemlock last saved it, meaning that
417 someone else has probably overwritten it. Since the version read into Hemlock
418 has been changed as well, the two versions may have inconsistent changes. If
419 this is the case, it would be a good idea to save your changes in another file
420 and compare the two versions.
421
422 Type one of the following commands:")
423 ((:confirm :yes)
424 "Prompt for a file to write the buffer out to, then read in the disk version."
425 (write-buffer-file
426 buffer
427 (prompt-for-file
428 :prompt "File to save changes in: "
429 :help (list "Save buffer ~S to this file before reading ~A."
430 (buffer-name buffer) (namestring pathname))
431 :must-exist nil
432 :default (buffer-default-pathname buffer)))
433 nil)
434 (:no
435 "Change to the buffer without reading the new version."
436 t)
437 (#\r
438 "Read in the new version, clobbering the changes in the buffer."
439 nil)))
440 (t
441 (not (prompt-for-yes-or-no :prompt
442 (list
443 "File has been changed on disk since it was read.~
444 ~%Read in the disk version of ~A? "
445 (namestring pathname))
446 :help
447 "Type Y to read in the new version or N to just switch to the buffer."
448 :default t))))))
449
450
451 (defhvar "Read File Hook"
452 "These functions are called when a file is read into a buffer. Each function
453 must take two arguments -- the buffer the file was read into and whether the
454 file existed (non-nil) or not (nil).")
455
456 (defun read-buffer-file (pathname buffer)
457 "Delete the buffer's region, and uses READ-FILE to read pathname into it.
458 If the file exists, set the buffer's write date to the file's; otherwise,
459 MESSAGE that this is a new file and set the buffer's write date to nil.
460 Move buffer's point to the beginning, set the buffer unmodified. If the
461 file exists, set the buffer's pathname to the probed pathname; else, set it
462 to pathname merged with DEFAULT-DIRECTORY. Set \"Pathname Defaults\" to the
463 same thing. Process the file options, and then invoke \"Read File Hook\"."
464 (setf (buffer-writable buffer) t)
465 (delete-region (buffer-region buffer))
466 (let* ((pathname (pathname pathname))
467 (probed-pathname (probe-file pathname)))
468 (cond (probed-pathname
469 (read-file probed-pathname (buffer-point buffer))
470 (setf (buffer-write-date buffer) (file-write-date probed-pathname)))
471 (t
472 (message "(New File)")
473 (setf (buffer-write-date buffer) nil)))
474 (buffer-start (buffer-point buffer))
475 (setf (buffer-modified buffer) nil)
476 (let ((stored-pathname (or probed-pathname
477 (merge-pathnames pathname (default-directory)))))
478 (setf (buffer-pathname buffer) stored-pathname)
479 (setf (value pathname-defaults) stored-pathname)
480 (process-file-options buffer stored-pathname)
481 (invoke-hook read-file-hook buffer probed-pathname))))
482
483
484
485 ;;;; File writing.
486
487 (defhvar "Add Newline at EOF on Writing File"
488 "This controls whether WRITE-BUFFER-FILE adds a newline at the end of the
489 file when it ends at the end of a non-empty line. When set, this may be
490 :ask-user and WRITE-BUFFER-FILE will prompt; otherwise, just add one and
491 inform the user. When nil, never add one and don't ask."
492 :value :ask-user)
493
494 (defhvar "Keep Backup Files"
495 "When set, .BAK files will be saved upon file writing. This defaults to nil."
496 :value nil)
497
498 (defhvar "Write File Hook"
499 "These functions are called when a buffer has been written. Each function
500 must take the buffer as an argument.")
501
502 (defun write-buffer-file (buffer pathname)
503 "Write's buffer to pathname. This assumes pathname is somehow related to
504 the buffer's pathname, and if the buffer's write date is not the same as
505 pathname's, then this prompts the user for confirmation before overwriting
506 the file. This consults \"Add Newline at EOF on Writing File\" and
507 interacts with the user if necessary. This sets \"Pathname Defaults\", and
508 the buffer is marked unmodified. The buffer's pathname and write date are
509 updated, and the buffer is renamed according to the new pathname if possible.
510 This invokes \"Write File Hook\"."
511 (let ((buffer-pn (buffer-pathname buffer)))
512 (let ((date (buffer-write-date buffer))
513 (file-date (when (probe-file pathname) (file-write-date pathname))))
514 (when (and buffer-pn date file-date
515 (equal (make-pathname :version nil :defaults buffer-pn)
516 (make-pathname :version nil :defaults pathname))
517 (/= date file-date))
518 (unless (prompt-for-yes-or-no :prompt (list
519 "File has been changed on disk since it was read.~%Overwrite ~A anyway? "
520 (namestring buffer-pn))
521 :help
522 "Type No to abort writing the file or Yes to overwrite the disk version."
523 :default nil)
524 (editor-error "Write aborted."))))
525 (let ((val (value add-newline-at-eof-on-writing-file)))
526 (when val
527 (let ((end (buffer-end-mark buffer)))
528 (unless (start-line-p end)
529 (when (if (eq val :ask-user)
530 (prompt-for-y-or-n
531 :prompt
532 (list "~A~%File does not have a newline at EOF, add one? "
533 (buffer-name buffer))
534 :default t)
535 t)
536 (insert-character end #\newline)
537 (message "Added newline at EOF."))))))
538 (setv pathname-defaults pathname)
539 (write-file (buffer-region buffer) pathname)
540 (let ((tn (truename pathname)))
541 (message "~A written." (namestring tn))
542 (setf (buffer-modified buffer) nil)
543 (unless (equal tn buffer-pn)
544 (setf (buffer-pathname buffer) tn))
545 (setf (buffer-write-date buffer) (file-write-date tn))
546 (let ((name (pathname-to-buffer-name tn)))
547 (unless (getstring name *buffer-names*)
548 (setf (buffer-name buffer) name)))))
549 (invoke-hook write-file-hook buffer))
550
551 (defcommand "Write File" (p &optional pathname (buffer (current-buffer)))
552 "Writes the contents of Buffer, which defaults to the current buffer to
553 the file named by Pathname. The prefix argument is ignored."
554 "Prompts for a file to write the contents of the current Buffer to.
555 The prefix argument is ignored."
556 (declare (ignore p))
557 (write-buffer-file
558 buffer
559 (or pathname
560 (prompt-for-file :prompt "Write File: "
561 :must-exist nil
562 :help "Name of file to write to"
563 :default (buffer-default-pathname buffer)))))
564
565 (defcommand "Save File" (p &optional (buffer (current-buffer)))
566 "Writes the contents of the current buffer to the associated file. If there
567 is no associated file, once is prompted for."
568 "Writes the contents of the current buffer to the associated file."
569 (declare (ignore p))
570 (when (or (buffer-modified buffer)
571 (prompt-for-y-or-n
572 :prompt "Buffer is unmodified, write it anyway? "
573 :default t))
574 (write-buffer-file
575 buffer
576 (or (buffer-pathname buffer)
577 (prompt-for-file :prompt "Save File: "
578 :help "Name of file to write to"
579 :default (buffer-default-pathname buffer)
580 :must-exist nil)))))
581
582 (defhvar "Save All Files Confirm"
583 "When non-nil, prompts for confirmation before writing each modified buffer."
584 :value t)
585
586 (defcommand "Save All Files" (p)
587 "Saves all modified buffers in their associated files.
588 If a buffer has no associated file it is ignored even if it is modified.."
589 "Saves each modified buffer that has a file."
590 (declare (ignore p))
591 (let ((saved-count 0))
592 (dolist (b *buffer-list*)
593 (let ((pn (buffer-pathname b))
594 (name (buffer-name b)))
595 (when
596 (and (buffer-modified b)
597 pn
598 (or (not (value save-all-files-confirm))
599 (prompt-for-y-or-n
600 :prompt (list
601 "Write ~:[buffer ~A as file ~S~;file ~*~S~], ~
602 Y or N: "
603 (string= (pathname-to-buffer-name pn) name)
604 name (namestring pn))
605 :default t)))
606 (write-buffer-file b pn)
607 (incf saved-count))))
608 (if (zerop saved-count)
609 (message "No files were saved.")
610 (message "Saved ~S file~:P." saved-count))))
611
612 (defcommand "Save All Files and Exit" (p)
613 "Save all modified buffers in their associated files and exit;
614 a combination of \"Save All Files\" and \"Exit Hemlock\"."
615 "Do a save-all-files-command and then an exit-hemlock."
616 (declare (ignore p))
617 (save-all-files-command ())
618 (exit-hemlock))
619
620 (defcommand "Backup File" (p)
621 "Write the buffer to a file without changing the associated name."
622 "Write the buffer to a file without changing the associated name."
623 (declare (ignore p))
624 (let ((file (prompt-for-file :prompt "Backup to File: "
625 :help
626 "Name of a file to backup the current buffer in."
627 :default (buffer-default-pathname (current-buffer))
628 :must-exist nil)))
629 (write-file (buffer-region (current-buffer)) file)
630 (message "~A written." (namestring (truename file)))))
631
632
633
634 ;;;; Buffer hacking commands:
635
636 (defvar *buffer-history* ()
637 "A list of buffers, in order from most recently to least recently selected.")
638
639 (defun previous-buffer ()
640 "Returns some previously selected buffer that is not the current buffer.
641 Returns nil if no such buffer exists."
642 (let ((b (car *buffer-history*)))
643 (or (if (eq b (current-buffer)) (cadr *buffer-history*) b)
644 (find-if-not #'(lambda (x)
645 (or (eq x (current-buffer))
646 (eq x *echo-area-buffer*)))
647 (the list *buffer-list*)))))
648
649 ;;; ADD-BUFFER-HISTORY-HOOK makes sure every buffer will be visited by
650 ;;; "Circulate Buffers" even if it has never been before.
651 ;;;
652 (defun add-buffer-history-hook (buffer)
653 (let ((ele (last *buffer-history*))
654 (new-stuff (list buffer)))
655 (if ele
656 (setf (cdr ele) new-stuff)
657 (setf *buffer-history* new-stuff))))
658 ;;;
659 (add-hook make-buffer-hook 'add-buffer-history-hook)
660
661 ;;; DELETE-BUFFER-HISTORY-HOOK makes sure we never end up in a dead buffer.
662 ;;;
663 (defun delete-buffer-history-hook (buffer)
664 (setq *buffer-history* (delq buffer *buffer-history*)))
665 ;;;
666 (add-hook delete-buffer-hook 'delete-buffer-history-hook)
667
668 (defun change-to-buffer (buffer)
669 "Switches to buffer in the current window maintaining *buffer-history*."
670 (setq *buffer-history*
671 (cons (current-buffer) (delq (current-buffer) *buffer-history*)))
672 (setf (current-buffer) buffer)
673 (setf (window-buffer (current-window)) buffer))
674
675 (defun delete-buffer-if-possible (buffer)
676 "Deletes a buffer if at all possible. If buffer is the only buffer, other
677 than the echo area, signals an error. Otherwise, find some recently current
678 buffer, and make all of buffer's windows display this recent buffer. If
679 buffer is current, set the current buffer to be this recently current
680 buffer."
681 (let ((new-buf (flet ((frob (b)
682 (or (eq b buffer) (eq b *echo-area-buffer*))))
683 (or (find-if-not #'frob (the list *buffer-history*))
684 (find-if-not #'frob (the list *buffer-list*))))))
685 (unless new-buf
686 (error "Cannot delete only buffer ~S." buffer))
687 (dolist (w (buffer-windows buffer))
688 (setf (window-buffer w) new-buf))
689 (when (eq buffer (current-buffer))
690 (setf (current-buffer) new-buf)))
691 (delete-buffer buffer))
692
693
694 (defvar *create-buffer-count* 0)
695
696 (defcommand "Create Buffer" (p &optional buffer-name)
697 "Create a new buffer. If a buffer with the specified name already exists,
698 then go to it."
699 "Create or go to the buffer with the specifed name."
700 (declare (ignore p))
701 (let ((name (or buffer-name
702 (prompt-for-buffer :prompt "Create Buffer: "
703 :default-string
704 (format nil "Buffer ~D"
705 (incf *create-buffer-count*))
706 :must-exist nil))))
707 (if (bufferp name)
708 (change-to-buffer name)
709 (change-to-buffer (or (getstring name *buffer-names*)
710 (make-buffer name))))))
711
712 (defcommand "Select Buffer" (p)
713 "Select a different buffer.
714 The buffer to go to is prompted for."
715 "Select a different buffer.
716 The buffer to go to is prompted for."
717 (declare (ignore p))
718 (let ((buf (prompt-for-buffer :prompt "Select Buffer: "
719 :default (previous-buffer))))
720 (when (eq buf *echo-area-buffer*)
721 (editor-error "Cannot select Echo Area buffer."))
722 (change-to-buffer buf)))
723
724
725 (defvar *buffer-history-ptr* ()
726 "The successively previous buffer to the current buffer.")
727
728 (defcommand "Select Previous Buffer" (p)
729 "Select the buffer selected before this one. If called repeatedly
730 with an argument, select the successively previous buffer to the
731 current one leaving the buffer history as it is."
732 "Select the buffer selected before this one."
733 (if p
734 (circulate-buffers-command nil)
735 (let ((b (previous-buffer)))
736 (unless b (editor-error "No previous buffer."))
737 (change-to-buffer b)
738 ;;
739 ;; If the pointer goes to nil, then "Circulate Buffers" will keep doing
740 ;; "Select Previous Buffer".
741 (setf *buffer-history-ptr* (cddr *buffer-history*))
742 (setf (last-command-type) :previous-buffer))))
743
744 (defcommand "Circulate Buffers" (p)
745 "Advance through buffer history, selecting successively previous buffer."
746 "Advance through buffer history, selecting successively previous buffer."
747 (declare (ignore p))
748 (if (and (eq (last-command-type) :previous-buffer)
749 *buffer-history-ptr*) ;Possibly nil if never CHANGE-TO-BUFFER.
750 (let ((b (pop *buffer-history-ptr*)))
751 (when (eq b (current-buffer))
752 (setf b (pop *buffer-history-ptr*)))
753 (unless b
754 (setf *buffer-history-ptr*
755 (or (cdr *buffer-history*) *buffer-history*))
756 (setf b (car *buffer-history*)))
757 (setf (current-buffer) b)
758 (setf (window-buffer (current-window)) b)
759 (setf (last-command-type) :previous-buffer))
760 (select-previous-buffer-command nil)))
761
762
763 (defcommand "Buffer Not Modified" (p)
764 "Make the current buffer not modified."
765 "Make the current buffer not modified."
766 (declare (ignore p))
767 (setf (buffer-modified (current-buffer)) nil)
768 (message "Buffer marked as unmodified."))
769
770 (defcommand "Check Buffer Modified" (p)
771 "Say whether the buffer is modified or not."
772 "Say whether the current buffer is modified or not."
773 (declare (ignore p))
774 (clear-echo-area)
775 (message "Buffer ~S ~:[is not~;is~] modified."
776 (buffer-name (current-buffer)) (buffer-modified (current-buffer))))
777
778 (defcommand "Set Buffer Read-Only" (p)
779 "Toggles the read-only flag for the current buffer."
780 "Toggles the read-only flag for the current buffer."
781 (declare (ignore p))
782 (let ((buffer (current-buffer)))
783 (message "Buffer ~S is now ~:[read-only~;writable~]."
784 (buffer-name buffer)
785 (setf (buffer-writable buffer) (not (buffer-writable buffer))))))
786
787 (defcommand "Set Buffer Writable" (p)
788 "Make the current buffer modifiable."
789 "Make the current buffer modifiable."
790 (declare (ignore p))
791 (let ((buffer (current-buffer)))
792 (setf (buffer-writable buffer) t)
793 (message "Buffer ~S is now writable." (buffer-name buffer))))
794
795 (defcommand "Kill Buffer" (p &optional buffer-name)
796 "Prompts for a buffer to delete.
797 If the buffer is modified, then let the user save the file before doing so.
798 When deleting the current buffer, prompts for a new buffer to select. If
799 a buffer other than the current one is deleted then any windows into it
800 are deleted."
801 "Delete buffer Buffer-Name, doing sensible things if the buffer is displayed
802 or current."
803 (declare (ignore p))
804 (let ((buffer (if buffer-name
805 (getstring buffer-name *buffer-names*)
806 (prompt-for-buffer :prompt "Kill Buffer: "
807 :default (current-buffer)))))
808 (if (not buffer)
809 (editor-error "No buffer named ~S" buffer-name))
810 (if (and (buffer-modified buffer)
811 (prompt-for-y-or-n :prompt "Save it first? "))
812 (save-file-command () buffer))
813 (if (eq buffer (current-buffer))
814 (let ((new (prompt-for-buffer :prompt "New Buffer: "
815 :default (previous-buffer)
816 :help "Buffer to change to after the current one is killed.")))
817 (when (eq new buffer)
818 (editor-error "You must select a different buffer."))
819 (dolist (w (buffer-windows buffer))
820 (setf (window-buffer w) new))
821 (setf (current-buffer) new))
822 (dolist (w (buffer-windows buffer))
823 (delete-window w)))
824 (delete-buffer buffer)))
825
826 (defcommand "Rename Buffer" (p)
827 "Change the current buffer's name.
828 The name, which is prompted for, defaults to the name of the associated
829 file."
830 "Change the name of the current buffer."
831 (declare (ignore p))
832 (let* ((buf (current-buffer))
833 (pn (buffer-pathname buf))
834 (name (if pn (pathname-to-buffer-name pn) (buffer-name buf)))
835 (new (prompt-for-string :prompt "New Name: "
836 :help "Give a new name for the current buffer"
837 :default name)))
838 (multiple-value-bind (entry foundp) (getstring new *buffer-names*)
839 (cond ((or (not foundp) (eq entry buf))
840 (setf (buffer-name buf) new))
841 (t (editor-error "Name ~S already in use." new))))))
842
843
844 (defcommand "Insert Buffer" (p)
845 "Insert the contents of a buffer.
846 The name of the buffer to insert is prompted for."
847 "Prompt for a buffer to insert at the point."
848 (declare (ignore p))
849 (let ((point (current-point))
850 (region (buffer-region (prompt-for-buffer
851 :default (previous-buffer)
852 :help
853 "Type the name of a buffer to insert."))))
854 ;;
855 ;; start and end will be deleted by undo stuff
856 (let ((save (region (copy-mark point :right-inserting)
857 (copy-mark point :left-inserting))))
858 (push-buffer-mark (copy-mark point))
859 (insert-region point region)
860 (make-region-undo :delete "Insert Buffer" save))))
861
862
863
864 ;;;; File utility commands:
865
866 (defcommand "Directory" (p)
867 "Do a directory into a pop-up window. If an argument is supplied, then
868 dot files are listed too (as with ls -a). Prompts for a pathname which
869 may contain wildcards in the name and type."
870 "Do a directory into a pop-up window."
871 (let* ((dpn (value pathname-defaults))
872 (pn (prompt-for-file
873 :prompt "Directory: "
874 :help "Pathname to do directory on."
875 :default (make-pathname :device (pathname-device dpn)
876 :directory (pathname-directory dpn))
877 :must-exist nil)))
878 (setf (value pathname-defaults) (merge-pathnames pn dpn))
879 (with-pop-up-display (s)
880 (print-directory pn s :all p))))
881
882 (defcommand "Verbose Directory" (p)
883 "Do a directory into a pop-up window. If an argument is supplied, then
884 dot files are listed too (as with ls -a). Prompts for a pathname which
885 may contain wildcards in the name and type."
886 "Do a directory into a pop-up window."
887 (let* ((dpn (value pathname-defaults))
888 (pn (prompt-for-file
889 :prompt "Verbose Directory: "
890 :help "Pathname to do directory on."
891 :default (make-pathname :device (pathname-device dpn)
892 :directory (pathname-directory dpn))
893 :must-exist nil)))
894 (setf (value pathname-defaults) (merge-pathnames pn dpn))
895 (with-pop-up-display (s)
896 (print-directory pn s :verbose t :all p))))
897
898
899
900 ;;;; Change log stuff:
901
902 (define-file-option "Log" (buffer value)
903 (defhvar "Log File Name"
904 "The name of the file for the change log for the file in this buffer."
905 :buffer buffer :value value))
906
907 (defhvar "Log Entry Template"
908 "The format string used to generate the template for a change-log entry.
909 Three arguments are given: the file, the date (create if available, now
910 otherwise) and the file author, or NIL if not available. The last \"@\"
911 is deleted and the point placed where it was."
912 :value "~A, ~A, Edit by ~:[???~;~:*~:(~A~)~].~% @~2%")
913
914 (defmode "Log"
915 :major-p t
916 :setup-function
917 #'(lambda (buffer)
918 (setf (buffer-minor-mode buffer "Fill") t))
919 :cleanup-function
920 #'(lambda (buffer)
921 (setf (buffer-minor-mode buffer "Fill") nil)))
922
923 (defhvar "Fill Prefix" "The fill prefix in Log mode."
924 :value " " :mode "Log")
925
926 (define-file-type-hook ("log") (buffer type)
927 (declare (ignore type))
928 (setf (buffer-major-mode buffer) "Log"))
929
930 (defun universal-time-to-string (ut)
931 (multiple-value-bind (sec min hour day month year)
932 (decode-universal-time ut)
933 (format nil "~2,'0D-~A-~2,'0D ~2,'0D:~2,'0D:~2,'0D"
934 day (svref '#("Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug"
935 "Sep" "Oct" "Nov" "Dec")
936 (1- month))
937 (rem year 100)
938 hour min sec)))
939
940 (defvar *back-to-@-pattern* (new-search-pattern :character :backward #\@))
941 (defcommand "Log Change" (p)
942 "Make an entry in the change-log file for this buffer.
943 Saves the file in the current buffer if it is modified, then finds the file
944 specified in the \"Log\" file option, adds the template for a change-log
945 entry at the beginning, then does a recursive edit, saving the log file on
946 exit."
947 "Find the change-log file as specified by \"Log File Name\" and edit it."
948 (declare (ignore p))
949 (unless (hemlock-bound-p 'log-file-name)
950 (editor-error "No log file defined."))
951 (let* ((buffer (current-buffer))
952 (pathname (buffer-pathname buffer)))
953 (when (or (buffer-modified buffer) (null pathname))
954 (save-file-command ()))
955 (unwind-protect
956 (progn
957 (find-file-command nil (merge-pathnames
958 (value log-file-name)
959 (buffer-default-pathname buffer)))
960 (let ((point (current-point)))
961 (buffer-start point)
962 (with-output-to-mark (s point :full)
963 (format s (value log-entry-template)
964 (namestring pathname)
965 (universal-time-to-string
966 (or (file-write-date pathname)
967 (get-universal-time)))
968 (file-author pathname)))
969 (when (find-pattern point *back-to-@-pattern*)
970 (delete-characters point 1)))
971 (do-recursive-edit)
972 (when (buffer-modified (current-buffer)) (save-file-command ())))
973 (if (member buffer *buffer-list* :test #'eq)
974 (change-to-buffer buffer)
975 (editor-error "Old buffer has been deleted.")))))
976
977
978
979 ;;;; Window hacking commands:
980
981 (defcommand "Next Window" (p)
982 "Change the current window to be the next window and the current buffer
983 to be it's buffer."
984 "Go to the next window.
985 If the next window is the bottom window then wrap around to the top window."
986 (declare (ignore p))
987 (let* ((next (next-window (current-window)))
988 (buffer (window-buffer next)))
989 (setf (current-buffer) buffer (current-window) next)))
990
991 (defcommand "Previous Window" (p)
992 "Change the current window to be the previous window and the current buffer
993 to be it's buffer."
994 "Go to the previous window.
995 If the Previous window is the top window then wrap around to the bottom."
996 (declare (ignore p))
997 (let* ((previous (previous-window (current-window)))
998 (buffer (window-buffer previous)))
999 (setf (current-buffer) buffer (current-window) previous)))
1000
1001 (defcommand "Split Window" (p)
1002 "Make a new window by splitting the current window.
1003 The new window is made the current window and displays starting at
1004 the same place as the current window."
1005 "Create a new window which displays starting at the same place
1006 as the current window."
1007 (declare (ignore p))
1008 (let ((new (make-window (window-display-start (current-window)))))
1009 (unless new (editor-error "Could not make a new window."))
1010 (setf (current-window) new)))
1011
1012 (defcommand "New Window" (p)
1013 "Make a new window and go to it.
1014 The window will display the same buffer as the current one."
1015 "Create a new window which displays starting at the same place
1016 as the current window."
1017 (declare (ignore p))
1018 (let ((new (make-window (window-display-start (current-window))
1019 :ask-user t)))
1020 (unless new (editor-error "Could not make a new window."))
1021 (setf (current-window) new)))
1022
1023 (defcommand "Delete Window" (p)
1024 "Delete the current window, going to the previous window."
1025 "Delete the window we are in, going to the previous window."
1026 (declare (ignore p))
1027 (when (= (length *window-list*) 2)
1028 (editor-error "Cannot delete only window."))
1029 (let ((window (current-window)))
1030 (previous-window-command nil)
1031 (delete-window window)))
1032
1033 (defcommand "Line to Top of Window" (p)
1034 "Move current line to top of window."
1035 "Move current line to top of window."
1036 (declare (ignore p))
1037 (with-mark ((mark (current-point)))
1038 (move-mark (window-display-start (current-window)) (line-start mark))))
1039
1040 (defcommand "Delete Next Window" (p)
1041 "Deletes the next window on display."
1042 "Deletes then next window on display."
1043 (declare (ignore p))
1044 (if (<= (length *window-list*) 2)
1045 (editor-error "Cannot delete only window")
1046 (delete-window (next-window (current-window)))))
1047
1048 (defcommand "Go to One Window" (p)
1049 "Deletes all windows leaving one with the \"Default Initial Window X\",
1050 \"Default Initial Window Y\", \"Default Initial Window Width\", and
1051 \"Default Initial Window Height\"."
1052 "Deletes all windows leaving one with the \"Default Initial Window X\",
1053 \"Default Initial Window Y\", \"Default Initial Window Width\", and
1054 \"Default Initial Window Height\"."
1055 (declare (ignore p))
1056 (let ((win (make-window (window-display-start (current-window))
1057 :ask-user t
1058 :x (value default-initial-window-x)
1059 :y (value default-initial-window-y)
1060 :width (value default-initial-window-width)
1061 :height (value default-initial-window-height))))
1062 (setf (current-window) win)
1063 (dolist (w *window-list*)
1064 (unless (or (eq w win)
1065 (eq w *echo-area-window*))
1066 (delete-window w)))))
1067
1068 (defcommand "Line to Center of Window" (p)
1069 "Moves current line to the center of the window."
1070 "Moves current line to the center of the window."
1071 (declare (ignore p))
1072 (center-window (current-window) (current-point)))

  ViewVC Help
Powered by ViewVC 1.1.5