]> code.delx.au - gnu-emacs/blobdiff - lisp/dired.el
(viper-insert-prev-from-insertion-ring)
[gnu-emacs] / lisp / dired.el
index bc49f0bf3015c10cebfc7b01997771634ea8fdfb..8ca1d47b2b19289f9319e0c31380450d1b4c1d1b 100644 (file)
@@ -157,37 +157,35 @@ The target is used in the prompt for file copy, rename etc."
   :type 'boolean
   :group 'dired)
 
-(defcustom dired-free-space-program "df"
-  "*Program to get the amount of free space on a file system.
-We assume the output has the format of `df'.
-The value of this variable must be just a command name or file name;
-if you want to specify options, use `dired-free-space-args'.
-
-A value of nil disables this feature."
-  :type '(choice (string :tag "Program") (const :tag "None" nil))
-  :group 'dired)
-
-(defcustom dired-free-space-args "-Pk"
-  "*Options to use when running `dired-free-space-program'."
-  :type 'string
-  :group 'dired)
+; These variables were deleted and the replacements are on files.el.
+; We leave aliases behind for back-compatibility.
+(defvaralias 'dired-free-space-program 'directory-free-space-program)
+(defvaralias 'dired-free-space-args 'directory-free-space-args)
 
 ;;; Hook variables
 
-(defvar dired-load-hook nil
+(defcustom dired-load-hook nil
   "Run after loading dired.
-You can customize key bindings or load extensions with this.")
+You can customize key bindings or load extensions with this."
+  :group 'dired
+  :type 'hook)
 
-(defvar dired-mode-hook nil
-  "Run at the very end of dired-mode.")
+(defcustom dired-mode-hook nil
+  "Run at the very end of dired-mode."
+  :group 'dired
+  :type 'hook)
 
-(defvar dired-before-readin-hook nil
-  "This hook is run before a dired buffer is read in (created or reverted).")
+(defcustom dired-before-readin-hook nil
+  "This hook is run before a dired buffer is read in (created or reverted)."
+  :group 'dired
+  :type 'hook)
 
-(defvar dired-after-readin-hook nil
+(defcustom dired-after-readin-hook nil
   "Hook run after each time a file or directory is read by Dired.
 After each listing of a file or directory, this hook is run
-with the buffer narrowed to the listing.")
+with the buffer narrowed to the listing."
+  :group 'dired
+  :type 'hook)
 ;; Note this can't simply be run inside function `dired-ls' as the hook
 ;; functions probably depend on the dired-subdir-alist to be OK.
 
@@ -399,18 +397,28 @@ BODY should not be too long as it is expanded four times."
      ;; save-excursion loses, again
      (dired-move-to-filename)))
 
-(defun dired-get-marked-files (&optional localp arg)
+(defun dired-get-marked-files (&optional localp arg filter)
   "Return the marked files' names as list of strings.
 The list is in the same order as the buffer, that is, the car is the
   first marked file.
-Values returned are normally absolute pathnames.
+Values returned are normally absolute file names.
 Optional arg LOCALP as in `dired-get-filename'.
-Optional second argument ARG forces to use other files.  If ARG is an
-  integer, use the next ARG files.  If ARG is otherwise non-nil, use
-  current file.  Usually ARG comes from the current prefix arg."
-  (save-excursion
-    (nreverse (dired-map-over-marks (dired-get-filename localp) arg))))
-
+Optional second argument ARG specifies files near point
+ instead of marked files.  If ARG is an integer, use the next ARG files.
+  If ARG is otherwise non-nil, use file.  Usually ARG comes from
+  the command's prefix arg.
+Optional third argument FILTER, if non-nil, is a function to select
+  some of the files--those for which (funcall FILTER FILENAME) is non-nil."
+  (let ((all-of-them
+        (save-excursion
+          (dired-map-over-marks (dired-get-filename localp) arg)))
+       result)
+    (if (not filter)
+       (nreverse all-of-them)
+      (dolist (file all-of-them)
+       (if (funcall filter file)
+           (push file result)))
+      result)))
 \f
 ;; Function dired-ls is redefinable for VMS, ange-ftp, Prospero or
 ;; other special applications.
@@ -638,6 +646,8 @@ If DIRNAME is already in a dired buffer, that buffer is used without refresh."
       (let ((attributes (file-attributes dirname)))
        (if (eq (car attributes) t)
            (set-visited-file-modtime (nth 5 attributes))))
+      (if (consp buffer-undo-list)
+         (setq buffer-undo-list nil))
       (set-buffer-modified-p nil))))
 
 ;; Subroutines of dired-readin
@@ -685,40 +695,7 @@ If DIRNAME is already in a dired buffer, that buffer is used without refresh."
         (cdr dir-or-list))
       ;; Expand the file name here because it may have been abbreviated
       ;; in dired-noselect.
-      (insert-directory (expand-file-name dir-or-list) switches wildcard full-p)
-      (when (and full-p dired-free-space-program)
-       (save-excursion
-         (goto-char (point-min))
-         (when (re-search-forward "total [0-9]+$" nil t)
-           (insert "  free ")
-           ;; Non-Posix systems don't always have dired-free-space-program,
-           ;; but might have an equivalent system call.
-           (if (fboundp 'file-system-info)
-               (let ((beg (point))
-                     (fsinfo (file-system-info dir-or-list)))
-                 (if fsinfo
-                     (insert
-                      (format "%.0f" (/ (nth 2 fsinfo) 1024)))
-                   ;; file-system-info failed; delete " free ".
-                   (delete-region (- beg 7) beg)))
-             (let ((beg (point)))
-               (condition-case nil
-                   (if (zerop (call-process dired-free-space-program nil t nil
-                                            dired-free-space-args
-                                            (expand-file-name dir-or-list)))
-                       (progn
-                         (goto-char beg)
-                         (forward-line 1)
-                         (skip-chars-forward "^ \t")
-                         (forward-word 2)
-                         (skip-chars-forward " \t")
-                         (delete-region beg (point))
-                         (forward-word 1)
-                         (delete-region (point)
-                                        (progn (forward-line 1) (point))))
-            ;; The dired-free-space-program failed; delete its output
-                     (delete-region (- beg 7) (point)))
-                 (error (delete-region (- beg 7) (point))))))))))
+      (insert-directory (expand-file-name dir-or-list) switches wildcard full-p))
     ;; Quote certain characters, unless ls quoted them for us.
     (if (not (string-match "b" dired-actual-switches))
        (save-excursion
@@ -940,7 +917,7 @@ If DIRNAME is already in a dired buffer, that buffer is used without refresh."
     (define-key map "*\177" 'dired-unmark-backward)
     (define-key map "*\C-n" 'dired-next-marked-file)
     (define-key map "*\C-p" 'dired-prev-marked-file)
-    (define-key map "*t" 'dired-do-toggle)
+    (define-key map "*t" 'dired-toggle-marks)
     ;; Lower keys for commands not operating on all the marked files
     (define-key map "a" 'dired-find-alternate-file)
     (define-key map "d" 'dired-flag-file-deletion)
@@ -959,9 +936,10 @@ If DIRNAME is already in a dired buffer, that buffer is used without refresh."
     (define-key map "p" 'dired-previous-line)
     (define-key map "q" 'quit-window)
     (define-key map "s" 'dired-sort-toggle-or-edit)
-    (define-key map "t" 'dired-do-toggle)
+    (define-key map "t" 'dired-toggle-marks)
     (define-key map "u" 'dired-unmark)
     (define-key map "v" 'dired-view-file)
+    (define-key map "w" 'dired-copy-filename-as-kill)
     (define-key map "x" 'dired-do-flagged-delete)
     (define-key map "y" 'dired-show-file-type)
     (define-key map "+" 'dired-create-directory)
@@ -1139,7 +1117,7 @@ If DIRNAME is already in a dired buffer, that buffer is used without refresh."
       '(menu-item "Mark" dired-mark
                  :help "Mark current line's file for future operations"))
     (define-key map [menu-bar mark toggle-marks]
-      '(menu-item "Toggle Marks" dired-do-toggle
+      '(menu-item "Toggle Marks" dired-toggle-marks
                  :help "Mark unmarked files, unmark marked ones"))
 
     (define-key map [menu-bar operate]
@@ -1386,26 +1364,50 @@ Creates a buffer if necessary."
 (defun dired-mouse-find-file-other-window (event)
   "In Dired, visit the file or directory name you click on."
   (interactive "e")
-  (let (file)
+  (let (window pos file)
     (save-excursion
-      (set-buffer (window-buffer (posn-window (event-end event))))
-      (save-excursion
-       (goto-char (posn-point (event-end event)))
-       (setq file (dired-get-file-for-visit))))
-    (select-window (posn-window (event-end event)))
+      (setq window (posn-window (event-end event))
+           pos (posn-point (event-end event)))
+      (if (not (windowp window))
+         (error "No file chosen"))
+      (set-buffer (window-buffer window))
+      (goto-char pos)
+      (setq file (dired-get-file-for-visit)))
+    (select-window window)
     (find-file-other-window (file-name-sans-versions file t))))
 
+(defcustom dired-view-command-alist
+  '(("[.]ps\\'" . "gv -spartan -color -watch")
+    ("[.]pdf\\'" . "xpdf")
+    ("[.]dvi\\'" . "xdvi -sidemargin 0.5 -topmargin 1"))
+  "Alist specifying how to view special types of files.
+Each element has the form (REGEXP . SHELL-COMMAND).
+When the file name matches REGEXP, `dired-view-file'
+invokes SHELL-COMMAND to view the file, putting the file name
+at the end of the command."
+  :group 'dired
+  :type '(alist :key-type regexp :value-type string)
+  :version "21.4")
+
 (defun dired-view-file ()
   "In Dired, examine a file in view mode, returning to dired when done.
-When file is a directory, show it in this buffer if it is inserted;
-otherwise, display it in another buffer."
+When file is a directory, show it in this buffer if it is inserted.
+Some kinds of files are displayed using external viewer programs;
+see `dired-view-command-alist'.  Otherwise, display it in another buffer."
   (interactive)
   (let ((file (dired-get-file-for-visit)))
     (if (file-directory-p file)
        (or (and (cdr dired-subdir-alist)
                 (dired-goto-subdir file))
            (dired file))
-      (view-file file))))
+      (let (cmd)
+       ;; Look for some other way to view a certain file.
+       (dolist (elt dired-view-command-alist)
+         (if (string-match (car elt) file)
+             (setq cmd (cdr elt))))
+       (if cmd
+           (dired-run-shell-command (concat cmd " " file))
+         (view-file file))))))
 
 (defun dired-find-file-other-window ()
   "In Dired, visit this file or directory in another window."
@@ -1459,14 +1461,25 @@ Optional arg NO-ERROR-IF-NOT-FILEP means return nil if no filename on
         (not default-file-name-coding-system)
         (setq file (encode-coding-string file buffer-file-coding-system)))
     (cond
+     ((null file)
+      nil)
      ((eq localp 'verbatim)
       file)
      ((and (eq localp 'no-dir) already-absolute)
       (file-name-nondirectory file))
-     ((or already-absolute (eq localp 'no-dir))
+     (already-absolute
+      (if (find-file-name-handler file nil)
+         (concat "/:" file)
+       file))
+     ((eq localp 'no-dir)
       file)
+     ((equal (dired-current-directory) "/")
+      (setq file (concat (dired-current-directory localp) file))
+      (if (find-file-name-handler file nil)
+         (concat "/:" file)
+       file))
      (t
-      (and file (concat (dired-current-directory localp) file))))))
+      (concat (dired-current-directory localp) file)))))
 
 (defun dired-string-replace-match (regexp string newtext
                                          &optional literal global)
@@ -1631,6 +1644,37 @@ regardless of the language.")
        nil
       (point))))
 
+\f
+;;; COPY NAMES OF MARKED FILES INTO KILL-RING.
+
+(defun dired-copy-filename-as-kill (&optional arg)
+  "Copy names of marked (or next ARG) files into the kill ring.
+The names are separated by a space.
+With a zero prefix arg, use the complete pathname of each marked file.
+With \\[universal-argument], use the relative pathname of each marked file.
+
+If on a subdir headerline, use subdirname instead; prefix arg is ignored
+in this case.
+
+You can then feed the file name(s) to other commands with \\[yank]."
+  (interactive "P")
+  (let ((string
+         (or (dired-get-subdir)
+             (mapconcat (function identity)
+                        (if arg
+                            (cond ((zerop (prefix-numeric-value arg))
+                                   (dired-get-marked-files))
+                                  ((integerp arg)
+                                   (dired-get-marked-files 'no-dir arg))
+                                  (t    ; else a raw arg
+                                   (dired-get-marked-files t)))
+                          (dired-get-marked-files 'no-dir))
+                        " "))))
+    (if (eq last-command 'kill-region)
+       (kill-append string nil)
+      (kill-new string))
+    (message "%s" string)))
+
 \f
 ;; Keeping Dired buffers in sync with the filesystem and with each other
 
@@ -1962,7 +2006,7 @@ Optional argument means return a file name relative to `default-directory'."
 
 (defcustom dired-recursive-deletes nil ; Default only delete empty directories.
   "*Decide whether recursive deletes are allowed.
-Nil means no recursive deletes.
+nil means no recursive deletes.
 `always' means delete recursively without asking.  This is DANGEROUS!
 `top' means ask for each directory at top level, but delete its subdirectories
 without asking.
@@ -2063,9 +2107,9 @@ if there are no flagged files."
                      ;; if we get here, removing worked
                      (setq succ (1+ succ))
                      (message "%s of %s deletions" succ count)
-                     (delete-region (progn (beginning-of-line) (point))
-                                    (progn (forward-line 1) (point)))
-                     (dired-clean-up-after-deletion fn))
+                     (dired-fun-in-all-buffers
+                      (file-name-directory fn) (file-name-nondirectory fn)
+                      (function dired-delete-entry) fn))
                  (error;; catch errors from failed deletions
                   (dired-log "%s\n" err)
                   (setq failures (cons (car (car l)) failures)))))
@@ -2080,6 +2124,29 @@ if there are no flagged files."
       (message "(No deletions performed)")))
   (dired-move-to-filename))
 
+(defun dired-fun-in-all-buffers (directory file fun &rest args)
+  ;; In all buffers dired'ing DIRECTORY, run FUN with ARGS.
+  ;; If the buffer has a wildcard pattern, check that it matches FILE.
+  ;; (FILE does not include a directory component.)
+  ;; FILE may be nil, in which case ignore it.
+  ;; Return list of buffers where FUN succeeded (i.e., returned non-nil).
+  (let (success-list)
+    (dolist (buf (dired-buffers-for-dir (expand-file-name directory)
+                                       file))
+      (with-current-buffer buf
+       (if (apply fun args)
+           (setq success-list (cons (buffer-name buf) success-list)))))
+    success-list))
+
+;; Delete the entry for FILE from 
+(defun dired-delete-entry (file)
+  (save-excursion
+    (and (dired-goto-file file)
+        (let (buffer-read-only)
+          (delete-region (progn (beginning-of-line) (point))
+                         (save-excursion (forward-line 1) (point))))))
+  (dired-clean-up-after-deletion file))
+
 ;; This is a separate function for the sake of dired-x.el.
 (defun dired-clean-up-after-deletion (fn)
   ;; Clean up after a deleted file or directory FN.
@@ -2118,7 +2185,7 @@ if there are no flagged files."
     ;; let window shrink to fit:
     (let ((window (selected-window))
          target-lines w2)
-      (cond ;; if split-window-threshold is enabled, use the largest window
+      (cond ;; if split-height-threshold is enabled, use the largest window
             ((and (> (window-height (setq w2 (get-largest-window)))
                     split-height-threshold)
                  (= (frame-width) (window-width w2)))
@@ -2326,9 +2393,8 @@ Optional prefix ARG says how many lines to unflag; default is one line."
   (interactive "p")
   (dired-unmark (- arg)))
 
-(defun dired-do-toggle ()
-  "Toggle marks.
-That is, currently marked files become unmarked and vice versa.
+(defun dired-toggle-marks ()
+  "Toggle marks: marked files become unmarked, and vice versa.
 Files marked with other flags (such as `D') are not affected.
 `.' and `..' are never toggled.
 As always, hidden subdirs are not affected."
@@ -2669,6 +2735,7 @@ With a prefix argument you can edit the current listing switches instead."
   (revert-buffer))
 
 ;; Some user code loads dired especially for this.
+;; Don't do that--use replace-regexp-in-string instead.
 (defun dired-replace-in-string (regexp newtext string)
   ;; Replace REGEXP with NEWTEXT everywhere in STRING and return result.
   ;; NEWTEXT is taken literally---no \\DIGIT escapes will be recognized.
@@ -2778,18 +2845,33 @@ If no files are marked or a specific numeric prefix arg is given,
 the next ARG files are used.  Just \\[universal-argument] means the current file.
 The prompt mentions the file(s) or the marker, as appropriate.
 
-If there is output, it goes to a separate buffer.
+If there is a `*' in COMMAND, surrounded by whitespace, this runs
+COMMAND just once with the entire file list substituted there.
 
-Normally the command is run on each file individually.
-However, if there is a `*' in the command then it is run
-just once with the entire file list substituted there.
+If there is no `*', but there is a `?' in COMMAND, surrounded by
+whitespace, this runs COMMAND on each file individually with the
+file name substituted for `?'.
 
-No automatic redisplay of dired buffers is attempted, as there's no
-telling what files the command may have changed.  Type
-\\[dired-do-redisplay] to redisplay the marked files.
+Otherwise, this runs COMMAND on each file individually with the
+file name added at the end of COMMAND (separated by a space).
 
-The shell command has the top level directory as working directory, so
-output files usually are created there instead of in a subdir."
+`*' and `?' when not surrounded by whitespace have no special
+significance for `dired-do-shell-command', and are passed through
+normally to the shell, but you must confirm first.  To pass `*' by
+itself to the shell as a wildcard, type `*\"\"'.
+
+If COMMAND produces output, it goes to a separate buffer.
+
+This feature does not try to redisplay Dired buffers afterward, as
+there's no telling what files COMMAND may have changed.
+Type \\[dired-do-redisplay] to redisplay the marked files.
+
+When COMMAND runs, its working directory is the top-level directory of
+the Dired buffer, so output files usually are created there instead of
+in a subdir.
+
+In a noninteractive call (from Lisp code), you must specify
+the list of file names explicitly with the FILE-LIST argument."
   t)
 
 (autoload 'dired-do-kill-lines "dired-aux"
@@ -2940,6 +3022,10 @@ Use \\[dired-hide-subdir] to (un)hide a particular subdirectory."
 If FILE is a symbolic link and the optional argument DEREF-SYMLINKS is
 true then the type of the file linked to by FILE is printed instead." 
   t)
+
+(autoload 'dired-run-shell-command "dired-aux")
+
+(autoload 'dired-query "dired-aux")
 \f
 (if (eq system-type 'vax-vms)
     (load "dired-vms"))