sh-script.el 113 KB
Newer Older
Richard M. Stallman's avatar
Richard M. Stallman committed
1
;;; sh-script.el --- shell-script editing commands for Emacs
Erik Naggum's avatar
Erik Naggum committed
2

3
;; Copyright (C) 1993, 94, 95, 96, 97, 1999, 2001, 2003
4
;;  Free Software Foundation, Inc.
Richard M. Stallman's avatar
Richard M. Stallman committed
5

Karl Heuer's avatar
Karl Heuer committed
6
;; Author: Daniel Pfeiffer <occitan@esperanto.org>
7
;; Version: 2.0f
Richard M. Stallman's avatar
Richard M. Stallman committed
8
;; Maintainer: FSF
9
;; Keywords: languages, unix
Richard M. Stallman's avatar
Richard M. Stallman committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23

;; This file is part of GNU Emacs.

;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
Erik Naggum's avatar
Erik Naggum committed
24 25 26
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
Richard M. Stallman's avatar
Richard M. Stallman committed
27 28 29

;;; Commentary:

30 31 32 33 34
;; Major mode for editing shell scripts.  Bourne, C and rc shells as well
;; as various derivatives are supported and easily derived from.  Structured
;; statements can be inserted with one command or abbrev.  Completion is
;; available for filenames, variables known from the script, the shell and
;; the environment as well as commands.
Richard M. Stallman's avatar
Richard M. Stallman committed
35

36 37
;;; Known Bugs:

38
;; - In Bourne the keyword `in' is not anchored to case, for, select ...
39 40
;; - Variables in `"' strings aren't fontified because there's no way of
;;   syntactically distinguishing those from `'' strings.
41

42 43 44 45
;;		Indentation
;;	 	===========
;; Indentation for rc and es modes is very limited, but for Bourne shells
;; and its derivatives it is quite customizable.
46
;;
47 48
;; The following description applies to sh and derived shells (bash,
;; zsh, ...).
49
;;
50 51 52 53
;; There are various customization variables which allow tailoring to
;; a wide variety of styles.  Most of these variables are named
;; sh-indent-for-XXX and sh-indent-after-XXX.  For example.
;; sh-indent-after-if controls the indenting of a line following
Stefan Monnier's avatar
Stefan Monnier committed
54
;; an if statement, and sh-indent-for-fi controls the indentation
55
;; of the line containing the fi.
56
;;
57 58 59 60
;; You can set each to a numeric value, but it is often more convenient
;; to a symbol such as `+' which uses the value of variable `sh-basic-offset'.
;; By changing this one variable you can increase or decrease how much
;; indentation there is.  Valid symbols:
61
;;
62 63 64 65 66 67
;; 	+   Indent right by sh-basic-offset
;; 	-   Indent left  by sh-basic-offset
;; 	++  Indent right twice sh-basic-offset
;; 	--  Indent left  twice sh-basic-offset
;; 	*   Indent right half sh-basic-offset
;; 	/   Indent left  half sh-basic-offset.
68
;;
69
;; There are 4 commands to help set the indentation variables:
70
;;
71 72 73
;; `sh-show-indent'
;;    This shows what variable controls the indentation of the current
;;    line and its value.
74
;;
75 76 77 78 79 80 81
;; `sh-set-indent'
;;    This allows you to set the value of the variable controlling the
;;    current line's indentation.  You can enter a number or one of a
;;    number of special symbols to denote the value of sh-basic-offset,
;;    or its negative, or half it, or twice it, etc.  If you've used
;;    cc-mode this should be familiar.  If you forget which symbols are
;;    valid simply press C-h at the prompt.
82
;;
83 84 85 86 87
;; `sh-learn-line-indent'
;;    Simply make the line look the way you want it, then invoke this
;;    command.  It will set the variable to the value that makes the line
;;    indent like that.  If called with a prefix argument then it will set
;;    the value to one of the symbols if applicable.
88
;;
89 90 91 92 93 94 95 96 97 98 99 100
;; `sh-learn-buffer-indent'
;;    This is the deluxe function!  It "learns" the whole buffer (use
;;    narrowing if you want it to process only part).  It outputs to a
;;    buffer *indent* any conflicts it finds, and all the variables it has
;;    learned.  This buffer is a sort of Occur mode buffer, allowing you to
;;    easily find where something was set.  It is popped to automatically
;;    if there are any conflicts found or if `sh-popup-occur-buffer' is
;;    non-nil.
;;    `sh-indent-comment' will be set if all comments follow  the same
;;    pattern;  if they don't it will be set to nil.
;;    Whether `sh-basic-offset' is set is determined by variable
;;    `sh-learn-basic-offset'.
101
;;
102 103 104 105 106 107 108
;;    Unfortunately, `sh-learn-buffer-indent' can take a long time to run
;;    (e.g. if there are large case statements).  Perhaps it does not make
;;    sense to run it on large buffers: if lots of lines have different
;;    indentation styles it will produce a lot of diagnostics in the
;;    *indent* buffer; if there is a consistent style then running
;;    `sh-learn-buffer-indent' on a small region of the buffer should
;;    suffice.
109
;;
110 111 112 113 114 115 116
;; 	Saving indentation values
;; 	-------------------------
;; After you've learned the values in a buffer, how to you remember
;; them?   Originally I had hoped that `sh-learn-buffer-indent'
;; would make this unnecessary;  simply learn the values when you visit
;; the buffer.
;; You can do this automatically like this:
117
;;   (add-hook 'sh-set-shell-hook 'sh-learn-buffer-indent)
118
;;
119
;; However...  `sh-learn-buffer-indent' is extremely slow,
Stefan Monnier's avatar
Stefan Monnier committed
120
;; especially on large-ish buffer.  Also, if there are conflicts the
121
;; "last one wins" which may not produce the desired setting.
122
;;
123 124
;; So...There is a minimal way of being able to save indentation values and
;; to reload them in another buffer or at another point in time.
125
;;
126 127 128 129 130 131 132
;; Use `sh-name-style' to give a name to the indentation settings of
;; 	the current buffer.
;; Use `sh-load-style' to load indentation settings for the current
;; 	buffer from a specific style.
;; Use `sh-save-styles-to-buffer' to write all the styles to a buffer
;; 	in lisp code.  You can then store it in a file and later use
;; 	`load-file' to load it.
133
;;
134 135 136 137
;; 	Indentation variables - buffer local or global?
;; 	----------------------------------------------
;; I think that often having them buffer-local makes sense,
;; especially if one is using `sh-learn-buffer-indent'.  However, if
Stefan Monnier's avatar
Stefan Monnier committed
138
;; a user sets values using customization, these changes won't appear
139
;; to work if the variables are already local!
140
;;
Stefan Monnier's avatar
Stefan Monnier committed
141
;; To get round this, there is a variable `sh-make-vars-local' and 2
142
;; functions: `sh-make-vars-local' and `sh-reset-indent-vars-to-global-values'.
143
;;
Stefan Monnier's avatar
Stefan Monnier committed
144
;; If `sh-make-vars-local' is non-nil, then these variables become
145
;; buffer local when the mode is established.
Stefan Monnier's avatar
Stefan Monnier committed
146
;; If this is nil, then the variables are global.  At any time you
147
;; can make them local with the command `sh-make-vars-local'.
Stefan Monnier's avatar
Stefan Monnier committed
148
;; Conversely, to update with the global values you can use the
149
;; command `sh-reset-indent-vars-to-global-values'.
150
;;
Stefan Monnier's avatar
Stefan Monnier committed
151
;; This may be awkward, but the intent is to cover all cases.
152
;;
153 154 155
;; 	Awkward things, pitfalls
;; 	------------------------
;; Indentation for a sh script is complicated for a number of reasons:
156
;;
Stefan Monnier's avatar
Stefan Monnier committed
157
;; 1. You can't format by simply looking at symbols, you need to look
158 159 160 161
;;    at keywords.  [This is not the case for rc and es shells.]
;; 2. The character ")" is used both as a matched pair "(" ... ")" and
;;    as a stand-alone symbol (in a case alternative).  This makes
;;    things quite tricky!
Stefan Monnier's avatar
Stefan Monnier committed
162
;; 3. Here-documents in a script should be treated "as is", and when
163 164 165 166 167
;;    they terminate we want to revert to the indentation of the line
;;    containing the "<<" symbol.
;; 4. A line may be continued using the "\".
;; 5. The character "#" (outside a string) normally starts a comment,
;;    but it doesn't in the sequence "$#"!
168
;;
169
;; To try and address points 2 3 and 5 I used a feature that cperl mode
Stefan Monnier's avatar
Stefan Monnier committed
170
;; uses, that of a text's syntax property.  This, however, has 2
171 172 173 174 175 176 177
;; disadvantages:
;; 1. We need to scan the buffer to find which ")" symbols belong to a
;;    case alternative, to find any here documents, and handle "$#".
;; 2. Setting the text property makes the buffer modified.  If the
;;    buffer is read-only buffer we have to cheat and bypass the read-only
;;    status.  This is for cases where the buffer started read-only buffer
;;    but the user issued `toggle-read-only'.
178
;;
179 180 181 182
;; 	Bugs
;; 	----
;; - Indenting many lines is slow.  It currently does each line
;;   independently, rather than saving state information.
183
;;
184
;; - `sh-learn-buffer-indent' is extremely slow.
185
;;
186 187
;; Richard Sharman <rsharman@pobox.com>  June 1999.

Richard M. Stallman's avatar
Richard M. Stallman committed
188 189 190
;;; Code:

;; page 1:	variables and settings
191 192 193 194
;; page 2:	indentation stuff
;; page 3:	mode-command and utility functions
;; page 4:	statement syntax-commands for various shells
;; page 5:	various other commands
Richard M. Stallman's avatar
Richard M. Stallman committed
195

Dave Love's avatar
Dave Love committed
196 197 198
(eval-when-compile
  (require 'skeleton)
  (require 'comint))
199 200
(require 'executable)

201 202


203 204 205 206 207 208 209 210 211 212 213 214
(defgroup sh nil
  "Shell programming utilities"
  :group 'unix
  :group 'languages)

(defgroup sh-script nil
  "Shell script mode"
  :group 'sh
  :prefix "sh-")


(defcustom sh-ancestor-alist
215 216
  '((ash . sh)
    (bash . jsh)
217
    (bash2 . jsh)
218 219 220 221 222 223 224 225 226 227 228 229 230
    (dtksh . ksh)
    (es . rc)
    (itcsh . tcsh)
    (jcsh . csh)
    (jsh . sh)
    (ksh . ksh88)
    (ksh88 . jsh)
    (oash . sh)
    (pdksh . ksh88)
    (posix . sh)
    (tcsh . csh)
    (wksh . ksh88)
    (wsh . sh)
231 232
    (zsh . ksh88)
    (rpm . sh))
233 234 235 236 237 238
  "*Alist showing the direct ancestor of various shells.
This is the basis for `sh-feature'.  See also `sh-alias-alist'.
By default we have the following three hierarchies:

csh		C Shell
  jcsh		C Shell with Job Control
239 240
  tcsh		Turbo C Shell
    itcsh	? Turbo C Shell
241 242 243 244 245 246 247 248 249 250 251 252 253 254
rc		Plan 9 Shell
  es		Extensible Shell
sh		Bourne Shell
  ash		? Shell
  jsh		Bourne Shell with Job Control
    bash	GNU Bourne Again Shell
    ksh88	Korn Shell '88
      ksh	Korn Shell '93
	dtksh	CDE Desktop Korn Shell
      pdksh	Public Domain Korn Shell
      wksh	Window Korn Shell
      zsh	Z Shell
  oash		SCO OA (curses) Shell
  posix		IEEE 1003.2 Shell Standard
255 256 257
  wsh		? Shell"
  :type '(repeat (cons symbol symbol))
  :group 'sh-script)
258 259


260
(defcustom sh-alias-alist
261
  (append (if (eq system-type 'gnu/linux)
262
	     '((csh . tcsh)
263
	       (ksh . pdksh)))
264 265
	 ;; for the time being
	 '((ksh . ksh88)
266
           (bash2 . bash)
267 268 269
	   (sh5 . sh)))
  "*Alist for transforming shell names to what they really are.
Use this where the name of the executable doesn't correspond to the type of
270 271 272
shell it really is."
  :type '(repeat (cons symbol symbol))
  :group 'sh-script)
273 274


275
(defcustom sh-shell-file
276
  (or
277 278
   ;; On MSDOS and Windows, collapse $SHELL to lower-case and remove
   ;; the executable extension, so comparisons with the list of
279
   ;; known shells work.
280
   (and (memq system-type '(ms-dos windows-nt))
281 282 283 284 285 286 287
	(let* ((shell (getenv "SHELL"))
	       (shell-base
		(and shell (file-name-nondirectory shell))))
	  ;; shell-script mode doesn't support DOS/Windows shells,
	  ;; so use the default instead.
	  (if (or (null shell)
		  (member (downcase shell-base)
288 289
			  '("command.com" "cmd.exe" "4dos.com" "ndos.com"
			    "cmdproxy.exe")))
290 291
	      "/bin/sh"
	    (file-name-sans-extension (downcase shell)))))
292 293
   (getenv "SHELL")
   "/bin/sh")
294 295 296
  "*The executable file name for the shell being programmed."
  :type 'string
  :group 'sh-script)
297 298


299
(defcustom sh-shell-arg
300
  ;; bash does not need any options when run in a shell script,
301
  '((bash)
302 303
    (csh . "-f")
    (pdksh)
304
    ;; Bill_Mann@praxisint.com says -p with ksh can do harm.
305
    (ksh88)
306
    ;; -p means don't initialize functions from the environment.
307
    (rc . "-p")
308 309 310 311
    ;; Someone proposed -motif, but we don't want to encourage
    ;; use of a non-free widget set.
    (wksh)
    ;; -f means don't run .zshrc.
312
    (zsh . "-f"))
313 314 315 316 317 318 319 320
  "*Single argument string for the magic number.  See `sh-feature'."
  :type '(repeat (cons (symbol :tag "Shell")
		       (choice (const :tag "No Arguments" nil)
			       (string :tag "Arguments")
			       (cons :format "Evaluate: %v"
				     (const :format "" eval)
				     sexp))))
  :group 'sh-script)
321

322
(defcustom sh-imenu-generic-expression
323 324
  `((sh
     . ((nil "^\\s-*\\(function\\s-+\\)?\\([A-Za-z_][A-Za-z_0-9]+\\)\\s-*()" 2))))
325 326 327 328 329 330 331 332
  "*Alist of regular expressions for recognizing shell function definitions.
See `sh-feature' and `imenu-generic-expression'."
  :type '(alist :key-type (symbol :tag "Shell")
		:value-type (alist :key-type (choice :tag "Title"
						     string
						     (const :tag "None" nil))
				   :value-type
				   (repeat :tag "Regexp, index..." sexp)))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
333
  :group 'sh-script
334
  :version "20.4")
335

336 337 338 339 340
(defvar sh-shell-variables nil
  "Alist of shell variable names that should be included in completion.
These are used for completion in addition to all the variables named
in `process-environment'.  Each element looks like (VAR . VAR), where
the car and cdr are the same symbol.")
341

342 343 344
(defvar sh-shell-variables-initialized nil
  "Non-nil if `sh-shell-variables' is initialized.")

345 346
(defun sh-canonicalize-shell (shell)
  "Convert a shell name SHELL to the one we should handle it as."
347
  (if (string-match "\\.exe\\'" shell)
348
      (setq shell (substring shell 0 (match-beginning 0))))
349 350 351 352
  (or (symbolp shell)
      (setq shell (intern shell)))
  (or (cdr (assq shell sh-alias-alist))
      shell))
353

354 355
(defvar sh-shell (sh-canonicalize-shell (file-name-nondirectory sh-shell-file))
  "The shell being programmed.  This is set by \\[sh-set-shell].")
356

357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
;; I turned off this feature because it doesn't permit typing commands
;; in the usual way without help.
;;(defvar sh-abbrevs
;;  '((csh eval sh-abbrevs shell
;;	 "switch" 'sh-case
;;	 "getopts" 'sh-while-getopts)

;;    (es eval sh-abbrevs shell
;;	"function" 'sh-function)

;;    (ksh88 eval sh-abbrevs sh
;;	   "select" 'sh-select)

;;    (rc eval sh-abbrevs shell
;;	"case" 'sh-case
;;	"function" 'sh-function)

;;    (sh eval sh-abbrevs shell
;;	"case" 'sh-case
;;	"function" 'sh-function
;;	"until" 'sh-until
;;	"getopts" 'sh-while-getopts)

;;    ;; The next entry is only used for defining the others
;;    (shell "for" sh-for
;;	   "loop" sh-indexed-loop
;;	   "if" sh-if
;;	   "tmpfile" sh-tmp-file
;;	   "while" sh-while)

;;    (zsh eval sh-abbrevs ksh88
;;	 "repeat" 'sh-repeat))
;;  "Abbrev-table used in Shell-Script mode.  See `sh-feature'.
390 391
;;;Due to the internal workings of abbrev tables, the shell name symbol is
;;;actually defined as the table for the like of \\[edit-abbrevs].")
Richard M. Stallman's avatar
Richard M. Stallman committed
392 393 394



395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
(defvar sh-mode-syntax-table
  '((sh eval sh-mode-syntax-table ()
	?\# "<"
	?\n ">#"
	?\" "\"\""
	?\' "\"'"
	?\` "\"`"
	?! "_"
	?% "_"
	?: "_"
	?. "_"
	?^ "_"
	?~ "_"
	?< "."
	?> ".")
    (csh eval identity sh)
    (rc eval identity sh))

  "Syntax-table used in Shell-Script mode.  See `sh-feature'.")
Richard M. Stallman's avatar
Richard M. Stallman committed
414 415

(defvar sh-mode-map
416 417
  (let ((map (make-sparse-keymap))
	(menu-map (make-sparse-keymap "Insert")))
Richard M. Stallman's avatar
Richard M. Stallman committed
418 419 420
    (define-key map "\C-c(" 'sh-function)
    (define-key map "\C-c\C-w" 'sh-while)
    (define-key map "\C-c\C-u" 'sh-until)
421
    (define-key map "\C-c\C-t" 'sh-tmp-file)
Richard M. Stallman's avatar
Richard M. Stallman committed
422
    (define-key map "\C-c\C-s" 'sh-select)
423 424
    (define-key map "\C-c\C-r" 'sh-repeat)
    (define-key map "\C-c\C-o" 'sh-while-getopts)
Richard M. Stallman's avatar
Richard M. Stallman committed
425 426 427 428
    (define-key map "\C-c\C-l" 'sh-indexed-loop)
    (define-key map "\C-c\C-i" 'sh-if)
    (define-key map "\C-c\C-f" 'sh-for)
    (define-key map "\C-c\C-c" 'sh-case)
429 430 431 432
    (define-key map "\C-c?" 'sh-show-indent)
    (define-key map "\C-c=" 'sh-set-indent)
    (define-key map "\C-c<" 'sh-learn-line-indent)
    (define-key map "\C-c>" 'sh-learn-buffer-indent)
433

Richard M. Stallman's avatar
Richard M. Stallman committed
434 435
    (define-key map "=" 'sh-assignment)
    (define-key map "\C-c+" 'sh-add)
436 437
    (define-key map "\C-\M-x" 'sh-execute-region)
    (define-key map "\C-c\C-x" 'executable-interpret)
438
    (define-key map "<" 'sh-maybe-here-document)
439 440 441 442 443 444
    (define-key map "(" 'skeleton-pair-insert-maybe)
    (define-key map "{" 'skeleton-pair-insert-maybe)
    (define-key map "[" 'skeleton-pair-insert-maybe)
    (define-key map "'" 'skeleton-pair-insert-maybe)
    (define-key map "`" 'skeleton-pair-insert-maybe)
    (define-key map "\"" 'skeleton-pair-insert-maybe)
Richard M. Stallman's avatar
Richard M. Stallman committed
445

446 447 448 449
    (define-key map [remap complete-tag] 'comint-dynamic-complete)
    (define-key map [remap newline-and-indent] 'sh-newline-and-indent)
    (define-key map [remap delete-backward-char]
      'backward-delete-char-untabify)
Richard M. Stallman's avatar
Richard M. Stallman committed
450
    (define-key map "\C-c:" 'sh-set-shell)
451 452 453 454
    (define-key map [remap beginning-of-defun]
      'sh-beginning-of-compound-command)
    (define-key map [remap backward-sentence] 'sh-beginning-of-command)
    (define-key map [remap forward-sentence] 'sh-end-of-command)
455 456 457 458 459 460
    (define-key map [menu-bar insert] (cons "Insert" menu-map))
    (define-key menu-map [sh-while]	'("While Loop" . sh-while))
    (define-key menu-map [sh-until]	'("Until Loop" . sh-until))
    (define-key menu-map [sh-tmp-file]	'("Temporary File" . sh-tmp-file))
    (define-key menu-map [sh-select]	'("Select Statement" . sh-select))
    (define-key menu-map [sh-repeat]	'("Repeat Loop" . sh-repeat))
461 462
    (define-key menu-map [sh-getopts]	'("Options Loop" . sh-while-getopts))
    (define-key menu-map [sh-indexed-loop] '("Indexed Loop" . sh-indexed-loop))
463 464 465
    (define-key menu-map [sh-if]	'("If Statement" . sh-if))
    (define-key menu-map [sh-for]	'("For Loop" . sh-for))
    (define-key menu-map [sh-case]	'("Case Statement" . sh-case))
Richard M. Stallman's avatar
Richard M. Stallman committed
466 467 468 469 470
    map)
  "Keymap used in Shell-Script mode.")



471
(defcustom sh-dynamic-complete-functions
472 473 474
  '(shell-dynamic-complete-environment-variable
    shell-dynamic-complete-command
    comint-dynamic-complete-filename)
475 476 477
  "*Functions for doing TAB dynamic completion."
  :type '(repeat function)
  :group 'sh-script)
Richard M. Stallman's avatar
Richard M. Stallman committed
478 479


480
(defcustom sh-require-final-newline
481 482 483 484 485
  '((csh . t)
    (pdksh . t)
    (rc eval . require-final-newline)
    (sh eval . require-final-newline))
  "*Value of `require-final-newline' in Shell-Script mode buffers.
486 487 488 489 490 491 492
See `sh-feature'."
  :type '(repeat (cons (symbol :tag "Shell")
		       (choice (const :tag "require" t)
			       (cons :format "Evaluate: %v"
				     (const :format "" eval)
				     sexp))))
  :group 'sh-script)
Richard M. Stallman's avatar
Richard M. Stallman committed
493 494


495
(defcustom sh-assignment-regexp
496 497 498 499 500 501 502
  '((csh . "\\<\\([a-zA-Z0-9_]+\\)\\(\\[.+\\]\\)?[ \t]*[-+*/%^]?=")
    ;; actually spaces are only supported in let/(( ... ))
    (ksh88 . "\\<\\([a-zA-Z0-9_]+\\)\\(\\[.+\\]\\)?[ \t]*\\([-+*/%&|~^]\\|<<\\|>>\\)?=")
    (rc . "\\<\\([a-zA-Z0-9_*]+\\)[ \t]*=")
    (sh . "\\<\\([a-zA-Z0-9_]+\\)="))
  "*Regexp for the variable name and what may follow in an assignment.
First grouping matches the variable name.  This is upto and including the `='
503 504 505 506 507 508 509
sign.  See `sh-feature'."
  :type '(repeat (cons (symbol :tag "Shell")
		       (choice regexp
			       (cons :format "Evaluate: %v"
				     (const :format "" eval)
				     sexp))))
  :group 'sh-script)
Richard M. Stallman's avatar
Richard M. Stallman committed
510 511


512 513 514 515
(defcustom sh-indentation 4
  "The width for further indentation in Shell-Script mode."
  :type 'integer
  :group 'sh-script)
Richard M. Stallman's avatar
Richard M. Stallman committed
516 517


518 519 520 521
(defcustom sh-remember-variable-min 3
  "*Don't remember variables less than this length for completing reads."
  :type 'integer
  :group 'sh-script)
Richard M. Stallman's avatar
Richard M. Stallman committed
522 523


524
(defvar sh-header-marker nil
525
  "When non-nil is the end of header for prepending by \\[sh-execute-region].
526 527 528
That command is also used for setting this variable.")


529
(defcustom sh-beginning-of-command
530
  "\\([;({`|&]\\|\\`\\|[^\\]\n\\)[ \t]*\\([/~a-zA-Z0-9:]\\)"
Richard M. Stallman's avatar
Richard M. Stallman committed
531
  "*Regexp to determine the beginning of a shell command.
532 533 534
The actual command starts at the beginning of the second \\(grouping\\)."
  :type 'regexp
  :group 'sh-script)
Richard M. Stallman's avatar
Richard M. Stallman committed
535

536

537
(defcustom sh-end-of-command
538
  "\\([/~a-zA-Z0-9:]\\)[ \t]*\\([;#)}`|&]\\|$\\)"
Richard M. Stallman's avatar
Richard M. Stallman committed
539
  "*Regexp to determine the end of a shell command.
540 541 542
The actual command ends at the end of the first \\(grouping\\)."
  :type 'regexp
  :group 'sh-script)
Richard M. Stallman's avatar
Richard M. Stallman committed
543 544 545



546
(defvar sh-here-document-word "EOF"
Richard M. Stallman's avatar
Richard M. Stallman committed
547 548
  "Word to delimit here documents.")

Karl Heuer's avatar
Karl Heuer committed
549 550 551 552 553
(defvar sh-test
  '((sh "[  ]" . 3)
    (ksh88 "[[  ]]" . 4))
  "Initial input in Bourne if, while and until skeletons.  See `sh-feature'.")

Richard M. Stallman's avatar
Richard M. Stallman committed
554

555 556 557
;; customized this out of sheer bravado.  not for the faint of heart.
;; but it *did* have an asterisk in the docstring!
(defcustom sh-builtins
558 559 560 561
  '((bash eval sh-append posix
	  "alias" "bg" "bind" "builtin" "declare" "dirs" "enable" "fc" "fg"
	  "help" "history" "jobs" "kill" "let" "local" "popd" "pushd" "source"
	  "suspend" "typeset" "unalias")
Richard M. Stallman's avatar
Richard M. Stallman committed
562

563 564
    ;; The next entry is only used for defining the others
    (bourne eval sh-append shell
565 566
	    "eval" "export" "getopts" "newgrp" "pwd" "read" "readonly"
	    "times" "ulimit")
Richard M. Stallman's avatar
Richard M. Stallman committed
567

568
    (csh eval sh-append shell
569 570 571 572
	 "alias" "chdir" "glob" "history" "limit" "nice" "nohup" "rehash"
	 "setenv" "source" "time" "unalias" "unhash")

    (dtksh eval identity wksh)
Richard M. Stallman's avatar
Richard M. Stallman committed
573

574 575
    (es "access" "apids" "cd" "echo" "eval" "false" "let" "limit" "local"
	"newpgrp" "result" "time" "umask" "var" "vars" "wait" "whatis")
Richard M. Stallman's avatar
Richard M. Stallman committed
576

577 578
    (jsh eval sh-append sh
	 "bg" "fg" "jobs" "kill" "stop" "suspend")
Richard M. Stallman's avatar
Richard M. Stallman committed
579

580
    (jcsh eval sh-append csh
581
	  "bg" "fg" "jobs" "kill" "notify" "stop" "suspend")
582 583

    (ksh88 eval sh-append bourne
584 585
	   "alias" "bg" "false" "fc" "fg" "jobs" "kill" "let" "print" "time"
	   "typeset" "unalias" "whence")
586 587 588 589 590 591 592 593 594 595 596 597 598

    (oash eval sh-append sh
	  "checkwin" "dateline" "error" "form" "menu" "newwin" "oadeinit"
	  "oaed" "oahelp" "oainit" "pp" "ppfile" "scan" "scrollok" "wattr"
	  "wclear" "werase" "win" "wmclose" "wmmessage" "wmopen" "wmove"
	  "wmtitle" "wrefresh")

    (pdksh eval sh-append ksh88
	   "bind")

    (posix eval sh-append sh
	   "command")

599 600
    (rc "builtin" "cd" "echo" "eval" "limit" "newpgrp" "shift" "umask" "wait"
	"whatis")
601 602 603 604 605

    (sh eval sh-append bourne
	"hash" "test" "type")

    ;; The next entry is only used for defining the others
606 607 608 609
    (shell "cd" "echo" "eval" "set" "shift" "umask" "unset" "wait")

    (wksh eval sh-append ksh88
	  "Xt[A-Z][A-Za-z]*")
610 611

    (zsh eval sh-append ksh88
612 613 614 615 616 617
	 "autoload" "bindkey" "builtin" "chdir" "compctl" "declare" "dirs"
	 "disable" "disown" "echotc" "enable" "functions" "getln" "hash"
	 "history" "integer" "limit" "local" "log" "popd" "pushd" "r"
	 "readonly" "rehash" "sched" "setopt" "source" "suspend" "true"
	 "ttyctl" "type" "unfunction" "unhash" "unlimit" "unsetopt" "vared"
	 "which"))
618 619
  "*List of all shell builtins for completing read and fontification.
Note that on some systems not all builtins are available or some are
620 621 622 623 624 625 626
implemented as aliases.  See `sh-feature'."
  :type '(repeat (cons (symbol :tag "Shell")
		       (choice (repeat string)
			       (cons :format "Evaluate: %v"
				     (const :format "" eval)
				     sexp))))
  :group 'sh-script)
627 628


629

630
(defcustom sh-leading-keywords
631 632 633 634 635 636 637 638 639 640
  '((csh "else")

    (es "true" "unwind-protect" "whatis")

    (rc "else")

    (sh "do" "elif" "else" "if" "then" "trap" "type" "until" "while"))
  "*List of keywords that may be immediately followed by a builtin or keyword.
Given some confusion between keywords and builtins depending on shell and
system, the distinction here has been based on whether they influence the
641 642 643 644 645 646 647
flow of control or syntax.  See `sh-feature'."
  :type '(repeat (cons (symbol :tag "Shell")
		       (choice (repeat string)
			       (cons :format "Evaluate: %v"
				     (const :format "" eval)
				     sexp))))
  :group 'sh-script)
648 649


650
(defcustom sh-other-keywords
651 652
  '((bash eval sh-append bourne
	  "bye" "logout")
653 654

    ;; The next entry is only used for defining the others
655 656
    (bourne eval sh-append sh
	    "function")
657

658 659 660
    (csh eval sh-append shell
	 "breaksw" "default" "end" "endif" "endsw" "foreach" "goto"
	 "if" "logout" "onintr" "repeat" "switch" "then" "while")
661

662 663
    (es "break" "catch" "exec" "exit" "fn" "for" "forever" "fork" "if"
	"return" "throw" "while")
664 665

    (ksh88 eval sh-append bourne
666
	   "select")
667

668 669
    (rc "break" "case" "exec" "exit" "fn" "for" "if" "in" "return" "switch"
	"while")
670

671 672 673
    (sh eval sh-append shell
	"done" "esac" "fi" "for" "in" "return")

674 675
    ;; The next entry is only used for defining the others
    (shell "break" "case" "continue" "exec" "exit")
676

677 678 679
    (zsh eval sh-append bash
	 "select"))
  "*List of keywords not in `sh-leading-keywords'.
680 681 682 683 684 685 686
See `sh-feature'."
  :type '(repeat (cons (symbol :tag "Shell")
		       (choice (repeat string)
			       (cons :format "Evaluate: %v"
				     (const :format "" eval)
				     sexp))))
  :group 'sh-script)
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710



(defvar sh-variables
  '((bash eval sh-append sh
	  "allow_null_glob_expansion" "auto_resume" "BASH" "BASH_VERSION"
	  "cdable_vars" "ENV" "EUID" "FCEDIT" "FIGNORE" "glob_dot_filenames"
	  "histchars" "HISTFILE" "HISTFILESIZE" "history_control" "HISTSIZE"
	  "hostname_completion_file" "HOSTTYPE" "IGNOREEOF" "ignoreeof"
	  "LINENO" "MAIL_WARNING" "noclobber" "nolinks" "notify"
	  "no_exit_on_failed_exec" "NO_PROMPT_VARS" "OLDPWD" "OPTERR" "PPID"
	  "PROMPT_COMMAND" "PS4" "pushd_silent" "PWD" "RANDOM" "REPLY"
	  "SECONDS" "SHLVL" "TMOUT" "UID")

    (csh eval sh-append shell
	 "argv" "cdpath" "child" "echo" "histchars" "history" "home"
	 "ignoreeof" "mail" "noclobber" "noglob" "nonomatch" "path" "prompt"
	 "shell" "status" "time" "verbose")

    (es eval sh-append shell
	"apid" "cdpath" "CDPATH" "history" "home" "ifs" "noexport" "path"
	"pid" "prompt" "signals")

    (jcsh eval sh-append csh
711
	  "notify")
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755

    (ksh88 eval sh-append sh
	   "ENV" "ERRNO" "FCEDIT" "FPATH" "HISTFILE" "HISTSIZE" "LINENO"
	   "OLDPWD" "PPID" "PS3" "PS4" "PWD" "RANDOM" "REPLY" "SECONDS"
	   "TMOUT")

    (oash eval sh-append sh
	  "FIELD" "FIELD_MAX" "LAST_KEY" "OALIB" "PP_ITEM" "PP_NUM")

    (rc eval sh-append shell
	"apid" "apids" "cdpath" "CDPATH" "history" "home" "ifs" "path" "pid"
	"prompt" "status")

    (sh eval sh-append shell
	"CDPATH" "IFS" "OPTARG" "OPTIND" "PS1" "PS2")

    ;; The next entry is only used for defining the others
    (shell "COLUMNS" "EDITOR" "HOME" "HUSHLOGIN" "LANG" "LC_COLLATE"
	   "LC_CTYPE" "LC_MESSAGES" "LC_MONETARY" "LC_NUMERIC" "LC_TIME"
	   "LINES" "LOGNAME" "MAIL" "MAILCHECK" "MAILPATH" "PAGER" "PATH"
	   "SHELL" "TERM" "TERMCAP" "TERMINFO" "VISUAL")

    (tcsh eval sh-append csh
	  "addsuffix" "ampm" "autocorrect" "autoexpand" "autolist"
	  "autologout" "chase_symlinks" "correct" "dextract" "edit" "el"
	  "fignore" "gid" "histlit" "HOST" "HOSTTYPE" "HPATH"
	  "ignore_symlinks" "listjobs" "listlinks" "listmax" "matchbeep"
	  "nobeep" "NOREBIND" "oid" "printexitvalue" "prompt2" "prompt3"
	  "pushdsilent" "pushdtohome" "recexact" "recognize_only_executables"
	  "rmstar" "savehist" "SHLVL" "showdots" "sl" "SYSTYPE" "tcsh" "term"
	  "tperiod" "tty" "uid" "version" "visiblebell" "watch" "who"
	  "wordchars")

    (zsh eval sh-append ksh88
	 "BAUD" "bindcmds" "cdpath" "DIRSTACKSIZE" "fignore" "FIGNORE" "fpath"
	 "HISTCHARS" "hostcmds" "hosts" "HOSTS" "LISTMAX" "LITHISTSIZE"
	 "LOGCHECK" "mailpath" "manpath" "NULLCMD" "optcmds" "path" "POSTEDIT"
	 "prompt" "PROMPT" "PROMPT2" "PROMPT3" "PROMPT4" "psvar" "PSVAR"
	 "READNULLCMD" "REPORTTIME" "RPROMPT" "RPS1" "SAVEHIST" "SPROMPT"
	 "STTY" "TIMEFMT" "TMOUT" "TMPPREFIX" "varcmds" "watch" "WATCH"
	 "WATCHFMT" "WORDCHARS" "ZDOTDIR"))
  "List of all shell variables available for completing read.
See `sh-feature'.")

756

757
;; Font-Lock support
758 759 760 761

(defface sh-heredoc-face
  '((((class color)
      (background dark))
762
     (:foreground "yellow" :weight bold))
763 764 765 766
    (((class color)
      (background light))
     (:foreground "tan" ))
    (t
767
     (:weight bold)))
768 769 770
  "Face to show a here-document"
  :group 'sh-indentation)
(defvar sh-heredoc-face 'sh-heredoc-face)
771 772 773 774 775


(defvar sh-font-lock-keywords
  '((csh eval sh-append shell
	 '("\\${?[#?]?\\([A-Za-z_][A-Za-z0-9_]*\\|0\\)" 1
Karl Heuer's avatar
Karl Heuer committed
776
	   font-lock-variable-name-face))
777 778 779

    (es eval sh-append executable-font-lock-keywords
	'("\\$#?\\([A-Za-z_][A-Za-z0-9_]*\\|[0-9]+\\)" 1
Karl Heuer's avatar
Karl Heuer committed
780
	  font-lock-variable-name-face))
781

782
    (rc eval identity es)
783 784

    (sh eval sh-append shell
785
	;; Variable names.
786
	'("\\$\\({#?\\)?\\([A-Za-z_][A-Za-z0-9_]*\\|[-#?@!]\\)" 2
787 788 789 790 791
	  font-lock-variable-name-face)
	;; Function names.
	'("^\\(\\sw+\\)[ \t]*(" 1 font-lock-function-name-face)
	'("\\<\\(function\\)\\>[ \t]*\\(\\sw+\\)?"
	  (1 font-lock-keyword-face) (2 font-lock-function-name-face nil t)))
792 793 794

    ;; The next entry is only used for defining the others
    (shell eval sh-append executable-font-lock-keywords
795
	   '("\\\\[^A-Za-z0-9]" 0 font-lock-string-face)
796
	   '("\\${?\\([A-Za-z_][A-Za-z0-9_]*\\|[0-9]+\\|[$*_]\\)" 1
797 798 799 800 801
	     font-lock-variable-name-face))
    (rpm eval sh-append rpm2
	 '("%{?\\(\\sw+\\)"  1 font-lock-keyword-face))
    (rpm2 eval sh-append shell
	  '("^\\(\\sw+\\):"  1 font-lock-variable-name-face)))
802
  "Default expressions to highlight in Shell Script modes.  See `sh-feature'.")
803

804
(defvar sh-font-lock-keywords-1
805
  '((sh "[ \t]in\\>"))
806
  "Subdued level highlighting for Shell Script modes.")
807 808

(defvar sh-font-lock-keywords-2 ()
809
  "Gaudy level highlighting for Shell Script modes.")
810

811 812 813
;; These are used for the syntax table stuff (derived from cperl-mode).
;; Note: parse-sexp-lookup-properties must be set to t for it to work.
(defconst sh-st-punc (string-to-syntax "."))
Stefan Monnier's avatar
Stefan Monnier committed
814
(defconst sh-st-symbol (string-to-syntax "_"))
815 816
(defconst sh-here-doc-syntax (string-to-syntax "|")) ;; generic string

817 818 819 820 821 822 823 824 825 826
(defconst sh-here-doc-open-re "<<-?\\s-*\\\\?\\(\\(?:['\"][^'\"]+['\"]\\|\\sw\\|\\s_\\)+\\).*\\(\n\\)")

(defvar sh-here-doc-markers nil)
(make-variable-buffer-local 'sh-here-doc-markers)
(defvar sh-here-doc-re sh-here-doc-open-re)
(make-variable-buffer-local 'sh-here-doc-re)

(defun sh-font-lock-close-heredoc (bol eof indented)
  "Determine the syntax of the \\n after an EOF.
If non-nil INDENTED indicates that the EOF was indented."
827
  (let* ((eof-re (if eof (regexp-quote eof) ""))
828
         ;; A rough regexp that should find the opening <<EOF back.
829 830
	 (sre (concat "<<\\(-?\\)\\s-*['\"\\]?"
		      ;; Use \s| to cheaply check it's an open-heredoc.
831
		      eof-re "['\"]?\\([ \t|;&)<>].*\\)?\\s|"))
832
	 ;; A regexp that will find other EOFs.
833
	 (ere (concat "^" (if indented "[ \t]*") eof-re "\n"))
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
	 (start (save-excursion
		  (goto-char bol)
		  (re-search-backward (concat sre "\\|" ere) nil t))))
    ;; If subgroup 1 matched, we found an open-heredoc, otherwise we first
    ;; found a close-heredoc which makes the current close-heredoc inoperant.
    (cond
     ((when (and start (match-end 1)
		 (not (and indented (= (match-beginning 1) (match-end 1))))
		 (not (sh-in-comment-or-string (match-beginning 0))))
	;; Make sure our `<<' is not the EOF1 of a `cat <<EOF1 <<EOF2'.
	(save-excursion
	  (goto-char start)
	  (setq start (line-beginning-position 2))
	  (while
	      (progn
		(re-search-forward "<<") ; Skip ourselves.
		(and (re-search-forward sh-here-doc-open-re start 'move)
		     (goto-char (match-beginning 0))
		     (sh-in-comment-or-string (point)))))
	  ;; No <<EOF2 found after our <<.
	  (= (point) start)))
      sh-here-doc-syntax)
     ((not (or start (save-excursion (re-search-forward sre nil t))))
      ;; There's no <<EOF either before or after us,
      ;; so we should remove ourselves from font-lock's keywords.
      (setq sh-here-doc-markers (delete eof sh-here-doc-markers))
      (setq sh-here-doc-re
	    (concat sh-here-doc-open-re "\\|^\\([ \t]*\\)"
		    (regexp-opt sh-here-doc-markers t) "\\(\n\\)"))
      nil))))

(defun sh-font-lock-open-heredoc (start string)
  "Determine the syntax of the \\n after a <<EOF.
START is the position of <<.
STRING is the actual word used as delimiter (f.ex. \"EOF\").
INDENTED is non-nil if the here document's content (and the EOF mark) can
be indented (i.e. a <<- was used rather than just <<)."
  (unless (or (memq (char-before start) '(?< ?>))
	      (sh-in-comment-or-string start))
873 874
    ;; We're looking at <<STRING, so we add "^STRING$" to the syntactic
    ;; font-lock keywords to detect the end of this here document.
875 876 877 878 879 880
    (let ((str (replace-regexp-in-string "['\"]" "" string)))
      (unless (member str sh-here-doc-markers)
	(push str sh-here-doc-markers)
	(setq sh-here-doc-re
	      (concat sh-here-doc-open-re "\\|^\\([ \t]*\\)"
		      (regexp-opt sh-here-doc-markers t) "\\(\n\\)"))))
881 882
    sh-here-doc-syntax))

883 884 885 886 887
(defun sh-font-lock-here-doc (limit)
  "Search for a heredoc marker."
  ;; This looks silly, but it's because `sh-here-doc-re' keeps changing.
  (re-search-forward sh-here-doc-re limit t))

888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907
(defun sh-font-lock-paren (start)
  (save-excursion
    (goto-char start)
    ;; Skip through all patterns
    (while
	(progn
	  (forward-comment (- (point-max)))
	  ;; Skip through one pattern
	  (while
	      (or (/= 0 (skip-syntax-backward "w_"))
		  (/= 0 (skip-chars-backward "?*/"))
		  (when (memq (char-before) '(?\" ?\'))
		    (condition-case nil (progn (backward-sexp 1) t)
		      (error nil)))))
	  (forward-comment (- (point-max)))
	  (when (eq (char-before) ?|)
	    (backward-char 1) t)))
    (when (save-excursion (backward-char 2) (looking-at ";;\\|in"))
      sh-st-punc)))

908
(defconst sh-font-lock-syntactic-keywords
Stefan Monnier's avatar
Stefan Monnier committed
909 910 911 912 913
  ;; A `#' begins a comment when it is unquoted and at the beginning of a
  ;; word.  In the shell, words are separated by metacharacters.
  ;; The list of special chars is taken from the single-unix spec
  ;; of the shell command language (under `quoting') but with `$' removed.
  `(("[^|&;<>()`\\\"' \t\n]\\(#+\\)" 1 ,sh-st-symbol)
914
    ;; Find HEREDOC starters and add a corresponding rule for the ender.
915 916 917 918 919
    (sh-font-lock-here-doc
     (2 (sh-font-lock-open-heredoc
	 (match-beginning 0) (match-string 1)) nil t)
     (5 (sh-font-lock-close-heredoc
	 (match-beginning 0) (match-string 4)
920 921
         (and (match-beginning 3) (/= (match-beginning 3) (match-end 3))))
      nil t))
922 923
    ;; Distinguish the special close-paren in `case'.
    (")" 0 (sh-font-lock-paren (match-beginning 0)))))
924

925 926 927 928 929 930 931
(defun sh-font-lock-syntactic-face-function (state)
  (if (nth 3 state)
      (if (char-valid-p (nth 3 state))
	  font-lock-string-face
	sh-heredoc-face)
    font-lock-comment-face))

932 933 934 935 936 937 938 939 940 941 942 943
(defgroup sh-indentation nil
  "Variables controlling indentation in shell scripts.

Note: customizing these variables will not affect existing buffers if
`sh-make-vars-local' is no-nil.  See the documentation for
variable `sh-make-vars-local', command `sh-make-vars-local'
and command `sh-reset-indent-vars-to-global-values'."
  :group 'sh-script)


(defcustom sh-set-shell-hook nil
  "*Hook run by `sh-set-shell'."
944
  :type 'hook
945 946 947 948
  :group 'sh-script)

(defcustom sh-mode-hook nil
  "*Hook run by `sh-mode'."
949
  :type 'hook
950 951 952 953 954 955 956 957 958 959 960
  :group 'sh-script)

(defcustom sh-learn-basic-offset nil
  "*When `sh-guess-basic-offset' should learn `sh-basic-offset'.

nil mean:              never.
t means:               only if there seems to be an obvious value.
Anything else means:   whenever we have a \"good guess\" as to the value."
  :type '(choice
	  (const :tag "Never" nil)
	  (const :tag "Only if sure"  t)
Stefan Monnier's avatar
Stefan Monnier committed
961
	  (const :tag "If have a good guess" usually))
962 963 964
  :group 'sh-indentation)

(defcustom sh-popup-occur-buffer nil
Stefan Monnier's avatar
Stefan Monnier committed
965 966
  "*Controls when  `sh-learn-buffer-indent' pops the *indent* buffer.
If t it is always shown.  If nil, it is shown only when there
967 968 969
are conflicts."
  :type '(choice
	  (const :tag "Only when there are conflicts." nil)
Stefan Monnier's avatar
Stefan Monnier committed
970
	  (const :tag "Always"  t))
971 972 973
  :group 'sh-indentation)

(defcustom sh-blink t
Stefan Monnier's avatar
Stefan Monnier committed
974
  "*If non-nil, `sh-show-indent' shows the line indentation is relative to.
975 976 977 978 979 980 981 982 983
The position on the line is not necessarily meaningful.
In some cases the line will be the matching keyword, but this is not
always the case."
  :type 'boolean
  :group 'sh-indentation)

(defcustom sh-first-lines-indent 0
  "*The indentation of the first non-blank non-comment line.
Usually 0 meaning first column.
Stefan Monnier's avatar
Stefan Monnier committed
984
Can be set to a number, or to nil which means leave it as is."
985 986 987
  :type '(choice
	  (const :tag "Leave as is"	nil)
	  (integer :tag "Column number"
Stefan Monnier's avatar
Stefan Monnier committed
988
		   :menu-tag "Indent to this col (0 means first col)" ))
989 990 991 992
  :group 'sh-indentation)


(defcustom sh-basic-offset 4
Stefan Monnier's avatar
Stefan Monnier committed
993
  "*The default indentation increment.
994 995 996 997 998 999 1000
This value is used for the + and - symbols in an indentation variable."
  :type 'integer
  :group 'sh-indentation)

(defcustom sh-indent-comment nil
  "*How a comment line is to be indented.
nil means leave it as it is;
Stefan Monnier's avatar
Stefan Monnier committed
1001
t  means indent it as a normal line, aligning it to previous non-blank
1002
   non-comment line;
Stefan Monnier's avatar
Stefan Monnier committed
1003
a number means align to that column, e.g. 0 means fist column."
1004 1005 1006 1007
  :type '(choice
	  (const :tag "Leave as is." nil)
	  (const :tag "Indent as a normal line."  t)
	  (integer :menu-tag "Indent to this col (0 means first col)."
1008
		   :tag "Indent to column number.") )
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
  :group 'sh-indentation)


(defvar sh-debug nil
  "Enable lots of debug messages - if function `sh-debug' is enabled.")


;; Uncomment this defun and comment the defmacro for debugging.
;; (defun sh-debug (&rest args)
;;   "For debugging:  display message ARGS if variable SH-DEBUG is non-nil."
;;   (if sh-debug
;;       (apply 'message args)))
(defmacro sh-debug (&rest args))

1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
(defconst sh-symbol-list
  '((const :tag "+ "  :value +
	   :menu-tag "+   Indent right by sh-basic-offset")
    (const :tag "- "  :value -
	   :menu-tag "-   Indent left  by sh-basic-offset")
    (const :tag "++"  :value  ++
	   :menu-tag "++  Indent right twice sh-basic-offset")
    (const :tag "--"  :value --
	   :menu-tag "--  Indent left  twice sh-basic-offset")
    (const :tag "* " :value *
	   :menu-tag "*   Indent right half sh-basic-offset")
    (const :tag "/ " :value /
	   :menu-tag "/   Indent left  half sh-basic-offset")))
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046

(defcustom sh-indent-for-else 0
  "*How much to indent an else relative to an if.  Usually 0."
  :type `(choice
	  (integer :menu-tag "A number (positive=>indent right)"
		   :tag "A number")
	  (const :tag "--") ;; separator!
	  ,@ sh-symbol-list
	  )
  :group 'sh-indentation)

1047
(defconst sh-number-or-symbol-list
1048 1049 1050
  (append '((integer :menu-tag "A number (positive=>indent right)"
		     :tag "A number")
	    (const :tag "--"))		; separator
1051
	  sh-symbol-list))
1052 1053

(defcustom sh-indent-for-fi 0
1054
  "*How much to indent a fi relative to an if.  Usually 0."
1055 1056 1057 1058
  :type `(choice ,@ sh-number-or-symbol-list )
  :group 'sh-indentation)

(defcustom sh-indent-for-done '0
1059
  "*How much to indent a done relative to its matching stmt.  Usually 0."
1060 <