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