1 ;;; em-unix --- UNIX command aliases
3 ;; Copyright (C) 1999, 2000 Free Software Foundation
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 2, or (at your option)
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; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
26 (eval-when-compile (require 'esh-maint
))
28 (defgroup eshell-unix nil
29 "This module defines many of the more common UNIX utilities as
30 aliases implemented in Lisp. These include mv, ln, cp, rm, etc. If
31 the user passes arguments which are too complex, or are unrecognized
32 by the Lisp variant, the external version will be called (if
33 available). The only reason not to use them would be because they are
34 usually much slower. But in several cases their tight integration
35 with Eshell makes them more versatile than their traditional cousins
36 \(such as being able to use `kill' to kill Eshell background processes
38 :tag
"UNIX commands in Lisp"
39 :group
'eshell-module
)
43 ;; This file contains implementations of several UNIX command in Emacs
44 ;; Lisp, for several reasons:
46 ;; 1) it makes them available on all platforms where the Lisp
47 ;; functions used are available
49 ;; 2) it makes their functionality accessible and modified by the
52 ;; 3) it allows Eshell to refrain from having to invoke external
53 ;; processes for common operations.
55 (defcustom eshell-unix-load-hook
'(eshell-unix-initialize)
56 "*A list of functions to run when `eshell-unix' is loaded."
60 (defcustom eshell-plain-grep-behavior nil
61 "*If non-nil, standalone \"grep\" commands will behave normally.
62 Standalone in this context means not redirected, and not on the
63 receiving side of a command pipeline."
67 (defcustom eshell-no-grep-available
(not (eshell-search-path "grep"))
68 "*If non-nil, no grep is available on the current machine."
72 (defcustom eshell-plain-diff-behavior nil
73 "*If non-nil, standalone \"diff\" commands will behave normally.
74 Standalone in this context means not redirected, and not on the
75 receiving side of a command pipeline."
79 (defcustom eshell-plain-locate-behavior nil
80 "*If non-nil, standalone \"locate\" commands will behave normally.
81 Standalone in this context means not redirected, and not on the
82 receiving side of a command pipeline."
86 (defcustom eshell-rm-removes-directories nil
87 "*If non-nil, `rm' will remove directory entries.
88 Otherwise, `rmdir' is required."
92 (defcustom eshell-rm-interactive-query
(= (user-uid) 0)
93 "*If non-nil, `rm' will query before removing anything."
97 (defcustom eshell-mv-interactive-query
(= (user-uid) 0)
98 "*If non-nil, `mv' will query before overwriting anything."
102 (defcustom eshell-mv-overwrite-files t
103 "*If non-nil, `mv' will overwrite files without warning."
107 (defcustom eshell-cp-interactive-query
(= (user-uid) 0)
108 "*If non-nil, `cp' will query before overwriting anything."
112 (defcustom eshell-cp-overwrite-files t
113 "*If non-nil, `cp' will overwrite files without warning."
117 (defcustom eshell-ln-interactive-query
(= (user-uid) 0)
118 "*If non-nil, `ln' will query before overwriting anything."
122 (defcustom eshell-ln-overwrite-files nil
123 "*If non-nil, `ln' will overwrite files without warning."
127 (defcustom eshell-default-target-is-dot nil
128 "*If non-nil, the default destination for cp, mv or ln is `.'."
132 (defcustom eshell-du-prefer-over-ange nil
133 "*Use Eshell's du in ange-ftp remote directories.
134 Otherwise, Emacs will attempt to use rsh to invoke du on the remote machine."
142 (defun eshell-unix-initialize ()
143 "Initialize the UNIX support/emulation code."
144 (make-local-hook 'eshell-post-command-hook
)
145 (when (eshell-using-module 'eshell-cmpl
)
146 (make-local-hook 'pcomplete-try-first-hook
)
147 (add-hook 'pcomplete-try-first-hook
148 'eshell-complete-host-reference nil t
))
149 (make-local-variable 'eshell-complex-commands
)
150 (setq eshell-complex-commands
151 (append '("grep" "egrep" "fgrep" "agrep" "glimpse" "locate"
152 "cat" "time" "cp" "mv" "make" "du" "diff")
153 eshell-complex-commands
)))
155 (defalias 'eshell
/date
'current-time-string
)
156 (defalias 'eshell
/basename
'file-name-nondirectory
)
157 (defalias 'eshell
/dirname
'file-name-directory
)
165 (defun eshell/man
(&rest args
)
166 "Invoke man, flattening the arguments appropriately."
167 (funcall 'man
(apply 'eshell-flatten-and-stringify args
)))
169 (defun eshell-remove-entries (path files
&optional top-level
)
170 "From PATH, remove all of the given FILES, perhaps interactively."
172 (if (string-match "\\`\\.\\.?\\'"
173 (file-name-nondirectory (car files
)))
175 (eshell-error "rm: cannot remove `.' or `..'\n"))
176 (if (and (file-directory-p (car files
))
177 (not (file-symlink-p (car files
))))
178 (let ((dir (file-name-as-directory (car files
))))
179 (eshell-remove-entries dir
184 (directory-files dir
)))
186 (eshell-printn (format "rm: removing directory `%s'"
192 (format "rm: remove directory `%s'? "
194 (eshell-funcalln 'delete-directory
(car files
))))
196 (eshell-printn (format "rm: removing file `%s'"
201 (format "rm: remove `%s'? "
203 (eshell-funcalln 'delete-file
(car files
)))))
204 (setq files
(cdr files
))))
206 (defun eshell/rm
(&rest args
)
207 "Implementation of rm in Lisp.
208 This is implemented to call either `delete-file', `kill-buffer',
209 `kill-process', or `unintern', depending on the nature of the
211 (setq args
(eshell-flatten-list args
))
212 (eshell-eval-using-options
214 '((?h
"help" nil nil
"show this usage screen")
215 (?f
"force" nil force-removal
"force removal")
216 (?i
"interactive" nil interactive
"prompt before any removal")
217 (?n
"preview" nil preview
"don't change anything on disk")
218 (?r
"recursive" nil recursive
219 "remove the contents of directories recursively")
220 (?R nil nil recursive
"(same)")
221 (?v
"verbose" nil verbose
"explain what is being done")
225 :usage
"[OPTION]... FILE...
226 Remove (unlink) the FILE(s).")
228 (setq interactive eshell-rm-interactive-query
))
229 (if (and force-removal interactive
)
230 (setq interactive nil
))
232 (let ((entry (if (stringp (car args
))
233 (directory-file-name (car args
))
234 (if (numberp (car args
))
235 (number-to-string (car args
))
240 (eshell-printn (format "rm: removing buffer `%s'" entry
)))
243 (not (y-or-n-p (format "rm: delete buffer `%s'? "
245 (eshell-funcalln 'kill-buffer entry
)))
246 ((eshell-processp entry
)
248 (eshell-printn (format "rm: killing process `%s'" entry
)))
251 (not (y-or-n-p (format "rm: kill process `%s'? "
253 (eshell-funcalln 'kill-process entry
)))
256 (eshell-printn (format "rm: uninterning symbol `%s'" entry
)))
260 (not (y-or-n-p (format "rm: unintern symbol `%s'? "
262 (eshell-funcalln 'unintern entry
)))
264 (if (and (file-directory-p entry
)
265 (not (file-symlink-p entry
)))
267 eshell-rm-removes-directories
)
271 (format "rm: descend into directory `%s'? "
273 (eshell-remove-entries nil
(list entry
) t
))
274 (eshell-error (format "rm: %s: is a directory\n" entry
)))
275 (eshell-remove-entries nil
(list entry
) t
)))))
276 (setq args
(cdr args
)))
279 (defun eshell/mkdir
(&rest args
)
280 "Implementation of mkdir in Lisp."
281 (eshell-eval-using-options
283 '((?h
"help" nil nil
"show this usage screen")
286 :usage
"[OPTION] DIRECTORY...
287 Create the DIRECTORY(ies), if they do not already exist.")
289 (eshell-funcalln 'make-directory
(car args
))
290 (setq args
(cdr args
)))
293 (defun eshell/rmdir
(&rest args
)
294 "Implementation of rmdir in Lisp."
295 (eshell-eval-using-options
297 '((?h
"help" nil nil
"show this usage screen")
300 :usage
"[OPTION] DIRECTORY...
301 Remove the DIRECTORY(ies), if they are empty.")
303 (eshell-funcalln 'delete-directory
(car args
))
304 (setq args
(cdr args
)))
308 (defvar no-dereference
)
312 (defvar eshell-warn-dot-directories t
)
314 (defun eshell-shuffle-files (command action files target func deep
&rest args
)
315 "Shuffle around some filesystem entries, using FUNC to do the work."
316 (let ((attr-target (eshell-file-attributes target
))
317 (is-dir (or (file-directory-p target
)
318 (and preview
(not eshell-warn-dot-directories
))))
320 (if (and (not preview
) (not is-dir
)
321 (> (length files
) 1))
322 (error "%s: when %s multiple files, last argument must be a directory"
325 (setcar files
(directory-file-name (car files
)))
327 ((string-match "\\`\\.\\.?\\'"
328 (file-name-nondirectory (car files
)))
329 (if eshell-warn-dot-directories
330 (eshell-error (format "%s: %s: omitting directory\n"
331 command
(car files
)))))
333 (or (not (eshell-under-windows-p))
334 (eq system-type
'ms-dos
))
335 (setq attr
(eshell-file-attributes (car files
)))
336 (nth 10 attr-target
) (nth 10 attr
)
337 (= (nth 10 attr-target
) (nth 10 attr
))
338 (nth 11 attr-target
) (nth 11 attr
)
339 (= (nth 11 attr-target
) (nth 11 attr
)))
340 (eshell-error (format "%s: `%s' and `%s' are the same file\n"
341 command
(car files
) target
)))
343 (let ((source (car files
))
346 (file-name-nondirectory (car files
)) target
)
349 (if (and (file-directory-p source
)
350 (or (not no-dereference
)
351 (not (file-symlink-p source
)))
352 (not (memq func
'(make-symbolic-link
354 (if (and (eq func
'copy-file
)
356 (eshell-error (format "%s: %s: omitting directory\n"
357 command
(car files
)))
358 (let (eshell-warn-dot-directories)
360 (eq func
'rename-file
)
361 (= (nth 11 (eshell-file-attributes
363 (expand-file-name source
))))
364 (nth 11 (eshell-file-attributes
366 (expand-file-name target
))))))
367 (apply 'eshell-funcalln func source target args
)
368 (unless (file-directory-p target
)
371 (format "%s: making directory %s"
374 (eshell-funcalln 'make-directory target
)))
375 (apply 'eshell-shuffle-files
380 (concat source
"/" file
)))
381 (directory-files source
))
383 (when (eq func
'rename-file
)
386 (format "%s: deleting directory %s"
389 (eshell-funcalln 'delete-directory source
))))))
391 (eshell-printn (format "%s: %s -> %s" command
394 (if (and no-dereference
395 (setq link
(file-symlink-p source
)))
397 (apply 'eshell-funcalln
'make-symbolic-link
399 (if (eq func
'rename-file
)
400 (if (and (file-directory-p source
)
401 (not (file-symlink-p source
)))
402 (eshell-funcalln 'delete-directory source
)
403 (eshell-funcalln 'delete-file source
))))
404 (apply 'eshell-funcalln func source target args
)))))))
405 (setq files
(cdr files
)))))
407 (defun eshell-shorthand-tar-command (command args
)
408 "Rewrite `cp -v dir a.tar.gz' to `tar cvzf a.tar.gz dir'."
409 (let* ((archive (car (last args
)))
411 (cond ((string-match "z2" archive
) "If")
412 ((string-match "gz" archive
) "zf")
413 ((string-match "\\(az\\|Z\\)" archive
) "Zf")
415 (if (file-exists-p archive
)
416 (setq tar-args
(concat "u" tar-args
))
417 (setq tar-args
(concat "c" tar-args
)))
419 (setq tar-args
(concat "v" tar-args
)))
420 (if (equal command
"mv")
421 (setq tar-args
(concat "--remove-files -" tar-args
)))
422 ;; truncate the archive name from the arguments
423 (setcdr (last args
2) nil
)
424 (throw 'eshell-replace-command
425 (eshell-parse-command
426 (format "tar %s %s" tar-args archive
) args
))))
428 ;; this is to avoid duplicating code...
429 (defmacro eshell-mvcpln-template
(command action func query-var
430 force-var
&optional preserve
)
431 `(let ((len (length args
)))
433 (and (= len
1) (null eshell-default-target-is-dot
)))
434 (error "%s: missing destination file or directory" ,command
))
437 (setq args
(eshell-stringify-list (eshell-flatten-list args
)))
438 (if (and ,(not (equal command
"ln"))
439 (string-match eshell-tar-regexp
(car (last args
)))
440 (or (> (length args
) 2)
441 (and (file-directory-p (car args
))
442 (or (not no-dereference
)
443 (not (file-symlink-p (car args
)))))))
444 (eshell-shorthand-tar-command ,command args
)
445 (let ((target (car (last args
)))
447 (setcdr (last args
2) nil
)
448 (eshell-shuffle-files
449 ,command
,action args target
,func nil
451 `((if (and (or interactive
454 1 (or force
,force-var
)))
459 (defun eshell/mv
(&rest args
)
460 "Implementation of mv in Lisp."
461 (eshell-eval-using-options
463 '((?f
"force" nil force
464 "remove existing destinations, never prompt")
465 (?i
"interactive" nil interactive
466 "request confirmation if target already exists")
467 (?n
"preview" nil preview
468 "don't change anything on disk")
469 (?v
"verbose" nil verbose
470 "explain what is being done")
471 (nil "help" nil nil
"show this usage screen")
475 :usage
"[OPTION]... SOURCE DEST
476 or: mv [OPTION]... SOURCE... DIRECTORY
477 Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.
478 \[OPTION] DIRECTORY...")
479 (let ((no-dereference t
))
480 (eshell-mvcpln-template "mv" "moving" 'rename-file
481 eshell-mv-interactive-query
482 eshell-mv-overwrite-files
))))
484 (defun eshell/cp
(&rest args
)
485 "Implementation of cp in Lisp."
486 (eshell-eval-using-options
488 '((?a
"archive" nil archive
490 (?d
"no-dereference" nil no-dereference
492 (?f
"force" nil force
493 "remove existing destinations, never prompt")
494 (?i
"interactive" nil interactive
495 "request confirmation if target already exists")
496 (?n
"preview" nil preview
497 "don't change anything on disk")
498 (?p
"preserve" nil preserve
499 "preserve file attributes if possible")
500 (?R
"recursive" nil recursive
501 "copy directories recursively")
502 (?v
"verbose" nil verbose
503 "explain what is being done")
504 (nil "help" nil nil
"show this usage screen")
508 :usage
"[OPTION]... SOURCE DEST
509 or: cp [OPTION]... SOURCE... DIRECTORY
510 Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.")
512 (setq preserve t no-dereference t recursive t
))
513 (eshell-mvcpln-template "cp" "copying" 'copy-file
514 eshell-cp-interactive-query
515 eshell-cp-overwrite-files preserve
)))
517 (defun eshell/ln
(&rest args
)
518 "Implementation of ln in Lisp."
519 (eshell-eval-using-options
521 '((?h
"help" nil nil
"show this usage screen")
522 (?s
"symbolic" nil symbolic
523 "make symbolic links instead of hard links")
524 (?i
"interactive" nil interactive
525 "request confirmation if target already exists")
526 (?f
"force" nil force
"remove existing destinations, never prompt")
527 (?n
"preview" nil preview
528 "don't change anything on disk")
529 (?v
"verbose" nil verbose
"explain what is being done")
533 :usage
"[OPTION]... TARGET [LINK_NAME]
534 or: ln [OPTION]... TARGET... DIRECTORY
535 Create a link to the specified TARGET with optional LINK_NAME. If there is
536 more than one TARGET, the last argument must be a directory; create links
537 in DIRECTORY to each TARGET. Create hard links by default, symbolic links
538 with '--symbolic'. When creating hard links, each TARGET must exist.")
539 (let ((no-dereference t
))
540 (eshell-mvcpln-template "ln" "linking"
544 eshell-ln-interactive-query
545 eshell-ln-overwrite-files
))))
547 (defun eshell/cat
(&rest args
)
548 "Implementation of cat in Lisp.
549 If in a pipeline, or the file is not a regular file, directory or
550 symlink, then revert to the system's definition of cat."
551 (setq args
(eshell-stringify-list (eshell-flatten-list args
)))
552 (if (or eshell-in-pipeline-p
555 (unless (let ((attrs (eshell-file-attributes arg
)))
556 (and attrs
(memq (aref (nth 8 attrs
) 0)
558 (throw 'special t
)))))
559 (let ((ext-cat (eshell-search-path "cat")))
561 (throw 'eshell-replace-command
562 (eshell-parse-command ext-cat args
))
563 (if eshell-in-pipeline-p
564 (error "Eshell's `cat' does not work in pipelines")
565 (error "Eshell's `cat' cannot display one of the files given"))))
566 (eshell-init-print-buffer)
567 (eshell-eval-using-options
569 '((?h
"help" nil nil
"show this usage screen")
572 :usage
"[OPTION] FILE...
573 Concatenate FILE(s), or standard input, to standard output.")
574 (eshell-for file args
575 (if (string= file
"-")
576 (throw 'eshell-external
577 (eshell-external-command "cat" args
))))
578 (let ((curbuf (current-buffer)))
579 (eshell-for file args
581 (insert-file-contents file
)
582 (goto-char (point-min))
584 (let ((str (buffer-substring
585 (point) (min (1+ (line-end-position))
587 (with-current-buffer curbuf
588 (eshell-buffered-print str
)))
591 ;; if the file does not end in a newline, do not emit one
592 (setq eshell-ensure-newline-p nil
))))
594 ;; special front-end functions for compilation-mode buffers
596 (defun eshell/make
(&rest args
)
597 "Use `compile' to do background makes."
598 (if (and eshell-current-subjob-p
599 (eshell-interactive-output-p))
600 (let ((compilation-process-setup-function
602 (list 'setq
'process-environment
603 (list 'quote
(eshell-copy-environment))))))
604 (compile (concat "make " (eshell-flatten-and-stringify args
))))
605 (throw 'eshell-replace-command
606 (eshell-parse-command "*make" (eshell-stringify-list
607 (eshell-flatten-list args
))))))
609 (defun eshell-occur-mode-goto-occurrence ()
610 "Go to the occurrence the current line describes."
612 (let ((pos (occur-mode-find-occurrence)))
613 (pop-to-buffer (marker-buffer pos
))
614 (goto-char (marker-position pos
))))
616 (defun eshell-occur-mode-mouse-goto (event)
617 "In Occur mode, go to the occurrence whose line you click on."
621 (set-buffer (window-buffer (posn-window (event-end event
))))
623 (goto-char (posn-point (event-end event
)))
624 (setq pos
(occur-mode-find-occurrence))
625 (setq buffer occur-buffer
)))
626 (pop-to-buffer (marker-buffer pos
))
627 (goto-char (marker-position pos
))))
629 (defun eshell-poor-mans-grep (args)
630 "A poor version of grep that opens every file and uses `occur'.
631 This eats up memory, since it leaves the buffers open (to speed future
632 searches), and it's very slow. But, if your system has no grep
634 (save-selected-window
635 (let ((default-dir default-directory
))
636 (with-current-buffer (get-buffer-create "*grep*")
637 (let ((inhibit-read-only t
)
638 (default-directory default-dir
))
641 (let ((files (eshell-stringify-list
642 (eshell-flatten-list (cdr args
))))
643 (inhibit-redisplay t
)
646 (if (get-buffer "*Occur*")
647 (kill-buffer (get-buffer "*Occur*")))
650 (with-current-buffer (find-file-noselect (car files
))
654 (if (get-buffer "*Occur*")
655 (with-current-buffer (get-buffer "*Occur*")
656 (setq string
(buffer-string))
657 (kill-buffer (current-buffer)))))
658 (if string
(insert string
))
660 files
(cdr files
)))))
661 (setq occur-buffer
(current-buffer))
662 (local-set-key [mouse-2
] 'eshell-occur-mode-mouse-goto
)
663 (local-set-key [(control ?c
) (control ?c
)]
664 'eshell-occur-mode-goto-occurrence
)
665 (local-set-key [(control ?m
)]
666 'eshell-occur-mode-goto-occurrence
)
667 (local-set-key [return] 'eshell-occur-mode-goto-occurrence)
668 (pop-to-buffer (current-buffer) t)
669 (goto-char (point-min))
670 (resize-temp-buffer-window))))))
672 (defun eshell-grep (command args &optional maybe-use-occur)
673 "Generic service function for the various grep aliases.
674 It calls Emacs' grep utility if the command is not redirecting output,
675 and if it's not part of a command pipeline. Otherwise, it calls the
677 (if (and maybe-use-occur eshell-no-grep-available)
678 (eshell-poor-mans-grep args)
679 (if (or eshell-plain-grep-behavior
680 (not (and (eshell-interactive-output-p)
681 (not eshell-in-pipeline-p)
682 (not eshell-in-subcommand-p))))
683 (throw 'eshell-replace-command
684 (eshell-parse-command (concat "*" command)
685 (eshell-stringify-list
686 (eshell-flatten-list args))))
687 (let* ((compilation-process-setup-function
689 (list 'setq 'process-environment
690 (list 'quote (eshell-copy-environment)))))
691 (args (mapconcat 'identity
692 (mapcar 'shell-quote-argument
693 (eshell-stringify-list
694 (eshell-flatten-list args)))
697 (set-text-properties 0 (length args)
699 (format "%s -n %s" command args)))
700 compilation-scroll-output)
703 (defun eshell/grep (&rest args)
704 "Use Emacs grep facility instead of calling external grep."
705 (eshell-grep "grep" args t))
707 (defun eshell/egrep (&rest args)
708 "Use Emacs grep facility instead of calling external egrep."
709 (eshell-grep "egrep" args t))
711 (defun eshell/fgrep (&rest args)
712 "Use Emacs grep facility instead of calling external fgrep."
713 (eshell-grep "fgrep" args t))
715 (defun eshell/agrep (&rest args)
716 "Use Emacs grep facility instead of calling external agrep."
717 (eshell-grep "agrep" args))
719 (defun eshell/glimpse (&rest args)
720 "Use Emacs grep facility instead of calling external glimpse."
722 (eshell-grep "glimpse" (append '("-z" "-y") args))))
724 ;; completions rules for some common UNIX commands
726 (defsubst eshell-complete-hostname ()
727 "Complete a command that wants a hostname for an argument."
728 (pcomplete-here (eshell-read-host-names)))
730 (defun eshell-complete-host-reference ()
731 "If there is a host reference, complete it."
732 (let ((arg (pcomplete-actual-arg))
734 (when (setq index (string-match "@[a-z.]*\\'" arg))
735 (setq pcomplete-stub (substring arg (1+ index))
736 pcomplete-last-completion-raw t)
737 (throw 'pcomplete-completions (eshell-read-host-names)))))
739 (defalias 'pcomplete/ftp 'eshell-complete-hostname)
740 (defalias 'pcomplete/ncftp 'eshell-complete-hostname)
741 (defalias 'pcomplete/ping 'eshell-complete-hostname)
742 (defalias 'pcomplete/rlogin 'eshell-complete-hostname)
744 (defun pcomplete/telnet ()
745 (require 'pcmpl-unix)
746 (pcomplete-opt "xl(pcmpl-unix-user-names)")
747 (eshell-complete-hostname))
749 (defun pcomplete/rsh ()
750 "Complete `rsh', which, after the user and hostname, is like xargs."
751 (require 'pcmpl-unix)
752 (pcomplete-opt "l(pcmpl-unix-user-names)")
753 (eshell-complete-hostname)
754 (pcomplete-here (funcall pcomplete-command-completion-function))
755 (funcall (or (pcomplete-find-completion-function (pcomplete-arg 1))
756 pcomplete-default-completion-function)))
758 (defalias 'pcomplete/ssh 'pcomplete/rsh)
763 (defvar dereference-links)
765 (defvar human-readable)
767 (defvar only-one-filesystem)
770 (defsubst eshell-du-size-string (size)
771 (let* ((str (eshell-printable-size size human-readable block-size t))
773 (concat str (if (< len 8)
774 (make-string (- 8 len) ? )))))
776 (defun eshell-du-sum-directory (path depth)
777 "Summarize PATH, and its member directories."
778 (let ((entries (eshell-directory-files-and-attributes path))
781 (unless (string-match "\\`\\.\\.?\\'" (caar entries))
782 (let* ((entry (concat path (char-to-string directory-sep-char)
784 (symlink (and (stringp (cadr (car entries)))
785 (cadr (car entries)))))
786 (unless (or (and symlink (not dereference-links))
787 (and only-one-filesystem
788 (/= only-one-filesystem
789 (nth 12 (car entries)))))
791 (setq entry symlink))
794 (if (eq t (cadr (car entries)))
795 (eshell-du-sum-directory entry (1+ depth))
796 (let ((file-size (nth 8 (car entries))))
801 (concat (eshell-du-size-string file-size)
802 entry "\n")))))))))))
803 (setq entries (cdr entries)))
804 (if (or (not max-depth)
807 (eshell-print (concat (eshell-du-size-string size)
808 (directory-file-name path) "\n")))
811 (defun eshell/du (&rest args)
812 "Implementation of \"du\" in Lisp, passing ARGS."
814 (eshell-stringify-list (eshell-flatten-list args))
816 (let ((ext-du (eshell-search-path "du")))
818 (not (catch 'have-ange-path
820 (if (eq (find-file-name-handler (expand-file-name arg)
822 'ange-ftp-hook-function)
823 (throw 'have-ange-path t))))))
824 (throw 'eshell-replace-command
825 (eshell-parse-command ext-du args))
826 (eshell-eval-using-options
828 '((?a "all" nil show-all
829 "write counts for all files, not just directories")
830 (nil "block-size" t block-size
831 "use SIZE-byte blocks (i.e., --block-size SIZE)")
832 (?b "bytes" nil by-bytes
833 "print size in bytes")
834 (?c "total" nil grand-total
835 "produce a grand total")
836 (?d "max-depth" t max-depth
837 "display data only this many levels of data")
838 (?h "human-readable" 1024 human-readable
839 "print sizes in human readable format")
840 (?H "is" 1000 human-readable
841 "likewise, but use powers of 1000 not 1024")
842 (?k "kilobytes" 1024 block-size
843 "like --block-size 1024")
844 (?L "dereference" nil dereference-links
845 "dereference all symbolic links")
846 (?m "megabytes" 1048576 block-size
847 "like --block-size 1048576")
848 (?s "summarize" 0 max-depth
849 "display only a total for each argument")
850 (?x "one-file-system" nil only-one-filesystem
851 "skip directories on different filesystems")
853 "show this usage screen")
855 :usage "[OPTION]... FILE...
856 Summarize disk usage of each FILE, recursively for directories.")
858 (setq block-size (or block-size 1024)))
859 (if (and max-depth (stringp max-depth))
860 (setq max-depth (string-to-int max-depth)))
861 ;; filesystem support means nothing under Windows
862 (if (eshell-under-windows-p)
863 (setq only-one-filesystem nil))
864 (let ((size 0.0) ange-cache)
866 (if only-one-filesystem
867 (setq only-one-filesystem
868 (nth 11 (eshell-file-attributes
869 (file-name-as-directory (car args))))))
870 (setq size (+ size (eshell-du-sum-directory
871 (directory-file-name (car args)) 0)))
872 (setq args (cdr args)))
874 (eshell-print (concat (eshell-du-size-string size)
877 (defvar eshell-time-start nil)
879 (defun eshell-show-elapsed-time ()
880 (let ((elapsed (format "%.3f secs\n"
881 (- (eshell-time-to-seconds (current-time))
882 eshell-time-start))))
883 (set-text-properties 0 (length elapsed) '(face bold) elapsed)
884 (eshell-interactive-print elapsed))
885 (remove-hook 'eshell-post-command-hook 'eshell-show-elapsed-time t))
887 (defun eshell/time (&rest args)
888 "Implementation of \"time\" in Lisp."
889 (let ((time-args (copy-alist args))
892 (while (and continue args)
893 (if (not (string-match "^-" (car args)))
896 (setcdr last-arg nil)
901 (eshell-eval-using-options
903 '((?h "help" nil nil "show this usage screen")
907 Show wall-clock time elapsed during execution of COMMAND.")
908 (setq eshell-time-start (eshell-time-to-seconds (current-time)))
909 (add-hook 'eshell-post-command-hook 'eshell-show-elapsed-time nil t)
911 (throw 'eshell-replace-command
912 (eshell-parse-command (car time-args) (cdr time-args))))))
914 (defalias 'eshell/whoami 'user-login-name)
916 (defvar eshell-diff-window-config nil)
918 (defun eshell-diff-quit ()
919 "Restore the window configuration previous to diff'ing."
921 (if eshell-diff-window-config
922 (set-window-configuration eshell-diff-window-config)))
924 (defun eshell/diff (&rest args)
925 "Alias \"diff\" to call Emacs `diff' function."
926 (let ((orig-args (eshell-stringify-list (eshell-flatten-list args))))
927 (if (or eshell-plain-diff-behavior
928 (not (and (eshell-interactive-output-p)
929 (not eshell-in-pipeline-p)
930 (not eshell-in-subcommand-p))))
931 (throw 'eshell-replace-command
932 (eshell-parse-command "*diff" orig-args))
933 (setq args (eshell-copy-list orig-args))
934 (if (< (length args) 2)
935 (throw 'eshell-replace-command
936 (eshell-parse-command "*diff" orig-args)))
937 (let ((old (car (last args 2)))
938 (new (car (last args)))
939 (config (current-window-configuration)))
940 (if (= (length args) 2)
942 (setcdr (last args 3) nil))
945 (diff old new (eshell-flatten-and-stringify args))
947 (throw 'eshell-replace-command
948 (eshell-parse-command "*diff" orig-args))))
949 (when (fboundp 'diff-mode)
951 (set (make-local-variable 'eshell-diff-window-config) config)
952 (local-set-key [?q] 'eshell-diff-quit)
953 (if (fboundp 'turn-on-font-lock-if-enabled)
954 (turn-on-font-lock-if-enabled))))
956 (goto-char (point-min))
959 (defun eshell/locate (&rest args)
960 "Alias \"locate\" to call Emacs `locate' function."
961 (if (or eshell-plain-locate-behavior
962 (not (and (eshell-interactive-output-p)
963 (not eshell-in-pipeline-p)
964 (not eshell-in-subcommand-p)))
965 (and (stringp (car args))
966 (string-match "^-" (car args))))
967 (throw 'eshell-replace-command
968 (eshell-parse-command "*locate" (eshell-stringify-list
969 (eshell-flatten-list args))))
970 (save-selected-window
971 (let ((locate-history-list (list (car args))))
972 (locate-with-filter (car args) (cadr args))))))
974 (defun eshell/occur (&rest args)
975 "Alias \"occur\" to call Emacs `occur' function."
976 (let ((inhibit-read-only t))
978 (error "usage: occur: (REGEXP)")
979 (occur (car args)))))
983 ;;; em-unix.el ends here