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