/[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.6 - (show annotations) (vendor branch)
Fri Jul 26 10:17:14 1991 UTC (22 years, 9 months ago) by chiles
Changes since 1.1.1.5: +8 -6 lines
Changed ARRAY-ELEMENT-FROM-MARK to use AREF for the Netnews stuff.  I
documented this to be an internal interface since a few other modes use it.
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.1.1.6 1991/07/26 10:17:14 chiles 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 (declare (simple-vector dirs))
338 (dired-command nil
339 (make-pathname
340 :device :absolute
341 :directory (subseq dirs 0 (1- (length dirs)))))))
342
343 (defcommand "Dired Up Directory" (p)
344 "Invokes \"Dired\" on the directory up one level from the current Dired
345 buffer."
346 "Invokes \"Dired\" on the directory up one level from the current Dired
347 buffer."
348 (declare (ignore p))
349 (unless (hemlock-bound-p 'dired-information)
350 (editor-error "Not in Dired buffer."))
351 (let ((dirs (pathname-directory
352 (dired-info-pathname (value dired-information)))))
353 (declare (simple-vector dirs))
354 (dired-command nil
355 (make-pathname
356 :device :absolute
357 :directory (subseq dirs 0 (1- (length dirs)))))))
358
359
360
361 ;;;; Dired misc. commands -- update, help, line motion.
362
363 (defcommand "Dired Update Buffer" (p)
364 "Recompute the contents of a dired buffer.
365 This maintains delete flags for files that have not been modified."
366 "Recompute the contents of a dired buffer.
367 This maintains delete flags for files that have not been modified."
368 (declare (ignore p))
369 (unless (hemlock-bound-p 'dired-information)
370 (editor-error "Not in Dired buffer."))
371 (let ((buffer (current-buffer))
372 (dir-info (value dired-information)))
373 (update-dired-buffer (dired-info-pathname dir-info)
374 (dired-info-pattern dir-info)
375 buffer)))
376
377 ;;; UPDATE-DIRED-BUFFER updates buffer with a dired of directory, deleting
378 ;;; whatever is in the buffer already. This assumes buffer was previously
379 ;;; used as a dired buffer having necessary variables bound. The new files
380 ;;; are compared to the old ones propagating any deleted flags if the name
381 ;;; and the write date is the same for both specifications.
382 ;;;
383 (defun update-dired-buffer (directory pattern buffer)
384 (with-writable-buffer (buffer)
385 (delete-region (buffer-region buffer))
386 (let ((dir-info (variable-value 'dired-information :buffer buffer)))
387 (multiple-value-bind (pathnames new-dired-files)
388 (dired-in-buffer directory pattern
389 (dired-info-dot-files-p dir-info)
390 buffer)
391 (let ((point (buffer-point buffer))
392 (old-dired-files (dired-info-files dir-info)))
393 (declare (simple-vector old-dired-files))
394 (dotimes (i (length old-dired-files))
395 (let ((old-file (svref old-dired-files i)))
396 (when (dired-file-deleted-p old-file)
397 (let ((pos (position (dired-file-pathname old-file)
398 new-dired-files :test #'equal
399 :key #'dired-file-pathname)))
400 (when pos
401 (let* ((new-file (svref new-dired-files pos))
402 (write-date (file-write-date
403 (dired-file-pathname new-file))))
404 (when (= (dired-file-write-date old-file) write-date)
405 (setf (dired-file-deleted-p new-file) t)
406 (setf (dired-file-write-date new-file) write-date)
407 (setf (next-character
408 (line-offset (buffer-start point) pos 0))
409 #\D))))))))
410 (setf (dired-info-files dir-info) new-dired-files)
411 (setf (dired-info-file-list dir-info) pathnames)
412 (setf (dired-info-write-date dir-info)
413 (file-write-date directory))
414 (move-mark point (buffer-start-mark buffer)))))))
415
416 ;;; DIRED-IN-BUFFER inserts a dired listing of directory in buffer returning
417 ;;; two values: a list of pathnames of files only, and an array of dired-file
418 ;;; structures. This uses FILTER-REGION to insert a space for the indication
419 ;;; of whether the file is flagged for deletion. Then we clean up extra header
420 ;;; and trailing lines known to be in the output (into every code a little
421 ;;; slime must fall).
422 ;;;
423 (defun dired-in-buffer (directory pattern dot-files-p buffer)
424 (let ((point (buffer-point buffer)))
425 (with-writable-buffer (buffer)
426 (let* ((pathnames (call-print-directory
427 (if pattern
428 (merge-pathnames directory pattern)
429 directory)
430 point
431 dot-files-p))
432 (dired-files (make-array (length pathnames))))
433 (declare (list pathnames) (simple-vector dired-files))
434 (filter-region #'(lambda (str)
435 (concatenate 'simple-string " " str))
436 (buffer-region buffer))
437 (delete-characters point -2)
438 (delete-region (line-to-region (mark-line (buffer-start point))))
439 (delete-characters point)
440 (do ((p pathnames (cdr p))
441 (i 0 (1+ i)))
442 ((null p))
443 (setf (svref dired-files i) (make-dired-file (car p))))
444 (values (delete-if #'directoryp pathnames) dired-files)))))
445
446
447 (defcommand "Dired Help" (p)
448 "How to use dired."
449 "How to use dired."
450 (declare (ignore p))
451 (describe-mode-command nil "Dired"))
452
453 (defcommand "Dired Next File" (p)
454 "Moves to next undeleted file."
455 "Moves to next undeleted file."
456 (unless (dired-line-offset (current-point) (or p 1))
457 (editor-error "Not enough lines.")))
458
459 (defcommand "Dired Previous File" (p)
460 "Moves to previous undeleted file."
461 "Moves to next undeleted file."
462 (unless (dired-line-offset (current-point) (or p -1))
463 (editor-error "Not enough lines.")))
464
465 ;;; DIRED-LINE-OFFSET moves mark n undeleted file lines, returning mark. If
466 ;;; there are not enough lines, mark remains unmoved, this returns nil.
467 ;;;
468 (defun dired-line-offset (mark n)
469 (with-mark ((m mark))
470 (let ((step (if (plusp n) 1 -1)))
471 (dotimes (i (abs n) (move-mark mark m))
472 (loop
473 (unless (line-offset m step 0)
474 (return-from dired-line-offset nil))
475 (when (blank-line-p (mark-line m))
476 (return-from dired-line-offset nil))
477 (when (char= (next-character m) #\space)
478 (return)))))))
479
480
481
482 ;;;; Dired user interaction functions.
483
484 (defun dired-error-function (string &rest args)
485 (apply #'editor-error string args))
486
487 (defun dired-report-function (string &rest args)
488 (clear-echo-area)
489 (apply #'message string args))
490
491 (defun dired-yesp-function (string &rest args)
492 (prompt-for-y-or-n :prompt (cons string args) :default t))
493
494
495
496 ;;;; Dired expunging and quitting.
497
498 (defcommand "Dired Expunge Files" (p)
499 "Expunges files marked for deletion.
500 Query the user if value of \"Dired File Expunge Confirm\" is non-nil. Do
501 the same with directories and the value of \"Dired Directory Expunge
502 Confirm\"."
503 "Expunges files marked for deletion.
504 Query the user if value of \"Dired File Expunge Confirm\" is non-nil. Do
505 the same with directories and the value of \"Dired Directory Expunge
506 Confirm\"."
507 (declare (ignore p))
508 (when (expunge-dired-files)
509 (dired-update-buffer-command nil))
510 (maintain-dired-consistency))
511
512 (defcommand "Dired Quit" (p)
513 "Expunges the files in a dired buffer and then exits."
514 "Expunges the files in a dired buffer and then exits."
515 (declare (ignore p))
516 (expunge-dired-files)
517 (delete-buffer-if-possible (current-buffer)))
518
519 (defhvar "Dired File Expunge Confirm"
520 "When set (the default), \"Dired Expunge Files\" and \"Dired Quit\" will ask
521 for confirmation before deleting the marked files."
522 :value t)
523
524 (defhvar "Dired Directory Expunge Confirm"
525 "When set (the default), \"Dired Expunge Files\" and \"Dired Quit\" will ask
526 for confirmation before deleting each marked directory."
527 :value t)
528
529 (defun expunge-dired-files ()
530 (multiple-value-bind (marked-files marked-dirs) (get-marked-dired-files)
531 (let ((dired:*error-function* #'dired-error-function)
532 (dired:*report-function* #'dired-report-function)
533 (dired:*yesp-function* #'dired-yesp-function)
534 (we-did-something nil))
535 (when (and marked-files
536 (or (not (value dired-file-expunge-confirm))
537 (prompt-for-y-or-n :prompt "Really delete files? "
538 :default t
539 :must-exist t
540 :default-string "Y")))
541 (setf we-did-something t)
542 (dolist (file-info marked-files)
543 (let ((pathname (car file-info))
544 (write-date (cdr file-info)))
545 (if (= write-date (file-write-date pathname))
546 (dired:delete-file (namestring pathname) :clobber t
547 :recursive nil)
548 (message "~A has been modified, it remains unchanged."
549 (namestring pathname))))))
550 (when marked-dirs
551 (dolist (dir-info marked-dirs)
552 (let ((dir (car dir-info))
553 (write-date (cdr dir-info)))
554 (if (= write-date (file-write-date dir))
555 (when (or (not (value dired-directory-expunge-confirm))
556 (prompt-for-y-or-n
557 :prompt (list "~a is a directory. Delete it? "
558 (directory-namestring dir))
559 :default t
560 :must-exist t
561 :default-string "Y"))
562 (dired:delete-file (directory-namestring dir) :clobber t
563 :recursive t)
564 (setf we-did-something t))
565 (message "~A has been modified, it remains unchanged.")))))
566 we-did-something)))
567
568
569
570 ;;;; Dired copying and renaming.
571
572 (defhvar "Dired Copy File Confirm"
573 "Can be either t, nil, or :update. T means always query before clobbering an
574 existing file, nil means don't query before clobbering an existing file, and
575 :update means only ask if the existing file is newer than the source."
576 :value T)
577
578 (defhvar "Dired Rename File Confirm"
579 "When non-nil, dired will query before clobbering an existing file."
580 :value T)
581
582 (defcommand "Dired Copy File" (p)
583 "Copy the file under the point"
584 "Copy the file under the point"
585 (declare (ignore p))
586 (let* ((point (current-point))
587 (confirm (value dired-copy-file-confirm))
588 (source (dired-file-pathname
589 (array-element-from-mark
590 point (dired-info-files (value dired-information)))))
591 (dest (prompt-for-file
592 :prompt (if (directoryp source)
593 "Destination Directory Name: "
594 "Destination Filename: ")
595 :help "Name of new file."
596 :default source
597 :must-exist nil))
598 (dired:*error-function* #'dired-error-function)
599 (dired:*report-function* #'dired-report-function)
600 (dired:*yesp-function* #'dired-yesp-function))
601 (dired:copy-file source dest :update (if (eq confirm :update) t nil)
602 :clobber (not confirm)))
603 (maintain-dired-consistency))
604
605 (defcommand "Dired Rename File" (p)
606 "Rename the file or directory under the point"
607 "Rename the file or directory under the point"
608 (declare (ignore p))
609 (let* ((point (current-point))
610 (source (dired-namify (dired-file-pathname
611 (array-element-from-mark
612 point
613 (dired-info-files (value dired-information))))))
614 (dest (prompt-for-file
615 :prompt "New Filename: "
616 :help "The new name for this file."
617 :default source
618 :must-exist nil))
619 (dired:*error-function* #'dired-error-function)
620 (dired:*report-function* #'dired-report-function)
621 (dired:*yesp-function* #'dired-yesp-function))
622 ;; ARRAY-ELEMENT-FROM-MARK moves mark to line start.
623 (dired:rename-file source dest :clobber (value dired-rename-file-confirm)))
624 (maintain-dired-consistency))
625
626 (defcommand "Dired Copy with Wildcard" (p)
627 "Copy files that match a pattern containing ONE wildcard."
628 "Copy files that match a pattern containing ONE wildcard."
629 (declare (ignore p))
630 (let* ((dir-info (value dired-information))
631 (confirm (value dired-copy-file-confirm))
632 (pattern (prompt-for-string
633 :prompt "Filename pattern: "
634 :help "Type a filename with a single asterisk."
635 :trim t))
636 (destination (namestring
637 (prompt-for-file
638 :prompt "Destination Spec: "
639 :help "Destination spec. May contain ONE asterisk."
640 :default (dired-info-pathname dir-info)
641 :must-exist nil)))
642 (dired:*error-function* #'dired-error-function)
643 (dired:*yesp-function* #'dired-yesp-function)
644 (dired:*report-function* #'dired-report-function))
645 (dired:copy-file pattern destination :update (if (eq confirm :update) t nil)
646 :clobber (not confirm)
647 :directory (dired-info-file-list dir-info)))
648 (maintain-dired-consistency))
649
650 (defcommand "Dired Rename with Wildcard" (p)
651 "Rename files that match a pattern containing ONE wildcard."
652 "Rename files that match a pattern containing ONE wildcard."
653 (declare (ignore p))
654 (let* ((dir-info (value dired-information))
655 (pattern (prompt-for-string
656 :prompt "Filename pattern: "
657 :help "Type a filename with a single asterisk."
658 :trim t))
659 (destination (namestring
660 (prompt-for-file
661 :prompt "Destination Spec: "
662 :help "Destination spec. May contain ONE asterisk."
663 :default (dired-info-pathname dir-info)
664 :must-exist nil)))
665 (dired:*error-function* #'dired-error-function)
666 (dired:*yesp-function* #'dired-yesp-function)
667 (dired:*report-function* #'dired-report-function))
668 (dired:rename-file pattern destination
669 :clobber (not (value dired-rename-file-confirm))
670 :directory (dired-info-file-list dir-info)))
671 (maintain-dired-consistency))
672
673 (defcommand "Delete File" (p)
674 "Delete a file. Specify directories with a trailing slash."
675 "Delete a file. Specify directories with a trailing slash."
676 (declare (ignore p))
677 (let* ((spec (namestring
678 (prompt-for-file
679 :prompt "Delete File: "
680 :help '("Name of File or Directory to delete. ~
681 One wildcard is permitted.")
682 :must-exist nil)))
683 (directoryp (directoryp spec))
684 (dired:*error-function* #'dired-error-function)
685 (dired:*report-function* #'dired-report-function)
686 (dired:*yesp-function* #'dired-yesp-function))
687 (when (or (not directoryp)
688 (not (value dired-directory-expunge-confirm))
689 (prompt-for-y-or-n
690 :prompt (list "~A is a directory. Delete it? "
691 (directory-namestring spec))
692 :default t :must-exist t :default-string "Y")))
693 (dired:delete-file spec :recursive t
694 :clobber (or directoryp
695 (value dired-file-expunge-confirm))))
696 (maintain-dired-consistency))
697
698 (defcommand "Copy File" (p)
699 "Copy a file, allowing ONE wildcard."
700 "Copy a file, allowing ONE wildcard."
701 (declare (ignore p))
702 (let* ((confirm (value dired-copy-file-confirm))
703 (source (namestring
704 (prompt-for-file
705 :prompt "Source Filename: "
706 :help "Name of File to copy. One wildcard is permitted."
707 :must-exist nil)))
708 (dest (namestring
709 (prompt-for-file
710 :prompt (if (directoryp source)
711 "Destination Directory Name: "
712 "Destination Filename: ")
713 :help "Name of new file."
714 :default source
715 :must-exist nil)))
716 (dired:*error-function* #'dired-error-function)
717 (dired:*report-function* #'dired-report-function)
718 (dired:*yesp-function* #'dired-yesp-function))
719 (dired:copy-file source dest :update (if (eq confirm :update) t nil)
720 :clobber (not confirm)))
721 (maintain-dired-consistency))
722
723 (defcommand "Rename File" (p)
724 "Rename a file, allowing ONE wildcard."
725 "Rename a file, allowing ONE wildcard."
726 (declare (ignore p))
727 (let* ((source (namestring
728 (prompt-for-file
729 :prompt "Source Filename: "
730 :help "Name of file to rename. One wildcard is permitted."
731 :must-exist nil)))
732 (dest (namestring
733 (prompt-for-file
734 :prompt (if (directoryp source)
735 "Destination Directory Name: "
736 "Destination Filename: ")
737 :help "Name of new file."
738 :default source
739 :must-exist nil)))
740 (dired:*error-function* #'dired-error-function)
741 (dired:*report-function* #'dired-report-function)
742 (dired:*yesp-function* #'dired-yesp-function))
743 (dired:rename-file source dest
744 :clobber (not (value dired-rename-file-confirm))))
745 (maintain-dired-consistency))
746
747 (defun maintain-dired-consistency ()
748 (dolist (info *pathnames-to-dired-buffers*)
749 (let* ((directory (directory-namestring (car info)))
750 (buffer (cdr info))
751 (dir-info (variable-value 'dired-information :buffer buffer))
752 (write-date (file-write-date directory)))
753 (unless (= (dired-info-write-date dir-info) write-date)
754 (update-dired-buffer directory (dired-info-pattern dir-info) buffer)))))
755
756
757
758 ;;;; Dired utilities.
759
760 ;;; GET-MARKED-DIRED-FILES returns as multiple values a list of file specs
761 ;;; and a list of directory specs that have been marked for deletion. This
762 ;;; assumes the current buffer is a "Dired" buffer.
763 ;;;
764 (defun get-marked-dired-files ()
765 (let* ((files (dired-info-files (value dired-information)))
766 (length (length files))
767 (marked-files ())
768 (marked-dirs ()))
769 (unless files (editor-error "Not in Dired buffer."))
770 (do ((i 0 (1+ i)))
771 ((= i length) (values (nreverse marked-files) (nreverse marked-dirs)))
772 (let* ((thing (svref files i))
773 (pathname (dired-file-pathname thing)))
774 (when (dired-file-deleted-p thing)
775 (if (directoryp pathname)
776 (push (cons pathname (file-write-date pathname)) marked-dirs)
777 (push (cons pathname (file-write-date pathname))
778 marked-files)))))))
779
780 ;;; ARRAY-ELEMENT-FROM-MARK -- Internal Interface.
781 ;;;
782 ;;; This counts the lines between it and the beginning of the buffer. The
783 ;;; number is used to index vector as if each line mapped to an element
784 ;;; starting with the zero'th element (lines are numbered starting at 1).
785 ;;; This must use AREF since some modes use this with extendable vectors.
786 ;;;
787 (defun array-element-from-mark (mark vector
788 &optional (error-msg "Invalid line."))
789 (when (blank-line-p (mark-line mark)) (editor-error error-msg))
790 (aref vector
791 (1- (count-lines (region
792 (buffer-start-mark (line-buffer (mark-line mark)))
793 mark)))))
794
795 ;;; DIRED-NAMIFY and DIRED-DIRECTORIFY are implementation dependent slime.
796 ;;;
797 (defun dired-namify (pathname)
798 (let* ((string (namestring pathname))
799 (last (1- (length string))))
800 (if (char= (schar string last) #\/)
801 (subseq string 0 last)
802 string)))
803 ;;;
804 ;;; This is necessary to derive a canonical representation for directory
805 ;;; names, so "Dired" can map various strings naming one directory to that
806 ;;; one directory.
807 ;;;
808 (defun dired-directorify (pathname)
809 (let ((directory (ext:unix-namestring pathname)))
810 (if (directoryp directory)
811 directory
812 (pathname (concatenate 'simple-string (namestring directory) "/")))))
813
814
815
816 ;;;; View Mode.
817
818 (defmode "View" :major-p nil
819 :setup-function 'setup-view-mode
820 :cleanup-function 'cleanup-view-mode
821 :precedence 5.0
822 :documentation
823 "View mode scrolls forwards and backwards in a file with the buffer read-only.
824 Scrolling off the end optionally deletes the buffer.")
825
826 (defun setup-view-mode (buffer)
827 (defhvar "View Return Function"
828 "Function that gets called when quitting or returning from view mode."
829 :value nil
830 :buffer buffer)
831 (setf (buffer-writable buffer) nil))
832 ;;;
833 (defun cleanup-view-mode (buffer)
834 (delete-variable 'view-return-function :buffer buffer)
835 (setf (buffer-writable buffer) t))
836
837 (defcommand "View File" (p &optional pathname)
838 "Reads a file in as if by \"Find File\", but read-only. Commands exist
839 for scrolling convenience."
840 "Reads a file in as if by \"Find File\", but read-only. Commands exist
841 for scrolling convenience."
842 (declare (ignore p))
843 (let* ((pn (or pathname
844 (prompt-for-file
845 :prompt "View File: " :must-exist t
846 :help "Name of existing file to read into its own buffer."
847 :default (buffer-default-pathname (current-buffer)))))
848 (buffer (make-buffer (format nil "View File ~A" (gensym)))))
849 (visit-file-command nil pn buffer)
850 (setf (buffer-minor-mode buffer "View") t)
851 (change-to-buffer buffer)
852 buffer))
853
854 (defcommand "View Return" (p)
855 "Return to a parent buffer, if it exists."
856 "Return to a parent buffer, if it exists."
857 (declare (ignore p))
858 (unless (call-view-return-fun)
859 (editor-error "No View return method for this buffer.")))
860
861 (defcommand "View Quit" (p)
862 "Delete a buffer in view mode."
863 "Delete a buffer in view mode, invoking VIEW-RETURN-FUNCTION if it exists for
864 this buffer."
865 (declare (ignore p))
866 (let* ((buf (current-buffer))
867 (funp (call-view-return-fun)))
868 (delete-buffer-if-possible buf)
869 (unless funp (editor-error "No View return method for this buffer."))))
870
871 ;;; CALL-VIEW-RETURN-FUN returns nil if there is no current
872 ;;; view-return-function. If there is one, it calls it and returns t.
873 ;;;
874 (defun call-view-return-fun ()
875 (if (hemlock-bound-p 'view-return-function)
876 (let ((fun (value view-return-function)))
877 (cond (fun
878 (funcall fun)
879 t)))))
880
881
882 (defhvar "View Scroll Deleting Buffer"
883 "When this is set, \"View Scroll Down\" deletes the buffer when the end
884 of the file is visible."
885 :value t)
886
887 (defcommand "View Scroll Down" (p)
888 "Scroll the current window down through its buffer.
889 If the end of the file is visible, then delete the buffer if \"View Scroll
890 Deleting Buffer\" is set. If the buffer is associated with a dired buffer,
891 this returns there instead of to the previous buffer."
892 "Scroll the current window down through its buffer.
893 If the end of the file is visible, then delete the buffer if \"View Scroll
894 Deleting Buffer\" is set. If the buffer is associated with a dired buffer,
895 this returns there instead of to the previous buffer."
896 (if (and (not p)
897 (displayed-p (buffer-end-mark (current-buffer))
898 (current-window))
899 (value view-scroll-deleting-buffer))
900 (view-quit-command nil)
901 (scroll-window-down-command p)))
902
903 (defcommand "View Edit File" (p)
904 "Turn off \"View\" mode in this buffer."
905 "Turn off \"View\" mode in this buffer."
906 (declare (ignore p))
907 (let ((buf (current-buffer)))
908 (setf (buffer-minor-mode buf "View") nil)
909 (warn-about-visit-file-buffers buf)))
910
911 (defcommand "View Help" (p)
912 "Shows \"View\" mode help message."
913 "Shows \"View\" mode help message."
914 (declare (ignore p))
915 (describe-mode-command nil "View"))

  ViewVC Help
Powered by ViewVC 1.1.5