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.11 $
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:12:44 $|$Revision: 1.11 $|~/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.11 1994/05/22 20:12:44 tromey
69 ; Fixed mark-defun for 19.23.
72 ; Revision 1.10 1994/05/22 20:02:03 tromey
74 ; Wrote bug-reporting code.
76 ; Revision 1.9 1994/05/22 05:26:51 tromey
79 ; Revision 1.8 1994/05/22 03:38:07 tromey
82 ; Revision 1.7 1994/05/03 01:23:42 tromey
83 ; *** empty log message ***
85 ; Revision 1.6 1994/04/23 16:23:36 tromey
86 ; Wrote tcl-indent-for-comment
89 ;; 18-Mar-1994 Tom Tromey Fourth beta release.
90 ;; Added {un,}comment-region to menu. Idea from
91 ;; Mike Scheidler <c23mts@kocrsv01.delcoelect.com>
92 ;; 17-Mar-1994 Tom Tromey
93 ;; Fixed tcl-restart-with-file. Bug fix attempt in
94 ;; tcl-internal-end-of-defun.
95 ;; 16-Mar-1994 Tom Tromey Third beta release
96 ;; Added support code for menu (from Tcl mode written by
97 ;; schmid@fb3-s7.math.TU-Berlin.DE (Gregor Schmid)).
98 ;; 12-Mar-1994 Tom Tromey
99 ;; Better documentation for inferior-tcl-buffer. Wrote
100 ;; tcl-restart-with-file. Wrote Lucid Emacs menu (but no
101 ;; code to install it).
102 ;; 12-Mar-1994 Tom Tromey
103 ;; Wrote tcl-guess-application. Another stab at making
104 ;; tcl-omit-ws-regexp work.
105 ;; 10-Mar-1994 Tom Tromey Second beta release
106 ;; Last Modified: Thu Mar 10 01:24:25 1994 (Tom Tromey)
107 ;; Wrote perl-mode style line indentation command.
108 ;; Wrote more documentation. Added tcl-continued-indent-level.
109 ;; Integrated help code.
110 ;; 8-Mar-1994 Tom Tromey
111 ;; Last Modified: Tue Mar 8 11:58:44 1994 (Tom Tromey)
113 ;; 6-Mar-1994 Tom Tromey
114 ;; Last Modified: Sun Mar 6 18:55:41 1994 (Tom Tromey)
115 ;; Updated auto-newline support.
116 ;; 6-Mar-1994 Tom Tromey Beta release
117 ;; Last Modified: Sat Mar 5 17:24:32 1994 (Tom Tromey)
118 ;; Wrote tcl-hashify-buffer. Other minor bug fixes.
119 ;; 5-Mar-1994 Tom Tromey
120 ;; Last Modified: Sat Mar 5 16:11:20 1994 (Tom Tromey)
121 ;; Wrote electric-hash code.
122 ;; 3-Mar-1994 Tom Tromey
123 ;; Last Modified: Thu Mar 3 02:53:40 1994 (Tom Tromey)
124 ;; Added code to handle auto-fill in comments.
125 ;; Added imenu support code.
127 ;; Better font-lock support.
128 ;; 28-Feb-1994 Tom Tromey
129 ;; Last Modified: Mon Feb 28 14:08:05 1994 (Tom Tromey)
130 ;; Made tcl-figure-type more easily configurable.
131 ;; 28-Feb-1994 Tom Tromey
132 ;; Last Modified: Mon Feb 28 01:02:58 1994 (Tom Tromey)
133 ;; Wrote inferior-tcl mode.
134 ;; 16-Feb-1994 Tom Tromey
135 ;; Last Modified: Wed Feb 16 17:05:19 1994 (Tom Tromey)
136 ;; Added support for font-lock-mode.
137 ;; 29-Oct-1993 Tom Tromey
138 ;; Last Modified: Sun Oct 24 17:39:14 1993 (Tom Tromey)
139 ;; Patches from Guido Bosch to make things work with Lucid Emacs.
140 ;; 22-Oct-1993 Tom Tromey
141 ;; Last Modified: Fri Oct 22 15:26:46 1993 (Tom Tromey)
142 ;; Made many characters have "_" syntax class; suggested by Guido
143 ;; Bosch <Guido.Bosch@loria.fr>. Note that this includes the "$"
144 ;; character, which might be a change you'd notice.
145 ;; 21-Oct-1993 Tom Tromey
146 ;; Last Modified: Thu Oct 21 20:28:40 1993 (Tom Tromey)
147 ;; More fixes for tcl-omit-ws-regexp.
148 ;; 20-Oct-1993 Tom Tromey
149 ;; Started keeping history. Fixed tcl-{beginning,end}-of-defun.
150 ;; Added some code to make things work with Emacs 18.
153 ;; Guido Bosch <Guido.Bosch@loria.fr>
154 ;; pgs1002@esc.cam.ac.uk (Dr P.G. Sjoerdsma)
155 ;; Mike Scheidler <c23mts@kocrsv01.delcoelect.com>
156 ;; Matt Newman <men@charney.colorado.edu>
157 ;; rwhitby@research.canon.oz.au (Rod Whitby)
158 ;; h9118101@hkuxa.hku.hk (Yip Chi Lap [Beta])
159 ;; Pertti Tapio Kasanen <ptk@delta.hut.fi>
160 ;; schmid@fb3-s7.math.TU-Berlin.DE (Gregor Schmid)
163 ;; * indent-region should skip blank lines. (It does in v19, so I'm
164 ;; not motivated to fix it here).
165 ;; * In Tcl "#" is not always a comment character. This can confuse
166 ;; tcl.el in certain circumstances. For now the only workaround is
167 ;; to enclose offending hash characters in quotes or precede it with
168 ;; a backslash. Note that using braces won't work -- quotes change
169 ;; the syntax class of characters between them, while braces do not.
170 ;; The electric-# mode helps alleviate this problem somewhat.
171 ;; * indent-tcl-exp is untested.
172 ;; * Doesn't work under Emacs 18 yet.
173 ;; * There's been a report that font-lock does strange things under
174 ;; Lucid Emacs 19.6. For instance in "proc foobar", the space
175 ;; before "foobar" is highlighted.
178 ;; * make add-log-tcl-defun smarter. should notice if we are in the
179 ;; middle of a defun, or between defuns. should notice if point is
180 ;; on first line of defun (or maybe even in comments before defun).
181 ;; * Allow continuation lines to be indented under the first argument
182 ;; of the preceeding line, like this:
185 ;; * There is a request that indentation work like this:
186 ;; button .fred -label Fred \
187 ;; -command {puts fred}
188 ;; * Should have tcl-complete-symbol that queries the inferior process.
189 ;; * Should have describe-symbol that works by sending the magic
190 ;; command to a tclX process.
191 ;; * Need C-x C-e binding (tcl-eval-last-exp).
192 ;; * Write indent-region function that is faster than indenting each
193 ;; line individually.
194 ;; * tcl-figure-type should stop at "beginning of line" (only ws
195 ;; before point, and no "\" on previous line). (see tcl-real-command-p).
196 ;; * overrides some comint keybindings; fix.
197 ;; * Trailing \ will eat blank lines. Should deal with this.
198 ;; (this would help catch some potential bugs).
199 ;; * Inferior should display in half the screen, not the whole screen.
200 ;; * Indentation should deal with "switch".
201 ;; * Consider writing code to find help files automatically (for
203 ;; * `#' shouldn't insert `\#' when point is in string.
211 ;; When compiling under GNU Emacs, load imenu during compilation. If
212 ;; you have 19.22 or earlier, comment this out, or get imenu.
213 (and (fboundp 'eval-when-compile)
215 (if (and tcl-using-emacs-19
216 (not tcl-using-lemacs-19))
220 (defconst tcl-version "$Revision: 1.11 $")
221 (defconst tcl-maintainer "Tom Tromey <tromey@busco.lanl.gov>")
227 (defvar tcl-indent-level 4
228 "*Indentation of Tcl statements with respect to containing block.")
230 (defvar tcl-continued-indent-level 4
231 "*Indentation of continuation line relative to first line of command.")
233 (defvar tcl-auto-newline nil
234 "*Non-nil means automatically newline before and after braces
235 inserted in Tcl code.")
237 (defvar tcl-tab-always-indent t
238 "*Control effect of TAB key.
239 If t (the default), always indent current line.
240 If nil and point is not in the indentation area at the beginning of
241 the line, a TAB is inserted.
242 Other values cause the first possible action from the following list
245 1. Move from beginning of line to correct indentation.
246 2. Delete an empty comment.
247 3. Move forward to start of comment, indenting if necessary.
248 4. Move forward to end of line, indenting if necessary.
249 5. Create an empty comment.
250 6. Move backward to start of comment, indenting if necessary.")
252 (defvar tcl-use-hairy-comment-detector t
253 "*If not `nil', the the more complicated, but slower, comment
254 detecting function is used. This variable is only used in GNU Emacs
255 19 (the fast function is always used elsewhere).")
257 (defvar tcl-electric-hash-style 'smart
258 "*Style of electric hash insertion to use.
259 Possible values are 'backslash, meaning that `\\' quoting should be
260 done; `quote, meaning that `\"' quoting should be done; 'smart,
261 meaning that the choice between 'backslash and 'quote should be
262 made depending on the number of hashes inserted; or nil, meaning that
263 no quoting should be done. Any other value for this variable is
264 taken to mean 'smart. The default is 'smart.")
266 (defvar tcl-help-directory-list nil
267 "*List of topmost directories containing TclX help files")
269 (defvar tcl-use-smart-word-finder t
270 "*If not nil, use a better way of finding the current word when
271 looking up help on a Tcl command.")
273 (defvar tcl-application "wish"
274 "*Name of Tcl application to run in inferior Tcl mode.")
276 (defvar tcl-command-switches nil
277 "*Switches to supply to `tcl-application'.")
279 (defvar tcl-prompt-regexp "^\\(% \\|\\)"
280 "*If not nil, a regexp that will match the prompt in the inferior process.
281 If nil, the prompt is the name of the application with \">\" appended.
283 The default is \"^\\(% \\|\\)\", which will match the default primary
284 and secondary prompts for tclsh and wish.")
286 (defvar inferior-tcl-source-command "source %s\n"
287 "*Format-string for building a Tcl command to load a file.
288 This format string should use `%s' to substitute a file name
289 and should result in a Tcl expression that will command the
290 inferior Tcl to load that file. The filename will be appropriately
294 ;; Keymaps, abbrevs, syntax tables.
297 (defvar tcl-mode-abbrev-table nil
298 "Abbrev table in use in Tcl-mode buffers.")
299 (if tcl-mode-abbrev-table
301 (define-abbrev-table 'tcl-mode-abbrev-table ()))
303 ;; I sure wish Emacs had a package that made it easy to extract this
304 ;; sort of information.
305 (defconst tcl-using-emacs-19 (string-match "19\\." emacs-version)
306 "Nil unless using Emacs 19 (Lucid or FSF).")
308 ;; FIXME this will break on Emacs 19.100.
309 (defconst tcl-using-emacs-19.23
310 (string-match "19\\.\\(2[3-9]\\|[3-9][0-9]\\)" emacs-version)
311 "Nil unless using Emacs 19.23 or later.")
313 (defconst tcl-using-lemacs-19 (string-match "Lucid" emacs-version)
314 "Nil unless using Lucid Emacs).")
316 (defvar tcl-mode-map ()
317 "Keymap used in Tcl mode.")
319 (defvar tcl-mode-syntax-table nil
320 "Syntax table in use in Tcl-mode buffers.")
321 (if tcl-mode-syntax-table
323 (setq tcl-mode-syntax-table (make-syntax-table))
324 (modify-syntax-entry ?% "_" tcl-mode-syntax-table)
325 (modify-syntax-entry ?@ "_" tcl-mode-syntax-table)
326 (modify-syntax-entry ?& "_" tcl-mode-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) ; FIXME use "'"?
334 (modify-syntax-entry ?/ "_" tcl-mode-syntax-table)
335 (modify-syntax-entry ?~ "_" tcl-mode-syntax-table)
336 (modify-syntax-entry ?< "_" tcl-mode-syntax-table)
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 ?\n "> " tcl-mode-syntax-table)
344 (modify-syntax-entry ?\f "> " tcl-mode-syntax-table)
345 (modify-syntax-entry ?# "< " tcl-mode-syntax-table))
347 (defvar inferior-tcl-mode-map nil
348 "Keymap used in Inferior Tcl mode.")
351 (defvar tcl-lucid-menu
353 ["Beginning of function" tcl-beginning-of-defun t]
354 ["End of function" tcl-end-of-defun t]
355 ["Mark function" tcl-mark-defun t]
356 ["Indent region" indent-region t]
357 ["Comment region" comment-region t]
358 ["Uncomment region" tcl-uncomment-region t]
360 ["Show Tcl process buffer" inferior-tcl t]
361 ["Send function to Tcl process" tcl-eval-defun t]
362 ["Send region to Tcl process" tcl-eval-region t]
363 ["Send file to Tcl process" tcl-load-file t]
364 ["Restart Tcl process with file" tcl-restart-with-file t]
366 ["Tcl help" tcl-help-on-word t])
367 "Lucid Emacs menu for Tcl mode.")
369 ;; GNU Emacs does menus via keymaps. Do it in a function in case we
370 ;; later decide to add it to inferior Tcl mode as well.
371 (defun tcl-add-fsf-menu (map)
372 (define-key map [menu-bar] (make-sparse-keymap))
373 ;; This fails in Emacs 19.22 and earlier.
375 (define-key map [menu-bar tcl]
376 (cons "Tcl" (make-lucid-menu-keymap "Tcl" (cdr tcl-lucid-menu)))))
378 (defun tcl-fill-mode-map ()
379 (define-key tcl-mode-map "{" 'tcl-electric-char)
380 (define-key tcl-mode-map "}" 'tcl-electric-brace)
381 (define-key tcl-mode-map "[" 'tcl-electric-char)
382 (define-key tcl-mode-map "]" 'tcl-electric-char)
383 (define-key tcl-mode-map ";" 'tcl-electric-char)
384 (define-key tcl-mode-map "#" 'tcl-electric-hash)
386 (define-key tcl-mode-map "\e\C-a" 'tcl-beginning-of-defun)
388 (define-key tcl-mode-map "\e\C-e" 'tcl-end-of-defun)
390 (define-key tcl-mode-map "\e\C-h" 'tcl-mark-defun)
391 (define-key tcl-mode-map "\e\C-q" 'indent-tcl-exp)
392 (define-key tcl-mode-map "\177" 'backward-delete-char-untabify)
393 (define-key tcl-mode-map "\t" 'tcl-indent-command)
394 (define-key tcl-mode-map "\M-;" 'tcl-indent-for-comment)
395 (define-key tcl-mode-map "\M-\C-x" 'tcl-eval-defun)
396 (and (fboundp 'comment-region)
397 (define-key tcl-mode-map "\C-c\C-c" 'comment-region))
398 (define-key tcl-mode-map "\C-c\C-d" 'tcl-help-on-word)
399 (define-key tcl-mode-map "\C-c\C-e" 'tcl-eval-defun)
400 (define-key tcl-mode-map "\C-c\C-l" 'tcl-load-file)
401 (define-key tcl-mode-map "\C-c\C-p" 'inferior-tcl)
402 (define-key tcl-mode-map "\C-c\C-r" 'tcl-eval-region)
403 (define-key tcl-mode-map "\C-c\C-z" 'switch-to-tcl)
406 (if tcl-using-emacs-19
407 (if tcl-using-lemacs-19
408 ;; In Lucid, button 3 seems to be the standard for this.
409 (define-key tcl-mode-map 'button3 'tcl-popup-menu)
410 ;; In FSF 19, there is no standard, so I use shift-button2.
411 (tcl-add-fsf-menu tcl-mode-map)
412 (define-key tcl-mode-map [S-down-mouse-2] 'tcl-popup-menu))))
414 (defun tcl-fill-inferior-map ()
415 (define-key inferior-tcl-mode-map "\e\C-a" 'tcl-beginning-of-defun)
416 (define-key inferior-tcl-mode-map "\e\C-e" 'tcl-end-of-defun)
417 (define-key inferior-tcl-mode-map "\177" 'backward-delete-char-untabify)
418 (define-key inferior-tcl-mode-map "\M-\C-x" 'tcl-eval-defun)
419 (define-key inferior-tcl-mode-map "\C-c\C-d" 'tcl-help-on-word)
420 (define-key inferior-tcl-mode-map "\C-c\C-e" 'tcl-eval-defun)
421 (define-key inferior-tcl-mode-map "\C-c\C-l" 'tcl-load-file)
422 (define-key inferior-tcl-mode-map "\C-c\C-p" 'inferior-tcl)
423 (define-key inferior-tcl-mode-map "\C-c\C-r" 'tcl-eval-region)
424 (define-key inferior-tcl-mode-map "\C-c\C-z" 'switch-to-tcl))
428 (setq tcl-mode-map (make-sparse-keymap))
431 (if inferior-tcl-mode-map
433 ;; FIXME Use keymap inheritance here? FIXME we override comint
434 ;; keybindings here. Maybe someone has a better set?
435 (setq inferior-tcl-mode-map (copy-keymap comint-mode-map))
436 (tcl-fill-inferior-map))
439 (defvar inferior-tcl-buffer nil
440 "*The current inferior-tcl process buffer.
442 MULTIPLE PROCESS SUPPORT
443 ===========================================================================
444 To run multiple Tcl processes, you start the first up with
445 \\[inferior-tcl]. It will be in a buffer named `*inferior-tcl*'.
446 Rename this buffer with \\[rename-buffer]. You may now start up a new
447 process with another \\[inferior-tcl]. It will be in a new buffer,
448 named `*inferior-tcl*'. You can switch between the different process
449 buffers with \\[switch-to-buffer].
451 Commands that send text from source buffers to Tcl processes -- like
452 `tcl-eval-defun' or `tcl-load-file' -- have to choose a process to
453 send to, when you have more than one Tcl process around. This is
454 determined by the global variable `inferior-tcl-buffer'. Suppose you
455 have three inferior Lisps running:
459 *inferior-tcl* inferior-tcl<3>
460 If you do a \\[tcl-eval-defun] command on some Lisp source code, what
461 process do you send it to?
463 - If you're in a process buffer (foo, bar, or *inferior-tcl*),
464 you send it to that process.
465 - If you're in some other buffer (e.g., a source file), you
466 send it to the process attached to buffer `inferior-tcl-buffer'.
467 This process selection is performed by function `inferior-tcl-proc'.
469 Whenever \\[inferior-tcl] fires up a new process, it resets
470 `inferior-tcl-buffer' to be the new process's buffer. If you only run
471 one process, this does the right thing. If you run multiple
472 processes, you can change `inferior-tcl-buffer' to another process
473 buffer with \\[set-variable].")
476 ;; Hooks and other customization.
479 (defvar tcl-mode-hook nil
480 "Hook run on entry to Tcl mode.
482 Several functions exist which are useful to run from your
483 `tcl-mode-hook' (see each function's documentation for more
486 tcl-guess-application
487 Guesses a default setting for `tcl-application' based on any
488 \"#!\" line at the top of the file.
490 Quotes all \"#\" characters that don't correspond to actual
491 Tcl comments. (Useful when editing code not originally created
494 Auto-filling of Tcl comments.
496 Emacs 19 users can add functions to the hook with `add-hook':
498 (add-hook 'tcl-mode-hook 'tcl-guess-application)
500 Emacs 18 users must use `setq':
502 (setq tcl-mode-hook (cons 'tcl-guess-application tcl-mode-hook))")
505 (defvar inferior-tcl-mode-hook nil
506 "Hook for customizing Inferior Tcl mode.")
508 (defvar tcl-proc-list
510 "List of commands whose first argument defines something.
511 This exists because some people (eg, me) use \"defvar\" et al.
512 Call `tcl-set-proc-regexp' and `tcl-set-font-lock-keywords'
513 after changing this list.")
515 (defvar tcl-proc-regexp nil
516 "Regexp to use when matching proc headers.")
518 (defvar tcl-typeword-list
520 "List of Tcl keywords denoting \"type\". Used only for highlighting.
521 Call `tcl-set-font-lock-keywords' after changing this list.")
523 ;; Generally I've picked control operators to be keywords.
524 (defvar tcl-keyword-list
525 '("if" "then" "else" "elseif" "for" "foreach" "break" "continue" "while"
526 "eval" "case" "in" "switch" "default" "exit" "error" "proc" "return"
527 "uplevel" "loop" "for_array_keys" "for_recursive_glob" "for_file")
528 "List of Tcl keywords. Used only for highlighting.
529 Default list includes some TclX keywords.
530 Call `tcl-set-font-lock-keywords' after changing this list.")
532 (defvar tcl-font-lock-keywords nil
533 "Keywords to highlight for Tcl. See variable `font-lock-keywords'.
534 This variable is generally set from `tcl-proc-regexp',
535 `tcl-typeword-list', and `tcl-keyword-list' by the function
536 `tcl-set-font-lock-keywords'.")
538 ;; FIXME need some way to recognize variables because array refs look
540 (defvar tcl-type-alist
543 ("catch" tcl-commands)
544 ("if" tcl-expr "then" tcl-commands)
545 ("elseif" tcl-expr "then" tcl-commands)
546 ("elseif" tcl-expr tcl-commands)
547 ("if" tcl-expr tcl-commands)
548 ("while" tcl-expr tcl-commands)
549 ("for" tcl-commands tcl-expr tcl-commands tcl-commands)
550 ("foreach" nil nil tcl-commands)
551 ("for_file" nil nil tcl-commands)
552 ("for_array_keys" nil nil tcl-commands)
553 ("for_recursive_glob" nil nil nil tcl-commands)
554 ;; Loop handling is not perfect, because the third argument can be
555 ;; either a command or an expr, and there is no real way to look
557 ("loop" nil tcl-expr tcl-expr tcl-commands)
558 ("loop" nil tcl-expr tcl-commands)
560 "Alist that controls indentation.
561 \(Actually, this really only controls what happens on continuation lines).
562 Each entry looks like `(KEYWORD TYPE ...)'.
563 Each type entry describes a sexp after the keyword, and can be one of:
564 * nil, meaning that this sexp has no particular type.
565 * tcl-expr, meaning that this sexp is an arithmetic expression.
566 * tcl-commands, meaning that this sexp holds Tcl commands.
567 * a string, which must exactly match the string at the corresponding
568 position for a match to be made.
570 For example, the entry for the \"loop\" command is:
572 (\"loop\" nil tcl-expr tcl-commands)
574 This means that the \"loop\" command has three arguments. The first
575 argument is ignored (for indentation purposes). The second argument
576 is a Tcl expression, and the last argument is Tcl commands.")
578 (defvar tcl-explain-indentation nil
579 "If not `nil', debugging message will be printed during indentation.")
584 ;; Work around differences between various versions of Emacs.
587 ;; We use this because Lemacs 19.9 has what we need.
588 (defconst tcl-pps-has-arg-6
589 (or tcl-using-emacs-19
590 (and tcl-using-lemacs-19
593 (parse-partial-sexp (point) (point) nil nil nil t)
596 "t if using an emacs which supports sixth (\"commentstop\") argument
597 to parse-partial-sexp.")
599 ;; Its pretty bogus to have to do this, but there is no easier way to
600 ;; say "match not syntax-1 and not syntax-2". Too bad you can't put
601 ;; \s in [...]. This sickness is used in Emacs 19 to match a defun
602 ;; starter. (It is used for this in v18 as well).
603 ;;(defconst tcl-omit-ws-regexp
605 ;; (mapconcat 'char-to-string "w_.()\"\\$'/" "\\|\\s")
607 ;; "Regular expression that matches everything except space, comment
608 ;;starter, and comment ender syntax codes.")
610 ;; FIXME? Instead of using the hairy regexp above, we just use a
612 ;;(defconst tcl-omit-ws-regexp "^[^] \t\n#}]\\S(*"
613 ;; "Regular expression used in locating function definitions.")
615 ;; Here's another stab. I think this one actually works. Now the
616 ;; problem seems to be that there is a bug in Emacs 19.22 where
617 ;; end-of-defun doesn't really use the brace matching the one that
618 ;; trails defun-prompt-regexp.
619 (defconst tcl-omit-ws-regexp "^[^ \t\n#}][^\n}]+}*[ \t]+")
621 (defun tcl-internal-beginning-of-defun (&optional arg)
622 "Move backward to next beginning-of-defun.
623 With argument, do this that many times.
624 Returns t unless search stops due to end of buffer."
626 (if (or (null arg) (= arg 0))
633 (re-search-backward tcl-omit-ws-regexp nil 'move 1))))
634 (while (and (looking-at "[]#}]")
636 (re-search-backward tcl-omit-ws-regexp nil 'move 1)))))
638 (not (null success))))
640 (defun tcl-internal-end-of-defun (&optional arg)
641 "Move forward to next end of defun.
642 An end of a defun is found by moving forward from the beginning of one."
644 (if (or (null arg) (= arg 0)) (setq arg 1))
645 (let ((start (point)))
646 ;; Was forward-char. I think this works a little better.
648 (tcl-beginning-of-defun)
650 (while (and (re-search-forward tcl-omit-ws-regexp nil 'move 1)
651 (progn (beginning-of-line) t)
653 (progn (forward-line) t)))
654 (let ((next-line (save-excursion
657 (while (< (point) next-line)
660 (if (> (point) start) (setq arg (1- arg))))))
662 ;; In Emacs 19, we can use begining-of-defun as long as we set up a
663 ;; certain regexp. In Emacs 18, we need our own function.
664 (fset 'tcl-beginning-of-defun
665 (if tcl-using-emacs-19
667 'tcl-internal-beginning-of-defun))
669 ;; Ditto end-of-defun.
670 (fset 'tcl-end-of-defun
671 (if tcl-using-emacs-19
673 'tcl-internal-end-of-defun))
675 ;; Internal mark-defun that is used for losing Emacsen.
676 (defun tcl-internal-mark-defun ()
677 "Put mark at end of Tcl function, point at beginning."
681 (if tcl-using-emacs-19
682 (push-mark (point) nil t)
684 (tcl-beginning-of-defun)
685 (backward-paragraph))
687 ;; In GNU Emacs 19.23 and later, mark-defun works as advertised. I
688 ;; don't know about Lucid Emacs, so for now it and Emacs 18 just lose.
689 (fset 'tcl-mark-defun
690 (if tcl-using-emacs-19.23
692 'tcl-internal-mark-defun))
697 ;; Some helper functions.
700 (defun tcl-set-proc-regexp ()
701 "Set `tcl-proc-regexp' from variable `tcl-proc-list'."
702 (setq tcl-proc-regexp (concat "^\\("
703 (mapconcat 'identity tcl-proc-list "\\|")
706 (defun tcl-set-font-lock-keywords ()
707 "Set `tcl-font-lock-keywords'.
708 Uses variables `tcl-proc-regexp' and `tcl-keyword-list'."
709 (setq tcl-font-lock-keywords
711 ;; Names of functions (and other "defining things").
712 (list (concat tcl-proc-regexp "\\([^ \t\n]+\\)")
713 2 'font-lock-function-name-face)
715 ;; Names of type-defining things.
716 (list (concat "\\(\\s-\\|^\\)\\("
717 ;; FIXME Use 'regexp-quote?
718 (mapconcat 'identity tcl-typeword-list "\\|")
720 2 'font-lock-type-face)
722 ;; Keywords. Only recognized if surrounded by whitespace.
723 ;; FIXME consider using "not word or symbol", not
725 (cons (concat "\\(\\s-\\|^\\)\\("
726 ;; FIXME Use regexp-quote?
727 (mapconcat 'identity tcl-keyword-list "\\|")
734 (tcl-set-proc-regexp))
736 (if tcl-font-lock-keywords
738 (tcl-set-font-lock-keywords))
747 "Major mode for editing Tcl code.
748 Expression and list commands understand all Tcl brackets.
749 Tab indents for Tcl code.
750 Paragraphs are separated by blank lines only.
751 Delete converts tabs to spaces as it moves back.
753 Variables controlling indentation style:
755 Indentation of Tcl statements within surrounding block.
756 tcl-continued-indent-level
757 Indentation of continuation line relative to first line of command.
759 Variables controlling user interaction with mode (see variable
760 documentation for details):
761 tcl-tab-always-indent
762 Controls action of TAB key.
764 Non-nil means automatically newline before and after braces, brackets,
765 and semicolons inserted in Tcl code.
766 tcl-electric-hash-style
767 Controls action of `#' key.
768 tcl-use-hairy-comment-detector
769 If t, use more complicated, but slower, comment detector.
770 This variable is only used in GNU Emacs 19.
772 Turning on Tcl mode calls the value of the variable `tcl-mode-hook'
773 with no args, if that value is non-nil. Read the documentation for
774 `tcl-mode-hook' to see what kinds of interesting hook functions
780 (kill-all-local-variables)
781 (use-local-map tcl-mode-map)
782 (setq major-mode 'tcl-mode)
783 (setq mode-name "Tcl")
784 (setq local-abbrev-table tcl-mode-abbrev-table)
785 (set-syntax-table tcl-mode-syntax-table)
787 (make-local-variable 'paragraph-start)
788 (setq paragraph-start (concat "^$\\|" page-delimiter))
789 (make-local-variable 'paragraph-separate)
790 (setq paragraph-separate paragraph-start)
791 (make-local-variable 'paragraph-ignore-fill-prefix)
792 (setq paragraph-ignore-fill-prefix t)
794 (make-local-variable 'indent-line-function)
795 (setq indent-line-function 'tcl-indent-line)
796 ;; Tcl doesn't require a final newline.
797 ;; (make-local-variable 'require-final-newline)
798 ;; (setq require-final-newline t)
800 (make-local-variable 'comment-start)
801 (setq comment-start "# ")
802 (make-local-variable 'comment-start-skip)
803 (setq comment-start-skip "#+ *")
804 (make-local-variable 'comment-column)
805 (setq comment-column 40)
806 (make-local-variable 'comment-end)
807 (setq comment-end "")
809 (make-local-variable 'font-lock-keywords)
810 (setq font-lock-keywords tcl-font-lock-keywords)
812 ;; The following only really makes sense under GNU Emacs 19.
813 (setq imenu-create-index-function 'tcl-imenu-create-index-function)
814 (make-local-variable 'parse-sexp-ignore-comments)
816 (if tcl-using-emacs-19
818 ;; This can only be set to t in Emacs 19 and Lucid Emacs.
819 ;; Emacs 18 and Epoch lose.
820 (setq parse-sexp-ignore-comments t)
821 ;; Lucid Emacs has defun-prompt-regexp, but I don't believe
822 ;; that it works for end-of-defun -- only for
823 ;; beginning-of-defun.
824 (make-local-variable 'defun-prompt-regexp)
825 (setq defun-prompt-regexp tcl-omit-ws-regexp)
826 ;; The following doesn't work in Lucid Emacs 19.6, but maybe
827 ;; it will appear in later versions.
828 (make-local-variable 'add-log-current-defun-function)
829 (setq add-log-current-defun-function 'add-log-tcl-defun))
830 (setq parse-sexp-ignore-comments nil))
832 ;; Put Tcl menu into menubar for Lucid Emacs. This happens
833 ;; automatically for GNU Emacs.
834 (if (and tcl-using-lemacs-19
836 (not (assoc "Tcl" current-menubar)))
838 (set-buffer-menubar (copy-sequence current-menubar))
839 (add-menu nil "Tcl" tcl-lucid-menu)))
841 (run-hooks 'tcl-mode-hook))
845 ;; This is used for braces, brackets, and semi (except for closing
846 ;; braces, which are handled specially).
847 (defun tcl-electric-char (arg)
848 "Insert character and correct line's indentation."
850 ;; Indent line first; this looks better if parens blink.
852 (self-insert-command arg)
853 (if (and tcl-auto-newline (= last-command-char ?\;))
858 ;; This is used for closing braces. If tcl-auto-newline is set, can
859 ;; insert a newline both before and after the brace, depending on
860 ;; context. FIXME should this be configurable? Does anyone use this?
861 (defun tcl-electric-brace (arg)
862 "Insert character and correct line's indentation."
864 ;; If auto-newlining and there is stuff on the same line, insert a
869 (skip-chars-backward " \t")
874 ;; In auto-newline case, must insert a newline after each
875 ;; brace. So an explicit loop is needed.
877 (insert last-command-char)
880 (setq arg (1- arg))))
881 (self-insert-command arg))
886 (defun tcl-indent-command (&optional arg)
887 "Indent current line as Tcl code, or in some cases insert a tab character.
888 If tcl-tab-always-indent is t (the default), always indent current line.
889 If tcl-tab-always-indent is nil and point is not in the indentation
890 area at the beginning of the line, a TAB is inserted.
891 Other values of tcl-tab-always-indent cause the first possible action
892 from the following list to take place:
894 1. Move from beginning of line to correct indentation.
895 2. Delete an empty comment.
896 3. Move forward to start of comment, indenting if necessary.
897 4. Move forward to end of line, indenting if necessary.
898 5. Create an empty comment.
899 6. Move backward to start of comment, indenting if necessary."
902 ((not tcl-tab-always-indent)
903 ;; Indent if in indentation area, otherwise insert TAB.
904 (if (<= (current-column) (current-indentation))
906 (self-insert-command arg)))
907 ((eq tcl-tab-always-indent t)
911 ;; "Perl-mode" style TAB command.
912 (let* ((ipoint (point))
916 (comment-p (tcl-in-comment)))
918 ((= ipoint (save-excursion
923 ;; If indenting didn't leave us in column 0, go to the
924 ;; indentation. Otherwise leave point at end of line. This
926 (if (= (point) (save-excursion
930 (back-to-indentation)))
931 ((and comment-p (looking-at "[ \t]*$"))
932 ;; Empty comment, so delete it. We also delete any ";"
933 ;; characters at the end of the line. I think this is
934 ;; friendlier, but I don't know how other people will feel.
936 (skip-chars-backward " \t;")
937 (delete-region (point) eolpoint))
938 ((and comment-p (< ipoint (point)))
939 ;; Before comment, so skip to it.
941 (indent-for-comment))
942 ((/= ipoint eolpoint)
943 ;; Go to end of line (since we're not there yet).
948 (tcl-indent-for-comment))
950 ;; Go to start of comment. We don't leave point where it is
951 ;; because we want to skip comment-start-skip.
953 (indent-for-comment)))))))
955 (defun tcl-indent-line ()
956 "Indent current line as Tcl code.
957 Return the amount the indentation changed by."
958 (let ((indent (calculate-tcl-indent nil))
960 (case-fold-search nil)
961 (pos (- (point-max) (point))))
964 (cond ((eq indent nil)
965 (setq indent (current-indentation)))
967 (skip-chars-forward " \t")
968 (if (listp indent) (setq indent (car indent)))
969 (cond ((= (following-char) ?})
970 (setq indent (- indent tcl-indent-level)))
971 ((= (following-char) ?\])
972 (setq indent (- indent 1))))))
973 (skip-chars-forward " \t")
974 (setq shift-amt (- indent (current-column)))
975 (if (zerop shift-amt)
976 (if (> (- (point-max) pos) (point))
977 (goto-char (- (point-max) pos)))
978 (delete-region beg (point))
980 ;; If initial point was within line's indentation,
981 ;; position after the indentation. Else stay at same point in text.
982 (if (> (- (point-max) pos) (point))
983 (goto-char (- (point-max) pos))))
986 (defun tcl-figure-type ()
987 "Determine type of sexp at point.
988 This is either 'tcl-expr, 'tcl-commands, or nil. Puts point at start
989 of sexp that indicates types.
991 See documentation for variable `tcl-type-alist' for more information."
995 (while (and (< count 5)
999 ;; FIXME should use "tcl-backward-sexp", which would skip
1000 ;; over entire variables, etc.
1002 (if (looking-at "[a-zA-Z_]+")
1003 (let ((list tcl-type-alist)
1005 (setq word-stack (cons (current-word) word-stack))
1006 (while (and list (not result))
1007 (setq entry (car list))
1008 (setq list (cdr list))
1010 (while (and entry (<= index count))
1011 ;; Abort loop if string does not match word on
1013 (and (stringp (car entry))
1014 (not (string= (car entry)
1015 (nth index word-stack)))
1017 (setq entry (cdr entry))
1018 (setq index (1+ index)))
1019 (and (> index count)
1020 (not (stringp (car entry)))
1021 (setq result (car entry)))
1023 (setq word-stack (cons nil word-stack))))
1025 (setq count (1+ count)))
1026 (and tcl-explain-indentation
1027 (message "Indentation type %s" result))
1030 (defun calculate-tcl-indent (&optional parse-start)
1031 "Return appropriate indentation for current line as Tcl code.
1032 In usual case returns an integer: the column to indent to.
1033 Returns nil if line starts inside a string, t if in a comment."
1036 (let* ((indent-point (point))
1037 (case-fold-search nil)
1043 (= ?\\ (preceding-char)))))
1044 (continued-indent-value (if continued-line
1045 tcl-continued-indent-level
1051 (goto-char parse-start)
1052 (tcl-beginning-of-defun))
1053 (while (< (point) indent-point)
1054 (setq parse-start (point))
1055 (setq state (parse-partial-sexp (point) indent-point 0))
1056 (setq containing-sexp (car (cdr state))))
1057 (cond ((or (nth 3 state) (nth 4 state))
1058 ;; Inside comment or string. Return nil or t if should
1059 ;; not change this line
1061 ((null containing-sexp)
1062 ;; Line is at top level.
1063 continued-indent-value)
1065 ;; Set expr-p if we are looking at the expression part of
1066 ;; an "if", "expr", etc statement. Set commands-p if we
1067 ;; are looking at the body part of an if, while, etc
1068 ;; statement. FIXME Should check for "for" loops here.
1069 (goto-char containing-sexp)
1070 (let* ((sexpr-type (tcl-figure-type))
1071 (expr-p (eq sexpr-type 'tcl-expr))
1072 (commands-p (eq sexpr-type 'tcl-commands))
1073 (expr-start (point)))
1074 ;; Find the first statement in the block and indent
1075 ;; like it. The first statement in the block might be
1076 ;; on the same line, so what we do is skip all
1077 ;; "virtually blank" lines, looking for a non-blank
1078 ;; one. A line is virtually blank if it only contains
1079 ;; a comment and whitespace. FIXME continued comments
1080 ;; aren't supported. They are a wart on Tcl anyway.
1081 ;; We do it this funky way because we want to know if
1082 ;; we've found a statement on some line _after_ the
1083 ;; line holding the sexp opener.
1084 (goto-char containing-sexp)
1086 (if (and (< (point) indent-point)
1087 (looking-at "[ \t]*\\(#.*\\)?$"))
1090 (while (and (< (point) indent-point)
1091 (looking-at "[ \t]*\\(#.*\\)?$"))
1092 (setq found-next-line t)
1094 (if (or continued-line
1095 (/= (char-after containing-sexp) ?{)
1098 ;; Line is continuation line, or the sexp opener
1099 ;; is not a curly brace, or we are are looking at
1100 ;; an `expr' expression (which must be split
1101 ;; specially). So indentation is column of first
1102 ;; good spot after sexp opener (with some added
1103 ;; in the continued-line case). If there is no
1104 ;; nonempty line before the indentation point, we
1105 ;; use the column of the character after the sexp
1107 (if (>= (point) indent-point)
1109 (goto-char containing-sexp)
1111 (skip-chars-forward " \t"))
1112 (+ (current-column) continued-indent-value))
1113 ;; After a curly brace, and not a continuation line.
1114 ;; So take indentation from first good line after
1115 ;; start of block, unless that line is on the same
1116 ;; line as the opening brace. In this case use the
1117 ;; indentation of the opening brace's line, plus
1118 ;; another indent step. If we are in the body part
1119 ;; of an "if" or "while" then the indentation is
1120 ;; taken from the line holding the start of the
1122 (if (and (< (point) indent-point)
1124 (current-indentation)
1126 (goto-char expr-start)
1127 (goto-char containing-sexp))
1128 (+ (current-indentation) tcl-indent-level)))))))))
1132 (defun indent-tcl-exp ()
1133 "Indent each line of the Tcl grouping following point."
1135 (let ((indent-stack (list nil))
1136 (contain-stack (list (point)))
1137 (case-fold-search nil)
1138 outer-loop-done inner-loop-done state ostate
1139 this-indent last-sexp continued-line
1145 (setq outer-loop-done nil)
1146 (while (and (not (eobp)) (not outer-loop-done))
1147 (setq last-depth next-depth)
1148 ;; Compute how depth changes over this line
1149 ;; plus enough other lines to get to one that
1150 ;; does not end inside a comment or string.
1151 ;; Meanwhile, do appropriate indentation on comment lines.
1152 (setq inner-loop-done nil)
1153 (while (and (not inner-loop-done)
1154 (not (and (eobp) (setq outer-loop-done t))))
1156 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
1158 (setq next-depth (car state))
1159 (if (and (car (cdr (cdr state)))
1160 (>= (car (cdr (cdr state))) 0))
1161 (setq last-sexp (car (cdr (cdr state)))))
1162 (if (or (nth 4 ostate))
1164 (if (or (nth 3 state))
1166 (setq inner-loop-done t)))
1167 (if (<= next-depth 0)
1168 (setq outer-loop-done t))
1171 ;; If this line had ..))) (((.. in it, pop out of the levels
1172 ;; that ended anywhere in this line, even if the final depth
1173 ;; doesn't indicate that they ended.
1174 (while (> last-depth (nth 6 state))
1175 (setq indent-stack (cdr indent-stack)
1176 contain-stack (cdr contain-stack)
1177 last-depth (1- last-depth)))
1178 (if (/= last-depth next-depth)
1179 (setq last-sexp nil))
1180 ;; Add levels for any parens that were started in this line.
1181 (while (< last-depth next-depth)
1182 (setq indent-stack (cons nil indent-stack)
1183 contain-stack (cons nil contain-stack)
1184 last-depth (1+ last-depth)))
1185 (if (null (car contain-stack))
1186 (setcar contain-stack
1187 (or (car (cdr state))
1192 (setq continued-line
1195 (= (preceding-char) ?\\)))
1196 (skip-chars-forward " \t")
1199 (if (and (car indent-stack)
1200 (>= (car indent-stack) 0))
1201 ;; Line is on an existing nesting level.
1202 (setq this-indent (car indent-stack))
1203 ;; Just started a new nesting level.
1204 ;; Compute the standard indent for this level.
1205 (let ((val (calculate-tcl-indent
1206 (if (car indent-stack)
1207 (- (car indent-stack))))))
1208 (setcar indent-stack
1209 (setq this-indent val))
1210 (setq continued-line nil)))
1211 (cond ((not (numberp this-indent)))
1212 ((= (following-char) ?})
1213 (setq this-indent (- this-indent tcl-indent-level)))
1214 ((= (following-char) ?\])
1215 (setq this-indent (- this-indent 1))))
1216 ;; Put chosen indentation into effect.
1217 (or (null this-indent)
1220 (+ this-indent tcl-indent-level)
1223 (delete-region (point) (progn (beginning-of-line) (point)))
1226 (+ this-indent tcl-indent-level)
1227 this-indent)))))))))
1233 ;; Interfaces to other packages.
1236 (defun tcl-imenu-create-index-function ()
1237 "Generate alist of indices for imenu."
1238 (let ((re (concat tcl-proc-regexp "\\([^ \t\n{]+\\)"))
1240 (imenu-progress-message 0)
1241 (goto-char (point-min))
1242 (while (re-search-forward re nil t)
1243 (imenu-progress-message nil)
1244 ;; Position on start of proc name, not beginning of line.
1246 (cons (buffer-substring (match-beginning 2) (match-end 2))
1247 (match-beginning 2))
1249 (imenu-progress-message 100)
1252 ;; FIXME Definition of function is very ad-hoc. Should use
1253 ;; tcl-beginning-of-defun. Also has incestuous knowledge about the
1254 ;; format of tcl-proc-regexp.
1255 (defun add-log-tcl-defun ()
1256 "Return name of Tcl function point is in, or nil."
1258 (if (re-search-backward
1259 (concat tcl-proc-regexp "\\([^ \t\n{]+\\)") nil t)
1260 (buffer-substring (match-beginning 2)
1266 ;; Helper functions for inferior Tcl mode.
1269 ;; This exists to let us delete the prompt when commands are sent
1270 ;; directly to the inferior Tcl. See gud.el for an explanation of how
1271 ;; it all works (I took it from there). This stuff doesn't really
1272 ;; work as well as I'd like it to. But I don't believe there is
1273 ;; anything useful that can be done.
1274 (defvar inferior-tcl-delete-prompt-marker nil)
1276 (defun tcl-filter (proc string)
1277 (let ((inhibit-quit t))
1279 (set-buffer (process-buffer proc))
1280 (goto-char (process-mark proc))
1281 ;; Delete prompt if requested.
1282 (if (marker-buffer inferior-tcl-delete-prompt-marker)
1284 (delete-region (point) inferior-tcl-delete-prompt-marker)
1285 (set-marker inferior-tcl-delete-prompt-marker nil)))))
1286 (comint-output-filter proc string))
1288 (defun tcl-send-string (proc string)
1290 (set-buffer (process-buffer proc))
1291 (goto-char (process-mark proc))
1293 (if (looking-at comint-prompt-regexp)
1294 (set-marker inferior-tcl-delete-prompt-marker (point))))
1295 (comint-send-string proc string))
1297 (defun tcl-send-region (proc start end)
1299 (set-buffer (process-buffer proc))
1300 (goto-char (process-mark proc))
1302 (if (looking-at comint-prompt-regexp)
1303 (set-marker inferior-tcl-delete-prompt-marker (point))))
1304 (comint-send-region proc start end))
1306 (defun switch-to-tcl (eob-p)
1307 "Switch to inferior Tcl process buffer.
1308 With argument, positions cursor at end of buffer."
1310 (if (get-buffer inferior-tcl-buffer)
1311 (pop-to-buffer inferior-tcl-buffer)
1312 (error "No current inferior Tcl buffer"))
1315 (goto-char (point-max)))))
1317 (defun inferior-tcl-proc ()
1318 "Return current inferior Tcl process.
1319 See variable `inferior-tcl-buffer'."
1320 (let ((proc (get-buffer-process (if (eq major-mode 'inferior-tcl-mode)
1322 inferior-tcl-buffer))))
1324 (error "No Tcl process; see variable `inferior-tcl-buffer'"))))
1326 (defun tcl-eval-region (start end &optional and-go)
1327 "Send the current region to the inferior Tcl process.
1328 Prefix argument means switch to the Tcl buffer afterwards."
1329 (interactive "r\nP")
1330 (let ((proc (inferior-tcl-proc)))
1331 (tcl-send-region proc start end)
1332 (tcl-send-string proc "\n")
1333 (if and-go (switch-to-tcl t))))
1335 (defun tcl-eval-defun (&optional and-go)
1336 "Send the current defun to the inferior Tcl process.
1337 Prefix argument means switch to the Tcl buffer afterwards."
1341 (let ((end (point)))
1342 (tcl-beginning-of-defun)
1343 (tcl-eval-region (point) end)))
1344 (if and-go (switch-to-tcl t)))
1349 ;; Inferior Tcl mode itself.
1352 (defun inferior-tcl-mode ()
1353 "Major mode for interacting with Tcl interpreter.
1355 A Tcl process can be started with M-x inferior-tcl.
1357 Entry to this mode runs the hooks comint-mode-hook and
1358 inferior-tcl-mode-hook, in that order.
1360 You can send text to the inferior Tcl process from other buffers
1361 containing Tcl source.
1363 Variables controlling Inferior Tcl mode:
1365 Name of program to run.
1366 tcl-command-switches
1367 Command line arguments to `tcl-application'.
1370 inferior-tcl-source-command
1371 Command to use to read Tcl file in running application.
1373 The current inferior Tcl process buffer. See variable
1374 documentation for details on multiple-process support.
1376 The following commands are available:
1377 \\{inferior-tcl-mode-map}"
1380 (setq comint-prompt-regexp (or tcl-prompt-regexp
1382 (regexp-quote tcl-application)
1384 (setq major-mode 'inferior-tcl-mode)
1385 (setq mode-name "Inferior Tcl")
1386 (setq mode-line-process '(": %s"))
1387 (use-local-map inferior-tcl-mode-map)
1388 (setq local-abbrev-table tcl-mode-abbrev-table)
1389 (set-syntax-table tcl-mode-syntax-table)
1390 (if tcl-using-emacs-19
1392 (make-local-variable 'defun-prompt-regexp)
1393 (setq defun-prompt-regexp tcl-omit-ws-regexp)))
1394 (make-local-variable 'inferior-tcl-delete-prompt-marker)
1395 (setq inferior-tcl-delete-prompt-marker (make-marker))
1396 (set-process-filter (get-buffer-process (current-buffer)) 'tcl-filter)
1397 (run-hooks 'inferior-tcl-mode-hook))
1399 (defun inferior-tcl (cmd)
1400 "Run inferior Tcl process.
1401 Prefix arg means enter program name interactively.
1402 See documentation for function `inferior-tcl-mode' for more information."
1404 (list (if current-prefix-arg
1405 (read-string "Run Tcl: " tcl-application)
1407 (if (not (comint-check-proc "*inferior-tcl*"))
1409 (set-buffer (apply (function make-comint) "inferior-tcl" cmd nil
1410 tcl-command-switches))
1411 (inferior-tcl-mode)))
1412 (make-local-variable 'tcl-application)
1413 (setq tcl-application cmd)
1414 (setq inferior-tcl-buffer "*inferior-tcl*")
1415 (switch-to-buffer "*inferior-tcl*"))
1417 (and (fboundp 'defalias)
1418 (defalias 'run-tcl 'inferior-tcl))
1423 ;; Auto-fill support.
1426 (defun tcl-real-command-p ()
1427 "Return nil if point is not at the beginning of a command.
1428 A command is the first word on an otherwise empty line, or the
1429 first word following a semicolon, opening brace, or opening bracket."
1431 (skip-chars-backward " \t")
1436 ;; Note -- continued comments are not supported here. I
1437 ;; consider those to be a wart on the language.
1438 (not (eq ?\\ (preceding-char))))
1440 (memq (preceding-char) '(?\; ?{ ?\[))))))
1442 ;; FIXME doesn't actually return t. See last case.
1443 (defun tcl-real-comment-p ()
1444 "Return t if point is just after the `#' beginning a real comment.
1445 Does not check to see if previous char is actually `#'.
1446 A real comment is either at the beginning of the buffer,
1447 preceeded only by whitespace on the line, or has a preceeding
1448 semicolon, opening brace, or opening bracket on the same line."
1451 (tcl-real-command-p)))
1453 (defun tcl-hairy-scan-for-comment (state end always-stop)
1454 "Determine if point is in a comment.
1455 Returns a list of the form `(FLAG . STATE)'. STATE can be used
1456 as input to future invocations. FLAG is nil if not in comment,
1457 t otherwise. If in comment, leaves point at beginning of comment.
1458 Only works in Emacs 19. See also `tcl-simple-scan-for-comment', a
1459 simpler version that is often right, and works in Emacs 18."
1460 (let ((bol (save-excursion
1466 (while (and (not last-cstart) (< (point) end))
1467 (setq real-comment nil) ;In case we've looped around and it is
1469 (setq state (parse-partial-sexp (point) end nil nil state t))
1472 ;; If ALWAYS-STOP is set, stop even if we don't have a
1473 ;; real comment, or if the comment isn't on the same line
1475 (if always-stop (setq last-cstart (point)))
1476 ;; If we have a real comment, then set the comment
1477 ;; starting point if we are on the same line as the ending
1479 (setq real-comment (tcl-real-comment-p))
1482 (and (> (point) bol) (setq last-cstart (point)))
1483 ;; NOTE Emacs 19 has a misfeature whereby calling
1484 ;; parse-partial-sexp with COMMENTSTOP set and with
1485 ;; an initial list that says point is in a comment
1486 ;; will cause an immediate return. So we must skip
1487 ;; over the comment ourselves.
1488 (beginning-of-line 2)))
1489 ;; Frob the state to make it look like we aren't in a
1491 (setcar (nthcdr 4 state) nil))))
1493 (goto-char last-cstart))
1494 (cons real-comment state)))
1496 (defun tcl-hairy-in-comment ()
1497 "Return t if point is in a comment, and leave point at beginning
1499 (let ((save (point)))
1500 (tcl-beginning-of-defun)
1501 (car (tcl-hairy-scan-for-comment nil save nil))))
1503 (defun tcl-simple-in-comment ()
1504 "Return t if point is in comment, and leave point at beginning
1505 of comment. This is faster that `tcl-hairy-in-comment', but is
1506 correct less often."
1507 (let ((save (point))
1510 (while (and (< (point) save) (not comment))
1511 (search-forward "#" save 'move)
1512 (setq comment (tcl-real-comment-p)))
1515 (defun tcl-in-comment ()
1516 "Return t if point is in comment, and leave point at beginning
1518 (if (and tcl-pps-has-arg-6
1519 tcl-use-hairy-comment-detector)
1520 (tcl-hairy-in-comment)
1521 (tcl-simple-in-comment)))
1523 (defun tcl-do-auto-fill ()
1524 "Auto-fill function for Tcl mode. Only auto-fills in a comment."
1528 (setq in-comment (tcl-in-comment))
1530 (setq col (1- (current-column)))))
1535 (back-to-indentation)
1536 (delete-region (point) (save-excursion
1539 (indent-to-column col))))))
1544 ;; Help-related code.
1547 (defvar tcl-help-saved-dirs nil
1548 "Saved help directories.
1549 If `tcl-help-directory-list' changes, this allows `tcl-help-on-word'
1550 to update the alist.")
1552 (defvar tcl-help-alist nil
1553 "Alist with command names as keys and filenames as values.")
1555 (defun tcl-help-snarf-commands (dirlist)
1556 "Build alist of commands and filenames."
1558 (let ((files (directory-files (car dirlist) t)))
1560 (if (and (file-directory-p (car files))
1562 (let ((fpart (file-name-nondirectory (car files))))
1563 (or (equal fpart ".")
1564 (equal fpart "..")))))
1565 (let ((matches (directory-files (car files) t)))
1567 (or (file-directory-p (car matches))
1568 (setq tcl-help-alist
1570 (cons (file-name-nondirectory (car matches))
1573 (setq matches (cdr matches)))))
1574 (setq files (cdr files))))
1575 (setq dirlist (cdr dirlist))))
1577 (defun tcl-reread-help-files ()
1578 "Set up to re-read files, and then do it."
1580 (message "Building Tcl help file index...")
1581 (setq tcl-help-saved-dirs tcl-help-directory-list)
1582 (setq tcl-help-alist nil)
1583 (tcl-help-snarf-commands tcl-help-directory-list)
1584 (message "Building Tcl help file index...done"))
1586 (defun tcl-current-word (flag)
1587 "Return current command word, or nil.
1588 If FLAG is nil, just uses `current-word'.
1589 Otherwise scans backward for most likely Tcl command word."
1590 (if (and flag (eq major-mode 'tcl-mode))
1593 ;; Look backward for first word actually in alist.
1596 (while (and (not (bobp))
1597 (not (tcl-real-command-p)))
1599 (if (assoc (current-word) tcl-help-alist)
1604 (defun tcl-help-on-word (command &optional arg)
1605 "Get help on Tcl command. Default is word at point.
1606 Prefix argument means invert sense of `tcl-use-smart-word-finder'."
1610 (if (not (equal tcl-help-directory-list tcl-help-saved-dirs))
1611 (tcl-reread-help-files))
1612 (let ((word (tcl-current-word
1613 (if current-prefix-arg
1614 (not tcl-use-smart-word-finder)
1615 tcl-use-smart-word-finder))))
1617 (if (or (null word) (string= word ""))
1618 "Help on Tcl command: "
1619 (format "Help on Tcl command (default %s): " word))
1620 tcl-help-alist nil t)))
1621 current-prefix-arg))
1622 (if (not (equal tcl-help-directory-list tcl-help-saved-dirs))
1623 (tcl-reread-help-files))
1624 (if (string= command "")
1625 (setq command (tcl-current-word
1627 (not tcl-use-smart-word-finder)
1628 tcl-use-smart-word-finder))))
1629 (let* ((help (get-buffer-create "*Tcl help*"))
1630 (cell (assoc command tcl-help-alist))
1631 (file (and cell (cdr cell))))
1633 (delete-region (point-min) (point-max))
1636 (insert "*** " command "\n\n")
1637 (insert-file-contents file))
1638 (if (string= command "")
1639 (insert "Magical Pig!")
1640 (insert "Tcl command " command " not in help\n")))
1641 (set-buffer-modified-p nil)
1642 (goto-char (point-min))
1643 (display-buffer help)))
1648 ;; Other interactive stuff.
1651 (defvar tcl-previous-dir/file nil
1652 "Record last directory and file used in loading.
1653 This holds a cons cell of the form `(DIRECTORY . FILE)'
1654 describing the last `tcl-load-file' command.")
1656 (defun tcl-load-file (file &optional and-go)
1657 "Load a Tcl file into the inferior Tcl process.
1658 Prefix argument means switch to the Tcl buffer afterwards."
1661 ;; car because comint-get-source returns a list holding the
1663 (car (comint-get-source "Load Tcl file: " tcl-previous-dir/file
1665 current-prefix-arg))
1666 (comint-check-source file)
1667 (setq tcl-previous-dir/file (cons (file-name-directory file)
1668 (file-name-nondirectory file)))
1669 (tcl-send-string (inferior-tcl-proc)
1670 (format inferior-tcl-source-command (tcl-quote file)))
1671 (if and-go (switch-to-tcl t)))
1673 ;; Maybe this should work just like tcl-load-file. But I think what
1674 ;; I've implemented will turn out to be more useful.
1675 (defun tcl-restart-with-file (file &optional and-go)
1676 "Restart inferior Tcl with file.
1677 If an inferior Tcl process exists, it is killed first.
1678 Prefix argument means switch to the Tcl buffer afterwards."
1681 (car (comint-get-source "Restart with Tcl file: "
1683 (eq major-mode 'tcl-mode)
1685 tcl-previous-dir/file)
1687 current-prefix-arg))
1688 (let* ((buf (if (eq major-mode 'inferior-tcl-mode)
1690 inferior-tcl-buffer))
1691 (proc (and buf (get-process buf))))
1693 ((not (and buf (get-buffer buf)))
1694 ;; I think this will be ok.
1695 (inferior-tcl tcl-application)
1696 (tcl-load-file file and-go))
1698 (not (comint-check-proc buf))
1700 "A Tcl process is running, are you sure you want to reset it? "))
1702 (comint-check-source file)
1703 (setq tcl-previous-dir/file (cons (file-name-directory file)
1704 (file-name-nondirectory file)))
1705 (comint-exec (get-buffer-create buf)
1709 tcl-application file tcl-command-switches)
1710 (if and-go (switch-to-tcl t)))))))
1712 ;; FIXME I imagine you can do this under Emacs 18. I just don't know
1714 (defun tcl-auto-fill-mode (&optional arg)
1715 "Like `auto-fill-mode', but controls filling of Tcl comments."
1717 (and (not tcl-using-emacs-19)
1718 (error "You must use Emacs 19 to get this feature."))
1719 ;; Following code taken from "auto-fill-mode" (simple.el).
1721 (setq auto-fill-function
1723 (not auto-fill-function)
1724 (> (prefix-numeric-value arg) 0))
1727 ;; Update mode line. FIXME I'd use force-mode-line-update, but I
1728 ;; don't know if it exists in v18.
1729 (set-buffer-modified-p (buffer-modified-p))))
1731 (defun tcl-electric-hash (&optional count)
1732 "Insert a `#' and quote if it does not start a real comment.
1733 Prefix arg is number of `#'s to insert.
1734 See variable `tcl-electric-hash-style' for description of quoting
1737 (or count (setq count 1))
1740 (if (eq tcl-electric-hash-style 'smart)
1741 (if (> count 3) ; FIXME what is "smart"?
1744 tcl-electric-hash-style))
1750 (setq comment (tcl-in-comment)))
1752 (and tcl-explain-indentation (message "comment: %s" comment))
1757 ((eq type 'backslash)
1758 ;; The following will set count to 0, so the
1759 ;; insert-char can still be run.
1763 (setq count (1- count)))))
1765 (insert-char ?# count))))
1767 (defun tcl-hashify-buffer ()
1768 "Quote all `#'s in current buffer that aren't Tcl comments."
1771 (goto-char (point-min))
1772 (if (and tcl-pps-has-arg-6 tcl-use-hairy-comment-detector)
1775 (while (< (point) (point-max))
1776 (setq result (tcl-hairy-scan-for-comment state (point-max) t))
1778 (beginning-of-line 2)
1780 (if (eq ?# (following-char))
1783 (setq state (cdr result))))
1784 (while (and (< (point) (point-max))
1785 (search-forward "#" nil 'move))
1786 (if (tcl-real-comment-p)
1787 (beginning-of-line 2)
1788 ;; There's really no good way for the simple converter to
1789 ;; work. So we just quote # if it isn't already quoted.
1790 ;; Bogus, but it works.
1792 (if (not (eq ?\\ (preceding-char)))
1796 (defun tcl-indent-for-comment ()
1797 "Indent this line's comment to comment column, or insert an empty comment.
1798 Is smart about syntax of Tcl comments.
1799 Parts of this were taken from indent-for-comment (simple.el)."
1802 (or (tcl-in-comment)
1804 ;; Not in a comment, so we have to insert one. Create an
1805 ;; empty comment (since there isn't one on this line). If
1806 ;; line is not blank, make sure we insert a ";" first.
1807 (skip-chars-backward " \t")
1808 (let ((eolpoint (point)))
1810 (if (/= (point) eolpoint)
1812 (goto-char eolpoint)
1814 (if (tcl-real-command-p) "" ";")
1816 (backward-char))))))
1817 ;; Point is just after the "#" starting a comment. Move it as
1819 (let* ((indent (if comment-indent-hook
1820 (funcall comment-indent-hook)
1821 (funcall comment-indent-function)))
1825 (if (/= begpos indent)
1827 (skip-chars-backward " \t" (save-excursion
1830 (delete-region (point) begpos)
1831 (indent-to indent)))
1832 (looking-at comment-start-skip) ; Always true.
1833 (goto-char (match-end 0))
1834 ;; I don't like the effect of the next two.
1835 ;;(skip-chars-backward " \t" (match-beginning 0))
1836 ;;(skip-chars-backward "^ \t" (match-beginning 0))
1839 ;; The following was inspired by the Tcl editing mode written by
1840 ;; Gregor Schmid <schmid@fb3-s7.math.TU-Berlin.DE>. His version also
1841 ;; attempts to snarf the command line options from the command line,
1842 ;; but I didn't think that would really be that helpful (doesn't seem
1843 ;; like it owould be right enough. His version also looks for the
1844 ;; "#!/bin/csh ... exec" hack, but that seemed even less useful.
1845 ;; FIXME should make sure that the application mentioned actually
1847 (defun tcl-guess-application ()
1848 "Attempt to guess Tcl application by looking at first line.
1849 The first line is assumed to look like \"#!.../program ...\"."
1851 (goto-char (point-min))
1852 (if (looking-at "#![^ \t]*/\\([^ \t/]+\\)\\([ \t]\\|$\\)")
1854 (make-local-variable 'tcl-application)
1855 (setq tcl-application (buffer-substring (match-beginning 1)
1858 ;; This only exists to put on the menubar. I couldn't figure out any
1859 ;; other way to do it. FIXME should take "number of #-marks"
1861 (defun tcl-uncomment-region (beg end)
1864 (comment-region beg end -1))
1869 ;; Lucid menu support.
1870 ;; Taken from schmid@fb3-s7.math.TU-Berlin.DE (Gregor Schmid),
1871 ;; who wrote a different Tcl mode.
1872 ;; We also have support for menus in FSF. We do this by
1873 ;; loading the Lucid menu emulation code.
1876 (defun tcl-popup-menu (e)
1878 (and tcl-using-emacs-19
1879 (not tcl-using-lemacs-19)
1880 (if tcl-using-emacs-19.23
1883 ;; * lmenu.el provides 'menubar, which is bogus.
1884 ;; * lmenu.el causes menubars to be turned on everywhere.
1886 ;; Both of these problems are fixed in Emacs 19.23. People
1887 ;; using an Emacs before that just suffer.
1888 (require 'menubar "lmenu"))) ;; This is annoying
1889 ;; IMHO popup-menu should be autoloaded in FSF Emacs. Oh well.
1890 (popup-menu tcl-lucid-menu))
1895 ;; Quoting and unquoting functions.
1898 ;; This quoting is sufficient to protect eg a filename from any sort
1899 ;; of expansion or splitting. Tcl quoting sure sucks.
1900 (defun tcl-quote (string)
1901 "Quote STRING according to Tcl rules."
1902 (mapconcat (function (lambda (char)
1903 (if (memq char '(?[ ?] ?{ ?} ?\\ ?\" ?$ ? ?\;))
1904 (concat "\\" (char-to-string char))
1905 (char-to-string char))))
1914 (and (fboundp 'eval-when-compile)
1916 (require 'reporter)))
1918 (defun tcl-submit-bug-report ()
1919 "Submit via mail a bug report on Tcl mode."
1923 (y-or-n-p "Do you really want to submit a bug report on Tcl mode? ")
1924 (reporter-submit-bug-report
1926 (concat "Tcl mode " tcl-version)
1928 tcl-continued-indent-level
1930 tcl-tab-always-indent
1931 tcl-use-hairy-comment-detector
1932 tcl-electric-has-style
1933 tcl-help-directory-list
1934 tcl-use-smart-word-finder
1936 tcl-command-switches
1938 inferior-tcl-source-command
1940 tcl-using-emacs-19.23
1946 tcl-font-lock-keywords
1947 tcl-pps-has-arg-6))))
1953 ;;; tcl.el ends here