Checked anti.texi, errors.texi, and maps.texi.
[emacs.git] / lisp / eshell / em-ls.el
blobe346806679ecaab99ac31ee5b9c87a0f7a2f834d
1 ;;; em-ls.el --- implementation of ls in Lisp
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 ;; 2008, 2009 Free Software Foundation, Inc.
6 ;; Author: John Wiegley <johnw@gnu.org>
8 ;; This file is part of GNU Emacs.
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 3 of the License, or
13 ;; (at your option) any later version.
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.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; Most of the command switches recognized by GNU's ls utility are
26 ;; supported ([(fileutils)ls invocation]).
28 ;;; Code:
30 (eval-when-compile
31 (require 'cl)
32 (require 'eshell))
33 (require 'esh-util)
34 (require 'esh-opt)
36 ;;;###autoload
37 (eshell-defgroup eshell-ls nil
38 "This module implements the \"ls\" utility fully in Lisp. If it is
39 passed any unrecognized command switches, it will revert to the
40 operating system's version. This version of \"ls\" uses text
41 properties to colorize its output based on the setting of
42 `eshell-ls-use-colors'."
43 :tag "Implementation of `ls' in Lisp"
44 :group 'eshell-module)
46 ;;; User Variables:
48 (defvar eshell-ls-orig-insert-directory
49 (symbol-function 'insert-directory)
50 "Preserve the original definition of `insert-directory'.")
52 (defcustom eshell-ls-unload-hook
53 (list
54 (function
55 (lambda ()
56 (fset 'insert-directory eshell-ls-orig-insert-directory))))
57 "*When unloading `eshell-ls', restore the definition of `insert-directory'."
58 :type 'hook
59 :group 'eshell-ls)
61 (defcustom eshell-ls-initial-args nil
62 "*If non-nil, this list of args is included before any call to `ls'.
63 This is useful for enabling human-readable format (-h), for example."
64 :type '(repeat :tag "Arguments" string)
65 :group 'eshell-ls)
67 (defcustom eshell-ls-dired-initial-args nil
68 "*If non-nil, args is included before any call to `ls' in Dired.
69 This is useful for enabling human-readable format (-h), for example."
70 :type '(repeat :tag "Arguments" string)
71 :group 'eshell-ls)
73 (defcustom eshell-ls-use-in-dired nil
74 "*If non-nil, use `eshell-ls' to read directories in Dired."
75 :set (lambda (symbol value)
76 (if value
77 (unless (and (boundp 'eshell-ls-use-in-dired)
78 eshell-ls-use-in-dired)
79 (fset 'insert-directory 'eshell-ls-insert-directory))
80 (when (and (boundp 'eshell-ls-insert-directory)
81 eshell-ls-use-in-dired)
82 (fset 'insert-directory eshell-ls-orig-insert-directory)))
83 (setq eshell-ls-use-in-dired value))
84 :type 'boolean
85 :require 'em-ls
86 :group 'eshell-ls)
88 (defcustom eshell-ls-default-blocksize 1024
89 "*The default blocksize to use when display file sizes with -s."
90 :type 'integer
91 :group 'eshell-ls)
93 (defcustom eshell-ls-exclude-regexp nil
94 "*Unless -a is specified, files matching this regexp will not be shown."
95 :type '(choice regexp (const nil))
96 :group 'eshell-ls)
98 (defcustom eshell-ls-exclude-hidden t
99 "*Unless -a is specified, files beginning with . will not be shown.
100 Using this boolean, instead of `eshell-ls-exclude-regexp', is both
101 faster and conserves more memory."
102 :type 'boolean
103 :group 'eshell-ls)
105 (defcustom eshell-ls-use-colors t
106 "*If non-nil, use colors in file listings."
107 :type 'boolean
108 :group 'eshell-ls)
110 (defface eshell-ls-directory
111 '((((class color) (background light)) (:foreground "Blue" :weight bold))
112 (((class color) (background dark)) (:foreground "SkyBlue" :weight bold))
113 (t (:weight bold)))
114 "*The face used for highlight directories."
115 :group 'eshell-ls)
116 ;; backward-compatibility alias
117 (put 'eshell-ls-directory-face 'face-alias 'eshell-ls-directory)
119 (defface eshell-ls-symlink
120 '((((class color) (background light)) (:foreground "Dark Cyan" :weight bold))
121 (((class color) (background dark)) (:foreground "Cyan" :weight bold)))
122 "*The face used for highlight symbolic links."
123 :group 'eshell-ls)
124 ;; backward-compatibility alias
125 (put 'eshell-ls-symlink-face 'face-alias 'eshell-ls-symlink)
127 (defface eshell-ls-executable
128 '((((class color) (background light)) (:foreground "ForestGreen" :weight bold))
129 (((class color) (background dark)) (:foreground "Green" :weight bold)))
130 "*The face used for highlighting executables (not directories, though)."
131 :group 'eshell-ls)
132 ;; backward-compatibility alias
133 (put 'eshell-ls-executable-face 'face-alias 'eshell-ls-executable)
135 (defface eshell-ls-readonly
136 '((((class color) (background light)) (:foreground "Brown"))
137 (((class color) (background dark)) (:foreground "Pink")))
138 "*The face used for highlighting read-only files."
139 :group 'eshell-ls)
140 ;; backward-compatibility alias
141 (put 'eshell-ls-readonly-face 'face-alias 'eshell-ls-readonly)
143 (defface eshell-ls-unreadable
144 '((((class color) (background light)) (:foreground "Grey30"))
145 (((class color) (background dark)) (:foreground "DarkGrey")))
146 "*The face used for highlighting unreadable files."
147 :group 'eshell-ls)
148 ;; backward-compatibility alias
149 (put 'eshell-ls-unreadable-face 'face-alias 'eshell-ls-unreadable)
151 (defface eshell-ls-special
152 '((((class color) (background light)) (:foreground "Magenta" :weight bold))
153 (((class color) (background dark)) (:foreground "Magenta" :weight bold)))
154 "*The face used for highlighting non-regular files."
155 :group 'eshell-ls)
156 ;; backward-compatibility alias
157 (put 'eshell-ls-special-face 'face-alias 'eshell-ls-special)
159 (defface eshell-ls-missing
160 '((((class color) (background light)) (:foreground "Red" :weight bold))
161 (((class color) (background dark)) (:foreground "Red" :weight bold)))
162 "*The face used for highlighting non-existent file names."
163 :group 'eshell-ls)
164 ;; backward-compatibility alias
165 (put 'eshell-ls-missing-face 'face-alias 'eshell-ls-missing)
167 (defcustom eshell-ls-archive-regexp
168 (concat "\\.\\(t\\(a[rz]\\|gz\\)\\|arj\\|lzh\\|"
169 "zip\\|[zZ]\\|gz\\|bz2\\|deb\\|rpm\\)\\'")
170 "*A regular expression that matches names of file archives.
171 This typically includes both traditional archives and compressed
172 files."
173 :type 'regexp
174 :group 'eshell-ls)
176 (defface eshell-ls-archive
177 '((((class color) (background light)) (:foreground "Orchid" :weight bold))
178 (((class color) (background dark)) (:foreground "Orchid" :weight bold)))
179 "*The face used for highlighting archived and compressed file names."
180 :group 'eshell-ls)
181 ;; backward-compatibility alias
182 (put 'eshell-ls-archive-face 'face-alias 'eshell-ls-archive)
184 (defcustom eshell-ls-backup-regexp
185 "\\(\\`\\.?#\\|\\(\\.bak\\|~\\)\\'\\)"
186 "*A regular expression that matches names of backup files."
187 :type 'regexp
188 :group 'eshell-ls)
190 (defface eshell-ls-backup
191 '((((class color) (background light)) (:foreground "OrangeRed"))
192 (((class color) (background dark)) (:foreground "LightSalmon")))
193 "*The face used for highlighting backup file names."
194 :group 'eshell-ls)
195 ;; backward-compatibility alias
196 (put 'eshell-ls-backup-face 'face-alias 'eshell-ls-backup)
198 (defcustom eshell-ls-product-regexp
199 "\\.\\(elc\\|o\\(bj\\)?\\|a\\|lib\\|res\\)\\'"
200 "*A regular expression that matches names of product files.
201 Products are files that get generated from a source file, and hence
202 ought to be recreatable if they are deleted."
203 :type 'regexp
204 :group 'eshell-ls)
206 (defface eshell-ls-product
207 '((((class color) (background light)) (:foreground "OrangeRed"))
208 (((class color) (background dark)) (:foreground "LightSalmon")))
209 "*The face used for highlighting files that are build products."
210 :group 'eshell-ls)
211 ;; backward-compatibility alias
212 (put 'eshell-ls-product-face 'face-alias 'eshell-ls-product)
214 (defcustom eshell-ls-clutter-regexp
215 "\\(^texput\\.log\\|^core\\)\\'"
216 "*A regular expression that matches names of junk files.
217 These are mainly files that get created for various reasons, but don't
218 really need to stick around for very long."
219 :type 'regexp
220 :group 'eshell-ls)
222 (defface eshell-ls-clutter
223 '((((class color) (background light)) (:foreground "OrangeRed" :weight bold))
224 (((class color) (background dark)) (:foreground "OrangeRed" :weight bold)))
225 "*The face used for highlighting junk file names."
226 :group 'eshell-ls)
227 ;; backward-compatibility alias
228 (put 'eshell-ls-clutter-face 'face-alias 'eshell-ls-clutter)
230 (defsubst eshell-ls-filetype-p (attrs type)
231 "Test whether ATTRS specifies a directory."
232 (if (nth 8 attrs)
233 (eq (aref (nth 8 attrs) 0) type)))
235 (defmacro eshell-ls-applicable (attrs index func file)
236 "Test whether, for ATTRS, the user UID can do what corresponds to INDEX.
237 This is really just for efficiency, to avoid having to stat the file
238 yet again."
239 `(if (numberp (nth 2 ,attrs))
240 (if (= (user-uid) (nth 2 ,attrs))
241 (not (eq (aref (nth 8 ,attrs) ,index) ?-))
242 (,(eval func) ,file))
243 (not (eq (aref (nth 8 ,attrs)
244 (+ ,index (if (member (nth 2 ,attrs)
245 (eshell-current-ange-uids))
246 0 6)))
247 ?-))))
249 (defcustom eshell-ls-highlight-alist nil
250 "*This alist correlates test functions to color.
251 The format of the members of this alist is
253 (TEST-SEXP . FACE)
255 If TEST-SEXP evals to non-nil, that face will be used to highlight the
256 name of the file. The first match wins. `file' and `attrs' are in
257 scope during the evaluation of TEST-SEXP."
258 :type '(repeat (cons function face))
259 :group 'eshell-ls)
261 ;;; Functions:
263 (defun eshell-ls-insert-directory
264 (file switches &optional wildcard full-directory-p)
265 "Insert directory listing for FILE, formatted according to SWITCHES.
266 Leaves point after the inserted text.
267 SWITCHES may be a string of options, or a list of strings.
268 Optional third arg WILDCARD means treat FILE as shell wildcard.
269 Optional fourth arg FULL-DIRECTORY-P means file is a directory and
270 switches do not contain `d', so that a full listing is expected.
272 This version of the function uses `eshell/ls'. If any of the switches
273 passed are not recognized, the operating system's version will be used
274 instead."
275 (let ((handler (find-file-name-handler file 'insert-directory)))
276 (if handler
277 (funcall handler 'insert-directory file switches
278 wildcard full-directory-p)
279 (if (stringp switches)
280 (setq switches (split-string switches)))
281 (let (eshell-current-handles
282 eshell-current-subjob-p
283 font-lock-mode)
284 ;; use the fancy highlighting in `eshell-ls' rather than font-lock
285 (when (and eshell-ls-use-colors
286 (featurep 'font-lock))
287 (font-lock-mode -1)
288 (setq font-lock-defaults nil)
289 (if (boundp 'font-lock-buffers)
290 (set 'font-lock-buffers
291 (delq (current-buffer)
292 (symbol-value 'font-lock-buffers)))))
293 (let ((insert-func 'insert)
294 (error-func 'insert)
295 (flush-func 'ignore)
296 eshell-ls-dired-initial-args)
297 (eshell-do-ls (append switches (list file))))))))
299 (defsubst eshell/ls (&rest args)
300 "An alias version of `eshell-do-ls'."
301 (let ((insert-func 'eshell-buffered-print)
302 (error-func 'eshell-error)
303 (flush-func 'eshell-flush))
304 (eshell-do-ls args)))
306 (put 'eshell/ls 'eshell-no-numeric-conversions t)
308 (defvar block-size)
309 (defvar dereference-links)
310 (defvar dir-literal)
311 (defvar error-func)
312 (defvar flush-func)
313 (defvar human-readable)
314 (defvar ignore-pattern)
315 (defvar insert-func)
316 (defvar listing-style)
317 (defvar numeric-uid-gid)
318 (defvar reverse-list)
319 (defvar show-all)
320 (defvar show-recursive)
321 (defvar show-size)
322 (defvar sort-method)
323 (defvar ange-cache)
324 (defvar dired-flag)
326 (defun eshell-do-ls (&rest args)
327 "Implementation of \"ls\" in Lisp, passing ARGS."
328 (funcall flush-func -1)
329 ;; process the command arguments, and begin listing files
330 (eshell-eval-using-options
331 "ls" (if eshell-ls-initial-args
332 (list eshell-ls-initial-args args)
333 args)
334 `((?a "all" nil show-all
335 "show all files in directory")
336 (?c nil by-ctime sort-method
337 "sort by last status change time")
338 (?d "directory" nil dir-literal
339 "list directory entries instead of contents")
340 (?k "kilobytes" 1024 block-size
341 "using 1024 as the block size")
342 (?h "human-readable" 1024 human-readable
343 "print sizes in human readable format")
344 (?H "si" 1000 human-readable
345 "likewise, but use powers of 1000 not 1024")
346 (?I "ignore" t ignore-pattern
347 "do not list implied entries matching pattern")
348 (?l nil long-listing listing-style
349 "use a long listing format")
350 (?n "numeric-uid-gid" nil numeric-uid-gid
351 "list numeric UIDs and GIDs instead of names")
352 (?r "reverse" nil reverse-list
353 "reverse order while sorting")
354 (?s "size" nil show-size
355 "print size of each file, in blocks")
356 (?t nil by-mtime sort-method
357 "sort by modification time")
358 (?u nil by-atime sort-method
359 "sort by last access time")
360 (?x nil by-lines listing-style
361 "list entries by lines instead of by columns")
362 (?C nil by-columns listing-style
363 "list entries by columns")
364 (?L "dereference" nil dereference-links
365 "list entries pointed to by symbolic links")
366 (?R "recursive" nil show-recursive
367 "list subdirectories recursively")
368 (?S nil by-size sort-method
369 "sort by file size")
370 (?U nil unsorted sort-method
371 "do not sort; list entries in directory order")
372 (?X nil by-extension sort-method
373 "sort alphabetically by entry extension")
374 (?1 nil single-column listing-style
375 "list one file per line")
376 (nil "dired" nil dired-flag
377 "Here for compatibility with GNU ls.")
378 (nil "help" nil nil
379 "show this usage display")
380 :external "ls"
381 :usage "[OPTION]... [FILE]...
382 List information about the FILEs (the current directory by default).
383 Sort entries alphabetically across.")
384 ;; setup some defaults, based on what the user selected
385 (unless block-size
386 (setq block-size eshell-ls-default-blocksize))
387 (unless listing-style
388 (setq listing-style 'by-columns))
389 (unless args
390 (setq args (list ".")))
391 (let ((eshell-ls-exclude-regexp eshell-ls-exclude-regexp) ange-cache)
392 (when ignore-pattern
393 (unless (eshell-using-module 'eshell-glob)
394 (error (concat "-I option requires that `eshell-glob'"
395 " be a member of `eshell-modules-list'")))
396 (set-text-properties 0 (length ignore-pattern) nil ignore-pattern)
397 (setq eshell-ls-exclude-regexp
398 (if eshell-ls-exclude-regexp
399 (concat "\\(" eshell-ls-exclude-regexp "\\|"
400 (eshell-glob-regexp ignore-pattern) "\\)")
401 (eshell-glob-regexp ignore-pattern))))
402 ;; list the files!
403 (eshell-ls-entries
404 (mapcar (function
405 (lambda (arg)
406 (cons (if (and (eshell-under-windows-p)
407 (file-name-absolute-p arg))
408 (expand-file-name arg)
409 arg)
410 (eshell-file-attributes arg))))
411 args)
412 t (expand-file-name default-directory)))
413 (funcall flush-func)))
415 (defsubst eshell-ls-printable-size (filesize &optional by-blocksize)
416 "Return a printable FILESIZE."
417 (eshell-printable-size filesize human-readable
418 (and by-blocksize block-size)
419 eshell-ls-use-colors))
421 (defsubst eshell-ls-size-string (attrs size-width)
422 "Return the size string for ATTRS length, using SIZE-WIDTH."
423 (let* ((str (eshell-ls-printable-size (nth 7 attrs) t))
424 (len (length str)))
425 (if (< len size-width)
426 (concat (make-string (- size-width len) ? ) str)
427 str)))
429 (defun eshell-ls-annotate (fileinfo)
430 "Given a FILEINFO object, return a resolved, decorated FILEINFO.
431 This means resolving any symbolic links, determining what face the
432 name should be displayed as, etc. Think of it as cooking a FILEINFO."
433 (if (not (and (stringp (cadr fileinfo))
434 (or dereference-links
435 (eq listing-style 'long-listing))))
436 (setcar fileinfo (eshell-ls-decorated-name fileinfo))
437 (let (dir attr)
438 (unless (file-name-absolute-p (cadr fileinfo))
439 (setq dir (file-truename
440 (file-name-directory
441 (expand-file-name (car fileinfo))))))
442 (setq attr
443 (eshell-file-attributes
444 (let ((target (if dir
445 (expand-file-name (cadr fileinfo) dir)
446 (cadr fileinfo))))
447 (if dereference-links
448 (file-truename target)
449 target))))
450 (if (or dereference-links
451 (string-match "^\\.\\.?$" (car fileinfo)))
452 (progn
453 (setcdr fileinfo attr)
454 (setcar fileinfo (eshell-ls-decorated-name fileinfo)))
455 (assert (eq listing-style 'long-listing))
456 (setcar fileinfo
457 (concat (eshell-ls-decorated-name fileinfo) " -> "
458 (eshell-ls-decorated-name
459 (cons (cadr fileinfo) attr)))))))
460 fileinfo)
462 (defun eshell-ls-file (fileinfo &optional size-width copy-fileinfo)
463 "Output FILE in long format.
464 FILE may be a string, or a cons cell whose car is the filename and
465 whose cdr is the list of file attributes."
466 (if (not (cdr fileinfo))
467 (funcall error-func (format "%s: No such file or directory\n"
468 (car fileinfo)))
469 (setq fileinfo
470 (eshell-ls-annotate (if copy-fileinfo
471 (cons (car fileinfo)
472 (cdr fileinfo))
473 fileinfo)))
474 (let ((file (car fileinfo))
475 (attrs (cdr fileinfo)))
476 (if (not (eq listing-style 'long-listing))
477 (if show-size
478 (funcall insert-func (eshell-ls-size-string attrs size-width)
479 " " file "\n")
480 (funcall insert-func file "\n"))
481 (let ((line
482 (concat
483 (if show-size
484 (concat (eshell-ls-size-string attrs size-width) " "))
485 (format
486 (if numeric-uid-gid
487 "%s%4d %-8s %-8s "
488 "%s%4d %-14s %-8s ")
489 (or (nth 8 attrs) "??????????")
490 (or (nth 1 attrs) 0)
491 (or (let ((user (nth 2 attrs)))
492 (and (stringp user)
493 (eshell-substring user 14)))
494 (nth 2 attrs)
496 (or (let ((group (nth 3 attrs)))
497 (and (stringp group)
498 (eshell-substring group 8)))
499 (nth 3 attrs)
500 ""))
501 (let* ((str (eshell-ls-printable-size (nth 7 attrs)))
502 (len (length str)))
503 ;; Let file sizes shorter than 9 align neatly.
504 (if (< len (or size-width 8))
505 (concat (make-string (- (or size-width 8) len) ? ) str)
506 str))
507 " " (format-time-string
508 (concat
509 "%b %e "
510 (if (= (nth 5 (decode-time (current-time)))
511 (nth 5 (decode-time
512 (nth (cond
513 ((eq sort-method 'by-atime) 4)
514 ((eq sort-method 'by-ctime) 6)
515 (t 5)) attrs))))
516 "%H:%M"
517 " %Y")) (nth (cond
518 ((eq sort-method 'by-atime) 4)
519 ((eq sort-method 'by-ctime) 6)
520 (t 5)) attrs)) " ")))
521 (funcall insert-func line file "\n"))))))
523 (defun eshell-ls-dir (dirinfo &optional insert-name root-dir size-width)
524 "Output the entries in DIRINFO.
525 If INSERT-NAME is non-nil, the name of DIRINFO will be output. If
526 ROOT-DIR is also non-nil, and a directory name, DIRINFO will be output
527 relative to that directory."
528 (let ((dir (car dirinfo)))
529 (if (not (cdr dirinfo))
530 (funcall error-func (format "%s: No such file or directory\n" dir))
531 (if dir-literal
532 (eshell-ls-file dirinfo size-width)
533 (if insert-name
534 (funcall insert-func
535 (eshell-ls-decorated-name
536 (cons (concat
537 (if root-dir
538 (file-relative-name dir root-dir)
539 (expand-file-name dir)))
540 (cdr dirinfo))) ":\n"))
541 (let ((entries (eshell-directory-files-and-attributes
542 dir nil (and (not show-all)
543 eshell-ls-exclude-hidden
544 "\\`[^.]") t
545 ;; Asking for UID and GID as
546 ;; strings saves another syscall
547 ;; later when we are going to
548 ;; display user and group names.
549 (if numeric-uid-gid 'integer 'string))))
550 (when (and (not show-all) eshell-ls-exclude-regexp)
551 (while (and entries (string-match eshell-ls-exclude-regexp
552 (caar entries)))
553 (setq entries (cdr entries)))
554 (let ((e entries))
555 (while (cdr e)
556 (if (string-match eshell-ls-exclude-regexp (car (cadr e)))
557 (setcdr e (cddr e))
558 (setq e (cdr e))))))
559 (when (or (eq listing-style 'long-listing) show-size)
560 (let ((total 0.0))
561 (setq size-width 0)
562 (eshell-for e entries
563 (if (nth 7 (cdr e))
564 (setq total (+ total (nth 7 (cdr e)))
565 size-width
566 (max size-width
567 (length (eshell-ls-printable-size
568 (nth 7 (cdr e))
569 (not
570 ;; If we are under -l, count length
571 ;; of sizes in bytes, not in blocks.
572 (eq listing-style 'long-listing))))))))
573 (funcall insert-func "total "
574 (eshell-ls-printable-size total t) "\n")))
575 (let ((default-directory (expand-file-name dir)))
576 (if show-recursive
577 (eshell-ls-entries
578 (let ((e entries) (good-entries (list t)))
579 (while e
580 (unless (let ((len (length (caar e))))
581 (and (eq (aref (caar e) 0) ?.)
582 (or (= len 1)
583 (and (= len 2)
584 (eq (aref (caar e) 1) ?.)))))
585 (nconc good-entries (list (car e))))
586 (setq e (cdr e)))
587 (cdr good-entries))
588 nil root-dir)
589 (eshell-ls-files (eshell-ls-sort-entries entries)
590 size-width))))))))
592 (defsubst eshell-ls-compare-entries (l r inx func)
593 "Compare the time of two files, L and R, the attribute indexed by INX."
594 (let ((lt (nth inx (cdr l)))
595 (rt (nth inx (cdr r))))
596 (if (equal lt rt)
597 (string-lessp (directory-file-name (car l))
598 (directory-file-name (car r)))
599 (funcall func rt lt))))
601 (defun eshell-ls-sort-entries (entries)
602 "Sort the given ENTRIES, which may be files, directories or both.
603 In Eshell's implementation of ls, ENTRIES is always reversed."
604 (if (eq sort-method 'unsorted)
605 (nreverse entries)
606 (sort entries
607 (function
608 (lambda (l r)
609 (let ((result
610 (cond
611 ((eq sort-method 'by-atime)
612 (eshell-ls-compare-entries l r 4 'eshell-time-less-p))
613 ((eq sort-method 'by-mtime)
614 (eshell-ls-compare-entries l r 5 'eshell-time-less-p))
615 ((eq sort-method 'by-ctime)
616 (eshell-ls-compare-entries l r 6 'eshell-time-less-p))
617 ((eq sort-method 'by-size)
618 (eshell-ls-compare-entries l r 7 '<))
619 ((eq sort-method 'by-extension)
620 (let ((lx (file-name-extension
621 (directory-file-name (car l))))
622 (rx (file-name-extension
623 (directory-file-name (car r)))))
624 (cond
625 ((or (and (not lx) (not rx))
626 (equal lx rx))
627 (string-lessp (directory-file-name (car l))
628 (directory-file-name (car r))))
629 ((not lx) t)
630 ((not rx) nil)
632 (string-lessp lx rx)))))
634 (string-lessp (directory-file-name (car l))
635 (directory-file-name (car r)))))))
636 (if reverse-list
637 (not result)
638 result)))))))
640 (defun eshell-ls-files (files &optional size-width copy-fileinfo)
641 "Output a list of FILES.
642 Each member of FILES is either a string or a cons cell of the form
643 \(FILE . ATTRS)."
644 ;; Mimic behavior of coreutils ls, which lists a single file per
645 ;; line when output is not a tty. Exceptions: if -x was supplied,
646 ;; or if we are the _last_ command in a pipeline.
647 ;; FIXME Not really the same since not testing output destination.
648 (if (or (and eshell-in-pipeline-p
649 (not (eq eshell-in-pipeline-p 'last))
650 (not (eq listing-style 'by-lines)))
651 (memq listing-style '(long-listing single-column)))
652 (eshell-for file files
653 (if file
654 (eshell-ls-file file size-width copy-fileinfo)))
655 (let ((f files)
656 last-f
657 display-files
658 ignore)
659 (while f
660 (if (cdar f)
661 (setq last-f f
662 f (cdr f))
663 (unless ignore
664 (funcall error-func
665 (format "%s: No such file or directory\n" (caar f))))
666 (if (eq f files)
667 (setq files (cdr files)
668 f files)
669 (if (not (cdr f))
670 (progn
671 (setcdr last-f nil)
672 (setq f nil))
673 (setcar f (cadr f))
674 (setcdr f (cddr f))))))
675 (if (not show-size)
676 (setq display-files (mapcar 'eshell-ls-annotate files))
677 (eshell-for file files
678 (let* ((str (eshell-ls-printable-size (nth 7 (cdr file)) t))
679 (len (length str)))
680 (if (< len size-width)
681 (setq str (concat (make-string (- size-width len) ? ) str)))
682 (setq file (eshell-ls-annotate file)
683 display-files (cons (cons (concat str " " (car file))
684 (cdr file))
685 display-files))))
686 (setq display-files (nreverse display-files)))
687 (let* ((col-vals
688 (if (eq listing-style 'by-columns)
689 (eshell-ls-find-column-lengths display-files)
690 (assert (eq listing-style 'by-lines))
691 (eshell-ls-find-column-widths display-files)))
692 (col-widths (car col-vals))
693 (display-files (cdr col-vals))
694 (columns (length col-widths))
695 (col-index 1)
696 need-return)
697 (eshell-for file display-files
698 (let ((name
699 (if (car file)
700 (if show-size
701 (concat (substring (car file) 0 size-width)
702 (eshell-ls-decorated-name
703 (cons (substring (car file) size-width)
704 (cdr file))))
705 (eshell-ls-decorated-name file))
706 "")))
707 (if (< col-index columns)
708 (setq need-return
709 (concat need-return name
710 (make-string
711 (max 0 (- (aref col-widths
712 (1- col-index))
713 (length name))) ? ))
714 col-index (1+ col-index))
715 (funcall insert-func need-return name "\n")
716 (setq col-index 1 need-return nil))))
717 (if need-return
718 (funcall insert-func need-return "\n"))))))
720 (defun eshell-ls-entries (entries &optional separate root-dir)
721 "Output PATH's directory ENTRIES, formatted according to OPTIONS.
722 Each member of ENTRIES may either be a string or a cons cell, the car
723 of which is the file name, and the cdr of which is the list of
724 attributes.
725 If SEPARATE is non-nil, directories name will be entirely separated
726 from the filenames. This is the normal behavior, except when doing a
727 recursive listing.
728 ROOT-DIR, if non-nil, specifies the root directory of the listing, to
729 which non-absolute directory names will be made relative if ever they
730 need to be printed."
731 (let (dirs files show-names need-return (size-width 0))
732 (eshell-for entry entries
733 (if (and (not dir-literal)
734 (or (eshell-ls-filetype-p (cdr entry) ?d)
735 (and (eshell-ls-filetype-p (cdr entry) ?l)
736 (file-directory-p (car entry)))))
737 (progn
738 (unless separate
739 (setq files (cons entry files)
740 size-width
741 (if show-size
742 (max size-width
743 (length (eshell-ls-printable-size
744 (nth 7 (cdr entry)) t))))))
745 (setq dirs (cons entry dirs)))
746 (setq files (cons entry files)
747 size-width
748 (if show-size
749 (max size-width
750 (length (eshell-ls-printable-size
751 (nth 7 (cdr entry)) t)))))))
752 (when files
753 (eshell-ls-files (eshell-ls-sort-entries files)
754 size-width show-recursive)
755 (setq need-return t))
756 (setq show-names (or show-recursive
757 (> (+ (length files) (length dirs)) 1)))
758 (eshell-for dir (eshell-ls-sort-entries dirs)
759 (if (and need-return (not dir-literal))
760 (funcall insert-func "\n"))
761 (eshell-ls-dir dir show-names
762 (unless (file-name-absolute-p (car dir)) root-dir)
763 size-width)
764 (setq need-return t))))
766 (defun eshell-ls-find-column-widths (files)
767 "Find the best fitting column widths for FILES.
768 It will be returned as a vector, whose length is the number of columns
769 to use, and each member of which is the width of that column
770 \(including spacing)."
771 (let* ((numcols 0)
772 (width 0)
773 (widths
774 (mapcar
775 (function
776 (lambda (file)
777 (+ 2 (length (car file)))))
778 files))
779 ;; must account for the added space...
780 (max-width (+ (window-width) 2))
781 (best-width 0)
782 col-widths)
784 ;; determine the largest number of columns in the first row
785 (let ((w widths))
786 (while (and w (< width max-width))
787 (setq width (+ width (car w))
788 numcols (1+ numcols)
789 w (cdr w))))
791 ;; refine it based on the following rows
792 (while (> numcols 0)
793 (let ((i 0)
794 (colw (make-vector numcols 0))
795 (w widths))
796 (while w
797 (if (= i numcols)
798 (setq i 0))
799 (aset colw i (max (aref colw i) (car w)))
800 (setq w (cdr w) i (1+ i)))
801 (setq i 0 width 0)
802 (while (< i numcols)
803 (setq width (+ width (aref colw i))
804 i (1+ i)))
805 (if (and (< width max-width)
806 (> width best-width))
807 (setq col-widths colw
808 best-width width)))
809 (setq numcols (1- numcols)))
811 (cons (or col-widths (vector max-width)) files)))
813 (defun eshell-ls-find-column-lengths (files)
814 "Find the best fitting column lengths for FILES.
815 It will be returned as a vector, whose length is the number of columns
816 to use, and each member of which is the width of that column
817 \(including spacing)."
818 (let* ((numcols 1)
819 (width 0)
820 (widths
821 (mapcar
822 (function
823 (lambda (file)
824 (+ 2 (length (car file)))))
825 files))
826 (max-width (+ (window-width) 2))
827 col-widths
828 colw)
830 ;; refine it based on the following rows
831 (while numcols
832 (let* ((rows (ceiling (/ (length widths)
833 (float numcols))))
834 (w widths)
835 (len (* rows numcols))
836 (index 0)
837 (i 0))
838 (setq width 0)
839 (unless (or (= rows 0)
840 (<= (/ (length widths) (float rows))
841 (float (1- numcols))))
842 (setq colw (make-vector numcols 0))
843 (while (> len 0)
844 (if (= i numcols)
845 (setq i 0 index (1+ index)))
846 (aset colw i
847 (max (aref colw i)
848 (or (nth (+ (* i rows) index) w) 0)))
849 (setq len (1- len) i (1+ i)))
850 (setq i 0)
851 (while (< i numcols)
852 (setq width (+ width (aref colw i))
853 i (1+ i))))
854 (if (>= width max-width)
855 (setq numcols nil)
856 (if colw
857 (setq col-widths colw))
858 (if (>= numcols (length widths))
859 (setq numcols nil)
860 (setq numcols (1+ numcols))))))
862 (if (not col-widths)
863 (cons (vector max-width) files)
864 (setq numcols (length col-widths))
865 (let* ((rows (ceiling (/ (length widths)
866 (float numcols))))
867 (len (* rows numcols))
868 (newfiles (make-list len nil))
869 (index 0)
870 (i 0)
871 (j 0))
872 (while (< j len)
873 (if (= i numcols)
874 (setq i 0 index (1+ index)))
875 (setcar (nthcdr j newfiles)
876 (nth (+ (* i rows) index) files))
877 (setq j (1+ j) i (1+ i)))
878 (cons col-widths newfiles)))))
880 (defun eshell-ls-decorated-name (file)
881 "Return FILE, possibly decorated."
882 (if eshell-ls-use-colors
883 (let ((face
884 (cond
885 ((not (cdr file))
886 'eshell-ls-missing)
888 ((stringp (cadr file))
889 'eshell-ls-symlink)
891 ((eq (cadr file) t)
892 'eshell-ls-directory)
894 ((not (eshell-ls-filetype-p (cdr file) ?-))
895 'eshell-ls-special)
897 ((and (/= (user-uid) 0) ; root can execute anything
898 (eshell-ls-applicable (cdr file) 3
899 'file-executable-p (car file)))
900 'eshell-ls-executable)
902 ((not (eshell-ls-applicable (cdr file) 1
903 'file-readable-p (car file)))
904 'eshell-ls-unreadable)
906 ((string-match eshell-ls-archive-regexp (car file))
907 'eshell-ls-archive)
909 ((string-match eshell-ls-backup-regexp (car file))
910 'eshell-ls-backup)
912 ((string-match eshell-ls-product-regexp (car file))
913 'eshell-ls-product)
915 ((string-match eshell-ls-clutter-regexp (car file))
916 'eshell-ls-clutter)
918 ((not (eshell-ls-applicable (cdr file) 2
919 'file-writable-p (car file)))
920 'eshell-ls-readonly)
921 (eshell-ls-highlight-alist
922 (let ((tests eshell-ls-highlight-alist)
923 value)
924 (while tests
925 (if (funcall (caar tests) (car file) (cdr file))
926 (setq value (cdar tests) tests nil)
927 (setq tests (cdr tests))))
928 value)))))
929 (if face
930 (add-text-properties 0 (length (car file))
931 (list 'face face)
932 (car file)))))
933 (car file))
935 (provide 'em-ls)
937 ;; Local Variables:
938 ;; generated-autoload-file: "esh-groups.el"
939 ;; End:
941 ;; arch-tag: 9295181c-0cb2-499c-999b-89f5359842cb
942 ;;; em-ls.el ends here