1 ;;; em-unix.el --- UNIX command aliases -*- lexical-binding:t -*-
3 ;; Copyright (C) 1999-2014 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 (files &optional toplevel
)
199 "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 (list entry
) t
))
300 (eshell-error (format "rm: %s: is a directory\n" entry
)))
301 (eshell-remove-entries (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 (defvar ange-cache
) ; XEmacs? See esh-util
466 ;; this is to avoid duplicating code...
467 (defmacro eshell-mvcpln-template
(command action func query-var
468 force-var
&optional preserve
)
469 `(let ((len (length args
)))
471 (and (= len
1) (null eshell-default-target-is-dot
)))
472 (error "%s: missing destination file or directory" ,command
))
475 (setq args
(eshell-stringify-list (eshell-flatten-list args
)))
476 (if (and ,(not (equal command
"ln"))
477 (string-match eshell-tar-regexp
(car (last args
)))
478 (or (> (length args
) 2)
479 (and (file-directory-p (car args
))
480 (or (not no-dereference
)
481 (not (file-symlink-p (car args
)))))))
482 (eshell-shorthand-tar-command ,command args
)
483 (let ((target (car (last args
)))
485 (setcdr (last args
2) nil
)
486 (eshell-shuffle-files
487 ,command
,action args target
,func nil
489 `((if (and (or em-interactive
492 1 (or force
,force-var
)))
497 (defun eshell/mv
(&rest args
)
498 "Implementation of mv in Lisp."
499 (eshell-eval-using-options
501 '((?f
"force" nil force
502 "remove existing destinations, never prompt")
503 (?i
"interactive" nil em-interactive
504 "request confirmation if target already exists")
505 (?n
"preview" nil em-preview
506 "don't change anything on disk")
507 (?v
"verbose" nil em-verbose
508 "explain what is being done")
509 (nil "help" nil nil
"show this usage screen")
513 :usage
"[OPTION]... SOURCE DEST
514 or: mv [OPTION]... SOURCE... DIRECTORY
515 Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.
516 \[OPTION] DIRECTORY...")
517 (let ((no-dereference t
))
518 (eshell-mvcpln-template "mv" "moving" 'rename-file
519 eshell-mv-interactive-query
520 eshell-mv-overwrite-files
))))
522 (put 'eshell
/mv
'eshell-no-numeric-conversions t
)
524 (defun eshell/cp
(&rest args
)
525 "Implementation of cp in Lisp."
526 (eshell-eval-using-options
528 '((?a
"archive" nil archive
530 (?d
"no-dereference" nil no-dereference
532 (?f
"force" nil force
533 "remove existing destinations, never prompt")
534 (?i
"interactive" nil em-interactive
535 "request confirmation if target already exists")
536 (?n
"preview" nil em-preview
537 "don't change anything on disk")
538 (?p
"preserve" nil preserve
539 "preserve file attributes if possible")
540 (?r
"recursive" nil em-recursive
541 "copy directories recursively")
542 (?R nil nil em-recursive
544 (?v
"verbose" nil em-verbose
545 "explain what is being done")
546 (nil "help" nil nil
"show this usage screen")
550 :usage
"[OPTION]... SOURCE DEST
551 or: cp [OPTION]... SOURCE... DIRECTORY
552 Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.")
554 (setq preserve t no-dereference t em-recursive t
))
555 (eshell-mvcpln-template "cp" "copying" 'copy-file
556 eshell-cp-interactive-query
557 eshell-cp-overwrite-files preserve
)))
559 (put 'eshell
/cp
'eshell-no-numeric-conversions t
)
561 (defun eshell/ln
(&rest args
)
562 "Implementation of ln in Lisp."
563 (eshell-eval-using-options
565 '((?h
"help" nil nil
"show this usage screen")
566 (?s
"symbolic" nil symbolic
567 "make symbolic links instead of hard links")
568 (?i
"interactive" nil em-interactive
569 "request confirmation if target already exists")
570 (?f
"force" nil force
"remove existing destinations, never prompt")
571 (?n
"preview" nil em-preview
572 "don't change anything on disk")
573 (?v
"verbose" nil em-verbose
"explain what is being done")
577 :usage
"[OPTION]... TARGET [LINK_NAME]
578 or: ln [OPTION]... TARGET... DIRECTORY
579 Create a link to the specified TARGET with optional LINK_NAME. If there is
580 more than one TARGET, the last argument must be a directory; create links
581 in DIRECTORY to each TARGET. Create hard links by default, symbolic links
582 with '--symbolic'. When creating hard links, each TARGET must exist.")
583 (let ((no-dereference t
))
584 (eshell-mvcpln-template "ln" "linking"
588 eshell-ln-interactive-query
589 eshell-ln-overwrite-files
))))
591 (put 'eshell
/ln
'eshell-no-numeric-conversions t
)
593 (defun eshell/cat
(&rest args
)
594 "Implementation of cat in Lisp.
595 If in a pipeline, or the file is not a regular file, directory or
596 symlink, then revert to the system's definition of cat."
597 (setq args
(eshell-stringify-list (eshell-flatten-list args
)))
598 (if (or eshell-in-pipeline-p
601 (unless (or (and (stringp arg
)
603 (eq (aref arg
0) ?-
))
604 (let ((attrs (eshell-file-attributes arg
)))
605 (and attrs
(memq (aref (nth 8 attrs
) 0)
607 (throw 'special t
)))))
608 (let ((ext-cat (eshell-search-path "cat")))
610 (throw 'eshell-replace-command
611 (eshell-parse-command (eshell-quote-argument ext-cat
) args
))
612 (if eshell-in-pipeline-p
613 (error "Eshell's `cat' does not work in pipelines")
614 (error "Eshell's `cat' cannot display one of the files given"))))
615 (eshell-init-print-buffer)
616 (eshell-eval-using-options
618 '((?h
"help" nil nil
"show this usage screen")
621 :usage
"[OPTION] FILE...
622 Concatenate FILE(s), or standard input, to standard output.")
624 (if (string= file
"-")
625 (throw 'eshell-external
626 (eshell-external-command "cat" args
))))
627 (let ((curbuf (current-buffer)))
630 (insert-file-contents file
)
631 (goto-char (point-min))
633 (let ((str (buffer-substring
634 (point) (min (1+ (line-end-position))
636 (with-current-buffer curbuf
637 (eshell-buffered-print str
)))
640 ;; if the file does not end in a newline, do not emit one
641 (setq eshell-ensure-newline-p nil
))))
643 (put 'eshell
/cat
'eshell-no-numeric-conversions t
)
645 ;; special front-end functions for compilation-mode buffers
647 (defun eshell/make
(&rest args
)
648 "Use `compile' to do background makes."
649 (if (and eshell-current-subjob-p
650 (eshell-interactive-output-p))
651 (let ((compilation-process-setup-function
653 (list 'setq
'process-environment
654 (list 'quote
(eshell-copy-environment))))))
655 (compile (concat "make " (eshell-flatten-and-stringify args
))))
656 (throw 'eshell-replace-command
657 (eshell-parse-command "*make" (eshell-stringify-list
658 (eshell-flatten-list args
))))))
660 (put 'eshell
/make
'eshell-no-numeric-conversions t
)
662 (defun eshell-occur-mode-goto-occurrence ()
663 "Go to the occurrence the current line describes."
665 (let ((pos (occur-mode-find-occurrence)))
666 (pop-to-buffer (marker-buffer pos
))
667 (goto-char (marker-position pos
))))
669 (defun eshell-occur-mode-mouse-goto (event)
670 "In Occur mode, go to the occurrence whose line you click on."
673 (with-current-buffer (window-buffer (posn-window (event-end event
)))
675 (goto-char (posn-point (event-end event
)))
676 (setq pos
(occur-mode-find-occurrence))))
677 (pop-to-buffer (marker-buffer pos
))
678 (goto-char (marker-position pos
))))
680 (defun eshell-poor-mans-grep (args)
681 "A poor version of grep that opens every file and uses `occur'.
682 This eats up memory, since it leaves the buffers open (to speed future
683 searches), and it's very slow. But, if your system has no grep
685 (save-selected-window
686 (let ((default-dir default-directory
))
687 (with-current-buffer (get-buffer-create "*grep*")
688 (let ((inhibit-read-only t
)
689 (default-directory default-dir
))
692 (let ((files (eshell-stringify-list
693 (eshell-flatten-list (cdr args
))))
694 (inhibit-redisplay t
)
697 (if (get-buffer "*Occur*")
698 (kill-buffer (get-buffer "*Occur*")))
701 (with-current-buffer (find-file-noselect (car files
))
705 (if (get-buffer "*Occur*")
706 (with-current-buffer (get-buffer "*Occur*")
707 (setq string
(buffer-string))
708 (kill-buffer (current-buffer)))))
709 (if string
(insert string
))
711 files
(cdr files
)))))
712 (local-set-key [mouse-2
] 'eshell-occur-mode-mouse-goto
)
713 (local-set-key [(control ?c
) (control ?c
)]
714 'eshell-occur-mode-goto-occurrence
)
715 (local-set-key [(control ?m
)]
716 'eshell-occur-mode-goto-occurrence
)
717 (local-set-key [return] 'eshell-occur-mode-goto-occurrence)
718 (pop-to-buffer (current-buffer) t)
719 (goto-char (point-min))
720 (resize-temp-buffer-window))))))
722 (defvar compilation-scroll-output)
724 (defun eshell-grep (command args &optional maybe-use-occur)
725 "Generic service function for the various grep aliases.
726 It calls Emacs's grep utility if the command is not redirecting output,
727 and if it's not part of a command pipeline. Otherwise, it calls the
729 (if (and maybe-use-occur eshell-no-grep-available)
730 (eshell-poor-mans-grep args)
731 (if (or eshell-plain-grep-behavior
732 (not (and (eshell-interactive-output-p)
733 (not eshell-in-pipeline-p)
734 (not eshell-in-subcommand-p))))
735 (throw 'eshell-replace-command
736 (eshell-parse-command (concat "*" command)
737 (eshell-stringify-list
738 (eshell-flatten-list args))))
739 (let* ((args (mapconcat 'identity
740 (mapcar 'shell-quote-argument
741 (eshell-stringify-list
742 (eshell-flatten-list args)))
745 (set-text-properties 0 (length args)
747 (format "%s -n %s" command args)))
748 compilation-scroll-output)
751 (defun eshell/grep (&rest args)
752 "Use Emacs grep facility instead of calling external grep."
753 (eshell-grep "grep" args t))
755 (defun eshell/egrep (&rest args)
756 "Use Emacs grep facility instead of calling external egrep."
757 (eshell-grep "egrep" args t))
759 (defun eshell/fgrep (&rest args)
760 "Use Emacs grep facility instead of calling external fgrep."
761 (eshell-grep "fgrep" args t))
763 (defun eshell/agrep (&rest args)
764 "Use Emacs grep facility instead of calling external agrep."
765 (eshell-grep "agrep" args))
767 (defun eshell/glimpse (&rest args)
768 "Use Emacs grep facility instead of calling external glimpse."
770 (eshell-grep "glimpse" (append '("-z" "-y") args))))
772 ;; completions rules for some common UNIX commands
774 (defsubst eshell-complete-hostname ()
775 "Complete a command that wants a hostname for an argument."
776 (pcomplete-here (eshell-read-host-names)))
778 (defun eshell-complete-host-reference ()
779 "If there is a host reference, complete it."
780 (let ((arg (pcomplete-actual-arg))
782 (when (setq index (string-match "@[a-z.]*\\'" arg))
783 (setq pcomplete-stub (substring arg (1+ index))
784 pcomplete-last-completion-raw t)
785 (throw 'pcomplete-completions (eshell-read-host-names)))))
787 (defalias 'pcomplete/ftp 'eshell-complete-hostname)
788 (defalias 'pcomplete/ncftp 'eshell-complete-hostname)
789 (defalias 'pcomplete/ping 'eshell-complete-hostname)
790 (defalias 'pcomplete/rlogin 'eshell-complete-hostname)
792 (defun pcomplete/telnet ()
793 (require 'pcmpl-unix)
794 (pcomplete-opt "xl(pcmpl-unix-user-names)")
795 (eshell-complete-hostname))
797 (defun pcomplete/rsh ()
798 "Complete `rsh', which, after the user and hostname, is like xargs."
799 (require 'pcmpl-unix)
800 (pcomplete-opt "l(pcmpl-unix-user-names)")
801 (eshell-complete-hostname)
802 (pcomplete-here (funcall pcomplete-command-completion-function))
803 (funcall (or (pcomplete-find-completion-function (pcomplete-arg 1))
804 pcomplete-default-completion-function)))
808 (defvar dereference-links)
810 (defvar human-readable)
812 (defvar only-one-filesystem)
815 (defsubst eshell-du-size-string (size)
816 (let* ((str (eshell-printable-size size human-readable block-size t))
818 (concat str (if (< len 8)
819 (make-string (- 8 len) ? )))))
821 (defun eshell-du-sum-directory (path depth)
822 "Summarize PATH, and its member directories."
823 (let ((entries (eshell-directory-files-and-attributes path))
826 (unless (string-match "\\`\\.\\.?\\'" (caar entries))
827 (let* ((entry (concat path "/"
829 (symlink (and (stringp (cadr (car entries)))
830 (cadr (car entries)))))
831 (unless (or (and symlink (not dereference-links))
832 (and only-one-filesystem
833 (/= only-one-filesystem
834 (nth 12 (car entries)))))
836 (setq entry symlink))
839 (if (eq t (cadr (car entries)))
840 (eshell-du-sum-directory entry (1+ depth))
841 (let ((file-size (nth 8 (car entries))))
846 (concat (eshell-du-size-string file-size)
847 entry "\n")))))))))))
848 (setq entries (cdr entries)))
849 (if (or (not max-depth)
852 (eshell-print (concat (eshell-du-size-string size)
853 (directory-file-name path) "\n")))
856 (defun eshell/du (&rest args)
857 "Implementation of \"du\" in Lisp, passing ARGS."
859 (eshell-stringify-list (eshell-flatten-list args))
861 (let ((ext-du (eshell-search-path "du")))
863 (not (catch 'have-ange-path
866 (file-remote-p (expand-file-name arg) 'method) "ftp")
867 (throw 'have-ange-path t))))))
868 (throw 'eshell-replace-command
869 (eshell-parse-command (eshell-quote-argument ext-du) args))
870 (eshell-eval-using-options
872 '((?a "all" nil show-all
873 "write counts for all files, not just directories")
874 (nil "block-size" t block-size
875 "use SIZE-byte blocks (i.e., --block-size SIZE)")
876 (?b "bytes" nil by-bytes
877 "print size in bytes")
878 (?c "total" nil grand-total
879 "produce a grand total")
880 (?d "max-depth" t max-depth
881 "display data only this many levels of data")
882 (?h "human-readable" 1024 human-readable
883 "print sizes in human readable format")
884 (?H "is" 1000 human-readable
885 "likewise, but use powers of 1000 not 1024")
886 (?k "kilobytes" 1024 block-size
887 "like --block-size 1024")
888 (?L "dereference" nil dereference-links
889 "dereference all symbolic links")
890 (?m "megabytes" 1048576 block-size
891 "like --block-size 1048576")
892 (?s "summarize" 0 max-depth
893 "display only a total for each argument")
894 (?x "one-file-system" nil only-one-filesystem
895 "skip directories on different filesystems")
897 "show this usage screen")
899 :usage "[OPTION]... FILE...
900 Summarize disk usage of each FILE, recursively for directories.")
902 (setq block-size (or block-size 1024)))
903 (if (and max-depth (stringp max-depth))
904 (setq max-depth (string-to-number max-depth)))
905 ;; filesystem support means nothing under Windows
906 (if (eshell-under-windows-p)
907 (setq only-one-filesystem nil))
908 (let ((size 0.0) ange-cache)
910 (if only-one-filesystem
911 (setq only-one-filesystem
912 (nth 11 (eshell-file-attributes
913 (file-name-as-directory (car args))))))
914 (setq size (+ size (eshell-du-sum-directory
915 (directory-file-name (car args)) 0)))
916 (setq args (cdr args)))
918 (eshell-print (concat (eshell-du-size-string size)
921 (defvar eshell-time-start nil)
923 (defun eshell-show-elapsed-time ()
924 (let ((elapsed (format "%.3f secs\n" (- (float-time) eshell-time-start))))
925 (set-text-properties 0 (length elapsed) '(face bold) elapsed)
926 (eshell-interactive-print elapsed))
927 (remove-hook 'eshell-post-command-hook 'eshell-show-elapsed-time t))
929 (defun eshell/time (&rest args)
930 "Implementation of \"time\" in Lisp."
931 (let ((time-args (copy-alist args))
934 (while (and continue args)
935 (if (not (string-match "^-" (car args)))
938 (setcdr last-arg nil)
943 (eshell-eval-using-options
945 '((?h "help" nil nil "show this usage screen")
949 Show wall-clock time elapsed during execution of COMMAND.")
950 (setq eshell-time-start (float-time))
951 (add-hook 'eshell-post-command-hook 'eshell-show-elapsed-time nil t)
953 (throw 'eshell-replace-command
954 (eshell-parse-command (car time-args)
955 ;;; http://lists.gnu.org/archive/html/bug-gnu-emacs/2007-08/msg00205.html
956 (eshell-stringify-list
957 (eshell-flatten-list (cdr time-args))))))))
959 (defun eshell/whoami (&rest args)
960 "Make \"whoami\" Tramp aware."
961 (or (file-remote-p default-directory 'user) (user-login-name)))
963 (defvar eshell-diff-window-config nil)
965 (defun eshell-diff-quit ()
966 "Restore the window configuration previous to diff'ing."
968 (if eshell-diff-window-config
969 (set-window-configuration eshell-diff-window-config)))
971 (defun nil-blank-string (string)
972 "Return STRING, or nil if STRING contains only non-blank characters."
974 ((string-match "[^[:blank:]]" string) string)
977 (autoload 'diff-no-select "diff")
979 (defun eshell/diff (&rest args)
980 "Alias \"diff\" to call Emacs `diff' function."
981 (let ((orig-args (eshell-stringify-list (eshell-flatten-list args))))
982 (if (or eshell-plain-diff-behavior
983 (not (and (eshell-interactive-output-p)
984 (not eshell-in-pipeline-p)
985 (not eshell-in-subcommand-p))))
986 (throw 'eshell-replace-command
987 (eshell-parse-command "*diff" orig-args))
988 (setq args (copy-sequence orig-args))
989 (if (< (length args) 2)
990 (throw 'eshell-replace-command
991 (eshell-parse-command "*diff" orig-args)))
992 (let ((old (car (last args 2)))
993 (new (car (last args)))
994 (config (current-window-configuration)))
995 (if (= (length args) 2)
997 (setcdr (last args 3) nil))
1002 (nil-blank-string (eshell-flatten-and-stringify args)))
1004 (throw 'eshell-replace-command
1005 (eshell-parse-command "*diff" orig-args))))
1006 (when (fboundp 'diff-mode)
1007 (make-local-variable 'compilation-finish-functions)
1009 'compilation-finish-functions
1011 (with-current-buffer buff
1013 (set (make-local-variable 'eshell-diff-window-config)
1015 (local-set-key [?q] 'eshell-diff-quit)
1016 (if (fboundp 'turn-on-font-lock-if-enabled)
1017 (turn-on-font-lock-if-enabled))
1018 (goto-char (point-min))))))
1019 (pop-to-buffer (current-buffer))))))
1022 (put 'eshell/diff 'eshell-no-numeric-conversions t)
1024 (defvar locate-history-list)
1026 (defun eshell/locate (&rest args)
1027 "Alias \"locate\" to call Emacs `locate' function."
1028 (if (or eshell-plain-locate-behavior
1029 (not (and (eshell-interactive-output-p)
1030 (not eshell-in-pipeline-p)
1031 (not eshell-in-subcommand-p)))
1032 (and (stringp (car args))
1033 (string-match "^-" (car args))))
1034 (throw 'eshell-replace-command
1035 (eshell-parse-command "*locate" (eshell-stringify-list
1036 (eshell-flatten-list args))))
1037 (save-selected-window
1038 (let ((locate-history-list (list (car args))))
1039 (locate-with-filter (car args) (cadr args))))))
1041 (put 'eshell/locate 'eshell-no-numeric-conversions t)
1043 (defun eshell/occur (&rest args)
1044 "Alias \"occur\" to call Emacs `occur' function."
1045 (let ((inhibit-read-only t))
1046 (if (> (length args) 2)
1047 (error "usage: occur: (REGEXP &optional NLINES)")
1048 (apply 'occur args))))
1050 (put 'eshell/occur 'eshell-no-numeric-conversions t)
1055 ;; generated-autoload-file: "esh-groups.el"
1058 ;;; em-unix.el ends here