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