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