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