Commit | Line | Data |
---|---|---|
ac59aed8 | 1 | ;;; sh-script.el --- shell-script editing commands for Emacs |
b578f267 | 2 | |
c4f6e489 GM |
3 | ;; Copyright (C) 1993, 1994, 1995, 1996, 1997, 1999, 2001, 2002, 2003, |
4 | ;; 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc. | |
ac59aed8 | 5 | |
3e910376 | 6 | ;; Author: Daniel Pfeiffer <occitan@esperanto.org> |
f964dfcb | 7 | ;; Version: 2.0f |
ac59aed8 | 8 | ;; Maintainer: FSF |
133693bc | 9 | ;; Keywords: languages, unix |
ac59aed8 RS |
10 | |
11 | ;; This file is part of GNU Emacs. | |
12 | ||
b1fc2b50 | 13 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
ac59aed8 | 14 | ;; it under the terms of the GNU General Public License as published by |
b1fc2b50 GM |
15 | ;; the Free Software Foundation, either version 3 of the License, or |
16 | ;; (at your option) any later version. | |
ac59aed8 RS |
17 | |
18 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
19 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | ;; GNU General Public License for more details. | |
22 | ||
23 | ;; You should have received a copy of the GNU General Public License | |
b1fc2b50 | 24 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
ac59aed8 RS |
25 | |
26 | ;;; Commentary: | |
27 | ||
133693bc KH |
28 | ;; Major mode for editing shell scripts. Bourne, C and rc shells as well |
29 | ;; as various derivatives are supported and easily derived from. Structured | |
30 | ;; statements can be inserted with one command or abbrev. Completion is | |
31 | ;; available for filenames, variables known from the script, the shell and | |
32 | ;; the environment as well as commands. | |
ac59aed8 | 33 | |
133693bc KH |
34 | ;;; Known Bugs: |
35 | ||
bfc8e97b | 36 | ;; - In Bourne the keyword `in' is not anchored to case, for, select ... |
133693bc KH |
37 | ;; - Variables in `"' strings aren't fontified because there's no way of |
38 | ;; syntactically distinguishing those from `'' strings. | |
e932f2d2 | 39 | |
f964dfcb GM |
40 | ;; Indentation |
41 | ;; =========== | |
42 | ;; Indentation for rc and es modes is very limited, but for Bourne shells | |
43 | ;; and its derivatives it is quite customizable. | |
035107fa | 44 | ;; |
f964dfcb GM |
45 | ;; The following description applies to sh and derived shells (bash, |
46 | ;; zsh, ...). | |
035107fa | 47 | ;; |
f964dfcb GM |
48 | ;; There are various customization variables which allow tailoring to |
49 | ;; a wide variety of styles. Most of these variables are named | |
50 | ;; sh-indent-for-XXX and sh-indent-after-XXX. For example. | |
51 | ;; sh-indent-after-if controls the indenting of a line following | |
8db2b9fb | 52 | ;; an if statement, and sh-indent-for-fi controls the indentation |
f964dfcb | 53 | ;; of the line containing the fi. |
035107fa | 54 | ;; |
f964dfcb GM |
55 | ;; You can set each to a numeric value, but it is often more convenient |
56 | ;; to a symbol such as `+' which uses the value of variable `sh-basic-offset'. | |
57 | ;; By changing this one variable you can increase or decrease how much | |
58 | ;; indentation there is. Valid symbols: | |
035107fa | 59 | ;; |
f964dfcb GM |
60 | ;; + Indent right by sh-basic-offset |
61 | ;; - Indent left by sh-basic-offset | |
62 | ;; ++ Indent right twice sh-basic-offset | |
63 | ;; -- Indent left twice sh-basic-offset | |
64 | ;; * Indent right half sh-basic-offset | |
65 | ;; / Indent left half sh-basic-offset. | |
035107fa | 66 | ;; |
f964dfcb | 67 | ;; There are 4 commands to help set the indentation variables: |
035107fa | 68 | ;; |
f964dfcb GM |
69 | ;; `sh-show-indent' |
70 | ;; This shows what variable controls the indentation of the current | |
71 | ;; line and its value. | |
035107fa | 72 | ;; |
f964dfcb GM |
73 | ;; `sh-set-indent' |
74 | ;; This allows you to set the value of the variable controlling the | |
75 | ;; current line's indentation. You can enter a number or one of a | |
76 | ;; number of special symbols to denote the value of sh-basic-offset, | |
77 | ;; or its negative, or half it, or twice it, etc. If you've used | |
78 | ;; cc-mode this should be familiar. If you forget which symbols are | |
79 | ;; valid simply press C-h at the prompt. | |
035107fa | 80 | ;; |
f964dfcb GM |
81 | ;; `sh-learn-line-indent' |
82 | ;; Simply make the line look the way you want it, then invoke this | |
83 | ;; command. It will set the variable to the value that makes the line | |
84 | ;; indent like that. If called with a prefix argument then it will set | |
85 | ;; the value to one of the symbols if applicable. | |
035107fa | 86 | ;; |
f964dfcb GM |
87 | ;; `sh-learn-buffer-indent' |
88 | ;; This is the deluxe function! It "learns" the whole buffer (use | |
89 | ;; narrowing if you want it to process only part). It outputs to a | |
90 | ;; buffer *indent* any conflicts it finds, and all the variables it has | |
91 | ;; learned. This buffer is a sort of Occur mode buffer, allowing you to | |
92 | ;; easily find where something was set. It is popped to automatically | |
93 | ;; if there are any conflicts found or if `sh-popup-occur-buffer' is | |
94 | ;; non-nil. | |
95 | ;; `sh-indent-comment' will be set if all comments follow the same | |
96 | ;; pattern; if they don't it will be set to nil. | |
97 | ;; Whether `sh-basic-offset' is set is determined by variable | |
98 | ;; `sh-learn-basic-offset'. | |
035107fa | 99 | ;; |
f964dfcb GM |
100 | ;; Unfortunately, `sh-learn-buffer-indent' can take a long time to run |
101 | ;; (e.g. if there are large case statements). Perhaps it does not make | |
102 | ;; sense to run it on large buffers: if lots of lines have different | |
103 | ;; indentation styles it will produce a lot of diagnostics in the | |
104 | ;; *indent* buffer; if there is a consistent style then running | |
105 | ;; `sh-learn-buffer-indent' on a small region of the buffer should | |
106 | ;; suffice. | |
035107fa | 107 | ;; |
f964dfcb GM |
108 | ;; Saving indentation values |
109 | ;; ------------------------- | |
110 | ;; After you've learned the values in a buffer, how to you remember | |
111 | ;; them? Originally I had hoped that `sh-learn-buffer-indent' | |
112 | ;; would make this unnecessary; simply learn the values when you visit | |
113 | ;; the buffer. | |
114 | ;; You can do this automatically like this: | |
6c5bcbc1 | 115 | ;; (add-hook 'sh-set-shell-hook 'sh-learn-buffer-indent) |
035107fa | 116 | ;; |
4a9592f6 | 117 | ;; However... `sh-learn-buffer-indent' is extremely slow, |
8db2b9fb | 118 | ;; especially on large-ish buffer. Also, if there are conflicts the |
f964dfcb | 119 | ;; "last one wins" which may not produce the desired setting. |
035107fa | 120 | ;; |
f964dfcb GM |
121 | ;; So...There is a minimal way of being able to save indentation values and |
122 | ;; to reload them in another buffer or at another point in time. | |
035107fa | 123 | ;; |
f964dfcb GM |
124 | ;; Use `sh-name-style' to give a name to the indentation settings of |
125 | ;; the current buffer. | |
126 | ;; Use `sh-load-style' to load indentation settings for the current | |
127 | ;; buffer from a specific style. | |
128 | ;; Use `sh-save-styles-to-buffer' to write all the styles to a buffer | |
129 | ;; in lisp code. You can then store it in a file and later use | |
130 | ;; `load-file' to load it. | |
035107fa | 131 | ;; |
f964dfcb GM |
132 | ;; Indentation variables - buffer local or global? |
133 | ;; ---------------------------------------------- | |
134 | ;; I think that often having them buffer-local makes sense, | |
135 | ;; especially if one is using `sh-learn-buffer-indent'. However, if | |
8db2b9fb | 136 | ;; a user sets values using customization, these changes won't appear |
f964dfcb | 137 | ;; to work if the variables are already local! |
035107fa | 138 | ;; |
8db2b9fb | 139 | ;; To get round this, there is a variable `sh-make-vars-local' and 2 |
f964dfcb | 140 | ;; functions: `sh-make-vars-local' and `sh-reset-indent-vars-to-global-values'. |
035107fa | 141 | ;; |
8db2b9fb | 142 | ;; If `sh-make-vars-local' is non-nil, then these variables become |
f964dfcb | 143 | ;; buffer local when the mode is established. |
8db2b9fb | 144 | ;; If this is nil, then the variables are global. At any time you |
f964dfcb | 145 | ;; can make them local with the command `sh-make-vars-local'. |
8db2b9fb | 146 | ;; Conversely, to update with the global values you can use the |
f964dfcb | 147 | ;; command `sh-reset-indent-vars-to-global-values'. |
035107fa | 148 | ;; |
8db2b9fb | 149 | ;; This may be awkward, but the intent is to cover all cases. |
035107fa | 150 | ;; |
f964dfcb GM |
151 | ;; Awkward things, pitfalls |
152 | ;; ------------------------ | |
153 | ;; Indentation for a sh script is complicated for a number of reasons: | |
035107fa | 154 | ;; |
8db2b9fb | 155 | ;; 1. You can't format by simply looking at symbols, you need to look |
f964dfcb GM |
156 | ;; at keywords. [This is not the case for rc and es shells.] |
157 | ;; 2. The character ")" is used both as a matched pair "(" ... ")" and | |
158 | ;; as a stand-alone symbol (in a case alternative). This makes | |
159 | ;; things quite tricky! | |
8db2b9fb | 160 | ;; 3. Here-documents in a script should be treated "as is", and when |
f964dfcb GM |
161 | ;; they terminate we want to revert to the indentation of the line |
162 | ;; containing the "<<" symbol. | |
163 | ;; 4. A line may be continued using the "\". | |
164 | ;; 5. The character "#" (outside a string) normally starts a comment, | |
165 | ;; but it doesn't in the sequence "$#"! | |
035107fa | 166 | ;; |
f964dfcb | 167 | ;; To try and address points 2 3 and 5 I used a feature that cperl mode |
8db2b9fb | 168 | ;; uses, that of a text's syntax property. This, however, has 2 |
f964dfcb GM |
169 | ;; disadvantages: |
170 | ;; 1. We need to scan the buffer to find which ")" symbols belong to a | |
171 | ;; case alternative, to find any here documents, and handle "$#". | |
035107fa | 172 | ;; |
f964dfcb GM |
173 | ;; Bugs |
174 | ;; ---- | |
f964dfcb GM |
175 | ;; - Indenting many lines is slow. It currently does each line |
176 | ;; independently, rather than saving state information. | |
035107fa | 177 | ;; |
f964dfcb | 178 | ;; - `sh-learn-buffer-indent' is extremely slow. |
035107fa | 179 | ;; |
74ab576e SM |
180 | ;; - "case $x in y) echo ;; esac)" the last ) is mis-identified as being |
181 | ;; part of a case-pattern. You need to add a semi-colon after "esac" to | |
182 | ;; coerce sh-script into doing the right thing. | |
183 | ;; | |
184 | ;; - "echo $z in ps | head)" the last ) is mis-identified as being part of | |
185 | ;; a case-pattern. You need to put the "in" between quotes to coerce | |
186 | ;; sh-script into doing the right thing. | |
187 | ;; | |
188 | ;; - A line starting with "}>foo" is not indented like "} >foo". | |
189 | ;; | |
f964dfcb GM |
190 | ;; Richard Sharman <rsharman@pobox.com> June 1999. |
191 | ||
ac59aed8 RS |
192 | ;;; Code: |
193 | ||
194 | ;; page 1: variables and settings | |
f964dfcb GM |
195 | ;; page 2: indentation stuff |
196 | ;; page 3: mode-command and utility functions | |
197 | ;; page 4: statement syntax-commands for various shells | |
198 | ;; page 5: various other commands | |
ac59aed8 | 199 | |
d2d00127 DL |
200 | (eval-when-compile |
201 | (require 'skeleton) | |
017708e9 | 202 | (require 'cl) |
d2d00127 | 203 | (require 'comint)) |
133693bc KH |
204 | (require 'executable) |
205 | ||
482db54b JB |
206 | (defvar font-lock-comment-face) |
207 | (defvar font-lock-set-defaults) | |
208 | (defvar font-lock-string-face) | |
2bffb7c4 | 209 | |
2bffb7c4 | 210 | |
cd482e05 | 211 | (defgroup sh nil |
1689f309 | 212 | "Shell programming utilities." |
cd482e05 RS |
213 | :group 'languages) |
214 | ||
215 | (defgroup sh-script nil | |
1689f309 | 216 | "Shell script mode." |
8ec3bce0 | 217 | :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces) |
cd482e05 RS |
218 | :group 'sh |
219 | :prefix "sh-") | |
220 | ||
221 | ||
222 | (defcustom sh-ancestor-alist | |
133693bc KH |
223 | '((ash . sh) |
224 | (bash . jsh) | |
457316e9 | 225 | (bash2 . jsh) |
133693bc KH |
226 | (dtksh . ksh) |
227 | (es . rc) | |
228 | (itcsh . tcsh) | |
229 | (jcsh . csh) | |
230 | (jsh . sh) | |
231 | (ksh . ksh88) | |
232 | (ksh88 . jsh) | |
233 | (oash . sh) | |
234 | (pdksh . ksh88) | |
235 | (posix . sh) | |
236 | (tcsh . csh) | |
237 | (wksh . ksh88) | |
238 | (wsh . sh) | |
547745f5 RS |
239 | (zsh . ksh88) |
240 | (rpm . sh)) | |
4f3a3368 | 241 | "Alist showing the direct ancestor of various shells. |
133693bc KH |
242 | This is the basis for `sh-feature'. See also `sh-alias-alist'. |
243 | By default we have the following three hierarchies: | |
244 | ||
245 | csh C Shell | |
246 | jcsh C Shell with Job Control | |
c0a4da52 CY |
247 | tcsh TENEX C Shell |
248 | itcsh Ian's TENEX C Shell | |
133693bc KH |
249 | rc Plan 9 Shell |
250 | es Extensible Shell | |
251 | sh Bourne Shell | |
309c7698 | 252 | ash Almquist Shell |
133693bc KH |
253 | jsh Bourne Shell with Job Control |
254 | bash GNU Bourne Again Shell | |
255 | ksh88 Korn Shell '88 | |
256 | ksh Korn Shell '93 | |
257 | dtksh CDE Desktop Korn Shell | |
258 | pdksh Public Domain Korn Shell | |
259 | wksh Window Korn Shell | |
260 | zsh Z Shell | |
261 | oash SCO OA (curses) Shell | |
262 | posix IEEE 1003.2 Shell Standard | |
cd482e05 RS |
263 | wsh ? Shell" |
264 | :type '(repeat (cons symbol symbol)) | |
265 | :group 'sh-script) | |
133693bc KH |
266 | |
267 | ||
cd482e05 | 268 | (defcustom sh-alias-alist |
3ee5ce58 | 269 | (append (if (eq system-type 'gnu/linux) |
133693bc | 270 | '((csh . tcsh) |
aafd074a | 271 | (ksh . pdksh))) |
133693bc KH |
272 | ;; for the time being |
273 | '((ksh . ksh88) | |
457316e9 | 274 | (bash2 . bash) |
133693bc | 275 | (sh5 . sh))) |
4f3a3368 | 276 | "Alist for transforming shell names to what they really are. |
133693bc | 277 | Use this where the name of the executable doesn't correspond to the type of |
cd482e05 RS |
278 | shell it really is." |
279 | :type '(repeat (cons symbol symbol)) | |
280 | :group 'sh-script) | |
133693bc KH |
281 | |
282 | ||
cd482e05 | 283 | (defcustom sh-shell-file |
d9de8c04 | 284 | (or |
5c449169 RS |
285 | ;; On MSDOS and Windows, collapse $SHELL to lower-case and remove |
286 | ;; the executable extension, so comparisons with the list of | |
d9de8c04 | 287 | ;; known shells work. |
5c449169 | 288 | (and (memq system-type '(ms-dos windows-nt)) |
eee86eff EZ |
289 | (let* ((shell (getenv "SHELL")) |
290 | (shell-base | |
291 | (and shell (file-name-nondirectory shell)))) | |
292 | ;; shell-script mode doesn't support DOS/Windows shells, | |
293 | ;; so use the default instead. | |
294 | (if (or (null shell) | |
295 | (member (downcase shell-base) | |
ced7b4a4 RS |
296 | '("command.com" "cmd.exe" "4dos.com" "ndos.com" |
297 | "cmdproxy.exe"))) | |
eee86eff EZ |
298 | "/bin/sh" |
299 | (file-name-sans-extension (downcase shell))))) | |
d9de8c04 RS |
300 | (getenv "SHELL") |
301 | "/bin/sh") | |
4f3a3368 | 302 | "The executable file name for the shell being programmed." |
cd482e05 RS |
303 | :type 'string |
304 | :group 'sh-script) | |
133693bc KH |
305 | |
306 | ||
cd482e05 | 307 | (defcustom sh-shell-arg |
8d31ff15 | 308 | ;; bash does not need any options when run in a shell script, |
8e46e267 | 309 | '((bash) |
133693bc | 310 | (csh . "-f") |
133693bc | 311 | (pdksh) |
8d31ff15 | 312 | ;; Bill_Mann@praxisint.com says -p with ksh can do harm. |
8e46e267 | 313 | (ksh88) |
8d31ff15 | 314 | ;; -p means don't initialize functions from the environment. |
133693bc | 315 | (rc . "-p") |
8d31ff15 RS |
316 | ;; Someone proposed -motif, but we don't want to encourage |
317 | ;; use of a non-free widget set. | |
318 | (wksh) | |
319 | ;; -f means don't run .zshrc. | |
133693bc | 320 | (zsh . "-f")) |
4f3a3368 | 321 | "Single argument string for the magic number. See `sh-feature'." |
cd482e05 RS |
322 | :type '(repeat (cons (symbol :tag "Shell") |
323 | (choice (const :tag "No Arguments" nil) | |
324 | (string :tag "Arguments") | |
6b61353c | 325 | (sexp :format "Evaluate: %v")))) |
cd482e05 | 326 | :group 'sh-script) |
133693bc | 327 | |
aa2c2426 | 328 | (defcustom sh-imenu-generic-expression |
6c5bcbc1 | 329 | `((sh |
4f3a3368 SM |
330 | . ((nil "^\\s-*\\(function\\s-+\\)?\\([[:alpha:]_][[:alnum:]_]+\\)\\s-*()" 2)))) |
331 | "Alist of regular expressions for recognizing shell function definitions. | |
83c5d68f DL |
332 | See `sh-feature' and `imenu-generic-expression'." |
333 | :type '(alist :key-type (symbol :tag "Shell") | |
334 | :value-type (alist :key-type (choice :tag "Title" | |
335 | string | |
336 | (const :tag "None" nil)) | |
337 | :value-type | |
338 | (repeat :tag "Regexp, index..." sexp))) | |
cd32a7ba | 339 | :group 'sh-script |
f964dfcb | 340 | :version "20.4") |
aa2c2426 | 341 | |
5a989d6e RS |
342 | (defvar sh-shell-variables nil |
343 | "Alist of shell variable names that should be included in completion. | |
344 | These are used for completion in addition to all the variables named | |
345 | in `process-environment'. Each element looks like (VAR . VAR), where | |
346 | the car and cdr are the same symbol.") | |
133693bc | 347 | |
5d73ac66 RS |
348 | (defvar sh-shell-variables-initialized nil |
349 | "Non-nil if `sh-shell-variables' is initialized.") | |
350 | ||
aafd074a KH |
351 | (defun sh-canonicalize-shell (shell) |
352 | "Convert a shell name SHELL to the one we should handle it as." | |
842cc0e6 | 353 | (if (string-match "\\.exe\\'" shell) |
c8b88e9f | 354 | (setq shell (substring shell 0 (match-beginning 0)))) |
aafd074a KH |
355 | (or (symbolp shell) |
356 | (setq shell (intern shell))) | |
357 | (or (cdr (assq shell sh-alias-alist)) | |
358 | shell)) | |
133693bc | 359 | |
aafd074a KH |
360 | (defvar sh-shell (sh-canonicalize-shell (file-name-nondirectory sh-shell-file)) |
361 | "The shell being programmed. This is set by \\[sh-set-shell].") | |
7fe9a6e3 | 362 | ;;;###autoload(put 'sh-shell 'safe-local-variable 'symbolp) |
133693bc | 363 | |
8a0d0722 RS |
364 | (defvar sh-mode-abbrev-table nil) |
365 | ||
366 | (define-abbrev-table 'sh-mode-abbrev-table ()) | |
367 | ||
368 | ||
3e2dd647 SM |
369 | ;; I turned off this feature because it doesn't permit typing commands |
370 | ;; in the usual way without help. | |
371 | ;;(defvar sh-abbrevs | |
6b61353c | 372 | ;; '((csh sh-abbrevs shell |
3e2dd647 SM |
373 | ;; "switch" 'sh-case |
374 | ;; "getopts" 'sh-while-getopts) | |
375 | ||
6b61353c | 376 | ;; (es sh-abbrevs shell |
3e2dd647 SM |
377 | ;; "function" 'sh-function) |
378 | ||
6b61353c | 379 | ;; (ksh88 sh-abbrevs sh |
3e2dd647 SM |
380 | ;; "select" 'sh-select) |
381 | ||
6b61353c | 382 | ;; (rc sh-abbrevs shell |
3e2dd647 SM |
383 | ;; "case" 'sh-case |
384 | ;; "function" 'sh-function) | |
385 | ||
6b61353c | 386 | ;; (sh sh-abbrevs shell |
3e2dd647 SM |
387 | ;; "case" 'sh-case |
388 | ;; "function" 'sh-function | |
389 | ;; "until" 'sh-until | |
390 | ;; "getopts" 'sh-while-getopts) | |
391 | ||
392 | ;; ;; The next entry is only used for defining the others | |
393 | ;; (shell "for" sh-for | |
394 | ;; "loop" sh-indexed-loop | |
395 | ;; "if" sh-if | |
396 | ;; "tmpfile" sh-tmp-file | |
397 | ;; "while" sh-while) | |
398 | ||
6b61353c | 399 | ;; (zsh sh-abbrevs ksh88 |
3e2dd647 SM |
400 | ;; "repeat" 'sh-repeat)) |
401 | ;; "Abbrev-table used in Shell-Script mode. See `sh-feature'. | |
aafd074a KH |
402 | ;;;Due to the internal workings of abbrev tables, the shell name symbol is |
403 | ;;;actually defined as the table for the like of \\[edit-abbrevs].") | |
ac59aed8 | 404 | |
ac59aed8 RS |
405 | |
406 | ||
6b61353c KH |
407 | (defun sh-mode-syntax-table (table &rest list) |
408 | "Copy TABLE and set syntax for successive CHARs according to strings S." | |
409 | (setq table (copy-syntax-table table)) | |
410 | (while list | |
411 | (modify-syntax-entry (pop list) (pop list) table)) | |
412 | table) | |
413 | ||
414 | (defvar sh-mode-syntax-table nil | |
415 | "The syntax table to use for Shell-Script mode. | |
416 | This is buffer-local in every such buffer.") | |
417 | ||
418 | (defvar sh-mode-default-syntax-table | |
419 | (sh-mode-syntax-table () | |
b1e851bb | 420 | ?\# "<" |
b1e851bb RS |
421 | ?\n ">#" |
422 | ?\" "\"\"" | |
423 | ?\' "\"'" | |
424 | ?\` "\"`" | |
8cec35c4 DN |
425 | ;; ?$ might also have a ". p" syntax. Both "'" and ". p" seem |
426 | ;; to work fine. This is needed so that dabbrev-expand | |
427 | ;; $VARNAME works. | |
428 | ?$ "'" | |
b1e851bb RS |
429 | ?! "_" |
430 | ?% "_" | |
431 | ?: "_" | |
432 | ?. "_" | |
433 | ?^ "_" | |
434 | ?~ "_" | |
29653ebc | 435 | ?, "_" |
56858354 | 436 | ?= "." |
b1e851bb RS |
437 | ?< "." |
438 | ?> ".") | |
6b61353c | 439 | "Default syntax table for shell mode.") |
b1e851bb | 440 | |
6b61353c KH |
441 | (defvar sh-mode-syntax-table-input |
442 | '((sh . nil)) | |
b1e851bb | 443 | "Syntax-table used in Shell-Script mode. See `sh-feature'.") |
ac59aed8 RS |
444 | |
445 | (defvar sh-mode-map | |
bfc8e97b | 446 | (let ((map (make-sparse-keymap)) |
f03562ec | 447 | (menu-map (make-sparse-keymap))) |
ac59aed8 RS |
448 | (define-key map "\C-c(" 'sh-function) |
449 | (define-key map "\C-c\C-w" 'sh-while) | |
450 | (define-key map "\C-c\C-u" 'sh-until) | |
133693bc | 451 | (define-key map "\C-c\C-t" 'sh-tmp-file) |
ac59aed8 | 452 | (define-key map "\C-c\C-s" 'sh-select) |
133693bc KH |
453 | (define-key map "\C-c\C-r" 'sh-repeat) |
454 | (define-key map "\C-c\C-o" 'sh-while-getopts) | |
ac59aed8 RS |
455 | (define-key map "\C-c\C-l" 'sh-indexed-loop) |
456 | (define-key map "\C-c\C-i" 'sh-if) | |
457 | (define-key map "\C-c\C-f" 'sh-for) | |
458 | (define-key map "\C-c\C-c" 'sh-case) | |
f964dfcb GM |
459 | (define-key map "\C-c?" 'sh-show-indent) |
460 | (define-key map "\C-c=" 'sh-set-indent) | |
461 | (define-key map "\C-c<" 'sh-learn-line-indent) | |
462 | (define-key map "\C-c>" 'sh-learn-buffer-indent) | |
bdd5fa99 | 463 | (define-key map "\C-c\C-\\" 'sh-backslash-region) |
133693bc | 464 | |
ac59aed8 RS |
465 | (define-key map "=" 'sh-assignment) |
466 | (define-key map "\C-c+" 'sh-add) | |
fd4ea9a2 RS |
467 | (define-key map "\C-\M-x" 'sh-execute-region) |
468 | (define-key map "\C-c\C-x" 'executable-interpret) | |
133693bc | 469 | (define-key map "<" 'sh-maybe-here-document) |
81ed2d75 KH |
470 | (define-key map "(" 'skeleton-pair-insert-maybe) |
471 | (define-key map "{" 'skeleton-pair-insert-maybe) | |
472 | (define-key map "[" 'skeleton-pair-insert-maybe) | |
473 | (define-key map "'" 'skeleton-pair-insert-maybe) | |
474 | (define-key map "`" 'skeleton-pair-insert-maybe) | |
475 | (define-key map "\"" 'skeleton-pair-insert-maybe) | |
ac59aed8 | 476 | |
5d1825c6 | 477 | (define-key map [remap complete-tag] 'comint-dynamic-complete) |
5d1825c6 AS |
478 | (define-key map [remap delete-backward-char] |
479 | 'backward-delete-char-untabify) | |
ac59aed8 | 480 | (define-key map "\C-c:" 'sh-set-shell) |
5d1825c6 AS |
481 | (define-key map [remap backward-sentence] 'sh-beginning-of-command) |
482 | (define-key map [remap forward-sentence] 'sh-end-of-command) | |
f03562ec DN |
483 | (define-key map [menu-bar sh-script] (cons "Sh-Script" menu-map)) |
484 | (define-key menu-map [sh-learn-buffer-indent] | |
485 | '(menu-item "Learn buffer indentation" sh-learn-buffer-indent | |
486 | :help "Learn how to indent the buffer the way it currently is.")) | |
487 | (define-key menu-map [sh-learn-line-indent] | |
488 | '(menu-item "Learn line indentation" sh-learn-line-indent | |
489 | :help "Learn how to indent a line as it currently is indented")) | |
490 | (define-key menu-map [sh-show-indent] | |
491 | '(menu-item "Show indentation" sh-show-indent | |
63616f52 | 492 | :help "Show the how the current line would be indented")) |
f03562ec DN |
493 | (define-key menu-map [sh-set-indent] |
494 | '(menu-item "Set indentation" sh-set-indent | |
495 | :help "Set the indentation for the current line")) | |
496 | ||
63616f52 | 497 | (define-key menu-map [sh-pair] |
e4bf03f6 CY |
498 | '(menu-item "Insert braces and quotes in pairs" |
499 | (lambda () | |
500 | (interactive) | |
501 | (require 'skeleton) | |
502 | (setq skeleton-pair (not skeleton-pair))) | |
503 | :button (:toggle . (and (boundp 'skeleton-pair) | |
504 | skeleton-pair)) | |
63616f52 DN |
505 | :help "Inserting a brace or quote automatically inserts the matching pair")) |
506 | ||
f03562ec DN |
507 | (define-key menu-map [sh-s0] '("--")) |
508 | ;; Insert | |
509 | (define-key menu-map [sh-function] | |
510 | '(menu-item "Function..." sh-function | |
511 | :help "Insert a function definition")) | |
512 | (define-key menu-map [sh-add] | |
513 | '(menu-item "Addition..." sh-add | |
514 | :help "Insert an addition of VAR and prefix DELTA for Bourne (type) shell")) | |
515 | (define-key menu-map [sh-until] | |
516 | '(menu-item "Until Loop" sh-until | |
517 | :help "Insert an until loop")) | |
518 | (define-key menu-map [sh-repeat] | |
519 | '(menu-item "Repeat Loop" sh-repeat | |
520 | :help "Insert a repeat loop definition")) | |
521 | (define-key menu-map [sh-while] | |
522 | '(menu-item "While Loop" sh-while | |
523 | :help "Insert a while loop")) | |
524 | (define-key menu-map [sh-getopts] | |
525 | '(menu-item "Options Loop" sh-while-getopts | |
526 | :help "Insert a while getopts loop.")) | |
527 | (define-key menu-map [sh-indexed-loop] | |
528 | '(menu-item "Indexed Loop" sh-indexed-loop | |
529 | :help "Insert an indexed loop from 1 to n.")) | |
530 | (define-key menu-map [sh-select] | |
531 | '(menu-item "Select Statement" sh-select | |
532 | :help "Insert a select statement ")) | |
533 | (define-key menu-map [sh-if] | |
534 | '(menu-item "If Statement" sh-if | |
535 | :help "Insert an if statement")) | |
536 | (define-key menu-map [sh-for] | |
537 | '(menu-item "For Loop" sh-for | |
538 | :help "Insert a for loop")) | |
539 | (define-key menu-map [sh-case] | |
540 | '(menu-item "Case Statement" sh-case | |
541 | :help "Insert a case/switch statement")) | |
542 | (define-key menu-map [sh-s1] '("--")) | |
543 | (define-key menu-map [sh-exec] | |
544 | '(menu-item "Execute region" sh-execute-region | |
545 | :help "Pass optional header and region to a subshell for noninteractive execution")) | |
546 | (define-key menu-map [sh-exec-interpret] | |
547 | '(menu-item "Execute script..." executable-interpret | |
548 | :help "Run script with user-specified args, and collect output in a buffer")) | |
549 | (define-key menu-map [sh-set-shell] | |
550 | '(menu-item "Set shell type..." sh-set-shell | |
551 | :help "Set this buffer's shell to SHELL (a string)")) | |
552 | (define-key menu-map [sh-backslash-region] | |
553 | '(menu-item "Backslash region" sh-backslash-region | |
554 | :help "Insert, align, or delete end-of-line backslashes on the lines in the region.")) | |
ac59aed8 RS |
555 | map) |
556 | "Keymap used in Shell-Script mode.") | |
557 | ||
0404a075 RS |
558 | (defvar sh-skeleton-pair-default-alist '((?( _ ?)) (?\)) |
559 | (?[ ?\s _ ?\s ?]) (?\]) | |
560 | (?{ _ ?}) (?\})) | |
561 | "Value to use for `skeleton-pair-default-alist' in Shell-Script mode.") | |
ac59aed8 | 562 | |
cd482e05 | 563 | (defcustom sh-dynamic-complete-functions |
133693bc KH |
564 | '(shell-dynamic-complete-environment-variable |
565 | shell-dynamic-complete-command | |
566 | comint-dynamic-complete-filename) | |
4f3a3368 | 567 | "Functions for doing TAB dynamic completion." |
cd482e05 RS |
568 | :type '(repeat function) |
569 | :group 'sh-script) | |
ac59aed8 RS |
570 | |
571 | ||
cd482e05 | 572 | (defcustom sh-require-final-newline |
133693bc | 573 | '((csh . t) |
61871c70 | 574 | (pdksh . t)) |
4f3a3368 | 575 | "Value of `require-final-newline' in Shell-Script mode buffers. |
61871c70 | 576 | \(SHELL . t) means use the value of `mode-require-final-newline' for SHELL. |
cd482e05 RS |
577 | See `sh-feature'." |
578 | :type '(repeat (cons (symbol :tag "Shell") | |
579 | (choice (const :tag "require" t) | |
6b61353c | 580 | (sexp :format "Evaluate: %v")))) |
cd482e05 | 581 | :group 'sh-script) |
ac59aed8 RS |
582 | |
583 | ||
c410bd65 | 584 | (defcustom sh-assignment-regexp |
4f3a3368 | 585 | '((csh . "\\<\\([[:alnum:]_]+\\)\\(\\[.+\\]\\)?[ \t]*[-+*/%^]?=") |
133693bc | 586 | ;; actually spaces are only supported in let/(( ... )) |
4f3a3368 SM |
587 | (ksh88 . "\\<\\([[:alnum:]_]+\\)\\(\\[.+\\]\\)?[ \t]*\\([-+*/%&|~^]\\|<<\\|>>\\)?=") |
588 | (rc . "\\<\\([[:alnum:]_*]+\\)[ \t]*=") | |
589 | (sh . "\\<\\([[:alnum:]_]+\\)=")) | |
590 | "Regexp for the variable name and what may follow in an assignment. | |
133693bc | 591 | First grouping matches the variable name. This is upto and including the `=' |
cd482e05 RS |
592 | sign. See `sh-feature'." |
593 | :type '(repeat (cons (symbol :tag "Shell") | |
594 | (choice regexp | |
6b61353c | 595 | (sexp :format "Evaluate: %v")))) |
cd482e05 | 596 | :group 'sh-script) |
ac59aed8 | 597 | |
ac59aed8 | 598 | |
cd482e05 RS |
599 | (defcustom sh-indentation 4 |
600 | "The width for further indentation in Shell-Script mode." | |
601 | :type 'integer | |
602 | :group 'sh-script) | |
218da0e9 | 603 | (put 'sh-indentation 'safe-local-variable 'integerp) |
ac59aed8 | 604 | |
cd482e05 | 605 | (defcustom sh-remember-variable-min 3 |
4f3a3368 | 606 | "Don't remember variables less than this length for completing reads." |
cd482e05 RS |
607 | :type 'integer |
608 | :group 'sh-script) | |
ac59aed8 RS |
609 | |
610 | ||
133693bc | 611 | (defvar sh-header-marker nil |
f964dfcb | 612 | "When non-nil is the end of header for prepending by \\[sh-execute-region]. |
133693bc KH |
613 | That command is also used for setting this variable.") |
614 | ||
615 | ||
cd482e05 | 616 | (defcustom sh-beginning-of-command |
4f3a3368 SM |
617 | "\\([;({`|&]\\|\\`\\|[^\\]\n\\)[ \t]*\\([/~[:alnum:]:]\\)" |
618 | "Regexp to determine the beginning of a shell command. | |
cd482e05 RS |
619 | The actual command starts at the beginning of the second \\(grouping\\)." |
620 | :type 'regexp | |
621 | :group 'sh-script) | |
ac59aed8 | 622 | |
133693bc | 623 | |
cd482e05 | 624 | (defcustom sh-end-of-command |
4f3a3368 SM |
625 | "\\([/~[:alnum:]:]\\)[ \t]*\\([;#)}`|&]\\|$\\)" |
626 | "Regexp to determine the end of a shell command. | |
cd482e05 RS |
627 | The actual command ends at the end of the first \\(grouping\\)." |
628 | :type 'regexp | |
629 | :group 'sh-script) | |
ac59aed8 RS |
630 | |
631 | ||
632 | ||
546e2f6f | 633 | (defcustom sh-here-document-word "EOF" |
18368c4a | 634 | "Word to delimit here documents. |
546e2f6f GM |
635 | If the first character of this string is \"-\", this is taken as |
636 | part of the redirection operator, rather than part of the | |
637 | word (that is, \"<<-\" instead of \"<<\"). This is a feature | |
638 | used by some shells (for example Bash) to indicate that leading | |
639 | tabs inside the here document should be ignored. In this case, | |
640 | Emacs indents the initial body and end of the here document with | |
641 | tabs, to the same level as the start (note that apart from this | |
642 | there is no support for indentation of here documents). This | |
643 | will only work correctly if `sh-basic-offset' is a multiple of | |
644 | `tab-width'. | |
645 | ||
646 | Any quote characters or leading whitespace in the word are | |
647 | removed when closing the here document." | |
648 | :type 'string | |
649 | :group 'sh-script) | |
650 | ||
ac59aed8 | 651 | |
225f6185 KH |
652 | (defvar sh-test |
653 | '((sh "[ ]" . 3) | |
654 | (ksh88 "[[ ]]" . 4)) | |
655 | "Initial input in Bourne if, while and until skeletons. See `sh-feature'.") | |
656 | ||
ac59aed8 | 657 | |
cd482e05 RS |
658 | ;; customized this out of sheer bravado. not for the faint of heart. |
659 | ;; but it *did* have an asterisk in the docstring! | |
660 | (defcustom sh-builtins | |
6b61353c | 661 | '((bash sh-append posix |
450a39ff | 662 | "." "alias" "bg" "bind" "builtin" "caller" "compgen" "complete" |
6b61353c KH |
663 | "declare" "dirs" "disown" "enable" "fc" "fg" "help" "history" |
664 | "jobs" "kill" "let" "local" "popd" "printf" "pushd" "shopt" | |
665 | "source" "suspend" "typeset" "unalias") | |
ac59aed8 | 666 | |
133693bc | 667 | ;; The next entry is only used for defining the others |
6b61353c | 668 | (bourne sh-append shell |
84bfbb44 KH |
669 | "eval" "export" "getopts" "newgrp" "pwd" "read" "readonly" |
670 | "times" "ulimit") | |
ac59aed8 | 671 | |
6b61353c | 672 | (csh sh-append shell |
84bfbb44 KH |
673 | "alias" "chdir" "glob" "history" "limit" "nice" "nohup" "rehash" |
674 | "setenv" "source" "time" "unalias" "unhash") | |
675 | ||
6b61353c | 676 | (dtksh sh-append wksh) |
ac59aed8 | 677 | |
84bfbb44 KH |
678 | (es "access" "apids" "cd" "echo" "eval" "false" "let" "limit" "local" |
679 | "newpgrp" "result" "time" "umask" "var" "vars" "wait" "whatis") | |
ac59aed8 | 680 | |
6b61353c | 681 | (jsh sh-append sh |
133693bc | 682 | "bg" "fg" "jobs" "kill" "stop" "suspend") |
ac59aed8 | 683 | |
6b61353c | 684 | (jcsh sh-append csh |
6c5bcbc1 | 685 | "bg" "fg" "jobs" "kill" "notify" "stop" "suspend") |
133693bc | 686 | |
6b61353c | 687 | (ksh88 sh-append bourne |
84bfbb44 KH |
688 | "alias" "bg" "false" "fc" "fg" "jobs" "kill" "let" "print" "time" |
689 | "typeset" "unalias" "whence") | |
133693bc | 690 | |
6b61353c | 691 | (oash sh-append sh |
133693bc KH |
692 | "checkwin" "dateline" "error" "form" "menu" "newwin" "oadeinit" |
693 | "oaed" "oahelp" "oainit" "pp" "ppfile" "scan" "scrollok" "wattr" | |
694 | "wclear" "werase" "win" "wmclose" "wmmessage" "wmopen" "wmove" | |
695 | "wmtitle" "wrefresh") | |
696 | ||
6b61353c | 697 | (pdksh sh-append ksh88 |
133693bc KH |
698 | "bind") |
699 | ||
6b61353c | 700 | (posix sh-append sh |
133693bc KH |
701 | "command") |
702 | ||
84bfbb44 KH |
703 | (rc "builtin" "cd" "echo" "eval" "limit" "newpgrp" "shift" "umask" "wait" |
704 | "whatis") | |
133693bc | 705 | |
6b61353c | 706 | (sh sh-append bourne |
133693bc KH |
707 | "hash" "test" "type") |
708 | ||
709 | ;; The next entry is only used for defining the others | |
84bfbb44 KH |
710 | (shell "cd" "echo" "eval" "set" "shift" "umask" "unset" "wait") |
711 | ||
6b61353c | 712 | (wksh sh-append ksh88 |
4f3a3368 | 713 | ;; FIXME: This looks too much like a regexp. --Stef |
84bfbb44 | 714 | "Xt[A-Z][A-Za-z]*") |
133693bc | 715 | |
6b61353c | 716 | (zsh sh-append ksh88 |
84bfbb44 KH |
717 | "autoload" "bindkey" "builtin" "chdir" "compctl" "declare" "dirs" |
718 | "disable" "disown" "echotc" "enable" "functions" "getln" "hash" | |
719 | "history" "integer" "limit" "local" "log" "popd" "pushd" "r" | |
720 | "readonly" "rehash" "sched" "setopt" "source" "suspend" "true" | |
721 | "ttyctl" "type" "unfunction" "unhash" "unlimit" "unsetopt" "vared" | |
722 | "which")) | |
4f3a3368 | 723 | "List of all shell builtins for completing read and fontification. |
133693bc | 724 | Note that on some systems not all builtins are available or some are |
cd482e05 RS |
725 | implemented as aliases. See `sh-feature'." |
726 | :type '(repeat (cons (symbol :tag "Shell") | |
727 | (choice (repeat string) | |
6b61353c | 728 | (sexp :format "Evaluate: %v")))) |
cd482e05 | 729 | :group 'sh-script) |
133693bc KH |
730 | |
731 | ||
84bfbb44 | 732 | |
cd482e05 | 733 | (defcustom sh-leading-keywords |
6b61353c KH |
734 | '((bash sh-append sh |
735 | "time") | |
736 | ||
737 | (csh "else") | |
84bfbb44 KH |
738 | |
739 | (es "true" "unwind-protect" "whatis") | |
740 | ||
741 | (rc "else") | |
742 | ||
14116f3c | 743 | (sh "!" "do" "elif" "else" "if" "then" "trap" "type" "until" "while")) |
4f3a3368 | 744 | "List of keywords that may be immediately followed by a builtin or keyword. |
84bfbb44 KH |
745 | Given some confusion between keywords and builtins depending on shell and |
746 | system, the distinction here has been based on whether they influence the | |
cd482e05 RS |
747 | flow of control or syntax. See `sh-feature'." |
748 | :type '(repeat (cons (symbol :tag "Shell") | |
749 | (choice (repeat string) | |
6b61353c | 750 | (sexp :format "Evaluate: %v")))) |
cd482e05 | 751 | :group 'sh-script) |
84bfbb44 KH |
752 | |
753 | ||
cd482e05 | 754 | (defcustom sh-other-keywords |
6b61353c | 755 | '((bash sh-append bourne |
bc387269 | 756 | "bye" "logout" "select") |
133693bc KH |
757 | |
758 | ;; The next entry is only used for defining the others | |
6b61353c | 759 | (bourne sh-append sh |
d9de8c04 | 760 | "function") |
133693bc | 761 | |
6b61353c | 762 | (csh sh-append shell |
84bfbb44 KH |
763 | "breaksw" "default" "end" "endif" "endsw" "foreach" "goto" |
764 | "if" "logout" "onintr" "repeat" "switch" "then" "while") | |
133693bc | 765 | |
84bfbb44 KH |
766 | (es "break" "catch" "exec" "exit" "fn" "for" "forever" "fork" "if" |
767 | "return" "throw" "while") | |
133693bc | 768 | |
6b61353c | 769 | (ksh88 sh-append bourne |
84bfbb44 | 770 | "select") |
133693bc | 771 | |
84bfbb44 KH |
772 | (rc "break" "case" "exec" "exit" "fn" "for" "if" "in" "return" "switch" |
773 | "while") | |
133693bc | 774 | |
6b61353c | 775 | (sh sh-append shell |
d9de8c04 RS |
776 | "done" "esac" "fi" "for" "in" "return") |
777 | ||
84bfbb44 KH |
778 | ;; The next entry is only used for defining the others |
779 | (shell "break" "case" "continue" "exec" "exit") | |
133693bc | 780 | |
6b61353c | 781 | (zsh sh-append bash |
84bfbb44 | 782 | "select")) |
4f3a3368 | 783 | "List of keywords not in `sh-leading-keywords'. |
cd482e05 RS |
784 | See `sh-feature'." |
785 | :type '(repeat (cons (symbol :tag "Shell") | |
786 | (choice (repeat string) | |
6b61353c | 787 | (sexp :format "Evaluate: %v")))) |
cd482e05 | 788 | :group 'sh-script) |
133693bc KH |
789 | |
790 | ||
791 | ||
792 | (defvar sh-variables | |
6b61353c KH |
793 | '((bash sh-append sh |
794 | "allow_null_glob_expansion" "auto_resume" "BASH" "BASH_ENV" | |
795 | "BASH_VERSINFO" "BASH_VERSION" "cdable_vars" "COMP_CWORD" | |
796 | "COMP_LINE" "COMP_POINT" "COMP_WORDS" "COMPREPLY" "DIRSTACK" | |
797 | "ENV" "EUID" "FCEDIT" "FIGNORE" "FUNCNAME" | |
798 | "glob_dot_filenames" "GLOBIGNORE" "GROUPS" "histchars" | |
799 | "HISTCMD" "HISTCONTROL" "HISTFILE" "HISTFILESIZE" | |
800 | "HISTIGNORE" "history_control" "HISTSIZE" | |
801 | "hostname_completion_file" "HOSTFILE" "HOSTTYPE" "IGNOREEOF" | |
802 | "ignoreeof" "INPUTRC" "LINENO" "MACHTYPE" "MAIL_WARNING" | |
803 | "noclobber" "nolinks" "notify" "no_exit_on_failed_exec" | |
804 | "NO_PROMPT_VARS" "OLDPWD" "OPTERR" "OSTYPE" "PIPESTATUS" | |
805 | "PPID" "POSIXLY_CORRECT" "PROMPT_COMMAND" "PS3" "PS4" | |
806 | "pushd_silent" "PWD" "RANDOM" "REPLY" "SECONDS" "SHELLOPTS" | |
807 | "SHLVL" "TIMEFORMAT" "TMOUT" "UID") | |
808 | ||
809 | (csh sh-append shell | |
133693bc KH |
810 | "argv" "cdpath" "child" "echo" "histchars" "history" "home" |
811 | "ignoreeof" "mail" "noclobber" "noglob" "nonomatch" "path" "prompt" | |
812 | "shell" "status" "time" "verbose") | |
813 | ||
6b61353c | 814 | (es sh-append shell |
133693bc KH |
815 | "apid" "cdpath" "CDPATH" "history" "home" "ifs" "noexport" "path" |
816 | "pid" "prompt" "signals") | |
817 | ||
6b61353c | 818 | (jcsh sh-append csh |
6c5bcbc1 | 819 | "notify") |
133693bc | 820 | |
6b61353c | 821 | (ksh88 sh-append sh |
133693bc KH |
822 | "ENV" "ERRNO" "FCEDIT" "FPATH" "HISTFILE" "HISTSIZE" "LINENO" |
823 | "OLDPWD" "PPID" "PS3" "PS4" "PWD" "RANDOM" "REPLY" "SECONDS" | |
824 | "TMOUT") | |
825 | ||
6b61353c | 826 | (oash sh-append sh |
133693bc KH |
827 | "FIELD" "FIELD_MAX" "LAST_KEY" "OALIB" "PP_ITEM" "PP_NUM") |
828 | ||
6b61353c | 829 | (rc sh-append shell |
133693bc KH |
830 | "apid" "apids" "cdpath" "CDPATH" "history" "home" "ifs" "path" "pid" |
831 | "prompt" "status") | |
832 | ||
6b61353c | 833 | (sh sh-append shell |
133693bc KH |
834 | "CDPATH" "IFS" "OPTARG" "OPTIND" "PS1" "PS2") |
835 | ||
836 | ;; The next entry is only used for defining the others | |
837 | (shell "COLUMNS" "EDITOR" "HOME" "HUSHLOGIN" "LANG" "LC_COLLATE" | |
838 | "LC_CTYPE" "LC_MESSAGES" "LC_MONETARY" "LC_NUMERIC" "LC_TIME" | |
839 | "LINES" "LOGNAME" "MAIL" "MAILCHECK" "MAILPATH" "PAGER" "PATH" | |
840 | "SHELL" "TERM" "TERMCAP" "TERMINFO" "VISUAL") | |
841 | ||
6b61353c | 842 | (tcsh sh-append csh |
133693bc KH |
843 | "addsuffix" "ampm" "autocorrect" "autoexpand" "autolist" |
844 | "autologout" "chase_symlinks" "correct" "dextract" "edit" "el" | |
845 | "fignore" "gid" "histlit" "HOST" "HOSTTYPE" "HPATH" | |
846 | "ignore_symlinks" "listjobs" "listlinks" "listmax" "matchbeep" | |
847 | "nobeep" "NOREBIND" "oid" "printexitvalue" "prompt2" "prompt3" | |
848 | "pushdsilent" "pushdtohome" "recexact" "recognize_only_executables" | |
849 | "rmstar" "savehist" "SHLVL" "showdots" "sl" "SYSTYPE" "tcsh" "term" | |
850 | "tperiod" "tty" "uid" "version" "visiblebell" "watch" "who" | |
851 | "wordchars") | |
852 | ||
6b61353c | 853 | (zsh sh-append ksh88 |
133693bc KH |
854 | "BAUD" "bindcmds" "cdpath" "DIRSTACKSIZE" "fignore" "FIGNORE" "fpath" |
855 | "HISTCHARS" "hostcmds" "hosts" "HOSTS" "LISTMAX" "LITHISTSIZE" | |
856 | "LOGCHECK" "mailpath" "manpath" "NULLCMD" "optcmds" "path" "POSTEDIT" | |
857 | "prompt" "PROMPT" "PROMPT2" "PROMPT3" "PROMPT4" "psvar" "PSVAR" | |
858 | "READNULLCMD" "REPORTTIME" "RPROMPT" "RPS1" "SAVEHIST" "SPROMPT" | |
859 | "STTY" "TIMEFMT" "TMOUT" "TMPPREFIX" "varcmds" "watch" "WATCH" | |
860 | "WATCHFMT" "WORDCHARS" "ZDOTDIR")) | |
861 | "List of all shell variables available for completing read. | |
862 | See `sh-feature'.") | |
863 | ||
aace6150 | 864 | \f |
3e2dd647 | 865 | ;; Font-Lock support |
aace6150 | 866 | |
33595ec6 | 867 | (defface sh-heredoc |
ea81d57e DN |
868 | '((((min-colors 88) (class color) |
869 | (background dark)) | |
870 | (:foreground "yellow1" :weight bold)) | |
871 | (((class color) | |
aace6150 | 872 | (background dark)) |
1fd714a4 | 873 | (:foreground "yellow" :weight bold)) |
aace6150 SM |
874 | (((class color) |
875 | (background light)) | |
f03562ec | 876 | (:foreground "tan1" )) |
aace6150 | 877 | (t |
1fd714a4 | 878 | (:weight bold))) |
aace6150 SM |
879 | "Face to show a here-document" |
880 | :group 'sh-indentation) | |
6d39902f | 881 | |
6772c8e1 | 882 | ;; These colors are probably icky. It's just a placeholder though. |
6d39902f EZ |
883 | (defface sh-quoted-exec |
884 | '((((class color) (background dark)) | |
885 | (:foreground "salmon")) | |
886 | (((class color) (background light)) | |
887 | (:foreground "magenta")) | |
888 | (t | |
889 | (:weight bold))) | |
890 | "Face to show quoted execs like ``" | |
891 | :group 'sh-indentation) | |
c4f6e489 | 892 | (define-obsolete-face-alias 'sh-heredoc-face 'sh-heredoc "22.1") |
33595ec6 | 893 | (defvar sh-heredoc-face 'sh-heredoc) |
133693bc | 894 | |
450a39ff GM |
895 | (defface sh-escaped-newline '((t :inherit font-lock-string-face)) |
896 | "Face used for (non-escaped) backslash at end of a line in Shell-script mode." | |
897 | :group 'sh-script | |
898 | :version "22.1") | |
133693bc | 899 | |
5789bd83 | 900 | (defvar sh-font-lock-keywords-var |
6b61353c | 901 | '((csh sh-append shell |
4f3a3368 | 902 | ("\\${?[#?]?\\([[:alpha:]_][[:alnum:]_]*\\|0\\)" 1 |
6b61353c | 903 | font-lock-variable-name-face)) |
133693bc | 904 | |
6b61353c | 905 | (es sh-append executable-font-lock-keywords |
4f3a3368 | 906 | ("\\$#?\\([[:alpha:]_][[:alnum:]_]*\\|[0-9]+\\)" 1 |
6b61353c | 907 | font-lock-variable-name-face)) |
133693bc | 908 | |
6b61353c | 909 | (rc sh-append es) |
6d39902f | 910 | (bash sh-append shell ("\\$(\\(\\sw+\\)" (1 'sh-quoted-exec t) )) |
6b61353c | 911 | (sh sh-append shell |
4d7ce99c | 912 | ;; Variable names. |
4f3a3368 | 913 | ("\\$\\({#?\\)?\\([[:alpha:]_][[:alnum:]_]*\\|[-#?@!]\\)" 2 |
4d7ce99c RS |
914 | font-lock-variable-name-face) |
915 | ;; Function names. | |
6b61353c KH |
916 | ("^\\(\\sw+\\)[ \t]*(" 1 font-lock-function-name-face) |
917 | ("\\<\\(function\\)\\>[ \t]*\\(\\sw+\\)?" | |
d5f2a899 DP |
918 | (1 font-lock-keyword-face) (2 font-lock-function-name-face nil t)) |
919 | ("\\(?:^\\s *\\|[[();&|]\\s *\\|\\(?:\\s +-[ao]\\|if\\|else\\|then\\|while\\|do\\)\\s +\\)\\(!\\)" | |
920 | 1 font-lock-negation-char-face)) | |
133693bc KH |
921 | |
922 | ;; The next entry is only used for defining the others | |
5789bd83 | 923 | (shell |
6b61353c | 924 | ;; Using font-lock-string-face here confuses sh-get-indent-info. |
450a39ff | 925 | ("\\(^\\|[^\\]\\)\\(\\\\\\\\\\)*\\(\\\\\\)$" 3 'sh-escaped-newline) |
4f3a3368 SM |
926 | ("\\\\[^[:alnum:]]" 0 font-lock-string-face) |
927 | ("\\${?\\([[:alpha:]_][[:alnum:]_]*\\|[0-9]+\\|[$*_]\\)" 1 | |
547745f5 | 928 | font-lock-variable-name-face)) |
6b61353c KH |
929 | (rpm sh-append rpm2 |
930 | ("%{?\\(\\sw+\\)" 1 font-lock-keyword-face)) | |
931 | (rpm2 sh-append shell | |
932 | ("^\\(\\sw+\\):" 1 font-lock-variable-name-face))) | |
38c979d3 | 933 | "Default expressions to highlight in Shell Script modes. See `sh-feature'.") |
133693bc | 934 | |
5789bd83 | 935 | (defvar sh-font-lock-keywords-var-1 |
bfc8e97b | 936 | '((sh "[ \t]in\\>")) |
38c979d3 | 937 | "Subdued level highlighting for Shell Script modes.") |
84bfbb44 | 938 | |
5789bd83 | 939 | (defvar sh-font-lock-keywords-var-2 () |
38c979d3 | 940 | "Gaudy level highlighting for Shell Script modes.") |
84bfbb44 | 941 | |
34939e2c SM |
942 | ;; These are used for the syntax table stuff (derived from cperl-mode). |
943 | ;; Note: parse-sexp-lookup-properties must be set to t for it to work. | |
944 | (defconst sh-st-punc (string-to-syntax ".")) | |
bffd712e | 945 | (defconst sh-st-symbol (string-to-syntax "_")) |
34939e2c SM |
946 | (defconst sh-here-doc-syntax (string-to-syntax "|")) ;; generic string |
947 | ||
9d37a5c0 | 948 | (defconst sh-escaped-line-re |
935e6b79 | 949 | ;; Should match until the real end-of-continued-line, but if that is not |
9d37a5c0 SM |
950 | ;; possible (because we bump into EOB or the search bound), then we should |
951 | ;; match until the search bound. | |
952 | "\\(?:\\(?:.*[^\\\n]\\)?\\(?:\\\\\\\\\\)*\\\\\n\\)*.*") | |
953 | ||
954 | (defconst sh-here-doc-open-re | |
955 | (concat "<<-?\\s-*\\\\?\\(\\(?:['\"][^'\"]+['\"]\\|\\sw\\)+\\)" | |
956 | sh-escaped-line-re "\\(\n\\)")) | |
3e2dd647 SM |
957 | |
958 | (defvar sh-here-doc-markers nil) | |
959 | (make-variable-buffer-local 'sh-here-doc-markers) | |
960 | (defvar sh-here-doc-re sh-here-doc-open-re) | |
961 | (make-variable-buffer-local 'sh-here-doc-re) | |
962 | ||
963 | (defun sh-font-lock-close-heredoc (bol eof indented) | |
964 | "Determine the syntax of the \\n after an EOF. | |
965 | If non-nil INDENTED indicates that the EOF was indented." | |
187cd25b | 966 | (let* ((eof-re (if eof (regexp-quote eof) "")) |
035107fa | 967 | ;; A rough regexp that should find the opening <<EOF back. |
3e2dd647 SM |
968 | (sre (concat "<<\\(-?\\)\\s-*['\"\\]?" |
969 | ;; Use \s| to cheaply check it's an open-heredoc. | |
9d37a5c0 SM |
970 | eof-re "['\"]?\\([ \t|;&)<>]" |
971 | sh-escaped-line-re | |
972 | "\\)?\\s|")) | |
3e2dd647 | 973 | ;; A regexp that will find other EOFs. |
035107fa | 974 | (ere (concat "^" (if indented "[ \t]*") eof-re "\n")) |
3e2dd647 SM |
975 | (start (save-excursion |
976 | (goto-char bol) | |
977 | (re-search-backward (concat sre "\\|" ere) nil t)))) | |
978 | ;; If subgroup 1 matched, we found an open-heredoc, otherwise we first | |
979 | ;; found a close-heredoc which makes the current close-heredoc inoperant. | |
980 | (cond | |
981 | ((when (and start (match-end 1) | |
982 | (not (and indented (= (match-beginning 1) (match-end 1)))) | |
983 | (not (sh-in-comment-or-string (match-beginning 0)))) | |
984 | ;; Make sure our `<<' is not the EOF1 of a `cat <<EOF1 <<EOF2'. | |
985 | (save-excursion | |
986 | (goto-char start) | |
987 | (setq start (line-beginning-position 2)) | |
988 | (while | |
989 | (progn | |
990 | (re-search-forward "<<") ; Skip ourselves. | |
991 | (and (re-search-forward sh-here-doc-open-re start 'move) | |
992 | (goto-char (match-beginning 0)) | |
993 | (sh-in-comment-or-string (point))))) | |
994 | ;; No <<EOF2 found after our <<. | |
995 | (= (point) start))) | |
996 | sh-here-doc-syntax) | |
997 | ((not (or start (save-excursion (re-search-forward sre nil t)))) | |
998 | ;; There's no <<EOF either before or after us, | |
999 | ;; so we should remove ourselves from font-lock's keywords. | |
1000 | (setq sh-here-doc-markers (delete eof sh-here-doc-markers)) | |
1001 | (setq sh-here-doc-re | |
1002 | (concat sh-here-doc-open-re "\\|^\\([ \t]*\\)" | |
1003 | (regexp-opt sh-here-doc-markers t) "\\(\n\\)")) | |
1004 | nil)))) | |
1005 | ||
1006 | (defun sh-font-lock-open-heredoc (start string) | |
1007 | "Determine the syntax of the \\n after a <<EOF. | |
1008 | START is the position of <<. | |
df321f09 | 1009 | STRING is the actual word used as delimiter (e.g. \"EOF\"). |
3e2dd647 | 1010 | INDENTED is non-nil if the here document's content (and the EOF mark) can |
9d37a5c0 SM |
1011 | be indented (i.e. a <<- was used rather than just <<). |
1012 | Point is at the beginning of the next line." | |
3e2dd647 SM |
1013 | (unless (or (memq (char-before start) '(?< ?>)) |
1014 | (sh-in-comment-or-string start)) | |
34939e2c SM |
1015 | ;; We're looking at <<STRING, so we add "^STRING$" to the syntactic |
1016 | ;; font-lock keywords to detect the end of this here document. | |
3e2dd647 SM |
1017 | (let ((str (replace-regexp-in-string "['\"]" "" string))) |
1018 | (unless (member str sh-here-doc-markers) | |
1019 | (push str sh-here-doc-markers) | |
1020 | (setq sh-here-doc-re | |
1021 | (concat sh-here-doc-open-re "\\|^\\([ \t]*\\)" | |
1022 | (regexp-opt sh-here-doc-markers t) "\\(\n\\)")))) | |
9d37a5c0 SM |
1023 | (let ((ppss (save-excursion (syntax-ppss (1- (point)))))) |
1024 | (if (nth 4 ppss) | |
1025 | ;; The \n not only starts the heredoc but also closes a comment. | |
1026 | ;; Let's close the comment just before the \n. | |
1027 | (put-text-property (1- (point)) (point) 'syntax-table '(12))) ;">" | |
1028 | (if (or (nth 5 ppss) (> (count-lines start (point)) 1)) | |
1029 | ;; If the sh-escaped-line-re part of sh-here-doc-re has matched | |
1030 | ;; several lines, make sure we refontify them together. | |
1031 | ;; Furthermore, if (nth 5 ppss) is non-nil (i.e. the \n is | |
1032 | ;; escaped), it means the right \n is actually further down. | |
1033 | ;; Don't bother fixing it now, but place a multiline property so | |
1034 | ;; that when jit-lock-context-* refontifies the rest of the | |
1035 | ;; buffer, it also refontifies the current line with it. | |
1036 | (put-text-property start (point) 'font-lock-multiline t))) | |
34939e2c SM |
1037 | sh-here-doc-syntax)) |
1038 | ||
3e2dd647 SM |
1039 | (defun sh-font-lock-here-doc (limit) |
1040 | "Search for a heredoc marker." | |
1041 | ;; This looks silly, but it's because `sh-here-doc-re' keeps changing. | |
1042 | (re-search-forward sh-here-doc-re limit t)) | |
1043 | ||
e58914d0 | 1044 | (defun sh-font-lock-quoted-subshell (limit) |
3a723c3a SM |
1045 | "Search for a subshell embedded in a string. |
1046 | Find all the unescaped \" characters within said subshell, remembering that | |
1047 | subshells can nest." | |
69c6ad83 SM |
1048 | ;; FIXME: This can (and often does) match multiple lines, yet it makes no |
1049 | ;; effort to handle multiline cases correctly, so it ends up being | |
1050 | ;; rather flakey. | |
4c7c5c7e SM |
1051 | (when (and (re-search-forward "\"\\(?:\\(?:.\\|\n\\)*?[^\\]\\(?:\\\\\\\\\\)*\\)??\\(\\$(\\|`\\)" limit t) |
1052 | ;; Make sure the " we matched is an opening quote. | |
1053 | (eq ?\" (nth 3 (syntax-ppss)))) | |
3a723c3a SM |
1054 | ;; bingo we have a $( or a ` inside a "" |
1055 | (let ((char (char-after (point))) | |
4f3a3368 SM |
1056 | ;; `state' can be: double-quote, backquote, code. |
1057 | (state (if (eq (char-before) ?`) 'backquote 'code)) | |
1058 | ;; Stacked states in the context. | |
1059 | (states '(double-quote))) | |
23ae1f25 | 1060 | (while (and state (progn (skip-chars-forward "^'\\\\\"`$()" limit) |
4f3a3368 SM |
1061 | (< (point) limit))) |
1062 | ;; unescape " inside a $( ... ) construct. | |
1063 | (case (char-after) | |
c9176b9f SM |
1064 | (?\' (case state |
1065 | (double-quote nil) | |
1066 | (t (forward-char 1) (skip-chars-forward "^'" limit)))) | |
4f3a3368 SM |
1067 | (?\\ (forward-char 1)) |
1068 | (?\" (case state | |
1069 | (double-quote (setq state (pop states))) | |
1070 | (t (push state states) (setq state 'double-quote))) | |
1071 | (if state (put-text-property (point) (1+ (point)) | |
1072 | 'syntax-table '(1)))) | |
1073 | (?\` (case state | |
1074 | (backquote (setq state (pop states))) | |
1075 | (t (push state states) (setq state 'backquote)))) | |
1076 | (?\$ (if (not (eq (char-after (1+ (point))) ?\()) | |
1077 | nil | |
e58914d0 | 1078 | (forward-char 1) |
4f3a3368 SM |
1079 | (case state |
1080 | (t (push state states) (setq state 'code))))) | |
1081 | (?\( (case state | |
1082 | (double-quote nil) | |
1083 | (t (push state states) (setq state 'code)))) | |
1084 | (?\) (case state | |
1085 | (double-quote nil) | |
1086 | (t (setq state (pop states))))) | |
e58914d0 | 1087 | (t (error "Internal error in sh-font-lock-quoted-subshell"))) |
4f3a3368 SM |
1088 | (forward-char 1))) |
1089 | t)) | |
f24a26a5 | 1090 | |
6d39902f | 1091 | |
485219e0 SM |
1092 | (defun sh-is-quoted-p (pos) |
1093 | (and (eq (char-before pos) ?\\) | |
1094 | (not (sh-is-quoted-p (1- pos))))) | |
1095 | ||
34939e2c SM |
1096 | (defun sh-font-lock-paren (start) |
1097 | (save-excursion | |
1098 | (goto-char start) | |
1099 | ;; Skip through all patterns | |
1100 | (while | |
1101 | (progn | |
1102 | (forward-comment (- (point-max))) | |
1103 | ;; Skip through one pattern | |
1104 | (while | |
1105 | (or (/= 0 (skip-syntax-backward "w_")) | |
6b61353c | 1106 | (/= 0 (skip-chars-backward "?[]*@/\\")) |
485219e0 SM |
1107 | (and (sh-is-quoted-p (1- (point))) |
1108 | (goto-char (- (point) 2))) | |
34939e2c SM |
1109 | (when (memq (char-before) '(?\" ?\')) |
1110 | (condition-case nil (progn (backward-sexp 1) t) | |
1111 | (error nil))))) | |
bd24684b SM |
1112 | (while (progn |
1113 | (forward-comment (- (point-max))) | |
1114 | ;; Maybe we've bumped into an escaped newline. | |
1115 | (sh-is-quoted-p (point))) | |
1116 | (backward-char 1)) | |
34939e2c SM |
1117 | (when (eq (char-before) ?|) |
1118 | (backward-char 1) t))) | |
74ab576e SM |
1119 | ;; FIXME: ";; esac )" is a case that looks like a case-pattern but it's |
1120 | ;; really just a close paren after a case statement. I.e. if we skipped | |
1121 | ;; over `esac' just now, we're not looking at a case-pattern. | |
1122 | (when (progn (backward-char 2) | |
1123 | (if (> start (line-end-position)) | |
1124 | (put-text-property (point) (1+ start) | |
1125 | 'font-lock-multiline t)) | |
1126 | ;; FIXME: The `in' may just be a random argument to | |
1127 | ;; a normal command rather than the real `in' keyword. | |
1128 | ;; I.e. we should look back to try and find the | |
1129 | ;; corresponding `case'. | |
1130 | (looking-at ";;\\|in")) | |
34939e2c SM |
1131 | sh-st-punc))) |
1132 | ||
935e6b79 SM |
1133 | (defun sh-font-lock-backslash-quote () |
1134 | (if (eq (save-excursion (nth 3 (syntax-ppss (match-beginning 0)))) ?\') | |
1135 | ;; In a '...' the backslash is not escaping. | |
1136 | sh-st-punc | |
1137 | nil)) | |
1138 | ||
1139 | (defun sh-font-lock-flush-syntax-ppss-cache (limit) | |
1140 | ;; This should probably be a standard function provided by font-lock.el | |
1141 | ;; (or syntax.el). | |
1142 | (syntax-ppss-flush-cache (point)) | |
1143 | (goto-char limit) | |
1144 | nil) | |
6d39902f | 1145 | |
38c979d3 | 1146 | (defconst sh-font-lock-syntactic-keywords |
bffd712e SM |
1147 | ;; A `#' begins a comment when it is unquoted and at the beginning of a |
1148 | ;; word. In the shell, words are separated by metacharacters. | |
1149 | ;; The list of special chars is taken from the single-unix spec | |
1150 | ;; of the shell command language (under `quoting') but with `$' removed. | |
1151 | `(("[^|&;<>()`\\\"' \t\n]\\(#+\\)" 1 ,sh-st-symbol) | |
ec121b96 | 1152 | ;; In a '...' the backslash is not escaping. |
935e6b79 SM |
1153 | ("\\(\\\\\\)'" (1 (sh-font-lock-backslash-quote))) |
1154 | ;; The previous rule uses syntax-ppss, but the subsequent rules may | |
1155 | ;; change the syntax, so we have to tell syntax-ppss that the states it | |
1156 | ;; has just computed will need to be recomputed. | |
1157 | (sh-font-lock-flush-syntax-ppss-cache) | |
67f2eaf1 | 1158 | ;; Make sure $@ and $? are correctly recognized as sexps. |
1712cca5 | 1159 | ("\\$\\([?@]\\)" 1 ,sh-st-symbol) |
34939e2c | 1160 | ;; Find HEREDOC starters and add a corresponding rule for the ender. |
3e2dd647 SM |
1161 | (sh-font-lock-here-doc |
1162 | (2 (sh-font-lock-open-heredoc | |
1163 | (match-beginning 0) (match-string 1)) nil t) | |
1164 | (5 (sh-font-lock-close-heredoc | |
1165 | (match-beginning 0) (match-string 4) | |
187cd25b SS |
1166 | (and (match-beginning 3) (/= (match-beginning 3) (match-end 3)))) |
1167 | nil t)) | |
34939e2c | 1168 | ;; Distinguish the special close-paren in `case'. |
4c7c5c7e SM |
1169 | (")" 0 (sh-font-lock-paren (match-beginning 0))) |
1170 | ;; highlight (possibly nested) subshells inside "" quoted regions correctly. | |
1171 | ;; This should be at the very end because it uses syntax-ppss. | |
e58914d0 | 1172 | (sh-font-lock-quoted-subshell))) |
f964dfcb | 1173 | |
aace6150 | 1174 | (defun sh-font-lock-syntactic-face-function (state) |
6d39902f EZ |
1175 | (let ((q (nth 3 state))) |
1176 | (if q | |
b883cdb2 | 1177 | (if (characterp q) |
6d39902f EZ |
1178 | (if (eq q ?\`) 'sh-quoted-exec font-lock-string-face) |
1179 | sh-heredoc-face) | |
1180 | font-lock-comment-face))) | |
aace6150 | 1181 | |
f964dfcb GM |
1182 | (defgroup sh-indentation nil |
1183 | "Variables controlling indentation in shell scripts. | |
1184 | ||
1185 | Note: customizing these variables will not affect existing buffers if | |
1186 | `sh-make-vars-local' is no-nil. See the documentation for | |
1187 | variable `sh-make-vars-local', command `sh-make-vars-local' | |
1188 | and command `sh-reset-indent-vars-to-global-values'." | |
1189 | :group 'sh-script) | |
1190 | ||
1191 | ||
1192 | (defcustom sh-set-shell-hook nil | |
4f3a3368 | 1193 | "Hook run by `sh-set-shell'." |
6c5bcbc1 | 1194 | :type 'hook |
f964dfcb GM |
1195 | :group 'sh-script) |
1196 | ||
1197 | (defcustom sh-mode-hook nil | |
4f3a3368 | 1198 | "Hook run by `sh-mode'." |
6c5bcbc1 | 1199 | :type 'hook |
f964dfcb GM |
1200 | :group 'sh-script) |
1201 | ||
1202 | (defcustom sh-learn-basic-offset nil | |
4f3a3368 | 1203 | "When `sh-guess-basic-offset' should learn `sh-basic-offset'. |
f964dfcb GM |
1204 | |
1205 | nil mean: never. | |
1206 | t means: only if there seems to be an obvious value. | |
1207 | Anything else means: whenever we have a \"good guess\" as to the value." | |
1208 | :type '(choice | |
1209 | (const :tag "Never" nil) | |
1210 | (const :tag "Only if sure" t) | |
8db2b9fb | 1211 | (const :tag "If have a good guess" usually)) |
f964dfcb GM |
1212 | :group 'sh-indentation) |
1213 | ||
1214 | (defcustom sh-popup-occur-buffer nil | |
4f3a3368 | 1215 | "Controls when `sh-learn-buffer-indent' pops the `*indent*' buffer. |
8db2b9fb | 1216 | If t it is always shown. If nil, it is shown only when there |
f964dfcb GM |
1217 | are conflicts." |
1218 | :type '(choice | |
1219 | (const :tag "Only when there are conflicts." nil) | |
8db2b9fb | 1220 | (const :tag "Always" t)) |
f964dfcb GM |
1221 | :group 'sh-indentation) |
1222 | ||
1223 | (defcustom sh-blink t | |
4f3a3368 | 1224 | "If non-nil, `sh-show-indent' shows the line indentation is relative to. |
f964dfcb GM |
1225 | The position on the line is not necessarily meaningful. |
1226 | In some cases the line will be the matching keyword, but this is not | |
1227 | always the case." | |
1228 | :type 'boolean | |
1229 | :group 'sh-indentation) | |
1230 | ||
1231 | (defcustom sh-first-lines-indent 0 | |
4f3a3368 | 1232 | "The indentation of the first non-blank non-comment line. |
f964dfcb | 1233 | Usually 0 meaning first column. |
8db2b9fb | 1234 | Can be set to a number, or to nil which means leave it as is." |
f964dfcb GM |
1235 | :type '(choice |
1236 | (const :tag "Leave as is" nil) | |
1237 | (integer :tag "Column number" | |
8db2b9fb | 1238 | :menu-tag "Indent to this col (0 means first col)" )) |
f964dfcb GM |
1239 | :group 'sh-indentation) |
1240 | ||
1241 | ||
1242 | (defcustom sh-basic-offset 4 | |
4f3a3368 | 1243 | "The default indentation increment. |
dd77d6f4 | 1244 | This value is used for the `+' and `-' symbols in an indentation variable." |
f964dfcb GM |
1245 | :type 'integer |
1246 | :group 'sh-indentation) | |
fc9a9287 | 1247 | (put 'sh-basic-offset 'safe-local-variable 'integerp) |
f964dfcb GM |
1248 | |
1249 | (defcustom sh-indent-comment nil | |
4f3a3368 | 1250 | "How a comment line is to be indented. |
f964dfcb | 1251 | nil means leave it as it is; |
8db2b9fb | 1252 | t means indent it as a normal line, aligning it to previous non-blank |
f964dfcb | 1253 | non-comment line; |
7d86c380 | 1254 | a number means align to that column, e.g. 0 means first column." |
f964dfcb GM |
1255 | :type '(choice |
1256 | (const :tag "Leave as is." nil) | |
1257 | (const :tag "Indent as a normal line." t) | |
1258 | (integer :menu-tag "Indent to this col (0 means first col)." | |
6c5bcbc1 | 1259 | :tag "Indent to column number.") ) |
f964dfcb GM |
1260 | :group 'sh-indentation) |
1261 | ||
1262 | ||
1263 | (defvar sh-debug nil | |
1264 | "Enable lots of debug messages - if function `sh-debug' is enabled.") | |
1265 | ||
1266 | ||
1267 | ;; Uncomment this defun and comment the defmacro for debugging. | |
1268 | ;; (defun sh-debug (&rest args) | |
1269 | ;; "For debugging: display message ARGS if variable SH-DEBUG is non-nil." | |
1270 | ;; (if sh-debug | |
1271 | ;; (apply 'message args))) | |
1272 | (defmacro sh-debug (&rest args)) | |
1273 | ||
d92474ed SM |
1274 | (defconst sh-symbol-list |
1275 | '((const :tag "+ " :value + | |
1276 | :menu-tag "+ Indent right by sh-basic-offset") | |
1277 | (const :tag "- " :value - | |
1278 | :menu-tag "- Indent left by sh-basic-offset") | |
1279 | (const :tag "++" :value ++ | |
1280 | :menu-tag "++ Indent right twice sh-basic-offset") | |
1281 | (const :tag "--" :value -- | |
1282 | :menu-tag "-- Indent left twice sh-basic-offset") | |
1283 | (const :tag "* " :value * | |
1284 | :menu-tag "* Indent right half sh-basic-offset") | |
1285 | (const :tag "/ " :value / | |
1286 | :menu-tag "/ Indent left half sh-basic-offset"))) | |
f964dfcb GM |
1287 | |
1288 | (defcustom sh-indent-for-else 0 | |
4f3a3368 | 1289 | "How much to indent an `else' relative to its `if'. Usually 0." |
f964dfcb GM |
1290 | :type `(choice |
1291 | (integer :menu-tag "A number (positive=>indent right)" | |
1292 | :tag "A number") | |
1293 | (const :tag "--") ;; separator! | |
1294 | ,@ sh-symbol-list | |
1295 | ) | |
1296 | :group 'sh-indentation) | |
1297 | ||
d92474ed | 1298 | (defconst sh-number-or-symbol-list |
6c5bcbc1 SM |
1299 | (append '((integer :menu-tag "A number (positive=>indent right)" |
1300 | :tag "A number") | |
1301 | (const :tag "--")) ; separator | |
d92474ed | 1302 | sh-symbol-list)) |
f964dfcb GM |
1303 | |
1304 | (defcustom sh-indent-for-fi 0 | |
4f3a3368 | 1305 | "How much to indent a `fi' relative to its `if'. Usually 0." |
f964dfcb GM |
1306 | :type `(choice ,@ sh-number-or-symbol-list ) |
1307 | :group 'sh-indentation) | |
1308 | ||
dd77d6f4 | 1309 | (defcustom sh-indent-for-done 0 |
4f3a3368 | 1310 | "How much to indent a `done' relative to its matching stmt. Usually 0." |
f964dfcb GM |
1311 | :type `(choice ,@ sh-number-or-symbol-list ) |
1312 | :group 'sh-indentation) | |
1313 | ||
1314 | (defcustom sh-indent-after-else '+ | |
4f3a3368 | 1315 | "How much to indent a statement after an `else' statement." |
f964dfcb GM |
1316 | :type `(choice ,@ sh-number-or-symbol-list ) |
1317 | :group 'sh-indentation) | |
1318 | ||
1319 | (defcustom sh-indent-after-if '+ | |
4f3a3368 | 1320 | "How much to indent a statement after an `if' statement. |
dd77d6f4 RS |
1321 | This includes lines after `else' and `elif' statements, too, but |
1322 | does not affect the `else', `elif' or `fi' statements themselves." | |
f964dfcb GM |
1323 | :type `(choice ,@ sh-number-or-symbol-list ) |
1324 | :group 'sh-indentation) | |
1325 | ||
bae7df15 | 1326 | (defcustom sh-indent-for-then 0 |
4f3a3368 | 1327 | "How much to indent a `then' relative to its `if'." |
f964dfcb GM |
1328 | :type `(choice ,@ sh-number-or-symbol-list ) |
1329 | :group 'sh-indentation) | |
1330 | ||
3f0f48c0 | 1331 | (defcustom sh-indent-for-do 0 |
4f3a3368 | 1332 | "How much to indent a `do' statement. |
32518913 RS |
1333 | This is relative to the statement before the `do', typically a |
1334 | `while', `until', `for', `repeat' or `select' statement." | |
f964dfcb GM |
1335 | :type `(choice ,@ sh-number-or-symbol-list) |
1336 | :group 'sh-indentation) | |
1337 | ||
dd77d6f4 | 1338 | (defcustom sh-indent-after-do '+ |
4f3a3368 | 1339 | "How much to indent a line after a `do' statement. |
dd77d6f4 | 1340 | This is used when the `do' is the first word of the line. |
32518913 RS |
1341 | This is relative to the statement before the `do', typically a |
1342 | `while', `until', `for', `repeat' or `select' statement." | |
f964dfcb GM |
1343 | :type `(choice ,@ sh-number-or-symbol-list) |
1344 | :group 'sh-indentation) | |
1345 | ||
1346 | (defcustom sh-indent-after-loop-construct '+ | |
4f3a3368 | 1347 | "How much to indent a statement after a loop construct. |
f964dfcb | 1348 | |
dd77d6f4 RS |
1349 | This variable is used when the keyword `do' is on the same line as the |
1350 | loop statement (e.g., `until', `while' or `for'). | |
1351 | If the `do' is on a line by itself, then `sh-indent-after-do' is used instead." | |
f964dfcb GM |
1352 | :type `(choice ,@ sh-number-or-symbol-list) |
1353 | :group 'sh-indentation) | |
1354 | ||
1355 | ||
1356 | (defcustom sh-indent-after-done 0 | |
4f3a3368 | 1357 | "How much to indent a statement after a `done' keyword. |
dd77d6f4 | 1358 | Normally this is 0, which aligns the `done' to the matching |
f964dfcb | 1359 | looping construct line. |
dd77d6f4 | 1360 | Setting it non-zero allows you to have the `do' statement on a line |
f964dfcb GM |
1361 | by itself and align the done under to do." |
1362 | :type `(choice ,@ sh-number-or-symbol-list) | |
1363 | :group 'sh-indentation) | |
1364 | ||
1365 | (defcustom sh-indent-for-case-label '+ | |
4f3a3368 | 1366 | "How much to indent a case label statement. |
dd77d6f4 | 1367 | This is relative to the line containing the `case' statement." |
f964dfcb GM |
1368 | :type `(choice ,@ sh-number-or-symbol-list) |
1369 | :group 'sh-indentation) | |
1370 | ||
1371 | (defcustom sh-indent-for-case-alt '++ | |
4f3a3368 | 1372 | "How much to indent statements after the case label. |
dd77d6f4 | 1373 | This is relative to the line containing the `case' statement." |
f964dfcb GM |
1374 | :type `(choice ,@ sh-number-or-symbol-list) |
1375 | :group 'sh-indentation) | |
1376 | ||
1377 | ||
1378 | (defcustom sh-indent-for-continuation '+ | |
4f3a3368 | 1379 | "How much to indent for a continuation statement." |
f964dfcb GM |
1380 | :type `(choice ,@ sh-number-or-symbol-list) |
1381 | :group 'sh-indentation) | |
1382 | ||
1383 | (defcustom sh-indent-after-open '+ | |
4f3a3368 | 1384 | "How much to indent after a line with an opening parenthesis or brace. |
dd77d6f4 | 1385 | For an open paren after a function, `sh-indent-after-function' is used." |
f964dfcb GM |
1386 | :type `(choice ,@ sh-number-or-symbol-list) |
1387 | :group 'sh-indentation) | |
1388 | ||
1389 | (defcustom sh-indent-after-function '+ | |
4f3a3368 | 1390 | "How much to indent after a function line." |
f964dfcb GM |
1391 | :type `(choice ,@ sh-number-or-symbol-list) |
1392 | :group 'sh-indentation) | |
1393 | ||
1394 | ;; These 2 are for the rc shell: | |
1395 | ||
1396 | (defcustom sh-indent-after-switch '+ | |
4f3a3368 | 1397 | "How much to indent a `case' statement relative to the `switch' statement. |
f964dfcb GM |
1398 | This is for the rc shell." |
1399 | :type `(choice ,@ sh-number-or-symbol-list) | |
1400 | :group 'sh-indentation) | |
1401 | ||
1402 | (defcustom sh-indent-after-case '+ | |
4f3a3368 | 1403 | "How much to indent a statement relative to the `case' statement. |
f964dfcb GM |
1404 | This is for the rc shell." |
1405 | :type `(choice ,@ sh-number-or-symbol-list) | |
1406 | :group 'sh-indentation) | |
1407 | ||
bdd5fa99 | 1408 | (defcustom sh-backslash-column 48 |
4f3a3368 | 1409 | "Column in which `sh-backslash-region' inserts backslashes." |
bdd5fa99 RS |
1410 | :type 'integer |
1411 | :group 'sh) | |
1412 | ||
1413 | (defcustom sh-backslash-align t | |
4f3a3368 | 1414 | "If non-nil, `sh-backslash-region' will align backslashes." |
bdd5fa99 RS |
1415 | :type 'boolean |
1416 | :group 'sh) | |
1417 | ||
f964dfcb GM |
1418 | ;; Internal use - not designed to be changed by the user: |
1419 | ||
f964dfcb GM |
1420 | (defun sh-mkword-regexpr (word) |
1421 | "Make a regexp which matches WORD as a word. | |
8db2b9fb | 1422 | This specifically excludes an occurrence of WORD followed by |
f964dfcb | 1423 | punctuation characters like '-'." |
4f3a3368 | 1424 | (concat word "\\([^-[:alnum:]_]\\|$\\)")) |
f964dfcb | 1425 | |
d92474ed | 1426 | (defconst sh-re-done (sh-mkword-regexpr "done")) |
f964dfcb GM |
1427 | |
1428 | ||
1429 | (defconst sh-kws-for-done | |
d92474ed | 1430 | '((sh . ( "while" "until" "for" ) ) |
f964dfcb GM |
1431 | (bash . ( "while" "until" "for" "select" ) ) |
1432 | (ksh88 . ( "while" "until" "for" "select" ) ) | |
d92474ed SM |
1433 | (zsh . ( "while" "until" "for" "repeat" "select" ) ) ) |
1434 | "Which keywords can match the word `done' in this shell.") | |
f964dfcb GM |
1435 | |
1436 | ||
1437 | (defconst sh-indent-supported | |
d92474ed | 1438 | '((sh . t) |
f964dfcb | 1439 | (csh . nil) |
d92474ed SM |
1440 | (rc . t)) |
1441 | "Shell types that shell indenting can do something with.") | |
1442 | ||
1443 | (defvar sh-indent-supported-here nil | |
1444 | "Non-nil if we support indentation for the current buffer's shell type.") | |
f964dfcb | 1445 | |
f964dfcb GM |
1446 | (defconst sh-var-list |
1447 | '( | |
1448 | sh-basic-offset sh-first-lines-indent sh-indent-after-case | |
1449 | sh-indent-after-do sh-indent-after-done | |
1450 | sh-indent-after-else | |
1451 | sh-indent-after-if | |
1452 | sh-indent-after-loop-construct | |
1453 | sh-indent-after-open | |
1454 | sh-indent-comment | |
1455 | sh-indent-for-case-alt | |
1456 | sh-indent-for-case-label | |
1457 | sh-indent-for-continuation | |
1458 | sh-indent-for-do | |
1459 | sh-indent-for-done | |
1460 | sh-indent-for-else | |
1461 | sh-indent-for-fi | |
1462 | sh-indent-for-then | |
1463 | ) | |
1464 | "A list of variables used by script mode to control indentation. | |
1465 | This list is used when switching between buffer-local and global | |
8db2b9fb | 1466 | values of variables, and for the commands using indentation styles.") |
f964dfcb GM |
1467 | |
1468 | (defvar sh-make-vars-local t | |
1469 | "*Controls whether indentation variables are local to the buffer. | |
8db2b9fb SM |
1470 | If non-nil, indentation variables are made local initially. |
1471 | If nil, you can later make the variables local by invoking | |
f964dfcb GM |
1472 | command `sh-make-vars-local'. |
1473 | The default is t because I assume that in one Emacs session one is | |
1474 | frequently editing existing scripts with different styles.") | |
1475 | ||
133693bc KH |
1476 | \f |
1477 | ;; mode-command and utility functions | |
1478 | ||
fc8318f6 | 1479 | ;;;###autoload |
a7dba40b | 1480 | (defun sh-mode () |
ac59aed8 RS |
1481 | "Major mode for editing shell scripts. |
1482 | This mode works for many shells, since they all have roughly the same syntax, | |
1483 | as far as commands, arguments, variables, pipes, comments etc. are concerned. | |
1484 | Unless the file's magic number indicates the shell, your usual shell is | |
1485 | assumed. Since filenames rarely give a clue, they are not further analyzed. | |
1486 | ||
133693bc KH |
1487 | This mode adapts to the variations between shells (see `sh-set-shell') by |
1488 | means of an inheritance based feature lookup (see `sh-feature'). This | |
1489 | mechanism applies to all variables (including skeletons) that pertain to | |
1490 | shell-specific features. | |
ac59aed8 | 1491 | |
133693bc KH |
1492 | The default style of this mode is that of Rosenblatt's Korn shell book. |
1493 | The syntax of the statements varies with the shell being used. The | |
1494 | following commands are available, based on the current shell's syntax: | |
917e8d0b | 1495 | \\<sh-mode-map> |
ac59aed8 RS |
1496 | \\[sh-case] case statement |
1497 | \\[sh-for] for loop | |
1498 | \\[sh-function] function definition | |
1499 | \\[sh-if] if statement | |
1500 | \\[sh-indexed-loop] indexed loop from 1 to n | |
133693bc KH |
1501 | \\[sh-while-getopts] while getopts loop |
1502 | \\[sh-repeat] repeat loop | |
1503 | \\[sh-select] select loop | |
ac59aed8 RS |
1504 | \\[sh-until] until loop |
1505 | \\[sh-while] while loop | |
1506 | ||
f964dfcb GM |
1507 | For sh and rc shells indentation commands are: |
1508 | \\[sh-show-indent] Show the variable controlling this line's indentation. | |
1509 | \\[sh-set-indent] Set then variable controlling this line's indentation. | |
1510 | \\[sh-learn-line-indent] Change the indentation variable so this line | |
1511 | would indent to the way it currently is. | |
1512 | \\[sh-learn-buffer-indent] Set the indentation variables so the | |
8db2b9fb | 1513 | buffer indents as it currently is indented. |
f964dfcb GM |
1514 | |
1515 | ||
ac59aed8 | 1516 | \\[backward-delete-char-untabify] Delete backward one position, even if it was a tab. |
2ae8360b | 1517 | \\[newline-and-indent] Delete unquoted space and indent new line same as this one. |
ac59aed8 RS |
1518 | \\[sh-end-of-command] Go to end of successive commands. |
1519 | \\[sh-beginning-of-command] Go to beginning of successive commands. | |
1520 | \\[sh-set-shell] Set this buffer's shell, and maybe its magic number. | |
133693bc | 1521 | \\[sh-execute-region] Have optional header and region be executed in a subshell. |
ac59aed8 | 1522 | |
ac59aed8 | 1523 | \\[sh-maybe-here-document] Without prefix, following an unquoted < inserts here document. |
b52b132f | 1524 | \{, (, [, ', \", ` |
133693bc KH |
1525 | Unless quoted with \\, insert the pairs {}, (), [], or '', \"\", ``. |
1526 | ||
1527 | If you generally program a shell different from your login shell you can | |
aafd074a | 1528 | set `sh-shell-file' accordingly. If your shell's file name doesn't correctly |
133693bc KH |
1529 | indicate what shell it is use `sh-alias-alist' to translate. |
1530 | ||
1531 | If your shell gives error messages with line numbers, you can use \\[executable-interpret] | |
1532 | with your script for an edit-interpret-debug cycle." | |
a7dba40b RS |
1533 | (interactive) |
1534 | (kill-all-local-variables) | |
1535 | (setq major-mode 'sh-mode | |
1536 | mode-name "Shell-script") | |
1537 | (use-local-map sh-mode-map) | |
84bfbb44 | 1538 | (make-local-variable 'skeleton-end-hook) |
133693bc KH |
1539 | (make-local-variable 'paragraph-start) |
1540 | (make-local-variable 'paragraph-separate) | |
ac59aed8 RS |
1541 | (make-local-variable 'comment-start) |
1542 | (make-local-variable 'comment-start-skip) | |
ac59aed8 | 1543 | (make-local-variable 'require-final-newline) |
133693bc | 1544 | (make-local-variable 'sh-header-marker) |
aafd074a | 1545 | (make-local-variable 'sh-shell-file) |
ac59aed8 | 1546 | (make-local-variable 'sh-shell) |
81ed2d75 | 1547 | (make-local-variable 'skeleton-pair-alist) |
21225d8f | 1548 | (make-local-variable 'skeleton-pair-filter-function) |
133693bc KH |
1549 | (make-local-variable 'comint-dynamic-complete-functions) |
1550 | (make-local-variable 'comint-prompt-regexp) | |
84bfbb44 | 1551 | (make-local-variable 'font-lock-defaults) |
62fda6d6 | 1552 | (make-local-variable 'skeleton-filter-function) |
cd76025c | 1553 | (make-local-variable 'skeleton-newline-indent-rigidly) |
5d73ac66 RS |
1554 | (make-local-variable 'sh-shell-variables) |
1555 | (make-local-variable 'sh-shell-variables-initialized) | |
aa2c2426 | 1556 | (make-local-variable 'imenu-generic-expression) |
f964dfcb | 1557 | (make-local-variable 'sh-indent-supported-here) |
0404a075 RS |
1558 | (make-local-variable 'skeleton-pair-default-alist) |
1559 | (setq skeleton-pair-default-alist sh-skeleton-pair-default-alist) | |
aace6150 | 1560 | (setq skeleton-end-hook (lambda () |
84bfbb44 | 1561 | (or (eolp) (newline) (indent-relative))) |
bfc8e97b | 1562 | paragraph-start (concat page-delimiter "\\|$") |
133693bc | 1563 | paragraph-separate paragraph-start |
ac59aed8 | 1564 | comment-start "# " |
5789bd83 RS |
1565 | comment-start-skip "#+[\t ]*" |
1566 | local-abbrev-table sh-mode-abbrev-table | |
133693bc KH |
1567 | comint-dynamic-complete-functions sh-dynamic-complete-functions |
1568 | ;; we can't look if previous line ended with `\' | |
1569 | comint-prompt-regexp "^[ \t]*" | |
5789bd83 | 1570 | imenu-case-fold-search nil |
84bfbb44 | 1571 | font-lock-defaults |
34939e2c | 1572 | `((sh-font-lock-keywords |
38c979d3 SM |
1573 | sh-font-lock-keywords-1 sh-font-lock-keywords-2) |
1574 | nil nil | |
1575 | ((?/ . "w") (?~ . "w") (?. . "w") (?- . "w") (?_ . "w")) nil | |
3e2dd647 | 1576 | (font-lock-syntactic-keywords . sh-font-lock-syntactic-keywords) |
aace6150 SM |
1577 | (font-lock-syntactic-face-function |
1578 | . sh-font-lock-syntactic-face-function)) | |
81ed2d75 | 1579 | skeleton-pair-alist '((?` _ ?`)) |
21225d8f | 1580 | skeleton-pair-filter-function 'sh-quoted-p |
133693bc KH |
1581 | skeleton-further-elements '((< '(- (min sh-indentation |
1582 | (current-column))))) | |
62fda6d6 | 1583 | skeleton-filter-function 'sh-feature |
f964dfcb | 1584 | skeleton-newline-indent-rigidly t |
f964dfcb | 1585 | sh-indent-supported-here nil) |
b6ddf8d7 SM |
1586 | (set (make-local-variable 'defun-prompt-regexp) |
1587 | (concat "^\\(function[ \t]\\|[[:alnum:]]+[ \t]+()[ \t]+\\)")) | |
6c5bcbc1 | 1588 | (set (make-local-variable 'parse-sexp-ignore-comments) t) |
e3dce9ba RS |
1589 | ;; Parse or insert magic number for exec, and set all variables depending |
1590 | ;; on the shell thus determined. | |
19cd88cc TTN |
1591 | (sh-set-shell |
1592 | (cond ((save-excursion | |
1593 | (goto-char (point-min)) | |
1594 | (looking-at "#![ \t]?\\([^ \t\n]*/bin/env[ \t]\\)?\\([^ \t\n]+\\)")) | |
1595 | (match-string 2)) | |
1596 | ((not buffer-file-name) | |
1597 | sh-shell-file) | |
1598 | ;; Checks that use `buffer-file-name' follow. | |
1599 | ((string-match "\\.m?spec\\'" buffer-file-name) | |
1600 | "rpm") | |
1601 | ((string-match "[.]sh\\>" buffer-file-name) | |
1602 | "sh") | |
1603 | ((string-match "[.]bash\\>" buffer-file-name) | |
1604 | "bash") | |
1605 | ((string-match "[.]ksh\\>" buffer-file-name) | |
1606 | "ksh") | |
1607 | ((string-match "[.]csh\\>" buffer-file-name) | |
1608 | "csh") | |
68c2f86d RS |
1609 | ((equal (file-name-nondirectory buffer-file-name) ".profile") |
1610 | "sh") | |
19cd88cc TTN |
1611 | (t |
1612 | sh-shell-file)) | |
1613 | nil nil) | |
9a969196 | 1614 | (run-mode-hooks 'sh-mode-hook)) |
aace6150 | 1615 | |
133693bc | 1616 | ;;;###autoload |
ac59aed8 RS |
1617 | (defalias 'shell-script-mode 'sh-mode) |
1618 | ||
1619 | ||
84bfbb44 KH |
1620 | (defun sh-font-lock-keywords (&optional keywords) |
1621 | "Function to get simple fontification based on `sh-font-lock-keywords'. | |
1622 | This adds rules for comments and assignments." | |
5789bd83 | 1623 | (sh-feature sh-font-lock-keywords-var |
b946fbad RS |
1624 | (when (stringp (sh-feature sh-assignment-regexp)) |
1625 | (lambda (list) | |
1626 | `((,(sh-feature sh-assignment-regexp) | |
1627 | 1 font-lock-variable-name-face) | |
1628 | ,@keywords | |
5789bd83 RS |
1629 | ,@list |
1630 | ,@executable-font-lock-keywords))))) | |
84bfbb44 KH |
1631 | |
1632 | (defun sh-font-lock-keywords-1 (&optional builtins) | |
1633 | "Function to get better fontification including keywords." | |
3e2dd647 SM |
1634 | (let ((keywords (concat "\\([;(){}`|&]\\|^\\)[ \t]*\\(\\(" |
1635 | (regexp-opt (sh-feature sh-leading-keywords) t) | |
1636 | "[ \t]+\\)?" | |
1637 | (regexp-opt (append (sh-feature sh-leading-keywords) | |
1638 | (sh-feature sh-other-keywords)) | |
1639 | t)))) | |
84bfbb44 KH |
1640 | (sh-font-lock-keywords |
1641 | `(,@(if builtins | |
3e2dd647 SM |
1642 | `((,(concat keywords "[ \t]+\\)?" |
1643 | (regexp-opt (sh-feature sh-builtins) t) | |
1644 | "\\>") | |
84bfbb44 | 1645 | (2 font-lock-keyword-face nil t) |
f802bd02 | 1646 | (6 font-lock-builtin-face)) |
5789bd83 | 1647 | ,@(sh-feature sh-font-lock-keywords-var-2))) |
84bfbb44 KH |
1648 | (,(concat keywords "\\)\\>") |
1649 | 2 font-lock-keyword-face) | |
5789bd83 | 1650 | ,@(sh-feature sh-font-lock-keywords-var-1))))) |
84bfbb44 KH |
1651 | |
1652 | (defun sh-font-lock-keywords-2 () | |
1653 | "Function to get better fontification including keywords and builtins." | |
1654 | (sh-font-lock-keywords-1 t)) | |
1655 | ||
ac59aed8 | 1656 | |
d92474ed SM |
1657 | (defvar sh-regexp-for-done nil |
1658 | "A buffer-local regexp to match opening keyword for done.") | |
1659 | ||
1660 | (defvar sh-kw-alist nil | |
1661 | "A buffer-local, since it is shell-type dependent, list of keywords.") | |
1662 | ||
1663 | ;; ( key-word first-on-this on-prev-line ) | |
1664 | ;; This is used to set `sh-kw-alist' which is a list of sublists each | |
1665 | ;; having 3 elements: | |
1666 | ;; a keyword | |
8db2b9fb SM |
1667 | ;; a rule to check when the keyword appears on "this" line |
1668 | ;; a rule to check when the keyword appears on "the previous" line | |
d92474ed | 1669 | ;; The keyword is usually a string and is the first word on a line. |
8db2b9fb SM |
1670 | ;; If this keyword appears on the line whose indentation is to be |
1671 | ;; calculated, the rule in element 2 is called. If this returns | |
1672 | ;; non-zero, the resulting point (which may be changed by the rule) | |
d92474ed SM |
1673 | ;; is used as the default indentation. |
1674 | ;; If it returned false or the keyword was not found in the table, | |
1675 | ;; then the keyword from the previous line is looked up and the rule | |
1676 | ;; in element 3 is called. In this case, however, | |
8db2b9fb | 1677 | ;; `sh-get-indent-info' does not stop but may keep going and test |
d92474ed | 1678 | ;; other keywords against rules in element 3. This is because the |
8db2b9fb | 1679 | ;; preceding line could have, for example, an opening "if" and an |
d92474ed SM |
1680 | ;; opening "while" keyword and we need to add the indentation offsets |
1681 | ;; for both. | |
1682 | ;; | |
1683 | (defconst sh-kw | |
6c5bcbc1 SM |
1684 | '((sh |
1685 | ("if" nil sh-handle-prev-if) | |
1686 | ("elif" sh-handle-this-else sh-handle-prev-else) | |
1687 | ("else" sh-handle-this-else sh-handle-prev-else) | |
1688 | ("fi" sh-handle-this-fi sh-handle-prev-fi) | |
1689 | ("then" sh-handle-this-then sh-handle-prev-then) | |
1690 | ("(" nil sh-handle-prev-open) | |
1691 | ("{" nil sh-handle-prev-open) | |
1692 | ("[" nil sh-handle-prev-open) | |
1693 | ("}" sh-handle-this-close nil) | |
1694 | (")" sh-handle-this-close nil) | |
1695 | ("]" sh-handle-this-close nil) | |
1696 | ("case" nil sh-handle-prev-case) | |
1697 | ("esac" sh-handle-this-esac sh-handle-prev-esac) | |
1698 | (case-label nil sh-handle-after-case-label) ;; ??? | |
1699 | (";;" nil sh-handle-prev-case-alt-end) ;; ??? | |
1700 | ("done" sh-handle-this-done sh-handle-prev-done) | |
1701 | ("do" sh-handle-this-do sh-handle-prev-do)) | |
d92474ed SM |
1702 | |
1703 | ;; Note: we don't need specific stuff for bash and zsh shells; | |
1704 | ;; the regexp `sh-regexp-for-done' handles the extra keywords | |
1705 | ;; these shells use. | |
1706 | (rc | |
6c5bcbc1 SM |
1707 | ("{" nil sh-handle-prev-open) |
1708 | ("}" sh-handle-this-close nil) | |
1709 | ("case" sh-handle-this-rc-case sh-handle-prev-rc-case)))) | |
d92474ed SM |
1710 | |
1711 | ||
5789bd83 | 1712 | |
616db04b | 1713 | (defun sh-set-shell (shell &optional no-query-flag insert-flag) |
133693bc | 1714 | "Set this buffer's shell to SHELL (a string). |
57270c90 RS |
1715 | When used interactively, insert the proper starting #!-line, |
1716 | and make the visited file executable via `executable-set-magic', | |
1717 | perhaps querying depending on the value of `executable-query'. | |
1718 | ||
1719 | When this function is called noninteractively, INSERT-FLAG (the third | |
1720 | argument) controls whether to insert a #!-line and think about making | |
1721 | the visited file executable, and NO-QUERY-FLAG (the second argument) | |
1722 | controls whether to query about making the visited file executable. | |
1723 | ||
133693bc | 1724 | Calls the value of `sh-set-shell-hook' if set." |
54aaebc5 JB |
1725 | (interactive (list (completing-read (format "Shell \(default %s\): " |
1726 | sh-shell-file) | |
1727 | interpreter-mode-alist | |
1728 | (lambda (x) (eq (cdr x) 'sh-mode)) | |
1729 | nil nil nil sh-shell-file) | |
616db04b RS |
1730 | (eq executable-query 'function) |
1731 | t)) | |
842cc0e6 | 1732 | (if (string-match "\\.exe\\'" shell) |
c8b88e9f | 1733 | (setq shell (substring shell 0 (match-beginning 0)))) |
133693bc KH |
1734 | (setq sh-shell (intern (file-name-nondirectory shell)) |
1735 | sh-shell (or (cdr (assq sh-shell sh-alias-alist)) | |
616db04b | 1736 | sh-shell)) |
e3dce9ba RS |
1737 | (if insert-flag |
1738 | (setq sh-shell-file | |
1739 | (executable-set-magic shell (sh-feature sh-shell-arg) | |
1740 | no-query-flag insert-flag))) | |
6b61353c | 1741 | (let ((tem (sh-feature sh-require-final-newline))) |
61871c70 RS |
1742 | (if (eq tem t) |
1743 | (setq require-final-newline mode-require-final-newline))) | |
6b61353c | 1744 | (setq |
133693bc | 1745 | mode-line-process (format "[%s]" sh-shell) |
5a989d6e | 1746 | sh-shell-variables nil |
5d73ac66 | 1747 | sh-shell-variables-initialized nil |
5789bd83 | 1748 | imenu-generic-expression (sh-feature sh-imenu-generic-expression)) |
6b61353c KH |
1749 | (make-local-variable 'sh-mode-syntax-table) |
1750 | (let ((tem (sh-feature sh-mode-syntax-table-input))) | |
1751 | (setq sh-mode-syntax-table | |
1752 | (if tem (apply 'sh-mode-syntax-table tem) | |
1753 | sh-mode-default-syntax-table))) | |
1754 | (set-syntax-table sh-mode-syntax-table) | |
6c5bcbc1 SM |
1755 | (dolist (var (sh-feature sh-variables)) |
1756 | (sh-remember-variable var)) | |
1757 | (make-local-variable 'indent-line-function) | |
f964dfcb GM |
1758 | (if (setq sh-indent-supported-here (sh-feature sh-indent-supported)) |
1759 | (progn | |
1760 | (message "Setting up indent for shell type %s" sh-shell) | |
6c5bcbc1 | 1761 | (set (make-local-variable 'parse-sexp-lookup-properties) t) |
6c5bcbc1 | 1762 | (set (make-local-variable 'sh-kw-alist) (sh-feature sh-kw)) |
f964dfcb GM |
1763 | (let ((regexp (sh-feature sh-kws-for-done))) |
1764 | (if regexp | |
6c5bcbc1 SM |
1765 | (set (make-local-variable 'sh-regexp-for-done) |
1766 | (sh-mkword-regexpr (regexp-opt regexp t))))) | |
f964dfcb GM |
1767 | (message "setting up indent stuff") |
1768 | ;; sh-mode has already made indent-line-function local | |
1769 | ;; but do it in case this is called before that. | |
f964dfcb | 1770 | (setq indent-line-function 'sh-indent-line) |
f964dfcb GM |
1771 | (if sh-make-vars-local |
1772 | (sh-make-vars-local)) | |
1773 | (message "Indentation setup for shell type %s" sh-shell)) | |
1774 | (message "No indentation for this shell type.") | |
1775 | (setq indent-line-function 'sh-basic-indent-line)) | |
5789bd83 RS |
1776 | (when font-lock-mode |
1777 | (setq font-lock-set-defaults nil) | |
1778 | (font-lock-set-defaults) | |
1779 | (font-lock-fontify-buffer)) | |
133693bc KH |
1780 | (run-hooks 'sh-set-shell-hook)) |
1781 | ||
1782 | ||
6b61353c | 1783 | (defun sh-feature (alist &optional function) |
133693bc KH |
1784 | "Index ALIST by the current shell. |
1785 | If ALIST isn't a list where every element is a cons, it is returned as is. | |
1786 | Else indexing follows an inheritance logic which works in two ways: | |
1787 | ||
1788 | - Fall back on successive ancestors (see `sh-ancestor-alist') as long as | |
1789 | the alist contains no value for the current shell. | |
1c64011b | 1790 | The ultimate default is always `sh'. |
133693bc | 1791 | |
6b61353c KH |
1792 | - If the value thus looked up is a list starting with `sh-append', |
1793 | we call the function `sh-append' with the rest of the list as | |
1794 | arguments, and use the value. However, the next element of the | |
1795 | list is not used as-is; instead, we look it up recursively | |
1796 | in ALIST to allow the function called to define the value for | |
1797 | one shell to be derived from another shell. | |
133693bc KH |
1798 | The value thus determined is physically replaced into the alist. |
1799 | ||
5789bd83 RS |
1800 | If FUNCTION is non-nil, it is called with one argument, |
1801 | the value thus obtained, and the result is used instead." | |
6b61353c | 1802 | (or (if (consp alist) |
5789bd83 | 1803 | ;; Check for something that isn't a valid alist. |
6b61353c | 1804 | (let ((l alist)) |
133693bc KH |
1805 | (while (and l (consp (car l))) |
1806 | (setq l (cdr l))) | |
6b61353c | 1807 | (if l alist))) |
5789bd83 RS |
1808 | |
1809 | (let ((orig-sh-shell sh-shell)) | |
1810 | (let ((sh-shell sh-shell) | |
1811 | elt val) | |
1812 | (while (and sh-shell | |
1813 | (not (setq elt (assq sh-shell alist)))) | |
1814 | (setq sh-shell (cdr (assq sh-shell sh-ancestor-alist)))) | |
1815 | ;; If the shell is not known, treat it as sh. | |
1816 | (unless elt | |
1817 | (setq elt (assq 'sh alist))) | |
1818 | (setq val (cdr elt)) | |
1819 | (if (and (consp val) | |
1820 | (memq (car val) '(sh-append sh-modify))) | |
1821 | (setq val | |
1822 | (apply (car val) | |
1823 | ;; Refer to the value for a different shell, | |
1824 | ;; as a kind of inheritance. | |
1825 | (let ((sh-shell (car (cdr val)))) | |
1826 | (sh-feature alist)) | |
1827 | (cddr val)))) | |
1828 | (if function | |
1829 | (setq sh-shell orig-sh-shell | |
1830 | val (funcall function val))) | |
1831 | val)))) | |
133693bc KH |
1832 | |
1833 | ||
1834 | ||
3e2dd647 SM |
1835 | ;; I commented this out because nobody calls it -- rms. |
1836 | ;;(defun sh-abbrevs (ancestor &rest list) | |
1837 | ;; "Iff it isn't, define the current shell as abbrev table and fill that. | |
1838 | ;;Abbrev table will inherit all abbrevs from ANCESTOR, which is either an abbrev | |
1839 | ;;table or a list of (NAME1 EXPANSION1 ...). In addition it will define abbrevs | |
1840 | ;;according to the remaining arguments NAMEi EXPANSIONi ... | |
1841 | ;;EXPANSION may be either a string or a skeleton command." | |
1842 | ;; (or (if (boundp sh-shell) | |
1843 | ;; (symbol-value sh-shell)) | |
1844 | ;; (progn | |
1845 | ;; (if (listp ancestor) | |
1846 | ;; (nconc list ancestor)) | |
1847 | ;; (define-abbrev-table sh-shell ()) | |
1848 | ;; (if (vectorp ancestor) | |
1849 | ;; (mapatoms (lambda (atom) | |
1850 | ;; (or (eq atom 0) | |
1851 | ;; (define-abbrev (symbol-value sh-shell) | |
1852 | ;; (symbol-name atom) | |
1853 | ;; (symbol-value atom) | |
1854 | ;; (symbol-function atom)))) | |
1855 | ;; ancestor)) | |
1856 | ;; (while list | |
1857 | ;; (define-abbrev (symbol-value sh-shell) | |
1858 | ;; (car list) | |
1859 | ;; (if (stringp (car (cdr list))) | |
1860 | ;; (car (cdr list)) | |
1861 | ;; "") | |
1862 | ;; (if (symbolp (car (cdr list))) | |
1863 | ;; (car (cdr list)))) | |
1864 | ;; (setq list (cdr (cdr list))))) | |
1865 | ;; (symbol-value sh-shell))) | |
133693bc KH |
1866 | |
1867 | ||
133693bc KH |
1868 | (defun sh-append (ancestor &rest list) |
1869 | "Return list composed of first argument (a list) physically appended to rest." | |
1870 | (nconc list ancestor)) | |
1871 | ||
1872 | ||
1873 | (defun sh-modify (skeleton &rest list) | |
1874 | "Modify a copy of SKELETON by replacing I1 with REPL1, I2 with REPL2 ..." | |
1875 | (setq skeleton (copy-sequence skeleton)) | |
1876 | (while list | |
1877 | (setcar (or (nthcdr (car list) skeleton) | |
1878 | (error "Index %d out of bounds" (car list))) | |
1879 | (car (cdr list))) | |
1880 | (setq list (nthcdr 2 list))) | |
1881 | skeleton) | |
ac59aed8 RS |
1882 | |
1883 | ||
f964dfcb | 1884 | (defun sh-basic-indent-line () |
54c87e27 RS |
1885 | "Indent a line for Sh mode (shell script mode). |
1886 | Indent as far as preceding non-empty line, then by steps of `sh-indentation'. | |
133693bc | 1887 | Lines containing only comments are considered empty." |
ac59aed8 RS |
1888 | (interactive) |
1889 | (let ((previous (save-excursion | |
54c87e27 RS |
1890 | (while (and (progn (beginning-of-line) |
1891 | (not (bobp))) | |
b46c06de RS |
1892 | (progn |
1893 | (forward-line -1) | |
1894 | (back-to-indentation) | |
1895 | (or (eolp) | |
1896 | (eq (following-char) ?#))))) | |
133693bc KH |
1897 | (current-column))) |
1898 | current) | |
ac59aed8 RS |
1899 | (save-excursion |
1900 | (indent-to (if (eq this-command 'newline-and-indent) | |
1901 | previous | |
1902 | (if (< (current-column) | |
133693bc KH |
1903 | (setq current (progn (back-to-indentation) |
1904 | (current-column)))) | |
ac59aed8 | 1905 | (if (eolp) previous 0) |
133693bc KH |
1906 | (delete-region (point) |
1907 | (progn (beginning-of-line) (point))) | |
ac59aed8 | 1908 | (if (eolp) |
133693bc KH |
1909 | (max previous (* (1+ (/ current sh-indentation)) |
1910 | sh-indentation)) | |
1911 | (* (1+ (/ current sh-indentation)) sh-indentation)))))) | |
1912 | (if (< (current-column) (current-indentation)) | |
1913 | (skip-chars-forward " \t")))) | |
1914 | ||
1915 | ||
1916 | (defun sh-execute-region (start end &optional flag) | |
1917 | "Pass optional header and region to a subshell for noninteractive execution. | |
1918 | The working directory is that of the buffer, and only environment variables | |
1919 | are already set which is why you can mark a header within the script. | |
1920 | ||
1921 | With a positive prefix ARG, instead of sending region, define header from | |
1922 | beginning of buffer to point. With a negative prefix ARG, instead of sending | |
1923 | region, clear header." | |
1924 | (interactive "r\nP") | |
1925 | (if flag | |
1926 | (setq sh-header-marker (if (> (prefix-numeric-value flag) 0) | |
1927 | (point-marker))) | |
1928 | (if sh-header-marker | |
1929 | (save-excursion | |
1930 | (let (buffer-undo-list) | |
1931 | (goto-char sh-header-marker) | |
1932 | (append-to-buffer (current-buffer) start end) | |
1933 | (shell-command-on-region (point-min) | |
1934 | (setq end (+ sh-header-marker | |
1935 | (- end start))) | |
aafd074a | 1936 | sh-shell-file) |
133693bc | 1937 | (delete-region sh-header-marker end))) |
aafd074a | 1938 | (shell-command-on-region start end (concat sh-shell-file " -"))))) |
ac59aed8 RS |
1939 | |
1940 | ||
1941 | (defun sh-remember-variable (var) | |
1942 | "Make VARIABLE available for future completing reads in this buffer." | |
1943 | (or (< (length var) sh-remember-variable-min) | |
133693bc | 1944 | (getenv var) |
5a989d6e | 1945 | (assoc var sh-shell-variables) |
6c5bcbc1 | 1946 | (push (cons var var) sh-shell-variables)) |
ac59aed8 RS |
1947 | var) |
1948 | ||
1949 | ||
ac59aed8 RS |
1950 | |
1951 | (defun sh-quoted-p () | |
1952 | "Is point preceded by an odd number of backslashes?" | |
133693bc | 1953 | (eq -1 (% (save-excursion (skip-chars-backward "\\\\")) 2))) |
ac59aed8 | 1954 | \f |
f964dfcb | 1955 | ;; Indentation stuff. |
f964dfcb GM |
1956 | (defun sh-must-support-indent () |
1957 | "*Signal an error if the shell type for this buffer is not supported. | |
8db2b9fb | 1958 | Also, the buffer must be in Shell-script mode." |
f964dfcb | 1959 | (unless sh-indent-supported-here |
4aa3ba0a | 1960 | (error "This buffer's shell does not support indentation through Emacs"))) |
f964dfcb GM |
1961 | |
1962 | (defun sh-make-vars-local () | |
1963 | "Make the indentation variables local to this buffer. | |
1964 | Normally they already are local. This command is provided in case | |
1965 | variable `sh-make-vars-local' has been set to nil. | |
1966 | ||
8db2b9fb | 1967 | To revert all these variables to the global values, use |
f964dfcb GM |
1968 | command `sh-reset-indent-vars-to-global-values'." |
1969 | (interactive) | |
f24a26a5 | 1970 | (mapc 'make-local-variable sh-var-list) |
3307f085 | 1971 | (message "Indentation variables are now local.")) |
f964dfcb GM |
1972 | |
1973 | (defun sh-reset-indent-vars-to-global-values () | |
8db2b9fb SM |
1974 | "Reset local indentation variables to the global values. |
1975 | Then, if variable `sh-make-vars-local' is non-nil, make them local." | |
f964dfcb | 1976 | (interactive) |
f24a26a5 | 1977 | (mapc 'kill-local-variable sh-var-list) |
f964dfcb GM |
1978 | (if sh-make-vars-local |
1979 | (mapcar 'make-local-variable sh-var-list))) | |
1980 | ||
1981 | ||
f964dfcb GM |
1982 | ;; Theoretically these are only needed in shell and derived modes. |
1983 | ;; However, the routines which use them are only called in those modes. | |
1984 | (defconst sh-special-keywords "then\\|do") | |
1985 | ||
f964dfcb GM |
1986 | (defun sh-help-string-for-variable (var) |
1987 | "Construct a string for `sh-read-variable' when changing variable VAR ." | |
1988 | (let ((msg (documentation-property var 'variable-documentation)) | |
1989 | (msg2 "")) | |
6c5bcbc1 | 1990 | (unless (memq var '(sh-first-lines-indent sh-indent-comment)) |
f964dfcb GM |
1991 | (setq msg2 |
1992 | (format "\n | |
8db2b9fb SM |
1993 | You can enter a number (positive to increase indentation, |
1994 | negative to decrease indentation, zero for no change to indentation). | |
f964dfcb | 1995 | |
8db2b9fb | 1996 | Or, you can enter one of the following symbols which are relative to |
f964dfcb GM |
1997 | the value of variable `sh-basic-offset' |
1998 | which in this buffer is currently %s. | |
1999 | ||
2000 | \t%s." | |
2001 | sh-basic-offset | |
3e2dd647 SM |
2002 | (mapconcat (lambda (x) |
2003 | (nth (1- (length x)) x)) | |
2004 | sh-symbol-list "\n\t")))) | |
f964dfcb GM |
2005 | (concat |
2006 | ;; The following shows the global not the local value! | |
2007 | ;; (format "Current value of %s is %s\n\n" var (symbol-value var)) | |
2008 | msg msg2))) | |
2009 | ||
2010 | (defun sh-read-variable (var) | |
2011 | "Read a new value for indentation variable VAR." | |
2012 | (interactive "*variable? ") ;; to test | |
2013 | (let ((minibuffer-help-form `(sh-help-string-for-variable | |
2014 | (quote ,var))) | |
2015 | val) | |
2016 | (setq val (read-from-minibuffer | |
6c5bcbc1 SM |
2017 | (format "New value for %s (press %s for help): " |
2018 | var (single-key-description help-char)) | |
2019 | (format "%s" (symbol-value var)) | |
2020 | nil t)) | |
f964dfcb GM |
2021 | val)) |
2022 | ||
2023 | ||
2024 | ||
2025 | (defun sh-in-comment-or-string (start) | |
2026 | "Return non-nil if START is in a comment or string." | |
2027 | (save-excursion | |
3e2dd647 SM |
2028 | (let ((state (syntax-ppss start))) |
2029 | (or (nth 3 state) (nth 4 state))))) | |
f964dfcb GM |
2030 | |
2031 | (defun sh-goto-matching-if () | |
2032 | "Go to the matching if for a fi. | |
2033 | This handles nested if..fi pairs." | |
2034 | (let ((found (sh-find-prev-matching "\\bif\\b" "\\bfi\\b" 1))) | |
2035 | (if found | |
2036 | (goto-char found)))) | |
2037 | ||
2038 | ||
2039 | ;; Functions named sh-handle-this-XXX are called when the keyword on the | |
2040 | ;; line whose indentation is being handled contain XXX; | |
8db2b9fb | 2041 | ;; those named sh-handle-prev-XXX are when XXX appears on the previous line. |
f964dfcb GM |
2042 | |
2043 | (defun sh-handle-prev-if () | |
2044 | (list '(+ sh-indent-after-if))) | |
2045 | ||
2046 | (defun sh-handle-this-else () | |
2047 | (if (sh-goto-matching-if) | |
2048 | ;; (list "aligned to if") | |
2049 | (list "aligned to if" '(+ sh-indent-for-else)) | |
2050 | nil | |
2051 | )) | |
2052 | ||
2053 | (defun sh-handle-prev-else () | |
2054 | (if (sh-goto-matching-if) | |
2055 | (list '(+ sh-indent-after-if)) | |
2056 | )) | |
2057 | ||
2058 | (defun sh-handle-this-fi () | |
2059 | (if (sh-goto-matching-if) | |
2060 | (list "aligned to if" '(+ sh-indent-for-fi)) | |
2061 | nil | |
2062 | )) | |
2063 | ||
2064 | (defun sh-handle-prev-fi () | |
2065 | ;; Why do we have this rule? Because we must go back to the if | |
2066 | ;; to get its indent. We may continue back from there. | |
2067 | ;; We return nil because we don't have anything to add to result, | |
2068 | ;; the side affect of setting align-point is all that matters. | |
2069 | ;; we could return a comment (a string) but I can't think of a good one... | |
2070 | (sh-goto-matching-if) | |
2071 | nil) | |
2072 | ||
2073 | (defun sh-handle-this-then () | |
2074 | (let ((p (sh-goto-matching-if))) | |
2075 | (if p | |
2076 | (list '(+ sh-indent-for-then)) | |
2077 | ))) | |
2078 | ||
2079 | (defun sh-handle-prev-then () | |
2080 | (let ((p (sh-goto-matching-if))) | |
2081 | (if p | |
2082 | (list '(+ sh-indent-after-if)) | |
2083 | ))) | |
2084 | ||
2085 | (defun sh-handle-prev-open () | |
2086 | (save-excursion | |
2087 | (let ((x (sh-prev-stmt))) | |
2088 | (if (and x | |
2089 | (progn | |
2090 | (goto-char x) | |
2091 | (or | |
2092 | (looking-at "function\\b") | |
2093 | (looking-at "\\s-*\\S-+\\s-*()") | |
2094 | ))) | |
2095 | (list '(+ sh-indent-after-function)) | |
2096 | (list '(+ sh-indent-after-open))) | |
2097 | ))) | |
2098 | ||
2099 | (defun sh-handle-this-close () | |
2100 | (forward-char 1) ;; move over ")" | |
6c5bcbc1 SM |
2101 | (if (sh-safe-forward-sexp -1) |
2102 | (list "aligned to opening paren"))) | |
f964dfcb GM |
2103 | |
2104 | (defun sh-goto-matching-case () | |
2105 | (let ((found (sh-find-prev-matching "\\bcase\\b" "\\besac\\b" 1))) | |
6c5bcbc1 | 2106 | (if found (goto-char found)))) |
f964dfcb GM |
2107 | |
2108 | (defun sh-handle-prev-case () | |
2109 | ;; This is typically called when point is on same line as a case | |
2110 | ;; we shouldn't -- and can't find prev-case | |
6c5bcbc1 | 2111 | (if (looking-at ".*\\<case\\>") |
f964dfcb | 2112 | (list '(+ sh-indent-for-case-label)) |
6c5bcbc1 | 2113 | (error "We don't seem to be on a line with a case"))) ;; debug |
f964dfcb GM |
2114 | |
2115 | (defun sh-handle-this-esac () | |
6c5bcbc1 SM |
2116 | (if (sh-goto-matching-case) |
2117 | (list "aligned to matching case"))) | |
f964dfcb GM |
2118 | |
2119 | (defun sh-handle-prev-esac () | |
6c5bcbc1 SM |
2120 | (if (sh-goto-matching-case) |
2121 | (list "matching case"))) | |
f964dfcb GM |
2122 | |
2123 | (defun sh-handle-after-case-label () | |
6c5bcbc1 SM |
2124 | (if (sh-goto-matching-case) |
2125 | (list '(+ sh-indent-for-case-alt)))) | |
f964dfcb GM |
2126 | |
2127 | (defun sh-handle-prev-case-alt-end () | |
6c5bcbc1 SM |
2128 | (if (sh-goto-matching-case) |
2129 | (list '(+ sh-indent-for-case-label)))) | |
f964dfcb | 2130 | |
6c5bcbc1 | 2131 | (defun sh-safe-forward-sexp (&optional arg) |
f964dfcb | 2132 | "Try and do a `forward-sexp', but do not error. |
8db2b9fb | 2133 | Return new point if successful, nil if an error occurred." |
f964dfcb GM |
2134 | (condition-case nil |
2135 | (progn | |
6c5bcbc1 SM |
2136 | (forward-sexp (or arg 1)) |
2137 | (point)) ;; return point if successful | |
f964dfcb GM |
2138 | (error |
2139 | (sh-debug "oops!(1) %d" (point)) | |
6c5bcbc1 | 2140 | nil))) ;; return nil if fail |
f964dfcb GM |
2141 | |
2142 | (defun sh-goto-match-for-done () | |
2143 | (let ((found (sh-find-prev-matching sh-regexp-for-done sh-re-done 1))) | |
2144 | (if found | |
2145 | (goto-char found)))) | |
2146 | ||
2147 | (defun sh-handle-this-done () | |
2148 | (if (sh-goto-match-for-done) | |
6c5bcbc1 | 2149 | (list "aligned to do stmt" '(+ sh-indent-for-done)))) |
f964dfcb GM |
2150 | |
2151 | (defun sh-handle-prev-done () | |
2152 | (if (sh-goto-match-for-done) | |
6c5bcbc1 | 2153 | (list "previous done"))) |
f964dfcb GM |
2154 | |
2155 | (defun sh-handle-this-do () | |
6c5bcbc1 SM |
2156 | (if (sh-goto-match-for-done) |
2157 | (list '(+ sh-indent-for-do)))) | |
f964dfcb GM |
2158 | |
2159 | (defun sh-handle-prev-do () | |
6c5bcbc1 SM |
2160 | (cond |
2161 | ((save-restriction | |
2162 | (narrow-to-region | |
2163 | (point) | |
2164 | (save-excursion | |
2165 | (beginning-of-line) | |
2166 | (point))) | |
2167 | (sh-goto-match-for-done)) | |
2168 | (sh-debug "match for done found on THIS line") | |
2169 | (list '(+ sh-indent-after-loop-construct))) | |
2170 | ((sh-goto-match-for-done) | |
2171 | (sh-debug "match for done found on PREV line") | |
2172 | (list '(+ sh-indent-after-do))) | |
2173 | (t | |
2174 | (message "match for done NOT found") | |
2175 | nil))) | |
f964dfcb GM |
2176 | |
2177 | ;; for rc: | |
2178 | (defun sh-find-prev-switch () | |
2179 | "Find the line for the switch keyword matching this line's case keyword." | |
8db2b9fb | 2180 | (re-search-backward "\\<switch\\>" nil t)) |
f964dfcb GM |
2181 | |
2182 | (defun sh-handle-this-rc-case () | |
2183 | (if (sh-find-prev-switch) | |
2184 | (list '(+ sh-indent-after-switch)) | |
6c5bcbc1 | 2185 | ;; (list '(+ sh-indent-for-case-label)) |
f964dfcb GM |
2186 | nil)) |
2187 | ||
2188 | (defun sh-handle-prev-rc-case () | |
2189 | (list '(+ sh-indent-after-case))) | |
2190 | ||
2191 | (defun sh-check-rule (n thing) | |
2192 | (let ((rule (nth n (assoc thing sh-kw-alist))) | |
2193 | (val nil)) | |
2194 | (if rule | |
2195 | (progn | |
2196 | (setq val (funcall rule)) | |
2197 | (sh-debug "rule (%d) for %s at %d is %s\n-> returned %s" | |
2198 | n thing (point) rule val))) | |
2199 | val)) | |
2200 | ||
2201 | ||
2202 | (defun sh-get-indent-info () | |
2203 | "Return indent-info for this line. | |
2204 | This is a list. nil means the line is to be left as is. | |
2205 | Otherwise it contains one or more of the following sublists: | |
8db2b9fb | 2206 | \(t NUMBER\) NUMBER is the base location in the buffer that indentation is |
f964dfcb GM |
2207 | relative to. If present, this is always the first of the |
2208 | sublists. The indentation of the line in question is | |
8db2b9fb | 2209 | derived from the indentation of this point, possibly |
f964dfcb GM |
2210 | modified by subsequent sublists. |
2211 | \(+ VAR\) | |
2212 | \(- VAR\) Get the value of variable VAR and add to or subtract from | |
2213 | the indentation calculated so far. | |
2214 | \(= VAR\) Get the value of variable VAR and *replace* the | |
8db2b9fb | 2215 | indentation with its value. This only occurs for |
f964dfcb GM |
2216 | special variables such as `sh-indent-comment'. |
2217 | STRING This is ignored for the purposes of calculating | |
8db2b9fb | 2218 | indentation, it is printed in certain cases to help show |
f964dfcb GM |
2219 | what the indentation is based on." |
2220 | ;; See comments before `sh-kw'. | |
2221 | (save-excursion | |
485219e0 | 2222 | (let ((have-result nil) |
f964dfcb | 2223 | this-kw |
f964dfcb | 2224 | start |
485219e0 | 2225 | val |
f964dfcb | 2226 | (result nil) |
f964dfcb GM |
2227 | (align-point nil) |
2228 | prev-line-end x) | |
2229 | (beginning-of-line) | |
2230 | ;; Note: setting result to t means we are done and will return nil. | |
6c5bcbc1 | 2231 | ;;(This function never returns just t.) |
f964dfcb | 2232 | (cond |
485219e0 SM |
2233 | ((or (and (boundp 'font-lock-string-face) (not (bobp)) |
2234 | (eq (get-text-property (1- (point)) 'face) | |
2235 | font-lock-string-face)) | |
b36581fb | 2236 | (eq (get-text-property (point) 'face) sh-heredoc-face)) |
f964dfcb GM |
2237 | (setq result t) |
2238 | (setq have-result t)) | |
2239 | ((looking-at "\\s-*#") ; was (equal this-kw "#") | |
2240 | (if (bobp) | |
6c5bcbc1 | 2241 | (setq result t) ;; return nil if 1st line! |
f964dfcb GM |
2242 | (setq result (list '(= sh-indent-comment))) |
2243 | ;; we still need to get previous line in case | |
8db2b9fb | 2244 | ;; sh-indent-comment is t (indent as normal) |
f964dfcb GM |
2245 | (setq align-point (sh-prev-line nil)) |
2246 | (setq have-result nil) | |
2247 | )) | |
6c5bcbc1 | 2248 | ) ;; cond |
035107fa | 2249 | |
f964dfcb GM |
2250 | (unless have-result |
2251 | ;; Continuation lines are handled specially | |
2252 | (if (sh-this-is-a-continuation) | |
2253 | (progn | |
090475f3 SM |
2254 | (setq result |
2255 | (if (save-excursion | |
2256 | (beginning-of-line) | |
2257 | (not (memq (char-before (- (point) 2)) '(?\s ?\t)))) | |
2258 | ;; By convention, if the continuation \ is not | |
2259 | ;; preceded by a SPC or a TAB it means that the line | |
2260 | ;; is cut at a place where spaces cannot be freely | |
2261 | ;; added/removed. I.e. do not indent the line. | |
2262 | (list '(= nil)) | |
2263 | ;; We assume the line being continued is already | |
2264 | ;; properly indented... | |
2265 | ;; (setq prev-line-end (sh-prev-line)) | |
2266 | (setq align-point (sh-prev-line nil)) | |
2267 | (list '(+ sh-indent-for-continuation)))) | |
f964dfcb GM |
2268 | (setq have-result t)) |
2269 | (beginning-of-line) | |
2270 | (skip-chars-forward " \t") | |
2271 | (setq this-kw (sh-get-kw))) | |
2272 | ||
2273 | ;; Handle "this" keyword: first word on the line we're | |
2274 | ;; calculating indentation info for. | |
2275 | (if this-kw | |
2276 | (if (setq val (sh-check-rule 1 this-kw)) | |
2277 | (progn | |
2278 | (setq align-point (point)) | |
2279 | (sh-debug | |
2280 | "this - setting align-point to %d" align-point) | |
2281 | (setq result (append result val)) | |
2282 | (setq have-result t) | |
2283 | ;; set prev-line to continue processing remainder | |
8db2b9fb | 2284 | ;; of this line as a previous line |
f964dfcb GM |
2285 | (setq prev-line-end (point)) |
2286 | )))) | |
2287 | ||
2288 | (unless have-result | |
2289 | (setq prev-line-end (sh-prev-line 'end))) | |
2290 | ||
2291 | (if prev-line-end | |
2292 | (save-excursion | |
2293 | ;; We start off at beginning of this line. | |
2294 | ;; Scan previous statements while this is <= | |
2295 | ;; start of previous line. | |
6c5bcbc1 | 2296 | (setq start (point)) ;; for debug only |
f964dfcb GM |
2297 | (goto-char prev-line-end) |
2298 | (setq x t) | |
2299 | (while (and x (setq x (sh-prev-thing))) | |
2300 | (sh-debug "at %d x is: %s result is: %s" (point) x result) | |
2301 | (cond | |
2302 | ((and (equal x ")") | |
2303 | (equal (get-text-property (1- (point)) 'syntax-table) | |
34939e2c | 2304 | sh-st-punc)) |
f964dfcb GM |
2305 | (sh-debug "Case label) here") |
2306 | (setq x 'case-label) | |
2307 | (if (setq val (sh-check-rule 2 x)) | |
2308 | (progn | |
2309 | (setq result (append result val)) | |
2310 | (setq align-point (point)))) | |
6b61353c KH |
2311 | (or (bobp) |
2312 | (forward-char -1)) | |
4f3a3368 | 2313 | ;; FIXME: This charset looks too much like a regexp. --Stef |
f964dfcb GM |
2314 | (skip-chars-forward "[a-z0-9]*?") |
2315 | ) | |
2316 | ((string-match "[])}]" x) | |
6c5bcbc1 | 2317 | (setq x (sh-safe-forward-sexp -1)) |
f964dfcb GM |
2318 | (if x |
2319 | (progn | |
2320 | (setq align-point (point)) | |
2321 | (setq result (append result | |
2322 | (list "aligned to opening paren"))) | |
2323 | ))) | |
2324 | ((string-match "[[({]" x) | |
2325 | (sh-debug "Checking special thing: %s" x) | |
2326 | (if (setq val (sh-check-rule 2 x)) | |
2327 | (setq result (append result val))) | |
2328 | (forward-char -1) | |
2329 | (setq align-point (point))) | |
2330 | ((string-match "[\"'`]" x) | |
2331 | (sh-debug "Skipping back for %s" x) | |
2332 | ;; this was oops-2 | |
6c5bcbc1 | 2333 | (setq x (sh-safe-forward-sexp -1))) |
f964dfcb GM |
2334 | ((stringp x) |
2335 | (sh-debug "Checking string %s at %s" x (point)) | |
2336 | (if (setq val (sh-check-rule 2 x)) | |
2337 | ;; (or (eq t (car val)) | |
2338 | ;; (eq t (car (car val)))) | |
2339 | (setq result (append result val))) | |
2340 | ;; not sure about this test Wed Jan 27 23:48:35 1999 | |
2341 | (setq align-point (point)) | |
2342 | (unless (bolp) | |
2343 | (forward-char -1))) | |
2344 | (t | |
2345 | (error "Don't know what to do with %s" x)) | |
2346 | ) | |
6c5bcbc1 | 2347 | ) ;; while |
f964dfcb GM |
2348 | (sh-debug "result is %s" result) |
2349 | ) | |
2350 | (sh-debug "No prev line!") | |
2351 | (sh-debug "result: %s align-point: %s" result align-point) | |
2352 | ) | |
035107fa | 2353 | |
f964dfcb GM |
2354 | (if align-point |
2355 | ;; was: (setq result (append result (list (list t align-point)))) | |
2356 | (setq result (append (list (list t align-point)) result)) | |
2357 | ) | |
2358 | (sh-debug "result is now: %s" result) | |
035107fa | 2359 | |
f964dfcb | 2360 | (or result |
090475f3 SM |
2361 | (setq result (list (if prev-line-end |
2362 | (list t prev-line-end) | |
2363 | (list '= 'sh-first-lines-indent))))) | |
035107fa | 2364 | |
f964dfcb GM |
2365 | (if (eq result t) |
2366 | (setq result nil)) | |
2367 | (sh-debug "result is: %s" result) | |
2368 | result | |
6c5bcbc1 | 2369 | ) ;; let |
f964dfcb GM |
2370 | )) |
2371 | ||
2372 | ||
2373 | (defun sh-get-indent-var-for-line (&optional info) | |
2374 | "Return the variable controlling indentation for this line. | |
2375 | If there is not [just] one such variable, return a string | |
2376 | indicating the problem. | |
2377 | If INFO is supplied it is used, else it is calculated." | |
2378 | (let ((var nil) | |
2379 | (result nil) | |
2380 | (reason nil) | |
2381 | sym elt) | |
2382 | (or info | |
2383 | (setq info (sh-get-indent-info))) | |
2384 | (if (null info) | |
2385 | (setq result "this line to be left as is") | |
2386 | (while (and info (null result)) | |
2387 | (setq elt (car info)) | |
2388 | (cond | |
2389 | ((stringp elt) | |
2390 | (setq reason elt) | |
2391 | ) | |
2392 | ((not (listp elt)) | |
2393 | (error "sh-get-indent-var-for-line invalid elt: %s" elt)) | |
2394 | ;; so it is a list | |
2395 | ((eq t (car elt)) | |
6c5bcbc1 | 2396 | ) ;; nothing |
f964dfcb GM |
2397 | ((symbolp (setq sym (nth 1 elt))) |
2398 | ;; A bit of a kludge - when we see the sh-indent-comment | |
2399 | ;; ignore other variables. Otherwise it is tricky to | |
2400 | ;; "learn" the comment indentation. | |
2401 | (if (eq var 'sh-indent-comment) | |
2402 | (setq result var) | |
2403 | (if var | |
2404 | (setq result | |
2405 | "this line is controlled by more than 1 variable.") | |
2406 | (setq var sym)))) | |
2407 | (t | |
2408 | (error "sh-get-indent-var-for-line invalid list elt: %s" elt))) | |
2409 | (setq info (cdr info)) | |
2410 | )) | |
2411 | (or result | |
2412 | (setq result var)) | |
2413 | (or result | |
2414 | (setq result reason)) | |
2415 | (if (null result) | |
2416 | ;; e.g. just had (t POS) | |
2417 | (setq result "line has default indentation")) | |
2418 | result)) | |
2419 | ||
2420 | ||
2421 | ||
2422 | ;; Finding the previous line isn't trivial. | |
2423 | ;; We must *always* go back one more and see if that is a continuation | |
8db2b9fb | 2424 | ;; line -- it is the PREVIOUS line which is continued, not the one |
f964dfcb GM |
2425 | ;; we are going to! |
2426 | ;; Also, we want to treat a whole "here document" as one big line, | |
2427 | ;; because we may want to a align to the beginning of it. | |
2428 | ;; | |
2429 | ;; What we do: | |
6c5bcbc1 | 2430 | ;; - go back to previous non-empty line |
8db2b9fb | 2431 | ;; - if this is in a here-document, go to the beginning of it |
6c5bcbc1 | 2432 | ;; - while previous line is continued, go back one line |
f964dfcb GM |
2433 | (defun sh-prev-line (&optional end) |
2434 | "Back to end of previous non-comment non-empty line. | |
8db2b9fb | 2435 | Go to beginning of logical line unless END is non-nil, in which case |
f964dfcb | 2436 | we go to the end of the previous line and do not check for continuations." |
6c5bcbc1 SM |
2437 | (save-excursion |
2438 | (beginning-of-line) | |
2439 | (forward-comment (- (point-max))) | |
2440 | (unless end (beginning-of-line)) | |
2441 | (when (and (not (bobp)) | |
34939e2c | 2442 | (equal (get-text-property (1- (point)) 'face) |
b36581fb | 2443 | sh-heredoc-face)) |
34939e2c | 2444 | (let ((p1 (previous-single-property-change (1- (point)) 'face))) |
6c5bcbc1 SM |
2445 | (when p1 |
2446 | (goto-char p1) | |
34939e2c SM |
2447 | (if end |
2448 | (end-of-line) | |
2449 | (beginning-of-line))))) | |
6c5bcbc1 SM |
2450 | (unless end |
2451 | ;; we must check previous lines to see if they are continuation lines | |
2452 | ;; if so, we must return position of first of them | |
2453 | (while (and (sh-this-is-a-continuation) | |
2454 | (>= 0 (forward-line -1)))) | |
f964dfcb | 2455 | (beginning-of-line) |
6c5bcbc1 SM |
2456 | (skip-chars-forward " \t")) |
2457 | (point))) | |
f964dfcb GM |
2458 | |
2459 | ||
2460 | (defun sh-prev-stmt () | |
2461 | "Return the address of the previous stmt or nil." | |
2462 | ;; This is used when we are trying to find a matching keyword. | |
8db2b9fb | 2463 | ;; Searching backward for the keyword would certainly be quicker, but |
f964dfcb GM |
2464 | ;; it is hard to remove "false matches" -- such as if the keyword |
2465 | ;; appears in a string or quote. This way is slower, but (I think) safer. | |
2466 | (interactive) | |
2467 | (save-excursion | |
2468 | (let ((going t) | |
2469 | (start (point)) | |
2470 | (found nil) | |
2471 | (prev nil)) | |
2472 | (skip-chars-backward " \t;|&({[") | |
2473 | (while (and (not found) | |
2474 | (not (bobp)) | |
2475 | going) | |
8db2b9fb | 2476 | ;; Do a backward-sexp if possible, else backup bit by bit... |
6c5bcbc1 | 2477 | (if (sh-safe-forward-sexp -1) |
f964dfcb GM |
2478 | (progn |
2479 | (if (looking-at sh-special-keywords) | |
2480 | (progn | |
2481 | (setq found prev)) | |
2482 | (setq prev (point)) | |
2483 | )) | |
2484 | ;; backward-sexp failed | |
2485 | (if (zerop (skip-chars-backward " \t()[\]{};`'")) | |
2486 | (forward-char -1)) | |
2487 | (if (bolp) | |
2488 | (let ((back (sh-prev-line nil))) | |
2489 | (if back | |
2490 | (goto-char back) | |
2491 | (setq going nil))))) | |
2492 | (unless found | |
2493 | (skip-chars-backward " \t") | |
2494 | (if (or (and (bolp) (not (sh-this-is-a-continuation))) | |
2495 | (eq (char-before) ?\;) | |
2496 | (looking-at "\\s-*[|&]")) | |
2497 | (setq found (point))))) | |
2498 | (if found | |
2499 | (goto-char found)) | |
2500 | (if found | |
2501 | (progn | |
2502 | (skip-chars-forward " \t|&({[") | |
2503 | (setq found (point)))) | |
2504 | (if (>= (point) start) | |
2505 | (progn | |
2506 | (debug "We didn't move!") | |
2507 | (setq found nil)) | |
2508 | (or found | |
2509 | (sh-debug "Did not find prev stmt."))) | |
34939e2c | 2510 | found))) |
f964dfcb GM |
2511 | |
2512 | ||
2513 | (defun sh-get-word () | |
2514 | "Get a shell word skipping whitespace from point." | |
2515 | (interactive) | |
2516 | (skip-chars-forward "\t ") | |
2517 | (let ((start (point))) | |
2518 | (while | |
2519 | (if (looking-at "[\"'`]") | |
2520 | (sh-safe-forward-sexp) | |
2521 | ;; (> (skip-chars-forward "^ \t\n\"'`") 0) | |
4f3a3368 | 2522 | (> (skip-chars-forward "-_$[:alnum:]") 0) |
f964dfcb GM |
2523 | )) |
2524 | (buffer-substring start (point)) | |
2525 | )) | |
2526 | ||
2527 | (defun sh-prev-thing () | |
2528 | "Return the previous thing this logical line." | |
2529 | ;; This is called when `sh-get-indent-info' is working backwards on | |
2530 | ;; the previous line(s) finding what keywords may be relevant for | |
8db2b9fb | 2531 | ;; indenting. It moves over sexps if possible, and will stop |
f964dfcb GM |
2532 | ;; on a ; and at the beginning of a line if it is not a continuation |
2533 | ;; line. | |
2534 | ;; | |
2535 | ;; Added a kludge for ";;" | |
2536 | ;; Possible return values: | |
2537 | ;; nil - nothing | |
2538 | ;; a string - possibly a keyword | |
035107fa | 2539 | ;; |
f964dfcb GM |
2540 | (if (bolp) |
2541 | nil | |
49c7a608 SM |
2542 | (let ((start (point)) |
2543 | (min-point (if (sh-this-is-a-continuation) | |
2544 | (sh-prev-line nil) | |
2545 | (line-beginning-position)))) | |
2546 | (skip-chars-backward " \t;" min-point) | |
2547 | (if (looking-at "\\s-*;;") | |
2548 | ;; (message "Found ;; !") | |
2549 | ";;" | |
2550 | (skip-chars-backward "^)}];\"'`({[" min-point) | |
2551 | (let ((c (if (> (point) min-point) (char-before)))) | |
2552 | (sh-debug "stopping at %d c is %s start=%d min-point=%d" | |
2553 | (point) c start min-point) | |
2554 | (if (not (memq c '(?\n nil ?\;))) | |
2555 | ;; c -- return a string | |
2556 | (char-to-string c) | |
2557 | ;; Return the leading keyword of the "command" we supposedly | |
2558 | ;; skipped over. Maybe we skipped too far (e.g. past a `do' or | |
2559 | ;; `then' that precedes the actual command), so check whether | |
2560 | ;; we're looking at such a keyword and if so, move back forward. | |
2561 | (let ((boundary (point)) | |
2562 | kwd next) | |
2563 | (while | |
2564 | (progn | |
2565 | ;; Skip forward over white space newline and \ at eol. | |
2566 | (skip-chars-forward " \t\n\\\\" start) | |
2567 | (if (>= (point) start) | |
2568 | (progn | |
2569 | (sh-debug "point: %d >= start: %d" (point) start) | |
2570 | nil) | |
2571 | (if next (setq boundary next)) | |
2572 | (sh-debug "Now at %d start=%d" (point) start) | |
2573 | (setq kwd (sh-get-word)) | |
49c7a608 SM |
2574 | (if (member kwd (sh-feature sh-leading-keywords)) |
2575 | (progn | |
2576 | (setq next (point)) | |
2577 | t) | |
2578 | nil)))) | |
2579 | (goto-char boundary) | |
2580 | kwd))))))) | |
f964dfcb GM |
2581 | |
2582 | ||
2583 | (defun sh-this-is-a-continuation () | |
2584 | "Return non-nil if current line is a continuation of previous line." | |
6c5bcbc1 SM |
2585 | (save-excursion |
2586 | (and (zerop (forward-line -1)) | |
2587 | (looking-at ".*\\\\$") | |
2588 | (not (nth 4 (parse-partial-sexp (match-beginning 0) (match-end 0) | |
2589 | nil nil nil t)))))) | |
f964dfcb GM |
2590 | |
2591 | (defun sh-get-kw (&optional where and-move) | |
2592 | "Return first word of line from WHERE. | |
2593 | If AND-MOVE is non-nil then move to end of word." | |
2594 | (let ((start (point))) | |
2595 | (if where | |
2596 | (goto-char where)) | |
2597 | (prog1 | |
2598 | (buffer-substring (point) | |
2ca2ebe6 | 2599 | (progn (skip-chars-forward "^ \t\n;&|")(point))) |
f964dfcb | 2600 | (unless and-move |
34939e2c | 2601 | (goto-char start))))) |
f964dfcb GM |
2602 | |
2603 | (defun sh-find-prev-matching (open close &optional depth) | |
2604 | "Find a matching token for a set of opening and closing keywords. | |
2605 | This takes into account that there may be nested open..close pairings. | |
2606 | OPEN and CLOSE are regexps denoting the tokens to be matched. | |
2607 | Optional parameter DEPTH (usually 1) says how many to look for." | |
2608 | (let ((parse-sexp-ignore-comments t) | |
2609 | prev) | |
2610 | (setq depth (or depth 1)) | |
2611 | (save-excursion | |
2612 | (condition-case nil | |
2613 | (while (and | |
2614 | (/= 0 depth) | |
2615 | (not (bobp)) | |
2616 | (setq prev (sh-prev-stmt))) | |
2617 | (goto-char prev) | |
2618 | (save-excursion | |
2619 | (if (looking-at "\\\\\n") | |
2620 | (progn | |
2621 | (forward-char 2) | |
2622 | (skip-chars-forward " \t"))) | |
2623 | (cond | |
2624 | ((looking-at open) | |
2625 | (setq depth (1- depth)) | |
2626 | (sh-debug "found open at %d - depth = %d" (point) depth)) | |
2627 | ((looking-at close) | |
2628 | (setq depth (1+ depth)) | |
2629 | (sh-debug "found close - depth = %d" depth)) | |
2630 | (t | |
2631 | )))) | |
6c5bcbc1 | 2632 | (error nil)) |
f964dfcb GM |
2633 | (if (eq depth 0) |
2634 | prev ;; (point) | |
2635 | nil) | |
2636 | ))) | |
2637 | ||
2638 | ||
2639 | (defun sh-var-value (var &optional ignore-error) | |
2640 | "Return the value of variable VAR, interpreting symbols. | |
2641 | It can also return t or nil. | |
eac9c0ef | 2642 | If an invalid value is found, throw an error unless Optional argument |
f964dfcb GM |
2643 | IGNORE-ERROR is non-nil." |
2644 | (let ((val (symbol-value var))) | |
2645 | (cond | |
2646 | ((numberp val) | |
2647 | val) | |
2648 | ((eq val t) | |
2649 | val) | |
2650 | ((null val) | |
2651 | val) | |
2652 | ((eq val '+) | |
2653 | sh-basic-offset) | |
2654 | ((eq val '-) | |
2655 | (- sh-basic-offset)) | |
2656 | ((eq val '++) | |
2657 | (* 2 sh-basic-offset)) | |
2658 | ((eq val '--) | |
2659 | (* 2 (- sh-basic-offset))) | |
2660 | ((eq val '*) | |
2661 | (/ sh-basic-offset 2)) | |
2662 | ((eq val '/) | |
2663 | (/ (- sh-basic-offset) 2)) | |
2664 | (t | |
2665 | (if ignore-error | |
6c5bcbc1 SM |
2666 | (progn |
2667 | (message "Don't know how to handle %s's value of %s" var val) | |
2668 | 0) | |
2669 | (error "Don't know how to handle %s's value of %s" var val)) | |
f964dfcb GM |
2670 | )))) |
2671 | ||
2672 | (defun sh-set-var-value (var value &optional no-symbol) | |
2673 | "Set variable VAR to VALUE. | |
8db2b9fb | 2674 | Unless optional argument NO-SYMBOL is non-nil, then if VALUE is |
f964dfcb GM |
2675 | can be represented by a symbol then do so." |
2676 | (cond | |
2677 | (no-symbol | |
2678 | (set var value)) | |
2679 | ((= value sh-basic-offset) | |
2680 | (set var '+)) | |
2681 | ((= value (- sh-basic-offset)) | |
2682 | (set var '-)) | |
2683 | ((eq value (* 2 sh-basic-offset)) | |
2684 | (set var '++)) | |
2685 | ((eq value (* 2 (- sh-basic-offset))) | |
2686 | (set var '--)) | |
2687 | ((eq value (/ sh-basic-offset 2)) | |
2688 | (set var '*)) | |
2689 | ((eq value (/ (- sh-basic-offset) 2)) | |
2690 | (set var '/)) | |
2691 | (t | |
2692 | (set var value))) | |
2693 | ) | |
2694 | ||
2695 | ||
2696 | (defun sh-calculate-indent (&optional info) | |
2697 | "Return the indentation for the current line. | |
2698 | If INFO is supplied it is used, else it is calculated from current line." | |
6c5bcbc1 | 2699 | (let ((ofs 0) |
f964dfcb GM |
2700 | (base-value 0) |
2701 | elt a b var val) | |
2702 | (or info | |
2703 | (setq info (sh-get-indent-info))) | |
6c5bcbc1 | 2704 | (when info |
f964dfcb GM |
2705 | (while info |
2706 | (sh-debug "info: %s ofs=%s" info ofs) | |
2707 | (setq elt (car info)) | |
2708 | (cond | |
6c5bcbc1 | 2709 | ((stringp elt)) ;; do nothing? |
f964dfcb GM |
2710 | ((listp elt) |
2711 | (setq a (car (car info))) | |
2712 | (setq b (nth 1 (car info))) | |
2713 | (cond | |
2714 | ((eq a t) | |
2715 | (save-excursion | |
2716 | (goto-char b) | |
2717 | (setq val (current-indentation))) | |
2718 | (setq base-value val)) | |
2719 | ((symbolp b) | |
2720 | (setq val (sh-var-value b)) | |
2721 | (cond | |
2722 | ((eq a '=) | |
2723 | (cond | |
2724 | ((null val) | |
2725 | ;; no indentation | |
2726 | ;; set info to nil so we stop immediately | |
2727 | (setq base-value nil ofs nil info nil)) | |
6c5bcbc1 | 2728 | ((eq val t) (setq ofs 0)) ;; indent as normal line |
f964dfcb GM |
2729 | (t |
2730 | ;; The following assume the (t POS) come first! | |
2731 | (setq ofs val base-value 0) | |
6c5bcbc1 SM |
2732 | (setq info nil)))) ;; ? stop now |
2733 | ((eq a '+) (setq ofs (+ ofs val))) | |
2734 | ((eq a '-) (setq ofs (- ofs val))) | |
f964dfcb GM |
2735 | (t |
2736 | (error "sh-calculate-indent invalid a a=%s b=%s" a b)))) | |
2737 | (t | |
6c5bcbc1 | 2738 | (error "sh-calculate-indent invalid elt: a=%s b=%s" a b)))) |
f964dfcb | 2739 | (t |
6c5bcbc1 SM |
2740 | (error "sh-calculate-indent invalid elt %s" elt))) |
2741 | (sh-debug "a=%s b=%s val=%s base-value=%s ofs=%s" | |
2742 | a b val base-value ofs) | |
2743 | (setq info (cdr info))) | |
f964dfcb GM |
2744 | ;; return value: |
2745 | (sh-debug "at end: base-value: %s ofs: %s" base-value ofs) | |
2746 | ||
2747 | (cond | |
2748 | ((or (null base-value)(null ofs)) | |
2749 | nil) | |
2750 | ((and (numberp base-value)(numberp ofs)) | |
2751 | (sh-debug "base (%d) + ofs (%d) = %d" | |
6c5bcbc1 | 2752 | base-value ofs (+ base-value ofs)) |
f964dfcb GM |
2753 | (+ base-value ofs)) ;; return value |
2754 | (t | |
2755 | (error "sh-calculate-indent: Help. base-value=%s ofs=%s" | |
2756 | base-value ofs) | |
6c5bcbc1 | 2757 | nil))))) |
f964dfcb GM |
2758 | |
2759 | ||
3e2dd647 | 2760 | (defun sh-indent-line () |
f964dfcb GM |
2761 | "Indent the current line." |
2762 | (interactive) | |
017708e9 | 2763 | (let ((indent (sh-calculate-indent)) |
f964dfcb | 2764 | (pos (- (point-max) (point)))) |
6c5bcbc1 SM |
2765 | (when indent |
2766 | (beginning-of-line) | |
6c5bcbc1 | 2767 | (skip-chars-forward " \t") |
017708e9 | 2768 | (indent-line-to indent) |
6c5bcbc1 SM |
2769 | ;; If initial point was within line's indentation, |
2770 | ;; position after the indentation. Else stay at same point in text. | |
2771 | (if (> (- (point-max) pos) (point)) | |
2772 | (goto-char (- (point-max) pos)))))) | |
f964dfcb GM |
2773 | |
2774 | ||
2775 | (defun sh-blink (blinkpos &optional msg) | |
2776 | "Move cursor momentarily to BLINKPOS and display MSG." | |
2777 | ;; We can get here without it being a number on first line | |
2778 | (if (numberp blinkpos) | |
2779 | (save-excursion | |
2780 | (goto-char blinkpos) | |
29a4e67d | 2781 | (if msg (message "%s" msg) (message nil)) |
f964dfcb | 2782 | (sit-for blink-matching-delay)) |
7fa1a8f9 | 2783 | (if msg (message "%s" msg) (message nil)))) |
f964dfcb GM |
2784 | |
2785 | (defun sh-show-indent (arg) | |
63616f52 | 2786 | "Show the how the current line would be indented. |
f964dfcb GM |
2787 | This tells you which variable, if any, controls the indentation of |
2788 | this line. | |
2789 | If optional arg ARG is non-null (called interactively with a prefix), | |
2790 | a pop up window describes this variable. | |
2791 | If variable `sh-blink' is non-nil then momentarily go to the line | |
2792 | we are indenting relative to, if applicable." | |
2793 | (interactive "P") | |
2794 | (sh-must-support-indent) | |
2795 | (let* ((info (sh-get-indent-info)) | |
2796 | (var (sh-get-indent-var-for-line info)) | |
6c5bcbc1 SM |
2797 | (curr-indent (current-indentation)) |
2798 | val msg) | |
f964dfcb | 2799 | (if (stringp var) |
29a4e67d | 2800 | (message "%s" (setq msg var)) |
f964dfcb GM |
2801 | (setq val (sh-calculate-indent info)) |
2802 | ||
2803 | (if (eq curr-indent val) | |
2804 | (setq msg (format "%s is %s" var (symbol-value var))) | |
2805 | (setq msg | |
2806 | (if val | |
2807 | (format "%s (%s) would change indent from %d to: %d" | |
2808 | var (symbol-value var) curr-indent val) | |
2809 | (format "%s (%s) would leave line as is" | |
2810 | var (symbol-value var))) | |
2811 | )) | |
2812 | (if (and arg var) | |
2813 | (describe-variable var))) | |
2814 | (if sh-blink | |
2815 | (let ((info (sh-get-indent-info))) | |
2816 | (if (and info (listp (car info)) | |
2817 | (eq (car (car info)) t)) | |
2818 | (sh-blink (nth 1 (car info)) msg) | |
29a4e67d DG |
2819 | (message "%s" msg))) |
2820 | (message "%s" msg)) | |
f964dfcb GM |
2821 | )) |
2822 | ||
2823 | (defun sh-set-indent () | |
2824 | "Set the indentation for the current line. | |
2825 | If the current line is controlled by an indentation variable, prompt | |
2826 | for a new value for it." | |
2827 | (interactive) | |
2828 | (sh-must-support-indent) | |
2829 | (let* ((info (sh-get-indent-info)) | |
2830 | (var (sh-get-indent-var-for-line info)) | |
485219e0 | 2831 | val old-val indent-val) |
f964dfcb | 2832 | (if (stringp var) |
29a4e67d | 2833 | (message "Cannot set indent - %s" var) |
f964dfcb GM |
2834 | (setq old-val (symbol-value var)) |
2835 | (setq val (sh-read-variable var)) | |
2836 | (condition-case nil | |
2837 | (progn | |
2838 | (set var val) | |
2839 | (setq indent-val (sh-calculate-indent info)) | |
2840 | (if indent-val | |
2841 | (message "Variable: %s Value: %s would indent to: %d" | |
2842 | var (symbol-value var) indent-val) | |
2843 | (message "Variable: %s Value: %s would leave line as is." | |
2844 | var (symbol-value var))) | |
8db2b9fb | 2845 | ;; I'm not sure about this, indenting it now? |
f964dfcb | 2846 | ;; No. Because it would give the impression that an undo would |
8db2b9fb | 2847 | ;; restore thing, but the value has been altered. |
f964dfcb GM |
2848 | ;; (sh-indent-line) |
2849 | ) | |
2850 | (error | |
2851 | (set var old-val) | |
8db2b9fb | 2852 | (message "Bad value for %s, restoring to previous value %s" |
f964dfcb GM |
2853 | var old-val) |
2854 | (sit-for 1) | |
2855 | nil)) | |
2856 | ))) | |
2857 | ||
2858 | ||
2859 | (defun sh-learn-line-indent (arg) | |
2860 | "Learn how to indent a line as it currently is indented. | |
2861 | ||
2862 | If there is an indentation variable which controls this line's indentation, | |
2863 | then set it to a value which would indent the line the way it | |
2864 | presently is. | |
2865 | ||
2866 | If the value can be represented by one of the symbols then do so | |
2867 | unless optional argument ARG (the prefix when interactive) is non-nil." | |
2868 | (interactive "*P") | |
2869 | (sh-must-support-indent) | |
2870 | ;; I'm not sure if we show allow learning on an empty line. | |
2871 | ;; Though it might occasionally be useful I think it usually | |
2872 | ;; would just be confusing. | |
2873 | (if (save-excursion | |
2874 | (beginning-of-line) | |
2875 | (looking-at "\\s-*$")) | |
2876 | (message "sh-learn-line-indent ignores empty lines.") | |
2877 | (let* ((info (sh-get-indent-info)) | |
2878 | (var (sh-get-indent-var-for-line info)) | |
2879 | ival sval diff new-val | |
2880 | (no-symbol arg) | |
2881 | (curr-indent (current-indentation))) | |
6c5bcbc1 SM |
2882 | (cond |
2883 | ((stringp var) | |
29a4e67d | 2884 | (message "Cannot learn line - %s" var)) |
6c5bcbc1 SM |
2885 | ((eq var 'sh-indent-comment) |
2886 | ;; This is arbitrary... | |
2887 | ;; - if curr-indent is 0, set to curr-indent | |
2888 | ;; - else if it has the indentation of a "normal" line, | |
2889 | ;; then set to t | |
2890 | ;; - else set to curr-indent. | |
2891 | (setq sh-indent-comment | |
2892 | (if (= curr-indent 0) | |
2893 | 0 | |
2894 | (let* ((sh-indent-comment t) | |
2895 | (val2 (sh-calculate-indent info))) | |
2896 | (if (= val2 curr-indent) | |
2897 | t | |
2898 | curr-indent)))) | |
2899 | (message "%s set to %s" var (symbol-value var)) | |
2900 | ) | |
2901 | ((numberp (setq sval (sh-var-value var))) | |
2902 | (setq ival (sh-calculate-indent info)) | |
2903 | (setq diff (- curr-indent ival)) | |
035107fa | 2904 | |
6c5bcbc1 SM |
2905 | (sh-debug "curr-indent: %d ival: %d diff: %d var:%s sval %s" |
2906 | curr-indent ival diff var sval) | |
2907 | (setq new-val (+ sval diff)) | |
f964dfcb GM |
2908 | ;;; I commented out this because someone might want to replace |
2909 | ;;; a value of `+' with the current value of sh-basic-offset | |
2910 | ;;; or vice-versa. | |
2911 | ;;; (if (= 0 diff) | |
2912 | ;;; (message "No change needed!") | |
6c5bcbc1 SM |
2913 | (sh-set-var-value var new-val no-symbol) |
2914 | (message "%s set to %s" var (symbol-value var)) | |
2915 | ) | |
2916 | (t | |
2917 | (debug) | |
2918 | (message "Cannot change %s" var)))))) | |
f964dfcb GM |
2919 | |
2920 | ||
2921 | ||
2922 | (defun sh-mark-init (buffer) | |
2923 | "Initialize a BUFFER to be used by `sh-mark-line'." | |
090475f3 | 2924 | (with-current-buffer (get-buffer-create buffer) |
348e1411 | 2925 | (erase-buffer) |
090475f3 | 2926 | (occur-mode))) |
f964dfcb GM |
2927 | |
2928 | ||
2929 | (defun sh-mark-line (message point buffer &optional add-linenum occur-point) | |
2930 | "Insert MESSAGE referring to location POINT in current buffer into BUFFER. | |
2931 | Buffer BUFFER is in `occur-mode'. | |
2932 | If ADD-LINENUM is non-nil the message is preceded by the line number. | |
8db2b9fb | 2933 | If OCCUR-POINT is non-nil then the line is marked as a new occurrence |
f964dfcb GM |
2934 | so that `occur-next' and `occur-prev' will work." |
2935 | (let ((m1 (make-marker)) | |
f964dfcb | 2936 | start |
6c5bcbc1 SM |
2937 | (line "")) |
2938 | (when point | |
2939 | (set-marker m1 point (current-buffer)) | |
2940 | (if add-linenum | |
2941 | (setq line (format "%d: " (1+ (count-lines 1 point)))))) | |
f964dfcb GM |
2942 | (save-excursion |
2943 | (if (get-buffer buffer) | |
2944 | (set-buffer (get-buffer buffer)) | |
2945 | (set-buffer (get-buffer-create buffer)) | |
2946 | (occur-mode) | |
f964dfcb GM |
2947 | ) |
2948 | (goto-char (point-max)) | |
2949 | (setq start (point)) | |
2950 | (insert line) | |
2951 | (if occur-point | |
2952 | (setq occur-point (point))) | |
2953 | (insert message) | |
2954 | (if point | |
06d74900 EZ |
2955 | (add-text-properties |
2956 | start (point) | |
2957 | '(mouse-face highlight | |
2958 | help-echo "mouse-2: go to the line where I learned this"))) | |
f964dfcb GM |
2959 | (insert "\n") |
2960 | (if point | |
2961 | (progn | |
348e1411 | 2962 | (put-text-property start (point) 'occur-target m1) |
f964dfcb | 2963 | (if occur-point |
348e1411 JB |
2964 | (put-text-property start occur-point |
2965 | 'occur-match t)) | |
f964dfcb GM |
2966 | )) |
2967 | ))) | |
2968 | ||
2969 | ||
2970 | ||
2971 | ;; Is this really worth having? | |
2972 | (defvar sh-learned-buffer-hook nil | |
8db2b9fb | 2973 | "*An abnormal hook, called with an alist of learned variables.") |
3e2dd647 | 2974 | ;; Example of how to use sh-learned-buffer-hook |
035107fa | 2975 | ;; |
f964dfcb GM |
2976 | ;; (defun what-i-learned (list) |
2977 | ;; (let ((p list)) | |
2978 | ;; (save-excursion | |
2979 | ;; (set-buffer "*scratch*") | |
2980 | ;; (goto-char (point-max)) | |
2981 | ;; (insert "(setq\n") | |
2982 | ;; (while p | |
2983 | ;; (insert (format " %s %s \n" | |
2984 | ;; (nth 0 (car p)) (nth 1 (car p)))) | |
2985 | ;; (setq p (cdr p))) | |
2986 | ;; (insert ")\n") | |
2987 | ;; ))) | |
035107fa | 2988 | ;; |
f964dfcb GM |
2989 | ;; (add-hook 'sh-learned-buffer-hook 'what-i-learned) |
2990 | ||
2991 | ||
2992 | ;; Originally this was sh-learn-region-indent (beg end) | |
8db2b9fb | 2993 | ;; However, in practice this was awkward so I changed it to |
f964dfcb GM |
2994 | ;; use the whole buffer. Use narrowing if needbe. |
2995 | (defun sh-learn-buffer-indent (&optional arg) | |
2996 | "Learn how to indent the buffer the way it currently is. | |
2997 | ||
2998 | Output in buffer \"*indent*\" shows any lines which have conflicting | |
8db2b9fb SM |
2999 | values of a variable, and the final value of all variables learned. |
3000 | This buffer is popped to automatically if there are any discrepancies. | |
f964dfcb | 3001 | |
8db2b9fb SM |
3002 | If no prefix ARG is given, then variables are set to numbers. |
3003 | If a prefix arg is given, then variables are set to symbols when | |
f964dfcb GM |
3004 | applicable -- e.g. to symbol `+' if the value is that of the |
3005 | basic indent. | |
3006 | If a positive numerical prefix is given, then `sh-basic-offset' | |
3007 | is set to the prefix's numerical value. | |
8db2b9fb | 3008 | Otherwise, sh-basic-offset may or may not be changed, according |
f964dfcb GM |
3009 | to the value of variable `sh-learn-basic-offset'. |
3010 | ||
3011 | Abnormal hook `sh-learned-buffer-hook' if non-nil is called when the | |
3012 | function completes. The function is abnormal because it is called | |
8db2b9fb | 3013 | with an alist of variables learned. This feature may be changed or |
f964dfcb GM |
3014 | removed in the future. |
3015 | ||
3016 | This command can often take a long time to run." | |
3017 | (interactive "P") | |
3018 | (sh-must-support-indent) | |
3019 | (save-excursion | |
3020 | (goto-char (point-min)) | |
3021 | (let ((learned-var-list nil) | |
3022 | (out-buffer "*indent*") | |
3023 | (num-diffs 0) | |
f964dfcb GM |
3024 | previous-set-info |
3025 | (max 17) | |
3026 | vec | |
3027 | msg | |
8db2b9fb | 3028 | (comment-col nil) ;; number if all same, t if seen diff values |
f964dfcb GM |
3029 | (comments-always-default t) ;; nil if we see one not default |
3030 | initial-msg | |
3031 | (specified-basic-offset (and arg (numberp arg) | |
3032 | (> arg 0))) | |
3033 | (linenum 0) | |
3034 | suggested) | |
3035 | (setq vec (make-vector max 0)) | |
3036 | (sh-mark-init out-buffer) | |
3037 | ||
3038 | (if specified-basic-offset | |
3039 | (progn | |
3040 | (setq sh-basic-offset arg) | |
3041 | (setq initial-msg | |
3042 | (format "Using specified sh-basic-offset of %d" | |
3043 | sh-basic-offset))) | |
3044 | (setq initial-msg | |
3045 | (format "Initial value of sh-basic-offset: %s" | |
3046 | sh-basic-offset))) | |
3047 | ||
3048 | (while (< (point) (point-max)) | |
3049 | (setq linenum (1+ linenum)) | |
6c5bcbc1 SM |
3050 | ;; (if (zerop (% linenum 10)) |
3051 | (message "line %d" linenum) | |
3052 | ;; ) | |
f964dfcb GM |
3053 | (unless (looking-at "\\s-*$") ;; ignore empty lines! |
3054 | (let* ((sh-indent-comment t) ;; info must return default indent | |
3055 | (info (sh-get-indent-info)) | |
3056 | (var (sh-get-indent-var-for-line info)) | |
3057 | sval ival diff new-val | |
3058 | (curr-indent (current-indentation))) | |
3059 | (cond | |
3060 | ((null var) | |
3061 | nil) | |
3062 | ((stringp var) | |
3063 | nil) | |
3064 | ((numberp (setq sval (sh-var-value var 'no-error))) | |
3065 | ;; the numberp excludes comments since sval will be t. | |
3066 | (setq ival (sh-calculate-indent)) | |
3067 | (setq diff (- curr-indent ival)) | |
3068 | (setq new-val (+ sval diff)) | |
3069 | (sh-set-var-value var new-val 'no-symbol) | |
6c5bcbc1 | 3070 | (unless (looking-at "\\s-*#") ;; don't learn from comments |
f964dfcb GM |
3071 | (if (setq previous-set-info (assoc var learned-var-list)) |
3072 | (progn | |
8db2b9fb | 3073 | ;; it was already there, is it same value ? |
f964dfcb GM |
3074 | (unless (eq (symbol-value var) |
3075 | (nth 1 previous-set-info)) | |
3076 | (sh-mark-line | |
3077 | (format "Variable %s was set to %s" | |
3078 | var (symbol-value var)) | |
3079 | (point) out-buffer t t) | |
3080 | (sh-mark-line | |
3081 | (format " but was previously set to %s" | |
3082 | (nth 1 previous-set-info)) | |
3083 | (nth 2 previous-set-info) out-buffer t) | |
3084 | (setq num-diffs (1+ num-diffs)) | |
3085 | ;; (delete previous-set-info learned-var-list) | |
3086 | (setcdr previous-set-info | |
3087 | (list (symbol-value var) (point))) | |
3088 | ) | |
3089 | ) | |
3090 | (setq learned-var-list | |
3091 | (append (list (list var (symbol-value var) | |
3092 | (point))) | |
3093 | learned-var-list))) | |
3094 | (if (numberp new-val) | |
3095 | (progn | |
3096 | (sh-debug | |
3097 | "This line's indent value: %d" new-val) | |
3098 | (if (< new-val 0) | |
3099 | (setq new-val (- new-val))) | |
3100 | (if (< new-val max) | |
3101 | (aset vec new-val (1+ (aref vec new-val)))))) | |
3102 | )) | |
3103 | ((eq var 'sh-indent-comment) | |
3104 | (unless (= curr-indent (sh-calculate-indent info)) | |
3105 | ;; this is not the default indentation | |
3106 | (setq comments-always-default nil) | |
6c5bcbc1 | 3107 | (if comment-col ;; then we have see one before |
f964dfcb | 3108 | (or (eq comment-col curr-indent) |
6c5bcbc1 | 3109 | (setq comment-col t)) ;; seen a different one |
f964dfcb | 3110 | (setq comment-col curr-indent)) |
6c5bcbc1 SM |
3111 | )) |
3112 | (t | |
f964dfcb GM |
3113 | (sh-debug "Cannot learn this line!!!") |
3114 | )) | |
3115 | (sh-debug | |
6c5bcbc1 | 3116 | "at %s learned-var-list is %s" (point) learned-var-list) |
f964dfcb GM |
3117 | )) |
3118 | (forward-line 1) | |
3119 | ) ;; while | |
3120 | (if sh-debug | |
3121 | (progn | |
3122 | (setq msg (format | |
3123 | "comment-col = %s comments-always-default = %s" | |
3124 | comment-col comments-always-default)) | |
3125 | ;; (message msg) | |
3126 | (sh-mark-line msg nil out-buffer))) | |
3127 | (cond | |
3128 | ((eq comment-col 0) | |
3129 | (setq msg "\nComments are all in 1st column.\n")) | |
3130 | (comments-always-default | |
3131 | (setq msg "\nComments follow default indentation.\n") | |
3132 | (setq comment-col t)) | |
3133 | ((numberp comment-col) | |
3134 | (setq msg (format "\nComments are in col %d." comment-col))) | |
3135 | (t | |
8db2b9fb | 3136 | (setq msg "\nComments seem to be mixed, leaving them as is.\n") |
f964dfcb GM |
3137 | (setq comment-col nil) |
3138 | )) | |
3139 | (sh-debug msg) | |
3140 | (sh-mark-line msg nil out-buffer) | |
3141 | ||
3142 | (sh-mark-line initial-msg nil out-buffer t t) | |
3143 | ||
3144 | (setq suggested (sh-guess-basic-offset vec)) | |
3145 | ||
3146 | (if (and suggested (not specified-basic-offset)) | |
3147 | (let ((new-value | |
3148 | (cond | |
3149 | ;; t => set it if we have a single value as a number | |
3150 | ((and (eq sh-learn-basic-offset t) (numberp suggested)) | |
3151 | suggested) | |
3152 | ;; other non-nil => set it if only one value was found | |
3153 | (sh-learn-basic-offset | |
3154 | (if (numberp suggested) | |
3155 | suggested | |
3156 | (if (= (length suggested) 1) | |
3157 | (car suggested)))) | |
3158 | (t | |
3159 | nil)))) | |
3160 | (if new-value | |
3161 | (progn | |
3162 | (setq learned-var-list | |
3163 | (append (list (list 'sh-basic-offset | |
3164 | (setq sh-basic-offset new-value) | |
3165 | (point-max))) | |
3166 | learned-var-list)) | |
3167 | ;; Not sure if we need to put this line in, since | |
3168 | ;; it will appear in the "Learned variable settings". | |
3169 | (sh-mark-line | |
3170 | (format "Changed sh-basic-offset to: %d" sh-basic-offset) | |
3171 | nil out-buffer)) | |
3172 | (sh-mark-line | |
3173 | (if (listp suggested) | |
3174 | (format "Possible value(s) for sh-basic-offset: %s" | |
3175 | (mapconcat 'int-to-string suggested " ")) | |
3176 | (format "Suggested sh-basic-offset: %d" suggested)) | |
3177 | nil out-buffer)))) | |
3178 | ||
035107fa | 3179 | |
f964dfcb GM |
3180 | (setq learned-var-list |
3181 | (append (list (list 'sh-indent-comment comment-col (point-max))) | |
6c5bcbc1 | 3182 | learned-var-list)) |
f964dfcb | 3183 | (setq sh-indent-comment comment-col) |
485219e0 | 3184 | (let ((name (buffer-name))) |
f964dfcb GM |
3185 | (sh-mark-line "\nLearned variable settings:" nil out-buffer) |
3186 | (if arg | |
3187 | ;; Set learned variables to symbolic rather than numeric | |
3188 | ;; values where possible. | |
6c5bcbc1 SM |
3189 | (dolist (learned-var (reverse learned-var-list)) |
3190 | (let ((var (car learned-var)) | |
3191 | (val (nth 1 learned-var))) | |
3192 | (when (and (not (eq var 'sh-basic-offset)) | |
3193 | (numberp val)) | |
3194 | (sh-set-var-value var val))))) | |
3195 | (dolist (learned-var (reverse learned-var-list)) | |
3196 | (let ((var (car learned-var))) | |
f964dfcb | 3197 | (sh-mark-line (format " %s %s" var (symbol-value var)) |
6c5bcbc1 | 3198 | (nth 2 learned-var) out-buffer))) |
090475f3 | 3199 | (with-current-buffer out-buffer |
6c5bcbc1 SM |
3200 | (goto-char (point-min)) |
3201 | (insert | |
3202 | (format "Indentation values for buffer %s.\n" name) | |
3203 | (format "%d indentation variable%s different values%s\n\n" | |
3204 | num-diffs | |
3205 | (if (= num-diffs 1) | |
3206 | " has" "s have") | |
3207 | (if (zerop num-diffs) | |
3208 | "." ":")) | |
3209 | ))) | |
f964dfcb GM |
3210 | ;; Are abnormal hooks considered bad form? |
3211 | (run-hook-with-args 'sh-learned-buffer-hook learned-var-list) | |
3212 | (if (or sh-popup-occur-buffer (> num-diffs 0)) | |
3213 | (pop-to-buffer out-buffer)) | |
3214 | ))) | |
3215 | ||
3216 | (defun sh-guess-basic-offset (vec) | |
8db2b9fb | 3217 | "See if we can determine a reasonable value for `sh-basic-offset'. |
f964dfcb GM |
3218 | This is experimental, heuristic and arbitrary! |
3219 | Argument VEC is a vector of information collected by | |
3220 | `sh-learn-buffer-indent'. | |
3221 | Return values: | |
3222 | number - there appears to be a good single value | |
8db2b9fb | 3223 | list of numbers - no obvious one, here is a list of one or more |
f964dfcb GM |
3224 | reasonable choices |
3225 | nil - we couldn't find a reasonable one." | |
3226 | (let* ((max (1- (length vec))) | |
6c5bcbc1 | 3227 | (i 1) |
485219e0 | 3228 | (totals (make-vector max 0))) |
f964dfcb GM |
3229 | (while (< i max) |
3230 | (aset totals i (+ (aref totals i) (* 4 (aref vec i)))) | |
f964dfcb GM |
3231 | (if (zerop (% i 2)) |
3232 | (aset totals i (+ (aref totals i) (aref vec (/ i 2))))) | |
3233 | (if (< (* i 2) max) | |
3234 | (aset totals i (+ (aref totals i) (aref vec (* i 2))))) | |
6c5bcbc1 SM |
3235 | (setq i (1+ i))) |
3236 | ||
f964dfcb GM |
3237 | (let ((x nil) |
3238 | (result nil) | |
3239 | tot sum p) | |
3240 | (setq i 1) | |
3241 | (while (< i max) | |
3242 | (if (/= (aref totals i) 0) | |
3243 | (setq x (append x (list (cons i (aref totals i)))))) | |
3244 | (setq i (1+ i))) | |
3245 | ||
6c5bcbc1 | 3246 | (setq x (sort x (lambda (a b) (> (cdr a) (cdr b))))) |
f964dfcb GM |
3247 | (setq tot (apply '+ (append totals nil))) |
3248 | (sh-debug (format "vec: %s\ntotals: %s\ntot: %d" | |
6c5bcbc1 | 3249 | vec totals tot)) |
f964dfcb GM |
3250 | (cond |
3251 | ((zerop (length x)) | |
3252 | (message "no values!")) ;; we return nil | |
3253 | ((= (length x) 1) | |
3254 | (message "only value is %d" (car (car x))) | |
6c5bcbc1 | 3255 | (setq result (car (car x)))) ;; return single value |
f964dfcb GM |
3256 | ((> (cdr (car x)) (/ tot 2)) |
3257 | ;; 1st is > 50% | |
3258 | (message "basic-offset is probably %d" (car (car x))) | |
3259 | (setq result (car (car x)))) ;; again, return a single value | |
3260 | ((>= (cdr (car x)) (* 2 (cdr (car (cdr x))))) | |
3261 | ;; 1st is >= 2 * 2nd | |
3262 | (message "basic-offset could be %d" (car (car x))) | |
3263 | (setq result (car (car x)))) | |
3264 | ((>= (+ (cdr (car x))(cdr (car (cdr x)))) (/ tot 2)) | |
3265 | ;; 1st & 2nd together >= 50% - return a list | |
3266 | (setq p x sum 0 result nil) | |
3267 | (while (and p | |
3268 | (<= (setq sum (+ sum (cdr (car p)))) (/ tot 2))) | |
3269 | (setq result (append result (list (car (car p))))) | |
3270 | (setq p (cdr p))) | |
3271 | (message "Possible choices for sh-basic-offset: %s" | |
3272 | (mapconcat 'int-to-string result " "))) | |
3273 | (t | |
3274 | (message "No obvious value for sh-basic-offset. Perhaps %d" | |
3275 | (car (car x))) | |
3276 | ;; result is nil here | |
3277 | )) | |
34939e2c | 3278 | result))) |
f964dfcb GM |
3279 | |
3280 | ;; ======================================================================== | |
3281 | ||
8db2b9fb | 3282 | ;; Styles -- a quick and dirty way of saving the indentation settings. |
f964dfcb GM |
3283 | |
3284 | (defvar sh-styles-alist nil | |
3285 | "A list of all known shell indentation styles.") | |
3286 | ||
3287 | (defun sh-name-style (name &optional confirm-overwrite) | |
3288 | "Name the current indentation settings as a style called NAME. | |
8db2b9fb | 3289 | If this name exists, the command will prompt whether it should be |
f964dfcb | 3290 | overwritten if |
8db2b9fb | 3291 | - - it was called interactively with a prefix argument, or |
f964dfcb GM |
3292 | - - called non-interactively with optional CONFIRM-OVERWRITE non-nil." |
3293 | ;; (interactive "sName for this style: ") | |
3294 | (interactive | |
3295 | (list | |
3296 | (read-from-minibuffer "Name for this style? " ) | |
3297 | (not current-prefix-arg))) | |
6c5bcbc1 SM |
3298 | (let ((slist (cons name |
3299 | (mapcar (lambda (var) (cons var (symbol-value var))) | |
3300 | sh-var-list))) | |
3301 | (style (assoc name sh-styles-alist))) | |
3302 | (if style | |
3303 | (if (and confirm-overwrite | |
3304 | (not (y-or-n-p "This style exists. Overwrite it? "))) | |
3305 | (message "Not changing style %s" name) | |
3306 | (message "Updating style %s" name) | |
3307 | (setcdr style (cdr slist))) | |
f964dfcb | 3308 | (message "Creating new style %s" name) |
6c5bcbc1 | 3309 | (push slist sh-styles-alist)))) |
f964dfcb GM |
3310 | |
3311 | (defun sh-load-style (name) | |
3312 | "Set shell indentation values for this buffer from those in style NAME." | |
3313 | (interactive (list (completing-read | |
3314 | "Which style to use for this buffer? " | |
3315 | sh-styles-alist nil t))) | |
3316 | (let ((sl (assoc name sh-styles-alist))) | |
3317 | (if (null sl) | |
3318 | (error "sh-load-style - style %s not known" name) | |
6c5bcbc1 SM |
3319 | (dolist (var (cdr sl)) |
3320 | (set (car var) (cdr var)))))) | |
f964dfcb GM |
3321 | |
3322 | (defun sh-save-styles-to-buffer (buff) | |
3323 | "Save all current styles in elisp to buffer BUFF. | |
3324 | This is always added to the end of the buffer." | |
3325 | (interactive (list | |
6c5bcbc1 SM |
3326 | (read-from-minibuffer "Buffer to save styles in? " "*scratch*"))) |
3327 | (with-current-buffer (get-buffer-create buff) | |
f964dfcb GM |
3328 | (goto-char (point-max)) |
3329 | (insert "\n") | |
6c5bcbc1 | 3330 | (pp `(setq sh-styles-alist ',sh-styles-alist) (current-buffer)))) |
f964dfcb GM |
3331 | |
3332 | ||
3333 | \f | |
ac59aed8 RS |
3334 | ;; statement syntax-commands for various shells |
3335 | ||
3336 | ;; You are welcome to add the syntax or even completely new statements as | |
3337 | ;; appropriate for your favorite shell. | |
3338 | ||
017708e9 SM |
3339 | (defconst sh-non-closing-paren |
3340 | ;; If we leave it rear-sticky, calling `newline' ends up inserting a \n | |
3341 | ;; that inherits this property, which then confuses the indentation. | |
3342 | (propertize ")" 'syntax-table sh-st-punc 'rear-nonsticky t)) | |
3343 | ||
c410bd65 RS |
3344 | (define-skeleton sh-case |
3345 | "Insert a case/switch statement. See `sh-feature'." | |
cef926f3 RS |
3346 | (csh "expression: " |
3347 | "switch( " str " )" \n | |
3348 | > "case " (read-string "pattern: ") ?: \n | |
c410bd65 | 3349 | > _ \n |
cef926f3 | 3350 | "breaksw" \n |
c410bd65 | 3351 | ( "other pattern, %s: " |
cef926f3 | 3352 | < "case " str ?: \n |
c410bd65 | 3353 | > _ \n |
cef926f3 RS |
3354 | "breaksw" \n) |
3355 | < "default:" \n | |
c410bd65 RS |
3356 | > _ \n |
3357 | resume: | |
3e2dd647 | 3358 | < < "endsw" \n) |
cef926f3 RS |
3359 | (es) |
3360 | (rc "expression: " | |
f964dfcb | 3361 | > "switch( " str " ) {" \n |
cef926f3 RS |
3362 | > "case " (read-string "pattern: ") \n |
3363 | > _ \n | |
3364 | ( "other pattern, %s: " | |
f964dfcb | 3365 | "case " str > \n |
cef926f3 | 3366 | > _ \n) |
f964dfcb | 3367 | "case *" > \n |
cef926f3 RS |
3368 | > _ \n |
3369 | resume: | |
035107fa | 3370 | ?\} > \n) |
cef926f3 | 3371 | (sh "expression: " |
f964dfcb | 3372 | > "case " str " in" \n |
017708e9 SM |
3373 | ( "pattern, %s: " |
3374 | > str sh-non-closing-paren \n | |
cef926f3 | 3375 | > _ \n |
8f0b0ca5 | 3376 | ";;" \n) |
017708e9 | 3377 | > "*" sh-non-closing-paren \n |
cef926f3 RS |
3378 | > _ \n |
3379 | resume: | |
3e2dd647 | 3380 | "esac" > \n)) |
133693bc KH |
3381 | |
3382 | (define-skeleton sh-for | |
3383 | "Insert a for loop. See `sh-feature'." | |
6b61353c | 3384 | (csh sh-modify sh |
f964dfcb GM |
3385 | 1 "" |
3386 | 2 "foreach " | |
3387 | 4 " ( " | |
3388 | 6 " )" | |
3389 | 15 '< | |
b36581fb | 3390 | 16 "end") |
6b61353c | 3391 | (es sh-modify rc |
f964dfcb | 3392 | 4 " = ") |
6b61353c | 3393 | (rc sh-modify sh |
f964dfcb GM |
3394 | 2 "for( " |
3395 | 6 " ) {" | |
035107fa | 3396 | 15 ?\} ) |
ac59aed8 | 3397 | (sh "Index variable: " |
f964dfcb | 3398 | > "for " str " in " _ "; do" \n |
133693bc | 3399 | > _ | ?$ & (sh-remember-variable str) \n |
3e2dd647 | 3400 | "done" > \n)) |
ac59aed8 RS |
3401 | |
3402 | ||
3403 | ||
133693bc KH |
3404 | (define-skeleton sh-indexed-loop |
3405 | "Insert an indexed loop from 1 to n. See `sh-feature'." | |
6b61353c | 3406 | (bash sh-modify posix) |
ac59aed8 RS |
3407 | (csh "Index variable: " |
3408 | "@ " str " = 1" \n | |
133693bc KH |
3409 | "while( $" str " <= " (read-string "upper limit: ") " )" \n |
3410 | > _ ?$ str \n | |
ac59aed8 | 3411 | "@ " str "++" \n |
3e2dd647 | 3412 | < "end" \n) |
6b61353c | 3413 | (es sh-modify rc |
f964dfcb | 3414 | 4 " =") |
133693bc | 3415 | (ksh88 "Index variable: " |
f964dfcb GM |
3416 | > "integer " str "=0" \n |
3417 | > "while (( ( " str " += 1 ) <= " | |
133693bc KH |
3418 | (read-string "upper limit: ") |
3419 | " )); do" \n | |
f964dfcb | 3420 | > _ ?$ (sh-remember-variable str) > \n |
3e2dd647 | 3421 | "done" > \n) |
133693bc | 3422 | (posix "Index variable: " |
f964dfcb | 3423 | > str "=1" \n |
133693bc KH |
3424 | "while [ $" str " -le " |
3425 | (read-string "upper limit: ") | |
3426 | " ]; do" \n | |
3427 | > _ ?$ str \n | |
3428 | str ?= (sh-add (sh-remember-variable str) 1) \n | |
3e2dd647 | 3429 | "done" > \n) |
133693bc | 3430 | (rc "Index variable: " |
f964dfcb | 3431 | > "for( " str " in" " `{awk 'BEGIN { for( i=1; i<=" |
133693bc | 3432 | (read-string "upper limit: ") |
f964dfcb | 3433 | "; i++ ) print i }'`}) {" \n |
133693bc | 3434 | > _ ?$ (sh-remember-variable str) \n |
035107fa | 3435 | ?\} > \n) |
133693bc | 3436 | (sh "Index variable: " |
f964dfcb | 3437 | > "for " str " in `awk 'BEGIN { for( i=1; i<=" |
133693bc KH |
3438 | (read-string "upper limit: ") |
3439 | "; i++ ) print i }'`; do" \n | |
3440 | > _ ?$ (sh-remember-variable str) \n | |
3e2dd647 | 3441 | "done" > \n)) |
ac59aed8 RS |
3442 | |
3443 | ||
5d73ac66 RS |
3444 | (defun sh-shell-initialize-variables () |
3445 | "Scan the buffer for variable assignments. | |
3446 | Add these variables to `sh-shell-variables'." | |
3447 | (message "Scanning buffer `%s' for variable assignments..." (buffer-name)) | |
3448 | (save-excursion | |
3449 | (goto-char (point-min)) | |
3450 | (setq sh-shell-variables-initialized t) | |
3451 | (while (search-forward "=" nil t) | |
3452 | (sh-assignment 0))) | |
3453 | (message "Scanning buffer `%s' for variable assignments...done" | |
3454 | (buffer-name))) | |
3455 | ||
3456 | (defvar sh-add-buffer) | |
3457 | ||
3458 | (defun sh-add-completer (string predicate code) | |
3459 | "Do completion using `sh-shell-variables', but initialize it first. | |
3460 | This function is designed for use as the \"completion table\", | |
3461 | so it takes three arguments: | |
3462 | STRING, the current buffer contents; | |
3463 | PREDICATE, the predicate for filtering possible matches; | |
3464 | CODE, which says what kind of things to do. | |
3465 | CODE can be nil, t or `lambda'. | |
3466 | nil means to return the best completion of STRING, or nil if there is none. | |
3467 | t means to return a list of all possible completions of STRING. | |
3468 | `lambda' means to return t if STRING is a valid completion as it stands." | |
3469 | (let ((sh-shell-variables | |
090475f3 | 3470 | (with-current-buffer sh-add-buffer |
5d73ac66 RS |
3471 | (or sh-shell-variables-initialized |
3472 | (sh-shell-initialize-variables)) | |
3473 | (nconc (mapcar (lambda (var) | |
3474 | (let ((name | |
3475 | (substring var 0 (string-match "=" var)))) | |
3476 | (cons name name))) | |
3477 | process-environment) | |
3478 | sh-shell-variables)))) | |
017708e9 | 3479 | (case code |
6b61353c | 3480 | ((nil) (try-completion string sh-shell-variables predicate)) |
017708e9 SM |
3481 | (lambda (test-completion string sh-shell-variables predicate)) |
3482 | (t (all-completions string sh-shell-variables predicate))))) | |
5d73ac66 | 3483 | |
ac59aed8 | 3484 | (defun sh-add (var delta) |
133693bc | 3485 | "Insert an addition of VAR and prefix DELTA for Bourne (type) shell." |
ac59aed8 | 3486 | (interactive |
5d73ac66 RS |
3487 | (let ((sh-add-buffer (current-buffer))) |
3488 | (list (completing-read "Variable: " 'sh-add-completer) | |
3489 | (prefix-numeric-value current-prefix-arg)))) | |
546e2f6f | 3490 | (insert (sh-feature '((bash . "$(( ") |
133693bc KH |
3491 | (ksh88 . "$(( ") |
3492 | (posix . "$(( ") | |
3493 | (rc . "`{expr $") | |
3494 | (sh . "`expr $") | |
3495 | (zsh . "$[ "))) | |
3496 | (sh-remember-variable var) | |
3497 | (if (< delta 0) " - " " + ") | |
3498 | (number-to-string (abs delta)) | |
546e2f6f | 3499 | (sh-feature '((bash . " ))") |
133693bc KH |
3500 | (ksh88 . " ))") |
3501 | (posix . " ))") | |
3502 | (rc . "}") | |
3503 | (sh . "`") | |
3504 | (zsh . " ]"))))) | |
3505 | ||
3506 | ||
3507 | ||
3508 | (define-skeleton sh-function | |
3509 | "Insert a function definition. See `sh-feature'." | |
6b61353c | 3510 | (bash sh-modify ksh88 |
133693bc KH |
3511 | 3 "() {") |
3512 | (ksh88 "name: " | |
3513 | "function " str " {" \n | |
3514 | > _ \n | |
3e2dd647 | 3515 | < "}" \n) |
6b61353c | 3516 | (rc sh-modify ksh88 |
6c5bcbc1 | 3517 | 1 "fn ") |
ac59aed8 RS |
3518 | (sh () |
3519 | "() {" \n | |
3520 | > _ \n | |
3e2dd647 | 3521 | < "}" \n)) |
ac59aed8 RS |
3522 | |
3523 | ||
3524 | ||
133693bc KH |
3525 | (define-skeleton sh-if |
3526 | "Insert an if statement. See `sh-feature'." | |
ac59aed8 RS |
3527 | (csh "condition: " |
3528 | "if( " str " ) then" \n | |
3529 | > _ \n | |
3530 | ( "other condition, %s: " | |
133693bc KH |
3531 | < "else if( " str " ) then" \n |
3532 | > _ \n) | |
ac59aed8 | 3533 | < "else" \n |
133693bc | 3534 | > _ \n |
ac59aed8 | 3535 | resume: |
3e2dd647 | 3536 | < "endif" \n) |
133693bc | 3537 | (es "condition: " |
6c5bcbc1 SM |
3538 | > "if { " str " } {" \n |
3539 | > _ \n | |
3540 | ( "other condition, %s: " | |
3541 | "} { " str " } {" > \n | |
3542 | > _ \n) | |
3543 | "} {" > \n | |
3544 | > _ \n | |
3545 | resume: | |
035107fa | 3546 | ?\} > \n) |
f964dfcb | 3547 | (rc "condition: " |
6c5bcbc1 SM |
3548 | > "if( " str " ) {" \n |
3549 | > _ \n | |
3550 | ( "other condition, %s: " | |
3551 | "} else if( " str " ) {" > \n | |
3552 | > _ \n) | |
3553 | "} else {" > \n | |
3554 | > _ \n | |
3555 | resume: | |
035107fa | 3556 | ?\} > \n) |
133693bc | 3557 | (sh "condition: " |
225f6185 | 3558 | '(setq input (sh-feature sh-test)) |
f964dfcb | 3559 | > "if " str "; then" \n |
133693bc KH |
3560 | > _ \n |
3561 | ( "other condition, %s: " | |
6c5bcbc1 | 3562 | > "elif " str "; then" > \n |
8f0b0ca5 | 3563 | > \n) |
6c5bcbc1 | 3564 | "else" > \n |
f964dfcb | 3565 | > \n |
133693bc | 3566 | resume: |
3e2dd647 | 3567 | "fi" > \n)) |
ac59aed8 RS |
3568 | |
3569 | ||
3570 | ||
133693bc KH |
3571 | (define-skeleton sh-repeat |
3572 | "Insert a repeat loop definition. See `sh-feature'." | |
3573 | (es nil | |
f964dfcb | 3574 | > "forever {" \n |
133693bc | 3575 | > _ \n |
035107fa | 3576 | ?\} > \n) |
133693bc | 3577 | (zsh "factor: " |
f964dfcb | 3578 | > "repeat " str "; do" > \n |
6c5bcbc1 | 3579 | > \n |
3e2dd647 | 3580 | "done" > \n)) |
f964dfcb | 3581 | |
ea39159e | 3582 | ;;;(put 'sh-repeat 'menu-enable '(sh-feature sh-repeat)) |
133693bc KH |
3583 | |
3584 | ||
3585 | ||
3586 | (define-skeleton sh-select | |
3587 | "Insert a select statement. See `sh-feature'." | |
3588 | (ksh88 "Index variable: " | |
f964dfcb | 3589 | > "select " str " in " _ "; do" \n |
133693bc | 3590 | > ?$ str \n |
3e2dd647 | 3591 | "done" > \n) |
6b61353c | 3592 | (bash sh-append ksh88)) |
ea39159e | 3593 | ;;;(put 'sh-select 'menu-enable '(sh-feature sh-select)) |
133693bc KH |
3594 | |
3595 | ||
3596 | ||
3597 | (define-skeleton sh-tmp-file | |
3598 | "Insert code to setup temporary file handling. See `sh-feature'." | |
6b61353c | 3599 | (bash sh-append ksh88) |
133693bc | 3600 | (csh (file-name-nondirectory (buffer-file-name)) |
decb2a9e | 3601 | "set tmp = `mktemp -t " str ".XXXXXX`" \n |
133693bc KH |
3602 | "onintr exit" \n _ |
3603 | (and (goto-char (point-max)) | |
3604 | (not (bolp)) | |
3605 | ?\n) | |
3606 | "exit:\n" | |
3e2dd647 | 3607 | "rm $tmp* >&/dev/null" > \n) |
133693bc | 3608 | (es (file-name-nondirectory (buffer-file-name)) |
decb2a9e RS |
3609 | > "local( signals = $signals sighup sigint;" \n |
3610 | > "tmp = `{ mktemp -t " str ".XXXXXX } ) {" \n | |
133693bc KH |
3611 | > "catch @ e {" \n |
3612 | > "rm $tmp^* >[2]/dev/null" \n | |
3613 | "throw $e" \n | |
f964dfcb | 3614 | "} {" > \n |
6c5bcbc1 | 3615 | _ \n |
035107fa SS |
3616 | ?\} > \n |
3617 | ?\} > \n) | |
6b61353c | 3618 | (ksh88 sh-modify sh |
f964dfcb | 3619 | 7 "EXIT") |
133693bc | 3620 | (rc (file-name-nondirectory (buffer-file-name)) |
decb2a9e | 3621 | > "tmp = `{ mktemp -t " str ".XXXXXX }" \n |
3e2dd647 | 3622 | "fn sigexit { rm $tmp^* >[2]/dev/null }" \n) |
133693bc | 3623 | (sh (file-name-nondirectory (buffer-file-name)) |
decb2a9e | 3624 | > "TMP=`mktemp -t " str ".XXXXXX`" \n |
3e2dd647 | 3625 | "trap \"rm $TMP* 2>/dev/null\" " ?0 \n)) |
ac59aed8 RS |
3626 | |
3627 | ||
3628 | ||
133693bc KH |
3629 | (define-skeleton sh-until |
3630 | "Insert an until loop. See `sh-feature'." | |
ac59aed8 | 3631 | (sh "condition: " |
225f6185 | 3632 | '(setq input (sh-feature sh-test)) |
f964dfcb | 3633 | > "until " str "; do" \n |
ac59aed8 | 3634 | > _ \n |
3e2dd647 | 3635 | "done" > \n)) |
ea39159e | 3636 | ;;;(put 'sh-until 'menu-enable '(sh-feature sh-until)) |
133693bc KH |
3637 | |
3638 | ||
3639 | ||
3640 | (define-skeleton sh-while | |
3641 | "Insert a while loop. See `sh-feature'." | |
6b61353c | 3642 | (csh sh-modify sh |
f964dfcb GM |
3643 | 2 "" |
3644 | 3 "while( " | |
3645 | 5 " )" | |
3646 | 10 '< | |
b36581fb | 3647 | 11 "end") |
6b61353c | 3648 | (es sh-modify sh |
f964dfcb GM |
3649 | 3 "while { " |
3650 | 5 " } {" | |
035107fa | 3651 | 10 ?\} ) |
6b61353c | 3652 | (rc sh-modify sh |
f964dfcb GM |
3653 | 3 "while( " |
3654 | 5 " ) {" | |
035107fa | 3655 | 10 ?\} ) |
ac59aed8 | 3656 | (sh "condition: " |
225f6185 | 3657 | '(setq input (sh-feature sh-test)) |
f964dfcb | 3658 | > "while " str "; do" \n |
ac59aed8 | 3659 | > _ \n |
3e2dd647 | 3660 | "done" > \n)) |
133693bc KH |
3661 | |
3662 | ||
3663 | ||
3664 | (define-skeleton sh-while-getopts | |
3665 | "Insert a while getopts loop. See `sh-feature'. | |
3666 | Prompts for an options string which consists of letters for each recognized | |
3667 | option followed by a colon `:' if the option accepts an argument." | |
6b61353c | 3668 | (bash sh-modify sh |
133693bc | 3669 | 18 "${0##*/}") |
225f6185 KH |
3670 | (csh nil |
3671 | "while( 1 )" \n | |
3672 | > "switch( \"$1\" )" \n | |
3673 | '(setq input '("- x" . 2)) | |
3674 | > > | |
3675 | ( "option, %s: " | |
3676 | < "case " '(eval str) | |
3677 | '(if (string-match " +" str) | |
3678 | (setq v1 (substring str (match-end 0)) | |
3679 | str (substring str 0 (match-beginning 0))) | |
3680 | (setq v1 nil)) | |
3681 | str ?: \n | |
3682 | > "set " v1 & " = $2" | -4 & _ \n | |
3683 | (if v1 "shift") & \n | |
3684 | "breaksw" \n) | |
3685 | < "case --:" \n | |
3686 | > "shift" \n | |
3687 | < "default:" \n | |
3688 | > "break" \n | |
3689 | resume: | |
3690 | < < "endsw" \n | |
3691 | "shift" \n | |
3e2dd647 | 3692 | < "end" \n) |
6b61353c | 3693 | (ksh88 sh-modify sh |
133693bc KH |
3694 | 16 "print" |
3695 | 18 "${0##*/}" | |
bc387269 | 3696 | 37 "OPTIND-1") |
6b61353c | 3697 | (posix sh-modify sh |
133693bc KH |
3698 | 18 "$(basename $0)") |
3699 | (sh "optstring: " | |
f964dfcb | 3700 | > "while getopts :" str " OPT; do" \n |
133693bc | 3701 | > "case $OPT in" \n |
133693bc KH |
3702 | '(setq v1 (append (vconcat str) nil)) |
3703 | ( (prog1 (if v1 (char-to-string (car v1))) | |
3704 | (if (eq (nth 1 v1) ?:) | |
3705 | (setq v1 (nthcdr 2 v1) | |
3706 | v2 "\"$OPTARG\"") | |
3707 | (setq v1 (cdr v1) | |
3708 | v2 nil))) | |
017708e9 | 3709 | > str "|+" str sh-non-closing-paren \n |
133693bc | 3710 | > _ v2 \n |
8f0b0ca5 | 3711 | > ";;" \n) |
017708e9 | 3712 | > "*" sh-non-closing-paren \n |
133693bc | 3713 | > "echo" " \"usage: " "`basename $0`" |
c898fb28 | 3714 | " [+-" '(setq v1 (point)) str |
133693bc KH |
3715 | '(save-excursion |
3716 | (while (search-backward ":" v1 t) | |
c898fb28 | 3717 | (replace-match " ARG] [+-" t t))) |
133693bc | 3718 | (if (eq (preceding-char) ?-) -5) |
16ed8416 | 3719 | (if (and (sequencep v1) (length v1)) "] " "} ") |
119b42eb | 3720 | "[--] ARGS...\"" \n |
f964dfcb | 3721 | "exit 2" > \n |
6c5bcbc1 SM |
3722 | "esac" > |
3723 | \n "done" | |
3724 | > \n | |
546e2f6f GM |
3725 | "shift " (sh-add "OPTIND" -1) \n |
3726 | "OPTIND=1" \n)) | |
ac59aed8 RS |
3727 | |
3728 | ||
3729 | ||
3730 | (defun sh-assignment (arg) | |
133693bc | 3731 | "Remember preceding identifier for future completion and do self-insert." |
ac59aed8 | 3732 | (interactive "p") |
133693bc KH |
3733 | (self-insert-command arg) |
3734 | (if (<= arg 1) | |
ac59aed8 RS |
3735 | (sh-remember-variable |
3736 | (save-excursion | |
133693bc KH |
3737 | (if (re-search-forward (sh-feature sh-assignment-regexp) |
3738 | (prog1 (point) | |
3739 | (beginning-of-line 1)) | |
3740 | t) | |
84bfbb44 | 3741 | (match-string 1)))))) |
ac59aed8 RS |
3742 | |
3743 | ||
ac59aed8 | 3744 | (defun sh-maybe-here-document (arg) |
6c5bcbc1 | 3745 | "Insert self. Without prefix, following unquoted `<' inserts here document. |
ac59aed8 RS |
3746 | The document is bounded by `sh-here-document-word'." |
3747 | (interactive "*P") | |
3748 | (self-insert-command (prefix-numeric-value arg)) | |
3749 | (or arg | |
1abbe4e5 | 3750 | (not (looking-back "[^<]<<")) |
ac59aed8 | 3751 | (save-excursion |
133693bc | 3752 | (backward-char 2) |
ac59aed8 | 3753 | (sh-quoted-p)) |
546e2f6f GM |
3754 | (let ((tabs (if (string-match "\\`-" sh-here-document-word) |
3755 | (make-string (/ (current-indentation) tab-width) ?\t) | |
3756 | "")) | |
3757 | (delim (replace-regexp-in-string "['\"]" "" | |
3758 | sh-here-document-word))) | |
ac59aed8 | 3759 | (insert sh-here-document-word) |
1689f309 | 3760 | (or (eolp) (looking-at "[ \t]") (insert ?\s)) |
ac59aed8 | 3761 | (end-of-line 1) |
133693bc KH |
3762 | (while |
3763 | (sh-quoted-p) | |
3764 | (end-of-line 2)) | |
546e2f6f | 3765 | (insert ?\n tabs) |
18368c4a | 3766 | (save-excursion |
546e2f6f GM |
3767 | (insert ?\n tabs (replace-regexp-in-string |
3768 | "\\`-?[ \t]*" "" delim)))))) | |
ac59aed8 RS |
3769 | |
3770 | \f | |
3771 | ;; various other commands | |
3772 | ||
133693bc KH |
3773 | (autoload 'comint-dynamic-complete "comint" |
3774 | "Dynamically perform completion at point." t) | |
3775 | ||
3776 | (autoload 'shell-dynamic-complete-command "shell" | |
3777 | "Dynamically complete the command at point." t) | |
3778 | ||
ac59aed8 RS |
3779 | (autoload 'comint-dynamic-complete-filename "comint" |
3780 | "Dynamically complete the filename at point." t) | |
3781 | ||
133693bc KH |
3782 | (autoload 'shell-dynamic-complete-environment-variable "shell" |
3783 | "Dynamically complete the environment variable at point." t) | |
3784 | ||
ac59aed8 RS |
3785 | |
3786 | ||
ac59aed8 RS |
3787 | (defun sh-beginning-of-command () |
3788 | "Move point to successive beginnings of commands." | |
3789 | (interactive) | |
3790 | (if (re-search-backward sh-beginning-of-command nil t) | |
3791 | (goto-char (match-beginning 2)))) | |
3792 | ||
ac59aed8 RS |
3793 | (defun sh-end-of-command () |
3794 | "Move point to successive ends of commands." | |
3795 | (interactive) | |
3796 | (if (re-search-forward sh-end-of-command nil t) | |
3797 | (goto-char (match-end 1)))) | |
3798 | ||
bdd5fa99 RS |
3799 | ;; Backslashification. Stolen from make-mode.el. |
3800 | ||
3801 | (defun sh-backslash-region (from to delete-flag) | |
3802 | "Insert, align, or delete end-of-line backslashes on the lines in the region. | |
3803 | With no argument, inserts backslashes and aligns existing backslashes. | |
3804 | With an argument, deletes the backslashes. | |
3805 | ||
3806 | This function does not modify the last line of the region if the region ends | |
3807 | right at the start of the following line; it does not modify blank lines | |
546e2f6f | 3808 | at the start of the region. So you can put the region around an entire |
bdd5fa99 RS |
3809 | shell command and conveniently use this command." |
3810 | (interactive "r\nP") | |
3811 | (save-excursion | |
3812 | (goto-char from) | |
3813 | (let ((column sh-backslash-column) | |
3814 | (endmark (make-marker))) | |
3815 | (move-marker endmark to) | |
3816 | ;; Compute the smallest column number past the ends of all the lines. | |
3817 | (if sh-backslash-align | |
3818 | (progn | |
3819 | (if (not delete-flag) | |
3820 | (while (< (point) to) | |
3821 | (end-of-line) | |
3822 | (if (= (preceding-char) ?\\) | |
3823 | (progn (forward-char -1) | |
3824 | (skip-chars-backward " \t"))) | |
3825 | (setq column (max column (1+ (current-column)))) | |
3826 | (forward-line 1))) | |
3827 | ;; Adjust upward to a tab column, if that doesn't push | |
3828 | ;; past the margin. | |
3829 | (if (> (% column tab-width) 0) | |
3830 | (let ((adjusted (* (/ (+ column tab-width -1) tab-width) | |
3831 | tab-width))) | |
3832 | (if (< adjusted (window-width)) | |
3833 | (setq column adjusted)))))) | |
3834 | ;; Don't modify blank lines at start of region. | |
3835 | (goto-char from) | |
3836 | (while (and (< (point) endmark) (eolp)) | |
3837 | (forward-line 1)) | |
3838 | ;; Add or remove backslashes on all the lines. | |
3839 | (while (and (< (point) endmark) | |
3840 | ;; Don't backslashify the last line | |
3841 | ;; if the region ends right at the start of the next line. | |
3842 | (save-excursion | |
3843 | (forward-line 1) | |
3844 | (< (point) endmark))) | |
3845 | (if (not delete-flag) | |
3846 | (sh-append-backslash column) | |
3847 | (sh-delete-backslash)) | |
3848 | (forward-line 1)) | |
3849 | (move-marker endmark nil)))) | |
3850 | ||
3851 | (defun sh-append-backslash (column) | |
3852 | (end-of-line) | |
3853 | ;; Note that "\\\\" is needed to get one backslash. | |
3854 | (if (= (preceding-char) ?\\) | |
3855 | (progn (forward-char -1) | |
3856 | (delete-horizontal-space) | |
3857 | (indent-to column (if sh-backslash-align nil 1))) | |
3858 | (indent-to column (if sh-backslash-align nil 1)) | |
3859 | (insert "\\"))) | |
3860 | ||
3861 | (defun sh-delete-backslash () | |
3862 | (end-of-line) | |
3863 | (or (bolp) | |
3864 | (progn | |
3865 | (forward-char -1) | |
3866 | (if (looking-at "\\\\") | |
3867 | (delete-region (1+ (point)) | |
3868 | (progn (skip-chars-backward " \t") (point))))))) | |
3869 | ||
f7c7053e | 3870 | (provide 'sh-script) |
43c89a96 | 3871 | |
67b9b71f | 3872 | ;; arch-tag: eccd8b72-f337-4fc2-ae86-18155a69d937 |
f964dfcb | 3873 | ;;; sh-script.el ends here |