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/>.
24 ;; Most of the command switches recognized by GNU's ls utility are
25 ;; supported ([(fileutils)ls invocation]).
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
)
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
55 (fset 'insert-directory eshell-ls-orig-insert-directory
))))
56 "When unloading `eshell-ls', restore the definition of `insert-directory'."
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\"."
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
)
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
)
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
)
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
))
95 (defcustom eshell-ls-default-blocksize
1024
96 "The default blocksize to use when display file sizes with -s."
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
))
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."
112 (defcustom eshell-ls-use-colors t
113 "If non-nil, use colors in file listings."
117 (defface eshell-ls-directory
118 '((((class color
) (background light
)) (:foreground
"Blue" :weight bold
))
119 (((class color
) (background dark
)) (:foreground
"SkyBlue" :weight bold
))
121 "The face used for highlight directories."
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."
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)."
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."
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."
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."
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."
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
176 :version
"24.1" ; added xz
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."
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."
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."
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."
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."
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."
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."
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."
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)))
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
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
))
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
285 (let ((handler (find-file-name-handler file
'insert-directory
)))
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
294 ;; use the fancy highlighting in `eshell-ls' rather than font-lock
295 (when (and eshell-ls-use-colors
296 (featurep 'font-lock
))
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
)
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
)
319 (defvar dereference-links
)
323 (defvar human-readable
)
324 (defvar ignore-pattern
)
326 (defvar listing-style
)
327 (defvar numeric-uid-gid
)
328 (defvar reverse-list
)
330 (defvar show-recursive
)
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
)
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
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.")
389 "show this usage display")
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
396 (setq block-size eshell-ls-default-blocksize
))
397 (unless listing-style
398 (setq listing-style
'by-columns
))
400 (setq args
(list ".")))
401 (let ((eshell-ls-exclude-regexp eshell-ls-exclude-regexp
) ange-cache
)
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
))))
414 (mapcar (lambda (arg)
415 (cons (if (and (eshell-under-windows-p)
416 (file-name-absolute-p arg
))
417 (expand-file-name arg
)
419 (eshell-file-attributes
420 arg
(if numeric-uid-gid
'integer
'string
))))
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
))
435 (if (< len size-width
)
436 (concat (make-string (- size-width len
) ?
) 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
))
448 (unless (file-name-absolute-p (cadr fileinfo
))
449 (setq dir
(file-truename
451 (expand-file-name (car fileinfo
))))))
453 (eshell-file-attributes
454 (let ((target (if dir
455 (expand-file-name (cadr fileinfo
) dir
)
457 (if dereference-links
458 (file-truename target
)
460 (if (or dereference-links
461 (string-match "^\\.\\.?$" (car fileinfo
)))
463 (setcdr fileinfo attr
)
464 (setcar fileinfo
(eshell-ls-decorated-name fileinfo
)))
465 (assert (eq listing-style
'long-listing
))
467 (concat (eshell-ls-decorated-name fileinfo
) " -> "
468 (eshell-ls-decorated-name
469 (cons (cadr fileinfo
) attr
)))))))
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"
480 (eshell-ls-annotate (if copy-fileinfo
484 (let ((file (car fileinfo
))
485 (attrs (cdr fileinfo
)))
486 (if (not (eq listing-style
'long-listing
))
488 (funcall insert-func
(eshell-ls-size-string attrs size-width
)
490 (funcall insert-func file
"\n"))
494 (concat (eshell-ls-size-string attrs size-width
) " "))
499 (or (nth 8 attrs
) "??????????")
501 (or (let ((user (nth 2 attrs
)))
503 (eshell-substring user
14)))
506 (or (let ((group (nth 3 attrs
)))
508 (eshell-substring group
8)))
511 (let* ((str (eshell-ls-printable-size (nth 7 attrs
)))
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
)
517 " " (format-time-string
519 eshell-ls-date-format
" "
520 (if (= (nth 5 (decode-time (current-time)))
523 ((eq sort-method
'by-atime
) 4)
524 ((eq sort-method
'by-ctime
) 6)
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
))
542 (eshell-ls-file dirinfo size-width
)
545 (eshell-ls-decorated-name
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
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
563 (setq entries
(cdr entries
)))
566 (if (string-match eshell-ls-exclude-regexp
(car (cadr e
)))
569 (when (or (eq listing-style
'long-listing
) show-size
)
574 (setq total
(+ total
(nth 7 (cdr e
)))
577 (length (eshell-ls-printable-size
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
)))
588 (let ((e entries
) (good-entries (list t
)))
590 (unless (let ((len (length (caar e
))))
591 (and (eq (aref (caar e
) 0) ?.
)
594 (eq (aref (caar e
) 1) ?.
)))))
595 (nconc good-entries
(list (car e
))))
599 (eshell-ls-files (eshell-ls-sort-entries entries
)
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
))))
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
)
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
)))))
635 ((or (and (not lx
) (not rx
))
637 (string-lessp (directory-file-name (car l
))
638 (directory-file-name (car r
))))
642 (string-lessp lx rx
)))))
644 (string-lessp (directory-file-name (car l
))
645 (directory-file-name (car r
)))))))
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
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
)))
664 (eshell-ls-file file size-width copy-fileinfo
)))
675 (format "%s: No such file or directory\n" (caar f
))))
677 (setq files
(cdr files
)
684 (setcdr f
(cddr f
))))))
686 (setq display-files
(mapcar 'eshell-ls-annotate files
))
688 (let* ((str (eshell-ls-printable-size (nth 7 (cdr file
)) t
))
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
))
696 (setq display-files
(nreverse display-files
)))
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
))
707 (dolist (file display-files
)
711 (concat (substring (car file
) 0 size-width
)
712 (eshell-ls-decorated-name
713 (cons (substring (car file
) size-width
)
715 (eshell-ls-decorated-name file
))
717 (if (< col-index columns
)
719 (concat need-return name
721 (max 0 (- (aref col-widths
724 col-index
(1+ col-index
))
725 (funcall insert-func need-return name
"\n")
726 (setq col-index
1 need-return nil
))))
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
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
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
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
)))))
749 (setq files
(cons entry files
)
753 (length (eshell-ls-printable-size
754 (nth 7 (cdr entry
)) t
))))))
755 (setq dirs
(cons entry dirs
)))
756 (setq files
(cons entry files
)
760 (length (eshell-ls-printable-size
761 (nth 7 (cdr entry
)) t
)))))))
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
)
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)."
787 (+ 2 (length (car file
)))))
789 ;; must account for the added space...
790 (max-width (+ (window-width) 2))
794 ;; determine the largest number of columns in the first row
796 (while (and w
(< width max-width
))
797 (setq width
(+ width
(car w
))
801 ;; refine it based on the following rows
804 (colw (make-vector numcols
0))
809 (aset colw i
(max (aref colw i
) (car w
)))
810 (setq w
(cdr w
) i
(1+ i
)))
813 (setq width
(+ width
(aref colw i
))
815 (if (and (< width max-width
)
816 (> width best-width
))
817 (setq col-widths colw
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)."
834 (+ 2 (length (car file
)))))
836 (max-width (+ (window-width) 2))
840 ;; refine it based on the following rows
842 (let* ((rows (ceiling (/ (length widths
)
845 (len (* rows numcols
))
849 (unless (or (= rows
0)
850 (<= (/ (length widths
) (float rows
))
851 (float (1- numcols
))))
852 (setq colw
(make-vector numcols
0))
855 (setq i
0 index
(1+ index
)))
858 (or (nth (+ (* i rows
) index
) w
) 0)))
859 (setq len
(1- len
) i
(1+ i
)))
862 (setq width
(+ width
(aref colw i
))
864 (if (>= width max-width
)
867 (setq col-widths colw
))
868 (if (>= numcols
(length widths
))
870 (setq numcols
(1+ numcols
))))))
873 (cons (vector max-width
) files
)
874 (setq numcols
(length col-widths
))
875 (let* ((rows (ceiling (/ (length widths
)
877 (len (* rows numcols
))
878 (newfiles (make-list len nil
))
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
898 ((stringp (cadr file
))
902 'eshell-ls-directory
)
904 ((not (eshell-ls-filetype-p (cdr file
) ?-
))
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
))
919 ((string-match eshell-ls-backup-regexp
(car file
))
922 ((string-match eshell-ls-product-regexp
(car file
))
925 ((string-match eshell-ls-clutter-regexp
(car file
))
928 ((not (eshell-ls-applicable (cdr file
) 2
929 'file-writable-p
(car file
)))
931 (eshell-ls-highlight-alist
932 (let ((tests eshell-ls-highlight-alist
)
935 (if (funcall (caar tests
) (car file
) (cdr file
))
936 (setq value
(cdar tests
) tests nil
)
937 (setq tests
(cdr tests
))))
940 (add-text-properties 0 (length (car file
))
948 ;; generated-autoload-file: "esh-groups.el"
951 ;;; em-ls.el ends here