1 ;;; em-ls.el --- implementation of ls in Lisp -*- lexical-binding:t -*-
3 ;; Copyright (C) 1999-2013 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]).
32 (eval-when-compile (require 'eshell
))
36 (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
53 (lambda () (fset 'insert-directory eshell-ls-orig-insert-directory
)))
54 "When unloading `eshell-ls', restore the definition of `insert-directory'."
58 (defcustom eshell-ls-date-format
"%Y-%m-%d"
59 "How to display time information in `eshell-ls-file'.
60 This is passed to `format-time-string' as a format string.
61 To display the date using the current locale, use \"%b \%e\"."
66 (defcustom eshell-ls-initial-args nil
67 "If non-nil, this list of args is included before any call to `ls'.
68 This is useful for enabling human-readable format (-h), for example."
69 :type
'(repeat :tag
"Arguments" string
)
72 (defcustom eshell-ls-dired-initial-args nil
73 "If non-nil, args is included before any call to `ls' in Dired.
74 This is useful for enabling human-readable format (-h), for example."
75 :type
'(repeat :tag
"Arguments" string
)
78 ;; FIXME should use advice, like ls-lisp.el does now.
79 (defcustom eshell-ls-use-in-dired nil
80 "If non-nil, use `eshell-ls' to read directories in Dired.
81 Changing this without using customize has no effect."
82 :set
(lambda (symbol value
)
84 (or (bound-and-true-p eshell-ls-use-in-dired
)
85 (fset 'insert-directory
'eshell-ls-insert-directory
))
86 (and (fboundp 'eshell-ls-insert-directory
) eshell-ls-use-in-dired
87 (fset 'insert-directory eshell-ls-orig-insert-directory
)))
93 (defcustom eshell-ls-default-blocksize
1024
94 "The default blocksize to use when display file sizes with -s."
98 (defcustom eshell-ls-exclude-regexp nil
99 "Unless -a is specified, files matching this regexp will not be shown."
100 :type
'(choice regexp
(const nil
))
103 (defcustom eshell-ls-exclude-hidden t
104 "Unless -a is specified, files beginning with . will not be shown.
105 Using this boolean, instead of `eshell-ls-exclude-regexp', is both
106 faster and conserves more memory."
110 (defcustom eshell-ls-use-colors t
111 "If non-nil, use colors in file listings."
115 (defface eshell-ls-directory
116 '((((class color
) (background light
)) (:foreground
"Blue" :weight bold
))
117 (((class color
) (background dark
)) (:foreground
"SkyBlue" :weight bold
))
119 "The face used for highlight directories."
121 (define-obsolete-face-alias 'eshell-ls-directory-face
122 'eshell-ls-directory
"22.1")
124 (defface eshell-ls-symlink
125 '((((class color
) (background light
)) (:foreground
"Dark Cyan" :weight bold
))
126 (((class color
) (background dark
)) (:foreground
"Cyan" :weight bold
)))
127 "The face used for highlight symbolic links."
129 (define-obsolete-face-alias 'eshell-ls-symlink-face
'eshell-ls-symlink
"22.1")
131 (defface eshell-ls-executable
132 '((((class color
) (background light
)) (:foreground
"ForestGreen" :weight bold
))
133 (((class color
) (background dark
)) (:foreground
"Green" :weight bold
)))
134 "The face used for highlighting executables (not directories, though)."
136 (define-obsolete-face-alias 'eshell-ls-executable-face
137 'eshell-ls-executable
"22.1")
139 (defface eshell-ls-readonly
140 '((((class color
) (background light
)) (:foreground
"Brown"))
141 (((class color
) (background dark
)) (:foreground
"Pink")))
142 "The face used for highlighting read-only files."
144 (define-obsolete-face-alias 'eshell-ls-readonly-face
'eshell-ls-readonly
"22.1")
146 (defface eshell-ls-unreadable
147 '((((class color
) (background light
)) (:foreground
"Grey30"))
148 (((class color
) (background dark
)) (:foreground
"DarkGrey")))
149 "The face used for highlighting unreadable files."
151 (define-obsolete-face-alias 'eshell-ls-unreadable-face
152 'eshell-ls-unreadable
"22.1")
154 (defface eshell-ls-special
155 '((((class color
) (background light
)) (:foreground
"Magenta" :weight bold
))
156 (((class color
) (background dark
)) (:foreground
"Magenta" :weight bold
)))
157 "The face used for highlighting non-regular files."
159 (define-obsolete-face-alias 'eshell-ls-special-face
'eshell-ls-special
"22.1")
161 (defface eshell-ls-missing
162 '((((class color
) (background light
)) (:foreground
"Red" :weight bold
))
163 (((class color
) (background dark
)) (:foreground
"Red" :weight bold
)))
164 "The face used for highlighting non-existent file names."
166 (define-obsolete-face-alias 'eshell-ls-missing-face
'eshell-ls-missing
"22.1")
168 (defcustom eshell-ls-archive-regexp
169 (concat "\\.\\(t\\(a[rz]\\|gz\\)\\|arj\\|lzh\\|"
170 "zip\\|[zZ]\\|gz\\|bz2\\|xz\\|deb\\|rpm\\)\\'")
171 "A regular expression that matches names of file archives.
172 This typically includes both traditional archives and compressed
174 :version
"24.1" ; added xz
178 (defface eshell-ls-archive
179 '((((class color
) (background light
)) (:foreground
"Orchid" :weight bold
))
180 (((class color
) (background dark
)) (:foreground
"Orchid" :weight bold
)))
181 "The face used for highlighting archived and compressed file names."
183 (define-obsolete-face-alias 'eshell-ls-archive-face
'eshell-ls-archive
"22.1")
185 (defcustom eshell-ls-backup-regexp
186 "\\(\\`\\.?#\\|\\(\\.bak\\|~\\)\\'\\)"
187 "A regular expression that matches names of backup files."
191 (defface eshell-ls-backup
192 '((((class color
) (background light
)) (:foreground
"OrangeRed"))
193 (((class color
) (background dark
)) (:foreground
"LightSalmon")))
194 "The face used for highlighting backup file names."
196 (define-obsolete-face-alias 'eshell-ls-backup-face
'eshell-ls-backup
"22.1")
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."
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."
211 (define-obsolete-face-alias 'eshell-ls-product-face
'eshell-ls-product
"22.1")
213 (defcustom eshell-ls-clutter-regexp
214 "\\(^texput\\.log\\|^core\\)\\'"
215 "A regular expression that matches names of junk files.
216 These are mainly files that get created for various reasons, but don't
217 really need to stick around for very long."
221 (defface eshell-ls-clutter
222 '((((class color
) (background light
)) (:foreground
"OrangeRed" :weight bold
))
223 (((class color
) (background dark
)) (:foreground
"OrangeRed" :weight bold
)))
224 "The face used for highlighting junk file names."
226 (define-obsolete-face-alias 'eshell-ls-clutter-face
'eshell-ls-clutter
"22.1")
228 (defsubst eshell-ls-filetype-p
(attrs type
)
229 "Test whether ATTRS specifies a directory."
231 (eq (aref (nth 8 attrs
) 0) type
)))
233 (defmacro eshell-ls-applicable
(attrs index func file
)
234 "Test whether, for ATTRS, the user can do what corresponds to INDEX.
235 ATTRS is a string of file modes. See `file-attributes'.
236 If we cannot determine the answer using ATTRS (e.g., if we need
237 to know what group the user is in), compute the return value by
238 calling FUNC with FILE as an argument."
239 `(let ((owner (nth 2 ,attrs
))
240 (modes (nth 8 ,attrs
)))
241 (cond ((cond ((numberp owner
)
242 (= owner
(user-uid)))
244 (or (string-equal owner
(user-login-name))
245 (member owner
(eshell-current-ange-uids)))))
246 ;; The user owns this file.
247 (not (eq (aref modes
,index
) ?-
)))
248 ((eq (aref modes
(+ ,index
3))
249 (aref modes
(+ ,index
6)))
250 ;; If the "group" and "other" fields give identical
251 ;; results, use that.
252 (not (eq (aref modes
(+ ,index
3)) ?-
)))
254 ;; Otherwise call FUNC.
255 (,(eval func
) ,file
)))))
257 (defcustom eshell-ls-highlight-alist nil
258 "This alist correlates test functions to color.
259 The format of the members of this alist is
263 If TEST-SEXP evals to non-nil, that face will be used to highlight the
264 name of the file. The first match wins. `file' and `attrs' are in
265 scope during the evaluation of TEST-SEXP."
266 :type
'(repeat (cons function face
))
270 (defvar dereference-links
)
274 (defvar human-readable
)
275 (defvar ignore-pattern
)
277 (defvar listing-style
)
278 (defvar numeric-uid-gid
)
279 (defvar reverse-list
)
281 (defvar show-almost-all
)
282 (defvar show-recursive
)
290 (defun eshell-ls-insert-directory
291 (file switches
&optional wildcard full-directory-p
)
292 "Insert directory listing for FILE, formatted according to SWITCHES.
293 Leaves point after the inserted text.
294 SWITCHES may be a string of options, or a list of strings.
295 Optional third arg WILDCARD means treat FILE as shell wildcard.
296 Optional fourth arg FULL-DIRECTORY-P means file is a directory and
297 switches do not contain `d', so that a full listing is expected.
299 This version of the function uses `eshell/ls'. If any of the switches
300 passed are not recognized, the operating system's version will be used
302 (let ((handler (find-file-name-handler file
'insert-directory
)))
304 (funcall handler
'insert-directory file switches
305 wildcard full-directory-p
)
306 (if (stringp switches
)
307 (setq switches
(split-string switches
)))
308 (let (eshell-current-handles
309 eshell-current-subjob-p
311 ;; use the fancy highlighting in `eshell-ls' rather than font-lock
312 (when (and eshell-ls-use-colors
313 (featurep 'font-lock
))
315 (setq font-lock-defaults nil
)
316 (if (boundp 'font-lock-buffers
)
317 (set 'font-lock-buffers
318 (delq (current-buffer)
319 (symbol-value 'font-lock-buffers
)))))
320 (let ((insert-func 'insert
)
323 eshell-ls-dired-initial-args
)
324 (eshell-do-ls (append switches
(list file
))))))))
326 (defsubst eshell
/ls
(&rest args
)
327 "An alias version of `eshell-do-ls'."
328 (let ((insert-func 'eshell-buffered-print
)
329 (error-func 'eshell-error
)
330 (flush-func 'eshell-flush
))
331 (apply 'eshell-do-ls args
)))
333 (put 'eshell
/ls
'eshell-no-numeric-conversions t
)
335 (declare-function eshell-glob-regexp
"em-glob" (pattern))
337 (defun eshell-do-ls (&rest args
)
338 "Implementation of \"ls\" in Lisp, passing ARGS."
339 (funcall flush-func -
1)
340 ;; Process the command arguments, and begin listing files.
341 (eshell-eval-using-options
342 "ls" (if eshell-ls-initial-args
343 (list eshell-ls-initial-args args
)
345 `((?a
"all" nil show-all
346 "do not ignore entries starting with .")
347 (?A
"almost-all" nil show-almost-all
348 "do not list implied . and ..")
349 (?c nil by-ctime sort-method
350 "sort by last status change time")
351 (?d
"directory" nil dir-literal
352 "list directory entries instead of contents")
353 (?k
"kilobytes" 1024 block-size
354 "using 1024 as the block size")
355 (?h
"human-readable" 1024 human-readable
356 "print sizes in human readable format")
357 (?H
"si" 1000 human-readable
358 "likewise, but use powers of 1000 not 1024")
359 (?I
"ignore" t ignore-pattern
360 "do not list implied entries matching pattern")
361 (?l nil long-listing listing-style
362 "use a long listing format")
363 (?n
"numeric-uid-gid" nil numeric-uid-gid
364 "list numeric UIDs and GIDs instead of names")
365 (?r
"reverse" nil reverse-list
366 "reverse order while sorting")
367 (?s
"size" nil show-size
368 "print size of each file, in blocks")
369 (?t nil by-mtime sort-method
370 "sort by modification time")
371 (?u nil by-atime sort-method
372 "sort by last access time")
373 (?x nil by-lines listing-style
374 "list entries by lines instead of by columns")
375 (?C nil by-columns listing-style
376 "list entries by columns")
377 (?L
"dereference" nil dereference-links
378 "list entries pointed to by symbolic links")
379 (?R
"recursive" nil show-recursive
380 "list subdirectories recursively")
381 (?S nil by-size sort-method
383 (?U nil unsorted sort-method
384 "do not sort; list entries in directory order")
385 (?X nil by-extension sort-method
386 "sort alphabetically by entry extension")
387 (?
1 nil single-column listing-style
388 "list one file per line")
389 (nil "dired" nil dired-flag
390 "Here for compatibility with GNU ls.")
392 "show this usage display")
394 :usage
"[OPTION]... [FILE]...
395 List information about the FILEs (the current directory by default).
396 Sort entries alphabetically across.")
397 ;; setup some defaults, based on what the user selected
399 (setq block-size eshell-ls-default-blocksize
))
400 (unless listing-style
401 (setq listing-style
'by-columns
))
403 (setq args
(list ".")))
404 (let ((eshell-ls-exclude-regexp eshell-ls-exclude-regexp
) ange-cache
)
406 (unless (eshell-using-module 'eshell-glob
)
407 (error (concat "-I option requires that `eshell-glob'"
408 " be a member of `eshell-modules-list'")))
409 (set-text-properties 0 (length ignore-pattern
) nil ignore-pattern
)
410 (setq eshell-ls-exclude-regexp
411 (if eshell-ls-exclude-regexp
412 (concat "\\(" eshell-ls-exclude-regexp
"\\|"
413 (eshell-glob-regexp ignore-pattern
) "\\)")
414 (eshell-glob-regexp ignore-pattern
))))
417 (mapcar (lambda (arg)
418 (cons (if (and (eshell-under-windows-p)
419 (file-name-absolute-p arg
))
420 (expand-file-name arg
)
422 (eshell-file-attributes
423 arg
(if numeric-uid-gid
'integer
'string
))))
425 t
(expand-file-name default-directory
)))
426 (funcall flush-func
)))
428 (defsubst eshell-ls-printable-size
(filesize &optional by-blocksize
)
429 "Return a printable FILESIZE."
430 (eshell-printable-size filesize human-readable
431 (and by-blocksize block-size
)
432 eshell-ls-use-colors
))
434 (defsubst eshell-ls-size-string
(attrs size-width
)
435 "Return the size string for ATTRS length, using SIZE-WIDTH."
436 (let* ((str (eshell-ls-printable-size (nth 7 attrs
) t
))
438 (if (< len size-width
)
439 (concat (make-string (- size-width len
) ?
) str
)
442 (defun eshell-ls-annotate (fileinfo)
443 "Given a FILEINFO object, return a resolved, decorated FILEINFO.
444 This means resolving any symbolic links, determining what face the
445 name should be displayed as, etc. Think of it as cooking a FILEINFO."
446 (if (not (and (stringp (cadr fileinfo
))
447 (or dereference-links
448 (eq listing-style
'long-listing
))))
449 (setcar fileinfo
(eshell-ls-decorated-name fileinfo
))
451 (unless (file-name-absolute-p (cadr fileinfo
))
452 (setq dir
(file-truename
454 (expand-file-name (car fileinfo
))))))
456 (eshell-file-attributes
457 (let ((target (if dir
458 (expand-file-name (cadr fileinfo
) dir
)
460 (if dereference-links
461 (file-truename target
)
463 (if (or dereference-links
464 (string-match "^\\.\\.?$" (car fileinfo
)))
466 (setcdr fileinfo attr
)
467 (setcar fileinfo
(eshell-ls-decorated-name fileinfo
)))
468 (cl-assert (eq listing-style
'long-listing
))
470 (concat (eshell-ls-decorated-name fileinfo
) " -> "
471 (eshell-ls-decorated-name
472 (cons (cadr fileinfo
) attr
)))))))
475 (defun eshell-ls-file (fileinfo &optional size-width copy-fileinfo
)
476 "Output FILE in long format.
477 FILE may be a string, or a cons cell whose car is the filename and
478 whose cdr is the list of file attributes."
479 (if (not (cdr fileinfo
))
480 (funcall error-func
(format "%s: No such file or directory\n"
483 (eshell-ls-annotate (if copy-fileinfo
487 (let ((file (car fileinfo
))
488 (attrs (cdr fileinfo
)))
489 (if (not (eq listing-style
'long-listing
))
491 (funcall insert-func
(eshell-ls-size-string attrs size-width
)
493 (funcall insert-func file
"\n"))
497 (concat (eshell-ls-size-string attrs size-width
) " "))
502 (or (nth 8 attrs
) "??????????")
504 (or (let ((user (nth 2 attrs
)))
506 (eshell-substring user
14)))
509 (or (let ((group (nth 3 attrs
)))
511 (eshell-substring group
8)))
514 (let* ((str (eshell-ls-printable-size (nth 7 attrs
)))
516 ;; Let file sizes shorter than 9 align neatly.
517 (if (< len
(or size-width
8))
518 (concat (make-string (- (or size-width
8) len
) ?
) str
)
520 " " (format-time-string
522 eshell-ls-date-format
" "
523 (if (= (nth 5 (decode-time (current-time)))
526 ((eq sort-method
'by-atime
) 4)
527 ((eq sort-method
'by-ctime
) 6)
531 ((eq sort-method
'by-atime
) 4)
532 ((eq sort-method
'by-ctime
) 6)
533 (t 5)) attrs
)) " ")))
534 (funcall insert-func line file
"\n"))))))
536 (defun eshell-ls-dir (dirinfo &optional insert-name root-dir size-width
)
537 "Output the entries in DIRINFO.
538 If INSERT-NAME is non-nil, the name of DIRINFO will be output. If
539 ROOT-DIR is also non-nil, and a directory name, DIRINFO will be output
540 relative to that directory."
541 (let ((dir (car dirinfo
)))
542 (if (not (cdr dirinfo
))
543 (funcall error-func
(format "%s: No such file or directory\n" dir
))
545 (eshell-ls-file dirinfo size-width
)
548 (eshell-ls-decorated-name
551 (file-relative-name dir root-dir
)
552 (expand-file-name dir
)))
553 (cdr dirinfo
))) ":\n"))
554 (let ((entries (eshell-directory-files-and-attributes
555 dir nil
(and (not (or show-all show-almost-all
))
556 eshell-ls-exclude-hidden
558 ;; Asking for UID and GID as
559 ;; strings saves another syscall
560 ;; later when we are going to
561 ;; display user and group names.
562 (if numeric-uid-gid
'integer
'string
))))
563 (when (and show-almost-all
568 (member (car entry
) '("." "..")))
570 (when (and (not (or show-all show-almost-all
))
571 eshell-ls-exclude-regexp
)
572 (while (and entries
(string-match eshell-ls-exclude-regexp
574 (setq entries
(cdr entries
)))
577 (if (string-match eshell-ls-exclude-regexp
(car (cadr e
)))
580 (when (or (eq listing-style
'long-listing
) show-size
)
585 (setq total
(+ total
(nth 7 (cdr e
)))
588 (length (eshell-ls-printable-size
591 ;; If we are under -l, count length
592 ;; of sizes in bytes, not in blocks.
593 (eq listing-style
'long-listing
))))))))
594 (funcall insert-func
"total "
595 (eshell-ls-printable-size total t
) "\n")))
596 (let ((default-directory (expand-file-name dir
)))
599 (let ((e entries
) (good-entries (list t
)))
601 (unless (let ((len (length (caar e
))))
602 (and (eq (aref (caar e
) 0) ?.
)
605 (eq (aref (caar e
) 1) ?.
)))))
606 (nconc good-entries
(list (car e
))))
610 (eshell-ls-files (eshell-ls-sort-entries entries
)
613 (defsubst eshell-ls-compare-entries
(l r inx func
)
614 "Compare the time of two files, L and R, the attribute indexed by INX."
615 (let ((lt (nth inx
(cdr l
)))
616 (rt (nth inx
(cdr r
))))
618 (string-lessp (directory-file-name (car l
))
619 (directory-file-name (car r
)))
620 (funcall func rt lt
))))
622 (defun eshell-ls-sort-entries (entries)
623 "Sort the given ENTRIES, which may be files, directories or both.
624 In Eshell's implementation of ls, ENTRIES is always reversed."
625 (if (eq sort-method
'unsorted
)
632 ((eq sort-method
'by-atime
)
633 (eshell-ls-compare-entries l r
4 'time-less-p
))
634 ((eq sort-method
'by-mtime
)
635 (eshell-ls-compare-entries l r
5 'time-less-p
))
636 ((eq sort-method
'by-ctime
)
637 (eshell-ls-compare-entries l r
6 'time-less-p
))
638 ((eq sort-method
'by-size
)
639 (eshell-ls-compare-entries l r
7 '<))
640 ((eq sort-method
'by-extension
)
641 (let ((lx (file-name-extension
642 (directory-file-name (car l
))))
643 (rx (file-name-extension
644 (directory-file-name (car r
)))))
646 ((or (and (not lx
) (not rx
))
648 (string-lessp (directory-file-name (car l
))
649 (directory-file-name (car r
))))
653 (string-lessp lx rx
)))))
655 (string-lessp (directory-file-name (car l
))
656 (directory-file-name (car r
)))))))
661 (defun eshell-ls-files (files &optional size-width copy-fileinfo
)
662 "Output a list of FILES.
663 Each member of FILES is either a string or a cons cell of the form
665 ;; Mimic behavior of coreutils ls, which lists a single file per
666 ;; line when output is not a tty. Exceptions: if -x was supplied,
667 ;; or if we are the _last_ command in a pipeline.
668 ;; FIXME Not really the same since not testing output destination.
669 (if (or (and eshell-in-pipeline-p
670 (not (eq eshell-in-pipeline-p
'last
))
671 (not (eq listing-style
'by-lines
)))
672 (memq listing-style
'(long-listing single-column
)))
675 (eshell-ls-file file size-width copy-fileinfo
)))
686 (format "%s: No such file or directory\n" (caar f
))))
688 (setq files
(cdr files
)
695 (setcdr f
(cddr f
))))))
697 (setq display-files
(mapcar 'eshell-ls-annotate files
))
699 (let* ((str (eshell-ls-printable-size (nth 7 (cdr file
)) t
))
701 (if (< len size-width
)
702 (setq str
(concat (make-string (- size-width len
) ?
) str
)))
703 (setq file
(eshell-ls-annotate file
)
704 display-files
(cons (cons (concat str
" " (car file
))
707 (setq display-files
(nreverse display-files
)))
709 (if (eq listing-style
'by-columns
)
710 (eshell-ls-find-column-lengths display-files
)
711 (cl-assert (eq listing-style
'by-lines
))
712 (eshell-ls-find-column-widths display-files
)))
713 (col-widths (car col-vals
))
714 (display-files (cdr col-vals
))
715 (columns (length col-widths
))
718 (dolist (file display-files
)
722 (concat (substring (car file
) 0 size-width
)
723 (eshell-ls-decorated-name
724 (cons (substring (car file
) size-width
)
726 (eshell-ls-decorated-name file
))
728 (if (< col-index columns
)
730 (concat need-return name
732 (max 0 (- (aref col-widths
735 col-index
(1+ col-index
))
736 (funcall insert-func need-return name
"\n")
737 (setq col-index
1 need-return nil
))))
739 (funcall insert-func need-return
"\n"))))))
741 (defun eshell-ls-entries (entries &optional separate root-dir
)
742 "Output PATH's directory ENTRIES.
743 Each member of ENTRIES may either be a string or a cons cell, the car
744 of which is the file name, and the cdr of which is the list of
746 If SEPARATE is non-nil, directories name will be entirely separated
747 from the filenames. This is the normal behavior, except when doing a
749 ROOT-DIR, if non-nil, specifies the root directory of the listing, to
750 which non-absolute directory names will be made relative if ever they
752 (let (dirs files show-names need-return
(size-width 0))
753 (dolist (entry entries
)
754 (if (and (not dir-literal
)
755 (or (eshell-ls-filetype-p (cdr entry
) ?d
)
756 (and (eshell-ls-filetype-p (cdr entry
) ?l
)
757 (file-directory-p (car entry
)))))
760 (setq files
(cons entry files
)
764 (length (eshell-ls-printable-size
765 (nth 7 (cdr entry
)) t
))))))
766 (setq dirs
(cons entry dirs
)))
767 (setq files
(cons entry files
)
771 (length (eshell-ls-printable-size
772 (nth 7 (cdr entry
)) t
)))))))
774 (eshell-ls-files (eshell-ls-sort-entries files
)
775 size-width show-recursive
)
776 (setq need-return t
))
777 (setq show-names
(or show-recursive
778 (> (+ (length files
) (length dirs
)) 1)))
779 (dolist (dir (eshell-ls-sort-entries dirs
))
780 (if (and need-return
(not dir-literal
))
781 (funcall insert-func
"\n"))
782 (eshell-ls-dir dir show-names
783 (unless (file-name-absolute-p (car dir
)) root-dir
)
785 (setq need-return t
))))
787 (defun eshell-ls-find-column-widths (files)
788 "Find the best fitting column widths for FILES.
789 It will be returned as a vector, whose length is the number of columns
790 to use, and each member of which is the width of that column
791 \(including spacing)."
798 (+ 2 (length (car file
)))))
800 ;; must account for the added space...
801 (max-width (+ (window-width) 2))
805 ;; determine the largest number of columns in the first row
807 (while (and w
(< width max-width
))
808 (setq width
(+ width
(car w
))
812 ;; refine it based on the following rows
815 (colw (make-vector numcols
0))
820 (aset colw i
(max (aref colw i
) (car w
)))
821 (setq w
(cdr w
) i
(1+ i
)))
824 (setq width
(+ width
(aref colw i
))
826 (if (and (< width max-width
)
827 (> width best-width
))
828 (setq col-widths colw
830 (setq numcols
(1- numcols
)))
832 (cons (or col-widths
(vector max-width
)) files
)))
834 (defun eshell-ls-find-column-lengths (files)
835 "Find the best fitting column lengths for FILES.
836 It will be returned as a vector, whose length is the number of columns
837 to use, and each member of which is the width of that column
838 \(including spacing)."
845 (+ 2 (length (car file
)))))
847 (max-width (+ (window-width) 2))
851 ;; refine it based on the following rows
853 (let* ((rows (ceiling (/ (length widths
)
856 (len (* rows numcols
))
860 (unless (or (= rows
0)
861 (<= (/ (length widths
) (float rows
))
862 (float (1- numcols
))))
863 (setq colw
(make-vector numcols
0))
866 (setq i
0 index
(1+ index
)))
869 (or (nth (+ (* i rows
) index
) w
) 0)))
870 (setq len
(1- len
) i
(1+ i
)))
873 (setq width
(+ width
(aref colw i
))
875 (if (>= width max-width
)
878 (setq col-widths colw
))
879 (if (>= numcols
(length widths
))
881 (setq numcols
(1+ numcols
))))))
884 (cons (vector max-width
) files
)
885 (setq numcols
(length col-widths
))
886 (let* ((rows (ceiling (/ (length widths
)
888 (len (* rows numcols
))
889 (newfiles (make-list len nil
))
895 (setq i
0 index
(1+ index
)))
896 (setcar (nthcdr j newfiles
)
897 (nth (+ (* i rows
) index
) files
))
898 (setq j
(1+ j
) i
(1+ i
)))
899 (cons col-widths newfiles
)))))
901 (defun eshell-ls-decorated-name (file)
902 "Return FILE, possibly decorated."
903 (if eshell-ls-use-colors
909 ((stringp (cadr file
))
913 'eshell-ls-directory
)
915 ((not (eshell-ls-filetype-p (cdr file
) ?-
))
918 ((and (/= (user-uid) 0) ; root can execute anything
919 (eshell-ls-applicable (cdr file
) 3
920 'file-executable-p
(car file
)))
921 'eshell-ls-executable
)
923 ((not (eshell-ls-applicable (cdr file
) 1
924 'file-readable-p
(car file
)))
925 'eshell-ls-unreadable
)
927 ((string-match eshell-ls-archive-regexp
(car file
))
930 ((string-match eshell-ls-backup-regexp
(car file
))
933 ((string-match eshell-ls-product-regexp
(car file
))
936 ((string-match eshell-ls-clutter-regexp
(car file
))
939 ((not (eshell-ls-applicable (cdr file
) 2
940 'file-writable-p
(car file
)))
942 (eshell-ls-highlight-alist
943 (let ((tests eshell-ls-highlight-alist
)
946 (if (funcall (caar tests
) (car file
) (cdr file
))
947 (setq value
(cdar tests
) tests nil
)
948 (setq tests
(cdr tests
))))
951 (add-text-properties 0 (length (car file
))
959 ;; generated-autoload-file: "esh-groups.el"
962 ;;; em-ls.el ends here