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