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