]> code.delx.au - gnu-emacs/blob - lisp/progmodes/tcl.el
(tcl-keyword-list): Added method, body, configbody, class
[gnu-emacs] / lisp / progmodes / tcl.el
1 ;; tcl.el --- Tcl code editing commands for Emacs
2
3 ;; Copyright (C) 1994, 1998, 1999 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.56 $
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 2, 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: 1999/07/18 18:40:27 $|$Revision: 1.56 $|~/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.56 1999/07/18 18:40:27 tromey
69 ;; (tcl-mode): Set font-lock-defaults, not font-lock-keywords.
70 ;;
71 ;; Revision 1.55 1999/07/18 05:21:33 tromey
72 ;; (tcl-proc-list): Reverted; already had `body'.
73 ;;
74 ;; Revision 1.54 1999/07/18 05:19:57 tromey
75 ;; (tcl-proc-list): Added `body'.
76 ;;
77 ;; Revision 1.53 1999/03/29 07:35:55 tromey
78 ;; (tcl-using-emacs-19-23): Recognize Emacs 20.
79 ;;
80 ;; Revision 1.52 1998/07/02 17:47:49 tromey
81 ;; Some itcl additions:
82 ;; (tcl-typeword-list): Added private, itk_option.
83 ;; (tcl-proc-list): Added body, configbody, class.
84 ;;
85 ;; Revision 1.51 1996/09/12 17:02:52 tromey
86 ;; (tcl-indent-command): Use insert-tab, not self-insert-command.
87 ;;
88 ;; Revision 1.50 1996/03/23 05:14:50 tromey
89 ;; (tcl-using-emacs-19): Work with XEmacs 20.0. From Ben Wing.
90 ;;
91 ;; Revision 1.49 1995/12/07 18:27:47 tromey
92 ;; (add-log-tcl-defun): Don't use tcl-beginning-of-defun; just go to end
93 ;; of line before searching.
94 ;;
95 ;; Revision 1.48 1995/12/07 18:18:21 tromey
96 ;; (add-log-tcl-defun): Now uses tcl-beginning-of-defun.
97 ;;
98 ;; Revision 1.47 1995/08/22 17:49:45 tromey
99 ;; (tcl-hilit): New function from "Chris Alfeld" <calfeld@math.utah.edu>
100 ;; (tcl-mode): Call it
101 ;;
102 ;; Revision 1.46 1995/08/07 16:02:01 tromey
103 ;; (tcl-do-auto-fill): Only fill past fill-column; for 19.29.
104 ;; (tcl-auto-fill-mode): Use force-mode-line-update.
105 ;;
106 ;; Revision 1.45 1995/07/23 23:51:25 tromey
107 ;; (tcl-word-no-props): New function.
108 ;; (tcl-figure-type): Use it.
109 ;; (tcl-current-word): Ditto.
110 ;;
111 ;; Revision 1.44 1995/07/23 20:26:47 tromey
112 ;; Doc fixes.
113 ;;
114 ;; Revision 1.43 1995/07/17 19:59:49 tromey
115 ;; (inferior-tcl-mode): Use modeline-process if it exists.
116 ;;
117 ;; Revision 1.42 1995/07/17 19:55:25 tromey
118 ;; XEmacs currently must use tcl-internal-end-of-defun
119 ;;
120 ;; Revision 1.41 1995/07/14 21:54:56 tromey
121 ;; Changes to make menus work in XEmacs.
122 ;; From Mike Scheidler <c23mts@kocrsv01.delcoelect.com>
123 ;;
124 ;; Revision 1.40 1995/07/11 03:13:15 tromey
125 ;; (tcl-mode): Customize for new dabbrev.
126 ;;
127 ;; Revision 1.39 1995/07/09 21:58:03 tromey
128 ;; (tcl-do-fill-paragraph): New function.
129 ;; (tcl-mode): Set up for paragraph filling.
130 ;;
131 ;; Revision 1.38 1995/07/09 21:30:32 tromey
132 ;; (tcl-mode): Fixes to 19.29 paragraph variables.
133 ;;
134 ;; Revision 1.37 1995/07/09 18:52:16 tromey
135 ;; (tcl-do-auto-fill): Set fill-prefix.
136 ;;
137 ;; Revision 1.36 1995/07/09 01:07:57 tromey
138 ;; (tcl-imenu-create-index-function): Work with imenu from Emacs 19.29
139 ;;
140 ;; Revision 1.35 1995/06/27 20:12:00 tromey
141 ;; (tcl-type-alist): More itcl changes.
142 ;;
143 ;; Revision 1.34 1995/06/27 20:06:05 tromey
144 ;; More changes for itcl.
145 ;; Bug fixes for Emacs 19.29.
146 ;;
147 ;; Revision 1.33 1995/06/27 20:01:29 tromey
148 ;; (tcl-set-proc-regexp): Allow leading spaces.
149 ;; (tcl-proc-list): Changes for itcl.
150 ;; (tcl-typeword-list): Ditto.
151 ;; (tcl-keyword-list): Ditto.
152 ;;
153 ;; Revision 1.32 1995/05/11 22:12:49 tromey
154 ;; (tcl-type-alist): Include entry for "proc".
155 ;;
156 ;; Revision 1.31 1995/05/10 23:38:12 tromey
157 ;; (tcl-add-fsf-menu): Use make-lucid-menu-keymap, not
158 ;; "make-xemacs-menu-keymap".
159 ;;
160 ;; Revision 1.30 1995/05/10 18:22:21 tromey
161 ;; Bug fix in menu code for XEmacs.
162 ;;
163 ;; Revision 1.29 1995/05/09 21:36:53 tromey
164 ;; Changed "Lucid Emacs" to "XEmacs".
165 ;; Tcl's popup menu now added to existing one, courtesy
166 ;; dfarmer@evolving.com (Doug Farmer)
167 ;;
168 ;; Revision 1.28 1995/04/08 19:52:50 tromey
169 ;; (tcl-outline-level): New function
170 ;; (tcl-mode): Added outline-handling stuff.
171 ;; From Jesper Pedersen <blackie@imada.ou.dk>
172 ;;
173 ;; Revision 1.27 1994/10/11 02:01:27 tromey
174 ;; (tcl-mode): imenu-create-index-function made buffer local.
175 ;;
176 ;; Revision 1.26 1994/09/01 18:06:24 tromey
177 ;; Added filename completion in inferior tcl mode
178 ;;
179 ;; Revision 1.25 1994/08/22 15:56:24 tromey
180 ;; tcl-load-file default to current buffer.
181 ;;
182 ;; Revision 1.24 1994/08/21 20:33:05 tromey
183 ;; Fixed bug in tcl-guess-application.
184 ;;
185 ;; Revision 1.23 1994/08/21 03:54:45 tromey
186 ;; Keybindings don't overshadown comint bindings.
187 ;;
188 ;; Revision 1.22 1994/07/26 00:46:07 tromey
189 ;; Emacs 18 changes from Carl Witty.
190 ;;
191 ;; Revision 1.21 1994/07/14 22:49:21 tromey
192 ;; Added ";;;###autoload" comments where appropriate.
193 ;;
194 ; Revision 1.20 1994/06/05 16:57:22 tromey
195 ; tcl-current-word does the right thing in inferior-tcl-mode.
196 ;
197 ; Revision 1.19 1994/06/03 21:09:19 tromey
198 ; Another menu fix.
199 ;
200 ; Revision 1.18 1994/06/03 20:39:14 tromey
201 ; Fixed menu bug.
202 ;
203 ; Revision 1.17 1994/06/03 00:47:15 tromey
204 ; Fixed bug in bug-reporting code.
205 ;
206 ; Revision 1.16 1994/05/26 05:06:14 tromey
207 ; Menu items now sensitive as appropriate.
208 ;
209 ; Revision 1.15 1994/05/22 20:38:11 tromey
210 ; Added bug-report keybindings and menu entries.
211 ;
212 ; Revision 1.14 1994/05/22 20:18:28 tromey
213 ; Even more compile stuff.
214 ;
215 ; Revision 1.13 1994/05/22 20:17:15 tromey
216 ; Moved emacs version checking code to very beginning.
217 ;
218 ; Revision 1.12 1994/05/22 20:14:59 tromey
219 ; Compile fixes.
220 ;
221 ; Revision 1.11 1994/05/22 20:12:44 tromey
222 ; Fixed mark-defun for 19.23.
223 ; More menu fixes.
224 ;
225 ; Revision 1.10 1994/05/22 20:02:03 tromey
226 ; Fixed bug with M-;.
227 ; Wrote bug-reporting code.
228 ;
229 ; Revision 1.9 1994/05/22 05:26:51 tromey
230 ; Fixes for imenu.
231 ;
232 ; Revision 1.8 1994/05/22 03:38:07 tromey
233 ; Fixed menu support.
234 ;
235 ; Revision 1.7 1994/05/03 01:23:42 tromey
236 ; *** empty log message ***
237 ;
238 ; Revision 1.6 1994/04/23 16:23:36 tromey
239 ; Wrote tcl-indent-for-comment
240 ;
241 ;;
242 ;; 18-Mar-1994 Tom Tromey Fourth beta release.
243 ;; Added {un,}comment-region to menu. Idea from
244 ;; Mike Scheidler <c23mts@kocrsv01.delcoelect.com>
245 ;; 17-Mar-1994 Tom Tromey
246 ;; Fixed tcl-restart-with-file. Bug fix attempt in
247 ;; tcl-internal-end-of-defun.
248 ;; 16-Mar-1994 Tom Tromey Third beta release
249 ;; Added support code for menu (from Tcl mode written by
250 ;; schmid@fb3-s7.math.TU-Berlin.DE (Gregor Schmid)).
251 ;; 12-Mar-1994 Tom Tromey
252 ;; Better documentation for inferior-tcl-buffer. Wrote
253 ;; tcl-restart-with-file. Wrote Lucid Emacs menu (but no
254 ;; code to install it).
255 ;; 12-Mar-1994 Tom Tromey
256 ;; Wrote tcl-guess-application. Another stab at making
257 ;; tcl-omit-ws-regexp work.
258 ;; 10-Mar-1994 Tom Tromey Second beta release
259 ;; Last Modified: Thu Mar 10 01:24:25 1994 (Tom Tromey)
260 ;; Wrote perl-mode style line indentation command.
261 ;; Wrote more documentation. Added tcl-continued-indent-level.
262 ;; Integrated help code.
263 ;; 8-Mar-1994 Tom Tromey
264 ;; Last Modified: Tue Mar 8 11:58:44 1994 (Tom Tromey)
265 ;; Bug fixes.
266 ;; 6-Mar-1994 Tom Tromey
267 ;; Last Modified: Sun Mar 6 18:55:41 1994 (Tom Tromey)
268 ;; Updated auto-newline support.
269 ;; 6-Mar-1994 Tom Tromey Beta release
270 ;; Last Modified: Sat Mar 5 17:24:32 1994 (Tom Tromey)
271 ;; Wrote tcl-hashify-buffer. Other minor bug fixes.
272 ;; 5-Mar-1994 Tom Tromey
273 ;; Last Modified: Sat Mar 5 16:11:20 1994 (Tom Tromey)
274 ;; Wrote electric-hash code.
275 ;; 3-Mar-1994 Tom Tromey
276 ;; Last Modified: Thu Mar 3 02:53:40 1994 (Tom Tromey)
277 ;; Added code to handle auto-fill in comments.
278 ;; Added imenu support code.
279 ;; Cleaned up code.
280 ;; Better font-lock support.
281 ;; 28-Feb-1994 Tom Tromey
282 ;; Last Modified: Mon Feb 28 14:08:05 1994 (Tom Tromey)
283 ;; Made tcl-figure-type more easily configurable.
284 ;; 28-Feb-1994 Tom Tromey
285 ;; Last Modified: Mon Feb 28 01:02:58 1994 (Tom Tromey)
286 ;; Wrote inferior-tcl mode.
287 ;; 16-Feb-1994 Tom Tromey
288 ;; Last Modified: Wed Feb 16 17:05:19 1994 (Tom Tromey)
289 ;; Added support for font-lock-mode.
290 ;; 29-Oct-1993 Tom Tromey
291 ;; Last Modified: Sun Oct 24 17:39:14 1993 (Tom Tromey)
292 ;; Patches from Guido Bosch to make things work with Lucid Emacs.
293 ;; 22-Oct-1993 Tom Tromey
294 ;; Last Modified: Fri Oct 22 15:26:46 1993 (Tom Tromey)
295 ;; Made many characters have "_" syntax class; suggested by Guido
296 ;; Bosch <Guido.Bosch@loria.fr>. Note that this includes the "$"
297 ;; character, which might be a change you'd notice.
298 ;; 21-Oct-1993 Tom Tromey
299 ;; Last Modified: Thu Oct 21 20:28:40 1993 (Tom Tromey)
300 ;; More fixes for tcl-omit-ws-regexp.
301 ;; 20-Oct-1993 Tom Tromey
302 ;; Started keeping history. Fixed tcl-{beginning,end}-of-defun.
303 ;; Added some code to make things work with Emacs 18.
304
305 ;; THANKS TO:
306 ;; Guido Bosch <Guido.Bosch@loria.fr>
307 ;; pgs1002@esc.cam.ac.uk (Dr P.G. Sjoerdsma)
308 ;; Mike Scheidler <c23mts@kocrsv01.delcoelect.com>
309 ;; Matt Newman <men@charney.colorado.edu>
310 ;; rwhitby@research.canon.oz.au (Rod Whitby)
311 ;; h9118101@hkuxa.hku.hk (Yip Chi Lap [Beta])
312 ;; Pertti Tapio Kasanen <ptk@delta.hut.fi>
313 ;; schmid@fb3-s7.math.TU-Berlin.DE (Gregor Schmid)
314 ;; warsaw@nlm.nih.gov (Barry A. Warsaw)
315 ;; Carl Witty <cwitty@ai.mit.edu>
316 ;; T. V. Raman <raman@crl.dec.com>
317 ;; Jesper Pedersen <blackie@imada.ou.dk>
318 ;; dfarmer@evolving.com (Doug Farmer)
319 ;; "Chris Alfeld" <calfeld@math.utah.edu>
320 ;; Ben Wing <wing@666.com>
321
322 ;; KNOWN BUGS:
323 ;; * indent-region should skip blank lines. (It does in v19, so I'm
324 ;; not motivated to fix it here).
325 ;; * In Tcl "#" is not always a comment character. This can confuse
326 ;; tcl.el in certain circumstances. For now the only workaround is
327 ;; to enclose offending hash characters in quotes or precede it with
328 ;; a backslash. Note that using braces won't work -- quotes change
329 ;; the syntax class of characters between them, while braces do not.
330 ;; The electric-# mode helps alleviate this problem somewhat.
331 ;; * indent-tcl-exp is untested.
332 ;; * Doesn't work under Emacs 18 yet.
333 ;; * There's been a report that font-lock does strange things under
334 ;; Lucid Emacs 19.6. For instance in "proc foobar", the space
335 ;; before "foobar" is highlighted.
336
337 ;; TODO:
338 ;; * make add-log-tcl-defun smarter. should notice if we are in the
339 ;; middle of a defun, or between defuns. should notice if point is
340 ;; on first line of defun (or maybe even in comments before defun).
341 ;; * Allow continuation lines to be indented under the first argument
342 ;; of the preceeding line, like this:
343 ;; [list something \
344 ;; something-else]
345 ;; * There is a request that indentation work like this:
346 ;; button .fred -label Fred \
347 ;; -command {puts fred}
348 ;; * Should have tcl-complete-symbol that queries the inferior process.
349 ;; * Should have describe-symbol that works by sending the magic
350 ;; command to a tclX process.
351 ;; * Need C-x C-e binding (tcl-eval-last-exp).
352 ;; * Write indent-region function that is faster than indenting each
353 ;; line individually.
354 ;; * tcl-figure-type should stop at "beginning of line" (only ws
355 ;; before point, and no "\" on previous line). (see tcl-real-command-p).
356 ;; * overrides some comint keybindings; fix.
357 ;; * Trailing \ will eat blank lines. Should deal with this.
358 ;; (this would help catch some potential bugs).
359 ;; * Inferior should display in half the screen, not the whole screen.
360 ;; * Indentation should deal with "switch".
361 ;; * Consider writing code to find help files automatically (for
362 ;; common cases).
363 ;; * `#' shouldn't insert `\#' when point is in string.
364
365 \f
366
367 ;;; Code:
368
369 ;; I sure wish Emacs had a package that made it easy to extract this
370 ;; sort of information. Strange definition works with XEmacs 20.0.
371 (defconst tcl-using-emacs-19 (not (string-match "18\\." emacs-version))
372 "Nil unless using Emacs 19 (XEmacs or FSF).")
373
374 ;; FIXME this will break on Emacs 19.100.
375 (defconst tcl-using-emacs-19-23
376 (or (string-match "19\\.\\(2[3-9]\\|[3-9][0-9]\\)" emacs-version)
377 (string-match "^20\\." emacs-version))
378 "Nil unless using Emacs 19-23 or later.")
379
380 (defconst tcl-using-xemacs-19 (string-match "XEmacs" emacs-version)
381 "Nil unless using XEmacs).")
382
383 (require 'comint)
384
385 ;; When compiling under GNU Emacs, load imenu during compilation. If
386 ;; you have 19.22 or earlier, comment this out, or get imenu.
387 (and (fboundp 'eval-when-compile)
388 (eval-when-compile
389 (if (and (string-match "19\\." emacs-version)
390 (not (string-match "XEmacs" emacs-version)))
391 (require 'imenu))
392 ()))
393
394 (defconst tcl-version "$Revision: 1.56 $")
395 (defconst tcl-maintainer "Tom Tromey <tromey@drip.colorado.edu>")
396
397 ;;
398 ;; User variables.
399 ;;
400
401 (defvar tcl-indent-level 4
402 "*Indentation of Tcl statements with respect to containing block.")
403
404 (defvar tcl-continued-indent-level 4
405 "*Indentation of continuation line relative to first line of command.")
406
407 (defvar tcl-auto-newline nil
408 "*Non-nil means automatically newline before and after braces
409 inserted in Tcl code.")
410
411 (defvar tcl-tab-always-indent t
412 "*Control effect of TAB key.
413 If t (the default), always indent current line.
414 If nil and point is not in the indentation area at the beginning of
415 the line, a TAB is inserted.
416 Other values cause the first possible action from the following list
417 to take place:
418
419 1. Move from beginning of line to correct indentation.
420 2. Delete an empty comment.
421 3. Move forward to start of comment, indenting if necessary.
422 4. Move forward to end of line, indenting if necessary.
423 5. Create an empty comment.
424 6. Move backward to start of comment, indenting if necessary.")
425
426 (defvar tcl-use-hairy-comment-detector t
427 "*If not `nil', the the more complicated, but slower, comment
428 detecting function is used. This variable is only used in GNU Emacs
429 19 (the fast function is always used elsewhere).")
430
431 (defvar tcl-electric-hash-style 'smart
432 "*Style of electric hash insertion to use.
433 Possible values are 'backslash, meaning that `\\' quoting should be
434 done; 'quote, meaning that `\"' quoting should be done; 'smart,
435 meaning that the choice between 'backslash and 'quote should be
436 made depending on the number of hashes inserted; or nil, meaning that
437 no quoting should be done. Any other value for this variable is
438 taken to mean 'smart. The default is 'smart.")
439
440 (defvar tcl-help-directory-list nil
441 "*List of topmost directories containing TclX help files")
442
443 (defvar tcl-use-smart-word-finder t
444 "*If not nil, use a better way of finding the current word when
445 looking up help on a Tcl command.")
446
447 (defvar tcl-application "wish"
448 "*Name of Tcl application to run in inferior Tcl mode.")
449
450 (defvar tcl-command-switches nil
451 "*Switches to supply to `tcl-application'.")
452
453 (defvar tcl-prompt-regexp "^\\(% \\|\\)"
454 "*If not nil, a regexp that will match the prompt in the inferior process.
455 If nil, the prompt is the name of the application with \">\" appended.
456
457 The default is \"^\\(% \\|\\)\", which will match the default primary
458 and secondary prompts for tclsh and wish.")
459
460 (defvar inferior-tcl-source-command "source %s\n"
461 "*Format-string for building a Tcl command to load a file.
462 This format string should use `%s' to substitute a file name
463 and should result in a Tcl expression that will command the
464 inferior Tcl to load that file. The filename will be appropriately
465 quoted for Tcl.")
466
467 ;;
468 ;; Keymaps, abbrevs, syntax tables.
469 ;;
470
471 (defvar tcl-mode-abbrev-table nil
472 "Abbrev table in use in Tcl-mode buffers.")
473 (if tcl-mode-abbrev-table
474 ()
475 (define-abbrev-table 'tcl-mode-abbrev-table ()))
476
477 (defvar tcl-mode-map ()
478 "Keymap used in Tcl mode.")
479
480 (defvar tcl-mode-syntax-table nil
481 "Syntax table in use in Tcl-mode buffers.")
482 (if tcl-mode-syntax-table
483 ()
484 (setq tcl-mode-syntax-table (make-syntax-table))
485 (modify-syntax-entry ?% "_" tcl-mode-syntax-table)
486 (modify-syntax-entry ?@ "_" tcl-mode-syntax-table)
487 (modify-syntax-entry ?& "_" tcl-mode-syntax-table)
488 (modify-syntax-entry ?* "_" tcl-mode-syntax-table)
489 (modify-syntax-entry ?+ "_" tcl-mode-syntax-table)
490 (modify-syntax-entry ?- "_" tcl-mode-syntax-table)
491 (modify-syntax-entry ?. "_" tcl-mode-syntax-table)
492 (modify-syntax-entry ?: "_" tcl-mode-syntax-table)
493 (modify-syntax-entry ?! "_" tcl-mode-syntax-table)
494 (modify-syntax-entry ?$ "_" tcl-mode-syntax-table) ; FIXME use "'"?
495 (modify-syntax-entry ?/ "_" tcl-mode-syntax-table)
496 (modify-syntax-entry ?~ "_" tcl-mode-syntax-table)
497 (modify-syntax-entry ?< "_" tcl-mode-syntax-table)
498 (modify-syntax-entry ?= "_" tcl-mode-syntax-table)
499 (modify-syntax-entry ?> "_" tcl-mode-syntax-table)
500 (modify-syntax-entry ?| "_" tcl-mode-syntax-table)
501 (modify-syntax-entry ?\( "()" tcl-mode-syntax-table)
502 (modify-syntax-entry ?\) ")(" tcl-mode-syntax-table)
503 (modify-syntax-entry ?\; "." tcl-mode-syntax-table)
504 (modify-syntax-entry ?\n "> " tcl-mode-syntax-table)
505 (modify-syntax-entry ?\f "> " tcl-mode-syntax-table)
506 (modify-syntax-entry ?# "< " tcl-mode-syntax-table))
507
508 (defvar inferior-tcl-mode-map nil
509 "Keymap used in Inferior Tcl mode.")
510
511 ;; XEmacs menu.
512 (defvar tcl-xemacs-menu
513 '(["Beginning of function" tcl-beginning-of-defun t]
514 ["End of function" tcl-end-of-defun t]
515 ["Mark function" tcl-mark-defun t]
516 ["Indent region" indent-region (tcl-mark)]
517 ["Comment region" comment-region (tcl-mark)]
518 ["Uncomment region" tcl-uncomment-region (tcl-mark)]
519 "----"
520 ["Show Tcl process buffer" inferior-tcl t]
521 ["Send function to Tcl process" tcl-eval-defun
522 (and inferior-tcl-buffer (get-buffer inferior-tcl-buffer))]
523 ["Send region to Tcl process" tcl-eval-region
524 (and inferior-tcl-buffer (get-buffer inferior-tcl-buffer))]
525 ["Send file to Tcl process" tcl-load-file
526 (and inferior-tcl-buffer (get-buffer inferior-tcl-buffer))]
527 ["Restart Tcl process with file" tcl-restart-with-file t]
528 "----"
529 ["Tcl help" tcl-help-on-word tcl-help-directory-list]
530 ["Send bug report" tcl-submit-bug-report t])
531 "XEmacs menu for Tcl mode.")
532
533 ;; GNU Emacs does menus via keymaps. Do it in a function in case we
534 ;; later decide to add it to inferior Tcl mode as well.
535 (defun tcl-add-fsf-menu (map)
536 (define-key map [menu-bar] (make-sparse-keymap))
537 ;; This fails in Emacs 19.22 and earlier.
538 (require 'lmenu)
539 (let ((menu (make-lucid-menu-keymap "Tcl" tcl-xemacs-menu)))
540 (define-key map [menu-bar tcl] (cons "Tcl" menu))
541 ;; The following is intended to compute the key sequence
542 ;; information for the menu. It doesn't work.
543 (x-popup-menu nil menu)))
544
545 (defun tcl-fill-mode-map ()
546 (define-key tcl-mode-map "{" 'tcl-electric-char)
547 (define-key tcl-mode-map "}" 'tcl-electric-brace)
548 (define-key tcl-mode-map "[" 'tcl-electric-char)
549 (define-key tcl-mode-map "]" 'tcl-electric-char)
550 (define-key tcl-mode-map ";" 'tcl-electric-char)
551 (define-key tcl-mode-map "#" 'tcl-electric-hash)
552 ;; FIXME.
553 (define-key tcl-mode-map "\e\C-a" 'tcl-beginning-of-defun)
554 ;; FIXME.
555 (define-key tcl-mode-map "\e\C-e" 'tcl-end-of-defun)
556 ;; FIXME.
557 (define-key tcl-mode-map "\e\C-h" 'tcl-mark-defun)
558 (define-key tcl-mode-map "\e\C-q" 'indent-tcl-exp)
559 (define-key tcl-mode-map "\177" 'backward-delete-char-untabify)
560 (define-key tcl-mode-map "\t" 'tcl-indent-command)
561 (define-key tcl-mode-map "\M-;" 'tcl-indent-for-comment)
562 (define-key tcl-mode-map "\M-\C-x" 'tcl-eval-defun)
563 (define-key tcl-mode-map "\C-c\C-b" 'tcl-submit-bug-report)
564 (and (fboundp 'comment-region)
565 (define-key tcl-mode-map "\C-c\C-c" 'comment-region))
566 (define-key tcl-mode-map "\C-c\C-i" 'tcl-help-on-word)
567 (define-key tcl-mode-map "\C-c\C-v" 'tcl-eval-defun)
568 (define-key tcl-mode-map "\C-c\C-f" 'tcl-load-file)
569 (define-key tcl-mode-map "\C-c\C-t" 'inferior-tcl)
570 (define-key tcl-mode-map "\C-c\C-x" 'tcl-eval-region)
571 (define-key tcl-mode-map "\C-c\C-s" 'switch-to-tcl)
572
573 ;; Make menus.
574 (if (and tcl-using-emacs-19 (not tcl-using-xemacs-19))
575 (progn
576 (tcl-add-fsf-menu tcl-mode-map))))
577
578 (defun tcl-fill-inferior-map ()
579 (define-key inferior-tcl-mode-map "\t" 'comint-dynamic-complete)
580 (define-key inferior-tcl-mode-map "\M-?"
581 'comint-dynamic-list-filename-completions)
582 (define-key inferior-tcl-mode-map "\e\C-a" 'tcl-beginning-of-defun)
583 (define-key inferior-tcl-mode-map "\e\C-e" 'tcl-end-of-defun)
584 (define-key inferior-tcl-mode-map "\177" 'backward-delete-char-untabify)
585 (define-key inferior-tcl-mode-map "\M-\C-x" 'tcl-eval-defun)
586 (define-key inferior-tcl-mode-map "\C-c\C-b" 'tcl-submit-bug-report)
587 (define-key inferior-tcl-mode-map "\C-c\C-i" 'tcl-help-on-word)
588 (define-key inferior-tcl-mode-map "\C-c\C-v" 'tcl-eval-defun)
589 (define-key inferior-tcl-mode-map "\C-c\C-f" 'tcl-load-file)
590 (define-key inferior-tcl-mode-map "\C-c\C-t" 'inferior-tcl)
591 (define-key inferior-tcl-mode-map "\C-c\C-x" 'tcl-eval-region)
592 (define-key inferior-tcl-mode-map "\C-c\C-s" 'switch-to-tcl))
593
594 (if tcl-mode-map
595 ()
596 (setq tcl-mode-map (make-sparse-keymap))
597 (tcl-fill-mode-map))
598
599 (if inferior-tcl-mode-map
600 ()
601 ;; FIXME Use keymap inheritance here? FIXME we override comint
602 ;; keybindings here. Maybe someone has a better set?
603 (setq inferior-tcl-mode-map (copy-keymap comint-mode-map))
604 (tcl-fill-inferior-map))
605
606
607 (defvar inferior-tcl-buffer nil
608 "*The current inferior-tcl process buffer.
609
610 MULTIPLE PROCESS SUPPORT
611 ===========================================================================
612 To run multiple Tcl processes, you start the first up with
613 \\[inferior-tcl]. It will be in a buffer named `*inferior-tcl*'.
614 Rename this buffer with \\[rename-buffer]. You may now start up a new
615 process with another \\[inferior-tcl]. It will be in a new buffer,
616 named `*inferior-tcl*'. You can switch between the different process
617 buffers with \\[switch-to-buffer].
618
619 Commands that send text from source buffers to Tcl processes -- like
620 `tcl-eval-defun' or `tcl-load-file' -- have to choose a process to
621 send to, when you have more than one Tcl process around. This is
622 determined by the global variable `inferior-tcl-buffer'. Suppose you
623 have three inferior Lisps running:
624 Buffer Process
625 foo inferior-tcl
626 bar inferior-tcl<2>
627 *inferior-tcl* inferior-tcl<3>
628 If you do a \\[tcl-eval-defun] command on some Lisp source code, what
629 process do you send it to?
630
631 - If you're in a process buffer (foo, bar, or *inferior-tcl*),
632 you send it to that process.
633 - If you're in some other buffer (e.g., a source file), you
634 send it to the process attached to buffer `inferior-tcl-buffer'.
635 This process selection is performed by function `inferior-tcl-proc'.
636
637 Whenever \\[inferior-tcl] fires up a new process, it resets
638 `inferior-tcl-buffer' to be the new process's buffer. If you only run
639 one process, this does the right thing. If you run multiple
640 processes, you can change `inferior-tcl-buffer' to another process
641 buffer with \\[set-variable].")
642
643 ;;
644 ;; Hooks and other customization.
645 ;;
646
647 (defvar tcl-mode-hook nil
648 "Hook run on entry to Tcl mode.
649
650 Several functions exist which are useful to run from your
651 `tcl-mode-hook' (see each function's documentation for more
652 information):
653
654 tcl-guess-application
655 Guesses a default setting for `tcl-application' based on any
656 \"#!\" line at the top of the file.
657 tcl-hashify-buffer
658 Quotes all \"#\" characters that don't correspond to actual
659 Tcl comments. (Useful when editing code not originally created
660 with this mode).
661 tcl-auto-fill-mode
662 Auto-filling of Tcl comments.
663
664 Emacs 19 users can add functions to the hook with `add-hook':
665
666 (add-hook 'tcl-mode-hook 'tcl-guess-application)
667
668 Emacs 18 users must use `setq':
669
670 (setq tcl-mode-hook (cons 'tcl-guess-application tcl-mode-hook))")
671
672
673 (defvar inferior-tcl-mode-hook nil
674 "Hook for customizing Inferior Tcl mode.")
675
676 (defvar tcl-proc-list
677 '("proc" "method" "itcl_class" "body" "configbody" "class")
678 "List of commands whose first argument defines something.
679 This exists because some people (eg, me) use \"defvar\" et al.
680 Call `tcl-set-proc-regexp' and `tcl-set-font-lock-keywords'
681 after changing this list.")
682
683 (defvar tcl-proc-regexp nil
684 "Regexp to use when matching proc headers.")
685
686 (defvar tcl-typeword-list
687 '("global" "upvar" "inherit" "public" "protected" "private"
688 "common" "itk_option")
689 "List of Tcl keywords denoting \"type\". Used only for highlighting.
690 Call `tcl-set-font-lock-keywords' after changing this list.")
691
692 ;; Generally I've picked control operators to be keywords.
693 (defvar tcl-keyword-list
694 '("if" "then" "else" "elseif" "for" "foreach" "break" "continue" "while"
695 "eval" "case" "in" "switch" "default" "exit" "error" "proc" "return"
696 "uplevel" "constructor" "destructor" "itcl_class" "loop" "for_array_keys"
697 "for_recursive_glob" "for_file" "method" "body" "configbody" "class")
698 "List of Tcl keywords. Used only for highlighting.
699 Default list includes some TclX keywords.
700 Call `tcl-set-font-lock-keywords' after changing this list.")
701
702 (defvar tcl-font-lock-keywords nil
703 "Keywords to highlight for Tcl. See variable `font-lock-keywords'.
704 This variable is generally set from `tcl-proc-regexp',
705 `tcl-typeword-list', and `tcl-keyword-list' by the function
706 `tcl-set-font-lock-keywords'.")
707
708 ;; FIXME need some way to recognize variables because array refs look
709 ;; like 2 sexps.
710 (defvar tcl-type-alist
711 '(
712 ("proc" nil tcl-expr tcl-commands)
713 ("method" nil tcl-expr tcl-commands)
714 ("destructor" tcl-commands)
715 ("constructor" tcl-commands)
716 ("expr" tcl-expr)
717 ("catch" tcl-commands)
718 ("if" tcl-expr "then" tcl-commands)
719 ("elseif" tcl-expr "then" tcl-commands)
720 ("elseif" tcl-expr tcl-commands)
721 ("if" tcl-expr tcl-commands)
722 ("while" tcl-expr tcl-commands)
723 ("for" tcl-commands tcl-expr tcl-commands tcl-commands)
724 ("foreach" nil nil tcl-commands)
725 ("for_file" nil nil tcl-commands)
726 ("for_array_keys" nil nil tcl-commands)
727 ("for_recursive_glob" nil nil nil tcl-commands)
728 ;; Loop handling is not perfect, because the third argument can be
729 ;; either a command or an expr, and there is no real way to look
730 ;; forward.
731 ("loop" nil tcl-expr tcl-expr tcl-commands)
732 ("loop" nil tcl-expr tcl-commands)
733 )
734 "Alist that controls indentation.
735 \(Actually, this really only controls what happens on continuation lines).
736 Each entry looks like `(KEYWORD TYPE ...)'.
737 Each type entry describes a sexp after the keyword, and can be one of:
738 * nil, meaning that this sexp has no particular type.
739 * tcl-expr, meaning that this sexp is an arithmetic expression.
740 * tcl-commands, meaning that this sexp holds Tcl commands.
741 * a string, which must exactly match the string at the corresponding
742 position for a match to be made.
743
744 For example, the entry for the \"loop\" command is:
745
746 (\"loop\" nil tcl-expr tcl-commands)
747
748 This means that the \"loop\" command has three arguments. The first
749 argument is ignored (for indentation purposes). The second argument
750 is a Tcl expression, and the last argument is Tcl commands.")
751
752 (defvar tcl-explain-indentation nil
753 "If not `nil', debugging message will be printed during indentation.")
754
755 \f
756
757 ;;
758 ;; Work around differences between various versions of Emacs.
759 ;;
760
761 ;; We use this because Lemacs 19.9 has what we need.
762 (defconst tcl-pps-has-arg-6
763 (or tcl-using-emacs-19
764 (and tcl-using-xemacs-19
765 (condition-case nil
766 (progn
767 (parse-partial-sexp (point) (point) nil nil nil t)
768 t)
769 (error nil))))
770 "t if using an emacs which supports sixth (\"commentstop\") argument
771 to parse-partial-sexp.")
772
773 ;; Its pretty bogus to have to do this, but there is no easier way to
774 ;; say "match not syntax-1 and not syntax-2". Too bad you can't put
775 ;; \s in [...]. This sickness is used in Emacs 19 to match a defun
776 ;; starter. (It is used for this in v18 as well).
777 ;;(defconst tcl-omit-ws-regexp
778 ;; (concat "^\\(\\s"
779 ;; (mapconcat 'char-to-string "w_.()\"\\$'/" "\\|\\s")
780 ;; "\\)\\S(*")
781 ;; "Regular expression that matches everything except space, comment
782 ;;starter, and comment ender syntax codes.")
783
784 ;; FIXME? Instead of using the hairy regexp above, we just use a
785 ;; simple one.
786 ;;(defconst tcl-omit-ws-regexp "^[^] \t\n#}]\\S(*"
787 ;; "Regular expression used in locating function definitions.")
788
789 ;; Here's another stab. I think this one actually works. Now the
790 ;; problem seems to be that there is a bug in Emacs 19.22 where
791 ;; end-of-defun doesn't really use the brace matching the one that
792 ;; trails defun-prompt-regexp.
793 (defconst tcl-omit-ws-regexp "^[^ \t\n#}][^\n}]+}*[ \t]+")
794
795 (defun tcl-internal-beginning-of-defun (&optional arg)
796 "Move backward to next beginning-of-defun.
797 With argument, do this that many times.
798 Returns t unless search stops due to end of buffer."
799 (interactive "p")
800 (if (or (null arg) (= arg 0))
801 (setq arg 1))
802 (let (success)
803 (while (progn
804 (setq arg (1- arg))
805 (and (>= arg 0)
806 (setq success
807 (re-search-backward tcl-omit-ws-regexp nil 'move 1))))
808 (while (and (looking-at "[]#}]")
809 (setq success
810 (re-search-backward tcl-omit-ws-regexp nil 'move 1)))))
811 (beginning-of-line)
812 (not (null success))))
813
814 (defun tcl-internal-end-of-defun (&optional arg)
815 "Move forward to next end of defun.
816 An end of a defun is found by moving forward from the beginning of one."
817 (interactive "p")
818 (if (or (null arg) (= arg 0)) (setq arg 1))
819 (let ((start (point)))
820 ;; Was forward-char. I think this works a little better.
821 (forward-line)
822 (tcl-beginning-of-defun)
823 (while (> arg 0)
824 (while (and (re-search-forward tcl-omit-ws-regexp nil 'move 1)
825 (progn (beginning-of-line) t)
826 (looking-at "[]#}]")
827 (progn (forward-line) t)))
828 (let ((next-line (save-excursion
829 (forward-line)
830 (point))))
831 (while (< (point) next-line)
832 (forward-sexp)))
833 (forward-line)
834 (if (> (point) start) (setq arg (1- arg))))))
835
836 ;; In Emacs 19, we can use begining-of-defun as long as we set up a
837 ;; certain regexp. In Emacs 18, we need our own function.
838 (fset 'tcl-beginning-of-defun
839 (if tcl-using-emacs-19
840 'beginning-of-defun
841 'tcl-internal-beginning-of-defun))
842
843 ;; Ditto end-of-defun.
844 (fset 'tcl-end-of-defun
845 (if (and tcl-using-emacs-19 (not tcl-using-xemacs-19))
846 'end-of-defun
847 'tcl-internal-end-of-defun))
848
849 ;; Internal mark-defun that is used for losing Emacsen.
850 (defun tcl-internal-mark-defun ()
851 "Put mark at end of Tcl function, point at beginning."
852 (interactive)
853 (push-mark (point))
854 (tcl-end-of-defun)
855 (if tcl-using-emacs-19
856 (push-mark (point) nil t)
857 (push-mark (point)))
858 (tcl-beginning-of-defun)
859 (backward-paragraph))
860
861 ;; In GNU Emacs 19-23 and later, mark-defun works as advertised. I
862 ;; don't know about XEmacs, so for now it and Emacs 18 just lose.
863 (fset 'tcl-mark-defun
864 (if tcl-using-emacs-19-23
865 'mark-defun
866 'tcl-internal-mark-defun))
867
868 ;; In GNU Emacs 19, mark takes an additional "force" argument. I
869 ;; don't know about XEmacs, so I'm just assuming it is the same.
870 ;; Emacs 18 doesn't have this argument.
871 (defun tcl-mark ()
872 "Return mark, or nil if none."
873 (if tcl-using-emacs-19
874 (mark t)
875 (mark)))
876
877 \f
878
879 ;;
880 ;; Some helper functions.
881 ;;
882
883 (defun tcl-set-proc-regexp ()
884 "Set `tcl-proc-regexp' from variable `tcl-proc-list'."
885 (setq tcl-proc-regexp (concat "^\\s-*\\("
886 (mapconcat 'identity tcl-proc-list "\\|")
887 "\\)[ \t]+")))
888
889 (defun tcl-set-font-lock-keywords ()
890 "Set `tcl-font-lock-keywords'.
891 Uses variables `tcl-proc-regexp' and `tcl-keyword-list'."
892 (setq tcl-font-lock-keywords
893 (list
894 ;; Names of functions (and other "defining things").
895 (list (concat tcl-proc-regexp "\\([^ \t\n]+\\)")
896 2 'font-lock-function-name-face)
897
898 ;; Names of type-defining things.
899 (list (concat "\\(\\s-\\|^\\)\\("
900 ;; FIXME Use 'regexp-quote?
901 (mapconcat 'identity tcl-typeword-list "\\|")
902 "\\)\\(\\s-\\|$\\)")
903 2 'font-lock-type-face)
904
905 ;; Keywords. Only recognized if surrounded by whitespace.
906 ;; FIXME consider using "not word or symbol", not
907 ;; "whitespace".
908 (cons (concat "\\(\\s-\\|^\\)\\("
909 ;; FIXME Use regexp-quote?
910 (mapconcat 'identity tcl-keyword-list "\\|")
911 "\\)\\(\\s-\\|$\\)")
912 2)
913 )))
914
915 (if tcl-proc-regexp
916 ()
917 (tcl-set-proc-regexp))
918
919 (if tcl-font-lock-keywords
920 ()
921 (tcl-set-font-lock-keywords))
922
923 \f
924
925 ;;
926 ;; The mode itself.
927 ;;
928
929 ;;;###autoload
930 (defun tcl-mode ()
931 "Major mode for editing Tcl code.
932 Expression and list commands understand all Tcl brackets.
933 Tab indents for Tcl code.
934 Paragraphs are separated by blank lines only.
935 Delete converts tabs to spaces as it moves back.
936
937 Variables controlling indentation style:
938 tcl-indent-level
939 Indentation of Tcl statements within surrounding block.
940 tcl-continued-indent-level
941 Indentation of continuation line relative to first line of command.
942
943 Variables controlling user interaction with mode (see variable
944 documentation for details):
945 tcl-tab-always-indent
946 Controls action of TAB key.
947 tcl-auto-newline
948 Non-nil means automatically newline before and after braces, brackets,
949 and semicolons inserted in Tcl code.
950 tcl-electric-hash-style
951 Controls action of `#' key.
952 tcl-use-hairy-comment-detector
953 If t, use more complicated, but slower, comment detector.
954 This variable is only used in GNU Emacs 19.
955 tcl-use-smart-word-finder
956 If not nil, use a smarter, Tcl-specific way to find the current
957 word when looking up help on a Tcl command.
958
959 Turning on Tcl mode calls the value of the variable `tcl-mode-hook'
960 with no args, if that value is non-nil. Read the documentation for
961 `tcl-mode-hook' to see what kinds of interesting hook functions
962 already exist.
963
964 Commands:
965 \\{tcl-mode-map}"
966 (interactive)
967 (kill-all-local-variables)
968 (use-local-map tcl-mode-map)
969 (setq major-mode 'tcl-mode)
970 (setq mode-name "Tcl")
971 (setq local-abbrev-table tcl-mode-abbrev-table)
972 (set-syntax-table tcl-mode-syntax-table)
973
974 (make-local-variable 'paragraph-start)
975 (make-local-variable 'paragraph-separate)
976 (if (and tcl-using-emacs-19-23
977 (>= emacs-minor-version 29))
978 (progn
979 ;; In Emacs 19.29, you aren't supposed to start these with a
980 ;; ^.
981 (setq paragraph-start "$\\|\f")
982 (setq paragraph-separate paragraph-start))
983 (setq paragraph-start (concat "^$\\|" page-delimiter))
984 (setq paragraph-separate paragraph-start))
985 (make-local-variable 'paragraph-ignore-fill-prefix)
986 (setq paragraph-ignore-fill-prefix t)
987 (make-local-variable 'fill-paragraph-function)
988 (setq fill-paragraph-function 'tcl-do-fill-paragraph)
989
990 (make-local-variable 'indent-line-function)
991 (setq indent-line-function 'tcl-indent-line)
992 ;; Tcl doesn't require a final newline.
993 ;; (make-local-variable 'require-final-newline)
994 ;; (setq require-final-newline t)
995
996 (make-local-variable 'comment-start)
997 (setq comment-start "# ")
998 (make-local-variable 'comment-start-skip)
999 (setq comment-start-skip "#+ *")
1000 (make-local-variable 'comment-column)
1001 (setq comment-column 40)
1002 (make-local-variable 'comment-end)
1003 (setq comment-end "")
1004
1005 (make-local-variable 'outline-regexp)
1006 (setq outline-regexp "[^\n\^M]")
1007 (make-local-variable 'outline-level)
1008 (setq outline-level 'tcl-outline-level)
1009
1010 (make-local-variable 'font-lock-defaults)
1011 (setq font-lock-defaults
1012 '(tcl-font-lock-keywords))
1013
1014 ;; The following only really makes sense under GNU Emacs 19.
1015 (make-local-variable 'imenu-create-index-function)
1016 (setq imenu-create-index-function 'tcl-imenu-create-index-function)
1017 (make-local-variable 'parse-sexp-ignore-comments)
1018
1019 ;; Settings for new dabbrev code.
1020 (make-local-variable 'dabbrev-case-fold-search)
1021 (setq dabbrev-case-fold-search nil)
1022 (make-local-variable 'dabbrev-case-replace)
1023 (setq dabbrev-case-replace nil)
1024 (make-local-variable 'dabbrev-abbrev-skip-leading-regexp)
1025 (setq dabbrev-abbrev-skip-leading-regexp "[$!]")
1026 (make-local-variable 'dabbrev-abbrev-char-regexp)
1027 (setq dabbrev-abbrev-char-regexp "\\sw\\|\\s_")
1028
1029 (if tcl-using-emacs-19
1030 (progn
1031 ;; This can only be set to t in Emacs 19 and XEmacs.
1032 ;; Emacs 18 and Epoch lose.
1033 (setq parse-sexp-ignore-comments t)
1034 ;; XEmacs has defun-prompt-regexp, but I don't believe
1035 ;; that it works for end-of-defun -- only for
1036 ;; beginning-of-defun.
1037 (make-local-variable 'defun-prompt-regexp)
1038 (setq defun-prompt-regexp tcl-omit-ws-regexp)
1039 ;; The following doesn't work in Lucid Emacs 19.6, but maybe
1040 ;; it will appear in later versions.
1041 (make-local-variable 'add-log-current-defun-function)
1042 (setq add-log-current-defun-function 'add-log-tcl-defun))
1043 (setq parse-sexp-ignore-comments nil))
1044
1045 ;; Put Tcl menu into menubar for XEmacs. This happens
1046 ;; automatically for GNU Emacs.
1047 (if (and tcl-using-xemacs-19
1048 current-menubar
1049 (not (assoc "Tcl" current-menubar)))
1050 (progn
1051 (set-buffer-menubar (copy-sequence current-menubar))
1052 (add-menu nil "Tcl" tcl-xemacs-menu)))
1053 ;; Append Tcl menu to popup menu for XEmacs.
1054 (if (and tcl-using-xemacs-19 (boundp 'mode-popup-menu))
1055 (setq mode-popup-menu
1056 (cons (concat mode-name " Mode Commands") tcl-xemacs-menu)))
1057
1058 ;; If hilit19 is loaded, add our stuff.
1059 (if (featurep 'hilit19)
1060 (tcl-hilit))
1061
1062 (run-hooks 'tcl-mode-hook))
1063
1064 \f
1065
1066 ;; This is used for braces, brackets, and semi (except for closing
1067 ;; braces, which are handled specially).
1068 (defun tcl-electric-char (arg)
1069 "Insert character and correct line's indentation."
1070 (interactive "p")
1071 ;; Indent line first; this looks better if parens blink.
1072 (tcl-indent-line)
1073 (self-insert-command arg)
1074 (if (and tcl-auto-newline (= last-command-char ?\;))
1075 (progn
1076 (newline)
1077 (tcl-indent-line))))
1078
1079 ;; This is used for closing braces. If tcl-auto-newline is set, can
1080 ;; insert a newline both before and after the brace, depending on
1081 ;; context. FIXME should this be configurable? Does anyone use this?
1082 (defun tcl-electric-brace (arg)
1083 "Insert character and correct line's indentation."
1084 (interactive "p")
1085 ;; If auto-newlining and there is stuff on the same line, insert a
1086 ;; newline first.
1087 (if tcl-auto-newline
1088 (progn
1089 (if (save-excursion
1090 (skip-chars-backward " \t")
1091 (bolp))
1092 ()
1093 (tcl-indent-line)
1094 (newline))
1095 ;; In auto-newline case, must insert a newline after each
1096 ;; brace. So an explicit loop is needed.
1097 (while (> arg 0)
1098 (insert last-command-char)
1099 (tcl-indent-line)
1100 (newline)
1101 (setq arg (1- arg))))
1102 (self-insert-command arg))
1103 (tcl-indent-line))
1104
1105 \f
1106
1107 (defun tcl-indent-command (&optional arg)
1108 "Indent current line as Tcl code, or in some cases insert a tab character.
1109 If tcl-tab-always-indent is t (the default), always indent current line.
1110 If tcl-tab-always-indent is nil and point is not in the indentation
1111 area at the beginning of the line, a TAB is inserted.
1112 Other values of tcl-tab-always-indent cause the first possible action
1113 from the following list to take place:
1114
1115 1. Move from beginning of line to correct indentation.
1116 2. Delete an empty comment.
1117 3. Move forward to start of comment, indenting if necessary.
1118 4. Move forward to end of line, indenting if necessary.
1119 5. Create an empty comment.
1120 6. Move backward to start of comment, indenting if necessary."
1121 (interactive "p")
1122 (cond
1123 ((not tcl-tab-always-indent)
1124 ;; Indent if in indentation area, otherwise insert TAB.
1125 (if (<= (current-column) (current-indentation))
1126 (tcl-indent-line)
1127 (insert-tab arg)))
1128 ((eq tcl-tab-always-indent t)
1129 ;; Always indent.
1130 (tcl-indent-line))
1131 (t
1132 ;; "Perl-mode" style TAB command.
1133 (let* ((ipoint (point))
1134 (eolpoint (progn
1135 (end-of-line)
1136 (point)))
1137 (comment-p (tcl-in-comment)))
1138 (cond
1139 ((= ipoint (save-excursion
1140 (beginning-of-line)
1141 (point)))
1142 (beginning-of-line)
1143 (tcl-indent-line)
1144 ;; If indenting didn't leave us in column 0, go to the
1145 ;; indentation. Otherwise leave point at end of line. This
1146 ;; is a hack.
1147 (if (= (point) (save-excursion
1148 (beginning-of-line)
1149 (point)))
1150 (end-of-line)
1151 (back-to-indentation)))
1152 ((and comment-p (looking-at "[ \t]*$"))
1153 ;; Empty comment, so delete it. We also delete any ";"
1154 ;; characters at the end of the line. I think this is
1155 ;; friendlier, but I don't know how other people will feel.
1156 (backward-char)
1157 (skip-chars-backward " \t;")
1158 (delete-region (point) eolpoint))
1159 ((and comment-p (< ipoint (point)))
1160 ;; Before comment, so skip to it.
1161 (tcl-indent-line)
1162 (indent-for-comment))
1163 ((/= ipoint eolpoint)
1164 ;; Go to end of line (since we're not there yet).
1165 (goto-char eolpoint)
1166 (tcl-indent-line))
1167 ((not comment-p)
1168 (tcl-indent-line)
1169 (tcl-indent-for-comment))
1170 (t
1171 ;; Go to start of comment. We don't leave point where it is
1172 ;; because we want to skip comment-start-skip.
1173 (tcl-indent-line)
1174 (indent-for-comment)))))))
1175
1176 (defun tcl-indent-line ()
1177 "Indent current line as Tcl code.
1178 Return the amount the indentation changed by."
1179 (let ((indent (calculate-tcl-indent nil))
1180 beg shift-amt
1181 (case-fold-search nil)
1182 (pos (- (point-max) (point))))
1183 (beginning-of-line)
1184 (setq beg (point))
1185 (cond ((eq indent nil)
1186 (setq indent (current-indentation)))
1187 (t
1188 (skip-chars-forward " \t")
1189 (if (listp indent) (setq indent (car indent)))
1190 (cond ((= (following-char) ?})
1191 (setq indent (- indent tcl-indent-level)))
1192 ((= (following-char) ?\])
1193 (setq indent (- indent 1))))))
1194 (skip-chars-forward " \t")
1195 (setq shift-amt (- indent (current-column)))
1196 (if (zerop shift-amt)
1197 (if (> (- (point-max) pos) (point))
1198 (goto-char (- (point-max) pos)))
1199 (delete-region beg (point))
1200 (indent-to indent)
1201 ;; If initial point was within line's indentation,
1202 ;; position after the indentation. Else stay at same point in text.
1203 (if (> (- (point-max) pos) (point))
1204 (goto-char (- (point-max) pos))))
1205 shift-amt))
1206
1207 (defun tcl-figure-type ()
1208 "Determine type of sexp at point.
1209 This is either 'tcl-expr, 'tcl-commands, or nil. Puts point at start
1210 of sexp that indicates types.
1211
1212 See documentation for variable `tcl-type-alist' for more information."
1213 (let ((count 0)
1214 result
1215 word-stack)
1216 (while (and (< count 5)
1217 (not result))
1218 (condition-case nil
1219 (progn
1220 ;; FIXME should use "tcl-backward-sexp", which would skip
1221 ;; over entire variables, etc.
1222 (backward-sexp)
1223 (if (looking-at "[a-zA-Z_]+")
1224 (let ((list tcl-type-alist)
1225 entry)
1226 (setq word-stack (cons (tcl-word-no-props) word-stack))
1227 (while (and list (not result))
1228 (setq entry (car list))
1229 (setq list (cdr list))
1230 (let ((index 0))
1231 (while (and entry (<= index count))
1232 ;; Abort loop if string does not match word on
1233 ;; stack.
1234 (and (stringp (car entry))
1235 (not (string= (car entry)
1236 (nth index word-stack)))
1237 (setq entry nil))
1238 (setq entry (cdr entry))
1239 (setq index (1+ index)))
1240 (and (> index count)
1241 (not (stringp (car entry)))
1242 (setq result (car entry)))
1243 )))
1244 (setq word-stack (cons nil word-stack))))
1245 (error nil))
1246 (setq count (1+ count)))
1247 (and tcl-explain-indentation
1248 (message "Indentation type %s" result))
1249 result))
1250
1251 (defun calculate-tcl-indent (&optional parse-start)
1252 "Return appropriate indentation for current line as Tcl code.
1253 In usual case returns an integer: the column to indent to.
1254 Returns nil if line starts inside a string, t if in a comment."
1255 (save-excursion
1256 (beginning-of-line)
1257 (let* ((indent-point (point))
1258 (case-fold-search nil)
1259 (continued-line
1260 (save-excursion
1261 (if (bobp)
1262 nil
1263 (backward-char)
1264 (= ?\\ (preceding-char)))))
1265 (continued-indent-value (if continued-line
1266 tcl-continued-indent-level
1267 0))
1268 state
1269 containing-sexp
1270 found-next-line)
1271 (if parse-start
1272 (goto-char parse-start)
1273 (tcl-beginning-of-defun))
1274 (while (< (point) indent-point)
1275 (setq parse-start (point))
1276 (setq state (parse-partial-sexp (point) indent-point 0))
1277 (setq containing-sexp (car (cdr state))))
1278 (cond ((or (nth 3 state) (nth 4 state))
1279 ;; Inside comment or string. Return nil or t if should
1280 ;; not change this line
1281 (nth 4 state))
1282 ((null containing-sexp)
1283 ;; Line is at top level.
1284 continued-indent-value)
1285 (t
1286 ;; Set expr-p if we are looking at the expression part of
1287 ;; an "if", "expr", etc statement. Set commands-p if we
1288 ;; are looking at the body part of an if, while, etc
1289 ;; statement. FIXME Should check for "for" loops here.
1290 (goto-char containing-sexp)
1291 (let* ((sexpr-type (tcl-figure-type))
1292 (expr-p (eq sexpr-type 'tcl-expr))
1293 (commands-p (eq sexpr-type 'tcl-commands))
1294 (expr-start (point)))
1295 ;; Find the first statement in the block and indent
1296 ;; like it. The first statement in the block might be
1297 ;; on the same line, so what we do is skip all
1298 ;; "virtually blank" lines, looking for a non-blank
1299 ;; one. A line is virtually blank if it only contains
1300 ;; a comment and whitespace. FIXME continued comments
1301 ;; aren't supported. They are a wart on Tcl anyway.
1302 ;; We do it this funky way because we want to know if
1303 ;; we've found a statement on some line _after_ the
1304 ;; line holding the sexp opener.
1305 (goto-char containing-sexp)
1306 (forward-char)
1307 (if (and (< (point) indent-point)
1308 (looking-at "[ \t]*\\(#.*\\)?$"))
1309 (progn
1310 (forward-line)
1311 (while (and (< (point) indent-point)
1312 (looking-at "[ \t]*\\(#.*\\)?$"))
1313 (setq found-next-line t)
1314 (forward-line))))
1315 (if (or continued-line
1316 (/= (char-after containing-sexp) ?{)
1317 expr-p)
1318 (progn
1319 ;; Line is continuation line, or the sexp opener
1320 ;; is not a curly brace, or we are are looking at
1321 ;; an `expr' expression (which must be split
1322 ;; specially). So indentation is column of first
1323 ;; good spot after sexp opener (with some added
1324 ;; in the continued-line case). If there is no
1325 ;; nonempty line before the indentation point, we
1326 ;; use the column of the character after the sexp
1327 ;; opener.
1328 (if (>= (point) indent-point)
1329 (progn
1330 (goto-char containing-sexp)
1331 (forward-char))
1332 (skip-chars-forward " \t"))
1333 (+ (current-column) continued-indent-value))
1334 ;; After a curly brace, and not a continuation line.
1335 ;; So take indentation from first good line after
1336 ;; start of block, unless that line is on the same
1337 ;; line as the opening brace. In this case use the
1338 ;; indentation of the opening brace's line, plus
1339 ;; another indent step. If we are in the body part
1340 ;; of an "if" or "while" then the indentation is
1341 ;; taken from the line holding the start of the
1342 ;; statement.
1343 (if (and (< (point) indent-point)
1344 found-next-line)
1345 (current-indentation)
1346 (if commands-p
1347 (goto-char expr-start)
1348 (goto-char containing-sexp))
1349 (+ (current-indentation) tcl-indent-level)))))))))
1350
1351 \f
1352
1353 (defun indent-tcl-exp ()
1354 "Indent each line of the Tcl grouping following point."
1355 (interactive)
1356 (let ((indent-stack (list nil))
1357 (contain-stack (list (point)))
1358 (case-fold-search nil)
1359 outer-loop-done inner-loop-done state ostate
1360 this-indent last-sexp continued-line
1361 (next-depth 0)
1362 last-depth)
1363 (save-excursion
1364 (forward-sexp 1))
1365 (save-excursion
1366 (setq outer-loop-done nil)
1367 (while (and (not (eobp)) (not outer-loop-done))
1368 (setq last-depth next-depth)
1369 ;; Compute how depth changes over this line
1370 ;; plus enough other lines to get to one that
1371 ;; does not end inside a comment or string.
1372 ;; Meanwhile, do appropriate indentation on comment lines.
1373 (setq inner-loop-done nil)
1374 (while (and (not inner-loop-done)
1375 (not (and (eobp) (setq outer-loop-done t))))
1376 (setq ostate state)
1377 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
1378 nil nil state))
1379 (setq next-depth (car state))
1380 (if (and (car (cdr (cdr state)))
1381 (>= (car (cdr (cdr state))) 0))
1382 (setq last-sexp (car (cdr (cdr state)))))
1383 (if (or (nth 4 ostate))
1384 (tcl-indent-line))
1385 (if (or (nth 3 state))
1386 (forward-line 1)
1387 (setq inner-loop-done t)))
1388 (if (<= next-depth 0)
1389 (setq outer-loop-done t))
1390 (if outer-loop-done
1391 nil
1392 ;; If this line had ..))) (((.. in it, pop out of the levels
1393 ;; that ended anywhere in this line, even if the final depth
1394 ;; doesn't indicate that they ended.
1395 (while (> last-depth (nth 6 state))
1396 (setq indent-stack (cdr indent-stack)
1397 contain-stack (cdr contain-stack)
1398 last-depth (1- last-depth)))
1399 (if (/= last-depth next-depth)
1400 (setq last-sexp nil))
1401 ;; Add levels for any parens that were started in this line.
1402 (while (< last-depth next-depth)
1403 (setq indent-stack (cons nil indent-stack)
1404 contain-stack (cons nil contain-stack)
1405 last-depth (1+ last-depth)))
1406 (if (null (car contain-stack))
1407 (setcar contain-stack
1408 (or (car (cdr state))
1409 (save-excursion
1410 (forward-sexp -1)
1411 (point)))))
1412 (forward-line 1)
1413 (setq continued-line
1414 (save-excursion
1415 (backward-char)
1416 (= (preceding-char) ?\\)))
1417 (skip-chars-forward " \t")
1418 (if (eolp)
1419 nil
1420 (if (and (car indent-stack)
1421 (>= (car indent-stack) 0))
1422 ;; Line is on an existing nesting level.
1423 (setq this-indent (car indent-stack))
1424 ;; Just started a new nesting level.
1425 ;; Compute the standard indent for this level.
1426 (let ((val (calculate-tcl-indent
1427 (if (car indent-stack)
1428 (- (car indent-stack))))))
1429 (setcar indent-stack
1430 (setq this-indent val))
1431 (setq continued-line nil)))
1432 (cond ((not (numberp this-indent)))
1433 ((= (following-char) ?})
1434 (setq this-indent (- this-indent tcl-indent-level)))
1435 ((= (following-char) ?\])
1436 (setq this-indent (- this-indent 1))))
1437 ;; Put chosen indentation into effect.
1438 (or (null this-indent)
1439 (= (current-column)
1440 (if continued-line
1441 (+ this-indent tcl-indent-level)
1442 this-indent))
1443 (progn
1444 (delete-region (point) (progn (beginning-of-line) (point)))
1445 (indent-to
1446 (if continued-line
1447 (+ this-indent tcl-indent-level)
1448 this-indent)))))))))
1449 )
1450
1451 \f
1452
1453 ;;
1454 ;; Interfaces to other packages.
1455 ;;
1456
1457 (defun tcl-imenu-create-index-function ()
1458 "Generate alist of indices for imenu."
1459 (let ((re (concat tcl-proc-regexp "\\([^ \t\n{]+\\)"))
1460 alist prev-pos)
1461 (goto-char (point-min))
1462 (imenu-progress-message prev-pos 0)
1463 (save-match-data
1464 (while (re-search-forward re nil t)
1465 (imenu-progress-message prev-pos)
1466 ;; Position on start of proc name, not beginning of line.
1467 (setq alist (cons
1468 (cons (buffer-substring (match-beginning 2) (match-end 2))
1469 (match-beginning 2))
1470 alist))))
1471 (imenu-progress-message prev-pos 100)
1472 (nreverse alist)))
1473
1474 ;; FIXME Definition of function is very ad-hoc. Should use
1475 ;; tcl-beginning-of-defun. Also has incestuous knowledge about the
1476 ;; format of tcl-proc-regexp.
1477 (defun add-log-tcl-defun ()
1478 "Return name of Tcl function point is in, or nil."
1479 (save-excursion
1480 (end-of-line)
1481 (if (re-search-backward (concat tcl-proc-regexp "\\([^ \t\n{]+\\)") nil t)
1482 (buffer-substring (match-beginning 2)
1483 (match-end 2)))))
1484
1485 (defun tcl-outline-level ()
1486 (save-excursion
1487 (skip-chars-forward " \t")
1488 (current-column)))
1489
1490 \f
1491
1492 ;;
1493 ;; Helper functions for inferior Tcl mode.
1494 ;;
1495
1496 ;; This exists to let us delete the prompt when commands are sent
1497 ;; directly to the inferior Tcl. See gud.el for an explanation of how
1498 ;; it all works (I took it from there). This stuff doesn't really
1499 ;; work as well as I'd like it to. But I don't believe there is
1500 ;; anything useful that can be done.
1501 (defvar inferior-tcl-delete-prompt-marker nil)
1502
1503 (defun tcl-filter (proc string)
1504 (let ((inhibit-quit t))
1505 (save-excursion
1506 (set-buffer (process-buffer proc))
1507 (goto-char (process-mark proc))
1508 ;; Delete prompt if requested.
1509 (if (marker-buffer inferior-tcl-delete-prompt-marker)
1510 (progn
1511 (delete-region (point) inferior-tcl-delete-prompt-marker)
1512 (set-marker inferior-tcl-delete-prompt-marker nil)))))
1513 (if tcl-using-emacs-19
1514 (comint-output-filter proc string)
1515 (funcall comint-output-filter string)))
1516
1517 (defun tcl-send-string (proc string)
1518 (save-excursion
1519 (set-buffer (process-buffer proc))
1520 (goto-char (process-mark proc))
1521 (beginning-of-line)
1522 (if (looking-at comint-prompt-regexp)
1523 (set-marker inferior-tcl-delete-prompt-marker (point))))
1524 (comint-send-string proc string))
1525
1526 (defun tcl-send-region (proc start end)
1527 (save-excursion
1528 (set-buffer (process-buffer proc))
1529 (goto-char (process-mark proc))
1530 (beginning-of-line)
1531 (if (looking-at comint-prompt-regexp)
1532 (set-marker inferior-tcl-delete-prompt-marker (point))))
1533 (comint-send-region proc start end))
1534
1535 (defun switch-to-tcl (eob-p)
1536 "Switch to inferior Tcl process buffer.
1537 With argument, positions cursor at end of buffer."
1538 (interactive "P")
1539 (if (get-buffer inferior-tcl-buffer)
1540 (pop-to-buffer inferior-tcl-buffer)
1541 (error "No current inferior Tcl buffer"))
1542 (cond (eob-p
1543 (push-mark)
1544 (goto-char (point-max)))))
1545
1546 (defun inferior-tcl-proc ()
1547 "Return current inferior Tcl process.
1548 See variable `inferior-tcl-buffer'."
1549 (let ((proc (get-buffer-process (if (eq major-mode 'inferior-tcl-mode)
1550 (current-buffer)
1551 inferior-tcl-buffer))))
1552 (or proc
1553 (error "No Tcl process; see variable `inferior-tcl-buffer'"))))
1554
1555 (defun tcl-eval-region (start end &optional and-go)
1556 "Send the current region to the inferior Tcl process.
1557 Prefix argument means switch to the Tcl buffer afterwards."
1558 (interactive "r\nP")
1559 (let ((proc (inferior-tcl-proc)))
1560 (tcl-send-region proc start end)
1561 (tcl-send-string proc "\n")
1562 (if and-go (switch-to-tcl t))))
1563
1564 (defun tcl-eval-defun (&optional and-go)
1565 "Send the current defun to the inferior Tcl process.
1566 Prefix argument means switch to the Tcl buffer afterwards."
1567 (interactive "P")
1568 (save-excursion
1569 (tcl-end-of-defun)
1570 (let ((end (point)))
1571 (tcl-beginning-of-defun)
1572 (tcl-eval-region (point) end)))
1573 (if and-go (switch-to-tcl t)))
1574
1575 \f
1576
1577 ;;
1578 ;; Inferior Tcl mode itself.
1579 ;;
1580
1581 (defun inferior-tcl-mode ()
1582 "Major mode for interacting with Tcl interpreter.
1583
1584 A Tcl process can be started with M-x inferior-tcl.
1585
1586 Entry to this mode runs the hooks comint-mode-hook and
1587 inferior-tcl-mode-hook, in that order.
1588
1589 You can send text to the inferior Tcl process from other buffers
1590 containing Tcl source.
1591
1592 Variables controlling Inferior Tcl mode:
1593 tcl-application
1594 Name of program to run.
1595 tcl-command-switches
1596 Command line arguments to `tcl-application'.
1597 tcl-prompt-regexp
1598 Matches prompt.
1599 inferior-tcl-source-command
1600 Command to use to read Tcl file in running application.
1601 inferior-tcl-buffer
1602 The current inferior Tcl process buffer. See variable
1603 documentation for details on multiple-process support.
1604
1605 The following commands are available:
1606 \\{inferior-tcl-mode-map}"
1607 (interactive)
1608 (comint-mode)
1609 (setq comint-prompt-regexp (or tcl-prompt-regexp
1610 (concat "^"
1611 (regexp-quote tcl-application)
1612 ">")))
1613 (setq major-mode 'inferior-tcl-mode)
1614 (setq mode-name "Inferior Tcl")
1615 (if (boundp 'modeline-process)
1616 (setq modeline-process '(": %s")) ; For XEmacs.
1617 (setq mode-line-process '(": %s")))
1618 (use-local-map inferior-tcl-mode-map)
1619 (setq local-abbrev-table tcl-mode-abbrev-table)
1620 (set-syntax-table tcl-mode-syntax-table)
1621 (if tcl-using-emacs-19
1622 (progn
1623 (make-local-variable 'defun-prompt-regexp)
1624 (setq defun-prompt-regexp tcl-omit-ws-regexp)))
1625 (make-local-variable 'inferior-tcl-delete-prompt-marker)
1626 (setq inferior-tcl-delete-prompt-marker (make-marker))
1627 (set-process-filter (get-buffer-process (current-buffer)) 'tcl-filter)
1628 (run-hooks 'inferior-tcl-mode-hook))
1629
1630 ;;;###autoload
1631 (defun inferior-tcl (cmd)
1632 "Run inferior Tcl process.
1633 Prefix arg means enter program name interactively.
1634 See documentation for function `inferior-tcl-mode' for more information."
1635 (interactive
1636 (list (if current-prefix-arg
1637 (read-string "Run Tcl: " tcl-application)
1638 tcl-application)))
1639 (if (not (comint-check-proc "*inferior-tcl*"))
1640 (progn
1641 (set-buffer (apply (function make-comint) "inferior-tcl" cmd nil
1642 tcl-command-switches))
1643 (inferior-tcl-mode)))
1644 (make-local-variable 'tcl-application)
1645 (setq tcl-application cmd)
1646 (setq inferior-tcl-buffer "*inferior-tcl*")
1647 (switch-to-buffer "*inferior-tcl*"))
1648
1649 (and (fboundp 'defalias)
1650 (defalias 'run-tcl 'inferior-tcl))
1651
1652 \f
1653
1654 ;;
1655 ;; Auto-fill support.
1656 ;;
1657
1658 (defun tcl-real-command-p ()
1659 "Return nil if point is not at the beginning of a command.
1660 A command is the first word on an otherwise empty line, or the
1661 first word following a semicolon, opening brace, or opening bracket."
1662 (save-excursion
1663 (skip-chars-backward " \t")
1664 (cond
1665 ((bobp) t)
1666 ((bolp)
1667 (backward-char)
1668 ;; Note -- continued comments are not supported here. I
1669 ;; consider those to be a wart on the language.
1670 (not (eq ?\\ (preceding-char))))
1671 (t
1672 (memq (preceding-char) '(?\; ?{ ?\[))))))
1673
1674 ;; FIXME doesn't actually return t. See last case.
1675 (defun tcl-real-comment-p ()
1676 "Return t if point is just after the `#' beginning a real comment.
1677 Does not check to see if previous char is actually `#'.
1678 A real comment is either at the beginning of the buffer,
1679 preceeded only by whitespace on the line, or has a preceeding
1680 semicolon, opening brace, or opening bracket on the same line."
1681 (save-excursion
1682 (backward-char)
1683 (tcl-real-command-p)))
1684
1685 (defun tcl-hairy-scan-for-comment (state end always-stop)
1686 "Determine if point is in a comment.
1687 Returns a list of the form `(FLAG . STATE)'. STATE can be used
1688 as input to future invocations. FLAG is nil if not in comment,
1689 t otherwise. If in comment, leaves point at beginning of comment.
1690 Only works in Emacs 19. See also `tcl-simple-scan-for-comment', a
1691 simpler version that is often right, and works in Emacs 18."
1692 (let ((bol (save-excursion
1693 (goto-char end)
1694 (beginning-of-line)
1695 (point)))
1696 real-comment
1697 last-cstart)
1698 (while (and (not last-cstart) (< (point) end))
1699 (setq real-comment nil) ;In case we've looped around and it is
1700 ;set.
1701 (setq state (parse-partial-sexp (point) end nil nil state t))
1702 (if (nth 4 state)
1703 (progn
1704 ;; If ALWAYS-STOP is set, stop even if we don't have a
1705 ;; real comment, or if the comment isn't on the same line
1706 ;; as the end.
1707 (if always-stop (setq last-cstart (point)))
1708 ;; If we have a real comment, then set the comment
1709 ;; starting point if we are on the same line as the ending
1710 ;; location.
1711 (setq real-comment (tcl-real-comment-p))
1712 (if real-comment
1713 (progn
1714 (and (> (point) bol) (setq last-cstart (point)))
1715 ;; NOTE Emacs 19 has a misfeature whereby calling
1716 ;; parse-partial-sexp with COMMENTSTOP set and with
1717 ;; an initial list that says point is in a comment
1718 ;; will cause an immediate return. So we must skip
1719 ;; over the comment ourselves.
1720 (beginning-of-line 2)))
1721 ;; Frob the state to make it look like we aren't in a
1722 ;; comment.
1723 (setcar (nthcdr 4 state) nil))))
1724 (and last-cstart
1725 (goto-char last-cstart))
1726 (cons real-comment state)))
1727
1728 (defun tcl-hairy-in-comment ()
1729 "Return t if point is in a comment, and leave point at beginning
1730 of comment."
1731 (let ((save (point)))
1732 (tcl-beginning-of-defun)
1733 (car (tcl-hairy-scan-for-comment nil save nil))))
1734
1735 (defun tcl-simple-in-comment ()
1736 "Return t if point is in comment, and leave point at beginning
1737 of comment. This is faster that `tcl-hairy-in-comment', but is
1738 correct less often."
1739 (let ((save (point))
1740 comment)
1741 (beginning-of-line)
1742 (while (and (< (point) save) (not comment))
1743 (search-forward "#" save 'move)
1744 (setq comment (tcl-real-comment-p)))
1745 comment))
1746
1747 (defun tcl-in-comment ()
1748 "Return t if point is in comment, and leave point at beginning
1749 of comment."
1750 (if (and tcl-pps-has-arg-6
1751 tcl-use-hairy-comment-detector)
1752 (tcl-hairy-in-comment)
1753 (tcl-simple-in-comment)))
1754
1755 (defun tcl-do-fill-paragraph (ignore)
1756 "fill-paragraph function for Tcl mode. Only fills in a comment."
1757 (let (in-comment col where)
1758 (save-excursion
1759 (end-of-line)
1760 (setq in-comment (tcl-in-comment))
1761 (if in-comment
1762 (progn
1763 (setq where (1+ (point)))
1764 (setq col (1- (current-column))))))
1765 (and in-comment
1766 (save-excursion
1767 (back-to-indentation)
1768 (= col (current-column)))
1769 ;; In a comment. Set the fill prefix, and find the paragraph
1770 ;; boundaries by searching for lines that look like
1771 ;; comment-only lines.
1772 (let ((fill-prefix (buffer-substring (progn
1773 (beginning-of-line)
1774 (point))
1775 where))
1776 p-start p-end)
1777 ;; Search backwards.
1778 (save-excursion
1779 (while (looking-at "^[ \t]*#")
1780 (forward-line -1))
1781 (forward-line)
1782 (setq p-start (point)))
1783
1784 ;; Search forwards.
1785 (save-excursion
1786 (while (looking-at "^[ \t]*#")
1787 (forward-line))
1788 (setq p-end (point)))
1789
1790 ;; Narrow and do the fill.
1791 (save-restriction
1792 (narrow-to-region p-start p-end)
1793 (fill-paragraph ignore)))))
1794 t)
1795
1796 (defun tcl-do-auto-fill ()
1797 "Auto-fill function for Tcl mode. Only auto-fills in a comment."
1798 (if (> (current-column) fill-column)
1799 (let ((fill-prefix "# ")
1800 in-comment col)
1801 (save-excursion
1802 (setq in-comment (tcl-in-comment))
1803 (if in-comment
1804 (setq col (1- (current-column)))))
1805 (if in-comment
1806 (progn
1807 (do-auto-fill)
1808 (save-excursion
1809 (back-to-indentation)
1810 (delete-region (point) (save-excursion
1811 (beginning-of-line)
1812 (point)))
1813 (indent-to-column col)))))))
1814
1815 \f
1816
1817 ;;
1818 ;; Help-related code.
1819 ;;
1820
1821 (defvar tcl-help-saved-dirs nil
1822 "Saved help directories.
1823 If `tcl-help-directory-list' changes, this allows `tcl-help-on-word'
1824 to update the alist.")
1825
1826 (defvar tcl-help-alist nil
1827 "Alist with command names as keys and filenames as values.")
1828
1829 (defun tcl-help-snarf-commands (dirlist)
1830 "Build alist of commands and filenames."
1831 (while dirlist
1832 (let ((files (directory-files (car dirlist) t)))
1833 (while files
1834 (if (and (file-directory-p (car files))
1835 (not
1836 (let ((fpart (file-name-nondirectory (car files))))
1837 (or (equal fpart ".")
1838 (equal fpart "..")))))
1839 (let ((matches (directory-files (car files) t)))
1840 (while matches
1841 (or (file-directory-p (car matches))
1842 (setq tcl-help-alist
1843 (cons
1844 (cons (file-name-nondirectory (car matches))
1845 (car matches))
1846 tcl-help-alist)))
1847 (setq matches (cdr matches)))))
1848 (setq files (cdr files))))
1849 (setq dirlist (cdr dirlist))))
1850
1851 (defun tcl-reread-help-files ()
1852 "Set up to re-read files, and then do it."
1853 (interactive)
1854 (message "Building Tcl help file index...")
1855 (setq tcl-help-saved-dirs tcl-help-directory-list)
1856 (setq tcl-help-alist nil)
1857 (tcl-help-snarf-commands tcl-help-directory-list)
1858 (message "Building Tcl help file index...done"))
1859
1860 (defun tcl-word-no-props ()
1861 "Like current-word, but strips properties."
1862 (let ((word (current-word)))
1863 (and (fboundp 'set-text-properties)
1864 (set-text-properties 0 (length word) nil word))
1865 word))
1866
1867 (defun tcl-current-word (flag)
1868 "Return current command word, or nil.
1869 If FLAG is nil, just uses `current-word'.
1870 Otherwise scans backward for most likely Tcl command word."
1871 (if (and flag
1872 (memq major-mode '(tcl-mode inferior-tcl-mode)))
1873 (condition-case nil
1874 (save-excursion
1875 ;; Look backward for first word actually in alist.
1876 (if (bobp)
1877 ()
1878 (while (and (not (bobp))
1879 (not (tcl-real-command-p)))
1880 (backward-sexp)))
1881 (if (assoc (tcl-word-no-props) tcl-help-alist)
1882 (tcl-word-no-props)))
1883 (error nil))
1884 (tcl-word-no-props)))
1885
1886 ;;;###autoload
1887 (defun tcl-help-on-word (command &optional arg)
1888 "Get help on Tcl command. Default is word at point.
1889 Prefix argument means invert sense of `tcl-use-smart-word-finder'."
1890 (interactive
1891 (list
1892 (progn
1893 (if (not (equal tcl-help-directory-list tcl-help-saved-dirs))
1894 (tcl-reread-help-files))
1895 (let ((word (tcl-current-word
1896 (if current-prefix-arg
1897 (not tcl-use-smart-word-finder)
1898 tcl-use-smart-word-finder))))
1899 (completing-read
1900 (if (or (null word) (string= word ""))
1901 "Help on Tcl command: "
1902 (format "Help on Tcl command (default %s): " word))
1903 tcl-help-alist nil t)))
1904 current-prefix-arg))
1905 (if (not (equal tcl-help-directory-list tcl-help-saved-dirs))
1906 (tcl-reread-help-files))
1907 (if (string= command "")
1908 (setq command (tcl-current-word
1909 (if arg
1910 (not tcl-use-smart-word-finder)
1911 tcl-use-smart-word-finder))))
1912 (let* ((help (get-buffer-create "*Tcl help*"))
1913 (cell (assoc command tcl-help-alist))
1914 (file (and cell (cdr cell))))
1915 (set-buffer help)
1916 (delete-region (point-min) (point-max))
1917 (if file
1918 (progn
1919 (insert "*** " command "\n\n")
1920 (insert-file-contents file))
1921 (if (string= command "")
1922 (insert "Magical Pig!")
1923 (insert "Tcl command " command " not in help\n")))
1924 (set-buffer-modified-p nil)
1925 (goto-char (point-min))
1926 (display-buffer help)))
1927
1928 \f
1929
1930 ;;
1931 ;; Other interactive stuff.
1932 ;;
1933
1934 (defvar tcl-previous-dir/file nil
1935 "Record last directory and file used in loading.
1936 This holds a cons cell of the form `(DIRECTORY . FILE)'
1937 describing the last `tcl-load-file' command.")
1938
1939 (defun tcl-load-file (file &optional and-go)
1940 "Load a Tcl file into the inferior Tcl process.
1941 Prefix argument means switch to the Tcl buffer afterwards."
1942 (interactive
1943 (list
1944 ;; car because comint-get-source returns a list holding the
1945 ;; filename.
1946 (car (comint-get-source "Load Tcl file: "
1947 (or (and
1948 (eq major-mode 'tcl-mode)
1949 (buffer-file-name))
1950 tcl-previous-dir/file)
1951 '(tcl-mode) t))
1952 current-prefix-arg))
1953 (comint-check-source file)
1954 (setq tcl-previous-dir/file (cons (file-name-directory file)
1955 (file-name-nondirectory file)))
1956 (tcl-send-string (inferior-tcl-proc)
1957 (format inferior-tcl-source-command (tcl-quote file)))
1958 (if and-go (switch-to-tcl t)))
1959
1960 (defun tcl-restart-with-file (file &optional and-go)
1961 "Restart inferior Tcl with file.
1962 If an inferior Tcl process exists, it is killed first.
1963 Prefix argument means switch to the Tcl buffer afterwards."
1964 (interactive
1965 (list
1966 (car (comint-get-source "Restart with Tcl file: "
1967 (or (and
1968 (eq major-mode 'tcl-mode)
1969 (buffer-file-name))
1970 tcl-previous-dir/file)
1971 '(tcl-mode) t))
1972 current-prefix-arg))
1973 (let* ((buf (if (eq major-mode 'inferior-tcl-mode)
1974 (current-buffer)
1975 inferior-tcl-buffer))
1976 (proc (and buf (get-process buf))))
1977 (cond
1978 ((not (and buf (get-buffer buf)))
1979 ;; I think this will be ok.
1980 (inferior-tcl tcl-application)
1981 (tcl-load-file file and-go))
1982 ((or
1983 (not (comint-check-proc buf))
1984 (yes-or-no-p
1985 "A Tcl process is running, are you sure you want to reset it? "))
1986 (save-excursion
1987 (comint-check-source file)
1988 (setq tcl-previous-dir/file (cons (file-name-directory file)
1989 (file-name-nondirectory file)))
1990 (comint-exec (get-buffer-create buf)
1991 (if proc
1992 (process-name proc)
1993 "inferior-tcl")
1994 tcl-application file tcl-command-switches)
1995 (if and-go (switch-to-tcl t)))))))
1996
1997 ;; FIXME I imagine you can do this under Emacs 18. I just don't know
1998 ;; how.
1999 (defun tcl-auto-fill-mode (&optional arg)
2000 "Like `auto-fill-mode', but controls filling of Tcl comments."
2001 (interactive "P")
2002 (and (not tcl-using-emacs-19)
2003 (error "You must use Emacs 19 to get this feature."))
2004 ;; Following code taken from "auto-fill-mode" (simple.el).
2005 (prog1
2006 (setq auto-fill-function
2007 (if (if (null arg)
2008 (not auto-fill-function)
2009 (> (prefix-numeric-value arg) 0))
2010 'tcl-do-auto-fill
2011 nil))
2012 (force-mode-line-update)))
2013
2014 ;; hilit19 support from "Chris Alfeld" <calfeld@math.utah.edu>
2015 (defun tcl-hilit ()
2016 (hilit-set-mode-patterns
2017 '(tcl-mode)
2018 '(
2019 ("\\(^ *\\|\; *\\)#.*$" nil comment)
2020 ("[^\\]\\(\\$[A-Za-z0-9\\-\\_./\\(\\)]+\\)" 1 label)
2021 ("[^_]\\<\\(append\\|array\\|auto_execok\\|auto_load\\|auto_mkindex\\|auto_reset\\|break\\|case\\|catch\\|cd\\|close\\|concat\\|continue\\|eof\\|error\\|eval\\|exec\\|exit\\|expr\\|file\\|flush\\|for\\|foreach\\|format\\|gets\\|glob\\|global\\|history\\|if\\|incr\\|info\\|join\\|lappend\\|lindex\\|linsert\\|list\\|llength\\|lrange\\|lreplace\\|lsearch\\|lsort\\|open\\|pid\\|proc\\|puts\\|pwd\\|read\\|regexp\\|regsub\\|rename\\|return\\|scan\\|seek\\|set\\|source\\|split\\|string\\|switch\\|tell\\|time\\|trace\\|unknown\\|unset\\|uplevel\\|upvar\\|while\\)\\>[^_]" 1 keyword) ; tcl keywords
2022 ("[^_]\\<\\(after\\|bell\\|bind\\|bindtags\\|clipboard\\|destroy\\|fileevent\\|focus\\|grab\\|image\\|lower\\|option\\|pack\\|place\\|raise\\|scale\\|selection\\|send\\|subst\\|tk\\|tk_popup\\|tkwait\\|update\\|winfo\\|wm\\)\\>[^_]" 1 define) ; tk keywords
2023 ("[^_]\\<\\(button\\|canvas\\|checkbutton\\|entry\\|frame\\|label\\|listbox\\|menu\\|menubutton\\|message\\|radiobutton\\|scrollbar\\|text\\|toplevel\\)\\>[^_]" 1 decl) ; tk widgets
2024 ("[^_]\\<\\(tix\\((ButtonBox\\|Baloon\\|Control\\|DirList\\|ExFileSelectBox\\|ExFileSelectDialog\\|FileEntry\\|HList\\|LabelEntry\\|LabelFrame\\|NoteBook\\|OptionMenu\\|PanedWindow\\|PopupMenu\\|ScrolledHList\\|ScrolledText\\|ScrolledWindow\\|Select\\|StdButtonBox\\)\\)\\>[^_]" 1 defun) ; tix widgets
2025 ("[{}\\\"\\(\\)]" nil include) ; misc punctuation
2026 )))
2027
2028 (defun tcl-electric-hash (&optional count)
2029 "Insert a `#' and quote if it does not start a real comment.
2030 Prefix arg is number of `#'s to insert.
2031 See variable `tcl-electric-hash-style' for description of quoting
2032 styles."
2033 (interactive "p")
2034 (or count (setq count 1))
2035 (if (> count 0)
2036 (let ((type
2037 (if (eq tcl-electric-hash-style 'smart)
2038 (if (> count 3) ; FIXME what is "smart"?
2039 'quote
2040 'backslash)
2041 tcl-electric-hash-style))
2042 comment)
2043 (if type
2044 (progn
2045 (save-excursion
2046 (insert "#")
2047 (setq comment (tcl-in-comment)))
2048 (delete-char 1)
2049 (and tcl-explain-indentation (message "comment: %s" comment))
2050 (cond
2051 ((eq type 'quote)
2052 (if (not comment)
2053 (insert "\"")))
2054 ((eq type 'backslash)
2055 ;; The following will set count to 0, so the
2056 ;; insert-char can still be run.
2057 (if (not comment)
2058 (while (> count 0)
2059 (insert "\\#")
2060 (setq count (1- count)))))
2061 (t nil))))
2062 (insert-char ?# count))))
2063
2064 (defun tcl-hashify-buffer ()
2065 "Quote all `#'s in current buffer that aren't Tcl comments."
2066 (interactive)
2067 (save-excursion
2068 (goto-char (point-min))
2069 (if (and tcl-pps-has-arg-6 tcl-use-hairy-comment-detector)
2070 (let (state
2071 result)
2072 (while (< (point) (point-max))
2073 (setq result (tcl-hairy-scan-for-comment state (point-max) t))
2074 (if (car result)
2075 (beginning-of-line 2)
2076 (backward-char)
2077 (if (eq ?# (following-char))
2078 (insert "\\"))
2079 (forward-char))
2080 (setq state (cdr result))))
2081 (while (and (< (point) (point-max))
2082 (search-forward "#" nil 'move))
2083 (if (tcl-real-comment-p)
2084 (beginning-of-line 2)
2085 ;; There's really no good way for the simple converter to
2086 ;; work. So we just quote # if it isn't already quoted.
2087 ;; Bogus, but it works.
2088 (backward-char)
2089 (if (not (eq ?\\ (preceding-char)))
2090 (insert "\\"))
2091 (forward-char))))))
2092
2093 (defun tcl-indent-for-comment ()
2094 "Indent this line's comment to comment column, or insert an empty comment.
2095 Is smart about syntax of Tcl comments.
2096 Parts of this were taken from indent-for-comment (simple.el)."
2097 (interactive "*")
2098 (end-of-line)
2099 (or (tcl-in-comment)
2100 (progn
2101 ;; Not in a comment, so we have to insert one. Create an
2102 ;; empty comment (since there isn't one on this line). If
2103 ;; line is not blank, make sure we insert a ";" first.
2104 (skip-chars-backward " \t")
2105 (let ((eolpoint (point)))
2106 (beginning-of-line)
2107 (if (/= (point) eolpoint)
2108 (progn
2109 (goto-char eolpoint)
2110 (insert
2111 (if (tcl-real-command-p) "" ";")
2112 "# ")
2113 (backward-char))))))
2114 ;; Point is just after the "#" starting a comment. Move it as
2115 ;; appropriate.
2116 (let* ((indent (if comment-indent-hook
2117 (funcall comment-indent-hook)
2118 (funcall comment-indent-function)))
2119 (begpos (progn
2120 (backward-char)
2121 (point))))
2122 (if (/= begpos indent)
2123 (progn
2124 (skip-chars-backward " \t" (save-excursion
2125 (beginning-of-line)
2126 (point)))
2127 (delete-region (point) begpos)
2128 (indent-to indent)))
2129 (looking-at comment-start-skip) ; Always true.
2130 (goto-char (match-end 0))
2131 ;; I don't like the effect of the next two.
2132 ;;(skip-chars-backward " \t" (match-beginning 0))
2133 ;;(skip-chars-backward "^ \t" (match-beginning 0))
2134 ))
2135
2136 ;; The following was inspired by the Tcl editing mode written by
2137 ;; Gregor Schmid <schmid@fb3-s7.math.TU-Berlin.DE>. His version also
2138 ;; attempts to snarf the command line options from the command line,
2139 ;; but I didn't think that would really be that helpful (doesn't seem
2140 ;; like it owould be right enough. His version also looks for the
2141 ;; "#!/bin/csh ... exec" hack, but that seemed even less useful.
2142 ;; FIXME should make sure that the application mentioned actually
2143 ;; exists.
2144 (defun tcl-guess-application ()
2145 "Attempt to guess Tcl application by looking at first line.
2146 The first line is assumed to look like \"#!.../program ...\"."
2147 (save-excursion
2148 (goto-char (point-min))
2149 (if (looking-at "#![^ \t]*/\\([^ \t\n/]+\\)\\([ \t]\\|$\\)")
2150 (progn
2151 (make-local-variable 'tcl-application)
2152 (setq tcl-application (buffer-substring (match-beginning 1)
2153 (match-end 1)))))))
2154
2155 ;; This only exists to put on the menubar. I couldn't figure out any
2156 ;; other way to do it. FIXME should take "number of #-marks"
2157 ;; argument.
2158 (defun tcl-uncomment-region (beg end)
2159 "Uncomment region."
2160 (interactive "r")
2161 (comment-region beg end -1))
2162
2163 \f
2164
2165 ;;
2166 ;; XEmacs menu support.
2167 ;; Taken from schmid@fb3-s7.math.TU-Berlin.DE (Gregor Schmid),
2168 ;; who wrote a different Tcl mode.
2169 ;; We also have support for menus in FSF. We do this by
2170 ;; loading the XEmacs menu emulation code.
2171 ;;
2172
2173 (defun tcl-popup-menu (e)
2174 (interactive "@e")
2175 (and tcl-using-emacs-19
2176 (not tcl-using-xemacs-19)
2177 (if tcl-using-emacs-19-23
2178 (require 'lmenu)
2179 ;; CAVEATS:
2180 ;; * lmenu.el provides 'menubar, which is bogus.
2181 ;; * lmenu.el causes menubars to be turned on everywhere.
2182 ;; Doubly bogus!
2183 ;; Both of these problems are fixed in Emacs 19.23. People
2184 ;; using an Emacs before that just suffer.
2185 (require 'menubar "lmenu"))) ;; This is annoying
2186 ;; IMHO popup-menu should be autoloaded in FSF Emacs. Oh well.
2187 (popup-menu tcl-xemacs-menu))
2188
2189 \f
2190
2191 ;;
2192 ;; Quoting and unquoting functions.
2193 ;;
2194
2195 ;; This quoting is sufficient to protect eg a filename from any sort
2196 ;; of expansion or splitting. Tcl quoting sure sucks.
2197 (defun tcl-quote (string)
2198 "Quote STRING according to Tcl rules."
2199 (mapconcat (function (lambda (char)
2200 (if (memq char '(?[ ?] ?{ ?} ?\\ ?\" ?$ ? ?\;))
2201 (concat "\\" (char-to-string char))
2202 (char-to-string char))))
2203 string ""))
2204
2205 \f
2206
2207 ;;
2208 ;; Bug reporting.
2209 ;;
2210
2211 (and (fboundp 'eval-when-compile)
2212 (eval-when-compile
2213 (require 'reporter)))
2214
2215 (defun tcl-submit-bug-report ()
2216 "Submit via mail a bug report on Tcl mode."
2217 (interactive)
2218 (require 'reporter)
2219 (and
2220 (y-or-n-p "Do you really want to submit a bug report on Tcl mode? ")
2221 (reporter-submit-bug-report
2222 tcl-maintainer
2223 (concat "Tcl mode " tcl-version)
2224 '(tcl-indent-level
2225 tcl-continued-indent-level
2226 tcl-auto-newline
2227 tcl-tab-always-indent
2228 tcl-use-hairy-comment-detector
2229 tcl-electric-hash-style
2230 tcl-help-directory-list
2231 tcl-use-smart-word-finder
2232 tcl-application
2233 tcl-command-switches
2234 tcl-prompt-regexp
2235 inferior-tcl-source-command
2236 tcl-using-emacs-19
2237 tcl-using-emacs-19-23
2238 tcl-using-xemacs-19
2239 tcl-proc-list
2240 tcl-proc-regexp
2241 tcl-typeword-list
2242 tcl-keyword-list
2243 tcl-font-lock-keywords
2244 tcl-pps-has-arg-6))))
2245
2246 \f
2247
2248 (provide 'tcl)
2249
2250 ;;; tcl.el ends here