Mercurial > emacs
annotate lisp/eshell/em-unix.el @ 31363:6865e0a3ad1d
*** empty log message ***
author | Dave Love <fx@gnu.org> |
---|---|
date | Mon, 04 Sep 2000 09:41:48 +0000 |
parents | 3099993cba0f |
children | aab90b31807c |
rev | line source |
---|---|
29876 | 1 ;;; em-unix --- UNIX command aliases |
2 | |
29934
34b1ab9d583d
Change spelling of the Free Software Foundation.
Gerd Moellmann <gerd@gnu.org>
parents:
29876
diff
changeset
|
3 ;; Copyright (C) 1999, 2000 Free Software Foundation |
29876 | 4 |
5 ;; This file is part of GNU Emacs. | |
6 | |
7 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
8 ;; it under the terms of the GNU General Public License as published by | |
9 ;; the Free Software Foundation; either version 2, or (at your option) | |
10 ;; any later version. | |
11 | |
12 ;; GNU Emacs is distributed in the hope that it will be useful, | |
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 ;; GNU General Public License for more details. | |
16 | |
17 ;; You should have received a copy of the GNU General Public License | |
18 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
20 ;; Boston, MA 02111-1307, USA. | |
21 | |
22 (provide 'em-unix) | |
23 | |
24 (eval-when-compile (require 'esh-maint)) | |
25 | |
26 (defgroup eshell-unix nil | |
27 "This module defines many of the more common UNIX utilities as | |
28 aliases implemented in Lisp. These include mv, ln, cp, rm, etc. If | |
29 the user passes arguments which are too complex, or are unrecognized | |
30 by the Lisp variant, the external version will be called (if | |
31 available). The only reason not to use them would be because they are | |
32 usually much slower. But in several cases their tight integration | |
33 with Eshell makes them more versatile than their traditional cousins | |
34 \(such as being able to use `kill' to kill Eshell background processes | |
35 by name)." | |
36 :tag "UNIX commands in Lisp" | |
37 :group 'eshell-module) | |
38 | |
39 ;;; Commentary: | |
40 | |
41 ;; This file contains implementations of several UNIX command in Emacs | |
42 ;; Lisp, for several reasons: | |
43 ;; | |
44 ;; 1) it makes them available on all platforms where the Lisp | |
45 ;; functions used are available | |
46 ;; | |
47 ;; 2) it makes their functionality accessible and modified by the | |
48 ;; Lisp programmer. | |
49 ;; | |
50 ;; 3) it allows Eshell to refrain from having to invoke external | |
51 ;; processes for common operations. | |
52 | |
53 (defcustom eshell-unix-load-hook '(eshell-unix-initialize) | |
54 "*A list of functions to run when `eshell-unix' is loaded." | |
55 :type 'hook | |
56 :group 'eshell-unix) | |
57 | |
58 (defcustom eshell-plain-grep-behavior nil | |
59 "*If non-nil, standalone \"grep\" commands will behave normally. | |
60 Standalone in this context means not redirected, and not on the | |
61 receiving side of a command pipeline." | |
62 :type 'boolean | |
63 :group 'eshell-unix) | |
64 | |
65 (defcustom eshell-no-grep-available (not (eshell-search-path "grep")) | |
66 "*If non-nil, no grep is available on the current machine." | |
67 :type 'boolean | |
68 :group 'eshell-unix) | |
69 | |
70 (defcustom eshell-plain-diff-behavior nil | |
71 "*If non-nil, standalone \"diff\" commands will behave normally. | |
72 Standalone in this context means not redirected, and not on the | |
73 receiving side of a command pipeline." | |
74 :type 'boolean | |
75 :group 'eshell-unix) | |
76 | |
77 (defcustom eshell-plain-locate-behavior nil | |
78 "*If non-nil, standalone \"locate\" commands will behave normally. | |
79 Standalone in this context means not redirected, and not on the | |
80 receiving side of a command pipeline." | |
81 :type 'boolean | |
82 :group 'eshell-unix) | |
83 | |
84 (defcustom eshell-rm-removes-directories nil | |
85 "*If non-nil, `rm' will remove directory entries. | |
86 Otherwise, `rmdir' is required." | |
87 :type 'boolean | |
88 :group 'eshell-unix) | |
89 | |
90 (defcustom eshell-rm-interactive-query (= (user-uid) 0) | |
91 "*If non-nil, `rm' will query before removing anything." | |
92 :type 'boolean | |
93 :group 'eshell-unix) | |
94 | |
95 (defcustom eshell-mv-interactive-query (= (user-uid) 0) | |
96 "*If non-nil, `mv' will query before overwriting anything." | |
97 :type 'boolean | |
98 :group 'eshell-unix) | |
99 | |
100 (defcustom eshell-mv-overwrite-files t | |
101 "*If non-nil, `mv' will overwrite files without warning." | |
102 :type 'boolean | |
103 :group 'eshell-unix) | |
104 | |
105 (defcustom eshell-cp-interactive-query (= (user-uid) 0) | |
106 "*If non-nil, `cp' will query before overwriting anything." | |
107 :type 'boolean | |
108 :group 'eshell-unix) | |
109 | |
110 (defcustom eshell-cp-overwrite-files t | |
111 "*If non-nil, `cp' will overwrite files without warning." | |
112 :type 'boolean | |
113 :group 'eshell-unix) | |
114 | |
115 (defcustom eshell-ln-interactive-query (= (user-uid) 0) | |
116 "*If non-nil, `ln' will query before overwriting anything." | |
117 :type 'boolean | |
118 :group 'eshell-unix) | |
119 | |
31241 | 120 (defcustom eshell-ln-overwrite-files nil |
29876 | 121 "*If non-nil, `ln' will overwrite files without warning." |
122 :type 'boolean | |
123 :group 'eshell-unix) | |
124 | |
125 (require 'esh-opt) | |
126 | |
127 ;;; Functions: | |
128 | |
129 (defun eshell-unix-initialize () | |
130 "Initialize the UNIX support/emulation code." | |
131 (make-local-hook 'eshell-post-command-hook) | |
132 (when (eshell-using-module 'eshell-cmpl) | |
133 (make-local-hook 'pcomplete-try-first-hook) | |
134 (add-hook 'pcomplete-try-first-hook | |
135 'eshell-complete-host-reference nil t))) | |
136 | |
137 (defalias 'eshell/date 'current-time-string) | |
138 (defalias 'eshell/basename 'file-name-nondirectory) | |
139 (defalias 'eshell/dirname 'file-name-directory) | |
140 | |
141 (eval-when-compile | |
142 (defvar interactive) | |
143 (defvar preview) | |
144 (defvar recursive) | |
145 (defvar verbose)) | |
146 | |
147 (defun eshell/man (&rest args) | |
148 "Invoke man, flattening the arguments appropriately." | |
149 (funcall 'man (apply 'eshell-flatten-and-stringify args))) | |
150 | |
151 (defun eshell-remove-entries (path files &optional top-level) | |
152 (while files | |
153 (if (string-match "\\`\\.\\.?\\'" | |
154 (file-name-nondirectory (car files))) | |
155 (if top-level | |
156 (eshell-error "rm: cannot remove `.' or `..'\n")) | |
157 (if (and (file-directory-p (car files)) | |
158 (not (file-symlink-p (car files)))) | |
159 (let ((dir (file-name-as-directory (car files)))) | |
160 (eshell-remove-entries dir | |
161 (mapcar | |
162 (function | |
163 (lambda (file) | |
164 (concat dir file))) | |
165 (directory-files dir))) | |
166 (if verbose | |
167 (eshell-printn (format "rm: removing directory `%s'" | |
168 (car files)))) | |
169 (unless | |
170 (or preview | |
171 (and interactive | |
172 (not (y-or-n-p | |
173 (format "rm: remove directory `%s'? " | |
174 (car files)))))) | |
175 (eshell-funcalln 'delete-directory (car files)))) | |
176 (if verbose | |
177 (eshell-printn (format "rm: removing file `%s'" | |
178 (car files)))) | |
179 (unless (or preview | |
180 (and interactive | |
181 (not (y-or-n-p | |
182 (format "rm: remove `%s'? " | |
183 (car files)))))) | |
184 (eshell-funcalln 'delete-file (car files))))) | |
185 (setq files (cdr files)))) | |
186 | |
187 (defun eshell/rm (&rest args) | |
188 "Implementation of rm in Lisp. | |
189 This is implemented to call either `delete-file', `kill-buffer', | |
190 `kill-process', or `unintern', depending on the nature of the | |
191 argument." | |
192 (setq args (eshell-flatten-list args)) | |
193 (eshell-eval-using-options | |
194 "rm" args | |
195 '((?h "help" nil nil "show this usage screen") | |
196 (?f "force" nil force-removal "force removal") | |
197 (?i "interactive" nil interactive "prompt before any removal") | |
198 (?n "preview" nil preview "don't change anything on disk") | |
199 (?r "recursive" nil recursive | |
200 "remove the contents of directories recursively") | |
201 (?R nil nil recursive "(same)") | |
202 (?v "verbose" nil verbose "explain what is being done") | |
203 :preserve-args | |
204 :external "rm" | |
205 :show-usage | |
206 :usage "[OPTION]... FILE... | |
207 Remove (unlink) the FILE(s).") | |
208 (unless interactive | |
209 (setq interactive eshell-rm-interactive-query)) | |
210 (if (and force-removal interactive) | |
211 (setq interactive nil)) | |
212 (while args | |
213 (let ((entry (if (stringp (car args)) | |
214 (directory-file-name (car args)) | |
215 (if (numberp (car args)) | |
216 (number-to-string (car args)) | |
217 (car args))))) | |
218 (cond | |
219 ((bufferp entry) | |
220 (if verbose | |
221 (eshell-printn (format "rm: removing buffer `%s'" entry))) | |
222 (unless (or preview | |
223 (and interactive | |
224 (not (y-or-n-p (format "rm: delete buffer `%s'? " | |
225 entry))))) | |
226 (eshell-funcalln 'kill-buffer entry))) | |
31241 | 227 ((eshell-processp entry) |
29876 | 228 (if verbose |
229 (eshell-printn (format "rm: killing process `%s'" entry))) | |
230 (unless (or preview | |
231 (and interactive | |
232 (not (y-or-n-p (format "rm: kill process `%s'? " | |
233 entry))))) | |
234 (eshell-funcalln 'kill-process entry))) | |
235 ((symbolp entry) | |
236 (if verbose | |
237 (eshell-printn (format "rm: uninterning symbol `%s'" entry))) | |
238 (unless | |
239 (or preview | |
240 (and interactive | |
241 (not (y-or-n-p (format "rm: unintern symbol `%s'? " | |
242 entry))))) | |
243 (eshell-funcalln 'unintern entry))) | |
244 ((stringp entry) | |
245 (if (and (file-directory-p entry) | |
246 (not (file-symlink-p entry))) | |
247 (if (or recursive | |
248 eshell-rm-removes-directories) | |
249 (if (or preview | |
250 (not interactive) | |
251 (y-or-n-p | |
252 (format "rm: descend into directory `%s'? " | |
253 entry))) | |
254 (eshell-remove-entries nil (list entry) t)) | |
255 (eshell-error (format "rm: %s: is a directory\n" entry))) | |
256 (eshell-remove-entries nil (list entry) t))))) | |
257 (setq args (cdr args))) | |
258 nil)) | |
259 | |
260 (defun eshell/mkdir (&rest args) | |
261 "Implementation of mkdir in Lisp." | |
262 (eshell-eval-using-options | |
263 "mkdir" args | |
264 '((?h "help" nil nil "show this usage screen") | |
265 :external "mkdir" | |
266 :show-usage | |
267 :usage "[OPTION] DIRECTORY... | |
268 Create the DIRECTORY(ies), if they do not already exist.") | |
269 (while args | |
270 (eshell-funcalln 'make-directory (car args)) | |
271 (setq args (cdr args))) | |
272 nil)) | |
273 | |
274 (defun eshell/rmdir (&rest args) | |
275 "Implementation of rmdir in Lisp." | |
276 (eshell-eval-using-options | |
277 "rmdir" args | |
278 '((?h "help" nil nil "show this usage screen") | |
279 :external "rmdir" | |
280 :show-usage | |
281 :usage "[OPTION] DIRECTORY... | |
282 Remove the DIRECTORY(ies), if they are empty.") | |
283 (while args | |
284 (eshell-funcalln 'delete-directory (car args)) | |
285 (setq args (cdr args))) | |
286 nil)) | |
287 | |
288 (eval-when-compile | |
289 (defvar no-dereference) | |
290 (defvar preview) | |
291 (defvar verbose)) | |
292 | |
293 (defvar eshell-warn-dot-directories t) | |
294 | |
295 (defun eshell-shuffle-files (command action files target func deep &rest args) | |
296 "Shuffle around some filesystem entries, using FUNC to do the work." | |
297 (if (null target) | |
298 (error "%s: missing destination file" command)) | |
299 (let ((attr-target (file-attributes target)) | |
300 (is-dir (or (file-directory-p target) | |
301 (and preview (not eshell-warn-dot-directories)))) | |
302 attr) | |
303 (if (and (not preview) (not is-dir) | |
304 (> (length files) 1)) | |
305 (error "%s: when %s multiple files, last argument must be a directory" | |
306 command action)) | |
307 (while files | |
308 (setcar files (directory-file-name (car files))) | |
309 (cond | |
310 ((string-match "\\`\\.\\.?\\'" | |
311 (file-name-nondirectory (car files))) | |
312 (if eshell-warn-dot-directories | |
313 (eshell-error (format "%s: %s: omitting directory\n" | |
314 command (car files))))) | |
315 ((and attr-target | |
30269
30829e002c1b
(eshell-shuffle-files): Don't disable
Eli Zaretskii <eliz@gnu.org>
parents:
29934
diff
changeset
|
316 (or (not (eshell-under-windows-p)) |
30829e002c1b
(eshell-shuffle-files): Don't disable
Eli Zaretskii <eliz@gnu.org>
parents:
29934
diff
changeset
|
317 (eq system-type 'ms-dos)) |
29876 | 318 (setq attr (file-attributes (car files))) |
319 (= (nth 10 attr-target) (nth 10 attr)) | |
320 (= (nth 11 attr-target) (nth 11 attr))) | |
321 (eshell-error (format "%s: `%s' and `%s' are the same file\n" | |
322 command (car files) target))) | |
323 (t | |
324 (let ((source (car files)) | |
325 (target (if is-dir | |
326 (expand-file-name | |
327 (file-name-nondirectory (car files)) target) | |
328 target)) | |
329 link) | |
330 (if (and (file-directory-p source) | |
331 (or (not no-dereference) | |
332 (not (file-symlink-p source))) | |
333 (not (memq func '(make-symbolic-link | |
334 add-name-to-file)))) | |
335 (if (and (eq func 'copy-file) | |
336 (not recursive)) | |
337 (eshell-error (format "%s: %s: omitting directory\n" | |
338 command (car files))) | |
339 (let (eshell-warn-dot-directories) | |
340 (if (and (not deep) | |
341 (eq func 'rename-file) | |
342 (= (nth 11 (file-attributes | |
343 (file-name-directory | |
344 (expand-file-name source)))) | |
345 (nth 11 (file-attributes | |
346 (file-name-directory | |
347 (expand-file-name target)))))) | |
348 (apply 'eshell-funcalln func source target args) | |
349 (unless (file-directory-p target) | |
350 (if verbose | |
351 (eshell-printn | |
352 (format "%s: making directory %s" | |
353 command target))) | |
354 (unless preview | |
355 (eshell-funcalln 'make-directory target))) | |
31241 | 356 (apply 'eshell-shuffle-files |
357 command action | |
358 (mapcar | |
359 (function | |
360 (lambda (file) | |
361 (concat source "/" file))) | |
362 (directory-files source)) | |
363 target func t args) | |
29876 | 364 (when (eq func 'rename-file) |
365 (if verbose | |
366 (eshell-printn | |
367 (format "%s: deleting directory %s" | |
368 command source))) | |
369 (unless preview | |
370 (eshell-funcalln 'delete-directory source)))))) | |
371 (if verbose | |
372 (eshell-printn (format "%s: %s -> %s" command | |
373 source target))) | |
374 (unless preview | |
375 (if (and no-dereference | |
376 (setq link (file-symlink-p source))) | |
377 (progn | |
378 (apply 'eshell-funcalln 'make-symbolic-link | |
379 link target args) | |
380 (if (eq func 'rename-file) | |
381 (if (and (file-directory-p source) | |
382 (not (file-symlink-p source))) | |
383 (eshell-funcalln 'delete-directory source) | |
384 (eshell-funcalln 'delete-file source)))) | |
385 (apply 'eshell-funcalln func source target args))))))) | |
386 (setq files (cdr files))))) | |
387 | |
388 (defun eshell-shorthand-tar-command (command args) | |
389 "Rewrite `cp -v dir a.tar.gz' to `tar cvzf a.tar.gz dir'." | |
390 (let* ((archive (car (last args))) | |
391 (tar-args | |
392 (cond ((string-match "z2" archive) "If") | |
393 ((string-match "gz" archive) "zf") | |
394 ((string-match "\\(az\\|Z\\)" archive) "Zf") | |
395 (t "f")))) | |
396 (if (file-exists-p archive) | |
397 (setq tar-args (concat "u" tar-args)) | |
398 (setq tar-args (concat "c" tar-args))) | |
399 (if verbose | |
400 (setq tar-args (concat "v" tar-args))) | |
401 (if (equal command "mv") | |
402 (setq tar-args (concat "--remove-files -" tar-args))) | |
403 ;; truncate the archive name from the arguments | |
404 (setcdr (last args 2) nil) | |
405 (throw 'eshell-replace-command | |
406 (eshell-parse-command | |
407 (format "tar %s %s" tar-args archive) args)))) | |
408 | |
409 ;; this is to avoid duplicating code... | |
410 (defmacro eshell-mvcp-template | |
411 (command action func query-var force-var &optional preserve) | |
412 `(if (and (string-match eshell-tar-regexp (car (last args))) | |
413 (or (> (length args) 2) | |
414 (and (file-directory-p (car args)) | |
415 (or (not no-dereference) | |
416 (not (file-symlink-p (car args))))))) | |
417 (eshell-shorthand-tar-command ,command args) | |
418 (let (target) | |
419 (if (> (length args) 1) | |
420 (progn | |
421 (setq target (car (last args))) | |
422 (setcdr (last args 2) nil)) | |
423 (setq args nil)) | |
424 (eshell-shuffle-files | |
425 ,command ,action args target ,func nil | |
426 ,@(append | |
427 `((if (and (or interactive | |
428 ,query-var) | |
429 (not force)) | |
430 1 (or force ,force-var))) | |
431 (if preserve | |
432 (list preserve))))) | |
433 nil)) | |
434 | |
435 (defun eshell/mv (&rest args) | |
436 "Implementation of mv in Lisp." | |
437 (eshell-eval-using-options | |
438 "mv" args | |
439 '((?f "force" nil force | |
440 "remove existing destinations, never prompt") | |
441 (?i "interactive" nil interactive | |
442 "request confirmation if target already exists") | |
443 (?n "preview" nil preview | |
444 "don't change anything on disk") | |
445 (?v "verbose" nil verbose | |
446 "explain what is being done") | |
447 (nil "help" nil nil "show this usage screen") | |
448 :external "mv" | |
449 :show-usage | |
450 :usage "[OPTION]... SOURCE DEST | |
451 or: mv [OPTION]... SOURCE... DIRECTORY | |
452 Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY. | |
453 \[OPTION] DIRECTORY...") | |
454 (let ((no-dereference t)) | |
455 (eshell-mvcp-template "mv" "moving" 'rename-file | |
456 eshell-mv-interactive-query | |
457 eshell-mv-overwrite-files)))) | |
458 | |
459 (defun eshell/cp (&rest args) | |
460 "Implementation of cp in Lisp." | |
461 (eshell-eval-using-options | |
462 "cp" args | |
463 '((?a "archive" nil archive | |
464 "same as -dpR") | |
465 (?d "no-dereference" nil no-dereference | |
466 "preserve links") | |
467 (?f "force" nil force | |
468 "remove existing destinations, never prompt") | |
469 (?i "interactive" nil interactive | |
470 "request confirmation if target already exists") | |
471 (?n "preview" nil preview | |
472 "don't change anything on disk") | |
473 (?p "preserve" nil preserve | |
474 "preserve file attributes if possible") | |
475 (?R "recursive" nil recursive | |
476 "copy directories recursively") | |
477 (?v "verbose" nil verbose | |
478 "explain what is being done") | |
479 (nil "help" nil nil "show this usage screen") | |
480 :external "cp" | |
481 :show-usage | |
482 :usage "[OPTION]... SOURCE DEST | |
483 or: cp [OPTION]... SOURCE... DIRECTORY | |
484 Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.") | |
485 (if archive | |
486 (setq preserve t no-dereference t recursive t)) | |
487 (eshell-mvcp-template "cp" "copying" 'copy-file | |
488 eshell-cp-interactive-query | |
489 eshell-cp-overwrite-files preserve))) | |
490 | |
491 (defun eshell/ln (&rest args) | |
492 "Implementation of ln in Lisp." | |
493 (eshell-eval-using-options | |
494 "ln" args | |
495 '((?h "help" nil nil "show this usage screen") | |
496 (?s "symbolic" nil symbolic | |
497 "make symbolic links instead of hard links") | |
498 (?i "interactive" nil interactive "request confirmation if target already exists") | |
499 (?f "force" nil force "remove existing destinations, never prompt") | |
500 (?n "preview" nil preview | |
501 "don't change anything on disk") | |
502 (?v "verbose" nil verbose "explain what is being done") | |
503 :external "ln" | |
504 :show-usage | |
505 :usage "[OPTION]... TARGET [LINK_NAME] | |
506 or: ln [OPTION]... TARGET... DIRECTORY | |
507 Create a link to the specified TARGET with optional LINK_NAME. If there is | |
508 more than one TARGET, the last argument must be a directory; create links | |
509 in DIRECTORY to each TARGET. Create hard links by default, symbolic links | |
510 with '--symbolic'. When creating hard links, each TARGET must exist.") | |
511 (let (target no-dereference) | |
512 (if (> (length args) 1) | |
513 (progn | |
514 (setq target (car (last args))) | |
515 (setcdr (last args 2) nil)) | |
516 (setq args nil)) | |
517 (eshell-shuffle-files "ln" "linking" args target | |
518 (if symbolic | |
519 'make-symbolic-link | |
520 'add-name-to-file) nil | |
521 (if (and (or interactive | |
522 eshell-ln-interactive-query) | |
523 (not force)) | |
524 1 (or force eshell-ln-overwrite-files)))) | |
525 nil)) | |
526 | |
527 (defun eshell/cat (&rest args) | |
528 "Implementation of cat in Lisp." | |
529 (if eshell-in-pipeline-p | |
530 (throw 'eshell-replace-command | |
31241 | 531 (eshell-parse-command "*cat" (eshell-flatten-list args))) |
29876 | 532 (eshell-init-print-buffer) |
533 (eshell-eval-using-options | |
534 "cat" args | |
535 '((?h "help" nil nil "show this usage screen") | |
536 :external "cat" | |
537 :show-usage | |
538 :usage "[OPTION] FILE... | |
539 Concatenate FILE(s), or standard input, to standard output.") | |
540 (eshell-for file args | |
541 (if (string= file "-") | |
542 (throw 'eshell-external | |
543 (eshell-external-command "cat" args)))) | |
544 (let ((curbuf (current-buffer))) | |
545 (eshell-for file args | |
546 (with-temp-buffer | |
547 (insert-file-contents file) | |
548 (goto-char (point-min)) | |
549 (while (not (eobp)) | |
550 (let ((str (buffer-substring | |
551 (point) (min (1+ (line-end-position)) | |
552 (point-max))))) | |
553 (with-current-buffer curbuf | |
554 (eshell-buffered-print str))) | |
555 (forward-line))))) | |
556 (eshell-flush) | |
557 ;; if the file does not end in a newline, do not emit one | |
558 (setq eshell-ensure-newline-p nil)))) | |
559 | |
560 ;; special front-end functions for compilation-mode buffers | |
561 | |
562 (defun eshell/make (&rest args) | |
563 "Use `compile' to do background makes." | |
564 (if (and eshell-current-subjob-p | |
565 (eshell-interactive-output-p)) | |
566 (let ((compilation-process-setup-function | |
567 (list 'lambda nil | |
568 (list 'setq 'process-environment | |
569 (list 'quote (eshell-copy-environment)))))) | |
570 (compile (concat "make " (eshell-flatten-and-stringify args)))) | |
571 (throw 'eshell-replace-command | |
31241 | 572 (eshell-parse-command "*make" (eshell-flatten-list args))))) |
29876 | 573 |
574 (defun eshell-occur-mode-goto-occurrence () | |
575 "Go to the occurrence the current line describes." | |
576 (interactive) | |
577 (let ((pos (occur-mode-find-occurrence))) | |
578 (pop-to-buffer (marker-buffer pos)) | |
579 (goto-char (marker-position pos)))) | |
580 | |
581 (defun eshell-occur-mode-mouse-goto (event) | |
582 "In Occur mode, go to the occurrence whose line you click on." | |
583 (interactive "e") | |
584 (let (buffer pos) | |
585 (save-excursion | |
586 (set-buffer (window-buffer (posn-window (event-end event)))) | |
587 (save-excursion | |
588 (goto-char (posn-point (event-end event))) | |
589 (setq pos (occur-mode-find-occurrence)) | |
590 (setq buffer occur-buffer))) | |
591 (pop-to-buffer (marker-buffer pos)) | |
592 (goto-char (marker-position pos)))) | |
593 | |
594 (defun eshell-poor-mans-grep (args) | |
595 "A poor version of grep that opens every file and uses `occur'. | |
596 This eats up memory, since it leaves the buffers open (to speed future | |
597 searches), and it's very slow. But, if your system has no grep | |
598 available..." | |
599 (save-selected-window | |
600 (let ((default-dir default-directory)) | |
601 (with-current-buffer (get-buffer-create "*grep*") | |
602 (let ((inhibit-read-only t) | |
603 (default-directory default-dir)) | |
604 (erase-buffer) | |
605 (occur-mode) | |
606 (let ((files (eshell-flatten-list (cdr args))) | |
607 (inhibit-redisplay t) | |
608 string) | |
609 (when (car args) | |
610 (if (get-buffer "*Occur*") | |
611 (kill-buffer (get-buffer "*Occur*"))) | |
612 (setq string nil) | |
613 (while files | |
614 (with-current-buffer (find-file-noselect (car files)) | |
615 (save-excursion | |
616 (ignore-errors | |
617 (occur (car args)))) | |
618 (if (get-buffer "*Occur*") | |
619 (with-current-buffer (get-buffer "*Occur*") | |
620 (setq string (buffer-string)) | |
621 (kill-buffer (current-buffer))))) | |
622 (if string (insert string)) | |
623 (setq string nil | |
624 files (cdr files))))) | |
625 (setq occur-buffer (current-buffer)) | |
626 (local-set-key [mouse-2] 'eshell-occur-mode-mouse-goto) | |
627 (local-set-key [(control ?c) (control ?c)] | |
628 'eshell-occur-mode-goto-occurrence) | |
629 (local-set-key [(control ?m)] | |
630 'eshell-occur-mode-goto-occurrence) | |
631 (local-set-key [return] 'eshell-occur-mode-goto-occurrence) | |
632 (pop-to-buffer (current-buffer) t) | |
633 (goto-char (point-min)) | |
634 (resize-temp-buffer-window)))))) | |
635 | |
636 (defun eshell-grep (command args &optional maybe-use-occur) | |
637 "Generic service function for the various grep aliases. | |
638 It calls Emacs' grep utility if the command is not redirecting output, | |
639 and if it's not part of a command pipeline. Otherwise, it calls the | |
640 external command." | |
641 (if (and maybe-use-occur eshell-no-grep-available) | |
642 (eshell-poor-mans-grep args) | |
643 (if (or eshell-plain-grep-behavior | |
644 (not (and (eshell-interactive-output-p) | |
645 (not eshell-in-pipeline-p) | |
646 (not eshell-in-subcommand-p)))) | |
647 (throw 'eshell-replace-command | |
31241 | 648 (eshell-parse-command (concat "*" command) |
649 (eshell-flatten-list args))) | |
29876 | 650 (let* ((compilation-process-setup-function |
651 (list 'lambda nil | |
652 (list 'setq 'process-environment | |
653 (list 'quote (eshell-copy-environment))))) | |
654 (args (mapconcat 'identity | |
655 (mapcar 'shell-quote-argument | |
656 (eshell-flatten-list args)) | |
657 " ")) | |
658 (cmd (progn | |
659 (set-text-properties 0 (length args) | |
660 '(invisible t) args) | |
661 (format "%s -n %s" command args))) | |
662 compilation-scroll-output) | |
663 (grep cmd))))) | |
664 | |
665 (defun eshell/grep (&rest args) | |
666 "Use Emacs grep facility instead of calling external grep." | |
667 (eshell-grep "grep" args t)) | |
668 | |
669 (defun eshell/egrep (&rest args) | |
670 "Use Emacs grep facility instead of calling external egrep." | |
671 (eshell-grep "egrep" args t)) | |
672 | |
673 (defun eshell/fgrep (&rest args) | |
674 "Use Emacs grep facility instead of calling external fgrep." | |
675 (eshell-grep "fgrep" args t)) | |
676 | |
677 (defun eshell/agrep (&rest args) | |
678 "Use Emacs grep facility instead of calling external agrep." | |
679 (eshell-grep "agrep" args)) | |
680 | |
681 (defun eshell/glimpse (&rest args) | |
682 "Use Emacs grep facility instead of calling external glimpse." | |
683 (let (null-device) | |
684 (eshell-grep "glimpse" (append '("-z" "-y") args)))) | |
685 | |
686 ;; completions rules for some common UNIX commands | |
687 | |
688 (defsubst eshell-complete-hostname () | |
689 "Complete a command that wants a hostname for an argument." | |
690 (pcomplete-here (eshell-read-host-names))) | |
691 | |
692 (defun eshell-complete-host-reference () | |
693 "If there is a host reference, complete it." | |
694 (let ((arg (pcomplete-actual-arg)) | |
695 index) | |
696 (when (setq index (string-match "@[a-z.]*\\'" arg)) | |
697 (setq pcomplete-stub (substring arg (1+ index)) | |
698 pcomplete-last-completion-raw t) | |
699 (throw 'pcomplete-completions (eshell-read-host-names))))) | |
700 | |
701 (defalias 'pcomplete/ftp 'eshell-complete-hostname) | |
702 (defalias 'pcomplete/ncftp 'eshell-complete-hostname) | |
703 (defalias 'pcomplete/ping 'eshell-complete-hostname) | |
704 (defalias 'pcomplete/rlogin 'eshell-complete-hostname) | |
705 | |
706 (defun pcomplete/telnet () | |
707 (require 'pcmpl-unix) | |
708 (pcomplete-opt "xl(pcmpl-unix-user-names)") | |
709 (eshell-complete-hostname)) | |
710 | |
711 (defun pcomplete/rsh () | |
712 "Complete `rsh', which, after the user and hostname, is like xargs." | |
713 (require 'pcmpl-unix) | |
714 (pcomplete-opt "l(pcmpl-unix-user-names)") | |
715 (eshell-complete-hostname) | |
716 (pcomplete-here (funcall pcomplete-command-completion-function)) | |
717 (funcall (or (pcomplete-find-completion-function (pcomplete-arg 1)) | |
718 pcomplete-default-completion-function))) | |
719 | |
720 (defalias 'pcomplete/ssh 'pcomplete/rsh) | |
721 | |
722 (eval-when-compile | |
723 (defvar block-size) | |
724 (defvar by-bytes) | |
725 (defvar dereference-links) | |
726 (defvar grand-total) | |
727 (defvar human-readable) | |
728 (defvar max-depth) | |
729 (defvar only-one-filesystem) | |
730 (defvar show-all)) | |
731 | |
732 (defsubst eshell-du-size-string (size) | |
733 (let* ((str (eshell-printable-size size human-readable block-size t)) | |
734 (len (length str))) | |
735 (concat str (if (< len 8) | |
736 (make-string (- 8 len) ? ))))) | |
737 | |
738 (defun eshell-du-sum-directory (path depth) | |
739 "Summarize PATH, and its member directories." | |
740 (let ((entries (eshell-directory-files-and-attributes path)) | |
741 (size 0.0)) | |
742 (while entries | |
743 (unless (string-match "\\`\\.\\.?\\'" (caar entries)) | |
744 (let* ((entry (concat path (char-to-string directory-sep-char) | |
745 (caar entries))) | |
746 (symlink (and (stringp (cadr (car entries))) | |
747 (cadr (car entries))))) | |
748 (unless (or (and symlink (not dereference-links)) | |
749 (and only-one-filesystem | |
31241 | 750 (/= only-one-filesystem |
751 (nth 12 (car entries))))) | |
29876 | 752 (if symlink |
753 (setq entry symlink)) | |
754 (setq size | |
755 (+ size | |
756 (if (eq t (cadr (car entries))) | |
757 (eshell-du-sum-directory entry (1+ depth)) | |
758 (let ((file-size (nth 8 (car entries)))) | |
759 (prog1 | |
760 file-size | |
761 (if show-all | |
762 (eshell-print | |
763 (concat (eshell-du-size-string file-size) | |
764 entry "\n"))))))))))) | |
765 (setq entries (cdr entries))) | |
766 (if (or (not max-depth) | |
767 (= depth max-depth) | |
768 (= depth 0)) | |
769 (eshell-print (concat (eshell-du-size-string size) | |
770 (directory-file-name path) "\n"))) | |
771 size)) | |
772 | |
773 (defun eshell/du (&rest args) | |
31241 | 774 "Implementation of \"du\" in Lisp, passing ARGS." |
29876 | 775 (if (eshell-search-path "du") |
776 (throw 'eshell-replace-command | |
31241 | 777 (eshell-parse-command "*du" (eshell-flatten-list args))) |
29876 | 778 (eshell-eval-using-options |
779 "du" args | |
780 '((?a "all" nil show-all | |
781 "write counts for all files, not just directories") | |
782 (nil "block-size" t block-size | |
783 "use SIZE-byte blocks (i.e., --block-size SIZE)") | |
784 (?b "bytes" nil by-bytes | |
785 "print size in bytes") | |
786 (?c "total" nil grand-total | |
787 "produce a grand total") | |
788 (?d "max-depth" t max-depth | |
789 "display data only this many levels of data") | |
790 (?h "human-readable" 1024 human-readable | |
791 "print sizes in human readable format") | |
792 (?H "is" 1000 human-readable | |
793 "likewise, but use powers of 1000 not 1024") | |
794 (?k "kilobytes" 1024 block-size | |
795 "like --block-size 1024") | |
796 (?L "dereference" nil dereference-links | |
797 "dereference all symbolic links") | |
798 (?m "megabytes" 1048576 block-size | |
799 "like --block-size 1048576") | |
800 (?s "summarize" 0 max-depth | |
801 "display only a total for each argument") | |
802 (?x "one-file-system" nil only-one-filesystem | |
803 "skip directories on different filesystems") | |
804 (nil "help" nil nil | |
805 "show this usage screen") | |
806 :external "du" | |
807 :usage "[OPTION]... FILE... | |
808 Summarize disk usage of each FILE, recursively for directories.") | |
809 (unless by-bytes | |
810 (setq block-size (or block-size 1024))) | |
811 (if (and max-depth (stringp max-depth)) | |
812 (setq max-depth (string-to-int max-depth))) | |
813 ;; filesystem support means nothing under Windows | |
814 (if (eshell-under-windows-p) | |
815 (setq only-one-filesystem nil)) | |
816 (unless args | |
817 (setq args '("."))) | |
818 (let ((size 0.0)) | |
819 (while args | |
820 (if only-one-filesystem | |
821 (setq only-one-filesystem | |
822 (nth 11 (file-attributes | |
823 (file-name-as-directory (car args)))))) | |
824 (setq size (+ size (eshell-du-sum-directory | |
825 (directory-file-name (car args)) 0))) | |
826 (setq args (cdr args))) | |
827 (if grand-total | |
828 (eshell-print (concat (eshell-du-size-string size) | |
829 "total\n"))))))) | |
830 | |
831 (defvar eshell-time-start nil) | |
832 | |
833 (defun eshell-show-elapsed-time () | |
834 (let ((elapsed (format "%.3f secs\n" | |
835 (- (eshell-time-to-seconds (current-time)) | |
836 eshell-time-start)))) | |
837 (set-text-properties 0 (length elapsed) '(face bold) elapsed) | |
838 (eshell-interactive-print elapsed)) | |
839 (remove-hook 'eshell-post-command-hook 'eshell-show-elapsed-time t)) | |
840 | |
841 (defun eshell/time (&rest args) | |
842 "Implementation of \"time\" in Lisp." | |
843 (let ((time-args (copy-alist args)) | |
844 (continue t) | |
845 last-arg) | |
846 (while (and continue args) | |
847 (if (not (string-match "^-" (car args))) | |
848 (progn | |
849 (if last-arg | |
850 (setcdr last-arg nil) | |
851 (setq args '(""))) | |
852 (setq continue nil)) | |
853 (setq last-arg args | |
854 args (cdr args)))) | |
855 (eshell-eval-using-options | |
856 "time" args | |
857 '((?h "help" nil nil "show this usage screen") | |
858 :external "time" | |
859 :show-usage | |
860 :usage "COMMAND... | |
861 Show wall-clock time elapsed during execution of COMMAND.") | |
862 (setq eshell-time-start (eshell-time-to-seconds (current-time))) | |
863 (add-hook 'eshell-post-command-hook 'eshell-show-elapsed-time nil t) | |
864 ;; after setting | |
865 (throw 'eshell-replace-command | |
866 (eshell-parse-command (car time-args) (cdr time-args)))))) | |
867 | |
868 (defalias 'eshell/whoami 'user-login-name) | |
869 | |
870 (defvar eshell-diff-window-config nil) | |
871 | |
872 (defun eshell-diff-quit () | |
873 "Restore the window configuration previous to diff'ing." | |
874 (interactive) | |
875 (if eshell-diff-window-config | |
876 (set-window-configuration eshell-diff-window-config))) | |
877 | |
878 (defun eshell/diff (&rest args) | |
879 "Alias \"diff\" to call Emacs `diff' function." | |
31241 | 880 (let ((orig-args (eshell-flatten-list args))) |
881 (if (or eshell-plain-diff-behavior | |
882 (not (and (eshell-interactive-output-p) | |
883 (not eshell-in-pipeline-p) | |
884 (not eshell-in-subcommand-p)))) | |
885 (throw 'eshell-replace-command | |
886 (eshell-parse-command "*diff" orig-args)) | |
887 (setq args (eshell-copy-list orig-args)) | |
888 (if (< (length args) 2) | |
889 (throw 'eshell-replace-command | |
890 (eshell-parse-command "*diff" orig-args))) | |
891 (let ((old (car (last args 2))) | |
892 (new (car (last args))) | |
893 (config (current-window-configuration))) | |
894 (if (= (length args) 2) | |
895 (setq args nil) | |
896 (setcdr (last args 3) nil)) | |
897 (with-current-buffer | |
898 (condition-case err | |
899 (diff old new (eshell-flatten-and-stringify args)) | |
900 (error | |
901 (throw 'eshell-replace-command | |
902 (eshell-parse-command "*diff" orig-args)))) | |
903 (when (fboundp 'diff-mode) | |
904 (diff-mode) | |
905 (set (make-local-variable 'eshell-diff-window-config) config) | |
906 (local-set-key [?q] 'eshell-diff-quit) | |
907 (if (fboundp 'turn-on-font-lock-if-enabled) | |
908 (turn-on-font-lock-if-enabled)))) | |
909 (other-window 1) | |
910 (goto-char (point-min)) | |
911 nil)))) | |
29876 | 912 |
913 (defun eshell/locate (&rest args) | |
914 "Alias \"locate\" to call Emacs `locate' function." | |
915 (if (or eshell-plain-locate-behavior | |
916 (not (and (eshell-interactive-output-p) | |
917 (not eshell-in-pipeline-p) | |
918 (not eshell-in-subcommand-p))) | |
919 (and (stringp (car args)) | |
920 (string-match "^-" (car args)))) | |
921 (throw 'eshell-replace-command | |
31241 | 922 (eshell-parse-command "*locate" (eshell-flatten-list args))) |
29876 | 923 (save-selected-window |
924 (let ((locate-history-list (list (car args)))) | |
925 (locate-with-filter (car args) (cadr args)))))) | |
926 | |
927 (defun eshell/occur (&rest args) | |
928 "Alias \"occur\" to call Emacs `occur' function." | |
929 (let ((inhibit-read-only t)) | |
930 (if args | |
931 (error "usage: occur: (REGEXP)") | |
932 (occur (car args))))) | |
933 | |
934 ;;; Code: | |
935 | |
936 ;;; em-unix.el ends here |