]> code.delx.au - gnu-emacs/blob - lisp/progmodes/tcl.el
0ed1029bd492534853169bcb68104ed2fb06f587
[gnu-emacs] / lisp / progmodes / tcl.el
1 ;; tcl.el --- Tcl code editing commands for Emacs
2
3 ;; Copyright (C) 1994 Free Software Foundation, Inc.
4
5 ;; Maintainer: Tom Tromey <tromey@busco.lanl.gov>
6 ;; Author: Tom Tromey <tromey@busco.lanl.gov>
7 ;; Chris Lindblad <cjl@lcs.mit.edu>
8 ;; Keywords: languages tcl modes
9 ;; Version: $Revision: 1.6 $
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 1, or (at your option)
16 ;; any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to
25 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26
27 ;; HOW TO INSTALL:
28 ;; Put the following forms in your .emacs to enable autoloading of Tcl
29 ;; mode, and auto-recognition of ".tcl" files.
30 ;;
31 ;; (autoload 'tcl-mode "tcl" "Tcl mode." t)
32 ;; (autoload 'inferior-tcl "tcl" "Run inferior Tcl process." t)
33 ;; (setq auto-mode-alist (append '(("\\.tcl$" . tcl-mode)) auto-mode-alist))
34 ;;
35 ;; If you plan to use the interface to the TclX help files, you must
36 ;; set the variable tcl-help-directory-list to point to the topmost
37 ;; directories containing the TclX help files. Eg:
38 ;;
39 ;; (setq tcl-help-directory-list '("/usr/local/lib/tclx/help"))
40 ;;
41 ;; Also you will want to add the following to your .emacs:
42 ;;
43 ;; (autoload 'tcl-help-on-word "tcl" "Help on Tcl commands" t)
44 ;;
45 ;; FYI a *very* useful thing to do is nroff all the Tk man pages and
46 ;; put them in a subdir of the help system.
47 ;;
48
49 ;;; Commentary:
50
51 ;; LCD Archive Entry:
52 ;; tcl|Tom Tromey|tromey@busco.lanl.gov|
53 ;; Major mode for editing Tcl|
54 ;; $Date: 1994/04/23 16:23:36 $|$Revision: 1.6 $|~/modes/tcl.el.Z|
55
56 ;; CUSTOMIZATION NOTES:
57 ;; * tcl-proc-list can be used to customize a list of things that
58 ;; "define" other things. Eg in my project I put "defvar" in this
59 ;; list.
60 ;; * tcl-typeword-list is similar, but uses font-lock-type-face.
61 ;; * tcl-keyword-list is a list of keywords. I've generally used this
62 ;; for flow-control words. Eg I add "unwind_protect" to this list.
63 ;; * tcl-type-alist can be used to minimally customize indentation
64 ;; according to context.
65
66 ;; Change log:
67 ;; $Log: tcl.el,v $
68 ; Revision 1.6 1994/04/23 16:23:36 tromey
69 ; Wrote tcl-indent-for-comment
70 ;
71 ;;
72 ;; 18-Mar-1994 Tom Tromey Fourth beta release.
73 ;; Added {un,}comment-region to menu. Idea from
74 ;; Mike Scheidler <c23mts@kocrsv01.delcoelect.com>
75 ;; 17-Mar-1994 Tom Tromey
76 ;; Fixed tcl-restart-with-file. Bug fix attempt in
77 ;; tcl-internal-end-of-defun.
78 ;; 16-Mar-1994 Tom Tromey Third beta release
79 ;; Added support code for menu (from Tcl mode written by
80 ;; schmid@fb3-s7.math.TU-Berlin.DE (Gregor Schmid)).
81 ;; 12-Mar-1994 Tom Tromey
82 ;; Better documentation for inferior-tcl-buffer. Wrote
83 ;; tcl-restart-with-file. Wrote Lucid Emacs menu (but no
84 ;; code to install it).
85 ;; 12-Mar-1994 Tom Tromey
86 ;; Wrote tcl-guess-application. Another stab at making
87 ;; tcl-omit-ws-regexp work.
88 ;; 10-Mar-1994 Tom Tromey Second beta release
89 ;; Last Modified: Thu Mar 10 01:24:25 1994 (Tom Tromey)
90 ;; Wrote perl-mode style line indentation command.
91 ;; Wrote more documentation. Added tcl-continued-indent-level.
92 ;; Integrated help code.
93 ;; 8-Mar-1994 Tom Tromey
94 ;; Last Modified: Tue Mar 8 11:58:44 1994 (Tom Tromey)
95 ;; Bug fixes.
96 ;; 6-Mar-1994 Tom Tromey
97 ;; Last Modified: Sun Mar 6 18:55:41 1994 (Tom Tromey)
98 ;; Updated auto-newline support.
99 ;; 6-Mar-1994 Tom Tromey Beta release
100 ;; Last Modified: Sat Mar 5 17:24:32 1994 (Tom Tromey)
101 ;; Wrote tcl-hashify-buffer. Other minor bug fixes.
102 ;; 5-Mar-1994 Tom Tromey
103 ;; Last Modified: Sat Mar 5 16:11:20 1994 (Tom Tromey)
104 ;; Wrote electric-hash code.
105 ;; 3-Mar-1994 Tom Tromey
106 ;; Last Modified: Thu Mar 3 02:53:40 1994 (Tom Tromey)
107 ;; Added code to handle auto-fill in comments.
108 ;; Added imenu support code.
109 ;; Cleaned up code.
110 ;; Better font-lock support.
111 ;; 28-Feb-1994 Tom Tromey
112 ;; Last Modified: Mon Feb 28 14:08:05 1994 (Tom Tromey)
113 ;; Made tcl-figure-type more easily configurable.
114 ;; 28-Feb-1994 Tom Tromey
115 ;; Last Modified: Mon Feb 28 01:02:58 1994 (Tom Tromey)
116 ;; Wrote inferior-tcl mode.
117 ;; 16-Feb-1994 Tom Tromey
118 ;; Last Modified: Wed Feb 16 17:05:19 1994 (Tom Tromey)
119 ;; Added support for font-lock-mode.
120 ;; 29-Oct-1993 Tom Tromey
121 ;; Last Modified: Sun Oct 24 17:39:14 1993 (Tom Tromey)
122 ;; Patches from Guido Bosch to make things work with Lucid Emacs.
123 ;; 22-Oct-1993 Tom Tromey
124 ;; Last Modified: Fri Oct 22 15:26:46 1993 (Tom Tromey)
125 ;; Made many characters have "_" syntax class; suggested by Guido
126 ;; Bosch <Guido.Bosch@loria.fr>. Note that this includes the "$"
127 ;; character, which might be a change you'd notice.
128 ;; 21-Oct-1993 Tom Tromey
129 ;; Last Modified: Thu Oct 21 20:28:40 1993 (Tom Tromey)
130 ;; More fixes for tcl-omit-ws-regexp.
131 ;; 20-Oct-1993 Tom Tromey
132 ;; Started keeping history. Fixed tcl-{beginning,end}-of-defun.
133 ;; Added some code to make things work with Emacs 18.
134
135 ;; THANKS TO:
136 ;; Guido Bosch <Guido.Bosch@loria.fr>
137 ;; pgs1002@esc.cam.ac.uk (Dr P.G. Sjoerdsma)
138 ;; Mike Scheidler <c23mts@kocrsv01.delcoelect.com>
139 ;; Matt Newman <men@charney.colorado.edu>
140 ;; rwhitby@research.canon.oz.au (Rod Whitby)
141 ;; h9118101@hkuxa.hku.hk (Yip Chi Lap [Beta])
142 ;; Pertti Tapio Kasanen <ptk@delta.hut.fi>
143 ;; schmid@fb3-s7.math.TU-Berlin.DE (Gregor Schmid)
144
145 ;; KNOWN BUGS:
146 ;; * indent-region should skip blank lines. (It does in v19, so I'm
147 ;; not motivated to fix it here).
148 ;; * In Tcl "#" is not always a comment character. This can confuse
149 ;; tcl.el in certain circumstances. For now the only workaround is
150 ;; to enclose offending hash characters in quotes or precede it with
151 ;; a backslash. Note that using braces won't work -- quotes change
152 ;; the syntax class of characters between them, while braces do not.
153 ;; The electric-# mode helps alleviate this problem somewhat.
154 ;; * indent-tcl-exp is untested.
155 ;; * Doesn't work under Emacs 18 yet.
156 ;; * There's been a report that font-lock does strange things under
157 ;; Lucid Emacs 19.6. For instance in "proc foobar", the space
158 ;; before "foobar" is highlighted.
159
160 ;; TODO:
161 ;; * make add-log-tcl-defun smarter. should notice if we are in the
162 ;; middle of a defun, or between defuns. should notice if point is
163 ;; on first line of defun (or maybe even in comments before defun).
164 ;; * Allow continuation lines to be indented under the first argument
165 ;; of the preceeding line, like this:
166 ;; [list something \
167 ;; something-else]
168 ;; * There is a request that indentation work like this:
169 ;; button .fred -label Fred \
170 ;; -command {puts fred}
171 ;; * Should have tcl-complete-symbol that queries the inferior process.
172 ;; * Should have describe-symbol that works by sending the magic
173 ;; command to a tclX process.
174 ;; * Need C-x C-e binding (tcl-eval-last-exp).
175 ;; * Write indent-region function that is faster than indenting each
176 ;; line individually.
177 ;; * tcl-figure-type should stop at "beginning of line" (only ws
178 ;; before point, and no "\" on previous line). (see tcl-real-command-p).
179 ;; * Fix beginning-of-defun. I believe this will be fully possible in
180 ;; FSF Emacs 19.23
181 ;; * overrides some comint keybindings; fix.
182 ;; * Trailing \ will eat blank lines. Should deal with this.
183 ;; (this would help catch some potential bugs).
184 ;; * Inferior should display in half the screen, not the whole screen.
185 ;; * Indentation should deal with "switch".
186 ;; * Consider writing code to find help files automatically (for
187 ;; common cases).
188 ;; * M-; sometimes fails (try on "if [blah] then {")
189 ;; * `#' shouldn't insert `\#' in string.
190
191 \f
192
193 ;;; Code:
194
195 (require 'comint)
196
197 ;;
198 ;; User variables.
199 ;;
200
201 (defvar tcl-indent-level 4
202 "*Indentation of Tcl statements with respect to containing block.")
203
204 (defvar tcl-continued-indent-level 4
205 "*Indentation of continuation line relative to first line of command.")
206
207 (defvar tcl-auto-newline nil
208 "*Non-nil means automatically newline before and after braces
209 inserted in Tcl code.")
210
211 (defvar tcl-tab-always-indent t
212 "*Control effect of TAB key.
213 If t (the default), always indent current line.
214 If nil and point is not in the indentation area at the beginning of
215 the line, a TAB is inserted.
216 Other values cause the first possible action from the following list
217 to take place:
218
219 1. Move from beginning of line to correct indentation.
220 2. Delete an empty comment.
221 3. Move forward to start of comment, indenting if necessary.
222 4. Move forward to end of line, indenting if necessary.
223 5. Create an empty comment.
224 6. Move backward to start of comment, indenting if necessary.")
225
226 (defvar tcl-use-hairy-comment-detector t
227 "*If not `nil', the the more complicated, but slower, comment
228 detecting function is used. This variable is only used in GNU Emacs
229 19 (the fast function is always used elsewhere).")
230
231 (defvar tcl-electric-hash-style 'smart
232 "*Style of electric hash insertion to use.
233 Possible values are 'backslash, meaning that `\\' quoting should be
234 done; `quote, meaning that `\"' quoting should be done; 'smart,
235 meaning that the choice between 'backslash and 'quote should be
236 made depending on the number of hashes inserted; or nil, meaning that
237 no quoting should be done. Any other value for this variable is
238 taken to mean 'smart. The default is 'smart.")
239
240 (defvar tcl-help-directory-list nil
241 "*List of topmost directories containing TclX help files")
242
243 (defvar tcl-use-smart-word-finder t
244 "*If not nil, use a better way of finding the current word when
245 looking up help on a Tcl command.")
246
247 (defvar tcl-application "wish"
248 "*Name of Tcl application to run in inferior Tcl mode.")
249
250 (defvar tcl-command-switches nil
251 "*Switches to supply to `tcl-application'.")
252
253 (defvar tcl-prompt-regexp "^\\(% \\|\\)"
254 "*If not nil, a regexp that will match the prompt in the inferior process.
255 If nil, the prompt is the name of the application with \">\" appended.
256
257 The default is \"^\\(% \\|\\)\", which will match the default primary
258 and secondary prompts for tclsh and wish.")
259
260 (defvar inferior-tcl-source-command "source %s\n"
261 "*Format-string for building a Tcl command to load a file.
262 This format string should use `%s' to substitute a file name
263 and should result in a Tcl expression that will command the
264 inferior Tcl to load that file. The filename will be appropriately
265 quoted for Tcl.")
266
267 ;;
268 ;; Keymaps, abbrevs, syntax tables.
269 ;;
270
271 (defvar tcl-mode-abbrev-table nil
272 "Abbrev table in use in Tcl-mode buffers.")
273 (if tcl-mode-abbrev-table
274 ()
275 (define-abbrev-table 'tcl-mode-abbrev-table ()))
276
277 ;; I sure wish Emacs had a package that made it easy to extract this
278 ;; sort of information.
279 (defconst tcl-using-emacs-19 (string-match "19\\." emacs-version)
280 "Nil unless using Emacs 19 (Lucid or FSF).")
281
282 ;; FIXME this will break on Emacs 19.100.
283 (defconst tcl-using-emacs-19.23
284 (string-match "19\\.\\(2[3-9]\\|[3-9][0-9]\\)" emacs-version)
285 "Nil unless using Emacs 19.23 or later.")
286
287 (defconst tcl-using-lemacs-19 (string-match "Lucid" emacs-version)
288 "Nil unless using Lucid Emacs).")
289
290 (defvar tcl-mode-map ()
291 "Keymap used in Tcl mode.")
292 (if tcl-mode-map
293 ()
294 (setq tcl-mode-map (make-sparse-keymap))
295 (define-key tcl-mode-map "{" 'tcl-electric-char)
296 (define-key tcl-mode-map "}" 'tcl-electric-brace)
297 (define-key tcl-mode-map "[" 'tcl-electric-char)
298 (define-key tcl-mode-map "]" 'tcl-electric-char)
299 (define-key tcl-mode-map ";" 'tcl-electric-char)
300 (define-key tcl-mode-map "#" 'tcl-electric-hash)
301 ;; FIXME.
302 (define-key tcl-mode-map "\e\C-a" 'tcl-beginning-of-defun)
303 ;; FIXME.
304 (define-key tcl-mode-map "\e\C-e" 'tcl-end-of-defun)
305 ;; FIXME.
306 (define-key tcl-mode-map "\e\C-h" 'mark-tcl-function)
307 (define-key tcl-mode-map "\e\C-q" 'indent-tcl-exp)
308 (define-key tcl-mode-map "\177" 'backward-delete-char-untabify)
309 (define-key tcl-mode-map "\t" 'tcl-indent-command)
310 (define-key tcl-mode-map "\M-;" 'tcl-indent-for-comment)
311 (define-key tcl-mode-map "\M-\C-x" 'tcl-eval-defun)
312 (and (fboundp 'comment-region)
313 (define-key tcl-mode-map "\C-c\C-c" 'comment-region))
314 (define-key tcl-mode-map "\C-c\C-d" 'tcl-help-on-word)
315 (define-key tcl-mode-map "\C-c\C-e" 'tcl-eval-defun)
316 (define-key tcl-mode-map "\C-c\C-l" 'tcl-load-file)
317 (define-key tcl-mode-map "\C-c\C-p" 'inferior-tcl)
318 (define-key tcl-mode-map "\C-c\C-r" 'tcl-eval-region)
319 (define-key tcl-mode-map "\C-c\C-z" 'switch-to-tcl))
320
321 (defvar tcl-mode-syntax-table nil
322 "Syntax table in use in Tcl-mode buffers.")
323 (if tcl-mode-syntax-table
324 ()
325 (setq tcl-mode-syntax-table (make-syntax-table))
326 (modify-syntax-entry ?% "_" tcl-mode-syntax-table)
327 (modify-syntax-entry ?@ "_" tcl-mode-syntax-table)
328 (modify-syntax-entry ?& "_" tcl-mode-syntax-table)
329 (modify-syntax-entry ?* "_" tcl-mode-syntax-table)
330 (modify-syntax-entry ?+ "_" tcl-mode-syntax-table)
331 (modify-syntax-entry ?- "_" tcl-mode-syntax-table)
332 (modify-syntax-entry ?. "_" tcl-mode-syntax-table)
333 (modify-syntax-entry ?: "_" tcl-mode-syntax-table)
334 (modify-syntax-entry ?! "_" tcl-mode-syntax-table)
335 (modify-syntax-entry ?$ "_" tcl-mode-syntax-table) ; FIXME use "'"?
336 (modify-syntax-entry ?/ "_" tcl-mode-syntax-table)
337 (modify-syntax-entry ?~ "_" tcl-mode-syntax-table)
338 (modify-syntax-entry ?< "_" tcl-mode-syntax-table)
339 (modify-syntax-entry ?= "_" tcl-mode-syntax-table)
340 (modify-syntax-entry ?> "_" tcl-mode-syntax-table)
341 (modify-syntax-entry ?| "_" tcl-mode-syntax-table)
342 (modify-syntax-entry ?\( "()" tcl-mode-syntax-table)
343 (modify-syntax-entry ?\) ")(" tcl-mode-syntax-table)
344 (modify-syntax-entry ?\; "." tcl-mode-syntax-table)
345 (modify-syntax-entry ?\n "> " tcl-mode-syntax-table)
346 (modify-syntax-entry ?\f "> " tcl-mode-syntax-table)
347 (modify-syntax-entry ?# "< " tcl-mode-syntax-table))
348
349 (defvar inferior-tcl-mode-map nil
350 "Keymap used in Inferior Tcl mode.")
351 (if inferior-tcl-mode-map
352 ()
353 ;; FIXME Use keymap inheritance here? FIXME we override comint
354 ;; keybindings here. Maybe someone has a better set?
355 (setq inferior-tcl-mode-map (copy-keymap comint-mode-map))
356 (define-key inferior-tcl-mode-map "\e\C-a" 'tcl-beginning-of-defun)
357 (define-key inferior-tcl-mode-map "\e\C-e" 'tcl-end-of-defun)
358 (define-key inferior-tcl-mode-map "\177" 'backward-delete-char-untabify)
359 (define-key inferior-tcl-mode-map "\M-\C-x" 'tcl-eval-defun)
360 (define-key inferior-tcl-mode-map "\C-c\C-d" 'tcl-help-on-word)
361 (define-key inferior-tcl-mode-map "\C-c\C-e" 'tcl-eval-defun)
362 (define-key inferior-tcl-mode-map "\C-c\C-l" 'tcl-load-file)
363 (define-key inferior-tcl-mode-map "\C-c\C-p" 'inferior-tcl)
364 (define-key inferior-tcl-mode-map "\C-c\C-r" 'tcl-eval-region)
365 (define-key inferior-tcl-mode-map "\C-c\C-z" 'switch-to-tcl))
366
367 ;; Lucid Emacs menu.
368 (defvar tcl-lucid-menu
369 '("Tcl"
370 ["Beginning of function" tcl-beginning-of-defun t]
371 ["End of function" tcl-end-of-defun t]
372 ["Mark function" mark-tcl-function t]
373 ["Indent region" indent-region t]
374 ["Comment region" comment-region t]
375 ["Uncomment region" tcl-uncomment-region t]
376 "----"
377 ["Show Tcl process buffer" inferior-tcl t]
378 ["Send function to Tcl process" tcl-eval-defun t]
379 ["Send region to Tcl process" tcl-eval-region t]
380 ["Send file to Tcl process" tcl-load-file t]
381 ["Restart Tcl process with file" tcl-restart-with-file t]
382 "----"
383 ["Tcl help" tcl-help-on-word t]))
384
385 (defvar inferior-tcl-buffer nil
386 "*The current inferior-tcl process buffer.
387
388 MULTIPLE PROCESS SUPPORT
389 ===========================================================================
390 To run multiple Tcl processes, you start the first up with
391 \\[inferior-tcl]. It will be in a buffer named `*inferior-tcl*'.
392 Rename this buffer with \\[rename-buffer]. You may now start up a new
393 process with another \\[inferior-tcl]. It will be in a new buffer,
394 named `*inferior-tcl*'. You can switch between the different process
395 buffers with \\[switch-to-buffer].
396
397 Commands that send text from source buffers to Tcl processes -- like
398 `tcl-eval-defun' or `tcl-load-file' -- have to choose a process to
399 send to, when you have more than one Tcl process around. This is
400 determined by the global variable `inferior-tcl-buffer'. Suppose you
401 have three inferior Lisps running:
402 Buffer Process
403 foo inferior-tcl
404 bar inferior-tcl<2>
405 *inferior-tcl* inferior-tcl<3>
406 If you do a \\[tcl-eval-defun] command on some Lisp source code, what
407 process do you send it to?
408
409 - If you're in a process buffer (foo, bar, or *inferior-tcl*),
410 you send it to that process.
411 - If you're in some other buffer (e.g., a source file), you
412 send it to the process attached to buffer `inferior-tcl-buffer'.
413 This process selection is performed by function `inferior-tcl-proc'.
414
415 Whenever \\[inferior-tcl] fires up a new process, it resets
416 `inferior-tcl-buffer' to be the new process's buffer. If you only run
417 one process, this does the right thing. If you run multiple
418 processes, you can change `inferior-tcl-buffer' to another process
419 buffer with \\[set-variable].")
420
421 ;;
422 ;; Hooks and other customization.
423 ;;
424
425 (defvar tcl-mode-hook nil
426 "Hook run on entry to Tcl mode.
427
428 Several functions exist which are useful to run from your
429 `tcl-mode-hook' (see each function's documentation for more
430 information):
431
432 tcl-install-menubar
433 Puts a \"Tcl\" menu on the menubar. Doesn't work in Emacs 18.
434 tcl-guess-application
435 Guesses a default setting for `tcl-application' based on any
436 \"#!\" line at the top of the file.
437 tcl-hashify-buffer
438 Quotes all \"#\" characters that don't correspond to actual
439 Tcl comments. (Useful when editing code not originally created
440 with this mode).
441 tcl-auto-fill-mode
442 Auto-filling of Tcl comments.
443
444 Emacs 19 users can add functions to the hook with `add-hook':
445
446 (add-hook 'tcl-mode-hook 'tcl-guess-application)
447
448 Emacs 18 users must use `setq':
449
450 (setq tcl-mode-hook (cons 'tcl-guess-application tcl-mode-hook))")
451
452
453 (defvar inferior-tcl-mode-hook nil
454 "Hook for customizing Inferior Tcl mode.")
455
456 (defvar tcl-proc-list
457 '("proc")
458 "List of commands whose first argument defines something.
459 This exists because some people (eg, me) use \"defvar\" et al.
460 Call `tcl-set-proc-regexp' and `tcl-set-font-lock-keywords'
461 after changing this list.")
462
463 (defvar tcl-proc-regexp nil
464 "Regexp to use when matching proc headers.")
465
466 (defvar tcl-typeword-list
467 '("global" "upvar")
468 "List of Tcl keywords deonting \"type\". Used only for highlighting.
469 Call `tcl-set-font-lock-keywords' after changing this list.")
470
471 ;; Generally I've picked control operators to be keywords.
472 (defvar tcl-keyword-list
473 '("if" "then" "else" "elseif" "for" "foreach" "break" "continue" "while"
474 "eval" "case" "in" "switch" "default" "exit" "error" "proc" "return"
475 "uplevel" "loop" "for_array_keys" "for_recursive_glob" "for_file")
476 "List of Tcl keywords. Used only for highlighting.
477 Default list includes some TclX keywords.
478 Call `tcl-set-font-lock-keywords' after changing this list.")
479
480 (defvar tcl-font-lock-keywords nil
481 "Keywords to highlight for Tcl. See variable `font-lock-keywords'.
482 This variable is generally set from `tcl-proc-regexp',
483 `tcl-typeword-list', and `tcl-keyword-list' by the function
484 `tcl-set-font-lock-keywords'.")
485
486 ;; FIXME need some way to recognize variables because array refs look
487 ;; like 2 sexps.
488 (defvar tcl-type-alist
489 '(
490 ("expr" tcl-expr)
491 ("catch" tcl-commands)
492 ("if" tcl-expr "then" tcl-commands)
493 ("elseif" tcl-expr "then" tcl-commands)
494 ("elseif" tcl-expr tcl-commands)
495 ("if" tcl-expr tcl-commands)
496 ("while" tcl-expr tcl-commands)
497 ("for" tcl-commands tcl-expr tcl-commands tcl-commands)
498 ("foreach" nil nil tcl-commands)
499 ("for_file" nil nil tcl-commands)
500 ("for_array_keys" nil nil tcl-commands)
501 ("for_recursive_glob" nil nil nil tcl-commands)
502 ;; Loop handling is not perfect, because the third argument can be
503 ;; either a command or an expr, and there is no real way to look
504 ;; forward.
505 ("loop" nil tcl-expr tcl-expr tcl-commands)
506 ("loop" nil tcl-expr tcl-commands)
507 )
508 "Alist that controls indentation.
509 \(Actually, this really only controls what happens on continuation lines).
510 Each entry looks like `(KEYWORD TYPE ...)'.
511 Each type entry describes a sexp after the keyword, and can be one of:
512 * nil, meaning that this sexp has no particular type.
513 * tcl-expr, meaning that this sexp is an arithmetic expression.
514 * tcl-commands, meaning that this sexp holds Tcl commands.
515 * a string, which must exactly match the string at the corresponding
516 position for a match to be made.
517
518 For example, the entry for the \"loop\" command is:
519
520 (\"loop\" nil tcl-expr tcl-commands)
521
522 This means that the \"loop\" command has three arguments. The first
523 argument is ignored (for indentation purposes). The second argument
524 is a Tcl expression, and the last argument is Tcl commands.")
525
526 (defvar tcl-explain-indentation nil
527 "If not `nil', debugging message will be printed during indentation.")
528
529 \f
530
531 ;;
532 ;; Work around differences between various versions of Emacs.
533 ;;
534
535 ;; We use this because Lemacs 19.9 has what we need.
536 (defconst tcl-pps-has-arg-6
537 (or tcl-using-emacs-19
538 (and tcl-using-lemacs-19
539 (condition-case nil
540 (progn
541 (parse-partial-sexp (point) (point) nil nil nil t)
542 t)
543 (error nil))))
544 "t if using an emacs which supports sixth (\"commentstop\") argument
545 to parse-partial-sexp.")
546
547 ;; Its pretty bogus to have to do this, but there is no easier way to
548 ;; say "match not syntax-1 and not syntax-2". Too bad you can't put
549 ;; \s in [...]. This sickness is used in Emacs 19 to match a defun
550 ;; starter. (It is used for this in v18 as well).
551 ;;(defconst tcl-omit-ws-regexp
552 ;; (concat "^\\(\\s"
553 ;; (mapconcat 'char-to-string "w_.()\"\\$'/" "\\|\\s")
554 ;; "\\)\\S(*")
555 ;; "Regular expression that matches everything except space, comment
556 ;;starter, and comment ender syntax codes.")
557
558 ;; FIXME? Instead of using the hairy regexp above, we just use a
559 ;; simple one.
560 ;;(defconst tcl-omit-ws-regexp "^[^] \t\n#}]\\S(*"
561 ;; "Regular expression used in locating function definitions.")
562
563 ;; Here's another stab. I think this one actually works. Now the
564 ;; problem seems to be that there is a bug in Emacs 19.22 where
565 ;; end-of-defun doesn't really use the brace matching the one that
566 ;; trails defun-prompt-regexp.
567 (defconst tcl-omit-ws-regexp "^[^ \t\n#}][^\n}]+}*[ \t]+")
568
569 (defun tcl-internal-beginning-of-defun (&optional arg)
570 "Move backward to next beginning-of-defun.
571 With argument, do this that many times.
572 Returns t unless search stops due to end of buffer."
573 (interactive "p")
574 (if (or (null arg) (= arg 0))
575 (setq arg 1))
576 (let (success)
577 (while (progn
578 (setq arg (1- arg))
579 (and (>= arg 0)
580 (setq success
581 (re-search-backward tcl-omit-ws-regexp nil 'move 1))))
582 (while (and (looking-at "[]#}]")
583 (setq success
584 (re-search-backward tcl-omit-ws-regexp nil 'move 1)))))
585 (beginning-of-line)
586 (not (null success))))
587
588 (defun tcl-internal-end-of-defun (&optional arg)
589 "Move forward to next end of defun.
590 An end of a defun is found by moving forward from the beginning of one."
591 (interactive "p")
592 (if (or (null arg) (= arg 0)) (setq arg 1))
593 (let ((start (point)))
594 ;; Was forward-char. I think this works a little better.
595 (forward-line)
596 (tcl-beginning-of-defun)
597 (while (> arg 0)
598 (while (and (re-search-forward tcl-omit-ws-regexp nil 'move 1)
599 (progn (beginning-of-line) t)
600 (looking-at "[]#}]")
601 (progn (forward-line) t)))
602 (let ((next-line (save-excursion
603 (forward-line)
604 (point))))
605 (while (< (point) next-line)
606 (forward-sexp)))
607 (forward-line)
608 (if (> (point) start) (setq arg (1- arg))))))
609
610 ;; In Emacs 19, we can use begining-of-defun as long as we set up a
611 ;; certain regexp. In Emacs 18, we need our own function.
612 (fset 'tcl-beginning-of-defun
613 (if tcl-using-emacs-19
614 'beginning-of-defun
615 'tcl-internal-beginning-of-defun))
616
617 ;; Only FSF Emacs 19 works correctly using end-of-defun. Emacs 18 and
618 ;; Lucid need our own function.
619 (fset 'tcl-end-of-defun
620 (if (and tcl-using-emacs-19 (not tcl-using-lemacs-19))
621 'end-of-defun
622 'tcl-internal-end-of-defun))
623
624 \f
625
626 ;;
627 ;; Some helper functions.
628 ;;
629
630 (defun tcl-set-proc-regexp ()
631 "Set `tcl-proc-regexp' from variable `tcl-proc-list'."
632 (setq tcl-proc-regexp (concat "^\\("
633 (mapconcat 'identity tcl-proc-list "\\|")
634 "\\)[ \t]+")))
635
636 (defun tcl-set-font-lock-keywords ()
637 "Set `tcl-font-lock-keywords'.
638 Uses variables `tcl-proc-regexp' and `tcl-keyword-list'."
639 (setq tcl-font-lock-keywords
640 (list
641 ;; Names of functions (and other "defining things").
642 (list (concat tcl-proc-regexp "\\([^ \t\n]+\\)")
643 2 'font-lock-function-name-face)
644
645 ;; Names of type-defining things.
646 (list (concat "\\(\\s-\\|^\\)\\("
647 ;; FIXME Use 'regexp-quote?
648 (mapconcat 'identity tcl-typeword-list "\\|")
649 "\\)\\(\\s-\\|$\\)")
650 2 'font-lock-type-face)
651
652 ;; Keywords. Only recognized if surrounded by whitespace.
653 ;; FIXME consider using "not word or symbol", not
654 ;; "whitespace".
655 (cons (concat "\\(\\s-\\|^\\)\\("
656 ;; FIXME Use regexp-quote?
657 (mapconcat 'identity tcl-keyword-list "\\|")
658 "\\)\\(\\s-\\|$\\)")
659 2)
660 )))
661
662 (if tcl-proc-regexp
663 ()
664 (tcl-set-proc-regexp))
665
666 (if tcl-font-lock-keywords
667 ()
668 (tcl-set-font-lock-keywords))
669
670 \f
671
672 ;;
673 ;; The mode itself.
674 ;;
675
676 (defun tcl-mode ()
677 "Major mode for editing Tcl code.
678 Expression and list commands understand all Tcl brackets.
679 Tab indents for Tcl code.
680 Paragraphs are separated by blank lines only.
681 Delete converts tabs to spaces as it moves back.
682
683 Variables controlling indentation style:
684 tcl-indent-level
685 Indentation of Tcl statements within surrounding block.
686 tcl-continued-indent-level
687 Indentation of continuation line relative to first line of command.
688
689 Variables controlling user interaction with mode (see variable
690 documentation for details):
691 tcl-tab-always-indent
692 Controls action of TAB key.
693 tcl-auto-newline
694 Non-nil means automatically newline before and after braces, brackets,
695 and semicolons inserted in Tcl code.
696 tcl-electric-hash-style
697 Controls action of `#' key.
698 tcl-use-hairy-comment-detector
699 If t, use more complicated, but slower, comment detector.
700 This variable is only used in GNU Emacs 19.
701
702 Turning on Tcl mode calls the value of the variable `tcl-mode-hook'
703 with no args, if that value is non-nil. Read the documentation for
704 `tcl-mode-hook' to see what kinds of interesting hook functions
705 already exist.
706
707 Commands:
708 \\{tcl-mode-map}"
709 (interactive)
710 (kill-all-local-variables)
711 (use-local-map tcl-mode-map)
712 (setq major-mode 'tcl-mode)
713 (setq mode-name "Tcl")
714 (setq local-abbrev-table tcl-mode-abbrev-table)
715 (set-syntax-table tcl-mode-syntax-table)
716 (make-local-variable 'paragraph-start)
717 (setq paragraph-start (concat "^$\\|" page-delimiter))
718 (make-local-variable 'paragraph-separate)
719 (setq paragraph-separate paragraph-start)
720 (make-local-variable 'paragraph-ignore-fill-prefix)
721 (setq paragraph-ignore-fill-prefix t)
722 (make-local-variable 'indent-line-function)
723 (setq indent-line-function 'tcl-indent-line)
724 ;; Tcl doesn't require a final newline.
725 ;; (make-local-variable 'require-final-newline)
726 ;; (setq require-final-newline t)
727 (make-local-variable 'comment-start)
728 (setq comment-start "# ")
729 (make-local-variable 'comment-start-skip)
730 (setq comment-start-skip "#+ *")
731 (make-local-variable 'comment-column)
732 (setq comment-column 40)
733 (make-local-variable 'comment-end)
734 (setq comment-end "")
735 (make-local-variable 'font-lock-keywords)
736 (setq font-lock-keywords tcl-font-lock-keywords)
737 (setq imenu-create-index-function 'tcl-imenu-create-index-function)
738 (make-local-variable 'parse-sexp-ignore-comments)
739 (if tcl-using-emacs-19
740 (progn
741 ;; This can only be set to t in Emacs 19 and Lucid Emacs.
742 ;; Emacs 18 and Epoch lose.
743 (setq parse-sexp-ignore-comments t)
744 ;; Lucid Emacs has defun-prompt-regexp, but I don't believe
745 ;; that it works for end-of-defun -- only for
746 ;; beginning-of-defun.
747 (make-local-variable 'defun-prompt-regexp)
748 (setq defun-prompt-regexp tcl-omit-ws-regexp)
749 ;; The following doesn't work in Lucid Emacs 19.6, but maybe
750 ;; it will appear in later versions.
751 (make-local-variable 'add-log-current-defun-function)
752 (setq add-log-current-defun-function 'add-log-tcl-defun))
753 (setq parse-sexp-ignore-comments nil))
754 (run-hooks 'tcl-mode-hook))
755
756 \f
757
758 ;; This is used for braces, brackets, and semi (except for closing
759 ;; braces, which are handled specially).
760 (defun tcl-electric-char (arg)
761 "Insert character and correct line's indentation."
762 (interactive "p")
763 ;; Indent line first; this looks better if parens blink.
764 (tcl-indent-line)
765 (self-insert-command arg)
766 (if (and tcl-auto-newline (= last-command-char ?\;))
767 (progn
768 (newline)
769 (tcl-indent-line))))
770
771 ;; This is used for closing braces. If tcl-auto-newline is set, can
772 ;; insert a newline both before and after the brace, depending on
773 ;; context. FIXME should this be configurable? Does anyone use this?
774 (defun tcl-electric-brace (arg)
775 "Insert character and correct line's indentation."
776 (interactive "p")
777 ;; If auto-newlining and there is stuff on the same line, insert a
778 ;; newline first.
779 (if tcl-auto-newline
780 (progn
781 (if (save-excursion
782 (skip-chars-backward " \t")
783 (bolp))
784 ()
785 (tcl-indent-line)
786 (newline))
787 ;; In auto-newline case, must insert a newline after each
788 ;; brace. So an explicit loop is needed.
789 (while (> arg 0)
790 (insert last-command-char)
791 (tcl-indent-line)
792 (newline)
793 (setq arg (1- arg))))
794 (self-insert-command arg))
795 (tcl-indent-line))
796
797 \f
798
799 (defun tcl-indent-command (&optional arg)
800 "Indent current line as Tcl code, or in some cases insert a tab character.
801 If tcl-tab-always-indent is t (the default), always indent current line.
802 If tcl-tab-always-indent is nil and point is not in the indentation
803 area at the beginning of the line, a TAB is inserted.
804 Other values of tcl-tab-always-indent cause the first possible action
805 from the following list to take place:
806
807 1. Move from beginning of line to correct indentation.
808 2. Delete an empty comment.
809 3. Move forward to start of comment, indenting if necessary.
810 4. Move forward to end of line, indenting if necessary.
811 5. Create an empty comment.
812 6. Move backward to start of comment, indenting if necessary."
813 (interactive "p")
814 (cond
815 ((not tcl-tab-always-indent)
816 ;; Indent if in indentation area, otherwise insert TAB.
817 (if (<= (current-column) (current-indentation))
818 (tcl-indent-line)
819 (self-insert-command arg)))
820 ((eq tcl-tab-always-indent t)
821 ;; Always indent.
822 (tcl-indent-line))
823 (t
824 ;; "Perl-mode" style TAB command.
825 (let* ((ipoint (point))
826 (eolpoint (progn
827 (end-of-line)
828 (point)))
829 (comment-p (tcl-in-comment)))
830 (cond
831 ((= ipoint (save-excursion
832 (beginning-of-line)
833 (point)))
834 (beginning-of-line)
835 (tcl-indent-line)
836 ;; If indenting didn't leave us in column 0, go to the
837 ;; indentation. Otherwise leave point at end of line. This
838 ;; is a hack.
839 (if (= (point) (save-excursion
840 (beginning-of-line)
841 (point)))
842 (end-of-line)
843 (back-to-indentation)))
844 ((and comment-p (looking-at "[ \t]*$"))
845 ;; Empty comment, so delete it. We also delete any ";"
846 ;; characters at the end of the line. I think this is
847 ;; friendlier, but I don't know how other people will feel.
848 (backward-char)
849 (skip-chars-backward " \t;")
850 (delete-region (point) eolpoint))
851 ((and comment-p (< ipoint (point)))
852 ;; Before comment, so skip to it.
853 (tcl-indent-line)
854 (indent-for-comment))
855 ((/= ipoint eolpoint)
856 ;; Go to end of line (since we're not there yet).
857 (goto-char eolpoint)
858 (tcl-indent-line))
859 ((not comment-p)
860 (tcl-indent-line)
861 (tcl-indent-for-comment))
862 (t
863 ;; Go to start of comment. We don't leave point where it is
864 ;; because we want to skip comment-start-skip.
865 (tcl-indent-line)
866 (indent-for-comment)))))))
867
868 (defun tcl-indent-line ()
869 "Indent current line as Tcl code.
870 Return the amount the indentation changed by."
871 (let ((indent (calculate-tcl-indent nil))
872 beg shift-amt
873 (case-fold-search nil)
874 (pos (- (point-max) (point))))
875 (beginning-of-line)
876 (setq beg (point))
877 (cond ((eq indent nil)
878 (setq indent (current-indentation)))
879 (t
880 (skip-chars-forward " \t")
881 (if (listp indent) (setq indent (car indent)))
882 (cond ((= (following-char) ?})
883 (setq indent (- indent tcl-indent-level)))
884 ((= (following-char) ?\])
885 (setq indent (- indent 1))))))
886 (skip-chars-forward " \t")
887 (setq shift-amt (- indent (current-column)))
888 (if (zerop shift-amt)
889 (if (> (- (point-max) pos) (point))
890 (goto-char (- (point-max) pos)))
891 (delete-region beg (point))
892 (indent-to indent)
893 ;; If initial point was within line's indentation,
894 ;; position after the indentation. Else stay at same point in text.
895 (if (> (- (point-max) pos) (point))
896 (goto-char (- (point-max) pos))))
897 shift-amt))
898
899 (defun tcl-figure-type ()
900 "Determine type of sexp at point.
901 This is either 'tcl-expr, 'tcl-commands, or nil. Puts point at start
902 of sexp that indicates types.
903
904 See documentation for variable `tcl-type-alist' for more information."
905 (let ((count 0)
906 result
907 word-stack)
908 (while (and (< count 5)
909 (not result))
910 (condition-case nil
911 (progn
912 ;; FIXME should use "tcl-backward-sexp", which would skip
913 ;; over entire variables, etc.
914 (backward-sexp)
915 (if (looking-at "[a-zA-Z_]+")
916 (let ((list tcl-type-alist)
917 entry)
918 (setq word-stack (cons (current-word) word-stack))
919 (while (and list (not result))
920 (setq entry (car list))
921 (setq list (cdr list))
922 (let ((index 0))
923 (while (and entry (<= index count))
924 ;; Abort loop if string does not match word on
925 ;; stack.
926 (and (stringp (car entry))
927 (not (string= (car entry)
928 (nth index word-stack)))
929 (setq entry nil))
930 (setq entry (cdr entry))
931 (setq index (1+ index)))
932 (and (> index count)
933 (not (stringp (car entry)))
934 (setq result (car entry)))
935 )))
936 (setq word-stack (cons nil word-stack))))
937 (error nil))
938 (setq count (1+ count)))
939 (and tcl-explain-indentation
940 (message "Indentation type %s" result))
941 result))
942
943 (defun calculate-tcl-indent (&optional parse-start)
944 "Return appropriate indentation for current line as Tcl code.
945 In usual case returns an integer: the column to indent to.
946 Returns nil if line starts inside a string, t if in a comment."
947 (save-excursion
948 (beginning-of-line)
949 (let* ((indent-point (point))
950 (case-fold-search nil)
951 (continued-line
952 (save-excursion
953 (if (bobp)
954 nil
955 (backward-char)
956 (= ?\\ (preceding-char)))))
957 (continued-indent-value (if continued-line
958 tcl-continued-indent-level
959 0))
960 state
961 containing-sexp
962 found-next-line)
963 (if parse-start
964 (goto-char parse-start)
965 (tcl-beginning-of-defun))
966 (while (< (point) indent-point)
967 (setq parse-start (point))
968 (setq state (parse-partial-sexp (point) indent-point 0))
969 (setq containing-sexp (car (cdr state))))
970 (cond ((or (nth 3 state) (nth 4 state))
971 ;; Inside comment or string. Return nil or t if should
972 ;; not change this line
973 (nth 4 state))
974 ((null containing-sexp)
975 ;; Line is at top level.
976 continued-indent-value)
977 (t
978 ;; Set expr-p if we are looking at the expression part of
979 ;; an "if", "expr", etc statement. Set commands-p if we
980 ;; are looking at the body part of an if, while, etc
981 ;; statement. FIXME Should check for "for" loops here.
982 (goto-char containing-sexp)
983 (let* ((sexpr-type (tcl-figure-type))
984 (expr-p (eq sexpr-type 'tcl-expr))
985 (commands-p (eq sexpr-type 'tcl-commands))
986 (expr-start (point)))
987 ;; Find the first statement in the block and indent
988 ;; like it. The first statement in the block might be
989 ;; on the same line, so what we do is skip all
990 ;; "virtually blank" lines, looking for a non-blank
991 ;; one. A line is virtually blank if it only contains
992 ;; a comment and whitespace. FIXME continued comments
993 ;; aren't supported. They are a wart on Tcl anyway.
994 ;; We do it this funky way because we want to know if
995 ;; we've found a statement on some line _after_ the
996 ;; line holding the sexp opener.
997 (goto-char containing-sexp)
998 (forward-char)
999 (if (and (< (point) indent-point)
1000 (looking-at "[ \t]*\\(#.*\\)?$"))
1001 (progn
1002 (forward-line)
1003 (while (and (< (point) indent-point)
1004 (looking-at "[ \t]*\\(#.*\\)?$"))
1005 (setq found-next-line t)
1006 (forward-line))))
1007 (if (or continued-line
1008 (/= (char-after containing-sexp) ?{)
1009 expr-p)
1010 (progn
1011 ;; Line is continuation line, or the sexp opener
1012 ;; is not a curly brace, or we are are looking at
1013 ;; an `expr' expression (which must be split
1014 ;; specially). So indentation is column of first
1015 ;; good spot after sexp opener (with some added
1016 ;; in the continued-line case). If there is no
1017 ;; nonempty line before the indentation point, we
1018 ;; use the column of the character after the sexp
1019 ;; opener.
1020 (if (>= (point) indent-point)
1021 (progn
1022 (goto-char containing-sexp)
1023 (forward-char))
1024 (skip-chars-forward " \t"))
1025 (+ (current-column) continued-indent-value))
1026 ;; After a curly brace, and not a continuation line.
1027 ;; So take indentation from first good line after
1028 ;; start of block, unless that line is on the same
1029 ;; line as the opening brace. In this case use the
1030 ;; indentation of the opening brace's line, plus
1031 ;; another indent step. If we are in the body part
1032 ;; of an "if" or "while" then the indentation is
1033 ;; taken from the line holding the start of the
1034 ;; statement.
1035 (if (and (< (point) indent-point)
1036 found-next-line)
1037 (current-indentation)
1038 (if commands-p
1039 (goto-char expr-start)
1040 (goto-char containing-sexp))
1041 (+ (current-indentation) tcl-indent-level)))))))))
1042
1043 \f
1044
1045 (defun mark-tcl-function ()
1046 "Put mark at end of Tcl function, point at beginning."
1047 (interactive)
1048 (push-mark (point))
1049 (tcl-end-of-defun)
1050 (if tcl-using-emacs-19
1051 (push-mark (point) nil t)
1052 (push-mark (point)))
1053 (tcl-beginning-of-defun)
1054 (backward-paragraph))
1055
1056 \f
1057
1058 (defun indent-tcl-exp ()
1059 "Indent each line of the Tcl grouping following point."
1060 (interactive)
1061 (let ((indent-stack (list nil))
1062 (contain-stack (list (point)))
1063 (case-fold-search nil)
1064 outer-loop-done inner-loop-done state ostate
1065 this-indent last-sexp continued-line
1066 (next-depth 0)
1067 last-depth)
1068 (save-excursion
1069 (forward-sexp 1))
1070 (save-excursion
1071 (setq outer-loop-done nil)
1072 (while (and (not (eobp)) (not outer-loop-done))
1073 (setq last-depth next-depth)
1074 ;; Compute how depth changes over this line
1075 ;; plus enough other lines to get to one that
1076 ;; does not end inside a comment or string.
1077 ;; Meanwhile, do appropriate indentation on comment lines.
1078 (setq inner-loop-done nil)
1079 (while (and (not inner-loop-done)
1080 (not (and (eobp) (setq outer-loop-done t))))
1081 (setq ostate state)
1082 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
1083 nil nil state))
1084 (setq next-depth (car state))
1085 (if (and (car (cdr (cdr state)))
1086 (>= (car (cdr (cdr state))) 0))
1087 (setq last-sexp (car (cdr (cdr state)))))
1088 (if (or (nth 4 ostate))
1089 (tcl-indent-line))
1090 (if (or (nth 3 state))
1091 (forward-line 1)
1092 (setq inner-loop-done t)))
1093 (if (<= next-depth 0)
1094 (setq outer-loop-done t))
1095 (if outer-loop-done
1096 nil
1097 ;; If this line had ..))) (((.. in it, pop out of the levels
1098 ;; that ended anywhere in this line, even if the final depth
1099 ;; doesn't indicate that they ended.
1100 (while (> last-depth (nth 6 state))
1101 (setq indent-stack (cdr indent-stack)
1102 contain-stack (cdr contain-stack)
1103 last-depth (1- last-depth)))
1104 (if (/= last-depth next-depth)
1105 (setq last-sexp nil))
1106 ;; Add levels for any parens that were started in this line.
1107 (while (< last-depth next-depth)
1108 (setq indent-stack (cons nil indent-stack)
1109 contain-stack (cons nil contain-stack)
1110 last-depth (1+ last-depth)))
1111 (if (null (car contain-stack))
1112 (setcar contain-stack
1113 (or (car (cdr state))
1114 (save-excursion
1115 (forward-sexp -1)
1116 (point)))))
1117 (forward-line 1)
1118 (setq continued-line
1119 (save-excursion
1120 (backward-char)
1121 (= (preceding-char) ?\\)))
1122 (skip-chars-forward " \t")
1123 (if (eolp)
1124 nil
1125 (if (and (car indent-stack)
1126 (>= (car indent-stack) 0))
1127 ;; Line is on an existing nesting level.
1128 (setq this-indent (car indent-stack))
1129 ;; Just started a new nesting level.
1130 ;; Compute the standard indent for this level.
1131 (let ((val (calculate-tcl-indent
1132 (if (car indent-stack)
1133 (- (car indent-stack))))))
1134 (setcar indent-stack
1135 (setq this-indent val))
1136 (setq continued-line nil)))
1137 (cond ((not (numberp this-indent)))
1138 ((= (following-char) ?})
1139 (setq this-indent (- this-indent tcl-indent-level)))
1140 ((= (following-char) ?\])
1141 (setq this-indent (- this-indent 1))))
1142 ;; Put chosen indentation into effect.
1143 (or (null this-indent)
1144 (= (current-column)
1145 (if continued-line
1146 (+ this-indent tcl-indent-level)
1147 this-indent))
1148 (progn
1149 (delete-region (point) (progn (beginning-of-line) (point)))
1150 (indent-to
1151 (if continued-line
1152 (+ this-indent tcl-indent-level)
1153 this-indent)))))))))
1154 )
1155
1156 \f
1157
1158 ;;
1159 ;; Interfaces to other packages.
1160 ;;
1161
1162 (autoload 'imenu-progress-message "imenu" "" nil 'macro)
1163
1164 (defun tcl-imenu-create-index-function ()
1165 "Generate alist of indices for imenu."
1166 (let ((re (concat tcl-proc-regexp "\\([^ \t\n{]+\\)"))
1167 alist)
1168 (imenu-progress-message 0)
1169 (goto-char (point-min))
1170 (while (re-search-forward re nil t)
1171 (imenu-progress-message nil)
1172 ;; Position on start of proc name, not beginning of line.
1173 (setq alist (cons
1174 (cons (buffer-substring (match-beginning 2) (match-end 2))
1175 (match-beginning 2))
1176 alist)))
1177 (imenu-progress-message 100)
1178 (nreverse alist)))
1179
1180 ;; FIXME Definition of function is very ad-hoc. Should use
1181 ;; tcl-beginning-of-defun. Also has incestuous knowledge about the
1182 ;; format of tcl-proc-regexp.
1183 (defun add-log-tcl-defun ()
1184 "Return name of Tcl function point is in, or nil."
1185 (save-excursion
1186 (if (re-search-backward
1187 (concat tcl-proc-regexp "\\([^ \t\n{]+\\)") nil t)
1188 (buffer-substring (match-beginning 2)
1189 (match-end 2)))))
1190
1191 \f
1192
1193 ;;
1194 ;; Helper functions for inferior Tcl mode.
1195 ;;
1196
1197 ;; This exists to let us delete the prompt when commands are sent
1198 ;; directly to the inferior Tcl. See gud.el for an explanation of how
1199 ;; it all works (I took it from there). This stuff doesn't really
1200 ;; work as well as I'd like it to. But I don't believe there is
1201 ;; anything useful that can be done.
1202 (defvar inferior-tcl-delete-prompt-marker nil)
1203
1204 (defun tcl-filter (proc string)
1205 (let ((inhibit-quit t))
1206 (save-excursion
1207 (set-buffer (process-buffer proc))
1208 (goto-char (process-mark proc))
1209 ;; Delete prompt if requested.
1210 (if (marker-buffer inferior-tcl-delete-prompt-marker)
1211 (progn
1212 (delete-region (point) inferior-tcl-delete-prompt-marker)
1213 (set-marker inferior-tcl-delete-prompt-marker nil)))))
1214 (comint-output-filter proc string))
1215
1216 (defun tcl-send-string (proc string)
1217 (save-excursion
1218 (set-buffer (process-buffer proc))
1219 (goto-char (process-mark proc))
1220 (beginning-of-line)
1221 (if (looking-at comint-prompt-regexp)
1222 (set-marker inferior-tcl-delete-prompt-marker (point))))
1223 (comint-send-string proc string))
1224
1225 (defun tcl-send-region (proc start end)
1226 (save-excursion
1227 (set-buffer (process-buffer proc))
1228 (goto-char (process-mark proc))
1229 (beginning-of-line)
1230 (if (looking-at comint-prompt-regexp)
1231 (set-marker inferior-tcl-delete-prompt-marker (point))))
1232 (comint-send-region proc start end))
1233
1234 (defun switch-to-tcl (eob-p)
1235 "Switch to inferior Tcl process buffer.
1236 With argument, positions cursor at end of buffer."
1237 (interactive "P")
1238 (if (get-buffer inferior-tcl-buffer)
1239 (pop-to-buffer inferior-tcl-buffer)
1240 (error "No current inferior Tcl buffer"))
1241 (cond (eob-p
1242 (push-mark)
1243 (goto-char (point-max)))))
1244
1245 (defun inferior-tcl-proc ()
1246 "Return current inferior Tcl process.
1247 See variable `inferior-tcl-buffer'."
1248 (let ((proc (get-buffer-process (if (eq major-mode 'inferior-tcl-mode)
1249 (current-buffer)
1250 inferior-tcl-buffer))))
1251 (or proc
1252 (error "No Tcl process; see variable `inferior-tcl-buffer'"))))
1253
1254 (defun tcl-eval-region (start end &optional and-go)
1255 "Send the current region to the inferior Tcl process.
1256 Prefix argument means switch to the Tcl buffer afterwards."
1257 (interactive "r\nP")
1258 (let ((proc (inferior-tcl-proc)))
1259 (tcl-send-region proc start end)
1260 (tcl-send-string proc "\n")
1261 (if and-go (switch-to-tcl t))))
1262
1263 (defun tcl-eval-defun (&optional and-go)
1264 "Send the current defun to the inferior Tcl process.
1265 Prefix argument means switch to the Tcl buffer afterwards."
1266 (interactive "P")
1267 (save-excursion
1268 (tcl-end-of-defun)
1269 (let ((end (point)))
1270 (tcl-beginning-of-defun)
1271 (tcl-eval-region (point) end)))
1272 (if and-go (switch-to-tcl t)))
1273
1274 \f
1275
1276 ;;
1277 ;; Inferior Tcl mode itself.
1278 ;;
1279
1280 (defun inferior-tcl-mode ()
1281 "Major mode for interacting with Tcl interpreter.
1282
1283 A Tcl process can be started with M-x inferior-tcl.
1284
1285 Entry to this mode runs the hooks comint-mode-hook and
1286 inferior-tcl-mode-hook, in that order.
1287
1288 You can send text to the inferior Tcl process from other buffers
1289 containing Tcl source.
1290
1291 Variables controlling Inferior Tcl mode:
1292 tcl-application
1293 Name of program to run.
1294 tcl-command-switches
1295 Command line arguments to `tcl-application'.
1296 tcl-prompt-regexp
1297 Matches prompt.
1298 inferior-tcl-source-command
1299 Command to use to read Tcl file in running application.
1300 inferior-tcl-buffer
1301 The current inferior Tcl process buffer. See variable
1302 documentation for details on multiple-process support.
1303
1304 The following commands are available:
1305 \\{inferior-tcl-mode-map}"
1306 (interactive)
1307 (comint-mode)
1308 (setq comint-prompt-regexp (or tcl-prompt-regexp
1309 (concat "^"
1310 (regexp-quote tcl-application)
1311 ">")))
1312 (setq major-mode 'inferior-tcl-mode)
1313 (setq mode-name "Inferior Tcl")
1314 (setq mode-line-process '(": %s"))
1315 (use-local-map inferior-tcl-mode-map)
1316 (setq local-abbrev-table tcl-mode-abbrev-table)
1317 (set-syntax-table tcl-mode-syntax-table)
1318 (if tcl-using-emacs-19
1319 (progn
1320 (make-local-variable 'defun-prompt-regexp)
1321 (setq defun-prompt-regexp tcl-omit-ws-regexp)))
1322 (make-local-variable 'inferior-tcl-delete-prompt-marker)
1323 (setq inferior-tcl-delete-prompt-marker (make-marker))
1324 (set-process-filter (get-buffer-process (current-buffer)) 'tcl-filter)
1325 (run-hooks 'inferior-tcl-mode-hook))
1326
1327 (defun inferior-tcl (cmd)
1328 "Run inferior Tcl process.
1329 Prefix arg means enter program name interactively.
1330 See documentation for function `inferior-tcl-mode' for more information."
1331 (interactive
1332 (list (if current-prefix-arg
1333 (read-string "Run Tcl: " tcl-application)
1334 tcl-application)))
1335 (if (not (comint-check-proc "*inferior-tcl*"))
1336 (progn
1337 (set-buffer (apply (function make-comint) "inferior-tcl" cmd nil
1338 tcl-command-switches))
1339 (inferior-tcl-mode)))
1340 (make-local-variable 'tcl-application)
1341 (setq tcl-application cmd)
1342 (setq inferior-tcl-buffer "*inferior-tcl*")
1343 (switch-to-buffer "*inferior-tcl*"))
1344
1345 (and (fboundp 'defalias)
1346 (defalias 'run-tcl 'inferior-tcl))
1347
1348 \f
1349
1350 ;;
1351 ;; Auto-fill support.
1352 ;;
1353
1354 (defun tcl-real-command-p ()
1355 "Return nil if point is not at the beginning of a command.
1356 A command is the first word on an otherwise empty line, or the
1357 first word following a semicolon, opening brace, or opening bracket."
1358 (save-excursion
1359 (skip-chars-backward " \t")
1360 (cond
1361 ((bobp) t)
1362 ((bolp)
1363 (backward-char)
1364 ;; Note -- continued comments are not supported here. I
1365 ;; consider those to be a wart on the language.
1366 (not (eq ?\\ (preceding-char))))
1367 (t
1368 (memq (preceding-char) '(?\; ?{ ?\[))))))
1369
1370 ;; FIXME doesn't actually return t. See last case.
1371 (defun tcl-real-comment-p ()
1372 "Return t if point is just after the `#' beginning a real comment.
1373 Does not check to see if previous char is actually `#'.
1374 A real comment is either at the beginning of the buffer,
1375 preceeded only by whitespace on the line, or has a preceeding
1376 semicolon, opening brace, or opening bracket on the same line."
1377 (save-excursion
1378 (backward-char)
1379 (tcl-real-command-p)))
1380
1381 (defun tcl-hairy-scan-for-comment (state end always-stop)
1382 "Determine if point is in a comment.
1383 Returns a list of the form `(FLAG . STATE)'. STATE can be used
1384 as input to future invocations. FLAG is nil if not in comment,
1385 t otherwise. If in comment, leaves point at beginning of comment.
1386 Only works in Emacs 19. See also `tcl-simple-scan-for-comment', a
1387 simpler version that is often right, and works in Emacs 18."
1388 (let ((bol (save-excursion
1389 (goto-char end)
1390 (beginning-of-line)
1391 (point)))
1392 real-comment
1393 last-cstart)
1394 (while (and (not last-cstart) (< (point) end))
1395 (setq real-comment nil) ;In case we've looped around and it is
1396 ;set.
1397 (setq state (parse-partial-sexp (point) end nil nil state t))
1398 (if (nth 4 state)
1399 (progn
1400 ;; If ALWAYS-STOP is set, stop even if we don't have a
1401 ;; real comment, or if the comment isn't on the same line
1402 ;; as the end.
1403 (if always-stop (setq last-cstart (point)))
1404 ;; If we have a real comment, then set the comment
1405 ;; starting point if we are on the same line as the ending
1406 ;; location.
1407 (setq real-comment (tcl-real-comment-p))
1408 (if real-comment
1409 (progn
1410 (and (> (point) bol) (setq last-cstart (point)))
1411 ;; NOTE Emacs 19 has a misfeature whereby calling
1412 ;; parse-partial-sexp with COMMENTSTOP set and with
1413 ;; an initial list that says point is in a comment
1414 ;; will cause an immediate return. So we must skip
1415 ;; over the comment ourselves.
1416 (beginning-of-line 2)))
1417 ;; Frob the state to make it look like we aren't in a
1418 ;; comment.
1419 (setcar (nthcdr 4 state) nil))))
1420 (and last-cstart
1421 (goto-char last-cstart))
1422 (cons real-comment state)))
1423
1424 (defun tcl-hairy-in-comment ()
1425 "Return t if point is in a comment, and leave point at beginning
1426 of comment."
1427 (let ((save (point)))
1428 (tcl-beginning-of-defun)
1429 (car (tcl-hairy-scan-for-comment nil save nil))))
1430
1431 (defun tcl-simple-in-comment ()
1432 "Return t if point is in comment, and leave point at beginning
1433 of comment. This is faster that `tcl-hairy-in-comment', but is
1434 correct less often."
1435 (let ((save (point))
1436 comment)
1437 (beginning-of-line)
1438 (while (and (< (point) save) (not comment))
1439 (search-forward "#" save 'move)
1440 (setq comment (tcl-real-comment-p)))
1441 comment))
1442
1443 (defun tcl-in-comment ()
1444 "Return t if point is in comment, and leave point at beginning
1445 of comment."
1446 (if (and tcl-pps-has-arg-6
1447 tcl-use-hairy-comment-detector)
1448 (tcl-hairy-in-comment)
1449 (tcl-simple-in-comment)))
1450
1451 (defun tcl-do-auto-fill ()
1452 "Auto-fill function for Tcl mode. Only auto-fills in a comment."
1453 (let (in-comment
1454 col)
1455 (save-excursion
1456 (setq in-comment (tcl-in-comment))
1457 (if in-comment
1458 (setq col (1- (current-column)))))
1459 (if in-comment
1460 (progn
1461 (do-auto-fill)
1462 (save-excursion
1463 (back-to-indentation)
1464 (delete-region (point) (save-excursion
1465 (beginning-of-line)
1466 (point)))
1467 (indent-to-column col))))))
1468
1469 \f
1470
1471 ;;
1472 ;; Help-related code.
1473 ;;
1474
1475 (defvar tcl-help-saved-dirs nil
1476 "Saved help directories.
1477 If `tcl-help-directory-list' changes, this allows `tcl-help-on-word'
1478 to update the alist.")
1479
1480 (defvar tcl-help-alist nil
1481 "Alist with command names as keys and filenames as values.")
1482
1483 (defun tcl-help-snarf-commands (dirlist)
1484 "Build alist of commands and filenames."
1485 (while dirlist
1486 (let ((files (directory-files (car dirlist) t)))
1487 (while files
1488 (if (and (file-directory-p (car files))
1489 (not
1490 (let ((fpart (file-name-nondirectory (car files))))
1491 (or (equal fpart ".")
1492 (equal fpart "..")))))
1493 (let ((matches (directory-files (car files) t)))
1494 (while matches
1495 (or (file-directory-p (car matches))
1496 (setq tcl-help-alist
1497 (cons
1498 (cons (file-name-nondirectory (car matches))
1499 (car matches))
1500 tcl-help-alist)))
1501 (setq matches (cdr matches)))))
1502 (setq files (cdr files))))
1503 (setq dirlist (cdr dirlist))))
1504
1505 (defun tcl-reread-help-files ()
1506 "Set up to re-read files, and then do it."
1507 (interactive)
1508 (message "Building Tcl help file index...")
1509 (setq tcl-help-saved-dirs tcl-help-directory-list)
1510 (setq tcl-help-alist nil)
1511 (tcl-help-snarf-commands tcl-help-directory-list)
1512 (message "Building Tcl help file index...done"))
1513
1514 (defun tcl-current-word (flag)
1515 "Return current command word, or nil.
1516 If FLAG is nil, just uses `current-word'.
1517 Otherwise scans backward for most likely Tcl command word."
1518 (if (and flag (eq major-mode 'tcl-mode))
1519 (condition-case nil
1520 (save-excursion
1521 ;; Look backward for first word actually in alist.
1522 (if (bobp)
1523 ()
1524 (while (and (not (bobp))
1525 (not (tcl-real-command-p)))
1526 (backward-sexp)))
1527 (if (assoc (current-word) tcl-help-alist)
1528 (current-word)))
1529 (error nil))
1530 (current-word)))
1531
1532 (defun tcl-help-on-word (command &optional arg)
1533 "Get help on Tcl command. Default is word at point.
1534 Prefix argument means invert sense of `tcl-use-smart-word-finder'."
1535 (interactive
1536 (list
1537 (progn
1538 (if (not (equal tcl-help-directory-list tcl-help-saved-dirs))
1539 (tcl-reread-help-files))
1540 (let ((word (tcl-current-word
1541 (if current-prefix-arg
1542 (not tcl-use-smart-word-finder)
1543 tcl-use-smart-word-finder))))
1544 (completing-read
1545 (if (or (null word) (string= word ""))
1546 "Help on Tcl command: "
1547 (format "Help on Tcl command (default %s): " word))
1548 tcl-help-alist nil t)))
1549 current-prefix-arg))
1550 (if (not (equal tcl-help-directory-list tcl-help-saved-dirs))
1551 (tcl-reread-help-files))
1552 (if (string= command "")
1553 (setq command (tcl-current-word
1554 (if arg
1555 (not tcl-use-smart-word-finder)
1556 tcl-use-smart-word-finder))))
1557 (let* ((help (get-buffer-create "*Tcl help*"))
1558 (cell (assoc command tcl-help-alist))
1559 (file (and cell (cdr cell))))
1560 (set-buffer help)
1561 (delete-region (point-min) (point-max))
1562 (if file
1563 (progn
1564 (insert "*** " command "\n\n")
1565 (insert-file-contents file))
1566 (if (string= command "")
1567 (insert "Magical Pig!")
1568 (insert "Tcl command " command " not in help\n")))
1569 (set-buffer-modified-p nil)
1570 (goto-char (point-min))
1571 (display-buffer help)))
1572
1573 \f
1574
1575 ;;
1576 ;; Other interactive stuff.
1577 ;;
1578
1579 (defvar tcl-previous-dir/file nil
1580 "Record last directory and file used in loading.
1581 This holds a cons cell of the form `(DIRECTORY . FILE)'
1582 describing the last `tcl-load-file' command.")
1583
1584 (defun tcl-load-file (file &optional and-go)
1585 "Load a Tcl file into the inferior Tcl process.
1586 Prefix argument means switch to the Tcl buffer afterwards."
1587 (interactive
1588 (list
1589 ;; car because comint-get-source returns a list holding the
1590 ;; filename.
1591 (car (comint-get-source "Load Tcl file: " tcl-previous-dir/file
1592 '(tcl-mode) t))
1593 current-prefix-arg))
1594 (comint-check-source file)
1595 (setq tcl-previous-dir/file (cons (file-name-directory file)
1596 (file-name-nondirectory file)))
1597 (tcl-send-string (inferior-tcl-proc)
1598 (format inferior-tcl-source-command (tcl-quote file)))
1599 (if and-go (switch-to-tcl t)))
1600
1601 ;; Maybe this should work just like tcl-load-file. But I think what
1602 ;; I've implemented will turn out to be more useful.
1603 (defun tcl-restart-with-file (file &optional and-go)
1604 "Restart inferior Tcl with file.
1605 If an inferior Tcl process exists, it is killed first.
1606 Prefix argument means switch to the Tcl buffer afterwards."
1607 (interactive
1608 (list
1609 (car (comint-get-source "Restart with Tcl file: "
1610 (or (and
1611 (eq major-mode 'tcl-mode)
1612 (buffer-file-name))
1613 tcl-previous-dir/file)
1614 '(tcl-mode) t))
1615 current-prefix-arg))
1616 (let* ((buf (if (eq major-mode 'inferior-tcl-mode)
1617 (current-buffer)
1618 inferior-tcl-buffer))
1619 (proc (and buf (get-process buf))))
1620 (cond
1621 ((not (and buf (get-buffer buf)))
1622 ;; I think this will be ok.
1623 (inferior-tcl tcl-application)
1624 (tcl-load-file file and-go))
1625 ((or
1626 (not (comint-check-proc buf))
1627 (yes-or-no-p
1628 "A Tcl process is running, are you sure you want to reset it? "))
1629 (save-excursion
1630 (comint-check-source file)
1631 (setq tcl-previous-dir/file (cons (file-name-directory file)
1632 (file-name-nondirectory file)))
1633 (comint-exec (get-buffer-create buf)
1634 (if proc
1635 (process-name proc)
1636 "inferior-tcl")
1637 tcl-application file tcl-command-switches)
1638 (if and-go (switch-to-tcl t)))))))
1639
1640 ;; FIXME I imagine you can do this under Emacs 18. I just don't know
1641 ;; how.
1642 (defun tcl-auto-fill-mode (&optional arg)
1643 "Like `auto-fill-mode', but controls filling of Tcl comments."
1644 (interactive "P")
1645 (and (not tcl-using-emacs-19)
1646 (error "You must use Emacs 19 to get this feature."))
1647 ;; Following code taken from "auto-fill-mode" (simple.el).
1648 (prog1
1649 (setq auto-fill-function
1650 (if (if (null arg)
1651 (not auto-fill-function)
1652 (> (prefix-numeric-value arg) 0))
1653 'tcl-do-auto-fill
1654 nil))
1655 ;; Update mode line. FIXME I'd use force-mode-line-update, but I
1656 ;; don't know if it exists in v18.
1657 (set-buffer-modified-p (buffer-modified-p))))
1658
1659 (defun tcl-electric-hash (&optional count)
1660 "Insert a `#' and quote if it does not start a real comment.
1661 Prefix arg is number of `#'s to insert.
1662 See variable `tcl-electric-hash-style' for description of quoting
1663 styles."
1664 (interactive "p")
1665 (or count (setq count 1))
1666 (if (> count 0)
1667 (let ((type
1668 (if (eq tcl-electric-hash-style 'smart)
1669 (if (> count 3) ; FIXME what is "smart"?
1670 'quote
1671 'backslash)
1672 tcl-electric-hash-style))
1673 comment)
1674 (if type
1675 (progn
1676 (save-excursion
1677 (insert "#")
1678 (setq comment (tcl-in-comment)))
1679 (delete-char 1)
1680 (and tcl-explain-indentation (message "comment: %s" comment))
1681 (cond
1682 ((eq type 'quote)
1683 (if (not comment)
1684 (insert "\"")))
1685 ((eq type 'backslash)
1686 ;; The following will set count to 0, so the
1687 ;; insert-char can still be run.
1688 (if (not comment)
1689 (while (> count 0)
1690 (insert "\\#")
1691 (setq count (1- count)))))
1692 (t nil))))
1693 (insert-char ?# count))))
1694
1695 (defun tcl-hashify-buffer ()
1696 "Quote all `#'s in current buffer that aren't Tcl comments."
1697 (interactive)
1698 (save-excursion
1699 (goto-char (point-min))
1700 (if (and tcl-pps-has-arg-6 tcl-use-hairy-comment-detector)
1701 (let (state
1702 result)
1703 (while (< (point) (point-max))
1704 (setq result (tcl-hairy-scan-for-comment state (point-max) t))
1705 (if (car result)
1706 (beginning-of-line 2)
1707 (backward-char)
1708 (if (eq ?# (following-char))
1709 (insert "\\"))
1710 (forward-char))
1711 (setq state (cdr result))))
1712 (while (and (< (point) (point-max))
1713 (search-forward "#" nil 'move))
1714 (if (tcl-real-comment-p)
1715 (beginning-of-line 2)
1716 ;; There's really no good way for the simple converter to
1717 ;; work. So we just quote # if it isn't already quoted.
1718 ;; Bogus, but it works.
1719 (backward-char)
1720 (if (not (eq ?\\ (preceding-char)))
1721 (insert "\\"))
1722 (forward-char))))))
1723
1724 (defun tcl-indent-for-comment ()
1725 "Indent this line's comment to comment column, or insert an empty comment.
1726 Is smart about syntax of Tcl comments.
1727 Parts of this were taken from indent-for-comment (simple.el)."
1728 (interactive "*")
1729 (end-of-line)
1730 (or (tcl-in-comment)
1731 (progn
1732 ;; Not in a comment, so we have to insert one. Create an
1733 ;; empty comment (since there isn't one on this line). If
1734 ;; line is not blank, make sure we insert a ";" first.
1735 (skip-chars-backward " \t")
1736 (let ((eolpoint (point)))
1737 (beginning-of-line)
1738 (if (/= (point) eolpoint)
1739 (progn
1740 (goto-char eolpoint)
1741 (or (tcl-real-command-p)
1742 (progn
1743 (insert ";# ")
1744 (backward-char))))))))
1745 ;; Point is just after the "#" starting a comment. Move it as
1746 ;; appropriate.
1747 (let* ((indent (if comment-indent-hook
1748 (funcall comment-indent-hook)
1749 (funcall comment-indent-function)))
1750 (begpos (progn
1751 (backward-char)
1752 (point))))
1753 (if (/= begpos indent)
1754 (progn
1755 (skip-chars-backward " \t" (save-excursion
1756 (beginning-of-line)
1757 (point)))
1758 (delete-region (point) begpos)
1759 (indent-to indent)))
1760 (looking-at comment-start-skip) ; Always true.
1761 (goto-char (match-end 0))
1762 ;; I don't like the effect of the next two.
1763 ;;(skip-chars-backward " \t" (match-beginning 0))
1764 ;;(skip-chars-backward "^ \t" (match-beginning 0))
1765 ))
1766
1767 ;; The following was inspired by the Tcl editing mode written by
1768 ;; Gregor Schmid <schmid@fb3-s7.math.TU-Berlin.DE>. His version also
1769 ;; attempts to snarf the command line options from the command line,
1770 ;; but I didn't think that would really be that helpful (doesn't seem
1771 ;; like it owould be right enough. His version also looks for the
1772 ;; "#!/bin/csh ... exec" hack, but that seemed even less useful.
1773 ;; FIXME should make sure that the application mentioned actually
1774 ;; exists.
1775 (defun tcl-guess-application ()
1776 "Attempt to guess Tcl application by looking at first line.
1777 The first line is assumed to look like \"#!.../program ...\"."
1778 (save-excursion
1779 (goto-char (point-min))
1780 (if (looking-at "#![^ \t]*/\\([^ \t/]+\\)\\([ \t]\\|$\\)")
1781 (progn
1782 (make-local-variable 'tcl-application)
1783 (setq tcl-application (buffer-substring (match-beginning 1)
1784 (match-end 1)))))))
1785
1786 ;; This only exists to put on the menubar. I couldn't figure out any
1787 ;; other way to do it. FIXME should take "number of #-marks"
1788 ;; argument.
1789 (defun tcl-uncomment-region (beg end)
1790 "Uncomment region."
1791 (interactive "r")
1792 (comment-region beg end -1))
1793
1794 \f
1795
1796 ;;
1797 ;; Lucid menu support.
1798 ;; Taken from schmid@fb3-s7.math.TU-Berlin.DE (Gregor Schmid),
1799 ;; who wrote a different Tcl mode.
1800 ;; We also have simple support for menus in FSF. We do this by
1801 ;; loading the Lucid menu emulation code.
1802 ;;
1803
1804 ;; Put this into your tcl-mode-hook.
1805 (defun tcl-install-menubar ()
1806 (and tcl-using-emacs-19
1807 (not tcl-using-lemacs-19)
1808 (if tcl-using-emacs-19.23
1809 (require 'lmenu)
1810 ;; CAVEATS:
1811 ;; * lmenu.el provides 'menubar, which is bogus.
1812 ;; * lmenu.el causes menubars to be turned on everywhere.
1813 ;; Doubly bogus!
1814 ;; Both of these problems are fixed in Emacs 19.23. People
1815 ;; using an Emacs before that just suffer.
1816 (require 'menubar "lmenu")))
1817 (if (not (assoc "Tcl" current-menubar))
1818 (progn
1819 (set-buffer-menubar (copy-sequence current-menubar))
1820 (add-menu nil "Tcl" (cdr tcl-lucid-menu))))
1821 ;; You might want to do something like the below. I have it
1822 ;; commented out because it overrides existing bindings.
1823 ;; For Lucid:
1824 ;; (define-key tcl-mode-map 'button3 'tcl-popup-menu)
1825 ;; For FSF:
1826 ;; (define-key tcl-mode-map [down-mouse-3] 'tcl-popup-menu)
1827 )
1828
1829 (defun tcl-popup-menu (e)
1830 (interactive "e")
1831 (and tcl-using-emacs-19
1832 (not tcl-using-lemacs-19)
1833 (if tcl-using-emacs-19.23
1834 (require 'lmenu)
1835 ;; CAVEATS:
1836 ;; * lmenu.el provides 'menubar, which is bogus.
1837 ;; * lmenu.el causes menubars to be turned on everywhere.
1838 ;; Doubly bogus!
1839 ;; Both of these problems are fixed in Emacs 19.23. People
1840 ;; using an Emacs before that just suffer.
1841 (require 'menubar "lmenu"))) ;; This is annoying
1842 ;;(mouse-set-point e)
1843 ;; IMHO popup-menu should be autoloaded. Oh well.
1844 (popup-menu tcl-lucid-menu))
1845
1846 \f
1847
1848 ;;
1849 ;; Quoting and unquoting functions.
1850 ;;
1851
1852 ;; This quoting is sufficient to protect eg a filename from any sort
1853 ;; of expansion or splitting. Tcl quoting sure sucks.
1854 (defun tcl-quote (string)
1855 "Quote STRING according to Tcl rules."
1856 (mapconcat (function (lambda (char)
1857 (if (memq char '(?[ ?] ?{ ?} ?\\ ?\" ?$ ? ?\;))
1858 (concat "\\" (char-to-string char))
1859 (char-to-string char))))
1860 string ""))
1861
1862 \f
1863
1864 (provide 'tcl)
1865
1866 ;;; tcl.el ends here