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