1 ;;; em-unix.el --- UNIX command aliases
3 ;; Copyright (C) 1999-2011 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.
43 (eshell-defgroup eshell-unix nil
44 "This module defines many of the more common UNIX utilities as
45 aliases implemented in Lisp. These include mv, ln, cp, rm, etc. If
46 the user passes arguments which are too complex, or are unrecognized
47 by the Lisp variant, the external version will be called (if
48 available). The only reason not to use them would be because they are
49 usually much slower. But in several cases their tight integration
50 with Eshell makes them more versatile than their traditional cousins
51 \(such as being able to use `kill' to kill Eshell background processes
53 :tag
"UNIX commands in Lisp"
54 :group
'eshell-module
)
56 (defcustom eshell-unix-load-hook nil
57 "A list of functions to run when `eshell-unix' is loaded."
58 :version
"24.1" ; removed eshell-unix-initialize
62 (defcustom eshell-plain-grep-behavior nil
63 "If non-nil, standalone \"grep\" commands will behave normally.
64 Standalone in this context means not redirected, and not on the
65 receiving side of a command pipeline."
69 (defcustom eshell-no-grep-available
(not (eshell-search-path "grep"))
70 "If non-nil, no grep is available on the current machine."
74 (defcustom eshell-plain-diff-behavior nil
75 "If non-nil, standalone \"diff\" commands will behave normally.
76 Standalone in this context means not redirected, and not on the
77 receiving side of a command pipeline."
81 (defcustom eshell-plain-locate-behavior
(featurep 'xemacs
)
82 "If non-nil, standalone \"locate\" commands will behave normally.
83 Standalone in this context means not redirected, and not on the
84 receiving side of a command pipeline."
88 (defcustom eshell-rm-removes-directories nil
89 "If non-nil, `rm' will remove directory entries.
90 Otherwise, `rmdir' is required."
94 (defcustom eshell-rm-interactive-query
(= (user-uid) 0)
95 "If non-nil, `rm' will query before removing anything."
99 (defcustom eshell-mv-interactive-query
(= (user-uid) 0)
100 "If non-nil, `mv' will query before overwriting anything."
104 (defcustom eshell-mv-overwrite-files t
105 "If non-nil, `mv' will overwrite files without warning."
109 (defcustom eshell-cp-interactive-query
(= (user-uid) 0)
110 "If non-nil, `cp' will query before overwriting anything."
114 (defcustom eshell-cp-overwrite-files t
115 "If non-nil, `cp' will overwrite files without warning."
119 (defcustom eshell-ln-interactive-query
(= (user-uid) 0)
120 "If non-nil, `ln' will query before overwriting anything."
124 (defcustom eshell-ln-overwrite-files nil
125 "If non-nil, `ln' will overwrite files without warning."
129 (defcustom eshell-default-target-is-dot nil
130 "If non-nil, the default destination for cp, mv or ln is `.'."
134 (defcustom eshell-du-prefer-over-ange nil
135 "Use Eshell's du in ange-ftp remote directories.
136 Otherwise, Emacs will attempt to use rsh to invoke du on the remote machine."
142 (defun eshell-unix-initialize ()
143 "Initialize the UNIX support/emulation code."
144 (when (eshell-using-module 'eshell-cmpl
)
145 (add-hook 'pcomplete-try-first-hook
146 'eshell-complete-host-reference nil t
))
147 (make-local-variable 'eshell-complex-commands
)
148 (setq eshell-complex-commands
149 (append '("grep" "egrep" "fgrep" "agrep" "glimpse" "locate"
150 "cat" "time" "cp" "mv" "make" "du" "diff" "su" "sudo")
151 eshell-complex-commands
)))
153 (defalias 'eshell
/date
'current-time-string
)
154 (defalias 'eshell
/basename
'file-name-nondirectory
)
155 (defalias 'eshell
/dirname
'file-name-directory
)
157 (defvar em-interactive
)
159 (defvar em-recursive
)
162 (defun eshell/man
(&rest args
)
163 "Invoke man, flattening the arguments appropriately."
164 (funcall 'man
(apply 'eshell-flatten-and-stringify args
)))
166 (put 'eshell
/man
'eshell-no-numeric-conversions t
)
168 (defun eshell/info
(&rest args
)
169 "Run the info command in-frame with the same behavior as command-line `info', ie:
170 'info' => goes to top info window
171 'info arg1' => IF arg1 is a file, then visits arg1
172 'info arg1' => OTHERWISE goes to top info window and then menu item arg1
173 'info arg1 arg2' => does action for arg1 (either visit-file or menu-item) and then menu item arg2
175 (eval-and-compile (require 'info
))
177 ((not (stringp (car args
)))
179 ((file-exists-p (expand-file-name (car args
)))
180 (expand-file-name (car args
)))
181 ((file-exists-p (concat (expand-file-name (car args
)) ".info"))
182 (concat (expand-file-name (car args
)) ".info")))))
184 ;; If the first arg is a file, then go to that file's Top node
185 ;; Otherwise, go to the global directory
188 (setq args
(cdr args
))
189 (Info-find-node file
"Top"))
192 ;; Treat all remaining args as menu references
194 (Info-menu (car args
))
195 (setq args
(cdr args
)))))
197 (defun eshell-remove-entries (path files
&optional top-level
)
198 "From PATH, remove all of the given FILES, perhaps interactively."
200 (if (string-match "\\`\\.\\.?\\'"
201 (file-name-nondirectory (car files
)))
203 (eshell-error "rm: cannot remove `.' or `..'\n"))
204 (if (and (file-directory-p (car files
))
205 (not (file-symlink-p (car files
))))
208 (eshell-printn (format "rm: removing directory `%s'"
214 (format "rm: remove directory `%s'? "
216 (eshell-funcalln 'delete-directory
(car files
) t t
)))
218 (eshell-printn (format "rm: removing file `%s'"
220 (unless (or em-preview
223 (format "rm: remove `%s'? "
225 (eshell-funcalln 'delete-file
(car files
) t
))))
226 (setq files
(cdr files
))))
228 (defun eshell/rm
(&rest args
)
229 "Implementation of rm in Lisp.
230 This is implemented to call either `delete-file', `kill-buffer',
231 `kill-process', or `unintern', depending on the nature of the
233 (setq args
(eshell-flatten-list args
))
234 (eshell-eval-using-options
236 '((?h
"help" nil nil
"show this usage screen")
237 (?f
"force" nil force-removal
"force removal")
238 (?i
"interactive" nil em-interactive
"prompt before any removal")
239 (?n
"preview" nil em-preview
"don't change anything on disk")
240 (?r
"recursive" nil em-recursive
241 "remove the contents of directories recursively")
242 (?R nil nil em-recursive
"(same)")
243 (?v
"verbose" nil em-verbose
"explain what is being done")
247 :usage
"[OPTION]... FILE...
248 Remove (unlink) the FILE(s).")
249 (unless em-interactive
250 (setq em-interactive eshell-rm-interactive-query
))
251 (if (and force-removal em-interactive
)
252 (setq em-interactive nil
))
254 (let ((entry (if (stringp (car args
))
255 (directory-file-name (car args
))
256 (if (numberp (car args
))
257 (number-to-string (car args
))
262 (eshell-printn (format "rm: removing buffer `%s'" entry
)))
263 (unless (or em-preview
265 (not (y-or-n-p (format "rm: delete buffer `%s'? "
267 (eshell-funcalln 'kill-buffer entry
)))
268 ((eshell-processp entry
)
270 (eshell-printn (format "rm: killing process `%s'" entry
)))
271 (unless (or em-preview
273 (not (y-or-n-p (format "rm: kill process `%s'? "
275 (eshell-funcalln 'kill-process entry
)))
278 (eshell-printn (format "rm: uninterning symbol `%s'" entry
)))
282 (not (y-or-n-p (format "rm: unintern symbol `%s'? "
284 (eshell-funcalln 'unintern entry
)))
286 (if (and (file-directory-p entry
)
287 (not (file-symlink-p entry
)))
289 eshell-rm-removes-directories
)
293 (format "rm: descend into directory `%s'? "
295 (eshell-remove-entries nil
(list entry
) t
))
296 (eshell-error (format "rm: %s: is a directory\n" entry
)))
297 (eshell-remove-entries nil
(list entry
) t
)))))
298 (setq args
(cdr args
)))
301 (put 'eshell
/rm
'eshell-no-numeric-conversions t
)
303 (defun eshell/mkdir
(&rest args
)
304 "Implementation of mkdir in Lisp."
305 (eshell-eval-using-options
307 '((?h
"help" nil nil
"show this usage screen")
310 :usage
"[OPTION] DIRECTORY...
311 Create the DIRECTORY(ies), if they do not already exist.")
313 (eshell-funcalln 'make-directory
(car args
))
314 (setq args
(cdr args
)))
317 (put 'eshell
/mkdir
'eshell-no-numeric-conversions t
)
319 (defun eshell/rmdir
(&rest args
)
320 "Implementation of rmdir in Lisp."
321 (eshell-eval-using-options
323 '((?h
"help" nil nil
"show this usage screen")
326 :usage
"[OPTION] DIRECTORY...
327 Remove the DIRECTORY(ies), if they are empty.")
329 (eshell-funcalln 'delete-directory
(car args
))
330 (setq args
(cdr args
)))
333 (put 'eshell
/rmdir
'eshell-no-numeric-conversions t
)
335 (defvar no-dereference
)
337 (defvar eshell-warn-dot-directories t
)
339 (defun eshell-shuffle-files (command action files target func deep
&rest args
)
340 "Shuffle around some filesystem entries, using FUNC to do the work."
341 (let ((attr-target (eshell-file-attributes target
))
342 (is-dir (or (file-directory-p target
)
343 (and em-preview
(not eshell-warn-dot-directories
))))
345 (if (and (not em-preview
) (not is-dir
)
346 (> (length files
) 1))
347 (error "%s: when %s multiple files, last argument must be a directory"
350 (setcar files
(directory-file-name (car files
)))
352 ((string-match "\\`\\.\\.?\\'"
353 (file-name-nondirectory (car files
)))
354 (if eshell-warn-dot-directories
355 (eshell-error (format "%s: %s: omitting directory\n"
356 command
(car files
)))))
358 (or (not (eshell-under-windows-p))
359 (eq system-type
'ms-dos
))
360 (setq attr
(eshell-file-attributes (car files
)))
361 (nth 10 attr-target
) (nth 10 attr
)
362 ;; Use equal, not -, since the inode and the device could
364 (equal (nth 10 attr-target
) (nth 10 attr
))
365 (nth 11 attr-target
) (nth 11 attr
)
366 (equal (nth 11 attr-target
) (nth 11 attr
)))
367 (eshell-error (format "%s: `%s' and `%s' are the same file\n"
368 command
(car files
) target
)))
370 (let ((source (car files
))
373 (file-name-nondirectory (car files
)) target
)
376 (if (and (file-directory-p source
)
377 (or (not no-dereference
)
378 (not (file-symlink-p source
)))
379 (not (memq func
'(make-symbolic-link
381 (if (and (eq func
'copy-file
)
383 (eshell-error (format "%s: %s: omitting directory\n"
384 command
(car files
)))
385 (let (eshell-warn-dot-directories)
387 (eq func
'rename-file
)
388 ;; Use equal, since the device might be a
390 (equal (nth 11 (eshell-file-attributes
393 (expand-file-name source
)))))
394 (nth 11 (eshell-file-attributes
397 (expand-file-name target
)))))))
398 (apply 'eshell-funcalln func source target args
)
399 (unless (file-directory-p target
)
402 (format "%s: making directory %s"
405 (eshell-funcalln 'make-directory target
)))
406 (apply 'eshell-shuffle-files
411 (concat source
"/" file
)))
412 (directory-files source
))
414 (when (eq func
'rename-file
)
417 (format "%s: deleting directory %s"
420 (eshell-funcalln 'delete-directory source
))))))
422 (eshell-printn (format "%s: %s -> %s" command
425 (if (and no-dereference
426 (setq link
(file-symlink-p source
)))
428 (apply 'eshell-funcalln
'make-symbolic-link
430 (if (eq func
'rename-file
)
431 (if (and (file-directory-p source
)
432 (not (file-symlink-p source
)))
433 (eshell-funcalln 'delete-directory source
)
434 (eshell-funcalln 'delete-file source
))))
435 (apply 'eshell-funcalln func source target args
)))))))
436 (setq files
(cdr files
)))))
438 (defun eshell-shorthand-tar-command (command args
)
439 "Rewrite `cp -v dir a.tar.gz' to `tar cvzf a.tar.gz dir'."
440 (let* ((archive (car (last args
)))
442 (cond ((string-match "z2" archive
) "If")
443 ((string-match "gz" archive
) "zf")
444 ((string-match "\\(az\\|Z\\)" archive
) "Zf")
446 (if (file-exists-p archive
)
447 (setq tar-args
(concat "u" tar-args
))
448 (setq tar-args
(concat "c" tar-args
)))
450 (setq tar-args
(concat "v" tar-args
)))
451 (if (equal command
"mv")
452 (setq tar-args
(concat "--remove-files -" tar-args
)))
453 ;; truncate the archive name from the arguments
454 (setcdr (last args
2) nil
)
455 (throw 'eshell-replace-command
456 (eshell-parse-command
457 (format "tar %s %s" tar-args archive
) args
))))
459 ;; this is to avoid duplicating code...
460 (defmacro eshell-mvcpln-template
(command action func query-var
461 force-var
&optional preserve
)
462 `(let ((len (length args
)))
464 (and (= len
1) (null eshell-default-target-is-dot
)))
465 (error "%s: missing destination file or directory" ,command
))
468 (setq args
(eshell-stringify-list (eshell-flatten-list args
)))
469 (if (and ,(not (equal command
"ln"))
470 (string-match eshell-tar-regexp
(car (last args
)))
471 (or (> (length args
) 2)
472 (and (file-directory-p (car args
))
473 (or (not no-dereference
)
474 (not (file-symlink-p (car args
)))))))
475 (eshell-shorthand-tar-command ,command args
)
476 (let ((target (car (last args
)))
478 (setcdr (last args
2) nil
)
479 (eshell-shuffle-files
480 ,command
,action args target
,func nil
482 `((if (and (or em-interactive
485 1 (or force
,force-var
)))
490 (defun eshell/mv
(&rest args
)
491 "Implementation of mv in Lisp."
492 (eshell-eval-using-options
494 '((?f
"force" nil force
495 "remove existing destinations, never prompt")
496 (?i
"interactive" nil em-interactive
497 "request confirmation if target already exists")
498 (?n
"preview" nil em-preview
499 "don't change anything on disk")
500 (?v
"verbose" nil em-verbose
501 "explain what is being done")
502 (nil "help" nil nil
"show this usage screen")
506 :usage
"[OPTION]... SOURCE DEST
507 or: mv [OPTION]... SOURCE... DIRECTORY
508 Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.
509 \[OPTION] DIRECTORY...")
510 (let ((no-dereference t
))
511 (eshell-mvcpln-template "mv" "moving" 'rename-file
512 eshell-mv-interactive-query
513 eshell-mv-overwrite-files
))))
515 (put 'eshell
/mv
'eshell-no-numeric-conversions t
)
517 (defun eshell/cp
(&rest args
)
518 "Implementation of cp in Lisp."
519 (eshell-eval-using-options
521 '((?a
"archive" nil archive
523 (?d
"no-dereference" nil no-dereference
525 (?f
"force" nil force
526 "remove existing destinations, never prompt")
527 (?i
"interactive" nil em-interactive
528 "request confirmation if target already exists")
529 (?n
"preview" nil em-preview
530 "don't change anything on disk")
531 (?p
"preserve" nil preserve
532 "preserve file attributes if possible")
533 (?R
"recursive" nil em-recursive
534 "copy directories recursively")
535 (?v
"verbose" nil em-verbose
536 "explain what is being done")
537 (nil "help" nil nil
"show this usage screen")
541 :usage
"[OPTION]... SOURCE DEST
542 or: cp [OPTION]... SOURCE... DIRECTORY
543 Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.")
545 (setq preserve t no-dereference t em-recursive t
))
546 (eshell-mvcpln-template "cp" "copying" 'copy-file
547 eshell-cp-interactive-query
548 eshell-cp-overwrite-files preserve
)))
550 (put 'eshell
/cp
'eshell-no-numeric-conversions t
)
552 (defun eshell/ln
(&rest args
)
553 "Implementation of ln in Lisp."
554 (eshell-eval-using-options
556 '((?h
"help" nil nil
"show this usage screen")
557 (?s
"symbolic" nil symbolic
558 "make symbolic links instead of hard links")
559 (?i
"interactive" nil em-interactive
560 "request confirmation if target already exists")
561 (?f
"force" nil force
"remove existing destinations, never prompt")
562 (?n
"preview" nil em-preview
563 "don't change anything on disk")
564 (?v
"verbose" nil em-verbose
"explain what is being done")
568 :usage
"[OPTION]... TARGET [LINK_NAME]
569 or: ln [OPTION]... TARGET... DIRECTORY
570 Create a link to the specified TARGET with optional LINK_NAME. If there is
571 more than one TARGET, the last argument must be a directory; create links
572 in DIRECTORY to each TARGET. Create hard links by default, symbolic links
573 with '--symbolic'. When creating hard links, each TARGET must exist.")
574 (let ((no-dereference t
))
575 (eshell-mvcpln-template "ln" "linking"
579 eshell-ln-interactive-query
580 eshell-ln-overwrite-files
))))
582 (put 'eshell
/ln
'eshell-no-numeric-conversions t
)
584 (defun eshell/cat
(&rest args
)
585 "Implementation of cat in Lisp.
586 If in a pipeline, or the file is not a regular file, directory or
587 symlink, then revert to the system's definition of cat."
588 (setq args
(eshell-stringify-list (eshell-flatten-list args
)))
589 (if (or eshell-in-pipeline-p
592 (unless (or (and (stringp arg
)
594 (eq (aref arg
0) ?-
))
595 (let ((attrs (eshell-file-attributes arg
)))
596 (and attrs
(memq (aref (nth 8 attrs
) 0)
598 (throw 'special t
)))))
599 (let ((ext-cat (eshell-search-path "cat")))
601 (throw 'eshell-replace-command
602 (eshell-parse-command ext-cat args
))
603 (if eshell-in-pipeline-p
604 (error "Eshell's `cat' does not work in pipelines")
605 (error "Eshell's `cat' cannot display one of the files given"))))
606 (eshell-init-print-buffer)
607 (eshell-eval-using-options
609 '((?h
"help" nil nil
"show this usage screen")
612 :usage
"[OPTION] FILE...
613 Concatenate FILE(s), or standard input, to standard output.")
615 (if (string= file
"-")
616 (throw 'eshell-external
617 (eshell-external-command "cat" args
))))
618 (let ((curbuf (current-buffer)))
621 (insert-file-contents file
)
622 (goto-char (point-min))
624 (let ((str (buffer-substring
625 (point) (min (1+ (line-end-position))
627 (with-current-buffer curbuf
628 (eshell-buffered-print str
)))
631 ;; if the file does not end in a newline, do not emit one
632 (setq eshell-ensure-newline-p nil
))))
634 (put 'eshell
/cat
'eshell-no-numeric-conversions t
)
636 ;; special front-end functions for compilation-mode buffers
638 (defun eshell/make
(&rest args
)
639 "Use `compile' to do background makes."
640 (if (and eshell-current-subjob-p
641 (eshell-interactive-output-p))
642 (let ((compilation-process-setup-function
644 (list 'setq
'process-environment
645 (list 'quote
(eshell-copy-environment))))))
646 (compile (concat "make " (eshell-flatten-and-stringify args
))))
647 (throw 'eshell-replace-command
648 (eshell-parse-command "*make" (eshell-stringify-list
649 (eshell-flatten-list args
))))))
651 (put 'eshell
/make
'eshell-no-numeric-conversions t
)
653 (defun eshell-occur-mode-goto-occurrence ()
654 "Go to the occurrence the current line describes."
656 (let ((pos (occur-mode-find-occurrence)))
657 (pop-to-buffer (marker-buffer pos
))
658 (goto-char (marker-position pos
))))
660 (defun eshell-occur-mode-mouse-goto (event)
661 "In Occur mode, go to the occurrence whose line you click on."
664 (with-current-buffer (window-buffer (posn-window (event-end event
)))
666 (goto-char (posn-point (event-end event
)))
667 (setq pos
(occur-mode-find-occurrence))))
668 (pop-to-buffer (marker-buffer pos
))
669 (goto-char (marker-position pos
))))
671 (defun eshell-poor-mans-grep (args)
672 "A poor version of grep that opens every file and uses `occur'.
673 This eats up memory, since it leaves the buffers open (to speed future
674 searches), and it's very slow. But, if your system has no grep
676 (save-selected-window
677 (let ((default-dir default-directory
))
678 (with-current-buffer (get-buffer-create "*grep*")
679 (let ((inhibit-read-only t
)
680 (default-directory default-dir
))
683 (let ((files (eshell-stringify-list
684 (eshell-flatten-list (cdr args
))))
685 (inhibit-redisplay t
)
688 (if (get-buffer "*Occur*")
689 (kill-buffer (get-buffer "*Occur*")))
692 (with-current-buffer (find-file-noselect (car files
))
696 (if (get-buffer "*Occur*")
697 (with-current-buffer (get-buffer "*Occur*")
698 (setq string
(buffer-string))
699 (kill-buffer (current-buffer)))))
700 (if string
(insert string
))
702 files
(cdr files
)))))
703 (local-set-key [mouse-2
] 'eshell-occur-mode-mouse-goto
)
704 (local-set-key [(control ?c
) (control ?c
)]
705 'eshell-occur-mode-goto-occurrence
)
706 (local-set-key [(control ?m
)]
707 'eshell-occur-mode-goto-occurrence
)
708 (local-set-key [return] 'eshell-occur-mode-goto-occurrence)
709 (pop-to-buffer (current-buffer) t)
710 (goto-char (point-min))
711 (resize-temp-buffer-window))))))
713 (defun eshell-grep (command args &optional maybe-use-occur)
714 "Generic service function for the various grep aliases.
715 It calls Emacs' grep utility if the command is not redirecting output,
716 and if it's not part of a command pipeline. Otherwise, it calls the
718 (if (and maybe-use-occur eshell-no-grep-available)
719 (eshell-poor-mans-grep args)
720 (if (or eshell-plain-grep-behavior
721 (not (and (eshell-interactive-output-p)
722 (not eshell-in-pipeline-p)
723 (not eshell-in-subcommand-p))))
724 (throw 'eshell-replace-command
725 (eshell-parse-command (concat "*" command)
726 (eshell-stringify-list
727 (eshell-flatten-list args))))
728 (let* ((args (mapconcat 'identity
729 (mapcar 'shell-quote-argument
730 (eshell-stringify-list
731 (eshell-flatten-list args)))
734 (set-text-properties 0 (length args)
736 (format "%s -n %s" command args)))
737 compilation-scroll-output)
740 (defun eshell/grep (&rest args)
741 "Use Emacs grep facility instead of calling external grep."
742 (eshell-grep "grep" args t))
744 (defun eshell/egrep (&rest args)
745 "Use Emacs grep facility instead of calling external egrep."
746 (eshell-grep "egrep" args t))
748 (defun eshell/fgrep (&rest args)
749 "Use Emacs grep facility instead of calling external fgrep."
750 (eshell-grep "fgrep" args t))
752 (defun eshell/agrep (&rest args)
753 "Use Emacs grep facility instead of calling external agrep."
754 (eshell-grep "agrep" args))
756 (defun eshell/glimpse (&rest args)
757 "Use Emacs grep facility instead of calling external glimpse."
759 (eshell-grep "glimpse" (append '("-z" "-y") args))))
761 ;; completions rules for some common UNIX commands
763 (defsubst eshell-complete-hostname ()
764 "Complete a command that wants a hostname for an argument."
765 (pcomplete-here (eshell-read-host-names)))
767 (defun eshell-complete-host-reference ()
768 "If there is a host reference, complete it."
769 (let ((arg (pcomplete-actual-arg))
771 (when (setq index (string-match "@[a-z.]*\\'" arg))
772 (setq pcomplete-stub (substring arg (1+ index))
773 pcomplete-last-completion-raw t)
774 (throw 'pcomplete-completions (eshell-read-host-names)))))
776 (defalias 'pcomplete/ftp 'eshell-complete-hostname)
777 (defalias 'pcomplete/ncftp 'eshell-complete-hostname)
778 (defalias 'pcomplete/ping 'eshell-complete-hostname)
779 (defalias 'pcomplete/rlogin 'eshell-complete-hostname)
781 (defun pcomplete/telnet ()
782 (require 'pcmpl-unix)
783 (pcomplete-opt "xl(pcmpl-unix-user-names)")
784 (eshell-complete-hostname))
786 (defun pcomplete/rsh ()
787 "Complete `rsh', which, after the user and hostname, is like xargs."
788 (require 'pcmpl-unix)
789 (pcomplete-opt "l(pcmpl-unix-user-names)")
790 (eshell-complete-hostname)
791 (pcomplete-here (funcall pcomplete-command-completion-function))
792 (funcall (or (pcomplete-find-completion-function (pcomplete-arg 1))
793 pcomplete-default-completion-function)))
795 (defalias 'pcomplete/ssh 'pcomplete/rsh)
799 (defvar dereference-links)
801 (defvar human-readable)
803 (defvar only-one-filesystem)
806 (defsubst eshell-du-size-string (size)
807 (let* ((str (eshell-printable-size size human-readable block-size t))
809 (concat str (if (< len 8)
810 (make-string (- 8 len) ? )))))
812 (defun eshell-du-sum-directory (path depth)
813 "Summarize PATH, and its member directories."
814 (let ((entries (eshell-directory-files-and-attributes path))
817 (unless (string-match "\\`\\.\\.?\\'" (caar entries))
818 (let* ((entry (concat path "/"
820 (symlink (and (stringp (cadr (car entries)))
821 (cadr (car entries)))))
822 (unless (or (and symlink (not dereference-links))
823 (and only-one-filesystem
824 (/= only-one-filesystem
825 (nth 12 (car entries)))))
827 (setq entry symlink))
830 (if (eq t (cadr (car entries)))
831 (eshell-du-sum-directory entry (1+ depth))
832 (let ((file-size (nth 8 (car entries))))
837 (concat (eshell-du-size-string file-size)
838 entry "\n")))))))))))
839 (setq entries (cdr entries)))
840 (if (or (not max-depth)
843 (eshell-print (concat (eshell-du-size-string size)
844 (directory-file-name path) "\n")))
847 (defun eshell/du (&rest args)
848 "Implementation of \"du\" in Lisp, passing ARGS."
850 (eshell-stringify-list (eshell-flatten-list args))
852 (let ((ext-du (eshell-search-path "du")))
854 (not (catch 'have-ange-path
857 (file-remote-p (expand-file-name arg) 'method) "ftp")
858 (throw 'have-ange-path t))))))
859 (throw 'eshell-replace-command
860 (eshell-parse-command ext-du args))
861 (eshell-eval-using-options
863 '((?a "all" nil show-all
864 "write counts for all files, not just directories")
865 (nil "block-size" t block-size
866 "use SIZE-byte blocks (i.e., --block-size SIZE)")
867 (?b "bytes" nil by-bytes
868 "print size in bytes")
869 (?c "total" nil grand-total
870 "produce a grand total")
871 (?d "max-depth" t max-depth
872 "display data only this many levels of data")
873 (?h "human-readable" 1024 human-readable
874 "print sizes in human readable format")
875 (?H "is" 1000 human-readable
876 "likewise, but use powers of 1000 not 1024")
877 (?k "kilobytes" 1024 block-size
878 "like --block-size 1024")
879 (?L "dereference" nil dereference-links
880 "dereference all symbolic links")
881 (?m "megabytes" 1048576 block-size
882 "like --block-size 1048576")
883 (?s "summarize" 0 max-depth
884 "display only a total for each argument")
885 (?x "one-file-system" nil only-one-filesystem
886 "skip directories on different filesystems")
888 "show this usage screen")
890 :usage "[OPTION]... FILE...
891 Summarize disk usage of each FILE, recursively for directories.")
893 (setq block-size (or block-size 1024)))
894 (if (and max-depth (stringp max-depth))
895 (setq max-depth (string-to-number max-depth)))
896 ;; filesystem support means nothing under Windows
897 (if (eshell-under-windows-p)
898 (setq only-one-filesystem nil))
899 (let ((size 0.0) ange-cache)
901 (if only-one-filesystem
902 (setq only-one-filesystem
903 (nth 11 (eshell-file-attributes
904 (file-name-as-directory (car args))))))
905 (setq size (+ size (eshell-du-sum-directory
906 (directory-file-name (car args)) 0)))
907 (setq args (cdr args)))
909 (eshell-print (concat (eshell-du-size-string size)
912 (defvar eshell-time-start nil)
914 (defun eshell-show-elapsed-time ()
915 (let ((elapsed (format "%.3f secs\n" (- (float-time) eshell-time-start))))
916 (set-text-properties 0 (length elapsed) '(face bold) elapsed)
917 (eshell-interactive-print elapsed))
918 (remove-hook 'eshell-post-command-hook 'eshell-show-elapsed-time t))
920 (defun eshell/time (&rest args)
921 "Implementation of \"time\" in Lisp."
922 (let ((time-args (copy-alist args))
925 (while (and continue args)
926 (if (not (string-match "^-" (car args)))
929 (setcdr last-arg nil)
934 (eshell-eval-using-options
936 '((?h "help" nil nil "show this usage screen")
940 Show wall-clock time elapsed during execution of COMMAND.")
941 (setq eshell-time-start (float-time))
942 (add-hook 'eshell-post-command-hook 'eshell-show-elapsed-time nil t)
944 (throw 'eshell-replace-command
945 (eshell-parse-command (car time-args)
946 ;;; http://lists.gnu.org/archive/html/bug-gnu-emacs/2007-08/msg00205.html
947 (eshell-stringify-list
948 (eshell-flatten-list (cdr time-args))))))))
950 (defun eshell/whoami (&rest args)
951 "Make \"whoami\" Tramp aware."
952 (or (file-remote-p default-directory 'user) (user-login-name)))
954 (defvar eshell-diff-window-config nil)
956 (defun eshell-diff-quit ()
957 "Restore the window configuration previous to diff'ing."
959 (if eshell-diff-window-config
960 (set-window-configuration eshell-diff-window-config)))
962 (defun nil-blank-string (string)
963 "Return STRING, or nil if STRING contains only non-blank characters."
965 ((string-match "[^[:blank:]]" string) string)
968 (defun eshell/diff (&rest args)
969 "Alias \"diff\" to call Emacs `diff' function."
970 (let ((orig-args (eshell-stringify-list (eshell-flatten-list args))))
971 (if (or eshell-plain-diff-behavior
972 (not (and (eshell-interactive-output-p)
973 (not eshell-in-pipeline-p)
974 (not eshell-in-subcommand-p))))
975 (throw 'eshell-replace-command
976 (eshell-parse-command "*diff" orig-args))
977 (setq args (copy-sequence orig-args))
978 (if (< (length args) 2)
979 (throw 'eshell-replace-command
980 (eshell-parse-command "*diff" orig-args)))
981 (let ((old (car (last args 2)))
982 (new (car (last args)))
983 (config (current-window-configuration)))
984 (if (= (length args) 2)
986 (setcdr (last args 3) nil))
990 (nil-blank-string (eshell-flatten-and-stringify args)))
992 (throw 'eshell-replace-command
993 (eshell-parse-command "*diff" orig-args))))
994 (when (fboundp 'diff-mode)
995 (make-local-variable 'compilation-finish-functions)
997 'compilation-finish-functions
999 (with-current-buffer buff
1001 (set (make-local-variable 'eshell-diff-window-config)
1003 (local-set-key [?q] 'eshell-diff-quit)
1004 (if (fboundp 'turn-on-font-lock-if-enabled)
1005 (turn-on-font-lock-if-enabled))
1006 (goto-char (point-min))))))
1007 (pop-to-buffer (current-buffer))))))
1010 (put 'eshell/diff 'eshell-no-numeric-conversions t)
1012 (defun eshell/locate (&rest args)
1013 "Alias \"locate\" to call Emacs `locate' function."
1014 (if (or eshell-plain-locate-behavior
1015 (not (and (eshell-interactive-output-p)
1016 (not eshell-in-pipeline-p)
1017 (not eshell-in-subcommand-p)))
1018 (and (stringp (car args))
1019 (string-match "^-" (car args))))
1020 (throw 'eshell-replace-command
1021 (eshell-parse-command "*locate" (eshell-stringify-list
1022 (eshell-flatten-list args))))
1023 (save-selected-window
1024 (let ((locate-history-list (list (car args))))
1025 (locate-with-filter (car args) (cadr args))))))
1027 (put 'eshell/locate 'eshell-no-numeric-conversions t)
1029 (defun eshell/occur (&rest args)
1030 "Alias \"occur\" to call Emacs `occur' function."
1031 (let ((inhibit-read-only t))
1032 (if (> (length args) 2)
1033 (error "usage: occur: (REGEXP &optional NLINES)")
1034 (apply 'occur args))))
1036 (put 'eshell/occur 'eshell-no-numeric-conversions t)
1038 ;; Pacify the byte-compiler.
1039 (defvar tramp-default-proxies-alist)
1041 (defun eshell/su (&rest args)
1042 "Alias \"su\" to call Tramp."
1044 (setq args (eshell-stringify-list (eshell-flatten-list args)))
1045 (let ((orig-args (copy-tree args)))
1046 (eshell-eval-using-options
1048 '((?h "help" nil nil "show this usage screen")
1049 (?l "login" nil login "provide a login environment")
1050 (? nil nil login "provide a login environment")
1051 :usage "[- | -l | --login] [USER]
1052 Become another USER during a login session.")
1053 (throw 'eshell-replace-command
1055 (host (or (file-remote-p default-directory 'host)
1057 (dir (or (file-remote-p default-directory 'localname)
1058 (expand-file-name default-directory))))
1060 (if (string-equal arg "-") (setq login t) (setq user arg)))
1061 ;; `eshell-eval-using-options' does not handle "-".
1062 (if (member "-" orig-args) (setq login t))
1063 (if login (setq dir "~/"))
1064 (if (and (file-remote-p default-directory)
1067 "su" (file-remote-p default-directory 'method)))
1069 user (file-remote-p default-directory 'user)))))
1071 'tramp-default-proxies-alist
1072 (list host user (file-remote-p default-directory))))
1073 (eshell-parse-command
1074 "cd" (list (format "/su:%s@%s:%s" user host dir))))))))
1076 (put 'eshell/su 'eshell-no-numeric-conversions t)
1078 (defun eshell/sudo (&rest args)
1079 "Alias \"sudo\" to call Tramp."
1081 (setq args (eshell-stringify-list (eshell-flatten-list args)))
1082 (let ((orig-args (copy-tree args)))
1083 (eshell-eval-using-options
1085 '((?h "help" nil nil "show this usage screen")
1086 (?u "user" t user "execute a command as another USER")
1088 :usage "[(-u | --user) USER] COMMAND
1089 Execute a COMMAND as the superuser or another USER.")
1090 (throw 'eshell-external
1091 (let ((user (or user "root"))
1092 (host (or (file-remote-p default-directory 'host)
1094 (dir (or (file-remote-p default-directory 'localname)
1095 (expand-file-name default-directory))))
1096 ;; `eshell-eval-using-options' reads options of COMMAND.
1097 (while (and (stringp (car orig-args))
1098 (member (car orig-args) '("-u" "--user")))
1099 (setq orig-args (cddr orig-args)))
1100 (if (and (file-remote-p default-directory)
1103 "sudo" (file-remote-p default-directory 'method)))
1105 user (file-remote-p default-directory 'user)))))
1107 'tramp-default-proxies-alist
1108 (list host user (file-remote-p default-directory))))
1109 (let ((default-directory (format "/sudo:%s@%s:%s" user host dir)))
1110 (eshell-named-command (car orig-args) (cdr orig-args))))))))
1112 (put 'eshell/sudo 'eshell-no-numeric-conversions t)
1117 ;; generated-autoload-file: "esh-groups.el"
1120 ;;; em-unix.el ends here