0
|
1 ;; ksh-mode.el --- sh (ksh, bash) script editing mode for GNU Emacs.
|
|
2
|
2
|
3 ;; Copyright (C) 1992-96 Gary Ellison.
|
0
|
4
|
|
5 ;; This file is part of XEmacs.
|
|
6
|
|
7 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
8 ;; under the terms of the GNU General Public License as published by
|
|
9 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
10 ;; any later version.
|
|
11
|
|
12 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
13 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
15 ;; General Public License for more details.
|
|
16
|
|
17 ;; You should have received a copy of the GNU General Public License
|
|
18 ;; along with XEmacs; see the file COPYING. If not, write to the Free
|
|
19 ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
20
|
2
|
21 ;; $Source: /afs/informatik.uni-tuebingen.de/local/web/xemacs/xemacs-cvs/XEmacs/xemacs-19/lisp/modes/ksh-mode.el,v $ --
|
|
22 ;;
|
0
|
23 ;; LCD Archive Entry:
|
2
|
24 ;; ksh-mode|Gary F. Ellison|Gary.F.Ellison@ATT.COM
|
|
25 ;; |Mode for editing sh/ksh/bash scripts
|
|
26 ;; |$Date: 1996/12/18 03:44:42 $|$Revision: 1.1.1.2 $|~/modes/ksh-mode.el.Z|
|
0
|
27
|
|
28 ;; Author: Gary F. Ellison <Gary.F.Ellison@ATT.COM>
|
2
|
29 ;; AT&T Laboratories
|
0
|
30 ;; 6200 East Broad Street
|
|
31 ;; Columbus, Ohio 43213 USA
|
|
32 ;;
|
|
33 ;; Maintainer: Gary F. Ellison <Gary.F.Ellison@ATT.COM>
|
|
34 ;; Created: Fri Jun 19
|
2
|
35 ;; $Revision: 1.1.1.2 $
|
|
36 ;; Keywords: shell, korn, bourne, sh, ksh, bash
|
0
|
37 ;;
|
2
|
38 ;; Delta On $Date: 1996/12/18 03:44:42 $
|
0
|
39 ;; Last Modified By: Gary Ellison
|
2
|
40 ;; Last Modified On: Mon Sep 11 12:26:47 1995
|
|
41 ;; Update Count : 35
|
0
|
42 ;; Status : Highly Functional
|
|
43 ;;
|
|
44
|
|
45 ;;; Commentary:
|
|
46
|
|
47 ;;
|
|
48 ;; Description:
|
|
49 ;; sh, ksh, and bash script editing commands for emacs.
|
|
50 ;;
|
|
51 ;; Installation:
|
|
52 ;; Put ksh-mode.el in some directory in your load-path.
|
|
53 ;; Refer to the installation section of ksh-mode's function definition.
|
|
54 ;;
|
|
55 ;; Usage:
|
|
56 ;; This major mode assists shell script writers with indentation
|
|
57 ;; control and control structure construct matching in much the same
|
|
58 ;; fashion as other programming language modes. Invoke describe-mode
|
|
59 ;; for more information.
|
|
60 ;;
|
|
61 ;; Bugs:
|
|
62 ;; When the ksh-align-to-keyword is non-nil and the nester
|
|
63 ;; is a multi-command expression with a compound command
|
|
64 ;; the lines following the compound end will align incorrectly
|
|
65 ;; to the compound command instead of it's current indentation.
|
|
66 ;; The fix will probably require the detection of syntax elements
|
|
67 ;; in the nesting line.
|
|
68 ;;
|
|
69 ;; Function ending brace "}" must be on a separate line for indent-line
|
|
70 ;; to do the right thing.
|
|
71 ;;
|
|
72 ;; Explicit function definition matching will proclaim in the minibuffer
|
|
73 ;; "No matching compound command" followed by "Matched ... "
|
|
74 ;;
|
|
75 ;; indent-for-comment fails to recognize a comment starting in column 0,
|
|
76 ;; hence it moves the comment-start in comment-column.
|
|
77
|
|
78 ;;; Code:
|
|
79
|
|
80 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
81 ;;
|
|
82 ;; HISTORY
|
2
|
83 ;; 6-Apr-96 Gary Ellison <gary.f.ellison@att.com>
|
|
84 ;; Depreciated font-lock-doc-string-face.
|
|
85 ;; Narly keywords inside strings bug fixed.
|
|
86 ;;
|
0
|
87 ;; 8-Aug-95 Jack Repenning <jackr@sgi.com>
|
|
88 ;; Fix documentation of `ksh-align-to-keyword' to conform to the 23
|
|
89 ;; Feb default change. Search for keywords obeying case, since the
|
|
90 ;; shell does.
|
|
91 ;;
|
|
92 ;; 23-Feb-1995 Gary Ellison
|
|
93 ;; Merged Jonathan Stigelman <Stig@hackvan.com> into 2.5 souce.
|
|
94 ;;
|
|
95 ;; 23 Feb 1995 Jonathan Stigelman <Stig@hackvan.com>
|
|
96 ;; Reshuffled documentation to make the format more consistant with other
|
|
97 ;; elisp. Added autoload and removed autoloading instructions from the
|
|
98 ;; ksh-mode docstring. Changed default value for `ksh-align-to-keyword'
|
|
99 ;; to nil because it doesn't work properly.
|
|
100 ;;
|
|
101 ;; 2-Aug-1994 Gary Ellison
|
|
102 ;; Last Modified: Mon Jun 13 16:52:55 1994 #29 (Gary Ellison)
|
|
103 ;; - Syntax table modifications to better support sexp navigation and
|
|
104 ;; parsing.
|
|
105 ;; - Fixed keyword regexps. Keywords were not being recoginized on the
|
|
106 ;; same line as " ' `.
|
|
107 ;;
|
|
108 ;; 13-Jun-1994 Gary Ellison
|
|
109 ;; Last Modified: Wed Mar 30 14:12:26 1994 #28 (Gary Ellison)
|
|
110 ;; - Minor excursion problem fixed in ksh-indent-command.
|
|
111 ;;
|
|
112 ;; 30-Mar-1994 Gary Ellison
|
|
113 ;; Last Modified: Fri Mar 25 15:42:29 1994 #25 (Gary Ellison)
|
|
114 ;; - Implement user customizable ksh-comment-regexp.
|
|
115 ;; - Make the keyword vs line indentation alignment customizable
|
|
116 ;; by calling ksh-align-to-keyword based on variable of same
|
|
117 ;; name. (If the code is obfuscated or convoluted I can attribute
|
|
118 ;; this to a severe head cold and not malice :)
|
|
119 ;;
|
|
120 ;; 25-Mar-1994 Gary Ellison
|
|
121 ;; Last Modified: Fri Feb 4 13:06:30 1994 #23 (Gary Ellison)
|
|
122 ;; - Nest relative to the line indentation not the keywords
|
|
123 ;; column.
|
|
124 ;;
|
|
125 ;; 4-Feb-1994 Gary Ellison
|
|
126 ;; Last Modified: Wed Nov 10 10:03:01 1993 #18 (Gary Ellison)
|
|
127 ;; - Add direct support for font-lock-mode. Thanks Espen Skoglund
|
|
128 ;; for the regular expressions.
|
|
129 ;;
|
|
130 ;; 10-Nov-1993 Gary Ellison
|
|
131 ;; Last Modified: Tue Oct 12 15:23:06 1993 #17 (Gary Ellison)
|
|
132 ;; Fix message on ksh-match-and-tell to not get invalid format
|
|
133 ;; when a % appears in the string.
|
|
134 ;;
|
|
135 ;; 12-Oct-1993 Espen Skoglund <espensk@stud.cs.uit.no>.
|
|
136 ;; Last Modified: Tue Oct 12 15:03:01 1993 #16 (Gary Ellison)
|
|
137 ;; Apply Line continuation patch supplied by Espen Skoglund
|
|
138 ;;
|
|
139 ;; 1-Sep-1993 Gary Ellison
|
|
140 ;; Last Modified: Tue Aug 17 17:18:18 1993 #14 (Gary Ellison)
|
|
141 ;; Get rid of this-line hack in ksh-get-nester-column.
|
|
142 ;;
|
|
143 ;; 17-Aug-1993 Gary Ellison
|
|
144 ;; Last Modified: Mon Jun 21 14:00:43 1993 #13 (Gary Ellison)
|
|
145 ;; Code uses builtin current-indentation instead of lisp defun
|
|
146 ;; ksh-indentation-on-this-line (thanks to Tom Tromey).
|
|
147 ;; More and better doc strings.
|
|
148 ;;
|
|
149 ;; 5-Aug-1993 Tom Tromey <tromey@cns.caltech.edu>
|
|
150 ;; Last Modified: Thu Aug 5 11:09:12 1993 #12 (Tom Tromey)
|
|
151 ;; ksh-indent-region skips blank lines. Uses let binding instead
|
|
152 ;; of setq. No longer marks buffer modified if indentation
|
|
153 ;; doesn't change.
|
|
154 ;;
|
|
155 ;; 21-Jun-1993 Gary Ellison
|
|
156 ;; Last Modified: Mon Mar 29 15:05:34 1993 #11 (Gary Ellison)
|
|
157 ;; Use make-local-variables instead of make-variables-buffer-local
|
|
158 ;; ksh-indent now supports nil (keyword aligned) or number (offset)
|
|
159 ;; Support ksh-tab-always-indent feature
|
|
160 ;; Variables offsetting indentation renamed to better reflect their
|
|
161 ;; role.
|
|
162 ;; Integrate keyword completion feature supplied by
|
|
163 ;; Haavard Rue <hrue@imf.unit.no>.
|
|
164 ;;
|
|
165 ;; 29-Mar-1993 Gary Ellison
|
|
166 ;; Last Modified: Tue Sep 29 16:14:02 1992 #10 (Gary Ellison)
|
|
167 ;; Integrate line continuation patch supplied by
|
|
168 ;; Haavard Rue <hrue@imf.unit.no>
|
|
169 ;; Name back to ksh-mode to avoid confusion with sh-mode
|
|
170 ;; by Thomas W. Strong, Jr. <strong+@cmu.edu>.
|
|
171 ;;
|
|
172 ;; 29-Sep-1992 Gary Ellison
|
|
173 ;; Last Modified: Wed Sep 2 08:51:40 1992 #9 (Gary Ellison)
|
|
174 ;; Full support of ksh88 case items.
|
|
175 ;; Align statements under "do" and "then" keywords one position
|
|
176 ;; past the keyword.
|
|
177 ;;
|
|
178 ;; 2-Sep-1992 Gary Ellison
|
|
179 ;; Last Modified: Tue Aug 4 14:34:35 1992 #8 (Gary Ellison)
|
|
180 ;; Use make-variable-buffer-local instead of make-local-variable
|
|
181 ;; Get rid of superflous ksh-default variables.
|
|
182 ;; Use end of word match \b for "then", "do", "else", "elif"
|
|
183 ;; Support process substitution lists and exclude ksh 88 case items
|
|
184 ;; Use default-tab-width for indentation defaults.
|
|
185 ;; Moved installation instructions to the mode level documentation
|
|
186 ;; section.
|
|
187 ;; Fixed auto-mode-alist documentation.
|
|
188 ;;
|
|
189 ;; 24-Jul-1992 Gary Ellison
|
|
190 ;; Last Modified: Fri Jul 24 09:45:11 1992 #7 (Gary Ellison)
|
|
191 ;; Modified ksh-indent-region to use marker versus fixed end point.
|
|
192 ;; comment-start-skip regexp no longer fooled by parameter substitution.
|
|
193 ;; Added constant ksh-mode-version.
|
|
194 ;;
|
|
195 ;; 21-Jul-1992 Gary Ellison
|
|
196 ;; Last Modified: Tue Jul 21 15:53:57 1992 #6 (Gary Ellison)
|
|
197 ;; Indent with tabs instead of spaces.
|
|
198 ;; Can handle just about all styles.
|
|
199 ;; Anti-newline in REs.
|
|
200 ;; Word delim "\b" in REs
|
|
201 ;; More syntax entries.
|
|
202 ;; Variables with regexp suffix abbreviated to re
|
|
203 ;; Better } handling
|
|
204 ;; Implemented minimal indent-region-function
|
|
205 ;; Mode documentation corrected.
|
|
206 ;; Minor lisp source format changes.
|
|
207 ;;
|
|
208 ;; 29-Jun-1992 Gary Ellison
|
|
209 ;; Last Modified: Mon Jun 29 15:39:35 1992 #5 (Gary Ellison)
|
|
210 ;; Optimize line-to-string
|
|
211 ;; Implicit/Explicit functions aok
|
|
212 ;; More indentation variables
|
|
213 ;; Superfluous defun killed.
|
|
214 ;; renamed to sh-mode
|
|
215 ;;
|
|
216 ;; 22-Jun-1992 Gary Ellison
|
|
217 ;; Last Modified: Mon Jun 22 15:01:14 1992 #4 (Gary Ellison)
|
|
218 ;; Cleanup pre att.emacs posting
|
|
219 ;;
|
|
220 ;; 19-Jun-1992 Gary Ellison
|
|
221 ;; Last Modified: Fri Jun 19 17:19:14 1992 #3 (Gary Ellison)
|
|
222 ;; Minimal case indent handling
|
|
223 ;;
|
|
224 ;; 19-Jun-1992 Gary Ellison
|
|
225 ;; Last Modified: Fri Jun 19 16:23:26 1992 #2 (Gary Ellison)
|
|
226 ;; Nesting handled except for case statement
|
|
227 ;;
|
|
228 ;; 19-Jun-1992 Gary Ellison
|
|
229 ;; Last Modified: Fri Jun 19 10:03:07 1992 #1 (Gary Ellison)
|
|
230 ;; Conception of this mode.
|
|
231 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
232
|
2
|
233 (defconst ksh-mode-version "$Revision: 1.1.1.2 $"
|
0
|
234 "*Version numbers of this version of ksh-mode")
|
|
235
|
|
236 ;;
|
|
237 ;; Variables controlling indentation style
|
|
238 ;;
|
|
239
|
|
240 (defvar ksh-indent 2
|
|
241 ;; perhaps c-basic-offset would be okay to use as a default, but using
|
|
242 ;; default-tab-width as the default is ridiculous --Stig
|
|
243 "*Indentation of ksh statements with respect to containing block. A value
|
|
244 of nil indicates compound list keyword \(\"do\" and \"then\"\) alignment.")
|
|
245 (defvar ksh-case-item-offset ksh-indent
|
|
246 "*Additional indentation for case items within a case statement.")
|
|
247 (defvar ksh-case-indent nil
|
|
248 "*Additional indentation for statements under case items.")
|
|
249 (defvar ksh-group-offset (- ksh-indent)
|
|
250 "*Additional indentation for keywords \"do\" and \"then\".")
|
|
251 (defvar ksh-brace-offset 0
|
|
252 "*Additional indentation of \"{\" under functions or brace groupings.")
|
|
253 (defvar ksh-multiline-offset 1
|
|
254 "*Additional indentation of line that is preceded of a line ending with a
|
|
255 \\ to make it continue on next line.")
|
|
256 (defvar ksh-match-and-tell t
|
|
257 "*If non-nil echo in the minibuffer the matching compound command
|
|
258 for the \"done\", \"}\", \"fi\", or \"esac\". ")
|
|
259 (defvar ksh-tab-always-indent t
|
|
260 "*Controls the operation of the TAB key. If t (the default), always
|
|
261 reindent the current line. If nil, indent the current line only if
|
|
262 point is at the left margin or in the line's indentation; otherwise
|
|
263 insert a tab.")
|
|
264
|
|
265 (defvar ksh-align-to-keyword nil
|
|
266 ;; #### - this is broken, so it should be disabled by default --Stig
|
|
267 "*Controls whether nested constructs align from the keyword or
|
|
268 the current indentation. If non-nil, indentation will be relative to
|
|
269 the column the keyword starts. If nil, indentation will be relative to
|
|
270 the current indentation of the line the keyword is on.
|
2
|
271 The default value is non-nil. The non-nil case doesn't work very well.")
|
0
|
272
|
|
273 (defvar ksh-comment-regexp "^\\s *#"
|
|
274 "*Regular expression used to recognize comments. Customize to support
|
|
275 ksh-like languages.")
|
|
276
|
|
277 (defun ksh-current-indentation ()
|
|
278 nil
|
|
279 )
|
|
280 ;;
|
|
281 (fset 'ksh-current-indentation 'current-column)
|
|
282 ;;
|
|
283 ;; Variables controlling completion
|
|
284 (defvar ksh-completion-list '())
|
|
285 (make-variable-buffer-local 'ksh-completion-list)
|
|
286 (set-default 'ksh-completion-list '())
|
|
287
|
|
288 ;;
|
|
289 ;; -type- : type number, 0:misc, 1:variable, 2:function
|
|
290 ;; -regexp-: regexp used to parse the script
|
|
291 ;; -match- : used by match-beginning/end to pickup target
|
|
292 ;;
|
|
293 (defvar ksh-completion-type-misc 0)
|
|
294 (defvar ksh-completion-regexp-var "\\([A-Za-z_0-9]+\\)=")
|
|
295 (defvar ksh-completion-type-var 1)
|
|
296 (defvar ksh-completion-match-var 1)
|
|
297 (defvar ksh-completion-regexp-var2 "\\$\\({\\|{#\\)?\\([A-Za-z_0-9]+\\)[#%:}]?")
|
|
298 (defvar ksh-completion-match-var2 2)
|
|
299 (defvar ksh-completion-regexp-function
|
|
300 "\\(function\\)?[ \t]*\\([A-Za-z_0-9]+\\)[ \t]*([ \t]*)")
|
|
301 (defvar ksh-completion-type-function 2)
|
|
302 (defvar ksh-completion-match-function 2)
|
|
303
|
|
304 ;;
|
|
305 ;; Variable controlling fontification
|
|
306 ;;
|
|
307 (defvar ksh-keywords '("for" "in" "do" "done" "select" "case" "esac" "if"
|
|
308 "then" "elif" "else" "fi" "while" "until" "function" "time"
|
2
|
309 "alias" "bg" "break" "continue" "cd" "exit" "echo" "fc" "fg" "getopts" "jobs"
|
|
310 "kill" "let" "newgrp" "print" "pwd" "read" "readonly" "return" "set" "shift"
|
|
311 "test" "times" "trap" "typeset" "ulimit" "umask" "unalias" "unset" "wait" "whence"))
|
0
|
312
|
|
313 ;; '("\\<function[ \t]+\\([^(; \t]+\\)" 1 font-lock-function-name-face)
|
|
314 (defconst ksh-font-lock-keywords
|
|
315 (list
|
|
316 ;; Fontify [[ ]] expressions
|
2
|
317 '("\\(\\[.*\\]\\)" 1 font-lock-string-face t)
|
0
|
318 ;; Fontify keywords
|
|
319 (cons (concat
|
|
320 "\\(\\<"
|
|
321 (mapconcat 'identity ksh-keywords "\\>\\|\\<")
|
|
322 "\\>\\)")
|
|
323 1)
|
|
324 ;; Fontify function names
|
|
325 '("\\<function[ \t]+\\([^(; \t]+\\)" 1 font-lock-function-name-face)
|
|
326 '("\\(^[ \t]*[A-Za-z_][A-Za-z_0-9]*[ \t]*()\\)" 1 font-lock-function-name-face)
|
|
327 ))
|
|
328
|
2
|
329 ;; XEmacs addition
|
0
|
330 (put 'ksh-mode 'font-lock-keywords 'ksh-font-lock-keywords)
|
|
331
|
|
332 ;;
|
|
333 ;; Context/indentation regular expressions
|
|
334 ;;
|
|
335 ;; indenting expressions
|
|
336 ;;
|
2
|
337 ;(defconst ksh-then-do-re "^[^#\n]*\\s\"*\\b\\(then\\|do\\)\\b"
|
|
338 (defconst ksh-then-do-re "\\s *\\b\\(then\\|do\\)\\b"
|
0
|
339 "*Regexp used to locate grouping keywords: \"then\" and \"do\"" )
|
|
340
|
2
|
341 (defconst ksh-do-re "\\s *\\bdo\\(\\b\\|$\\)"
|
0
|
342 "*Regexp used to match keyword: do")
|
|
343
|
2
|
344 (defconst ksh-then-re "\\s *\\bthen\\(\\b\\|$\\)"
|
0
|
345 "*Regexp used to match keyword: then")
|
|
346
|
|
347 ;;
|
|
348 ;; Structure starting/indenting keywords
|
|
349 ;;
|
2
|
350 (defconst ksh-else-re "\\s *\\belse\\(\\b\\|$\\)"
|
0
|
351 "*Regexp used to match keyword: else")
|
|
352
|
2
|
353 (defconst ksh-elif-re "\\s *\\belif\\(\\b\\|$\\)"
|
0
|
354 "*Regexp used to match keyword: elif")
|
|
355
|
2
|
356 (defconst ksh-brace-re "\\S>*{[ \t\n]"
|
0
|
357 "*Regexp used to match syntactic entity: { ")
|
|
358
|
2
|
359 (defconst ksh-case-item-end-re "\\S>*;;[ \t\n]"
|
0
|
360 "*Regexp used to match case item end syntactic entity: ;;")
|
|
361
|
|
362 (defconst ksh-keywords-re
|
2
|
363 "\\s *\\b\\(else\\|if\\|elif\\|case\\|while\\|for\\|until\\|select\\)\\b"
|
0
|
364 "*Regexp used to detect compound command keywords: if, else, elif case,
|
|
365 while, for, until, and select")
|
|
366
|
|
367
|
2
|
368 (defconst ksh-if-re "\\s *\\b\\(if\\)\\b"
|
0
|
369 "*Regexp used to match keyword: if")
|
|
370
|
|
371 (defconst ksh-iteration-keywords-re
|
2
|
372 "\\s *\\b\\(while\\|for\\|until\\|select\\)\\b"
|
0
|
373 "*Match one of the keywords: while, until, for, select")
|
|
374
|
2
|
375 (defconst ksh-case-re "\\s *\\bcase\\b"
|
0
|
376 "*Regexp used to match keyword: case")
|
|
377
|
|
378 (defconst ksh-explicit-func-re
|
|
379 "^\\s *\\(function\\s [a-zA-z_][a-zA-Z0-1_]*\\)\\b"
|
|
380 "*Match an explicit function definition: function name")
|
|
381
|
|
382 (defconst ksh-implicit-func-re
|
|
383 "^\\s *\\([a-zA-z_][a-zA-Z0-1_]*\\)\\s *()\\s *"
|
|
384 "*Match an implicit function definition: name ()")
|
|
385
|
|
386 (defconst ksh-func-brace-re "^\\s *\\(.*{\\)[ \t\n]+"
|
|
387 "*Match a implicit function definition brace: name { ")
|
|
388
|
|
389 ;;
|
|
390 ;; indenting
|
|
391 (defconst ksh-case-item-re "^[^#\n]*\\s\"*\\()\\)"
|
|
392 "*Regexp used to match case-items including ksh88")
|
|
393
|
|
394 (defconst ksh-paren-re "^[^#\n]*\\s\"*)[ \t\n]+"
|
|
395 "*Regexp used to match compound list & case items")
|
|
396
|
|
397 ;;
|
|
398 ;; structure ending keyword regular expressions
|
2
|
399 (defconst ksh-fi-re "\\s *\\bfi\\b"
|
0
|
400 "*Regexp used to match keyword: fi")
|
|
401
|
2
|
402 (defconst ksh-esac-re "\\s *\\besac\\b"
|
0
|
403 "*Regexp used to match keyword: esac")
|
|
404
|
2
|
405 (defconst ksh-done-re "\\s *\\bdone\\b"
|
0
|
406 "*Regexp used to match keyword: done")
|
|
407
|
2
|
408 (defconst ksh-brace-end-re "\\s *}\\s *"
|
0
|
409 "*Regexp used to match function brace-groups")
|
|
410
|
|
411 (defconst ksh-multiline-re "^.*\\\\$"
|
|
412 "*Regexp used to match a line with a statement using more lines.")
|
|
413
|
|
414 ;;
|
|
415 ;;
|
|
416 ;; Create mode specific tables
|
|
417 (defvar ksh-mode-syntax-table nil
|
|
418 "Syntax table used while in ksh mode.")
|
|
419 (if ksh-mode-syntax-table
|
|
420 ()
|
2
|
421 (setq ksh-mode-syntax-table (make-syntax-table (standard-syntax-table)))
|
|
422 (modify-syntax-entry ?\( "." ksh-mode-syntax-table)
|
|
423 (modify-syntax-entry ?\) "." ksh-mode-syntax-table)
|
|
424 (modify-syntax-entry ?{ "." ksh-mode-syntax-table)
|
|
425 (modify-syntax-entry ?} "." ksh-mode-syntax-table)
|
|
426 (modify-syntax-entry ?\[ "(]" ksh-mode-syntax-table)
|
|
427 (modify-syntax-entry ?\] ")[" ksh-mode-syntax-table)
|
0
|
428 (modify-syntax-entry ?\' "\"" ksh-mode-syntax-table)
|
|
429 (modify-syntax-entry ?` "\"" ksh-mode-syntax-table)
|
|
430 (modify-syntax-entry ?\n ">" ksh-mode-syntax-table)
|
|
431 (modify-syntax-entry ?\f ">" ksh-mode-syntax-table)
|
|
432 (modify-syntax-entry ?# "<" ksh-mode-syntax-table)
|
|
433 (modify-syntax-entry ?_ "w" ksh-mode-syntax-table)
|
|
434 (modify-syntax-entry ?< "." ksh-mode-syntax-table)
|
|
435 (modify-syntax-entry ?> "." ksh-mode-syntax-table)
|
|
436 (modify-syntax-entry ?& "." ksh-mode-syntax-table)
|
|
437 (modify-syntax-entry ?| "." ksh-mode-syntax-table)
|
2
|
438 (modify-syntax-entry ?$ "\\" ksh-mode-syntax-table)
|
0
|
439 (modify-syntax-entry ?% "." ksh-mode-syntax-table)
|
|
440 (modify-syntax-entry ?= "." ksh-mode-syntax-table)
|
|
441 (modify-syntax-entry ?/ "." ksh-mode-syntax-table)
|
|
442 (modify-syntax-entry ?+ "." ksh-mode-syntax-table)
|
|
443 (modify-syntax-entry ?* "." ksh-mode-syntax-table)
|
|
444 (modify-syntax-entry ?- "." ksh-mode-syntax-table)
|
|
445 (modify-syntax-entry ?\; "." ksh-mode-syntax-table)
|
2
|
446 (modify-syntax-entry ?: "." ksh-mode-syntax-table)
|
0
|
447 )
|
|
448
|
|
449 (defvar ksh-mode-abbrev-table nil
|
|
450 "Abbrev table used while in ksh mode.")
|
|
451 (define-abbrev-table 'ksh-mode-abbrev-table ())
|
|
452
|
|
453 (defvar ksh-mode-map nil
|
|
454 "Keymap used in ksh mode")
|
|
455
|
|
456 (if ksh-mode-map
|
|
457 ()
|
|
458 (setq ksh-mode-map (make-sparse-keymap))
|
|
459 (define-key ksh-mode-map "\t" 'ksh-indent-command)
|
|
460 ;; (define-key ksh-mode-map "\t" 'ksh-indent-line)
|
|
461 ;; (define-key ksh-mode-map "\177" 'backward-delete-char-untabify)
|
|
462 (define-key ksh-mode-map "\C-j" 'reindent-then-newline-and-indent)
|
|
463 (define-key ksh-mode-map "\e\t" 'ksh-complete-symbol)
|
|
464 (define-key ksh-mode-map "\C-c\t" 'ksh-completion-init-and-pickup)
|
|
465 )
|
|
466
|
|
467
|
|
468 ;;;###autoload
|
|
469 (defun ksh-mode ()
|
2
|
470 "ksh-mode $Revision: 1.1.1.2 $ - Major mode for editing (Bourne, Korn or Bourne again)
|
0
|
471 shell scripts.
|
|
472 Special key bindings and commands:
|
|
473 \\{ksh-mode-map}
|
|
474 Variables controlling indentation style:
|
|
475 ksh-indent
|
|
476 Indentation of ksh statements with respect to containing block.
|
|
477 Default value is 2.
|
|
478 ksh-case-indent
|
|
479 Additional indentation for statements under case items.
|
|
480 Default value is nil which will align the statements one position
|
|
481 past the \")\" of the pattern.
|
|
482 ksh-case-item-offset
|
|
483 Additional indentation for case items within a case statement.
|
|
484 Default value is 2.
|
|
485 ksh-group-offset
|
|
486 Additional indentation for keywords \"do\" and \"then\".
|
|
487 Default value is -2.
|
|
488 ksh-brace-offset
|
|
489 Additional indentation of \"{\" under functions or brace groupings.
|
|
490 Default value is 0.
|
|
491 ksh-multiline-offset
|
|
492 Additional indentation of line that is preceded of a line ending with a
|
|
493 \\ to make it continue on next line.
|
|
494 ksh-tab-always-indent
|
|
495 Controls the operation of the TAB key. If t (the default), always
|
|
496 reindent the current line. If nil, indent the current line only if
|
|
497 point is at the left margin or in the line's indentation; otherwise
|
|
498 insert a tab.
|
|
499 ksh-match-and-tell
|
|
500 If non-nil echo in the minibuffer the matching compound command
|
|
501 for the \"done\", \"}\", \"fi\", or \"esac\". Default value is t.
|
|
502
|
|
503 ksh-align-to-keyword
|
|
504 Controls whether nested constructs align from the keyword or
|
|
505 the current indentation. If non-nil, indentation will be relative to
|
|
506 the column the keyword starts. If nil, indentation will be relative to
|
|
507 the current indentation of the line the keyword is on.
|
|
508 The default value is non-nil.
|
|
509
|
|
510 ksh-comment-regexp
|
|
511 Regular expression used to recognize comments. Customize to support
|
|
512 ksh-like languages. Default value is \"\^\\\\s *#\".
|
|
513
|
|
514 Style Guide.
|
|
515 By setting
|
|
516 (setq ksh-indent default-tab-width)
|
|
517 (setq ksh-group-offset 0)
|
|
518
|
|
519 The following style is obtained:
|
|
520
|
|
521 if [ -z $foo ]
|
|
522 then
|
|
523 bar # <-- ksh-group-offset is additive to ksh-indent
|
|
524 foo
|
|
525 fi
|
|
526
|
|
527 By setting
|
|
528 (setq ksh-indent default-tab-width)
|
|
529 (setq ksh-group-offset (- 0 ksh-indent))
|
|
530
|
|
531 The following style is obtained:
|
|
532
|
|
533 if [ -z $foo ]
|
|
534 then
|
|
535 bar
|
|
536 foo
|
|
537 fi
|
|
538
|
|
539 By setting
|
|
540 (setq ksh-case-item-offset 1)
|
|
541 (setq ksh-case-indent nil)
|
|
542
|
|
543 The following style is obtained:
|
|
544
|
|
545 case x in *
|
|
546 foo) bar # <-- ksh-case-item-offset
|
|
547 baz;; # <-- ksh-case-indent aligns with \")\"
|
|
548 foobar) foo
|
|
549 bar;;
|
|
550 esac
|
|
551
|
|
552 By setting
|
|
553 (setq ksh-case-item-offset 1)
|
|
554 (setq ksh-case-indent 6)
|
|
555
|
|
556 The following style is obtained:
|
|
557
|
|
558 case x in *
|
|
559 foo) bar # <-- ksh-case-item-offset
|
|
560 baz;; # <-- ksh-case-indent
|
|
561 foobar) foo
|
|
562 bar;;
|
|
563 esac
|
|
564
|
|
565
|
|
566 Installation:
|
|
567
|
|
568 (setq ksh-mode-hook
|
|
569 (function (lambda ()
|
|
570 (font-lock-mode 1) ;; font-lock the buffer
|
|
571 (setq ksh-indent 8)
|
2
|
572 (setq ksh-group-offset -8)
|
0
|
573 (setq ksh-brace-offset -8)
|
|
574 (setq ksh-tab-always-indent t)
|
|
575 (setq ksh-match-and-tell t)
|
|
576 (setq ksh-align-to-keyword t) ;; Turn on keyword alignment
|
|
577 )))"
|
2
|
578 ;;
|
|
579 ;; and away we go
|
0
|
580 (interactive)
|
|
581 (kill-all-local-variables)
|
|
582 (use-local-map ksh-mode-map)
|
|
583 (setq major-mode 'ksh-mode)
|
|
584 (setq mode-name "Ksh")
|
|
585 (setq local-abbrev-table ksh-mode-abbrev-table)
|
|
586 (set-syntax-table ksh-mode-syntax-table)
|
|
587 (make-local-variable 'indent-line-function)
|
|
588 (setq indent-line-function 'ksh-indent-line)
|
|
589 (make-local-variable 'indent-region-function)
|
|
590 (setq indent-region-function 'ksh-indent-region)
|
|
591 (make-local-variable 'comment-start)
|
|
592 (setq comment-start "# ")
|
|
593 (make-local-variable 'comment-end)
|
|
594 (setq comment-end "")
|
|
595 (make-local-variable 'comment-column)
|
|
596 (setq comment-column 32)
|
|
597 (make-local-variable 'comment-start-skip)
|
|
598 (setq comment-start-skip "#+ *")
|
|
599 ;;
|
|
600 ;; config font-lock mode
|
|
601 (make-local-variable 'font-lock-keywords)
|
|
602 (setq font-lock-keywords ksh-font-lock-keywords)
|
|
603 ;;
|
|
604 ;; Let the user customize
|
|
605 (run-hooks 'ksh-mode-hook)
|
|
606 (if (not ksh-align-to-keyword)
|
|
607 (ksh-align-to-keyword -1)
|
|
608 )
|
|
609 ) ;; defun
|
|
610
|
|
611 ;;
|
|
612 ;; Support functions
|
|
613
|
|
614 (defun ksh-align-to-keyword (&optional arg)
|
|
615 "Toggle value of ksh-align-to-keyword and rebind the ksh-current-indentation
|
|
616 function. With arg, force alignment to keyword if and only if arg is positive."
|
|
617 (interactive)
|
|
618 (if (null arg) ;just toggle
|
|
619 (cond ((not ksh-align-to-keyword)
|
|
620 (setq ksh-align-to-keyword t)
|
|
621 (fset 'ksh-current-indentation 'current-column))
|
|
622 (t
|
|
623 (setq ksh-align-to-keyword nil)
|
|
624 (fset 'ksh-current-indentation 'current-indentation))
|
|
625 )
|
|
626 (cond ((natnump arg)
|
|
627 (setq ksh-align-to-keyword t)
|
|
628 (fset 'ksh-current-indentation 'current-column))
|
|
629 (t
|
|
630 (setq ksh-align-to-keyword nil)
|
|
631 (fset 'ksh-current-indentation 'current-indentation))
|
|
632 ))
|
|
633 )
|
|
634
|
|
635 (defun ksh-current-line ()
|
|
636 "Return the vertical position of point in the buffer.
|
|
637 Top line is 1."
|
|
638 (+ (count-lines (point-min) (point))
|
|
639 (if (= (current-column) 0) 1 0))
|
|
640 )
|
|
641
|
|
642
|
|
643 (defun ksh-line-to-string ()
|
|
644 "From point, construct a string from all characters on
|
|
645 current line"
|
|
646 (skip-chars-forward " \t") ;; skip tabs as well as spaces
|
|
647 (buffer-substring (point)
|
|
648 (progn
|
|
649 (end-of-line 1)
|
|
650 (point))))
|
|
651
|
|
652 (defun ksh-get-nest-level ()
|
|
653 "Return a 2 element list (nest-level nest-line) describing where the
|
|
654 current line should nest."
|
|
655 (let ((case-fold-search)
|
2
|
656 (level))
|
0
|
657 (save-excursion
|
|
658 (forward-line -1)
|
|
659 (while (and (not (bobp))
|
|
660 (null level))
|
|
661 (if (and (not (looking-at "^\\s *$"))
|
|
662 (not (save-excursion
|
|
663 (forward-line -1)
|
|
664 (beginning-of-line)
|
|
665 (looking-at ksh-multiline-re)))
|
|
666 (not (looking-at ksh-comment-regexp)))
|
|
667 (setq level (cons (current-indentation)
|
|
668 (ksh-current-line)))
|
|
669 (forward-line -1)
|
|
670 );; if
|
|
671 );; while
|
|
672 (if (null level)
|
|
673 (cons (current-indentation) (ksh-current-line))
|
|
674 level)
|
|
675 )
|
|
676 )
|
|
677 )
|
|
678
|
|
679 (defun ksh-looking-at-compound-list ()
|
|
680 "Return true if current line contains compound list initiating keyword"
|
|
681 (or
|
|
682 (looking-at ksh-do-re)
|
|
683 (looking-at ksh-then-re)
|
|
684 ) ;; or
|
|
685 ) ;; defun
|
|
686
|
|
687 (defun ksh-looking-at-case-item ()
|
|
688 "Return true if current line is a case-item .vs. paren compound list"
|
|
689 (save-excursion
|
|
690 (beginning-of-line)
|
|
691 ;;
|
|
692 ;; Handle paren indentation constructs for this line
|
|
693 (cond ((looking-at ksh-paren-re)
|
|
694 (goto-line (cdr (ksh-get-nest-level)))
|
|
695 ;;
|
|
696 ;; The question is whether this is really a case item or just
|
|
697 ;; parenthesized compound list.
|
|
698 (cond ((or (looking-at ksh-case-re)
|
|
699 (looking-at ksh-case-item-end-re)))
|
|
700 ;;
|
|
701 ;; turns out to be a parenthesized compound list
|
|
702 ;; so propigate the nil for cond
|
|
703 )
|
|
704 ))
|
|
705 )
|
|
706 ) ;; defun
|
|
707
|
2
|
708
|
0
|
709 (defun ksh-get-case-indent ()
|
|
710 "Return the column of the closest open case statement"
|
|
711 (save-excursion
|
|
712 (let (
|
|
713 (nest-list (ksh-get-compound-level ksh-case-re ksh-esac-re (point)))
|
|
714 )
|
|
715 (if (null nest-list)
|
|
716 (progn
|
|
717 (if ksh-match-and-tell
|
|
718 (message "No matching case for ;;"))
|
|
719 0)
|
|
720 (car nest-list)))
|
|
721 )
|
|
722 )
|
|
723
|
2
|
724
|
0
|
725 ;;
|
|
726 ;; Functions which make this mode what it is
|
|
727 ;;
|
|
728
|
|
729 (defun ksh-get-nester-column (nest-line)
|
|
730 "Return the column to indent to with respect to nest-line taking
|
|
731 into consideration keywords and other nesting constructs."
|
|
732 (save-excursion
|
|
733 (let ((fence-post)
|
2
|
734 ; (start-post)
|
0
|
735 (nester-column)
|
2
|
736 (case-fold-search)
|
0
|
737 (start-line (ksh-current-line)))
|
2
|
738 (cond
|
|
739 ;;
|
|
740 ;; Handle case item indentation constructs for this line
|
|
741 ((ksh-looking-at-case-item)
|
|
742 (save-excursion
|
|
743 (goto-line nest-line)
|
|
744 (back-to-indentation)
|
|
745 (let ((fence-post (ksh-eol-point)))
|
|
746 ;;
|
|
747 ;; Now know there is a case-item so detect whether
|
|
748 ;; it is first under case, just another case-item, or
|
|
749 ;; a case-item and case-item-end all rolled together.
|
|
750 ;;
|
|
751 (cond ((ksh-search-forward-sexp ksh-case-re fence-post)
|
|
752 (+ (ksh-current-indentation) ksh-case-item-offset))
|
|
753
|
|
754 ((ksh-looking-at-case-item)
|
|
755 (current-indentation))
|
|
756
|
|
757 ((looking-at ksh-case-item-end-re)
|
|
758 (end-of-line)
|
|
759 (+ (ksh-get-case-indent) ksh-case-item-offset))
|
|
760 )
|
|
761 )))
|
|
762 (t;; Not a case-item. What to do relative to the nest-line?
|
|
763 (save-excursion
|
|
764 (goto-line nest-line)
|
|
765 ; (setq start-post (point))
|
|
766 (setq fence-post (ksh-eol-point))
|
|
767 (setq nester-column
|
|
768 (save-excursion
|
|
769 (cond
|
|
770 ;;
|
|
771 ;; Check if we are in a continued statement
|
|
772 ((and (looking-at ksh-multiline-re)
|
|
773 (save-excursion
|
|
774 (goto-line (1- start-line))
|
|
775 (looking-at ksh-multiline-re)))
|
|
776 (+ (current-indentation) ksh-multiline-offset))
|
|
777 ;; In order to locate the column of the keyword,
|
|
778 ;; which might be embedded within a case-item,
|
|
779 ;; it is necessary to iterate over sexp.
|
|
780 ((progn
|
|
781 (save-excursion
|
|
782 (back-to-indentation)
|
|
783 (if (ksh-search-forward-sexp ksh-keywords-re fence-post)
|
|
784 (progn
|
|
785 ;;
|
|
786 ;; special pun intended 'case'
|
|
787 (if (looking-at ksh-case-re)
|
|
788 (+ (ksh-current-indentation)
|
|
789 ksh-case-item-offset)
|
|
790 (+ (ksh-current-indentation)
|
|
791 (if (null ksh-indent)
|
|
792 2 ksh-indent))))
|
|
793 nil))
|
|
794 ))
|
|
795 ;;
|
|
796 ;; handle then or do
|
|
797 ((progn
|
|
798 (save-excursion
|
|
799 (back-to-indentation)
|
|
800 (if (ksh-search-forward-sexp ksh-then-do-re fence-post)
|
|
801 (progn
|
0
|
802 (if (null ksh-indent)
|
2
|
803 (+ (ksh-current-indentation) 1)
|
|
804 (+ (ksh-current-indentation) ksh-indent)))
|
|
805 nil))))
|
0
|
806
|
2
|
807 ((looking-at ksh-brace-re)
|
|
808 (+ (current-indentation)
|
|
809 (if (null ksh-indent)
|
|
810 2 ksh-indent)
|
|
811 ))
|
|
812 ;;
|
|
813 ;; Forces functions to first column
|
|
814 ((or (looking-at ksh-implicit-func-re)
|
|
815 (looking-at ksh-explicit-func-re))
|
|
816 (if (looking-at ksh-func-brace-re)
|
|
817 (if (null ksh-indent)
|
|
818 2 ksh-indent)
|
|
819 ksh-brace-offset))
|
0
|
820
|
2
|
821 ;;
|
|
822 ;; Need to first detect the end of a case-item
|
|
823 ((looking-at ksh-case-item-end-re)
|
|
824 (end-of-line)
|
|
825 (+ (ksh-get-case-indent) ksh-case-item-offset))
|
|
826 ;;
|
|
827 ;; Now detect first statement under a case item
|
|
828 ((ksh-looking-at-case-item)
|
|
829 (if (null ksh-case-indent)
|
|
830 (progn
|
|
831 (re-search-forward ksh-case-item-re fence-post t)
|
|
832 (goto-char (match-end 1))
|
|
833 (+ (current-column) 1))
|
|
834 (+ (current-indentation) ksh-case-indent)))
|
|
835
|
|
836 ;; This is hosed when using current-column
|
|
837 ;; and there is a multi-command expression as the
|
|
838 ;; nester.
|
|
839 (t (current-indentation)))
|
|
840 )
|
|
841 ));; excursion over
|
|
842 ;;
|
|
843 ;; Handle additional indentation constructs for this line
|
|
844 (cond ((ksh-looking-at-compound-list)
|
|
845 (+ nester-column ksh-group-offset))
|
|
846 ((looking-at ksh-brace-re)
|
|
847 (+ nester-column ksh-brace-offset))
|
|
848 (t nester-column))
|
|
849 );; Not a case-item
|
|
850 )
|
0
|
851 );;let
|
|
852 );; excursion
|
2
|
853 ) ;; defun
|
0
|
854
|
|
855 (defun ksh-indent-command ()
|
|
856 "Indent current line relative to containing block and allow for
|
|
857 ksh-tab-always-indent customization"
|
|
858 (interactive)
|
|
859 (let (case-fold-search)
|
|
860 (cond ((save-excursion
|
|
861 (skip-chars-backward " \t")
|
|
862 (bolp))
|
|
863 (ksh-indent-line))
|
|
864 (ksh-tab-always-indent
|
|
865 (save-excursion
|
|
866 (ksh-indent-line)))
|
|
867 (t (insert-tab))
|
|
868 ))
|
|
869 )
|
|
870
|
|
871
|
|
872 (defun ksh-indent-line ()
|
|
873 "Indent current line as far as it should go according
|
|
874 to the syntax/context"
|
|
875 (interactive)
|
|
876 (let (case-fold-search)
|
|
877 (save-excursion
|
|
878 (beginning-of-line)
|
|
879 (if (bobp)
|
|
880 nil
|
|
881 ;;
|
|
882 ;; Align this line to current nesting level
|
|
883 (let*
|
|
884 (
|
|
885 (level-list (ksh-get-nest-level)) ; Where to nest against
|
|
886 ;; (last-line-level (car level-list))
|
|
887 (this-line-level (current-indentation))
|
|
888 (nester-column (ksh-get-nester-column (cdr level-list)))
|
|
889 (struct-match (ksh-match-structure-and-reindent))
|
|
890 )
|
|
891 (if struct-match
|
|
892 (setq nester-column struct-match))
|
|
893 (if (eq nester-column this-line-level)
|
|
894 nil
|
|
895 (beginning-of-line)
|
|
896 (let ((beg (point)))
|
|
897 (back-to-indentation)
|
|
898 (delete-region beg (point)))
|
|
899 (indent-to nester-column))
|
|
900 );; let*
|
|
901 );; if
|
|
902 );; excursion
|
|
903 ;;
|
|
904 ;; Position point on this line
|
|
905 (let*
|
|
906 (
|
|
907 (this-line-level (current-indentation))
|
2
|
908 (this-bol (ksh-bol-point))
|
0
|
909 (this-point (- (point) this-bol))
|
|
910 )
|
|
911 (cond ((> this-line-level this-point);; point in initial white space
|
|
912 (back-to-indentation))
|
|
913 (t nil)
|
|
914 );; cond
|
|
915 );; let*
|
|
916 );; let
|
|
917 );; defun
|
|
918
|
|
919
|
|
920 (defun ksh-match-indent-level (begin-re end-re)
|
|
921 "Match the compound command and indent. Return nil on no match,
|
|
922 indentation to use for this line otherwise."
|
|
923 (interactive)
|
|
924 (let* ((case-fold-search)
|
|
925 (nest-list
|
|
926 (save-excursion
|
|
927 (ksh-get-compound-level begin-re end-re (point))
|
|
928 ))
|
|
929 ) ;; bindings
|
|
930 (if (null nest-list)
|
|
931 (progn
|
|
932 (if ksh-match-and-tell
|
|
933 (message "No matching compound command"))
|
|
934 nil) ;; Propagate a miss.
|
|
935 (let* (
|
|
936 (nest-level (car nest-list))
|
|
937 (match-line (cdr nest-list))
|
|
938 ) ;; bindings
|
|
939 (if ksh-match-and-tell
|
|
940 (save-excursion
|
|
941 (goto-line match-line)
|
|
942 (message "Matched ... %s" (ksh-line-to-string))
|
|
943 ) ;; excursion
|
|
944 ) ;; if ksh-match-and-tell
|
|
945 nest-level ;;Propagate a hit.
|
|
946 ) ;; let*
|
|
947 ) ;; if
|
|
948 ) ;; let*
|
|
949 ) ;; defun ksh-match-indent-level
|
|
950
|
|
951 (defun ksh-match-structure-and-reindent ()
|
|
952 "If the current line matches one of the indenting keywords
|
|
953 or one of the control structure ending keywords then reindent. Also
|
|
954 if ksh-match-and-tell is non-nil the matching structure will echo in
|
|
955 the minibuffer"
|
|
956 (interactive)
|
|
957 (let (case-fold-search)
|
|
958 (save-excursion
|
|
959 (beginning-of-line)
|
2
|
960 (back-to-indentation)
|
0
|
961 (cond ((looking-at ksh-else-re)
|
|
962 (ksh-match-indent-level ksh-if-re ksh-fi-re))
|
|
963 ((looking-at ksh-elif-re)
|
|
964 (ksh-match-indent-level ksh-if-re ksh-fi-re))
|
|
965 ((looking-at ksh-fi-re)
|
|
966 (ksh-match-indent-level ksh-if-re ksh-fi-re))
|
|
967 ((looking-at ksh-done-re)
|
|
968 (ksh-match-indent-level ksh-iteration-keywords-re ksh-done-re))
|
|
969 ((looking-at ksh-esac-re)
|
|
970 (ksh-match-indent-level ksh-case-re ksh-esac-re))
|
|
971 ;;
|
|
972 ((looking-at ksh-brace-end-re)
|
|
973 (cond
|
|
974 ((ksh-match-indent-level ksh-implicit-func-re ksh-brace-end-re))
|
|
975 ((ksh-match-indent-level ksh-explicit-func-re ksh-brace-end-re))
|
|
976 ((ksh-match-indent-level ksh-func-brace-re ksh-brace-end-re))
|
|
977 (t nil)))
|
|
978 (t nil)
|
|
979 );; cond
|
|
980 )
|
|
981 ))
|
|
982
|
2
|
983
|
|
984 (defun ksh-search-backward-sexp (sexp-re fence-post)
|
|
985 (let
|
|
986 ((old-pnt (point))
|
|
987 (sentinal nil)
|
|
988 )
|
|
989 (while
|
|
990 (progn
|
|
991 (if (not sentinal)
|
|
992 (backward-sexp 1))
|
|
993 (and (> (point) fence-post)
|
|
994 (not sentinal))
|
|
995 )
|
|
996 (if (looking-at sexp-re)
|
|
997 (save-excursion ;avoid comment foolage
|
|
998 (let ((key-fence (point)))
|
|
999 (beginning-of-line)
|
|
1000 (back-to-indentation)
|
|
1001 (while (and (ksh-search-forward-sexp sexp-re key-fence)
|
|
1002 (< (point) key-fence)))
|
|
1003
|
|
1004 (if (= key-fence (point))
|
|
1005 (setq sentinal t))
|
|
1006 ))
|
|
1007 ))
|
|
1008
|
|
1009 (if (< (point) fence-post)
|
|
1010 (progn (goto-char old-pnt)
|
|
1011 nil)
|
|
1012 t)
|
|
1013 ))
|
|
1014
|
|
1015 (defun ksh-forward-sexp ()
|
|
1016 "Special incantation to march over syntax expressions and
|
|
1017 avoid all sorts of nonsense"
|
|
1018 (if (char-equal ?< (char-syntax (char-after (point))))
|
|
1019 (end-of-line)
|
|
1020 (if (char-equal ?. (char-syntax (char-after (point))))
|
|
1021 (forward-char)
|
|
1022 (forward-sexp 1))
|
|
1023 )
|
|
1024 (if (eolp)
|
|
1025 (forward-line))
|
|
1026 (skip-chars-forward ") \t") ;damn case
|
|
1027 )
|
|
1028
|
|
1029 (defun ksh-search-forward-sexp (sexp-re fence-post)
|
|
1030 "Search for an sexp. Return t on success with point at the
|
|
1031 beginning of the sexp. Return nil on failure and restoring point
|
|
1032 to it's original position"
|
|
1033 (let
|
|
1034 ((old-pnt (point))
|
|
1035 )
|
|
1036 (while (and (< (point) fence-post)
|
|
1037 (not (looking-at sexp-re)))
|
|
1038 (ksh-forward-sexp))
|
|
1039
|
|
1040 (if (> (point) fence-post)
|
|
1041 (progn (goto-char old-pnt)
|
|
1042 nil)
|
|
1043 t)
|
|
1044 ))
|
|
1045
|
|
1046
|
|
1047 (defun ksh-get-compound-level
|
0
|
1048 (begin-re end-re anchor-point &optional balance-list)
|
|
1049 "Determine how much to indent this structure. Return a list (level line)
|
|
1050 of the matching compound command or nil if no match found."
|
|
1051 (let*
|
|
1052 (;; Locate the next compound begin keyword bounded by point-min
|
2
|
1053 (match-point
|
|
1054 (if (and (ksh-search-backward-sexp begin-re (point-min))
|
|
1055 (>= (point) (point-min))
|
|
1056 )
|
|
1057 (point)
|
|
1058 0))
|
|
1059
|
0
|
1060 (nest-column (if (zerop match-point)
|
|
1061 1
|
|
1062 (progn
|
|
1063 (goto-char match-point)
|
|
1064 (ksh-current-indentation))))
|
|
1065 (nest-list (cons 0 0)) ;; sentinel cons since cdr is >= 1
|
|
1066 )
|
|
1067 (if (zerop match-point)
|
|
1068 nil ;; graceful exit from recursion
|
|
1069 (progn
|
|
1070 (if (nlistp balance-list)
|
|
1071 (setq balance-list (list)))
|
|
1072 ;; Now search forward from matching start keyword for end keyword
|
2
|
1073 ;; which will locate interceding compound commands
|
0
|
1074 (while (and (consp nest-list) (zerop (cdr nest-list))
|
2
|
1075 (ksh-search-forward-sexp end-re anchor-point)
|
|
1076 (> anchor-point (point))
|
|
1077 )
|
0
|
1078 (if (not (memq (point) balance-list))
|
|
1079 (progn
|
|
1080 (setq balance-list (cons (point) balance-list))
|
|
1081 (goto-char match-point) ;; beginning of compound cmd
|
|
1082 (setq nest-list
|
|
1083 (ksh-get-compound-level begin-re end-re
|
2
|
1084 anchor-point balance-list))
|
|
1085 )
|
|
1086 (ksh-forward-sexp)
|
|
1087 ))
|
0
|
1088
|
|
1089 (cond ((consp nest-list)
|
|
1090 (if (zerop (cdr nest-list))
|
|
1091 (progn
|
|
1092 (goto-char match-point)
|
|
1093 (cons nest-column (ksh-current-line)))
|
|
1094 nest-list))
|
|
1095 (t nil)
|
|
1096 )
|
|
1097 )
|
|
1098 )
|
|
1099 )
|
|
1100 )
|
|
1101
|
|
1102
|
|
1103 (defun ksh-indent-region (start end)
|
|
1104 "From start to end, indent each line."
|
|
1105 ;; The algorithm is just moving through the region line by line with
|
|
1106 ;; the match noise turned off. Only modifies nonempty lines.
|
|
1107 (save-excursion
|
|
1108 (let (ksh-match-and-tell
|
|
1109 (endmark (copy-marker end)))
|
|
1110
|
|
1111 (goto-char start)
|
|
1112 (beginning-of-line)
|
|
1113 (setq start (point))
|
|
1114 (while (> (marker-position endmark) start)
|
|
1115 (if (not (and (bolp) (eolp)))
|
|
1116 (ksh-indent-line))
|
|
1117 (forward-line 1)
|
|
1118 (setq start (point)))
|
|
1119
|
|
1120 (set-marker endmark nil)
|
|
1121 )
|
|
1122 )
|
|
1123 )
|
|
1124
|
|
1125 ;;
|
|
1126 ;; Completion code supplied by Haavard Rue <hrue@imf.unit.no>.
|
|
1127 ;;
|
|
1128 ;;
|
|
1129 ;; add a completion with a given type to the list
|
|
1130 ;;
|
|
1131 (defun ksh-addto-alist (completion type)
|
|
1132 (setq ksh-completion-list
|
|
1133 (append ksh-completion-list
|
|
1134 (list (cons completion type)))))
|
|
1135 ;;
|
|
1136 ;; init the list and pickup all
|
|
1137 ;;
|
|
1138 (defun ksh-completion-init-and-pickup ()
|
|
1139 (interactive)
|
|
1140 (let (case-fold-search)
|
|
1141 (ksh-completion-list-init)
|
|
1142 (ksh-pickup-all)))
|
|
1143
|
|
1144 ;;
|
|
1145 ;; init the list
|
|
1146 ;;
|
|
1147 (defun ksh-completion-list-init ()
|
|
1148 (interactive)
|
|
1149 (setq ksh-completion-list
|
|
1150 (list
|
|
1151 (cons "if" ksh-completion-type-misc)
|
|
1152 (cons "while" ksh-completion-type-misc)
|
|
1153 (cons "until" ksh-completion-type-misc)
|
|
1154 (cons "select" ksh-completion-type-misc)
|
|
1155 (cons "for" ksh-completion-type-misc)
|
|
1156 (cons "continue" ksh-completion-type-misc)
|
|
1157 (cons "function" ksh-completion-type-misc)
|
|
1158 (cons "fi" ksh-completion-type-misc)
|
|
1159 (cons "case" ksh-completion-type-misc)
|
|
1160 (cons "esac" ksh-completion-type-misc)
|
|
1161 (cons "break" ksh-completion-type-misc)
|
|
1162 (cons "exit" ksh-completion-type-misc)
|
|
1163 (cons "done" ksh-completion-type-misc)
|
|
1164 (cons "do" ksh-completion-type-misc))))
|
|
1165
|
|
1166 (defun ksh-eol-point ()
|
|
1167 (save-excursion
|
|
1168 (end-of-line)
|
|
1169 (point)))
|
|
1170
|
|
1171 (defun ksh-bol-point ()
|
|
1172 (save-excursion
|
|
1173 (beginning-of-line)
|
|
1174 (point)))
|
|
1175
|
|
1176 (defun ksh-pickup-all ()
|
|
1177 "Pickup all completions in buffer."
|
|
1178 (interactive)
|
|
1179 (ksh-pickup-completion-driver (point-min) (point-max) t))
|
|
1180
|
|
1181 (defun ksh-pickup-this-line ()
|
|
1182 "Pickup all completions in current line."
|
|
1183 (interactive)
|
|
1184 (ksh-pickup-completion-driver (ksh-bol-point) (ksh-eol-point) nil))
|
|
1185
|
|
1186 (defun ksh-pickup-completion-driver (pmin pmax message)
|
|
1187 "Driver routine for ksh-pickup-completion."
|
|
1188 (if message
|
|
1189 (message "pickup completion..."))
|
|
1190 (let* (
|
|
1191 (i1
|
|
1192 (ksh-pickup-completion ksh-completion-regexp-var
|
|
1193 ksh-completion-type-var
|
|
1194 ksh-completion-match-var
|
|
1195 pmin pmax))
|
|
1196 (i2
|
|
1197 (ksh-pickup-completion ksh-completion-regexp-var2
|
|
1198 ksh-completion-type-var
|
|
1199 ksh-completion-match-var2
|
|
1200 pmin pmax))
|
|
1201 (i3
|
|
1202 (ksh-pickup-completion ksh-completion-regexp-function
|
|
1203 ksh-completion-type-function
|
|
1204 ksh-completion-match-function
|
|
1205 pmin pmax)))
|
|
1206 (if message
|
|
1207 (message "pickup %d variables and %d functions." (+ i1 i2) i3))))
|
|
1208
|
|
1209 (defun ksh-pickup-completion (regexp type match pmin pmax)
|
|
1210 "Pickup completion in region and addit to the list, if not already
|
|
1211 there."
|
|
1212 (let ((i 0) kw obj)
|
|
1213 (save-excursion
|
|
1214 (goto-char pmin)
|
|
1215 (while (and
|
|
1216 (re-search-forward regexp pmax t)
|
|
1217 (match-beginning match)
|
|
1218 (setq kw (buffer-substring
|
|
1219 (match-beginning match)
|
|
1220 (match-end match))))
|
|
1221 (progn
|
|
1222 (setq obj (assoc kw ksh-completion-list))
|
|
1223 (if (or (equal nil obj)
|
|
1224 (and (not (equal nil obj))
|
|
1225 (not (= type (cdr obj)))))
|
|
1226 (progn
|
|
1227 (setq i (1+ i))
|
|
1228 (ksh-addto-alist kw type))))))
|
|
1229 i))
|
|
1230
|
|
1231 (defun ksh-complete-symbol ()
|
|
1232 "Perform completion."
|
|
1233 (interactive)
|
|
1234 (let* ((case-fold-search)
|
|
1235 (end (point))
|
|
1236 (beg (unwind-protect
|
|
1237 (save-excursion
|
|
1238 (backward-sexp 1)
|
|
1239 (while (= (char-syntax (following-char)) ?\')
|
|
1240 (forward-char 1))
|
|
1241 (point))))
|
|
1242 (pattern (buffer-substring beg end))
|
|
1243 (predicate
|
|
1244 ;;
|
|
1245 ;; ` or $( mark a function
|
|
1246 ;;
|
|
1247 (save-excursion
|
|
1248 (goto-char beg)
|
|
1249 (if (or
|
|
1250 (save-excursion
|
|
1251 (backward-char 1)
|
|
1252 (looking-at "`"))
|
|
1253 (save-excursion
|
|
1254 (backward-char 2)
|
|
1255 (looking-at "\\$(")))
|
|
1256 (function (lambda (sym)
|
|
1257 (equal (cdr sym) ksh-completion-type-function)))
|
|
1258 ;;
|
|
1259 ;; a $, ${ or ${# mark a variable
|
|
1260 ;;
|
|
1261 (if (or
|
|
1262 (save-excursion
|
|
1263 (backward-char 1)
|
|
1264 (looking-at "\\$"))
|
|
1265 (save-excursion
|
|
1266 (backward-char 2)
|
|
1267 (looking-at "\\${"))
|
|
1268 (save-excursion
|
|
1269 (backward-char 3)
|
|
1270 (looking-at "\\${#")))
|
|
1271 (function (lambda (sym)
|
|
1272 (equal (cdr sym)
|
|
1273 ksh-completion-type-var)))
|
|
1274 ;;
|
|
1275 ;; don't know. use 'em all
|
|
1276 ;;
|
|
1277 (function (lambda (sym) t))))))
|
|
1278 ;;
|
|
1279 (completion (try-completion pattern ksh-completion-list predicate)))
|
|
1280 ;;
|
|
1281 (cond ((eq completion t))
|
|
1282 ;;
|
|
1283 ;; oops, what is this ?
|
|
1284 ;;
|
|
1285 ((null completion)
|
|
1286 (message "Can't find completion for \"%s\"" pattern))
|
|
1287 ;;
|
|
1288 ;; insert
|
|
1289 ;;
|
|
1290 ((not (string= pattern completion))
|
|
1291 (delete-region beg end)
|
|
1292 (insert completion))
|
|
1293 ;;
|
|
1294 ;; write possible completion in the minibuffer,
|
|
1295 ;; use this instead of a seperate buffer (usual)
|
|
1296 ;;
|
|
1297 (t
|
|
1298 (let ((list (all-completions pattern ksh-completion-list predicate))
|
|
1299 (string ""))
|
|
1300 (while list
|
|
1301 (progn
|
|
1302 (setq string (concat string (format "%s " (car list))))
|
|
1303 (setq list (cdr list))))
|
|
1304 (message string))))))
|
|
1305
|
|
1306 (provide 'ksh-mode)
|
|
1307 ;;; ksh-mode.el ends here
|