1 ;;; em-ls.el --- implementation of ls in Lisp
3 ;; Copyright (C) 1999-2011 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\"."
67 (defcustom eshell-ls-initial-args nil
68 "If non-nil, this list of args is included before any call to `ls'.
69 This is useful for enabling human-readable format (-h), for example."
70 :type
'(repeat :tag
"Arguments" string
)
73 (defcustom eshell-ls-dired-initial-args nil
74 "If non-nil, args is included before any call to `ls' in Dired.
75 This is useful for enabling human-readable format (-h), for example."
76 :type
'(repeat :tag
"Arguments" string
)
79 (defcustom eshell-ls-use-in-dired nil
80 "If non-nil, use `eshell-ls' to read directories in Dired."
81 :set
(lambda (symbol value
)
83 (unless (and (boundp 'eshell-ls-use-in-dired
)
84 eshell-ls-use-in-dired
)
85 (fset 'insert-directory
'eshell-ls-insert-directory
))
86 (when (and (boundp 'eshell-ls-insert-directory
)
87 eshell-ls-use-in-dired
)
88 (fset 'insert-directory eshell-ls-orig-insert-directory
)))
89 (setq eshell-ls-use-in-dired value
))
94 (defcustom eshell-ls-default-blocksize
1024
95 "The default blocksize to use when display file sizes with -s."
99 (defcustom eshell-ls-exclude-regexp nil
100 "Unless -a is specified, files matching this regexp will not be shown."
101 :type
'(choice regexp
(const nil
))
104 (defcustom eshell-ls-exclude-hidden t
105 "Unless -a is specified, files beginning with . will not be shown.
106 Using this boolean, instead of `eshell-ls-exclude-regexp', is both
107 faster and conserves more memory."
111 (defcustom eshell-ls-use-colors t
112 "If non-nil, use colors in file listings."
116 (defface eshell-ls-directory
117 '((((class color
) (background light
)) (:foreground
"Blue" :weight bold
))
118 (((class color
) (background dark
)) (:foreground
"SkyBlue" :weight bold
))
120 "The face used for highlight directories."
122 (define-obsolete-face-alias 'eshell-ls-directory-face
123 'eshell-ls-directory
"22.1")
125 (defface eshell-ls-symlink
126 '((((class color
) (background light
)) (:foreground
"Dark Cyan" :weight bold
))
127 (((class color
) (background dark
)) (:foreground
"Cyan" :weight bold
)))
128 "The face used for highlight symbolic links."
130 (define-obsolete-face-alias 'eshell-ls-symlink-face
'eshell-ls-symlink
"22.1")
132 (defface eshell-ls-executable
133 '((((class color
) (background light
)) (:foreground
"ForestGreen" :weight bold
))
134 (((class color
) (background dark
)) (:foreground
"Green" :weight bold
)))
135 "The face used for highlighting executables (not directories, though)."
137 (define-obsolete-face-alias 'eshell-ls-executable-face
138 'eshell-ls-executable
"22.1")
140 (defface eshell-ls-readonly
141 '((((class color
) (background light
)) (:foreground
"Brown"))
142 (((class color
) (background dark
)) (:foreground
"Pink")))
143 "The face used for highlighting read-only files."
145 (define-obsolete-face-alias 'eshell-ls-readonly-face
'eshell-ls-readonly
"22.1")
147 (defface eshell-ls-unreadable
148 '((((class color
) (background light
)) (:foreground
"Grey30"))
149 (((class color
) (background dark
)) (:foreground
"DarkGrey")))
150 "The face used for highlighting unreadable files."
152 (define-obsolete-face-alias 'eshell-ls-unreadable-face
153 'eshell-ls-unreadable
"22.1")
155 (defface eshell-ls-special
156 '((((class color
) (background light
)) (:foreground
"Magenta" :weight bold
))
157 (((class color
) (background dark
)) (:foreground
"Magenta" :weight bold
)))
158 "The face used for highlighting non-regular files."
160 (define-obsolete-face-alias 'eshell-ls-special-face
'eshell-ls-special
"22.1")
162 (defface eshell-ls-missing
163 '((((class color
) (background light
)) (:foreground
"Red" :weight bold
))
164 (((class color
) (background dark
)) (:foreground
"Red" :weight bold
)))
165 "The face used for highlighting non-existent file names."
167 (define-obsolete-face-alias 'eshell-ls-missing-face
'eshell-ls-missing
"22.1")
169 (defcustom eshell-ls-archive-regexp
170 (concat "\\.\\(t\\(a[rz]\\|gz\\)\\|arj\\|lzh\\|"
171 "zip\\|[zZ]\\|gz\\|bz2\\|xz\\|deb\\|rpm\\)\\'")
172 "A regular expression that matches names of file archives.
173 This typically includes both traditional archives and compressed
175 :version
"24.1" ; added xz
179 (defface eshell-ls-archive
180 '((((class color
) (background light
)) (:foreground
"Orchid" :weight bold
))
181 (((class color
) (background dark
)) (:foreground
"Orchid" :weight bold
)))
182 "The face used for highlighting archived and compressed file names."
184 (define-obsolete-face-alias 'eshell-ls-archive-face
'eshell-ls-archive
"22.1")
186 (defcustom eshell-ls-backup-regexp
187 "\\(\\`\\.?#\\|\\(\\.bak\\|~\\)\\'\\)"
188 "A regular expression that matches names of backup files."
192 (defface eshell-ls-backup
193 '((((class color
) (background light
)) (:foreground
"OrangeRed"))
194 (((class color
) (background dark
)) (:foreground
"LightSalmon")))
195 "The face used for highlighting backup file names."
197 (define-obsolete-face-alias 'eshell-ls-backup-face
'eshell-ls-backup
"22.1")
199 (defcustom eshell-ls-product-regexp
200 "\\.\\(elc\\|o\\(bj\\)?\\|a\\|lib\\|res\\)\\'"
201 "A regular expression that matches names of product files.
202 Products are files that get generated from a source file, and hence
203 ought to be recreatable if they are deleted."
207 (defface eshell-ls-product
208 '((((class color
) (background light
)) (:foreground
"OrangeRed"))
209 (((class color
) (background dark
)) (:foreground
"LightSalmon")))
210 "The face used for highlighting files that are build products."
212 (define-obsolete-face-alias 'eshell-ls-product-face
'eshell-ls-product
"22.1")
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."
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."
227 (define-obsolete-face-alias 'eshell-ls-clutter-face
'eshell-ls-clutter
"22.1")
229 (defsubst eshell-ls-filetype-p
(attrs type
)
230 "Test whether ATTRS specifies a directory."
232 (eq (aref (nth 8 attrs
) 0) type
)))
234 (defmacro eshell-ls-applicable
(attrs index func file
)
235 "Test whether, for ATTRS, the user can do what corresponds to INDEX.
236 ATTRS is a string of file modes. See `file-attributes'.
237 If we cannot determine the answer using ATTRS (e.g., if we need
238 to know what group the user is in), compute the return value by
239 calling FUNC with FILE as an argument."
240 `(let ((owner (nth 2 ,attrs
))
241 (modes (nth 8 ,attrs
)))
242 (cond ((cond ((numberp owner
)
243 (= owner
(user-uid)))
245 (or (string-equal owner
(user-login-name))
246 (member owner
(eshell-current-ange-uids)))))
247 ;; The user owns this file.
248 (not (eq (aref modes
,index
) ?-
)))
249 ((eq (aref modes
(+ ,index
3))
250 (aref modes
(+ ,index
6)))
251 ;; If the "group" and "other" fields give identical
252 ;; results, use that.
253 (not (eq (aref modes
(+ ,index
3)) ?-
)))
255 ;; Otherwise call FUNC.
256 (,(eval func
) ,file
)))))
258 (defcustom eshell-ls-highlight-alist nil
259 "This alist correlates test functions to color.
260 The format of the members of this alist is
264 If TEST-SEXP evals to non-nil, that face will be used to highlight the
265 name of the file. The first match wins. `file' and `attrs' are in
266 scope during the evaluation of TEST-SEXP."
267 :type
'(repeat (cons function face
))
272 (defun eshell-ls-insert-directory
273 (file switches
&optional wildcard full-directory-p
)
274 "Insert directory listing for FILE, formatted according to SWITCHES.
275 Leaves point after the inserted text.
276 SWITCHES may be a string of options, or a list of strings.
277 Optional third arg WILDCARD means treat FILE as shell wildcard.
278 Optional fourth arg FULL-DIRECTORY-P means file is a directory and
279 switches do not contain `d', so that a full listing is expected.
281 This version of the function uses `eshell/ls'. If any of the switches
282 passed are not recognized, the operating system's version will be used
284 (let ((handler (find-file-name-handler file
'insert-directory
)))
286 (funcall handler
'insert-directory file switches
287 wildcard full-directory-p
)
288 (if (stringp switches
)
289 (setq switches
(split-string switches
)))
290 (let (eshell-current-handles
291 eshell-current-subjob-p
293 ;; use the fancy highlighting in `eshell-ls' rather than font-lock
294 (when (and eshell-ls-use-colors
295 (featurep 'font-lock
))
297 (setq font-lock-defaults nil
)
298 (if (boundp 'font-lock-buffers
)
299 (set 'font-lock-buffers
300 (delq (current-buffer)
301 (symbol-value 'font-lock-buffers
)))))
302 (let ((insert-func 'insert
)
305 eshell-ls-dired-initial-args
)
306 (eshell-do-ls (append switches
(list file
))))))))
308 (defsubst eshell
/ls
(&rest args
)
309 "An alias version of `eshell-do-ls'."
310 (let ((insert-func 'eshell-buffered-print
)
311 (error-func 'eshell-error
)
312 (flush-func 'eshell-flush
))
313 (eshell-do-ls args
)))
315 (put 'eshell
/ls
'eshell-no-numeric-conversions t
)
318 (defvar dereference-links
)
322 (defvar human-readable
)
323 (defvar ignore-pattern
)
325 (defvar listing-style
)
326 (defvar numeric-uid-gid
)
327 (defvar reverse-list
)
329 (defvar show-recursive
)
335 (defun eshell-do-ls (&rest args
)
336 "Implementation of \"ls\" in Lisp, passing ARGS."
337 (funcall flush-func -
1)
338 ;; process the command arguments, and begin listing files
339 (eshell-eval-using-options
340 "ls" (if eshell-ls-initial-args
341 (list eshell-ls-initial-args args
)
343 `((?a
"all" nil show-all
344 "show all files in directory")
345 (?c nil by-ctime sort-method
346 "sort by last status change time")
347 (?d
"directory" nil dir-literal
348 "list directory entries instead of contents")
349 (?k
"kilobytes" 1024 block-size
350 "using 1024 as the block size")
351 (?h
"human-readable" 1024 human-readable
352 "print sizes in human readable format")
353 (?H
"si" 1000 human-readable
354 "likewise, but use powers of 1000 not 1024")
355 (?I
"ignore" t ignore-pattern
356 "do not list implied entries matching pattern")
357 (?l nil long-listing listing-style
358 "use a long listing format")
359 (?n
"numeric-uid-gid" nil numeric-uid-gid
360 "list numeric UIDs and GIDs instead of names")
361 (?r
"reverse" nil reverse-list
362 "reverse order while sorting")
363 (?s
"size" nil show-size
364 "print size of each file, in blocks")
365 (?t nil by-mtime sort-method
366 "sort by modification time")
367 (?u nil by-atime sort-method
368 "sort by last access time")
369 (?x nil by-lines listing-style
370 "list entries by lines instead of by columns")
371 (?C nil by-columns listing-style
372 "list entries by columns")
373 (?L
"dereference" nil dereference-links
374 "list entries pointed to by symbolic links")
375 (?R
"recursive" nil show-recursive
376 "list subdirectories recursively")
377 (?S nil by-size sort-method
379 (?U nil unsorted sort-method
380 "do not sort; list entries in directory order")
381 (?X nil by-extension sort-method
382 "sort alphabetically by entry extension")
383 (?
1 nil single-column listing-style
384 "list one file per line")
385 (nil "dired" nil dired-flag
386 "Here for compatibility with GNU ls.")
388 "show this usage display")
390 :usage
"[OPTION]... [FILE]...
391 List information about the FILEs (the current directory by default).
392 Sort entries alphabetically across.")
393 ;; setup some defaults, based on what the user selected
395 (setq block-size eshell-ls-default-blocksize
))
396 (unless listing-style
397 (setq listing-style
'by-columns
))
399 (setq args
(list ".")))
400 (let ((eshell-ls-exclude-regexp eshell-ls-exclude-regexp
) ange-cache
)
402 (unless (eshell-using-module 'eshell-glob
)
403 (error (concat "-I option requires that `eshell-glob'"
404 " be a member of `eshell-modules-list'")))
405 (set-text-properties 0 (length ignore-pattern
) nil ignore-pattern
)
406 (setq eshell-ls-exclude-regexp
407 (if eshell-ls-exclude-regexp
408 (concat "\\(" eshell-ls-exclude-regexp
"\\|"
409 (eshell-glob-regexp ignore-pattern
) "\\)")
410 (eshell-glob-regexp ignore-pattern
))))
413 (mapcar (lambda (arg)
414 (cons (if (and (eshell-under-windows-p)
415 (file-name-absolute-p arg
))
416 (expand-file-name arg
)
418 (eshell-file-attributes
419 arg
(if numeric-uid-gid
'integer
'string
))))
421 t
(expand-file-name default-directory
)))
422 (funcall flush-func
)))
424 (defsubst eshell-ls-printable-size
(filesize &optional by-blocksize
)
425 "Return a printable FILESIZE."
426 (eshell-printable-size filesize human-readable
427 (and by-blocksize block-size
)
428 eshell-ls-use-colors
))
430 (defsubst eshell-ls-size-string
(attrs size-width
)
431 "Return the size string for ATTRS length, using SIZE-WIDTH."
432 (let* ((str (eshell-ls-printable-size (nth 7 attrs
) t
))
434 (if (< len size-width
)
435 (concat (make-string (- size-width len
) ?
) str
)
438 (defun eshell-ls-annotate (fileinfo)
439 "Given a FILEINFO object, return a resolved, decorated FILEINFO.
440 This means resolving any symbolic links, determining what face the
441 name should be displayed as, etc. Think of it as cooking a FILEINFO."
442 (if (not (and (stringp (cadr fileinfo
))
443 (or dereference-links
444 (eq listing-style
'long-listing
))))
445 (setcar fileinfo
(eshell-ls-decorated-name fileinfo
))
447 (unless (file-name-absolute-p (cadr fileinfo
))
448 (setq dir
(file-truename
450 (expand-file-name (car fileinfo
))))))
452 (eshell-file-attributes
453 (let ((target (if dir
454 (expand-file-name (cadr fileinfo
) dir
)
456 (if dereference-links
457 (file-truename target
)
459 (if (or dereference-links
460 (string-match "^\\.\\.?$" (car fileinfo
)))
462 (setcdr fileinfo attr
)
463 (setcar fileinfo
(eshell-ls-decorated-name fileinfo
)))
464 (assert (eq listing-style
'long-listing
))
466 (concat (eshell-ls-decorated-name fileinfo
) " -> "
467 (eshell-ls-decorated-name
468 (cons (cadr fileinfo
) attr
)))))))
471 (defun eshell-ls-file (fileinfo &optional size-width copy-fileinfo
)
472 "Output FILE in long format.
473 FILE may be a string, or a cons cell whose car is the filename and
474 whose cdr is the list of file attributes."
475 (if (not (cdr fileinfo
))
476 (funcall error-func
(format "%s: No such file or directory\n"
479 (eshell-ls-annotate (if copy-fileinfo
483 (let ((file (car fileinfo
))
484 (attrs (cdr fileinfo
)))
485 (if (not (eq listing-style
'long-listing
))
487 (funcall insert-func
(eshell-ls-size-string attrs size-width
)
489 (funcall insert-func file
"\n"))
493 (concat (eshell-ls-size-string attrs size-width
) " "))
498 (or (nth 8 attrs
) "??????????")
500 (or (let ((user (nth 2 attrs
)))
502 (eshell-substring user
14)))
505 (or (let ((group (nth 3 attrs
)))
507 (eshell-substring group
8)))
510 (let* ((str (eshell-ls-printable-size (nth 7 attrs
)))
512 ;; Let file sizes shorter than 9 align neatly.
513 (if (< len
(or size-width
8))
514 (concat (make-string (- (or size-width
8) len
) ?
) str
)
516 " " (format-time-string
518 eshell-ls-date-format
" "
519 (if (= (nth 5 (decode-time (current-time)))
522 ((eq sort-method
'by-atime
) 4)
523 ((eq sort-method
'by-ctime
) 6)
527 ((eq sort-method
'by-atime
) 4)
528 ((eq sort-method
'by-ctime
) 6)
529 (t 5)) attrs
)) " ")))
530 (funcall insert-func line file
"\n"))))))
532 (defun eshell-ls-dir (dirinfo &optional insert-name root-dir size-width
)
533 "Output the entries in DIRINFO.
534 If INSERT-NAME is non-nil, the name of DIRINFO will be output. If
535 ROOT-DIR is also non-nil, and a directory name, DIRINFO will be output
536 relative to that directory."
537 (let ((dir (car dirinfo
)))
538 (if (not (cdr dirinfo
))
539 (funcall error-func
(format "%s: No such file or directory\n" dir
))
541 (eshell-ls-file dirinfo size-width
)
544 (eshell-ls-decorated-name
547 (file-relative-name dir root-dir
)
548 (expand-file-name dir
)))
549 (cdr dirinfo
))) ":\n"))
550 (let ((entries (eshell-directory-files-and-attributes
551 dir nil
(and (not show-all
)
552 eshell-ls-exclude-hidden
554 ;; Asking for UID and GID as
555 ;; strings saves another syscall
556 ;; later when we are going to
557 ;; display user and group names.
558 (if numeric-uid-gid
'integer
'string
))))
559 (when (and (not show-all
) eshell-ls-exclude-regexp
)
560 (while (and entries
(string-match eshell-ls-exclude-regexp
562 (setq entries
(cdr entries
)))
565 (if (string-match eshell-ls-exclude-regexp
(car (cadr e
)))
568 (when (or (eq listing-style
'long-listing
) show-size
)
573 (setq total
(+ total
(nth 7 (cdr e
)))
576 (length (eshell-ls-printable-size
579 ;; If we are under -l, count length
580 ;; of sizes in bytes, not in blocks.
581 (eq listing-style
'long-listing
))))))))
582 (funcall insert-func
"total "
583 (eshell-ls-printable-size total t
) "\n")))
584 (let ((default-directory (expand-file-name dir
)))
587 (let ((e entries
) (good-entries (list t
)))
589 (unless (let ((len (length (caar e
))))
590 (and (eq (aref (caar e
) 0) ?.
)
593 (eq (aref (caar e
) 1) ?.
)))))
594 (nconc good-entries
(list (car e
))))
598 (eshell-ls-files (eshell-ls-sort-entries entries
)
601 (defsubst eshell-ls-compare-entries
(l r inx func
)
602 "Compare the time of two files, L and R, the attribute indexed by INX."
603 (let ((lt (nth inx
(cdr l
)))
604 (rt (nth inx
(cdr r
))))
606 (string-lessp (directory-file-name (car l
))
607 (directory-file-name (car r
)))
608 (funcall func rt lt
))))
610 (defun eshell-ls-sort-entries (entries)
611 "Sort the given ENTRIES, which may be files, directories or both.
612 In Eshell's implementation of ls, ENTRIES is always reversed."
613 (if (eq sort-method
'unsorted
)
620 ((eq sort-method
'by-atime
)
621 (eshell-ls-compare-entries l r
4 'time-less-p
))
622 ((eq sort-method
'by-mtime
)
623 (eshell-ls-compare-entries l r
5 'time-less-p
))
624 ((eq sort-method
'by-ctime
)
625 (eshell-ls-compare-entries l r
6 'time-less-p
))
626 ((eq sort-method
'by-size
)
627 (eshell-ls-compare-entries l r
7 '<))
628 ((eq sort-method
'by-extension
)
629 (let ((lx (file-name-extension
630 (directory-file-name (car l
))))
631 (rx (file-name-extension
632 (directory-file-name (car r
)))))
634 ((or (and (not lx
) (not rx
))
636 (string-lessp (directory-file-name (car l
))
637 (directory-file-name (car r
))))
641 (string-lessp lx rx
)))))
643 (string-lessp (directory-file-name (car l
))
644 (directory-file-name (car r
)))))))
649 (defun eshell-ls-files (files &optional size-width copy-fileinfo
)
650 "Output a list of FILES.
651 Each member of FILES is either a string or a cons cell of the form
653 ;; Mimic behavior of coreutils ls, which lists a single file per
654 ;; line when output is not a tty. Exceptions: if -x was supplied,
655 ;; or if we are the _last_ command in a pipeline.
656 ;; FIXME Not really the same since not testing output destination.
657 (if (or (and eshell-in-pipeline-p
658 (not (eq eshell-in-pipeline-p
'last
))
659 (not (eq listing-style
'by-lines
)))
660 (memq listing-style
'(long-listing single-column
)))
663 (eshell-ls-file file size-width copy-fileinfo
)))
674 (format "%s: No such file or directory\n" (caar f
))))
676 (setq files
(cdr files
)
683 (setcdr f
(cddr f
))))))
685 (setq display-files
(mapcar 'eshell-ls-annotate files
))
687 (let* ((str (eshell-ls-printable-size (nth 7 (cdr file
)) t
))
689 (if (< len size-width
)
690 (setq str
(concat (make-string (- size-width len
) ?
) str
)))
691 (setq file
(eshell-ls-annotate file
)
692 display-files
(cons (cons (concat str
" " (car file
))
695 (setq display-files
(nreverse display-files
)))
697 (if (eq listing-style
'by-columns
)
698 (eshell-ls-find-column-lengths display-files
)
699 (assert (eq listing-style
'by-lines
))
700 (eshell-ls-find-column-widths display-files
)))
701 (col-widths (car col-vals
))
702 (display-files (cdr col-vals
))
703 (columns (length col-widths
))
706 (dolist (file display-files
)
710 (concat (substring (car file
) 0 size-width
)
711 (eshell-ls-decorated-name
712 (cons (substring (car file
) size-width
)
714 (eshell-ls-decorated-name file
))
716 (if (< col-index columns
)
718 (concat need-return name
720 (max 0 (- (aref col-widths
723 col-index
(1+ col-index
))
724 (funcall insert-func need-return name
"\n")
725 (setq col-index
1 need-return nil
))))
727 (funcall insert-func need-return
"\n"))))))
729 (defun eshell-ls-entries (entries &optional separate root-dir
)
730 "Output PATH's directory ENTRIES.
731 Each member of ENTRIES may either be a string or a cons cell, the car
732 of which is the file name, and the cdr of which is the list of
734 If SEPARATE is non-nil, directories name will be entirely separated
735 from the filenames. This is the normal behavior, except when doing a
737 ROOT-DIR, if non-nil, specifies the root directory of the listing, to
738 which non-absolute directory names will be made relative if ever they
740 (let (dirs files show-names need-return
(size-width 0))
741 (dolist (entry entries
)
742 (if (and (not dir-literal
)
743 (or (eshell-ls-filetype-p (cdr entry
) ?d
)
744 (and (eshell-ls-filetype-p (cdr entry
) ?l
)
745 (file-directory-p (car entry
)))))
748 (setq files
(cons entry files
)
752 (length (eshell-ls-printable-size
753 (nth 7 (cdr entry
)) t
))))))
754 (setq dirs
(cons entry dirs
)))
755 (setq files
(cons entry files
)
759 (length (eshell-ls-printable-size
760 (nth 7 (cdr entry
)) t
)))))))
762 (eshell-ls-files (eshell-ls-sort-entries files
)
763 size-width show-recursive
)
764 (setq need-return t
))
765 (setq show-names
(or show-recursive
766 (> (+ (length files
) (length dirs
)) 1)))
767 (dolist (dir (eshell-ls-sort-entries dirs
))
768 (if (and need-return
(not dir-literal
))
769 (funcall insert-func
"\n"))
770 (eshell-ls-dir dir show-names
771 (unless (file-name-absolute-p (car dir
)) root-dir
)
773 (setq need-return t
))))
775 (defun eshell-ls-find-column-widths (files)
776 "Find the best fitting column widths for FILES.
777 It will be returned as a vector, whose length is the number of columns
778 to use, and each member of which is the width of that column
779 \(including spacing)."
786 (+ 2 (length (car file
)))))
788 ;; must account for the added space...
789 (max-width (+ (window-width) 2))
793 ;; determine the largest number of columns in the first row
795 (while (and w
(< width max-width
))
796 (setq width
(+ width
(car w
))
800 ;; refine it based on the following rows
803 (colw (make-vector numcols
0))
808 (aset colw i
(max (aref colw i
) (car w
)))
809 (setq w
(cdr w
) i
(1+ i
)))
812 (setq width
(+ width
(aref colw i
))
814 (if (and (< width max-width
)
815 (> width best-width
))
816 (setq col-widths colw
818 (setq numcols
(1- numcols
)))
820 (cons (or col-widths
(vector max-width
)) files
)))
822 (defun eshell-ls-find-column-lengths (files)
823 "Find the best fitting column lengths for FILES.
824 It will be returned as a vector, whose length is the number of columns
825 to use, and each member of which is the width of that column
826 \(including spacing)."
833 (+ 2 (length (car file
)))))
835 (max-width (+ (window-width) 2))
839 ;; refine it based on the following rows
841 (let* ((rows (ceiling (/ (length widths
)
844 (len (* rows numcols
))
848 (unless (or (= rows
0)
849 (<= (/ (length widths
) (float rows
))
850 (float (1- numcols
))))
851 (setq colw
(make-vector numcols
0))
854 (setq i
0 index
(1+ index
)))
857 (or (nth (+ (* i rows
) index
) w
) 0)))
858 (setq len
(1- len
) i
(1+ i
)))
861 (setq width
(+ width
(aref colw i
))
863 (if (>= width max-width
)
866 (setq col-widths colw
))
867 (if (>= numcols
(length widths
))
869 (setq numcols
(1+ numcols
))))))
872 (cons (vector max-width
) files
)
873 (setq numcols
(length col-widths
))
874 (let* ((rows (ceiling (/ (length widths
)
876 (len (* rows numcols
))
877 (newfiles (make-list len nil
))
883 (setq i
0 index
(1+ index
)))
884 (setcar (nthcdr j newfiles
)
885 (nth (+ (* i rows
) index
) files
))
886 (setq j
(1+ j
) i
(1+ i
)))
887 (cons col-widths newfiles
)))))
889 (defun eshell-ls-decorated-name (file)
890 "Return FILE, possibly decorated."
891 (if eshell-ls-use-colors
897 ((stringp (cadr file
))
901 'eshell-ls-directory
)
903 ((not (eshell-ls-filetype-p (cdr file
) ?-
))
906 ((and (/= (user-uid) 0) ; root can execute anything
907 (eshell-ls-applicable (cdr file
) 3
908 'file-executable-p
(car file
)))
909 'eshell-ls-executable
)
911 ((not (eshell-ls-applicable (cdr file
) 1
912 'file-readable-p
(car file
)))
913 'eshell-ls-unreadable
)
915 ((string-match eshell-ls-archive-regexp
(car file
))
918 ((string-match eshell-ls-backup-regexp
(car file
))
921 ((string-match eshell-ls-product-regexp
(car file
))
924 ((string-match eshell-ls-clutter-regexp
(car file
))
927 ((not (eshell-ls-applicable (cdr file
) 2
928 'file-writable-p
(car file
)))
930 (eshell-ls-highlight-alist
931 (let ((tests eshell-ls-highlight-alist
)
934 (if (funcall (caar tests
) (car file
) (cdr file
))
935 (setq value
(cdar tests
) tests nil
)
936 (setq tests
(cdr tests
))))
939 (add-text-properties 0 (length (car file
))
947 ;; generated-autoload-file: "esh-groups.el"
950 ;;; em-ls.el ends here