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