1 ;; tcl.el --- Tcl code editing commands for Emacs
3 ;; Copyright (C) 1994 Free Software Foundation, Inc.
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.12 $
11 ;; This file is part of GNU Emacs.
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)
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.
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.
28 ;; Put the following forms in your .emacs to enable autoloading of Tcl
29 ;; mode, and auto-recognition of ".tcl" files.
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))
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:
39 ;; (setq tcl-help-directory-list '("/usr/local/lib/tclx/help"))
41 ;; Also you will want to add the following to your .emacs:
43 ;; (autoload 'tcl-help-on-word "tcl" "Help on Tcl commands" t)
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.
52 ;; tcl|Tom Tromey|tromey@busco.lanl.gov|
53 ;; Major mode for editing Tcl|
54 ;; $Date: 1994/05/22 20:14:59 $|$Revision: 1.12 $|~/modes/tcl.el.Z|
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
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.
68 ; Revision 1.12 1994/05/22 20:14:59 tromey
71 ; Revision 1.11 1994/05/22 20:12:44 tromey
72 ; Fixed mark-defun for 19.23.
75 ; Revision 1.10 1994/05/22 20:02:03 tromey
77 ; Wrote bug-reporting code.
79 ; Revision 1.9 1994/05/22 05:26:51 tromey
82 ; Revision 1.8 1994/05/22 03:38:07 tromey
85 ; Revision 1.7 1994/05/03 01:23:42 tromey
86 ; *** empty log message ***
88 ; Revision 1.6 1994/04/23 16:23:36 tromey
89 ; Wrote tcl-indent-for-comment
92 ;; 18-Mar-1994 Tom Tromey Fourth beta release.
93 ;; Added {un,}comment-region to menu. Idea from
94 ;; Mike Scheidler <c23mts@kocrsv01.delcoelect.com>
95 ;; 17-Mar-1994 Tom Tromey
96 ;; Fixed tcl-restart-with-file. Bug fix attempt in
97 ;; tcl-internal-end-of-defun.
98 ;; 16-Mar-1994 Tom Tromey Third beta release
99 ;; Added support code for menu (from Tcl mode written by
100 ;; schmid@fb3-s7.math.TU-Berlin.DE (Gregor Schmid)).
101 ;; 12-Mar-1994 Tom Tromey
102 ;; Better documentation for inferior-tcl-buffer. Wrote
103 ;; tcl-restart-with-file. Wrote Lucid Emacs menu (but no
104 ;; code to install it).
105 ;; 12-Mar-1994 Tom Tromey
106 ;; Wrote tcl-guess-application. Another stab at making
107 ;; tcl-omit-ws-regexp work.
108 ;; 10-Mar-1994 Tom Tromey Second beta release
109 ;; Last Modified: Thu Mar 10 01:24:25 1994 (Tom Tromey)
110 ;; Wrote perl-mode style line indentation command.
111 ;; Wrote more documentation. Added tcl-continued-indent-level.
112 ;; Integrated help code.
113 ;; 8-Mar-1994 Tom Tromey
114 ;; Last Modified: Tue Mar 8 11:58:44 1994 (Tom Tromey)
116 ;; 6-Mar-1994 Tom Tromey
117 ;; Last Modified: Sun Mar 6 18:55:41 1994 (Tom Tromey)
118 ;; Updated auto-newline support.
119 ;; 6-Mar-1994 Tom Tromey Beta release
120 ;; Last Modified: Sat Mar 5 17:24:32 1994 (Tom Tromey)
121 ;; Wrote tcl-hashify-buffer. Other minor bug fixes.
122 ;; 5-Mar-1994 Tom Tromey
123 ;; Last Modified: Sat Mar 5 16:11:20 1994 (Tom Tromey)
124 ;; Wrote electric-hash code.
125 ;; 3-Mar-1994 Tom Tromey
126 ;; Last Modified: Thu Mar 3 02:53:40 1994 (Tom Tromey)
127 ;; Added code to handle auto-fill in comments.
128 ;; Added imenu support code.
130 ;; Better font-lock support.
131 ;; 28-Feb-1994 Tom Tromey
132 ;; Last Modified: Mon Feb 28 14:08:05 1994 (Tom Tromey)
133 ;; Made tcl-figure-type more easily configurable.
134 ;; 28-Feb-1994 Tom Tromey
135 ;; Last Modified: Mon Feb 28 01:02:58 1994 (Tom Tromey)
136 ;; Wrote inferior-tcl mode.
137 ;; 16-Feb-1994 Tom Tromey
138 ;; Last Modified: Wed Feb 16 17:05:19 1994 (Tom Tromey)
139 ;; Added support for font-lock-mode.
140 ;; 29-Oct-1993 Tom Tromey
141 ;; Last Modified: Sun Oct 24 17:39:14 1993 (Tom Tromey)
142 ;; Patches from Guido Bosch to make things work with Lucid Emacs.
143 ;; 22-Oct-1993 Tom Tromey
144 ;; Last Modified: Fri Oct 22 15:26:46 1993 (Tom Tromey)
145 ;; Made many characters have "_" syntax class; suggested by Guido
146 ;; Bosch <Guido.Bosch@loria.fr>. Note that this includes the "$"
147 ;; character, which might be a change you'd notice.
148 ;; 21-Oct-1993 Tom Tromey
149 ;; Last Modified: Thu Oct 21 20:28:40 1993 (Tom Tromey)
150 ;; More fixes for tcl-omit-ws-regexp.
151 ;; 20-Oct-1993 Tom Tromey
152 ;; Started keeping history. Fixed tcl-{beginning,end}-of-defun.
153 ;; Added some code to make things work with Emacs 18.
156 ;; Guido Bosch <Guido.Bosch@loria.fr>
157 ;; pgs1002@esc.cam.ac.uk (Dr P.G. Sjoerdsma)
158 ;; Mike Scheidler <c23mts@kocrsv01.delcoelect.com>
159 ;; Matt Newman <men@charney.colorado.edu>
160 ;; rwhitby@research.canon.oz.au (Rod Whitby)
161 ;; h9118101@hkuxa.hku.hk (Yip Chi Lap [Beta])
162 ;; Pertti Tapio Kasanen <ptk@delta.hut.fi>
163 ;; schmid@fb3-s7.math.TU-Berlin.DE (Gregor Schmid)
166 ;; * indent-region should skip blank lines. (It does in v19, so I'm
167 ;; not motivated to fix it here).
168 ;; * In Tcl "#" is not always a comment character. This can confuse
169 ;; tcl.el in certain circumstances. For now the only workaround is
170 ;; to enclose offending hash characters in quotes or precede it with
171 ;; a backslash. Note that using braces won't work -- quotes change
172 ;; the syntax class of characters between them, while braces do not.
173 ;; The electric-# mode helps alleviate this problem somewhat.
174 ;; * indent-tcl-exp is untested.
175 ;; * Doesn't work under Emacs 18 yet.
176 ;; * There's been a report that font-lock does strange things under
177 ;; Lucid Emacs 19.6. For instance in "proc foobar", the space
178 ;; before "foobar" is highlighted.
181 ;; * make add-log-tcl-defun smarter. should notice if we are in the
182 ;; middle of a defun, or between defuns. should notice if point is
183 ;; on first line of defun (or maybe even in comments before defun).
184 ;; * Allow continuation lines to be indented under the first argument
185 ;; of the preceeding line, like this:
188 ;; * There is a request that indentation work like this:
189 ;; button .fred -label Fred \
190 ;; -command {puts fred}
191 ;; * Should have tcl-complete-symbol that queries the inferior process.
192 ;; * Should have describe-symbol that works by sending the magic
193 ;; command to a tclX process.
194 ;; * Need C-x C-e binding (tcl-eval-last-exp).
195 ;; * Write indent-region function that is faster than indenting each
196 ;; line individually.
197 ;; * tcl-figure-type should stop at "beginning of line" (only ws
198 ;; before point, and no "\" on previous line). (see tcl-real-command-p).
199 ;; * overrides some comint keybindings; fix.
200 ;; * Trailing \ will eat blank lines. Should deal with this.
201 ;; (this would help catch some potential bugs).
202 ;; * Inferior should display in half the screen, not the whole screen.
203 ;; * Indentation should deal with "switch".
204 ;; * Consider writing code to find help files automatically (for
206 ;; * `#' shouldn't insert `\#' when point is in string.
212 ;; I sure wish Emacs had a package that made it easy to extract this
213 ;; sort of information.
214 (defconst tcl-using-emacs-19 (string-match "19\\." emacs-version)
215 "Nil unless using Emacs 19 (Lucid or FSF).")
217 ;; FIXME this will break on Emacs 19.100.
218 (defconst tcl-using-emacs-19.23
219 (string-match "19\\.\\(2[3-9]\\|[3-9][0-9]\\)" emacs-version)
220 "Nil unless using Emacs 19.23 or later.")
222 (defconst tcl-using-lemacs-19 (string-match "Lucid" emacs-version)
223 "Nil unless using Lucid Emacs).")
227 ;; When compiling under GNU Emacs, load imenu during compilation. If
228 ;; you have 19.22 or earlier, comment this out, or get imenu.
229 (and (fboundp 'eval-when-compile)
231 (if (and tcl-using-emacs-19
232 (not tcl-using-lemacs-19))
236 (defconst tcl-version "$Revision: 1.12 $")
237 (defconst tcl-maintainer "Tom Tromey <tromey@busco.lanl.gov>")
243 (defvar tcl-indent-level 4
244 "*Indentation of Tcl statements with respect to containing block.")
246 (defvar tcl-continued-indent-level 4
247 "*Indentation of continuation line relative to first line of command.")
249 (defvar tcl-auto-newline nil
250 "*Non-nil means automatically newline before and after braces
251 inserted in Tcl code.")
253 (defvar tcl-tab-always-indent t
254 "*Control effect of TAB key.
255 If t (the default), always indent current line.
256 If nil and point is not in the indentation area at the beginning of
257 the line, a TAB is inserted.
258 Other values cause the first possible action from the following list
261 1. Move from beginning of line to correct indentation.
262 2. Delete an empty comment.
263 3. Move forward to start of comment, indenting if necessary.
264 4. Move forward to end of line, indenting if necessary.
265 5. Create an empty comment.
266 6. Move backward to start of comment, indenting if necessary.")
268 (defvar tcl-use-hairy-comment-detector t
269 "*If not `nil', the the more complicated, but slower, comment
270 detecting function is used. This variable is only used in GNU Emacs
271 19 (the fast function is always used elsewhere).")
273 (defvar tcl-electric-hash-style 'smart
274 "*Style of electric hash insertion to use.
275 Possible values are 'backslash, meaning that `\\' quoting should be
276 done; `quote, meaning that `\"' quoting should be done; 'smart,
277 meaning that the choice between 'backslash and 'quote should be
278 made depending on the number of hashes inserted; or nil, meaning that
279 no quoting should be done. Any other value for this variable is
280 taken to mean 'smart. The default is 'smart.")
282 (defvar tcl-help-directory-list nil
283 "*List of topmost directories containing TclX help files")
285 (defvar tcl-use-smart-word-finder t
286 "*If not nil, use a better way of finding the current word when
287 looking up help on a Tcl command.")
289 (defvar tcl-application "wish"
290 "*Name of Tcl application to run in inferior Tcl mode.")
292 (defvar tcl-command-switches nil
293 "*Switches to supply to `tcl-application'.")
295 (defvar tcl-prompt-regexp "^\\(% \\|\\)"
296 "*If not nil, a regexp that will match the prompt in the inferior process.
297 If nil, the prompt is the name of the application with \">\" appended.
299 The default is \"^\\(% \\|\\)\", which will match the default primary
300 and secondary prompts for tclsh and wish.")
302 (defvar inferior-tcl-source-command "source %s\n"
303 "*Format-string for building a Tcl command to load a file.
304 This format string should use `%s' to substitute a file name
305 and should result in a Tcl expression that will command the
306 inferior Tcl to load that file. The filename will be appropriately
310 ;; Keymaps, abbrevs, syntax tables.
313 (defvar tcl-mode-abbrev-table nil
314 "Abbrev table in use in Tcl-mode buffers.")
315 (if tcl-mode-abbrev-table
317 (define-abbrev-table 'tcl-mode-abbrev-table ()))
319 (defvar tcl-mode-map ()
320 "Keymap used in Tcl mode.")
322 (defvar tcl-mode-syntax-table nil
323 "Syntax table in use in Tcl-mode buffers.")
324 (if tcl-mode-syntax-table
326 (setq tcl-mode-syntax-table (make-syntax-table))
327 (modify-syntax-entry ?% "_" tcl-mode-syntax-table)
328 (modify-syntax-entry ?@ "_" tcl-mode-syntax-table)
329 (modify-syntax-entry ?& "_" tcl-mode-syntax-table)
330 (modify-syntax-entry ?* "_" tcl-mode-syntax-table)
331 (modify-syntax-entry ?+ "_" tcl-mode-syntax-table)
332 (modify-syntax-entry ?- "_" tcl-mode-syntax-table)
333 (modify-syntax-entry ?. "_" tcl-mode-syntax-table)
334 (modify-syntax-entry ?: "_" tcl-mode-syntax-table)
335 (modify-syntax-entry ?! "_" tcl-mode-syntax-table)
336 (modify-syntax-entry ?$ "_" tcl-mode-syntax-table) ; FIXME use "'"?
337 (modify-syntax-entry ?/ "_" tcl-mode-syntax-table)
338 (modify-syntax-entry ?~ "_" tcl-mode-syntax-table)
339 (modify-syntax-entry ?< "_" tcl-mode-syntax-table)
340 (modify-syntax-entry ?= "_" tcl-mode-syntax-table)
341 (modify-syntax-entry ?> "_" tcl-mode-syntax-table)
342 (modify-syntax-entry ?| "_" tcl-mode-syntax-table)
343 (modify-syntax-entry ?\( "()" tcl-mode-syntax-table)
344 (modify-syntax-entry ?\) ")(" tcl-mode-syntax-table)
345 (modify-syntax-entry ?\; "." tcl-mode-syntax-table)
346 (modify-syntax-entry ?\n "> " tcl-mode-syntax-table)
347 (modify-syntax-entry ?\f "> " tcl-mode-syntax-table)
348 (modify-syntax-entry ?# "< " tcl-mode-syntax-table))
350 (defvar inferior-tcl-mode-map nil
351 "Keymap used in Inferior Tcl mode.")
354 (defvar tcl-lucid-menu
356 ["Beginning of function" tcl-beginning-of-defun t]
357 ["End of function" tcl-end-of-defun t]
358 ["Mark function" tcl-mark-defun t]
359 ["Indent region" indent-region t]
360 ["Comment region" comment-region t]
361 ["Uncomment region" tcl-uncomment-region t]
363 ["Show Tcl process buffer" inferior-tcl t]
364 ["Send function to Tcl process" tcl-eval-defun t]
365 ["Send region to Tcl process" tcl-eval-region t]
366 ["Send file to Tcl process" tcl-load-file t]
367 ["Restart Tcl process with file" tcl-restart-with-file t]
369 ["Tcl help" tcl-help-on-word t])
370 "Lucid Emacs menu for Tcl mode.")
372 ;; GNU Emacs does menus via keymaps. Do it in a function in case we
373 ;; later decide to add it to inferior Tcl mode as well.
374 (defun tcl-add-fsf-menu (map)
375 (define-key map [menu-bar] (make-sparse-keymap))
376 ;; This fails in Emacs 19.22 and earlier.
378 (define-key map [menu-bar tcl]
379 (cons "Tcl" (make-lucid-menu-keymap "Tcl" (cdr tcl-lucid-menu)))))
381 (defun tcl-fill-mode-map ()
382 (define-key tcl-mode-map "{" 'tcl-electric-char)
383 (define-key tcl-mode-map "}" 'tcl-electric-brace)
384 (define-key tcl-mode-map "[" 'tcl-electric-char)
385 (define-key tcl-mode-map "]" 'tcl-electric-char)
386 (define-key tcl-mode-map ";" 'tcl-electric-char)
387 (define-key tcl-mode-map "#" 'tcl-electric-hash)
389 (define-key tcl-mode-map "\e\C-a" 'tcl-beginning-of-defun)
391 (define-key tcl-mode-map "\e\C-e" 'tcl-end-of-defun)
393 (define-key tcl-mode-map "\e\C-h" 'tcl-mark-defun)
394 (define-key tcl-mode-map "\e\C-q" 'indent-tcl-exp)
395 (define-key tcl-mode-map "\177" 'backward-delete-char-untabify)
396 (define-key tcl-mode-map "\t" 'tcl-indent-command)
397 (define-key tcl-mode-map "\M-;" 'tcl-indent-for-comment)
398 (define-key tcl-mode-map "\M-\C-x" 'tcl-eval-defun)
399 (and (fboundp 'comment-region)
400 (define-key tcl-mode-map "\C-c\C-c" 'comment-region))
401 (define-key tcl-mode-map "\C-c\C-d" 'tcl-help-on-word)
402 (define-key tcl-mode-map "\C-c\C-e" 'tcl-eval-defun)
403 (define-key tcl-mode-map "\C-c\C-l" 'tcl-load-file)
404 (define-key tcl-mode-map "\C-c\C-p" 'inferior-tcl)
405 (define-key tcl-mode-map "\C-c\C-r" 'tcl-eval-region)
406 (define-key tcl-mode-map "\C-c\C-z" 'switch-to-tcl)
409 (if tcl-using-emacs-19
410 (if tcl-using-lemacs-19
411 ;; In Lucid, button 3 seems to be the standard for this.
412 (define-key tcl-mode-map 'button3 'tcl-popup-menu)
413 ;; In FSF 19, there is no standard, so I use shift-button2.
414 (tcl-add-fsf-menu tcl-mode-map)
415 (define-key tcl-mode-map [S-down-mouse-2] 'tcl-popup-menu))))
417 (defun tcl-fill-inferior-map ()
418 (define-key inferior-tcl-mode-map "\e\C-a" 'tcl-beginning-of-defun)
419 (define-key inferior-tcl-mode-map "\e\C-e" 'tcl-end-of-defun)
420 (define-key inferior-tcl-mode-map "\177" 'backward-delete-char-untabify)
421 (define-key inferior-tcl-mode-map "\M-\C-x" 'tcl-eval-defun)
422 (define-key inferior-tcl-mode-map "\C-c\C-d" 'tcl-help-on-word)
423 (define-key inferior-tcl-mode-map "\C-c\C-e" 'tcl-eval-defun)
424 (define-key inferior-tcl-mode-map "\C-c\C-l" 'tcl-load-file)
425 (define-key inferior-tcl-mode-map "\C-c\C-p" 'inferior-tcl)
426 (define-key inferior-tcl-mode-map "\C-c\C-r" 'tcl-eval-region)
427 (define-key inferior-tcl-mode-map "\C-c\C-z" 'switch-to-tcl))
431 (setq tcl-mode-map (make-sparse-keymap))
434 (if inferior-tcl-mode-map
436 ;; FIXME Use keymap inheritance here? FIXME we override comint
437 ;; keybindings here. Maybe someone has a better set?
438 (setq inferior-tcl-mode-map (copy-keymap comint-mode-map))
439 (tcl-fill-inferior-map))
442 (defvar inferior-tcl-buffer nil
443 "*The current inferior-tcl process buffer.
445 MULTIPLE PROCESS SUPPORT
446 ===========================================================================
447 To run multiple Tcl processes, you start the first up with
448 \\[inferior-tcl]. It will be in a buffer named `*inferior-tcl*'.
449 Rename this buffer with \\[rename-buffer]. You may now start up a new
450 process with another \\[inferior-tcl]. It will be in a new buffer,
451 named `*inferior-tcl*'. You can switch between the different process
452 buffers with \\[switch-to-buffer].
454 Commands that send text from source buffers to Tcl processes -- like
455 `tcl-eval-defun' or `tcl-load-file' -- have to choose a process to
456 send to, when you have more than one Tcl process around. This is
457 determined by the global variable `inferior-tcl-buffer'. Suppose you
458 have three inferior Lisps running:
462 *inferior-tcl* inferior-tcl<3>
463 If you do a \\[tcl-eval-defun] command on some Lisp source code, what
464 process do you send it to?
466 - If you're in a process buffer (foo, bar, or *inferior-tcl*),
467 you send it to that process.
468 - If you're in some other buffer (e.g., a source file), you
469 send it to the process attached to buffer `inferior-tcl-buffer'.
470 This process selection is performed by function `inferior-tcl-proc'.
472 Whenever \\[inferior-tcl] fires up a new process, it resets
473 `inferior-tcl-buffer' to be the new process's buffer. If you only run
474 one process, this does the right thing. If you run multiple
475 processes, you can change `inferior-tcl-buffer' to another process
476 buffer with \\[set-variable].")
479 ;; Hooks and other customization.
482 (defvar tcl-mode-hook nil
483 "Hook run on entry to Tcl mode.
485 Several functions exist which are useful to run from your
486 `tcl-mode-hook' (see each function's documentation for more
489 tcl-guess-application
490 Guesses a default setting for `tcl-application' based on any
491 \"#!\" line at the top of the file.
493 Quotes all \"#\" characters that don't correspond to actual
494 Tcl comments. (Useful when editing code not originally created
497 Auto-filling of Tcl comments.
499 Emacs 19 users can add functions to the hook with `add-hook':
501 (add-hook 'tcl-mode-hook 'tcl-guess-application)
503 Emacs 18 users must use `setq':
505 (setq tcl-mode-hook (cons 'tcl-guess-application tcl-mode-hook))")
508 (defvar inferior-tcl-mode-hook nil
509 "Hook for customizing Inferior Tcl mode.")
511 (defvar tcl-proc-list
513 "List of commands whose first argument defines something.
514 This exists because some people (eg, me) use \"defvar\" et al.
515 Call `tcl-set-proc-regexp' and `tcl-set-font-lock-keywords'
516 after changing this list.")
518 (defvar tcl-proc-regexp nil
519 "Regexp to use when matching proc headers.")
521 (defvar tcl-typeword-list
523 "List of Tcl keywords denoting \"type\". Used only for highlighting.
524 Call `tcl-set-font-lock-keywords' after changing this list.")
526 ;; Generally I've picked control operators to be keywords.
527 (defvar tcl-keyword-list
528 '("if" "then" "else" "elseif" "for" "foreach" "break" "continue" "while"
529 "eval" "case" "in" "switch" "default" "exit" "error" "proc" "return"
530 "uplevel" "loop" "for_array_keys" "for_recursive_glob" "for_file")
531 "List of Tcl keywords. Used only for highlighting.
532 Default list includes some TclX keywords.
533 Call `tcl-set-font-lock-keywords' after changing this list.")
535 (defvar tcl-font-lock-keywords nil
536 "Keywords to highlight for Tcl. See variable `font-lock-keywords'.
537 This variable is generally set from `tcl-proc-regexp',
538 `tcl-typeword-list', and `tcl-keyword-list' by the function
539 `tcl-set-font-lock-keywords'.")
541 ;; FIXME need some way to recognize variables because array refs look
543 (defvar tcl-type-alist
546 ("catch" tcl-commands)
547 ("if" tcl-expr "then" tcl-commands)
548 ("elseif" tcl-expr "then" tcl-commands)
549 ("elseif" tcl-expr tcl-commands)
550 ("if" tcl-expr tcl-commands)
551 ("while" tcl-expr tcl-commands)
552 ("for" tcl-commands tcl-expr tcl-commands tcl-commands)
553 ("foreach" nil nil tcl-commands)
554 ("for_file" nil nil tcl-commands)
555 ("for_array_keys" nil nil tcl-commands)
556 ("for_recursive_glob" nil nil nil tcl-commands)
557 ;; Loop handling is not perfect, because the third argument can be
558 ;; either a command or an expr, and there is no real way to look
560 ("loop" nil tcl-expr tcl-expr tcl-commands)
561 ("loop" nil tcl-expr tcl-commands)
563 "Alist that controls indentation.
564 \(Actually, this really only controls what happens on continuation lines).
565 Each entry looks like `(KEYWORD TYPE ...)'.
566 Each type entry describes a sexp after the keyword, and can be one of:
567 * nil, meaning that this sexp has no particular type.
568 * tcl-expr, meaning that this sexp is an arithmetic expression.
569 * tcl-commands, meaning that this sexp holds Tcl commands.
570 * a string, which must exactly match the string at the corresponding
571 position for a match to be made.
573 For example, the entry for the \"loop\" command is:
575 (\"loop\" nil tcl-expr tcl-commands)
577 This means that the \"loop\" command has three arguments. The first
578 argument is ignored (for indentation purposes). The second argument
579 is a Tcl expression, and the last argument is Tcl commands.")
581 (defvar tcl-explain-indentation nil
582 "If not `nil', debugging message will be printed during indentation.")
587 ;; Work around differences between various versions of Emacs.
590 ;; We use this because Lemacs 19.9 has what we need.
591 (defconst tcl-pps-has-arg-6
592 (or tcl-using-emacs-19
593 (and tcl-using-lemacs-19
596 (parse-partial-sexp (point) (point) nil nil nil t)
599 "t if using an emacs which supports sixth (\"commentstop\") argument
600 to parse-partial-sexp.")
602 ;; Its pretty bogus to have to do this, but there is no easier way to
603 ;; say "match not syntax-1 and not syntax-2". Too bad you can't put
604 ;; \s in [...]. This sickness is used in Emacs 19 to match a defun
605 ;; starter. (It is used for this in v18 as well).
606 ;;(defconst tcl-omit-ws-regexp
608 ;; (mapconcat 'char-to-string "w_.()\"\\$'/" "\\|\\s")
610 ;; "Regular expression that matches everything except space, comment
611 ;;starter, and comment ender syntax codes.")
613 ;; FIXME? Instead of using the hairy regexp above, we just use a
615 ;;(defconst tcl-omit-ws-regexp "^[^] \t\n#}]\\S(*"
616 ;; "Regular expression used in locating function definitions.")
618 ;; Here's another stab. I think this one actually works. Now the
619 ;; problem seems to be that there is a bug in Emacs 19.22 where
620 ;; end-of-defun doesn't really use the brace matching the one that
621 ;; trails defun-prompt-regexp.
622 (defconst tcl-omit-ws-regexp "^[^ \t\n#}][^\n}]+}*[ \t]+")
624 (defun tcl-internal-beginning-of-defun (&optional arg)
625 "Move backward to next beginning-of-defun.
626 With argument, do this that many times.
627 Returns t unless search stops due to end of buffer."
629 (if (or (null arg) (= arg 0))
636 (re-search-backward tcl-omit-ws-regexp nil 'move 1))))
637 (while (and (looking-at "[]#}]")
639 (re-search-backward tcl-omit-ws-regexp nil 'move 1)))))
641 (not (null success))))
643 (defun tcl-internal-end-of-defun (&optional arg)
644 "Move forward to next end of defun.
645 An end of a defun is found by moving forward from the beginning of one."
647 (if (or (null arg) (= arg 0)) (setq arg 1))
648 (let ((start (point)))
649 ;; Was forward-char. I think this works a little better.
651 (tcl-beginning-of-defun)
653 (while (and (re-search-forward tcl-omit-ws-regexp nil 'move 1)
654 (progn (beginning-of-line) t)
656 (progn (forward-line) t)))
657 (let ((next-line (save-excursion
660 (while (< (point) next-line)
663 (if (> (point) start) (setq arg (1- arg))))))
665 ;; In Emacs 19, we can use begining-of-defun as long as we set up a
666 ;; certain regexp. In Emacs 18, we need our own function.
667 (fset 'tcl-beginning-of-defun
668 (if tcl-using-emacs-19
670 'tcl-internal-beginning-of-defun))
672 ;; Ditto end-of-defun.
673 (fset 'tcl-end-of-defun
674 (if tcl-using-emacs-19
676 'tcl-internal-end-of-defun))
678 ;; Internal mark-defun that is used for losing Emacsen.
679 (defun tcl-internal-mark-defun ()
680 "Put mark at end of Tcl function, point at beginning."
684 (if tcl-using-emacs-19
685 (push-mark (point) nil t)
687 (tcl-beginning-of-defun)
688 (backward-paragraph))
690 ;; In GNU Emacs 19.23 and later, mark-defun works as advertised. I
691 ;; don't know about Lucid Emacs, so for now it and Emacs 18 just lose.
692 (fset 'tcl-mark-defun
693 (if tcl-using-emacs-19.23
695 'tcl-internal-mark-defun))
700 ;; Some helper functions.
703 (defun tcl-set-proc-regexp ()
704 "Set `tcl-proc-regexp' from variable `tcl-proc-list'."
705 (setq tcl-proc-regexp (concat "^\\("
706 (mapconcat 'identity tcl-proc-list "\\|")
709 (defun tcl-set-font-lock-keywords ()
710 "Set `tcl-font-lock-keywords'.
711 Uses variables `tcl-proc-regexp' and `tcl-keyword-list'."
712 (setq tcl-font-lock-keywords
714 ;; Names of functions (and other "defining things").
715 (list (concat tcl-proc-regexp "\\([^ \t\n]+\\)")
716 2 'font-lock-function-name-face)
718 ;; Names of type-defining things.
719 (list (concat "\\(\\s-\\|^\\)\\("
720 ;; FIXME Use 'regexp-quote?
721 (mapconcat 'identity tcl-typeword-list "\\|")
723 2 'font-lock-type-face)
725 ;; Keywords. Only recognized if surrounded by whitespace.
726 ;; FIXME consider using "not word or symbol", not
728 (cons (concat "\\(\\s-\\|^\\)\\("
729 ;; FIXME Use regexp-quote?
730 (mapconcat 'identity tcl-keyword-list "\\|")
737 (tcl-set-proc-regexp))
739 (if tcl-font-lock-keywords
741 (tcl-set-font-lock-keywords))
750 "Major mode for editing Tcl code.
751 Expression and list commands understand all Tcl brackets.
752 Tab indents for Tcl code.
753 Paragraphs are separated by blank lines only.
754 Delete converts tabs to spaces as it moves back.
756 Variables controlling indentation style:
758 Indentation of Tcl statements within surrounding block.
759 tcl-continued-indent-level
760 Indentation of continuation line relative to first line of command.
762 Variables controlling user interaction with mode (see variable
763 documentation for details):
764 tcl-tab-always-indent
765 Controls action of TAB key.
767 Non-nil means automatically newline before and after braces, brackets,
768 and semicolons inserted in Tcl code.
769 tcl-electric-hash-style
770 Controls action of `#' key.
771 tcl-use-hairy-comment-detector
772 If t, use more complicated, but slower, comment detector.
773 This variable is only used in GNU Emacs 19.
775 Turning on Tcl mode calls the value of the variable `tcl-mode-hook'
776 with no args, if that value is non-nil. Read the documentation for
777 `tcl-mode-hook' to see what kinds of interesting hook functions
783 (kill-all-local-variables)
784 (use-local-map tcl-mode-map)
785 (setq major-mode 'tcl-mode)
786 (setq mode-name "Tcl")
787 (setq local-abbrev-table tcl-mode-abbrev-table)
788 (set-syntax-table tcl-mode-syntax-table)
790 (make-local-variable 'paragraph-start)
791 (setq paragraph-start (concat "^$\\|" page-delimiter))
792 (make-local-variable 'paragraph-separate)
793 (setq paragraph-separate paragraph-start)
794 (make-local-variable 'paragraph-ignore-fill-prefix)
795 (setq paragraph-ignore-fill-prefix t)
797 (make-local-variable 'indent-line-function)
798 (setq indent-line-function 'tcl-indent-line)
799 ;; Tcl doesn't require a final newline.
800 ;; (make-local-variable 'require-final-newline)
801 ;; (setq require-final-newline t)
803 (make-local-variable 'comment-start)
804 (setq comment-start "# ")
805 (make-local-variable 'comment-start-skip)
806 (setq comment-start-skip "#+ *")
807 (make-local-variable 'comment-column)
808 (setq comment-column 40)
809 (make-local-variable 'comment-end)
810 (setq comment-end "")
812 (make-local-variable 'font-lock-keywords)
813 (setq font-lock-keywords tcl-font-lock-keywords)
815 ;; The following only really makes sense under GNU Emacs 19.
816 (setq imenu-create-index-function 'tcl-imenu-create-index-function)
817 (make-local-variable 'parse-sexp-ignore-comments)
819 (if tcl-using-emacs-19
821 ;; This can only be set to t in Emacs 19 and Lucid Emacs.
822 ;; Emacs 18 and Epoch lose.
823 (setq parse-sexp-ignore-comments t)
824 ;; Lucid Emacs has defun-prompt-regexp, but I don't believe
825 ;; that it works for end-of-defun -- only for
826 ;; beginning-of-defun.
827 (make-local-variable 'defun-prompt-regexp)
828 (setq defun-prompt-regexp tcl-omit-ws-regexp)
829 ;; The following doesn't work in Lucid Emacs 19.6, but maybe
830 ;; it will appear in later versions.
831 (make-local-variable 'add-log-current-defun-function)
832 (setq add-log-current-defun-function 'add-log-tcl-defun))
833 (setq parse-sexp-ignore-comments nil))
835 ;; Put Tcl menu into menubar for Lucid Emacs. This happens
836 ;; automatically for GNU Emacs.
837 (if (and tcl-using-lemacs-19
839 (not (assoc "Tcl" current-menubar)))
841 (set-buffer-menubar (copy-sequence current-menubar))
842 (add-menu nil "Tcl" tcl-lucid-menu)))
844 (run-hooks 'tcl-mode-hook))
848 ;; This is used for braces, brackets, and semi (except for closing
849 ;; braces, which are handled specially).
850 (defun tcl-electric-char (arg)
851 "Insert character and correct line's indentation."
853 ;; Indent line first; this looks better if parens blink.
855 (self-insert-command arg)
856 (if (and tcl-auto-newline (= last-command-char ?\;))
861 ;; This is used for closing braces. If tcl-auto-newline is set, can
862 ;; insert a newline both before and after the brace, depending on
863 ;; context. FIXME should this be configurable? Does anyone use this?
864 (defun tcl-electric-brace (arg)
865 "Insert character and correct line's indentation."
867 ;; If auto-newlining and there is stuff on the same line, insert a
872 (skip-chars-backward " \t")
877 ;; In auto-newline case, must insert a newline after each
878 ;; brace. So an explicit loop is needed.
880 (insert last-command-char)
883 (setq arg (1- arg))))
884 (self-insert-command arg))
889 (defun tcl-indent-command (&optional arg)
890 "Indent current line as Tcl code, or in some cases insert a tab character.
891 If tcl-tab-always-indent is t (the default), always indent current line.
892 If tcl-tab-always-indent is nil and point is not in the indentation
893 area at the beginning of the line, a TAB is inserted.
894 Other values of tcl-tab-always-indent cause the first possible action
895 from the following list to take place:
897 1. Move from beginning of line to correct indentation.
898 2. Delete an empty comment.
899 3. Move forward to start of comment, indenting if necessary.
900 4. Move forward to end of line, indenting if necessary.
901 5. Create an empty comment.
902 6. Move backward to start of comment, indenting if necessary."
905 ((not tcl-tab-always-indent)
906 ;; Indent if in indentation area, otherwise insert TAB.
907 (if (<= (current-column) (current-indentation))
909 (self-insert-command arg)))
910 ((eq tcl-tab-always-indent t)
914 ;; "Perl-mode" style TAB command.
915 (let* ((ipoint (point))
919 (comment-p (tcl-in-comment)))
921 ((= ipoint (save-excursion
926 ;; If indenting didn't leave us in column 0, go to the
927 ;; indentation. Otherwise leave point at end of line. This
929 (if (= (point) (save-excursion
933 (back-to-indentation)))
934 ((and comment-p (looking-at "[ \t]*$"))
935 ;; Empty comment, so delete it. We also delete any ";"
936 ;; characters at the end of the line. I think this is
937 ;; friendlier, but I don't know how other people will feel.
939 (skip-chars-backward " \t;")
940 (delete-region (point) eolpoint))
941 ((and comment-p (< ipoint (point)))
942 ;; Before comment, so skip to it.
944 (indent-for-comment))
945 ((/= ipoint eolpoint)
946 ;; Go to end of line (since we're not there yet).
951 (tcl-indent-for-comment))
953 ;; Go to start of comment. We don't leave point where it is
954 ;; because we want to skip comment-start-skip.
956 (indent-for-comment)))))))
958 (defun tcl-indent-line ()
959 "Indent current line as Tcl code.
960 Return the amount the indentation changed by."
961 (let ((indent (calculate-tcl-indent nil))
963 (case-fold-search nil)
964 (pos (- (point-max) (point))))
967 (cond ((eq indent nil)
968 (setq indent (current-indentation)))
970 (skip-chars-forward " \t")
971 (if (listp indent) (setq indent (car indent)))
972 (cond ((= (following-char) ?})
973 (setq indent (- indent tcl-indent-level)))
974 ((= (following-char) ?\])
975 (setq indent (- indent 1))))))
976 (skip-chars-forward " \t")
977 (setq shift-amt (- indent (current-column)))
978 (if (zerop shift-amt)
979 (if (> (- (point-max) pos) (point))
980 (goto-char (- (point-max) pos)))
981 (delete-region beg (point))
983 ;; If initial point was within line's indentation,
984 ;; position after the indentation. Else stay at same point in text.
985 (if (> (- (point-max) pos) (point))
986 (goto-char (- (point-max) pos))))
989 (defun tcl-figure-type ()
990 "Determine type of sexp at point.
991 This is either 'tcl-expr, 'tcl-commands, or nil. Puts point at start
992 of sexp that indicates types.
994 See documentation for variable `tcl-type-alist' for more information."
998 (while (and (< count 5)
1002 ;; FIXME should use "tcl-backward-sexp", which would skip
1003 ;; over entire variables, etc.
1005 (if (looking-at "[a-zA-Z_]+")
1006 (let ((list tcl-type-alist)
1008 (setq word-stack (cons (current-word) word-stack))
1009 (while (and list (not result))
1010 (setq entry (car list))
1011 (setq list (cdr list))
1013 (while (and entry (<= index count))
1014 ;; Abort loop if string does not match word on
1016 (and (stringp (car entry))
1017 (not (string= (car entry)
1018 (nth index word-stack)))
1020 (setq entry (cdr entry))
1021 (setq index (1+ index)))
1022 (and (> index count)
1023 (not (stringp (car entry)))
1024 (setq result (car entry)))
1026 (setq word-stack (cons nil word-stack))))
1028 (setq count (1+ count)))
1029 (and tcl-explain-indentation
1030 (message "Indentation type %s" result))
1033 (defun calculate-tcl-indent (&optional parse-start)
1034 "Return appropriate indentation for current line as Tcl code.
1035 In usual case returns an integer: the column to indent to.
1036 Returns nil if line starts inside a string, t if in a comment."
1039 (let* ((indent-point (point))
1040 (case-fold-search nil)
1046 (= ?\\ (preceding-char)))))
1047 (continued-indent-value (if continued-line
1048 tcl-continued-indent-level
1054 (goto-char parse-start)
1055 (tcl-beginning-of-defun))
1056 (while (< (point) indent-point)
1057 (setq parse-start (point))
1058 (setq state (parse-partial-sexp (point) indent-point 0))
1059 (setq containing-sexp (car (cdr state))))
1060 (cond ((or (nth 3 state) (nth 4 state))
1061 ;; Inside comment or string. Return nil or t if should
1062 ;; not change this line
1064 ((null containing-sexp)
1065 ;; Line is at top level.
1066 continued-indent-value)
1068 ;; Set expr-p if we are looking at the expression part of
1069 ;; an "if", "expr", etc statement. Set commands-p if we
1070 ;; are looking at the body part of an if, while, etc
1071 ;; statement. FIXME Should check for "for" loops here.
1072 (goto-char containing-sexp)
1073 (let* ((sexpr-type (tcl-figure-type))
1074 (expr-p (eq sexpr-type 'tcl-expr))
1075 (commands-p (eq sexpr-type 'tcl-commands))
1076 (expr-start (point)))
1077 ;; Find the first statement in the block and indent
1078 ;; like it. The first statement in the block might be
1079 ;; on the same line, so what we do is skip all
1080 ;; "virtually blank" lines, looking for a non-blank
1081 ;; one. A line is virtually blank if it only contains
1082 ;; a comment and whitespace. FIXME continued comments
1083 ;; aren't supported. They are a wart on Tcl anyway.
1084 ;; We do it this funky way because we want to know if
1085 ;; we've found a statement on some line _after_ the
1086 ;; line holding the sexp opener.
1087 (goto-char containing-sexp)
1089 (if (and (< (point) indent-point)
1090 (looking-at "[ \t]*\\(#.*\\)?$"))
1093 (while (and (< (point) indent-point)
1094 (looking-at "[ \t]*\\(#.*\\)?$"))
1095 (setq found-next-line t)
1097 (if (or continued-line
1098 (/= (char-after containing-sexp) ?{)
1101 ;; Line is continuation line, or the sexp opener
1102 ;; is not a curly brace, or we are are looking at
1103 ;; an `expr' expression (which must be split
1104 ;; specially). So indentation is column of first
1105 ;; good spot after sexp opener (with some added
1106 ;; in the continued-line case). If there is no
1107 ;; nonempty line before the indentation point, we
1108 ;; use the column of the character after the sexp
1110 (if (>= (point) indent-point)
1112 (goto-char containing-sexp)
1114 (skip-chars-forward " \t"))
1115 (+ (current-column) continued-indent-value))
1116 ;; After a curly brace, and not a continuation line.
1117 ;; So take indentation from first good line after
1118 ;; start of block, unless that line is on the same
1119 ;; line as the opening brace. In this case use the
1120 ;; indentation of the opening brace's line, plus
1121 ;; another indent step. If we are in the body part
1122 ;; of an "if" or "while" then the indentation is
1123 ;; taken from the line holding the start of the
1125 (if (and (< (point) indent-point)
1127 (current-indentation)
1129 (goto-char expr-start)
1130 (goto-char containing-sexp))
1131 (+ (current-indentation) tcl-indent-level)))))))))
1135 (defun indent-tcl-exp ()
1136 "Indent each line of the Tcl grouping following point."
1138 (let ((indent-stack (list nil))
1139 (contain-stack (list (point)))
1140 (case-fold-search nil)
1141 outer-loop-done inner-loop-done state ostate
1142 this-indent last-sexp continued-line
1148 (setq outer-loop-done nil)
1149 (while (and (not (eobp)) (not outer-loop-done))
1150 (setq last-depth next-depth)
1151 ;; Compute how depth changes over this line
1152 ;; plus enough other lines to get to one that
1153 ;; does not end inside a comment or string.
1154 ;; Meanwhile, do appropriate indentation on comment lines.
1155 (setq inner-loop-done nil)
1156 (while (and (not inner-loop-done)
1157 (not (and (eobp) (setq outer-loop-done t))))
1159 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
1161 (setq next-depth (car state))
1162 (if (and (car (cdr (cdr state)))
1163 (>= (car (cdr (cdr state))) 0))
1164 (setq last-sexp (car (cdr (cdr state)))))
1165 (if (or (nth 4 ostate))
1167 (if (or (nth 3 state))
1169 (setq inner-loop-done t)))
1170 (if (<= next-depth 0)
1171 (setq outer-loop-done t))
1174 ;; If this line had ..))) (((.. in it, pop out of the levels
1175 ;; that ended anywhere in this line, even if the final depth
1176 ;; doesn't indicate that they ended.
1177 (while (> last-depth (nth 6 state))
1178 (setq indent-stack (cdr indent-stack)
1179 contain-stack (cdr contain-stack)
1180 last-depth (1- last-depth)))
1181 (if (/= last-depth next-depth)
1182 (setq last-sexp nil))
1183 ;; Add levels for any parens that were started in this line.
1184 (while (< last-depth next-depth)
1185 (setq indent-stack (cons nil indent-stack)
1186 contain-stack (cons nil contain-stack)
1187 last-depth (1+ last-depth)))
1188 (if (null (car contain-stack))
1189 (setcar contain-stack
1190 (or (car (cdr state))
1195 (setq continued-line
1198 (= (preceding-char) ?\\)))
1199 (skip-chars-forward " \t")
1202 (if (and (car indent-stack)
1203 (>= (car indent-stack) 0))
1204 ;; Line is on an existing nesting level.
1205 (setq this-indent (car indent-stack))
1206 ;; Just started a new nesting level.
1207 ;; Compute the standard indent for this level.
1208 (let ((val (calculate-tcl-indent
1209 (if (car indent-stack)
1210 (- (car indent-stack))))))
1211 (setcar indent-stack
1212 (setq this-indent val))
1213 (setq continued-line nil)))
1214 (cond ((not (numberp this-indent)))
1215 ((= (following-char) ?})
1216 (setq this-indent (- this-indent tcl-indent-level)))
1217 ((= (following-char) ?\])
1218 (setq this-indent (- this-indent 1))))
1219 ;; Put chosen indentation into effect.
1220 (or (null this-indent)
1223 (+ this-indent tcl-indent-level)
1226 (delete-region (point) (progn (beginning-of-line) (point)))
1229 (+ this-indent tcl-indent-level)
1230 this-indent)))))))))
1236 ;; Interfaces to other packages.
1239 (defun tcl-imenu-create-index-function ()
1240 "Generate alist of indices for imenu."
1241 (let ((re (concat tcl-proc-regexp "\\([^ \t\n{]+\\)"))
1243 (imenu-progress-message 0)
1244 (goto-char (point-min))
1245 (while (re-search-forward re nil t)
1246 (imenu-progress-message nil)
1247 ;; Position on start of proc name, not beginning of line.
1249 (cons (buffer-substring (match-beginning 2) (match-end 2))
1250 (match-beginning 2))
1252 (imenu-progress-message 100)
1255 ;; FIXME Definition of function is very ad-hoc. Should use
1256 ;; tcl-beginning-of-defun. Also has incestuous knowledge about the
1257 ;; format of tcl-proc-regexp.
1258 (defun add-log-tcl-defun ()
1259 "Return name of Tcl function point is in, or nil."
1261 (if (re-search-backward
1262 (concat tcl-proc-regexp "\\([^ \t\n{]+\\)") nil t)
1263 (buffer-substring (match-beginning 2)
1269 ;; Helper functions for inferior Tcl mode.
1272 ;; This exists to let us delete the prompt when commands are sent
1273 ;; directly to the inferior Tcl. See gud.el for an explanation of how
1274 ;; it all works (I took it from there). This stuff doesn't really
1275 ;; work as well as I'd like it to. But I don't believe there is
1276 ;; anything useful that can be done.
1277 (defvar inferior-tcl-delete-prompt-marker nil)
1279 (defun tcl-filter (proc string)
1280 (let ((inhibit-quit t))
1282 (set-buffer (process-buffer proc))
1283 (goto-char (process-mark proc))
1284 ;; Delete prompt if requested.
1285 (if (marker-buffer inferior-tcl-delete-prompt-marker)
1287 (delete-region (point) inferior-tcl-delete-prompt-marker)
1288 (set-marker inferior-tcl-delete-prompt-marker nil)))))
1289 (comint-output-filter proc string))
1291 (defun tcl-send-string (proc string)
1293 (set-buffer (process-buffer proc))
1294 (goto-char (process-mark proc))
1296 (if (looking-at comint-prompt-regexp)
1297 (set-marker inferior-tcl-delete-prompt-marker (point))))
1298 (comint-send-string proc string))
1300 (defun tcl-send-region (proc start end)
1302 (set-buffer (process-buffer proc))
1303 (goto-char (process-mark proc))
1305 (if (looking-at comint-prompt-regexp)
1306 (set-marker inferior-tcl-delete-prompt-marker (point))))
1307 (comint-send-region proc start end))
1309 (defun switch-to-tcl (eob-p)
1310 "Switch to inferior Tcl process buffer.
1311 With argument, positions cursor at end of buffer."
1313 (if (get-buffer inferior-tcl-buffer)
1314 (pop-to-buffer inferior-tcl-buffer)
1315 (error "No current inferior Tcl buffer"))
1318 (goto-char (point-max)))))
1320 (defun inferior-tcl-proc ()
1321 "Return current inferior Tcl process.
1322 See variable `inferior-tcl-buffer'."
1323 (let ((proc (get-buffer-process (if (eq major-mode 'inferior-tcl-mode)
1325 inferior-tcl-buffer))))
1327 (error "No Tcl process; see variable `inferior-tcl-buffer'"))))
1329 (defun tcl-eval-region (start end &optional and-go)
1330 "Send the current region to the inferior Tcl process.
1331 Prefix argument means switch to the Tcl buffer afterwards."
1332 (interactive "r\nP")
1333 (let ((proc (inferior-tcl-proc)))
1334 (tcl-send-region proc start end)
1335 (tcl-send-string proc "\n")
1336 (if and-go (switch-to-tcl t))))
1338 (defun tcl-eval-defun (&optional and-go)
1339 "Send the current defun to the inferior Tcl process.
1340 Prefix argument means switch to the Tcl buffer afterwards."
1344 (let ((end (point)))
1345 (tcl-beginning-of-defun)
1346 (tcl-eval-region (point) end)))
1347 (if and-go (switch-to-tcl t)))
1352 ;; Inferior Tcl mode itself.
1355 (defun inferior-tcl-mode ()
1356 "Major mode for interacting with Tcl interpreter.
1358 A Tcl process can be started with M-x inferior-tcl.
1360 Entry to this mode runs the hooks comint-mode-hook and
1361 inferior-tcl-mode-hook, in that order.
1363 You can send text to the inferior Tcl process from other buffers
1364 containing Tcl source.
1366 Variables controlling Inferior Tcl mode:
1368 Name of program to run.
1369 tcl-command-switches
1370 Command line arguments to `tcl-application'.
1373 inferior-tcl-source-command
1374 Command to use to read Tcl file in running application.
1376 The current inferior Tcl process buffer. See variable
1377 documentation for details on multiple-process support.
1379 The following commands are available:
1380 \\{inferior-tcl-mode-map}"
1383 (setq comint-prompt-regexp (or tcl-prompt-regexp
1385 (regexp-quote tcl-application)
1387 (setq major-mode 'inferior-tcl-mode)
1388 (setq mode-name "Inferior Tcl")
1389 (setq mode-line-process '(": %s"))
1390 (use-local-map inferior-tcl-mode-map)
1391 (setq local-abbrev-table tcl-mode-abbrev-table)
1392 (set-syntax-table tcl-mode-syntax-table)
1393 (if tcl-using-emacs-19
1395 (make-local-variable 'defun-prompt-regexp)
1396 (setq defun-prompt-regexp tcl-omit-ws-regexp)))
1397 (make-local-variable 'inferior-tcl-delete-prompt-marker)
1398 (setq inferior-tcl-delete-prompt-marker (make-marker))
1399 (set-process-filter (get-buffer-process (current-buffer)) 'tcl-filter)
1400 (run-hooks 'inferior-tcl-mode-hook))
1402 (defun inferior-tcl (cmd)
1403 "Run inferior Tcl process.
1404 Prefix arg means enter program name interactively.
1405 See documentation for function `inferior-tcl-mode' for more information."
1407 (list (if current-prefix-arg
1408 (read-string "Run Tcl: " tcl-application)
1410 (if (not (comint-check-proc "*inferior-tcl*"))
1412 (set-buffer (apply (function make-comint) "inferior-tcl" cmd nil
1413 tcl-command-switches))
1414 (inferior-tcl-mode)))
1415 (make-local-variable 'tcl-application)
1416 (setq tcl-application cmd)
1417 (setq inferior-tcl-buffer "*inferior-tcl*")
1418 (switch-to-buffer "*inferior-tcl*"))
1420 (and (fboundp 'defalias)
1421 (defalias 'run-tcl 'inferior-tcl))
1426 ;; Auto-fill support.
1429 (defun tcl-real-command-p ()
1430 "Return nil if point is not at the beginning of a command.
1431 A command is the first word on an otherwise empty line, or the
1432 first word following a semicolon, opening brace, or opening bracket."
1434 (skip-chars-backward " \t")
1439 ;; Note -- continued comments are not supported here. I
1440 ;; consider those to be a wart on the language.
1441 (not (eq ?\\ (preceding-char))))
1443 (memq (preceding-char) '(?\; ?{ ?\[))))))
1445 ;; FIXME doesn't actually return t. See last case.
1446 (defun tcl-real-comment-p ()
1447 "Return t if point is just after the `#' beginning a real comment.
1448 Does not check to see if previous char is actually `#'.
1449 A real comment is either at the beginning of the buffer,
1450 preceeded only by whitespace on the line, or has a preceeding
1451 semicolon, opening brace, or opening bracket on the same line."
1454 (tcl-real-command-p)))
1456 (defun tcl-hairy-scan-for-comment (state end always-stop)
1457 "Determine if point is in a comment.
1458 Returns a list of the form `(FLAG . STATE)'. STATE can be used
1459 as input to future invocations. FLAG is nil if not in comment,
1460 t otherwise. If in comment, leaves point at beginning of comment.
1461 Only works in Emacs 19. See also `tcl-simple-scan-for-comment', a
1462 simpler version that is often right, and works in Emacs 18."
1463 (let ((bol (save-excursion
1469 (while (and (not last-cstart) (< (point) end))
1470 (setq real-comment nil) ;In case we've looped around and it is
1472 (setq state (parse-partial-sexp (point) end nil nil state t))
1475 ;; If ALWAYS-STOP is set, stop even if we don't have a
1476 ;; real comment, or if the comment isn't on the same line
1478 (if always-stop (setq last-cstart (point)))
1479 ;; If we have a real comment, then set the comment
1480 ;; starting point if we are on the same line as the ending
1482 (setq real-comment (tcl-real-comment-p))
1485 (and (> (point) bol) (setq last-cstart (point)))
1486 ;; NOTE Emacs 19 has a misfeature whereby calling
1487 ;; parse-partial-sexp with COMMENTSTOP set and with
1488 ;; an initial list that says point is in a comment
1489 ;; will cause an immediate return. So we must skip
1490 ;; over the comment ourselves.
1491 (beginning-of-line 2)))
1492 ;; Frob the state to make it look like we aren't in a
1494 (setcar (nthcdr 4 state) nil))))
1496 (goto-char last-cstart))
1497 (cons real-comment state)))
1499 (defun tcl-hairy-in-comment ()
1500 "Return t if point is in a comment, and leave point at beginning
1502 (let ((save (point)))
1503 (tcl-beginning-of-defun)
1504 (car (tcl-hairy-scan-for-comment nil save nil))))
1506 (defun tcl-simple-in-comment ()
1507 "Return t if point is in comment, and leave point at beginning
1508 of comment. This is faster that `tcl-hairy-in-comment', but is
1509 correct less often."
1510 (let ((save (point))
1513 (while (and (< (point) save) (not comment))
1514 (search-forward "#" save 'move)
1515 (setq comment (tcl-real-comment-p)))
1518 (defun tcl-in-comment ()
1519 "Return t if point is in comment, and leave point at beginning
1521 (if (and tcl-pps-has-arg-6
1522 tcl-use-hairy-comment-detector)
1523 (tcl-hairy-in-comment)
1524 (tcl-simple-in-comment)))
1526 (defun tcl-do-auto-fill ()
1527 "Auto-fill function for Tcl mode. Only auto-fills in a comment."
1531 (setq in-comment (tcl-in-comment))
1533 (setq col (1- (current-column)))))
1538 (back-to-indentation)
1539 (delete-region (point) (save-excursion
1542 (indent-to-column col))))))
1547 ;; Help-related code.
1550 (defvar tcl-help-saved-dirs nil
1551 "Saved help directories.
1552 If `tcl-help-directory-list' changes, this allows `tcl-help-on-word'
1553 to update the alist.")
1555 (defvar tcl-help-alist nil
1556 "Alist with command names as keys and filenames as values.")
1558 (defun tcl-help-snarf-commands (dirlist)
1559 "Build alist of commands and filenames."
1561 (let ((files (directory-files (car dirlist) t)))
1563 (if (and (file-directory-p (car files))
1565 (let ((fpart (file-name-nondirectory (car files))))
1566 (or (equal fpart ".")
1567 (equal fpart "..")))))
1568 (let ((matches (directory-files (car files) t)))
1570 (or (file-directory-p (car matches))
1571 (setq tcl-help-alist
1573 (cons (file-name-nondirectory (car matches))
1576 (setq matches (cdr matches)))))
1577 (setq files (cdr files))))
1578 (setq dirlist (cdr dirlist))))
1580 (defun tcl-reread-help-files ()
1581 "Set up to re-read files, and then do it."
1583 (message "Building Tcl help file index...")
1584 (setq tcl-help-saved-dirs tcl-help-directory-list)
1585 (setq tcl-help-alist nil)
1586 (tcl-help-snarf-commands tcl-help-directory-list)
1587 (message "Building Tcl help file index...done"))
1589 (defun tcl-current-word (flag)
1590 "Return current command word, or nil.
1591 If FLAG is nil, just uses `current-word'.
1592 Otherwise scans backward for most likely Tcl command word."
1593 (if (and flag (eq major-mode 'tcl-mode))
1596 ;; Look backward for first word actually in alist.
1599 (while (and (not (bobp))
1600 (not (tcl-real-command-p)))
1602 (if (assoc (current-word) tcl-help-alist)
1607 (defun tcl-help-on-word (command &optional arg)
1608 "Get help on Tcl command. Default is word at point.
1609 Prefix argument means invert sense of `tcl-use-smart-word-finder'."
1613 (if (not (equal tcl-help-directory-list tcl-help-saved-dirs))
1614 (tcl-reread-help-files))
1615 (let ((word (tcl-current-word
1616 (if current-prefix-arg
1617 (not tcl-use-smart-word-finder)
1618 tcl-use-smart-word-finder))))
1620 (if (or (null word) (string= word ""))
1621 "Help on Tcl command: "
1622 (format "Help on Tcl command (default %s): " word))
1623 tcl-help-alist nil t)))
1624 current-prefix-arg))
1625 (if (not (equal tcl-help-directory-list tcl-help-saved-dirs))
1626 (tcl-reread-help-files))
1627 (if (string= command "")
1628 (setq command (tcl-current-word
1630 (not tcl-use-smart-word-finder)
1631 tcl-use-smart-word-finder))))
1632 (let* ((help (get-buffer-create "*Tcl help*"))
1633 (cell (assoc command tcl-help-alist))
1634 (file (and cell (cdr cell))))
1636 (delete-region (point-min) (point-max))
1639 (insert "*** " command "\n\n")
1640 (insert-file-contents file))
1641 (if (string= command "")
1642 (insert "Magical Pig!")
1643 (insert "Tcl command " command " not in help\n")))
1644 (set-buffer-modified-p nil)
1645 (goto-char (point-min))
1646 (display-buffer help)))
1651 ;; Other interactive stuff.
1654 (defvar tcl-previous-dir/file nil
1655 "Record last directory and file used in loading.
1656 This holds a cons cell of the form `(DIRECTORY . FILE)'
1657 describing the last `tcl-load-file' command.")
1659 (defun tcl-load-file (file &optional and-go)
1660 "Load a Tcl file into the inferior Tcl process.
1661 Prefix argument means switch to the Tcl buffer afterwards."
1664 ;; car because comint-get-source returns a list holding the
1666 (car (comint-get-source "Load Tcl file: " tcl-previous-dir/file
1668 current-prefix-arg))
1669 (comint-check-source file)
1670 (setq tcl-previous-dir/file (cons (file-name-directory file)
1671 (file-name-nondirectory file)))
1672 (tcl-send-string (inferior-tcl-proc)
1673 (format inferior-tcl-source-command (tcl-quote file)))
1674 (if and-go (switch-to-tcl t)))
1676 ;; Maybe this should work just like tcl-load-file. But I think what
1677 ;; I've implemented will turn out to be more useful.
1678 (defun tcl-restart-with-file (file &optional and-go)
1679 "Restart inferior Tcl with file.
1680 If an inferior Tcl process exists, it is killed first.
1681 Prefix argument means switch to the Tcl buffer afterwards."
1684 (car (comint-get-source "Restart with Tcl file: "
1686 (eq major-mode 'tcl-mode)
1688 tcl-previous-dir/file)
1690 current-prefix-arg))
1691 (let* ((buf (if (eq major-mode 'inferior-tcl-mode)
1693 inferior-tcl-buffer))
1694 (proc (and buf (get-process buf))))
1696 ((not (and buf (get-buffer buf)))
1697 ;; I think this will be ok.
1698 (inferior-tcl tcl-application)
1699 (tcl-load-file file and-go))
1701 (not (comint-check-proc buf))
1703 "A Tcl process is running, are you sure you want to reset it? "))
1705 (comint-check-source file)
1706 (setq tcl-previous-dir/file (cons (file-name-directory file)
1707 (file-name-nondirectory file)))
1708 (comint-exec (get-buffer-create buf)
1712 tcl-application file tcl-command-switches)
1713 (if and-go (switch-to-tcl t)))))))
1715 ;; FIXME I imagine you can do this under Emacs 18. I just don't know
1717 (defun tcl-auto-fill-mode (&optional arg)
1718 "Like `auto-fill-mode', but controls filling of Tcl comments."
1720 (and (not tcl-using-emacs-19)
1721 (error "You must use Emacs 19 to get this feature."))
1722 ;; Following code taken from "auto-fill-mode" (simple.el).
1724 (setq auto-fill-function
1726 (not auto-fill-function)
1727 (> (prefix-numeric-value arg) 0))
1730 ;; Update mode line. FIXME I'd use force-mode-line-update, but I
1731 ;; don't know if it exists in v18.
1732 (set-buffer-modified-p (buffer-modified-p))))
1734 (defun tcl-electric-hash (&optional count)
1735 "Insert a `#' and quote if it does not start a real comment.
1736 Prefix arg is number of `#'s to insert.
1737 See variable `tcl-electric-hash-style' for description of quoting
1740 (or count (setq count 1))
1743 (if (eq tcl-electric-hash-style 'smart)
1744 (if (> count 3) ; FIXME what is "smart"?
1747 tcl-electric-hash-style))
1753 (setq comment (tcl-in-comment)))
1755 (and tcl-explain-indentation (message "comment: %s" comment))
1760 ((eq type 'backslash)
1761 ;; The following will set count to 0, so the
1762 ;; insert-char can still be run.
1766 (setq count (1- count)))))
1768 (insert-char ?# count))))
1770 (defun tcl-hashify-buffer ()
1771 "Quote all `#'s in current buffer that aren't Tcl comments."
1774 (goto-char (point-min))
1775 (if (and tcl-pps-has-arg-6 tcl-use-hairy-comment-detector)
1778 (while (< (point) (point-max))
1779 (setq result (tcl-hairy-scan-for-comment state (point-max) t))
1781 (beginning-of-line 2)
1783 (if (eq ?# (following-char))
1786 (setq state (cdr result))))
1787 (while (and (< (point) (point-max))
1788 (search-forward "#" nil 'move))
1789 (if (tcl-real-comment-p)
1790 (beginning-of-line 2)
1791 ;; There's really no good way for the simple converter to
1792 ;; work. So we just quote # if it isn't already quoted.
1793 ;; Bogus, but it works.
1795 (if (not (eq ?\\ (preceding-char)))
1799 (defun tcl-indent-for-comment ()
1800 "Indent this line's comment to comment column, or insert an empty comment.
1801 Is smart about syntax of Tcl comments.
1802 Parts of this were taken from indent-for-comment (simple.el)."
1805 (or (tcl-in-comment)
1807 ;; Not in a comment, so we have to insert one. Create an
1808 ;; empty comment (since there isn't one on this line). If
1809 ;; line is not blank, make sure we insert a ";" first.
1810 (skip-chars-backward " \t")
1811 (let ((eolpoint (point)))
1813 (if (/= (point) eolpoint)
1815 (goto-char eolpoint)
1817 (if (tcl-real-command-p) "" ";")
1819 (backward-char))))))
1820 ;; Point is just after the "#" starting a comment. Move it as
1822 (let* ((indent (if comment-indent-hook
1823 (funcall comment-indent-hook)
1824 (funcall comment-indent-function)))
1828 (if (/= begpos indent)
1830 (skip-chars-backward " \t" (save-excursion
1833 (delete-region (point) begpos)
1834 (indent-to indent)))
1835 (looking-at comment-start-skip) ; Always true.
1836 (goto-char (match-end 0))
1837 ;; I don't like the effect of the next two.
1838 ;;(skip-chars-backward " \t" (match-beginning 0))
1839 ;;(skip-chars-backward "^ \t" (match-beginning 0))
1842 ;; The following was inspired by the Tcl editing mode written by
1843 ;; Gregor Schmid <schmid@fb3-s7.math.TU-Berlin.DE>. His version also
1844 ;; attempts to snarf the command line options from the command line,
1845 ;; but I didn't think that would really be that helpful (doesn't seem
1846 ;; like it owould be right enough. His version also looks for the
1847 ;; "#!/bin/csh ... exec" hack, but that seemed even less useful.
1848 ;; FIXME should make sure that the application mentioned actually
1850 (defun tcl-guess-application ()
1851 "Attempt to guess Tcl application by looking at first line.
1852 The first line is assumed to look like \"#!.../program ...\"."
1854 (goto-char (point-min))
1855 (if (looking-at "#![^ \t]*/\\([^ \t/]+\\)\\([ \t]\\|$\\)")
1857 (make-local-variable 'tcl-application)
1858 (setq tcl-application (buffer-substring (match-beginning 1)
1861 ;; This only exists to put on the menubar. I couldn't figure out any
1862 ;; other way to do it. FIXME should take "number of #-marks"
1864 (defun tcl-uncomment-region (beg end)
1867 (comment-region beg end -1))
1872 ;; Lucid menu support.
1873 ;; Taken from schmid@fb3-s7.math.TU-Berlin.DE (Gregor Schmid),
1874 ;; who wrote a different Tcl mode.
1875 ;; We also have support for menus in FSF. We do this by
1876 ;; loading the Lucid menu emulation code.
1879 (defun tcl-popup-menu (e)
1881 (and tcl-using-emacs-19
1882 (not tcl-using-lemacs-19)
1883 (if tcl-using-emacs-19.23
1886 ;; * lmenu.el provides 'menubar, which is bogus.
1887 ;; * lmenu.el causes menubars to be turned on everywhere.
1889 ;; Both of these problems are fixed in Emacs 19.23. People
1890 ;; using an Emacs before that just suffer.
1891 (require 'menubar "lmenu"))) ;; This is annoying
1892 ;; IMHO popup-menu should be autoloaded in FSF Emacs. Oh well.
1893 (popup-menu tcl-lucid-menu))
1898 ;; Quoting and unquoting functions.
1901 ;; This quoting is sufficient to protect eg a filename from any sort
1902 ;; of expansion or splitting. Tcl quoting sure sucks.
1903 (defun tcl-quote (string)
1904 "Quote STRING according to Tcl rules."
1905 (mapconcat (function (lambda (char)
1906 (if (memq char '(?[ ?] ?{ ?} ?\\ ?\" ?$ ? ?\;))
1907 (concat "\\" (char-to-string char))
1908 (char-to-string char))))
1917 (and (fboundp 'eval-when-compile)
1919 (require 'reporter)))
1921 (defun tcl-submit-bug-report ()
1922 "Submit via mail a bug report on Tcl mode."
1926 (y-or-n-p "Do you really want to submit a bug report on Tcl mode? ")
1927 (reporter-submit-bug-report
1929 (concat "Tcl mode " tcl-version)
1931 tcl-continued-indent-level
1933 tcl-tab-always-indent
1934 tcl-use-hairy-comment-detector
1935 tcl-electric-has-style
1936 tcl-help-directory-list
1937 tcl-use-smart-word-finder
1939 tcl-command-switches
1941 inferior-tcl-source-command
1943 tcl-using-emacs-19.23
1949 tcl-font-lock-keywords
1950 tcl-pps-has-arg-6))))
1956 ;;; tcl.el ends here