Commit | Line | Data |
---|---|---|
60370d40 | 1 | ;;; esh-util.el --- general utilities |
26b4dc84 | 2 | |
8c9f73a2 | 3 | ;; Copyright (C) 1999, 2000, 2001 Free Software Foundation |
26b4dc84 | 4 | |
7de5b421 GM |
5 | ;; Author: John Wiegley <johnw@gnu.org> |
6 | ||
26b4dc84 GM |
7 | ;; This file is part of GNU Emacs. |
8 | ||
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. | |
13 | ||
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. | |
18 | ||
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. | |
23 | ||
24 | (provide 'esh-util) | |
25 | ||
26 | (eval-when-compile (require 'esh-maint)) | |
27 | ||
28 | (defgroup eshell-util nil | |
29 | "This is general utility code, meant for use by Eshell itself." | |
30 | :tag "General utilities" | |
31 | :group 'eshell) | |
32 | ||
33 | ;;; Commentary: | |
34 | ||
35 | (require 'pp) | |
36 | ||
37 | ;;; User Variables: | |
38 | ||
dace60cf JW |
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) | |
46 | ||
26b4dc84 GM |
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) | |
51 | ||
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) | |
56 | ||
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) | |
61 | ||
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) | |
67 | ||
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) | |
72 | ||
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) | |
77 | ||
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) | |
83 | ||
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 | |
90d94608 JW |
87 | `eshell-number-regexp'. |
88 | ||
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: | |
93 | ||
94 | (put 'find-file 'eshell-no-numeric-conversions t) | |
95 | ||
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'." | |
26b4dc84 GM |
99 | :type 'boolean |
100 | :group 'eshell-util) | |
101 | ||
cee38ad6 | 102 | (defcustom eshell-number-regexp "-?\\([0-9]*\\.\\)?[0-9]+\\(e[-0-9.]+\\)?" |
26b4dc84 GM |
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) | |
109 | ||
8c6b1d83 JW |
110 | (defcustom eshell-ange-ls-uids nil |
111 | "*List of user/host/id strings, used to determine remote ownership." | |
219227ea JW |
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))))) | |
8c6b1d83 JW |
117 | :group 'eshell-util) |
118 | ||
26b4dc84 GM |
119 | ;;; Internal Variables: |
120 | ||
121 | (defvar eshell-group-names nil | |
122 | "A cache to hold the names of groups.") | |
123 | ||
124 | (defvar eshell-group-timestamp nil | |
125 | "A timestamp of when the group file was read.") | |
126 | ||
127 | (defvar eshell-user-names nil | |
128 | "A cache to hold the names of users.") | |
129 | ||
130 | (defvar eshell-user-timestamp nil | |
131 | "A timestamp of when the user file was read.") | |
132 | ||
133 | (defvar eshell-host-names nil | |
134 | "A cache the names of frequently accessed hosts.") | |
135 | ||
136 | (defvar eshell-host-timestamp nil | |
137 | "A timestamp of when the hosts file was read.") | |
138 | ||
139 | ;;; Functions: | |
140 | ||
141 | (defsubst eshell-under-xemacs-p () | |
142 | "Return non-nil if we are running under XEmacs." | |
143 | (boundp 'xemacs-logo)) | |
144 | ||
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))) | |
148 | ||
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)) | |
156 | ||
157 | (put 'eshell-condition-case 'lisp-indent-function 2) | |
158 | ||
159 | (defmacro eshell-deftest (module name label &rest forms) | |
160 | (if (and (fboundp 'cl-compiling-file) (cl-compiling-file)) | |
161 | nil | |
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))))))))) | |
168 | ||
169 | (put 'eshell-deftest 'lisp-indent-function 2) | |
170 | ||
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))))))) | |
217 | ||
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)))))) | |
235 | ||
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." | |
a9304a86 | 239 | (let* ((a (copy-sequence l)) |
26b4dc84 GM |
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))) | |
247 | a)) | |
248 | ||
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) directory-sep-char) | |
257 | (eq (aref path 1) directory-sep-char)) | |
258 | (setq i 2)) | |
259 | (while (< i len) | |
260 | (if (and (eq (aref path i) directory-sep-char) | |
261 | (not (get-text-property i 'escaped path))) | |
262 | (setq parts (cons (if (= li i) | |
263 | (char-to-string directory-sep-char) | |
264 | (substring path li (1+ i))) parts) | |
265 | li (1+ i))) | |
266 | (setq i (1+ i))) | |
267 | (if (< li i) | |
268 | (setq parts (cons (substring path li i) parts))) | |
269 | (if (and (eshell-under-windows-p) | |
270 | (string-match "\\`[A-Za-z]:\\'" (car (last parts)))) | |
271 | (setcar (last parts) | |
272 | (concat (car (last parts)) | |
273 | (char-to-string directory-sep-char)))) | |
274 | (nreverse parts))) | |
275 | ||
276 | (defun eshell-to-flat-string (value) | |
277 | "Make value a string. If separated by newlines change them to spaces." | |
278 | (let ((text (eshell-stringify value))) | |
279 | (if (string-match "\n+\\'" text) | |
280 | (setq text (replace-match "" t t text))) | |
281 | (while (string-match "\n+" text) | |
282 | (setq text (replace-match " " t t text))) | |
283 | text)) | |
284 | ||
285 | (defmacro eshell-for (for-var for-list &rest forms) | |
286 | "Iterate through a list" | |
287 | `(let ((list-iter ,for-list)) | |
288 | (while list-iter | |
289 | (let ((,for-var (car list-iter))) | |
290 | ,@forms) | |
291 | (setq list-iter (cdr list-iter))))) | |
292 | ||
293 | (put 'eshell-for 'lisp-indent-function 2) | |
294 | ||
ca7aae91 | 295 | (defun eshell-flatten-list (args) |
26b4dc84 GM |
296 | "Flatten any lists within ARGS, so that there are no sublists." |
297 | (let ((new-list (list t))) | |
298 | (eshell-for a args | |
299 | (if (and (listp a) | |
300 | (listp (cdr a))) | |
301 | (nconc new-list (eshell-flatten-list a)) | |
302 | (nconc new-list (list a)))) | |
303 | (cdr new-list))) | |
304 | ||
305 | (defun eshell-uniqify-list (l) | |
306 | "Remove occurring multiples in L. You probably want to sort first." | |
307 | (let ((m l)) | |
308 | (while m | |
309 | (while (and (cdr m) | |
310 | (string= (car m) | |
311 | (cadr m))) | |
312 | (setcdr m (cddr m))) | |
313 | (setq m (cdr m)))) | |
314 | l) | |
315 | ||
316 | (defun eshell-stringify (object) | |
317 | "Convert OBJECT into a string value." | |
318 | (cond | |
319 | ((stringp object) object) | |
320 | ((and (listp object) | |
321 | (not (eq object nil))) | |
322 | (let ((string (pp-to-string object))) | |
323 | (substring string 0 (1- (length string))))) | |
324 | ((numberp object) | |
325 | (number-to-string object)) | |
326 | (t | |
dace60cf JW |
327 | (unless (and (eq object t) |
328 | (not eshell-stringify-t)) | |
329 | (pp-to-string object))))) | |
26b4dc84 GM |
330 | |
331 | (defsubst eshell-stringify-list (args) | |
332 | "Convert each element of ARGS into a string value." | |
333 | (mapcar 'eshell-stringify args)) | |
334 | ||
335 | (defsubst eshell-flatten-and-stringify (&rest args) | |
336 | "Flatten and stringify all of the ARGS into a single string." | |
337 | (mapconcat 'eshell-stringify (eshell-flatten-list args) " ")) | |
338 | ||
339 | ;; the next two are from GNUS, and really should be made part of Emacs | |
340 | ;; some day | |
341 | (defsubst eshell-time-less-p (t1 t2) | |
342 | "Say whether time T1 is less than time T2." | |
343 | (or (< (car t1) (car t2)) | |
344 | (and (= (car t1) (car t2)) | |
345 | (< (nth 1 t1) (nth 1 t2))))) | |
346 | ||
347 | (defsubst eshell-time-to-seconds (time) | |
348 | "Convert TIME to a floating point number." | |
349 | (+ (* (car time) 65536.0) | |
350 | (cadr time) | |
351 | (/ (or (car (cdr (cdr time))) 0) 1000000.0))) | |
352 | ||
353 | (defsubst eshell-directory-files (regexp &optional directory) | |
354 | "Return a list of files in the given DIRECTORY matching REGEXP." | |
355 | (directory-files (or directory default-directory) | |
356 | directory regexp)) | |
357 | ||
358 | (defun eshell-regexp-arg (prompt) | |
359 | "Return list of regexp and prefix arg using PROMPT." | |
360 | (let* (;; Don't clobber this. | |
361 | (last-command last-command) | |
362 | (regexp (read-from-minibuffer prompt nil nil nil | |
363 | 'minibuffer-history-search-history))) | |
364 | (list (if (string-equal regexp "") | |
365 | (setcar minibuffer-history-search-history | |
366 | (nth 1 minibuffer-history-search-history)) | |
367 | regexp) | |
368 | (prefix-numeric-value current-prefix-arg)))) | |
369 | ||
370 | (defun eshell-printable-size (filesize &optional human-readable | |
371 | block-size use-colors) | |
372 | "Return a printable FILESIZE." | |
373 | (let ((size (float (or filesize 0)))) | |
374 | (if human-readable | |
375 | (if (< size human-readable) | |
376 | (if (= (round size) 0) | |
377 | "0" | |
378 | (if block-size | |
379 | "1.0k" | |
380 | (format "%.0f" size))) | |
381 | (setq size (/ size human-readable)) | |
382 | (if (< size human-readable) | |
383 | (if (<= size 9.94) | |
384 | (format "%.1fk" size) | |
385 | (format "%.0fk" size)) | |
386 | (setq size (/ size human-readable)) | |
387 | (if (< size human-readable) | |
388 | (let ((str (if (<= size 9.94) | |
389 | (format "%.1fM" size) | |
390 | (format "%.0fM" size)))) | |
391 | (if use-colors | |
392 | (put-text-property 0 (length str) | |
393 | 'face 'bold str)) | |
394 | str) | |
395 | (setq size (/ size human-readable)) | |
396 | (if (< size human-readable) | |
397 | (let ((str (if (<= size 9.94) | |
398 | (format "%.1fG" size) | |
399 | (format "%.0fG" size)))) | |
400 | (if use-colors | |
401 | (put-text-property 0 (length str) | |
402 | 'face 'bold-italic str)) | |
403 | str))))) | |
404 | (if block-size | |
405 | (setq size (/ size block-size))) | |
406 | (format "%.0f" size)))) | |
407 | ||
408 | (defun eshell-winnow-list (entries exclude &optional predicates) | |
409 | "Pare down the ENTRIES list using the EXCLUDE regexp, and PREDICATES. | |
410 | The original list is not affected. If the result is only one element | |
411 | long, it will be returned itself, rather than returning a one-element | |
412 | list." | |
413 | (let ((flist (list t)) | |
414 | valid p listified) | |
415 | (unless (listp entries) | |
416 | (setq entries (list entries) | |
417 | listified t)) | |
418 | (eshell-for entry entries | |
419 | (unless (and exclude (string-match exclude entry)) | |
420 | (setq p predicates valid (null p)) | |
421 | (while p | |
422 | (if (funcall (car p) entry) | |
423 | (setq valid t) | |
424 | (setq p nil valid nil)) | |
425 | (setq p (cdr p))) | |
426 | (when valid | |
427 | (nconc flist (list entry))))) | |
428 | (if listified | |
429 | (cadr flist) | |
430 | (cdr flist)))) | |
431 | ||
432 | (defsubst eshell-redisplay () | |
433 | "Allow Emacs to redisplay buffers." | |
434 | ;; for some strange reason, Emacs 21 is prone to trigger an | |
435 | ;; "args out of range" error in `sit-for', if this function | |
436 | ;; runs while point is in the minibuffer and the users attempt | |
437 | ;; to use completion. Don't ask me. | |
438 | (ignore-errors (sit-for 0 0))) | |
439 | ||
440 | (defun eshell-read-passwd-file (file) | |
441 | "Return an alist correlating gids to group names in FILE." | |
442 | (let (names) | |
443 | (when (file-readable-p file) | |
444 | (with-temp-buffer | |
445 | (insert-file-contents file) | |
446 | (goto-char (point-min)) | |
447 | (while (not (eobp)) | |
448 | (let* ((fields | |
449 | (split-string (buffer-substring | |
450 | (point) (progn (end-of-line) | |
451 | (point))) ":"))) | |
b4bd214e JW |
452 | (if (and (and fields (nth 0 fields) (nth 2 fields)) |
453 | (not (assq (string-to-int (nth 2 fields)) names))) | |
26b4dc84 GM |
454 | (setq names (cons (cons (string-to-int (nth 2 fields)) |
455 | (nth 0 fields)) | |
456 | names)))) | |
457 | (forward-line)))) | |
458 | names)) | |
459 | ||
460 | (defun eshell-read-passwd (file result-var timestamp-var) | |
461 | "Read the contents of /etc/passwd for user names." | |
462 | (if (or (not (symbol-value result-var)) | |
463 | (not (symbol-value timestamp-var)) | |
464 | (eshell-time-less-p | |
465 | (symbol-value timestamp-var) | |
466 | (nth 5 (file-attributes file)))) | |
467 | (progn | |
468 | (set result-var (eshell-read-passwd-file file)) | |
469 | (set timestamp-var (current-time)))) | |
470 | (symbol-value result-var)) | |
471 | ||
472 | (defun eshell-read-group-names () | |
473 | "Read the contents of /etc/group for group names." | |
474 | (if eshell-group-file | |
475 | (eshell-read-passwd eshell-group-file 'eshell-group-names | |
476 | 'eshell-group-timestamp))) | |
477 | ||
478 | (defsubst eshell-group-id (name) | |
479 | "Return the user id for user NAME." | |
480 | (car (rassoc name (eshell-read-group-names)))) | |
481 | ||
482 | (defsubst eshell-group-name (gid) | |
483 | "Return the group name for the given GID." | |
484 | (cdr (assoc gid (eshell-read-group-names)))) | |
485 | ||
486 | (defun eshell-read-user-names () | |
487 | "Read the contents of /etc/passwd for user names." | |
488 | (if eshell-passwd-file | |
489 | (eshell-read-passwd eshell-passwd-file 'eshell-user-names | |
490 | 'eshell-user-timestamp))) | |
491 | ||
492 | (defsubst eshell-user-id (name) | |
493 | "Return the user id for user NAME." | |
494 | (car (rassoc name (eshell-read-user-names)))) | |
495 | ||
496 | (defalias 'eshell-user-name 'user-login-name) | |
497 | ||
498 | (defun eshell-read-hosts-file (filename) | |
499 | "Read in the hosts from the /etc/hosts file." | |
500 | (let (hosts) | |
501 | (with-temp-buffer | |
502 | (insert-file-contents eshell-hosts-file) | |
503 | (goto-char (point-min)) | |
504 | (while (re-search-forward | |
505 | "^\\(\\S-+\\)\\s-+\\(\\S-+\\)\\(\\s-*\\(\\S-+\\)\\)?" nil t) | |
506 | (if (match-string 1) | |
507 | (add-to-list 'hosts (match-string 1))) | |
508 | (if (match-string 2) | |
509 | (add-to-list 'hosts (match-string 2))) | |
510 | (if (match-string 4) | |
511 | (add-to-list 'hosts (match-string 4))))) | |
512 | (sort hosts 'string-lessp))) | |
513 | ||
514 | (defun eshell-read-hosts (file result-var timestamp-var) | |
515 | "Read the contents of /etc/passwd for user names." | |
516 | (if (or (not (symbol-value result-var)) | |
517 | (not (symbol-value timestamp-var)) | |
518 | (eshell-time-less-p | |
519 | (symbol-value timestamp-var) | |
520 | (nth 5 (file-attributes file)))) | |
521 | (progn | |
522 | (set result-var (eshell-read-hosts-file file)) | |
523 | (set timestamp-var (current-time)))) | |
524 | (symbol-value result-var)) | |
525 | ||
526 | (defun eshell-read-host-names () | |
527 | "Read the contents of /etc/hosts for host names." | |
528 | (if eshell-hosts-file | |
529 | (eshell-read-hosts eshell-hosts-file 'eshell-host-names | |
530 | 'eshell-host-timestamp))) | |
531 | ||
532 | (unless (fboundp 'line-end-position) | |
533 | (defsubst line-end-position (&optional N) | |
534 | (save-excursion (end-of-line N) (point)))) | |
535 | ||
536 | (unless (fboundp 'line-beginning-position) | |
537 | (defsubst line-beginning-position (&optional N) | |
538 | (save-excursion (beginning-of-line N) (point)))) | |
539 | ||
540 | (unless (fboundp 'subst-char-in-string) | |
541 | (defun subst-char-in-string (fromchar tochar string &optional inplace) | |
542 | "Replace FROMCHAR with TOCHAR in STRING each time it occurs. | |
543 | Unless optional argument INPLACE is non-nil, return a new string." | |
544 | (let ((i (length string)) | |
545 | (newstr (if inplace string (copy-sequence string)))) | |
546 | (while (> i 0) | |
547 | (setq i (1- i)) | |
548 | (if (eq (aref newstr i) fromchar) | |
549 | (aset newstr i tochar))) | |
550 | newstr))) | |
551 | ||
552 | (defsubst eshell-copy-environment () | |
553 | "Return an unrelated copy of `process-environment'." | |
554 | (mapcar 'concat process-environment)) | |
555 | ||
556 | (defun eshell-subgroups (groupsym) | |
557 | "Return all of the subgroups of GROUPSYM." | |
558 | (let ((subgroups (get groupsym 'custom-group)) | |
559 | (subg (list t))) | |
560 | (while subgroups | |
561 | (if (eq (cadr (car subgroups)) 'custom-group) | |
562 | (nconc subg (list (caar subgroups)))) | |
563 | (setq subgroups (cdr subgroups))) | |
564 | (cdr subg))) | |
565 | ||
566 | (defmacro eshell-with-file-modes (modes &rest forms) | |
567 | "Evaluate, with file-modes set to MODES, the given FORMS." | |
568 | `(let ((modes (default-file-modes))) | |
569 | (set-default-file-modes ,modes) | |
570 | (unwind-protect | |
571 | (progn ,@forms) | |
572 | (set-default-file-modes modes)))) | |
573 | ||
574 | (defmacro eshell-with-private-file-modes (&rest forms) | |
575 | "Evaluate FORMS with private file modes set." | |
576 | `(eshell-with-file-modes ,eshell-private-file-modes ,@forms)) | |
577 | ||
578 | (defsubst eshell-make-private-directory (dir &optional parents) | |
579 | "Make DIR with file-modes set to `eshell-private-directory-modes'." | |
580 | (eshell-with-file-modes eshell-private-directory-modes | |
581 | (make-directory dir parents))) | |
582 | ||
583 | (defsubst eshell-substring (string sublen) | |
584 | "Return the beginning of STRING, up to SUBLEN bytes." | |
585 | (if string | |
586 | (if (> (length string) sublen) | |
587 | (substring string 0 sublen) | |
588 | string))) | |
589 | ||
590 | (unless (fboundp 'directory-files-and-attributes) | |
591 | (defun directory-files-and-attributes (dir &optional full match nosort) | |
592 | (documentation 'directory-files) | |
8c6b1d83 | 593 | (let ((dir (expand-file-name dir)) ange-cache) |
26b4dc84 GM |
594 | (mapcar |
595 | (function | |
596 | (lambda (file) | |
8c6b1d83 | 597 | (cons file (eshell-file-attributes (expand-file-name file dir))))) |
26b4dc84 GM |
598 | (directory-files dir full match nosort))))) |
599 | ||
8c6b1d83 JW |
600 | (eval-when-compile |
601 | (defvar ange-cache)) | |
602 | ||
26b4dc84 GM |
603 | (defun eshell-directory-files-and-attributes (dir &optional full match nosort) |
604 | "Make sure to use the handler for `directory-file-and-attributes'." | |
8c6b1d83 JW |
605 | (let* ((dir (expand-file-name dir)) |
606 | (dfh (find-file-name-handler dir 'directory-files))) | |
26b4dc84 GM |
607 | (if (not dfh) |
608 | (directory-files-and-attributes dir full match nosort) | |
8c6b1d83 JW |
609 | (let ((files (funcall dfh 'directory-files dir full match nosort)) |
610 | (fah (find-file-name-handler dir 'file-attributes))) | |
26b4dc84 GM |
611 | (mapcar |
612 | (function | |
613 | (lambda (file) | |
8c6b1d83 JW |
614 | (cons file (if fah |
615 | (eshell-file-attributes | |
616 | (expand-file-name file dir)) | |
617 | (file-attributes (expand-file-name file dir)))))) | |
26b4dc84 GM |
618 | files))))) |
619 | ||
8c6b1d83 JW |
620 | (defun eshell-current-ange-uids () |
621 | (if (string-match "/\\([^@]+\\)@\\([^:]+\\):" default-directory) | |
622 | (let* ((host (match-string 2 default-directory)) | |
623 | (user (match-string 1 default-directory)) | |
624 | (host-users (assoc host eshell-ange-ls-uids))) | |
625 | (when host-users | |
626 | (setq host-users (cdr host-users)) | |
627 | (cdr (assoc user host-users)))))) | |
628 | ||
629 | ;; Add an autoload for parse-time-string | |
630 | (if (and (not (fboundp 'parse-time-string)) | |
631 | (locate-library "parse-time")) | |
632 | (autoload 'parse-time-string "parse-time")) | |
633 | ||
57a24508 | 634 | (eval-when-compile |
dace60cf | 635 | (load "ange-ftp" t)) |
57a24508 | 636 | |
8c6b1d83 JW |
637 | (defun eshell-parse-ange-ls (dir) |
638 | (let (entry) | |
639 | (with-temp-buffer | |
640 | (insert (ange-ftp-ls dir "-la" nil)) | |
641 | (goto-char (point-min)) | |
642 | (if (looking-at "^total [0-9]+$") | |
643 | (forward-line 1)) | |
644 | ;; Some systems put in a blank line here. | |
645 | (if (eolp) (forward-line 1)) | |
646 | (while (looking-at | |
647 | `,(concat "\\([dlscb-][rwxst-]+\\)" | |
648 | "\\s-*" "\\([0-9]+\\)" "\\s-+" | |
649 | "\\(\\S-+\\)" "\\s-+" | |
650 | "\\(\\S-+\\)" "\\s-+" | |
651 | "\\([0-9]+\\)" "\\s-+" "\\(.*\\)")) | |
652 | (let* ((perms (match-string 1)) | |
653 | (links (string-to-number (match-string 2))) | |
654 | (user (match-string 3)) | |
655 | (group (match-string 4)) | |
656 | (size (string-to-number (match-string 5))) | |
657 | (mtime | |
658 | (if (fboundp 'parse-time-string) | |
659 | (let ((moment (parse-time-string | |
660 | (match-string 6)))) | |
661 | (if (nth 0 moment) | |
662 | (setcar (nthcdr 5 moment) | |
663 | (nth 5 (decode-time (current-time)))) | |
664 | (setcar (nthcdr 0 moment) 0) | |
665 | (setcar (nthcdr 1 moment) 0) | |
666 | (setcar (nthcdr 2 moment) 0)) | |
667 | (apply 'encode-time moment)) | |
668 | (ange-ftp-file-modtime (expand-file-name name dir)))) | |
669 | (name (ange-ftp-parse-filename)) | |
670 | symlink) | |
671 | (if (string-match "\\(.+\\) -> \\(.+\\)" name) | |
672 | (setq symlink (match-string 2 name) | |
673 | name (match-string 1 name))) | |
674 | (setq entry | |
675 | (cons | |
676 | (cons name | |
677 | (list (if (eq (aref perms 0) ?d) | |
678 | t | |
679 | symlink) | |
680 | links user group | |
681 | nil mtime nil | |
682 | size perms nil nil)) entry))) | |
683 | (forward-line))) | |
684 | entry)) | |
685 | ||
686 | (defun eshell-file-attributes (file) | |
687 | "Return the attributes of FILE, playing tricks if it's over ange-ftp." | |
688 | (let* ((file (expand-file-name file)) | |
689 | (handler (find-file-name-handler file 'file-attributes)) | |
690 | entry) | |
691 | (if (not handler) | |
692 | (file-attributes file) | |
693 | (if (eq (find-file-name-handler (file-name-directory file) | |
694 | 'directory-files) | |
695 | 'ange-ftp-hook-function) | |
696 | (let ((base (file-name-nondirectory file)) | |
697 | (dir (file-name-directory file))) | |
698 | (if (boundp 'ange-cache) | |
699 | (setq entry (cdr (assoc base (cdr (assoc dir ange-cache)))))) | |
700 | (unless entry | |
701 | (setq entry (eshell-parse-ange-ls dir)) | |
702 | (if (boundp 'ange-cache) | |
703 | (setq ange-cache | |
704 | (cons (cons dir entry) | |
705 | ange-cache))) | |
706 | (if entry | |
707 | (let ((fentry (assoc base (cdr entry)))) | |
708 | (if fentry | |
709 | (setq entry (cdr fentry)) | |
710 | (setq entry nil))))))) | |
711 | (or entry (funcall handler 'file-attributes file))))) | |
712 | ||
efbddb8e | 713 | (defalias 'eshell-copy-tree 'copy-tree) |
26b4dc84 | 714 | |
b4bd214e JW |
715 | (defsubst eshell-processp (proc) |
716 | "If the `processp' function does not exist, PROC is not a process." | |
717 | (and (fboundp 'processp) (processp proc))) | |
718 | ||
26b4dc84 GM |
719 | ; (defun eshell-copy-file |
720 | ; (file newname &optional ok-if-already-exists keep-date) | |
721 | ; "Copy FILE to NEWNAME. See docs for `copy-file'." | |
722 | ; (let (copied) | |
723 | ; (if (string-match "\\`\\([^:]+\\):\\(.*\\)" file) | |
724 | ; (let ((front (match-string 1 file)) | |
725 | ; (back (match-string 2 file)) | |
726 | ; buffer) | |
727 | ; (if (and front (string-match eshell-tar-regexp front) | |
728 | ; (setq buffer (find-file-noselect front))) | |
729 | ; (with-current-buffer buffer | |
730 | ; (goto-char (point-min)) | |
731 | ; (if (re-search-forward (concat " " (regexp-quote back) | |
732 | ; "$") nil t) | |
733 | ; (progn | |
734 | ; (tar-copy (if (file-directory-p newname) | |
735 | ; (expand-file-name | |
736 | ; (file-name-nondirectory back) newname) | |
737 | ; newname)) | |
738 | ; (setq copied t)) | |
739 | ; (error "%s not found in tar file %s" back front)))))) | |
740 | ; (unless copied | |
741 | ; (copy-file file newname ok-if-already-exists keep-date)))) | |
742 | ||
743 | ; (defun eshell-file-attributes (filename) | |
744 | ; "Return a list of attributes of file FILENAME. | |
745 | ; See the documentation for `file-attributes'." | |
746 | ; (let (result) | |
747 | ; (when (string-match "\\`\\([^:]+\\):\\(.*\\)\\'" filename) | |
748 | ; (let ((front (match-string 1 filename)) | |
749 | ; (back (match-string 2 filename)) | |
750 | ; buffer) | |
751 | ; (when (and front (string-match eshell-tar-regexp front) | |
752 | ; (setq buffer (find-file-noselect front))) | |
753 | ; (with-current-buffer buffer | |
754 | ; (goto-char (point-min)) | |
755 | ; (when (re-search-forward (concat " " (regexp-quote back) | |
756 | ; "\\s-*$") nil t) | |
757 | ; (let* ((descrip (tar-current-descriptor)) | |
758 | ; (tokens (tar-desc-tokens descrip))) | |
759 | ; (setq result | |
760 | ; (list | |
761 | ; (cond | |
762 | ; ((eq (tar-header-link-type tokens) 5) | |
763 | ; t) | |
764 | ; ((eq (tar-header-link-type tokens) t) | |
765 | ; (tar-header-link-name tokens))) | |
766 | ; 1 | |
767 | ; (tar-header-uid tokens) | |
768 | ; (tar-header-gid tokens) | |
769 | ; (tar-header-date tokens) | |
770 | ; (tar-header-date tokens) | |
771 | ; (tar-header-date tokens) | |
772 | ; (tar-header-size tokens) | |
773 | ; (concat | |
774 | ; (cond | |
775 | ; ((eq (tar-header-link-type tokens) 5) "d") | |
776 | ; ((eq (tar-header-link-type tokens) t) "l") | |
777 | ; (t "-")) | |
778 | ; (tar-grind-file-mode (tar-header-mode tokens) | |
779 | ; (make-string 9 ? ) 0)) | |
780 | ; nil nil nil)))))))) | |
781 | ; (or result | |
782 | ; (file-attributes filename)))) | |
783 | ||
784 | ;;; Code: | |
785 | ||
786 | ;;; esh-util.el ends here |