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