1 ;;; em-unix.el --- UNIX command aliases
3 ;; Copyright (C) 1999-2012 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" "su" "sudo")
152 eshell-complex-commands
)))
154 (defalias 'eshell
/date
'current-time-string
)
155 (defalias 'eshell
/basename
'file-name-nondirectory
)
156 (defalias 'eshell
/dirname
'file-name-directory
)
158 (defvar em-interactive
)
160 (defvar em-recursive
)
163 (defun eshell/man
(&rest args
)
164 "Invoke man, flattening the arguments appropriately."
165 (funcall 'man
(apply 'eshell-flatten-and-stringify args
)))
167 (put 'eshell
/man
'eshell-no-numeric-conversions t
)
169 (defun eshell/info
(&rest args
)
170 "Run the info command in-frame with the same behavior as command-line `info', ie:
171 'info' => goes to top info window
172 'info arg1' => IF arg1 is a file, then visits arg1
173 'info arg1' => OTHERWISE goes to top info window and then menu item arg1
174 'info arg1 arg2' => does action for arg1 (either visit-file or menu-item) and then menu item arg2
176 (eval-and-compile (require 'info
))
178 ((not (stringp (car args
)))
180 ((file-exists-p (expand-file-name (car args
)))
181 (expand-file-name (car args
)))
182 ((file-exists-p (concat (expand-file-name (car args
)) ".info"))
183 (concat (expand-file-name (car args
)) ".info")))))
185 ;; If the first arg is a file, then go to that file's Top node
186 ;; Otherwise, go to the global directory
189 (setq args
(cdr args
))
190 (Info-find-node file
"Top"))
193 ;; Treat all remaining args as menu references
195 (Info-menu (car args
))
196 (setq args
(cdr args
)))))
198 (defun eshell-remove-entries (path files
&optional top-level
)
199 "From PATH, remove all of the given FILES, perhaps interactively."
201 (if (string-match "\\`\\.\\.?\\'"
202 (file-name-nondirectory (car files
)))
204 (eshell-error "rm: cannot remove `.' or `..'\n"))
205 (if (and (file-directory-p (car files
))
206 (not (file-symlink-p (car files
))))
209 (eshell-printn (format "rm: removing directory `%s'"
215 (format "rm: remove directory `%s'? "
217 (eshell-funcalln 'delete-directory
(car files
) t t
)))
219 (eshell-printn (format "rm: removing file `%s'"
221 (unless (or em-preview
224 (format "rm: remove `%s'? "
226 (eshell-funcalln 'delete-file
(car files
) t
))))
227 (setq files
(cdr files
))))
229 (defun eshell/rm
(&rest args
)
230 "Implementation of rm in Lisp.
231 This is implemented to call either `delete-file', `kill-buffer',
232 `kill-process', or `unintern', depending on the nature of the
234 (setq args
(eshell-flatten-list args
))
235 (eshell-eval-using-options
237 '((?h
"help" nil nil
"show this usage screen")
238 (?f
"force" nil force-removal
"force removal")
239 (?i
"interactive" nil em-interactive
"prompt before any removal")
240 (?n
"preview" nil em-preview
"don't change anything on disk")
241 (?r
"recursive" nil em-recursive
242 "remove the contents of directories recursively")
243 (?R nil nil em-recursive
"(same)")
244 (?v
"verbose" nil em-verbose
"explain what is being done")
248 :usage
"[OPTION]... FILE...
249 Remove (unlink) the FILE(s).")
250 (unless em-interactive
251 (setq em-interactive eshell-rm-interactive-query
))
252 (if (and force-removal em-interactive
)
253 (setq em-interactive nil
))
255 (let ((entry (if (stringp (car args
))
256 (directory-file-name (car args
))
257 (if (numberp (car args
))
258 (number-to-string (car args
))
263 (eshell-printn (format "rm: removing buffer `%s'" entry
)))
264 (unless (or em-preview
266 (not (y-or-n-p (format "rm: delete buffer `%s'? "
268 (eshell-funcalln 'kill-buffer entry
)))
269 ((eshell-processp entry
)
271 (eshell-printn (format "rm: killing process `%s'" entry
)))
272 (unless (or em-preview
274 (not (y-or-n-p (format "rm: kill process `%s'? "
276 (eshell-funcalln 'kill-process entry
)))
279 (eshell-printn (format "rm: uninterning symbol `%s'" entry
)))
283 (not (y-or-n-p (format "rm: unintern symbol `%s'? "
285 (eshell-funcalln 'unintern entry
)))
287 (if (and (file-directory-p entry
)
288 (not (file-symlink-p entry
)))
290 eshell-rm-removes-directories
)
294 (format "rm: descend into directory `%s'? "
296 (eshell-remove-entries nil
(list entry
) t
))
297 (eshell-error (format "rm: %s: is a directory\n" entry
)))
298 (eshell-remove-entries nil
(list entry
) t
)))))
299 (setq args
(cdr args
)))
302 (put 'eshell
/rm
'eshell-no-numeric-conversions t
)
304 (defun eshell/mkdir
(&rest args
)
305 "Implementation of mkdir in Lisp."
306 (eshell-eval-using-options
308 '((?h
"help" nil nil
"show this usage screen")
309 (?p
"parents" nil em-parents
"make parent directories as needed")
312 :usage
"[OPTION] DIRECTORY...
313 Create the DIRECTORY(ies), if they do not already exist.")
315 (eshell-funcalln 'make-directory
(car args
) em-parents
)
316 (setq args
(cdr args
)))
319 (put 'eshell
/mkdir
'eshell-no-numeric-conversions t
)
321 (defun eshell/rmdir
(&rest args
)
322 "Implementation of rmdir in Lisp."
323 (eshell-eval-using-options
325 '((?h
"help" nil nil
"show this usage screen")
328 :usage
"[OPTION] DIRECTORY...
329 Remove the DIRECTORY(ies), if they are empty.")
331 (eshell-funcalln 'delete-directory
(car args
))
332 (setq args
(cdr args
)))
335 (put 'eshell
/rmdir
'eshell-no-numeric-conversions t
)
337 (defvar no-dereference
)
339 (defvar eshell-warn-dot-directories t
)
341 (defun eshell-shuffle-files (command action files target func deep
&rest args
)
342 "Shuffle around some filesystem entries, using FUNC to do the work."
343 (let ((attr-target (eshell-file-attributes target
))
344 (is-dir (or (file-directory-p target
)
345 (and em-preview
(not eshell-warn-dot-directories
))))
347 (if (and (not em-preview
) (not is-dir
)
348 (> (length files
) 1))
349 (error "%s: when %s multiple files, last argument must be a directory"
352 (setcar files
(directory-file-name (car files
)))
354 ((string-match "\\`\\.\\.?\\'"
355 (file-name-nondirectory (car files
)))
356 (if eshell-warn-dot-directories
357 (eshell-error (format "%s: %s: omitting directory\n"
358 command
(car files
)))))
360 (or (not (eshell-under-windows-p))
361 (eq system-type
'ms-dos
))
362 (setq attr
(eshell-file-attributes (car files
)))
363 (nth 10 attr-target
) (nth 10 attr
)
364 ;; Use equal, not -, since the inode and the device could
366 (equal (nth 10 attr-target
) (nth 10 attr
))
367 (nth 11 attr-target
) (nth 11 attr
)
368 (equal (nth 11 attr-target
) (nth 11 attr
)))
369 (eshell-error (format "%s: `%s' and `%s' are the same file\n"
370 command
(car files
) target
)))
372 (let ((source (car files
))
375 (file-name-nondirectory (car files
)) target
)
378 (if (and (file-directory-p source
)
379 (or (not no-dereference
)
380 (not (file-symlink-p source
)))
381 (not (memq func
'(make-symbolic-link
383 (if (and (eq func
'copy-file
)
385 (eshell-error (format "%s: %s: omitting directory\n"
386 command
(car files
)))
387 (let (eshell-warn-dot-directories)
389 (eq func
'rename-file
)
390 ;; Use equal, since the device might be a
392 (equal (nth 11 (eshell-file-attributes
395 (expand-file-name source
)))))
396 (nth 11 (eshell-file-attributes
399 (expand-file-name target
)))))))
400 (apply 'eshell-funcalln func source target args
)
401 (unless (file-directory-p target
)
404 (format "%s: making directory %s"
407 (eshell-funcalln 'make-directory target
)))
408 (apply 'eshell-shuffle-files
413 (concat source
"/" file
)))
414 (directory-files source
))
416 (when (eq func
'rename-file
)
419 (format "%s: deleting directory %s"
422 (eshell-funcalln 'delete-directory source
))))))
424 (eshell-printn (format "%s: %s -> %s" command
427 (if (and no-dereference
428 (setq link
(file-symlink-p source
)))
430 (apply 'eshell-funcalln
'make-symbolic-link
432 (if (eq func
'rename-file
)
433 (if (and (file-directory-p source
)
434 (not (file-symlink-p source
)))
435 (eshell-funcalln 'delete-directory source
)
436 (eshell-funcalln 'delete-file source
))))
437 (apply 'eshell-funcalln func source target args
)))))))
438 (setq files
(cdr files
)))))
440 (defun eshell-shorthand-tar-command (command args
)
441 "Rewrite `cp -v dir a.tar.gz' to `tar cvzf a.tar.gz dir'."
442 (let* ((archive (car (last args
)))
444 (cond ((string-match "z2" archive
) "If")
445 ((string-match "gz" archive
) "zf")
446 ((string-match "\\(az\\|Z\\)" archive
) "Zf")
448 (if (file-exists-p archive
)
449 (setq tar-args
(concat "u" tar-args
))
450 (setq tar-args
(concat "c" tar-args
)))
452 (setq tar-args
(concat "v" tar-args
)))
453 (if (equal command
"mv")
454 (setq tar-args
(concat "--remove-files -" tar-args
)))
455 ;; truncate the archive name from the arguments
456 (setcdr (last args
2) nil
)
457 (throw 'eshell-replace-command
458 (eshell-parse-command
459 (format "tar %s %s" tar-args archive
) args
))))
461 ;; this is to avoid duplicating code...
462 (defmacro eshell-mvcpln-template
(command action func query-var
463 force-var
&optional preserve
)
464 `(let ((len (length args
)))
466 (and (= len
1) (null eshell-default-target-is-dot
)))
467 (error "%s: missing destination file or directory" ,command
))
470 (setq args
(eshell-stringify-list (eshell-flatten-list args
)))
471 (if (and ,(not (equal command
"ln"))
472 (string-match eshell-tar-regexp
(car (last args
)))
473 (or (> (length args
) 2)
474 (and (file-directory-p (car args
))
475 (or (not no-dereference
)
476 (not (file-symlink-p (car args
)))))))
477 (eshell-shorthand-tar-command ,command args
)
478 (let ((target (car (last args
)))
480 (setcdr (last args
2) nil
)
481 (eshell-shuffle-files
482 ,command
,action args target
,func nil
484 `((if (and (or em-interactive
487 1 (or force
,force-var
)))
492 (defun eshell/mv
(&rest args
)
493 "Implementation of mv in Lisp."
494 (eshell-eval-using-options
496 '((?f
"force" nil force
497 "remove existing destinations, never prompt")
498 (?i
"interactive" nil em-interactive
499 "request confirmation if target already exists")
500 (?n
"preview" nil em-preview
501 "don't change anything on disk")
502 (?v
"verbose" nil em-verbose
503 "explain what is being done")
504 (nil "help" nil nil
"show this usage screen")
508 :usage
"[OPTION]... SOURCE DEST
509 or: mv [OPTION]... SOURCE... DIRECTORY
510 Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.
511 \[OPTION] DIRECTORY...")
512 (let ((no-dereference t
))
513 (eshell-mvcpln-template "mv" "moving" 'rename-file
514 eshell-mv-interactive-query
515 eshell-mv-overwrite-files
))))
517 (put 'eshell
/mv
'eshell-no-numeric-conversions t
)
519 (defun eshell/cp
(&rest args
)
520 "Implementation of cp in Lisp."
521 (eshell-eval-using-options
523 '((?a
"archive" nil archive
525 (?d
"no-dereference" nil no-dereference
527 (?f
"force" nil force
528 "remove existing destinations, never prompt")
529 (?i
"interactive" nil em-interactive
530 "request confirmation if target already exists")
531 (?n
"preview" nil em-preview
532 "don't change anything on disk")
533 (?p
"preserve" nil preserve
534 "preserve file attributes if possible")
535 (?R
"recursive" nil em-recursive
536 "copy directories recursively")
537 (?v
"verbose" nil em-verbose
538 "explain what is being done")
539 (nil "help" nil nil
"show this usage screen")
543 :usage
"[OPTION]... SOURCE DEST
544 or: cp [OPTION]... SOURCE... DIRECTORY
545 Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.")
547 (setq preserve t no-dereference t em-recursive t
))
548 (eshell-mvcpln-template "cp" "copying" 'copy-file
549 eshell-cp-interactive-query
550 eshell-cp-overwrite-files preserve
)))
552 (put 'eshell
/cp
'eshell-no-numeric-conversions t
)
554 (defun eshell/ln
(&rest args
)
555 "Implementation of ln in Lisp."
556 (eshell-eval-using-options
558 '((?h
"help" nil nil
"show this usage screen")
559 (?s
"symbolic" nil symbolic
560 "make symbolic links instead of hard links")
561 (?i
"interactive" nil em-interactive
562 "request confirmation if target already exists")
563 (?f
"force" nil force
"remove existing destinations, never prompt")
564 (?n
"preview" nil em-preview
565 "don't change anything on disk")
566 (?v
"verbose" nil em-verbose
"explain what is being done")
570 :usage
"[OPTION]... TARGET [LINK_NAME]
571 or: ln [OPTION]... TARGET... DIRECTORY
572 Create a link to the specified TARGET with optional LINK_NAME. If there is
573 more than one TARGET, the last argument must be a directory; create links
574 in DIRECTORY to each TARGET. Create hard links by default, symbolic links
575 with '--symbolic'. When creating hard links, each TARGET must exist.")
576 (let ((no-dereference t
))
577 (eshell-mvcpln-template "ln" "linking"
581 eshell-ln-interactive-query
582 eshell-ln-overwrite-files
))))
584 (put 'eshell
/ln
'eshell-no-numeric-conversions t
)
586 (defun eshell/cat
(&rest args
)
587 "Implementation of cat in Lisp.
588 If in a pipeline, or the file is not a regular file, directory or
589 symlink, then revert to the system's definition of cat."
590 (setq args
(eshell-stringify-list (eshell-flatten-list args
)))
591 (if (or eshell-in-pipeline-p
594 (unless (or (and (stringp arg
)
596 (eq (aref arg
0) ?-
))
597 (let ((attrs (eshell-file-attributes arg
)))
598 (and attrs
(memq (aref (nth 8 attrs
) 0)
600 (throw 'special t
)))))
601 (let ((ext-cat (eshell-search-path "cat")))
603 (throw 'eshell-replace-command
604 (eshell-parse-command (eshell-quote-argument ext-cat
) args
))
605 (if eshell-in-pipeline-p
606 (error "Eshell's `cat' does not work in pipelines")
607 (error "Eshell's `cat' cannot display one of the files given"))))
608 (eshell-init-print-buffer)
609 (eshell-eval-using-options
611 '((?h
"help" nil nil
"show this usage screen")
614 :usage
"[OPTION] FILE...
615 Concatenate FILE(s), or standard input, to standard output.")
617 (if (string= file
"-")
618 (throw 'eshell-external
619 (eshell-external-command "cat" args
))))
620 (let ((curbuf (current-buffer)))
623 (insert-file-contents file
)
624 (goto-char (point-min))
626 (let ((str (buffer-substring
627 (point) (min (1+ (line-end-position))
629 (with-current-buffer curbuf
630 (eshell-buffered-print str
)))
633 ;; if the file does not end in a newline, do not emit one
634 (setq eshell-ensure-newline-p nil
))))
636 (put 'eshell
/cat
'eshell-no-numeric-conversions t
)
638 ;; special front-end functions for compilation-mode buffers
640 (defun eshell/make
(&rest args
)
641 "Use `compile' to do background makes."
642 (if (and eshell-current-subjob-p
643 (eshell-interactive-output-p))
644 (let ((compilation-process-setup-function
646 (list 'setq
'process-environment
647 (list 'quote
(eshell-copy-environment))))))
648 (compile (concat "make " (eshell-flatten-and-stringify args
))))
649 (throw 'eshell-replace-command
650 (eshell-parse-command "*make" (eshell-stringify-list
651 (eshell-flatten-list args
))))))
653 (put 'eshell
/make
'eshell-no-numeric-conversions t
)
655 (defun eshell-occur-mode-goto-occurrence ()
656 "Go to the occurrence the current line describes."
658 (let ((pos (occur-mode-find-occurrence)))
659 (pop-to-buffer (marker-buffer pos
))
660 (goto-char (marker-position pos
))))
662 (defun eshell-occur-mode-mouse-goto (event)
663 "In Occur mode, go to the occurrence whose line you click on."
666 (with-current-buffer (window-buffer (posn-window (event-end event
)))
668 (goto-char (posn-point (event-end event
)))
669 (setq pos
(occur-mode-find-occurrence))))
670 (pop-to-buffer (marker-buffer pos
))
671 (goto-char (marker-position pos
))))
673 (defun eshell-poor-mans-grep (args)
674 "A poor version of grep that opens every file and uses `occur'.
675 This eats up memory, since it leaves the buffers open (to speed future
676 searches), and it's very slow. But, if your system has no grep
678 (save-selected-window
679 (let ((default-dir default-directory
))
680 (with-current-buffer (get-buffer-create "*grep*")
681 (let ((inhibit-read-only t
)
682 (default-directory default-dir
))
685 (let ((files (eshell-stringify-list
686 (eshell-flatten-list (cdr args
))))
687 (inhibit-redisplay t
)
690 (if (get-buffer "*Occur*")
691 (kill-buffer (get-buffer "*Occur*")))
694 (with-current-buffer (find-file-noselect (car files
))
698 (if (get-buffer "*Occur*")
699 (with-current-buffer (get-buffer "*Occur*")
700 (setq string
(buffer-string))
701 (kill-buffer (current-buffer)))))
702 (if string
(insert string
))
704 files
(cdr files
)))))
705 (local-set-key [mouse-2
] 'eshell-occur-mode-mouse-goto
)
706 (local-set-key [(control ?c
) (control ?c
)]
707 'eshell-occur-mode-goto-occurrence
)
708 (local-set-key [(control ?m
)]
709 'eshell-occur-mode-goto-occurrence
)
710 (local-set-key [return] 'eshell-occur-mode-goto-occurrence)
711 (pop-to-buffer (current-buffer) t)
712 (goto-char (point-min))
713 (resize-temp-buffer-window))))))
715 (defun eshell-grep (command args &optional maybe-use-occur)
716 "Generic service function for the various grep aliases.
717 It calls Emacs's grep utility if the command is not redirecting output,
718 and if it's not part of a command pipeline. Otherwise, it calls the
720 (if (and maybe-use-occur eshell-no-grep-available)
721 (eshell-poor-mans-grep args)
722 (if (or eshell-plain-grep-behavior
723 (not (and (eshell-interactive-output-p)
724 (not eshell-in-pipeline-p)
725 (not eshell-in-subcommand-p))))
726 (throw 'eshell-replace-command
727 (eshell-parse-command (concat "*" command)
728 (eshell-stringify-list
729 (eshell-flatten-list args))))
730 (let* ((args (mapconcat 'identity
731 (mapcar 'shell-quote-argument
732 (eshell-stringify-list
733 (eshell-flatten-list args)))
736 (set-text-properties 0 (length args)
738 (format "%s -n %s" command args)))
739 compilation-scroll-output)
742 (defun eshell/grep (&rest args)
743 "Use Emacs grep facility instead of calling external grep."
744 (eshell-grep "grep" args t))
746 (defun eshell/egrep (&rest args)
747 "Use Emacs grep facility instead of calling external egrep."
748 (eshell-grep "egrep" args t))
750 (defun eshell/fgrep (&rest args)
751 "Use Emacs grep facility instead of calling external fgrep."
752 (eshell-grep "fgrep" args t))
754 (defun eshell/agrep (&rest args)
755 "Use Emacs grep facility instead of calling external agrep."
756 (eshell-grep "agrep" args))
758 (defun eshell/glimpse (&rest args)
759 "Use Emacs grep facility instead of calling external glimpse."
761 (eshell-grep "glimpse" (append '("-z" "-y") args))))
763 ;; completions rules for some common UNIX commands
765 (defsubst eshell-complete-hostname ()
766 "Complete a command that wants a hostname for an argument."
767 (pcomplete-here (eshell-read-host-names)))
769 (defun eshell-complete-host-reference ()
770 "If there is a host reference, complete it."
771 (let ((arg (pcomplete-actual-arg))
773 (when (setq index (string-match "@[a-z.]*\\'" arg))
774 (setq pcomplete-stub (substring arg (1+ index))
775 pcomplete-last-completion-raw t)
776 (throw 'pcomplete-completions (eshell-read-host-names)))))
778 (defalias 'pcomplete/ftp 'eshell-complete-hostname)
779 (defalias 'pcomplete/ncftp 'eshell-complete-hostname)
780 (defalias 'pcomplete/ping 'eshell-complete-hostname)
781 (defalias 'pcomplete/rlogin 'eshell-complete-hostname)
783 (defun pcomplete/telnet ()
784 (require 'pcmpl-unix)
785 (pcomplete-opt "xl(pcmpl-unix-user-names)")
786 (eshell-complete-hostname))
788 (defun pcomplete/rsh ()
789 "Complete `rsh', which, after the user and hostname, is like xargs."
790 (require 'pcmpl-unix)
791 (pcomplete-opt "l(pcmpl-unix-user-names)")
792 (eshell-complete-hostname)
793 (pcomplete-here (funcall pcomplete-command-completion-function))
794 (funcall (or (pcomplete-find-completion-function (pcomplete-arg 1))
795 pcomplete-default-completion-function)))
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 (eshell-quote-argument 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 (autoload 'diff-no-select "diff")
970 (defun eshell/diff (&rest args)
971 "Alias \"diff\" to call Emacs `diff' function."
972 (let ((orig-args (eshell-stringify-list (eshell-flatten-list args))))
973 (if (or eshell-plain-diff-behavior
974 (not (and (eshell-interactive-output-p)
975 (not eshell-in-pipeline-p)
976 (not eshell-in-subcommand-p))))
977 (throw 'eshell-replace-command
978 (eshell-parse-command "*diff" orig-args))
979 (setq args (copy-sequence orig-args))
980 (if (< (length args) 2)
981 (throw 'eshell-replace-command
982 (eshell-parse-command "*diff" orig-args)))
983 (let ((old (car (last args 2)))
984 (new (car (last args)))
985 (config (current-window-configuration)))
986 (if (= (length args) 2)
988 (setcdr (last args 3) nil))
993 (nil-blank-string (eshell-flatten-and-stringify args)))
995 (throw 'eshell-replace-command
996 (eshell-parse-command "*diff" orig-args))))
997 (when (fboundp 'diff-mode)
998 (make-local-variable 'compilation-finish-functions)
1000 'compilation-finish-functions
1002 (with-current-buffer buff
1004 (set (make-local-variable 'eshell-diff-window-config)
1006 (local-set-key [?q] 'eshell-diff-quit)
1007 (if (fboundp 'turn-on-font-lock-if-enabled)
1008 (turn-on-font-lock-if-enabled))
1009 (goto-char (point-min))))))
1010 (pop-to-buffer (current-buffer))))))
1013 (put 'eshell/diff 'eshell-no-numeric-conversions t)
1015 (defun eshell/locate (&rest args)
1016 "Alias \"locate\" to call Emacs `locate' function."
1017 (if (or eshell-plain-locate-behavior
1018 (not (and (eshell-interactive-output-p)
1019 (not eshell-in-pipeline-p)
1020 (not eshell-in-subcommand-p)))
1021 (and (stringp (car args))
1022 (string-match "^-" (car args))))
1023 (throw 'eshell-replace-command
1024 (eshell-parse-command "*locate" (eshell-stringify-list
1025 (eshell-flatten-list args))))
1026 (save-selected-window
1027 (let ((locate-history-list (list (car args))))
1028 (locate-with-filter (car args) (cadr args))))))
1030 (put 'eshell/locate 'eshell-no-numeric-conversions t)
1032 (defun eshell/occur (&rest args)
1033 "Alias \"occur\" to call Emacs `occur' function."
1034 (let ((inhibit-read-only t))
1035 (if (> (length args) 2)
1036 (error "usage: occur: (REGEXP &optional NLINES)")
1037 (apply 'occur args))))
1039 (put 'eshell/occur 'eshell-no-numeric-conversions t)
1041 (defun eshell/su (&rest args)
1042 "Alias \"su\" to call Tramp."
1043 (setq args (eshell-stringify-list (eshell-flatten-list args)))
1044 (let ((orig-args (copy-tree args)))
1045 (eshell-eval-using-options
1047 '((?h "help" nil nil "show this usage screen")
1048 (?l "login" nil login "provide a login environment")
1049 (? nil nil login "provide a login environment")
1050 :usage "[- | -l | --login] [USER]
1051 Become another USER during a login session.")
1052 (throw 'eshell-replace-command
1054 (host (or (file-remote-p default-directory 'host)
1056 (dir (or (file-remote-p default-directory 'localname)
1057 (expand-file-name default-directory)))
1058 (prefix (file-remote-p 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 "~/"))
1067 "su" (file-remote-p default-directory 'method)))
1069 user (file-remote-p default-directory 'user)))))
1070 (eshell-parse-command
1071 "cd" (list (format "%s|su:%s@%s:%s"
1072 (substring prefix 0 -1) user host dir)))
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."
1080 (setq args (eshell-stringify-list (eshell-flatten-list args)))
1081 (let ((orig-args (copy-tree args)))
1082 (eshell-eval-using-options
1084 '((?h "help" nil nil "show this usage screen")
1085 (?u "user" t user "execute a command as another USER")
1087 :usage "[(-u | --user) USER] COMMAND
1088 Execute a COMMAND as the superuser or another USER.")
1089 (throw 'eshell-external
1090 (let ((user (or user "root"))
1091 (host (or (file-remote-p default-directory 'host)
1093 (dir (or (file-remote-p default-directory 'localname)
1094 (expand-file-name default-directory)))
1095 (prefix (file-remote-p 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 (let ((default-directory
1106 (file-remote-p default-directory 'method)))
1110 (file-remote-p default-directory 'user)))))
1111 (format "%s|sudo:%s@%s:%s"
1112 (substring prefix 0 -1) user host dir)
1113 (format "/sudo:%s@%s:%s" user host dir))))
1114 ;; Ensure, that Tramp has connected to that construct already.
1115 (ignore (file-exists-p default-directory))
1116 (eshell-named-command (car orig-args) (cdr orig-args))))))))
1118 (put 'eshell/sudo 'eshell-no-numeric-conversions t)
1123 ;; generated-autoload-file: "esh-groups.el"
1126 ;;; em-unix.el ends here