em-ls.el 31 KB

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