| 1 | ;;; dired.el --- directory-browsing commands |
| 2 | |
| 3 | ;; Copyright (C) 1985, 86, 92, 93, 94, 95, 96, 1997 Free Software Foundation, Inc. |
| 4 | |
| 5 | ;; Author: Sebastian Kremer <sk@thp.uni-koeln.de> |
| 6 | ;; Maintainer: FSF |
| 7 | |
| 8 | ;; This file is part of GNU Emacs. |
| 9 | |
| 10 | ;; GNU Emacs is free software; you can redistribute it and/or modify |
| 11 | ;; it under the terms of the GNU General Public License as published by |
| 12 | ;; the Free Software Foundation; either version 2, or (at your option) |
| 13 | ;; any later version. |
| 14 | |
| 15 | ;; GNU Emacs is distributed in the hope that it will be useful, |
| 16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | ;; GNU General Public License for more details. |
| 19 | |
| 20 | ;; You should have received a copy of the GNU General Public License |
| 21 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
| 22 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 23 | ;; Boston, MA 02111-1307, USA. |
| 24 | |
| 25 | ;;; Commentary: |
| 26 | |
| 27 | ;; This is a major mode for directory browsing and editing. It is |
| 28 | ;; documented in the Emacs manual. |
| 29 | |
| 30 | ;; Rewritten in 1990/1991 to add tree features, file marking and |
| 31 | ;; sorting by Sebastian Kremer <sk@thp.uni-koeln.de>. |
| 32 | ;; Finished up by rms in 1992. |
| 33 | |
| 34 | ;;; Code: |
| 35 | |
| 36 | ;;; Customizable variables |
| 37 | |
| 38 | (defgroup dired nil |
| 39 | "Directory editing." |
| 40 | :group 'environment) |
| 41 | |
| 42 | (defgroup dired-mark nil |
| 43 | "Handling marks in dired." |
| 44 | :prefix "dired-" |
| 45 | :group 'dired) |
| 46 | |
| 47 | |
| 48 | ;;;###autoload |
| 49 | (defcustom dired-listing-switches "-al" |
| 50 | "*Switches passed to `ls' for dired. MUST contain the `l' option. |
| 51 | May contain all other options that don't contradict `-l'; |
| 52 | may contain even `F', `b', `i' and `s'. See also the variable |
| 53 | `dired-ls-F-marks-symlinks' concerning the `F' switch." |
| 54 | :type 'string |
| 55 | :group 'dired) |
| 56 | |
| 57 | ; Don't use absolute paths as /bin should be in any PATH and people |
| 58 | ; may prefer /usr/local/gnu/bin or whatever. However, chown is |
| 59 | ; usually not in PATH. |
| 60 | |
| 61 | ;;;###autoload |
| 62 | (defvar dired-chown-program |
| 63 | (if (memq system-type '(hpux dgux usg-unix-v irix linux gnu/linux)) |
| 64 | "chown" |
| 65 | (if (file-exists-p "/usr/sbin/chown") |
| 66 | "/usr/sbin/chown" |
| 67 | "/etc/chown")) |
| 68 | "Name of chown command (usually `chown' or `/etc/chown').") |
| 69 | |
| 70 | (defvar dired-chmod-program "chmod" |
| 71 | "Name of chmod command (usually `chmod').") |
| 72 | |
| 73 | ;;;###autoload |
| 74 | (defcustom dired-ls-F-marks-symlinks nil |
| 75 | "*Informs dired about how `ls -lF' marks symbolic links. |
| 76 | Set this to t if `ls' (or whatever program is specified by |
| 77 | `insert-directory-program') with `-lF' marks the symbolic link |
| 78 | itself with a trailing @ (usually the case under Ultrix). |
| 79 | |
| 80 | Example: if `ln -s foo bar; ls -F bar' gives `bar -> foo', set it to |
| 81 | nil (the default), if it gives `bar@ -> foo', set it to t. |
| 82 | |
| 83 | Dired checks if there is really a @ appended. Thus, if you have a |
| 84 | marking `ls' program on one host and a non-marking on another host, and |
| 85 | don't care about symbolic links which really end in a @, you can |
| 86 | always set this variable to t." |
| 87 | :type 'boolean |
| 88 | :group 'dired-mark) |
| 89 | |
| 90 | ;;;###autoload |
| 91 | (defcustom dired-trivial-filenames "^\\.\\.?$\\|^#" |
| 92 | "*Regexp of files to skip when finding first file of a directory. |
| 93 | A value of nil means move to the subdir line. |
| 94 | A value of t means move to first file." |
| 95 | :type '(choice (const :tag "Move to subdir" nil) |
| 96 | (const :tag "Move to first" t) |
| 97 | regexp) |
| 98 | :group 'dired) |
| 99 | |
| 100 | ;;;###autoload |
| 101 | (defcustom dired-keep-marker-rename t |
| 102 | ;; Use t as default so that moved files "take their markers with them". |
| 103 | "*Controls marking of renamed files. |
| 104 | If t, files keep their previous marks when they are renamed. |
| 105 | If a character, renamed files (whether previously marked or not) |
| 106 | are afterward marked with that character." |
| 107 | :type '(choice (const :tag "Keep" t) |
| 108 | (character :tag "Mark")) |
| 109 | :group 'dired-mark) |
| 110 | |
| 111 | ;;;###autoload |
| 112 | (defcustom dired-keep-marker-copy ?C |
| 113 | "*Controls marking of copied files. |
| 114 | If t, copied files are marked if and as the corresponding original files were. |
| 115 | If a character, copied files are unconditionally marked with that character." |
| 116 | :type '(choice (const :tag "Keep" t) |
| 117 | (character :tag "Mark")) |
| 118 | :group 'dired-mark) |
| 119 | |
| 120 | ;;;###autoload |
| 121 | (defcustom dired-keep-marker-hardlink ?H |
| 122 | "*Controls marking of newly made hard links. |
| 123 | If t, they are marked if and as the files linked to were marked. |
| 124 | If a character, new links are unconditionally marked with that character." |
| 125 | :type '(choice (const :tag "Keep" t) |
| 126 | (character :tag "Mark")) |
| 127 | :group 'dired-mark) |
| 128 | |
| 129 | ;;;###autoload |
| 130 | (defcustom dired-keep-marker-symlink ?Y |
| 131 | "*Controls marking of newly made symbolic links. |
| 132 | If t, they are marked if and as the files linked to were marked. |
| 133 | If a character, new links are unconditionally marked with that character." |
| 134 | :type '(choice (const :tag "Keep" t) |
| 135 | (character :tag "Mark")) |
| 136 | :group 'dired-mark) |
| 137 | |
| 138 | ;;;###autoload |
| 139 | (defcustom dired-dwim-target nil |
| 140 | "*If non-nil, dired tries to guess a default target directory. |
| 141 | This means: if there is a dired buffer displayed in the next window, |
| 142 | use its current subdir, instead of the current subdir of this dired buffer. |
| 143 | |
| 144 | The target is used in the prompt for file copy, rename etc." |
| 145 | :type 'boolean |
| 146 | :group 'dired) |
| 147 | |
| 148 | ;;;###autoload |
| 149 | (defcustom dired-copy-preserve-time t |
| 150 | "*If non-nil, Dired preserves the last-modified time in a file copy. |
| 151 | \(This works on only some systems.)" |
| 152 | :type 'boolean |
| 153 | :group 'dired) |
| 154 | |
| 155 | ;;; Hook variables |
| 156 | |
| 157 | (defvar dired-load-hook nil |
| 158 | "Run after loading dired. |
| 159 | You can customize key bindings or load extensions with this.") |
| 160 | |
| 161 | (defvar dired-mode-hook nil |
| 162 | "Run at the very end of dired-mode.") |
| 163 | |
| 164 | (defvar dired-before-readin-hook nil |
| 165 | "This hook is run before a dired buffer is read in (created or reverted).") |
| 166 | |
| 167 | (defvar dired-after-readin-hook nil |
| 168 | "Hook run after each time a file or directory is read by Dired. |
| 169 | After each listing of a file or directory, this hook is run |
| 170 | with the buffer narrowed to the listing.") |
| 171 | ;; Note this can't simply be run inside function `dired-ls' as the hook |
| 172 | ;; functions probably depend on the dired-subdir-alist to be OK. |
| 173 | |
| 174 | ;;; Internal variables |
| 175 | |
| 176 | (defvar dired-marker-char ?* ; the answer is 42 |
| 177 | ;; so that you can write things like |
| 178 | ;; (let ((dired-marker-char ?X)) |
| 179 | ;; ;; great code using X markers ... |
| 180 | ;; ) |
| 181 | ;; For example, commands operating on two sets of files, A and B. |
| 182 | ;; Or marking files with digits 0-9. This could implicate |
| 183 | ;; concentric sets or an order for the marked files. |
| 184 | ;; The code depends on dynamic scoping on the marker char. |
| 185 | "In Dired, the current mark character. |
| 186 | This is what the `do' commands look for and what the `mark' commands store.") |
| 187 | |
| 188 | (defvar dired-del-marker ?D |
| 189 | "Character used to flag files for deletion.") |
| 190 | |
| 191 | (defvar dired-shrink-to-fit |
| 192 | t |
| 193 | ;; I see no reason ever to make this nil -- rms. |
| 194 | ;; (> baud-rate search-slow-speed) |
| 195 | "Non-nil means Dired shrinks the display buffer to fit the marked files.") |
| 196 | |
| 197 | (defvar dired-flagging-regexp nil);; Last regexp used to flag files. |
| 198 | |
| 199 | (defvar dired-file-version-alist) |
| 200 | |
| 201 | (defvar dired-directory nil |
| 202 | "The directory name or shell wildcard that was used as argument to `ls'. |
| 203 | Local to each dired buffer. May be a list, in which case the car is the |
| 204 | directory name and the cdr is the actual files to list.") |
| 205 | |
| 206 | (defvar dired-actual-switches nil |
| 207 | "The value of `dired-listing-switches' used to make this buffer's text.") |
| 208 | |
| 209 | (defvar dired-re-inode-size "[0-9 \t]*" |
| 210 | "Regexp for optional initial inode and file size as made by `ls -i -s'.") |
| 211 | |
| 212 | ;; These regexps must be tested at beginning-of-line, but are also |
| 213 | ;; used to search for next matches, so neither omitting "^" nor |
| 214 | ;; replacing "^" by "\n" (to make it slightly faster) will work. |
| 215 | |
| 216 | (defvar dired-re-mark "^[^ \n]") |
| 217 | ;; "Regexp matching a marked line. |
| 218 | ;; Important: the match ends just after the marker." |
| 219 | (defvar dired-re-maybe-mark "^. ") |
| 220 | (defvar dired-re-dir (concat dired-re-maybe-mark dired-re-inode-size "d")) |
| 221 | (defvar dired-re-sym (concat dired-re-maybe-mark dired-re-inode-size "l")) |
| 222 | (defvar dired-re-exe;; match ls permission string of an executable file |
| 223 | (mapconcat (function |
| 224 | (lambda (x) |
| 225 | (concat dired-re-maybe-mark dired-re-inode-size x))) |
| 226 | '("-[-r][-w][xs][-r][-w].[-r][-w]." |
| 227 | "-[-r][-w].[-r][-w][xs][-r][-w]." |
| 228 | "-[-r][-w].[-r][-w].[-r][-w][xst]") |
| 229 | "\\|")) |
| 230 | (defvar dired-re-perms "[-bcdlps][-r][-w].[-r][-w].[-r][-w].") |
| 231 | (defvar dired-re-dot "^.* \\.\\.?$") |
| 232 | |
| 233 | ;; The subdirectory names in this list are expanded. |
| 234 | (defvar dired-subdir-alist nil |
| 235 | "Association list of subdirectories and their buffer positions. |
| 236 | Each subdirectory has an element: (DIRNAME . STARTMARKER). |
| 237 | The order of elements is the reverse of the order in the buffer. |
| 238 | In simple cases, this list contains one element.") |
| 239 | |
| 240 | (defvar dired-subdir-regexp "^. \\([^\n\r]+\\)\\(:\\)[\n\r]" |
| 241 | "Regexp matching a maybe hidden subdirectory line in `ls -lR' output. |
| 242 | Subexpression 1 is the subdirectory proper, no trailing colon. |
| 243 | The match starts at the beginning of the line and ends after the end |
| 244 | of the line (\\n or \\r). |
| 245 | Subexpression 2 must end right before the \\n or \\r.") |
| 246 | |
| 247 | (defvar dired-font-lock-keywords |
| 248 | (list |
| 249 | ;; |
| 250 | ;; Directory headers. |
| 251 | (list dired-subdir-regexp '(1 font-lock-type-face)) |
| 252 | ;; |
| 253 | ;; We make heavy use of MATCH-ANCHORED, since the regexps don't identify the |
| 254 | ;; file name itself. We search for Dired defined regexps, and then use the |
| 255 | ;; Dired defined function `dired-move-to-filename' before searching for the |
| 256 | ;; simple regexp ".+". It is that regexp which matches the file name. |
| 257 | ;; |
| 258 | ;; Dired marks. |
| 259 | (list dired-re-mark |
| 260 | '(0 font-lock-constant-face) |
| 261 | '(".+" (dired-move-to-filename) nil (0 font-lock-warning-face))) |
| 262 | ;; People who are paranoid about security would consider this more |
| 263 | ;; important than other things such as whether it is a directory. |
| 264 | ;; But we don't want to encourage paranoia, so our default |
| 265 | ;; should be what's most useful for non-paranoids. -- rms. |
| 266 | ;;; ;; |
| 267 | ;;; ;; Files that are group or world writable. |
| 268 | ;;; (list (concat dired-re-maybe-mark dired-re-inode-size |
| 269 | ;;; "\\([-d]\\(....w....\\|.......w.\\)\\)") |
| 270 | ;;; '(1 font-lock-comment-face) |
| 271 | ;;; '(".+" (dired-move-to-filename) nil (0 font-lock-comment-face))) |
| 272 | ;; |
| 273 | ;; Subdirectories. |
| 274 | (list dired-re-dir |
| 275 | '(".+" (dired-move-to-filename) nil (0 font-lock-function-name-face))) |
| 276 | ;; |
| 277 | ;; Symbolic links. |
| 278 | (list dired-re-sym |
| 279 | '(".+" (dired-move-to-filename) nil (0 font-lock-keyword-face))) |
| 280 | ;; |
| 281 | ;; Files suffixed with `completion-ignored-extensions'. |
| 282 | '(eval . |
| 283 | (let ((extensions (mapcar 'regexp-quote completion-ignored-extensions))) |
| 284 | ;; It is quicker to first find just an extension, then go back to the |
| 285 | ;; start of that file name. So we do this complex MATCH-ANCHORED form. |
| 286 | (list (concat "\\(" (mapconcat 'identity extensions "\\|") "\\|#\\)$") |
| 287 | '(".+" (dired-move-to-filename) nil (0 font-lock-string-face)))))) |
| 288 | "Additional expressions to highlight in Dired mode.") |
| 289 | \f |
| 290 | ;;; Macros must be defined before they are used, for the byte compiler. |
| 291 | |
| 292 | ;; Mark all files for which CONDITION evals to non-nil. |
| 293 | ;; CONDITION is evaluated on each line, with point at beginning of line. |
| 294 | ;; MSG is a noun phrase for the type of files being marked. |
| 295 | ;; It should end with a noun that can be pluralized by adding `s'. |
| 296 | ;; Return value is the number of files marked, or nil if none were marked. |
| 297 | (defmacro dired-mark-if (predicate msg) |
| 298 | (` (let (buffer-read-only count) |
| 299 | (save-excursion |
| 300 | (setq count 0) |
| 301 | (if (, msg) (message "Marking %ss..." (, msg))) |
| 302 | (goto-char (point-min)) |
| 303 | (while (not (eobp)) |
| 304 | (if (, predicate) |
| 305 | (progn |
| 306 | (delete-char 1) |
| 307 | (insert dired-marker-char) |
| 308 | (setq count (1+ count)))) |
| 309 | (forward-line 1)) |
| 310 | (if (, msg) (message "%s %s%s %s%s." |
| 311 | count |
| 312 | (, msg) |
| 313 | (dired-plural-s count) |
| 314 | (if (eq dired-marker-char ?\040) "un" "") |
| 315 | (if (eq dired-marker-char dired-del-marker) |
| 316 | "flagged" "marked")))) |
| 317 | (and (> count 0) count)))) |
| 318 | |
| 319 | (defmacro dired-map-over-marks (body arg &optional show-progress) |
| 320 | ;; "Macro: Perform BODY with point somewhere on each marked line |
| 321 | ;;and return a list of BODY's results. |
| 322 | ;;If no marked file could be found, execute BODY on the current line. |
| 323 | ;; If ARG is an integer, use the next ARG (or previous -ARG, if ARG<0) |
| 324 | ;; files instead of the marked files. |
| 325 | ;; In that case point is dragged along. This is so that commands on |
| 326 | ;; the next ARG (instead of the marked) files can be chained easily. |
| 327 | ;; If ARG is otherwise non-nil, use current file instead. |
| 328 | ;;If optional third arg SHOW-PROGRESS evaluates to non-nil, |
| 329 | ;; redisplay the dired buffer after each file is processed. |
| 330 | ;;No guarantee is made about the position on the marked line. |
| 331 | ;; BODY must ensure this itself if it depends on this. |
| 332 | ;;Search starts at the beginning of the buffer, thus the car of the list |
| 333 | ;; corresponds to the line nearest to the buffer's bottom. This |
| 334 | ;; is also true for (positive and negative) integer values of ARG. |
| 335 | ;;BODY should not be too long as it is expanded four times." |
| 336 | ;; |
| 337 | ;;Warning: BODY must not add new lines before point - this may cause an |
| 338 | ;;endless loop. |
| 339 | ;;This warning should not apply any longer, sk 2-Sep-1991 14:10. |
| 340 | (` (prog1 |
| 341 | (let (buffer-read-only case-fold-search found results) |
| 342 | (if (, arg) |
| 343 | (if (integerp (, arg)) |
| 344 | (progn;; no save-excursion, want to move point. |
| 345 | (dired-repeat-over-lines |
| 346 | (, arg) |
| 347 | (function (lambda () |
| 348 | (if (, show-progress) (sit-for 0)) |
| 349 | (setq results (cons (, body) results))))) |
| 350 | (if (< (, arg) 0) |
| 351 | (nreverse results) |
| 352 | results)) |
| 353 | ;; non-nil, non-integer ARG means use current file: |
| 354 | (list (, body))) |
| 355 | (let ((regexp (dired-marker-regexp)) next-position) |
| 356 | (save-excursion |
| 357 | (goto-char (point-min)) |
| 358 | ;; remember position of next marked file before BODY |
| 359 | ;; can insert lines before the just found file, |
| 360 | ;; confusing us by finding the same marked file again |
| 361 | ;; and again and... |
| 362 | (setq next-position (and (re-search-forward regexp nil t) |
| 363 | (point-marker)) |
| 364 | found (not (null next-position))) |
| 365 | (while next-position |
| 366 | (goto-char next-position) |
| 367 | (if (, show-progress) (sit-for 0)) |
| 368 | (setq results (cons (, body) results)) |
| 369 | ;; move after last match |
| 370 | (goto-char next-position) |
| 371 | (forward-line 1) |
| 372 | (set-marker next-position nil) |
| 373 | (setq next-position (and (re-search-forward regexp nil t) |
| 374 | (point-marker))))) |
| 375 | (if found |
| 376 | results |
| 377 | (list (, body)))))) |
| 378 | ;; save-excursion loses, again |
| 379 | (dired-move-to-filename)))) |
| 380 | |
| 381 | (defun dired-get-marked-files (&optional localp arg) |
| 382 | "Return the marked files' names as list of strings. |
| 383 | The list is in the same order as the buffer, that is, the car is the |
| 384 | first marked file. |
| 385 | Values returned are normally absolute pathnames. |
| 386 | Optional arg LOCALP as in `dired-get-filename'. |
| 387 | Optional second argument ARG forces to use other files. If ARG is an |
| 388 | integer, use the next ARG files. If ARG is otherwise non-nil, use |
| 389 | current file. Usually ARG comes from the current prefix arg." |
| 390 | (save-excursion |
| 391 | (nreverse (dired-map-over-marks (dired-get-filename localp) arg)))) |
| 392 | |
| 393 | \f |
| 394 | ;; Function dired-ls is redefinable for VMS, ange-ftp, Prospero or |
| 395 | ;; other special applications. |
| 396 | \f |
| 397 | ;; The dired command |
| 398 | |
| 399 | (defun dired-read-dir-and-switches (str) |
| 400 | ;; For use in interactive. |
| 401 | (reverse (list |
| 402 | (if current-prefix-arg |
| 403 | (read-string "Dired listing switches: " |
| 404 | dired-listing-switches)) |
| 405 | (read-file-name (format "Dired %s(directory): " str) |
| 406 | nil default-directory nil)))) |
| 407 | |
| 408 | ;;;###autoload (define-key ctl-x-map "d" 'dired) |
| 409 | ;;;###autoload |
| 410 | (defun dired (dirname &optional switches) |
| 411 | "\"Edit\" directory DIRNAME--delete, rename, print, etc. some files in it. |
| 412 | Optional second argument SWITCHES specifies the `ls' options used. |
| 413 | \(Interactively, use a prefix argument to be able to specify SWITCHES.) |
| 414 | Dired displays a list of files in DIRNAME (which may also have |
| 415 | shell wildcards appended to select certain files). If DIRNAME is a cons, |
| 416 | its first element is taken as the directory name and the rest as an explicit |
| 417 | list of files to make directory entries for. |
| 418 | \\<dired-mode-map>\ |
| 419 | You can move around in it with the usual commands. |
| 420 | You can flag files for deletion with \\[dired-flag-file-deletion] and then |
| 421 | delete them by typing \\[dired-do-flagged-delete]. |
| 422 | Type \\[describe-mode] after entering dired for more info. |
| 423 | |
| 424 | If DIRNAME is already in a dired buffer, that buffer is used without refresh." |
| 425 | ;; Cannot use (interactive "D") because of wildcards. |
| 426 | (interactive (dired-read-dir-and-switches "")) |
| 427 | (switch-to-buffer (dired-noselect dirname switches))) |
| 428 | |
| 429 | ;;;###autoload (define-key ctl-x-4-map "d" 'dired-other-window) |
| 430 | ;;;###autoload |
| 431 | (defun dired-other-window (dirname &optional switches) |
| 432 | "\"Edit\" directory DIRNAME. Like `dired' but selects in another window." |
| 433 | (interactive (dired-read-dir-and-switches "in other window ")) |
| 434 | (switch-to-buffer-other-window (dired-noselect dirname switches))) |
| 435 | |
| 436 | ;;;###autoload (define-key ctl-x-5-map "d" 'dired-other-frame) |
| 437 | ;;;###autoload |
| 438 | (defun dired-other-frame (dirname &optional switches) |
| 439 | "\"Edit\" directory DIRNAME. Like `dired' but makes a new frame." |
| 440 | (interactive (dired-read-dir-and-switches "in other frame ")) |
| 441 | (switch-to-buffer-other-frame (dired-noselect dirname switches))) |
| 442 | |
| 443 | ;;;###autoload |
| 444 | (defun dired-noselect (dir-or-list &optional switches) |
| 445 | "Like `dired' but returns the dired buffer as value, does not select it." |
| 446 | (or dir-or-list (setq dir-or-list default-directory)) |
| 447 | ;; This loses the distinction between "/foo/*/" and "/foo/*" that |
| 448 | ;; some shells make: |
| 449 | (let (dirname initially-was-dirname) |
| 450 | (if (consp dir-or-list) |
| 451 | (setq dirname (car dir-or-list)) |
| 452 | (setq dirname dir-or-list)) |
| 453 | (setq initially-was-dirname |
| 454 | (string= (file-name-as-directory dirname) dirname)) |
| 455 | (setq dirname (abbreviate-file-name |
| 456 | (expand-file-name (directory-file-name dirname)))) |
| 457 | (if find-file-visit-truename |
| 458 | (setq dirname (file-truename dirname))) |
| 459 | ;; If the argument was syntactically a directory name not a file name, |
| 460 | ;; or if it happens to name a file that is a directory, |
| 461 | ;; convert it syntactically to a directory name. |
| 462 | ;; The reason for checking initially-was-dirname |
| 463 | ;; and not just file-directory-p |
| 464 | ;; is that file-directory-p is slow over ftp. |
| 465 | (if (or initially-was-dirname (file-directory-p dirname)) |
| 466 | (setq dirname (file-name-as-directory dirname))) |
| 467 | (if (consp dir-or-list) |
| 468 | (setq dir-or-list (cons dirname (cdr dir-or-list))) |
| 469 | (setq dir-or-list dirname)) |
| 470 | (dired-internal-noselect dir-or-list switches))) |
| 471 | |
| 472 | ;; Separate function from dired-noselect for the sake of dired-vms.el. |
| 473 | (defun dired-internal-noselect (dir-or-list &optional switches mode) |
| 474 | ;; If there is an existing dired buffer for DIRNAME, just leave |
| 475 | ;; buffer as it is (don't even call dired-revert). |
| 476 | ;; This saves time especially for deep trees or with ange-ftp. |
| 477 | ;; The user can type `g'easily, and it is more consistent with find-file. |
| 478 | ;; But if SWITCHES are given they are probably different from the |
| 479 | ;; buffer's old value, so call dired-sort-other, which does |
| 480 | ;; revert the buffer. |
| 481 | ;; A pity we can't possibly do "Directory has changed - refresh? " |
| 482 | ;; like find-file does. |
| 483 | ;; Optional argument MODE is passed to dired-find-buffer-nocreate, |
| 484 | ;; see there. |
| 485 | (let* ((dirname (if (consp dir-or-list) (car dir-or-list) dir-or-list)) |
| 486 | ;; The following line used to use dir-or-list. |
| 487 | ;; That never found an existing buffer, in the case |
| 488 | ;; where it is a list. |
| 489 | (buffer (dired-find-buffer-nocreate dirname mode)) |
| 490 | ;; note that buffer already is in dired-mode, if found |
| 491 | (new-buffer-p (not buffer)) |
| 492 | (old-buf (current-buffer))) |
| 493 | (or buffer |
| 494 | (let ((default-major-mode 'fundamental-mode)) |
| 495 | ;; We don't want default-major-mode to run hooks and set auto-fill |
| 496 | ;; or whatever, now that dired-mode does not |
| 497 | ;; kill-all-local-variables any longer. |
| 498 | (setq buffer (create-file-buffer (directory-file-name dirname))))) |
| 499 | (set-buffer buffer) |
| 500 | (if (not new-buffer-p) ; existing buffer ... |
| 501 | (cond (switches ; ... but new switches |
| 502 | ;; file list may have changed |
| 503 | (if (consp dir-or-list) |
| 504 | (setq dired-directory dir-or-list)) |
| 505 | ;; this calls dired-revert |
| 506 | (dired-sort-other switches)) |
| 507 | ;; If directory has changed on disk, offer to revert. |
| 508 | ((if (let ((attributes (file-attributes dirname)) |
| 509 | (modtime (visited-file-modtime))) |
| 510 | (or (eq modtime 0) |
| 511 | (not (eq (car attributes) t)) |
| 512 | (and (= (car (nth 5 attributes)) (car modtime)) |
| 513 | (= (nth 1 (nth 5 attributes)) (cdr modtime))))) |
| 514 | nil |
| 515 | (message "%s" |
| 516 | (substitute-command-keys |
| 517 | "Directory has changed on disk; type \\[revert-buffer] to update Dired"))))) |
| 518 | ;; Else a new buffer |
| 519 | (setq default-directory |
| 520 | ;; We can do this unconditionally |
| 521 | ;; because dired-noselect ensures that the name |
| 522 | ;; is passed in directory name syntax |
| 523 | ;; if it was the name of a directory at all. |
| 524 | (file-name-directory dirname)) |
| 525 | (or switches (setq switches dired-listing-switches)) |
| 526 | (if mode (funcall mode) |
| 527 | (dired-mode dirname switches)) |
| 528 | ;; default-directory and dired-actual-switches are set now |
| 529 | ;; (buffer-local), so we can call dired-readin: |
| 530 | (let ((failed t)) |
| 531 | (unwind-protect |
| 532 | (progn (dired-readin dir-or-list buffer) |
| 533 | (setq failed nil)) |
| 534 | ;; dired-readin can fail if parent directories are inaccessible. |
| 535 | ;; Don't leave an empty buffer around in that case. |
| 536 | (if failed (kill-buffer buffer)))) |
| 537 | ;; No need to narrow since the whole buffer contains just |
| 538 | ;; dired-readin's output, nothing else. The hook can |
| 539 | ;; successfully use dired functions (e.g. dired-get-filename) |
| 540 | ;; as the subdir-alist has been built in dired-readin. |
| 541 | (run-hooks 'dired-after-readin-hook) |
| 542 | (goto-char (point-min)) |
| 543 | (dired-initial-position dirname)) |
| 544 | (set-buffer old-buf) |
| 545 | buffer)) |
| 546 | |
| 547 | (defun dired-find-buffer-nocreate (dirname &optional mode) |
| 548 | ;; This differs from dired-buffers-for-dir in that it does not consider |
| 549 | ;; subdirs of default-directory and searches for the first match only. |
| 550 | ;; Also, the major mode must be MODE. |
| 551 | (let (found (blist dired-buffers)) ; was (buffer-list) |
| 552 | (or mode (setq mode 'dired-mode)) |
| 553 | (while blist |
| 554 | (if (null (buffer-name (cdr (car blist)))) |
| 555 | (setq blist (cdr blist)) |
| 556 | (save-excursion |
| 557 | (set-buffer (cdr (car blist))) |
| 558 | (if (and (eq major-mode mode) |
| 559 | (if (consp dired-directory) |
| 560 | (equal (car dired-directory) dirname) |
| 561 | (equal dired-directory dirname))) |
| 562 | (setq found (cdr (car blist)) |
| 563 | blist nil) |
| 564 | (setq blist (cdr blist)))))) |
| 565 | found)) |
| 566 | |
| 567 | \f |
| 568 | ;; Read in a new dired buffer |
| 569 | |
| 570 | ;; dired-readin differs from dired-insert-subdir in that it accepts |
| 571 | ;; wildcards, erases the buffer, and builds the subdir-alist anew |
| 572 | ;; (including making it buffer-local and clearing it first). |
| 573 | (defun dired-readin (dir-or-list buffer) |
| 574 | ;; default-directory and dired-actual-switches must be buffer-local |
| 575 | ;; and initialized by now. |
| 576 | ;; Thus we can test (equal default-directory dirname) instead of |
| 577 | ;; (file-directory-p dirname) and save a filesystem transaction. |
| 578 | ;; Also, we can run this hook which may want to modify the switches |
| 579 | ;; based on default-directory, e.g. with ange-ftp to a SysV host |
| 580 | ;; where ls won't understand -Al switches. |
| 581 | (let (dirname) |
| 582 | (if (consp dir-or-list) |
| 583 | (setq dirname (car dir-or-list)) |
| 584 | (setq dirname dir-or-list)) |
| 585 | (setq dirname (expand-file-name dirname)) |
| 586 | (if (consp dir-or-list) |
| 587 | (setq dir-or-list (cons dirname (cdr dir-or-list)))) |
| 588 | (run-hooks 'dired-before-readin-hook) |
| 589 | (save-excursion |
| 590 | (message "Reading directory %s..." dirname) |
| 591 | (set-buffer buffer) |
| 592 | (let (buffer-read-only (failed t)) |
| 593 | (widen) |
| 594 | (erase-buffer) |
| 595 | (dired-readin-insert dir-or-list) |
| 596 | (indent-rigidly (point-min) (point-max) 2) |
| 597 | ;; We need this to make the root dir have a header line as all |
| 598 | ;; other subdirs have: |
| 599 | (goto-char (point-min)) |
| 600 | (if (not (looking-at "^ /.*:$")) |
| 601 | (dired-insert-headerline default-directory)) |
| 602 | ;; can't run dired-after-readin-hook here, it may depend on the subdir |
| 603 | ;; alist to be OK. |
| 604 | ) |
| 605 | (message "Reading directory %s...done" dirname) |
| 606 | ;; Must first make alist buffer local and set it to nil because |
| 607 | ;; dired-build-subdir-alist will call dired-clear-alist first |
| 608 | (set (make-local-variable 'dired-subdir-alist) nil) |
| 609 | (dired-build-subdir-alist) |
| 610 | (let ((attributes (file-attributes dirname))) |
| 611 | (if (eq (car attributes) t) |
| 612 | (set-visited-file-modtime (nth 5 attributes)))) |
| 613 | (set-buffer-modified-p nil)))) |
| 614 | |
| 615 | ;; Subroutines of dired-readin |
| 616 | |
| 617 | (defun dired-readin-insert (dir-or-list) |
| 618 | ;; Just insert listing for the passed-in directory or |
| 619 | ;; directory-and-file list, assuming a clean buffer. |
| 620 | (let (dirname) |
| 621 | (if (consp dir-or-list) |
| 622 | (setq dirname (car dir-or-list)) |
| 623 | (setq dirname dir-or-list)) |
| 624 | ;; Expand before comparing in case one or both have been abbreviated. |
| 625 | (if (and (equal (expand-file-name default-directory) |
| 626 | (expand-file-name dirname)) |
| 627 | (not (consp dir-or-list))) |
| 628 | ;; If we are reading a whole single directory... |
| 629 | (dired-insert-directory dir-or-list dired-actual-switches nil t) |
| 630 | (if (not (file-readable-p |
| 631 | (directory-file-name (file-name-directory dirname)))) |
| 632 | (error "Directory %s inaccessible or nonexistent" dirname) |
| 633 | ;; Else assume it contains wildcards, |
| 634 | ;; unless it is an explicit list of files. |
| 635 | (dired-insert-directory dir-or-list dired-actual-switches |
| 636 | (not (listp dir-or-list))) |
| 637 | (save-excursion ;; insert wildcard instead of total line: |
| 638 | (goto-char (point-min)) |
| 639 | (insert "wildcard " (file-name-nondirectory dirname) "\n")))))) |
| 640 | |
| 641 | (defun dired-insert-directory (dir-or-list switches &optional wildcard full-p) |
| 642 | ;; Do the right thing whether dir-or-list is atomic or not. If it is, |
| 643 | ;; inset all files listed in the cdr (the car is the passed-in directory |
| 644 | ;; list). |
| 645 | (let ((opoint (point)) |
| 646 | (process-environment (copy-sequence process-environment)) |
| 647 | end) |
| 648 | ;; We used to specify the C locale here, to force English month names; |
| 649 | ;; but this should not be necessary any more, |
| 650 | ;; with the new value of dired-move-to-filename-regexp. |
| 651 | (if (consp dir-or-list) |
| 652 | ;; In this case, use the file names in the cdr |
| 653 | ;; exactly as originally given to dired-noselect. |
| 654 | (mapcar |
| 655 | (function (lambda (x) (insert-directory x switches wildcard full-p))) |
| 656 | (cdr dir-or-list)) |
| 657 | ;; Expand the file name here because it may have been abbreviated |
| 658 | ;; in dired-noselect. |
| 659 | (insert-directory (expand-file-name dir-or-list) switches wildcard full-p)) |
| 660 | ;; Quote certain characters, unless ls quoted them for us. |
| 661 | (if (not (string-match "b" dired-actual-switches)) |
| 662 | (save-excursion |
| 663 | (setq end (point-marker)) |
| 664 | (goto-char opoint) |
| 665 | (while (search-forward "\\" end t) |
| 666 | (replace-match "\\\\" nil t)) |
| 667 | (goto-char opoint) |
| 668 | (while (search-forward "\^m" end t) |
| 669 | (replace-match "\\015" nil t)) |
| 670 | (set-marker end nil))) |
| 671 | (dired-insert-set-properties opoint (point))) |
| 672 | (setq dired-directory dir-or-list)) |
| 673 | |
| 674 | ;; Make the file names highlight when the mouse is on them. |
| 675 | (defun dired-insert-set-properties (beg end) |
| 676 | (save-excursion |
| 677 | (goto-char beg) |
| 678 | (while (< (point) end) |
| 679 | (condition-case nil |
| 680 | (if (dired-move-to-filename) |
| 681 | (put-text-property (point) |
| 682 | (save-excursion |
| 683 | (dired-move-to-end-of-filename) |
| 684 | (point)) |
| 685 | 'mouse-face 'highlight)) |
| 686 | (error nil)) |
| 687 | (forward-line 1)))) |
| 688 | |
| 689 | (defun dired-insert-headerline (dir);; also used by dired-insert-subdir |
| 690 | ;; Insert DIR's headerline with no trailing slash, exactly like ls |
| 691 | ;; would, and put cursor where dired-build-subdir-alist puts subdir |
| 692 | ;; boundaries. |
| 693 | (save-excursion (insert " " (directory-file-name dir) ":\n"))) |
| 694 | |
| 695 | \f |
| 696 | ;; Reverting a dired buffer |
| 697 | |
| 698 | (defun dired-revert (&optional arg noconfirm) |
| 699 | ;; Reread the dired buffer. Must also be called after |
| 700 | ;; dired-actual-switches have changed. |
| 701 | ;; Should not fail even on completely garbaged buffers. |
| 702 | ;; Preserves old cursor, marks/flags, hidden-p. |
| 703 | (widen) ; just in case user narrowed |
| 704 | (let ((opoint (point)) |
| 705 | (ofile (dired-get-filename nil t)) |
| 706 | (mark-alist nil) ; save marked files |
| 707 | (hidden-subdirs (dired-remember-hidden)) |
| 708 | (old-subdir-alist (cdr (reverse dired-subdir-alist))) ; except pwd |
| 709 | (case-fold-search nil) ; we check for upper case ls flags |
| 710 | buffer-read-only) |
| 711 | (goto-char (point-min)) |
| 712 | (setq mark-alist;; only after dired-remember-hidden since this unhides: |
| 713 | (dired-remember-marks (point-min) (point-max))) |
| 714 | ;; treat top level dir extra (it may contain wildcards) |
| 715 | (dired-uncache |
| 716 | (if (consp dired-directory) (car dired-directory) dired-directory)) |
| 717 | (dired-readin dired-directory (current-buffer)) |
| 718 | (let ((dired-after-readin-hook nil)) |
| 719 | ;; don't run that hook for each subdir... |
| 720 | (dired-insert-old-subdirs old-subdir-alist)) |
| 721 | (dired-mark-remembered mark-alist) ; mark files that were marked |
| 722 | ;; ... run the hook for the whole buffer, and only after markers |
| 723 | ;; have been reinserted (else omitting in dired-x would omit marked files) |
| 724 | (run-hooks 'dired-after-readin-hook) ; no need to narrow |
| 725 | (or (and ofile (dired-goto-file ofile)) ; move cursor to where it |
| 726 | (goto-char opoint)) ; was before |
| 727 | (dired-move-to-filename) |
| 728 | (save-excursion ; hide subdirs that were hidden |
| 729 | (mapcar (function (lambda (dir) |
| 730 | (if (dired-goto-subdir dir) |
| 731 | (dired-hide-subdir 1)))) |
| 732 | hidden-subdirs))) |
| 733 | ;; outside of the let scope |
| 734 | ;;; Might as well not override the user if the user changed this. |
| 735 | ;;; (setq buffer-read-only t) |
| 736 | ) |
| 737 | |
| 738 | ;; Subroutines of dired-revert |
| 739 | ;; Some of these are also used when inserting subdirs. |
| 740 | |
| 741 | (defun dired-remember-marks (beg end) |
| 742 | ;; Return alist of files and their marks, from BEG to END. |
| 743 | (if selective-display ; must unhide to make this work. |
| 744 | (let (buffer-read-only) |
| 745 | (subst-char-in-region beg end ?\r ?\n))) |
| 746 | (let (fil chr alist) |
| 747 | (save-excursion |
| 748 | (goto-char beg) |
| 749 | (while (re-search-forward dired-re-mark end t) |
| 750 | (if (setq fil (dired-get-filename nil t)) |
| 751 | (setq chr (preceding-char) |
| 752 | alist (cons (cons fil chr) alist))))) |
| 753 | alist)) |
| 754 | |
| 755 | ;; Mark all files remembered in ALIST. |
| 756 | ;; Each element of ALIST looks like (FILE . MARKERCHAR). |
| 757 | (defun dired-mark-remembered (alist) |
| 758 | (let (elt fil chr) |
| 759 | (while alist |
| 760 | (setq elt (car alist) |
| 761 | alist (cdr alist) |
| 762 | fil (car elt) |
| 763 | chr (cdr elt)) |
| 764 | (if (dired-goto-file fil) |
| 765 | (save-excursion |
| 766 | (beginning-of-line) |
| 767 | (delete-char 1) |
| 768 | (insert chr)))))) |
| 769 | |
| 770 | ;; Return a list of names of subdirs currently hidden. |
| 771 | (defun dired-remember-hidden () |
| 772 | (let ((l dired-subdir-alist) dir pos result) |
| 773 | (while l |
| 774 | (setq dir (car (car l)) |
| 775 | pos (cdr (car l)) |
| 776 | l (cdr l)) |
| 777 | (goto-char pos) |
| 778 | (skip-chars-forward "^\r\n") |
| 779 | (if (eq (following-char) ?\r) |
| 780 | (setq result (cons dir result)))) |
| 781 | result)) |
| 782 | |
| 783 | ;; Try to insert all subdirs that were displayed before, |
| 784 | ;; according to the former subdir alist OLD-SUBDIR-ALIST. |
| 785 | (defun dired-insert-old-subdirs (old-subdir-alist) |
| 786 | (or (string-match "R" dired-actual-switches) |
| 787 | (let (elt dir) |
| 788 | (while old-subdir-alist |
| 789 | (setq elt (car old-subdir-alist) |
| 790 | old-subdir-alist (cdr old-subdir-alist) |
| 791 | dir (car elt)) |
| 792 | (condition-case () |
| 793 | (progn |
| 794 | (dired-uncache dir) |
| 795 | (dired-insert-subdir dir)) |
| 796 | (error nil)))))) |
| 797 | |
| 798 | ;; Remove directory DIR from any directory cache. |
| 799 | (defun dired-uncache (dir) |
| 800 | (let ((handler (find-file-name-handler dir 'dired-uncache))) |
| 801 | (if handler |
| 802 | (funcall handler 'dired-uncache dir)))) |
| 803 | \f |
| 804 | ;; dired mode key bindings and initialization |
| 805 | |
| 806 | (defvar dired-mode-map nil "Local keymap for dired-mode buffers.") |
| 807 | (if dired-mode-map |
| 808 | nil |
| 809 | ;; This looks ugly when substitute-command-keys uses C-d instead d: |
| 810 | ;; (define-key dired-mode-map "\C-d" 'dired-flag-file-deletion) |
| 811 | |
| 812 | (let ((map (make-keymap))) |
| 813 | (suppress-keymap map) |
| 814 | (define-key map [mouse-2] 'dired-mouse-find-file-other-window) |
| 815 | ;; Commands to mark or flag certain categories of files |
| 816 | (define-key map "#" 'dired-flag-auto-save-files) |
| 817 | (define-key map "." 'dired-clean-directory) |
| 818 | (define-key map "~" 'dired-flag-backup-files) |
| 819 | (define-key map "&" 'dired-flag-garbage-files) |
| 820 | ;; Upper case keys (except !) for operating on the marked files |
| 821 | (define-key map "A" 'dired-do-search) |
| 822 | (define-key map "C" 'dired-do-copy) |
| 823 | (define-key map "B" 'dired-do-byte-compile) |
| 824 | (define-key map "D" 'dired-do-delete) |
| 825 | (define-key map "G" 'dired-do-chgrp) |
| 826 | (define-key map "H" 'dired-do-hardlink) |
| 827 | (define-key map "L" 'dired-do-load) |
| 828 | (define-key map "M" 'dired-do-chmod) |
| 829 | (define-key map "O" 'dired-do-chown) |
| 830 | (define-key map "P" 'dired-do-print) |
| 831 | (define-key map "Q" 'dired-do-query-replace) |
| 832 | (define-key map "R" 'dired-do-rename) |
| 833 | (define-key map "S" 'dired-do-symlink) |
| 834 | (define-key map "X" 'dired-do-shell-command) |
| 835 | (define-key map "Z" 'dired-do-compress) |
| 836 | (define-key map "!" 'dired-do-shell-command) |
| 837 | ;; Comparison commands |
| 838 | (define-key map "=" 'dired-diff) |
| 839 | (define-key map "\M-=" 'dired-backup-diff) |
| 840 | ;; Tree Dired commands |
| 841 | (define-key map "\M-\C-?" 'dired-unmark-all-files) |
| 842 | (define-key map "\M-\C-d" 'dired-tree-down) |
| 843 | (define-key map "\M-\C-u" 'dired-tree-up) |
| 844 | (define-key map "\M-\C-n" 'dired-next-subdir) |
| 845 | (define-key map "\M-\C-p" 'dired-prev-subdir) |
| 846 | ;; move to marked files |
| 847 | (define-key map "\M-{" 'dired-prev-marked-file) |
| 848 | (define-key map "\M-}" 'dired-next-marked-file) |
| 849 | ;; Make all regexp commands share a `%' prefix: |
| 850 | ;; We used to get to the submap via a symbol dired-regexp-prefix, |
| 851 | ;; but that seems to serve little purpose, and copy-keymap |
| 852 | ;; does a better job without it. |
| 853 | (define-key map "%" nil) |
| 854 | (define-key map "%u" 'dired-upcase) |
| 855 | (define-key map "%l" 'dired-downcase) |
| 856 | (define-key map "%d" 'dired-flag-files-regexp) |
| 857 | (define-key map "%g" 'dired-mark-files-containing-regexp) |
| 858 | (define-key map "%m" 'dired-mark-files-regexp) |
| 859 | (define-key map "%r" 'dired-do-rename-regexp) |
| 860 | (define-key map "%C" 'dired-do-copy-regexp) |
| 861 | (define-key map "%H" 'dired-do-hardlink-regexp) |
| 862 | (define-key map "%R" 'dired-do-rename-regexp) |
| 863 | (define-key map "%S" 'dired-do-symlink-regexp) |
| 864 | ;; Commands for marking and unmarking. |
| 865 | (define-key map "*" nil) |
| 866 | (define-key map "**" 'dired-mark-executables) |
| 867 | (define-key map "*/" 'dired-mark-directories) |
| 868 | (define-key map "*@" 'dired-mark-symlinks) |
| 869 | (define-key map "*%" 'dired-mark-files-regexp) |
| 870 | (define-key map "*c" 'dired-change-marks) |
| 871 | (define-key map "*s" 'dired-mark-subdir-files) |
| 872 | (define-key map "*m" 'dired-mark) |
| 873 | (define-key map "*u" 'dired-unmark) |
| 874 | (define-key map "*?" 'dired-unmark-all-files) |
| 875 | (define-key map "*!" 'dired-unmark-all-marks) |
| 876 | (define-key map "*\177" 'dired-unmark-backward) |
| 877 | (define-key map "*\C-n" 'dired-next-marked-file) |
| 878 | (define-key map "*\C-p" 'dired-prev-marked-file) |
| 879 | (define-key map "*t" 'dired-do-toggle) |
| 880 | ;; Lower keys for commands not operating on all the marked files |
| 881 | (define-key map "d" 'dired-flag-file-deletion) |
| 882 | (define-key map "e" 'dired-find-file) |
| 883 | (define-key map "f" 'dired-find-file) |
| 884 | (define-key map "\C-m" 'dired-advertised-find-file) |
| 885 | (define-key map "g" 'revert-buffer) |
| 886 | (define-key map "h" 'describe-mode) |
| 887 | (define-key map "i" 'dired-maybe-insert-subdir) |
| 888 | (define-key map "k" 'dired-do-kill-lines) |
| 889 | (define-key map "l" 'dired-do-redisplay) |
| 890 | (define-key map "m" 'dired-mark) |
| 891 | (define-key map "n" 'dired-next-line) |
| 892 | (define-key map "o" 'dired-find-file-other-window) |
| 893 | (define-key map "\C-o" 'dired-display-file) |
| 894 | (define-key map "p" 'dired-previous-line) |
| 895 | (define-key map "q" 'quit-window) |
| 896 | (define-key map "s" 'dired-sort-toggle-or-edit) |
| 897 | (define-key map "t" 'dired-do-toggle) |
| 898 | (define-key map "u" 'dired-unmark) |
| 899 | (define-key map "v" 'dired-view-file) |
| 900 | (define-key map "x" 'dired-do-flagged-delete) |
| 901 | (define-key map "+" 'dired-create-directory) |
| 902 | ;; moving |
| 903 | (define-key map "<" 'dired-prev-dirline) |
| 904 | (define-key map ">" 'dired-next-dirline) |
| 905 | (define-key map "^" 'dired-up-directory) |
| 906 | (define-key map " " 'dired-next-line) |
| 907 | (define-key map "\C-n" 'dired-next-line) |
| 908 | (define-key map "\C-p" 'dired-previous-line) |
| 909 | (define-key map [down] 'dired-next-line) |
| 910 | (define-key map [up] 'dired-previous-line) |
| 911 | ;; hiding |
| 912 | (define-key map "$" 'dired-hide-subdir) |
| 913 | (define-key map "\M-$" 'dired-hide-all) |
| 914 | ;; misc |
| 915 | (define-key map "?" 'dired-summary) |
| 916 | (define-key map "\177" 'dired-unmark-backward) |
| 917 | (define-key map "\C-_" 'dired-undo) |
| 918 | (define-key map "\C-xu" 'dired-undo) |
| 919 | |
| 920 | ;; Make menu bar items. |
| 921 | |
| 922 | ;; Get rid of the Edit menu bar item to save space. |
| 923 | (define-key map [menu-bar edit] 'undefined) |
| 924 | |
| 925 | (define-key map [menu-bar subdir] |
| 926 | (cons "Subdir" (make-sparse-keymap "Subdir"))) |
| 927 | |
| 928 | (define-key map [menu-bar subdir hide-all] |
| 929 | '("Hide All" . dired-hide-all)) |
| 930 | (define-key map [menu-bar subdir hide-subdir] |
| 931 | '("Hide Subdir" . dired-hide-subdir)) |
| 932 | (define-key map [menu-bar subdir tree-down] |
| 933 | '("Tree Down" . dired-tree-down)) |
| 934 | (define-key map [menu-bar subdir tree-up] |
| 935 | '("Tree Up" . dired-tree-up)) |
| 936 | (define-key map [menu-bar subdir up] |
| 937 | '("Up Directory" . dired-up-directory)) |
| 938 | (define-key map [menu-bar subdir prev-subdir] |
| 939 | '("Prev Subdir" . dired-prev-subdir)) |
| 940 | (define-key map [menu-bar subdir next-subdir] |
| 941 | '("Next Subdir" . dired-next-subdir)) |
| 942 | (define-key map [menu-bar subdir prev-dirline] |
| 943 | '("Prev Dirline" . dired-prev-dirline)) |
| 944 | (define-key map [menu-bar subdir next-dirline] |
| 945 | '("Next Dirline" . dired-next-dirline)) |
| 946 | (define-key map [menu-bar subdir insert] |
| 947 | '("Insert This Subdir" . dired-maybe-insert-subdir)) |
| 948 | |
| 949 | (define-key map [menu-bar immediate] |
| 950 | (cons "Immediate" (make-sparse-keymap "Immediate"))) |
| 951 | |
| 952 | (define-key map [menu-bar immediate revert-buffer] |
| 953 | '("Update Buffer" . revert-buffer)) |
| 954 | |
| 955 | (define-key map [menu-bar immediate dashes] |
| 956 | '("--")) |
| 957 | |
| 958 | (define-key map [menu-bar immediate backup-diff] |
| 959 | '("Compare with Backup" . dired-backup-diff)) |
| 960 | (define-key map [menu-bar immediate diff] |
| 961 | '("Diff" . dired-diff)) |
| 962 | (define-key map [menu-bar immediate view] |
| 963 | '("View This File" . dired-view-file)) |
| 964 | (define-key map [menu-bar immediate display] |
| 965 | '("Display in Other Window" . dired-display-file)) |
| 966 | (define-key map [menu-bar immediate find-file-other-window] |
| 967 | '("Find in Other Window" . dired-find-file-other-window)) |
| 968 | (define-key map [menu-bar immediate find-file] |
| 969 | '("Find This File" . dired-find-file)) |
| 970 | (define-key map [menu-bar immediate create-directory] |
| 971 | '("Create Directory..." . dired-create-directory)) |
| 972 | |
| 973 | (define-key map [menu-bar regexp] |
| 974 | (cons "Regexp" (make-sparse-keymap "Regexp"))) |
| 975 | |
| 976 | (define-key map [menu-bar regexp downcase] |
| 977 | '("Downcase" . dired-downcase)) |
| 978 | (define-key map [menu-bar regexp upcase] |
| 979 | '("Upcase" . dired-upcase)) |
| 980 | (define-key map [menu-bar regexp hardlink] |
| 981 | '("Hardlink..." . dired-do-hardlink-regexp)) |
| 982 | (define-key map [menu-bar regexp symlink] |
| 983 | '("Symlink..." . dired-do-symlink-regexp)) |
| 984 | (define-key map [menu-bar regexp rename] |
| 985 | '("Rename..." . dired-do-rename-regexp)) |
| 986 | (define-key map [menu-bar regexp copy] |
| 987 | '("Copy..." . dired-do-copy-regexp)) |
| 988 | (define-key map [menu-bar regexp flag] |
| 989 | '("Flag..." . dired-flag-files-regexp)) |
| 990 | (define-key map [menu-bar regexp mark] |
| 991 | '("Mark..." . dired-mark-files-regexp)) |
| 992 | (define-key map [menu-bar regexp mark] |
| 993 | '("Mark Containing..." . dired-mark-files-containing-regexp)) |
| 994 | |
| 995 | (define-key map [menu-bar mark] |
| 996 | (cons "Mark" (make-sparse-keymap "Mark"))) |
| 997 | |
| 998 | (define-key map [menu-bar mark prev] |
| 999 | '("Previous Marked" . dired-prev-marked-file)) |
| 1000 | (define-key map [menu-bar mark next] |
| 1001 | '("Next Marked" . dired-next-marked-file)) |
| 1002 | (define-key map [menu-bar mark marks] |
| 1003 | '("Change Marks..." . dired-change-marks)) |
| 1004 | (define-key map [menu-bar mark unmark-all] |
| 1005 | '("Unmark All" . dired-unmark-all-marks)) |
| 1006 | (define-key map [menu-bar mark symlinks] |
| 1007 | '("Mark Symlinks" . dired-mark-symlinks)) |
| 1008 | (define-key map [menu-bar mark directories] |
| 1009 | '("Mark Directories" . dired-mark-directories)) |
| 1010 | (define-key map [menu-bar mark directory] |
| 1011 | '("Mark Old Backups" . dired-clean-directory)) |
| 1012 | (define-key map [menu-bar mark executables] |
| 1013 | '("Mark Executables" . dired-mark-executables)) |
| 1014 | (define-key map [menu-bar mark garbage-files] |
| 1015 | '("Flag Garbage Files" . dired-flag-garbage-files)) |
| 1016 | (define-key map [menu-bar mark backup-files] |
| 1017 | '("Flag Backup Files" . dired-flag-backup-files)) |
| 1018 | (define-key map [menu-bar mark auto-save-files] |
| 1019 | '("Flag Auto-save Files" . dired-flag-auto-save-files)) |
| 1020 | (define-key map [menu-bar mark deletion] |
| 1021 | '("Flag" . dired-flag-file-deletion)) |
| 1022 | (define-key map [menu-bar mark unmark] |
| 1023 | '("Unmark" . dired-unmark)) |
| 1024 | (define-key map [menu-bar mark mark] |
| 1025 | '("Mark" . dired-mark)) |
| 1026 | (define-key map [menu-bar mark toggle-marks] |
| 1027 | '("Toggle Marks" . dired-do-toggle)) |
| 1028 | |
| 1029 | (define-key map [menu-bar operate] |
| 1030 | (cons "Operate" (make-sparse-keymap "Operate"))) |
| 1031 | |
| 1032 | (define-key map [menu-bar operate query-replace] |
| 1033 | '("Query Replace in Files..." . dired-do-query-replace)) |
| 1034 | (define-key map [menu-bar operate search] |
| 1035 | '("Search Files..." . dired-do-search)) |
| 1036 | (define-key map [menu-bar operate chown] |
| 1037 | '("Change Owner..." . dired-do-chown)) |
| 1038 | (define-key map [menu-bar operate chgrp] |
| 1039 | '("Change Group..." . dired-do-chgrp)) |
| 1040 | (define-key map [menu-bar operate chmod] |
| 1041 | '("Change Mode..." . dired-do-chmod)) |
| 1042 | (define-key map [menu-bar operate load] |
| 1043 | '("Load" . dired-do-load)) |
| 1044 | (define-key map [menu-bar operate compile] |
| 1045 | '("Byte-compile" . dired-do-byte-compile)) |
| 1046 | (define-key map [menu-bar operate compress] |
| 1047 | '("Compress" . dired-do-compress)) |
| 1048 | (define-key map [menu-bar operate print] |
| 1049 | '("Print" . dired-do-print)) |
| 1050 | (define-key map [menu-bar operate hardlink] |
| 1051 | '("Hardlink to..." . dired-do-hardlink)) |
| 1052 | (define-key map [menu-bar operate symlink] |
| 1053 | '("Symlink to..." . dired-do-symlink)) |
| 1054 | (define-key map [menu-bar operate command] |
| 1055 | '("Shell Command..." . dired-do-shell-command)) |
| 1056 | (define-key map [menu-bar operate delete] |
| 1057 | '("Delete" . dired-do-delete)) |
| 1058 | (define-key map [menu-bar operate rename] |
| 1059 | '("Rename to..." . dired-do-rename)) |
| 1060 | (define-key map [menu-bar operate copy] |
| 1061 | '("Copy to..." . dired-do-copy)) |
| 1062 | |
| 1063 | (setq dired-mode-map map))) |
| 1064 | \f |
| 1065 | ;; Dired mode is suitable only for specially formatted data. |
| 1066 | (put 'dired-mode 'mode-class 'special) |
| 1067 | |
| 1068 | (defun dired-mode (&optional dirname switches) |
| 1069 | "\ |
| 1070 | Mode for \"editing\" directory listings. |
| 1071 | In dired, you are \"editing\" a list of the files in a directory and |
| 1072 | \(optionally) its subdirectories, in the format of `ls -lR'. |
| 1073 | Each directory is a page: use \\[backward-page] and \\[forward-page] to move pagewise. |
| 1074 | \"Editing\" means that you can run shell commands on files, visit, |
| 1075 | compress, load or byte-compile them, change their file attributes |
| 1076 | and insert subdirectories into the same buffer. You can \"mark\" |
| 1077 | files for later commands or \"flag\" them for deletion, either file |
| 1078 | by file or all files matching certain criteria. |
| 1079 | You can move using the usual cursor motion commands.\\<dired-mode-map> |
| 1080 | Letters no longer insert themselves. Digits are prefix arguments. |
| 1081 | Instead, type \\[dired-flag-file-deletion] to flag a file for Deletion. |
| 1082 | Type \\[dired-mark] to Mark a file or subdirectory for later commands. |
| 1083 | Most commands operate on the marked files and use the current file |
| 1084 | if no files are marked. Use a numeric prefix argument to operate on |
| 1085 | the next ARG (or previous -ARG if ARG<0) files, or just `1' |
| 1086 | to operate on the current file only. Prefix arguments override marks. |
| 1087 | Mark-using commands display a list of failures afterwards. Type \\[dired-summary] |
| 1088 | to see why something went wrong. |
| 1089 | Type \\[dired-unmark] to Unmark a file or all files of a subdirectory. |
| 1090 | Type \\[dired-unmark-backward] to back up one line and unflag. |
| 1091 | Type \\[dired-do-flagged-delete] to eXecute the deletions requested. |
| 1092 | Type \\[dired-advertised-find-file] to Find the current line's file |
| 1093 | (or dired it in another buffer, if it is a directory). |
| 1094 | Type \\[dired-find-file-other-window] to find file or dired directory in Other window. |
| 1095 | Type \\[dired-maybe-insert-subdir] to Insert a subdirectory in this buffer. |
| 1096 | Type \\[dired-do-rename] to Rename a file or move the marked files to another directory. |
| 1097 | Type \\[dired-do-copy] to Copy files. |
| 1098 | Type \\[dired-sort-toggle-or-edit] to toggle sorting by name/date or change the `ls' switches. |
| 1099 | Type \\[revert-buffer] to read all currently expanded directories again. |
| 1100 | This retains all marks and hides subdirs again that were hidden before. |
| 1101 | SPC and DEL can be used to move down and up by lines. |
| 1102 | |
| 1103 | If dired ever gets confused, you can either type \\[revert-buffer] \ |
| 1104 | to read the |
| 1105 | directories again, type \\[dired-do-redisplay] \ |
| 1106 | to relist a single or the marked files or a |
| 1107 | subdirectory, or type \\[dired-build-subdir-alist] to parse the buffer |
| 1108 | again for the directory tree. |
| 1109 | |
| 1110 | Customization variables (rename this buffer and type \\[describe-variable] on each line |
| 1111 | for more info): |
| 1112 | |
| 1113 | dired-listing-switches |
| 1114 | dired-trivial-filenames |
| 1115 | dired-shrink-to-fit |
| 1116 | dired-marker-char |
| 1117 | dired-del-marker |
| 1118 | dired-keep-marker-rename |
| 1119 | dired-keep-marker-copy |
| 1120 | dired-keep-marker-hardlink |
| 1121 | dired-keep-marker-symlink |
| 1122 | |
| 1123 | Hooks (use \\[describe-variable] to see their documentation): |
| 1124 | |
| 1125 | dired-before-readin-hook |
| 1126 | dired-after-readin-hook |
| 1127 | dired-mode-hook |
| 1128 | dired-load-hook |
| 1129 | |
| 1130 | Keybindings: |
| 1131 | \\{dired-mode-map}" |
| 1132 | ;; Not to be called interactively (e.g. dired-directory will be set |
| 1133 | ;; to default-directory, which is wrong with wildcards). |
| 1134 | (kill-all-local-variables) |
| 1135 | (use-local-map dired-mode-map) |
| 1136 | (dired-advertise) ; default-directory is already set |
| 1137 | (setq major-mode 'dired-mode |
| 1138 | mode-name "Dired" |
| 1139 | ;; case-fold-search nil |
| 1140 | buffer-read-only t |
| 1141 | selective-display t ; for subdirectory hiding |
| 1142 | mode-line-buffer-identification '("%17b")) |
| 1143 | (set (make-local-variable 'revert-buffer-function) |
| 1144 | (function dired-revert)) |
| 1145 | (set (make-local-variable 'page-delimiter) |
| 1146 | "\n\n") |
| 1147 | (set (make-local-variable 'dired-directory) |
| 1148 | (or dirname default-directory)) |
| 1149 | ;; list-buffers uses this to display the dir being edited in this buffer. |
| 1150 | (set (make-local-variable 'list-buffers-directory) |
| 1151 | (expand-file-name dired-directory)) |
| 1152 | (set (make-local-variable 'dired-actual-switches) |
| 1153 | (or switches dired-listing-switches)) |
| 1154 | (set (make-local-variable 'font-lock-defaults) '(dired-font-lock-keywords t)) |
| 1155 | (dired-sort-other dired-actual-switches t) |
| 1156 | (run-hooks 'dired-mode-hook)) |
| 1157 | \f |
| 1158 | ;; Idiosyncratic dired commands that don't deal with marks. |
| 1159 | |
| 1160 | (defun dired-summary () |
| 1161 | "Summarize basic Dired commands and show recent Dired errors." |
| 1162 | (interactive) |
| 1163 | (dired-why) |
| 1164 | ;>> this should check the key-bindings and use substitute-command-keys if non-standard |
| 1165 | (message |
| 1166 | "d-elete, u-ndelete, x-punge, f-ind, o-ther window, R-ename, C-opy, h-elp")) |
| 1167 | |
| 1168 | (defun dired-undo () |
| 1169 | "Undo in a dired buffer. |
| 1170 | This doesn't recover lost files, it just undoes changes in the buffer itself. |
| 1171 | You can use it to recover marks, killed lines or subdirs. |
| 1172 | In the latter case, you have to do \\[dired-build-subdir-alist] to |
| 1173 | parse the buffer again." |
| 1174 | (interactive) |
| 1175 | (let (buffer-read-only) |
| 1176 | (undo))) |
| 1177 | |
| 1178 | (defun dired-next-line (arg) |
| 1179 | "Move down lines then position at filename. |
| 1180 | Optional prefix ARG says how many lines to move; default is one line." |
| 1181 | (interactive "p") |
| 1182 | (next-line arg) |
| 1183 | (dired-move-to-filename)) |
| 1184 | |
| 1185 | (defun dired-previous-line (arg) |
| 1186 | "Move up lines then position at filename. |
| 1187 | Optional prefix ARG says how many lines to move; default is one line." |
| 1188 | (interactive "p") |
| 1189 | (previous-line arg) |
| 1190 | (dired-move-to-filename)) |
| 1191 | |
| 1192 | (defun dired-next-dirline (arg &optional opoint) |
| 1193 | "Goto ARG'th next directory file line." |
| 1194 | (interactive "p") |
| 1195 | (or opoint (setq opoint (point))) |
| 1196 | (if (if (> arg 0) |
| 1197 | (re-search-forward dired-re-dir nil t arg) |
| 1198 | (beginning-of-line) |
| 1199 | (re-search-backward dired-re-dir nil t (- arg))) |
| 1200 | (dired-move-to-filename) ; user may type `i' or `f' |
| 1201 | (goto-char opoint) |
| 1202 | (error "No more subdirectories"))) |
| 1203 | |
| 1204 | (defun dired-prev-dirline (arg) |
| 1205 | "Goto ARG'th previous directory file line." |
| 1206 | (interactive "p") |
| 1207 | (dired-next-dirline (- arg))) |
| 1208 | |
| 1209 | (defun dired-up-directory (&optional other-window) |
| 1210 | "Run dired on parent directory of current directory. |
| 1211 | Find the parent directory either in this buffer or another buffer. |
| 1212 | Creates a buffer if necessary." |
| 1213 | (interactive "P") |
| 1214 | (let* ((dir (dired-current-directory)) |
| 1215 | (up (file-name-directory (directory-file-name dir)))) |
| 1216 | (or (dired-goto-file (directory-file-name dir)) |
| 1217 | ;; Only try dired-goto-subdir if buffer has more than one dir. |
| 1218 | (and (cdr dired-subdir-alist) |
| 1219 | (dired-goto-subdir up)) |
| 1220 | (progn |
| 1221 | (if other-window |
| 1222 | (dired-other-window up) |
| 1223 | (dired up)) |
| 1224 | (dired-goto-file dir))))) |
| 1225 | |
| 1226 | ;; Force `f' rather than `e' in the mode doc: |
| 1227 | (defalias 'dired-advertised-find-file 'dired-find-file) |
| 1228 | (defun dired-find-file () |
| 1229 | "In dired, visit the file or directory named on this line." |
| 1230 | (interactive) |
| 1231 | (let ((file-name (file-name-sans-versions (dired-get-filename) t))) |
| 1232 | (if (file-exists-p file-name) |
| 1233 | (find-file file-name) |
| 1234 | (if (file-symlink-p file-name) |
| 1235 | (error "File is a symlink to a nonexistent target") |
| 1236 | (error "File no longer exists; type `g' to update Dired buffer"))))) |
| 1237 | |
| 1238 | (defun dired-mouse-find-file-other-window (event) |
| 1239 | "In dired, visit the file or directory name you click on." |
| 1240 | (interactive "e") |
| 1241 | (let (file) |
| 1242 | (save-excursion |
| 1243 | (set-buffer (window-buffer (posn-window (event-end event)))) |
| 1244 | (save-excursion |
| 1245 | (goto-char (posn-point (event-end event))) |
| 1246 | (setq file (dired-get-filename)))) |
| 1247 | (select-window (posn-window (event-end event))) |
| 1248 | (find-file-other-window (file-name-sans-versions file t)))) |
| 1249 | |
| 1250 | (defun dired-view-file () |
| 1251 | "In dired, examine a file in view mode, returning to dired when done. |
| 1252 | When file is a directory, show it in this buffer if it is inserted; |
| 1253 | otherwise, display it in another buffer." |
| 1254 | (interactive) |
| 1255 | (if (file-directory-p (dired-get-filename)) |
| 1256 | (or (and (cdr dired-subdir-alist) |
| 1257 | (dired-goto-subdir (dired-get-filename))) |
| 1258 | (dired (dired-get-filename))) |
| 1259 | (view-file (dired-get-filename)))) |
| 1260 | |
| 1261 | (defun dired-find-file-other-window () |
| 1262 | "In dired, visit this file or directory in another window." |
| 1263 | (interactive) |
| 1264 | (find-file-other-window (file-name-sans-versions (dired-get-filename) t))) |
| 1265 | |
| 1266 | (defun dired-display-file () |
| 1267 | "In dired, display this file or directory in another window." |
| 1268 | (interactive) |
| 1269 | (let ((file (file-name-sans-versions (dired-get-filename) t))) |
| 1270 | (display-buffer (find-file-noselect file)))) |
| 1271 | \f |
| 1272 | ;;; Functions for extracting and manipulating file names in dired buffers. |
| 1273 | |
| 1274 | (defun dired-get-filename (&optional localp no-error-if-not-filep) |
| 1275 | "In dired, return name of file mentioned on this line. |
| 1276 | Value returned normally includes the directory name. |
| 1277 | Optional arg LOCALP with value `no-dir' means don't include directory |
| 1278 | name in result. A value of t means construct name relative to |
| 1279 | `default-directory', which still may contain slashes if in a subdirectory. |
| 1280 | Optional arg NO-ERROR-IF-NOT-FILEP means return nil if no filename on |
| 1281 | this line, otherwise an error occurs." |
| 1282 | (let (case-fold-search file p1 p2) |
| 1283 | (save-excursion |
| 1284 | (if (setq p1 (dired-move-to-filename (not no-error-if-not-filep))) |
| 1285 | (setq p2 (dired-move-to-end-of-filename no-error-if-not-filep)))) |
| 1286 | ;; nil if no file on this line, but no-error-if-not-filep is t: |
| 1287 | (if (setq file (and p1 p2 (buffer-substring p1 p2))) |
| 1288 | (progn |
| 1289 | ;; Get rid of the mouse-face property that file names have. |
| 1290 | (set-text-properties 0 (length file) nil file) |
| 1291 | ;; Unquote names quoted by ls or by dired-insert-directory. |
| 1292 | ;; Using read to unquote is much faster than substituting |
| 1293 | ;; \007 (4 chars) -> ^G (1 char) etc. in a lisp loop. |
| 1294 | (setq file |
| 1295 | (read |
| 1296 | (concat "\"" |
| 1297 | ;; some ls -b don't escape quotes, argh! |
| 1298 | ;; This is not needed for GNU ls, though. |
| 1299 | (or (dired-string-replace-match |
| 1300 | "\\([^\\]\\|\\`\\)\"" file "\\1\\\\\"" nil t) |
| 1301 | file) |
| 1302 | "\""))))) |
| 1303 | (and file buffer-file-coding-system |
| 1304 | (not file-name-coding-system) |
| 1305 | (not default-file-name-coding-system) |
| 1306 | (setq file (encode-coding-string file buffer-file-coding-system))) |
| 1307 | (if (eq localp 'no-dir) |
| 1308 | file |
| 1309 | (and file (concat (dired-current-directory localp) file))))) |
| 1310 | |
| 1311 | (defun dired-string-replace-match (regexp string newtext |
| 1312 | &optional literal global) |
| 1313 | "Replace first match of REGEXP in STRING with NEWTEXT. |
| 1314 | If it does not match, nil is returned instead of the new string. |
| 1315 | Optional arg LITERAL means to take NEWTEXT literally. |
| 1316 | Optional arg GLOBAL means to replace all matches." |
| 1317 | (if global |
| 1318 | (let ((start 0)) |
| 1319 | (while (string-match regexp string start) |
| 1320 | (let ((from-end (- (length string) (match-end 0)))) |
| 1321 | (setq string (replace-match newtext t literal string)) |
| 1322 | (setq start (- (length string) from-end)))) |
| 1323 | string) |
| 1324 | (if (not (string-match regexp string 0)) |
| 1325 | nil |
| 1326 | (replace-match newtext t literal string)))) |
| 1327 | |
| 1328 | (defun dired-make-absolute (file &optional dir) |
| 1329 | ;;"Convert FILE (a pathname relative to DIR) to an absolute pathname." |
| 1330 | ;; We can't always use expand-file-name as this would get rid of `.' |
| 1331 | ;; or expand in / instead default-directory if DIR=="". |
| 1332 | ;; This should be good enough for ange-ftp, but might easily be |
| 1333 | ;; redefined (for VMS?). |
| 1334 | ;; It should be reasonably fast, though, as it is called in |
| 1335 | ;; dired-get-filename. |
| 1336 | (concat (or dir default-directory) file)) |
| 1337 | |
| 1338 | (defun dired-make-relative (file &optional dir ignore) |
| 1339 | "Convert FILE (an absolute file name) to a name relative to DIR. |
| 1340 | If this is impossible, return FILE unchanged. |
| 1341 | DIR must be a directory name, not a file name." |
| 1342 | (or dir (setq dir default-directory)) |
| 1343 | ;; This case comes into play if default-directory is set to |
| 1344 | ;; use ~. |
| 1345 | (if (and (> (length dir) 0) (= (aref dir 0) ?~)) |
| 1346 | (setq dir (expand-file-name dir))) |
| 1347 | (if (string-match (concat "^" (regexp-quote dir)) file) |
| 1348 | (substring file (match-end 0)) |
| 1349 | ;;; (or no-error |
| 1350 | ;;; (error "%s: not in directory tree growing at %s" file dir)) |
| 1351 | file)) |
| 1352 | \f |
| 1353 | ;;; Functions for finding the file name in a dired buffer line. |
| 1354 | |
| 1355 | (defvar dired-move-to-filename-regexp |
| 1356 | (let* ((l "\\([A-Za-z]\\|[^\0-\177]\\)") |
| 1357 | ;; In some locales, month abbreviations are as short as 2 letters, |
| 1358 | ;; and they can be padded on the right with spaces. |
| 1359 | (month (concat l l "+ *")) |
| 1360 | ;; Recognize any non-ASCII character. |
| 1361 | ;; The purpose is to match a Kanji character. |
| 1362 | (k "[^\0-\177]") |
| 1363 | ;; (k "[^\x00-\x7f\x80-\xff]") |
| 1364 | (s " ") |
| 1365 | (yyyy "[0-9][0-9][0-9][0-9]") |
| 1366 | (mm "[ 0-1][0-9]") |
| 1367 | (dd "[ 0-3][0-9]") |
| 1368 | (HH:MM "[ 0-2][0-9]:[0-5][0-9]") |
| 1369 | (western (concat "\\(" month s dd "\\|" dd s month "\\)" |
| 1370 | s "\\(" HH:MM "\\|" s yyyy "\\|" yyyy s "\\)")) |
| 1371 | (japanese (concat mm k s dd k s "\\(" s HH:MM "\\|" yyyy k "\\)"))) |
| 1372 | ;; Require the previous column to end in a digit. |
| 1373 | ;; This avoids recognizing `1 may 1997' as a date in the line: |
| 1374 | ;; -r--r--r-- 1 may 1997 1168 Oct 19 16:49 README |
| 1375 | (concat "[0-9]" s "\\(" western "\\|" japanese "\\)" s)) |
| 1376 | "Regular expression to match up to the file name in a directory listing. |
| 1377 | The default value is designed to recognize dates and times |
| 1378 | regardless of the language.") |
| 1379 | |
| 1380 | (defvar dired-permission-flags-regexp |
| 1381 | "\\([^ ]\\)[-r][-w]\\([^ ]\\)[-r][-w]\\([^ ]\\)[-r][-w]\\([^ ]\\)" |
| 1382 | "Regular expression to match the permission flags in `ls -l'.") |
| 1383 | |
| 1384 | ;; Move to first char of filename on this line. |
| 1385 | ;; Returns position (point) or nil if no filename on this line." |
| 1386 | (defun dired-move-to-filename (&optional raise-error eol) |
| 1387 | ;; This is the UNIX version. |
| 1388 | (or eol (setq eol (progn (end-of-line) (point)))) |
| 1389 | (beginning-of-line) |
| 1390 | (if (re-search-forward dired-move-to-filename-regexp eol t) |
| 1391 | (goto-char (match-end 0)) |
| 1392 | (if raise-error |
| 1393 | (error "No file on this line")))) |
| 1394 | |
| 1395 | (defun dired-move-to-end-of-filename (&optional no-error) |
| 1396 | ;; Assumes point is at beginning of filename, |
| 1397 | ;; thus the rwx bit re-search-backward below will succeed in *this* |
| 1398 | ;; line if at all. So, it should be called only after |
| 1399 | ;; (dired-move-to-filename t). |
| 1400 | ;; On failure, signals an error (with non-nil NO-ERROR just returns nil). |
| 1401 | ;; This is the UNIX version. |
| 1402 | (let (opoint file-type executable symlink hidden case-fold-search used-F eol) |
| 1403 | ;; case-fold-search is nil now, so we can test for capital F: |
| 1404 | (setq used-F (string-match "F" dired-actual-switches) |
| 1405 | opoint (point) |
| 1406 | eol (save-excursion (end-of-line) (point)) |
| 1407 | hidden (and selective-display |
| 1408 | (save-excursion (search-forward "\r" eol t)))) |
| 1409 | (if hidden |
| 1410 | nil |
| 1411 | (save-excursion;; Find out what kind of file this is: |
| 1412 | ;; Restrict perm bits to be non-blank, |
| 1413 | ;; otherwise this matches one char to early (looking backward): |
| 1414 | ;; "l---------" (some systems make symlinks that way) |
| 1415 | ;; "----------" (plain file with zero perms) |
| 1416 | (if (re-search-backward |
| 1417 | dired-permission-flags-regexp nil t) |
| 1418 | (setq file-type (char-after (match-beginning 1)) |
| 1419 | symlink (eq file-type ?l) |
| 1420 | ;; Only with -F we need to know whether it's an executable |
| 1421 | executable (and |
| 1422 | used-F |
| 1423 | (string-match |
| 1424 | "[xst]";; execute bit set anywhere? |
| 1425 | (concat |
| 1426 | (buffer-substring (match-beginning 2) |
| 1427 | (match-end 2)) |
| 1428 | (buffer-substring (match-beginning 3) |
| 1429 | (match-end 3)) |
| 1430 | (buffer-substring (match-beginning 4) |
| 1431 | (match-end 4)))))) |
| 1432 | (or no-error (error "No file on this line")))) |
| 1433 | ;; Move point to end of name: |
| 1434 | (if symlink |
| 1435 | (if (search-forward " ->" eol t) |
| 1436 | (progn |
| 1437 | (forward-char -3) |
| 1438 | (and used-F |
| 1439 | dired-ls-F-marks-symlinks |
| 1440 | (eq (preceding-char) ?@);; did ls really mark the link? |
| 1441 | (forward-char -1)))) |
| 1442 | (goto-char eol);; else not a symbolic link |
| 1443 | ;; ls -lF marks dirs, sockets and executables with exactly one |
| 1444 | ;; trailing character. (Executable bits on symlinks ain't mean |
| 1445 | ;; a thing, even to ls, but we know it's not a symlink.) |
| 1446 | (and used-F |
| 1447 | (or (memq file-type '(?d ?s)) |
| 1448 | executable) |
| 1449 | (forward-char -1)))) |
| 1450 | (or no-error |
| 1451 | (not (eq opoint (point))) |
| 1452 | (error (if hidden |
| 1453 | (substitute-command-keys |
| 1454 | "File line is hidden, type \\[dired-hide-subdir] to unhide") |
| 1455 | "No file on this line"))) |
| 1456 | (if (eq opoint (point)) |
| 1457 | nil |
| 1458 | (point)))) |
| 1459 | |
| 1460 | \f |
| 1461 | ;; Keeping Dired buffers in sync with the filesystem and with each other |
| 1462 | |
| 1463 | (defvar dired-buffers nil |
| 1464 | ;; Enlarged by dired-advertise |
| 1465 | ;; Queried by function dired-buffers-for-dir. When this detects a |
| 1466 | ;; killed buffer, it is removed from this list. |
| 1467 | "Alist of expanded directories and their associated dired buffers.") |
| 1468 | |
| 1469 | (defun dired-buffers-for-dir (dir &optional file) |
| 1470 | ;; Return a list of buffers that dired DIR (top level or in-situ subdir). |
| 1471 | ;; If FILE is non-nil, include only those whose wildcard pattern (if any) |
| 1472 | ;; matches FILE. |
| 1473 | ;; The list is in reverse order of buffer creation, most recent last. |
| 1474 | ;; As a side effect, killed dired buffers for DIR are removed from |
| 1475 | ;; dired-buffers. |
| 1476 | (setq dir (file-name-as-directory dir)) |
| 1477 | (let ((alist dired-buffers) result elt buf pattern) |
| 1478 | (while alist |
| 1479 | (setq elt (car alist) |
| 1480 | buf (cdr elt)) |
| 1481 | (if (buffer-name buf) |
| 1482 | (if (dired-in-this-tree dir (car elt)) |
| 1483 | (with-current-buffer buf |
| 1484 | (and (assoc dir dired-subdir-alist) |
| 1485 | (or (null file) |
| 1486 | (let ((wildcards |
| 1487 | (file-name-nondirectory dired-directory))) |
| 1488 | (or (= 0 (length wildcards)) |
| 1489 | (string-match (dired-glob-regexp wildcards) |
| 1490 | file)))) |
| 1491 | (setq result (cons buf result))))) |
| 1492 | ;; else buffer is killed - clean up: |
| 1493 | (setq dired-buffers (delq elt dired-buffers))) |
| 1494 | (setq alist (cdr alist))) |
| 1495 | result)) |
| 1496 | |
| 1497 | (defun dired-glob-regexp (pattern) |
| 1498 | "Convert glob-pattern PATTERN to a regular expression." |
| 1499 | (let ((matched-in-pattern 0) ;; How many chars of PATTERN we've handled. |
| 1500 | regexp) |
| 1501 | (while (string-match "[[?*]" pattern matched-in-pattern) |
| 1502 | (let ((op-end (match-end 0)) |
| 1503 | (next-op (aref pattern (match-beginning 0)))) |
| 1504 | (setq regexp (concat regexp |
| 1505 | (regexp-quote |
| 1506 | (substring pattern matched-in-pattern |
| 1507 | (match-beginning 0))))) |
| 1508 | (cond ((= next-op ??) |
| 1509 | (setq regexp (concat regexp ".")) |
| 1510 | (setq matched-in-pattern op-end)) |
| 1511 | ((= next-op ?\[) |
| 1512 | ;; Fails to handle ^ yet ???? |
| 1513 | (let* ((set-start (match-beginning 0)) |
| 1514 | (set-cont |
| 1515 | (if (= (aref pattern (1+ set-start)) ?^) |
| 1516 | (+ 3 set-start) |
| 1517 | (+ 2 set-start))) |
| 1518 | (set-end (string-match "]" pattern set-cont)) |
| 1519 | (set (substring pattern set-start (1+ set-end)))) |
| 1520 | (setq regexp (concat regexp set)) |
| 1521 | (setq matched-in-pattern (1+ set-end)))) |
| 1522 | ((= next-op ?*) |
| 1523 | (setq regexp (concat regexp ".*")) |
| 1524 | (setq matched-in-pattern op-end))))) |
| 1525 | (concat "\\`" |
| 1526 | regexp |
| 1527 | (regexp-quote |
| 1528 | (substring pattern matched-in-pattern)) |
| 1529 | "\\'"))) |
| 1530 | |
| 1531 | |
| 1532 | |
| 1533 | (defun dired-advertise () |
| 1534 | ;;"Advertise in variable `dired-buffers' that we dired `default-directory'." |
| 1535 | ;; With wildcards we actually advertise too much. |
| 1536 | (let ((expanded-default (expand-file-name default-directory))) |
| 1537 | (if (memq (current-buffer) (dired-buffers-for-dir expanded-default)) |
| 1538 | t ; we have already advertised ourselves |
| 1539 | (setq dired-buffers |
| 1540 | (cons (cons expanded-default (current-buffer)) |
| 1541 | dired-buffers))))) |
| 1542 | |
| 1543 | (defun dired-unadvertise (dir) |
| 1544 | ;; Remove DIR from the buffer alist in variable dired-buffers. |
| 1545 | ;; This has the effect of removing any buffer whose main directory is DIR. |
| 1546 | ;; It does not affect buffers in which DIR is a subdir. |
| 1547 | ;; Removing is also done as a side-effect in dired-buffer-for-dir. |
| 1548 | (setq dired-buffers |
| 1549 | (delq (assoc (expand-file-name dir) dired-buffers) dired-buffers))) |
| 1550 | \f |
| 1551 | ;; Tree Dired |
| 1552 | |
| 1553 | ;;; utility functions |
| 1554 | |
| 1555 | (defun dired-in-this-tree (file dir) |
| 1556 | ;;"Is FILE part of the directory tree starting at DIR?" |
| 1557 | (let (case-fold-search) |
| 1558 | (string-match (concat "^" (regexp-quote dir)) file))) |
| 1559 | |
| 1560 | (defun dired-normalize-subdir (dir) |
| 1561 | ;; Prepend default-directory to DIR if relative path name. |
| 1562 | ;; dired-get-filename must be able to make a valid filename from a |
| 1563 | ;; file and its directory DIR. |
| 1564 | (file-name-as-directory |
| 1565 | (if (file-name-absolute-p dir) |
| 1566 | dir |
| 1567 | (expand-file-name dir default-directory)))) |
| 1568 | |
| 1569 | (defun dired-get-subdir () |
| 1570 | ;;"Return the subdir name on this line, or nil if not on a headerline." |
| 1571 | ;; Look up in the alist whether this is a headerline. |
| 1572 | (save-excursion |
| 1573 | (let ((cur-dir (dired-current-directory))) |
| 1574 | (beginning-of-line) ; alist stores b-o-l positions |
| 1575 | (and (zerop (- (point) |
| 1576 | (dired-get-subdir-min (assoc cur-dir |
| 1577 | dired-subdir-alist)))) |
| 1578 | cur-dir)))) |
| 1579 | |
| 1580 | ;(defun dired-get-subdir-min (elt) |
| 1581 | ; (cdr elt)) |
| 1582 | ;; can't use macro, must be redefinable for other alist format in dired-nstd. |
| 1583 | (defalias 'dired-get-subdir-min 'cdr) |
| 1584 | |
| 1585 | (defun dired-get-subdir-max (elt) |
| 1586 | (save-excursion |
| 1587 | (goto-char (dired-get-subdir-min elt)) |
| 1588 | (dired-subdir-max))) |
| 1589 | |
| 1590 | (defun dired-clear-alist () |
| 1591 | (while dired-subdir-alist |
| 1592 | (set-marker (dired-get-subdir-min (car dired-subdir-alist)) nil) |
| 1593 | (setq dired-subdir-alist (cdr dired-subdir-alist)))) |
| 1594 | |
| 1595 | (defun dired-subdir-index (dir) |
| 1596 | ;; Return an index into alist for use with nth |
| 1597 | ;; for the sake of subdir moving commands. |
| 1598 | (let (found (index 0) (alist dired-subdir-alist)) |
| 1599 | (while alist |
| 1600 | (if (string= dir (car (car alist))) |
| 1601 | (setq alist nil found t) |
| 1602 | (setq alist (cdr alist) index (1+ index)))) |
| 1603 | (if found index nil))) |
| 1604 | |
| 1605 | (defun dired-next-subdir (arg &optional no-error-if-not-found no-skip) |
| 1606 | "Go to next subdirectory, regardless of level." |
| 1607 | ;; Use 0 arg to go to this directory's header line. |
| 1608 | ;; NO-SKIP prevents moving to end of header line, returning whatever |
| 1609 | ;; position was found in dired-subdir-alist. |
| 1610 | (interactive "p") |
| 1611 | (let ((this-dir (dired-current-directory)) |
| 1612 | pos index) |
| 1613 | ;; nth with negative arg does not return nil but the first element |
| 1614 | (setq index (- (dired-subdir-index this-dir) arg)) |
| 1615 | (setq pos (if (>= index 0) |
| 1616 | (dired-get-subdir-min (nth index dired-subdir-alist)))) |
| 1617 | (if pos |
| 1618 | (progn |
| 1619 | (goto-char pos) |
| 1620 | (or no-skip (skip-chars-forward "^\n\r")) |
| 1621 | (point)) |
| 1622 | (if no-error-if-not-found |
| 1623 | nil ; return nil if not found |
| 1624 | (error "%s directory" (if (> arg 0) "Last" "First")))))) |
| 1625 | |
| 1626 | (defun dired-build-subdir-alist () |
| 1627 | "Build `dired-subdir-alist' by parsing the buffer. |
| 1628 | Returns the new value of the alist." |
| 1629 | (interactive) |
| 1630 | (dired-clear-alist) |
| 1631 | (save-excursion |
| 1632 | (let ((count 0) |
| 1633 | (buffer-read-only nil) |
| 1634 | new-dir-name) |
| 1635 | (goto-char (point-min)) |
| 1636 | (setq dired-subdir-alist nil) |
| 1637 | (while (and (re-search-forward dired-subdir-regexp nil t) |
| 1638 | ;; Avoid taking a file name ending in a colon |
| 1639 | ;; as a subdir name. |
| 1640 | (not (save-excursion |
| 1641 | (goto-char (match-beginning 0)) |
| 1642 | (beginning-of-line) |
| 1643 | (forward-char 2) |
| 1644 | (save-match-data (looking-at dired-re-perms))))) |
| 1645 | (save-excursion |
| 1646 | (goto-char (match-beginning 1)) |
| 1647 | (setq new-dir-name |
| 1648 | (expand-file-name (buffer-substring (point) (match-end 1)))) |
| 1649 | (delete-region (point) (match-end 1)) |
| 1650 | (insert new-dir-name)) |
| 1651 | (setq count (1+ count)) |
| 1652 | (dired-alist-add-1 new-dir-name |
| 1653 | ;; Place a sub directory boundary between lines. |
| 1654 | (save-excursion |
| 1655 | (goto-char (match-beginning 0)) |
| 1656 | (beginning-of-line) |
| 1657 | (point-marker)))) |
| 1658 | (if (> count 1) |
| 1659 | (message "Buffer includes %d directories" count)) |
| 1660 | ;; We don't need to sort it because it is in buffer order per |
| 1661 | ;; constructionem. Return new alist: |
| 1662 | dired-subdir-alist))) |
| 1663 | |
| 1664 | (defun dired-alist-add-1 (dir new-marker) |
| 1665 | ;; Add new DIR at NEW-MARKER. Don't sort. |
| 1666 | (setq dired-subdir-alist |
| 1667 | (cons (cons (dired-normalize-subdir dir) new-marker) |
| 1668 | dired-subdir-alist))) |
| 1669 | |
| 1670 | (defun dired-goto-next-nontrivial-file () |
| 1671 | ;; Position point on first nontrivial file after point. |
| 1672 | (dired-goto-next-file);; so there is a file to compare with |
| 1673 | (if (stringp dired-trivial-filenames) |
| 1674 | (while (and (not (eobp)) |
| 1675 | (string-match dired-trivial-filenames |
| 1676 | (file-name-nondirectory |
| 1677 | (or (dired-get-filename nil t) "")))) |
| 1678 | (forward-line 1) |
| 1679 | (dired-move-to-filename)))) |
| 1680 | |
| 1681 | (defun dired-goto-next-file () |
| 1682 | (let ((max (1- (dired-subdir-max)))) |
| 1683 | (while (and (not (dired-move-to-filename)) (< (point) max)) |
| 1684 | (forward-line 1)))) |
| 1685 | |
| 1686 | (defun dired-goto-file (file) |
| 1687 | "Go to file line of FILE in this dired buffer." |
| 1688 | ;; Return value of point on success, else nil. |
| 1689 | ;; FILE must be an absolute pathname. |
| 1690 | ;; Loses if FILE contains control chars like "\007" for which ls |
| 1691 | ;; either inserts "?" or "\\007" into the buffer, so we won't find |
| 1692 | ;; it in the buffer. |
| 1693 | (interactive |
| 1694 | (prog1 ; let push-mark display its message |
| 1695 | (list (expand-file-name |
| 1696 | (read-file-name "Goto file: " |
| 1697 | (dired-current-directory)))) |
| 1698 | (push-mark))) |
| 1699 | (setq file (directory-file-name file)) ; does no harm if no directory |
| 1700 | (let (found case-fold-search dir) |
| 1701 | (setq dir (or (file-name-directory file) |
| 1702 | (error "Need absolute pathname for %s" file))) |
| 1703 | (save-excursion |
| 1704 | ;; The hair here is to get the result of dired-goto-subdir |
| 1705 | ;; without really calling it if we don't have any subdirs. |
| 1706 | (if (if (string= dir (expand-file-name default-directory)) |
| 1707 | (goto-char (point-min)) |
| 1708 | (and (cdr dired-subdir-alist) |
| 1709 | (dired-goto-subdir dir))) |
| 1710 | (let ((base (file-name-nondirectory file)) |
| 1711 | (boundary (dired-subdir-max))) |
| 1712 | (while (and (not found) |
| 1713 | ;; filenames are preceded by SPC, this makes |
| 1714 | ;; the search faster (e.g. for the filename "-"!). |
| 1715 | (search-forward (concat " " base) boundary 'move)) |
| 1716 | ;; Match could have BASE just as initial substring or |
| 1717 | ;; or in permission bits or date or |
| 1718 | ;; not be a proper filename at all: |
| 1719 | (if (equal base (dired-get-filename 'no-dir t)) |
| 1720 | ;; Must move to filename since an (actually |
| 1721 | ;; correct) match could have been elsewhere on the |
| 1722 | ;; ;; line (e.g. "-" would match somewhere in the |
| 1723 | ;; permission bits). |
| 1724 | (setq found (dired-move-to-filename)) |
| 1725 | ;; If this isn't the right line, move forward to avoid |
| 1726 | ;; trying this line again. |
| 1727 | (forward-line 1)))))) |
| 1728 | (and found |
| 1729 | ;; return value of point (i.e., FOUND): |
| 1730 | (goto-char found)))) |
| 1731 | |
| 1732 | (defun dired-initial-position (dirname) |
| 1733 | ;; Where point should go in a new listing of DIRNAME. |
| 1734 | ;; Point assumed at beginning of new subdir line. |
| 1735 | ;; You may redefine this function as you wish, e.g. like in dired-x.el. |
| 1736 | (end-of-line) |
| 1737 | (if dired-trivial-filenames (dired-goto-next-nontrivial-file))) |
| 1738 | \f |
| 1739 | ;; These are hooks which make tree dired work. |
| 1740 | ;; They are in this file because other parts of dired need to call them. |
| 1741 | ;; But they don't call the rest of tree dired unless there are subdirs loaded. |
| 1742 | |
| 1743 | ;; This function is called for each retrieved filename. |
| 1744 | ;; It could stand to be faster, though it's mostly function call |
| 1745 | ;; overhead. Avoiding the function call seems to save about 10% in |
| 1746 | ;; dired-get-filename. Make it a defsubst? |
| 1747 | (defun dired-current-directory (&optional localp) |
| 1748 | "Return the name of the subdirectory to which this line belongs. |
| 1749 | This returns a string with trailing slash, like `default-directory'. |
| 1750 | Optional argument means return a file name relative to `default-directory'." |
| 1751 | (let ((here (point)) |
| 1752 | (alist (or dired-subdir-alist |
| 1753 | ;; probably because called in a non-dired buffer |
| 1754 | (error "No subdir-alist in %s" (current-buffer)))) |
| 1755 | elt dir) |
| 1756 | (while alist |
| 1757 | (setq elt (car alist) |
| 1758 | dir (car elt) |
| 1759 | ;; use `<=' (not `<') as subdir line is part of subdir |
| 1760 | alist (if (<= (dired-get-subdir-min elt) here) |
| 1761 | nil ; found |
| 1762 | (cdr alist)))) |
| 1763 | (if localp |
| 1764 | (dired-make-relative dir default-directory) |
| 1765 | dir))) |
| 1766 | |
| 1767 | ;; Subdirs start at the beginning of their header lines and end just |
| 1768 | ;; before the beginning of the next header line (or end of buffer). |
| 1769 | |
| 1770 | (defun dired-subdir-max () |
| 1771 | (save-excursion |
| 1772 | (if (or (null (cdr dired-subdir-alist)) (not (dired-next-subdir 1 t t))) |
| 1773 | (point-max) |
| 1774 | (point)))) |
| 1775 | \f |
| 1776 | ;; Deleting files |
| 1777 | |
| 1778 | (defun dired-do-flagged-delete (&optional nomessage) |
| 1779 | "In dired, delete the files flagged for deletion. |
| 1780 | If NOMESSAGE is non-nil, we don't display any message |
| 1781 | if there are no flagged files." |
| 1782 | (interactive) |
| 1783 | (let* ((dired-marker-char dired-del-marker) |
| 1784 | (regexp (dired-marker-regexp)) |
| 1785 | case-fold-search) |
| 1786 | (if (save-excursion (goto-char (point-min)) |
| 1787 | (re-search-forward regexp nil t)) |
| 1788 | (dired-internal-do-deletions |
| 1789 | ;; this can't move point since ARG is nil |
| 1790 | (dired-map-over-marks (cons (dired-get-filename) (point)) |
| 1791 | nil) |
| 1792 | nil) |
| 1793 | (or nomessage |
| 1794 | (message "(No deletions requested)"))))) |
| 1795 | |
| 1796 | (defun dired-do-delete (&optional arg) |
| 1797 | "Delete all marked (or next ARG) files." |
| 1798 | ;; This is more consistent with the file marking feature than |
| 1799 | ;; dired-do-flagged-delete. |
| 1800 | (interactive "P") |
| 1801 | (dired-internal-do-deletions |
| 1802 | ;; this may move point if ARG is an integer |
| 1803 | (dired-map-over-marks (cons (dired-get-filename) (point)) |
| 1804 | arg) |
| 1805 | arg)) |
| 1806 | |
| 1807 | (defvar dired-deletion-confirmer 'yes-or-no-p) ; or y-or-n-p? |
| 1808 | |
| 1809 | (defun dired-internal-do-deletions (l arg) |
| 1810 | ;; L is an alist of files to delete, with their buffer positions. |
| 1811 | ;; ARG is the prefix arg. |
| 1812 | ;; Filenames are absolute (VMS needs this for logical search paths). |
| 1813 | ;; (car L) *must* be the *last* (bottommost) file in the dired buffer. |
| 1814 | ;; That way as changes are made in the buffer they do not shift the |
| 1815 | ;; lines still to be changed, so the (point) values in L stay valid. |
| 1816 | ;; Also, for subdirs in natural order, a subdir's files are deleted |
| 1817 | ;; before the subdir itself - the other way around would not work. |
| 1818 | (let ((files (mapcar (function car) l)) |
| 1819 | (count (length l)) |
| 1820 | (succ 0)) |
| 1821 | ;; canonicalize file list for pop up |
| 1822 | (setq files (nreverse (mapcar (function dired-make-relative) files))) |
| 1823 | (if (dired-mark-pop-up |
| 1824 | " *Deletions*" 'delete files dired-deletion-confirmer |
| 1825 | (format "Delete %s " (dired-mark-prompt arg files))) |
| 1826 | (save-excursion |
| 1827 | (let (failures);; files better be in reverse order for this loop! |
| 1828 | (while l |
| 1829 | (goto-char (cdr (car l))) |
| 1830 | (let (buffer-read-only) |
| 1831 | (condition-case err |
| 1832 | (let ((fn (car (car l)))) |
| 1833 | ;; This test is equivalent to |
| 1834 | ;; (and (file-directory-p fn) (not (file-symlink-p fn))) |
| 1835 | ;; but more efficient |
| 1836 | (if (eq t (car (file-attributes fn))) |
| 1837 | (delete-directory fn) |
| 1838 | (delete-file fn)) |
| 1839 | ;; if we get here, removing worked |
| 1840 | (setq succ (1+ succ)) |
| 1841 | (message "%s of %s deletions" succ count) |
| 1842 | (delete-region (progn (beginning-of-line) (point)) |
| 1843 | (progn (forward-line 1) (point))) |
| 1844 | (dired-clean-up-after-deletion fn)) |
| 1845 | (error;; catch errors from failed deletions |
| 1846 | (dired-log "%s\n" err) |
| 1847 | (setq failures (cons (car (car l)) failures))))) |
| 1848 | (setq l (cdr l))) |
| 1849 | (if (not failures) |
| 1850 | (message "%d deletion%s done" count (dired-plural-s count)) |
| 1851 | (dired-log-summary |
| 1852 | (format "%d of %d deletion%s failed" |
| 1853 | (length failures) count |
| 1854 | (dired-plural-s count)) |
| 1855 | failures)))) |
| 1856 | (message "(No deletions performed)"))) |
| 1857 | (dired-move-to-filename)) |
| 1858 | |
| 1859 | ;; This is a separate function for the sake of dired-x.el. |
| 1860 | (defun dired-clean-up-after-deletion (fn) |
| 1861 | ;; Clean up after a deleted file or directory FN. |
| 1862 | (save-excursion (and (cdr dired-subdir-alist) |
| 1863 | (dired-goto-subdir fn) |
| 1864 | (dired-kill-subdir)))) |
| 1865 | \f |
| 1866 | ;; Confirmation |
| 1867 | |
| 1868 | (defun dired-marker-regexp () |
| 1869 | (concat "^" (regexp-quote (char-to-string dired-marker-char)))) |
| 1870 | |
| 1871 | (defun dired-plural-s (count) |
| 1872 | (if (= 1 count) "" "s")) |
| 1873 | |
| 1874 | (defun dired-mark-prompt (arg files) |
| 1875 | ;; Return a string for use in a prompt, either the current file |
| 1876 | ;; name, or the marker and a count of marked files. |
| 1877 | (let ((count (length files))) |
| 1878 | (if (= count 1) |
| 1879 | (car files) |
| 1880 | ;; more than 1 file: |
| 1881 | (if (integerp arg) |
| 1882 | ;; abs(arg) = count |
| 1883 | ;; Perhaps this is nicer, but it also takes more screen space: |
| 1884 | ;;(format "[%s %d files]" (if (> arg 0) "next" "previous") |
| 1885 | ;; count) |
| 1886 | (format "[next %d files]" arg) |
| 1887 | (format "%c [%d files]" dired-marker-char count))))) |
| 1888 | |
| 1889 | (defun dired-pop-to-buffer (buf) |
| 1890 | ;; Pop up buffer BUF. |
| 1891 | ;; If dired-shrink-to-fit is t, make its window fit its contents. |
| 1892 | (if (not dired-shrink-to-fit) |
| 1893 | (pop-to-buffer (get-buffer-create buf)) |
| 1894 | ;; let window shrink to fit: |
| 1895 | (let ((window (selected-window)) |
| 1896 | target-lines w2) |
| 1897 | (cond ;; if split-window-threshold is enabled, use the largest window |
| 1898 | ((and (> (window-height (setq w2 (get-largest-window))) |
| 1899 | split-height-threshold) |
| 1900 | (= (frame-width) (window-width w2))) |
| 1901 | (setq window w2)) |
| 1902 | ;; if the least-recently-used window is big enough, use it |
| 1903 | ((and (> (window-height (setq w2 (get-lru-window))) |
| 1904 | (* 2 window-min-height)) |
| 1905 | (= (frame-width) (window-width w2))) |
| 1906 | (setq window w2))) |
| 1907 | (save-excursion |
| 1908 | (set-buffer buf) |
| 1909 | (goto-char (point-max)) |
| 1910 | (skip-chars-backward "\n\r\t ") |
| 1911 | (setq target-lines (count-lines (point-min) (point))) |
| 1912 | ;; Don't forget to count the last line. |
| 1913 | (if (not (bolp)) |
| 1914 | (setq target-lines (1+ target-lines)))) |
| 1915 | (if (<= (window-height window) (* 2 window-min-height)) |
| 1916 | ;; At this point, every window on the frame is too small to split. |
| 1917 | (setq w2 (display-buffer buf)) |
| 1918 | (setq w2 (split-window window |
| 1919 | (max window-min-height |
| 1920 | (- (window-height window) |
| 1921 | (1+ (max window-min-height target-lines))))))) |
| 1922 | (set-window-buffer w2 buf) |
| 1923 | (if (< (1- (window-height w2)) target-lines) |
| 1924 | (progn |
| 1925 | (select-window w2) |
| 1926 | (enlarge-window (- target-lines (1- (window-height w2)))))) |
| 1927 | (set-window-start w2 1) |
| 1928 | ))) |
| 1929 | |
| 1930 | (defvar dired-no-confirm nil |
| 1931 | "A list of symbols for commands dired should not confirm. |
| 1932 | Command symbols are `byte-compile', `chgrp', `chmod', `chown', `compress', |
| 1933 | `copy', `delete', `hardlink', `load', `move', `print', `shell', `symlink' and |
| 1934 | `uncompress'.") |
| 1935 | |
| 1936 | (defun dired-mark-pop-up (bufname op-symbol files function &rest args) |
| 1937 | ;;"Args BUFNAME OP-SYMBOL FILES FUNCTION &rest ARGS. |
| 1938 | ;;Return FUNCTION's result on ARGS after popping up a window (in a buffer |
| 1939 | ;;named BUFNAME, nil gives \" *Marked Files*\") showing the marked |
| 1940 | ;;files. Uses function `dired-pop-to-buffer' to do that. |
| 1941 | ;; FUNCTION should not manipulate files. |
| 1942 | ;; It should only read input (an argument or confirmation). |
| 1943 | ;;The window is not shown if there is just one file or |
| 1944 | ;; OP-SYMBOL is a member of the list in `dired-no-confirm'. |
| 1945 | ;;FILES is the list of marked files." |
| 1946 | (or bufname (setq bufname " *Marked Files*")) |
| 1947 | (if (or (eq dired-no-confirm t) |
| 1948 | (memq op-symbol dired-no-confirm) |
| 1949 | (= (length files) 1)) |
| 1950 | (apply function args) |
| 1951 | (save-excursion |
| 1952 | (set-buffer (get-buffer-create bufname)) |
| 1953 | (erase-buffer) |
| 1954 | (dired-format-columns-of-files files) |
| 1955 | (remove-text-properties (point-min) (point-max) '(mouse-face))) |
| 1956 | (save-window-excursion |
| 1957 | (dired-pop-to-buffer bufname) |
| 1958 | (apply function args)))) |
| 1959 | |
| 1960 | (defun dired-format-columns-of-files (files) |
| 1961 | ;; Files should be in forward order for this loop. |
| 1962 | ;; i.e., (car files) = first file in buffer. |
| 1963 | ;; Returns the number of lines used. |
| 1964 | (let* ((maxlen (+ 2 (apply 'max (mapcar 'length files)))) |
| 1965 | (width (- (window-width (selected-window)) 2)) |
| 1966 | (columns (max 1 (/ width maxlen))) |
| 1967 | (nfiles (length files)) |
| 1968 | (rows (+ (/ nfiles columns) |
| 1969 | (if (zerop (% nfiles columns)) 0 1))) |
| 1970 | (i 0) |
| 1971 | (j 0)) |
| 1972 | (setq files (nconc (copy-sequence files) ; fill up with empty fns |
| 1973 | (make-list (- (* columns rows) nfiles) ""))) |
| 1974 | (setcdr (nthcdr (1- (length files)) files) files) ; make circular |
| 1975 | (while (< j rows) |
| 1976 | (while (< i columns) |
| 1977 | (indent-to (* i maxlen)) |
| 1978 | (insert (car files)) |
| 1979 | (setq files (nthcdr rows files) |
| 1980 | i (1+ i))) |
| 1981 | (insert "\n") |
| 1982 | (setq i 0 |
| 1983 | j (1+ j) |
| 1984 | files (cdr files))) |
| 1985 | rows)) |
| 1986 | \f |
| 1987 | ;; Commands to mark or flag file(s) at or near current line. |
| 1988 | |
| 1989 | (defun dired-repeat-over-lines (arg function) |
| 1990 | ;; This version skips non-file lines. |
| 1991 | (let ((pos (make-marker))) |
| 1992 | (beginning-of-line) |
| 1993 | (while (and (> arg 0) (not (eobp))) |
| 1994 | (setq arg (1- arg)) |
| 1995 | (beginning-of-line) |
| 1996 | (while (and (not (eobp)) (dired-between-files)) (forward-line 1)) |
| 1997 | (save-excursion |
| 1998 | (forward-line 1) |
| 1999 | (move-marker pos (1+ (point)))) |
| 2000 | (save-excursion (funcall function)) |
| 2001 | ;; Advance to the next line--actually, to the line that *was* next. |
| 2002 | ;; (If FUNCTION inserted some new lines in between, skip them.) |
| 2003 | (goto-char pos)) |
| 2004 | (while (and (< arg 0) (not (bobp))) |
| 2005 | (setq arg (1+ arg)) |
| 2006 | (forward-line -1) |
| 2007 | (while (and (not (bobp)) (dired-between-files)) (forward-line -1)) |
| 2008 | (beginning-of-line) |
| 2009 | (save-excursion (funcall function))) |
| 2010 | (move-marker pos nil) |
| 2011 | (dired-move-to-filename))) |
| 2012 | |
| 2013 | (defun dired-between-files () |
| 2014 | ;; Point must be at beginning of line |
| 2015 | ;; Should be equivalent to (save-excursion (not (dired-move-to-filename))) |
| 2016 | ;; but is about 1.5..2.0 times as fast. (Actually that's not worth it) |
| 2017 | (or (looking-at "^$\\|^. *$\\|^. total\\|^. wildcard") |
| 2018 | (and (looking-at dired-subdir-regexp) |
| 2019 | (save-excursion (not (dired-move-to-filename)))))) |
| 2020 | |
| 2021 | (defun dired-next-marked-file (arg &optional wrap opoint) |
| 2022 | "Move to the next marked file, wrapping around the end of the buffer." |
| 2023 | (interactive "p\np") |
| 2024 | (or opoint (setq opoint (point)));; return to where interactively started |
| 2025 | (if (if (> arg 0) |
| 2026 | (re-search-forward dired-re-mark nil t arg) |
| 2027 | (beginning-of-line) |
| 2028 | (re-search-backward dired-re-mark nil t (- arg))) |
| 2029 | (dired-move-to-filename) |
| 2030 | (if (null wrap) |
| 2031 | (progn |
| 2032 | (goto-char opoint) |
| 2033 | (error "No next marked file")) |
| 2034 | (message "(Wraparound for next marked file)") |
| 2035 | (goto-char (if (> arg 0) (point-min) (point-max))) |
| 2036 | (dired-next-marked-file arg nil opoint)))) |
| 2037 | |
| 2038 | (defun dired-prev-marked-file (arg &optional wrap) |
| 2039 | "Move to the previous marked file, wrapping around the end of the buffer." |
| 2040 | (interactive "p\np") |
| 2041 | (dired-next-marked-file (- arg) wrap)) |
| 2042 | |
| 2043 | (defun dired-file-marker (file) |
| 2044 | ;; Return FILE's marker, or nil if unmarked. |
| 2045 | (save-excursion |
| 2046 | (and (dired-goto-file file) |
| 2047 | (progn |
| 2048 | (beginning-of-line) |
| 2049 | (if (not (equal ?\040 (following-char))) |
| 2050 | (following-char)))))) |
| 2051 | |
| 2052 | (defun dired-mark-files-in-region (start end) |
| 2053 | (let (buffer-read-only) |
| 2054 | (if (> start end) |
| 2055 | (error "start > end")) |
| 2056 | (goto-char start) ; assumed at beginning of line |
| 2057 | (while (< (point) end) |
| 2058 | ;; Skip subdir line and following garbage like the `total' line: |
| 2059 | (while (and (< (point) end) (dired-between-files)) |
| 2060 | (forward-line 1)) |
| 2061 | (if (and (not (looking-at dired-re-dot)) |
| 2062 | (dired-get-filename nil t)) |
| 2063 | (progn |
| 2064 | (delete-char 1) |
| 2065 | (insert dired-marker-char))) |
| 2066 | (forward-line 1)))) |
| 2067 | |
| 2068 | (defun dired-mark (arg) |
| 2069 | "Mark the current (or next ARG) files. |
| 2070 | If on a subdir headerline, mark all its files except `.' and `..'. |
| 2071 | |
| 2072 | Use \\[dired-unmark-all-files] to remove all marks |
| 2073 | and \\[dired-unmark] on a subdir to remove the marks in |
| 2074 | this subdir." |
| 2075 | (interactive "P") |
| 2076 | (if (dired-get-subdir) |
| 2077 | (save-excursion (dired-mark-subdir-files)) |
| 2078 | (let (buffer-read-only) |
| 2079 | (dired-repeat-over-lines |
| 2080 | (prefix-numeric-value arg) |
| 2081 | (function (lambda () (delete-char 1) (insert dired-marker-char))))))) |
| 2082 | |
| 2083 | (defun dired-unmark (arg) |
| 2084 | "Unmark the current (or next ARG) files. |
| 2085 | If looking at a subdir, unmark all its files except `.' and `..'." |
| 2086 | (interactive "P") |
| 2087 | (let ((dired-marker-char ?\040)) |
| 2088 | (dired-mark arg))) |
| 2089 | |
| 2090 | (defun dired-flag-file-deletion (arg) |
| 2091 | "In dired, flag the current line's file for deletion. |
| 2092 | With prefix arg, repeat over several lines. |
| 2093 | |
| 2094 | If on a subdir headerline, mark all its files except `.' and `..'." |
| 2095 | (interactive "P") |
| 2096 | (let ((dired-marker-char dired-del-marker)) |
| 2097 | (dired-mark arg))) |
| 2098 | |
| 2099 | (defun dired-unmark-backward (arg) |
| 2100 | "In dired, move up lines and remove deletion flag there. |
| 2101 | Optional prefix ARG says how many lines to unflag; default is one line." |
| 2102 | (interactive "p") |
| 2103 | (dired-unmark (- arg))) |
| 2104 | |
| 2105 | (defun dired-do-toggle () |
| 2106 | "Toggle marks. |
| 2107 | That is, currently marked files become unmarked and vice versa. |
| 2108 | Files marked with other flags (such as `D') are not affected. |
| 2109 | `.' and `..' are never toggled. |
| 2110 | As always, hidden subdirs are not affected." |
| 2111 | (interactive) |
| 2112 | (save-excursion |
| 2113 | (goto-char (point-min)) |
| 2114 | (let (buffer-read-only) |
| 2115 | (while (not (eobp)) |
| 2116 | (or (dired-between-files) |
| 2117 | (looking-at dired-re-dot) |
| 2118 | ;; use subst instead of insdel because it does not move |
| 2119 | ;; the gap and thus should be faster and because |
| 2120 | ;; other characters are left alone automatically |
| 2121 | (apply 'subst-char-in-region |
| 2122 | (point) (1+ (point)) |
| 2123 | (if (eq ?\040 (following-char)) ; SPC |
| 2124 | (list ?\040 dired-marker-char) |
| 2125 | (list dired-marker-char ?\040)))) |
| 2126 | (forward-line 1))))) |
| 2127 | \f |
| 2128 | ;;; Commands to mark or flag files based on their characteristics or names. |
| 2129 | |
| 2130 | (defvar dired-regexp-history nil |
| 2131 | "History list of regular expressions used in Dired commands.") |
| 2132 | |
| 2133 | (defun dired-read-regexp (prompt) |
| 2134 | (read-from-minibuffer prompt nil nil nil 'dired-regexp-history)) |
| 2135 | |
| 2136 | (defun dired-mark-files-regexp (regexp &optional marker-char) |
| 2137 | "Mark all files matching REGEXP for use in later commands. |
| 2138 | A prefix argument means to unmark them instead. |
| 2139 | `.' and `..' are never marked. |
| 2140 | |
| 2141 | REGEXP is an Emacs regexp, not a shell wildcard. Thus, use `\\.o$' for |
| 2142 | object files--just `.o' will mark more than you might think." |
| 2143 | (interactive |
| 2144 | (list (dired-read-regexp (concat (if current-prefix-arg "Unmark" "Mark") |
| 2145 | " files (regexp): ")) |
| 2146 | (if current-prefix-arg ?\040))) |
| 2147 | (let ((dired-marker-char (or marker-char dired-marker-char))) |
| 2148 | (dired-mark-if |
| 2149 | (and (not (looking-at dired-re-dot)) |
| 2150 | (not (eolp)) ; empty line |
| 2151 | (let ((fn (dired-get-filename nil t))) |
| 2152 | (and fn (string-match regexp (file-name-nondirectory fn))))) |
| 2153 | "matching file"))) |
| 2154 | |
| 2155 | (defun dired-mark-files-containing-regexp (regexp &optional marker-char) |
| 2156 | "Mark all files with contents containing REGEXP for use in later commands. |
| 2157 | A prefix argument means to unmark them instead. |
| 2158 | `.' and `..' are never marked." |
| 2159 | (interactive |
| 2160 | (list (dired-read-regexp (concat (if current-prefix-arg "Unmark" "Mark") |
| 2161 | " files containing (regexp): ")) |
| 2162 | (if current-prefix-arg ?\040))) |
| 2163 | (let ((dired-marker-char (or marker-char dired-marker-char))) |
| 2164 | (dired-mark-if |
| 2165 | (and (not (looking-at dired-re-dot)) |
| 2166 | (not (eolp)) ; empty line |
| 2167 | (let ((fn (dired-get-filename nil t))) |
| 2168 | (and fn (let ((prebuf (get-file-buffer fn))) |
| 2169 | (message "Checking %s" fn) |
| 2170 | ;; For now we do it inside emacs |
| 2171 | ;; Grep might be better if there are a lot of files |
| 2172 | (if prebuf |
| 2173 | (with-current-buffer prebuf |
| 2174 | (save-excursion |
| 2175 | (goto-char (point-min)) |
| 2176 | (re-search-forward regexp nil t))) |
| 2177 | (with-temp-buffer |
| 2178 | (insert-file-contents fn) |
| 2179 | (goto-char (point-min)) |
| 2180 | (re-search-forward regexp nil t)))) |
| 2181 | ))) |
| 2182 | "matching file"))) |
| 2183 | |
| 2184 | (defun dired-flag-files-regexp (regexp) |
| 2185 | "In dired, flag all files containing the specified REGEXP for deletion. |
| 2186 | The match is against the non-directory part of the filename. Use `^' |
| 2187 | and `$' to anchor matches. Exclude subdirs by hiding them. |
| 2188 | `.' and `..' are never flagged." |
| 2189 | (interactive (list (dired-read-regexp "Flag for deletion (regexp): "))) |
| 2190 | (dired-mark-files-regexp regexp dired-del-marker)) |
| 2191 | |
| 2192 | (defun dired-mark-symlinks (unflag-p) |
| 2193 | "Mark all symbolic links. |
| 2194 | With prefix argument, unflag all those files." |
| 2195 | (interactive "P") |
| 2196 | (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char))) |
| 2197 | (dired-mark-if (looking-at dired-re-sym) "symbolic link"))) |
| 2198 | |
| 2199 | (defun dired-mark-directories (unflag-p) |
| 2200 | "Mark all directory file lines except `.' and `..'. |
| 2201 | With prefix argument, unflag all those files." |
| 2202 | (interactive "P") |
| 2203 | (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char))) |
| 2204 | (dired-mark-if (and (looking-at dired-re-dir) |
| 2205 | (not (looking-at dired-re-dot))) |
| 2206 | "directory file"))) |
| 2207 | |
| 2208 | (defun dired-mark-executables (unflag-p) |
| 2209 | "Mark all executable files. |
| 2210 | With prefix argument, unflag all those files." |
| 2211 | (interactive "P") |
| 2212 | (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char))) |
| 2213 | (dired-mark-if (looking-at dired-re-exe) "executable file"))) |
| 2214 | |
| 2215 | ;; dired-x.el has a dired-mark-sexp interactive command: mark |
| 2216 | ;; files for which PREDICATE returns non-nil. |
| 2217 | |
| 2218 | (defun dired-flag-auto-save-files (&optional unflag-p) |
| 2219 | "Flag for deletion files whose names suggest they are auto save files. |
| 2220 | A prefix argument says to unflag those files instead." |
| 2221 | (interactive "P") |
| 2222 | (let ((dired-marker-char (if unflag-p ?\040 dired-del-marker))) |
| 2223 | (dired-mark-if |
| 2224 | ;; It is less than general to check for # here, |
| 2225 | ;; but it's the only way this runs fast enough. |
| 2226 | (and (save-excursion (end-of-line) |
| 2227 | (or |
| 2228 | (eq (preceding-char) ?#) |
| 2229 | ;; Handle executables in case of -F option. |
| 2230 | ;; We need not worry about the other kinds |
| 2231 | ;; of markings that -F makes, since they won't |
| 2232 | ;; appear on real auto-save files. |
| 2233 | (if (eq (preceding-char) ?*) |
| 2234 | (progn |
| 2235 | (forward-char -1) |
| 2236 | (eq (preceding-char) ?#))))) |
| 2237 | (not (looking-at dired-re-dir)) |
| 2238 | (let ((fn (dired-get-filename t t))) |
| 2239 | (if fn (auto-save-file-name-p |
| 2240 | (file-name-nondirectory fn))))) |
| 2241 | "auto save file"))) |
| 2242 | |
| 2243 | (defvar dired-garbage-files-regexp |
| 2244 | "\\.log$\\|\\.toc$\\|\\.dvi$\\|\\.bak$\\|\\.orig$\\|\\.rej$" |
| 2245 | "*Regular expression to match \"garbage\" files for `dired-flag-garbage-files'.") |
| 2246 | |
| 2247 | (defun dired-flag-garbage-files () |
| 2248 | "Flag for deletion all files that match `dired-garbage-files-regexp'." |
| 2249 | (interactive) |
| 2250 | (dired-flag-files-regexp dired-garbage-files-regexp)) |
| 2251 | |
| 2252 | (defun dired-flag-backup-files (&optional unflag-p) |
| 2253 | "Flag all backup files (names ending with `~') for deletion. |
| 2254 | With prefix argument, unflag these files." |
| 2255 | (interactive "P") |
| 2256 | (let ((dired-marker-char (if unflag-p ?\ dired-del-marker))) |
| 2257 | (dired-mark-if |
| 2258 | ;; Don't call backup-file-name-p unless the last character looks like |
| 2259 | ;; it might be the end of a backup file name. This isn't very general, |
| 2260 | ;; but it's the only way this runs fast enough. |
| 2261 | (and (save-excursion (end-of-line) |
| 2262 | ;; Handle executables in case of -F option. |
| 2263 | ;; We need not worry about the other kinds |
| 2264 | ;; of markings that -F makes, since they won't |
| 2265 | ;; appear on real backup files. |
| 2266 | (if (eq (preceding-char) ?*) |
| 2267 | (forward-char -1)) |
| 2268 | (eq (preceding-char) ?~)) |
| 2269 | (not (looking-at dired-re-dir)) |
| 2270 | (let ((fn (dired-get-filename t t))) |
| 2271 | (if fn (backup-file-name-p fn)))) |
| 2272 | "backup file"))) |
| 2273 | |
| 2274 | (defun dired-change-marks (&optional old new) |
| 2275 | "Change all OLD marks to NEW marks. |
| 2276 | OLD and NEW are both characters used to mark files." |
| 2277 | (interactive |
| 2278 | (let* ((cursor-in-echo-area t) |
| 2279 | (old (progn (message "Change (old mark): ") (read-char))) |
| 2280 | (new (progn (message "Change %c marks to (new mark): " old) |
| 2281 | (read-char)))) |
| 2282 | (list old new))) |
| 2283 | (if (or (eq old ?\r) (eq new ?\r)) |
| 2284 | (ding) |
| 2285 | (let ((string (format "\n%c" old)) |
| 2286 | (buffer-read-only)) |
| 2287 | (save-excursion |
| 2288 | (goto-char (point-min)) |
| 2289 | (while (search-forward string nil t) |
| 2290 | (if (if (= old ?\ ) |
| 2291 | (save-match-data |
| 2292 | (dired-get-filename 'no-dir t)) |
| 2293 | t) |
| 2294 | (subst-char-in-region (match-beginning 0) |
| 2295 | (match-end 0) old new))))))) |
| 2296 | |
| 2297 | (defun dired-unmark-all-marks () |
| 2298 | "Remove all marks from all files in the Dired buffer." |
| 2299 | (interactive) |
| 2300 | (dired-unmark-all-files ?\r)) |
| 2301 | |
| 2302 | (defun dired-unmark-all-files (mark &optional arg) |
| 2303 | "Remove a specific mark (or any mark) from every file. |
| 2304 | After this command, type the mark character to remove, |
| 2305 | or type RET to remove all marks. |
| 2306 | With prefix arg, query for each marked file. |
| 2307 | Type \\[help-command] at that time for help." |
| 2308 | (interactive "cRemove marks (RET means all): \nP") |
| 2309 | (save-excursion |
| 2310 | (let* ((count 0) |
| 2311 | buffer-read-only case-fold-search query |
| 2312 | (string (format "\n%c" mark)) |
| 2313 | (help-form "\ |
| 2314 | Type SPC or `y' to unmark one file, DEL or `n' to skip to next, |
| 2315 | `!' to unmark all remaining files with no more questions.")) |
| 2316 | (goto-char (point-min)) |
| 2317 | (while (if (eq mark ?\r) |
| 2318 | (re-search-forward dired-re-mark nil t) |
| 2319 | (search-forward string nil t)) |
| 2320 | (if (or (not arg) |
| 2321 | (dired-query 'query "Unmark file `%s'? " |
| 2322 | (dired-get-filename t))) |
| 2323 | (progn (subst-char-in-region (1- (point)) (point) |
| 2324 | (preceding-char) ?\ ) |
| 2325 | (setq count (1+ count))))) |
| 2326 | (message (if (= count 1) "1 mark removed" |
| 2327 | "%d marks removed") |
| 2328 | count)))) |
| 2329 | \f |
| 2330 | ;; Logging failures operating on files, and showing the results. |
| 2331 | |
| 2332 | (defvar dired-log-buffer "*Dired log*") |
| 2333 | |
| 2334 | (defun dired-why () |
| 2335 | "Pop up a buffer with error log output from Dired. |
| 2336 | A group of errors from a single command ends with a formfeed. |
| 2337 | Thus, use \\[backward-page] to find the beginning of a group of errors." |
| 2338 | (interactive) |
| 2339 | (if (get-buffer dired-log-buffer) |
| 2340 | (let ((owindow (selected-window)) |
| 2341 | (window (display-buffer (get-buffer dired-log-buffer)))) |
| 2342 | (unwind-protect |
| 2343 | (progn |
| 2344 | (select-window window) |
| 2345 | (goto-char (point-max)) |
| 2346 | (recenter -1)) |
| 2347 | (select-window owindow))))) |
| 2348 | |
| 2349 | (defun dired-log (log &rest args) |
| 2350 | ;; Log a message or the contents of a buffer. |
| 2351 | ;; If LOG is a string and there are more args, it is formatted with |
| 2352 | ;; those ARGS. Usually the LOG string ends with a \n. |
| 2353 | ;; End each bunch of errors with (dired-log t): this inserts |
| 2354 | ;; current time and buffer, and a \f (formfeed). |
| 2355 | (let ((obuf (current-buffer))) |
| 2356 | (unwind-protect ; want to move point |
| 2357 | (progn |
| 2358 | (set-buffer (get-buffer-create dired-log-buffer)) |
| 2359 | (goto-char (point-max)) |
| 2360 | (let (buffer-read-only) |
| 2361 | (cond ((stringp log) |
| 2362 | (insert (if args |
| 2363 | (apply (function format) log args) |
| 2364 | log))) |
| 2365 | ((bufferp log) |
| 2366 | (insert-buffer log)) |
| 2367 | ((eq t log) |
| 2368 | (insert "\n\t" (current-time-string) |
| 2369 | "\tBuffer `" (buffer-name obuf) "'\n\f\n"))))) |
| 2370 | (set-buffer obuf)))) |
| 2371 | |
| 2372 | (defun dired-log-summary (string failures) |
| 2373 | (message (if failures "%s--type ? for details (%s)" |
| 2374 | "%s--type ? for details") |
| 2375 | string failures) |
| 2376 | ;; Log a summary describing a bunch of errors. |
| 2377 | (dired-log (concat "\n" string)) |
| 2378 | (dired-log t)) |
| 2379 | \f |
| 2380 | ;;; Sorting |
| 2381 | |
| 2382 | ;; Most ls can only sort by name or by date (with -t), nothing else. |
| 2383 | ;; GNU ls sorts on size with -S, on extension with -X, and unsorted with -U. |
| 2384 | ;; So anything that does not contain these is sort "by name". |
| 2385 | |
| 2386 | (defvar dired-ls-sorting-switches "SXU" |
| 2387 | "String of `ls' switches (single letters) except `t' that influence sorting.") |
| 2388 | |
| 2389 | (defvar dired-sort-by-date-regexp |
| 2390 | (concat "^-[^" dired-ls-sorting-switches |
| 2391 | "]*t[^" dired-ls-sorting-switches "]*$") |
| 2392 | "Regexp recognized by dired to set `by date' mode.") |
| 2393 | |
| 2394 | (defvar dired-sort-by-name-regexp |
| 2395 | (concat "^-[^t" dired-ls-sorting-switches "]+$") |
| 2396 | "Regexp recognized by dired to set `by name' mode.") |
| 2397 | |
| 2398 | (defun dired-sort-set-modeline () |
| 2399 | ;; Set modeline display according to dired-actual-switches. |
| 2400 | ;; Modeline display of "by name" or "by date" guarantees the user a |
| 2401 | ;; match with the corresponding regexps. Non-matching switches are |
| 2402 | ;; shown literally. |
| 2403 | (setq mode-name |
| 2404 | (let (case-fold-search) |
| 2405 | (cond ((string-match dired-sort-by-name-regexp dired-actual-switches) |
| 2406 | "Dired by name") |
| 2407 | ((string-match dired-sort-by-date-regexp dired-actual-switches) |
| 2408 | "Dired by date") |
| 2409 | (t |
| 2410 | (concat "Dired " dired-actual-switches))))) |
| 2411 | (force-mode-line-update)) |
| 2412 | |
| 2413 | (defun dired-sort-toggle-or-edit (&optional arg) |
| 2414 | "Toggle between sort by date/name and refresh the dired buffer. |
| 2415 | With a prefix argument you can edit the current listing switches instead." |
| 2416 | (interactive "P") |
| 2417 | (if arg |
| 2418 | (dired-sort-other |
| 2419 | (read-string "ls switches (must contain -l): " dired-actual-switches)) |
| 2420 | (dired-sort-toggle))) |
| 2421 | |
| 2422 | (defun dired-sort-toggle () |
| 2423 | ;; Toggle between sort by date/name. Reverts the buffer. |
| 2424 | (setq dired-actual-switches |
| 2425 | (let (case-fold-search) |
| 2426 | (if (string-match " " dired-actual-switches) |
| 2427 | ;; New toggle scheme: add/remove a trailing " -t" |
| 2428 | (if (string-match " -t\\'" dired-actual-switches) |
| 2429 | (dired-replace-in-string " -t\\'" "" dired-actual-switches) |
| 2430 | (concat dired-actual-switches " -t")) |
| 2431 | ;; old toggle scheme: look for some 't' switch and add/remove it |
| 2432 | (concat |
| 2433 | "-l" |
| 2434 | (dired-replace-in-string (concat "[-lt" |
| 2435 | dired-ls-sorting-switches "]") |
| 2436 | "" |
| 2437 | dired-actual-switches) |
| 2438 | (if (string-match (concat "[t" dired-ls-sorting-switches "]") |
| 2439 | dired-actual-switches) |
| 2440 | "" |
| 2441 | "t"))))) |
| 2442 | (dired-sort-set-modeline) |
| 2443 | (revert-buffer)) |
| 2444 | |
| 2445 | (defun dired-replace-in-string (regexp newtext string) |
| 2446 | ;; Replace REGEXP with NEWTEXT everywhere in STRING and return result. |
| 2447 | ;; NEWTEXT is taken literally---no \\DIGIT escapes will be recognized. |
| 2448 | (let ((result "") (start 0) mb me) |
| 2449 | (while (string-match regexp string start) |
| 2450 | (setq mb (match-beginning 0) |
| 2451 | me (match-end 0) |
| 2452 | result (concat result (substring string start mb) newtext) |
| 2453 | start me)) |
| 2454 | (concat result (substring string start)))) |
| 2455 | |
| 2456 | (defun dired-sort-other (switches &optional no-revert) |
| 2457 | ;; Specify new ls SWITCHES for current dired buffer. Values matching |
| 2458 | ;; `dired-sort-by-date-regexp' or `dired-sort-by-name-regexp' set the |
| 2459 | ;; minor mode accordingly, others appear literally in the mode line. |
| 2460 | ;; With optional second arg NO-REVERT, don't refresh the listing afterwards. |
| 2461 | (setq dired-actual-switches switches) |
| 2462 | (if (eq major-mode 'dired-mode) (dired-sort-set-modeline)) |
| 2463 | (or no-revert (revert-buffer))) |
| 2464 | \f |
| 2465 | ;; To make this file smaller, the less common commands |
| 2466 | ;; go in a separate file. But autoload them here |
| 2467 | ;; to make the separation invisible. |
| 2468 | |
| 2469 | (autoload 'dired-diff "dired-aux" |
| 2470 | "Compare file at point with file FILE using `diff'. |
| 2471 | FILE defaults to the file at the mark. |
| 2472 | The prompted-for file is the first file given to `diff'." |
| 2473 | t) |
| 2474 | |
| 2475 | (autoload 'dired-backup-diff "dired-aux" |
| 2476 | "Diff this file with its backup file or vice versa. |
| 2477 | Uses the latest backup, if there are several numerical backups. |
| 2478 | If this file is a backup, diff it with its original. |
| 2479 | The backup file is the first file given to `diff'." |
| 2480 | t) |
| 2481 | |
| 2482 | (autoload 'dired-clean-directory "dired-aux" |
| 2483 | "Flag numerical backups for deletion. |
| 2484 | Spares `dired-kept-versions' latest versions, and `kept-old-versions' oldest. |
| 2485 | Positive prefix arg KEEP overrides `dired-kept-versions'; |
| 2486 | Negative prefix arg KEEP overrides `kept-old-versions' with KEEP made positive. |
| 2487 | |
| 2488 | To clear the flags on these files, you can use \\[dired-flag-backup-files] |
| 2489 | with a prefix argument." |
| 2490 | t) |
| 2491 | |
| 2492 | (autoload 'dired-do-chmod "dired-aux" |
| 2493 | "Change the mode of the marked (or next ARG) files. |
| 2494 | This calls chmod, thus symbolic modes like `g+w' are allowed." |
| 2495 | t) |
| 2496 | |
| 2497 | (autoload 'dired-do-chgrp "dired-aux" |
| 2498 | "Change the group of the marked (or next ARG) files." |
| 2499 | t) |
| 2500 | |
| 2501 | (autoload 'dired-do-chown "dired-aux" |
| 2502 | "Change the owner of the marked (or next ARG) files." |
| 2503 | t) |
| 2504 | |
| 2505 | (autoload 'dired-do-print "dired-aux" |
| 2506 | "Print the marked (or next ARG) files. |
| 2507 | Uses the shell command coming from variables `lpr-command' and |
| 2508 | `lpr-switches' as default." |
| 2509 | t) |
| 2510 | |
| 2511 | (autoload 'dired-do-shell-command "dired-aux" |
| 2512 | "Run a shell command COMMAND on the marked files. |
| 2513 | If no files are marked or a specific numeric prefix arg is given, |
| 2514 | the next ARG files are used. Just \\[universal-argument] means the current file. |
| 2515 | The prompt mentions the file(s) or the marker, as appropriate. |
| 2516 | |
| 2517 | If there is output, it goes to a separate buffer. |
| 2518 | |
| 2519 | Normally the command is run on each file individually. |
| 2520 | However, if there is a `*' in the command then it is run |
| 2521 | just once with the entire file list substituted there. |
| 2522 | |
| 2523 | No automatic redisplay of dired buffers is attempted, as there's no |
| 2524 | telling what files the command may have changed. Type |
| 2525 | \\[dired-do-redisplay] to redisplay the marked files. |
| 2526 | |
| 2527 | The shell command has the top level directory as working directory, so |
| 2528 | output files usually are created there instead of in a subdir." |
| 2529 | t) |
| 2530 | |
| 2531 | (autoload 'dired-do-kill-lines "dired-aux" |
| 2532 | "Kill all marked lines (not the files). |
| 2533 | With a prefix arg, kill all lines not marked or flagged." |
| 2534 | t) |
| 2535 | |
| 2536 | (autoload 'dired-do-compress "dired-aux" |
| 2537 | "Compress or uncompress marked (or next ARG) files." |
| 2538 | t) |
| 2539 | |
| 2540 | (autoload 'dired-do-byte-compile "dired-aux" |
| 2541 | "Byte compile marked (or next ARG) Emacs Lisp files." |
| 2542 | t) |
| 2543 | |
| 2544 | (autoload 'dired-do-load "dired-aux" |
| 2545 | "Load the marked (or next ARG) Emacs Lisp files." |
| 2546 | t) |
| 2547 | |
| 2548 | (autoload 'dired-do-redisplay "dired-aux" |
| 2549 | "Redisplay all marked (or next ARG) files. |
| 2550 | If on a subdir line, redisplay that subdirectory. In that case, |
| 2551 | a prefix arg lets you edit the `ls' switches used for the new listing." |
| 2552 | t) |
| 2553 | |
| 2554 | (autoload 'dired-create-directory "dired-aux" |
| 2555 | "Create a directory called DIRECTORY." |
| 2556 | t) |
| 2557 | |
| 2558 | (autoload 'dired-do-copy "dired-aux" |
| 2559 | "Copy all marked (or next ARG) files, or copy the current file. |
| 2560 | Thus, a zero prefix argument copies nothing. But it toggles the |
| 2561 | variable `dired-copy-preserve-time' (which see)." |
| 2562 | t) |
| 2563 | |
| 2564 | (autoload 'dired-do-symlink "dired-aux" |
| 2565 | "Make symbolic links to current file or all marked (or next ARG) files. |
| 2566 | When operating on just the current file, you specify the new name. |
| 2567 | When operating on multiple or marked files, you specify a directory |
| 2568 | and new symbolic links are made in that directory |
| 2569 | with the same names that the files currently have." |
| 2570 | t) |
| 2571 | |
| 2572 | (autoload 'dired-do-hardlink "dired-aux" |
| 2573 | "Add names (hard links) current file or all marked (or next ARG) files. |
| 2574 | When operating on just the current file, you specify the new name. |
| 2575 | When operating on multiple or marked files, you specify a directory |
| 2576 | and new hard links are made in that directory |
| 2577 | with the same names that the files currently have." |
| 2578 | t) |
| 2579 | |
| 2580 | (autoload 'dired-do-rename "dired-aux" |
| 2581 | "Rename current file or all marked (or next ARG) files. |
| 2582 | When renaming just the current file, you specify the new name. |
| 2583 | When renaming multiple or marked files, you specify a directory." |
| 2584 | t) |
| 2585 | |
| 2586 | (autoload 'dired-do-rename-regexp "dired-aux" |
| 2587 | "Rename marked files containing REGEXP to NEWNAME. |
| 2588 | As each match is found, the user must type a character saying |
| 2589 | what to do with it. For directions, type \\[help-command] at that time. |
| 2590 | NEWNAME may contain \\=\\<n> or \\& as in `query-replace-regexp'. |
| 2591 | REGEXP defaults to the last regexp used. |
| 2592 | With a zero prefix arg, renaming by regexp affects the complete |
| 2593 | pathname - usually only the non-directory part of file names is used |
| 2594 | and changed." |
| 2595 | t) |
| 2596 | |
| 2597 | (autoload 'dired-do-copy-regexp "dired-aux" |
| 2598 | "Copy all marked files containing REGEXP to NEWNAME. |
| 2599 | See function `dired-rename-regexp' for more info." |
| 2600 | t) |
| 2601 | |
| 2602 | (autoload 'dired-do-hardlink-regexp "dired-aux" |
| 2603 | "Hardlink all marked files containing REGEXP to NEWNAME. |
| 2604 | See function `dired-rename-regexp' for more info." |
| 2605 | t) |
| 2606 | |
| 2607 | (autoload 'dired-do-symlink-regexp "dired-aux" |
| 2608 | "Symlink all marked files containing REGEXP to NEWNAME. |
| 2609 | See function `dired-rename-regexp' for more info." |
| 2610 | t) |
| 2611 | |
| 2612 | (autoload 'dired-upcase "dired-aux" |
| 2613 | "Rename all marked (or next ARG) files to upper case." |
| 2614 | t) |
| 2615 | |
| 2616 | (autoload 'dired-downcase "dired-aux" |
| 2617 | "Rename all marked (or next ARG) files to lower case." |
| 2618 | t) |
| 2619 | |
| 2620 | (autoload 'dired-maybe-insert-subdir "dired-aux" |
| 2621 | "Insert this subdirectory into the same dired buffer. |
| 2622 | If it is already present, just move to it (type \\[dired-do-redisplay] to refresh), |
| 2623 | else inserts it at its natural place (as `ls -lR' would have done). |
| 2624 | With a prefix arg, you may edit the ls switches used for this listing. |
| 2625 | You can add `R' to the switches to expand the whole tree starting at |
| 2626 | this subdirectory. |
| 2627 | This function takes some pains to conform to `ls -lR' output." |
| 2628 | t) |
| 2629 | |
| 2630 | (autoload 'dired-next-subdir "dired-aux" |
| 2631 | "Go to next subdirectory, regardless of level." |
| 2632 | t) |
| 2633 | |
| 2634 | (autoload 'dired-prev-subdir "dired-aux" |
| 2635 | "Go to previous subdirectory, regardless of level. |
| 2636 | When called interactively and not on a subdir line, go to this subdir's line." |
| 2637 | t) |
| 2638 | |
| 2639 | (autoload 'dired-goto-subdir "dired-aux" |
| 2640 | "Go to end of header line of DIR in this dired buffer. |
| 2641 | Return value of point on success, otherwise return nil. |
| 2642 | The next char is either \\n, or \\r if DIR is hidden." |
| 2643 | t) |
| 2644 | |
| 2645 | (autoload 'dired-mark-subdir-files "dired-aux" |
| 2646 | "Mark all files except `.' and `..'." |
| 2647 | t) |
| 2648 | |
| 2649 | (autoload 'dired-kill-subdir "dired-aux" |
| 2650 | "Remove all lines of current subdirectory. |
| 2651 | Lower levels are unaffected." |
| 2652 | t) |
| 2653 | |
| 2654 | (autoload 'dired-tree-up "dired-aux" |
| 2655 | "Go up ARG levels in the dired tree." |
| 2656 | t) |
| 2657 | |
| 2658 | (autoload 'dired-tree-down "dired-aux" |
| 2659 | "Go down in the dired tree." |
| 2660 | t) |
| 2661 | |
| 2662 | (autoload 'dired-hide-subdir "dired-aux" |
| 2663 | "Hide or unhide the current subdirectory and move to next directory. |
| 2664 | Optional prefix arg is a repeat factor. |
| 2665 | Use \\[dired-hide-all] to (un)hide all directories." |
| 2666 | t) |
| 2667 | |
| 2668 | (autoload 'dired-hide-all "dired-aux" |
| 2669 | "Hide all subdirectories, leaving only their header lines. |
| 2670 | If there is already something hidden, make everything visible again. |
| 2671 | Use \\[dired-hide-subdir] to (un)hide a particular subdirectory." |
| 2672 | t) |
| 2673 | \f |
| 2674 | (if (eq system-type 'vax-vms) |
| 2675 | (load "dired-vms")) |
| 2676 | |
| 2677 | (provide 'dired) |
| 2678 | |
| 2679 | (run-hooks 'dired-load-hook) ; for your customizations |
| 2680 | |
| 2681 | ;;; dired.el ends here |