Mercurial > emacs
annotate lisp/progmodes/sh-script.el @ 9427:3181c9270f40
(mouse-select): Pass an arg to mouse-extend.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Tue, 11 Oct 1994 01:49:05 +0000 |
parents | 2cccfdee4f95 |
children | 255efa3fb8d0 |
rev | line source |
---|---|
6463 | 1 ;;; sh-script.el --- shell-script editing commands for Emacs |
2 ;; Copyright (C) 1993 Free Software Foundation, Inc. | |
3 | |
4 ;; Author: Daniel Pfeiffer, fax (+49 69) 75 88 529, c/o <bonhoure@cict.fr> | |
5 ;; Maintainer: FSF | |
6 ;; Keywords: shell programming | |
7 | |
8 ;; This file is part of GNU Emacs. | |
9 | |
10 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
11 ;; it under the terms of the GNU General Public License as published by | |
12 ;; the Free Software Foundation; either version 2, or (at your option) | |
13 ;; any later version. | |
14 | |
15 ;; GNU Emacs is distributed in the hope that it will be useful, | |
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 ;; GNU General Public License for more details. | |
19 | |
20 ;; You should have received a copy of the GNU General Public License | |
21 ;; along with GNU Emacs; see the file COPYING. If not, write to | |
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
23 | |
24 ;;; Commentary: | |
25 | |
26 ;; Major mode for editing shell scripts. Currently sh, ksh, bash and csh, | |
27 ;; tcsh are supported. Structured statements can be inserted with one | |
28 ;; command. | |
29 | |
8257
bbf6304598d9
"Comment out" autoload cookies.
Richard M. Stallman <rms@gnu.org>
parents:
8121
diff
changeset
|
30 ;; Autoloading of these functions is currently turned off |
bbf6304598d9
"Comment out" autoload cookies.
Richard M. Stallman <rms@gnu.org>
parents:
8121
diff
changeset
|
31 ;; because it's not clear whether this mode is really desirable to use. |
bbf6304598d9
"Comment out" autoload cookies.
Richard M. Stallman <rms@gnu.org>
parents:
8121
diff
changeset
|
32 ;; -- rms |
bbf6304598d9
"Comment out" autoload cookies.
Richard M. Stallman <rms@gnu.org>
parents:
8121
diff
changeset
|
33 |
6463 | 34 ;;; Code: |
35 | |
36 ;; page 1: variables and settings | |
37 ;; page 2: mode-command and utility functions | |
38 ;; page 3: statement syntax-commands for various shells | |
39 ;; page 4: various other commands | |
40 | |
41 | |
8257
bbf6304598d9
"Comment out" autoload cookies.
Richard M. Stallman <rms@gnu.org>
parents:
8121
diff
changeset
|
42 ;;;###dont-autoload |
6463 | 43 (setq auto-mode-alist |
44 ;; matches files | |
8428
68b00de2ac0f
(auto-mode-alist): Fix typos in comments.
Karl Heuer <kwzh@gnu.org>
parents:
8257
diff
changeset
|
45 ;; - whose path contains /bin/, but not directories |
6463 | 46 (cons '("/bin/" . sh-or-other-mode) |
47 ;; - that have a suffix .sh or .shar (shell archive) | |
8428
68b00de2ac0f
(auto-mode-alist): Fix typos in comments.
Karl Heuer <kwzh@gnu.org>
parents:
8257
diff
changeset
|
48 ;; - that contain resources for the various shells |
6463 | 49 ;; - startup files for X11 |
9059
23fe3c3fffe5
(auto-mode-alist): Use \' in auto-mode-alist regexps.
Richard M. Stallman <rms@gnu.org>
parents:
8428
diff
changeset
|
50 (cons '("\\.sh\\'\\|\\.shar\\'\\|/\\.\\(profile\\|bash_profile\\|login\\|bash_login\\|logout\\|bash_logout\\|bashrc\\|t?cshrc\\|xinitrc\\|startxrc\\|xsession\\)\\'" . sh-mode) |
6463 | 51 auto-mode-alist))) |
52 | |
53 | |
54 (defvar sh-mode-syntax-table | |
55 (let ((table (copy-syntax-table))) | |
56 (modify-syntax-entry ?\# "<" table) | |
57 (modify-syntax-entry ?\^l ">#" table) | |
58 (modify-syntax-entry ?\n ">#" table) | |
59 (modify-syntax-entry ?\" "\"\"" table) | |
60 (modify-syntax-entry ?\' "\"'" table) | |
61 (modify-syntax-entry ?\` "$`" table) | |
62 (modify-syntax-entry ?$ "_" table) | |
63 (modify-syntax-entry ?! "_" table) | |
64 (modify-syntax-entry ?% "_" table) | |
65 (modify-syntax-entry ?: "_" table) | |
66 (modify-syntax-entry ?. "_" table) | |
67 (modify-syntax-entry ?^ "_" table) | |
68 (modify-syntax-entry ?~ "_" table) | |
69 table) | |
70 "Syntax table in use in Shell-Script mode.") | |
71 | |
72 | |
73 | |
74 (defvar sh-use-prefix nil | |
75 "If non-nil when loading, `$' and `<' will be C-c $ and C-c < .") | |
76 | |
77 (defvar sh-mode-map | |
78 (let ((map (make-sparse-keymap))) | |
79 (define-key map "\C-c(" 'sh-function) | |
80 (define-key map "\C-c\C-w" 'sh-while) | |
81 (define-key map "\C-c\C-u" 'sh-until) | |
82 (define-key map "\C-c\C-s" 'sh-select) | |
83 (define-key map "\C-c\C-l" 'sh-indexed-loop) | |
84 (define-key map "\C-c\C-i" 'sh-if) | |
85 (define-key map "\C-c\C-f" 'sh-for) | |
86 (define-key map "\C-c\C-c" 'sh-case) | |
87 | |
88 (define-key map (if sh-use-prefix "\C-c$" "$") | |
89 'sh-query-for-variable) | |
90 (define-key map "=" 'sh-assignment) | |
91 (define-key map "\C-c+" 'sh-add) | |
92 (define-key map (if sh-use-prefix "\C-c<" "<") | |
93 'sh-maybe-here-document) | |
94 (define-key map "(" 'pair-insert-maybe) | |
95 (define-key map "{" 'pair-insert-maybe) | |
96 (define-key map "[" 'pair-insert-maybe) | |
97 (define-key map "'" 'pair-insert-maybe) | |
98 (define-key map "`" 'pair-insert-maybe) | |
99 (define-key map "\"" 'pair-insert-maybe) | |
100 | |
101 (define-key map "\t" 'sh-indent-line) | |
102 (substitute-key-definition 'complete-tag 'comint-dynamic-complete-filename | |
103 map (current-global-map)) | |
104 (substitute-key-definition 'newline-and-indent 'sh-newline-and-indent | |
105 map (current-global-map)) | |
106 ;; Now that tabs work properly, this might be unwanted. | |
107 (substitute-key-definition 'delete-backward-char | |
108 'backward-delete-char-untabify | |
109 map (current-global-map)) | |
110 (define-key map "\C-c:" 'sh-set-shell) | |
111 (substitute-key-definition 'beginning-of-defun | |
112 'sh-beginning-of-compound-command | |
113 map (current-global-map)) | |
114 (substitute-key-definition 'backward-sentence 'sh-beginning-of-command | |
115 map (current-global-map)) | |
116 (substitute-key-definition 'forward-sentence 'sh-end-of-command | |
117 map (current-global-map)) | |
118 (substitute-key-definition 'manual-entry 'sh-manual-entry | |
119 map (current-global-map)) | |
120 (define-key map [menu-bar insert] | |
121 (cons "Insert" (make-sparse-keymap "Insert"))) | |
122 (define-key map [menu-bar insert sh-while] | |
123 '("While loop" . sh-while)) | |
124 (define-key map [menu-bar insert sh-until] | |
125 '("Until loop" . sh-until)) | |
126 (define-key map [menu-bar insert sh-select] | |
127 '("Select statement" . sh-select)) | |
128 (define-key map [menu-bar insert sh-indexed-loop] | |
129 '("Indexed loop" . sh-indexed-loop)) | |
130 (define-key map [menu-bar insert sh-if] | |
131 '("If statement" . sh-if)) | |
132 (define-key map [menu-bar insert sh-for] | |
133 '("For loop" . sh-for)) | |
134 (define-key map [menu-bar insert sh-case] | |
135 '("Case statement" . sh-case)) | |
136 map) | |
137 "Keymap used in Shell-Script mode.") | |
138 | |
139 | |
140 | |
141 (defvar sh-find-file-modifies t | |
142 "*What to do when newly found file has no magic number: | |
143 nil do nothing | |
144 t insert magic number | |
145 other insert magic number, but mark as unmodified.") | |
146 | |
147 | |
148 (defvar sh-query-for-magic t | |
149 "*If non-nil, ask user before changing or inserting magic number.") | |
150 | |
151 | |
152 (defvar sh-magicless-file-regexp "/\\.[^/]+$" | |
153 "*On files with this kind of name no magic is inserted or changed.") | |
154 | |
155 | |
156 ;; someone who understands /etc/magic better than me should beef this up | |
157 ;; this currently covers only SCO Unix and Sinix executables | |
158 ;; the elegant way would be to read /etc/magic | |
159 (defvar magic-number-alist '(("L\^a\^h\\|\^?ELF" . hexl-mode) | |
160 ("#!.*perl" . perl-mode)) | |
161 "A regexp to match the magic number of a found file. | |
162 Currently this is only used by function `sh-or-other-mode'.") | |
163 | |
164 | |
165 (defvar sh-executable ".* is \\([^ \t]*\\)\n" | |
166 "*Regexp to match the output of sh builtin `type' command on your machine. | |
167 The regexp must match the whole output, and must contain a \\(something\\) | |
168 construct which matches the actual executable.") | |
169 | |
170 | |
171 | |
8121
cf243e9e63f8
(sh-chmod-argument): Change default.
Richard M. Stallman <rms@gnu.org>
parents:
8106
diff
changeset
|
172 (defvar sh-chmod-argument "+x" |
6463 | 173 "*After saving, if the file is not executable, set this mode. |
8121
cf243e9e63f8
(sh-chmod-argument): Change default.
Richard M. Stallman <rms@gnu.org>
parents:
8106
diff
changeset
|
174 The mode can be absolute, such as \"777\", or relative, such as \"+x\". |
cf243e9e63f8
(sh-chmod-argument): Change default.
Richard M. Stallman <rms@gnu.org>
parents:
8106
diff
changeset
|
175 Do nothing if this is nil.") |
6463 | 176 |
177 | |
178 (defvar sh-shell-path (or (getenv "SHELL") "/bin/sh") | |
179 "*The executable of the shell being programmed.") | |
180 | |
181 (defvar sh-shell-argument nil | |
182 "*A single argument for the magic number, or nil.") | |
183 | |
184 (defvar sh-shell nil | |
185 "The shell being programmed. This is set by \\[sh-set-shell].") | |
186 | |
187 (defvar sh-shell-is-csh nil | |
188 "The shell being programmed. This is set by \\[sh-set-shell].") | |
189 | |
190 (defvar sh-tab-width 4 | |
191 "The default value for `tab-width' in Shell-Script mode. | |
192 This is the width of tab stops after the indentation of the preceeding line.") | |
193 | |
194 (defvar sh-remember-variable-min 3 | |
195 "*Don't remember variables less than this length for completing reads.") | |
196 | |
197 | |
198 (defvar sh-beginning-of-command | |
199 "\\([;({`|&]\\|^\\)[ \t]*\\([/~:a-zA-Z0-9]\\)" | |
200 "*Regexp to determine the beginning of a shell command. | |
201 The actual command starts at the beginning of the second \\(grouping\\).") | |
202 | |
203 (defvar sh-end-of-command | |
204 "\\([/~:a-zA-Z0-9]\\)[ \t]*\\([;#)}`|&]\\|$\\)" | |
205 "*Regexp to determine the end of a shell command. | |
206 The actual command ends at the end of the first \\(grouping\\).") | |
207 | |
208 | |
209 | |
210 (defvar sh-assignment-space '(csh tcsh) | |
211 "List of shells that allow spaces around the assignment =.") | |
212 | |
213 (defvar sh-here-document-word "+" | |
214 "Word to delimit here documents.") | |
215 | |
216 | |
217 ;process-environment | |
218 (defvar sh-variables | |
219 '(("addsuffix" tcsh) ("allow_null_glob_expansion" bash) | |
220 ("ampm" tcsh) ("argv" csh tcsh) | |
221 ("autocorrect" tcsh) ("autoexpand" tcsh) | |
222 ("autolist" tcsh) ("autologout" tcsh) | |
223 ("auto_resume" bash) ("BASH" bash) | |
224 ("BASH_VERSION" bash) ("cdable_vars" bash) | |
225 ("cdpath" csh tcsh) ("CDPATH" sh ksh bash) | |
226 ("chase_symlinks" tcsh) ("child" csh tcsh) | |
227 ("COLUMNS" ksh tcsh) ("correct" tcsh) | |
228 ("dextract" tcsh) ("echo" csh tcsh) | |
229 ("edit" tcsh) ("EDITOR") | |
230 ("el" tcsh) ("ENV" ksh bash) | |
231 ("ERRNO" ksh) ("EUID" bash) | |
232 ("FCEDIT" ksh bash) ("FIGNORE" bash) | |
233 ("fignore" tcsh) ("FPATH" ksh) | |
234 ("gid" tcsh) ("glob_dot_filenames" bash) | |
235 ("histchars" bash csh tcsh) ("HISTFILE" ksh bash) | |
236 ("HISTFILESIZE" bash) ("histlit" tcsh) | |
237 ("history" csh tcsh) ("history_control" bash) | |
238 ("HISTSIZE" bash) ("home" csh tcsh) | |
239 ("HOME") ("HOST" tcsh) | |
240 ("hostname_completion_file" bash) ("HOSTTYPE" bash tcsh) | |
241 ("HPATH" tcsh) ("HUSHLOGIN") | |
242 ("IFS" sh ksh bash) ("ignoreeof" bash csh tcsh) | |
243 ("IGNOREEOF" bash) ("ignore_symlinks" tcsh) | |
244 ("LANG") ("LC_COLLATE") | |
245 ("LC_CTYPE") ("LC_MESSAGES") | |
246 ("LC_MONETARY") ("LC_NUMERIC") | |
247 ("LC_TIME") ("LINENO" ksh bash) | |
248 ("LINES" ksh tcsh) ("listjobs" tcsh) | |
249 ("listlinks" tcsh) ("listmax" tcsh) | |
250 ("LOGNAME") ("mail" csh tcsh) | |
251 ("MAIL") ("MAILCHECK") | |
252 ("MAILPATH") ("MAIL_WARNING" bash) | |
253 ("matchbeep" tcsh) ("nobeep" tcsh) | |
254 ("noclobber" bash csh tcsh) ("noglob" csh tcsh) | |
255 ("nolinks" bash) ("nonomatch" csh tcsh) | |
256 ("NOREBIND" tcsh) ("notify" bash) | |
257 ("no_exit_on_failed_exec" bash) ("NO_PROMPT_VARS" bash) | |
258 ("oid" tcsh) ("OLDPWD" ksh bash) | |
259 ("OPTARG" sh ksh bash) ("OPTERR" bash) | |
260 ("OPTIND" sh ksh bash) ("PAGER") | |
261 ("path" csh tcsh) ("PATH") | |
262 ("PPID" ksh bash) ("printexitvalue" tcsh) | |
263 ("prompt" csh tcsh) ("prompt2" tcsh) | |
264 ("prompt3" tcsh) ("PROMPT_COMMAND" bash) | |
265 ("PS1" sh ksh bash) ("PS2" sh ksh bash) | |
266 ("PS3" ksh) ("PS4" ksh bash) | |
267 ("pushdsilent" tcsh) ("pushdtohome" tcsh) | |
268 ("pushd_silent" bash) ("PWD" ksh bash) | |
269 ("RANDOM" ksh bash) ("recexact" tcsh) | |
270 ("recognize_only_executables" tcsh) ("REPLY" ksh bash) | |
271 ("rmstar" tcsh) ("savehist" tcsh) | |
272 ("SECONDS" ksh bash) ("shell" csh tcsh) | |
273 ("SHELL") ("SHLVL" bash tcsh) | |
274 ("showdots" tcsh) ("sl" tcsh) | |
275 ("status" csh tcsh) ("SYSTYPE" tcsh) | |
276 ("tcsh" tcsh) ("term" tcsh) | |
277 ("TERM") ("TERMCAP") | |
278 ("time" csh tcsh) ("TMOUT" ksh bash) | |
279 ("tperiod" tcsh) ("tty" tcsh) | |
280 ("UID" bash) ("uid" tcsh) | |
281 ("verbose" csh tcsh) ("version" tcsh) | |
282 ("visiblebell" tcsh) ("VISUAL") | |
283 ("watch" tcsh) ("who" tcsh) | |
284 ("wordchars" tcsh)) | |
285 "Alist of all environment and shell variables used for completing read. | |
286 Variables only understood by some shells are associated to a list of those.") | |
287 | |
288 | |
289 | |
9375
2cccfdee4f95
(sh-font-lock-keywords): Default to nil.
Richard M. Stallman <rms@gnu.org>
parents:
9059
diff
changeset
|
290 (defvar sh-font-lock-keywords nil |
2cccfdee4f95
(sh-font-lock-keywords): Default to nil.
Richard M. Stallman <rms@gnu.org>
parents:
9059
diff
changeset
|
291 ;; This is done syntactically: |
2cccfdee4f95
(sh-font-lock-keywords): Default to nil.
Richard M. Stallman <rms@gnu.org>
parents:
9059
diff
changeset
|
292 ;'(("[ \t]\\(#.*\\)" 1 font-lock-comment-face) |
2cccfdee4f95
(sh-font-lock-keywords): Default to nil.
Richard M. Stallman <rms@gnu.org>
parents:
9059
diff
changeset
|
293 ; ("\"[^`]*\"\\|'.*'\\|\\\\[^\nntc]" . font-lock-string-face)) |
6463 | 294 "*Rules for highlighting shell scripts. |
295 This variable is included into the various variables | |
296 `sh-SHELL-font-lock-keywords'. If no such variable exists for some shell, | |
297 this one is used.") | |
298 | |
299 | |
300 (defvar sh-sh-font-lock-keywords | |
301 (append sh-font-lock-keywords | |
302 '(("\\(^\\|[^-._a-z0-9]\\)\\(case\\|do\\|done\\|elif\\|else\\|esac\\|fi\\|for\\|if\\|in\\|then\\|until\\|while\\)\\($\\|[^-._a-z0-9]\\)" 2 font-lock-keyword-face t))) | |
303 "*Rules for highlighting Bourne shell scripts.") | |
304 | |
305 (defvar sh-ksh-font-lock-keywords | |
306 (append sh-sh-font-lock-keywords | |
307 '(("\\(^\\|[^-._a-z0-9]\\)\\(function\\|select\\)\\($\\|[^-._a-z0-9]\\)" 2 font-lock-keyword-face t))) | |
308 "*Rules for highlighting Korn shell scripts.") | |
309 | |
310 (defvar sh-bash-font-lock-keywords | |
311 (append sh-sh-font-lock-keywords | |
312 '(("\\(^\\|[^-._a-z0-9]\\)\\(function\\)\\($\\|[^-._a-z0-9]\\)" 2 font-lock-keyword-face t))) | |
313 "*Rules for highlighting Bourne again shell scripts.") | |
314 | |
315 | |
316 (defvar sh-csh-font-lock-keywords | |
317 (append sh-font-lock-keywords | |
318 '(("\\(^\\|[^-._a-z0-9]\\)\\(breaksw\\|case\\|default\\|else\\|end\\|endif\\|foreach\\|if\\|switch\\|then\\|while\\)\\($\\|[^-._a-z0-9]\\)" 2 font-lock-keyword-face t))) | |
319 "*Rules for highlighting C shell scripts.") | |
320 | |
321 (defvar sh-tcsh-font-lock-keywords sh-csh-font-lock-keywords | |
322 "*Rules for highlighting Toronto C shell scripts.") | |
323 | |
324 | |
325 | |
326 ;; mode-command and utility functions | |
327 | |
8257
bbf6304598d9
"Comment out" autoload cookies.
Richard M. Stallman <rms@gnu.org>
parents:
8121
diff
changeset
|
328 ;;;###dont-autoload |
6463 | 329 (defun sh-or-other-mode () |
330 "Decide whether this is a compiled executable or a script. | |
331 Usually the file-names of scripts and binaries cannot be automatically | |
332 distinguished, so the presence of an executable's magic number is used." | |
333 (funcall (or (let ((l magic-number-alist)) | |
334 (while (and l | |
335 (not (looking-at (car (car l))))) | |
336 (setq l (cdr l))) | |
337 (cdr (car l))) | |
338 'sh-mode))) | |
339 | |
340 | |
8257
bbf6304598d9
"Comment out" autoload cookies.
Richard M. Stallman <rms@gnu.org>
parents:
8121
diff
changeset
|
341 ;;;###dont-autoload |
6463 | 342 (defun sh-mode () |
343 "Major mode for editing shell scripts. | |
344 This mode works for many shells, since they all have roughly the same syntax, | |
345 as far as commands, arguments, variables, pipes, comments etc. are concerned. | |
346 Unless the file's magic number indicates the shell, your usual shell is | |
347 assumed. Since filenames rarely give a clue, they are not further analyzed. | |
348 | |
349 The syntax of the statements varies with the shell being used. The syntax of | |
350 statements can be modified by putting a property on the command or new ones | |
351 defined with `define-sh-skeleton'. For example | |
352 | |
353 (put 'sh-until 'ksh '(() \"until \" _ \\n > \"do\" \\n \"done\")) | |
354 or | |
355 (put 'sh-if 'smush '(\"What? \" \"If ya got ( \" str \" ) ya betta { \" _ \" }\")) | |
356 | |
357 where `sh-until' or `sh-if' have been or will be defined by `define-sh-skeleton'. | |
358 | |
359 The following commands are available, based on the current shell's syntax: | |
360 | |
361 \\[sh-case] case statement | |
362 \\[sh-for] for loop | |
363 \\[sh-function] function definition | |
364 \\[sh-if] if statement | |
365 \\[sh-indexed-loop] indexed loop from 1 to n | |
366 \\[sh-select] select statement | |
367 \\[sh-until] until loop | |
368 \\[sh-while] while loop | |
369 | |
370 \\[backward-delete-char-untabify] Delete backward one position, even if it was a tab. | |
371 \\[sh-newline-and-indent] Delete unquoted space and indent new line same as this one. | |
372 \\[sh-end-of-command] Go to end of successive commands. | |
373 \\[sh-beginning-of-command] Go to beginning of successive commands. | |
374 \\[sh-set-shell] Set this buffer's shell, and maybe its magic number. | |
375 \\[sh-manual-entry] Display the Unix manual entry for the current command or shell. | |
376 | |
377 \\[sh-query-for-variable] Unless quoted with \\, query for a variable with completions offered. | |
378 \\[sh-maybe-here-document] Without prefix, following an unquoted < inserts here document. | |
379 {, (, [, ', \", ` | |
380 Unless quoted with \\, insert the pairs {}, (), [], or '', \"\", ``." | |
381 (interactive) | |
382 (kill-all-local-variables) | |
383 (set-syntax-table sh-mode-syntax-table) | |
384 (use-local-map sh-mode-map) | |
385 (make-local-variable 'indent-line-function) | |
386 (make-local-variable 'comment-start) | |
387 (make-local-variable 'comment-start-skip) | |
388 (make-local-variable 'after-save-hook) | |
389 (make-local-variable 'require-final-newline) | |
390 (make-local-variable 'sh-shell-path) | |
391 (make-local-variable 'sh-shell) | |
392 (make-local-variable 'sh-shell-is-csh) | |
393 (make-local-variable 'pair-alist) | |
394 (make-local-variable 'pair-filter) | |
395 (make-local-variable 'font-lock-keywords) | |
396 (make-local-variable 'font-lock-keywords-case-fold-search) | |
397 (make-local-variable 'sh-variables) | |
398 (setq major-mode 'sh-mode | |
399 mode-name "Shell-script" | |
400 ;; Why can't Emacs have one standard function with some parameters? | |
401 ;; Only few modes actually analyse the previous line's contents | |
402 indent-line-function 'sh-indent-line | |
403 comment-start "# " | |
404 after-save-hook 'sh-chmod | |
405 tab-width sh-tab-width | |
406 ;; C shells do | |
407 require-final-newline t | |
408 font-lock-keywords-case-fold-search nil | |
409 pair-alist '((?` _ ?`)) | |
410 pair-filter 'sh-quoted-p) | |
8106
ec8291e58762
(sh-set-shell): Obey sh-query-for-magic in all cases.
Richard M. Stallman <rms@gnu.org>
parents:
6463
diff
changeset
|
411 ;; parse or insert magic number for exec |
ec8291e58762
(sh-set-shell): Obey sh-query-for-magic in all cases.
Richard M. Stallman <rms@gnu.org>
parents:
6463
diff
changeset
|
412 (save-excursion |
ec8291e58762
(sh-set-shell): Obey sh-query-for-magic in all cases.
Richard M. Stallman <rms@gnu.org>
parents:
6463
diff
changeset
|
413 (goto-char (point-min)) |
ec8291e58762
(sh-set-shell): Obey sh-query-for-magic in all cases.
Richard M. Stallman <rms@gnu.org>
parents:
6463
diff
changeset
|
414 (sh-set-shell |
ec8291e58762
(sh-set-shell): Obey sh-query-for-magic in all cases.
Richard M. Stallman <rms@gnu.org>
parents:
6463
diff
changeset
|
415 (if (looking-at "#![\t ]*\\([^\t\n ]+\\)") |
ec8291e58762
(sh-set-shell): Obey sh-query-for-magic in all cases.
Richard M. Stallman <rms@gnu.org>
parents:
6463
diff
changeset
|
416 (buffer-substring (match-beginning 1) (match-end 1)) |
ec8291e58762
(sh-set-shell): Obey sh-query-for-magic in all cases.
Richard M. Stallman <rms@gnu.org>
parents:
6463
diff
changeset
|
417 sh-shell-path))) |
6463 | 418 ;; find-file is set by `normal-mode' when called by `after-find-file' |
419 (and (boundp 'find-file) find-file | |
420 (or (eq sh-find-file-modifies t) | |
421 (set-buffer-modified-p nil))) | |
422 (run-hooks 'sh-mode-hook)) | |
8257
bbf6304598d9
"Comment out" autoload cookies.
Richard M. Stallman <rms@gnu.org>
parents:
8121
diff
changeset
|
423 ;;;###dont-autoload |
6463 | 424 (defalias 'shell-script-mode 'sh-mode) |
425 | |
426 | |
427 | |
428 (defmacro define-sh-skeleton (command documentation &rest definitions) | |
429 "Define COMMAND with [DOCSTRING] to insert statements as in DEFINITION ... | |
430 Prior definitions (e.g. from ~/.emacs) are maintained. | |
431 Each definition is built up as (SHELL PROMPT ELEMENT ...). Alternately | |
432 a synonym definition can be (SHELL . PREVIOUSLY-DEFINED-SHELL). | |
433 | |
434 For the meaning of (PROMPT ELEMENT ...) see `skeleton-insert'. | |
435 Each DEFINITION is actually stored as | |
436 (put COMMAND SHELL (PROMPT ELEMENT ...)), | |
437 which you can also do yourself." | |
438 (or (stringp documentation) | |
439 (setq definitions (cons documentation definitions) | |
440 documentation "")) | |
441 ;; The compiled version doesn't. | |
442 (require 'backquote) | |
443 (`(progn | |
444 (let ((definitions '(, definitions))) | |
445 (while definitions | |
446 ;; skeleton need not be loaded to define these | |
447 (or (and (not (if (boundp 'skeleton-debug) skeleton-debug)) | |
448 (get '(, command) (car (car definitions)))) | |
449 (put '(, command) (car (car definitions)) | |
450 (if (symbolp (cdr (car definitions))) | |
451 (get '(, command) (cdr (car definitions))) | |
452 (cdr (car definitions))))) | |
453 (setq definitions (cdr definitions)))) | |
454 (put '(, command) 'menu-enable '(get '(, command) sh-shell)) | |
455 (defun (, command) () | |
456 (, documentation) | |
457 (interactive) | |
458 (skeleton-insert | |
459 (or (get '(, command) sh-shell) | |
460 (error "%s statement syntax not defined for shell %s." | |
461 '(, command) sh-shell))))))) | |
462 | |
463 | |
464 | |
465 (defun sh-indent-line () | |
466 "Indent as far as preceding line, then by steps of `tab-width'. | |
467 If previous line starts with a comment, it's considered empty." | |
468 (interactive) | |
469 (let ((previous (save-excursion | |
470 (line-move -1) | |
471 (back-to-indentation) | |
472 (if (looking-at comment-start-skip) | |
473 0 | |
474 (current-column))))) | |
475 (save-excursion | |
476 (indent-to (if (eq this-command 'newline-and-indent) | |
477 previous | |
478 (if (< (current-column) | |
479 (progn (back-to-indentation) | |
480 (current-column))) | |
481 (if (eolp) previous 0) | |
482 (if (eolp) | |
483 (max previous (* (1+ (/ (current-column) tab-width)) | |
484 tab-width)) | |
485 (* (1+ (/ (current-column) tab-width)) tab-width)))))) | |
486 (if (< (current-column) (current-indentation)) | |
487 (skip-chars-forward " \t")))) | |
488 | |
489 | |
490 (defun sh-remember-variable (var) | |
491 "Make VARIABLE available for future completing reads in this buffer." | |
492 (or (< (length var) sh-remember-variable-min) | |
493 (assoc var sh-variables) | |
494 (setq sh-variables (cons (list var) sh-variables))) | |
495 var) | |
496 | |
497 | |
498 ;; Augment the standard variables by those found in the environment. | |
499 (if (boundp 'process-environment)(let ((l process-environment)) | |
500 (while l | |
501 (sh-remember-variable (substring (car l) | |
502 0 (string-match "=" (car l)))) | |
503 (setq l (cdr l))))) | |
504 | |
505 | |
506 | |
507 (defun sh-quoted-p () | |
508 "Is point preceded by an odd number of backslashes?" | |
509 (eq 1 (% (- (point) (save-excursion | |
510 (skip-chars-backward "\\\\") | |
511 (point))) | |
512 2))) | |
513 | |
514 | |
515 | |
516 (defun sh-executable (command) | |
517 "If COMMAND is an executable in $PATH its full name is returned. Else nil." | |
518 (let ((point (point)) | |
519 (buffer-modified-p (buffer-modified-p)) | |
520 buffer-read-only after-change-function) | |
521 (call-process "sh" nil t nil "-c" (concat "type " command)) | |
522 (setq point (prog1 (point) | |
523 (goto-char point))) | |
524 (prog1 | |
525 (and (looking-at sh-executable) | |
526 (eq point (match-end 0)) | |
527 (buffer-substring (match-beginning 1) (match-end 1))) | |
528 (delete-region (point) point) | |
529 (set-buffer-modified-p buffer-modified-p)))) | |
530 | |
531 | |
532 | |
533 (defun sh-chmod () | |
534 "This gets called after saving a file to assure that it be executable. | |
535 You can set the absolute or relative mode with `sh-chmod-argument'." | |
536 (if sh-chmod-argument | |
537 (or (file-executable-p buffer-file-name) | |
538 (shell-command (concat "chmod " sh-chmod-argument | |
539 " " buffer-file-name))))) | |
540 | |
541 ;; statement syntax-commands for various shells | |
542 | |
543 ;; You are welcome to add the syntax or even completely new statements as | |
544 ;; appropriate for your favorite shell. | |
545 | |
546 (define-sh-skeleton sh-case | |
547 "Insert a case/switch statement in the current shell's syntax." | |
548 (sh "expression: " | |
549 "case " str " in" \n | |
550 > (read-string "pattern: ") ?\) \n | |
551 > _ \n | |
552 ";;" \n | |
553 ( "other pattern, %s: " | |
554 < str ?\) \n | |
555 > \n | |
556 ";;" \n) | |
557 < "*)" \n | |
558 > \n | |
559 resume: | |
560 < < "esac") | |
561 (ksh . sh) | |
562 (bash . sh) | |
563 (csh "expression: " | |
564 "switch( " str " )" \n | |
565 > "case " (read-string "pattern: ") ?: \n | |
566 > _ \n | |
567 "breaksw" \n | |
568 ( "other pattern, %s: " | |
569 < "case " str ?: \n | |
570 > \n | |
571 "breaksw" \n) | |
572 < "default:" \n | |
573 > \n | |
574 resume: | |
575 < < "endsw") | |
576 (tcsh . csh)) | |
577 | |
578 | |
579 | |
580 (define-sh-skeleton sh-for | |
581 "Insert a for loop in the current shell's syntax." | |
582 (sh "Index variable: " | |
583 "for " str " in " _ "; do" \n | |
584 > ?$ (sh-remember-variable str) \n | |
585 < "done") | |
586 (ksh . sh) | |
587 (bash . sh) | |
588 (csh "Index variable: " | |
589 "foreach " str " ( " _ " )" \n | |
590 > ?$ (sh-remember-variable str) \n | |
591 < "end") | |
592 (tcsh . csh)) | |
593 | |
594 | |
595 | |
596 (define-sh-skeleton sh-indexed-loop | |
597 "Insert an indexed loop from 1 to n in the current shell's syntax." | |
598 (sh "Index variable: " | |
599 str "=1" \n | |
600 "while [ $" str " -le " | |
601 (read-string "upper limit: ") | |
602 " ]; do" \n | |
603 > _ ?$ str \n | |
604 str ?= (sh-add (sh-remember-variable str) 1) \n | |
605 < "done") | |
606 (ksh . sh) | |
607 (bash . sh) | |
608 (csh "Index variable: " | |
609 "@ " str " = 1" \n | |
610 "while( $" str " <= " | |
611 (read-string "upper limit: ") | |
612 " )" \n | |
613 > _ ?$ (sh-remember-variable str) \n | |
614 "@ " str "++" \n | |
615 < "end") | |
616 (tcsh . csh)) | |
617 | |
618 | |
619 | |
620 (defun sh-add (var delta) | |
621 "Insert an addition of VAR and prefix DELTA for Bourne type shells." | |
622 (interactive | |
623 (list (sh-remember-variable | |
624 (completing-read "Variable: " sh-variables | |
625 (lambda (element) | |
626 (or (not (cdr element)) | |
627 (memq sh-shell (cdr element)))))) | |
628 (prefix-numeric-value current-prefix-arg))) | |
629 (setq delta (concat (if (< delta 0) " - " " + ") | |
630 (abs delta))) | |
631 (skeleton-insert | |
632 (assq sh-shell | |
633 '((sh "`expr $" var delta "`") | |
634 (ksh "$(( $" var delta " ))") | |
635 (bash "$[ $" var delta " ]"))) | |
636 t)) | |
637 | |
638 | |
639 | |
640 (define-sh-skeleton sh-function | |
641 "Insert a function definition in the current shell's syntax." | |
642 (sh () | |
643 "() {" \n | |
644 > _ \n | |
645 < "}") | |
646 (ksh "name: " | |
647 "function " str " {" \n | |
648 > _ \n | |
649 < "}") | |
650 (bash "name: " | |
651 "function " str "() {" \n | |
652 > _ \n | |
653 < "}")) | |
654 | |
655 | |
656 | |
657 (define-sh-skeleton sh-if | |
658 "Insert an if statement in the current shell's syntax." | |
659 (sh "condition: " | |
660 "if [ " str " ]; then" \n | |
661 > _ \n | |
662 ( "other condition, %s: " | |
663 < "elif [ " str " ]; then" \n | |
664 > \n) | |
665 < "else" \n | |
666 > \n | |
667 resume: | |
668 < "fi") | |
669 (ksh . sh) | |
670 (bash . sh) | |
671 (csh "condition: " | |
672 "if( " str " ) then" \n | |
673 > _ \n | |
674 ( "other condition, %s: " | |
675 < "else if ( " str " ) then" \n | |
676 > \n) | |
677 < "else" \n | |
678 > \n | |
679 resume: | |
680 < "endif") | |
681 (tcsh . csh)) | |
682 | |
683 | |
684 | |
685 (define-sh-skeleton sh-select | |
686 "Insert a select statement in the current shell's syntax." | |
687 (ksh "Index variable: " | |
688 "select " str " in " _ "; do" \n | |
689 > ?$ str \n | |
690 < "done")) | |
691 (put 'sh-select 'menu-enable '(get 'sh-select sh-shell)) | |
692 | |
693 | |
694 | |
695 (define-sh-skeleton sh-until | |
696 "Insert an until loop in the current shell's syntax." | |
697 (sh "condition: " | |
698 "until [ " str " ]; do" \n | |
699 > _ \n | |
700 < "done") | |
701 (ksh . sh) | |
702 (bash . sh)) | |
703 (put 'sh-until 'menu-enable '(get 'sh-until sh-shell)) | |
704 | |
705 | |
706 (define-sh-skeleton sh-while | |
707 "Insert a while loop in the current shell's syntax." | |
708 (sh "condition: " | |
709 "while [ " str " ]; do" \n | |
710 > _ \n | |
711 < "done") | |
712 (ksh . sh) | |
713 (bash . sh) | |
714 (csh "condition: " | |
715 "while( " str " )" \n | |
716 > _ \n | |
717 < "end") | |
718 (tcsh . csh)) | |
719 | |
720 | |
721 | |
722 (defun sh-query-for-variable (arg) | |
723 "Unless quoted with `\\', query for variable-name with completions. | |
724 Prefix arg 0 means don't insert `$' before the variable. | |
725 Prefix arg 2 or more means only do self-insert that many times. | |
726 If { is pressed as the first character, it will surround the variable name." | |
727 (interactive "*p") | |
728 (or (prog1 (or (> arg 1) | |
729 (sh-quoted-p)) | |
730 (self-insert-command arg)) | |
731 (let (completion-ignore-case | |
732 (minibuffer-local-completion-map | |
733 (or (get 'sh-query-for-variable 'keymap) | |
734 (put 'sh-query-for-variable 'keymap | |
735 (copy-keymap minibuffer-local-completion-map)))) | |
736 (buffer (current-buffer))) | |
737 ;; local function that depends on `arg' and `buffer' | |
738 (define-key minibuffer-local-completion-map "{" | |
739 (lambda () (interactive) | |
740 (if (or arg (> (point) 1)) | |
741 (beep) | |
742 (save-window-excursion | |
743 (setq arg t) | |
744 (switch-to-buffer-other-window buffer) | |
745 (insert "{}"))))) | |
746 (insert | |
747 (prog1 | |
748 (sh-remember-variable | |
749 (completing-read "Variable: " sh-variables | |
750 (lambda (element) | |
751 (or (not (cdr element)) | |
752 (memq sh-shell (cdr element)))))) | |
753 (if (eq t arg) (forward-char 1)))) | |
754 (if (eq t arg) (forward-char 1))))) | |
755 | |
756 | |
757 | |
758 (defun sh-assignment (arg) | |
759 "Insert self. Remember previous identifier for future completing read." | |
760 (interactive "p") | |
761 (if (eq arg 1) | |
762 (sh-remember-variable | |
763 (save-excursion | |
764 (buffer-substring | |
765 (progn | |
766 (if (memq sh-shell sh-assignment-space) | |
767 (skip-chars-backward " \t")) | |
768 (point)) | |
769 (progn | |
770 (skip-chars-backward "a-zA-Z0-9_") | |
771 (point)))))) | |
772 (self-insert-command arg)) | |
773 | |
774 | |
775 | |
776 (defun sh-maybe-here-document (arg) | |
777 "Inserts self. Without prefix, following unquoted `<' inserts here document. | |
778 The document is bounded by `sh-here-document-word'." | |
779 (interactive "*P") | |
780 (self-insert-command (prefix-numeric-value arg)) | |
781 (or arg | |
782 (not (eq (char-after (- (point) 2)) last-command-char)) | |
783 (save-excursion | |
784 (goto-char (- (point) 2)) | |
785 (sh-quoted-p)) | |
786 (progn | |
787 (insert sh-here-document-word) | |
788 (or (looking-at "[ \t\n]") (insert ? )) | |
789 (end-of-line 1) | |
790 (newline) | |
791 (save-excursion (insert ?\n sh-here-document-word))))) | |
792 | |
793 | |
794 ;; various other commands | |
795 | |
796 (autoload 'comint-dynamic-complete-filename "comint" | |
797 "Dynamically complete the filename at point." t) | |
798 | |
799 | |
800 | |
801 (defun sh-newline-and-indent (&optional arg) | |
802 "Strip unquoted whitespace, insert newline, and indent like current line. | |
803 Unquoted whitespace is stripped from the current line's end, unless a | |
804 prefix ARG is given." | |
805 (interactive "*P") | |
806 (let ((previous (current-indentation)) | |
807 (end-of-line (point))) | |
808 (if arg () | |
809 (skip-chars-backward " \t") | |
810 (and (< (point) end-of-line) | |
811 (sh-quoted-p) | |
812 (forward-char 1)) | |
813 (delete-region (point) end-of-line)) | |
814 (newline) | |
815 (indent-to previous))) | |
816 | |
817 | |
818 | |
819 (defun sh-set-shell (shell) | |
820 "Set this buffer's shell to SHELL (a string). | |
821 Calls the value of `sh-set-shell-hook' if set." | |
822 (interactive "sName or path of shell: ") | |
823 (save-excursion | |
824 (goto-char (point-min)) | |
825 (setq sh-shell-path (if (file-name-absolute-p shell) | |
826 shell | |
827 (or (sh-executable shell) | |
828 (error "Cannot find %s." shell))) | |
829 sh-shell (intern (file-name-nondirectory sh-shell-path)) | |
830 sh-shell-is-csh (memq sh-shell '(csh tcsh)) | |
831 font-lock-keywords | |
832 (intern-soft (format "sh-%s-font-lock-keywords" sh-shell)) | |
833 font-lock-keywords (if (and font-lock-keywords | |
834 (boundp font-lock-keywords)) | |
835 (symbol-value font-lock-keywords) | |
836 sh-font-lock-keywords) | |
837 comment-start-skip (if sh-shell-is-csh | |
838 "\\(^\\|[^$]\\|\\$[^{]\\)#+[\t ]*" | |
839 "\\(^\\|[^$]\\|\\$[^{]\\)\\B#+[\t ]*") | |
840 mode-line-process (format ": %s" sh-shell) | |
841 shell (concat sh-shell-path | |
842 (and sh-shell-argument " ") | |
843 sh-shell-argument)) | |
844 (and (not buffer-read-only) | |
845 (not (if buffer-file-name | |
846 (string-match sh-magicless-file-regexp buffer-file-name))) | |
847 ;; find-file is set by `normal-mode' when called by `after-find-file' | |
848 (if (and (boundp 'find-file) find-file) sh-find-file-modifies t) | |
849 (if (looking-at "#!") | |
850 (and (skip-chars-forward "#! \t") | |
851 (not (string= shell | |
852 (buffer-substring (point) | |
853 (save-excursion (end-of-line) | |
854 (point))))) | |
855 (if sh-query-for-magic | |
856 (y-or-n-p (concat "Replace magic number by ``#! " | |
857 shell "''? ")) | |
858 (message "Magic number ``%s'' replaced." | |
859 (buffer-substring (point-min) (point)))) | |
860 (not (delete-region (point) (progn (end-of-line) (point)))) | |
861 (insert shell)) | |
8106
ec8291e58762
(sh-set-shell): Obey sh-query-for-magic in all cases.
Richard M. Stallman <rms@gnu.org>
parents:
6463
diff
changeset
|
862 (if (if sh-query-for-magic |
ec8291e58762
(sh-set-shell): Obey sh-query-for-magic in all cases.
Richard M. Stallman <rms@gnu.org>
parents:
6463
diff
changeset
|
863 (y-or-n-p (concat "Add ``#! " shell "''? ")) |
ec8291e58762
(sh-set-shell): Obey sh-query-for-magic in all cases.
Richard M. Stallman <rms@gnu.org>
parents:
6463
diff
changeset
|
864 t) |
ec8291e58762
(sh-set-shell): Obey sh-query-for-magic in all cases.
Richard M. Stallman <rms@gnu.org>
parents:
6463
diff
changeset
|
865 (insert "#! " shell ?\n))))) |
6463 | 866 (run-hooks 'sh-set-shell-hook)) |
867 | |
868 | |
869 | |
870 (defun sh-beginning-of-command () | |
871 "Move point to successive beginnings of commands." | |
872 (interactive) | |
873 (if (re-search-backward sh-beginning-of-command nil t) | |
874 (goto-char (match-beginning 2)))) | |
875 | |
876 | |
877 | |
878 (defun sh-end-of-command () | |
879 "Move point to successive ends of commands." | |
880 (interactive) | |
881 (if (re-search-forward sh-end-of-command nil t) | |
882 (goto-char (match-end 1)))) | |
883 | |
884 | |
885 | |
886 (defun sh-manual-entry (arg) | |
887 "Display the Unix manual entry for the current command or shell. | |
888 Universal argument ARG, is passed to `Man-getpage-in-background'." | |
889 (interactive "P") | |
890 (let ((command (save-excursion | |
891 (sh-beginning-of-command) | |
892 (sh-executable | |
893 (buffer-substring (point) | |
894 (progn (forward-sexp) (point))))))) | |
895 (setq command (read-input (concat "Manual entry (default " | |
896 (symbol-name sh-shell) | |
897 "): ") | |
898 (if command | |
899 (file-name-nondirectory command)))) | |
900 (manual-entry (if (string= command "") | |
901 (symbol-name sh-shell) | |
902 command) | |
903 arg))) | |
904 | |
905 ;; sh-script.el ends here |