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