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