1 ;;; em-unix.el --- UNIX command aliases -*- lexical-binding:t -*-
3 ;; Copyright (C) 1999-2013 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/>.
24 ;; This file contains implementations of several UNIX command in Emacs
25 ;; Lisp, for several reasons:
27 ;; 1) it makes them available on all platforms where the Lisp
28 ;; functions used are available
30 ;; 2) it makes their functionality accessible and modified by the
33 ;; 3) it allows Eshell to refrain from having to invoke external
34 ;; processes for common operations.
44 (defgroup eshell-unix nil
45 "This module defines many of the more common UNIX utilities as
46 aliases implemented in Lisp. These include mv, ln, cp, rm, etc. If
47 the user passes arguments which are too complex, or are unrecognized
48 by the Lisp variant, the external version will be called (if
49 available). The only reason not to use them would be because they are
50 usually much slower. But in several cases their tight integration
51 with Eshell makes them more versatile than their traditional cousins
52 \(such as being able to use `kill' to kill Eshell background processes
54 :tag
"UNIX commands in Lisp"
55 :group
'eshell-module
))
57 (defcustom eshell-unix-load-hook nil
58 "A list of functions to run when `eshell-unix' is loaded."
59 :version
"24.1" ; removed eshell-unix-initialize
63 (defcustom eshell-plain-grep-behavior nil
64 "If non-nil, standalone \"grep\" commands will behave normally.
65 Standalone in this context means not redirected, and not on the
66 receiving side of a command pipeline."
70 (defcustom eshell-no-grep-available
(not (eshell-search-path "grep"))
71 "If non-nil, no grep is available on the current machine."
75 (defcustom eshell-plain-diff-behavior nil
76 "If non-nil, standalone \"diff\" commands will behave normally.
77 Standalone in this context means not redirected, and not on the
78 receiving side of a command pipeline."
82 (defcustom eshell-plain-locate-behavior
(featurep 'xemacs
)
83 "If non-nil, standalone \"locate\" commands will behave normally.
84 Standalone in this context means not redirected, and not on the
85 receiving side of a command pipeline."
89 (defcustom eshell-rm-removes-directories nil
90 "If non-nil, `rm' will remove directory entries.
91 Otherwise, `rmdir' is required."
95 (defcustom eshell-rm-interactive-query
(= (user-uid) 0)
96 "If non-nil, `rm' will query before removing anything."
100 (defcustom eshell-mv-interactive-query
(= (user-uid) 0)
101 "If non-nil, `mv' will query before overwriting anything."
105 (defcustom eshell-mv-overwrite-files t
106 "If non-nil, `mv' will overwrite files without warning."
110 (defcustom eshell-cp-interactive-query
(= (user-uid) 0)
111 "If non-nil, `cp' will query before overwriting anything."
115 (defcustom eshell-cp-overwrite-files t
116 "If non-nil, `cp' will overwrite files without warning."
120 (defcustom eshell-ln-interactive-query
(= (user-uid) 0)
121 "If non-nil, `ln' will query before overwriting anything."
125 (defcustom eshell-ln-overwrite-files nil
126 "If non-nil, `ln' will overwrite files without warning."
130 (defcustom eshell-default-target-is-dot nil
131 "If non-nil, the default destination for cp, mv or ln is `.'."
135 (defcustom eshell-du-prefer-over-ange nil
136 "Use Eshell's du in ange-ftp remote directories.
137 Otherwise, Emacs will attempt to use rsh to invoke du on the remote machine."
143 (defun eshell-unix-initialize ()
144 "Initialize the UNIX support/emulation code."
145 (when (eshell-using-module 'eshell-cmpl
)
146 (add-hook 'pcomplete-try-first-hook
147 'eshell-complete-host-reference nil t
))
148 (make-local-variable 'eshell-complex-commands
)
149 (setq eshell-complex-commands
150 (append '("grep" "egrep" "fgrep" "agrep" "glimpse" "locate"
151 "cat" "time" "cp" "mv" "make" "du" "diff")
152 eshell-complex-commands
)))
154 (defalias 'eshell
/date
'current-time-string
)
155 (defalias 'eshell
/basename
'file-name-nondirectory
)
156 (defalias 'eshell
/dirname
'file-name-directory
)
158 (defvar em-interactive
)
160 (defvar em-recursive
)
163 (defun eshell/man
(&rest args
)
164 "Invoke man, flattening the arguments appropriately."
165 (funcall 'man
(apply 'eshell-flatten-and-stringify args
)))
167 (put 'eshell
/man
'eshell-no-numeric-conversions t
)
169 (defun eshell/info
(&rest args
)
170 "Run the info command in-frame with the same behavior as command-line `info', ie:
171 'info' => goes to top info window
172 'info arg1' => IF arg1 is a file, then visits arg1
173 'info arg1' => OTHERWISE goes to top info window and then menu item arg1
174 'info arg1 arg2' => does action for arg1 (either visit-file or menu-item) and then menu item arg2
176 (eval-and-compile (require 'info
))
178 ((not (stringp (car args
)))
180 ((file-exists-p (expand-file-name (car args
)))
181 (expand-file-name (car args
)))
182 ((file-exists-p (concat (expand-file-name (car args
)) ".info"))
183 (concat (expand-file-name (car args
)) ".info")))))
185 ;; If the first arg is a file, then go to that file's Top node
186 ;; Otherwise, go to the global directory
189 (setq args
(cdr args
))
190 (Info-find-node file
"Top"))
193 ;; Treat all remaining args as menu references
195 (Info-menu (car args
))
196 (setq args
(cdr args
)))))
198 (defun eshell-remove-entries (path files
&optional top-level
)
199 "From PATH, remove all of the given FILES, perhaps interactively."
201 (if (string-match "\\`\\.\\.?\\'"
202 (file-name-nondirectory (car files
)))
204 (eshell-error "rm: cannot remove `.' or `..'\n"))
205 (if (and (file-directory-p (car files
))
206 (not (file-symlink-p (car files
))))
209 (eshell-printn (format "rm: removing directory `%s'"
215 (format "rm: remove directory `%s'? "
217 (eshell-funcalln 'delete-directory
(car files
) t t
)))
219 (eshell-printn (format "rm: removing file `%s'"
221 (unless (or em-preview
224 (format "rm: remove `%s'? "
226 (eshell-funcalln 'delete-file
(car files
) t
))))
227 (setq files
(cdr files
))))
229 (defun eshell/rm
(&rest args
)
230 "Implementation of rm in Lisp.
231 This is implemented to call either `delete-file', `kill-buffer',
232 `kill-process', or `unintern', depending on the nature of the
234 (setq args
(eshell-flatten-list args
))
235 (eshell-eval-using-options
237 '((?h
"help" nil nil
"show this usage screen")
238 (?f
"force" nil force-removal
"force removal")
239 (?i
"interactive" nil em-interactive
"prompt before any removal")
240 (?n
"preview" nil em-preview
"don't change anything on disk")
241 (?r
"recursive" nil em-recursive
242 "remove the contents of directories recursively")
243 (?R nil nil em-recursive
"(same)")
244 (?v
"verbose" nil em-verbose
"explain what is being done")
248 :usage
"[OPTION]... FILE...
249 Remove (unlink) the FILE(s).")
250 (unless em-interactive
251 (setq em-interactive eshell-rm-interactive-query
))
252 (if (and force-removal em-interactive
)
253 (setq em-interactive nil
))
255 (let ((entry (if (stringp (car args
))
256 (directory-file-name (car args
))
257 (if (numberp (car args
))
258 (number-to-string (car args
))
263 (eshell-printn (format "rm: removing buffer `%s'" entry
)))
264 (unless (or em-preview
266 (not (y-or-n-p (format "rm: delete buffer `%s'? "
268 (eshell-funcalln 'kill-buffer entry
)))
269 ((eshell-processp entry
)
271 (eshell-printn (format "rm: killing process `%s'" entry
)))
272 (unless (or em-preview
274 (not (y-or-n-p (format "rm: kill process `%s'? "
276 (eshell-funcalln 'kill-process entry
)))
279 (eshell-printn (format "rm: uninterning symbol `%s'" entry
)))
283 (not (y-or-n-p (format "rm: unintern symbol `%s'? "
285 (eshell-funcalln 'unintern entry
)))
287 ;; -f should silently ignore missing files (bug#15373).
288 (unless (and force-removal
289 (not (file-exists-p entry
)))
290 (if (and (file-directory-p entry
)
291 (not (file-symlink-p entry
)))
293 eshell-rm-removes-directories
)
297 (format "rm: descend into directory `%s'? "
299 (eshell-remove-entries nil
(list entry
) t
))
300 (eshell-error (format "rm: %s: is a directory\n" entry
)))
301 (eshell-remove-entries nil
(list entry
) t
))))))
302 (setq args
(cdr args
)))
305 (put 'eshell
/rm
'eshell-no-numeric-conversions t
)
307 (defun eshell/mkdir
(&rest args
)
308 "Implementation of mkdir in Lisp."
309 (eshell-eval-using-options
311 '((?h
"help" nil nil
"show this usage screen")
312 (?p
"parents" nil em-parents
"make parent directories as needed")
315 :usage
"[OPTION] DIRECTORY...
316 Create the DIRECTORY(ies), if they do not already exist.")
318 (eshell-funcalln 'make-directory
(car args
) em-parents
)
319 (setq args
(cdr args
)))
322 (put 'eshell
/mkdir
'eshell-no-numeric-conversions t
)
324 (defun eshell/rmdir
(&rest args
)
325 "Implementation of rmdir in Lisp."
326 (eshell-eval-using-options
328 '((?h
"help" nil nil
"show this usage screen")
331 :usage
"[OPTION] DIRECTORY...
332 Remove the DIRECTORY(ies), if they are empty.")
334 (eshell-funcalln 'delete-directory
(car args
))
335 (setq args
(cdr args
)))
338 (put 'eshell
/rmdir
'eshell-no-numeric-conversions t
)
340 (defvar no-dereference
)
342 (defvar eshell-warn-dot-directories t
)
344 (defun eshell-shuffle-files (command action files target func deep
&rest args
)
345 "Shuffle around some filesystem entries, using FUNC to do the work."
346 (let ((attr-target (eshell-file-attributes target
))
347 (is-dir (or (file-directory-p target
)
348 (and em-preview
(not eshell-warn-dot-directories
))))
350 (if (and (not em-preview
) (not is-dir
)
351 (> (length files
) 1))
352 (error "%s: when %s multiple files, last argument must be a directory"
355 (setcar files
(directory-file-name (car files
)))
357 ((string-match "\\`\\.\\.?\\'"
358 (file-name-nondirectory (car files
)))
359 (if eshell-warn-dot-directories
360 (eshell-error (format "%s: %s: omitting directory\n"
361 command
(car files
)))))
363 (or (not (eshell-under-windows-p))
364 (eq system-type
'ms-dos
))
365 (setq attr
(eshell-file-attributes (car files
)))
366 (nth 10 attr-target
) (nth 10 attr
)
367 ;; Use equal, not -, since the inode and the device could
369 (equal (nth 10 attr-target
) (nth 10 attr
))
370 (nth 11 attr-target
) (nth 11 attr
)
371 (equal (nth 11 attr-target
) (nth 11 attr
)))
372 (eshell-error (format "%s: `%s' and `%s' are the same file\n"
373 command
(car files
) target
)))
375 (let ((source (car files
))
378 (file-name-nondirectory (car files
)) target
)
381 (if (and (file-directory-p source
)
382 (or (not no-dereference
)
383 (not (file-symlink-p source
)))
384 (not (memq func
'(make-symbolic-link
386 (if (and (eq func
'copy-file
)
388 (eshell-error (format "%s: %s: omitting directory\n"
389 command
(car files
)))
390 (let (eshell-warn-dot-directories)
392 (eq func
'rename-file
)
393 ;; Use equal, since the device might be a
395 (equal (nth 11 (eshell-file-attributes
398 (expand-file-name source
)))))
399 (nth 11 (eshell-file-attributes
402 (expand-file-name target
)))))))
403 (apply 'eshell-funcalln func source target args
)
404 (unless (file-directory-p target
)
407 (format "%s: making directory %s"
410 (eshell-funcalln 'make-directory target
)))
411 (apply 'eshell-shuffle-files
416 (concat source
"/" file
)))
417 (directory-files source
))
419 (when (eq func
'rename-file
)
422 (format "%s: deleting directory %s"
425 (eshell-funcalln 'delete-directory source
))))))
427 (eshell-printn (format "%s: %s -> %s" command
430 (if (and no-dereference
431 (setq link
(file-symlink-p source
)))
433 (apply 'eshell-funcalln
'make-symbolic-link
435 (if (eq func
'rename-file
)
436 (if (and (file-directory-p source
)
437 (not (file-symlink-p source
)))
438 (eshell-funcalln 'delete-directory source
)
439 (eshell-funcalln 'delete-file source
))))
440 (apply 'eshell-funcalln func source target args
)))))))
441 (setq files
(cdr files
)))))
443 (defun eshell-shorthand-tar-command (command args
)
444 "Rewrite `cp -v dir a.tar.gz' to `tar cvzf a.tar.gz dir'."
445 (let* ((archive (car (last args
)))
447 (cond ((string-match "z2" archive
) "If")
448 ((string-match "gz" archive
) "zf")
449 ((string-match "\\(az\\|Z\\)" archive
) "Zf")
451 (if (file-exists-p archive
)
452 (setq tar-args
(concat "u" tar-args
))
453 (setq tar-args
(concat "c" tar-args
)))
455 (setq tar-args
(concat "v" tar-args
)))
456 (if (equal command
"mv")
457 (setq tar-args
(concat "--remove-files -" tar-args
)))
458 ;; truncate the archive name from the arguments
459 (setcdr (last args
2) nil
)
460 (throw 'eshell-replace-command
461 (eshell-parse-command
462 (format "tar %s %s" tar-args archive
) args
))))
464 ;; this is to avoid duplicating code...
465 (defmacro eshell-mvcpln-template
(command action func query-var
466 force-var
&optional preserve
)
467 `(let ((len (length args
)))
469 (and (= len
1) (null eshell-default-target-is-dot
)))
470 (error "%s: missing destination file or directory" ,command
))
473 (setq args
(eshell-stringify-list (eshell-flatten-list args
)))
474 (if (and ,(not (equal command
"ln"))
475 (string-match eshell-tar-regexp
(car (last args
)))
476 (or (> (length args
) 2)
477 (and (file-directory-p (car args
))
478 (or (not no-dereference
)
479 (not (file-symlink-p (car args
)))))))
480 (eshell-shorthand-tar-command ,command args
)
481 (let ((target (car (last args
)))
483 (setcdr (last args
2) nil
)
484 (eshell-shuffle-files
485 ,command
,action args target
,func nil
487 `((if (and (or em-interactive
490 1 (or force
,force-var
)))
495 (defun eshell/mv
(&rest args
)
496 "Implementation of mv in Lisp."
497 (eshell-eval-using-options
499 '((?f
"force" nil force
500 "remove existing destinations, never prompt")
501 (?i
"interactive" nil em-interactive
502 "request confirmation if target already exists")
503 (?n
"preview" nil em-preview
504 "don't change anything on disk")
505 (?v
"verbose" nil em-verbose
506 "explain what is being done")
507 (nil "help" nil nil
"show this usage screen")
511 :usage
"[OPTION]... SOURCE DEST
512 or: mv [OPTION]... SOURCE... DIRECTORY
513 Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.
514 \[OPTION] DIRECTORY...")
515 (let ((no-dereference t
))
516 (eshell-mvcpln-template "mv" "moving" 'rename-file
517 eshell-mv-interactive-query
518 eshell-mv-overwrite-files
))))
520 (put 'eshell
/mv
'eshell-no-numeric-conversions t
)
522 (defun eshell/cp
(&rest args
)
523 "Implementation of cp in Lisp."
524 (eshell-eval-using-options
526 '((?a
"archive" nil archive
528 (?d
"no-dereference" nil no-dereference
530 (?f
"force" nil force
531 "remove existing destinations, never prompt")
532 (?i
"interactive" nil em-interactive
533 "request confirmation if target already exists")
534 (?n
"preview" nil em-preview
535 "don't change anything on disk")
536 (?p
"preserve" nil preserve
537 "preserve file attributes if possible")
538 (?r
"recursive" nil em-recursive
539 "copy directories recursively")
540 (?R nil nil em-recursive
542 (?v
"verbose" nil em-verbose
543 "explain what is being done")
544 (nil "help" nil nil
"show this usage screen")
548 :usage
"[OPTION]... SOURCE DEST
549 or: cp [OPTION]... SOURCE... DIRECTORY
550 Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.")
552 (setq preserve t no-dereference t em-recursive t
))
553 (eshell-mvcpln-template "cp" "copying" 'copy-file
554 eshell-cp-interactive-query
555 eshell-cp-overwrite-files preserve
)))
557 (put 'eshell
/cp
'eshell-no-numeric-conversions t
)
559 (defun eshell/ln
(&rest args
)
560 "Implementation of ln in Lisp."
561 (eshell-eval-using-options
563 '((?h
"help" nil nil
"show this usage screen")
564 (?s
"symbolic" nil symbolic
565 "make symbolic links instead of hard links")
566 (?i
"interactive" nil em-interactive
567 "request confirmation if target already exists")
568 (?f
"force" nil force
"remove existing destinations, never prompt")
569 (?n
"preview" nil em-preview
570 "don't change anything on disk")
571 (?v
"verbose" nil em-verbose
"explain what is being done")
575 :usage
"[OPTION]... TARGET [LINK_NAME]
576 or: ln [OPTION]... TARGET... DIRECTORY
577 Create a link to the specified TARGET with optional LINK_NAME. If there is
578 more than one TARGET, the last argument must be a directory; create links
579 in DIRECTORY to each TARGET. Create hard links by default, symbolic links
580 with '--symbolic'. When creating hard links, each TARGET must exist.")
581 (let ((no-dereference t
))
582 (eshell-mvcpln-template "ln" "linking"
586 eshell-ln-interactive-query
587 eshell-ln-overwrite-files
))))
589 (put 'eshell
/ln
'eshell-no-numeric-conversions t
)
591 (defun eshell/cat
(&rest args
)
592 "Implementation of cat in Lisp.
593 If in a pipeline, or the file is not a regular file, directory or
594 symlink, then revert to the system's definition of cat."
595 (setq args
(eshell-stringify-list (eshell-flatten-list args
)))
596 (if (or eshell-in-pipeline-p
599 (unless (or (and (stringp arg
)
601 (eq (aref arg
0) ?-
))
602 (let ((attrs (eshell-file-attributes arg
)))
603 (and attrs
(memq (aref (nth 8 attrs
) 0)
605 (throw 'special t
)))))
606 (let ((ext-cat (eshell-search-path "cat")))
608 (throw 'eshell-replace-command
609 (eshell-parse-command (eshell-quote-argument ext-cat
) args
))
610 (if eshell-in-pipeline-p
611 (error "Eshell's `cat' does not work in pipelines")
612 (error "Eshell's `cat' cannot display one of the files given"))))
613 (eshell-init-print-buffer)
614 (eshell-eval-using-options
616 '((?h
"help" nil nil
"show this usage screen")
619 :usage
"[OPTION] FILE...
620 Concatenate FILE(s), or standard input, to standard output.")
622 (if (string= file
"-")
623 (throw 'eshell-external
624 (eshell-external-command "cat" args
))))
625 (let ((curbuf (current-buffer)))
628 (insert-file-contents file
)
629 (goto-char (point-min))
631 (let ((str (buffer-substring
632 (point) (min (1+ (line-end-position))
634 (with-current-buffer curbuf
635 (eshell-buffered-print str
)))
638 ;; if the file does not end in a newline, do not emit one
639 (setq eshell-ensure-newline-p nil
))))
641 (put 'eshell
/cat
'eshell-no-numeric-conversions t
)
643 ;; special front-end functions for compilation-mode buffers
645 (defun eshell/make
(&rest args
)
646 "Use `compile' to do background makes."
647 (if (and eshell-current-subjob-p
648 (eshell-interactive-output-p))
649 (let ((compilation-process-setup-function
651 (list 'setq
'process-environment
652 (list 'quote
(eshell-copy-environment))))))
653 (compile (concat "make " (eshell-flatten-and-stringify args
))))
654 (throw 'eshell-replace-command
655 (eshell-parse-command "*make" (eshell-stringify-list
656 (eshell-flatten-list args
))))))
658 (put 'eshell
/make
'eshell-no-numeric-conversions t
)
660 (defun eshell-occur-mode-goto-occurrence ()
661 "Go to the occurrence the current line describes."
663 (let ((pos (occur-mode-find-occurrence)))
664 (pop-to-buffer (marker-buffer pos
))
665 (goto-char (marker-position pos
))))
667 (defun eshell-occur-mode-mouse-goto (event)
668 "In Occur mode, go to the occurrence whose line you click on."
671 (with-current-buffer (window-buffer (posn-window (event-end event
)))
673 (goto-char (posn-point (event-end event
)))
674 (setq pos
(occur-mode-find-occurrence))))
675 (pop-to-buffer (marker-buffer pos
))
676 (goto-char (marker-position pos
))))
678 (defun eshell-poor-mans-grep (args)
679 "A poor version of grep that opens every file and uses `occur'.
680 This eats up memory, since it leaves the buffers open (to speed future
681 searches), and it's very slow. But, if your system has no grep
683 (save-selected-window
684 (let ((default-dir default-directory
))
685 (with-current-buffer (get-buffer-create "*grep*")
686 (let ((inhibit-read-only t
)
687 (default-directory default-dir
))
690 (let ((files (eshell-stringify-list
691 (eshell-flatten-list (cdr args
))))
692 (inhibit-redisplay t
)
695 (if (get-buffer "*Occur*")
696 (kill-buffer (get-buffer "*Occur*")))
699 (with-current-buffer (find-file-noselect (car files
))
703 (if (get-buffer "*Occur*")
704 (with-current-buffer (get-buffer "*Occur*")
705 (setq string
(buffer-string))
706 (kill-buffer (current-buffer)))))
707 (if string
(insert string
))
709 files
(cdr files
)))))
710 (local-set-key [mouse-2
] 'eshell-occur-mode-mouse-goto
)
711 (local-set-key [(control ?c
) (control ?c
)]
712 'eshell-occur-mode-goto-occurrence
)
713 (local-set-key [(control ?m
)]
714 'eshell-occur-mode-goto-occurrence
)
715 (local-set-key [return] 'eshell-occur-mode-goto-occurrence)
716 (pop-to-buffer (current-buffer) t)
717 (goto-char (point-min))
718 (resize-temp-buffer-window))))))
720 (defvar compilation-scroll-output)
722 (defun eshell-grep (command args &optional maybe-use-occur)
723 "Generic service function for the various grep aliases.
724 It calls Emacs's grep utility if the command is not redirecting output,
725 and if it's not part of a command pipeline. Otherwise, it calls the
727 (if (and maybe-use-occur eshell-no-grep-available)
728 (eshell-poor-mans-grep args)
729 (if (or eshell-plain-grep-behavior
730 (not (and (eshell-interactive-output-p)
731 (not eshell-in-pipeline-p)
732 (not eshell-in-subcommand-p))))
733 (throw 'eshell-replace-command
734 (eshell-parse-command (concat "*" command)
735 (eshell-stringify-list
736 (eshell-flatten-list args))))
737 (let* ((args (mapconcat 'identity
738 (mapcar 'shell-quote-argument
739 (eshell-stringify-list
740 (eshell-flatten-list args)))
743 (set-text-properties 0 (length args)
745 (format "%s -n %s" command args)))
746 compilation-scroll-output)
749 (defun eshell/grep (&rest args)
750 "Use Emacs grep facility instead of calling external grep."
751 (eshell-grep "grep" args t))
753 (defun eshell/egrep (&rest args)
754 "Use Emacs grep facility instead of calling external egrep."
755 (eshell-grep "egrep" args t))
757 (defun eshell/fgrep (&rest args)
758 "Use Emacs grep facility instead of calling external fgrep."
759 (eshell-grep "fgrep" args t))
761 (defun eshell/agrep (&rest args)
762 "Use Emacs grep facility instead of calling external agrep."
763 (eshell-grep "agrep" args))
765 (defun eshell/glimpse (&rest args)
766 "Use Emacs grep facility instead of calling external glimpse."
768 (eshell-grep "glimpse" (append '("-z" "-y") args))))
770 ;; completions rules for some common UNIX commands
772 (defsubst eshell-complete-hostname ()
773 "Complete a command that wants a hostname for an argument."
774 (pcomplete-here (eshell-read-host-names)))
776 (defun eshell-complete-host-reference ()
777 "If there is a host reference, complete it."
778 (let ((arg (pcomplete-actual-arg))
780 (when (setq index (string-match "@[a-z.]*\\'" arg))
781 (setq pcomplete-stub (substring arg (1+ index))
782 pcomplete-last-completion-raw t)
783 (throw 'pcomplete-completions (eshell-read-host-names)))))
785 (defalias 'pcomplete/ftp 'eshell-complete-hostname)
786 (defalias 'pcomplete/ncftp 'eshell-complete-hostname)
787 (defalias 'pcomplete/ping 'eshell-complete-hostname)
788 (defalias 'pcomplete/rlogin 'eshell-complete-hostname)
790 (defun pcomplete/telnet ()
791 (require 'pcmpl-unix)
792 (pcomplete-opt "xl(pcmpl-unix-user-names)")
793 (eshell-complete-hostname))
795 (defun pcomplete/rsh ()
796 "Complete `rsh', which, after the user and hostname, is like xargs."
797 (require 'pcmpl-unix)
798 (pcomplete-opt "l(pcmpl-unix-user-names)")
799 (eshell-complete-hostname)
800 (pcomplete-here (funcall pcomplete-command-completion-function))
801 (funcall (or (pcomplete-find-completion-function (pcomplete-arg 1))
802 pcomplete-default-completion-function)))
806 (defvar dereference-links)
808 (defvar human-readable)
810 (defvar only-one-filesystem)
813 (defsubst eshell-du-size-string (size)
814 (let* ((str (eshell-printable-size size human-readable block-size t))
816 (concat str (if (< len 8)
817 (make-string (- 8 len) ? )))))
819 (defun eshell-du-sum-directory (path depth)
820 "Summarize PATH, and its member directories."
821 (let ((entries (eshell-directory-files-and-attributes path))
824 (unless (string-match "\\`\\.\\.?\\'" (caar entries))
825 (let* ((entry (concat path "/"
827 (symlink (and (stringp (cadr (car entries)))
828 (cadr (car entries)))))
829 (unless (or (and symlink (not dereference-links))
830 (and only-one-filesystem
831 (/= only-one-filesystem
832 (nth 12 (car entries)))))
834 (setq entry symlink))
837 (if (eq t (cadr (car entries)))
838 (eshell-du-sum-directory entry (1+ depth))
839 (let ((file-size (nth 8 (car entries))))
844 (concat (eshell-du-size-string file-size)
845 entry "\n")))))))))))
846 (setq entries (cdr entries)))
847 (if (or (not max-depth)
850 (eshell-print (concat (eshell-du-size-string size)
851 (directory-file-name path) "\n")))
854 (defun eshell/du (&rest args)
855 "Implementation of \"du\" in Lisp, passing ARGS."
857 (eshell-stringify-list (eshell-flatten-list args))
859 (let ((ext-du (eshell-search-path "du")))
861 (not (catch 'have-ange-path
864 (file-remote-p (expand-file-name arg) 'method) "ftp")
865 (throw 'have-ange-path t))))))
866 (throw 'eshell-replace-command
867 (eshell-parse-command (eshell-quote-argument ext-du) args))
868 (eshell-eval-using-options
870 '((?a "all" nil show-all
871 "write counts for all files, not just directories")
872 (nil "block-size" t block-size
873 "use SIZE-byte blocks (i.e., --block-size SIZE)")
874 (?b "bytes" nil by-bytes
875 "print size in bytes")
876 (?c "total" nil grand-total
877 "produce a grand total")
878 (?d "max-depth" t max-depth
879 "display data only this many levels of data")
880 (?h "human-readable" 1024 human-readable
881 "print sizes in human readable format")
882 (?H "is" 1000 human-readable
883 "likewise, but use powers of 1000 not 1024")
884 (?k "kilobytes" 1024 block-size
885 "like --block-size 1024")
886 (?L "dereference" nil dereference-links
887 "dereference all symbolic links")
888 (?m "megabytes" 1048576 block-size
889 "like --block-size 1048576")
890 (?s "summarize" 0 max-depth
891 "display only a total for each argument")
892 (?x "one-file-system" nil only-one-filesystem
893 "skip directories on different filesystems")
895 "show this usage screen")
897 :usage "[OPTION]... FILE...
898 Summarize disk usage of each FILE, recursively for directories.")
900 (setq block-size (or block-size 1024)))
901 (if (and max-depth (stringp max-depth))
902 (setq max-depth (string-to-number max-depth)))
903 ;; filesystem support means nothing under Windows
904 (if (eshell-under-windows-p)
905 (setq only-one-filesystem nil))
906 (let ((size 0.0) ange-cache)
908 (if only-one-filesystem
909 (setq only-one-filesystem
910 (nth 11 (eshell-file-attributes
911 (file-name-as-directory (car args))))))
912 (setq size (+ size (eshell-du-sum-directory
913 (directory-file-name (car args)) 0)))
914 (setq args (cdr args)))
916 (eshell-print (concat (eshell-du-size-string size)
919 (defvar eshell-time-start nil)
921 (defun eshell-show-elapsed-time ()
922 (let ((elapsed (format "%.3f secs\n" (- (float-time) eshell-time-start))))
923 (set-text-properties 0 (length elapsed) '(face bold) elapsed)
924 (eshell-interactive-print elapsed))
925 (remove-hook 'eshell-post-command-hook 'eshell-show-elapsed-time t))
927 (defun eshell/time (&rest args)
928 "Implementation of \"time\" in Lisp."
929 (let ((time-args (copy-alist args))
932 (while (and continue args)
933 (if (not (string-match "^-" (car args)))
936 (setcdr last-arg nil)
941 (eshell-eval-using-options
943 '((?h "help" nil nil "show this usage screen")
947 Show wall-clock time elapsed during execution of COMMAND.")
948 (setq eshell-time-start (float-time))
949 (add-hook 'eshell-post-command-hook 'eshell-show-elapsed-time nil t)
951 (throw 'eshell-replace-command
952 (eshell-parse-command (car time-args)
953 ;;; http://lists.gnu.org/archive/html/bug-gnu-emacs/2007-08/msg00205.html
954 (eshell-stringify-list
955 (eshell-flatten-list (cdr time-args))))))))
957 (defun eshell/whoami (&rest args)
958 "Make \"whoami\" Tramp aware."
959 (or (file-remote-p default-directory 'user) (user-login-name)))
961 (defvar eshell-diff-window-config nil)
963 (defun eshell-diff-quit ()
964 "Restore the window configuration previous to diff'ing."
966 (if eshell-diff-window-config
967 (set-window-configuration eshell-diff-window-config)))
969 (defun nil-blank-string (string)
970 "Return STRING, or nil if STRING contains only non-blank characters."
972 ((string-match "[^[:blank:]]" string) string)
975 (autoload 'diff-no-select "diff")
977 (defun eshell/diff (&rest args)
978 "Alias \"diff\" to call Emacs `diff' function."
979 (let ((orig-args (eshell-stringify-list (eshell-flatten-list args))))
980 (if (or eshell-plain-diff-behavior
981 (not (and (eshell-interactive-output-p)
982 (not eshell-in-pipeline-p)
983 (not eshell-in-subcommand-p))))
984 (throw 'eshell-replace-command
985 (eshell-parse-command "*diff" orig-args))
986 (setq args (copy-sequence orig-args))
987 (if (< (length args) 2)
988 (throw 'eshell-replace-command
989 (eshell-parse-command "*diff" orig-args)))
990 (let ((old (car (last args 2)))
991 (new (car (last args)))
992 (config (current-window-configuration)))
993 (if (= (length args) 2)
995 (setcdr (last args 3) nil))
1000 (nil-blank-string (eshell-flatten-and-stringify args)))
1002 (throw 'eshell-replace-command
1003 (eshell-parse-command "*diff" orig-args))))
1004 (when (fboundp 'diff-mode)
1005 (make-local-variable 'compilation-finish-functions)
1007 'compilation-finish-functions
1009 (with-current-buffer buff
1011 (set (make-local-variable 'eshell-diff-window-config)
1013 (local-set-key [?q] 'eshell-diff-quit)
1014 (if (fboundp 'turn-on-font-lock-if-enabled)
1015 (turn-on-font-lock-if-enabled))
1016 (goto-char (point-min))))))
1017 (pop-to-buffer (current-buffer))))))
1020 (put 'eshell/diff 'eshell-no-numeric-conversions t)
1022 (defvar locate-history-list)
1024 (defun eshell/locate (&rest args)
1025 "Alias \"locate\" to call Emacs `locate' function."
1026 (if (or eshell-plain-locate-behavior
1027 (not (and (eshell-interactive-output-p)
1028 (not eshell-in-pipeline-p)
1029 (not eshell-in-subcommand-p)))
1030 (and (stringp (car args))
1031 (string-match "^-" (car args))))
1032 (throw 'eshell-replace-command
1033 (eshell-parse-command "*locate" (eshell-stringify-list
1034 (eshell-flatten-list args))))
1035 (save-selected-window
1036 (let ((locate-history-list (list (car args))))
1037 (locate-with-filter (car args) (cadr args))))))
1039 (put 'eshell/locate 'eshell-no-numeric-conversions t)
1041 (defun eshell/occur (&rest args)
1042 "Alias \"occur\" to call Emacs `occur' function."
1043 (let ((inhibit-read-only t))
1044 (if (> (length args) 2)
1045 (error "usage: occur: (REGEXP &optional NLINES)")
1046 (apply 'occur args))))
1048 (put 'eshell/occur 'eshell-no-numeric-conversions t)
1053 ;; generated-autoload-file: "esh-groups.el"
1056 ;;; em-unix.el ends here