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