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

Contents of /src/hemlock/diredcoms.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5