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

Contents of /src/hemlock/diredcoms.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1.1.1 - (show annotations) (vendor branch)
Fri Jul 13 14:47:58 1990 UTC (23 years, 9 months ago) by ram
Changes since 1.1: +0 -2 lines
*** empty log message ***
1 ;;; -*- Log: hemlock.log; Package: Hemlock -*-
2 ;;;
3 ;;; **********************************************************************
4 ;;; This code was written as part of the Spice Lisp project at
5 ;;; Carnegie-Mellon University, and has been placed in the public domain.
6 ;;; Spice Lisp is currently incomplete and under active development.
7 ;;; If you want to use this code or any part of Spice Lisp, please contact
8 ;;; Scott Fahlman (FAHLMAN@CMUC).
9 ;;; **********************************************************************
10 ;;;
11 ;;; Simple directory editing support.
12 ;;; This file contains site dependent calls.
13 ;;;
14 ;;; Written by Blaine Burks and Bill Chiles.
15 ;;;
16
17 (in-package "HEMLOCK")
18
19
20 (defmode "Dired" :major-p t
21 :documentation
22 "Dired permits convenient directory browsing and file operations including
23 viewing, deleting, copying, renaming, and wildcard specifications.")
24
25
26 (defstruct (dired-information (:print-function print-dired-information)
27 (:conc-name dired-info-))
28 pathname ; Pathname of directory.
29 pattern ; FILE-NAMESTRING with wildcard possibly.
30 dot-files-p ; Whether to include UNIX dot files.
31 write-date ; Write date of directory.
32 files ; Simple-vector of dired-file structures.
33 file-list) ; List of pathnames for files, excluding directories.
34
35 (defun print-dired-information (obj str n)
36 (declare (ignore n))
37 (format str "#<Dired Info ~S>" (namestring (dired-info-pathname obj))))
38
39
40 (defstruct (dired-file (:print-function print-dired-file)
41 (:constructor make-dired-file (pathname)))
42 pathname
43 (deleted-p nil)
44 (write-date nil))
45
46 (defun print-dired-file (obj str n)
47 (declare (ignore n))
48 (format str "#<Dired-file ~A>" (namestring (dired-file-pathname obj))))
49
50
51
52 ;;;; "Dired" command.
53
54 ;;; *pathnames-to-dired-buffers* is an a-list mapping directory namestrings to
55 ;;; buffers that display their contents.
56 ;;;
57 (defvar *pathnames-to-dired-buffers* ())
58
59 (make-modeline-field
60 :name :dired-cmds :width 20
61 :function
62 #'(lambda (buffer window)
63 (declare (ignore buffer window))
64 " Type ? for help. "))
65
66 (defcommand "Dired" (p &optional directory)
67 "Prompt for a directory and edit it. If a dired for that directory already
68 exists, go to that buffer, otherwise create one. With an argument, include
69 UNIX dot files."
70 "Prompt for a directory and edit it. If a dired for that directory already
71 exists, go to that buffer, otherwise create one. With an argument, include
72 UNIX dot files."
73 (let ((info (if directory (value dired-information))))
74 (dired-guts nil
75 ;; Propagate dot-files property to subdirectory edits.
76 (or (and info (dired-info-dot-files-p info))
77 p)
78 directory)))
79
80 (defcommand "Dired with Pattern" (p)
81 "Do a dired, prompting for a pattern which may include a single *. With an
82 argument, include UNIX dit files."
83 "Do a dired, prompting for a pattern which may include a single *. With an
84 argument, include UNIX dit files."
85 (dired-guts t p nil))
86
87 (defun dired-guts (patternp dot-files-p directory)
88 (let* ((dpn (value pathname-defaults))
89 (directory (dired-directorify
90 (or directory
91 (prompt-for-file
92 :prompt "Edit Directory: "
93 :help "Pathname to edit."
94 :default (make-pathname
95 :device (pathname-device dpn)
96 :directory (pathname-directory dpn))
97 :must-exist nil))))
98 (pattern (if patternp
99 (prompt-for-string
100 :prompt "Filename pattern: "
101 :help "Type a filename with a single asterisk."
102 :trim t)))
103 (full-name (namestring (if pattern
104 (merge-pathnames directory pattern)
105 directory)))
106 (name (concatenate 'simple-string "Dired " full-name))
107 (buffer (cdr (assoc full-name *pathnames-to-dired-buffers*
108 :test #'string=))))
109 (declare (simple-string full-name))
110 (setf (value pathname-defaults) (merge-pathnames directory dpn))
111 (change-to-buffer
112 (cond (buffer
113 (when (and dot-files-p
114 (not (dired-info-dot-files-p
115 (variable-value 'dired-information
116 :buffer buffer))))
117 (setf (dired-info-dot-files-p (variable-value 'dired-information
118 :buffer buffer))
119 t)
120 (update-dired-buffer directory pattern buffer))
121 buffer)
122 (t
123 (let ((buffer (make-buffer
124 name :modes '("Dired")
125 :modeline-fields
126 (append (value default-modeline-fields)
127 (list (modeline-field :dired-cmds)))
128 :delete-hook (list 'dired-buffer-delete-hook))))
129 (unless (initialize-dired-buffer directory pattern
130 dot-files-p buffer)
131 (delete-buffer-if-possible buffer)
132 (editor-error "No entries for ~A." full-name))
133 (push (cons full-name buffer) *pathnames-to-dired-buffers*)
134 buffer))))))
135
136 ;;; INITIALIZE-DIRED-BUFFER gets a dired in the buffer and defines some
137 ;;; variables to make it usable as a dired buffer. If there are no file
138 ;;; satisfying directory, then this returns nil, otherwise t.
139 ;;;
140 (defun initialize-dired-buffer (directory pattern dot-files-p buffer)
141 (multiple-value-bind (pathnames dired-files)
142 (dired-in-buffer directory pattern dot-files-p buffer)
143 (if (zerop (length dired-files))
144 nil
145 (defhvar "Dired Information"
146 "Contains the information neccessary to manipulate dired buffers."
147 :buffer buffer
148 :value (make-dired-information :pathname directory
149 :pattern pattern
150 :dot-files-p dot-files-p
151 :write-date (file-write-date directory)
152 :files dired-files
153 :file-list pathnames)))))
154
155 ;;; CALL-PRINT-DIRECTORY gives us a nice way to report PRINT-DIRECTORY errors
156 ;;; to the user and to clean up the dired buffer.
157 ;;;
158 (defun call-print-directory (directory mark dot-files-p)
159 (handler-case (with-output-to-mark (s mark :full)
160 (print-directory directory s
161 :all dot-files-p :verbose t :return-list t))
162 (error (condx)
163 (delete-buffer-if-possible (line-buffer (mark-line mark)))
164 (editor-error "~A" condx))))
165
166 ;;; DIRED-BUFFER-DELETE-HOOK is called on dired buffers upon deletion. This
167 ;;; removes the buffer from the pathnames mapping, and it deletes and buffer
168 ;;; local variables referring to it.
169 ;;;
170 (defun dired-buffer-delete-hook (buffer)
171 (setf *pathnames-to-dired-buffers*
172 (delete buffer *pathnames-to-dired-buffers* :test #'eq :key #'cdr)))
173
174
175
176 ;;;; Dired deletion and undeletion.
177
178 (defcommand "Dired Delete File" (p)
179 "Marks a file for deletion; signals an error if not in a dired buffer.
180 With an argument, this prompts for a pattern that may contain at most one
181 wildcard, an asterisk, and all names matching the pattern will be flagged
182 for deletion."
183 "Marks a file for deletion; signals an error if not in a dired buffer."
184 (dired-frob-deletion p t))
185
186 (defcommand "Dired Undelete File" (p)
187 "Removes a mark for deletion; signals and error if not in a dired buffer.
188 With an argument, this prompts for a pattern that may contain at most one
189 wildcard, an asterisk, and all names matching the pattern will be unflagged
190 for deletion."
191 "Removes a mark for deletion; signals and error if not in a dired buffer."
192 (dired-frob-deletion p nil))
193
194 (defcommand "Dired Delete File and Down Line" (p)
195 "Marks file for deletion and moves down a line.
196 See \"Dired Delete File\"."
197 "Marks file for deletion and moves down a line.
198 See \"Dired Delete File\"."
199 (declare (ignore p))
200 (dired-frob-deletion nil t)
201 (dired-down-line (current-point)))
202
203 (defcommand "Dired Undelete File and Down Line" (p)
204 "Marks file undeleted and moves down a line.
205 See \"Dired Delete File\"."
206 "Marks file undeleted and moves down a line.
207 See \"Dired Delete File\"."
208 (declare (ignore p))
209 (dired-frob-deletion nil nil)
210 (dired-down-line (current-point)))
211
212 (defcommand "Dired Delete File with Pattern" (p)
213 "Prompts for a pattern and marks matching files for deletion.
214 See \"Dired Delete File\"."
215 "Prompts for a pattern and marks matching files for deletion.
216 See \"Dired Delete File\"."
217 (declare (ignore p))
218 (dired-frob-deletion t t)
219 (dired-down-line (current-point)))
220
221 (defcommand "Dired Undelete File with Pattern" (p)
222 "Prompts for a pattern and marks matching files undeleted.
223 See \"Dired Delete File\"."
224 "Prompts for a pattern and marks matching files undeleted.
225 See \"Dired Delete File\"."
226 (declare (ignore p))
227 (dired-frob-deletion t nil)
228 (dired-down-line (current-point)))
229
230 ;;; DIRED-FROB-DELETION takes arguments indicating whether to prompt for a
231 ;;; pattern and whether to mark the file deleted or undeleted. This uses
232 ;;; CURRENT-POINT and CURRENT-BUFFER, and if not in a dired buffer, signal
233 ;;; an error.
234 ;;;
235 (defun dired-frob-deletion (patternp deletep)
236 (unless (hemlock-bound-p 'dired-information)
237 (editor-error "Not in Dired buffer."))
238 (with-mark ((mark (current-point) :left-inserting))
239 (let* ((dir-info (value dired-information))
240 (files (dired-info-files dir-info))
241 (del-files
242 (if patternp
243 (dired:pathnames-from-pattern
244 (prompt-for-string
245 :prompt "Filename pattern: "
246 :help "Type a filename with a single asterisk."
247 :trim t)
248 (dired-info-file-list dir-info))
249 (list (dired-file-pathname
250 (array-element-from-mark mark files)))))
251 (note-char (if deletep #\D #\space)))
252 (with-writable-buffer ((current-buffer))
253 (dolist (f del-files)
254 (let* ((pos (position f files :test #'equal
255 :key #'dired-file-pathname))
256 (dired-file (svref files pos)))
257 (buffer-start mark)
258 (line-offset mark pos 0)
259 (setf (dired-file-deleted-p dired-file) deletep)
260 (if deletep
261 (setf (dired-file-write-date dired-file)
262 (file-write-date (dired-file-pathname dired-file)))
263 (setf (dired-file-write-date dired-file) nil))
264 (setf (next-character mark) note-char)))))))
265
266 (defun dired-down-line (point)
267 (line-offset point 1)
268 (when (blank-line-p (mark-line point))
269 (line-offset point -1)))
270
271
272
273 ;;;; Dired file finding and going to dired buffers.
274
275 (defcommand "Dired Edit File" (p)
276 "Read in file or recursively \"Dired\" a directory."
277 "Read in file or recursively \"Dired\" a directory."
278 (declare (ignore p))
279 (let ((point (current-point)))
280 (when (blank-line-p (mark-line point)) (editor-error "Not on a file line."))
281 (let ((pathname (dired-file-pathname
282 (array-element-from-mark
283 point (dired-info-files (value dired-information))))))
284 (if (directoryp pathname)
285 (dired-command nil (directory-namestring pathname))
286 (change-to-buffer (find-file-buffer pathname))))))
287
288 (defcommand "Dired View File" (p)
289 "Read in file as if by \"View File\" or recursively \"Dired\" a directory.
290 This associates the file's buffer with the dired buffer."
291 "Read in file as if by \"View File\".
292 This associates the file's buffer with the dired buffer."
293 (declare (ignore p))
294 (let ((point (current-point)))
295 (when (blank-line-p (mark-line point)) (editor-error "Not on a file line."))
296 (let ((pathname (dired-file-pathname
297 (array-element-from-mark
298 point (dired-info-files (value dired-information))))))
299 (if (directoryp pathname)
300 (dired-command nil (directory-namestring pathname))
301 (let* ((dired-buf (current-buffer))
302 (buffer (view-file-command nil pathname)))
303 (push #'(lambda (buffer)
304 (declare (ignore buffer))
305 (setf dired-buf nil))
306 (buffer-delete-hook dired-buf))
307 (setf (variable-value 'view-return-function :buffer buffer)
308 #'(lambda ()
309 (if dired-buf
310 (change-to-buffer dired-buf)
311 (dired-from-buffer-pathname-command nil)))))))))
312
313 (defcommand "Dired from Buffer Pathname" (p)
314 "Invokes \"Dired\" on the directory part of the current buffer's pathname.
315 With an argument, also prompt for a file pattern within that directory."
316 "Invokes \"Dired\" on the directory part of the current buffer's pathname.
317 With an argument, also prompt for a file pattern within that directory."
318 (let ((pathname (buffer-pathname (current-buffer))))
319 (if pathname
320 (dired-command p (directory-namestring pathname))
321 (editor-error "No pathname associated with buffer."))))
322
323
324
325 ;;;; Dired misc. commands -- update, help, line motion.
326
327 (defcommand "Dired Update Buffer" (p)
328 "Recompute the contents of a dired buffer.
329 This maintains delete flags for files that have not been modified."
330 "Recompute the contents of a dired buffer.
331 This maintains delete flags for files that have not been modified."
332 (declare (ignore p))
333 (unless (hemlock-bound-p 'dired-information)
334 (editor-error "Not in Dired buffer."))
335 (let ((buffer (current-buffer))
336 (dir-info (value dired-information)))
337 (update-dired-buffer (dired-info-pathname dir-info)
338 (dired-info-pattern dir-info)
339 buffer)))
340
341 ;;; UPDATE-DIRED-BUFFER updates buffer with a dired of directory, deleting
342 ;;; whatever is in the buffer already. This assumes buffer was previously
343 ;;; used as a dired buffer having necessary variables bound. The new files
344 ;;; are compared to the old ones propagating any deleted flags if the name
345 ;;; and the write date is the same for both specifications.
346 ;;;
347 (defun update-dired-buffer (directory pattern buffer)
348 (with-writable-buffer (buffer)
349 (delete-region (buffer-region buffer))
350 (let ((dir-info (variable-value 'dired-information :buffer buffer)))
351 (multiple-value-bind (pathnames new-dired-files)
352 (dired-in-buffer directory pattern
353 (dired-info-dot-files-p dir-info)
354 buffer)
355 (let ((point (buffer-point buffer))
356 (old-dired-files (dired-info-files dir-info)))
357 (declare (simple-vector old-dired-files))
358 (dotimes (i (length old-dired-files))
359 (let ((old-file (svref old-dired-files i)))
360 (when (dired-file-deleted-p old-file)
361 (let ((pos (position (dired-file-pathname old-file)
362 new-dired-files :test #'equal
363 :key #'dired-file-pathname)))
364 (when pos
365 (let* ((new-file (svref new-dired-files pos))
366 (write-date (file-write-date
367 (dired-file-pathname new-file))))
368 (when (= (dired-file-write-date old-file) write-date)
369 (setf (dired-file-deleted-p new-file) t)
370 (setf (dired-file-write-date new-file) write-date)
371 (setf (next-character
372 (line-offset (buffer-start point) pos 0))
373 #\D))))))))
374 (setf (dired-info-files dir-info) new-dired-files)
375 (setf (dired-info-file-list dir-info) pathnames)
376 (setf (dired-info-write-date dir-info)
377 (file-write-date directory))
378 (move-mark point (buffer-start-mark buffer)))))))
379
380 ;;; DIRED-IN-BUFFER inserts a dired listing of directory in buffer returning
381 ;;; two values: a list of pathnames of files only, and an array of dired-file
382 ;;; structures. This uses FILTER-REGION to insert a space for the indication
383 ;;; of whether the file is flagged for deletion. Then we clean up extra header
384 ;;; and trailing lines known to be in the output (into every code a little
385 ;;; slime must fall).
386 ;;;
387 (defun dired-in-buffer (directory pattern dot-files-p buffer)
388 (let ((point (buffer-point buffer)))
389 (with-writable-buffer (buffer)
390 (let* ((pathnames (call-print-directory
391 (if pattern
392 (merge-pathnames directory pattern)
393 directory)
394 point
395 dot-files-p))
396 (dired-files (make-array (length pathnames))))
397 (declare (list pathnames) (simple-vector dired-files))
398 (filter-region #'(lambda (str)
399 (concatenate 'simple-string " " str))
400 (buffer-region buffer))
401 (delete-characters point -2)
402 (delete-region (line-to-region (mark-line (buffer-start point))))
403 (delete-characters point)
404 (do ((p pathnames (cdr p))
405 (i 0 (1+ i)))
406 ((null p))
407 (setf (svref dired-files i) (make-dired-file (car p))))
408 (values (delete-if #'directoryp pathnames) dired-files)))))
409
410
411 (defcommand "Dired Help" (p)
412 "How to use dired."
413 "How to use dired."
414 (declare (ignore p))
415 (describe-mode-command nil "Dired"))
416
417 (defcommand "Dired Next File" (p)
418 "Moves to next undeleted file."
419 "Moves to next undeleted file."
420 (unless (dired-line-offset (current-point) (or p 1))
421 (editor-error "Not enough lines.")))
422
423 (defcommand "Dired Previous File" (p)
424 "Moves to previous undeleted file."
425 "Moves to next undeleted file."
426 (unless (dired-line-offset (current-point) (or p -1))
427 (editor-error "Not enough lines.")))
428
429 ;;; DIRED-LINE-OFFSET moves mark n undeleted file lines, returning mark. If
430 ;;; there are not enough lines, mark remains unmoved, this returns nil.
431 ;;;
432 (defun dired-line-offset (mark n)
433 (with-mark ((m mark))
434 (let ((step (if (plusp n) 1 -1)))
435 (dotimes (i (abs n) (move-mark mark m))
436 (loop
437 (unless (line-offset m step 0)
438 (return-from dired-line-offset nil))
439 (when (blank-line-p (mark-line m))
440 (return-from dired-line-offset nil))
441 (when (char= (next-character m) #\space)
442 (return)))))))
443
444
445
446 ;;;; Dired user interaction functions.
447
448 (defun dired-error-function (string &rest args)
449 (apply #'editor-error string args))
450
451 (defun dired-report-function (string &rest args)
452 (clear-echo-area)
453 (apply #'message string args))
454
455 (defun dired-yesp-function (string &rest args)
456 (prompt-for-y-or-n :prompt (cons string args) :default t))
457
458
459
460 ;;;; Dired expunging and quitting.
461
462 (defcommand "Dired Expunge Files" (p)
463 "Expunges files marked for deletion.
464 Query the user if value of \"Dired File Expunge Confirm\" is non-nil. Do
465 the same with directories and the value of \"Dired Directory Expunge
466 Confirm\"."
467 "Expunges files marked for deletion.
468 Query the user if value of \"Dired File Expunge Confirm\" is non-nil. Do
469 the same with directories and the value of \"Dired Directory Expunge
470 Confirm\"."
471 (declare (ignore p))
472 (when (expunge-dired-files)
473 (dired-update-buffer-command nil))
474 (maintain-dired-consistency))
475
476 (defcommand "Dired Quit" (p)
477 "Expunges the files in a dired buffer and then exits."
478 "Expunges the files in a dired buffer and then exits."
479 (declare (ignore p))
480 (expunge-dired-files)
481 (delete-buffer-if-possible (current-buffer)))
482
483 (defhvar "Dired File Expunge Confirm"
484 "When set (the default), \"Dired Expunge Files\" and \"Dired Quit\" will ask
485 for confirmation before deleting the marked files."
486 :value t)
487
488 (defhvar "Dired Directory Expunge Confirm"
489 "When set (the default), \"Dired Expunge Files\" and \"Dired Quit\" will ask
490 for confirmation before deleting each marked directory."
491 :value t)
492
493 (defun expunge-dired-files ()
494 (multiple-value-bind (marked-files marked-dirs) (get-marked-dired-files)
495 (let ((dired:*error-function* #'dired-error-function)
496 (dired:*report-function* #'dired-report-function)
497 (dired:*yesp-function* #'dired-yesp-function)
498 (we-did-something nil))
499 (when (and marked-files
500 (or (not (value dired-file-expunge-confirm))
501 (prompt-for-y-or-n :prompt "Really delete files? "
502 :default t
503 :must-exist t
504 :default-string "Y")))
505 (setf we-did-something t)
506 (dolist (file-info marked-files)
507 (let ((pathname (car file-info))
508 (write-date (cdr file-info)))
509 (if (= write-date (file-write-date pathname))
510 (dired:delete-file (namestring pathname) :clobber t
511 :recursive nil)
512 (message "~A has been modified, it remains unchanged."
513 (namestring pathname))))))
514 (when marked-dirs
515 (dolist (dir-info marked-dirs)
516 (let ((dir (car dir-info))
517 (write-date (cdr dir-info)))
518 (if (= write-date (file-write-date dir))
519 (when (or (not (value dired-directory-expunge-confirm))
520 (prompt-for-y-or-n
521 :prompt (list "~a is a directory. Delete it? "
522 (directory-namestring dir))
523 :default t
524 :must-exist t
525 :default-string "Y"))
526 (dired:delete-file (directory-namestring dir) :clobber t
527 :recursive t)
528 (setf we-did-something t))
529 (message "~A has been modified, it remains unchanged.")))))
530 we-did-something)))
531
532
533
534 ;;;; Dired copying and renaming.
535
536 (defhvar "Dired Copy File Confirm"
537 "Can be either t, nil, or :update. T means always query before clobbering an
538 existing file, nil means don't query before clobbering an existing file, and
539 :update means only ask if the existing file is newer than the source."
540 :value T)
541
542 (defhvar "Dired Rename File Confirm"
543 "When non-nil, dired will query before clobbering an existing file."
544 :value T)
545
546 (defcommand "Dired Copy File" (p)
547 "Copy the file under the point"
548 "Copy the file under the point"
549 (declare (ignore p))
550 (let* ((point (current-point))
551 (confirm (value dired-copy-file-confirm))
552 (source (dired-file-pathname
553 (array-element-from-mark
554 point (dired-info-files (value dired-information)))))
555 (dest (prompt-for-file
556 :prompt (if (directoryp source)
557 "Destination Directory Name: "
558 "Destination Filename: ")
559 :help "Name of new file."
560 :default source
561 :must-exist nil))
562 (dired:*error-function* #'dired-error-function)
563 (dired:*report-function* #'dired-report-function)
564 (dired:*yesp-function* #'dired-yesp-function))
565 (dired:copy-file source dest :update (if (eq confirm :update) t nil)
566 :clobber (not confirm)))
567 (maintain-dired-consistency))
568
569 (defcommand "Dired Rename File" (p)
570 "Rename the file or directory under the point"
571 "Rename the file or directory under the point"
572 (declare (ignore p))
573 (let* ((point (current-point))
574 (source (dired-namify (dired-file-pathname
575 (array-element-from-mark
576 point
577 (dired-info-files (value dired-information))))))
578 (dest (prompt-for-file
579 :prompt "New Filename: "
580 :help "The new name for this file."
581 :default source
582 :must-exist nil))
583 (dired:*error-function* #'dired-error-function)
584 (dired:*report-function* #'dired-report-function)
585 (dired:*yesp-function* #'dired-yesp-function))
586 ;; ARRAY-ELEMENT-FROM-MARK moves mark to line start.
587 (dired:rename-file source dest :clobber (value dired-rename-file-confirm)))
588 (maintain-dired-consistency))
589
590 (defcommand "Dired Copy with Wildcard" (p)
591 "Copy files that match a pattern containing ONE wildcard."
592 "Copy files that match a pattern containing ONE wildcard."
593 (declare (ignore p))
594 (let* ((dir-info (value dired-information))
595 (confirm (value dired-copy-file-confirm))
596 (pattern (prompt-for-string
597 :prompt "Filename pattern: "
598 :help "Type a filename with a single asterisk."
599 :trim t))
600 (destination (namestring
601 (prompt-for-file
602 :prompt "Destination Spec: "
603 :help "Destination spec. May contain ONE asterisk."
604 :default (dired-info-pathname dir-info)
605 :must-exist nil)))
606 (dired:*error-function* #'dired-error-function)
607 (dired:*yesp-function* #'dired-yesp-function)
608 (dired:*report-function* #'dired-report-function))
609 (dired:copy-file pattern destination :update (if (eq confirm :update) t nil)
610 :clobber (not confirm)
611 :directory (dired-info-file-list dir-info)))
612 (maintain-dired-consistency))
613
614 (defcommand "Dired Rename with Wildcard" (p)
615 "Rename files that match a pattern containing ONE wildcard."
616 "Rename files that match a pattern containing ONE wildcard."
617 (declare (ignore p))
618 (let* ((dir-info (value dired-information))
619 (pattern (prompt-for-string
620 :prompt "Filename pattern: "
621 :help "Type a filename with a single asterisk."
622 :trim t))
623 (destination (namestring
624 (prompt-for-file
625 :prompt "Destination Spec: "
626 :help "Destination spec. May contain ONE asterisk."
627 :default (dired-info-pathname dir-info)
628 :must-exist nil)))
629 (dired:*error-function* #'dired-error-function)
630 (dired:*yesp-function* #'dired-yesp-function)
631 (dired:*report-function* #'dired-report-function))
632 (dired:rename-file pattern destination
633 :clobber (not (value dired-rename-file-confirm))
634 :directory (dired-info-file-list dir-info)))
635 (maintain-dired-consistency))
636
637 (defcommand "Delete File" (p)
638 "Delete a file. Specify directories with a trailing slash."
639 "Delete a file. Specify directories with a trailing slash."
640 (declare (ignore p))
641 (let* ((spec (namestring
642 (prompt-for-file
643 :prompt "Delete File: "
644 :help '("Name of File or Directory to delete. ~
645 One wildcard is permitted.")
646 :must-exist nil)))
647 (directoryp (directoryp spec))
648 (dired:*error-function* #'dired-error-function)
649 (dired:*report-function* #'dired-report-function)
650 (dired:*yesp-function* #'dired-yesp-function))
651 (when (or (not directoryp)
652 (not (value dired-directory-expunge-confirm))
653 (prompt-for-y-or-n
654 :prompt (list "~A is a directory. Delete it? "
655 (directory-namestring spec))
656 :default t :must-exist t :default-string "Y")))
657 (dired:delete-file spec :recursive t
658 :clobber (or directoryp
659 (value dired-file-expunge-confirm))))
660 (maintain-dired-consistency))
661
662 (defcommand "Copy File" (p)
663 "Copy a file, allowing ONE wildcard."
664 "Copy a file, allowing ONE wildcard."
665 (declare (ignore p))
666 (let* ((confirm (value dired-copy-file-confirm))
667 (source (namestring
668 (prompt-for-file
669 :prompt "Source Filename: "
670 :help "Name of File to copy. One wildcard is permitted."
671 :must-exist nil)))
672 (dest (namestring
673 (prompt-for-file
674 :prompt (if (directoryp source)
675 "Destination Directory Name: "
676 "Destination Filename: ")
677 :help "Name of new file."
678 :default source
679 :must-exist nil)))
680 (dired:*error-function* #'dired-error-function)
681 (dired:*report-function* #'dired-report-function)
682 (dired:*yesp-function* #'dired-yesp-function))
683 (dired:copy-file source dest :update (if (eq confirm :update) t nil)
684 :clobber (not confirm)))
685 (maintain-dired-consistency))
686
687 (defcommand "Rename File" (p)
688 "Rename a file, allowing ONE wildcard."
689 "Rename a file, allowing ONE wildcard."
690 (declare (ignore p))
691 (let* ((source (namestring
692 (prompt-for-file
693 :prompt "Source Filename: "
694 :help "Name of file to rename. One wildcard is permitted."
695 :must-exist nil)))
696 (dest (namestring
697 (prompt-for-file
698 :prompt (if (directoryp source)
699 "Destination Directory Name: "
700 "Destination Filename: ")
701 :help "Name of new file."
702 :default source
703 :must-exist nil)))
704 (dired:*error-function* #'dired-error-function)
705 (dired:*report-function* #'dired-report-function)
706 (dired:*yesp-function* #'dired-yesp-function))
707 (dired:rename-file source dest
708 :clobber (not (value dired-rename-file-confirm))))
709 (maintain-dired-consistency))
710
711 (defun maintain-dired-consistency ()
712 (dolist (info *pathnames-to-dired-buffers*)
713 (let* ((directory (directory-namestring (car info)))
714 (buffer (cdr info))
715 (dir-info (variable-value 'dired-information :buffer buffer))
716 (write-date (file-write-date directory)))
717 (unless (= (dired-info-write-date dir-info) write-date)
718 (update-dired-buffer directory (dired-info-pattern dir-info) buffer)))))
719
720
721
722 ;;;; Dired utilities.
723
724 ;;; GET-MARKED-DIRED-FILES returns as multiple values a list of file specs
725 ;;; and a list of directory specs that have been marked for deletion. This
726 ;;; assumes the current buffer is a "Dired" buffer.
727 ;;;
728 (defun get-marked-dired-files ()
729 (let* ((files (dired-info-files (value dired-information)))
730 (length (length files))
731 (marked-files ())
732 (marked-dirs ()))
733 (unless files (editor-error "Not in Dired buffer."))
734 (do ((i 0 (1+ i)))
735 ((= i length) (values (nreverse marked-files) (nreverse marked-dirs)))
736 (let* ((thing (svref files i))
737 (pathname (dired-file-pathname thing)))
738 (when (dired-file-deleted-p thing)
739 (if (directoryp pathname)
740 (push (cons pathname (file-write-date pathname)) marked-dirs)
741 (push (cons pathname (file-write-date pathname))
742 marked-files)))))))
743
744 ;;; ARRAY-ELEMENT-FROM-MARK counts the lines between it and the beginning
745 ;;; of the buffer. The number is used to index vector as if each line
746 ;;; mapped to an element starting with the zero'th element (lines are
747 ;;; numbered starting at 1).
748 ;;;
749 (defun array-element-from-mark (mark vector
750 &optional (error-msg "Invalid line."))
751 (when (blank-line-p (mark-line mark)) (editor-error error-msg))
752 (svref vector
753 (1- (count-lines (region
754 (buffer-start-mark (line-buffer (mark-line mark)))
755 mark)))))
756
757 ;;; DIRED-NAMIFY and DIRED-DIRECTORIFY are implementation dependent slime.
758 ;;;
759 (defun dired-namify (pathname)
760 (let* ((string (namestring pathname))
761 (last (1- (length string))))
762 (if (char= (schar string last) #\/)
763 (subseq string 0 last)
764 string)))
765 ;;;
766 ;;; This is necessary to derive a canonical representation for directory
767 ;;; names, so "Dired" can map various strings naming one directory to that
768 ;;; one directory.
769 ;;;
770 (defun dired-directorify (pathname)
771 (let ((directory (lisp::predict-name pathname t)))
772 (if (directoryp directory)
773 directory
774 (pathname (concatenate 'simple-string (namestring directory) "/")))))
775
776
777
778 ;;;; View Mode.
779
780 (defmode "View" :major-p nil
781 :setup-function 'setup-view-mode
782 :cleanup-function 'cleanup-view-mode
783 :precedence 5.0
784 :documentation
785 "View mode scrolls forwards and backwards in a file with the buffer read-only.
786 Scrolling off the end optionally deletes the buffer.")
787
788 (defun setup-view-mode (buffer)
789 (defhvar "View Return Function"
790 "Function that gets called when quitting or returning from view mode."
791 :value nil
792 :buffer buffer)
793 (setf (buffer-writable buffer) nil))
794 ;;;
795 (defun cleanup-view-mode (buffer)
796 (delete-variable 'view-return-function :buffer buffer)
797 (setf (buffer-writable buffer) t))
798
799 (defcommand "View File" (p &optional pathname)
800 "Reads a file in as if by \"Find File\", but read-only. Commands exist
801 for scrolling convenience."
802 "Reads a file in as if by \"Find File\", but read-only. Commands exist
803 for scrolling convenience."
804 (declare (ignore p))
805 (let* ((pn (or pathname
806 (prompt-for-file
807 :prompt "View File: " :must-exist t
808 :help "Name of existing file to read into its own buffer."
809 :default (buffer-default-pathname (current-buffer)))))
810 (buffer (make-buffer (format nil "View File ~A" (gensym)))))
811 (visit-file-command nil pn buffer)
812 (setf (buffer-minor-mode buffer "View") t)
813 (change-to-buffer buffer)
814 buffer))
815
816 (defcommand "View Return" (p)
817 "Return to a parent buffer, if it exists."
818 "Return to a parent buffer, if it exists."
819 (declare (ignore p))
820 (unless (call-view-return-fun)
821 (editor-error "No View return method for this buffer.")))
822
823 (defcommand "View Quit" (p)
824 "Delete a buffer in view mode."
825 "Delete a buffer in view mode, invoking VIEW-RETURN-FUNCTION if it exists for
826 this buffer."
827 (declare (ignore p))
828 (let* ((buf (current-buffer))
829 (funp (call-view-return-fun)))
830 (delete-buffer-if-possible buf)
831 (unless funp (editor-error "No View return method for this buffer."))))
832
833 ;;; CALL-VIEW-RETURN-FUN returns nil if there is no current
834 ;;; view-return-function. If there is one, it calls it and returns t.
835 ;;;
836 (defun call-view-return-fun ()
837 (if (hemlock-bound-p 'view-return-function)
838 (let ((fun (value view-return-function)))
839 (cond (fun
840 (funcall fun)
841 t)))))
842
843
844 (defhvar "View Scroll Deleting Buffer"
845 "When this is set, \"View Scroll Down\" deletes the buffer when the end
846 of the file is visible."
847 :value t)
848
849 (defcommand "View Scroll Down" (p)
850 "Scroll the current window down through its buffer.
851 If the end of the file is visible, then delete the buffer if \"View Scroll
852 Deleting Buffer\" is set. If the buffer is associated with a dired buffer,
853 this returns there instead of to the previous buffer."
854 "Scroll the current window down through its buffer.
855 If the end of the file is visible, then delete the buffer if \"View Scroll
856 Deleting Buffer\" is set. If the buffer is associated with a dired buffer,
857 this returns there instead of to the previous buffer."
858 (if (and (not p)
859 (displayed-p (buffer-end-mark (current-buffer))
860 (current-window))
861 (value view-scroll-deleting-buffer))
862 (view-quit-command nil)
863 (scroll-window-down-command p)))
864
865 (defcommand "View Edit File" (p)
866 "Turn off \"View\" mode in this buffer."
867 "Turn off \"View\" mode in this buffer."
868 (declare (ignore p))
869 (let ((buf (current-buffer)))
870 (setf (buffer-minor-mode buf "View") nil)
871 (warn-about-visit-file-buffers buf)))
872
873 (defcommand "View Help" (p)
874 "Shows \"View\" mode help message."
875 "Shows \"View\" mode help message."
876 (declare (ignore p))
877 (describe-mode-command nil "View"))

  ViewVC Help
Powered by ViewVC 1.1.5