sh-script.el 126 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

Nick Roberts's avatar
Nick Roberts committed
3 4
;; Copyright (C) 1993, 1994, 1995, 1996, 1997, 1999, 2001, 2003, 2004, 2005,
;;  2006  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
;; along with GNU Emacs; see the file COPYING.  If not, write to the
Lute Kamstra's avatar
Lute Kamstra committed
25 26
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, 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
(eval-when-compile
  (require 'skeleton)
198
  (require 'cl)
Dave Love's avatar
Dave Love committed
199
  (require 'comint))
200 201
(require 'executable)

202 203 204
(defvar font-lock-comment-face)
(defvar font-lock-set-defaults)
(defvar font-lock-string-face)
205 206


207
(defgroup sh nil
208
  "Shell programming utilities."
209 210 211 212
  :group 'unix
  :group 'languages)

(defgroup sh-script nil
213
  "Shell script mode."
214
  :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
215 216 217 218 219
  :group 'sh
  :prefix "sh-")


(defcustom sh-ancestor-alist
220 221
  '((ash . sh)
    (bash . jsh)
222
    (bash2 . jsh)
223 224 225 226 227 228 229 230 231 232 233 234 235
    (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)
236 237
    (zsh . ksh88)
    (rpm . sh))
238 239 240 241 242 243
  "*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
244 245
  tcsh		Turbo C Shell
    itcsh	? Turbo C Shell
246 247 248 249 250 251 252 253 254 255 256 257 258 259
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
260 261 262
  wsh		? Shell"
  :type '(repeat (cons symbol symbol))
  :group 'sh-script)
263 264


265
(defcustom sh-alias-alist
266
  (append (if (eq system-type 'gnu/linux)
267
	     '((csh . tcsh)
268
	       (ksh . pdksh)))
269 270
	 ;; for the time being
	 '((ksh . ksh88)
271
           (bash2 . bash)
272 273 274
	   (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
275 276 277
shell it really is."
  :type '(repeat (cons symbol symbol))
  :group 'sh-script)
278 279


280
(defcustom sh-shell-file
281
  (or
282 283
   ;; On MSDOS and Windows, collapse $SHELL to lower-case and remove
   ;; the executable extension, so comparisons with the list of
284
   ;; known shells work.
285
   (and (memq system-type '(ms-dos windows-nt))
286 287 288 289 290 291 292
	(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)
293 294
			  '("command.com" "cmd.exe" "4dos.com" "ndos.com"
			    "cmdproxy.exe")))
295 296
	      "/bin/sh"
	    (file-name-sans-extension (downcase shell)))))
297 298
   (getenv "SHELL")
   "/bin/sh")
299 300 301
  "*The executable file name for the shell being programmed."
  :type 'string
  :group 'sh-script)
302 303


304
(defcustom sh-shell-arg
305
  ;; bash does not need any options when run in a shell script,
306
  '((bash)
307 308
    (csh . "-f")
    (pdksh)
309
    ;; Bill_Mann@praxisint.com says -p with ksh can do harm.
310
    (ksh88)
311
    ;; -p means don't initialize functions from the environment.
312
    (rc . "-p")
313 314 315 316
    ;; Someone proposed -motif, but we don't want to encourage
    ;; use of a non-free widget set.
    (wksh)
    ;; -f means don't run .zshrc.
317
    (zsh . "-f"))
318 319 320 321
  "*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")
322
			       (sexp :format "Evaluate: %v"))))
323
  :group 'sh-script)
324

325
(defcustom sh-imenu-generic-expression
326 327
  `((sh
     . ((nil "^\\s-*\\(function\\s-+\\)?\\([A-Za-z_][A-Za-z_0-9]+\\)\\s-*()" 2))))
328 329 330 331 332 333 334 335
  "*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
336
  :group 'sh-script
337
  :version "20.4")
338

339 340 341 342 343
(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.")
344

345 346 347
(defvar sh-shell-variables-initialized nil
  "Non-nil if `sh-shell-variables' is initialized.")

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

357 358
(defvar sh-shell (sh-canonicalize-shell (file-name-nondirectory sh-shell-file))
  "The shell being programmed.  This is set by \\[sh-set-shell].")
Dan Nicolaescu's avatar
Dan Nicolaescu committed
359
;;;###autoload(put 'sh-shell 'safe-local-variable 'symbolp)
360

361 362 363 364 365
(defvar sh-mode-abbrev-table nil)

(define-abbrev-table 'sh-mode-abbrev-table ())


366 367 368
;; I turned off this feature because it doesn't permit typing commands
;; in the usual way without help.
;;(defvar sh-abbrevs
369
;;  '((csh sh-abbrevs shell
370 371 372
;;	 "switch" 'sh-case
;;	 "getopts" 'sh-while-getopts)

373
;;    (es sh-abbrevs shell
374 375
;;	"function" 'sh-function)

376
;;    (ksh88 sh-abbrevs sh
377 378
;;	   "select" 'sh-select)

379
;;    (rc sh-abbrevs shell
380 381 382
;;	"case" 'sh-case
;;	"function" 'sh-function)

383
;;    (sh sh-abbrevs shell
384 385 386 387 388 389 390 391 392 393 394 395
;;	"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)

396
;;    (zsh sh-abbrevs ksh88
397 398
;;	 "repeat" 'sh-repeat))
;;  "Abbrev-table used in Shell-Script mode.  See `sh-feature'.
399 400
;;;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
401 402 403



404 405 406 407 408 409 410
(defun sh-mode-syntax-table (table &rest list)
  "Copy TABLE and set syntax for successive CHARs according to strings S."
  (setq table (copy-syntax-table table))
  (while list
    (modify-syntax-entry (pop list) (pop list) table))
  table)

411 412 413 414
(defvar sh-mode-syntax-table nil
  "The syntax table to use for Shell-Script mode.
This is buffer-local in every such buffer.")

415 416
(defvar sh-mode-default-syntax-table
  (sh-mode-syntax-table ()
417 418 419 420 421
	?\# "<"
	?\n ">#"
	?\" "\"\""
	?\' "\"'"
	?\` "\"`"
422 423 424 425
	;; ?$ might also have a ". p" syntax. Both "'" and ". p" seem
	;; to work fine. This is needed so that dabbrev-expand
	;; $VARNAME works.
	?$ "'"
426 427 428 429 430 431
	?! "_"
	?% "_"
	?: "_"
	?. "_"
	?^ "_"
	?~ "_"
432
	?, "_"
433
	?= "."
434 435
	?< "."
	?> ".")
436
  "Default syntax table for shell mode.")
437

438 439
(defvar sh-mode-syntax-table-input
  '((sh . nil))
440
  "Syntax-table used in Shell-Script mode.  See `sh-feature'.")
Richard M. Stallman's avatar
Richard M. Stallman committed
441 442

(defvar sh-mode-map
443 444
  (let ((map (make-sparse-keymap))
	(menu-map (make-sparse-keymap "Insert")))
Richard M. Stallman's avatar
Richard M. Stallman committed
445 446 447
    (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)
448
    (define-key map "\C-c\C-t" 'sh-tmp-file)
Richard M. Stallman's avatar
Richard M. Stallman committed
449
    (define-key map "\C-c\C-s" 'sh-select)
450 451
    (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
452 453 454 455
    (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)
456 457 458 459
    (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)
460
    (define-key map "\C-c\C-\\" 'sh-backslash-region)
461

Richard M. Stallman's avatar
Richard M. Stallman committed
462 463
    (define-key map "=" 'sh-assignment)
    (define-key map "\C-c+" 'sh-add)
464 465
    (define-key map "\C-\M-x" 'sh-execute-region)
    (define-key map "\C-c\C-x" 'executable-interpret)
466
    (define-key map "<" 'sh-maybe-here-document)
467 468 469 470 471 472
    (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
473

474 475 476 477
    (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
478
    (define-key map "\C-c:" 'sh-set-shell)
479 480
    (define-key map [remap backward-sentence] 'sh-beginning-of-command)
    (define-key map [remap forward-sentence] 'sh-end-of-command)
481 482 483 484 485 486
    (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))
487 488
    (define-key menu-map [sh-getopts]	'("Options Loop" . sh-while-getopts))
    (define-key menu-map [sh-indexed-loop] '("Indexed Loop" . sh-indexed-loop))
489 490 491
    (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
492 493 494
    map)
  "Keymap used in Shell-Script mode.")

495 496 497 498
(defvar sh-skeleton-pair-default-alist '((?( _ ?)) (?\))
				      (?[ ?\s _ ?\s ?]) (?\])
				      (?{ _ ?}) (?\}))
  "Value to use for `skeleton-pair-default-alist' in Shell-Script mode.")
Richard M. Stallman's avatar
Richard M. Stallman committed
499

500
(defcustom sh-dynamic-complete-functions
501 502 503
  '(shell-dynamic-complete-environment-variable
    shell-dynamic-complete-command
    comint-dynamic-complete-filename)
504 505 506
  "*Functions for doing TAB dynamic completion."
  :type '(repeat function)
  :group 'sh-script)
Richard M. Stallman's avatar
Richard M. Stallman committed
507 508


509
(defcustom sh-require-final-newline
510
  '((csh . t)
511
    (pdksh . t))
512
  "*Value of `require-final-newline' in Shell-Script mode buffers.
513
\(SHELL . t) means use the value of `mode-require-final-newline' for SHELL.
514 515 516
See `sh-feature'."
  :type '(repeat (cons (symbol :tag "Shell")
		       (choice (const :tag "require" t)
517
			       (sexp :format "Evaluate: %v"))))
518
  :group 'sh-script)
Richard M. Stallman's avatar
Richard M. Stallman committed
519 520


521
(defcustom sh-assignment-regexp
522 523 524 525 526 527 528
  '((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 `='
529 530 531
sign.  See `sh-feature'."
  :type '(repeat (cons (symbol :tag "Shell")
		       (choice regexp
532
			       (sexp :format "Evaluate: %v"))))
533
  :group 'sh-script)
Richard M. Stallman's avatar
Richard M. Stallman committed
534 535


536 537 538 539
(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
540 541


542 543 544 545
(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
546 547


548
(defvar sh-header-marker nil
549
  "When non-nil is the end of header for prepending by \\[sh-execute-region].
550 551 552
That command is also used for setting this variable.")


553
(defcustom sh-beginning-of-command
554
  "\\([;({`|&]\\|\\`\\|[^\\]\n\\)[ \t]*\\([/~a-zA-Z0-9:]\\)"
Richard M. Stallman's avatar
Richard M. Stallman committed
555
  "*Regexp to determine the beginning of a shell command.
556 557 558
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
559

560

561
(defcustom sh-end-of-command
562
  "\\([/~a-zA-Z0-9:]\\)[ \t]*\\([;#)}`|&]\\|$\\)"
Richard M. Stallman's avatar
Richard M. Stallman committed
563
  "*Regexp to determine the end of a shell command.
564 565 566
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
567 568 569



570
(defcustom sh-here-document-word "EOF"
571
  "Word to delimit here documents.
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
If the first character of this string is \"-\", this is taken as
part of the redirection operator, rather than part of the
word (that is, \"<<-\" instead of \"<<\").  This is a feature
used by some shells (for example Bash) to indicate that leading
tabs inside the here document should be ignored.  In this case,
Emacs indents the initial body and end of the here document with
tabs, to the same level as the start (note that apart from this
there is no support for indentation of here documents).  This
will only work correctly if `sh-basic-offset' is a multiple of
`tab-width'.

Any quote characters or leading whitespace in the word are
removed when closing the here document."
  :type 'string
  :group 'sh-script)

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

Karl Heuer's avatar
Karl Heuer committed
589 590 591 592 593
(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
594

595 596 597
;; customized this out of sheer bravado.  not for the faint of heart.
;; but it *did* have an asterisk in the docstring!
(defcustom sh-builtins
598
  '((bash sh-append posix
599
	  "." "alias" "bg" "bind" "builtin" "caller" "compgen" "complete"
600
          "declare" "dirs" "disown" "enable" "fc" "fg" "help" "history"
601 602
          "jobs" "kill" "let" "local" "popd" "printf" "pushd" "shopt"
          "source" "suspend" "typeset" "unalias")
Richard M. Stallman's avatar
Richard M. Stallman committed
603

604
    ;; The next entry is only used for defining the others
605
    (bourne sh-append shell
606 607
	    "eval" "export" "getopts" "newgrp" "pwd" "read" "readonly"
	    "times" "ulimit")
Richard M. Stallman's avatar
Richard M. Stallman committed
608

609
    (csh sh-append shell
610 611 612
	 "alias" "chdir" "glob" "history" "limit" "nice" "nohup" "rehash"
	 "setenv" "source" "time" "unalias" "unhash")

613
    (dtksh sh-append wksh)
Richard M. Stallman's avatar
Richard M. Stallman committed
614

615 616
    (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
617

618
    (jsh sh-append sh
619
	 "bg" "fg" "jobs" "kill" "stop" "suspend")
Richard M. Stallman's avatar
Richard M. Stallman committed
620

621
    (jcsh sh-append csh
622
	  "bg" "fg" "jobs" "kill" "notify" "stop" "suspend")
623

624
    (ksh88 sh-append bourne
625 626
	   "alias" "bg" "false" "fc" "fg" "jobs" "kill" "let" "print" "time"
	   "typeset" "unalias" "whence")
627

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

634
    (pdksh sh-append ksh88
635 636
	   "bind")

637
    (posix sh-append sh
638 639
	   "command")

640 641
    (rc "builtin" "cd" "echo" "eval" "limit" "newpgrp" "shift" "umask" "wait"
	"whatis")
642

643
    (sh sh-append bourne
644 645 646
	"hash" "test" "type")

    ;; The next entry is only used for defining the others
647 648
    (shell "cd" "echo" "eval" "set" "shift" "umask" "unset" "wait")

649
    (wksh sh-append ksh88
650
	  "Xt[A-Z][A-Za-z]*")
651

652
    (zsh sh-append ksh88
653 654 655 656 657 658
	 "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"))
659 660
  "*List of all shell builtins for completing read and fontification.
Note that on some systems not all builtins are available or some are
661 662 663
implemented as aliases.  See `sh-feature'."
  :type '(repeat (cons (symbol :tag "Shell")
		       (choice (repeat string)
664
			       (sexp :format "Evaluate: %v"))))
665
  :group 'sh-script)
666 667


668

669
(defcustom sh-leading-keywords
670
  '((bash sh-append sh
671 672 673
          "time")

    (csh "else")
674 675 676 677 678

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

    (rc "else")

679
    (sh "!" "do" "elif" "else" "if" "then" "trap" "type" "until" "while"))
680 681 682
  "*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
683 684 685
flow of control or syntax.  See `sh-feature'."
  :type '(repeat (cons (symbol :tag "Shell")
		       (choice (repeat string)
686
			       (sexp :format "Evaluate: %v"))))
687
  :group 'sh-script)
688 689


690
(defcustom sh-other-keywords
691
  '((bash sh-append bourne
692
	  "bye" "logout" "select")
693 694

    ;; The next entry is only used for defining the others
695
    (bourne sh-append sh
696
	    "function")
697

698
    (csh sh-append shell
699 700
	 "breaksw" "default" "end" "endif" "endsw" "foreach" "goto"
	 "if" "logout" "onintr" "repeat" "switch" "then" "while")
701

702 703
    (es "break" "catch" "exec" "exit" "fn" "for" "forever" "fork" "if"
	"return" "throw" "while")
704

705
    (ksh88 sh-append bourne
706
	   "select")
707

708 709
    (rc "break" "case" "exec" "exit" "fn" "for" "if" "in" "return" "switch"
	"while")
710

711
    (sh sh-append shell
712 713
	"done" "esac" "fi" "for" "in" "return")

714 715
    ;; The next entry is only used for defining the others
    (shell "break" "case" "continue" "exec" "exit")
716

717
    (zsh sh-append bash
718 719
	 "select"))
  "*List of keywords not in `sh-leading-keywords'.
720 721 722
See `sh-feature'."
  :type '(repeat (cons (symbol :tag "Shell")
		       (choice (repeat string)
723
			       (sexp :format "Evaluate: %v"))))
724
  :group 'sh-script)
725 726 727 728



(defvar sh-variables
729
  '((bash sh-append sh
730 731 732 733 734 735 736 737 738 739 740 741 742 743
	  "allow_null_glob_expansion" "auto_resume" "BASH" "BASH_ENV"
	  "BASH_VERSINFO" "BASH_VERSION" "cdable_vars" "COMP_CWORD"
	  "COMP_LINE" "COMP_POINT" "COMP_WORDS" "COMPREPLY" "DIRSTACK"
	  "ENV" "EUID" "FCEDIT" "FIGNORE" "FUNCNAME"
	  "glob_dot_filenames" "GLOBIGNORE" "GROUPS" "histchars"
	  "HISTCMD" "HISTCONTROL" "HISTFILE" "HISTFILESIZE"
	  "HISTIGNORE" "history_control" "HISTSIZE"
	  "hostname_completion_file" "HOSTFILE" "HOSTTYPE" "IGNOREEOF"
	  "ignoreeof" "INPUTRC" "LINENO" "MACHTYPE" "MAIL_WARNING"
	  "noclobber" "nolinks" "notify" "no_exit_on_failed_exec"
	  "NO_PROMPT_VARS" "OLDPWD" "OPTERR" "OSTYPE" "PIPESTATUS"
	  "PPID" "POSIXLY_CORRECT" "PROMPT_COMMAND" "PS3" "PS4"
	  "pushd_silent" "PWD" "RANDOM" "REPLY" "SECONDS" "SHELLOPTS"
	  "SHLVL" "TIMEFORMAT" "TMOUT" "UID")
744

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

750
    (es sh-append shell
751 752 753
	"apid" "cdpath" "CDPATH" "history" "home" "ifs" "noexport" "path"
	"pid" "prompt" "signals")

754
    (jcsh sh-append csh
755
	  "notify")
756

757
    (ksh88 sh-append sh
758 759 760 761
	   "ENV" "ERRNO" "FCEDIT" "FPATH" "HISTFILE" "HISTSIZE" "LINENO"
	   "OLDPWD" "PPID" "PS3" "PS4" "PWD" "RANDOM" "REPLY" "SECONDS"
	   "TMOUT")

762
    (oash sh-append sh
763 764
	  "FIELD" "FIELD_MAX" "LAST_KEY" "OALIB" "PP_ITEM" "PP_NUM")

765
    (rc sh-append shell
766 767 768
	"apid" "apids" "cdpath" "CDPATH" "history" "home" "ifs" "path" "pid"
	"prompt" "status")

769
    (sh sh-append shell
770 771 772 773 774 775 776 777
	"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")

778
    (tcsh sh-append csh
779 780 781 782 783 784 785 786 787 788
	  "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")

789
    (zsh sh-append ksh88
790 791 792 793 794 795 796 797 798 799
	 "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'.")

800

801
;; Font-Lock support
802

803
(defface sh-heredoc
804 805 806 807
  '((((min-colors 88) (class color)
      (background dark))
     (:foreground "yellow1" :weight bold))
    (((class color)
808
      (background dark))
809
     (:foreground "yellow" :weight bold))
810 811 812 813
    (((class color)
      (background light))
     (:foreground "tan" ))
    (t
814
     (:weight bold)))
815 816
  "Face to show a here-document"
  :group 'sh-indentation)
817 818 819 820 821 822 823 824 825 826 827 828

;; These colours are probably icky.  It's just a placeholder though.
(defface sh-quoted-exec
  '((((class color) (background dark))
     (:foreground "salmon"))
    (((class color) (background light))
     (:foreground "magenta"))
    (t
     (:weight bold)))
  "Face to show quoted execs like ``"
  :group 'sh-indentation)

829 830 831
;; backward-compatibility alias
(put 'sh-heredoc-face 'face-alias 'sh-heredoc)
(defvar sh-heredoc-face 'sh-heredoc)
832

833 834 835 836
(defface sh-escaped-newline '((t :inherit font-lock-string-face))
  "Face used for (non-escaped) backslash at end of a line in Shell-script mode."
  :group 'sh-script
  :version "22.1")
837

838
(defvar sh-font-lock-keywords-var
839
  '((csh sh-append shell
840 841
	 ("\\${?[#?]?\\([A-Za-z_][A-Za-z0-9_]*\\|0\\)" 1
          font-lock-variable-name-face))
842

843
    (es sh-append executable-font-lock-keywords
844 845
	("\\$#?\\([A-Za-z_][A-Za-z0-9_]*\\|[0-9]+\\)" 1
         font-lock-variable-name-face))
846

847
    (rc sh-append es)
848
    (bash sh-append shell ("\\$(\\(\\sw+\\)" (1 'sh-quoted-exec t) ))
849
    (sh sh-append shell
850
	;; Variable names.
851
	("\\$\\({#?\\)?\\([A-Za-z_][A-Za-z0-9_]*\\|[-#?@!]\\)" 2
852 853
	  font-lock-variable-name-face)
	;; Function names.
854 855
	("^\\(\\sw+\\)[ \t]*(" 1 font-lock-function-name-face)
	("\\<\\(function\\)\\>[ \t]*\\(\\sw+\\)?"
856 857 858
	  (1 font-lock-keyword-face) (2 font-lock-function-name-face nil t))
	("\\(?:^\\s *\\|[[();&|]\\s *\\|\\(?:\\s +-[ao]\\|if\\|else\\|then\\|while\\|do\\)\\s +\\)\\(!\\)"
	 1 font-lock-negation-char-face))
859 860

    ;; The next entry is only used for defining the others
861
    (shell
862
           ;; Using font-lock-string-face here confuses sh-get-indent-info.
863
           ("\\(^\\|[^\\]\\)\\(\\\\\\\\\\)*\\(\\\\\\)$" 3 'sh-escaped-newline)
864 865
	   ("\\\\[^A-Za-z0-9]" 0 font-lock-string-face)
	   ("\\${?\\([A-Za-z_][A-Za-z0-9_]*\\|[0-9]+\\|[$*_]\\)" 1
866
	     font-lock-variable-name-face))
867
    (rpm sh-append rpm2
868
	 ("%{?\\(\\sw+\\)"  1 font-lock-keyword-face))