(dired-font-lock-keywords): Don't specially highlight
[bpt/emacs.git] / lisp / dired.el
index 34526f4..30348d6 100644 (file)
@@ -1,6 +1,6 @@
 ;;; dired.el --- directory-browsing commands
 
-;; Copyright (C) 1985, 86, 92, 93, 94, 95, 1996 Free Software Foundation, Inc.
+;; Copyright (C) 1985, 86, 92, 93, 94, 95, 96, 1997 Free Software Foundation, Inc.
 
 ;; Author: Sebastian Kremer <sk@thp.uni-koeln.de>
 ;; Maintainer: FSF
 
 ;;; Customizable variables
 
+(defgroup dired nil
+  "Directory editing."
+  :group 'environment)
+
+(defgroup dired-mark nil
+  "Handling marks in dired."
+  :prefix "dired-"
+  :group 'dired)
+
+
 ;;;###autoload
-(defvar dired-listing-switches "-al"
+(defcustom dired-listing-switches "-al"
   "*Switches passed to `ls' for dired.  MUST contain the `l' option.
 May contain all other options that don't contradict `-l';
 may contain even `F', `b', `i' and `s'.  See also the variable
-`dired-ls-F-marks-symlinks' concerning the `F' switch.")
+`dired-ls-F-marks-symlinks' concerning the `F' switch."
+  :type 'string
+  :group 'dired)
 
 ; Don't use absolute paths as /bin should be in any PATH and people
 ; may prefer /usr/local/gnu/bin or whatever.  However, chown is
@@ -61,7 +73,7 @@ may contain even `F', `b', `i' and `s'.  See also the variable
   "Name of chmod command (usually `chmod' or `chmode').")
 
 ;;;###autoload
-(defvar dired-ls-F-marks-symlinks nil
+(defcustom dired-ls-F-marks-symlinks nil
   "*Informs dired about how `ls -lF' marks symbolic links.
 Set this to t if `ls' (or whatever program is specified by
 `insert-directory-program') with `-lF' marks the symbolic link
@@ -73,52 +85,74 @@ nil (the default), if it gives `bar@ -> foo', set it to t.
 Dired checks if there is really a @ appended.  Thus, if you have a
 marking `ls' program on one host and a non-marking on another host, and
 don't care about symbolic links which really end in a @, you can
-always set this variable to t.")
+always set this variable to t."
+  :type 'boolean
+  :group 'dired-mark)
 
 ;;;###autoload
-(defvar dired-trivial-filenames "^\\.\\.?$\\|^#"
+(defcustom dired-trivial-filenames "^\\.\\.?$\\|^#"
   "*Regexp of files to skip when finding first file of a directory.
 A value of nil means move to the subdir line.
-A value of t means move to first file.")
+A value of t means move to first file."
+  :type '(choice (const :tag "Move to subdir" nil)
+                (const :tag "Move to first" t)
+                regexp)
+  :group 'dired)
 
 ;;;###autoload
-(defvar dired-keep-marker-rename t
+(defcustom dired-keep-marker-rename t
   ;; Use t as default so that moved files "take their markers with them".
   "*Controls marking of renamed files.
 If t, files keep their previous marks when they are renamed.
 If a character, renamed files (whether previously marked or not)
-are afterward marked with that character.")
+are afterward marked with that character."
+  :type '(choice (const :tag "Keep" t)
+                (character :tag "Mark"))
+  :group 'dired-mark)
 
 ;;;###autoload
-(defvar dired-keep-marker-copy ?C
+(defcustom dired-keep-marker-copy ?C
   "*Controls marking of copied files.
 If t, copied files are marked if and as the corresponding original files were.
-If a character, copied files are unconditionally marked with that character.")
+If a character, copied files are unconditionally marked with that character."
+  :type '(choice (const :tag "Keep" t)
+                (character :tag "Mark"))
+  :group 'dired-mark)
 
 ;;;###autoload
-(defvar dired-keep-marker-hardlink ?H
+(defcustom dired-keep-marker-hardlink ?H
   "*Controls marking of newly made hard links.
 If t, they are marked if and as the files linked to were marked.
-If a character, new links are unconditionally marked with that character.")
+If a character, new links are unconditionally marked with that character."
+  :type '(choice (const :tag "Keep" t)
+                (character :tag "Mark"))
+  :group 'dired-mark)
 
 ;;;###autoload
-(defvar dired-keep-marker-symlink ?Y
+(defcustom dired-keep-marker-symlink ?Y
   "*Controls marking of newly made symbolic links.
 If t, they are marked if and as the files linked to were marked.
-If a character, new links are unconditionally marked with that character.")
+If a character, new links are unconditionally marked with that character."
+  :type '(choice (const :tag "Keep" t)
+                (character :tag "Mark"))
+  :group 'dired-mark)
 
 ;;;###autoload
-(defvar dired-dwim-target nil
+(defcustom dired-dwim-target nil
   "*If non-nil, dired tries to guess a default target directory.
 This means: if there is a dired buffer displayed in the next window,
 use its current subdir, instead of the current subdir of this dired buffer.
 
-The target is used in the prompt for file copy, rename etc.")
+The target is used in the prompt for file copy, rename etc."
+  :type 'boolean
+  :group 'dired)
 
 ;;;###autoload
-(defvar dired-copy-preserve-time t
+(defcustom dired-copy-preserve-time t
   "*If non-nil, Dired preserves the last-modified time in a file copy.
-\(This works on only some systems.)")
+\(This works on only some systems.)"
+  :type 'boolean
+  :group 'dired)
 
 ;;; Hook variables
 
@@ -227,12 +261,16 @@ Subexpression 2 must end right before the \\n or \\r.")
    (list dired-re-mark
         '(0 font-lock-reference-face)
         '(".+" (dired-move-to-filename) nil (0 font-lock-warning-face)))
-   ;;
-   ;; Files that are group or world writable.
-   (list (concat dired-re-maybe-mark dired-re-inode-size
-                "\\([-d]\\(....w....\\|.......w.\\)\\)")
-        '(1 font-lock-comment-face)
-        '(".+" (dired-move-to-filename) nil (0 font-lock-comment-face)))
+   ;; People who are paranoid about security would consider this more
+   ;; important than other things such as whether it is a directory.
+   ;; But we don't want to encourage paranoia, so our default
+   ;; should be what's most useful for non-paranoids. -- rms.
+;;;   ;;
+;;;   ;; Files that are group or world writable.
+;;;   (list (concat dired-re-maybe-mark dired-re-inode-size
+;;;             "\\([-d]\\(....w....\\|.......w.\\)\\)")
+;;;     '(1 font-lock-comment-face)
+;;;     '(".+" (dired-move-to-filename) nil (0 font-lock-comment-face)))
    ;;
    ;; Subdirectories.
    (list dired-re-dir
@@ -410,16 +448,24 @@ If DIRNAME is already in a dired buffer, that buffer is used without refresh."
   (or dir-or-list (setq dir-or-list default-directory))
   ;; This loses the distinction between "/foo/*/" and "/foo/*" that
   ;; some shells make:
-  (let (dirname)
+  (let (dirname initially-was-dirname)
     (if (consp dir-or-list)
        (setq dirname (car dir-or-list))
       (setq dirname dir-or-list))
+    (setq initially-was-dirname
+         (string= (file-name-as-directory dirname) dirname))
     (setq dirname (abbreviate-file-name
                   (expand-file-name (directory-file-name dirname))))
     (if find-file-visit-truename
        (setq dirname (file-truename dirname)))
-    (if (file-directory-p dirname)
-       (setq dirname (file-name-as-directory dirname)))
+    ;; If the argument was syntactically  a directory name not a file name,
+    ;; or if it happens to name a file that is a directory,
+    ;; convert it syntactically to a directory name.
+    ;; The reason for checking initially-was-dirname
+    ;; and not just file-directory-p
+    ;; is that file-directory-p is slow over ftp.
+    (if (or initially-was-dirname (file-directory-p dirname))
+       (setq dirname  (file-name-as-directory dirname)))
     (if (consp dir-or-list)
        (setq dir-or-list (cons dirname (cdr dir-or-list)))
       (setq dir-or-list dirname))
@@ -473,9 +519,11 @@ If DIRNAME is already in a dired buffer, that buffer is used without refresh."
                           "Directory has changed on disk; type \\[revert-buffer] to update Dired")))))
       ;; Else a new buffer
       (setq default-directory
-           (if (file-directory-p dirname)
-               dirname
-             (file-name-directory dirname)))
+           ;; We can do this unconditionally
+           ;; because dired-noselect ensures that the name
+           ;; is passed in directory name syntax
+           ;; if it was the name of a directory at all.
+           (file-name-directory dirname))
       (or switches (setq switches dired-listing-switches))
       (dired-mode dirname switches)
       (if mode (funcall mode))
@@ -769,6 +817,7 @@ If DIRNAME is already in a dired buffer, that buffer is used without refresh."
     (define-key map "#" 'dired-flag-auto-save-files)
     (define-key map "." 'dired-clean-directory)
     (define-key map "~" 'dired-flag-backup-files)
+    (define-key map "&" 'dired-flag-garbage-files)
     ;; Upper case keys (except !) for operating on the marked files
     (define-key map "A" 'dired-do-search)
     (define-key map "C" 'dired-do-copy)
@@ -823,7 +872,7 @@ If DIRNAME is already in a dired buffer, that buffer is used without refresh."
     (define-key map "*m" 'dired-mark)
     (define-key map "*u" 'dired-unmark)
     (define-key map "*?" 'dired-unmark-all-files)
-    (define-key map "*!" 'dired-unmark-all-files-no-query)
+    (define-key map "*!" 'dired-unmark-all-marks)
     (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)
@@ -949,7 +998,7 @@ If DIRNAME is already in a dired buffer, that buffer is used without refresh."
     (define-key map [menu-bar mark marks]
       '("Change Marks..." . dired-change-marks))
     (define-key map [menu-bar mark unmark-all]
-      '("Unmark All" . dired-unmark-all-files-no-query))
+      '("Unmark All" . dired-unmark-all-marks))
     (define-key map [menu-bar mark symlinks]
       '("Mark Symlinks" . dired-mark-symlinks))
     (define-key map [menu-bar mark directories]
@@ -958,6 +1007,8 @@ If DIRNAME is already in a dired buffer, that buffer is used without refresh."
       '("Mark Old Backups" . dired-clean-directory))
     (define-key map [menu-bar mark executables]
       '("Mark Executables" . dired-mark-executables))
+    (define-key map [menu-bar mark garbage-files]
+      '("Flag Garbage Files" . dired-flag-garbage-files))
     (define-key map [menu-bar mark backup-files]
       '("Flag Backup Files" . dired-flag-backup-files))
     (define-key map [menu-bar mark auto-save-files]
@@ -1082,7 +1133,7 @@ Keybindings:
 ;;     case-fold-search nil
        buffer-read-only t
        selective-display t             ; for subdirectory hiding
-       mode-line-buffer-identification '("Dired: %17b"))
+       mode-line-buffer-identification '("%17b"))
   (set (make-local-variable 'revert-buffer-function)
        (function dired-revert))
   (set (make-local-variable 'page-delimiter)
@@ -1179,7 +1230,9 @@ Creates a buffer if necessary."
   (let ((file-name (file-name-sans-versions (dired-get-filename) t)))
     (if (file-exists-p file-name)
        (find-file file-name)
-      (error "File no longer exists; type `g' to update Dired buffer"))))
+      (if (file-symlink-p file-name)
+         (error "File is a symlink to a nonexistent target")
+       (error "File no longer exists; type `g' to update Dired buffer")))))
 
 (defun dired-mouse-find-file-other-window (event)
   "In dired, visit the file or directory name you click on."
@@ -1388,27 +1441,70 @@ DIR must be a directory name, not a file name."
   ;; killed buffer, it is removed from this list.
   "Alist of expanded directories and their associated dired buffers.")
 
-(defun dired-buffers-for-dir (dir)
+(defun dired-buffers-for-dir (dir &optional file)
 ;; Return a list of buffers that dired DIR (top level or in-situ subdir).
+;; If FILE is non-nil, include only those whose wildcard pattern (if any)
+;; matches FILE.
 ;; The list is in reverse order of buffer creation, most recent last.
 ;; As a side effect, killed dired buffers for DIR are removed from
 ;; dired-buffers.
   (setq dir (file-name-as-directory dir))
-  (let ((alist dired-buffers) result elt buf)
+  (let ((alist dired-buffers) result elt buf pattern)
     (while alist
       (setq elt (car alist)
            buf (cdr elt))
       (if (buffer-name buf)
          (if (dired-in-this-tree dir (car elt))
-             (if (assoc dir (save-excursion
-                              (set-buffer buf)
-                              dired-subdir-alist))
-                 (setq result (cons buf result))))
+             (with-current-buffer buf
+               (and (assoc dir dired-subdir-alist)
+                    (or (null file)
+                        (let ((wildcards
+                               (file-name-nondirectory dired-directory)))
+                          (or (= 0 (length wildcards))
+                              (string-match (dired-glob-regexp wildcards)
+                                            file))))
+                    (setq result (cons buf result)))))
        ;; else buffer is killed - clean up:
        (setq dired-buffers (delq elt dired-buffers)))
       (setq alist (cdr alist)))
     result))
 
+(defun dired-glob-regexp (pattern)
+  "Convert glob-pattern PATTERN to a regular expression."
+  (let ((matched-in-pattern 0)  ;; How many chars of PATTERN we've handled.
+       regexp)
+    (while (string-match "[[?*]" pattern matched-in-pattern)
+      (let ((op-end (match-end 0))
+           (next-op (aref pattern (match-beginning 0))))
+       (setq regexp (concat regexp
+                            (regexp-quote
+                             (substring pattern matched-in-pattern
+                                        (match-beginning 0)))))
+       (cond ((= next-op ??)
+              (setq regexp (concat regexp "."))
+              (setq matched-in-pattern op-end))
+             ((= next-op ?\[)
+              ;; Fails to handle ^ yet ????
+              (let* ((set-start (match-beginning 0))
+                     (set-cont
+                      (if (= (aref pattern (1+ set-start)) ?^)
+                          (+ 3 set-start)
+                        (+ 2 set-start)))
+                     (set-end (string-match "]" pattern set-cont))
+                     (set (substring pattern set-start (1+ set-end))))
+                (setq regexp (concat regexp set))
+                (setq matched-in-pattern (1+ set-end))))
+             ((= next-op ?*)
+              (setq regexp (concat regexp ".*"))
+              (setq matched-in-pattern op-end)))))
+    (concat "\\`"
+           regexp
+           (regexp-quote
+            (substring pattern matched-in-pattern))
+           "\\'")))
+
+                
+
 (defun dired-advertise ()
   ;;"Advertise in variable `dired-buffers' that we dired `default-directory'."
   ;; With wildcards we actually advertise too much.
@@ -1807,12 +1903,10 @@ if there are no flagged files."
       )))
 
 (defvar dired-no-confirm nil
-;;  "If non-nil, list of symbols for commands dired should not confirm.
-;;It can be a sublist of
-;;
-;;  '(byte-compile chgrp chmod chown compress copy delete hardlink load
-;;    move print shell symlink uncompress)"
-  )
+  "A list of symbols for commands dired should not confirm.
+Command symbols are `byte-compile', `chgrp', `chmod', `chown', `compress',
+`copy', `delete', `hardlink', `load', `move', `print', `shell', `symlink' and
+`uncompress'.")
 
 (defun dired-mark-pop-up (bufname op-symbol files function &rest args)
   ;;"Args BUFNAME OP-SYMBOL FILES FUNCTION &rest ARGS.
@@ -1825,7 +1919,8 @@ if there are no flagged files."
   ;; OP-SYMBOL is a member of the list in `dired-no-confirm'.
   ;;FILES is the list of marked files."
   (or bufname (setq bufname  " *Marked Files*"))
-  (if (or (memq op-symbol dired-no-confirm)
+  (if (or (eq dired-no-confirm t)
+         (memq op-symbol dired-no-confirm)
          (= (length files) 1))
       (apply function args)
     (save-excursion
@@ -2068,6 +2163,15 @@ A prefix argument says to unflag those files instead."
                    (file-name-nondirectory fn)))))
      "auto save file")))
 
+(defvar dired-garbage-files-regexp
+  "\\.log$\\|\\.toc$\\|.dvi$|\\.bak$\\|\\.orig$\\|\\.rej$" 
+  "*Regular expression to match \"garbage\" files for `dired-flag-garbage-files'.")
+
+(defun dired-flag-garbage-files ()
+  (interactive)
+  "Flag for deletion all files that match `dired-garbage-files-regexp'."
+  (dired-flag-files-regexp dired-garbage-files-regexp))
+
 (defun dired-flag-backup-files (&optional unflag-p)
   "Flag all backup files (names ending with `~') for deletion.
 With prefix argument, unflag these files."
@@ -2113,7 +2217,7 @@ OLD and NEW are both characters used to mark files."
              (subst-char-in-region (match-beginning 0)
                                    (match-end 0) old new)))))))
 
-(defun dired-unmark-all-files-no-query ()
+(defun dired-unmark-all-marks ()
   "Remove all marks from all files in the Dired buffer."
   (interactive)
   (dired-unmark-all-files ?\r))