(directory-files-and-attributes): Accept and ignore additional optional
[bpt/emacs.git] / lisp / eshell / esh-util.el
1 ;;; esh-util.el --- general utilities
2
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 ;; 2008, 2009 Free Software Foundation, Inc.
5
6 ;; Author: John Wiegley <johnw@gnu.org>
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 (defgroup eshell-util nil
28 "This is general utility code, meant for use by Eshell itself."
29 :tag "General utilities"
30 :group 'eshell)
31
32 ;;; User Variables:
33
34 (defcustom eshell-stringify-t t
35 "*If non-nil, the string representation of t is 't'.
36 If nil, t will be represented only in the exit code of the function,
37 and not printed as a string. This causes Lisp functions to behave
38 similarly to external commands, as far as successful result output."
39 :type 'boolean
40 :group 'eshell-util)
41
42 (defcustom eshell-group-file "/etc/group"
43 "*If non-nil, the name of the group file on your system."
44 :type '(choice (const :tag "No group file" nil) file)
45 :group 'eshell-util)
46
47 (defcustom eshell-passwd-file "/etc/passwd"
48 "*If non-nil, the name of the passwd file on your system."
49 :type '(choice (const :tag "No passwd file" nil) file)
50 :group 'eshell-util)
51
52 (defcustom eshell-hosts-file "/etc/hosts"
53 "*The name of the /etc/hosts file."
54 :type '(choice (const :tag "No hosts file" nil) file)
55 :group 'eshell-util)
56
57 (defcustom eshell-handle-errors t
58 "*If non-nil, Eshell will handle errors itself.
59 Setting this to nil is offered as an aid to debugging only."
60 :type 'boolean
61 :group 'eshell-util)
62
63 (defcustom eshell-private-file-modes 384 ; umask 177
64 "*The file-modes value to use for creating \"private\" files."
65 :type 'integer
66 :group 'eshell-util)
67
68 (defcustom eshell-private-directory-modes 448 ; umask 077
69 "*The file-modes value to use for creating \"private\" directories."
70 :type 'integer
71 :group 'eshell-util)
72
73 (defcustom eshell-tar-regexp
74 "\\.t\\(ar\\(\\.\\(gz\\|bz2\\|Z\\)\\)?\\|gz\\|a[zZ]\\|z2\\)\\'"
75 "*Regular expression used to match tar file names."
76 :type 'regexp
77 :group 'eshell-util)
78
79 (defcustom eshell-convert-numeric-arguments t
80 "*If non-nil, converting arguments of numeric form to Lisp numbers.
81 Numeric form is tested using the regular expression
82 `eshell-number-regexp'.
83
84 NOTE: If you find that numeric conversions are intefering with the
85 specification of filenames (for example, in calling `find-file', or
86 some other Lisp function that deals with files, not numbers), add the
87 following in your .emacs file:
88
89 (put 'find-file 'eshell-no-numeric-conversions t)
90
91 Any function with the property `eshell-no-numeric-conversions' set to
92 a non-nil value, will be passed strings, not numbers, even when an
93 argument matches `eshell-number-regexp'."
94 :type 'boolean
95 :group 'eshell-util)
96
97 (defcustom eshell-number-regexp "-?\\([0-9]*\\.\\)?[0-9]+\\(e[-0-9.]+\\)?"
98 "*Regular expression used to match numeric arguments.
99 If `eshell-convert-numeric-arguments' is non-nil, and an argument
100 matches this regexp, it will be converted to a Lisp number, using the
101 function `string-to-number'."
102 :type 'regexp
103 :group 'eshell-util)
104
105 (defcustom eshell-ange-ls-uids nil
106 "*List of user/host/id strings, used to determine remote ownership."
107 :type '(repeat (cons :tag "Host for User/UID map"
108 (string :tag "Hostname")
109 (repeat (cons :tag "User/UID List"
110 (string :tag "Username")
111 (repeat :tag "UIDs" string)))))
112 :group 'eshell-util)
113
114 ;;; Internal Variables:
115
116 (defvar eshell-group-names nil
117 "A cache to hold the names of groups.")
118
119 (defvar eshell-group-timestamp nil
120 "A timestamp of when the group file was read.")
121
122 (defvar eshell-user-names nil
123 "A cache to hold the names of users.")
124
125 (defvar eshell-user-timestamp nil
126 "A timestamp of when the user file was read.")
127
128 (defvar eshell-host-names nil
129 "A cache the names of frequently accessed hosts.")
130
131 (defvar eshell-host-timestamp nil
132 "A timestamp of when the hosts file was read.")
133
134 ;;; Functions:
135
136 (defsubst eshell-under-windows-p ()
137 "Return non-nil if we are running under MS-DOS/Windows."
138 (memq system-type '(ms-dos windows-nt)))
139
140 (defmacro eshell-condition-case (tag form &rest handlers)
141 "Like `condition-case', but only if `eshell-pass-through-errors' is nil."
142 (if eshell-handle-errors
143 `(condition-case ,tag
144 ,form
145 ,@handlers)
146 form))
147
148 (put 'eshell-condition-case 'lisp-indent-function 2)
149
150 (defmacro eshell-deftest (module name label &rest forms)
151 (if (and (fboundp 'cl-compiling-file) (cl-compiling-file))
152 nil
153 (let ((fsym (intern (concat "eshell-test--" (symbol-name name)))))
154 `(eval-when-compile
155 (ignore
156 (defun ,fsym () ,label
157 (eshell-run-test (quote ,module) (quote ,fsym) ,label
158 (quote (progn ,@forms)))))))))
159
160 (put 'eshell-deftest 'lisp-indent-function 2)
161
162 (defun eshell-find-delimiter
163 (open close &optional bound reverse-p backslash-p)
164 "From point, find the CLOSE delimiter corresponding to OPEN.
165 The matching is bounded by BOUND.
166 If REVERSE-P is non-nil, process the region backwards.
167 If BACKSLASH-P is non-nil, and OPEN and CLOSE are the same character,
168 then quoting is done by a backslash, rather than a doubled delimiter."
169 (save-excursion
170 (let ((depth 1)
171 (bound (or bound (point-max))))
172 (if (if reverse-p
173 (eq (char-before) close)
174 (eq (char-after) open))
175 (forward-char (if reverse-p -1 1)))
176 (while (and (> depth 0)
177 (funcall (if reverse-p '> '<) (point) bound))
178 (let ((c (if reverse-p (char-before) (char-after))) nc)
179 (cond ((and (not reverse-p)
180 (or (not (eq open close))
181 backslash-p)
182 (eq c ?\\)
183 (setq nc (char-after (1+ (point))))
184 (or (eq nc open) (eq nc close)))
185 (forward-char 1))
186 ((and reverse-p
187 (or (not (eq open close))
188 backslash-p)
189 (or (eq c open) (eq c close))
190 (eq (char-before (1- (point)))
191 ?\\))
192 (forward-char -1))
193 ((eq open close)
194 (if (eq c open)
195 (if (and (not backslash-p)
196 (eq (if reverse-p
197 (char-before (1- (point)))
198 (char-after (1+ (point)))) open))
199 (forward-char (if reverse-p -1 1))
200 (setq depth (1- depth)))))
201 ((= c open)
202 (setq depth (+ depth (if reverse-p -1 1))))
203 ((= c close)
204 (setq depth (+ depth (if reverse-p 1 -1))))))
205 (forward-char (if reverse-p -1 1)))
206 (if (= depth 0)
207 (if reverse-p (point) (1- (point)))))))
208
209 (defun eshell-convert (string)
210 "Convert STRING into a more native looking Lisp object."
211 (if (not (stringp string))
212 string
213 (let ((len (length string)))
214 (if (= len 0)
215 string
216 (if (eq (aref string (1- len)) ?\n)
217 (setq string (substring string 0 (1- len))))
218 (if (string-match "\n" string)
219 (split-string string "\n")
220 (if (and eshell-convert-numeric-arguments
221 (string-match
222 (concat "\\`\\s-*" eshell-number-regexp
223 "\\s-*\\'") string))
224 (string-to-number string)
225 string))))))
226
227 (defun eshell-sublist (l &optional n m)
228 "Return from LIST the N to M elements.
229 If N or M is nil, it means the end of the list."
230 (let* ((a (copy-sequence l))
231 result)
232 (if (and m (consp (nthcdr m a)))
233 (setcdr (nthcdr m a) nil))
234 (if n
235 (setq a (nthcdr n a))
236 (setq n (1- (length a))
237 a (last a)))
238 a))
239
240 (defun eshell-split-path (path)
241 "Split a path into multiple subparts."
242 (let ((len (length path))
243 (i 0) (li 0)
244 parts)
245 (if (and (eshell-under-windows-p)
246 (> len 2)
247 (eq (aref path 0) ?/)
248 (eq (aref path 1) ?/))
249 (setq i 2))
250 (while (< i len)
251 (if (and (eq (aref path i) ?/)
252 (not (get-text-property i 'escaped path)))
253 (setq parts (cons (if (= li i) "/"
254 (substring path li (1+ i))) parts)
255 li (1+ i)))
256 (setq i (1+ i)))
257 (if (< li i)
258 (setq parts (cons (substring path li i) parts)))
259 (if (and (eshell-under-windows-p)
260 (string-match "\\`[A-Za-z]:\\'" (car (last parts))))
261 (setcar (last parts) (concat (car (last parts)) "/")))
262 (nreverse parts)))
263
264 (defun eshell-to-flat-string (value)
265 "Make value a string. If separated by newlines change them to spaces."
266 (let ((text (eshell-stringify value)))
267 (if (string-match "\n+\\'" text)
268 (setq text (replace-match "" t t text)))
269 (while (string-match "\n+" text)
270 (setq text (replace-match " " t t text)))
271 text))
272
273 ;; FIXME this is just dolist.
274 (defmacro eshell-for (for-var for-list &rest forms)
275 "Iterate through a list"
276 `(let ((list-iter ,for-list))
277 (while list-iter
278 (let ((,for-var (car list-iter)))
279 ,@forms)
280 (setq list-iter (cdr list-iter)))))
281
282 (put 'eshell-for 'lisp-indent-function 2)
283
284 (defun eshell-flatten-list (args)
285 "Flatten any lists within ARGS, so that there are no sublists."
286 (let ((new-list (list t)))
287 (eshell-for a args
288 (if (and (listp a)
289 (listp (cdr a)))
290 (nconc new-list (eshell-flatten-list a))
291 (nconc new-list (list a))))
292 (cdr new-list)))
293
294 (defun eshell-uniqify-list (l)
295 "Remove occurring multiples in L. You probably want to sort first."
296 (let ((m l))
297 (while m
298 (while (and (cdr m)
299 (string= (car m)
300 (cadr m)))
301 (setcdr m (cddr m)))
302 (setq m (cdr m))))
303 l)
304
305 (defun eshell-stringify (object)
306 "Convert OBJECT into a string value."
307 (cond
308 ((stringp object) object)
309 ((and (listp object)
310 (not (eq object nil)))
311 (let ((string (pp-to-string object)))
312 (substring string 0 (1- (length string)))))
313 ((numberp object)
314 (number-to-string object))
315 (t
316 (unless (and (eq object t)
317 (not eshell-stringify-t))
318 (pp-to-string object)))))
319
320 (defsubst eshell-stringify-list (args)
321 "Convert each element of ARGS into a string value."
322 (mapcar 'eshell-stringify args))
323
324 (defsubst eshell-flatten-and-stringify (&rest args)
325 "Flatten and stringify all of the ARGS into a single string."
326 (mapconcat 'eshell-stringify (eshell-flatten-list args) " "))
327
328 ;; the next two are from GNUS, and really should be made part of Emacs
329 ;; some day
330 (defsubst eshell-time-less-p (t1 t2)
331 "Say whether time T1 is less than time T2."
332 (or (< (car t1) (car t2))
333 (and (= (car t1) (car t2))
334 (< (nth 1 t1) (nth 1 t2)))))
335
336 (defsubst eshell-time-to-seconds (time)
337 "Convert TIME to a floating point number."
338 (+ (* (car time) 65536.0)
339 (cadr time)
340 (/ (or (car (cdr (cdr time))) 0) 1000000.0)))
341
342 (defsubst eshell-directory-files (regexp &optional directory)
343 "Return a list of files in the given DIRECTORY matching REGEXP."
344 (directory-files (or directory default-directory)
345 directory regexp))
346
347 (defun eshell-regexp-arg (prompt)
348 "Return list of regexp and prefix arg using PROMPT."
349 (let* (;; Don't clobber this.
350 (last-command last-command)
351 (regexp (read-from-minibuffer prompt nil nil nil
352 'minibuffer-history-search-history)))
353 (list (if (string-equal regexp "")
354 (setcar minibuffer-history-search-history
355 (nth 1 minibuffer-history-search-history))
356 regexp)
357 (prefix-numeric-value current-prefix-arg))))
358
359 (defun eshell-printable-size (filesize &optional human-readable
360 block-size use-colors)
361 "Return a printable FILESIZE."
362 (let ((size (float (or filesize 0))))
363 (if human-readable
364 (if (< size human-readable)
365 (if (= (round size) 0)
366 "0"
367 (if block-size
368 "1.0k"
369 (format "%.0f" size)))
370 (setq size (/ size human-readable))
371 (if (< size human-readable)
372 (if (<= size 9.94)
373 (format "%.1fk" size)
374 (format "%.0fk" size))
375 (setq size (/ size human-readable))
376 (if (< size human-readable)
377 (let ((str (if (<= size 9.94)
378 (format "%.1fM" size)
379 (format "%.0fM" size))))
380 (if use-colors
381 (put-text-property 0 (length str)
382 'face 'bold str))
383 str)
384 (setq size (/ size human-readable))
385 (if (< size human-readable)
386 (let ((str (if (<= size 9.94)
387 (format "%.1fG" size)
388 (format "%.0fG" size))))
389 (if use-colors
390 (put-text-property 0 (length str)
391 'face 'bold-italic str))
392 str)))))
393 (if block-size
394 (setq size (/ size block-size)))
395 (format "%.0f" size))))
396
397 (defun eshell-winnow-list (entries exclude &optional predicates)
398 "Pare down the ENTRIES list using the EXCLUDE regexp, and PREDICATES.
399 The original list is not affected. If the result is only one element
400 long, it will be returned itself, rather than returning a one-element
401 list."
402 (let ((flist (list t))
403 valid p listified)
404 (unless (listp entries)
405 (setq entries (list entries)
406 listified t))
407 (eshell-for entry entries
408 (unless (and exclude (string-match exclude entry))
409 (setq p predicates valid (null p))
410 (while p
411 (if (funcall (car p) entry)
412 (setq valid t)
413 (setq p nil valid nil))
414 (setq p (cdr p)))
415 (when valid
416 (nconc flist (list entry)))))
417 (if listified
418 (cadr flist)
419 (cdr flist))))
420
421 (defsubst eshell-redisplay ()
422 "Allow Emacs to redisplay buffers."
423 ;; for some strange reason, Emacs 21 is prone to trigger an
424 ;; "args out of range" error in `sit-for', if this function
425 ;; runs while point is in the minibuffer and the users attempt
426 ;; to use completion. Don't ask me.
427 (condition-case nil
428 (sit-for 0 0)
429 (error nil)))
430
431 (defun eshell-read-passwd-file (file)
432 "Return an alist correlating gids to group names in FILE."
433 (let (names)
434 (when (file-readable-p file)
435 (with-temp-buffer
436 (insert-file-contents file)
437 (goto-char (point-min))
438 (while (not (eobp))
439 (let* ((fields
440 (split-string (buffer-substring
441 (point) (progn (end-of-line)
442 (point))) ":")))
443 (if (and (and fields (nth 0 fields) (nth 2 fields))
444 (not (assq (string-to-number (nth 2 fields)) names)))
445 (setq names (cons (cons (string-to-number (nth 2 fields))
446 (nth 0 fields))
447 names))))
448 (forward-line))))
449 names))
450
451 (defun eshell-read-passwd (file result-var timestamp-var)
452 "Read the contents of /etc/passwd for user names."
453 (if (or (not (symbol-value result-var))
454 (not (symbol-value timestamp-var))
455 (eshell-time-less-p
456 (symbol-value timestamp-var)
457 (nth 5 (file-attributes file))))
458 (progn
459 (set result-var (eshell-read-passwd-file file))
460 (set timestamp-var (current-time))))
461 (symbol-value result-var))
462
463 (defun eshell-read-group-names ()
464 "Read the contents of /etc/group for group names."
465 (if eshell-group-file
466 (eshell-read-passwd eshell-group-file 'eshell-group-names
467 'eshell-group-timestamp)))
468
469 (defsubst eshell-group-id (name)
470 "Return the user id for user NAME."
471 (car (rassoc name (eshell-read-group-names))))
472
473 (defsubst eshell-group-name (gid)
474 "Return the group name for the given GID."
475 (cdr (assoc gid (eshell-read-group-names))))
476
477 (defun eshell-read-user-names ()
478 "Read the contents of /etc/passwd for user names."
479 (if eshell-passwd-file
480 (eshell-read-passwd eshell-passwd-file 'eshell-user-names
481 'eshell-user-timestamp)))
482
483 (defsubst eshell-user-id (name)
484 "Return the user id for user NAME."
485 (car (rassoc name (eshell-read-user-names))))
486
487 (defalias 'eshell-user-name 'user-login-name)
488
489 (defun eshell-read-hosts-file (filename)
490 "Read in the hosts from the /etc/hosts file."
491 (let (hosts)
492 (with-temp-buffer
493 (insert-file-contents eshell-hosts-file)
494 (goto-char (point-min))
495 (while (re-search-forward
496 "^\\(\\S-+\\)\\s-+\\(\\S-+\\)\\(\\s-*\\(\\S-+\\)\\)?" nil t)
497 (if (match-string 1)
498 (add-to-list 'hosts (match-string 1)))
499 (if (match-string 2)
500 (add-to-list 'hosts (match-string 2)))
501 (if (match-string 4)
502 (add-to-list 'hosts (match-string 4)))))
503 (sort hosts 'string-lessp)))
504
505 (defun eshell-read-hosts (file result-var timestamp-var)
506 "Read the contents of /etc/passwd for user names."
507 (if (or (not (symbol-value result-var))
508 (not (symbol-value timestamp-var))
509 (eshell-time-less-p
510 (symbol-value timestamp-var)
511 (nth 5 (file-attributes file))))
512 (progn
513 (set result-var (eshell-read-hosts-file file))
514 (set timestamp-var (current-time))))
515 (symbol-value result-var))
516
517 (defun eshell-read-host-names ()
518 "Read the contents of /etc/hosts for host names."
519 (if eshell-hosts-file
520 (eshell-read-hosts eshell-hosts-file 'eshell-host-names
521 'eshell-host-timestamp)))
522
523 (unless (fboundp 'line-end-position)
524 (defsubst line-end-position (&optional N)
525 (save-excursion (end-of-line N) (point))))
526
527 (unless (fboundp 'line-beginning-position)
528 (defsubst line-beginning-position (&optional N)
529 (save-excursion (beginning-of-line N) (point))))
530
531 (unless (fboundp 'subst-char-in-string)
532 (defun subst-char-in-string (fromchar tochar string &optional inplace)
533 "Replace FROMCHAR with TOCHAR in STRING each time it occurs.
534 Unless optional argument INPLACE is non-nil, return a new string."
535 (let ((i (length string))
536 (newstr (if inplace string (copy-sequence string))))
537 (while (> i 0)
538 (setq i (1- i))
539 (if (eq (aref newstr i) fromchar)
540 (aset newstr i tochar)))
541 newstr)))
542
543 (defsubst eshell-copy-environment ()
544 "Return an unrelated copy of `process-environment'."
545 (mapcar 'concat process-environment))
546
547 (defun eshell-subgroups (groupsym)
548 "Return all of the subgroups of GROUPSYM."
549 (let ((subgroups (get groupsym 'custom-group))
550 (subg (list t)))
551 (while subgroups
552 (if (eq (cadr (car subgroups)) 'custom-group)
553 (nconc subg (list (caar subgroups))))
554 (setq subgroups (cdr subgroups)))
555 (cdr subg)))
556
557 (defmacro eshell-with-file-modes (modes &rest forms)
558 "Evaluate, with file-modes set to MODES, the given FORMS."
559 `(let ((modes (default-file-modes)))
560 (set-default-file-modes ,modes)
561 (unwind-protect
562 (progn ,@forms)
563 (set-default-file-modes modes))))
564
565 (defmacro eshell-with-private-file-modes (&rest forms)
566 "Evaluate FORMS with private file modes set."
567 `(eshell-with-file-modes ,eshell-private-file-modes ,@forms))
568
569 (defsubst eshell-make-private-directory (dir &optional parents)
570 "Make DIR with file-modes set to `eshell-private-directory-modes'."
571 (eshell-with-file-modes eshell-private-directory-modes
572 (make-directory dir parents)))
573
574 (defsubst eshell-substring (string sublen)
575 "Return the beginning of STRING, up to SUBLEN bytes."
576 (if string
577 (if (> (length string) sublen)
578 (substring string 0 sublen)
579 string)))
580
581 (unless (fboundp 'directory-files-and-attributes)
582 (defun directory-files-and-attributes (directory &optional full match nosort id-format)
583 "Return a list of names of files and their attributes in DIRECTORY.
584 There are three optional arguments:
585 If FULL is non-nil, return absolute file names. Otherwise return names
586 that are relative to the specified directory.
587 If MATCH is non-nil, mention only file names that match the regexp MATCH.
588 If NOSORT is non-nil, the list is not sorted--its order is unpredictable.
589 NOSORT is useful if you plan to sort the result yourself."
590 (let ((directory (expand-file-name directory)) ange-cache)
591 (mapcar
592 (function
593 (lambda (file)
594 (cons file (eshell-file-attributes (expand-file-name file directory)))))
595 (directory-files directory full match nosort)))))
596
597 (defvar ange-cache)
598
599 (defun eshell-directory-files-and-attributes (dir &optional full match nosort id-format)
600 "Make sure to use the handler for `directory-file-and-attributes'."
601 (let* ((dir (expand-file-name dir))
602 (dfh (find-file-name-handler dir 'directory-files)))
603 (if (not dfh)
604 (directory-files-and-attributes dir full match nosort id-format)
605 (let ((files (funcall dfh 'directory-files dir full match nosort))
606 (fah (find-file-name-handler dir 'file-attributes)))
607 (mapcar
608 (function
609 (lambda (file)
610 (cons file (if fah
611 (eshell-file-attributes
612 (expand-file-name file dir))
613 (file-attributes (expand-file-name file dir))))))
614 files)))))
615
616 (defun eshell-current-ange-uids ()
617 (if (string-match "/\\([^@]+\\)@\\([^:]+\\):" default-directory)
618 (let* ((host (match-string 2 default-directory))
619 (user (match-string 1 default-directory))
620 (host-users (assoc host eshell-ange-ls-uids)))
621 (when host-users
622 (setq host-users (cdr host-users))
623 (cdr (assoc user host-users))))))
624
625 ;; Add an autoload for parse-time-string
626 (if (and (not (fboundp 'parse-time-string))
627 (locate-library "parse-time"))
628 (autoload 'parse-time-string "parse-time"))
629
630 (eval-when-compile
631 (require 'ange-ftp nil t))
632
633 (defun eshell-parse-ange-ls (dir)
634 (let (entry)
635 (with-temp-buffer
636 (insert (ange-ftp-ls dir "-la" nil))
637 (goto-char (point-min))
638 (if (looking-at "^total [0-9]+$")
639 (forward-line 1))
640 ;; Some systems put in a blank line here.
641 (if (eolp) (forward-line 1))
642 (while (looking-at
643 `,(concat "\\([dlscb-][rwxst-]+\\)"
644 "\\s-*" "\\([0-9]+\\)" "\\s-+"
645 "\\(\\S-+\\)" "\\s-+"
646 "\\(\\S-+\\)" "\\s-+"
647 "\\([0-9]+\\)" "\\s-+" "\\(.*\\)"))
648 (let* ((perms (match-string 1))
649 (links (string-to-number (match-string 2)))
650 (user (match-string 3))
651 (group (match-string 4))
652 (size (string-to-number (match-string 5)))
653 (name (ange-ftp-parse-filename))
654 (mtime
655 (if (fboundp 'parse-time-string)
656 (let ((moment (parse-time-string
657 (match-string 6))))
658 (if (nth 0 moment)
659 (setcar (nthcdr 5 moment)
660 (nth 5 (decode-time (current-time))))
661 (setcar (nthcdr 0 moment) 0)
662 (setcar (nthcdr 1 moment) 0)
663 (setcar (nthcdr 2 moment) 0))
664 (apply 'encode-time moment))
665 (ange-ftp-file-modtime (expand-file-name name dir))))
666 symlink)
667 (if (string-match "\\(.+\\) -> \\(.+\\)" name)
668 (setq symlink (match-string 2 name)
669 name (match-string 1 name)))
670 (setq entry
671 (cons
672 (cons name
673 (list (if (eq (aref perms 0) ?d)
674 t
675 symlink)
676 links user group
677 nil mtime nil
678 size perms nil nil)) entry)))
679 (forward-line)))
680 entry))
681
682 (defun eshell-file-attributes (file)
683 "Return the attributes of FILE, playing tricks if it's over ange-ftp."
684 (let* ((file (expand-file-name file))
685 (handler (find-file-name-handler file 'file-attributes))
686 entry)
687 (if (not handler)
688 (file-attributes file)
689 (if (eq (find-file-name-handler (file-name-directory file)
690 'directory-files)
691 'ange-ftp-hook-function)
692 (let ((base (file-name-nondirectory file))
693 (dir (file-name-directory file)))
694 (if (boundp 'ange-cache)
695 (setq entry (cdr (assoc base (cdr (assoc dir ange-cache))))))
696 (unless entry
697 (setq entry (eshell-parse-ange-ls dir))
698 (if (boundp 'ange-cache)
699 (setq ange-cache
700 (cons (cons dir entry)
701 ange-cache)))
702 (if entry
703 (let ((fentry (assoc base (cdr entry))))
704 (if fentry
705 (setq entry (cdr fentry))
706 (setq entry nil)))))))
707 (or entry (funcall handler 'file-attributes file)))))
708
709 (defalias 'eshell-copy-tree 'copy-tree)
710
711 (defsubst eshell-processp (proc)
712 "If the `processp' function does not exist, PROC is not a process."
713 (and (fboundp 'processp) (processp proc)))
714
715 ; (defun eshell-copy-file
716 ; (file newname &optional ok-if-already-exists keep-date)
717 ; "Copy FILE to NEWNAME. See docs for `copy-file'."
718 ; (let (copied)
719 ; (if (string-match "\\`\\([^:]+\\):\\(.*\\)" file)
720 ; (let ((front (match-string 1 file))
721 ; (back (match-string 2 file))
722 ; buffer)
723 ; (if (and front (string-match eshell-tar-regexp front)
724 ; (setq buffer (find-file-noselect front)))
725 ; (with-current-buffer buffer
726 ; (goto-char (point-min))
727 ; (if (re-search-forward (concat " " (regexp-quote back)
728 ; "$") nil t)
729 ; (progn
730 ; (tar-copy (if (file-directory-p newname)
731 ; (expand-file-name
732 ; (file-name-nondirectory back) newname)
733 ; newname))
734 ; (setq copied t))
735 ; (error "%s not found in tar file %s" back front))))))
736 ; (unless copied
737 ; (copy-file file newname ok-if-already-exists keep-date))))
738
739 ; (defun eshell-file-attributes (filename)
740 ; "Return a list of attributes of file FILENAME.
741 ; See the documentation for `file-attributes'."
742 ; (let (result)
743 ; (when (string-match "\\`\\([^:]+\\):\\(.*\\)\\'" filename)
744 ; (let ((front (match-string 1 filename))
745 ; (back (match-string 2 filename))
746 ; buffer)
747 ; (when (and front (string-match eshell-tar-regexp front)
748 ; (setq buffer (find-file-noselect front)))
749 ; (with-current-buffer buffer
750 ; (goto-char (point-min))
751 ; (when (re-search-forward (concat " " (regexp-quote back)
752 ; "\\s-*$") nil t)
753 ; (let* ((descrip (tar-current-descriptor))
754 ; (tokens (tar-desc-tokens descrip)))
755 ; (setq result
756 ; (list
757 ; (cond
758 ; ((eq (tar-header-link-type tokens) 5)
759 ; t)
760 ; ((eq (tar-header-link-type tokens) t)
761 ; (tar-header-link-name tokens)))
762 ; 1
763 ; (tar-header-uid tokens)
764 ; (tar-header-gid tokens)
765 ; (tar-header-date tokens)
766 ; (tar-header-date tokens)
767 ; (tar-header-date tokens)
768 ; (tar-header-size tokens)
769 ; (concat
770 ; (cond
771 ; ((eq (tar-header-link-type tokens) 5) "d")
772 ; ((eq (tar-header-link-type tokens) t) "l")
773 ; (t "-"))
774 ; (tar-grind-file-mode (tar-header-mode tokens)
775 ; (make-string 9 ? ) 0))
776 ; nil nil nil))))))))
777 ; (or result
778 ; (file-attributes filename))))
779
780 (provide 'esh-util)
781
782 ;; arch-tag: 70159778-5c7a-480a-bae4-3ad332fca19d
783 ;;; esh-util.el ends here