Merge from emacs-24; up to 2013-01-03T02:31:36Z!rgm@gnu.org
[emacs.git] / lisp / eshell / em-unix.el
blob5792fe175063c14c2c0651cb3b1f3de368ff9c03
1 ;;; em-unix.el --- UNIX command aliases -*- lexical-binding:t -*-
3 ;; Copyright (C) 1999-2013 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/>.
22 ;;; Commentary:
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
31 ;; Lisp programmer.
33 ;; 3) it allows Eshell to refrain from having to invoke external
34 ;; processes for common operations.
36 ;;; Code:
38 (require 'eshell)
39 (require 'esh-opt)
40 (require 'pcomplete)
42 ;;;###autoload
43 (progn
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
53 by name)."
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
60 :type 'hook
61 :group 'eshell-unix)
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."
67 :type 'boolean
68 :group 'eshell-unix)
70 (defcustom eshell-no-grep-available (not (eshell-search-path "grep"))
71 "If non-nil, no grep is available on the current machine."
72 :type 'boolean
73 :group 'eshell-unix)
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."
79 :type 'boolean
80 :group 'eshell-unix)
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."
86 :type 'boolean
87 :group 'eshell-unix)
89 (defcustom eshell-rm-removes-directories nil
90 "If non-nil, `rm' will remove directory entries.
91 Otherwise, `rmdir' is required."
92 :type 'boolean
93 :group 'eshell-unix)
95 (defcustom eshell-rm-interactive-query (= (user-uid) 0)
96 "If non-nil, `rm' will query before removing anything."
97 :type 'boolean
98 :group 'eshell-unix)
100 (defcustom eshell-mv-interactive-query (= (user-uid) 0)
101 "If non-nil, `mv' will query before overwriting anything."
102 :type 'boolean
103 :group 'eshell-unix)
105 (defcustom eshell-mv-overwrite-files t
106 "If non-nil, `mv' will overwrite files without warning."
107 :type 'boolean
108 :group 'eshell-unix)
110 (defcustom eshell-cp-interactive-query (= (user-uid) 0)
111 "If non-nil, `cp' will query before overwriting anything."
112 :type 'boolean
113 :group 'eshell-unix)
115 (defcustom eshell-cp-overwrite-files t
116 "If non-nil, `cp' will overwrite files without warning."
117 :type 'boolean
118 :group 'eshell-unix)
120 (defcustom eshell-ln-interactive-query (= (user-uid) 0)
121 "If non-nil, `ln' will query before overwriting anything."
122 :type 'boolean
123 :group 'eshell-unix)
125 (defcustom eshell-ln-overwrite-files nil
126 "If non-nil, `ln' will overwrite files without warning."
127 :type 'boolean
128 :group 'eshell-unix)
130 (defcustom eshell-default-target-is-dot nil
131 "If non-nil, the default destination for cp, mv or ln is `.'."
132 :type 'boolean
133 :group 'eshell-unix)
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."
138 :type 'boolean
139 :group 'eshell-unix)
141 ;;; Functions:
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)
159 (defvar em-preview)
160 (defvar em-recursive)
161 (defvar em-verbose)
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
175 etc."
176 (eval-and-compile (require 'info))
177 (let ((file (cond
178 ((not (stringp (car args)))
179 nil)
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
187 (if file
188 (progn
189 (setq args (cdr args))
190 (Info-find-node file "Top"))
191 (Info-directory))
193 ;; Treat all remaining args as menu references
194 (while args
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."
200 (while files
201 (if (string-match "\\`\\.\\.?\\'"
202 (file-name-nondirectory (car files)))
203 (if top-level
204 (eshell-error "rm: cannot remove `.' or `..'\n"))
205 (if (and (file-directory-p (car files))
206 (not (file-symlink-p (car files))))
207 (progn
208 (if em-verbose
209 (eshell-printn (format "rm: removing directory `%s'"
210 (car files))))
211 (unless
212 (or em-preview
213 (and em-interactive
214 (not (y-or-n-p
215 (format "rm: remove directory `%s'? "
216 (car files))))))
217 (eshell-funcalln 'delete-directory (car files) t t)))
218 (if em-verbose
219 (eshell-printn (format "rm: removing file `%s'"
220 (car files))))
221 (unless (or em-preview
222 (and em-interactive
223 (not (y-or-n-p
224 (format "rm: remove `%s'? "
225 (car files))))))
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
233 argument."
234 (setq args (eshell-flatten-list args))
235 (eshell-eval-using-options
236 "rm" args
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")
245 :preserve-args
246 :external "rm"
247 :show-usage
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))
254 (while args
255 (let ((entry (if (stringp (car args))
256 (directory-file-name (car args))
257 (if (numberp (car args))
258 (number-to-string (car args))
259 (car args)))))
260 (cond
261 ((bufferp entry)
262 (if em-verbose
263 (eshell-printn (format "rm: removing buffer `%s'" entry)))
264 (unless (or em-preview
265 (and em-interactive
266 (not (y-or-n-p (format "rm: delete buffer `%s'? "
267 entry)))))
268 (eshell-funcalln 'kill-buffer entry)))
269 ((eshell-processp entry)
270 (if em-verbose
271 (eshell-printn (format "rm: killing process `%s'" entry)))
272 (unless (or em-preview
273 (and em-interactive
274 (not (y-or-n-p (format "rm: kill process `%s'? "
275 entry)))))
276 (eshell-funcalln 'kill-process entry)))
277 ((symbolp entry)
278 (if em-verbose
279 (eshell-printn (format "rm: uninterning symbol `%s'" entry)))
280 (unless
281 (or em-preview
282 (and em-interactive
283 (not (y-or-n-p (format "rm: unintern symbol `%s'? "
284 entry)))))
285 (eshell-funcalln 'unintern entry)))
286 ((stringp entry)
287 ;; -f should silently ignore missing files (bug#15373).
288 (unless (and force-removal
289 (not (file-exists-p entry)))
290 (if (and (file-directory-p entry)
291 (not (file-symlink-p entry)))
292 (if (or em-recursive
293 eshell-rm-removes-directories)
294 (if (or em-preview
295 (not em-interactive)
296 (y-or-n-p
297 (format "rm: descend into directory `%s'? "
298 entry)))
299 (eshell-remove-entries nil (list entry) t))
300 (eshell-error (format "rm: %s: is a directory\n" entry)))
301 (eshell-remove-entries nil (list entry) t))))))
302 (setq args (cdr args)))
303 nil))
305 (put 'eshell/rm 'eshell-no-numeric-conversions t)
307 (defun eshell/mkdir (&rest args)
308 "Implementation of mkdir in Lisp."
309 (eshell-eval-using-options
310 "mkdir" args
311 '((?h "help" nil nil "show this usage screen")
312 (?p "parents" nil em-parents "make parent directories as needed")
313 :external "mkdir"
314 :show-usage
315 :usage "[OPTION] DIRECTORY...
316 Create the DIRECTORY(ies), if they do not already exist.")
317 (while args
318 (eshell-funcalln 'make-directory (car args) em-parents)
319 (setq args (cdr args)))
320 nil))
322 (put 'eshell/mkdir 'eshell-no-numeric-conversions t)
324 (defun eshell/rmdir (&rest args)
325 "Implementation of rmdir in Lisp."
326 (eshell-eval-using-options
327 "rmdir" args
328 '((?h "help" nil nil "show this usage screen")
329 :external "rmdir"
330 :show-usage
331 :usage "[OPTION] DIRECTORY...
332 Remove the DIRECTORY(ies), if they are empty.")
333 (while args
334 (eshell-funcalln 'delete-directory (car args))
335 (setq args (cdr args)))
336 nil))
338 (put 'eshell/rmdir 'eshell-no-numeric-conversions t)
340 (defvar no-dereference)
342 (defvar eshell-warn-dot-directories t)
344 (defun eshell-shuffle-files (command action files target func deep &rest args)
345 "Shuffle around some filesystem entries, using FUNC to do the work."
346 (let ((attr-target (eshell-file-attributes target))
347 (is-dir (or (file-directory-p target)
348 (and em-preview (not eshell-warn-dot-directories))))
349 attr)
350 (if (and (not em-preview) (not is-dir)
351 (> (length files) 1))
352 (error "%s: when %s multiple files, last argument must be a directory"
353 command action))
354 (while files
355 (setcar files (directory-file-name (car files)))
356 (cond
357 ((string-match "\\`\\.\\.?\\'"
358 (file-name-nondirectory (car files)))
359 (if eshell-warn-dot-directories
360 (eshell-error (format "%s: %s: omitting directory\n"
361 command (car files)))))
362 ((and attr-target
363 (or (not (eshell-under-windows-p))
364 (eq system-type 'ms-dos))
365 (setq attr (eshell-file-attributes (car files)))
366 (nth 10 attr-target) (nth 10 attr)
367 ;; Use equal, not -, since the inode and the device could
368 ;; cons cells.
369 (equal (nth 10 attr-target) (nth 10 attr))
370 (nth 11 attr-target) (nth 11 attr)
371 (equal (nth 11 attr-target) (nth 11 attr)))
372 (eshell-error (format "%s: `%s' and `%s' are the same file\n"
373 command (car files) target)))
375 (let ((source (car files))
376 (target (if is-dir
377 (expand-file-name
378 (file-name-nondirectory (car files)) target)
379 target))
380 link)
381 (if (and (file-directory-p source)
382 (or (not no-dereference)
383 (not (file-symlink-p source)))
384 (not (memq func '(make-symbolic-link
385 add-name-to-file))))
386 (if (and (eq func 'copy-file)
387 (not em-recursive))
388 (eshell-error (format "%s: %s: omitting directory\n"
389 command (car files)))
390 (let (eshell-warn-dot-directories)
391 (if (and (not deep)
392 (eq func 'rename-file)
393 ;; Use equal, since the device might be a
394 ;; cons cell.
395 (equal (nth 11 (eshell-file-attributes
396 (file-name-directory
397 (directory-file-name
398 (expand-file-name source)))))
399 (nth 11 (eshell-file-attributes
400 (file-name-directory
401 (directory-file-name
402 (expand-file-name target)))))))
403 (apply 'eshell-funcalln func source target args)
404 (unless (file-directory-p target)
405 (if em-verbose
406 (eshell-printn
407 (format "%s: making directory %s"
408 command target)))
409 (unless em-preview
410 (eshell-funcalln 'make-directory target)))
411 (apply 'eshell-shuffle-files
412 command action
413 (mapcar
414 (function
415 (lambda (file)
416 (concat source "/" file)))
417 (directory-files source))
418 target func t args)
419 (when (eq func 'rename-file)
420 (if em-verbose
421 (eshell-printn
422 (format "%s: deleting directory %s"
423 command source)))
424 (unless em-preview
425 (eshell-funcalln 'delete-directory source))))))
426 (if em-verbose
427 (eshell-printn (format "%s: %s -> %s" command
428 source target)))
429 (unless em-preview
430 (if (and no-dereference
431 (setq link (file-symlink-p source)))
432 (progn
433 (apply 'eshell-funcalln 'make-symbolic-link
434 link target args)
435 (if (eq func 'rename-file)
436 (if (and (file-directory-p source)
437 (not (file-symlink-p source)))
438 (eshell-funcalln 'delete-directory source)
439 (eshell-funcalln 'delete-file source))))
440 (apply 'eshell-funcalln func source target args)))))))
441 (setq files (cdr files)))))
443 (defun eshell-shorthand-tar-command (command args)
444 "Rewrite `cp -v dir a.tar.gz' to `tar cvzf a.tar.gz dir'."
445 (let* ((archive (car (last args)))
446 (tar-args
447 (cond ((string-match "z2" archive) "If")
448 ((string-match "gz" archive) "zf")
449 ((string-match "\\(az\\|Z\\)" archive) "Zf")
450 (t "f"))))
451 (if (file-exists-p archive)
452 (setq tar-args (concat "u" tar-args))
453 (setq tar-args (concat "c" tar-args)))
454 (if em-verbose
455 (setq tar-args (concat "v" tar-args)))
456 (if (equal command "mv")
457 (setq tar-args (concat "--remove-files -" tar-args)))
458 ;; truncate the archive name from the arguments
459 (setcdr (last args 2) nil)
460 (throw 'eshell-replace-command
461 (eshell-parse-command
462 (format "tar %s %s" tar-args archive) args))))
464 ;; this is to avoid duplicating code...
465 (defmacro eshell-mvcpln-template (command action func query-var
466 force-var &optional preserve)
467 `(let ((len (length args)))
468 (if (or (= len 0)
469 (and (= len 1) (null eshell-default-target-is-dot)))
470 (error "%s: missing destination file or directory" ,command))
471 (if (= len 1)
472 (nconc args '(".")))
473 (setq args (eshell-stringify-list (eshell-flatten-list args)))
474 (if (and ,(not (equal command "ln"))
475 (string-match eshell-tar-regexp (car (last args)))
476 (or (> (length args) 2)
477 (and (file-directory-p (car args))
478 (or (not no-dereference)
479 (not (file-symlink-p (car args)))))))
480 (eshell-shorthand-tar-command ,command args)
481 (let ((target (car (last args)))
482 ange-cache)
483 (setcdr (last args 2) nil)
484 (eshell-shuffle-files
485 ,command ,action args target ,func nil
486 ,@(append
487 `((if (and (or em-interactive
488 ,query-var)
489 (not force))
490 1 (or force ,force-var)))
491 (if preserve
492 (list preserve)))))
493 nil)))
495 (defun eshell/mv (&rest args)
496 "Implementation of mv in Lisp."
497 (eshell-eval-using-options
498 "mv" args
499 '((?f "force" nil force
500 "remove existing destinations, never prompt")
501 (?i "interactive" nil em-interactive
502 "request confirmation if target already exists")
503 (?n "preview" nil em-preview
504 "don't change anything on disk")
505 (?v "verbose" nil em-verbose
506 "explain what is being done")
507 (nil "help" nil nil "show this usage screen")
508 :preserve-args
509 :external "mv"
510 :show-usage
511 :usage "[OPTION]... SOURCE DEST
512 or: mv [OPTION]... SOURCE... DIRECTORY
513 Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.
514 \[OPTION] DIRECTORY...")
515 (let ((no-dereference t))
516 (eshell-mvcpln-template "mv" "moving" 'rename-file
517 eshell-mv-interactive-query
518 eshell-mv-overwrite-files))))
520 (put 'eshell/mv 'eshell-no-numeric-conversions t)
522 (defun eshell/cp (&rest args)
523 "Implementation of cp in Lisp."
524 (eshell-eval-using-options
525 "cp" args
526 '((?a "archive" nil archive
527 "same as -dpR")
528 (?d "no-dereference" nil no-dereference
529 "preserve links")
530 (?f "force" nil force
531 "remove existing destinations, never prompt")
532 (?i "interactive" nil em-interactive
533 "request confirmation if target already exists")
534 (?n "preview" nil em-preview
535 "don't change anything on disk")
536 (?p "preserve" nil preserve
537 "preserve file attributes if possible")
538 (?r "recursive" nil em-recursive
539 "copy directories recursively")
540 (?R nil nil em-recursive
541 "as for -r")
542 (?v "verbose" nil em-verbose
543 "explain what is being done")
544 (nil "help" nil nil "show this usage screen")
545 :preserve-args
546 :external "cp"
547 :show-usage
548 :usage "[OPTION]... SOURCE DEST
549 or: cp [OPTION]... SOURCE... DIRECTORY
550 Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.")
551 (if archive
552 (setq preserve t no-dereference t em-recursive t))
553 (eshell-mvcpln-template "cp" "copying" 'copy-file
554 eshell-cp-interactive-query
555 eshell-cp-overwrite-files preserve)))
557 (put 'eshell/cp 'eshell-no-numeric-conversions t)
559 (defun eshell/ln (&rest args)
560 "Implementation of ln in Lisp."
561 (eshell-eval-using-options
562 "ln" args
563 '((?h "help" nil nil "show this usage screen")
564 (?s "symbolic" nil symbolic
565 "make symbolic links instead of hard links")
566 (?i "interactive" nil em-interactive
567 "request confirmation if target already exists")
568 (?f "force" nil force "remove existing destinations, never prompt")
569 (?n "preview" nil em-preview
570 "don't change anything on disk")
571 (?v "verbose" nil em-verbose "explain what is being done")
572 :preserve-args
573 :external "ln"
574 :show-usage
575 :usage "[OPTION]... TARGET [LINK_NAME]
576 or: ln [OPTION]... TARGET... DIRECTORY
577 Create a link to the specified TARGET with optional LINK_NAME. If there is
578 more than one TARGET, the last argument must be a directory; create links
579 in DIRECTORY to each TARGET. Create hard links by default, symbolic links
580 with '--symbolic'. When creating hard links, each TARGET must exist.")
581 (let ((no-dereference t))
582 (eshell-mvcpln-template "ln" "linking"
583 (if symbolic
584 'make-symbolic-link
585 'add-name-to-file)
586 eshell-ln-interactive-query
587 eshell-ln-overwrite-files))))
589 (put 'eshell/ln 'eshell-no-numeric-conversions t)
591 (defun eshell/cat (&rest args)
592 "Implementation of cat in Lisp.
593 If in a pipeline, or the file is not a regular file, directory or
594 symlink, then revert to the system's definition of cat."
595 (setq args (eshell-stringify-list (eshell-flatten-list args)))
596 (if (or eshell-in-pipeline-p
597 (catch 'special
598 (dolist (arg args)
599 (unless (or (and (stringp arg)
600 (> (length arg) 0)
601 (eq (aref arg 0) ?-))
602 (let ((attrs (eshell-file-attributes arg)))
603 (and attrs (memq (aref (nth 8 attrs) 0)
604 '(?d ?l ?-)))))
605 (throw 'special t)))))
606 (let ((ext-cat (eshell-search-path "cat")))
607 (if ext-cat
608 (throw 'eshell-replace-command
609 (eshell-parse-command (eshell-quote-argument ext-cat) args))
610 (if eshell-in-pipeline-p
611 (error "Eshell's `cat' does not work in pipelines")
612 (error "Eshell's `cat' cannot display one of the files given"))))
613 (eshell-init-print-buffer)
614 (eshell-eval-using-options
615 "cat" args
616 '((?h "help" nil nil "show this usage screen")
617 :external "cat"
618 :show-usage
619 :usage "[OPTION] FILE...
620 Concatenate FILE(s), or standard input, to standard output.")
621 (dolist (file args)
622 (if (string= file "-")
623 (throw 'eshell-external
624 (eshell-external-command "cat" args))))
625 (let ((curbuf (current-buffer)))
626 (dolist (file args)
627 (with-temp-buffer
628 (insert-file-contents file)
629 (goto-char (point-min))
630 (while (not (eobp))
631 (let ((str (buffer-substring
632 (point) (min (1+ (line-end-position))
633 (point-max)))))
634 (with-current-buffer curbuf
635 (eshell-buffered-print str)))
636 (forward-line)))))
637 (eshell-flush)
638 ;; if the file does not end in a newline, do not emit one
639 (setq eshell-ensure-newline-p nil))))
641 (put 'eshell/cat 'eshell-no-numeric-conversions t)
643 ;; special front-end functions for compilation-mode buffers
645 (defun eshell/make (&rest args)
646 "Use `compile' to do background makes."
647 (if (and eshell-current-subjob-p
648 (eshell-interactive-output-p))
649 (let ((compilation-process-setup-function
650 (list 'lambda nil
651 (list 'setq 'process-environment
652 (list 'quote (eshell-copy-environment))))))
653 (compile (concat "make " (eshell-flatten-and-stringify args))))
654 (throw 'eshell-replace-command
655 (eshell-parse-command "*make" (eshell-stringify-list
656 (eshell-flatten-list args))))))
658 (put 'eshell/make 'eshell-no-numeric-conversions t)
660 (defun eshell-occur-mode-goto-occurrence ()
661 "Go to the occurrence the current line describes."
662 (interactive)
663 (let ((pos (occur-mode-find-occurrence)))
664 (pop-to-buffer (marker-buffer pos))
665 (goto-char (marker-position pos))))
667 (defun eshell-occur-mode-mouse-goto (event)
668 "In Occur mode, go to the occurrence whose line you click on."
669 (interactive "e")
670 (let (pos)
671 (with-current-buffer (window-buffer (posn-window (event-end event)))
672 (save-excursion
673 (goto-char (posn-point (event-end event)))
674 (setq pos (occur-mode-find-occurrence))))
675 (pop-to-buffer (marker-buffer pos))
676 (goto-char (marker-position pos))))
678 (defun eshell-poor-mans-grep (args)
679 "A poor version of grep that opens every file and uses `occur'.
680 This eats up memory, since it leaves the buffers open (to speed future
681 searches), and it's very slow. But, if your system has no grep
682 available..."
683 (save-selected-window
684 (let ((default-dir default-directory))
685 (with-current-buffer (get-buffer-create "*grep*")
686 (let ((inhibit-read-only t)
687 (default-directory default-dir))
688 (erase-buffer)
689 (occur-mode)
690 (let ((files (eshell-stringify-list
691 (eshell-flatten-list (cdr args))))
692 (inhibit-redisplay t)
693 string)
694 (when (car args)
695 (if (get-buffer "*Occur*")
696 (kill-buffer (get-buffer "*Occur*")))
697 (setq string nil)
698 (while files
699 (with-current-buffer (find-file-noselect (car files))
700 (save-excursion
701 (ignore-errors
702 (occur (car args))))
703 (if (get-buffer "*Occur*")
704 (with-current-buffer (get-buffer "*Occur*")
705 (setq string (buffer-string))
706 (kill-buffer (current-buffer)))))
707 (if string (insert string))
708 (setq string nil
709 files (cdr files)))))
710 (local-set-key [mouse-2] 'eshell-occur-mode-mouse-goto)
711 (local-set-key [(control ?c) (control ?c)]
712 'eshell-occur-mode-goto-occurrence)
713 (local-set-key [(control ?m)]
714 'eshell-occur-mode-goto-occurrence)
715 (local-set-key [return] 'eshell-occur-mode-goto-occurrence)
716 (pop-to-buffer (current-buffer) t)
717 (goto-char (point-min))
718 (resize-temp-buffer-window))))))
720 (defvar compilation-scroll-output)
722 (defun eshell-grep (command args &optional maybe-use-occur)
723 "Generic service function for the various grep aliases.
724 It calls Emacs's grep utility if the command is not redirecting output,
725 and if it's not part of a command pipeline. Otherwise, it calls the
726 external command."
727 (if (and maybe-use-occur eshell-no-grep-available)
728 (eshell-poor-mans-grep args)
729 (if (or eshell-plain-grep-behavior
730 (not (and (eshell-interactive-output-p)
731 (not eshell-in-pipeline-p)
732 (not eshell-in-subcommand-p))))
733 (throw 'eshell-replace-command
734 (eshell-parse-command (concat "*" command)
735 (eshell-stringify-list
736 (eshell-flatten-list args))))
737 (let* ((args (mapconcat 'identity
738 (mapcar 'shell-quote-argument
739 (eshell-stringify-list
740 (eshell-flatten-list args)))
741 " "))
742 (cmd (progn
743 (set-text-properties 0 (length args)
744 '(invisible t) args)
745 (format "%s -n %s" command args)))
746 compilation-scroll-output)
747 (grep cmd)))))
749 (defun eshell/grep (&rest args)
750 "Use Emacs grep facility instead of calling external grep."
751 (eshell-grep "grep" args t))
753 (defun eshell/egrep (&rest args)
754 "Use Emacs grep facility instead of calling external egrep."
755 (eshell-grep "egrep" args t))
757 (defun eshell/fgrep (&rest args)
758 "Use Emacs grep facility instead of calling external fgrep."
759 (eshell-grep "fgrep" args t))
761 (defun eshell/agrep (&rest args)
762 "Use Emacs grep facility instead of calling external agrep."
763 (eshell-grep "agrep" args))
765 (defun eshell/glimpse (&rest args)
766 "Use Emacs grep facility instead of calling external glimpse."
767 (let (null-device)
768 (eshell-grep "glimpse" (append '("-z" "-y") args))))
770 ;; completions rules for some common UNIX commands
772 (defsubst eshell-complete-hostname ()
773 "Complete a command that wants a hostname for an argument."
774 (pcomplete-here (eshell-read-host-names)))
776 (defun eshell-complete-host-reference ()
777 "If there is a host reference, complete it."
778 (let ((arg (pcomplete-actual-arg))
779 index)
780 (when (setq index (string-match "@[a-z.]*\\'" arg))
781 (setq pcomplete-stub (substring arg (1+ index))
782 pcomplete-last-completion-raw t)
783 (throw 'pcomplete-completions (eshell-read-host-names)))))
785 (defalias 'pcomplete/ftp 'eshell-complete-hostname)
786 (defalias 'pcomplete/ncftp 'eshell-complete-hostname)
787 (defalias 'pcomplete/ping 'eshell-complete-hostname)
788 (defalias 'pcomplete/rlogin 'eshell-complete-hostname)
790 (defun pcomplete/telnet ()
791 (require 'pcmpl-unix)
792 (pcomplete-opt "xl(pcmpl-unix-user-names)")
793 (eshell-complete-hostname))
795 (defun pcomplete/rsh ()
796 "Complete `rsh', which, after the user and hostname, is like xargs."
797 (require 'pcmpl-unix)
798 (pcomplete-opt "l(pcmpl-unix-user-names)")
799 (eshell-complete-hostname)
800 (pcomplete-here (funcall pcomplete-command-completion-function))
801 (funcall (or (pcomplete-find-completion-function (pcomplete-arg 1))
802 pcomplete-default-completion-function)))
804 (defvar block-size)
805 (defvar by-bytes)
806 (defvar dereference-links)
807 (defvar grand-total)
808 (defvar human-readable)
809 (defvar max-depth)
810 (defvar only-one-filesystem)
811 (defvar show-all)
813 (defsubst eshell-du-size-string (size)
814 (let* ((str (eshell-printable-size size human-readable block-size t))
815 (len (length str)))
816 (concat str (if (< len 8)
817 (make-string (- 8 len) ? )))))
819 (defun eshell-du-sum-directory (path depth)
820 "Summarize PATH, and its member directories."
821 (let ((entries (eshell-directory-files-and-attributes path))
822 (size 0.0))
823 (while entries
824 (unless (string-match "\\`\\.\\.?\\'" (caar entries))
825 (let* ((entry (concat path "/"
826 (caar entries)))
827 (symlink (and (stringp (cadr (car entries)))
828 (cadr (car entries)))))
829 (unless (or (and symlink (not dereference-links))
830 (and only-one-filesystem
831 (/= only-one-filesystem
832 (nth 12 (car entries)))))
833 (if symlink
834 (setq entry symlink))
835 (setq size
836 (+ size
837 (if (eq t (cadr (car entries)))
838 (eshell-du-sum-directory entry (1+ depth))
839 (let ((file-size (nth 8 (car entries))))
840 (prog1
841 file-size
842 (if show-all
843 (eshell-print
844 (concat (eshell-du-size-string file-size)
845 entry "\n")))))))))))
846 (setq entries (cdr entries)))
847 (if (or (not max-depth)
848 (= depth max-depth)
849 (= depth 0))
850 (eshell-print (concat (eshell-du-size-string size)
851 (directory-file-name path) "\n")))
852 size))
854 (defun eshell/du (&rest args)
855 "Implementation of \"du\" in Lisp, passing ARGS."
856 (setq args (if args
857 (eshell-stringify-list (eshell-flatten-list args))
858 '(".")))
859 (let ((ext-du (eshell-search-path "du")))
860 (if (and ext-du
861 (not (catch 'have-ange-path
862 (dolist (arg args)
863 (if (string-equal
864 (file-remote-p (expand-file-name arg) 'method) "ftp")
865 (throw 'have-ange-path t))))))
866 (throw 'eshell-replace-command
867 (eshell-parse-command (eshell-quote-argument ext-du) args))
868 (eshell-eval-using-options
869 "du" args
870 '((?a "all" nil show-all
871 "write counts for all files, not just directories")
872 (nil "block-size" t block-size
873 "use SIZE-byte blocks (i.e., --block-size SIZE)")
874 (?b "bytes" nil by-bytes
875 "print size in bytes")
876 (?c "total" nil grand-total
877 "produce a grand total")
878 (?d "max-depth" t max-depth
879 "display data only this many levels of data")
880 (?h "human-readable" 1024 human-readable
881 "print sizes in human readable format")
882 (?H "is" 1000 human-readable
883 "likewise, but use powers of 1000 not 1024")
884 (?k "kilobytes" 1024 block-size
885 "like --block-size 1024")
886 (?L "dereference" nil dereference-links
887 "dereference all symbolic links")
888 (?m "megabytes" 1048576 block-size
889 "like --block-size 1048576")
890 (?s "summarize" 0 max-depth
891 "display only a total for each argument")
892 (?x "one-file-system" nil only-one-filesystem
893 "skip directories on different filesystems")
894 (nil "help" nil nil
895 "show this usage screen")
896 :external "du"
897 :usage "[OPTION]... FILE...
898 Summarize disk usage of each FILE, recursively for directories.")
899 (unless by-bytes
900 (setq block-size (or block-size 1024)))
901 (if (and max-depth (stringp max-depth))
902 (setq max-depth (string-to-number max-depth)))
903 ;; filesystem support means nothing under Windows
904 (if (eshell-under-windows-p)
905 (setq only-one-filesystem nil))
906 (let ((size 0.0) ange-cache)
907 (while args
908 (if only-one-filesystem
909 (setq only-one-filesystem
910 (nth 11 (eshell-file-attributes
911 (file-name-as-directory (car args))))))
912 (setq size (+ size (eshell-du-sum-directory
913 (directory-file-name (car args)) 0)))
914 (setq args (cdr args)))
915 (if grand-total
916 (eshell-print (concat (eshell-du-size-string size)
917 "total\n"))))))))
919 (defvar eshell-time-start nil)
921 (defun eshell-show-elapsed-time ()
922 (let ((elapsed (format "%.3f secs\n" (- (float-time) eshell-time-start))))
923 (set-text-properties 0 (length elapsed) '(face bold) elapsed)
924 (eshell-interactive-print elapsed))
925 (remove-hook 'eshell-post-command-hook 'eshell-show-elapsed-time t))
927 (defun eshell/time (&rest args)
928 "Implementation of \"time\" in Lisp."
929 (let ((time-args (copy-alist args))
930 (continue t)
931 last-arg)
932 (while (and continue args)
933 (if (not (string-match "^-" (car args)))
934 (progn
935 (if last-arg
936 (setcdr last-arg nil)
937 (setq args '("")))
938 (setq continue nil))
939 (setq last-arg args
940 args (cdr args))))
941 (eshell-eval-using-options
942 "time" args
943 '((?h "help" nil nil "show this usage screen")
944 :external "time"
945 :show-usage
946 :usage "COMMAND...
947 Show wall-clock time elapsed during execution of COMMAND.")
948 (setq eshell-time-start (float-time))
949 (add-hook 'eshell-post-command-hook 'eshell-show-elapsed-time nil t)
950 ;; after setting
951 (throw 'eshell-replace-command
952 (eshell-parse-command (car time-args)
953 ;;; http://lists.gnu.org/archive/html/bug-gnu-emacs/2007-08/msg00205.html
954 (eshell-stringify-list
955 (eshell-flatten-list (cdr time-args))))))))
957 (defun eshell/whoami (&rest args)
958 "Make \"whoami\" Tramp aware."
959 (or (file-remote-p default-directory 'user) (user-login-name)))
961 (defvar eshell-diff-window-config nil)
963 (defun eshell-diff-quit ()
964 "Restore the window configuration previous to diff'ing."
965 (interactive)
966 (if eshell-diff-window-config
967 (set-window-configuration eshell-diff-window-config)))
969 (defun nil-blank-string (string)
970 "Return STRING, or nil if STRING contains only non-blank characters."
971 (cond
972 ((string-match "[^[:blank:]]" string) string)
973 (nil)))
975 (autoload 'diff-no-select "diff")
977 (defun eshell/diff (&rest args)
978 "Alias \"diff\" to call Emacs `diff' function."
979 (let ((orig-args (eshell-stringify-list (eshell-flatten-list args))))
980 (if (or eshell-plain-diff-behavior
981 (not (and (eshell-interactive-output-p)
982 (not eshell-in-pipeline-p)
983 (not eshell-in-subcommand-p))))
984 (throw 'eshell-replace-command
985 (eshell-parse-command "*diff" orig-args))
986 (setq args (copy-sequence orig-args))
987 (if (< (length args) 2)
988 (throw 'eshell-replace-command
989 (eshell-parse-command "*diff" orig-args)))
990 (let ((old (car (last args 2)))
991 (new (car (last args)))
992 (config (current-window-configuration)))
993 (if (= (length args) 2)
994 (setq args nil)
995 (setcdr (last args 3) nil))
996 (with-current-buffer
997 (condition-case nil
998 (diff-no-select
999 old new
1000 (nil-blank-string (eshell-flatten-and-stringify args)))
1001 (error
1002 (throw 'eshell-replace-command
1003 (eshell-parse-command "*diff" orig-args))))
1004 (when (fboundp 'diff-mode)
1005 (make-local-variable 'compilation-finish-functions)
1006 (add-hook
1007 'compilation-finish-functions
1008 `(lambda (buff msg)
1009 (with-current-buffer buff
1010 (diff-mode)
1011 (set (make-local-variable 'eshell-diff-window-config)
1012 ,config)
1013 (local-set-key [?q] 'eshell-diff-quit)
1014 (if (fboundp 'turn-on-font-lock-if-enabled)
1015 (turn-on-font-lock-if-enabled))
1016 (goto-char (point-min))))))
1017 (pop-to-buffer (current-buffer))))))
1018 nil)
1020 (put 'eshell/diff 'eshell-no-numeric-conversions t)
1022 (defvar locate-history-list)
1024 (defun eshell/locate (&rest args)
1025 "Alias \"locate\" to call Emacs `locate' function."
1026 (if (or eshell-plain-locate-behavior
1027 (not (and (eshell-interactive-output-p)
1028 (not eshell-in-pipeline-p)
1029 (not eshell-in-subcommand-p)))
1030 (and (stringp (car args))
1031 (string-match "^-" (car args))))
1032 (throw 'eshell-replace-command
1033 (eshell-parse-command "*locate" (eshell-stringify-list
1034 (eshell-flatten-list args))))
1035 (save-selected-window
1036 (let ((locate-history-list (list (car args))))
1037 (locate-with-filter (car args) (cadr args))))))
1039 (put 'eshell/locate 'eshell-no-numeric-conversions t)
1041 (defun eshell/occur (&rest args)
1042 "Alias \"occur\" to call Emacs `occur' function."
1043 (let ((inhibit-read-only t))
1044 (if (> (length args) 2)
1045 (error "usage: occur: (REGEXP &optional NLINES)")
1046 (apply 'occur args))))
1048 (put 'eshell/occur 'eshell-no-numeric-conversions t)
1050 (provide 'em-unix)
1052 ;; Local Variables:
1053 ;; generated-autoload-file: "esh-groups.el"
1054 ;; End:
1056 ;;; em-unix.el ends here