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