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