1 ;;; sh-script.el --- shell-script editing commands for Emacs
3 ;; Copyright (C) 1993, 94, 95, 96, 97, 1999 by Free Software Foundation, Inc.
5 ;; Author: Daniel Pfeiffer <occitan@esperanto.org>
8 ;; Keywords: languages, unix
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
29 ;; Major mode for editing shell scripts. Bourne, C and rc shells as well
30 ;; as various derivatives are supported and easily derived from. Structured
31 ;; statements can be inserted with one command or abbrev. Completion is
32 ;; available for filenames, variables known from the script, the shell and
33 ;; the environment as well as commands.
37 ;; - In Bourne the keyword `in' is not anchored to case, for, select ...
38 ;; - Variables in `"' strings aren't fontified because there's no way of
39 ;; syntactically distinguishing those from `'' strings.
43 ;; Indentation for rc and es modes is very limited, but for Bourne shells
44 ;; and its derivatives it is quite customizable.
46 ;; The following description applies to sh and derived shells (bash,
49 ;; There are various customization variables which allow tailoring to
50 ;; a wide variety of styles. Most of these variables are named
51 ;; sh-indent-for-XXX and sh-indent-after-XXX. For example.
52 ;; sh-indent-after-if controls the indenting of a line following
53 ;; an if statement, and sh-indent-for-fi controls the indentation
54 ;; of the line containing the fi.
56 ;; You can set each to a numeric value, but it is often more convenient
57 ;; to a symbol such as `+' which uses the value of variable `sh-basic-offset'.
58 ;; By changing this one variable you can increase or decrease how much
59 ;; indentation there is. Valid symbols:
61 ;; + Indent right by sh-basic-offset
62 ;; - Indent left by sh-basic-offset
63 ;; ++ Indent right twice sh-basic-offset
64 ;; -- Indent left twice sh-basic-offset
65 ;; * Indent right half sh-basic-offset
66 ;; / Indent left half sh-basic-offset.
68 ;; There are 4 commands to help set the indentation variables:
71 ;; This shows what variable controls the indentation of the current
72 ;; line and its value.
75 ;; This allows you to set the value of the variable controlling the
76 ;; current line's indentation. You can enter a number or one of a
77 ;; number of special symbols to denote the value of sh-basic-offset,
78 ;; or its negative, or half it, or twice it, etc. If you've used
79 ;; cc-mode this should be familiar. If you forget which symbols are
80 ;; valid simply press C-h at the prompt.
82 ;; `sh-learn-line-indent'
83 ;; Simply make the line look the way you want it, then invoke this
84 ;; command. It will set the variable to the value that makes the line
85 ;; indent like that. If called with a prefix argument then it will set
86 ;; the value to one of the symbols if applicable.
88 ;; `sh-learn-buffer-indent'
89 ;; This is the deluxe function! It "learns" the whole buffer (use
90 ;; narrowing if you want it to process only part). It outputs to a
91 ;; buffer *indent* any conflicts it finds, and all the variables it has
92 ;; learned. This buffer is a sort of Occur mode buffer, allowing you to
93 ;; easily find where something was set. It is popped to automatically
94 ;; if there are any conflicts found or if `sh-popup-occur-buffer' is
96 ;; `sh-indent-comment' will be set if all comments follow the same
97 ;; pattern; if they don't it will be set to nil.
98 ;; Whether `sh-basic-offset' is set is determined by variable
99 ;; `sh-learn-basic-offset'.
101 ;; Unfortunately, `sh-learn-buffer-indent' can take a long time to run
102 ;; (e.g. if there are large case statements). Perhaps it does not make
103 ;; sense to run it on large buffers: if lots of lines have different
104 ;; indentation styles it will produce a lot of diagnostics in the
105 ;; *indent* buffer; if there is a consistent style then running
106 ;; `sh-learn-buffer-indent' on a small region of the buffer should
109 ;; Saving indentation values
110 ;; -------------------------
111 ;; After you've learned the values in a buffer, how to you remember
112 ;; them? Originally I had hoped that `sh-learn-buffer-indent'
113 ;; would make this unnecessary; simply learn the values when you visit
115 ;; You can do this automatically like this:
116 ; (add-hook 'sh-set-shell-hook 'sh-learn-buffer-indent)
118 ;; However... `sh-learn-buffer-indent' is extremely slow,
119 ;; especially on large-ish buffer. Also, if there are conflicts the
120 ;; "last one wins" which may not produce the desired setting.
122 ;; So...There is a minimal way of being able to save indentation values and
123 ;; to reload them in another buffer or at another point in time.
125 ;; Use `sh-name-style' to give a name to the indentation settings of
126 ;; the current buffer.
127 ;; Use `sh-load-style' to load indentation settings for the current
128 ;; buffer from a specific style.
129 ;; Use `sh-save-styles-to-buffer' to write all the styles to a buffer
130 ;; in lisp code. You can then store it in a file and later use
131 ;; `load-file' to load it.
133 ;; Indentation variables - buffer local or global?
134 ;; ----------------------------------------------
135 ;; I think that often having them buffer-local makes sense,
136 ;; especially if one is using `sh-learn-buffer-indent'. However, if
137 ;; a user sets values using customization, these changes won't appear
138 ;; to work if the variables are already local!
140 ;; To get round this, there is a variable `sh-make-vars-local' and 2
141 ;; functions: `sh-make-vars-local' and `sh-reset-indent-vars-to-global-values'.
143 ;; If `sh-make-vars-local' is non-nil, then these variables become
144 ;; buffer local when the mode is established.
145 ;; If this is nil, then the variables are global. At any time you
146 ;; can make them local with the command `sh-make-vars-local'.
147 ;; Conversely, to update with the global values you can use the
148 ;; command `sh-reset-indent-vars-to-global-values'.
150 ;; This may be awkward, but the intent is to cover all cases.
152 ;; Awkward things, pitfalls
153 ;; ------------------------
154 ;; Indentation for a sh script is complicated for a number of reasons:
156 ;; 1. You can't format by simply looking at symbols, you need to look
157 ;; at keywords. [This is not the case for rc and es shells.]
158 ;; 2. The character ")" is used both as a matched pair "(" ... ")" and
159 ;; as a stand-alone symbol (in a case alternative). This makes
160 ;; things quite tricky!
161 ;; 3. Here-documents in a script should be treated "as is", and when
162 ;; they terminate we want to revert to the indentation of the line
163 ;; containing the "<<" symbol.
164 ;; 4. A line may be continued using the "\".
165 ;; 5. The character "#" (outside a string) normally starts a comment,
166 ;; but it doesn't in the sequence "$#"!
168 ;; To try and address points 2 3 and 5 I used a feature that cperl mode
169 ;; uses, that of a text's syntax property. This, however, has 2
171 ;; 1. We need to scan the buffer to find which ")" symbols belong to a
172 ;; case alternative, to find any here documents, and handle "$#".
173 ;; 2. Setting the text property makes the buffer modified. If the
174 ;; buffer is read-only buffer we have to cheat and bypass the read-only
175 ;; status. This is for cases where the buffer started read-only buffer
176 ;; but the user issued `toggle-read-only'.
180 ;; - Here-documents are marked with text properties face and syntax
181 ;; table. This serves 2 purposes: stopping indentation while inside
182 ;; them, and moving over them when finding the previous line to
183 ;; indent to. However, if font-lock mode is active when there is
184 ;; any change inside the here-document font-lock clears that
185 ;; property. This causes several problems: lines after the here-doc
186 ;; will not be re-indentation properly, words in the here-doc region
187 ;; may be fontified, and indentation may occur within the
189 ;; I'm not sure how to fix this, perhaps using the point-entered
190 ;; property. Anyway, if you use font lock and change a
191 ;; here-document, I recommend using M-x sh-rescan-buffer after the
192 ;; changes are made. Similarly, when using higlight-changes-mode,
193 ;; changes inside a here-document may confuse shell indenting, but again
194 ;; using `sh-rescan-buffer' should fix them.
196 ;; - Indenting many lines is slow. It currently does each line
197 ;; independently, rather than saving state information.
199 ;; - `sh-learn-buffer-indent' is extremely slow.
201 ;; Richard Sharman <rsharman@pobox.com> June 1999.
205 ;; page 1: variables and settings
206 ;; page 2: indentation stuff
207 ;; page 3: mode-command and utility functions
208 ;; page 4: statement syntax-commands for various shells
209 ;; page 5: various other commands
211 (require 'executable
)
216 "Shell programming utilities"
220 (defgroup sh-script nil
226 (defcustom sh-ancestor-alist
244 "*Alist showing the direct ancestor of various shells.
245 This is the basis for `sh-feature'. See also `sh-alias-alist'.
246 By default we have the following three hierarchies:
249 jcsh C Shell with Job Control
251 itcsh ? Toronto C Shell
256 jsh Bourne Shell with Job Control
257 bash GNU Bourne Again Shell
260 dtksh CDE Desktop Korn Shell
261 pdksh Public Domain Korn Shell
262 wksh Window Korn Shell
264 oash SCO OA (curses) Shell
265 posix IEEE 1003.2 Shell Standard
267 :type
'(repeat (cons symbol symbol
))
271 (defcustom sh-alias-alist
272 (nconc (if (eq system-type
'gnu
/linux
)
275 ;; for the time being
278 "*Alist for transforming shell names to what they really are.
279 Use this where the name of the executable doesn't correspond to the type of
281 :type
'(repeat (cons symbol symbol
))
285 (defcustom sh-shell-file
287 ;; On MSDOS and Windows, collapse $SHELL to lower-case and remove
288 ;; the executable extension, so comparisons with the list of
289 ;; known shells work.
290 (and (memq system-type
'(ms-dos windows-nt
))
291 (let* ((shell (getenv "SHELL"))
293 (and shell
(file-name-nondirectory shell
))))
294 ;; shell-script mode doesn't support DOS/Windows shells,
295 ;; so use the default instead.
297 (member (downcase shell-base
)
298 '("command.com" "cmd.exe" "4dos.com" "ndos.com"
301 (file-name-sans-extension (downcase shell
)))))
304 "*The executable file name for the shell being programmed."
309 (defcustom sh-shell-arg
310 ;; bash does not need any options when run in a shell script,
314 ;; Bill_Mann@praxisint.com says -p with ksh can do harm.
316 ;; -p means don't initialize functions from the environment.
318 ;; Someone proposed -motif, but we don't want to encourage
319 ;; use of a non-free widget set.
321 ;; -f means don't run .zshrc.
323 "*Single argument string for the magic number. See `sh-feature'."
324 :type
'(repeat (cons (symbol :tag
"Shell")
325 (choice (const :tag
"No Arguments" nil
)
326 (string :tag
"Arguments")
327 (cons :format
"Evaluate: %v"
328 (const :format
"" eval
)
332 (defcustom sh-imenu-generic-expression
336 "\\(^\\s-*function\\s-+[A-Za-z_][A-Za-z_0-9]*\\)"
338 "\\(^\\s-*[A-Za-z_][A-Za-z_0-9]*\\s-*()\\)")))
339 "*Regular expression for recognizing shell function definitions.
341 :type
'(repeat (cons (symbol :tag
"Shell")
346 (defvar sh-shell-variables nil
347 "Alist of shell variable names that should be included in completion.
348 These are used for completion in addition to all the variables named
349 in `process-environment'. Each element looks like (VAR . VAR), where
350 the car and cdr are the same symbol.")
352 (defvar sh-shell-variables-initialized nil
353 "Non-nil if `sh-shell-variables' is initialized.")
355 (defun sh-canonicalize-shell (shell)
356 "Convert a shell name SHELL to the one we should handle it as."
357 (if (string-match "\\.exe\\'" shell
)
358 (setq shell
(substring shell
0 (match-beginning 0))))
360 (setq shell
(intern shell
)))
361 (or (cdr (assq shell sh-alias-alist
))
364 (defvar sh-shell
(sh-canonicalize-shell (file-name-nondirectory sh-shell-file
))
365 "The shell being programmed. This is set by \\[sh-set-shell].")
367 ;;; I turned off this feature because it doesn't permit typing commands
368 ;;; in the usual way without help.
369 ;;;(defvar sh-abbrevs
370 ;;; '((csh eval sh-abbrevs shell
371 ;;; "switch" 'sh-case
372 ;;; "getopts" 'sh-while-getopts)
374 ;;; (es eval sh-abbrevs shell
375 ;;; "function" 'sh-function)
377 ;;; (ksh88 eval sh-abbrevs sh
378 ;;; "select" 'sh-select)
380 ;;; (rc eval sh-abbrevs shell
382 ;;; "function" 'sh-function)
384 ;;; (sh eval sh-abbrevs shell
386 ;;; "function" 'sh-function
387 ;;; "until" 'sh-until
388 ;;; "getopts" 'sh-while-getopts)
390 ;;; ;; The next entry is only used for defining the others
391 ;;; (shell "for" sh-for
392 ;;; "loop" sh-indexed-loop
394 ;;; "tmpfile" sh-tmp-file
395 ;;; "while" sh-while)
397 ;;; (zsh eval sh-abbrevs ksh88
398 ;;; "repeat" 'sh-repeat))
399 ;;; "Abbrev-table used in Shell-Script mode. See `sh-feature'.
400 ;;;Due to the internal workings of abbrev tables, the shell name symbol is
401 ;;;actually defined as the table for the like of \\[edit-abbrevs].")
405 (defvar sh-mode-syntax-table
406 '((sh eval sh-mode-syntax-table
()
419 (csh eval identity sh
)
420 (rc eval identity sh
))
421 "Syntax-table used in Shell-Script mode. See `sh-feature'.")
426 (let ((map (make-sparse-keymap))
427 (menu-map (make-sparse-keymap "Insert")))
428 (define-key map
"\C-c(" 'sh-function
)
429 (define-key map
"\C-c\C-w" 'sh-while
)
430 (define-key map
"\C-c\C-u" 'sh-until
)
431 (define-key map
"\C-c\C-t" 'sh-tmp-file
)
432 (define-key map
"\C-c\C-s" 'sh-select
)
433 (define-key map
"\C-c\C-r" 'sh-repeat
)
434 (define-key map
"\C-c\C-o" 'sh-while-getopts
)
435 (define-key map
"\C-c\C-l" 'sh-indexed-loop
)
436 (define-key map
"\C-c\C-i" 'sh-if
)
437 (define-key map
"\C-c\C-f" 'sh-for
)
438 (define-key map
"\C-c\C-c" 'sh-case
)
439 (define-key map
"\C-c?" 'sh-show-indent
)
440 (define-key map
"\C-c=" 'sh-set-indent
)
441 (define-key map
"\C-c<" 'sh-learn-line-indent
)
442 (define-key map
"\C-c>" 'sh-learn-buffer-indent
)
444 (define-key map
"=" 'sh-assignment
)
445 (define-key map
"\C-c+" 'sh-add
)
446 (define-key map
"\C-\M-x" 'sh-execute-region
)
447 (define-key map
"\C-c\C-x" 'executable-interpret
)
448 (define-key map
"<" 'sh-maybe-here-document
)
449 (define-key map
"(" 'skeleton-pair-insert-maybe
)
450 (define-key map
"{" 'skeleton-pair-insert-maybe
)
451 (define-key map
"[" 'skeleton-pair-insert-maybe
)
452 (define-key map
"'" 'skeleton-pair-insert-maybe
)
453 (define-key map
"`" 'skeleton-pair-insert-maybe
)
454 (define-key map
"\"" 'skeleton-pair-insert-maybe
)
455 (define-key map
")" 'sh-electric-rparen
)
456 (define-key map
"<" 'sh-electric-less
)
457 (define-key map
"#" 'sh-electric-hash
)
459 (substitute-key-definition 'complete-tag
'comint-dynamic-complete
460 map
(current-global-map))
461 (substitute-key-definition 'newline-and-indent
'sh-newline-and-indent
462 map
(current-global-map))
463 (substitute-key-definition 'delete-backward-char
464 'backward-delete-char-untabify
465 map
(current-global-map))
466 (define-key map
"\C-c:" 'sh-set-shell
)
467 (substitute-key-definition 'beginning-of-defun
468 'sh-beginning-of-compound-command
469 map
(current-global-map))
470 (substitute-key-definition 'backward-sentence
'sh-beginning-of-command
471 map
(current-global-map))
472 (substitute-key-definition 'forward-sentence
'sh-end-of-command
473 map
(current-global-map))
474 (define-key map
[menu-bar insert
] (cons "Insert" menu-map
))
475 (define-key menu-map
[sh-while
] '("While Loop" . sh-while
))
476 (define-key menu-map
[sh-until
] '("Until Loop" . sh-until
))
477 (define-key menu-map
[sh-tmp-file
] '("Temporary File" . sh-tmp-file
))
478 (define-key menu-map
[sh-select
] '("Select Statement" . sh-select
))
479 (define-key menu-map
[sh-repeat
] '("Repeat Loop" . sh-repeat
))
480 (define-key menu-map
[sh-while-getopts
]
481 '("Options Loop" . sh-while-getopts
))
482 (define-key menu-map
[sh-indexed-loop
]
483 '("Indexed Loop" . sh-indexed-loop
))
484 (define-key menu-map
[sh-if
] '("If Statement" . sh-if
))
485 (define-key menu-map
[sh-for
] '("For Loop" . sh-for
))
486 (define-key menu-map
[sh-case
] '("Case Statement" . sh-case
))
488 "Keymap used in Shell-Script mode.")
492 (defcustom sh-dynamic-complete-functions
493 '(shell-dynamic-complete-environment-variable
494 shell-dynamic-complete-command
495 comint-dynamic-complete-filename
)
496 "*Functions for doing TAB dynamic completion."
497 :type
'(repeat function
)
501 (defcustom sh-require-final-newline
504 (rc eval . require-final-newline
)
505 (sh eval . require-final-newline
))
506 "*Value of `require-final-newline' in Shell-Script mode buffers.
508 :type
'(repeat (cons (symbol :tag
"Shell")
509 (choice (const :tag
"require" t
)
510 (cons :format
"Evaluate: %v"
511 (const :format
"" eval
)
516 (defcustom sh-assignment-regexp
517 '((csh .
"\\<\\([a-zA-Z0-9_]+\\)\\(\\[.+\\]\\)?[ \t]*[-+*/%^]?=")
518 ;; actually spaces are only supported in let/(( ... ))
519 (ksh88 .
"\\<\\([a-zA-Z0-9_]+\\)\\(\\[.+\\]\\)?[ \t]*\\([-+*/%&|~^]\\|<<\\|>>\\)?=")
520 (rc .
"\\<\\([a-zA-Z0-9_*]+\\)[ \t]*=")
521 (sh .
"\\<\\([a-zA-Z0-9_]+\\)="))
522 "*Regexp for the variable name and what may follow in an assignment.
523 First grouping matches the variable name. This is upto and including the `='
524 sign. See `sh-feature'."
525 :type
'(repeat (cons (symbol :tag
"Shell")
527 (cons :format
"Evaluate: %v"
528 (const :format
"" eval
)
533 (defcustom sh-indentation
4
534 "The width for further indentation in Shell-Script mode."
539 (defcustom sh-remember-variable-min
3
540 "*Don't remember variables less than this length for completing reads."
545 (defvar sh-header-marker nil
546 "When non-nil is the end of header for prepending by \\[sh-execute-region].
547 That command is also used for setting this variable.")
550 (defcustom sh-beginning-of-command
551 "\\([;({`|&]\\|\\`\\|[^\\]\n\\)[ \t]*\\([/~a-zA-Z0-9:]\\)"
552 "*Regexp to determine the beginning of a shell command.
553 The actual command starts at the beginning of the second \\(grouping\\)."
558 (defcustom sh-end-of-command
559 "\\([/~a-zA-Z0-9:]\\)[ \t]*\\([;#)}`|&]\\|$\\)"
560 "*Regexp to determine the end of a shell command.
561 The actual command ends at the end of the first \\(grouping\\)."
567 (defvar sh-here-document-word
"EOF"
568 "Word to delimit here documents.")
573 "Initial input in Bourne if, while and until skeletons. See `sh-feature'.")
576 ;; customized this out of sheer bravado. not for the faint of heart.
577 ;; but it *did* have an asterisk in the docstring!
578 (defcustom sh-builtins
579 '((bash eval sh-append posix
580 "alias" "bg" "bind" "builtin" "declare" "dirs" "enable" "fc" "fg"
581 "help" "history" "jobs" "kill" "let" "local" "popd" "pushd" "source"
582 "suspend" "typeset" "unalias")
584 ;; The next entry is only used for defining the others
585 (bourne eval sh-append shell
586 "eval" "export" "getopts" "newgrp" "pwd" "read" "readonly"
589 (csh eval sh-append shell
590 "alias" "chdir" "glob" "history" "limit" "nice" "nohup" "rehash"
591 "setenv" "source" "time" "unalias" "unhash")
593 (dtksh eval identity wksh
)
595 (es "access" "apids" "cd" "echo" "eval" "false" "let" "limit" "local"
596 "newpgrp" "result" "time" "umask" "var" "vars" "wait" "whatis")
598 (jsh eval sh-append sh
599 "bg" "fg" "jobs" "kill" "stop" "suspend")
601 (jcsh eval sh-append csh
602 "bg" "fg" "jobs" "kill" "notify" "stop" "suspend")
604 (ksh88 eval sh-append bourne
605 "alias" "bg" "false" "fc" "fg" "jobs" "kill" "let" "print" "time"
606 "typeset" "unalias" "whence")
608 (oash eval sh-append sh
609 "checkwin" "dateline" "error" "form" "menu" "newwin" "oadeinit"
610 "oaed" "oahelp" "oainit" "pp" "ppfile" "scan" "scrollok" "wattr"
611 "wclear" "werase" "win" "wmclose" "wmmessage" "wmopen" "wmove"
612 "wmtitle" "wrefresh")
614 (pdksh eval sh-append ksh88
617 (posix eval sh-append sh
620 (rc "builtin" "cd" "echo" "eval" "limit" "newpgrp" "shift" "umask" "wait"
623 (sh eval sh-append bourne
624 "hash" "test" "type")
626 ;; The next entry is only used for defining the others
627 (shell "cd" "echo" "eval" "set" "shift" "umask" "unset" "wait")
629 (wksh eval sh-append ksh88
632 (zsh eval sh-append ksh88
633 "autoload" "bindkey" "builtin" "chdir" "compctl" "declare" "dirs"
634 "disable" "disown" "echotc" "enable" "functions" "getln" "hash"
635 "history" "integer" "limit" "local" "log" "popd" "pushd" "r"
636 "readonly" "rehash" "sched" "setopt" "source" "suspend" "true"
637 "ttyctl" "type" "unfunction" "unhash" "unlimit" "unsetopt" "vared"
639 "*List of all shell builtins for completing read and fontification.
640 Note that on some systems not all builtins are available or some are
641 implemented as aliases. See `sh-feature'."
642 :type
'(repeat (cons (symbol :tag
"Shell")
643 (choice (repeat string
)
644 (cons :format
"Evaluate: %v"
645 (const :format
"" eval
)
651 (defcustom sh-leading-keywords
654 (es "true" "unwind-protect" "whatis")
658 (sh "do" "elif" "else" "if" "then" "trap" "type" "until" "while"))
659 "*List of keywords that may be immediately followed by a builtin or keyword.
660 Given some confusion between keywords and builtins depending on shell and
661 system, the distinction here has been based on whether they influence the
662 flow of control or syntax. See `sh-feature'."
663 :type
'(repeat (cons (symbol :tag
"Shell")
664 (choice (repeat string
)
665 (cons :format
"Evaluate: %v"
666 (const :format
"" eval
)
671 (defcustom sh-other-keywords
672 '((bash eval sh-append bourne
675 ;; The next entry is only used for defining the others
676 (bourne eval sh-append sh
679 (csh eval sh-append shell
680 "breaksw" "default" "end" "endif" "endsw" "foreach" "goto"
681 "if" "logout" "onintr" "repeat" "switch" "then" "while")
683 (es "break" "catch" "exec" "exit" "fn" "for" "forever" "fork" "if"
684 "return" "throw" "while")
686 (ksh88 eval sh-append bourne
689 (rc "break" "case" "exec" "exit" "fn" "for" "if" "in" "return" "switch"
692 (sh eval sh-append shell
693 "done" "esac" "fi" "for" "in" "return")
695 ;; The next entry is only used for defining the others
696 (shell "break" "case" "continue" "exec" "exit")
698 (zsh eval sh-append bash
700 "*List of keywords not in `sh-leading-keywords'.
702 :type
'(repeat (cons (symbol :tag
"Shell")
703 (choice (repeat string
)
704 (cons :format
"Evaluate: %v"
705 (const :format
"" eval
)
712 '((bash eval sh-append sh
713 "allow_null_glob_expansion" "auto_resume" "BASH" "BASH_VERSION"
714 "cdable_vars" "ENV" "EUID" "FCEDIT" "FIGNORE" "glob_dot_filenames"
715 "histchars" "HISTFILE" "HISTFILESIZE" "history_control" "HISTSIZE"
716 "hostname_completion_file" "HOSTTYPE" "IGNOREEOF" "ignoreeof"
717 "LINENO" "MAIL_WARNING" "noclobber" "nolinks" "notify"
718 "no_exit_on_failed_exec" "NO_PROMPT_VARS" "OLDPWD" "OPTERR" "PPID"
719 "PROMPT_COMMAND" "PS4" "pushd_silent" "PWD" "RANDOM" "REPLY"
720 "SECONDS" "SHLVL" "TMOUT" "UID")
722 (csh eval sh-append shell
723 "argv" "cdpath" "child" "echo" "histchars" "history" "home"
724 "ignoreeof" "mail" "noclobber" "noglob" "nonomatch" "path" "prompt"
725 "shell" "status" "time" "verbose")
727 (es eval sh-append shell
728 "apid" "cdpath" "CDPATH" "history" "home" "ifs" "noexport" "path"
729 "pid" "prompt" "signals")
731 (jcsh eval sh-append csh
734 (ksh88 eval sh-append sh
735 "ENV" "ERRNO" "FCEDIT" "FPATH" "HISTFILE" "HISTSIZE" "LINENO"
736 "OLDPWD" "PPID" "PS3" "PS4" "PWD" "RANDOM" "REPLY" "SECONDS"
739 (oash eval sh-append sh
740 "FIELD" "FIELD_MAX" "LAST_KEY" "OALIB" "PP_ITEM" "PP_NUM")
742 (rc eval sh-append shell
743 "apid" "apids" "cdpath" "CDPATH" "history" "home" "ifs" "path" "pid"
746 (sh eval sh-append shell
747 "CDPATH" "IFS" "OPTARG" "OPTIND" "PS1" "PS2")
749 ;; The next entry is only used for defining the others
750 (shell "COLUMNS" "EDITOR" "HOME" "HUSHLOGIN" "LANG" "LC_COLLATE"
751 "LC_CTYPE" "LC_MESSAGES" "LC_MONETARY" "LC_NUMERIC" "LC_TIME"
752 "LINES" "LOGNAME" "MAIL" "MAILCHECK" "MAILPATH" "PAGER" "PATH"
753 "SHELL" "TERM" "TERMCAP" "TERMINFO" "VISUAL")
755 (tcsh eval sh-append csh
756 "addsuffix" "ampm" "autocorrect" "autoexpand" "autolist"
757 "autologout" "chase_symlinks" "correct" "dextract" "edit" "el"
758 "fignore" "gid" "histlit" "HOST" "HOSTTYPE" "HPATH"
759 "ignore_symlinks" "listjobs" "listlinks" "listmax" "matchbeep"
760 "nobeep" "NOREBIND" "oid" "printexitvalue" "prompt2" "prompt3"
761 "pushdsilent" "pushdtohome" "recexact" "recognize_only_executables"
762 "rmstar" "savehist" "SHLVL" "showdots" "sl" "SYSTYPE" "tcsh" "term"
763 "tperiod" "tty" "uid" "version" "visiblebell" "watch" "who"
766 (zsh eval sh-append ksh88
767 "BAUD" "bindcmds" "cdpath" "DIRSTACKSIZE" "fignore" "FIGNORE" "fpath"
768 "HISTCHARS" "hostcmds" "hosts" "HOSTS" "LISTMAX" "LITHISTSIZE"
769 "LOGCHECK" "mailpath" "manpath" "NULLCMD" "optcmds" "path" "POSTEDIT"
770 "prompt" "PROMPT" "PROMPT2" "PROMPT3" "PROMPT4" "psvar" "PSVAR"
771 "READNULLCMD" "REPORTTIME" "RPROMPT" "RPS1" "SAVEHIST" "SPROMPT"
772 "STTY" "TIMEFMT" "TMOUT" "TMPPREFIX" "varcmds" "watch" "WATCH"
773 "WATCHFMT" "WORDCHARS" "ZDOTDIR"))
774 "List of all shell variables available for completing read.
779 (defvar sh-font-lock-keywords
780 '((csh eval sh-append shell
781 '("\\${?[#?]?\\([A-Za-z_][A-Za-z0-9_]*\\|0\\)" 1
782 font-lock-variable-name-face
))
784 (es eval sh-append executable-font-lock-keywords
785 '("\\$#?\\([A-Za-z_][A-Za-z0-9_]*\\|[0-9]+\\)" 1
786 font-lock-variable-name-face
))
788 (rc eval identity es
)
790 (sh eval sh-append shell
792 '("\\$\\({#?\\)?\\([A-Za-z_][A-Za-z0-9_]*\\|[-#?@!]\\)" 2
793 font-lock-variable-name-face
)
795 '("^\\(\\sw+\\)[ \t]*(" 1 font-lock-function-name-face
)
796 '("\\<\\(function\\)\\>[ \t]*\\(\\sw+\\)?"
797 (1 font-lock-keyword-face
) (2 font-lock-function-name-face nil t
)))
799 ;; The next entry is only used for defining the others
800 (shell eval sh-append executable-font-lock-keywords
801 '("\\\\[^A-Za-z0-9]" 0 font-lock-string-face
)
802 '("\\${?\\([A-Za-z_][A-Za-z0-9_]*\\|[0-9]+\\|[$*_]\\)" 1
803 font-lock-variable-name-face
))
804 (rpm eval sh-append rpm2
805 '("%{?\\(\\sw+\\)" 1 font-lock-keyword-face
))
806 (rpm2 eval sh-append shell
807 '("^\\(\\sw+\\):" 1 font-lock-variable-name-face
)))
808 "Default expressions to highlight in Shell Script modes. See `sh-feature'.")
810 (defvar sh-font-lock-keywords-1
812 "Subdued level highlighting for Shell Script modes.")
814 (defvar sh-font-lock-keywords-2
()
815 "Gaudy level highlighting for Shell Script modes.")
817 (defconst sh-font-lock-syntactic-keywords
818 ;; Mark a `#' character as having punctuation syntax in a variable reference.
819 ;; Really we should do this properly. From Chet Ramey and Brian Fox:
820 ;; "A `#' begins a comment when it is unquoted and at the beginning of a
821 ;; word. In the shell, words are separated by metacharacters."
822 ;; To do this in a regexp would be slow as it would be anchored to the right.
823 ;; But I can't be bothered to write a function to do it properly and
824 ;; efficiently. So we only do it properly for `#' in variable references and
825 ;; do it efficiently by anchoring the regexp to the left.
826 '(("\\${?[^}#\n\t ]*\\(##?\\)" 1 (1 . nil
))))
828 (defgroup sh-indentation nil
829 "Variables controlling indentation in shell scripts.
831 Note: customizing these variables will not affect existing buffers if
832 `sh-make-vars-local' is no-nil. See the documentation for
833 variable `sh-make-vars-local', command `sh-make-vars-local'
834 and command `sh-reset-indent-vars-to-global-values'."
838 (defcustom sh-set-shell-hook nil
839 "*Hook run by `sh-set-shell'."
843 (defcustom sh-mode-hook nil
844 "*Hook run by `sh-mode'."
848 (defcustom sh-learn-basic-offset nil
849 "*When `sh-guess-basic-offset' should learn `sh-basic-offset'.
852 t means: only if there seems to be an obvious value.
853 Anything else means: whenever we have a \"good guess\" as to the value."
855 (const :tag
"Never" nil
)
856 (const :tag
"Only if sure" t
)
857 (const :tag
"If have a good guess" usually
)
859 :group
'sh-indentation
)
861 (defcustom sh-popup-occur-buffer nil
862 "*Controls when `sh-learn-buffer-indent' poos the *indent* buffer.
863 If t it is always shown. If nil, it is shown only when there
866 (const :tag
"Only when there are conflicts." nil
)
867 (const :tag
"Always" t
)
869 :group
'sh-indentation
)
871 (defcustom sh-blink t
872 "*If non-nil, `sh-show-indent' shows the line indentation is relative to.
873 The position on the line is not necessarily meaningful.
874 In some cases the line will be the matching keyword, but this is not
877 :group
'sh-indentation
)
879 (defcustom sh-first-lines-indent
0
880 "*The indentation of the first non-blank non-comment line.
881 Usually 0 meaning first column.
882 Can be set to a number, or to nil which means leave it as is."
884 (const :tag
"Leave as is" nil
)
885 (integer :tag
"Column number"
886 :menu-tag
"Indent to this col (0 means first col)" )
888 :group
'sh-indentation
)
891 (defcustom sh-basic-offset
4
892 "*The default indentation incrementation.
893 This value is used for the + and - symbols in an indentation variable."
895 :group
'sh-indentation
)
897 (defcustom sh-indent-comment nil
898 "*How a comment line is to be indented.
899 nil means leave it as it is;
900 t means indent it as a normal line, aligning it to previous non-blank
902 a number means align to that column, e.g. 0 means fist column."
904 (const :tag
"Leave as is." nil
)
905 (const :tag
"Indent as a normal line." t
)
906 (integer :menu-tag
"Indent to this col (0 means first col)."
907 :tag
"Indent to column number.") )
908 :group
'sh-indentation
)
912 "Enable lots of debug messages - if function `sh-debug' is enabled.")
915 ;; Uncomment this defun and comment the defmacro for debugging.
916 ;; (defun sh-debug (&rest args)
917 ;; "For debugging: display message ARGS if variable SH-DEBUG is non-nil."
919 ;; (apply 'message args)))
920 (defmacro sh-debug
(&rest args
))
924 (const :tag
"+ " :value
+
925 :menu-tag
"+ Indent right by sh-basic-offset")
926 (const :tag
"- " :value -
927 :menu-tag
"- Indent left by sh-basic-offset")
928 (const :tag
"++" :value
++
929 :menu-tag
"++ Indent right twice sh-basic-offset")
930 (const :tag
"--" :value --
931 :menu-tag
"-- Indent left twice sh-basic-offset")
932 (const :tag
"* " :value
*
933 :menu-tag
"* Indent right half sh-basic-offset")
934 (const :tag
"/ " :value
/
935 :menu-tag
"/ Indent left half sh-basic-offset")
938 (defcustom sh-indent-for-else
0
939 "*How much to indent an else relative to an if. Usually 0."
941 (integer :menu-tag
"A number (positive=>indent right)"
943 (const :tag
"--") ;; separator!
946 :group
'sh-indentation
)
948 (setq sh-number-or-symbol-list
950 integer
:menu-tag
"A number (positive=>indent right)"
952 '(const :tag
"--") ;; separator
956 (defcustom sh-indent-for-fi
0
957 "*How much to indent a fi relative to an if. Usually 0."
958 :type
`(choice ,@ sh-number-or-symbol-list
)
959 :group
'sh-indentation
)
961 (defcustom sh-indent-for-done
'0
962 "*How much to indent a done relative to its matching stmt. Usually 0."
963 :type
`(choice ,@ sh-number-or-symbol-list
)
964 :group
'sh-indentation
)
966 (defcustom sh-indent-after-else
'+
967 "*How much to indent a statement after an else statement."
968 :type
`(choice ,@ sh-number-or-symbol-list
)
969 :group
'sh-indentation
)
971 (defcustom sh-indent-after-if
'+
972 "*How much to indent a statement after an if statement.
973 This includes lines after else and elif statements, too, but
974 does not affect then else elif or fi statements themselves."
975 :type
`(choice ,@ sh-number-or-symbol-list
)
976 :group
'sh-indentation
)
978 (defcustom sh-indent-for-then
'+
979 "*How much to indent an then relative to an if."
980 :type
`(choice ,@ sh-number-or-symbol-list
)
981 :group
'sh-indentation
)
983 (defcustom sh-indent-for-do
'*
984 "*How much to indent a do statement.
985 This is relative to the statement before the do, i.e. the
986 while until or for statement."
987 :type
`(choice ,@ sh-number-or-symbol-list
)
988 :group
'sh-indentation
)
990 (defcustom sh-indent-after-do
'*
991 "*How much to indent a line after a do statement.
992 This is used when the do is the first word of the line.
993 This is relative to the statement before the do, e.g. a
994 while for repeat or select statement."
995 :type
`(choice ,@ sh-number-or-symbol-list
)
996 :group
'sh-indentation
)
998 (defcustom sh-indent-after-loop-construct
'+
999 "*How much to indent a statement after a loop construct.
1001 This variable is used when the keyword \"do\" is on the same line as the
1002 loop statement (e.g. \"until\", \"while\" or \"for\").
1003 If the do is on a line by itself, then `sh-indent-after-do' is used instead."
1004 :type
`(choice ,@ sh-number-or-symbol-list
)
1005 :group
'sh-indentation
)
1008 (defcustom sh-indent-after-done
0
1009 "*How much to indent a statement after a \"done\" keyword.
1010 Normally this is 0, which aligns the \"done\" to the matching
1011 looping construct line.
1012 Setting it non-zero allows you to have the \"do\" statement on a line
1013 by itself and align the done under to do."
1014 :type
`(choice ,@ sh-number-or-symbol-list
)
1015 :group
'sh-indentation
)
1017 (defcustom sh-indent-for-case-label
'+
1018 "*How much to indent a case label statement.
1019 This is relative to the line containing the case statement."
1020 :type
`(choice ,@ sh-number-or-symbol-list
)
1021 :group
'sh-indentation
)
1023 (defcustom sh-indent-for-case-alt
'++
1024 "*How much to indent statements after the case label.
1025 This is relative to the line containing the case statement."
1026 :type
`(choice ,@ sh-number-or-symbol-list
)
1027 :group
'sh-indentation
)
1030 (defcustom sh-indent-for-continuation
'+
1031 "*How much to indent for a continuation statement."
1032 :type
`(choice ,@ sh-number-or-symbol-list
)
1033 :group
'sh-indentation
)
1035 (defcustom sh-indent-after-open
'+
1036 "*How much to indent after a line with an opening parenthesis or brace.
1037 For an open paren after a function `sh-indent-after-function' is used."
1038 :type
`(choice ,@ sh-number-or-symbol-list
)
1039 :group
'sh-indentation
)
1041 (defcustom sh-indent-after-function
'+
1042 "*How much to indent after a function line."
1043 :type
`(choice ,@ sh-number-or-symbol-list
)
1044 :group
'sh-indentation
)
1046 ;; These 2 are for the rc shell:
1048 (defcustom sh-indent-after-switch
'+
1049 "*How much to indent a case statement relative to the switch statement.
1050 This is for the rc shell."
1051 :type
`(choice ,@ sh-number-or-symbol-list
)
1052 :group
'sh-indentation
)
1054 (defcustom sh-indent-after-case
'+
1055 "*How much to indent a statement relative to the case statement.
1056 This is for the rc shell."
1057 :type
`(choice ,@ sh-number-or-symbol-list
)
1058 :group
'sh-indentation
)
1060 (defface sh-heredoc-face
1063 (:foreground
"yellow" :bold t
))
1066 (:foreground
"tan" ))
1069 "Face to show a here-document"
1070 :group
'sh-indentation
)
1075 (:foreground
"yellow" :bold t
))
1078 (:foreground
"tan" ))
1081 "Face to show characters with special syntax properties."
1082 :group
'sh-indentation
)
1085 ;; Internal use - not designed to be changed by the user:
1087 ;; These are used for the syntax table stuff (derived from cperl-mode).
1088 ;; Note: parse-sexp-lookup-properties must be set to t for it to work.
1089 (defconst sh-here-doc-syntax
'(15)) ;; generic string
1090 (defconst sh-st-punc
'(1))
1091 (defconst sh-special-syntax sh-st-punc
)
1093 (defun sh-mkword-regexpr (word)
1094 "Make a regexp which matches WORD as a word.
1095 This specifically excludes an occurance of WORD followed by
1096 punctuation characters like '-'."
1097 (concat word
"\\([^-a-z0-9_]\\|$\\)"))
1099 (defun sh-mkword-regexp (word)
1100 "Make a regexp which matches WORD as a word.
1101 This specifically excludes an occurance of WORD followed by
1102 or preceded by punctuation characters like '-'."
1103 (concat "\\(^\\|[^-a-z0-9_]\\)" word
"\\([^-a-z0-9_]\\|$\\)"))
1105 (setq sh-re-done
(sh-mkword-regexpr "done"))
1108 (defconst sh-kws-for-done
1110 (sh .
( "while" "until" "for" ) )
1111 (bash .
( "while" "until" "for" "select" ) )
1112 (ksh88 .
( "while" "until" "for" "select" ) )
1113 (zsh .
( "while" "until" "for" "repeat" "select" ) )
1115 "Which keywords can match the word `done' in this shell."
1119 (defconst sh-indent-supported
1125 "Shell types that shell indenting can do something with."
1128 (defconst sh-electric-rparen-needed
1131 "Non-nil if the shell type needs an electric handling of case alternatives."
1134 (defconst sh-var-list
1136 sh-basic-offset sh-first-lines-indent sh-indent-after-case
1137 sh-indent-after-do sh-indent-after-done
1138 sh-indent-after-else
1140 sh-indent-after-loop-construct
1141 sh-indent-after-open
1143 sh-indent-for-case-alt
1144 sh-indent-for-case-label
1145 sh-indent-for-continuation
1152 "A list of variables used by script mode to control indentation.
1153 This list is used when switching between buffer-local and global
1154 values of variables, and for the commands using indenation styles.")
1156 (defvar sh-make-vars-local t
1157 "*Controls whether indentation variables are local to the buffer.
1158 If non-nil, indentation variables are made local initially.
1159 If nil, you can later make the variables local by invoking
1160 command `sh-make-vars-local'.
1161 The default is t because I assume that in one Emacs session one is
1162 frequently editing existing scripts with different styles.")
1165 ;; mode-command and utility functions
1168 (put 'sh-mode
'mode-class
'special
)
1172 "Major mode for editing shell scripts.
1173 This mode works for many shells, since they all have roughly the same syntax,
1174 as far as commands, arguments, variables, pipes, comments etc. are concerned.
1175 Unless the file's magic number indicates the shell, your usual shell is
1176 assumed. Since filenames rarely give a clue, they are not further analyzed.
1178 This mode adapts to the variations between shells (see `sh-set-shell') by
1179 means of an inheritance based feature lookup (see `sh-feature'). This
1180 mechanism applies to all variables (including skeletons) that pertain to
1181 shell-specific features.
1183 The default style of this mode is that of Rosenblatt's Korn shell book.
1184 The syntax of the statements varies with the shell being used. The
1185 following commands are available, based on the current shell's syntax:
1187 \\[sh-case] case statement
1189 \\[sh-function] function definition
1190 \\[sh-if] if statement
1191 \\[sh-indexed-loop] indexed loop from 1 to n
1192 \\[sh-while-getopts] while getopts loop
1193 \\[sh-repeat] repeat loop
1194 \\[sh-select] select loop
1195 \\[sh-until] until loop
1196 \\[sh-while] while loop
1198 For sh and rc shells indentation commands are:
1199 \\[sh-show-indent] Show the variable controlling this line's indentation.
1200 \\[sh-set-indent] Set then variable controlling this line's indentation.
1201 \\[sh-learn-line-indent] Change the indentation variable so this line
1202 would indent to the way it currently is.
1203 \\[sh-learn-buffer-indent] Set the indentation variables so the
1204 buffer indents as it currently is indendeted.
1207 \\[backward-delete-char-untabify] Delete backward one position, even if it was a tab.
1208 \\[sh-newline-and-indent] Delete unquoted space and indent new line same as this one.
1209 \\[sh-end-of-command] Go to end of successive commands.
1210 \\[sh-beginning-of-command] Go to beginning of successive commands.
1211 \\[sh-set-shell] Set this buffer's shell, and maybe its magic number.
1212 \\[sh-execute-region] Have optional header and region be executed in a subshell.
1214 \\[sh-maybe-here-document] Without prefix, following an unquoted < inserts here document.
1216 Unless quoted with \\, insert the pairs {}, (), [], or '', \"\", ``.
1218 If you generally program a shell different from your login shell you can
1219 set `sh-shell-file' accordingly. If your shell's file name doesn't correctly
1220 indicate what shell it is use `sh-alias-alist' to translate.
1222 If your shell gives error messages with line numbers, you can use \\[executable-interpret]
1223 with your script for an edit-interpret-debug cycle."
1225 (kill-all-local-variables)
1226 (use-local-map sh-mode-map
)
1227 (make-local-variable 'indent-line-function
)
1228 (make-local-variable 'indent-region-function
)
1229 (make-local-variable 'skeleton-end-hook
)
1230 (make-local-variable 'paragraph-start
)
1231 (make-local-variable 'paragraph-separate
)
1232 (make-local-variable 'comment-start
)
1233 (make-local-variable 'comment-start-skip
)
1234 (make-local-variable 'require-final-newline
)
1235 (make-local-variable 'sh-header-marker
)
1236 (make-local-variable 'sh-shell-file
)
1237 (make-local-variable 'sh-shell
)
1238 (make-local-variable 'skeleton-pair-alist
)
1239 (make-local-variable 'skeleton-pair-filter
)
1240 (make-local-variable 'comint-dynamic-complete-functions
)
1241 (make-local-variable 'comint-prompt-regexp
)
1242 (make-local-variable 'font-lock-defaults
)
1243 (make-local-variable 'skeleton-filter
)
1244 (make-local-variable 'skeleton-newline-indent-rigidly
)
1245 (make-local-variable 'sh-shell-variables
)
1246 (make-local-variable 'sh-shell-variables-initialized
)
1247 (make-local-variable 'imenu-generic-expression
)
1248 (make-local-variable 'sh-electric-rparen-needed-here
)
1249 (make-local-variable 'sh-indent-supported-here
)
1250 (make-local-variable 'font-lock-unfontify-region-function
)
1251 (setq major-mode
'sh-mode
1252 mode-name
"Shell-script"
1253 ;; not very clever, but enables wrapping skeletons around regions
1254 indent-region-function
(lambda (b e
)
1257 (skip-syntax-backward "-")
1260 (skip-syntax-backward "-")
1261 (indent-rigidly b
(point) sh-indentation
)))
1262 skeleton-end-hook
(lambda ()
1263 (or (eolp) (newline) (indent-relative)))
1264 paragraph-start
(concat page-delimiter
"\\|$")
1265 paragraph-separate paragraph-start
1267 comint-dynamic-complete-functions sh-dynamic-complete-functions
1268 ;; we can't look if previous line ended with `\'
1269 comint-prompt-regexp
"^[ \t]*"
1271 '((sh-font-lock-keywords
1272 sh-font-lock-keywords-1 sh-font-lock-keywords-2
)
1274 ((?
/ .
"w") (?~ .
"w") (?. .
"w") (?- .
"w") (?_ .
"w")) nil
1275 (font-lock-syntactic-keywords . sh-font-lock-syntactic-keywords
))
1276 font-lock-unfontify-region-function
1277 'sh-font-lock-unfontify-region-function
1278 skeleton-pair-alist
'((?
` _ ?
`))
1279 skeleton-pair-filter
'sh-quoted-p
1280 skeleton-further-elements
'((< '(- (min sh-indentation
1281 (current-column)))))
1282 skeleton-filter
'sh-feature
1283 skeleton-newline-indent-rigidly t
1284 sh-electric-rparen-needed-here nil
1285 sh-indent-supported-here nil
)
1286 (make-local-variable 'parse-sexp-ignore-comments
)
1287 (setq parse-sexp-ignore-comments t
)
1288 ;; Parse or insert magic number for exec, and set all variables depending
1289 ;; on the shell thus determined.
1292 (goto-char (point-min))
1293 (cond ((looking-at "#![ \t]?\\([^ \t\n]*/bin/env[ \t]\\)?\\([^ \t\n]+\\)")
1295 ((and buffer-file-name
1296 (string-match "\\.m?spec$" buffer-file-name
))
1299 (sh-set-shell interpreter nil nil
)
1300 (sh-set-shell sh-shell-file nil nil
))
1301 (run-hooks 'sh-mode-hook
)))
1303 (defalias 'shell-script-mode
'sh-mode
)
1306 (defun sh-font-lock-keywords (&optional keywords
)
1307 "Function to get simple fontification based on `sh-font-lock-keywords'.
1308 This adds rules for comments and assignments."
1309 (sh-feature sh-font-lock-keywords
1310 (when (stringp (sh-feature sh-assignment-regexp
))
1312 `((,(sh-feature sh-assignment-regexp
)
1313 1 font-lock-variable-name-face
)
1317 (defun sh-font-lock-keywords-1 (&optional builtins
)
1318 "Function to get better fontification including keywords."
1319 (let ((keywords (concat "\\([;(){}`|&]\\|^\\)[ \t]*\\(\\(\\("
1320 (mapconcat 'identity
1321 (sh-feature sh-leading-keywords
)
1324 (mapconcat 'identity
1325 (append (sh-feature sh-leading-keywords
)
1326 (sh-feature sh-other-keywords
))
1329 (sh-font-lock-keywords
1331 `((,(concat keywords
"[ \t]+\\)?\\("
1332 (mapconcat 'identity
(sh-feature sh-builtins
) "\\|")
1334 (2 font-lock-keyword-face nil t
)
1335 (6 font-lock-builtin-face
))
1336 ,@(sh-feature sh-font-lock-keywords-2
)))
1337 (,(concat keywords
"\\)\\>")
1338 2 font-lock-keyword-face
)
1339 ,@(sh-feature sh-font-lock-keywords-1
)))))
1341 (defun sh-font-lock-keywords-2 ()
1342 "Function to get better fontification including keywords and builtins."
1343 (sh-font-lock-keywords-1 t
))
1346 (defun sh-set-shell (shell &optional no-query-flag insert-flag
)
1347 "Set this buffer's shell to SHELL (a string).
1348 Makes this script executable via `executable-set-magic', and sets up the
1349 proper starting #!-line, if INSERT-FLAG is non-nil.
1350 Calls the value of `sh-set-shell-hook' if set."
1351 (interactive (list (completing-read "Name or path of shell: "
1352 interpreter-mode-alist
1353 (lambda (x) (eq (cdr x
) 'sh-mode
)))
1354 (eq executable-query
'function
)
1356 (if (string-match "\\.exe\\'" shell
)
1357 (setq shell
(substring shell
0 (match-beginning 0))))
1358 (setq sh-shell
(intern (file-name-nondirectory shell
))
1359 sh-shell
(or (cdr (assq sh-shell sh-alias-alist
))
1363 (executable-set-magic shell
(sh-feature sh-shell-arg
)
1364 no-query-flag insert-flag
)))
1365 (setq require-final-newline
(sh-feature sh-require-final-newline
)
1366 ;;; local-abbrev-table (sh-feature sh-abbrevs)
1367 ;; Packages should not need to set these variables directly. sm.
1368 ; font-lock-keywords nil ; force resetting
1369 ; font-lock-syntax-table nil
1370 comment-start-skip
"#+[\t ]*"
1371 mode-line-process
(format "[%s]" sh-shell
)
1372 sh-shell-variables nil
1373 sh-shell-variables-initialized nil
1374 imenu-generic-expression
(sh-feature sh-imenu-generic-expression
)
1375 imenu-case-fold-search nil
)
1376 (set-syntax-table (or (sh-feature sh-mode-syntax-table
)
1377 (standard-syntax-table)))
1378 (let ((vars (sh-feature sh-variables
)))
1380 (sh-remember-variable (car vars
))
1381 (setq vars
(cdr vars
))))
1382 ;; Packages should not need to toggle Font Lock mode. sm.
1383 ; (and (boundp 'font-lock-mode)
1385 ; (font-lock-mode (font-lock-mode 0)))
1386 (if (setq sh-indent-supported-here
(sh-feature sh-indent-supported
))
1388 (message "Setting up indent for shell type %s" sh-shell
)
1389 (make-local-variable 'sh-kw-alist
)
1390 (make-local-variable 'sh-regexp-for-done
)
1391 (make-local-variable 'parse-sexp-lookup-properties
)
1392 (setq sh-electric-rparen-needed-here
1393 (sh-feature sh-electric-rparen-needed
))
1394 (setq parse-sexp-lookup-properties t
)
1396 (setq sh-kw-alist
(sh-feature sh-kw
))
1397 (let ((regexp (sh-feature sh-kws-for-done
)))
1399 (setq sh-regexp-for-done
1400 (sh-mkword-regexpr (regexp-opt regexp t
)))))
1401 (message "setting up indent stuff")
1402 ;; sh-mode has already made indent-line-function local
1403 ;; but do it in case this is called before that.
1404 (make-local-variable 'indent-line-function
)
1405 (setq indent-line-function
'sh-indent-line
)
1406 ;; This is very inefficient, but this at least makes indent-region work:
1407 (make-local-variable 'indent-region-function
)
1408 (setq indent-region-function nil
)
1409 (if sh-make-vars-local
1410 (sh-make-vars-local))
1411 (message "Indentation setup for shell type %s" sh-shell
))
1412 (message "No indentation for this shell type.")
1413 (setq indent-line-function
'sh-basic-indent-line
))
1414 (run-hooks 'sh-set-shell-hook
))
1418 (defun sh-feature (list &optional function
)
1419 "Index ALIST by the current shell.
1420 If ALIST isn't a list where every element is a cons, it is returned as is.
1421 Else indexing follows an inheritance logic which works in two ways:
1423 - Fall back on successive ancestors (see `sh-ancestor-alist') as long as
1424 the alist contains no value for the current shell.
1425 The ultimate default is always `sh'.
1427 - If the value thus looked up is a list starting with `eval' its `cdr' is
1428 first evaluated. If that is also a list and the first argument is a
1429 symbol in ALIST it is not evaluated, but rather recursively looked up in
1430 ALIST to allow the function called to define the value for one shell to be
1431 derived from another shell. While calling the function, is the car of the
1432 alist element is the current shell.
1433 The value thus determined is physically replaced into the alist.
1435 Optional FUNCTION is applied to the determined value and the result is cached
1437 (or (if (consp list
)
1439 (while (and l
(consp (car l
)))
1443 (cdr (assoc (setq function
(cons sh-shell function
)) list
)))
1444 (let ((sh-shell sh-shell
)
1446 (while (and sh-shell
1447 (not (setq elt
(assq sh-shell list
))))
1448 (setq sh-shell
(cdr (assq sh-shell sh-ancestor-alist
))))
1449 ;; If the shell is not known, treat it as sh.
1451 (setq elt
(assq 'sh list
)))
1452 (if (and (consp (setq val
(cdr elt
)))
1453 (eq (car val
) 'eval
))
1456 (eval (if (consp (setq val
(cdr val
)))
1457 (let ((sh-shell (car (cdr val
)))
1459 (if (assq sh-shell list
)
1462 (sh-feature list
))))
1467 (list (cons function
1468 (setq sh-shell
(car function
)
1469 val
(funcall (cdr function
) val
))))))
1474 ;;; I commented this out because nobody calls it -- rms.
1475 ;;;(defun sh-abbrevs (ancestor &rest list)
1476 ;;; "Iff it isn't, define the current shell as abbrev table and fill that.
1477 ;;;Abbrev table will inherit all abbrevs from ANCESTOR, which is either an abbrev
1478 ;;;table or a list of (NAME1 EXPANSION1 ...). In addition it will define abbrevs
1479 ;;;according to the remaining arguments NAMEi EXPANSIONi ...
1480 ;;;EXPANSION may be either a string or a skeleton command."
1481 ;;; (or (if (boundp sh-shell)
1482 ;;; (symbol-value sh-shell))
1484 ;;; (if (listp ancestor)
1485 ;;; (nconc list ancestor))
1486 ;;; (define-abbrev-table sh-shell ())
1487 ;;; (if (vectorp ancestor)
1488 ;;; (mapatoms (lambda (atom)
1490 ;;; (define-abbrev (symbol-value sh-shell)
1491 ;;; (symbol-name atom)
1492 ;;; (symbol-value atom)
1493 ;;; (symbol-function atom))))
1496 ;;; (define-abbrev (symbol-value sh-shell)
1498 ;;; (if (stringp (car (cdr list)))
1499 ;;; (car (cdr list))
1501 ;;; (if (symbolp (car (cdr list)))
1502 ;;; (car (cdr list))))
1503 ;;; (setq list (cdr (cdr list)))))
1504 ;;; (symbol-value sh-shell)))
1507 (defun sh-mode-syntax-table (table &rest list
)
1508 "Copy TABLE and set syntax for successive CHARs according to strings S."
1509 (setq table
(copy-syntax-table table
))
1511 (modify-syntax-entry (car list
) (car (cdr list
)) table
)
1512 (setq list
(cdr (cdr list
))))
1516 (defun sh-append (ancestor &rest list
)
1517 "Return list composed of first argument (a list) physically appended to rest."
1518 (nconc list ancestor
))
1521 (defun sh-modify (skeleton &rest list
)
1522 "Modify a copy of SKELETON by replacing I1 with REPL1, I2 with REPL2 ..."
1523 (setq skeleton
(copy-sequence skeleton
))
1525 (setcar (or (nthcdr (car list
) skeleton
)
1526 (error "Index %d out of bounds" (car list
)))
1528 (setq list
(nthcdr 2 list
)))
1532 (defun sh-basic-indent-line ()
1533 "Indent a line for Sh mode (shell script mode).
1534 Indent as far as preceding non-empty line, then by steps of `sh-indentation'.
1535 Lines containing only comments are considered empty."
1537 (let ((previous (save-excursion
1538 (while (and (progn (beginning-of-line)
1542 (back-to-indentation)
1544 (eq (following-char) ?
#)))))
1548 (indent-to (if (eq this-command
'newline-and-indent
)
1550 (if (< (current-column)
1551 (setq current
(progn (back-to-indentation)
1553 (if (eolp) previous
0)
1554 (delete-region (point)
1555 (progn (beginning-of-line) (point)))
1557 (max previous
(* (1+ (/ current sh-indentation
))
1559 (* (1+ (/ current sh-indentation
)) sh-indentation
))))))
1560 (if (< (current-column) (current-indentation))
1561 (skip-chars-forward " \t"))))
1564 (defun sh-execute-region (start end
&optional flag
)
1565 "Pass optional header and region to a subshell for noninteractive execution.
1566 The working directory is that of the buffer, and only environment variables
1567 are already set which is why you can mark a header within the script.
1569 With a positive prefix ARG, instead of sending region, define header from
1570 beginning of buffer to point. With a negative prefix ARG, instead of sending
1571 region, clear header."
1572 (interactive "r\nP")
1574 (setq sh-header-marker
(if (> (prefix-numeric-value flag
) 0)
1576 (if sh-header-marker
1578 (let (buffer-undo-list)
1579 (goto-char sh-header-marker
)
1580 (append-to-buffer (current-buffer) start end
)
1581 (shell-command-on-region (point-min)
1582 (setq end
(+ sh-header-marker
1585 (delete-region sh-header-marker end
)))
1586 (shell-command-on-region start end
(concat sh-shell-file
" -")))))
1589 (defun sh-remember-variable (var)
1590 "Make VARIABLE available for future completing reads in this buffer."
1591 (or (< (length var
) sh-remember-variable-min
)
1593 (assoc var sh-shell-variables
)
1594 (setq sh-shell-variables
(cons (cons var var
) sh-shell-variables
)))
1599 (defun sh-quoted-p ()
1600 "Is point preceded by an odd number of backslashes?"
1601 (eq -
1 (%
(save-excursion (skip-chars-backward "\\\\")) 2)))
1603 ;; Indentation stuff.
1604 (defun sh-must-be-shell-mode ()
1605 "Signal an error if not in Shell-script mode."
1606 (unless (eq major-mode
'sh-mode
)
1607 (error "This buffer is not in Shell-script mode")))
1609 (defun sh-must-support-indent ()
1610 "*Signal an error if the shell type for this buffer is not supported.
1611 Also, the buffer must be in Shell-script mode."
1612 (sh-must-be-shell-mode)
1613 (unless sh-indent-supported-here
1614 (error "This buffer's shell type is not supported for this command")))
1616 (defun sh-make-vars-local ()
1617 "Make the indentation variables local to this buffer.
1618 Normally they already are local. This command is provided in case
1619 variable `sh-make-vars-local' has been set to nil.
1621 To revert all these variables to the global values, use
1622 command `sh-reset-indent-vars-to-global-values'."
1624 (sh-must-be-shell-mode)
1625 (mapcar 'make-local-variable sh-var-list
)
1626 (message "Indentation variable are now local."))
1628 (defun sh-reset-indent-vars-to-global-values ()
1629 "Reset local indenatation variables to the global values.
1630 Then, if variable `sh-make-vars-local' is non-nil, make them local."
1632 (sh-must-be-shell-mode)
1633 (mapcar 'kill-local-variable sh-var-list
)
1634 (if sh-make-vars-local
1635 (mapcar 'make-local-variable sh-var-list
)))
1638 (defvar sh-kw-alist nil
1639 "A buffer-local, since it is shell-type dependent, list of keywords.")
1641 (defvar sh-regexp-for-done nil
1642 "A buffer-local regexp to match opening keyword for done.")
1644 ;; Theoretically these are only needed in shell and derived modes.
1645 ;; However, the routines which use them are only called in those modes.
1646 (defconst sh-special-keywords
"then\\|do")
1648 ;; ( key-word first-on-this on-prev-line )
1649 ;; This is used to set `sh-kw-alist' which is a list of sublists each
1650 ;; having 3 elements:
1652 ;; a rule to check when the keyword apepars on "this" line
1653 ;; a rule to check when the keyword apepars on "the previous" line
1654 ;; The keyword is usually a string and is the first word on a line.
1655 ;; If this keyword appears on the line whose indenation is to be
1656 ;; calculated, the rule in element 2 is called. If this returns
1657 ;; non-zero, the resulting point (which may be changed by the rule)
1658 ;; is used as the default indentation.
1659 ;; If it returned false or the keyword was not found in the table,
1660 ;; then the keyword from the previous line is looked up and the rule
1661 ;; in element 3 is called. In this case, however,
1662 ;; `sh-get-indent-info' does not stop but may keepp going and test
1663 ;; other keywords against rules in element 3. This is because the
1664 ;; precending line could have, for example, an opening "if" and an
1665 ;; opening "while" keyword and we need to add the indentation offsets
1676 sh-handle-prev-else
)
1679 sh-handle-prev-else
)
1685 sh-handle-prev-then
)
1688 sh-handle-prev-open
)
1691 sh-handle-prev-open
)
1694 sh-handle-prev-open
)
1696 sh-handle-this-close
1699 sh-handle-this-close
1702 sh-handle-this-close
1706 sh-handle-prev-case
)
1709 sh-handle-prev-esac
)
1712 sh-handle-after-case-label
)
1715 sh-handle-prev-case-alt-end
;; ??
1719 sh-handle-prev-done
)
1725 ;; Note: we don't need specific stuff for bash and zsh shells;
1726 ;; the regexp `sh-regexp-for-done' handles the extra keywords
1727 ;; these shells use.
1731 sh-handle-prev-open
)
1733 sh-handle-this-close
1736 sh-handle-this-rc-case
1737 sh-handle-prev-rc-case
)
1742 (defun sh-help-string-for-variable (var)
1743 "Construct a string for `sh-read-variable' when changing variable VAR ."
1744 (let ((msg (documentation-property var
'variable-documentation
))
1747 (eq var
'sh-first-lines-indent
)
1748 (eq var
'sh-indent-comment
))
1751 You can enter a number (positive to increase indentenation,
1752 negative to decrease indentation, zero for no change to indentnation).
1754 Or, you can enter one of the following symbols which are relative to
1755 the value of variable `sh-basic-offset'
1756 which in this buffer is currently %s.
1760 (mapconcat '(lambda (x)
1761 (nth (1- (length x
)) x
) )
1762 sh-symbol-list
"\n\t")
1766 ;; The following shows the global not the local value!
1767 ;; (format "Current value of %s is %s\n\n" var (symbol-value var))
1770 (defun sh-read-variable (var)
1771 "Read a new value for indentation variable VAR."
1772 (interactive "*variable? ") ;; to test
1773 (let ((minibuffer-help-form `(sh-help-string-for-variable
1776 (setq val
(read-from-minibuffer
1777 (format "New value for %s (press %s for help): "
1778 var
(single-key-description help-char
))
1779 (format "%s" (symbol-value var
))
1785 (defun sh-in-comment-or-string (start)
1786 "Return non-nil if START is in a comment or string."
1790 (setq state
(parse-partial-sexp (point) start nil nil nil t
))
1791 (or (nth 3 state
)(nth 4 state
)))))
1793 (defun sh-goto-matching-if ()
1794 "Go to the matching if for a fi.
1795 This handles nested if..fi pairs."
1796 (let ((found (sh-find-prev-matching "\\bif\\b" "\\bfi\\b" 1)))
1798 (goto-char found
))))
1801 ;; Functions named sh-handle-this-XXX are called when the keyword on the
1802 ;; line whose indentation is being handled contain XXX;
1803 ;; those named sh-handle-prev-XXX are when XXX appears on the prevoius line.
1805 (defun sh-handle-prev-if ()
1806 (list '(+ sh-indent-after-if
)))
1808 (defun sh-handle-this-else ()
1809 (if (sh-goto-matching-if)
1810 ;; (list "aligned to if")
1811 (list "aligned to if" '(+ sh-indent-for-else
))
1815 (defun sh-handle-prev-else ()
1816 (if (sh-goto-matching-if)
1817 (list '(+ sh-indent-after-if
))
1820 (defun sh-handle-this-fi ()
1821 (if (sh-goto-matching-if)
1822 (list "aligned to if" '(+ sh-indent-for-fi
))
1826 (defun sh-handle-prev-fi ()
1827 ;; Why do we have this rule? Because we must go back to the if
1828 ;; to get its indent. We may continue back from there.
1829 ;; We return nil because we don't have anything to add to result,
1830 ;; the side affect of setting align-point is all that matters.
1831 ;; we could return a comment (a string) but I can't think of a good one...
1832 (sh-goto-matching-if)
1835 (defun sh-handle-this-then ()
1836 (let ((p (sh-goto-matching-if)))
1838 (list '(+ sh-indent-for-then
))
1841 (defun sh-handle-prev-then ()
1842 (let ((p (sh-goto-matching-if)))
1844 (list '(+ sh-indent-after-if
))
1847 (defun sh-handle-prev-open ()
1849 (let ((x (sh-prev-stmt)))
1854 (looking-at "function\\b")
1855 (looking-at "\\s-*\\S-+\\s-*()")
1857 (list '(+ sh-indent-after-function
))
1858 (list '(+ sh-indent-after-open
)))
1861 (defun sh-handle-this-close ()
1862 (forward-char 1) ;; move over ")"
1863 (let ((p (sh-safe-backward-sexp)))
1865 (list "aligned to opening paren")
1869 (defun sh-goto-matching-case ()
1870 (let ((found (sh-find-prev-matching "\\bcase\\b" "\\besac\\b" 1)))
1872 (goto-char found
))))
1874 (defun sh-handle-prev-case ()
1875 ;; This is typically called when point is on same line as a case
1876 ;; we shouldn't -- and can't find prev-case
1877 (if (looking-at ".*\\bcase\\b")
1878 (list '(+ sh-indent-for-case-label
))
1879 (error "We don't see to be on a line with a case") ;; debug
1882 (defun sh-handle-this-esac ()
1883 (let ((p (sh-goto-matching-case)))
1885 (list "aligned to matching case")
1890 (defun sh-handle-prev-esac ()
1891 (let ((p (sh-goto-matching-case)))
1893 (list "matching case")
1897 (defun sh-handle-after-case-label ()
1898 (let ((p (sh-goto-matching-case)))
1900 (list '( + sh-indent-for-case-alt
))
1904 (defun sh-handle-prev-case-alt-end ()
1905 (let ((p (sh-goto-matching-case)))
1907 (list '( + sh-indent-for-case-label
))
1911 (defun sh-safe-backward-sexp ()
1912 "Try and do a `backward-sexp', but do not error.
1913 Return new point if successful, nil if an error occurred."
1917 (point) ;; return point if successful
1920 (sh-debug "oops!(0) %d" (point))
1921 nil
;; return nil if fail
1924 (defun sh-safe-forward-sexp ()
1925 "Try and do a `forward-sexp', but do not error.
1926 Return new point if successful, nil if an error occurred."
1930 (point) ;; return point if successful
1933 (sh-debug "oops!(1) %d" (point))
1934 nil
;; return nil if fail
1937 (defun sh-goto-match-for-done ()
1938 (let ((found (sh-find-prev-matching sh-regexp-for-done sh-re-done
1)))
1940 (goto-char found
))))
1942 (defun sh-handle-this-done ()
1943 (if (sh-goto-match-for-done)
1944 (list "aligned to do stmt" '(+ sh-indent-for-done
))
1948 (defun sh-handle-prev-done ()
1949 (if (sh-goto-match-for-done)
1950 (list "previous done")
1954 (defun sh-handle-this-do ()
1955 (let ( (p (sh-goto-match-for-done)) )
1957 (list '(+ sh-indent-for-do
))
1961 (defun sh-handle-prev-do ()
1970 (sh-goto-match-for-done))
1971 (sh-debug "match for done found on THIS line")
1972 (list '(+ sh-indent-after-loop-construct
)))
1973 ((sh-goto-match-for-done)
1974 (sh-debug "match for done found on PREV line")
1975 (list '(+ sh-indent-after-do
)))
1977 (message "match for done NOT found")
1981 (defun sh-find-prev-switch ()
1982 "Find the line for the switch keyword matching this line's case keyword."
1983 (re-search-backward "\\bswitch\\b" nil t
))
1985 (defun sh-handle-this-rc-case ()
1986 (if (sh-find-prev-switch)
1987 (list '(+ sh-indent-after-switch
))
1988 ;; (list '(+ sh-indent-for-case-label))
1991 (defun sh-handle-prev-rc-case ()
1992 (list '(+ sh-indent-after-case
)))
1994 (defun sh-check-rule (n thing
)
1995 (let ((rule (nth n
(assoc thing sh-kw-alist
)))
1999 (setq val
(funcall rule
))
2000 (sh-debug "rule (%d) for %s at %d is %s\n-> returned %s"
2001 n thing
(point) rule val
)))
2005 (defun sh-get-indent-info ()
2006 "Return indent-info for this line.
2007 This is a list. nil means the line is to be left as is.
2008 Otherwise it contains one or more of the following sublists:
2009 \(t NUMBER\) NUMBER is the base location in the buffer that indendation is
2010 relative to. If present, this is always the first of the
2011 sublists. The indentation of the line in question is
2012 derived from the indentation of this point, possibly
2013 modified by subsequent sublists.
2015 \(- VAR\) Get the value of variable VAR and add to or subtract from
2016 the indentation calculated so far.
2017 \(= VAR\) Get the value of variable VAR and *replace* the
2018 indentation with itss value. This only occurs for
2019 special variables such as `sh-indent-comment'.
2020 STRING This is ignored for the purposes of calculating
2021 indentation, it is printed in certain cases to help show
2022 what the indentation is based on."
2023 ;; See comments before `sh-kw'.
2032 (depth-prev-bol nil
)
2042 ;; Note: setting result to t means we are done and will return nil.
2043 ;;( This function never returns just t.)
2045 ((equal (get-text-property (point) 'syntax-table
) sh-here-doc-syntax
)
2047 (setq have-result t
))
2048 ((looking-at "\\s-*#") ; was (equal this-kw "#")
2050 (setq result t
);; return nil if 1st line!
2051 (setq result
(list '(= sh-indent-comment
)))
2052 ;; we still need to get previous line in case
2053 ;; sh-indent-comnent is t (indent as normal)
2054 (setq align-point
(sh-prev-line nil
))
2055 (setq have-result nil
)
2060 ;; Continuation lines are handled specially
2061 (if (sh-this-is-a-continuation)
2063 ;; We assume the line being continued is already
2064 ;; properly indented...
2065 ;; (setq prev-line-end (sh-prev-line))
2066 (setq align-point
(sh-prev-line nil
))
2067 (setq result
(list '(+ sh-indent-for-continuation
)))
2068 (setq have-result t
))
2070 (skip-chars-forward " \t")
2071 (setq this-kw
(sh-get-kw)))
2073 ;; Handle "this" keyword: first word on the line we're
2074 ;; calculating indentation info for.
2076 (if (setq val
(sh-check-rule 1 this-kw
))
2078 (setq align-point
(point))
2080 "this - setting align-point to %d" align-point
)
2081 (setq result
(append result val
))
2082 (setq have-result t
)
2083 ;; set prev-line to continue processing remainder
2084 ;; of this line as a previous l ine
2085 (setq prev-line-end
(point))
2089 (setq prev-line-end
(sh-prev-line 'end
)))
2093 ;; We start off at beginning of this line.
2094 ;; Scan previous statements while this is <=
2095 ;; start of previous line.
2096 (setq start
(point));; for debug only
2097 (goto-char prev-line-end
)
2099 (while (and x
(setq x
(sh-prev-thing)))
2100 (sh-debug "at %d x is: %s result is: %s" (point) x result
)
2103 (equal (get-text-property (1- (point)) 'syntax-table
)
2105 (sh-debug "Case label) here")
2106 (setq x
'case-label
)
2107 (if (setq val
(sh-check-rule 2 x
))
2109 (setq result
(append result val
))
2110 (setq align-point
(point))))
2112 (skip-chars-forward "[a-z0-9]*?")
2114 ((string-match "[])}]" x
)
2115 (setq x
(sh-safe-backward-sexp))
2118 (setq align-point
(point))
2119 (setq result
(append result
2120 (list "aligned to opening paren")))
2122 ((string-match "[[({]" x
)
2123 (sh-debug "Checking special thing: %s" x
)
2124 (if (setq val
(sh-check-rule 2 x
))
2125 (setq result
(append result val
)))
2127 (setq align-point
(point)))
2128 ((string-match "[\"'`]" x
)
2129 (sh-debug "Skipping back for %s" x
)
2131 (setq x
(sh-safe-backward-sexp)))
2133 (sh-debug "Checking string %s at %s" x
(point))
2134 (if (setq val
(sh-check-rule 2 x
))
2135 ;; (or (eq t (car val))
2136 ;; (eq t (car (car val))))
2137 (setq result
(append result val
)))
2138 ;; not sure about this test Wed Jan 27 23:48:35 1999
2139 (setq align-point
(point))
2143 (error "Don't know what to do with %s" x
))
2146 (sh-debug "result is %s" result
)
2148 (sh-debug "No prev line!")
2149 (sh-debug "result: %s align-point: %s" result align-point
)
2153 ;; was: (setq result (append result (list (list t align-point))))
2154 (setq result
(append (list (list t align-point
)) result
))
2156 (sh-debug "result is now: %s" result
)
2160 (setq result
(list (list t prev-line-end
)))
2161 (setq result
(list (list '= 'sh-first-lines-indent
)))
2166 (sh-debug "result is: %s" result
)
2172 (defun sh-get-indent-var-for-line (&optional info
)
2173 "Return the variable controlling indentation for this line.
2174 If there is not [just] one such variable, return a string
2175 indicating the problem.
2176 If INFO is supplied it is used, else it is calculated."
2182 (setq info
(sh-get-indent-info)))
2184 (setq result
"this line to be left as is")
2185 (while (and info
(null result
))
2186 (setq elt
(car info
))
2192 (error "sh-get-indent-var-for-line invalid elt: %s" elt
))
2196 ((symbolp (setq sym
(nth 1 elt
)))
2197 ;; A bit of a kludge - when we see the sh-indent-comment
2198 ;; ignore other variables. Otherwise it is tricky to
2199 ;; "learn" the comment indentation.
2200 (if (eq var
'sh-indent-comment
)
2204 "this line is controlled by more than 1 variable.")
2207 (error "sh-get-indent-var-for-line invalid list elt: %s" elt
)))
2208 (setq info
(cdr info
))
2213 (setq result reason
))
2215 ;; e.g. just had (t POS)
2216 (setq result
"line has default indentation"))
2221 ;; Finding the previous line isn't trivial.
2222 ;; We must *always* go back one more and see if that is a continuation
2223 ;; line -- it is the PREVIOUS line which is continued, not the one
2225 ;; Also, we want to treat a whole "here document" as one big line,
2226 ;; because we may want to a align to the beginning of it.
2229 ;; - go back a line, if empty repeat
2230 ;; - (we are now at a previous non empty line)
2232 ;; - if this is in a here-document, go to the beginning of it
2234 ;; - go back one more physcial line and see if it is a continuation line
2235 ;; - if yes, save it and repeat
2236 ;; - if no, go back to where we last saved.
2237 (defun sh-prev-line (&optional end
)
2238 "Back to end of previous non-comment non-empty line.
2239 Go to beginning of logical line unless END is non-nil, in which case
2240 we go to the end of the previous line and do not check for continuations."
2241 (sh-must-be-shell-mode)
2243 (last-contin-line nil
)
2250 (>= 0 (forward-line -
1))
2256 (setq state
(parse-partial-sexp bol eol nil nil nil t
))
2258 (setq eol
(nth 8 state
)))
2259 (narrow-to-region bol eol
)
2262 ((looking-at "\\s-*$"))
2270 (equal (get-text-property (1- result
) 'syntax-table
)
2271 sh-here-doc-syntax
))
2272 (let ((p1 (previous-single-property-change
2273 (1- result
) 'syntax-table
)))
2280 (setq result
(point)))
2283 ;; we must check previous lines to see if they are continuation lines
2284 ;; if so, we must return position of first of them
2285 (while (and (sh-this-is-a-continuation)
2286 (>= 0 (forward-line -
1)))
2287 (setq result
(point)))
2292 (skip-chars-forward " \t")
2293 (setq result
(point))
2300 (defun sh-prev-stmt ()
2301 "Return the address of the previous stmt or nil."
2302 ;; This is used when we are trying to find a matching keyword.
2303 ;; Searching backward for the keyword would certainly be quicker, but
2304 ;; it is hard to remove "false matches" -- such as if the keyword
2305 ;; appears in a string or quote. This way is slower, but (I think) safer.
2312 (skip-chars-backward " \t;|&({[")
2313 (while (and (not found
)
2316 ;; Do a backward-sexp if possible, else backup bit by bit...
2317 (if (sh-safe-backward-sexp)
2319 (if (looking-at sh-special-keywords
)
2324 ;; backward-sexp failed
2325 (if (zerop (skip-chars-backward " \t()[\]{};`'"))
2328 (let ((back (sh-prev-line nil
)))
2331 (setq going nil
)))))
2333 (skip-chars-backward " \t")
2334 (if (or (and (bolp) (not (sh-this-is-a-continuation)))
2335 (eq (char-before) ?\
;)
2336 (looking-at "\\s-*[|&]"))
2337 (setq found
(point)))))
2342 (skip-chars-forward " \t|&({[")
2343 (setq found
(point))))
2344 (if (>= (point) start
)
2346 (debug "We didn't move!")
2349 (sh-debug "Did not find prev stmt.")))
2354 (defun sh-get-word ()
2355 "Get a shell word skipping whitespace from point."
2357 (skip-chars-forward "\t ")
2358 (let ((start (point)))
2360 (if (looking-at "[\"'`]")
2361 (sh-safe-forward-sexp)
2362 ;; (> (skip-chars-forward "^ \t\n\"'`") 0)
2363 (> (skip-chars-forward "-_a-zA-Z\$0-9") 0)
2365 (buffer-substring start
(point))
2368 (defun sh-prev-thing ()
2369 "Return the previous thing this logical line."
2370 ;; This is called when `sh-get-indent-info' is working backwards on
2371 ;; the previous line(s) finding what keywords may be relevant for
2372 ;; indenting. It moves over sexps if possible, and will stop
2373 ;; on a ; and at the beginning of a line if it is not a continuation
2376 ;; Added a kludge for ";;"
2377 ;; Possible return values:
2379 ;; a string - possibly a keyword
2390 (if (sh-this-is-a-continuation)
2391 (setq min-point
(sh-prev-line nil
))
2394 (setq min-point
(point))))
2396 (skip-chars-backward " \t;")
2397 (unless (looking-at "\\s-*;;")
2398 (skip-chars-backward "^)}];\"'`({[")
2399 (setq c
(char-before))))
2400 (sh-debug "stopping at %d c is %s start=%d min-point=%d"
2401 (point) c start min-point
)
2402 (if (< (point) min-point
)
2403 (error "point %d < min-point %d" (point) min-point
))
2405 ((looking-at "\\s-*;;")
2406 ;; (message "Found ;; !")
2412 ;; skip forward over white space newline and \ at eol
2413 (skip-chars-forward " \t\n\\\\")
2414 (sh-debug "Now at %d start=%d" (point) start
)
2415 (if (>= (point) start
)
2417 (sh-debug "point: %d >= start: %d" (point) start
)
2422 ;; c -- return a string
2428 (defun sh-this-is-a-continuation ()
2429 "Return non-nil if current line is a continuation of previous line."
2433 (if (and (zerop (forward-line -
1))
2434 (looking-at ".*\\\\$"))
2439 (setq state
(parse-partial-sexp bol eol nil nil nil t
))
2440 (unless (nth 4 state
)
2444 (defun sh-get-kw (&optional where and-move
)
2445 "Return first word of line from WHERE.
2446 If AND-MOVE is non-nil then move to end of word."
2447 (let ((start (point)))
2451 (buffer-substring (point)
2452 (progn (skip-chars-forward "^ \t\n;")(point)))
2457 (defun sh-find-prev-matching (open close
&optional depth
)
2458 "Find a matching token for a set of opening and closing keywords.
2459 This takes into account that there may be nested open..close pairings.
2460 OPEN and CLOSE are regexps denoting the tokens to be matched.
2461 Optional parameter DEPTH (usually 1) says how many to look for."
2462 (let ((parse-sexp-ignore-comments t
)
2464 (setq depth
(or depth
1))
2470 (setq prev
(sh-prev-stmt)))
2473 (if (looking-at "\\\\\n")
2476 (skip-chars-forward " \t")))
2479 (setq depth
(1- depth
))
2480 (sh-debug "found open at %d - depth = %d" (point) depth
))
2482 (setq depth
(1+ depth
))
2483 (sh-debug "found close - depth = %d" depth
))
2493 (defun sh-var-value (var &optional ignore-error
)
2494 "Return the value of variable VAR, interpreting symbols.
2495 It can also return t or nil.
2496 If an illegal value is found, throw an error unless Optional argument
2497 IGNORE-ERROR is non-nil."
2498 (let ((val (symbol-value var
)))
2509 (- sh-basic-offset
))
2511 (* 2 sh-basic-offset
))
2513 (* 2 (- sh-basic-offset
)))
2515 (/ sh-basic-offset
2))
2517 (/ (- sh-basic-offset
) 2))
2521 (message "Don't konw how to handle %s's value of %s" var val
)
2523 (error "Don't know how to handle %s's value of %s" var val
))
2526 (defun sh-set-var-value (var value
&optional no-symbol
)
2527 "Set variable VAR to VALUE.
2528 Unless optional argument NO-SYMBOL is non-nil, then if VALUE is
2529 can be represented by a symbol then do so."
2533 ((= value sh-basic-offset
)
2535 ((= value
(- sh-basic-offset
))
2537 ((eq value
(* 2 sh-basic-offset
))
2539 ((eq value
(* 2 (- sh-basic-offset
)))
2541 ((eq value
(/ sh-basic-offset
2))
2543 ((eq value
(/ (- sh-basic-offset
) 2))
2550 (defun sh-calculate-indent (&optional info
)
2551 "Return the indentation for the current line.
2552 If INFO is supplied it is used, else it is calculated from current line."
2558 (setq info
(sh-get-indent-info)))
2562 (sh-debug "info: %s ofs=%s" info ofs
)
2563 (setq elt
(car info
))
2569 (setq a
(car (car info
)))
2570 (setq b
(nth 1 (car info
)))
2575 (setq val
(current-indentation)))
2576 (setq base-value val
))
2578 (setq val
(sh-var-value b
))
2584 ;; set info to nil so we stop immediately
2585 (setq base-value nil ofs nil info nil
))
2587 ;; indent as normal line
2590 ;; The following assume the (t POS) come first!
2591 (setq ofs val base-value
0)
2592 (setq info nil
) ;; ? stop now
2596 (setq ofs
(+ ofs val
)))
2598 (setq ofs
(- ofs val
)))
2600 (error "sh-calculate-indent invalid a a=%s b=%s" a b
))))
2602 (error "sh-calculate-indent invalid elt: a=%s b=%s" a b
)))
2605 (error "sh-calculate-indent invalid elt %s" elt
))
2607 (sh-debug "a=%s b=%s val=%s base-value=%s ofs=%s"
2608 a b val base-value ofs
)
2609 (setq info
(cdr info
))
2612 (sh-debug "at end: base-value: %s ofs: %s" base-value ofs
)
2615 ((or (null base-value
)(null ofs
))
2617 ((and (numberp base-value
)(numberp ofs
))
2618 (sh-debug "base (%d) + ofs (%d) = %d"
2619 base-value ofs
(+ base-value ofs
))
2620 (+ base-value ofs
)) ;; return value
2622 (error "sh-calculate-indent: Help. base-value=%s ofs=%s"
2628 (defun sh-indent-line ()
2629 "Indent the current line."
2631 (sh-must-be-shell-mode)
2632 (let ((indent (sh-calculate-indent)) shift-amt beg end
2633 (pos (- (point-max) (point))))
2638 (skip-chars-forward " \t")
2639 (setq shift-amt
(- indent
(current-column)))
2640 (if (zerop shift-amt
)
2642 (delete-region beg
(point))
2644 ;; If initial point was within line's indentation,
2645 ;; position after the indentation. Else stay at same point in text.
2646 (if (> (- (point-max) pos
) (point))
2647 (goto-char (- (point-max) pos
)))
2651 (defun sh-blink (blinkpos &optional msg
)
2652 "Move cursor momentarily to BLINKPOS and display MSG."
2653 ;; We can get here without it being a number on first line
2654 (if (numberp blinkpos
)
2656 (goto-char blinkpos
)
2658 (sit-for blink-matching-delay
))
2662 (defun sh-show-indent (arg)
2663 "Show the how the currently line would be indented.
2664 This tells you which variable, if any, controls the indentation of
2666 If optional arg ARG is non-null (called interactively with a prefix),
2667 a pop up window describes this variable.
2668 If variable `sh-blink' is non-nil then momentarily go to the line
2669 we are indenting relative to, if applicable."
2671 (sh-must-support-indent)
2672 (let* ((info (sh-get-indent-info))
2673 (var (sh-get-indent-var-for-line info
))
2675 (curr-indent (current-indentation))
2678 (message (setq msg var
))
2679 (setq val
(sh-calculate-indent info
))
2681 (if (eq curr-indent val
)
2682 (setq msg
(format "%s is %s" var
(symbol-value var
)))
2685 (format "%s (%s) would change indent from %d to: %d"
2686 var
(symbol-value var
) curr-indent val
)
2687 (format "%s (%s) would leave line as is"
2688 var
(symbol-value var
)))
2691 (describe-variable var
)))
2693 (let ((info (sh-get-indent-info)))
2694 (if (and info
(listp (car info
))
2695 (eq (car (car info
)) t
))
2696 (sh-blink (nth 1 (car info
)) msg
)
2701 (defun sh-set-indent ()
2702 "Set the indentation for the current line.
2703 If the current line is controlled by an indentation variable, prompt
2704 for a new value for it."
2706 (sh-must-support-indent)
2707 (let* ((info (sh-get-indent-info))
2708 (var (sh-get-indent-var-for-line info
))
2709 val val0 new-val old-val indent-val
)
2711 (message (format "Cannot set indent - %s" var
))
2712 (setq old-val
(symbol-value var
))
2713 (setq val
(sh-read-variable var
))
2717 (setq indent-val
(sh-calculate-indent info
))
2719 (message "Variable: %s Value: %s would indent to: %d"
2720 var
(symbol-value var
) indent-val
)
2721 (message "Variable: %s Value: %s would leave line as is."
2722 var
(symbol-value var
)))
2723 ;; I'm not sure about this, indenting it now?
2724 ;; No. Because it would give the impression that an undo would
2725 ;; restore thing, but the value has been altered.
2730 (message "Bad value for %s, restoring to previous value %s"
2737 (defun sh-learn-line-indent (arg)
2738 "Learn how to indent a line as it currently is indented.
2740 If there is an indentation variable which controls this line's indentation,
2741 then set it to a value which would indent the line the way it
2744 If the value can be represented by one of the symbols then do so
2745 unless optional argument ARG (the prefix when interactive) is non-nil."
2747 (sh-must-support-indent)
2748 ;; I'm not sure if we show allow learning on an empty line.
2749 ;; Though it might occasionally be useful I think it usually
2750 ;; would just be confusing.
2753 (looking-at "\\s-*$"))
2754 (message "sh-learn-line-indent ignores empty lines.")
2755 (let* ((info (sh-get-indent-info))
2756 (var (sh-get-indent-var-for-line info
))
2757 ival sval diff new-val
2759 (curr-indent (current-indentation)))
2762 (message (format "Cannot learn line - %s" var
)))
2763 ((eq var
'sh-indent-comment
)
2764 ;; This is arbitrary...
2765 ;; - if curr-indent is 0, set to curr-indent
2766 ;; - else if it has the indentation of a "normal" line,
2768 ;; - else set to curr-indent.
2769 (setq sh-indent-comment
2770 (if (= curr-indent
0)
2772 (let* ((sh-indent-comment t
)
2773 (val2 (sh-calculate-indent info
)))
2774 (if (= val2 curr-indent
)
2777 (message "%s set to %s" var
(symbol-value var
))
2779 ((numberp (setq sval
(sh-var-value var
)))
2780 (setq ival
(sh-calculate-indent info
))
2781 (setq diff
(- curr-indent ival
))
2783 (sh-debug "curr-indent: %d ival: %d diff: %d var:%s sval %s"
2784 curr-indent ival diff var sval
)
2785 (setq new-val
(+ sval diff
))
2786 ;;; I commented out this because someone might want to replace
2787 ;;; a value of `+' with the current value of sh-basic-offset
2790 ;;; (message "No change needed!")
2791 (sh-set-var-value var new-val no-symbol
)
2792 (message "%s set to %s" var
(symbol-value var
))
2796 (message "Cannot change %s" var
))
2801 (defun sh-mark-init (buffer)
2802 "Initialize a BUFFER to be used by `sh-mark-line'."
2803 (let ((main-buffer (current-buffer)))
2805 (set-buffer (get-buffer-create buffer
))
2808 (setq occur-buffer main-buffer
)
2812 (defun sh-mark-line (message point buffer
&optional add-linenum occur-point
)
2813 "Insert MESSAGE referring to location POINT in current buffer into BUFFER.
2814 Buffer BUFFER is in `occur-mode'.
2815 If ADD-LINENUM is non-nil the message is preceded by the line number.
2816 If OCCUR-POINT is non-nil then the line is marked as a new occurence
2817 so that `occur-next' and `occur-prev' will work."
2818 (let ((m1 (make-marker))
2819 (main-buffer (current-buffer))
2824 (set-marker m1 point
(current-buffer))
2826 (setq line
(format "%d: " (1+ (count-lines 1 point
)))))))
2828 (if (get-buffer buffer
)
2829 (set-buffer (get-buffer buffer
))
2830 (set-buffer (get-buffer-create buffer
))
2832 (setq occur-buffer main-buffer
)
2834 (goto-char (point-max))
2835 (setq start
(point))
2838 (setq occur-point
(point)))
2841 (put-text-property start
(point) 'mouse-face
'highlight
))
2845 (put-text-property start
(point) 'occur m1
)
2847 (put-text-property occur-point
(1+ occur-point
)
2854 ;; Is this really worth having?
2855 (defvar sh-learned-buffer-hook nil
2856 "*An abnormal hook, called with an alist of leared variables.")
2857 ;;; Example of how to use sh-learned-buffer-hook
2859 ;; (defun what-i-learned (list)
2862 ;; (set-buffer "*scratch*")
2863 ;; (goto-char (point-max))
2864 ;; (insert "(setq\n")
2866 ;; (insert (format " %s %s \n"
2867 ;; (nth 0 (car p)) (nth 1 (car p))))
2868 ;; (setq p (cdr p)))
2872 ;; (add-hook 'sh-learned-buffer-hook 'what-i-learned)
2875 ;; Originally this was sh-learn-region-indent (beg end)
2876 ;; However, in practise this was awkward so I changed it to
2877 ;; use the whole buffer. Use narrowing if needbe.
2878 (defun sh-learn-buffer-indent (&optional arg
)
2879 "Learn how to indent the buffer the way it currently is.
2881 Output in buffer \"*indent*\" shows any lines which have conflicting
2882 values of a variable, and the final value of all variables learnt.
2883 This buffer is popped to automatically if there are any discrepencies.
2885 If no prefix ARG is given, then variables are set to numbers.
2886 If a prefix arg is given, then variables are set to symbols when
2887 applicable -- e.g. to symbol `+' if the value is that of the
2889 If a positive numerical prefix is given, then `sh-basic-offset'
2890 is set to the prefix's numerical value.
2891 Otherwise, sh-basic-offset may or may not be changed, according
2892 to the value of variable `sh-learn-basic-offset'.
2894 Abnormal hook `sh-learned-buffer-hook' if non-nil is called when the
2895 function completes. The function is abnormal because it is called
2896 with an alist of variables learnt. This feature may be changed or
2897 removed in the future.
2899 This command can often take a long time to run."
2901 (sh-must-support-indent)
2903 (goto-char (point-min))
2904 (let ((learned-var-list nil
)
2905 (out-buffer "*indent*")
2912 (comment-col nil
) ;; number if all same, t if seen diff values
2913 (comments-always-default t
) ;; nil if we see one not default
2915 (specified-basic-offset (and arg
(numberp arg
)
2919 (setq vec
(make-vector max
0))
2920 (sh-mark-init out-buffer
)
2922 (if specified-basic-offset
2924 (setq sh-basic-offset arg
)
2926 (format "Using specified sh-basic-offset of %d"
2929 (format "Initial value of sh-basic-offset: %s"
2932 (while (< (point) (point-max))
2933 (setq linenum
(1+ linenum
))
2934 ;; (if (zerop (% linenum 10))
2935 (message "line %d" linenum
)
2937 (unless (looking-at "\\s-*$") ;; ignore empty lines!
2938 (let* ((sh-indent-comment t
) ;; info must return default indent
2939 (info (sh-get-indent-info))
2940 (var (sh-get-indent-var-for-line info
))
2941 sval ival diff new-val
2942 (curr-indent (current-indentation)))
2948 ((numberp (setq sval
(sh-var-value var
'no-error
)))
2949 ;; the numberp excludes comments since sval will be t.
2950 (setq ival
(sh-calculate-indent))
2951 (setq diff
(- curr-indent ival
))
2952 (setq new-val
(+ sval diff
))
2953 (sh-set-var-value var new-val
'no-symbol
)
2954 (unless (looking-at "\\s-*#");; don't learn from comments
2955 (if (setq previous-set-info
(assoc var learned-var-list
))
2957 ;; it was already there, is it same value ?
2958 (unless (eq (symbol-value var
)
2959 (nth 1 previous-set-info
))
2961 (format "Variable %s was set to %s"
2962 var
(symbol-value var
))
2963 (point) out-buffer t t
)
2965 (format " but was previously set to %s"
2966 (nth 1 previous-set-info
))
2967 (nth 2 previous-set-info
) out-buffer t
)
2968 (setq num-diffs
(1+ num-diffs
))
2969 ;; (delete previous-set-info learned-var-list)
2970 (setcdr previous-set-info
2971 (list (symbol-value var
) (point)))
2974 (setq learned-var-list
2975 (append (list (list var
(symbol-value var
)
2978 (if (numberp new-val
)
2981 "This line's indent value: %d" new-val
)
2983 (setq new-val
(- new-val
)))
2985 (aset vec new-val
(1+ (aref vec new-val
))))))
2987 ((eq var
'sh-indent-comment
)
2988 (unless (= curr-indent
(sh-calculate-indent info
))
2989 ;; this is not the default indentation
2990 (setq comments-always-default nil
)
2991 (if comment-col
;; then we have see one before
2992 (or (eq comment-col curr-indent
)
2993 (setq comment-col t
));; seen a different one
2994 (setq comment-col curr-indent
))
2997 (sh-debug "Cannot learn this line!!!")
3000 "at %s learned-var-list is %s" (point) learned-var-list
)
3007 "comment-col = %s comments-always-default = %s"
3008 comment-col comments-always-default
))
3010 (sh-mark-line msg nil out-buffer
)))
3013 (setq msg
"\nComments are all in 1st column.\n"))
3014 (comments-always-default
3015 (setq msg
"\nComments follow default indentation.\n")
3016 (setq comment-col t
))
3017 ((numberp comment-col
)
3018 (setq msg
(format "\nComments are in col %d." comment-col
)))
3020 (setq msg
"\nComments seem to be mixed, leaving them as is.\n")
3021 (setq comment-col nil
)
3024 (sh-mark-line msg nil out-buffer
)
3026 (sh-mark-line initial-msg nil out-buffer t t
)
3028 (setq suggested
(sh-guess-basic-offset vec
))
3030 (if (and suggested
(not specified-basic-offset
))
3033 ;; t => set it if we have a single value as a number
3034 ((and (eq sh-learn-basic-offset t
) (numberp suggested
))
3036 ;; other non-nil => set it if only one value was found
3037 (sh-learn-basic-offset
3038 (if (numberp suggested
)
3040 (if (= (length suggested
) 1)
3046 (setq learned-var-list
3047 (append (list (list 'sh-basic-offset
3048 (setq sh-basic-offset new-value
)
3051 ;; Not sure if we need to put this line in, since
3052 ;; it will appear in the "Learned variable settings".
3054 (format "Changed sh-basic-offset to: %d" sh-basic-offset
)
3057 (if (listp suggested
)
3058 (format "Possible value(s) for sh-basic-offset: %s"
3059 (mapconcat 'int-to-string suggested
" "))
3060 (format "Suggested sh-basic-offset: %d" suggested
))
3064 (setq learned-var-list
3065 (append (list (list 'sh-indent-comment comment-col
(point-max)))
3067 (setq sh-indent-comment comment-col
)
3068 (let ((name (buffer-name))
3069 (lines (if (and (eq (point-min) 1)
3070 (eq (point-max) (1+ (buffer-size))))
3072 (format "lines %d to %d of "
3073 (1+ (count-lines 1 (point-min)))
3074 (1+ (count-lines 1 (point-max))))))
3076 (sh-mark-line "\nLearned variable settings:" nil out-buffer
)
3078 ;; Set learned variables to symbolic rather than numeric
3079 ;; values where possible.
3081 (let ((p (reverse learned-var-list
))
3084 (setq var
(car (car p
)))
3085 (setq val
(nth 1 (car p
)))
3087 ((eq var
'sh-basic-offset
)
3090 (sh-set-var-value var val
))
3095 (let ((p (reverse learned-var-list
))
3098 (setq var
(car (car p
)))
3099 (sh-mark-line (format " %s %s" var
(symbol-value var
))
3100 (nth 2 (car p
)) out-buffer
)
3103 (set-buffer out-buffer
)
3104 (goto-char (point-min))
3106 (format "Indentation values for buffer %s.\n" name
)
3107 (format "%d indentation variable%s different values%s\n\n"
3111 (if (zerop num-diffs
)
3114 ;; Are abnormal hooks considered bad form?
3115 (run-hook-with-args 'sh-learned-buffer-hook learned-var-list
)
3116 (if (or sh-popup-occur-buffer
(> num-diffs
0))
3117 (pop-to-buffer out-buffer
))
3120 (defun sh-guess-basic-offset (vec)
3121 "See if we can determine a reasonbable value for `sh-basic-offset'.
3122 This is experimental, heuristic and arbitrary!
3123 Argument VEC is a vector of information collected by
3124 `sh-learn-buffer-indent'.
3126 number - there appears to be a good single value
3127 list of numbers - no obvious one, here is a list of one or more
3129 nil - we couldn't find a reasonable one."
3130 (let* ((max (1- (length vec
)))
3132 (totals (make-vector max
0))
3136 (aset totals i
(+ (aref totals i
) (* 4 (aref vec i
))))
3139 (aset totals i
(+ (aref totals i
) (aref vec
(/ i
2)))))
3141 (aset totals i
(+ (aref totals i
) (aref vec
(* i
2)))))
3149 (if (/= (aref totals i
) 0)
3150 (setq x
(append x
(list (cons i
(aref totals i
))))))
3153 (setq x
(sort x
'(lambda (a b
)
3154 (> (cdr a
)(cdr b
)))))
3155 (setq tot
(apply '+ (append totals nil
)))
3156 (sh-debug (format "vec: %s\ntotals: %s\ntot: %d"
3160 (message "no values!")) ;; we return nil
3162 (message "only value is %d" (car (car x
)))
3163 (setq result
(car (car x
)))) ;; return single value
3164 ((> (cdr (car x
)) (/ tot
2))
3166 (message "basic-offset is probably %d" (car (car x
)))
3167 (setq result
(car (car x
)))) ;; again, return a single value
3168 ((>= (cdr (car x
)) (* 2 (cdr (car (cdr x
)))))
3169 ;; 1st is >= 2 * 2nd
3170 (message "basic-offset could be %d" (car (car x
)))
3171 (setq result
(car (car x
))))
3172 ((>= (+ (cdr (car x
))(cdr (car (cdr x
)))) (/ tot
2))
3173 ;; 1st & 2nd together >= 50% - return a list
3174 (setq p x sum
0 result nil
)
3176 (<= (setq sum
(+ sum
(cdr (car p
)))) (/ tot
2)))
3177 (setq result
(append result
(list (car (car p
)))))
3179 (message "Possible choices for sh-basic-offset: %s"
3180 (mapconcat 'int-to-string result
" ")))
3182 (message "No obvious value for sh-basic-offset. Perhaps %d"
3184 ;; result is nil here
3190 (defun sh-do-nothing (a b c
)
3191 ;; checkdoc-params: (a b c)
3192 "A dummy function to prevent font-lock from re-fontifying a change.
3193 Otherwise, we fontify something and font-lock overwrites it."
3196 ;; The default font-lock-unfontify-region-function removes
3197 ;; syntax-table properties, and so removes our information.
3198 (defun sh-font-lock-unfontify-region-function (beg end
)
3199 (let* ((modified (buffer-modified-p)) (buffer-undo-list t
)
3200 (inhibit-read-only t
) (inhibit-point-motion-hooks t
)
3201 before-change-functions after-change-functions
3202 deactivate-mark buffer-file-name buffer-file-truename
)
3203 (remove-text-properties beg end
'(face nil
))
3204 (when (and (not modified
) (buffer-modified-p))
3205 (set-buffer-modified-p nil
))))
3207 (defun sh-set-char-syntax (where new-prop
)
3208 "Set the character's syntax table property at WHERE to be NEW-PROP."
3210 (setq where
(point)))
3211 (let ((font-lock-fontify-region-function 'sh-do-nothing
))
3212 (put-text-property where
(1+ where
) 'syntax-table new-prop
)
3213 (add-text-properties where
(1+ where
)
3214 '(face sh-st-face rear-nonsticky t
))
3218 (defun sh-check-paren-in-case ()
3219 "Make syntax class of case label's right parenthesis not close parenthesis.
3220 If this parenthesis is a case alternative, set its syntax class to a word."
3221 (let ((start (point))
3223 ;; First test if this is a possible candidate, the first "(" or ")"
3224 ;; on the line; then, if go, check prev line is ;; or case.
3227 ;; stop at comment or when depth becomes -1
3228 (setq state
(parse-partial-sexp (point) start -
1 nil nil t
))
3232 (setq prev-line
(sh-prev-line nil
)))
3234 (goto-char prev-line
)
3236 ;; (setq case-stmt-start (point))
3237 ;; (if (looking-at "\\(^\\s-*case[^-a-z0-9_]\\|[^#]*;;\\s-*$\\)")
3238 (if (sh-search-word "\\(case\\|;;\\)" start
)
3239 (sh-set-char-syntax (1- start
) sh-special-syntax
)
3242 (defun sh-electric-rparen ()
3243 "Insert a right parethese, and check if it is a case alternative.
3244 If so, its syntax class is set to word, and its text proerty
3245 is set to have face `sh-st-face'."
3248 (if sh-electric-rparen-needed-here
3249 (sh-check-paren-in-case)))
3251 (defun sh-electric-hash ()
3252 "Insert a hash, but check it is preceded by \"$\".
3253 If so, it is given a syntax type of comment.
3254 Its text proerty has face `sh-st-face'."
3256 (let ((pos (point)))
3258 (if (eq (char-before pos
) ?$
)
3259 (sh-set-char-syntax pos sh-st-punc
))))
3261 (defun sh-electric-less (arg)
3262 "Insert a \"<\" and see if this is the start of a here-document.
3263 If so, the syntax class is set so that it will not be automatically
3265 Argument ARG if non-nil disables this test."
3267 (let ((p1 (point)) p2 p3
)
3268 (sh-maybe-here-document arg
) ;; call the original fn in sh-script.el.
3270 (if (/= (+ p1
(prefix-numeric-value arg
)) p2
)
3275 (sh-set-here-doc-region p2 p3
))
3278 (defun sh-set-here-doc-region (start end
)
3279 "Mark a here-document from START to END so that it will not be reindented."
3281 ;; Make the whole thing have syntax type word...
3282 ;; That way sexp movement doens't worry about any parentheses.
3283 ;; A disadvantage of this is we can't use forward-word within a
3284 ;; here-doc, which is annoying.
3285 (let ((font-lock-fontify-region-function 'sh-do-nothing
))
3286 (put-text-property start end
'syntax-table sh-here-doc-syntax
)
3287 (put-text-property start end
'face
'sh-heredoc-face
)
3288 (put-text-property (1- end
) end
'rear-nonsticky t
)
3289 (put-text-property start
(1+ start
) 'front-sticky t
)
3292 (defun sh-remove-our-text-properties ()
3293 "Remove text properties relating to right parentheses and here documents."
3296 (goto-char (point-min))
3298 (let ((plist (text-properties-at (point)))
3300 (or (next-single-property-change (point) 'syntax-table
3303 ;; Process text from point to NEXT-CHANGE...
3304 (if (get-text-property (point) 'syntax-table
)
3306 (sh-debug "-- removing props from %d to %d --"
3307 (point) next-change
)
3308 (remove-text-properties (point) next-change
3309 '(syntax-table nil
))
3310 (remove-text-properties (point) next-change
'(face nil
))
3312 (goto-char next-change
)))
3315 (defun sh-search-word (word &optional limit
)
3316 "Search forward for regexp WORD occuring as a word not in string nor comment.
3317 If found, returns non nil with the match available in \(match-string 2\).
3318 Yes 2, not 1, since we build a regexp to guard against false matches
3319 such as matching \"a-case\" when we are searching for \"case\".
3320 If not found, it returns nil.
3321 The search maybe limited by optional argument LIMIT."
3322 (interactive "sSearch for: ")
3324 ;; Cannot use \\b here since it matches "-" and "_"
3325 (regexp (sh-mkword-regexp word
))
3327 (setq start
(point))
3328 (while (and (setq start
(point))
3330 (re-search-forward regexp limit t
))
3331 ;; Found str; check it is not in a comment or string.
3334 (parse-partial-sexp start
(point) nil nil nil
'syntax_table
))
3335 (if (setq where
(nth 8 state
))
3336 ;; in comment or string
3338 (setq found
(point))
3339 (if (eq (char-after where
) ?
#)
3342 (unless (sh-safe-forward-sexp)
3343 ;; If the above fails we must either give up or
3344 ;; move forward and try again.
3347 ;; not in comment or string, so accept it
3348 (setq found
(point))
3353 (defun sh-scan-case ()
3354 "Scan a case statement for right parens belonging to case alternatives.
3355 Mark each as having syntax `sh-special-syntax'.
3356 Called from scan-buff. If ok, return non-nil."
3359 (depth 1) ;; we are called at a "case"
3362 ;; We enter here at a case statement
3363 ;; First, find limits of the case.
3364 (while (and (> depth
0)
3365 (sh-search-word "\\(case\\|esac\\)"))
3366 (if (equal (match-string 2) "case")
3367 (setq depth
(1+ depth
))
3368 (setq depth
(1- depth
))))
3369 ;; (message "end of search for esac at %d depth=%d" (point) depth)
3372 ;; if we found the esac, then fix all appropriate ')'s in the region
3375 (while (< (point) end
)
3376 ;; search for targetdepth of -1 meaning extra right paren
3377 (setq state
(parse-partial-sexp (point) end -
1 nil nil nil
))
3378 (if (and (= (car state
) -
1)
3379 (= (char-before) ?\
)))
3381 ;; (message "At %d state is %s" (point) state)
3382 ;; (message "Fixing %d" (point))
3383 (sh-set-char-syntax (1- (point)) sh-special-syntax
)
3384 ;; we could advance to the next ";;" perhaps
3386 ;; (message "? Not found at %d" (point)) ; ok, could be "]"
3389 (message "No matching esac for case at %d" start
)
3396 (defun sh-scan-buffer ()
3397 "Scan a sh buffer for case statements and here-documents.
3399 For each case alternative found, mark its \")\" with a text property
3400 so that its syntax class is no longer a close parenthesis character.
3402 Each here-document is also marked so that it is effectively immune
3403 from indenation changes."
3404 ;; Do not call this interactively, call `sh-rescan-buffer' instead.
3405 (sh-must-be-shell-mode)
3407 (initial-buffer-modified-p (buffer-modified-p))
3408 start end where label ws
)
3410 (goto-char (point-min))
3411 ;; 1. Scan for ")" in case statements.
3412 (while (and ;; (re-search-forward "^[^#]*\\bcase\\b" nil t)
3413 (sh-search-word "\\(case\\|esac\\)")
3414 ;; (progn (message "Found a case at %d" (point)) t)
3416 ;; 2. Scan for here docs
3417 (goto-char (point-min))
3418 ;; while (re-search-forward "<<\\(-?\\)\\(\\s-*\\)\\(.*\\)$" nil t)
3419 (while (re-search-forward "<<\\(-?\\)" nil t
)
3420 (unless (sh-in-comment-or-string (match-beginning 0))
3421 ;; (setq label (match-string 3))
3422 (setq label
(sh-get-word))
3423 (if (string= (match-string 1) "-")
3424 ;; if <<- then we allow whitespace
3426 ;; otherwise we don't
3428 (while (string-match "['\"\\]" label
)
3429 (setq label
(replace-match "" nil nil label
)))
3430 (if (setq n
(string-match "\\s-+$" label
))
3431 (setq label
(substring label
0 n
)))
3433 ;; the line containing the << could be continued...
3434 (while (sh-this-is-a-continuation)
3436 (setq start
(point))
3437 (if (re-search-forward (concat "^" ws
(regexp-quote label
)
3440 (sh-set-here-doc-region start
(point))
3441 (sh-debug "missing here-doc delimiter `%s'" label
))))
3442 ;; 3. Scan for $# -- make the "#" a punctuation not a comment
3443 (goto-char (point-min))
3445 (while (and (not (eobp))
3446 (setq state
(parse-partial-sexp
3447 (1+ (point))(point-max) nil nil nil t
))
3449 (goto-char (nth 8 state
))
3450 (sh-debug "At %d %s" (point) (eq (char-before) ?$
))
3451 (if (eq (char-before) ?$
)
3452 (sh-set-char-syntax (point) sh-st-punc
) ;; not a comment!
3453 (end-of-line) ;; if this *was* a comment, ignore rest of line!
3455 ;; 4. Hide these changes from making a previously unmodified
3456 ;; buffer into a modified buffer.
3458 (if initial-buffer-modified-p
3459 (message "buffer was initially modified")
3461 "buffer not initially modified - so clearing modified flag")))
3462 (set-buffer-modified-p initial-buffer-modified-p
)
3465 (defun sh-rescan-buffer ()
3466 "Rescan the buffer for case alternative parentheses and here documents."
3468 (if (eq major-mode
'sh-mode
)
3469 (let ((inhibit-read-only t
))
3470 (sh-remove-our-text-properties)
3471 (message "Re-scanning buffer...")
3473 (message "Re-scanning buffer...done")
3476 ;; ========================================================================
3478 ;; Styles -- a quick and dirty way of saving the indenation settings.
3480 (defvar sh-styles-alist nil
3481 "A list of all known shell indentation styles.")
3483 (defun sh-name-style (name &optional confirm-overwrite
)
3484 "Name the current indentation settings as a style called NAME.
3485 If this name exists, the command will prompt whether it should be
3487 - - it was called interactively with a prefix argument, or
3488 - - called non-interactively with optional CONFIRM-OVERWRITE non-nil."
3489 ;; (interactive "sName for this style: ")
3492 (read-from-minibuffer "Name for this style? " )
3493 (not current-prefix-arg
)))
3494 (let ((slist (list name
))
3499 (setq slist
(append slist
(list (cons var
(symbol-value var
)))))
3501 (if (setq style
(assoc name sh-styles-alist
))
3502 (if (or (not confirm-overwrite
)
3503 (y-or-n-p "This style exists. Overwrite it? "))
3505 (message "Updating style %s" name
)
3506 (setcdr style
(cdr slist
)))
3507 (message "Not changing style %s" name
))
3508 (message "Creating new style %s" name
)
3509 (setq sh-styles-alist
(append sh-styles-alist
3513 (defun sh-load-style (name)
3514 "Set shell indentation values for this buffer from those in style NAME."
3515 (interactive (list (completing-read
3516 "Which style to use for this buffer? "
3517 sh-styles-alist nil t
)))
3518 (let ((sl (assoc name sh-styles-alist
)))
3520 (error "sh-load-style - style %s not known" name
)
3523 (set (car (car sl
)) (cdr (car sl
)))
3527 (defun sh-save-styles-to-buffer (buff)
3528 "Save all current styles in elisp to buffer BUFF.
3529 This is always added to the end of the buffer."
3531 (read-from-minibuffer "Buffer to save styles in? " "*scratch*")))
3532 ;; This is an attempt to sort of pretty print it...
3534 (set-buffer (get-buffer-create buff
))
3535 (goto-char (point-max))
3537 (let ((p sh-styles-alist
) q
)
3538 (insert "(setq sh-styles-alist '(\n")
3541 (insert " ( " (prin1-to-string (car q
)) "\n")
3544 (insert " "(prin1-to-string (car q
)) "\n")
3555 ;; statement syntax-commands for various shells
3557 ;; You are welcome to add the syntax or even completely new statements as
3558 ;; appropriate for your favorite shell.
3560 (define-skeleton sh-case
3561 "Insert a case/switch statement. See `sh-feature'."
3563 "switch( " str
" )" \n
3564 > "case " (read-string "pattern: ") ?
: \n
3567 ( "other pattern, %s: "
3577 > "switch( " str
" ) {" \n
3578 > "case " (read-string "pattern: ") \n
3580 ( "other pattern, %s: "
3588 > "case " str
" in" \n
3589 > (read-string "pattern: ")
3590 '(sh-electric-rparen)
3594 ( "other pattern, %s: "
3595 > str
'(sh-electric-rparen) \n
3598 > "*" '(sh-electric-rparen) \n
3603 (define-skeleton sh-for
3604 "Insert a for loop. See `sh-feature'."
3605 (csh eval sh-modify sh
3613 (es eval sh-modify rc
3615 (rc eval sh-modify sh
3619 (sh "Index variable: "
3620 > "for " str
" in " _
"; do" \n
3621 > _ | ?$
& (sh-remember-variable str
) \n
3626 (define-skeleton sh-indexed-loop
3627 "Insert an indexed loop from 1 to n. See `sh-feature'."
3628 (bash eval identity posix
)
3629 (csh "Index variable: "
3631 "while( $" str
" <= " (read-string "upper limit: ") " )" \n
3635 (es eval sh-modify rc
3637 (ksh88 "Index variable: "
3638 > "integer " str
"=0" \n
3639 > "while (( ( " str
" += 1 ) <= "
3640 (read-string "upper limit: ")
3642 > _ ?$
(sh-remember-variable str
) > \n
3644 (posix "Index variable: "
3646 "while [ $" str
" -le "
3647 (read-string "upper limit: ")
3650 str ?
= (sh-add (sh-remember-variable str
) 1) \n
3652 (rc "Index variable: "
3653 > "for( " str
" in" " `{awk 'BEGIN { for( i=1; i<="
3654 (read-string "upper limit: ")
3655 "; i++ ) print i }'`}) {" \n
3656 > _ ?$
(sh-remember-variable str
) \n
3658 (sh "Index variable: "
3659 > "for " str
" in `awk 'BEGIN { for( i=1; i<="
3660 (read-string "upper limit: ")
3661 "; i++ ) print i }'`; do" \n
3662 > _ ?$
(sh-remember-variable str
) \n
3666 (defun sh-shell-initialize-variables ()
3667 "Scan the buffer for variable assignments.
3668 Add these variables to `sh-shell-variables'."
3669 (message "Scanning buffer `%s' for variable assignments..." (buffer-name))
3671 (goto-char (point-min))
3672 (setq sh-shell-variables-initialized t
)
3673 (while (search-forward "=" nil t
)
3675 (message "Scanning buffer `%s' for variable assignments...done"
3678 (defvar sh-add-buffer
)
3680 (defun sh-add-completer (string predicate code
)
3681 "Do completion using `sh-shell-variables', but initialize it first.
3682 This function is designed for use as the \"completion table\",
3683 so it takes three arguments:
3684 STRING, the current buffer contents;
3685 PREDICATE, the predicate for filtering possible matches;
3686 CODE, which says what kind of things to do.
3687 CODE can be nil, t or `lambda'.
3688 nil means to return the best completion of STRING, or nil if there is none.
3689 t means to return a list of all possible completions of STRING.
3690 `lambda' means to return t if STRING is a valid completion as it stands."
3691 (let ((sh-shell-variables
3693 (set-buffer sh-add-buffer
)
3694 (or sh-shell-variables-initialized
3695 (sh-shell-initialize-variables))
3696 (nconc (mapcar (lambda (var)
3698 (substring var
0 (string-match "=" var
))))
3700 process-environment
)
3701 sh-shell-variables
))))
3703 (try-completion string sh-shell-variables predicate
))
3705 (all-completions string sh-shell-variables predicate
))
3707 (assoc string sh-shell-variables
)))))
3709 (defun sh-add (var delta
)
3710 "Insert an addition of VAR and prefix DELTA for Bourne (type) shell."
3712 (let ((sh-add-buffer (current-buffer)))
3713 (list (completing-read "Variable: " 'sh-add-completer
)
3714 (prefix-numeric-value current-prefix-arg
))))
3715 (insert (sh-feature '((bash .
"$[ ")
3721 (sh-remember-variable var
)
3722 (if (< delta
0) " - " " + ")
3723 (number-to-string (abs delta
))
3724 (sh-feature '((bash .
" ]")
3733 (define-skeleton sh-function
3734 "Insert a function definition. See `sh-feature'."
3735 (bash eval sh-modify ksh88
3738 "function " str
" {" \n
3741 (rc eval sh-modify ksh88
3750 (define-skeleton sh-if
3751 "Insert an if statement. See `sh-feature'."
3753 "if( " str
" ) then" \n
3755 ( "other condition, %s: "
3756 < "else if( " str
" ) then" \n
3763 > "if { " str
" } {" \n
3765 ( "other condition, %s: "
3766 "} { " str
" } {" > \n
3773 > "if( " str
" ) {" \n
3775 ( "other condition, %s: "
3776 "} else if( " str
" ) {" > \n
3784 '(setq input
(sh-feature sh-test
))
3785 > "if " str
"; then" \n
3787 ( "other condition, %s: "
3788 > "elif " str
"; then" > \n
3797 (define-skeleton sh-repeat
3798 "Insert a repeat loop definition. See `sh-feature'."
3804 > "repeat " str
"; do" > \n
3808 ;;;(put 'sh-repeat 'menu-enable '(sh-feature sh-repeat))
3812 (define-skeleton sh-select
3813 "Insert a select statement. See `sh-feature'."
3814 (ksh88 "Index variable: "
3815 > "select " str
" in " _
"; do" \n
3818 (bash eval sh-append ksh88
)
3820 ;;;(put 'sh-select 'menu-enable '(sh-feature sh-select))
3824 (define-skeleton sh-tmp-file
3825 "Insert code to setup temporary file handling. See `sh-feature'."
3826 (bash eval identity ksh88
)
3827 (csh (file-name-nondirectory (buffer-file-name))
3828 "set tmp = /tmp/" str
".$$" \n
3830 (and (goto-char (point-max))
3834 "rm $tmp* >&/dev/null" >)
3835 (es (file-name-nondirectory (buffer-file-name))
3836 > "local( signals = $signals sighup sigint; tmp = /tmp/" str
3839 > "rm $tmp^* >[2]/dev/null" \n
3845 (ksh88 eval sh-modify sh
3847 (rc (file-name-nondirectory (buffer-file-name))
3848 > "tmp = /tmp/" str
".$pid" \n
3849 "fn sigexit { rm $tmp^* >[2]/dev/null }")
3850 (sh (file-name-nondirectory (buffer-file-name))
3851 > "TMP=${TMPDIR:-/tmp}/" str
".$$" \n
3852 "trap \"rm $TMP* 2>/dev/null\" " ?
0))
3856 (define-skeleton sh-until
3857 "Insert an until loop. See `sh-feature'."
3859 '(setq input
(sh-feature sh-test
))
3860 > "until " str
"; do" \n
3863 ;;;(put 'sh-until 'menu-enable '(sh-feature sh-until))
3867 (define-skeleton sh-while
3868 "Insert a while loop. See `sh-feature'."
3869 (csh eval sh-modify sh
3875 (es eval sh-modify sh
3879 (rc eval sh-modify sh
3884 '(setq input
(sh-feature sh-test
))
3885 > "while " str
"; do" \n
3891 (define-skeleton sh-while-getopts
3892 "Insert a while getopts loop. See `sh-feature'.
3893 Prompts for an options string which consists of letters for each recognized
3894 option followed by a colon `:' if the option accepts an argument."
3895 (bash eval sh-modify sh
3899 > "switch( \"$1\" )" \n
3900 '(setq input
'("- x" .
2))
3903 < "case " '(eval str
)
3904 '(if (string-match " +" str
)
3905 (setq v1
(substring str
(match-end 0))
3906 str
(substring str
0 (match-beginning 0)))
3909 > "set " v1
& " = $2" | -
4 & _
\n
3910 (if v1
"shift") & \n
3920 (ksh88 eval sh-modify sh
3924 (posix eval sh-modify sh
3925 18 "$(basename $0)")
3927 > "while getopts :" str
" OPT; do" \n
3929 '(setq v1
(append (vconcat str
) nil
))
3930 ( (prog1 (if v1
(char-to-string (car v1
)))
3931 (if (eq (nth 1 v1
) ?
:)
3932 (setq v1
(nthcdr 2 v1
)
3936 > str
"|+" str
'(sh-electric-rparen) \n
3939 > "*" '(sh-electric-rparen) \n
3940 > "echo" " \"usage: " "`basename $0`"
3941 " [+-" '(setq v1
(point)) str
3943 (while (search-backward ":" v1 t
)
3944 (replace-match " ARG] [+-" t t
)))
3945 (if (eq (preceding-char) ?-
) -
5)
3946 "] [--] ARGS...\"" \n
3951 "shift " (sh-add "OPTIND" -
1)))
3955 (defun sh-assignment (arg)
3956 "Remember preceding identifier for future completion and do self-insert."
3958 (self-insert-command arg
)
3960 (sh-remember-variable
3962 (if (re-search-forward (sh-feature sh-assignment-regexp
)
3964 (beginning-of-line 1))
3966 (match-string 1))))))
3970 (defun sh-maybe-here-document (arg)
3971 "Inserts self. Without prefix, following unquoted `<' inserts here document.
3972 The document is bounded by `sh-here-document-word'."
3974 (self-insert-command (prefix-numeric-value arg
))
3976 (not (eq (char-after (- (point) 2)) last-command-char
))
3981 (insert sh-here-document-word
)
3982 (or (eolp) (looking-at "[ \t]") (insert ?
))
3988 (save-excursion (insert ?
\n sh-here-document-word
)))))
3991 ;; various other commands
3993 (autoload 'comint-dynamic-complete
"comint"
3994 "Dynamically perform completion at point." t
)
3996 (autoload 'shell-dynamic-complete-command
"shell"
3997 "Dynamically complete the command at point." t
)
3999 (autoload 'comint-dynamic-complete-filename
"comint"
4000 "Dynamically complete the filename at point." t
)
4002 (autoload 'shell-dynamic-complete-environment-variable
"shell"
4003 "Dynamically complete the environment variable at point." t
)
4007 (defun sh-newline-and-indent ()
4008 "Strip unquoted whitespace, insert newline, and indent like current line."
4010 (indent-to (prog1 (current-indentation)
4011 (delete-region (point)
4013 (or (zerop (skip-chars-backward " \t"))
4019 (defun sh-beginning-of-command ()
4020 "Move point to successive beginnings of commands."
4022 (if (re-search-backward sh-beginning-of-command nil t
)
4023 (goto-char (match-beginning 2))))
4025 (defun sh-end-of-command ()
4026 "Move point to successive ends of commands."
4028 (if (re-search-forward sh-end-of-command nil t
)
4029 (goto-char (match-end 1))))
4031 (provide 'sh-script
)
4033 ;;; sh-script.el ends here