Delete Emacs 25 test in mode-local.el
[emacs.git] / lisp / eshell / esh-util.el
blob170779fc5cdf7c4f45c0372d8c63f8a32ed72f76
1 ;;; esh-util.el --- general utilities -*- lexical-binding:t -*-
3 ;; Copyright (C) 1999-2015 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 (declare (obsolete with-file-modes "25.1"))
543 `(with-file-modes ,modes ,@forms))
545 (defmacro eshell-with-private-file-modes (&rest forms)
546 "Evaluate FORMS with private file modes set."
547 `(with-file-modes ,eshell-private-file-modes ,@forms))
549 (defsubst eshell-make-private-directory (dir &optional parents)
550 "Make DIR with file-modes set to `eshell-private-directory-modes'."
551 (with-file-modes eshell-private-directory-modes
552 (make-directory dir parents)))
554 (defsubst eshell-substring (string sublen)
555 "Return the beginning of STRING, up to SUBLEN bytes."
556 (if string
557 (if (> (length string) sublen)
558 (substring string 0 sublen)
559 string)))
561 (defvar ange-cache)
563 ;; Partial reimplementation of Emacs's builtin directory-files-and-attributes.
564 ;; id-format not implemented.
565 (and (featurep 'xemacs)
566 (not (fboundp 'directory-files-and-attributes))
567 (defun directory-files-and-attributes (directory &optional full match nosort _id-format)
568 "Return a list of names of files and their attributes in DIRECTORY.
569 There are three optional arguments:
570 If FULL is non-nil, return absolute file names. Otherwise return names
571 that are relative to the specified directory.
572 If MATCH is non-nil, mention only file names that match the regexp MATCH.
573 If NOSORT is non-nil, the list is not sorted--its order is unpredictable.
574 NOSORT is useful if you plan to sort the result yourself."
575 (let ((directory (expand-file-name directory)) ange-cache)
576 (mapcar
577 (function
578 (lambda (file)
579 (cons file (eshell-file-attributes (expand-file-name file directory)))))
580 (directory-files directory full match nosort)))))
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)) ; ange-ftp-parse-filename
610 (defvar tramp-file-name-structure)
611 (declare-function ange-ftp-ls "ange-ftp"
612 (file lsargs parse &optional no-error wildcard))
613 (declare-function ange-ftp-file-modtime "ange-ftp" (file))
615 (defun eshell-parse-ange-ls (dir)
616 (require 'ange-ftp)
617 (require 'tramp)
618 (let ((ange-ftp-name-format
619 (list (nth 0 tramp-file-name-structure)
620 (nth 3 tramp-file-name-structure)
621 (nth 2 tramp-file-name-structure)
622 (nth 4 tramp-file-name-structure)))
623 ;; ange-ftp uses `ange-ftp-ftp-name-arg' and `ange-ftp-ftp-name-res'
624 ;; for optimization in `ange-ftp-ftp-name'. If Tramp wasn't active,
625 ;; there could be incorrect values from previous calls in case the
626 ;; "ftp" method is used in the Tramp file name. So we unset
627 ;; those values.
628 (ange-ftp-ftp-name-arg "")
629 (ange-ftp-ftp-name-res nil)
630 entry)
631 (with-temp-buffer
632 (insert (ange-ftp-ls dir "-la" nil))
633 (goto-char (point-min))
634 (if (looking-at "^total [0-9]+$")
635 (forward-line 1))
636 ;; Some systems put in a blank line here.
637 (if (eolp) (forward-line 1))
638 (while (looking-at
639 `,(concat "\\([dlscb-][rwxst-]+\\)"
640 "\\s-*" "\\([0-9]+\\)" "\\s-+"
641 "\\(\\S-+\\)" "\\s-+"
642 "\\(\\S-+\\)" "\\s-+"
643 "\\([0-9]+\\)" "\\s-+" "\\(.*\\)"))
644 (let* ((perms (match-string 1))
645 (links (string-to-number (match-string 2)))
646 (user (match-string 3))
647 (group (match-string 4))
648 (size (string-to-number (match-string 5)))
649 (name (ange-ftp-parse-filename))
650 (mtime
651 (if (fboundp 'parse-time-string)
652 (let ((moment (parse-time-string
653 (match-string 6))))
654 (if (nth 0 moment)
655 (setcar (nthcdr 5 moment)
656 (nth 5 (decode-time)))
657 (setcar (nthcdr 0 moment) 0)
658 (setcar (nthcdr 1 moment) 0)
659 (setcar (nthcdr 2 moment) 0))
660 (apply 'encode-time moment))
661 (ange-ftp-file-modtime (expand-file-name name dir))))
662 symlink)
663 (if (string-match "\\(.+\\) -> \\(.+\\)" name)
664 (setq symlink (match-string 2 name)
665 name (match-string 1 name)))
666 (setq entry
667 (cons
668 (cons name
669 (list (if (eq (aref perms 0) ?d)
671 symlink)
672 links user group
673 nil mtime nil
674 size perms nil nil)) entry)))
675 (forward-line)))
676 entry))
678 (defun eshell-file-attributes (file &optional id-format)
679 "Return the attributes of FILE, playing tricks if it's over ange-ftp.
680 The optional argument ID-FORMAT specifies the preferred uid and
681 gid format. Valid values are 'string and 'integer, defaulting to
682 'integer. See `file-attributes'."
683 (let* ((file (expand-file-name file))
684 entry)
685 (if (string-equal (file-remote-p file 'method) "ftp")
686 (let ((base (file-name-nondirectory file))
687 (dir (file-name-directory file)))
688 (if (string-equal "" base) (setq base "."))
689 (if (boundp 'ange-cache)
690 (setq entry (cdr (assoc base (cdr (assoc dir ange-cache))))))
691 (unless entry
692 (setq entry (eshell-parse-ange-ls dir))
693 (if (boundp 'ange-cache)
694 (setq ange-cache
695 (cons (cons dir entry)
696 ange-cache)))
697 (if entry
698 (let ((fentry (assoc base (cdr entry))))
699 (if fentry
700 (setq entry (cdr fentry))
701 (setq entry nil)))))
702 entry)
703 (file-attributes file id-format))))
705 (defalias 'eshell-copy-tree 'copy-tree)
707 (defsubst eshell-processp (proc)
708 "If the `processp' function does not exist, PROC is not a process."
709 (and (fboundp 'processp) (processp proc)))
711 ; (defun eshell-copy-file
712 ; (file newname &optional ok-if-already-exists keep-date)
713 ; "Copy FILE to NEWNAME. See docs for `copy-file'."
714 ; (let (copied)
715 ; (if (string-match "\\`\\([^:]+\\):\\(.*\\)" file)
716 ; (let ((front (match-string 1 file))
717 ; (back (match-string 2 file))
718 ; buffer)
719 ; (if (and front (string-match eshell-tar-regexp front)
720 ; (setq buffer (find-file-noselect front)))
721 ; (with-current-buffer buffer
722 ; (goto-char (point-min))
723 ; (if (re-search-forward (concat " " (regexp-quote back)
724 ; "$") nil t)
725 ; (progn
726 ; (tar-copy (if (file-directory-p newname)
727 ; (expand-file-name
728 ; (file-name-nondirectory back) newname)
729 ; newname))
730 ; (setq copied t))
731 ; (error "%s not found in tar file %s" back front))))))
732 ; (unless copied
733 ; (copy-file file newname ok-if-already-exists keep-date))))
735 ; (defun eshell-file-attributes (filename)
736 ; "Return a list of attributes of file FILENAME.
737 ; See the documentation for `file-attributes'."
738 ; (let (result)
739 ; (when (string-match "\\`\\([^:]+\\):\\(.*\\)\\'" filename)
740 ; (let ((front (match-string 1 filename))
741 ; (back (match-string 2 filename))
742 ; buffer)
743 ; (when (and front (string-match eshell-tar-regexp front)
744 ; (setq buffer (find-file-noselect front)))
745 ; (with-current-buffer buffer
746 ; (goto-char (point-min))
747 ; (when (re-search-forward (concat " " (regexp-quote back)
748 ; "\\s-*$") nil t)
749 ; (let* ((descrip (tar-current-descriptor))
750 ; (tokens (tar-desc-tokens descrip)))
751 ; (setq result
752 ; (list
753 ; (cond
754 ; ((eq (tar-header-link-type tokens) 5)
755 ; t)
756 ; ((eq (tar-header-link-type tokens) t)
757 ; (tar-header-link-name tokens)))
759 ; (tar-header-uid tokens)
760 ; (tar-header-gid tokens)
761 ; (tar-header-date tokens)
762 ; (tar-header-date tokens)
763 ; (tar-header-date tokens)
764 ; (tar-header-size tokens)
765 ; (concat
766 ; (cond
767 ; ((eq (tar-header-link-type tokens) 5) "d")
768 ; ((eq (tar-header-link-type tokens) t) "l")
769 ; (t "-"))
770 ; (tar-grind-file-mode (tar-header-mode tokens)
771 ; (make-string 9 ? ) 0))
772 ; nil nil nil))))))))
773 ; (or result
774 ; (file-attributes filename))))
776 (provide 'esh-util)
778 ;;; esh-util.el ends here