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