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