1 ;;; em-ls.el --- implementation of ls in Lisp -*- lexical-binding:t -*-
3 ;; Copyright (C) 1999-2014 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 (defcustom eshell-ls-date-format
"%Y-%m-%d"
48 "How to display time information in `eshell-ls-file'.
49 This is passed to `format-time-string' as a format string.
50 To display the date using the current locale, use \"%b \%e\"."
54 (defcustom eshell-ls-initial-args nil
55 "If non-nil, this list of args is included before any call to `ls'.
56 This is useful for enabling human-readable format (-h), for example."
57 :type
'(repeat :tag
"Arguments" string
))
59 (defcustom eshell-ls-dired-initial-args nil
60 "If non-nil, args is included before any call to `ls' in Dired.
61 This is useful for enabling human-readable format (-h), for example."
62 :type
'(repeat :tag
"Arguments" string
))
64 (defcustom eshell-ls-use-in-dired nil
65 "If non-nil, use `eshell-ls' to read directories in Dired.
66 Changing this without using customize has no effect."
67 :set
(lambda (symbol value
)
69 (advice-add 'insert-directory
:around
70 #'eshell-ls--insert-directory
)
71 (advice-remove 'insert-directory
72 #'eshell-ls--insert-directory
))
76 (add-hook 'eshell-ls-unload-hook
77 (lambda () (advice-remove 'insert-directory
78 #'eshell-ls--insert-directory
)))
81 (defcustom eshell-ls-default-blocksize
1024
82 "The default blocksize to use when display file sizes with -s."
85 (defcustom eshell-ls-exclude-regexp nil
86 "Unless -a is specified, files matching this regexp will not be shown."
87 :type
'(choice regexp
(const nil
)))
89 (defcustom eshell-ls-exclude-hidden t
90 "Unless -a is specified, files beginning with . will not be shown.
91 Using this boolean, instead of `eshell-ls-exclude-regexp', is both
92 faster and conserves more memory."
95 (defcustom eshell-ls-use-colors t
96 "If non-nil, use colors in file listings."
99 (defface eshell-ls-directory
100 '((((class color
) (background light
)) (:foreground
"Blue" :weight bold
))
101 (((class color
) (background dark
)) (:foreground
"SkyBlue" :weight bold
))
103 "The face used for highlighting directories.")
104 (define-obsolete-face-alias 'eshell-ls-directory-face
105 'eshell-ls-directory
"22.1")
107 (defface eshell-ls-symlink
108 '((((class color
) (background light
)) (:foreground
"Dark Cyan" :weight bold
))
109 (((class color
) (background dark
)) (:foreground
"Cyan" :weight bold
)))
110 "The face used for highlighting symbolic links.")
111 (define-obsolete-face-alias 'eshell-ls-symlink-face
'eshell-ls-symlink
"22.1")
113 (defface eshell-ls-executable
114 '((((class color
) (background light
)) (:foreground
"ForestGreen" :weight bold
))
115 (((class color
) (background dark
)) (:foreground
"Green" :weight bold
)))
116 "The face used for highlighting executables (not directories, though).")
117 (define-obsolete-face-alias 'eshell-ls-executable-face
118 'eshell-ls-executable
"22.1")
120 (defface eshell-ls-readonly
121 '((((class color
) (background light
)) (:foreground
"Brown"))
122 (((class color
) (background dark
)) (:foreground
"Pink")))
123 "The face used for highlighting read-only files.")
124 (define-obsolete-face-alias 'eshell-ls-readonly-face
'eshell-ls-readonly
"22.1")
126 (defface eshell-ls-unreadable
127 '((((class color
) (background light
)) (:foreground
"Grey30"))
128 (((class color
) (background dark
)) (:foreground
"DarkGrey")))
129 "The face used for highlighting unreadable files.")
130 (define-obsolete-face-alias 'eshell-ls-unreadable-face
131 'eshell-ls-unreadable
"22.1")
133 (defface eshell-ls-special
134 '((((class color
) (background light
)) (:foreground
"Magenta" :weight bold
))
135 (((class color
) (background dark
)) (:foreground
"Magenta" :weight bold
)))
136 "The face used for highlighting non-regular files.")
137 (define-obsolete-face-alias 'eshell-ls-special-face
'eshell-ls-special
"22.1")
139 (defface eshell-ls-missing
140 '((((class color
) (background light
)) (:foreground
"Red" :weight bold
))
141 (((class color
) (background dark
)) (:foreground
"Red" :weight bold
)))
142 "The face used for highlighting non-existent file names.")
143 (define-obsolete-face-alias 'eshell-ls-missing-face
'eshell-ls-missing
"22.1")
145 (defcustom eshell-ls-archive-regexp
146 (concat "\\.\\(t\\(a[rz]\\|gz\\)\\|arj\\|lzh\\|"
147 "zip\\|[zZ]\\|gz\\|bz2\\|xz\\|deb\\|rpm\\)\\'")
148 "A regular expression that matches names of file archives.
149 This typically includes both traditional archives and compressed
151 :version
"24.1" ; added xz
154 (defface eshell-ls-archive
155 '((((class color
) (background light
)) (:foreground
"Orchid" :weight bold
))
156 (((class color
) (background dark
)) (:foreground
"Orchid" :weight bold
)))
157 "The face used for highlighting archived and compressed file names.")
158 (define-obsolete-face-alias 'eshell-ls-archive-face
'eshell-ls-archive
"22.1")
160 (defcustom eshell-ls-backup-regexp
161 "\\(\\`\\.?#\\|\\(\\.bak\\|~\\)\\'\\)"
162 "A regular expression that matches names of backup files."
165 (defface eshell-ls-backup
166 '((((class color
) (background light
)) (:foreground
"OrangeRed"))
167 (((class color
) (background dark
)) (:foreground
"LightSalmon")))
168 "The face used for highlighting backup file names.")
169 (define-obsolete-face-alias 'eshell-ls-backup-face
'eshell-ls-backup
"22.1")
171 (defcustom eshell-ls-product-regexp
172 "\\.\\(elc\\|o\\(bj\\)?\\|a\\|lib\\|res\\)\\'"
173 "A regular expression that matches names of product files.
174 Products are files that get generated from a source file, and hence
175 ought to be recreatable if they are deleted."
178 (defface eshell-ls-product
179 '((((class color
) (background light
)) (:foreground
"OrangeRed"))
180 (((class color
) (background dark
)) (:foreground
"LightSalmon")))
181 "The face used for highlighting files that are build products.")
182 (define-obsolete-face-alias 'eshell-ls-product-face
'eshell-ls-product
"22.1")
184 (defcustom eshell-ls-clutter-regexp
185 "\\(^texput\\.log\\|^core\\)\\'"
186 "A regular expression that matches names of junk files.
187 These are mainly files that get created for various reasons, but don't
188 really need to stick around for very long."
191 (defface eshell-ls-clutter
192 '((((class color
) (background light
)) (:foreground
"OrangeRed" :weight bold
))
193 (((class color
) (background dark
)) (:foreground
"OrangeRed" :weight bold
)))
194 "The face used for highlighting junk file names.")
195 (define-obsolete-face-alias 'eshell-ls-clutter-face
'eshell-ls-clutter
"22.1")
197 (defsubst eshell-ls-filetype-p
(attrs type
)
198 "Test whether ATTRS specifies a directory."
200 (eq (aref (nth 8 attrs
) 0) type
)))
202 (defmacro eshell-ls-applicable
(attrs index func file
)
203 "Test whether, for ATTRS, the user can do what corresponds to INDEX.
204 ATTRS is a string of file modes. See `file-attributes'.
205 If we cannot determine the answer using ATTRS (e.g., if we need
206 to know what group the user is in), compute the return value by
207 calling FUNC with FILE as an argument."
208 `(let ((owner (nth 2 ,attrs
))
209 (modes (nth 8 ,attrs
)))
210 (cond ((cond ((numberp owner
)
211 (= owner
(user-uid)))
213 (or (string-equal owner
(user-login-name))
214 (member owner
(eshell-current-ange-uids)))))
215 ;; The user owns this file.
216 (not (eq (aref modes
,index
) ?-
)))
217 ((eq (aref modes
(+ ,index
3))
218 (aref modes
(+ ,index
6)))
219 ;; If the "group" and "other" fields give identical
220 ;; results, use that.
221 (not (eq (aref modes
(+ ,index
3)) ?-
)))
223 ;; Otherwise call FUNC.
224 (,(eval func
) ,file
)))))
226 (defcustom eshell-ls-highlight-alist nil
227 "This alist correlates test functions to color.
228 The format of the members of this alist is
232 If TEST-SEXP evals to non-nil, that face will be used to highlight the
233 name of the file. The first match wins. `file' and `attrs' are in
234 scope during the evaluation of TEST-SEXP."
235 :type
'(repeat (cons function face
)))
238 (defvar dereference-links
)
242 (defvar human-readable
)
243 (defvar ignore-pattern
)
245 (defvar listing-style
)
246 (defvar numeric-uid-gid
)
247 (defvar reverse-list
)
249 (defvar show-almost-all
)
250 (defvar show-recursive
)
258 (defun eshell-ls--insert-directory
259 (orig-fun file switches
&optional wildcard full-directory-p
)
260 "Insert directory listing for FILE, formatted according to SWITCHES.
261 Leaves point after the inserted text.
262 SWITCHES may be a string of options, or a list of strings.
263 Optional third arg WILDCARD means treat FILE as shell wildcard.
264 Optional fourth arg FULL-DIRECTORY-P means file is a directory and
265 switches do not contain `d', so that a full listing is expected.
267 This version of the function uses `eshell/ls'. If any of the switches
268 passed are not recognized, the operating system's version will be used
270 (if (not eshell-ls-use-in-dired
)
271 (funcall orig-fun file switches wildcard full-directory-p
)
272 (let ((handler (find-file-name-handler file
'insert-directory
)))
274 (funcall handler
'insert-directory file switches
275 wildcard full-directory-p
)
276 (if (stringp switches
)
277 (setq switches
(split-string switches
)))
278 (let (eshell-current-handles
279 eshell-current-subjob-p
281 ;; use the fancy highlighting in `eshell-ls' rather than font-lock
282 (when (and eshell-ls-use-colors
283 (featurep 'font-lock
))
285 (setq font-lock-defaults nil
)
286 (if (boundp 'font-lock-buffers
)
287 (set 'font-lock-buffers
288 (delq (current-buffer)
289 (symbol-value 'font-lock-buffers
)))))
290 (let ((insert-func 'insert
)
293 eshell-ls-dired-initial-args
)
294 (eshell-do-ls (append switches
(list file
)))))))))
296 (defsubst eshell
/ls
(&rest args
)
297 "An alias version of `eshell-do-ls'."
298 (let ((insert-func 'eshell-buffered-print
)
299 (error-func 'eshell-error
)
300 (flush-func 'eshell-flush
))
301 (apply 'eshell-do-ls args
)))
303 (put 'eshell
/ls
'eshell-no-numeric-conversions t
)
305 (declare-function eshell-glob-regexp
"em-glob" (pattern))
307 (defun eshell-do-ls (&rest args
)
308 "Implementation of \"ls\" in Lisp, passing ARGS."
309 (funcall flush-func -
1)
310 ;; Process the command arguments, and begin listing files.
311 (eshell-eval-using-options
312 "ls" (if eshell-ls-initial-args
313 (list eshell-ls-initial-args args
)
315 `((?a
"all" nil show-all
316 "do not ignore entries starting with .")
317 (?A
"almost-all" nil show-almost-all
318 "do not list implied . and ..")
319 (?c nil by-ctime sort-method
320 "sort by last status change time")
321 (?d
"directory" nil dir-literal
322 "list directory entries instead of contents")
323 (?k
"kilobytes" 1024 block-size
324 "using 1024 as the block size")
325 (?h
"human-readable" 1024 human-readable
326 "print sizes in human readable format")
327 (?H
"si" 1000 human-readable
328 "likewise, but use powers of 1000 not 1024")
329 (?I
"ignore" t ignore-pattern
330 "do not list implied entries matching pattern")
331 (?l nil long-listing listing-style
332 "use a long listing format")
333 (?n
"numeric-uid-gid" nil numeric-uid-gid
334 "list numeric UIDs and GIDs instead of names")
335 (?r
"reverse" nil reverse-list
336 "reverse order while sorting")
337 (?s
"size" nil show-size
338 "print size of each file, in blocks")
339 (?t nil by-mtime sort-method
340 "sort by modification time")
341 (?u nil by-atime sort-method
342 "sort by last access time")
343 (?x nil by-lines listing-style
344 "list entries by lines instead of by columns")
345 (?C nil by-columns listing-style
346 "list entries by columns")
347 (?L
"dereference" nil dereference-links
348 "list entries pointed to by symbolic links")
349 (?R
"recursive" nil show-recursive
350 "list subdirectories recursively")
351 (?S nil by-size sort-method
353 (?U nil unsorted sort-method
354 "do not sort; list entries in directory order")
355 (?X nil by-extension sort-method
356 "sort alphabetically by entry extension")
357 (?
1 nil single-column listing-style
358 "list one file per line")
359 (nil "dired" nil dired-flag
360 "Here for compatibility with GNU ls.")
362 "show this usage display")
364 :usage
"[OPTION]... [FILE]...
365 List information about the FILEs (the current directory by default).
366 Sort entries alphabetically across.")
367 ;; setup some defaults, based on what the user selected
369 (setq block-size eshell-ls-default-blocksize
))
370 (unless listing-style
371 (setq listing-style
'by-columns
))
373 (setq args
(list ".")))
374 (let ((eshell-ls-exclude-regexp eshell-ls-exclude-regexp
) ange-cache
)
376 (unless (eshell-using-module 'eshell-glob
)
377 (error (concat "-I option requires that `eshell-glob'"
378 " be a member of `eshell-modules-list'")))
379 (set-text-properties 0 (length ignore-pattern
) nil ignore-pattern
)
380 (setq eshell-ls-exclude-regexp
381 (if eshell-ls-exclude-regexp
382 (concat "\\(" eshell-ls-exclude-regexp
"\\|"
383 (eshell-glob-regexp ignore-pattern
) "\\)")
384 (eshell-glob-regexp ignore-pattern
))))
387 (mapcar (lambda (arg)
388 (cons (if (and (eshell-under-windows-p)
389 (file-name-absolute-p arg
))
390 (expand-file-name arg
)
392 (eshell-file-attributes
393 arg
(if numeric-uid-gid
'integer
'string
))))
395 t
(expand-file-name default-directory
)))
396 (funcall flush-func
)))
398 (defsubst eshell-ls-printable-size
(filesize &optional by-blocksize
)
399 "Return a printable FILESIZE."
400 (eshell-printable-size filesize human-readable
401 (and by-blocksize block-size
)
402 eshell-ls-use-colors
))
404 (defsubst eshell-ls-size-string
(attrs size-width
)
405 "Return the size string for ATTRS length, using SIZE-WIDTH."
406 (let* ((str (eshell-ls-printable-size (nth 7 attrs
) t
))
408 (if (< len size-width
)
409 (concat (make-string (- size-width len
) ?
) str
)
412 (defun eshell-ls-annotate (fileinfo)
413 "Given a FILEINFO object, return a resolved, decorated FILEINFO.
414 This means resolving any symbolic links, determining what face the
415 name should be displayed as, etc. Think of it as cooking a FILEINFO."
416 (if (not (and (stringp (cadr fileinfo
))
417 (or dereference-links
418 (eq listing-style
'long-listing
))))
419 (setcar fileinfo
(eshell-ls-decorated-name fileinfo
))
421 (unless (file-name-absolute-p (cadr fileinfo
))
422 (setq dir
(file-truename
424 (expand-file-name (car fileinfo
))))))
426 (eshell-file-attributes
427 (let ((target (if dir
428 (expand-file-name (cadr fileinfo
) dir
)
430 (if dereference-links
431 (file-truename target
)
433 (if (or dereference-links
434 (string-match "^\\.\\.?$" (car fileinfo
)))
436 (setcdr fileinfo attr
)
437 (setcar fileinfo
(eshell-ls-decorated-name fileinfo
)))
438 (cl-assert (eq listing-style
'long-listing
))
440 (concat (eshell-ls-decorated-name fileinfo
) " -> "
441 (eshell-ls-decorated-name
442 (cons (cadr fileinfo
) attr
)))))))
445 (defun eshell-ls-file (fileinfo &optional size-width copy-fileinfo
)
446 "Output FILE in long format.
447 FILE may be a string, or a cons cell whose car is the filename and
448 whose cdr is the list of file attributes."
449 (if (not (cdr fileinfo
))
450 (funcall error-func
(format "%s: No such file or directory\n"
453 (eshell-ls-annotate (if copy-fileinfo
457 (let ((file (car fileinfo
))
458 (attrs (cdr fileinfo
)))
459 (if (not (eq listing-style
'long-listing
))
461 (funcall insert-func
(eshell-ls-size-string attrs size-width
)
463 (funcall insert-func file
"\n"))
467 (concat (eshell-ls-size-string attrs size-width
) " "))
472 (or (nth 8 attrs
) "??????????")
474 (or (let ((user (nth 2 attrs
)))
476 (eshell-substring user
14)))
479 (or (let ((group (nth 3 attrs
)))
481 (eshell-substring group
8)))
484 (let* ((str (eshell-ls-printable-size (nth 7 attrs
)))
486 ;; Let file sizes shorter than 9 align neatly.
487 (if (< len
(or size-width
8))
488 (concat (make-string (- (or size-width
8) len
) ?
) str
)
490 " " (format-time-string
492 eshell-ls-date-format
" "
493 (if (= (nth 5 (decode-time (current-time)))
496 ((eq sort-method
'by-atime
) 4)
497 ((eq sort-method
'by-ctime
) 6)
501 ((eq sort-method
'by-atime
) 4)
502 ((eq sort-method
'by-ctime
) 6)
503 (t 5)) attrs
)) " ")))
504 (funcall insert-func line file
"\n"))))))
506 (defun eshell-ls-dir (dirinfo &optional insert-name root-dir size-width
)
507 "Output the entries in DIRINFO.
508 If INSERT-NAME is non-nil, the name of DIRINFO will be output. If
509 ROOT-DIR is also non-nil, and a directory name, DIRINFO will be output
510 relative to that directory."
511 (let ((dir (car dirinfo
)))
512 (if (not (cdr dirinfo
))
513 (funcall error-func
(format "%s: No such file or directory\n" dir
))
515 (eshell-ls-file dirinfo size-width
)
518 (eshell-ls-decorated-name
521 (file-relative-name dir root-dir
)
522 (expand-file-name dir
)))
523 (cdr dirinfo
))) ":\n"))
524 (let ((entries (eshell-directory-files-and-attributes
525 dir nil
(and (not (or show-all show-almost-all
))
526 eshell-ls-exclude-hidden
528 ;; Asking for UID and GID as
529 ;; strings saves another syscall
530 ;; later when we are going to
531 ;; display user and group names.
532 (if numeric-uid-gid
'integer
'string
))))
533 (when (and show-almost-all
538 (member (car entry
) '("." "..")))
540 (when (and (not (or show-all show-almost-all
))
541 eshell-ls-exclude-regexp
)
542 (while (and entries
(string-match eshell-ls-exclude-regexp
544 (setq entries
(cdr entries
)))
547 (if (string-match eshell-ls-exclude-regexp
(car (cadr e
)))
550 (when (or (eq listing-style
'long-listing
) show-size
)
555 (setq total
(+ total
(nth 7 (cdr e
)))
558 (length (eshell-ls-printable-size
561 ;; If we are under -l, count length
562 ;; of sizes in bytes, not in blocks.
563 (eq listing-style
'long-listing
))))))))
564 (funcall insert-func
"total "
565 (eshell-ls-printable-size total t
) "\n")))
566 (let ((default-directory (expand-file-name dir
)))
569 (let ((e entries
) (good-entries (list t
)))
571 (unless (let ((len (length (caar e
))))
572 (and (eq (aref (caar e
) 0) ?.
)
575 (eq (aref (caar e
) 1) ?.
)))))
576 (nconc good-entries
(list (car e
))))
580 (eshell-ls-files (eshell-ls-sort-entries entries
)
583 (defsubst eshell-ls-compare-entries
(l r inx func
)
584 "Compare the time of two files, L and R, the attribute indexed by INX."
585 (let ((lt (nth inx
(cdr l
)))
586 (rt (nth inx
(cdr r
))))
588 (string-lessp (directory-file-name (car l
))
589 (directory-file-name (car r
)))
590 (funcall func rt lt
))))
592 (defun eshell-ls-sort-entries (entries)
593 "Sort the given ENTRIES, which may be files, directories or both.
594 In Eshell's implementation of ls, ENTRIES is always reversed."
595 (if (eq sort-method
'unsorted
)
602 ((eq sort-method
'by-atime
)
603 (eshell-ls-compare-entries l r
4 'time-less-p
))
604 ((eq sort-method
'by-mtime
)
605 (eshell-ls-compare-entries l r
5 'time-less-p
))
606 ((eq sort-method
'by-ctime
)
607 (eshell-ls-compare-entries l r
6 'time-less-p
))
608 ((eq sort-method
'by-size
)
609 (eshell-ls-compare-entries l r
7 '<))
610 ((eq sort-method
'by-extension
)
611 (let ((lx (file-name-extension
612 (directory-file-name (car l
))))
613 (rx (file-name-extension
614 (directory-file-name (car r
)))))
616 ((or (and (not lx
) (not rx
))
618 (string-lessp (directory-file-name (car l
))
619 (directory-file-name (car r
))))
623 (string-lessp lx rx
)))))
625 (string-lessp (directory-file-name (car l
))
626 (directory-file-name (car r
)))))))
631 (defun eshell-ls-files (files &optional size-width copy-fileinfo
)
632 "Output a list of FILES.
633 Each member of FILES is either a string or a cons cell of the form
635 ;; Mimic behavior of coreutils ls, which lists a single file per
636 ;; line when output is not a tty. Exceptions: if -x was supplied,
637 ;; or if we are the _last_ command in a pipeline.
638 ;; FIXME Not really the same since not testing output destination.
639 (if (or (and eshell-in-pipeline-p
640 (not (eq eshell-in-pipeline-p
'last
))
641 (not (eq listing-style
'by-lines
)))
642 (memq listing-style
'(long-listing single-column
)))
645 (eshell-ls-file file size-width copy-fileinfo
)))
656 (format "%s: No such file or directory\n" (caar f
))))
658 (setq files
(cdr files
)
665 (setcdr f
(cddr f
))))))
667 (setq display-files
(mapcar 'eshell-ls-annotate files
))
669 (let* ((str (eshell-ls-printable-size (nth 7 (cdr file
)) t
))
671 (if (< len size-width
)
672 (setq str
(concat (make-string (- size-width len
) ?
) str
)))
673 (setq file
(eshell-ls-annotate file
)
674 display-files
(cons (cons (concat str
" " (car file
))
677 (setq display-files
(nreverse display-files
)))
679 (if (eq listing-style
'by-columns
)
680 (eshell-ls-find-column-lengths display-files
)
681 (cl-assert (eq listing-style
'by-lines
))
682 (eshell-ls-find-column-widths display-files
)))
683 (col-widths (car col-vals
))
684 (display-files (cdr col-vals
))
685 (columns (length col-widths
))
688 (dolist (file display-files
)
692 (concat (substring (car file
) 0 size-width
)
693 (eshell-ls-decorated-name
694 (cons (substring (car file
) size-width
)
696 (eshell-ls-decorated-name file
))
698 (if (< col-index columns
)
700 (concat need-return name
702 (max 0 (- (aref col-widths
705 col-index
(1+ col-index
))
706 (funcall insert-func need-return name
"\n")
707 (setq col-index
1 need-return nil
))))
709 (funcall insert-func need-return
"\n"))))))
711 (defun eshell-ls-entries (entries &optional separate root-dir
)
712 "Output PATH's directory ENTRIES.
713 Each member of ENTRIES may either be a string or a cons cell, the car
714 of which is the file name, and the cdr of which is the list of
716 If SEPARATE is non-nil, directories name will be entirely separated
717 from the filenames. This is the normal behavior, except when doing a
719 ROOT-DIR, if non-nil, specifies the root directory of the listing, to
720 which non-absolute directory names will be made relative if ever they
722 (let (dirs files show-names need-return
(size-width 0))
723 (dolist (entry entries
)
724 (if (and (not dir-literal
)
725 (or (eshell-ls-filetype-p (cdr entry
) ?d
)
726 (and (eshell-ls-filetype-p (cdr entry
) ?l
)
727 (file-directory-p (car entry
)))))
730 (setq files
(cons entry files
)
734 (length (eshell-ls-printable-size
735 (nth 7 (cdr entry
)) t
))))))
736 (setq dirs
(cons entry dirs
)))
737 (setq files
(cons entry files
)
741 (length (eshell-ls-printable-size
742 (nth 7 (cdr entry
)) t
)))))))
744 (eshell-ls-files (eshell-ls-sort-entries files
)
745 size-width show-recursive
)
746 (setq need-return t
))
747 (setq show-names
(or show-recursive
748 (> (+ (length files
) (length dirs
)) 1)))
749 (dolist (dir (eshell-ls-sort-entries dirs
))
750 (if (and need-return
(not dir-literal
))
751 (funcall insert-func
"\n"))
752 (eshell-ls-dir dir show-names
753 (unless (file-name-absolute-p (car dir
)) root-dir
)
755 (setq need-return t
))))
757 (defun eshell-ls-find-column-widths (files)
758 "Find the best fitting column widths for FILES.
759 It will be returned as a vector, whose length is the number of columns
760 to use, and each member of which is the width of that column
761 \(including spacing)."
768 (+ 2 (length (car file
)))))
770 ;; must account for the added space...
771 (max-width (+ (window-width) 2))
775 ;; determine the largest number of columns in the first row
777 (while (and w
(< width max-width
))
778 (setq width
(+ width
(car w
))
782 ;; refine it based on the following rows
785 (colw (make-vector numcols
0))
790 (aset colw i
(max (aref colw i
) (car w
)))
791 (setq w
(cdr w
) i
(1+ i
)))
794 (setq width
(+ width
(aref colw i
))
796 (if (and (< width max-width
)
797 (> width best-width
))
798 (setq col-widths colw
800 (setq numcols
(1- numcols
)))
802 (cons (or col-widths
(vector max-width
)) files
)))
804 (defun eshell-ls-find-column-lengths (files)
805 "Find the best fitting column lengths for FILES.
806 It will be returned as a vector, whose length is the number of columns
807 to use, and each member of which is the width of that column
808 \(including spacing)."
815 (+ 2 (length (car file
)))))
817 (max-width (+ (window-width) 2))
821 ;; refine it based on the following rows
823 (let* ((rows (ceiling (/ (length widths
)
826 (len (* rows numcols
))
830 (unless (or (= rows
0)
831 (<= (/ (length widths
) (float rows
))
832 (float (1- numcols
))))
833 (setq colw
(make-vector numcols
0))
836 (setq i
0 index
(1+ index
)))
839 (or (nth (+ (* i rows
) index
) w
) 0)))
840 (setq len
(1- len
) i
(1+ i
)))
843 (setq width
(+ width
(aref colw i
))
845 (if (>= width max-width
)
848 (setq col-widths colw
))
849 (if (>= numcols
(length widths
))
851 (setq numcols
(1+ numcols
))))))
854 (cons (vector max-width
) files
)
855 (setq numcols
(length col-widths
))
856 (let* ((rows (ceiling (/ (length widths
)
858 (len (* rows numcols
))
859 (newfiles (make-list len nil
))
865 (setq i
0 index
(1+ index
)))
866 (setcar (nthcdr j newfiles
)
867 (nth (+ (* i rows
) index
) files
))
868 (setq j
(1+ j
) i
(1+ i
)))
869 (cons col-widths newfiles
)))))
871 (defun eshell-ls-decorated-name (file)
872 "Return FILE, possibly decorated."
873 (if eshell-ls-use-colors
879 ((stringp (cadr file
))
883 'eshell-ls-directory
)
885 ((not (eshell-ls-filetype-p (cdr file
) ?-
))
888 ((and (/= (user-uid) 0) ; root can execute anything
889 (eshell-ls-applicable (cdr file
) 3
890 'file-executable-p
(car file
)))
891 'eshell-ls-executable
)
893 ((not (eshell-ls-applicable (cdr file
) 1
894 'file-readable-p
(car file
)))
895 'eshell-ls-unreadable
)
897 ((string-match eshell-ls-archive-regexp
(car file
))
900 ((string-match eshell-ls-backup-regexp
(car file
))
903 ((string-match eshell-ls-product-regexp
(car file
))
906 ((string-match eshell-ls-clutter-regexp
(car file
))
909 ((not (eshell-ls-applicable (cdr file
) 2
910 'file-writable-p
(car file
)))
912 (eshell-ls-highlight-alist
913 (let ((tests eshell-ls-highlight-alist
)
916 (if (funcall (caar tests
) (car file
) (cdr file
))
917 (setq value
(cdar tests
) tests nil
)
918 (setq tests
(cdr tests
))))
921 (add-text-properties 0 (length (car file
))
922 (list 'font-lock-face face
)
929 ;; generated-autoload-file: "esh-groups.el"
932 ;;; em-ls.el ends here