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