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