A
download shell-command.el
Language: Lisp
License: GPL
Copyright: (C) 1998-2002 TSUCHIYA Masatoshi
LOC: 235
Project Info
Galatea
Server: BerliOS (SVN)
Type: svn
...alatea\trank\.xemacs\elisp\
   active-menu.el
   autorevert.el
   color-theme.el.flc
   ctypes.el
   dired-single.el
   dos2unix.el
   doxymacs.el
   doxymacs.el.flc
   face-list.el
   file-confirm.el
   flame.el
   follow.el
   gtags.el
   highlight-completion.el
   highlight-current-line.el
   iv-mode.el
   member-functions.el
   nc.el
   nc.el.flc
   plsql.el
   psql-mode.el
   psvn.el
   psvn.el.flc
   searchmenu.el
   session.el
   shell-command.el
   template.el
   tex-site.el
   xml-parse.el

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
;;; shell-command.el --- enabling (tab)completion for shell-command

;; Copyright (C) 1998-2002 TSUCHIYA Masatoshi <tsuchiya@namazu.org>

;; Author: TSUCHIYA Masatoshi <tsuchiya@namazu.org>
;; Keywords: shell
;; Version: $Revision: 1.1.1.1 $

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program; if not, you can either send email to this
;; program's maintainer or write to: The Free Software Foundation,
;; Inc.; 59 Temple Place, Suite 330; Boston, MA 02111-1307, USA.

;;; Commentary:

;; This is an enhancement for shell-command, enabling (tab)completion
;; of commands and dir/filenames within the shell-command input
;; context.

;; The latest version of this program can be downloaded from
;; http://namazu.org/~tsuchiya/elisp/shell-command.el.

;;; Install:

;; Install this file to appropriate directory, and put these lines
;; into your ~/.emacs.

;;     (require 'shell-command)

;;; Code:
(eval-when-compile
  (require 'shell)
  (require 'comint))

(eval-and-compile
  ;; Stuffs to keep compatibility between Emacsen.
  (or (fboundp 'defgroup)
      (defmacro defgroup (symbol members doc &rest args) nil))
  (or (fboundp 'defcustom)
      (defmacro defcustom (symbol value doc &rest args)
	(list 'defvar symbol value doc)))
  ;; These macros, such as `when' and `unless' are imported from
  ;; subr.el of Emacs-21.2.
  (or (fboundp 'when)
      (progn
	(defmacro when (cond &rest body)
	  "If COND yields non-nil, do BODY, else return nil."
	  (list 'if cond (cons 'progn body)))
	(put 'when 'edebug-form-spec '(form body))
	(put 'when 'lisp-indent-function 1)))
  (or (fboundp 'unless)
      (progn
	(defmacro unless (cond &rest body)
	  "If COND yields nil, do BODY, else return nil."
	  (cons 'if (cons cond (cons nil body))))
	(put 'unless 'edebug-form-spec '(form body))
	(put 'unless 'lisp-indent-function 1))))

(defcustom shell-command-complete-functions
  '(shell-dynamic-complete-environment-variable
    shell-dynamic-complete-command
    shell-replace-by-expanded-directory
    comint-dynamic-complete-filename)
  "Function list to complete shell commands."
  :type '(repeat function)
  :group 'shell)

(defcustom shell-command-prompt
  "Shell command [%w]%$ "
  "Prompt string of shell-command.
A number of %-sequences is available to customize.  Note
`shell-command-make-prompt-string'."
  :type 'string
  :group 'shell)

(defcustom shell-command-on-region-prompt
  "Shell command on region [%w]%$ "
  "Prompt string of shell-command-on-region.
A number of %-sequences is available to customize.  Note
`shell-command-make-prompt-string'."
  :type 'string
  :group 'shell)

(defcustom grep-prompt
  "Run grep [%w]%$ "
  "Prompt string of grep.
A number of %-sequences is available to customize.  Note
`shell-command-make-prompt-string'."
  :type 'string
  :group 'shell)

(defcustom grep-find-prompt
  "Run find [%w]%$ "
  "Prompt string of grep.
A number of %-sequences is available to customize.  Note
`shell-command-make-prompt-string'."
  :type 'string
  :group 'shell)


(put 'shell-command/static-if 'lisp-indent-function 2)
(defmacro shell-command/static-if (cond then &rest else)
  (if (eval cond) then (` (progn  (,@ else)))))


(defun shell-command-make-prompt-string (format-string current-directory) "\
Function to generate prompt string

Use FORMAT-STRING to generate prompt string at the directory
CURRENT-DIRECTORY.  The following `%' escapes are available for use in
FORMAT-STRING:

%d     the date in \"Weekday Month Date\" format \(e.g., \"Tue May 26\"\)
%h     the hostname up to the first `.'
%H     the hostname
%t     the current time in 24-hour HH:MM:SS format
%T     the current time in 12-hour HH:MM:SS format
%@     the current time in 12-hour am/pm format
%u     the username of the current user
%w     the current working directory
%W     the basename of the current working directory
%$     if the effective UID is 0, a #, otherwise a $
%%     Insert a literal `%'.
"
  (let ((case-fold-search nil)
	start buf
	(list (list format-string))
	(alist (let ((system-name (system-name))
		     host-name
		     fqdn-name
		     (time (current-time))
		     (dir (directory-file-name
			   (abbreviate-file-name current-directory))))
		 (shell-command/static-if (featurep 'xemacs)
		     (cond
		      ((string= dir (user-home-directory))
		       (setq dir "~"))
		      ((string-match (concat "^"
					     (regexp-quote
					      (file-name-as-directory
					       (user-home-directory))))
				     dir)
		       (setq dir
			     (concat "~/" (substring dir (match-end 0)))))))
		 (if (string-match "^\\([^.]+\\)\\.[^.]" system-name)
		     (setq fqdn-name system-name
			   host-name (match-string 1 system-name))
		   (setq host-name system-name
			 fqdn-name
			 (cond
			  ((and (boundp 'mail-host-address)
				(stringp mail-host-address)
				(string-match "\\." mail-host-address))
			   mail-host-address)
			  ((and user-mail-address
				(string-match "\\." user-mail-address)
				(string-match "@\\(.*\\)\\'"
					      user-mail-address))
			   (match-string 1 user-mail-address))
			  (t system-name))))
		 `(("%%" . "%")
		   ("%d" . ,(format-time-string "%a %b %e" time))
		   ("%h" . ,host-name)
		   ("%H" . ,fqdn-name)
		   ("%t" . ,(format-time-string "%H:%M:%S" time))
		   ("%T" . ,(format-time-string "%I:%M:%S" time))
		   ("%@" . ,(format-time-string "%I:%M%p" time))
		   ("%u" . ,(user-login-name))
		   ("%w" . ,dir)
		   ("%W" . ,(file-name-nondirectory
			     (directory-file-name current-directory)))
		   ("%\\$" . ,(if (= (user-uid) 0) "#" "$"))))))
    (while alist
      (setq buf nil)
      (while list
	(setq start 0)
	(while (string-match (car (car alist)) (car list) start)
	  (setq buf (cons (cdr (car alist))
			  (cons (substring (car list) start
					   (match-beginning 0))
				buf))
		start (match-end 0)))
	(setq buf (cons (substring (car list) start) buf)
	      list (cdr list)))
      (setq list (nreverse buf)
	    alist (cdr alist)))
    (apply 'concat list)))


(defmacro shell-command-minibuffer-prompt-end ()
  (if (fboundp 'minibuffer-prompt-end)
      '(minibuffer-prompt-end)
    '(point-min)))

(defun shell-command-read-minibuffer
  (format-string current-directory &optional initial-contents
		 user-keymap read hist)
  "Read a command string in the minibuffer, with completion."
  (let ((keymap (copy-keymap (or user-keymap minibuffer-local-map)))
	(prompt (shell-command-make-prompt-string
		 format-string current-directory)))
    (define-key keymap "\t"
      (lambda ()
	(interactive)
	(let ((orig-function (symbol-function 'message)))
	  (unwind-protect
	      (progn
		(defun message (string &rest arguments)
		  (let* ((s1 (concat prompt
				     (buffer-substring
				      (shell-command-minibuffer-prompt-end)
				      (point-max))))
			 (s2 (apply (function format) string arguments))
			 (w (- (window-width)
			       (string-width s1)
			       (string-width s2)
			       1)))
		    (funcall orig-function
			     (if (>= w 0)
				 (concat s1 (make-string w ?\ ) s2)
			       s2))
		    (if (sit-for 0.3) (funcall orig-function s1))
		    s2))
		(require 'shell)
		(require 'comint)
		(run-hook-with-args-until-success
		 'shell-command-complete-functions))
	    (fset 'message orig-function)))))
    (read-from-minibuffer prompt initial-contents keymap read hist)))


(let (current-load-list)
  (defadvice shell-command
    (before shell-command-with-completion activate compile)
    (interactive
     (list
      (shell-command-read-minibuffer shell-command-prompt
				     default-directory
				     nil nil nil 'shell-command-history)
      current-prefix-arg))))


(let (current-load-list)
  (defadvice shell-command-on-region
    (before shell-command-on-region-with-completion activate compile)
    (interactive
     (list (region-beginning) (region-end)
	   (shell-command-read-minibuffer shell-command-on-region-prompt
					  default-directory
					  nil nil nil 'shell-command-history)
	   current-prefix-arg
	   current-prefix-arg
	   shell-command-default-error-buffer))))


(let (current-load-list)
  (defadvice grep
    (before grep-with-completion activate compile)
    (interactive
     (let (grep-default (arg current-prefix-arg))
       (unless grep-command
	 (grep-compute-defaults))
       (when arg
	 (let* ((tag-default
		 (funcall (or find-tag-default-function
			      (get major-mode 'find-tag-default-function)
			      'grep-tag-default))))
	   (setq grep-default (or (car grep-history) grep-command))
	   (when (string-match
		  "[^ ]+\\s +\\(-[^ ]+\\s +\\)*\\(\"[^\"]+\"\\|[^ ]+\\)"
		  grep-default)
	     (setq grep-default
		   (replace-match tag-default t t grep-default 2)))))
       (list (shell-command-read-minibuffer grep-prompt
					    default-directory
					    (or grep-default grep-command)
					    nil nil 'grep-history))))))


(let (current-load-list)
  (defadvice grep-find
    (before grep-find-with-completion activate compile)
    (interactive
     (progn
       (unless grep-find-command
	 (grep-compute-defaults))
       (list (shell-command-read-minibuffer grep-find-prompt
					    default-directory
					    grep-find-command
					    nil nil 'grep-find-history))))))

(provide 'shell-command)

;;; shell-command.el ends here.

About Koders | Resources | Downloads | Support | Black Duck | Terms of Service | DMCA | Privacy Policy | Contact Us