1 ;;; em-unix.el --- UNIX command aliases
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 (if (and (file-directory-p entry
)
288 (not (file-symlink-p entry
)))
290 eshell-rm-removes-directories
)
294 (format "rm: descend into directory `%s'? "
296 (eshell-remove-entries nil
(list entry
) t
))
297 (eshell-error (format "rm: %s: is a directory\n" entry
)))
298 (eshell-remove-entries nil
(list entry
) t
)))))
299 (setq args
(cdr args
)))
302 (put 'eshell
/rm
'eshell-no-numeric-conversions t
)
304 (defun eshell/mkdir
(&rest args
)
305 "Implementation of mkdir in Lisp."
306 (eshell-eval-using-options
308 '((?h
"help" nil nil
"show this usage screen")
309 (?p
"parents" nil em-parents
"make parent directories as needed")
312 :usage
"[OPTION] DIRECTORY...
313 Create the DIRECTORY(ies), if they do not already exist.")
315 (eshell-funcalln 'make-directory
(car args
) em-parents
)
316 (setq args
(cdr args
)))
319 (put 'eshell
/mkdir
'eshell-no-numeric-conversions t
)
321 (defun eshell/rmdir
(&rest args
)
322 "Implementation of rmdir in Lisp."
323 (eshell-eval-using-options
325 '((?h
"help" nil nil
"show this usage screen")
328 :usage
"[OPTION] DIRECTORY...
329 Remove the DIRECTORY(ies), if they are empty.")
331 (eshell-funcalln 'delete-directory
(car args
))
332 (setq args
(cdr args
)))
335 (put 'eshell
/rmdir
'eshell-no-numeric-conversions t
)
337 (defvar no-dereference
)
339 (defvar eshell-warn-dot-directories t
)
341 (defun eshell-shuffle-files (command action files target func deep
&rest args
)
342 "Shuffle around some filesystem entries, using FUNC to do the work."
343 (let ((attr-target (eshell-file-attributes target
))
344 (is-dir (or (file-directory-p target
)
345 (and em-preview
(not eshell-warn-dot-directories
))))
347 (if (and (not em-preview
) (not is-dir
)
348 (> (length files
) 1))
349 (error "%s: when %s multiple files, last argument must be a directory"
352 (setcar files
(directory-file-name (car files
)))
354 ((string-match "\\`\\.\\.?\\'"
355 (file-name-nondirectory (car files
)))
356 (if eshell-warn-dot-directories
357 (eshell-error (format "%s: %s: omitting directory\n"
358 command
(car files
)))))
360 (or (not (eshell-under-windows-p))
361 (eq system-type
'ms-dos
))
362 (setq attr
(eshell-file-attributes (car files
)))
363 (nth 10 attr-target
) (nth 10 attr
)
364 ;; Use equal, not -, since the inode and the device could
366 (equal (nth 10 attr-target
) (nth 10 attr
))
367 (nth 11 attr-target
) (nth 11 attr
)
368 (equal (nth 11 attr-target
) (nth 11 attr
)))
369 (eshell-error (format "%s: `%s' and `%s' are the same file\n"
370 command
(car files
) target
)))
372 (let ((source (car files
))
375 (file-name-nondirectory (car files
)) target
)
378 (if (and (file-directory-p source
)
379 (or (not no-dereference
)
380 (not (file-symlink-p source
)))
381 (not (memq func
'(make-symbolic-link
383 (if (and (eq func
'copy-file
)
385 (eshell-error (format "%s: %s: omitting directory\n"
386 command
(car files
)))
387 (let (eshell-warn-dot-directories)
389 (eq func
'rename-file
)
390 ;; Use equal, since the device might be a
392 (equal (nth 11 (eshell-file-attributes
395 (expand-file-name source
)))))
396 (nth 11 (eshell-file-attributes
399 (expand-file-name target
)))))))
400 (apply 'eshell-funcalln func source target args
)
401 (unless (file-directory-p target
)
404 (format "%s: making directory %s"
407 (eshell-funcalln 'make-directory target
)))
408 (apply 'eshell-shuffle-files
413 (concat source
"/" file
)))
414 (directory-files source
))
416 (when (eq func
'rename-file
)
419 (format "%s: deleting directory %s"
422 (eshell-funcalln 'delete-directory source
))))))
424 (eshell-printn (format "%s: %s -> %s" command
427 (if (and no-dereference
428 (setq link
(file-symlink-p source
)))
430 (apply 'eshell-funcalln
'make-symbolic-link
432 (if (eq func
'rename-file
)
433 (if (and (file-directory-p source
)
434 (not (file-symlink-p source
)))
435 (eshell-funcalln 'delete-directory source
)
436 (eshell-funcalln 'delete-file source
))))
437 (apply 'eshell-funcalln func source target args
)))))))
438 (setq files
(cdr files
)))))
440 (defun eshell-shorthand-tar-command (command args
)
441 "Rewrite `cp -v dir a.tar.gz' to `tar cvzf a.tar.gz dir'."
442 (let* ((archive (car (last args
)))
444 (cond ((string-match "z2" archive
) "If")
445 ((string-match "gz" archive
) "zf")
446 ((string-match "\\(az\\|Z\\)" archive
) "Zf")
448 (if (file-exists-p archive
)
449 (setq tar-args
(concat "u" tar-args
))
450 (setq tar-args
(concat "c" tar-args
)))
452 (setq tar-args
(concat "v" tar-args
)))
453 (if (equal command
"mv")
454 (setq tar-args
(concat "--remove-files -" tar-args
)))
455 ;; truncate the archive name from the arguments
456 (setcdr (last args
2) nil
)
457 (throw 'eshell-replace-command
458 (eshell-parse-command
459 (format "tar %s %s" tar-args archive
) args
))))
461 ;; this is to avoid duplicating code...
462 (defmacro eshell-mvcpln-template
(command action func query-var
463 force-var
&optional preserve
)
464 `(let ((len (length args
)))
466 (and (= len
1) (null eshell-default-target-is-dot
)))
467 (error "%s: missing destination file or directory" ,command
))
470 (setq args
(eshell-stringify-list (eshell-flatten-list args
)))
471 (if (and ,(not (equal command
"ln"))
472 (string-match eshell-tar-regexp
(car (last args
)))
473 (or (> (length args
) 2)
474 (and (file-directory-p (car args
))
475 (or (not no-dereference
)
476 (not (file-symlink-p (car args
)))))))
477 (eshell-shorthand-tar-command ,command args
)
478 (let ((target (car (last args
)))
480 (setcdr (last args
2) nil
)
481 (eshell-shuffle-files
482 ,command
,action args target
,func nil
484 `((if (and (or em-interactive
487 1 (or force
,force-var
)))
492 (defun eshell/mv
(&rest args
)
493 "Implementation of mv in Lisp."
494 (eshell-eval-using-options
496 '((?f
"force" nil force
497 "remove existing destinations, never prompt")
498 (?i
"interactive" nil em-interactive
499 "request confirmation if target already exists")
500 (?n
"preview" nil em-preview
501 "don't change anything on disk")
502 (?v
"verbose" nil em-verbose
503 "explain what is being done")
504 (nil "help" nil nil
"show this usage screen")
508 :usage
"[OPTION]... SOURCE DEST
509 or: mv [OPTION]... SOURCE... DIRECTORY
510 Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.
511 \[OPTION] DIRECTORY...")
512 (let ((no-dereference t
))
513 (eshell-mvcpln-template "mv" "moving" 'rename-file
514 eshell-mv-interactive-query
515 eshell-mv-overwrite-files
))))
517 (put 'eshell
/mv
'eshell-no-numeric-conversions t
)
519 (defun eshell/cp
(&rest args
)
520 "Implementation of cp in Lisp."
521 (eshell-eval-using-options
523 '((?a
"archive" nil archive
525 (?d
"no-dereference" nil no-dereference
527 (?f
"force" nil force
528 "remove existing destinations, never prompt")
529 (?i
"interactive" nil em-interactive
530 "request confirmation if target already exists")
531 (?n
"preview" nil em-preview
532 "don't change anything on disk")
533 (?p
"preserve" nil preserve
534 "preserve file attributes if possible")
535 (?r
"recursive" nil em-recursive
536 "copy directories recursively")
537 (?R nil nil em-recursive
539 (?v
"verbose" nil em-verbose
540 "explain what is being done")
541 (nil "help" nil nil
"show this usage screen")
545 :usage
"[OPTION]... SOURCE DEST
546 or: cp [OPTION]... SOURCE... DIRECTORY
547 Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.")
549 (setq preserve t no-dereference t em-recursive t
))
550 (eshell-mvcpln-template "cp" "copying" 'copy-file
551 eshell-cp-interactive-query
552 eshell-cp-overwrite-files preserve
)))
554 (put 'eshell
/cp
'eshell-no-numeric-conversions t
)
556 (defun eshell/ln
(&rest args
)
557 "Implementation of ln in Lisp."
558 (eshell-eval-using-options
560 '((?h
"help" nil nil
"show this usage screen")
561 (?s
"symbolic" nil symbolic
562 "make symbolic links instead of hard links")
563 (?i
"interactive" nil em-interactive
564 "request confirmation if target already exists")
565 (?f
"force" nil force
"remove existing destinations, never prompt")
566 (?n
"preview" nil em-preview
567 "don't change anything on disk")
568 (?v
"verbose" nil em-verbose
"explain what is being done")
572 :usage
"[OPTION]... TARGET [LINK_NAME]
573 or: ln [OPTION]... TARGET... DIRECTORY
574 Create a link to the specified TARGET with optional LINK_NAME. If there is
575 more than one TARGET, the last argument must be a directory; create links
576 in DIRECTORY to each TARGET. Create hard links by default, symbolic links
577 with '--symbolic'. When creating hard links, each TARGET must exist.")
578 (let ((no-dereference t
))
579 (eshell-mvcpln-template "ln" "linking"
583 eshell-ln-interactive-query
584 eshell-ln-overwrite-files
))))
586 (put 'eshell
/ln
'eshell-no-numeric-conversions t
)
588 (defun eshell/cat
(&rest args
)
589 "Implementation of cat in Lisp.
590 If in a pipeline, or the file is not a regular file, directory or
591 symlink, then revert to the system's definition of cat."
592 (setq args
(eshell-stringify-list (eshell-flatten-list args
)))
593 (if (or eshell-in-pipeline-p
596 (unless (or (and (stringp arg
)
598 (eq (aref arg
0) ?-
))
599 (let ((attrs (eshell-file-attributes arg
)))
600 (and attrs
(memq (aref (nth 8 attrs
) 0)
602 (throw 'special t
)))))
603 (let ((ext-cat (eshell-search-path "cat")))
605 (throw 'eshell-replace-command
606 (eshell-parse-command (eshell-quote-argument ext-cat
) args
))
607 (if eshell-in-pipeline-p
608 (error "Eshell's `cat' does not work in pipelines")
609 (error "Eshell's `cat' cannot display one of the files given"))))
610 (eshell-init-print-buffer)
611 (eshell-eval-using-options
613 '((?h
"help" nil nil
"show this usage screen")
616 :usage
"[OPTION] FILE...
617 Concatenate FILE(s), or standard input, to standard output.")
619 (if (string= file
"-")
620 (throw 'eshell-external
621 (eshell-external-command "cat" args
))))
622 (let ((curbuf (current-buffer)))
625 (insert-file-contents file
)
626 (goto-char (point-min))
628 (let ((str (buffer-substring
629 (point) (min (1+ (line-end-position))
631 (with-current-buffer curbuf
632 (eshell-buffered-print str
)))
635 ;; if the file does not end in a newline, do not emit one
636 (setq eshell-ensure-newline-p nil
))))
638 (put 'eshell
/cat
'eshell-no-numeric-conversions t
)
640 ;; special front-end functions for compilation-mode buffers
642 (defun eshell/make
(&rest args
)
643 "Use `compile' to do background makes."
644 (if (and eshell-current-subjob-p
645 (eshell-interactive-output-p))
646 (let ((compilation-process-setup-function
648 (list 'setq
'process-environment
649 (list 'quote
(eshell-copy-environment))))))
650 (compile (concat "make " (eshell-flatten-and-stringify args
))))
651 (throw 'eshell-replace-command
652 (eshell-parse-command "*make" (eshell-stringify-list
653 (eshell-flatten-list args
))))))
655 (put 'eshell
/make
'eshell-no-numeric-conversions t
)
657 (defun eshell-occur-mode-goto-occurrence ()
658 "Go to the occurrence the current line describes."
660 (let ((pos (occur-mode-find-occurrence)))
661 (pop-to-buffer (marker-buffer pos
))
662 (goto-char (marker-position pos
))))
664 (defun eshell-occur-mode-mouse-goto (event)
665 "In Occur mode, go to the occurrence whose line you click on."
668 (with-current-buffer (window-buffer (posn-window (event-end event
)))
670 (goto-char (posn-point (event-end event
)))
671 (setq pos
(occur-mode-find-occurrence))))
672 (pop-to-buffer (marker-buffer pos
))
673 (goto-char (marker-position pos
))))
675 (defun eshell-poor-mans-grep (args)
676 "A poor version of grep that opens every file and uses `occur'.
677 This eats up memory, since it leaves the buffers open (to speed future
678 searches), and it's very slow. But, if your system has no grep
680 (save-selected-window
681 (let ((default-dir default-directory
))
682 (with-current-buffer (get-buffer-create "*grep*")
683 (let ((inhibit-read-only t
)
684 (default-directory default-dir
))
687 (let ((files (eshell-stringify-list
688 (eshell-flatten-list (cdr args
))))
689 (inhibit-redisplay t
)
692 (if (get-buffer "*Occur*")
693 (kill-buffer (get-buffer "*Occur*")))
696 (with-current-buffer (find-file-noselect (car files
))
700 (if (get-buffer "*Occur*")
701 (with-current-buffer (get-buffer "*Occur*")
702 (setq string
(buffer-string))
703 (kill-buffer (current-buffer)))))
704 (if string
(insert string
))
706 files
(cdr files
)))))
707 (local-set-key [mouse-2
] 'eshell-occur-mode-mouse-goto
)
708 (local-set-key [(control ?c
) (control ?c
)]
709 'eshell-occur-mode-goto-occurrence
)
710 (local-set-key [(control ?m
)]
711 'eshell-occur-mode-goto-occurrence
)
712 (local-set-key [return] 'eshell-occur-mode-goto-occurrence)
713 (pop-to-buffer (current-buffer) t)
714 (goto-char (point-min))
715 (resize-temp-buffer-window))))))
717 (defun eshell-grep (command args &optional maybe-use-occur)
718 "Generic service function for the various grep aliases.
719 It calls Emacs's grep utility if the command is not redirecting output,
720 and if it's not part of a command pipeline. Otherwise, it calls the
722 (if (and maybe-use-occur eshell-no-grep-available)
723 (eshell-poor-mans-grep args)
724 (if (or eshell-plain-grep-behavior
725 (not (and (eshell-interactive-output-p)
726 (not eshell-in-pipeline-p)
727 (not eshell-in-subcommand-p))))
728 (throw 'eshell-replace-command
729 (eshell-parse-command (concat "*" command)
730 (eshell-stringify-list
731 (eshell-flatten-list args))))
732 (let* ((args (mapconcat 'identity
733 (mapcar 'shell-quote-argument
734 (eshell-stringify-list
735 (eshell-flatten-list args)))
738 (set-text-properties 0 (length args)
740 (format "%s -n %s" command args)))
741 compilation-scroll-output)
744 (defun eshell/grep (&rest args)
745 "Use Emacs grep facility instead of calling external grep."
746 (eshell-grep "grep" args t))
748 (defun eshell/egrep (&rest args)
749 "Use Emacs grep facility instead of calling external egrep."
750 (eshell-grep "egrep" args t))
752 (defun eshell/fgrep (&rest args)
753 "Use Emacs grep facility instead of calling external fgrep."
754 (eshell-grep "fgrep" args t))
756 (defun eshell/agrep (&rest args)
757 "Use Emacs grep facility instead of calling external agrep."
758 (eshell-grep "agrep" args))
760 (defun eshell/glimpse (&rest args)
761 "Use Emacs grep facility instead of calling external glimpse."
763 (eshell-grep "glimpse" (append '("-z" "-y") args))))
765 ;; completions rules for some common UNIX commands
767 (defsubst eshell-complete-hostname ()
768 "Complete a command that wants a hostname for an argument."
769 (pcomplete-here (eshell-read-host-names)))
771 (defun eshell-complete-host-reference ()
772 "If there is a host reference, complete it."
773 (let ((arg (pcomplete-actual-arg))
775 (when (setq index (string-match "@[a-z.]*\\'" arg))
776 (setq pcomplete-stub (substring arg (1+ index))
777 pcomplete-last-completion-raw t)
778 (throw 'pcomplete-completions (eshell-read-host-names)))))
780 (defalias 'pcomplete/ftp 'eshell-complete-hostname)
781 (defalias 'pcomplete/ncftp 'eshell-complete-hostname)
782 (defalias 'pcomplete/ping 'eshell-complete-hostname)
783 (defalias 'pcomplete/rlogin 'eshell-complete-hostname)
785 (defun pcomplete/telnet ()
786 (require 'pcmpl-unix)
787 (pcomplete-opt "xl(pcmpl-unix-user-names)")
788 (eshell-complete-hostname))
790 (defun pcomplete/rsh ()
791 "Complete `rsh', which, after the user and hostname, is like xargs."
792 (require 'pcmpl-unix)
793 (pcomplete-opt "l(pcmpl-unix-user-names)")
794 (eshell-complete-hostname)
795 (pcomplete-here (funcall pcomplete-command-completion-function))
796 (funcall (or (pcomplete-find-completion-function (pcomplete-arg 1))
797 pcomplete-default-completion-function)))
801 (defvar dereference-links)
803 (defvar human-readable)
805 (defvar only-one-filesystem)
808 (defsubst eshell-du-size-string (size)
809 (let* ((str (eshell-printable-size size human-readable block-size t))
811 (concat str (if (< len 8)
812 (make-string (- 8 len) ? )))))
814 (defun eshell-du-sum-directory (path depth)
815 "Summarize PATH, and its member directories."
816 (let ((entries (eshell-directory-files-and-attributes path))
819 (unless (string-match "\\`\\.\\.?\\'" (caar entries))
820 (let* ((entry (concat path "/"
822 (symlink (and (stringp (cadr (car entries)))
823 (cadr (car entries)))))
824 (unless (or (and symlink (not dereference-links))
825 (and only-one-filesystem
826 (/= only-one-filesystem
827 (nth 12 (car entries)))))
829 (setq entry symlink))
832 (if (eq t (cadr (car entries)))
833 (eshell-du-sum-directory entry (1+ depth))
834 (let ((file-size (nth 8 (car entries))))
839 (concat (eshell-du-size-string file-size)
840 entry "\n")))))))))))
841 (setq entries (cdr entries)))
842 (if (or (not max-depth)
845 (eshell-print (concat (eshell-du-size-string size)
846 (directory-file-name path) "\n")))
849 (defun eshell/du (&rest args)
850 "Implementation of \"du\" in Lisp, passing ARGS."
852 (eshell-stringify-list (eshell-flatten-list args))
854 (let ((ext-du (eshell-search-path "du")))
856 (not (catch 'have-ange-path
859 (file-remote-p (expand-file-name arg) 'method) "ftp")
860 (throw 'have-ange-path t))))))
861 (throw 'eshell-replace-command
862 (eshell-parse-command (eshell-quote-argument ext-du) args))
863 (eshell-eval-using-options
865 '((?a "all" nil show-all
866 "write counts for all files, not just directories")
867 (nil "block-size" t block-size
868 "use SIZE-byte blocks (i.e., --block-size SIZE)")
869 (?b "bytes" nil by-bytes
870 "print size in bytes")
871 (?c "total" nil grand-total
872 "produce a grand total")
873 (?d "max-depth" t max-depth
874 "display data only this many levels of data")
875 (?h "human-readable" 1024 human-readable
876 "print sizes in human readable format")
877 (?H "is" 1000 human-readable
878 "likewise, but use powers of 1000 not 1024")
879 (?k "kilobytes" 1024 block-size
880 "like --block-size 1024")
881 (?L "dereference" nil dereference-links
882 "dereference all symbolic links")
883 (?m "megabytes" 1048576 block-size
884 "like --block-size 1048576")
885 (?s "summarize" 0 max-depth
886 "display only a total for each argument")
887 (?x "one-file-system" nil only-one-filesystem
888 "skip directories on different filesystems")
890 "show this usage screen")
892 :usage "[OPTION]... FILE...
893 Summarize disk usage of each FILE, recursively for directories.")
895 (setq block-size (or block-size 1024)))
896 (if (and max-depth (stringp max-depth))
897 (setq max-depth (string-to-number max-depth)))
898 ;; filesystem support means nothing under Windows
899 (if (eshell-under-windows-p)
900 (setq only-one-filesystem nil))
901 (let ((size 0.0) ange-cache)
903 (if only-one-filesystem
904 (setq only-one-filesystem
905 (nth 11 (eshell-file-attributes
906 (file-name-as-directory (car args))))))
907 (setq size (+ size (eshell-du-sum-directory
908 (directory-file-name (car args)) 0)))
909 (setq args (cdr args)))
911 (eshell-print (concat (eshell-du-size-string size)
914 (defvar eshell-time-start nil)
916 (defun eshell-show-elapsed-time ()
917 (let ((elapsed (format "%.3f secs\n" (- (float-time) eshell-time-start))))
918 (set-text-properties 0 (length elapsed) '(face bold) elapsed)
919 (eshell-interactive-print elapsed))
920 (remove-hook 'eshell-post-command-hook 'eshell-show-elapsed-time t))
922 (defun eshell/time (&rest args)
923 "Implementation of \"time\" in Lisp."
924 (let ((time-args (copy-alist args))
927 (while (and continue args)
928 (if (not (string-match "^-" (car args)))
931 (setcdr last-arg nil)
936 (eshell-eval-using-options
938 '((?h "help" nil nil "show this usage screen")
942 Show wall-clock time elapsed during execution of COMMAND.")
943 (setq eshell-time-start (float-time))
944 (add-hook 'eshell-post-command-hook 'eshell-show-elapsed-time nil t)
946 (throw 'eshell-replace-command
947 (eshell-parse-command (car time-args)
948 ;;; http://lists.gnu.org/archive/html/bug-gnu-emacs/2007-08/msg00205.html
949 (eshell-stringify-list
950 (eshell-flatten-list (cdr time-args))))))))
952 (defun eshell/whoami (&rest args)
953 "Make \"whoami\" Tramp aware."
954 (or (file-remote-p default-directory 'user) (user-login-name)))
956 (defvar eshell-diff-window-config nil)
958 (defun eshell-diff-quit ()
959 "Restore the window configuration previous to diff'ing."
961 (if eshell-diff-window-config
962 (set-window-configuration eshell-diff-window-config)))
964 (defun nil-blank-string (string)
965 "Return STRING, or nil if STRING contains only non-blank characters."
967 ((string-match "[^[:blank:]]" string) string)
970 (autoload 'diff-no-select "diff")
972 (defun eshell/diff (&rest args)
973 "Alias \"diff\" to call Emacs `diff' function."
974 (let ((orig-args (eshell-stringify-list (eshell-flatten-list args))))
975 (if (or eshell-plain-diff-behavior
976 (not (and (eshell-interactive-output-p)
977 (not eshell-in-pipeline-p)
978 (not eshell-in-subcommand-p))))
979 (throw 'eshell-replace-command
980 (eshell-parse-command "*diff" orig-args))
981 (setq args (copy-sequence orig-args))
982 (if (< (length args) 2)
983 (throw 'eshell-replace-command
984 (eshell-parse-command "*diff" orig-args)))
985 (let ((old (car (last args 2)))
986 (new (car (last args)))
987 (config (current-window-configuration)))
988 (if (= (length args) 2)
990 (setcdr (last args 3) nil))
995 (nil-blank-string (eshell-flatten-and-stringify args)))
997 (throw 'eshell-replace-command
998 (eshell-parse-command "*diff" orig-args))))
999 (when (fboundp 'diff-mode)
1000 (make-local-variable 'compilation-finish-functions)
1002 'compilation-finish-functions
1004 (with-current-buffer buff
1006 (set (make-local-variable 'eshell-diff-window-config)
1008 (local-set-key [?q] 'eshell-diff-quit)
1009 (if (fboundp 'turn-on-font-lock-if-enabled)
1010 (turn-on-font-lock-if-enabled))
1011 (goto-char (point-min))))))
1012 (pop-to-buffer (current-buffer))))))
1015 (put 'eshell/diff 'eshell-no-numeric-conversions t)
1017 (defun eshell/locate (&rest args)
1018 "Alias \"locate\" to call Emacs `locate' function."
1019 (if (or eshell-plain-locate-behavior
1020 (not (and (eshell-interactive-output-p)
1021 (not eshell-in-pipeline-p)
1022 (not eshell-in-subcommand-p)))
1023 (and (stringp (car args))
1024 (string-match "^-" (car args))))
1025 (throw 'eshell-replace-command
1026 (eshell-parse-command "*locate" (eshell-stringify-list
1027 (eshell-flatten-list args))))
1028 (save-selected-window
1029 (let ((locate-history-list (list (car args))))
1030 (locate-with-filter (car args) (cadr args))))))
1032 (put 'eshell/locate 'eshell-no-numeric-conversions t)
1034 (defun eshell/occur (&rest args)
1035 "Alias \"occur\" to call Emacs `occur' function."
1036 (let ((inhibit-read-only t))
1037 (if (> (length args) 2)
1038 (error "usage: occur: (REGEXP &optional NLINES)")
1039 (apply 'occur args))))
1041 (put 'eshell/occur 'eshell-no-numeric-conversions t)
1046 ;; generated-autoload-file: "esh-groups.el"
1049 ;;; em-unix.el ends here