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

Contents of /src/hemlock/diredcoms.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5