2
|
1 ;;; sh-script.el --- shell-script editing commands for Emacs
|
|
2
|
|
3 ;; Copyright (C) 1993, 1994, 1995, 1996 by Free Software Foundation, Inc.
|
|
4
|
|
5 ;; Author: Daniel.Pfeiffer@Informatik.START.dbp.de, fax (+49 69) 7588-2389
|
|
6 ;; Version: 2.0e
|
|
7 ;; Maintainer: FSF
|
|
8 ;; Keywords: languages, unix
|
|
9
|
|
10 ;; This file is part of XEmacs.
|
|
11
|
|
12 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
13 ;; under the terms of the GNU General Public License as published by
|
|
14 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
15 ;; any later version.
|
|
16
|
|
17 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
20 ;; General Public License for more details.
|
|
21
|
|
22 ;; You should have received a copy of the GNU General Public License
|
|
23 ;; along with XEmacs; see the file COPYING. If not, write to the Free
|
|
24 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
25 ;; 02111-1307, USA.
|
|
26
|
|
27 ;;; Synched up with: FSF 19.34.
|
|
28
|
|
29 ;;; Commentary:
|
|
30
|
|
31 ;; Major mode for editing shell scripts. Bourne, C and rc shells as well
|
|
32 ;; as various derivatives are supported and easily derived from. Structured
|
|
33 ;; statements can be inserted with one command or abbrev. Completion is
|
|
34 ;; available for filenames, variables known from the script, the shell and
|
|
35 ;; the environment as well as commands.
|
|
36
|
|
37 ;;; Known Bugs:
|
|
38
|
|
39 ;; - In Bourne the keyword `in' is not anchored to case, for, select ...
|
|
40 ;; - Variables in `"' strings aren't fontified because there's no way of
|
|
41 ;; syntactically distinguishing those from `'' strings.
|
|
42
|
|
43 ;;; Code:
|
|
44
|
|
45 ;; page 1: variables and settings
|
|
46 ;; page 2: mode-command and utility functions
|
|
47 ;; page 3: statement syntax-commands for various shells
|
|
48 ;; page 4: various other commands
|
|
49
|
|
50 (require 'executable)
|
|
51
|
98
|
52 ;;; interpreter-mode-alist is not compatible between Emacs and XEmacs.
|
|
53 ;;; So fake it.
|
|
54
|
|
55 (defvar sh-interpreter-mode-alist
|
|
56 '(("perl" . perl-mode)
|
|
57 ("perl5" . perl-mode)
|
|
58 ("wish" . tcl-mode)
|
|
59 ("wishx" . tcl-mode)
|
|
60 ("tcl" . tcl-mode)
|
|
61 ("tclsh" . tcl-mode)
|
|
62 ("awk" . awk-mode)
|
|
63 ("mawk" . awk-mode)
|
|
64 ("nawk" . awk-mode)
|
|
65 ("gawk" . awk-mode)
|
|
66 ("scm" . scheme-mode)
|
|
67 ("ash" . sh-mode)
|
|
68 ("bash" . sh-mode)
|
|
69 ("csh" . sh-mode)
|
|
70 ("dtksh" . sh-mode)
|
|
71 ("es" . sh-mode)
|
|
72 ("itcsh" . sh-mode)
|
|
73 ("jsh" . sh-mode)
|
|
74 ("ksh" . sh-mode)
|
|
75 ("oash" . sh-mode)
|
|
76 ("pdksh" . sh-mode)
|
|
77 ("rc" . sh-mode)
|
|
78 ("sh" . sh-mode)
|
|
79 ("sh5" . sh-mode)
|
|
80 ("tcsh" . sh-mode)
|
|
81 ("wksh" . sh-mode)
|
|
82 ("wsh" . sh-mode)
|
|
83 ("zsh" . sh-mode)
|
|
84 ("tail" . text-mode)
|
|
85 ("more" . text-mode)
|
|
86 ("less" . text-mode)
|
|
87 ("pg" . text-mode))
|
|
88 "Alist mapping interpreter names to major modes.
|
|
89 This alist applies to files whose first line starts with `#!'.
|
|
90 Each element looks like (INTERPRETER . MODE).
|
|
91 The car of each element is compared with
|
|
92 the name of the interpreter specified in the first line.
|
|
93 If it matches, mode MODE is selected.")
|
|
94
|
100
|
95 (defvar sh-mode-hook nil
|
|
96 "*Hook run by `sh-mode'.")
|
|
97
|
|
98 (defvar sh-set-shell-hook nil
|
|
99 "*Hook run by `sh-set-shell'.")
|
|
100
|
2
|
101 (defvar sh-ancestor-alist
|
|
102 '((ash . sh)
|
|
103 (bash . jsh)
|
|
104 (dtksh . ksh)
|
|
105 (es . rc)
|
|
106 (itcsh . tcsh)
|
|
107 (jcsh . csh)
|
|
108 (jsh . sh)
|
|
109 (ksh . ksh88)
|
|
110 (ksh88 . jsh)
|
|
111 (oash . sh)
|
|
112 (pdksh . ksh88)
|
|
113 (posix . sh)
|
|
114 (tcsh . csh)
|
|
115 (wksh . ksh88)
|
|
116 (wsh . sh)
|
|
117 (zsh . ksh88))
|
|
118 "*Alist showing the direct ancestor of various shells.
|
|
119 This is the basis for `sh-feature'. See also `sh-alias-alist'.
|
|
120 By default we have the following three hierarchies:
|
|
121
|
|
122 csh C Shell
|
|
123 jcsh C Shell with Job Control
|
|
124 tcsh Toronto C Shell
|
|
125 itcsh ? Toronto C Shell
|
|
126 rc Plan 9 Shell
|
|
127 es Extensible Shell
|
|
128 sh Bourne Shell
|
|
129 ash ? Shell
|
|
130 jsh Bourne Shell with Job Control
|
|
131 bash GNU Bourne Again Shell
|
|
132 ksh88 Korn Shell '88
|
|
133 ksh Korn Shell '93
|
|
134 dtksh CDE Desktop Korn Shell
|
|
135 pdksh Public Domain Korn Shell
|
|
136 wksh Window Korn Shell
|
|
137 zsh Z Shell
|
|
138 oash SCO OA (curses) Shell
|
|
139 posix IEEE 1003.2 Shell Standard
|
|
140 wsh ? Shell")
|
|
141
|
|
142
|
|
143 (defvar sh-alias-alist
|
|
144 ;; XEmacs: Linux is spelled `linux'
|
|
145 (nconc (if (eq system-type 'linux)
|
|
146 '((csh . tcsh)
|
|
147 (ksh . pdksh)))
|
|
148 ;; for the time being
|
|
149 '((ksh . ksh88)
|
|
150 (sh5 . sh)))
|
|
151 "*Alist for transforming shell names to what they really are.
|
|
152 Use this where the name of the executable doesn't correspond to the type of
|
|
153 shell it really is.")
|
|
154
|
|
155
|
|
156 (defvar sh-shell-file (or (getenv "SHELL") "/bin/sh")
|
|
157 "*The executable file name for the shell being programmed.")
|
|
158
|
|
159
|
|
160 (defvar sh-shell-arg
|
|
161 ;; bash does not need any options when run in a shell script,
|
|
162 '((bash)
|
|
163 (csh . "-f")
|
|
164 (pdksh)
|
|
165 ;; Bill_Mann@praxisint.com says -p with ksh can do harm.
|
|
166 (ksh88)
|
|
167 ;; -p means don't initialize functions from the environment.
|
|
168 (rc . "-p")
|
|
169 ;; Someone proposed -motif, but we don't want to encourage
|
|
170 ;; use of a non-free widget set.
|
|
171 (wksh)
|
|
172 ;; -f means don't run .zshrc.
|
|
173 (zsh . "-f"))
|
|
174 "*Single argument string for the magic number. See `sh-feature'.")
|
|
175
|
|
176 (defvar sh-shell-variables nil
|
|
177 "Alist of shell variable names that should be included in completion.
|
|
178 These are used for completion in addition to all the variables named
|
|
179 in `process-environment'. Each element looks like (VAR . VAR), where
|
|
180 the car and cdr are the same symbol.")
|
|
181
|
|
182 (defvar sh-shell-variables-initialized nil
|
|
183 "Non-nil if `sh-shell-variables' is initialized.")
|
|
184
|
|
185 (defun sh-canonicalize-shell (shell)
|
|
186 "Convert a shell name SHELL to the one we should handle it as."
|
|
187 (or (symbolp shell)
|
|
188 (setq shell (intern shell)))
|
|
189 (or (cdr (assq shell sh-alias-alist))
|
|
190 shell))
|
|
191
|
|
192 (defvar sh-shell (sh-canonicalize-shell (file-name-nondirectory sh-shell-file))
|
|
193 "The shell being programmed. This is set by \\[sh-set-shell].")
|
|
194
|
|
195 ;;; I turned off this feature because it doesn't permit typing commands
|
|
196 ;;; in the usual way without help.
|
|
197 ;;;(defvar sh-abbrevs
|
|
198 ;;; '((csh eval sh-abbrevs shell
|
|
199 ;;; "switch" 'sh-case
|
|
200 ;;; "getopts" 'sh-while-getopts)
|
|
201
|
|
202 ;;; (es eval sh-abbrevs shell
|
|
203 ;;; "function" 'sh-function)
|
|
204
|
|
205 ;;; (ksh88 eval sh-abbrevs sh
|
|
206 ;;; "select" 'sh-select)
|
|
207
|
|
208 ;;; (rc eval sh-abbrevs shell
|
|
209 ;;; "case" 'sh-case
|
|
210 ;;; "function" 'sh-function)
|
|
211
|
|
212 ;;; (sh eval sh-abbrevs shell
|
|
213 ;;; "case" 'sh-case
|
|
214 ;;; "function" 'sh-function
|
|
215 ;;; "until" 'sh-until
|
|
216 ;;; "getopts" 'sh-while-getopts)
|
|
217
|
|
218 ;;; ;; The next entry is only used for defining the others
|
|
219 ;;; (shell "for" sh-for
|
|
220 ;;; "loop" sh-indexed-loop
|
|
221 ;;; "if" sh-if
|
|
222 ;;; "tmpfile" sh-tmp-file
|
|
223 ;;; "while" sh-while)
|
|
224
|
|
225 ;;; (zsh eval sh-abbrevs ksh88
|
|
226 ;;; "repeat" 'sh-repeat))
|
|
227 ;;; "Abbrev-table used in Shell-Script mode. See `sh-feature'.
|
|
228 ;;;Due to the internal workings of abbrev tables, the shell name symbol is
|
|
229 ;;;actually defined as the table for the like of \\[edit-abbrevs].")
|
|
230
|
|
231
|
|
232
|
|
233 (defvar sh-mode-syntax-table
|
|
234 '((csh eval identity sh)
|
|
235 (sh eval sh-mode-syntax-table ()
|
|
236 ;; #'s meanings depend on context which can't be expressed here
|
|
237 ;; ?\# "<"
|
|
238 ;; ?\^l ">#"
|
|
239 ;; ?\n ">#"
|
|
240 ?\" "\"\""
|
|
241 ?\' "\"'"
|
|
242 ?\` ".`"
|
|
243 ?$ "_"
|
|
244 ?! "_"
|
|
245 ?% "_"
|
|
246 ?: "_"
|
|
247 ?. "_"
|
|
248 ?^ "_"
|
|
249 ?~ "_")
|
|
250 (rc eval sh-mode-syntax-table sh
|
|
251 ?\" "_"
|
|
252 ?\` "."))
|
|
253 "Syntax-table used in Shell-Script mode. See `sh-feature'.")
|
|
254
|
|
255
|
|
256
|
|
257 (defvar sh-mode-map
|
|
258 (let ((map (make-sparse-keymap))
|
|
259 (menu-map (make-sparse-keymap "Insert")))
|
|
260 (define-key map "\C-c(" 'sh-function)
|
|
261 (define-key map "\C-c\C-w" 'sh-while)
|
|
262 (define-key map "\C-c\C-u" 'sh-until)
|
|
263 (define-key map "\C-c\C-t" 'sh-tmp-file)
|
|
264 (define-key map "\C-c\C-s" 'sh-select)
|
|
265 (define-key map "\C-c\C-r" 'sh-repeat)
|
|
266 (define-key map "\C-c\C-o" 'sh-while-getopts)
|
|
267 (define-key map "\C-c\C-l" 'sh-indexed-loop)
|
|
268 (define-key map "\C-c\C-i" 'sh-if)
|
|
269 (define-key map "\C-c\C-f" 'sh-for)
|
|
270 (define-key map "\C-c\C-c" 'sh-case)
|
|
271
|
|
272 (define-key map "=" 'sh-assignment)
|
|
273 (define-key map "\C-c+" 'sh-add)
|
|
274 (define-key map "\C-\M-x" 'sh-execute-region)
|
|
275 (define-key map "\C-c\C-x" 'executable-interpret)
|
|
276 (define-key map "<" 'sh-maybe-here-document)
|
|
277 (define-key map "(" 'skeleton-pair-insert-maybe)
|
|
278 (define-key map "{" 'skeleton-pair-insert-maybe)
|
|
279 (define-key map "[" 'skeleton-pair-insert-maybe)
|
|
280 (define-key map "'" 'skeleton-pair-insert-maybe)
|
|
281 (define-key map "`" 'skeleton-pair-insert-maybe)
|
|
282 (define-key map "\"" 'skeleton-pair-insert-maybe)
|
|
283
|
|
284 (define-key map "\t" 'sh-indent-line)
|
|
285 (substitute-key-definition 'complete-tag 'comint-dynamic-complete
|
|
286 map (current-global-map))
|
|
287 (substitute-key-definition 'newline-and-indent 'sh-newline-and-indent
|
|
288 map (current-global-map))
|
|
289 (substitute-key-definition 'delete-backward-char
|
|
290 'backward-delete-char-untabify
|
|
291 map (current-global-map))
|
|
292 (define-key map "\C-c:" 'sh-set-shell)
|
|
293 (substitute-key-definition 'beginning-of-defun
|
|
294 'sh-beginning-of-compound-command
|
|
295 map (current-global-map))
|
|
296 (substitute-key-definition 'backward-sentence 'sh-beginning-of-command
|
|
297 map (current-global-map))
|
|
298 (substitute-key-definition 'forward-sentence 'sh-end-of-command
|
|
299 map (current-global-map))
|
|
300 (define-key map [menu-bar insert] (cons "Insert" menu-map))
|
|
301 (define-key menu-map [sh-while] '("While Loop" . sh-while))
|
|
302 (define-key menu-map [sh-until] '("Until Loop" . sh-until))
|
|
303 (define-key menu-map [sh-tmp-file] '("Temporary File" . sh-tmp-file))
|
|
304 (define-key menu-map [sh-select] '("Select Statement" . sh-select))
|
|
305 (define-key menu-map [sh-repeat] '("Repeat Loop" . sh-repeat))
|
|
306 (define-key menu-map [sh-while-getopts]
|
|
307 '("Options Loop" . sh-while-getopts))
|
|
308 (define-key menu-map [sh-indexed-loop]
|
|
309 '("Indexed Loop" . sh-indexed-loop))
|
|
310 (define-key menu-map [sh-if] '("If Statement" . sh-if))
|
|
311 (define-key menu-map [sh-for] '("For Loop" . sh-for))
|
|
312 (define-key menu-map [sh-case] '("Case Statement" . sh-case))
|
|
313 map)
|
|
314 "Keymap used in Shell-Script mode.")
|
|
315
|
|
316
|
|
317
|
|
318 (defvar sh-dynamic-complete-functions
|
|
319 '(shell-dynamic-complete-environment-variable
|
|
320 shell-dynamic-complete-command
|
|
321 comint-dynamic-complete-filename)
|
|
322 "*Functions for doing TAB dynamic completion.")
|
|
323
|
|
324
|
|
325 (defvar sh-require-final-newline
|
|
326 '((csh . t)
|
|
327 (pdksh . t)
|
|
328 (rc eval . require-final-newline)
|
|
329 (sh eval . require-final-newline))
|
|
330 "*Value of `require-final-newline' in Shell-Script mode buffers.
|
|
331 See `sh-feature'.")
|
|
332
|
|
333
|
|
334 (defvar sh-comment-prefix
|
|
335 '((csh . "\\(^\\|[^$]\\|\\$[^{]\\)")
|
|
336 (rc eval identity csh)
|
|
337 (sh . "\\(^\\|[ \t|&;()]\\)"))
|
|
338 "*Regexp matching what may come before a comment `#'.
|
|
339 This must contain one \\(grouping\\) since it is the basis for fontifying
|
|
340 comments as well as for `comment-start-skip'.
|
|
341 See `sh-feature'.")
|
|
342
|
|
343
|
|
344 (defvar sh-assignment-regexp
|
|
345 '((csh . "\\<\\([a-zA-Z0-9_]+\\)\\(\\[.+\\]\\)?[ \t]*[-+*/%^]?=")
|
|
346 ;; actually spaces are only supported in let/(( ... ))
|
|
347 (ksh88 . "\\<\\([a-zA-Z0-9_]+\\)\\(\\[.+\\]\\)?[ \t]*\\([-+*/%&|~^]\\|<<\\|>>\\)?=")
|
|
348 (rc . "\\<\\([a-zA-Z0-9_*]+\\)[ \t]*=")
|
|
349 (sh . "\\<\\([a-zA-Z0-9_]+\\)="))
|
|
350 "*Regexp for the variable name and what may follow in an assignment.
|
|
351 First grouping matches the variable name. This is upto and including the `='
|
|
352 sign. See `sh-feature'.")
|
|
353
|
|
354
|
|
355 (defvar sh-indentation 4
|
|
356 "The width for further indentation in Shell-Script mode.")
|
|
357
|
|
358
|
|
359 (defvar sh-remember-variable-min 3
|
|
360 "*Don't remember variables less than this length for completing reads.")
|
|
361
|
|
362
|
|
363 (defvar sh-header-marker nil
|
|
364 "When non-`nil' is the end of header for prepending by \\[sh-execute-region].
|
|
365 That command is also used for setting this variable.")
|
|
366
|
|
367
|
|
368 (defvar sh-beginning-of-command
|
|
369 "\\([;({`|&]\\|\\`\\|[^\\]\n\\)[ \t]*\\([/~a-zA-Z0-9:]\\)"
|
|
370 "*Regexp to determine the beginning of a shell command.
|
|
371 The actual command starts at the beginning of the second \\(grouping\\).")
|
|
372
|
|
373
|
|
374 (defvar sh-end-of-command
|
|
375 "\\([/~a-zA-Z0-9:]\\)[ \t]*\\([;#)}`|&]\\|$\\)"
|
|
376 "*Regexp to determine the end of a shell command.
|
|
377 The actual command ends at the end of the first \\(grouping\\).")
|
|
378
|
|
379
|
|
380
|
|
381 (defvar sh-here-document-word "EOF"
|
|
382 "Word to delimit here documents.")
|
|
383
|
|
384 (defvar sh-test
|
70
|
385 '((sh "[ ]" . 3)
|
|
386 (ksh88 "[[ ]]" . 4))
|
2
|
387 "Initial input in Bourne if, while and until skeletons. See `sh-feature'.")
|
|
388
|
|
389
|
|
390 (defvar sh-builtins
|
|
391 '((bash eval sh-append posix
|
|
392 "alias" "bg" "bind" "builtin" "declare" "dirs" "enable" "fc" "fg"
|
|
393 "help" "history" "jobs" "kill" "let" "local" "popd" "pushd" "source"
|
|
394 "suspend" "typeset" "unalias")
|
|
395
|
|
396 ;; The next entry is only used for defining the others
|
|
397 (bourne eval sh-append shell
|
|
398 "eval" "export" "getopts" "newgrp" "pwd" "read" "readonly"
|
|
399 "times" "ulimit")
|
|
400
|
|
401 (csh eval sh-append shell
|
|
402 "alias" "chdir" "glob" "history" "limit" "nice" "nohup" "rehash"
|
|
403 "setenv" "source" "time" "unalias" "unhash")
|
|
404
|
|
405 (dtksh eval identity wksh)
|
|
406
|
|
407 (es "access" "apids" "cd" "echo" "eval" "false" "let" "limit" "local"
|
|
408 "newpgrp" "result" "time" "umask" "var" "vars" "wait" "whatis")
|
|
409
|
|
410 (jsh eval sh-append sh
|
|
411 "bg" "fg" "jobs" "kill" "stop" "suspend")
|
|
412
|
|
413 (jcsh eval sh-append csh
|
|
414 "bg" "fg" "jobs" "kill" "notify" "stop" "suspend")
|
|
415
|
|
416 (ksh88 eval sh-append bourne
|
|
417 "alias" "bg" "false" "fc" "fg" "jobs" "kill" "let" "print" "time"
|
|
418 "typeset" "unalias" "whence")
|
|
419
|
|
420 (oash eval sh-append sh
|
|
421 "checkwin" "dateline" "error" "form" "menu" "newwin" "oadeinit"
|
|
422 "oaed" "oahelp" "oainit" "pp" "ppfile" "scan" "scrollok" "wattr"
|
|
423 "wclear" "werase" "win" "wmclose" "wmmessage" "wmopen" "wmove"
|
|
424 "wmtitle" "wrefresh")
|
|
425
|
|
426 (pdksh eval sh-append ksh88
|
|
427 "bind")
|
|
428
|
|
429 (posix eval sh-append sh
|
|
430 "command")
|
|
431
|
|
432 (rc "builtin" "cd" "echo" "eval" "limit" "newpgrp" "shift" "umask" "wait"
|
|
433 "whatis")
|
|
434
|
|
435 (sh eval sh-append bourne
|
|
436 "hash" "test" "type")
|
|
437
|
|
438 ;; The next entry is only used for defining the others
|
|
439 (shell "cd" "echo" "eval" "set" "shift" "umask" "unset" "wait")
|
|
440
|
|
441 (wksh eval sh-append ksh88
|
|
442 "Xt[A-Z][A-Za-z]*")
|
|
443
|
|
444 (zsh eval sh-append ksh88
|
|
445 "autoload" "bindkey" "builtin" "chdir" "compctl" "declare" "dirs"
|
|
446 "disable" "disown" "echotc" "enable" "functions" "getln" "hash"
|
|
447 "history" "integer" "limit" "local" "log" "popd" "pushd" "r"
|
|
448 "readonly" "rehash" "sched" "setopt" "source" "suspend" "true"
|
|
449 "ttyctl" "type" "unfunction" "unhash" "unlimit" "unsetopt" "vared"
|
|
450 "which"))
|
|
451 "*List of all shell builtins for completing read and fontification.
|
|
452 Note that on some systems not all builtins are available or some are
|
|
453 implemented as aliases. See `sh-feature'.")
|
|
454
|
|
455
|
|
456
|
|
457 (defvar sh-leading-keywords
|
|
458 '((csh "else")
|
|
459
|
|
460 (es "true" "unwind-protect" "whatis")
|
|
461
|
|
462 (rc "else")
|
|
463
|
|
464 (sh "do" "elif" "else" "if" "then" "trap" "type" "until" "while"))
|
|
465 "*List of keywords that may be immediately followed by a builtin or keyword.
|
|
466 Given some confusion between keywords and builtins depending on shell and
|
|
467 system, the distinction here has been based on whether they influence the
|
|
468 flow of control or syntax. See `sh-feature'.")
|
|
469
|
|
470
|
|
471 (defvar sh-other-keywords
|
|
472 '((bash eval sh-append bourne
|
|
473 "bye" "logout")
|
|
474
|
|
475 ;; The next entry is only used for defining the others
|
|
476 (bourne eval sh-append shell
|
|
477 "done" "esac" "fi" "for" "function" "in" "return")
|
|
478
|
|
479 (csh eval sh-append shell
|
|
480 "breaksw" "default" "end" "endif" "endsw" "foreach" "goto"
|
|
481 "if" "logout" "onintr" "repeat" "switch" "then" "while")
|
|
482
|
|
483 (es "break" "catch" "exec" "exit" "fn" "for" "forever" "fork" "if"
|
|
484 "return" "throw" "while")
|
|
485
|
|
486 (ksh88 eval sh-append bourne
|
|
487 "select")
|
|
488
|
|
489 (rc "break" "case" "exec" "exit" "fn" "for" "if" "in" "return" "switch"
|
|
490 "while")
|
|
491
|
|
492 ;; The next entry is only used for defining the others
|
|
493 (shell "break" "case" "continue" "exec" "exit")
|
|
494
|
|
495 (zsh eval sh-append bash
|
|
496 "select"))
|
|
497 "*List of keywords not in `sh-leading-keywords'.
|
|
498 See `sh-feature'.")
|
|
499
|
|
500
|
|
501
|
|
502 (defvar sh-variables
|
|
503 '((bash eval sh-append sh
|
|
504 "allow_null_glob_expansion" "auto_resume" "BASH" "BASH_VERSION"
|
|
505 "cdable_vars" "ENV" "EUID" "FCEDIT" "FIGNORE" "glob_dot_filenames"
|
|
506 "histchars" "HISTFILE" "HISTFILESIZE" "history_control" "HISTSIZE"
|
|
507 "hostname_completion_file" "HOSTTYPE" "IGNOREEOF" "ignoreeof"
|
|
508 "LINENO" "MAIL_WARNING" "noclobber" "nolinks" "notify"
|
|
509 "no_exit_on_failed_exec" "NO_PROMPT_VARS" "OLDPWD" "OPTERR" "PPID"
|
|
510 "PROMPT_COMMAND" "PS4" "pushd_silent" "PWD" "RANDOM" "REPLY"
|
|
511 "SECONDS" "SHLVL" "TMOUT" "UID")
|
|
512
|
|
513 (csh eval sh-append shell
|
|
514 "argv" "cdpath" "child" "echo" "histchars" "history" "home"
|
|
515 "ignoreeof" "mail" "noclobber" "noglob" "nonomatch" "path" "prompt"
|
|
516 "shell" "status" "time" "verbose")
|
|
517
|
|
518 (es eval sh-append shell
|
|
519 "apid" "cdpath" "CDPATH" "history" "home" "ifs" "noexport" "path"
|
|
520 "pid" "prompt" "signals")
|
|
521
|
|
522 (jcsh eval sh-append csh
|
|
523 "notify")
|
|
524
|
|
525 (ksh88 eval sh-append sh
|
|
526 "ENV" "ERRNO" "FCEDIT" "FPATH" "HISTFILE" "HISTSIZE" "LINENO"
|
|
527 "OLDPWD" "PPID" "PS3" "PS4" "PWD" "RANDOM" "REPLY" "SECONDS"
|
|
528 "TMOUT")
|
|
529
|
|
530 (oash eval sh-append sh
|
|
531 "FIELD" "FIELD_MAX" "LAST_KEY" "OALIB" "PP_ITEM" "PP_NUM")
|
|
532
|
|
533 (rc eval sh-append shell
|
|
534 "apid" "apids" "cdpath" "CDPATH" "history" "home" "ifs" "path" "pid"
|
|
535 "prompt" "status")
|
|
536
|
|
537 (sh eval sh-append shell
|
|
538 "CDPATH" "IFS" "OPTARG" "OPTIND" "PS1" "PS2")
|
|
539
|
|
540 ;; The next entry is only used for defining the others
|
|
541 (shell "COLUMNS" "EDITOR" "HOME" "HUSHLOGIN" "LANG" "LC_COLLATE"
|
|
542 "LC_CTYPE" "LC_MESSAGES" "LC_MONETARY" "LC_NUMERIC" "LC_TIME"
|
|
543 "LINES" "LOGNAME" "MAIL" "MAILCHECK" "MAILPATH" "PAGER" "PATH"
|
|
544 "SHELL" "TERM" "TERMCAP" "TERMINFO" "VISUAL")
|
|
545
|
|
546 (tcsh eval sh-append csh
|
|
547 "addsuffix" "ampm" "autocorrect" "autoexpand" "autolist"
|
|
548 "autologout" "chase_symlinks" "correct" "dextract" "edit" "el"
|
|
549 "fignore" "gid" "histlit" "HOST" "HOSTTYPE" "HPATH"
|
|
550 "ignore_symlinks" "listjobs" "listlinks" "listmax" "matchbeep"
|
|
551 "nobeep" "NOREBIND" "oid" "printexitvalue" "prompt2" "prompt3"
|
|
552 "pushdsilent" "pushdtohome" "recexact" "recognize_only_executables"
|
|
553 "rmstar" "savehist" "SHLVL" "showdots" "sl" "SYSTYPE" "tcsh" "term"
|
|
554 "tperiod" "tty" "uid" "version" "visiblebell" "watch" "who"
|
|
555 "wordchars")
|
|
556
|
|
557 (zsh eval sh-append ksh88
|
|
558 "BAUD" "bindcmds" "cdpath" "DIRSTACKSIZE" "fignore" "FIGNORE" "fpath"
|
|
559 "HISTCHARS" "hostcmds" "hosts" "HOSTS" "LISTMAX" "LITHISTSIZE"
|
|
560 "LOGCHECK" "mailpath" "manpath" "NULLCMD" "optcmds" "path" "POSTEDIT"
|
|
561 "prompt" "PROMPT" "PROMPT2" "PROMPT3" "PROMPT4" "psvar" "PSVAR"
|
|
562 "READNULLCMD" "REPORTTIME" "RPROMPT" "RPS1" "SAVEHIST" "SPROMPT"
|
|
563 "STTY" "TIMEFMT" "TMOUT" "TMPPREFIX" "varcmds" "watch" "WATCH"
|
|
564 "WATCHFMT" "WORDCHARS" "ZDOTDIR"))
|
|
565 "List of all shell variables available for completing read.
|
|
566 See `sh-feature'.")
|
|
567
|
|
568
|
|
569
|
|
570 (defvar sh-font-lock-keywords
|
|
571 '((csh eval sh-append shell
|
|
572 '("\\${?[#?]?\\([A-Za-z_][A-Za-z0-9_]*\\|0\\)" 1
|
|
573 font-lock-variable-name-face))
|
|
574
|
|
575 (es eval sh-append executable-font-lock-keywords
|
|
576 '("\\$#?\\([A-Za-z_][A-Za-z0-9_]*\\|[0-9]+\\)" 1
|
|
577 font-lock-variable-name-face))
|
|
578
|
|
579 (rc eval identity es)
|
|
580
|
|
581 (sh eval sh-append shell
|
|
582 '("\\$\\({#?\\)?\\([A-Za-z_][A-Za-z0-9_]*\\|[-#?@!]\\)" 2
|
|
583 font-lock-variable-name-face))
|
|
584
|
|
585 ;; The next entry is only used for defining the others
|
|
586 (shell eval sh-append executable-font-lock-keywords
|
|
587 '("\\\\[^A-Za-z0-9]" 0 font-lock-string-face)
|
|
588 '("\\${?\\([A-Za-z_][A-Za-z0-9_]*\\|[0-9]+\\|[$*_]\\)" 1
|
|
589 font-lock-variable-name-face)))
|
|
590 "*Rules for highlighting shell scripts. See `sh-feature'.")
|
|
591
|
|
592 (defvar sh-font-lock-keywords-1
|
|
593 '((sh "[ \t]in\\>"))
|
|
594 "*Additional rules for highlighting shell scripts. See `sh-feature'.")
|
|
595
|
|
596 (defvar sh-font-lock-keywords-2 ()
|
|
597 "*Yet more rules for highlighting shell scripts. See `sh-feature'.")
|
|
598
|
|
599 (defvar sh-font-lock-keywords-only t
|
|
600 "*Value of `font-lock-keywords-only' for highlighting shell scripts.
|
|
601 Default value is `t' because Emacs' syntax is not expressive enough to
|
|
602 detect that $# does not start a comment. Thus comments are fontified by
|
|
603 regexp which means that a single apostrophe in a comment turns everything
|
|
604 upto the next one or end of buffer into a string.")
|
|
605
|
|
606 ;; mode-command and utility functions
|
|
607
|
|
608 ;;;###autoload
|
|
609 (put 'sh-mode 'mode-class 'special)
|
|
610
|
|
611 ;;;###autoload
|
|
612 (defun sh-mode ()
|
|
613 "Major mode for editing shell scripts.
|
|
614 This mode works for many shells, since they all have roughly the same syntax,
|
|
615 as far as commands, arguments, variables, pipes, comments etc. are concerned.
|
|
616 Unless the file's magic number indicates the shell, your usual shell is
|
|
617 assumed. Since filenames rarely give a clue, they are not further analyzed.
|
|
618
|
|
619 This mode adapts to the variations between shells (see `sh-set-shell') by
|
|
620 means of an inheritance based feature lookup (see `sh-feature'). This
|
|
621 mechanism applies to all variables (including skeletons) that pertain to
|
|
622 shell-specific features.
|
|
623
|
|
624 The default style of this mode is that of Rosenblatt's Korn shell book.
|
|
625 The syntax of the statements varies with the shell being used. The
|
|
626 following commands are available, based on the current shell's syntax:
|
|
627
|
|
628 \\[sh-case] case statement
|
|
629 \\[sh-for] for loop
|
|
630 \\[sh-function] function definition
|
|
631 \\[sh-if] if statement
|
|
632 \\[sh-indexed-loop] indexed loop from 1 to n
|
|
633 \\[sh-while-getopts] while getopts loop
|
|
634 \\[sh-repeat] repeat loop
|
|
635 \\[sh-select] select loop
|
|
636 \\[sh-until] until loop
|
|
637 \\[sh-while] while loop
|
|
638
|
|
639 \\[backward-delete-char-untabify] Delete backward one position, even if it was a tab.
|
|
640 \\[sh-newline-and-indent] Delete unquoted space and indent new line same as this one.
|
|
641 \\[sh-end-of-command] Go to end of successive commands.
|
|
642 \\[sh-beginning-of-command] Go to beginning of successive commands.
|
|
643 \\[sh-set-shell] Set this buffer's shell, and maybe its magic number.
|
|
644 \\[sh-execute-region] Have optional header and region be executed in a subshell.
|
|
645
|
|
646 \\[sh-maybe-here-document] Without prefix, following an unquoted < inserts here document.
|
|
647 {, (, [, ', \", `
|
|
648 Unless quoted with \\, insert the pairs {}, (), [], or '', \"\", ``.
|
|
649
|
|
650 If you generally program a shell different from your login shell you can
|
|
651 set `sh-shell-file' accordingly. If your shell's file name doesn't correctly
|
|
652 indicate what shell it is use `sh-alias-alist' to translate.
|
|
653
|
|
654 If your shell gives error messages with line numbers, you can use \\[executable-interpret]
|
|
655 with your script for an edit-interpret-debug cycle."
|
|
656 (interactive)
|
|
657 (kill-all-local-variables)
|
|
658 (use-local-map sh-mode-map)
|
|
659 (make-local-variable 'indent-line-function)
|
|
660 (make-local-variable 'indent-region-function)
|
|
661 (make-local-variable 'skeleton-end-hook)
|
|
662 (make-local-variable 'paragraph-start)
|
|
663 (make-local-variable 'paragraph-separate)
|
|
664 (make-local-variable 'comment-start)
|
|
665 (make-local-variable 'comment-start-skip)
|
|
666 (make-local-variable 'require-final-newline)
|
|
667 (make-local-variable 'sh-header-marker)
|
|
668 (make-local-variable 'sh-shell-file)
|
|
669 (make-local-variable 'sh-shell)
|
|
670 (make-local-variable 'skeleton-pair-alist)
|
|
671 (make-local-variable 'skeleton-pair-filter)
|
|
672 (make-local-variable 'comint-dynamic-complete-functions)
|
|
673 (make-local-variable 'comint-prompt-regexp)
|
|
674 (make-local-variable 'font-lock-keywords)
|
|
675 (make-local-variable 'font-lock-defaults)
|
|
676 (make-local-variable 'skeleton-filter)
|
|
677 (make-local-variable 'skeleton-newline-indent-rigidly)
|
|
678 (make-local-variable 'sh-shell-variables)
|
|
679 (make-local-variable 'sh-shell-variables-initialized)
|
|
680 (setq major-mode 'sh-mode
|
|
681 mode-name "Shell-script"
|
|
682 indent-line-function 'sh-indent-line
|
|
683 ;; not very clever, but enables wrapping skeletons around regions
|
|
684 indent-region-function (lambda (b e)
|
|
685 (save-excursion
|
|
686 (goto-char b)
|
|
687 (skip-syntax-backward "-")
|
|
688 (setq b (point))
|
|
689 (goto-char e)
|
|
690 (skip-syntax-backward "-")
|
|
691 (indent-rigidly b (point) sh-indentation)))
|
|
692 skeleton-end-hook (lambda ()
|
|
693 (or (eolp) (newline) (indent-relative)))
|
|
694 paragraph-start (concat page-delimiter "\\|$")
|
|
695 paragraph-separate paragraph-start
|
|
696 comment-start "# "
|
|
697 comint-dynamic-complete-functions sh-dynamic-complete-functions
|
|
698 ;; we can't look if previous line ended with `\'
|
|
699 comint-prompt-regexp "^[ \t]*"
|
|
700 font-lock-defaults
|
|
701 `((sh-font-lock-keywords
|
|
702 sh-font-lock-keywords-1
|
|
703 sh-font-lock-keywords-2)
|
|
704 ,sh-font-lock-keywords-only
|
|
705 nil
|
|
706 ((?/ . "w") (?~ . "w") (?. . "w") (?- . "w") (?_ . "w")))
|
|
707 skeleton-pair-alist '((?` _ ?`))
|
|
708 skeleton-pair-filter 'sh-quoted-p
|
|
709 skeleton-further-elements '((< '(- (min sh-indentation
|
|
710 (current-column)))))
|
|
711 skeleton-filter 'sh-feature
|
|
712 skeleton-newline-indent-rigidly t)
|
|
713 (save-excursion
|
|
714 ;; parse or insert magic number for exec() and set all variables depending
|
|
715 ;; on the shell thus determined
|
|
716 (goto-char (point-min))
|
|
717 (and (zerop (buffer-size))
|
|
718 (not buffer-read-only)
|
|
719 (sh-set-shell sh-shell-file)))
|
|
720 (run-hooks 'sh-mode-hook))
|
|
721 ;;;###autoload
|
|
722 (defalias 'shell-script-mode 'sh-mode)
|
|
723
|
98
|
724 ;;; XEmacs
|
|
725 (put 'sh-mode 'font-lock-defaults
|
|
726 `((sh-font-lock-keywords
|
|
727 sh-font-lock-keywords-1
|
|
728 sh-font-lock-keywords-2)
|
|
729 ,sh-font-lock-keywords-only
|
|
730 nil
|
|
731 ((?/ . "w") (?~ . "w") (?. . "w") (?- . "w") (?_ . "w"))))
|
|
732
|
2
|
733
|
|
734 (defun sh-font-lock-keywords (&optional keywords)
|
|
735 "Function to get simple fontification based on `sh-font-lock-keywords'.
|
|
736 This adds rules for comments and assignments."
|
|
737 (sh-feature sh-font-lock-keywords
|
|
738 (lambda (list)
|
|
739 `((,(concat (sh-feature sh-comment-prefix) "\\(#.*\\)")
|
|
740 2 font-lock-comment-face t)
|
|
741 (,(sh-feature sh-assignment-regexp)
|
|
742 1 font-lock-variable-name-face)
|
|
743 ,@keywords
|
|
744 ,@list))))
|
|
745
|
|
746 (defun sh-font-lock-keywords-1 (&optional builtins)
|
|
747 "Function to get better fontification including keywords."
|
|
748 (let ((keywords (concat "\\([;(){}`|&]\\|^\\)[ \t]*\\(\\(\\("
|
|
749 (mapconcat 'identity
|
|
750 (sh-feature sh-leading-keywords)
|
|
751 "\\|")
|
|
752 "\\)[ \t]+\\)?\\("
|
|
753 (mapconcat 'identity
|
|
754 (append (sh-feature sh-leading-keywords)
|
|
755 (sh-feature sh-other-keywords))
|
|
756 "\\|")
|
|
757 "\\)")))
|
|
758 (sh-font-lock-keywords
|
|
759 `(,@(if builtins
|
|
760 `((,(concat keywords "[ \t]+\\)?\\("
|
|
761 (mapconcat 'identity (sh-feature sh-builtins) "\\|")
|
|
762 "\\)\\>")
|
|
763 (2 font-lock-keyword-face nil t)
|
|
764 (6 font-lock-function-name-face))
|
|
765 ,@(sh-feature sh-font-lock-keywords-2)))
|
|
766 (,(concat keywords "\\)\\>")
|
|
767 2 font-lock-keyword-face)
|
|
768 ,@(sh-feature sh-font-lock-keywords-1)))))
|
|
769
|
|
770 (defun sh-font-lock-keywords-2 ()
|
|
771 "Function to get better fontification including keywords and builtins."
|
|
772 (sh-font-lock-keywords-1 t))
|
|
773
|
|
774
|
|
775 (defun sh-set-shell (shell &optional no-query-flag insert-flag)
|
|
776 "Set this buffer's shell to SHELL (a string).
|
|
777 Makes this script executable via `executable-set-magic'.
|
|
778 Calls the value of `sh-set-shell-hook' if set."
|
|
779 (interactive (list (completing-read "Name or path of shell: "
|
98
|
780 ;; XEmacs change
|
|
781 sh-interpreter-mode-alist
|
2
|
782 (lambda (x) (eq (cdr x) 'sh-mode)))
|
|
783 (eq executable-query 'function)
|
|
784 t))
|
|
785 (setq sh-shell (intern (file-name-nondirectory shell))
|
|
786 sh-shell (or (cdr (assq sh-shell sh-alias-alist))
|
|
787 sh-shell))
|
|
788 (setq sh-shell-file (executable-set-magic shell (sh-feature sh-shell-arg)))
|
|
789 (setq require-final-newline (sh-feature sh-require-final-newline)
|
|
790 ;;; local-abbrev-table (sh-feature sh-abbrevs)
|
98
|
791 font-lock-defaults-computed nil
|
|
792 ;; Next two lines kill XEmacs
|
|
793 ;font-lock-keywords nil ; force resetting
|
|
794 ;font-lock-syntax-table nil
|
2
|
795 comment-start-skip (concat (sh-feature sh-comment-prefix) "#+[\t ]*")
|
|
796 mode-line-process (format "[%s]" sh-shell)
|
|
797 sh-shell-variables nil
|
|
798 sh-shell-variables-initialized nil
|
|
799 shell (sh-feature sh-variables))
|
|
800 (set-syntax-table (sh-feature sh-mode-syntax-table))
|
|
801 (while shell
|
|
802 (sh-remember-variable (car shell))
|
|
803 (setq shell (cdr shell)))
|
|
804 (and (boundp 'font-lock-mode)
|
|
805 font-lock-mode
|
98
|
806 ;; Gnu Emacs, doesn't work
|
2
|
807 (font-lock-mode (font-lock-mode 0)))
|
98
|
808 ;; (font-lock-fontify-buffer))
|
2
|
809 (run-hooks 'sh-set-shell-hook))
|
|
810
|
|
811
|
|
812
|
|
813 (defun sh-feature (list &optional function)
|
|
814 "Index ALIST by the current shell.
|
|
815 If ALIST isn't a list where every element is a cons, it is returned as is.
|
|
816 Else indexing follows an inheritance logic which works in two ways:
|
|
817
|
|
818 - Fall back on successive ancestors (see `sh-ancestor-alist') as long as
|
|
819 the alist contains no value for the current shell.
|
|
820
|
|
821 - If the value thus looked up is a list starting with `eval' its `cdr' is
|
|
822 first evaluated. If that is also a list and the first argument is a
|
|
823 symbol in ALIST it is not evaluated, but rather recursively looked up in
|
|
824 ALIST to allow the function called to define the value for one shell to be
|
|
825 derived from another shell. While calling the function, is the car of the
|
|
826 alist element is the current shell.
|
|
827 The value thus determined is physically replaced into the alist.
|
|
828
|
|
829 Optional FUNCTION is applied to the determined value and the result is cached
|
|
830 in ALIST."
|
|
831 (or (if (consp list)
|
|
832 (let ((l list))
|
|
833 (while (and l (consp (car l)))
|
|
834 (setq l (cdr l)))
|
|
835 (if l list)))
|
|
836 (if function
|
|
837 (cdr (assoc (setq function (cons sh-shell function)) list)))
|
|
838 (let ((sh-shell sh-shell)
|
|
839 elt val)
|
|
840 (while (and sh-shell
|
|
841 (not (setq elt (assq sh-shell list))))
|
|
842 (setq sh-shell (cdr (assq sh-shell sh-ancestor-alist))))
|
|
843 (if (and (consp (setq val (cdr elt)))
|
|
844 (eq (car val) 'eval))
|
|
845 (setcdr elt
|
|
846 (setq val
|
|
847 (eval (if (consp (setq val (cdr val)))
|
|
848 (let ((sh-shell (car (cdr val)))
|
|
849 function)
|
|
850 (if (assq sh-shell list)
|
|
851 (setcar (cdr val)
|
|
852 (list 'quote
|
|
853 (sh-feature list))))
|
|
854 val)
|
|
855 val)))))
|
|
856 (if function
|
|
857 (nconc list
|
|
858 (list (cons function
|
|
859 (setq sh-shell (car function)
|
|
860 val (funcall (cdr function) val))))))
|
|
861 val)))
|
|
862
|
|
863
|
|
864
|
|
865 ;;; I commented this out because nobody calls it -- rms.
|
|
866 ;;;(defun sh-abbrevs (ancestor &rest list)
|
|
867 ;;; "Iff it isn't, define the current shell as abbrev table and fill that.
|
|
868 ;;;Abbrev table will inherit all abbrevs from ANCESTOR, which is either an abbrev
|
|
869 ;;;table or a list of (NAME1 EXPANSION1 ...). In addition it will define abbrevs
|
|
870 ;;;according to the remaining arguments NAMEi EXPANSIONi ...
|
|
871 ;;;EXPANSION may be either a string or a skeleton command."
|
|
872 ;;; (or (if (boundp sh-shell)
|
|
873 ;;; (symbol-value sh-shell))
|
|
874 ;;; (progn
|
|
875 ;;; (if (listp ancestor)
|
|
876 ;;; (nconc list ancestor))
|
|
877 ;;; (define-abbrev-table sh-shell ())
|
|
878 ;;; (if (vectorp ancestor)
|
|
879 ;;; (mapatoms (lambda (atom)
|
|
880 ;;; (or (eq atom 0)
|
|
881 ;;; (define-abbrev (symbol-value sh-shell)
|
|
882 ;;; (symbol-name atom)
|
|
883 ;;; (symbol-value atom)
|
|
884 ;;; (symbol-function atom))))
|
|
885 ;;; ancestor))
|
|
886 ;;; (while list
|
|
887 ;;; (define-abbrev (symbol-value sh-shell)
|
|
888 ;;; (car list)
|
|
889 ;;; (if (stringp (car (cdr list)))
|
|
890 ;;; (car (cdr list))
|
|
891 ;;; "")
|
|
892 ;;; (if (symbolp (car (cdr list)))
|
|
893 ;;; (car (cdr list))))
|
|
894 ;;; (setq list (cdr (cdr list)))))
|
|
895 ;;; (symbol-value sh-shell)))
|
|
896
|
|
897
|
|
898 (defun sh-mode-syntax-table (table &rest list)
|
|
899 "Copy TABLE and set syntax for successive CHARs according to strings S."
|
|
900 (setq table (copy-syntax-table table))
|
|
901 (while list
|
|
902 (modify-syntax-entry (car list) (car (cdr list)) table)
|
|
903 (setq list (cdr (cdr list))))
|
|
904 table)
|
|
905
|
|
906
|
|
907 (defun sh-append (ancestor &rest list)
|
|
908 "Return list composed of first argument (a list) physically appended to rest."
|
|
909 (nconc list ancestor))
|
|
910
|
|
911
|
|
912 (defun sh-modify (skeleton &rest list)
|
|
913 "Modify a copy of SKELETON by replacing I1 with REPL1, I2 with REPL2 ..."
|
|
914 (setq skeleton (copy-sequence skeleton))
|
|
915 (while list
|
|
916 (setcar (or (nthcdr (car list) skeleton)
|
|
917 (error "Index %d out of bounds" (car list)))
|
|
918 (car (cdr list)))
|
|
919 (setq list (nthcdr 2 list)))
|
|
920 skeleton)
|
|
921
|
|
922
|
|
923 (defun sh-indent-line ()
|
|
924 "Indent as far as preceding non-empty line, then by steps of `sh-indentation'.
|
|
925 Lines containing only comments are considered empty."
|
|
926 (interactive)
|
|
927 (let ((previous (save-excursion
|
|
928 (while (and (not (bobp))
|
|
929 (progn
|
|
930 (forward-line -1)
|
|
931 (back-to-indentation)
|
|
932 (or (eolp)
|
|
933 (eq (following-char) ?#)))))
|
|
934 (current-column)))
|
|
935 current)
|
|
936 (save-excursion
|
|
937 (indent-to (if (eq this-command 'newline-and-indent)
|
|
938 previous
|
|
939 (if (< (current-column)
|
|
940 (setq current (progn (back-to-indentation)
|
|
941 (current-column))))
|
|
942 (if (eolp) previous 0)
|
|
943 (delete-region (point)
|
|
944 (progn (beginning-of-line) (point)))
|
|
945 (if (eolp)
|
|
946 (max previous (* (1+ (/ current sh-indentation))
|
|
947 sh-indentation))
|
|
948 (* (1+ (/ current sh-indentation)) sh-indentation))))))
|
|
949 (if (< (current-column) (current-indentation))
|
|
950 (skip-chars-forward " \t"))))
|
|
951
|
|
952
|
|
953 (defun sh-execute-region (start end &optional flag)
|
|
954 "Pass optional header and region to a subshell for noninteractive execution.
|
|
955 The working directory is that of the buffer, and only environment variables
|
|
956 are already set which is why you can mark a header within the script.
|
|
957
|
|
958 With a positive prefix ARG, instead of sending region, define header from
|
|
959 beginning of buffer to point. With a negative prefix ARG, instead of sending
|
|
960 region, clear header."
|
|
961 (interactive "r\nP")
|
|
962 (if flag
|
|
963 (setq sh-header-marker (if (> (prefix-numeric-value flag) 0)
|
|
964 (point-marker)))
|
|
965 (if sh-header-marker
|
|
966 (save-excursion
|
|
967 (let (buffer-undo-list)
|
|
968 (goto-char sh-header-marker)
|
|
969 (append-to-buffer (current-buffer) start end)
|
|
970 (shell-command-on-region (point-min)
|
|
971 (setq end (+ sh-header-marker
|
|
972 (- end start)))
|
|
973 sh-shell-file)
|
|
974 (delete-region sh-header-marker end)))
|
|
975 (shell-command-on-region start end (concat sh-shell-file " -")))))
|
|
976
|
|
977
|
|
978 (defun sh-remember-variable (var)
|
|
979 "Make VARIABLE available for future completing reads in this buffer."
|
|
980 (or (< (length var) sh-remember-variable-min)
|
|
981 (getenv var)
|
|
982 (assoc var sh-shell-variables)
|
|
983 (setq sh-shell-variables (cons (cons var var) sh-shell-variables)))
|
|
984 var)
|
|
985
|
|
986
|
|
987
|
|
988 (defun sh-quoted-p ()
|
|
989 "Is point preceded by an odd number of backslashes?"
|
|
990 (eq -1 (% (save-excursion (skip-chars-backward "\\\\")) 2)))
|
|
991
|
|
992 ;; statement syntax-commands for various shells
|
|
993
|
|
994 ;; You are welcome to add the syntax or even completely new statements as
|
|
995 ;; appropriate for your favorite shell.
|
|
996
|
|
997 (define-skeleton sh-case
|
|
998 "Insert a case/switch statement. See `sh-feature'."
|
|
999 (csh "expression: "
|
|
1000 "switch( " str " )" \n
|
|
1001 > "case " (read-string "pattern: ") ?: \n
|
|
1002 > _ \n
|
|
1003 "breaksw" \n
|
|
1004 ( "other pattern, %s: "
|
|
1005 < "case " str ?: \n
|
|
1006 > _ \n
|
|
1007 "breaksw" \n)
|
|
1008 < "default:" \n
|
|
1009 > _ \n
|
|
1010 resume:
|
|
1011 < < "endsw")
|
|
1012 (es)
|
|
1013 (rc "expression: "
|
|
1014 "switch( " str " ) {" \n
|
|
1015 > "case " (read-string "pattern: ") \n
|
|
1016 > _ \n
|
|
1017 ( "other pattern, %s: "
|
|
1018 < "case " str \n
|
|
1019 > _ \n)
|
|
1020 < "case *" \n
|
|
1021 > _ \n
|
|
1022 resume:
|
|
1023 < < ?})
|
|
1024 (sh "expression: "
|
|
1025 "case " str " in" \n
|
|
1026 > (read-string "pattern: ") ?\) \n
|
|
1027 > _ \n
|
|
1028 ";;" \n
|
|
1029 ( "other pattern, %s: "
|
|
1030 < str ?\) \n
|
|
1031 > _ \n
|
|
1032 ";;" \n)
|
|
1033 < "*)" \n
|
|
1034 > _ \n
|
|
1035 resume:
|
|
1036 < < "esac"))
|
|
1037 (put 'sh-case 'menu-enable '(sh-feature sh-case))
|
|
1038
|
|
1039
|
|
1040
|
|
1041 (define-skeleton sh-for
|
|
1042 "Insert a for loop. See `sh-feature'."
|
|
1043 (csh eval sh-modify sh
|
|
1044 1 "foreach "
|
|
1045 3 " ( "
|
|
1046 5 " )"
|
|
1047 15 "end")
|
|
1048 (es eval sh-modify rc
|
|
1049 3 " = ")
|
|
1050 (rc eval sh-modify sh
|
|
1051 1 "for( "
|
|
1052 5 " ) {"
|
|
1053 15 ?})
|
|
1054 (sh "Index variable: "
|
|
1055 "for " str " in " _ "; do" \n
|
|
1056 > _ | ?$ & (sh-remember-variable str) \n
|
|
1057 < "done"))
|
|
1058
|
|
1059
|
|
1060
|
|
1061 (define-skeleton sh-indexed-loop
|
|
1062 "Insert an indexed loop from 1 to n. See `sh-feature'."
|
|
1063 (bash eval identity posix)
|
|
1064 (csh "Index variable: "
|
|
1065 "@ " str " = 1" \n
|
|
1066 "while( $" str " <= " (read-string "upper limit: ") " )" \n
|
|
1067 > _ ?$ str \n
|
|
1068 "@ " str "++" \n
|
|
1069 < "end")
|
|
1070 (es eval sh-modify rc
|
|
1071 3 " =")
|
|
1072 (ksh88 "Index variable: "
|
|
1073 "integer " str "=0" \n
|
|
1074 "while (( ( " str " += 1 ) <= "
|
|
1075 (read-string "upper limit: ")
|
|
1076 " )); do" \n
|
|
1077 > _ ?$ (sh-remember-variable str) \n
|
|
1078 < "done")
|
|
1079 (posix "Index variable: "
|
|
1080 str "=1" \n
|
|
1081 "while [ $" str " -le "
|
|
1082 (read-string "upper limit: ")
|
|
1083 " ]; do" \n
|
|
1084 > _ ?$ str \n
|
|
1085 str ?= (sh-add (sh-remember-variable str) 1) \n
|
|
1086 < "done")
|
|
1087 (rc "Index variable: "
|
|
1088 "for( " str " in" " `{awk 'BEGIN { for( i=1; i<="
|
|
1089 (read-string "upper limit: ")
|
|
1090 "; i++ ) print i }'}) {" \n
|
|
1091 > _ ?$ (sh-remember-variable str) \n
|
|
1092 < ?})
|
|
1093 (sh "Index variable: "
|
|
1094 "for " str " in `awk 'BEGIN { for( i=1; i<="
|
|
1095 (read-string "upper limit: ")
|
|
1096 "; i++ ) print i }'`; do" \n
|
|
1097 > _ ?$ (sh-remember-variable str) \n
|
|
1098 < "done"))
|
|
1099
|
|
1100
|
|
1101 (defun sh-shell-initialize-variables ()
|
|
1102 "Scan the buffer for variable assignments.
|
|
1103 Add these variables to `sh-shell-variables'."
|
|
1104 (message "Scanning buffer `%s' for variable assignments..." (buffer-name))
|
|
1105 (save-excursion
|
|
1106 (goto-char (point-min))
|
|
1107 (setq sh-shell-variables-initialized t)
|
|
1108 (while (search-forward "=" nil t)
|
|
1109 (sh-assignment 0)))
|
|
1110 (message "Scanning buffer `%s' for variable assignments...done"
|
|
1111 (buffer-name)))
|
|
1112
|
|
1113 (defvar sh-add-buffer)
|
|
1114
|
|
1115 (defun sh-add-completer (string predicate code)
|
|
1116 "Do completion using `sh-shell-variables', but initialize it first.
|
|
1117 This function is designed for use as the \"completion table\",
|
|
1118 so it takes three arguments:
|
|
1119 STRING, the current buffer contents;
|
|
1120 PREDICATE, the predicate for filtering possible matches;
|
|
1121 CODE, which says what kind of things to do.
|
|
1122 CODE can be nil, t or `lambda'.
|
|
1123 nil means to return the best completion of STRING, or nil if there is none.
|
|
1124 t means to return a list of all possible completions of STRING.
|
|
1125 `lambda' means to return t if STRING is a valid completion as it stands."
|
|
1126 (let ((sh-shell-variables
|
|
1127 (save-excursion
|
|
1128 (set-buffer sh-add-buffer)
|
|
1129 (or sh-shell-variables-initialized
|
|
1130 (sh-shell-initialize-variables))
|
|
1131 (nconc (mapcar (lambda (var)
|
|
1132 (let ((name
|
|
1133 (substring var 0 (string-match "=" var))))
|
|
1134 (cons name name)))
|
|
1135 process-environment)
|
|
1136 sh-shell-variables))))
|
|
1137 (cond ((null code)
|
|
1138 (try-completion string sh-shell-variables predicate))
|
|
1139 ((eq code t)
|
|
1140 (all-completions string sh-shell-variables predicate))
|
|
1141 ((eq code 'lambda)
|
|
1142 (assoc string sh-shell-variables)))))
|
|
1143
|
|
1144 (defun sh-add (var delta)
|
|
1145 "Insert an addition of VAR and prefix DELTA for Bourne (type) shell."
|
|
1146 (interactive
|
|
1147 (let ((sh-add-buffer (current-buffer)))
|
|
1148 (list (completing-read "Variable: " 'sh-add-completer)
|
|
1149 (prefix-numeric-value current-prefix-arg))))
|
|
1150 (insert (sh-feature '((bash . "$[ ")
|
|
1151 (ksh88 . "$(( ")
|
|
1152 (posix . "$(( ")
|
|
1153 (rc . "`{expr $")
|
|
1154 (sh . "`expr $")
|
|
1155 (zsh . "$[ ")))
|
|
1156 (sh-remember-variable var)
|
|
1157 (if (< delta 0) " - " " + ")
|
|
1158 (number-to-string (abs delta))
|
|
1159 (sh-feature '((bash . " ]")
|
|
1160 (ksh88 . " ))")
|
|
1161 (posix . " ))")
|
|
1162 (rc . "}")
|
|
1163 (sh . "`")
|
|
1164 (zsh . " ]")))))
|
|
1165
|
|
1166
|
|
1167
|
|
1168 (define-skeleton sh-function
|
|
1169 "Insert a function definition. See `sh-feature'."
|
|
1170 (bash eval sh-modify ksh88
|
|
1171 3 "() {")
|
|
1172 (ksh88 "name: "
|
|
1173 "function " str " {" \n
|
|
1174 > _ \n
|
|
1175 < "}")
|
|
1176 (rc eval sh-modify ksh88
|
|
1177 1 "fn ")
|
|
1178 (sh ()
|
|
1179 "() {" \n
|
|
1180 > _ \n
|
|
1181 < "}"))
|
|
1182
|
|
1183
|
|
1184
|
|
1185 (define-skeleton sh-if
|
|
1186 "Insert an if statement. See `sh-feature'."
|
|
1187 (csh "condition: "
|
|
1188 "if( " str " ) then" \n
|
|
1189 > _ \n
|
|
1190 ( "other condition, %s: "
|
|
1191 < "else if( " str " ) then" \n
|
|
1192 > _ \n)
|
|
1193 < "else" \n
|
|
1194 > _ \n
|
|
1195 resume:
|
|
1196 < "endif")
|
|
1197 (es "condition: "
|
|
1198 "if { " str " } {" \n
|
|
1199 > _ \n
|
|
1200 ( "other condition, %s: "
|
|
1201 < "} { " str " } {" \n
|
|
1202 > _ \n)
|
|
1203 < "} {" \n
|
|
1204 > _ \n
|
|
1205 resume:
|
|
1206 < ?})
|
|
1207 (rc eval sh-modify csh
|
|
1208 3 " ) {"
|
|
1209 8 '( "other condition, %s: "
|
|
1210 < "} else if( " str " ) {" \n
|
|
1211 > _ \n)
|
|
1212 10 "} else {"
|
|
1213 17 ?})
|
|
1214 (sh "condition: "
|
|
1215 '(setq input (sh-feature sh-test))
|
|
1216 "if " str "; then" \n
|
|
1217 > _ \n
|
|
1218 ( "other condition, %s: "
|
|
1219 < "elif " str "; then" \n
|
|
1220 > _ \n)
|
|
1221 < "else" \n
|
|
1222 > _ \n
|
|
1223 resume:
|
|
1224 < "fi"))
|
|
1225
|
|
1226
|
|
1227
|
|
1228 (define-skeleton sh-repeat
|
|
1229 "Insert a repeat loop definition. See `sh-feature'."
|
|
1230 (es nil
|
|
1231 "forever {" \n
|
|
1232 > _ \n
|
|
1233 < ?})
|
|
1234 (zsh "factor: "
|
|
1235 "repeat " str "; do"\n
|
|
1236 > _ \n
|
|
1237 < "done"))
|
|
1238 (put 'sh-repeat 'menu-enable '(sh-feature sh-repeat))
|
|
1239
|
|
1240
|
|
1241
|
|
1242 (define-skeleton sh-select
|
|
1243 "Insert a select statement. See `sh-feature'."
|
|
1244 (ksh88 "Index variable: "
|
|
1245 "select " str " in " _ "; do" \n
|
|
1246 > ?$ str \n
|
|
1247 < "done"))
|
|
1248 (put 'sh-select 'menu-enable '(sh-feature sh-select))
|
|
1249
|
|
1250
|
|
1251
|
|
1252 (define-skeleton sh-tmp-file
|
|
1253 "Insert code to setup temporary file handling. See `sh-feature'."
|
|
1254 (bash eval identity ksh88)
|
|
1255 (csh (file-name-nondirectory (buffer-file-name))
|
|
1256 "set tmp = /tmp/" str ".$$" \n
|
|
1257 "onintr exit" \n _
|
|
1258 (and (goto-char (point-max))
|
|
1259 (not (bolp))
|
|
1260 ?\n)
|
|
1261 "exit:\n"
|
|
1262 "rm $tmp* >&/dev/null" >)
|
|
1263 (es (file-name-nondirectory (buffer-file-name))
|
|
1264 "local( signals = $signals sighup sigint; tmp = /tmp/" str ".$pid ) {" \n
|
|
1265 > "catch @ e {" \n
|
|
1266 > "rm $tmp^* >[2]/dev/null" \n
|
|
1267 "throw $e" \n
|
|
1268 < "} {" \n
|
|
1269 > _ \n
|
|
1270 < ?} \n
|
|
1271 < ?})
|
|
1272 (ksh88 eval sh-modify sh
|
|
1273 6 "EXIT")
|
|
1274 (rc (file-name-nondirectory (buffer-file-name))
|
|
1275 "tmp = /tmp/" str ".$pid" \n
|
|
1276 "fn sigexit { rm $tmp^* >[2]/dev/null }")
|
|
1277 (sh (file-name-nondirectory (buffer-file-name))
|
|
1278 "TMP=/tmp/" str ".$$" \n
|
|
1279 "trap \"rm $TMP* 2>/dev/null\" " ?0))
|
|
1280
|
|
1281
|
|
1282
|
|
1283 (define-skeleton sh-until
|
|
1284 "Insert an until loop. See `sh-feature'."
|
|
1285 (sh "condition: "
|
|
1286 '(setq input (sh-feature sh-test))
|
|
1287 "until " str "; do" \n
|
|
1288 > _ \n
|
|
1289 < "done"))
|
|
1290 (put 'sh-until 'menu-enable '(sh-feature sh-until))
|
|
1291
|
|
1292
|
|
1293
|
|
1294 (define-skeleton sh-while
|
|
1295 "Insert a while loop. See `sh-feature'."
|
|
1296 (csh eval sh-modify sh
|
|
1297 2 "while( "
|
|
1298 4 " )"
|
|
1299 10 "end")
|
|
1300 (es eval sh-modify rc
|
|
1301 2 "while { "
|
|
1302 4 " } {")
|
|
1303 (rc eval sh-modify csh
|
|
1304 4 " ) {"
|
|
1305 10 ?})
|
|
1306 (sh "condition: "
|
|
1307 '(setq input (sh-feature sh-test))
|
|
1308 "while " str "; do" \n
|
|
1309 > _ \n
|
|
1310 < "done"))
|
|
1311
|
|
1312
|
|
1313
|
|
1314 (define-skeleton sh-while-getopts
|
|
1315 "Insert a while getopts loop. See `sh-feature'.
|
|
1316 Prompts for an options string which consists of letters for each recognized
|
|
1317 option followed by a colon `:' if the option accepts an argument."
|
|
1318 (bash eval sh-modify sh
|
|
1319 18 "${0##*/}")
|
|
1320 (csh nil
|
|
1321 "while( 1 )" \n
|
|
1322 > "switch( \"$1\" )" \n
|
|
1323 '(setq input '("- x" . 2))
|
|
1324 > >
|
|
1325 ( "option, %s: "
|
|
1326 < "case " '(eval str)
|
|
1327 '(if (string-match " +" str)
|
|
1328 (setq v1 (substring str (match-end 0))
|
|
1329 str (substring str 0 (match-beginning 0)))
|
|
1330 (setq v1 nil))
|
|
1331 str ?: \n
|
|
1332 > "set " v1 & " = $2" | -4 & _ \n
|
|
1333 (if v1 "shift") & \n
|
|
1334 "breaksw" \n)
|
|
1335 < "case --:" \n
|
|
1336 > "shift" \n
|
|
1337 < "default:" \n
|
|
1338 > "break" \n
|
|
1339 resume:
|
|
1340 < < "endsw" \n
|
|
1341 "shift" \n
|
|
1342 < "end")
|
|
1343 (ksh88 eval sh-modify sh
|
|
1344 16 "print"
|
|
1345 18 "${0##*/}"
|
|
1346 36 "OPTIND-1")
|
|
1347 (posix eval sh-modify sh
|
|
1348 18 "$(basename $0)")
|
|
1349 (sh "optstring: "
|
|
1350 "while getopts :" str " OPT; do" \n
|
|
1351 > "case $OPT in" \n
|
|
1352 > >
|
|
1353 '(setq v1 (append (vconcat str) nil))
|
|
1354 ( (prog1 (if v1 (char-to-string (car v1)))
|
|
1355 (if (eq (nth 1 v1) ?:)
|
|
1356 (setq v1 (nthcdr 2 v1)
|
|
1357 v2 "\"$OPTARG\"")
|
|
1358 (setq v1 (cdr v1)
|
|
1359 v2 nil)))
|
|
1360 < str "|+" str ?\) \n
|
|
1361 > _ v2 \n
|
|
1362 ";;" \n)
|
|
1363 < "*)" \n
|
|
1364 > "echo" " \"usage: " "`basename $0`"
|
|
1365 " [+-" '(setq v1 (point)) str
|
|
1366 '(save-excursion
|
|
1367 (while (search-backward ":" v1 t)
|
|
1368 (replace-match " ARG] [+-" t t)))
|
|
1369 (if (eq (preceding-char) ?-) -5)
|
|
1370 "] [--] ARGS...\"" \n
|
|
1371 "exit 2" \n
|
|
1372 < < "esac" \n
|
|
1373 < "done" \n
|
|
1374 "shift " (sh-add "OPTIND" -1)))
|
|
1375 (put 'sh-while-getopts 'menu-enable '(sh-feature sh-while-getopts))
|
|
1376
|
|
1377
|
|
1378
|
|
1379 (defun sh-assignment (arg)
|
|
1380 "Remember preceding identifier for future completion and do self-insert."
|
|
1381 (interactive "p")
|
|
1382 (self-insert-command arg)
|
|
1383 (if (<= arg 1)
|
|
1384 (sh-remember-variable
|
|
1385 (save-excursion
|
|
1386 (if (re-search-forward (sh-feature sh-assignment-regexp)
|
|
1387 (prog1 (point)
|
|
1388 (beginning-of-line 1))
|
|
1389 t)
|
|
1390 (match-string 1))))))
|
|
1391
|
|
1392
|
|
1393
|
|
1394 (defun sh-maybe-here-document (arg)
|
|
1395 "Inserts self. Without prefix, following unquoted `<' inserts here document.
|
|
1396 The document is bounded by `sh-here-document-word'."
|
|
1397 (interactive "*P")
|
|
1398 (self-insert-command (prefix-numeric-value arg))
|
|
1399 (or arg
|
|
1400 (not (eq (char-after (- (point) 2)) last-command-char))
|
|
1401 (save-excursion
|
|
1402 (backward-char 2)
|
|
1403 (sh-quoted-p))
|
|
1404 (progn
|
|
1405 (insert sh-here-document-word)
|
|
1406 (or (eolp) (looking-at "[ \t]") (insert ? ))
|
|
1407 (end-of-line 1)
|
|
1408 (while
|
|
1409 (sh-quoted-p)
|
|
1410 (end-of-line 2))
|
|
1411 (newline)
|
|
1412 (save-excursion (insert ?\n sh-here-document-word)))))
|
|
1413
|
|
1414
|
|
1415 ;; various other commands
|
|
1416
|
|
1417 (autoload 'comint-dynamic-complete "comint"
|
|
1418 "Dynamically perform completion at point." t)
|
|
1419
|
|
1420 (autoload 'shell-dynamic-complete-command "shell"
|
|
1421 "Dynamically complete the command at point." t)
|
|
1422
|
|
1423 (autoload 'comint-dynamic-complete-filename "comint"
|
|
1424 "Dynamically complete the filename at point." t)
|
|
1425
|
|
1426 (autoload 'shell-dynamic-complete-environment-variable "shell"
|
|
1427 "Dynamically complete the environment variable at point." t)
|
|
1428
|
|
1429
|
|
1430
|
|
1431 (defun sh-newline-and-indent ()
|
|
1432 "Strip unquoted whitespace, insert newline, and indent like current line."
|
|
1433 (interactive "*")
|
|
1434 (indent-to (prog1 (current-indentation)
|
|
1435 (delete-region (point)
|
|
1436 (progn
|
|
1437 (or (zerop (skip-chars-backward " \t"))
|
|
1438 (if (sh-quoted-p)
|
|
1439 (forward-char)))
|
|
1440 (point)))
|
|
1441 (newline))))
|
|
1442
|
|
1443
|
|
1444
|
|
1445 (defun sh-beginning-of-command ()
|
|
1446 "Move point to successive beginnings of commands."
|
|
1447 (interactive)
|
|
1448 (if (re-search-backward sh-beginning-of-command nil t)
|
|
1449 (goto-char (match-beginning 2))))
|
|
1450
|
|
1451
|
|
1452 (defun sh-end-of-command ()
|
|
1453 "Move point to successive ends of commands."
|
|
1454 (interactive)
|
|
1455 (if (re-search-forward sh-end-of-command nil t)
|
|
1456 (goto-char (match-end 1))))
|
|
1457
|
|
1458 (provide 'sh-script)
|
|
1459 ;; sh-script.el ends here
|
|
1460
|