1 ;;; em-unix.el --- UNIX command aliases -*- lexical-binding:t -*-
3 ;; Copyright (C) 1999-2016 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-message "rm: removing directory `%s'"
215 (format-message "rm: remove directory `%s'? "
217 (eshell-funcalln 'delete-directory
(car files
) t t
)))
219 (eshell-printn (format-message "rm: removing file `%s'"
221 (unless (or em-preview
224 (format-message "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-message "rm: removing buffer `%s'" entry
)))
264 (unless (or em-preview
266 (not (y-or-n-p (format-message
267 "rm: delete buffer `%s'? "
269 (eshell-funcalln 'kill-buffer entry
)))
270 ((eshell-processp entry
)
272 (eshell-printn (format-message "rm: killing process `%s'" entry
)))
273 (unless (or em-preview
275 (not (y-or-n-p (format-message
276 "rm: kill process `%s'? "
278 (eshell-funcalln 'kill-process entry
)))
281 (eshell-printn (format-message
282 "rm: uninterning symbol `%s'" entry
)))
286 (not (y-or-n-p (format-message
287 "rm: unintern symbol `%s'? "
289 (eshell-funcalln 'unintern entry
)))
291 ;; -f should silently ignore missing files (bug#15373).
292 (unless (and force-removal
293 (not (file-exists-p entry
)))
294 (if (and (file-directory-p entry
)
295 (not (file-symlink-p entry
)))
297 eshell-rm-removes-directories
)
301 (format-message "rm: descend into directory `%s'? "
303 (eshell-remove-entries (list entry
) t
))
304 (eshell-error (format "rm: %s: is a directory\n" entry
)))
305 (eshell-remove-entries (list entry
) t
))))))
306 (setq args
(cdr args
)))
309 (put 'eshell
/rm
'eshell-no-numeric-conversions t
)
311 (defun eshell/mkdir
(&rest args
)
312 "Implementation of mkdir in Lisp."
313 (eshell-eval-using-options
315 '((?h
"help" nil nil
"show this usage screen")
316 (?p
"parents" nil em-parents
"make parent directories as needed")
319 :usage
"[OPTION] DIRECTORY...
320 Create the DIRECTORY(ies), if they do not already exist.")
322 (eshell-funcalln 'make-directory
(car args
) em-parents
)
323 (setq args
(cdr args
)))
326 (put 'eshell
/mkdir
'eshell-no-numeric-conversions t
)
328 (defun eshell/rmdir
(&rest args
)
329 "Implementation of rmdir in Lisp."
330 (eshell-eval-using-options
332 '((?h
"help" nil nil
"show this usage screen")
335 :usage
"[OPTION] DIRECTORY...
336 Remove the DIRECTORY(ies), if they are empty.")
338 (eshell-funcalln 'delete-directory
(car args
))
339 (setq args
(cdr args
)))
342 (put 'eshell
/rmdir
'eshell-no-numeric-conversions t
)
344 (defvar no-dereference
)
346 (defvar eshell-warn-dot-directories t
)
348 (defun eshell-shuffle-files (command action files target func deep
&rest args
)
349 "Shuffle around some filesystem entries, using FUNC to do the work."
350 (let ((attr-target (eshell-file-attributes target
))
351 (is-dir (or (file-directory-p target
)
352 (and em-preview
(not eshell-warn-dot-directories
))))
354 (if (and (not em-preview
) (not is-dir
)
355 (> (length files
) 1))
356 (error "%s: when %s multiple files, last argument must be a directory"
359 (setcar files
(directory-file-name (car files
)))
361 ((string-match "\\`\\.\\.?\\'"
362 (file-name-nondirectory (car files
)))
363 (if eshell-warn-dot-directories
364 (eshell-error (format "%s: %s: omitting directory\n"
365 command
(car files
)))))
367 (or (not (eshell-under-windows-p))
368 (eq system-type
'ms-dos
))
369 (setq attr
(eshell-file-attributes (car files
)))
370 (nth 10 attr-target
) (nth 10 attr
)
371 ;; Use equal, not -, since the inode and the device could
373 (equal (nth 10 attr-target
) (nth 10 attr
))
374 (nth 11 attr-target
) (nth 11 attr
)
375 (equal (nth 11 attr-target
) (nth 11 attr
)))
376 (eshell-error (format-message "%s: `%s' and `%s' are the same file\n"
377 command
(car files
) target
)))
379 (let ((source (car files
))
382 (file-name-nondirectory (car files
)) target
)
385 (if (and (file-directory-p source
)
386 (or (not no-dereference
)
387 (not (file-symlink-p source
)))
388 (not (memq func
'(make-symbolic-link
390 (if (and (eq func
'copy-file
)
392 (eshell-error (format "%s: %s: omitting directory\n"
393 command
(car files
)))
394 (let (eshell-warn-dot-directories)
396 (eq func
'rename-file
)
397 ;; Use equal, since the device might be a
399 (equal (nth 11 (eshell-file-attributes
402 (expand-file-name source
)))))
403 (nth 11 (eshell-file-attributes
406 (expand-file-name target
)))))))
407 (apply 'eshell-funcalln func source target args
)
408 (unless (file-directory-p target
)
411 (format "%s: making directory %s"
414 (eshell-funcalln 'make-directory target
)))
415 (apply 'eshell-shuffle-files
420 (concat source
"/" file
)))
421 (directory-files source
))
423 (when (eq func
'rename-file
)
426 (format "%s: deleting directory %s"
429 (eshell-funcalln 'delete-directory source
))))))
431 (eshell-printn (format "%s: %s -> %s" command
434 (if (and no-dereference
435 (setq link
(file-symlink-p source
)))
437 (apply 'eshell-funcalln
'make-symbolic-link
439 (if (eq func
'rename-file
)
440 (if (and (file-directory-p source
)
441 (not (file-symlink-p source
)))
442 (eshell-funcalln 'delete-directory source
)
443 (eshell-funcalln 'delete-file source
))))
444 (apply 'eshell-funcalln func source target args
)))))))
445 (setq files
(cdr files
)))))
447 (defun eshell-shorthand-tar-command (command args
)
448 "Rewrite `cp -v dir a.tar.gz' to `tar cvzf a.tar.gz dir'."
449 (let* ((archive (car (last args
)))
451 (cond ((string-match "z2" archive
) "If")
452 ((string-match "gz" archive
) "zf")
453 ((string-match "\\(az\\|Z\\)" archive
) "Zf")
455 (if (file-exists-p archive
)
456 (setq tar-args
(concat "u" tar-args
))
457 (setq tar-args
(concat "c" tar-args
)))
459 (setq tar-args
(concat "v" tar-args
)))
460 (if (equal command
"mv")
461 (setq tar-args
(concat "--remove-files -" tar-args
)))
462 ;; truncate the archive name from the arguments
463 (setcdr (last args
2) nil
)
464 (throw 'eshell-replace-command
465 (eshell-parse-command
466 (format "tar %s %s" tar-args archive
) args
))))
468 (defvar ange-cache
) ; XEmacs? See esh-util
470 ;; this is to avoid duplicating code...
471 (defmacro eshell-mvcpln-template
(command action func query-var
472 force-var
&optional preserve
)
473 `(let ((len (length args
)))
475 (and (= len
1) (null eshell-default-target-is-dot
)))
476 (error "%s: missing destination file or directory" ,command
))
479 (setq args
(eshell-stringify-list (eshell-flatten-list args
)))
480 (if (and ,(not (equal command
"ln"))
481 (string-match eshell-tar-regexp
(car (last args
)))
482 (or (> (length args
) 2)
483 (and (file-directory-p (car args
))
484 (or (not no-dereference
)
485 (not (file-symlink-p (car args
)))))))
486 (eshell-shorthand-tar-command ,command args
)
487 (let ((target (car (last args
)))
489 (setcdr (last args
2) nil
)
490 (eshell-shuffle-files
491 ,command
,action args target
,func nil
493 `((if (and (or em-interactive
496 1 (or force
,force-var
)))
501 (defun eshell/mv
(&rest args
)
502 "Implementation of mv in Lisp."
503 (eshell-eval-using-options
505 '((?f
"force" nil force
506 "remove existing destinations, never prompt")
507 (?i
"interactive" nil em-interactive
508 "request confirmation if target already exists")
509 (?n
"preview" nil em-preview
510 "don't change anything on disk")
511 (?v
"verbose" nil em-verbose
512 "explain what is being done")
513 (nil "help" nil nil
"show this usage screen")
517 :usage
"[OPTION]... SOURCE DEST
518 or: mv [OPTION]... SOURCE... DIRECTORY
519 Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.
520 [OPTION] DIRECTORY...")
521 (let ((no-dereference t
))
522 (eshell-mvcpln-template "mv" "moving" 'rename-file
523 eshell-mv-interactive-query
524 eshell-mv-overwrite-files
))))
526 (put 'eshell
/mv
'eshell-no-numeric-conversions t
)
528 (defun eshell/cp
(&rest args
)
529 "Implementation of cp in Lisp."
530 (eshell-eval-using-options
532 '((?a
"archive" nil archive
534 (?d
"no-dereference" nil no-dereference
536 (?f
"force" nil force
537 "remove existing destinations, never prompt")
538 (?i
"interactive" nil em-interactive
539 "request confirmation if target already exists")
540 (?n
"preview" nil em-preview
541 "don't change anything on disk")
542 (?p
"preserve" nil preserve
543 "preserve file attributes if possible")
544 (?r
"recursive" nil em-recursive
545 "copy directories recursively")
546 (?R nil nil em-recursive
548 (?v
"verbose" nil em-verbose
549 "explain what is being done")
550 (nil "help" nil nil
"show this usage screen")
554 :usage
"[OPTION]... SOURCE DEST
555 or: cp [OPTION]... SOURCE... DIRECTORY
556 Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.")
558 (setq preserve t no-dereference t em-recursive t
))
559 (eshell-mvcpln-template "cp" "copying" 'copy-file
560 eshell-cp-interactive-query
561 eshell-cp-overwrite-files preserve
)))
563 (put 'eshell
/cp
'eshell-no-numeric-conversions t
)
565 (defun eshell/ln
(&rest args
)
566 "Implementation of ln in Lisp."
567 (eshell-eval-using-options
569 '((?h
"help" nil nil
"show this usage screen")
570 (?s
"symbolic" nil symbolic
571 "make symbolic links instead of hard links")
572 (?i
"interactive" nil em-interactive
573 "request confirmation if target already exists")
574 (?f
"force" nil force
"remove existing destinations, never prompt")
575 (?n
"preview" nil em-preview
576 "don't change anything on disk")
577 (?v
"verbose" nil em-verbose
"explain what is being done")
581 :usage
"[OPTION]... TARGET [LINK_NAME]
582 or: ln [OPTION]... TARGET... DIRECTORY
583 Create a link to the specified TARGET with optional LINK_NAME. If there is
584 more than one TARGET, the last argument must be a directory; create links
585 in DIRECTORY to each TARGET. Create hard links by default, symbolic links
586 with `--symbolic'. When creating hard links, each TARGET must exist.")
587 (let ((no-dereference t
))
588 (eshell-mvcpln-template "ln" "linking"
592 eshell-ln-interactive-query
593 eshell-ln-overwrite-files
))))
595 (put 'eshell
/ln
'eshell-no-numeric-conversions t
)
597 (defun eshell/cat
(&rest args
)
598 "Implementation of cat in Lisp.
599 If in a pipeline, or the file is not a regular file, directory or
600 symlink, then revert to the system's definition of cat."
601 (setq args
(eshell-stringify-list (eshell-flatten-list args
)))
602 (if (or eshell-in-pipeline-p
605 (unless (or (and (stringp arg
)
607 (eq (aref arg
0) ?-
))
608 (let ((attrs (eshell-file-attributes arg
)))
609 (and attrs
(memq (aref (nth 8 attrs
) 0)
611 (throw 'special t
)))))
612 (let ((ext-cat (eshell-search-path "cat")))
614 (throw 'eshell-replace-command
615 (eshell-parse-command (eshell-quote-argument ext-cat
) args
))
616 (if eshell-in-pipeline-p
617 (error "Eshell's `cat' does not work in pipelines")
618 (error "Eshell's `cat' cannot display one of the files given"))))
619 (eshell-init-print-buffer)
620 (eshell-eval-using-options
622 '((?h
"help" nil nil
"show this usage screen")
625 :usage
"[OPTION] FILE...
626 Concatenate FILE(s), or standard input, to standard output.")
628 (if (string= file
"-")
629 (throw 'eshell-external
630 (eshell-external-command "cat" args
))))
631 (let ((curbuf (current-buffer)))
634 (insert-file-contents file
)
635 (goto-char (point-min))
637 (let ((str (buffer-substring
638 (point) (min (1+ (line-end-position))
640 (with-current-buffer curbuf
641 (eshell-buffered-print str
)))
644 ;; if the file does not end in a newline, do not emit one
645 (setq eshell-ensure-newline-p nil
))))
647 (put 'eshell
/cat
'eshell-no-numeric-conversions t
)
649 ;; special front-end functions for compilation-mode buffers
651 (defun eshell/make
(&rest args
)
652 "Use `compile' to do background makes."
653 (if (and eshell-current-subjob-p
654 (eshell-interactive-output-p))
655 (let ((compilation-process-setup-function
657 (list 'setq
'process-environment
658 (list 'quote
(eshell-copy-environment))))))
659 (compile (concat "make " (eshell-flatten-and-stringify args
))))
660 (throw 'eshell-replace-command
661 (eshell-parse-command "*make" (eshell-stringify-list
662 (eshell-flatten-list args
))))))
664 (put 'eshell
/make
'eshell-no-numeric-conversions t
)
666 (defun eshell-occur-mode-goto-occurrence ()
667 "Go to the occurrence the current line describes."
669 (let ((pos (occur-mode-find-occurrence)))
670 (pop-to-buffer (marker-buffer pos
))
671 (goto-char (marker-position pos
))))
673 (defun eshell-occur-mode-mouse-goto (event)
674 "In Occur mode, go to the occurrence whose line you click on."
677 (with-current-buffer (window-buffer (posn-window (event-end event
)))
679 (goto-char (posn-point (event-end event
)))
680 (setq pos
(occur-mode-find-occurrence))))
681 (pop-to-buffer (marker-buffer pos
))
682 (goto-char (marker-position pos
))))
684 (defun eshell-poor-mans-grep (args)
685 "A poor version of grep that opens every file and uses `occur'.
686 This eats up memory, since it leaves the buffers open (to speed future
687 searches), and it's very slow. But, if your system has no grep
689 (save-selected-window
690 (let ((default-dir default-directory
))
691 (with-current-buffer (get-buffer-create "*grep*")
692 (let ((inhibit-read-only t
)
693 (default-directory default-dir
))
696 (let ((files (eshell-stringify-list
697 (eshell-flatten-list (cdr args
))))
698 (inhibit-redisplay t
)
701 (if (get-buffer "*Occur*")
702 (kill-buffer (get-buffer "*Occur*")))
705 (with-current-buffer (find-file-noselect (car files
))
709 (if (get-buffer "*Occur*")
710 (with-current-buffer (get-buffer "*Occur*")
711 (setq string
(buffer-string))
712 (kill-buffer (current-buffer)))))
713 (if string
(insert string
))
715 files
(cdr files
)))))
716 (local-set-key [mouse-2
] 'eshell-occur-mode-mouse-goto
)
717 (local-set-key [(control ?c
) (control ?c
)]
718 'eshell-occur-mode-goto-occurrence
)
719 (local-set-key [(control ?m
)]
720 'eshell-occur-mode-goto-occurrence
)
721 (local-set-key [return] 'eshell-occur-mode-goto-occurrence)
722 (pop-to-buffer (current-buffer) t)
723 (goto-char (point-min))
724 (resize-temp-buffer-window))))))
726 (defvar compilation-scroll-output)
728 (defun eshell-grep (command args &optional maybe-use-occur)
729 "Generic service function for the various grep aliases.
730 It calls Emacs's grep utility if the command is not redirecting output,
731 and if it's not part of a command pipeline. Otherwise, it calls the
733 (if (and maybe-use-occur eshell-no-grep-available)
734 (eshell-poor-mans-grep args)
735 (if (or eshell-plain-grep-behavior
736 (not (and (eshell-interactive-output-p)
737 (not eshell-in-pipeline-p)
738 (not eshell-in-subcommand-p))))
739 (throw 'eshell-replace-command
740 (eshell-parse-command (concat "*" command)
741 (eshell-stringify-list
742 (eshell-flatten-list args))))
743 (let* ((args (mapconcat 'identity
744 (mapcar 'shell-quote-argument
745 (eshell-stringify-list
746 (eshell-flatten-list args)))
749 (set-text-properties 0 (length args)
757 compilation-scroll-output)
760 (defun eshell/grep (&rest args)
761 "Use Emacs grep facility instead of calling external grep."
762 (eshell-grep "grep" args t))
764 (defun eshell/egrep (&rest args)
765 "Use Emacs grep facility instead of calling external grep -E."
766 (eshell-grep "egrep" args t))
768 (defun eshell/fgrep (&rest args)
769 "Use Emacs grep facility instead of calling external grep -F."
770 (eshell-grep "fgrep" args t))
772 (defun eshell/agrep (&rest args)
773 "Use Emacs grep facility instead of calling external agrep."
774 (eshell-grep "agrep" args))
776 (defun eshell/glimpse (&rest args)
777 "Use Emacs grep facility instead of calling external glimpse."
779 (eshell-grep "glimpse" (append '("-z" "-y") args))))
781 ;; completions rules for some common UNIX commands
783 (defsubst eshell-complete-hostname ()
784 "Complete a command that wants a hostname for an argument."
785 (pcomplete-here (eshell-read-host-names)))
787 (defun eshell-complete-host-reference ()
788 "If there is a host reference, complete it."
789 (let ((arg (pcomplete-actual-arg))
791 (when (setq index (string-match "@[a-z.]*\\'" arg))
792 (setq pcomplete-stub (substring arg (1+ index))
793 pcomplete-last-completion-raw t)
794 (throw 'pcomplete-completions (eshell-read-host-names)))))
796 (defalias 'pcomplete/ftp 'eshell-complete-hostname)
797 (defalias 'pcomplete/ncftp 'eshell-complete-hostname)
798 (defalias 'pcomplete/ping 'eshell-complete-hostname)
799 (defalias 'pcomplete/rlogin 'eshell-complete-hostname)
801 (defun pcomplete/telnet ()
802 (require 'pcmpl-unix)
803 (pcomplete-opt "xl(pcmpl-unix-user-names)")
804 (eshell-complete-hostname))
806 (defun pcomplete/rsh ()
807 "Complete `rsh', which, after the user and hostname, is like xargs."
808 (require 'pcmpl-unix)
809 (pcomplete-opt "l(pcmpl-unix-user-names)")
810 (eshell-complete-hostname)
811 (pcomplete-here (funcall pcomplete-command-completion-function))
812 (funcall (or (pcomplete-find-completion-function (pcomplete-arg 1))
813 pcomplete-default-completion-function)))
817 (defvar dereference-links)
819 (defvar human-readable)
821 (defvar only-one-filesystem)
824 (defsubst eshell-du-size-string (size)
825 (let* ((str (eshell-printable-size size human-readable block-size t))
827 (concat str (if (< len 8)
828 (make-string (- 8 len) ? )))))
830 (defun eshell-du-sum-directory (path depth)
831 "Summarize PATH, and its member directories."
832 (let ((entries (eshell-directory-files-and-attributes path))
835 (unless (string-match "\\`\\.\\.?\\'" (caar entries))
836 (let* ((entry (concat path "/"
838 (symlink (and (stringp (cadr (car entries)))
839 (cadr (car entries)))))
840 (unless (or (and symlink (not dereference-links))
841 (and only-one-filesystem
842 (/= only-one-filesystem
843 (nth 12 (car entries)))))
845 (setq entry symlink))
848 (if (eq t (cadr (car entries)))
849 (eshell-du-sum-directory entry (1+ depth))
850 (let ((file-size (nth 8 (car entries))))
855 (concat (eshell-du-size-string file-size)
856 entry "\n")))))))))))
857 (setq entries (cdr entries)))
858 (if (or (not max-depth)
861 (eshell-print (concat (eshell-du-size-string size)
862 (directory-file-name path) "\n")))
865 (defun eshell/du (&rest args)
866 "Implementation of \"du\" in Lisp, passing ARGS."
868 (eshell-stringify-list (eshell-flatten-list args))
870 (let ((ext-du (eshell-search-path "du")))
872 (not (catch 'have-ange-path
875 (file-remote-p (expand-file-name arg) 'method) "ftp")
876 (throw 'have-ange-path t))))))
877 (throw 'eshell-replace-command
878 (eshell-parse-command (eshell-quote-argument ext-du) args))
879 (eshell-eval-using-options
881 '((?a "all" nil show-all
882 "write counts for all files, not just directories")
883 (nil "block-size" t block-size
884 "use SIZE-byte blocks (i.e., --block-size SIZE)")
885 (?b "bytes" nil by-bytes
886 "print size in bytes")
887 (?c "total" nil grand-total
888 "produce a grand total")
889 (?d "max-depth" t max-depth
890 "display data only this many levels of data")
891 (?h "human-readable" 1024 human-readable
892 "print sizes in human readable format")
893 (?H "is" 1000 human-readable
894 "likewise, but use powers of 1000 not 1024")
895 (?k "kilobytes" 1024 block-size
896 "like --block-size 1024")
897 (?L "dereference" nil dereference-links
898 "dereference all symbolic links")
899 (?m "megabytes" 1048576 block-size
900 "like --block-size 1048576")
901 (?s "summarize" 0 max-depth
902 "display only a total for each argument")
903 (?x "one-file-system" nil only-one-filesystem
904 "skip directories on different filesystems")
906 "show this usage screen")
908 :usage "[OPTION]... FILE...
909 Summarize disk usage of each FILE, recursively for directories.")
911 (setq block-size (or block-size 1024)))
912 (if (and max-depth (stringp max-depth))
913 (setq max-depth (string-to-number max-depth)))
914 ;; filesystem support means nothing under Windows
915 (if (eshell-under-windows-p)
916 (setq only-one-filesystem nil))
917 (let ((size 0.0) ange-cache)
919 (if only-one-filesystem
920 (setq only-one-filesystem
921 (nth 11 (eshell-file-attributes
922 (file-name-as-directory (car args))))))
923 (setq size (+ size (eshell-du-sum-directory
924 (directory-file-name (car args)) 0)))
925 (setq args (cdr args)))
927 (eshell-print (concat (eshell-du-size-string size)
930 (defvar eshell-time-start nil)
932 (defun eshell-show-elapsed-time ()
933 (let ((elapsed (format "%.3f secs\n" (- (float-time) eshell-time-start))))
934 (set-text-properties 0 (length elapsed) '(face bold) elapsed)
935 (eshell-interactive-print elapsed))
936 (remove-hook 'eshell-post-command-hook 'eshell-show-elapsed-time t))
938 (defun eshell/time (&rest args)
939 "Implementation of \"time\" in Lisp."
940 (let ((time-args (copy-alist args))
943 (while (and continue args)
944 (if (not (string-match "^-" (car args)))
947 (setcdr last-arg nil)
952 (eshell-eval-using-options
954 '((?h "help" nil nil "show this usage screen")
958 Show wall-clock time elapsed during execution of COMMAND.")
959 (setq eshell-time-start (float-time))
960 (add-hook 'eshell-post-command-hook 'eshell-show-elapsed-time nil t)
962 (throw 'eshell-replace-command
963 (eshell-parse-command (car time-args)
964 ;;; http://lists.gnu.org/archive/html/bug-gnu-emacs/2007-08/msg00205.html
965 (eshell-stringify-list
966 (eshell-flatten-list (cdr time-args))))))))
968 (defun eshell/whoami (&rest args)
969 "Make \"whoami\" Tramp aware."
970 (or (file-remote-p default-directory 'user) (user-login-name)))
972 (defvar eshell-diff-window-config nil)
974 (defun eshell-diff-quit ()
975 "Restore the window configuration previous to diff'ing."
977 (if eshell-diff-window-config
978 (set-window-configuration eshell-diff-window-config)))
980 (defun nil-blank-string (string)
981 "Return STRING, or nil if STRING contains only non-blank characters."
983 ((string-match "[^[:blank:]]" string) string)
986 (autoload 'diff-no-select "diff")
988 (defun eshell/diff (&rest args)
989 "Alias \"diff\" to call Emacs `diff' function."
990 (let ((orig-args (eshell-stringify-list (eshell-flatten-list args))))
991 (if (or eshell-plain-diff-behavior
992 (not (and (eshell-interactive-output-p)
993 (not eshell-in-pipeline-p)
994 (not eshell-in-subcommand-p))))
995 (throw 'eshell-replace-command
996 (eshell-parse-command "*diff" orig-args))
997 (setq args (copy-sequence orig-args))
998 (if (< (length args) 2)
999 (throw 'eshell-replace-command
1000 (eshell-parse-command "*diff" orig-args)))
1001 (let ((old (car (last args 2)))
1002 (new (car (last args)))
1003 (config (current-window-configuration)))
1004 (if (= (length args) 2)
1006 (setcdr (last args 3) nil))
1007 (with-current-buffer
1011 (nil-blank-string (eshell-flatten-and-stringify args)))
1013 (throw 'eshell-replace-command
1014 (eshell-parse-command "*diff" orig-args))))
1015 (when (fboundp 'diff-mode)
1016 (make-local-variable 'compilation-finish-functions)
1018 'compilation-finish-functions
1020 (with-current-buffer buff
1022 (set (make-local-variable 'eshell-diff-window-config)
1024 (local-set-key [?q] 'eshell-diff-quit)
1025 (if (fboundp 'turn-on-font-lock-if-enabled)
1026 (turn-on-font-lock-if-enabled))
1027 (goto-char (point-min))))))
1028 (pop-to-buffer (current-buffer))))))
1031 (put 'eshell/diff 'eshell-no-numeric-conversions t)
1033 (defvar locate-history-list)
1035 (defun eshell/locate (&rest args)
1036 "Alias \"locate\" to call Emacs `locate' function."
1037 (if (or eshell-plain-locate-behavior
1038 (not (and (eshell-interactive-output-p)
1039 (not eshell-in-pipeline-p)
1040 (not eshell-in-subcommand-p)))
1041 (and (stringp (car args))
1042 (string-match "^-" (car args))))
1043 (throw 'eshell-replace-command
1044 (eshell-parse-command "*locate" (eshell-stringify-list
1045 (eshell-flatten-list args))))
1046 (save-selected-window
1047 (let ((locate-history-list (list (car args))))
1048 (locate-with-filter (car args) (cadr args))))))
1050 (put 'eshell/locate 'eshell-no-numeric-conversions t)
1052 (defun eshell/occur (&rest args)
1053 "Alias \"occur\" to call Emacs `occur' function."
1054 (let ((inhibit-read-only t))
1055 (if (> (length args) 2)
1056 (error "usage: occur: (REGEXP &optional NLINES)")
1057 (apply 'occur args))))
1059 (put 'eshell/occur 'eshell-no-numeric-conversions t)
1064 ;; generated-autoload-file: "esh-groups.el"
1067 ;;; em-unix.el ends here