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.3 $
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 to point to the topmost
37 ;; directory containing the TclX help files. Eg:
39 ;; (setq tcl-help-directory "/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$|$Revision: 1.3 $|
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.
67 ;; 18-Mar-1994 Tom Tromey Fourth beta release.
68 ;; Added {un,}comment-region to menu. Idea from
69 ;; Mike Scheidler <c23mts@kocrsv01.delcoelect.com>
70 ;; 17-Mar-1994 Tom Tromey
71 ;; Fixed tcl-restart-with-file. Bug fix attempt in
72 ;; tcl-internal-end-of-defun.
73 ;; 16-Mar-1994 Tom Tromey Third beta release
74 ;; Added support code for menu (from Tcl mode written by
75 ;; schmid@fb3-s7.math.TU-Berlin.DE (Gregor Schmid)).
76 ;; 12-Mar-1994 Tom Tromey
77 ;; Better documentation for inferior-tcl-buffer. Wrote
78 ;; tcl-restart-with-file. Wrote Lucid Emacs menu (but no
79 ;; code to install it).
80 ;; 12-Mar-1994 Tom Tromey
81 ;; Wrote tcl-guess-application. Another stab at making
82 ;; tcl-omit-ws-regexp work.
83 ;; 10-Mar-1994 Tom Tromey Second beta release
84 ;; Last Modified: Thu Mar 10 01:24:25 1994 (Tom Tromey)
85 ;; Wrote perl-mode style line indentation command.
86 ;; Wrote more documentation. Added tcl-continued-indent-level.
87 ;; Integrated help code.
88 ;; 8-Mar-1994 Tom Tromey
89 ;; Last Modified: Tue Mar 8 11:58:44 1994 (Tom Tromey)
91 ;; 6-Mar-1994 Tom Tromey
92 ;; Last Modified: Sun Mar 6 18:55:41 1994 (Tom Tromey)
93 ;; Updated auto-newline support.
94 ;; 6-Mar-1994 Tom Tromey Beta release
95 ;; Last Modified: Sat Mar 5 17:24:32 1994 (Tom Tromey)
96 ;; Wrote tcl-hashify-buffer. Other minor bug fixes.
97 ;; 5-Mar-1994 Tom Tromey
98 ;; Last Modified: Sat Mar 5 16:11:20 1994 (Tom Tromey)
99 ;; Wrote electric-hash code.
100 ;; 3-Mar-1994 Tom Tromey
101 ;; Last Modified: Thu Mar 3 02:53:40 1994 (Tom Tromey)
102 ;; Added code to handle auto-fill in comments.
103 ;; Added imenu support code.
105 ;; Better font-lock support.
106 ;; 28-Feb-1994 Tom Tromey
107 ;; Last Modified: Mon Feb 28 14:08:05 1994 (Tom Tromey)
108 ;; Made tcl-figure-type more easily configurable.
109 ;; 28-Feb-1994 Tom Tromey
110 ;; Last Modified: Mon Feb 28 01:02:58 1994 (Tom Tromey)
111 ;; Wrote inferior-tcl mode.
112 ;; 16-Feb-1994 Tom Tromey
113 ;; Last Modified: Wed Feb 16 17:05:19 1994 (Tom Tromey)
114 ;; Added support for font-lock-mode.
115 ;; 29-Oct-1993 Tom Tromey
116 ;; Last Modified: Sun Oct 24 17:39:14 1993 (Tom Tromey)
117 ;; Patches from Guido Bosch to make things work with Lucid Emacs.
118 ;; 22-Oct-1993 Tom Tromey
119 ;; Last Modified: Fri Oct 22 15:26:46 1993 (Tom Tromey)
120 ;; Made many characters have "_" syntax class; suggested by Guido
121 ;; Bosch <Guido.Bosch@loria.fr>. Note that this includes the "$"
122 ;; character, which might be a change you'd notice.
123 ;; 21-Oct-1993 Tom Tromey
124 ;; Last Modified: Thu Oct 21 20:28:40 1993 (Tom Tromey)
125 ;; More fixes for tcl-omit-ws-regexp.
126 ;; 20-Oct-1993 Tom Tromey
127 ;; Started keeping history. Fixed tcl-{beginning,end}-of-defun.
128 ;; Added some code to make things work with Emacs 18.
131 ;; Guido Bosch <Guido.Bosch@loria.fr>
132 ;; pgs1002@esc.cam.ac.uk (Dr P.G. Sjoerdsma)
133 ;; Mike Scheidler <c23mts@kocrsv01.delcoelect.com>
134 ;; Matt Newman <men@charney.colorado.edu>
135 ;; rwhitby@research.canon.oz.au (Rod Whitby)
136 ;; h9118101@hkuxa.hku.hk (Yip Chi Lap [Beta])
137 ;; Pertti Tapio Kasanen <ptk@delta.hut.fi>
138 ;; schmid@fb3-s7.math.TU-Berlin.DE (Gregor Schmid)
141 ;; * indent-region should skip blank lines. (It does in v19, so I'm
142 ;; not motivated to fix it here).
143 ;; * In Tcl "#" is not always a comment character. This can confuse
144 ;; tcl.el in certain circumstances. For now the only workaround is
145 ;; to enclose offending hash characters in quotes or precede it with
146 ;; a backslash. Note that using braces won't work -- quotes change
147 ;; the syntax class of characters between them, while braces do not.
148 ;; The electric-# mode helps alleviate this problem somewhat.
149 ;; * indent-tcl-exp is untested.
150 ;; * Doesn't work under Emacs 18 yet.
151 ;; * There's been a report that font-lock does strange things under
152 ;; Lucid Emacs 19.6. For instance in "proc foobar", the space
153 ;; before "foobar" is highlighted.
156 ;; * make add-log-tcl-defun smarter. should notice if we are in the
157 ;; middle of a defun, or between defuns. should notice if point is
158 ;; on first line of defun (or maybe even in comments before defun).
159 ;; * Allow continuation lines to be indented under the first argument
160 ;; of the preceeding line, like this:
163 ;; * There is a request that indentation work like this:
164 ;; button .fred -label Fred \
165 ;; -command {puts fred}
166 ;; * Should have tcl-complete-symbol that queries the inferior process.
167 ;; * Should have describe-symbol that works by sending the magic
168 ;; command to a tclX process.
169 ;; * Need C-x C-e binding (tcl-eval-last-exp).
170 ;; * Write indent-region function that is faster than indenting each
171 ;; line individually.
172 ;; * tcl-figure-type should stop at "beginning of line" (only ws
173 ;; before point, and no "\" on previous line). (see tcl-real-command-p).
174 ;; * Fix beginning-of-defun. I believe this will be fully possible in
176 ;; * overrides some comint keybindings; fix.
177 ;; * Trailing \ will eat blank lines. Should deal with this.
178 ;; (this would help catch some potential bugs).
179 ;; * Inferior should display in half the screen, not the whole screen.
191 (defvar tcl-indent-level 4
192 "*Indentation of Tcl statements with respect to containing block.")
194 (defvar tcl-continued-indent-level 4
195 "*Indentation of continuation line relative to first line of command.")
197 (defvar tcl-auto-newline nil
198 "*Non-nil means automatically newline before and after braces
199 inserted in Tcl code.")
201 (defvar tcl-tab-always-indent t
202 "*Control effect of TAB key.
203 If t (the default), always indent current line.
204 If nil and point is not in the indentation area at the beginning of
205 the line, a TAB is inserted.
206 Other values cause the first possible action from the following list
209 1. Move from beginning of line to correct indentation.
210 2. Delete an empty comment.
211 3. Move forward to start of comment, indenting if necessary.
212 4. Move forward to end of line, indenting if necessary.
213 5. Create an empty comment.
214 6. Move backward to start of comment, indenting if necessary.")
216 (defvar tcl-use-hairy-comment-detector t
217 "*If not `nil', the the more complicated, but slower, comment
218 detecting function is used. This variable is only used in GNU Emacs
219 19 (the fast function is always used elsewhere).")
221 (defvar tcl-electric-hash-style 'smart
222 "*Style of electric hash insertion to use.
223 Possible values are 'backslash, meaning that `\\' quoting should be
224 done; `quote, meaning that `\"' quoting should be done; 'smart,
225 meaning that the choice between 'backslash and 'quote should be
226 made depending on the number of hashes inserted; or nil, meaning that
227 no quoting should be done. Any other value for this variable is
228 taken to mean 'smart. The default is 'smart.")
230 (defvar tcl-help-directory nil
231 "*Name of topmost directory containing TclX help files")
233 (defvar tcl-use-smart-word-finder t
234 "*If not nil, use a better way of finding the current word when
235 looking up help on a Tcl command.")
237 (defvar tcl-application "wish"
238 "*Name of Tcl application to run in inferior Tcl mode.")
240 (defvar tcl-command-switches nil
241 "*Switches to supply to `tcl-application'.")
243 (defvar tcl-prompt-regexp "^\\(% \\|\\)"
244 "*If not nil, a regexp that will match the prompt in the inferior process.
245 If nil, the prompt is the name of the application with \">\" appended.
247 The default is \"^\\(% \\|\\)\", which will match the default primary
248 and secondary prompts for tclsh and wish.")
250 (defvar inferior-tcl-source-command "source %s\n"
251 "*Format-string for building a Tcl command to load a file.
252 This format string should use `%s' to substitute a file name
253 and should result in a Tcl expression that will command the
254 inferior Tcl to load that file. The filename will be appropriately
258 ;; Keymaps, abbrevs, syntax tables.
261 (defvar tcl-mode-abbrev-table nil
262 "Abbrev table in use in Tcl-mode buffers.")
263 (if tcl-mode-abbrev-table
265 (define-abbrev-table 'tcl-mode-abbrev-table ()))
267 ;; I sure wish Emacs had a package that made it easy to extract this
268 ;; sort of information.
269 (defconst tcl-using-emacs-19 (string-match "19\\." emacs-version)
270 "Nil unless using Emacs 19 (Lucid or FSF).")
272 ;; FIXME this will break on Emacs 19.100.
273 (defconst tcl-using-emacs-19.23
274 (string-match "19\\.\\(2[3-9]\\|[3-9][0-9]\\)" emacs-version)
275 "Nil unless using Emacs 19.23 or later.")
277 (defconst tcl-using-lemacs-19 (string-match "Lucid" emacs-version)
278 "Nil unless using Lucid Emacs).")
280 (defvar tcl-mode-map ()
281 "Keymap used in Tcl mode.")
284 (setq tcl-mode-map (make-sparse-keymap))
285 (define-key tcl-mode-map "{" 'tcl-electric-char)
286 (define-key tcl-mode-map "}" 'tcl-electric-brace)
287 (define-key tcl-mode-map "[" 'tcl-electric-char)
288 (define-key tcl-mode-map "]" 'tcl-electric-char)
289 (define-key tcl-mode-map ";" 'tcl-electric-char)
290 (define-key tcl-mode-map "#" 'tcl-electric-hash)
292 (define-key tcl-mode-map "\e\C-a" 'tcl-beginning-of-defun)
294 (define-key tcl-mode-map "\e\C-e" 'tcl-end-of-defun)
296 (define-key tcl-mode-map "\e\C-h" 'mark-tcl-function)
297 (define-key tcl-mode-map "\e\C-q" 'indent-tcl-exp)
298 (define-key tcl-mode-map "\177" 'backward-delete-char-untabify)
299 (define-key tcl-mode-map "\t" 'tcl-indent-command)
300 (define-key tcl-mode-map "\M-\C-x" 'tcl-eval-defun)
301 (and (fboundp 'comment-region)
302 (define-key tcl-mode-map "\C-c\C-c" 'comment-region))
303 (define-key tcl-mode-map "\C-c\C-d" 'tcl-help-on-word)
304 (define-key tcl-mode-map "\C-c\C-e" 'tcl-eval-defun)
305 (define-key tcl-mode-map "\C-c\C-l" 'tcl-load-file)
306 (define-key tcl-mode-map "\C-c\C-p" 'inferior-tcl)
307 (define-key tcl-mode-map "\C-c\C-r" 'tcl-eval-region)
308 (define-key tcl-mode-map "\C-c\C-z" 'switch-to-tcl))
310 (defvar tcl-mode-syntax-table nil
311 "Syntax table in use in Tcl-mode buffers.")
312 (if tcl-mode-syntax-table
314 (setq tcl-mode-syntax-table (make-syntax-table))
315 (modify-syntax-entry ?% "_" tcl-mode-syntax-table)
316 (modify-syntax-entry ?@ "_" tcl-mode-syntax-table)
317 (modify-syntax-entry ?& "_" tcl-mode-syntax-table)
318 (modify-syntax-entry ?* "_" tcl-mode-syntax-table)
319 (modify-syntax-entry ?+ "_" tcl-mode-syntax-table)
320 (modify-syntax-entry ?- "_" tcl-mode-syntax-table)
321 (modify-syntax-entry ?. "_" tcl-mode-syntax-table)
322 (modify-syntax-entry ?: "_" tcl-mode-syntax-table)
323 (modify-syntax-entry ?! "_" tcl-mode-syntax-table)
324 (modify-syntax-entry ?$ "_" tcl-mode-syntax-table) ; FIXME use "'"?
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)
334 (modify-syntax-entry ?\n "> " tcl-mode-syntax-table)
335 (modify-syntax-entry ?\f "> " tcl-mode-syntax-table)
336 (modify-syntax-entry ?# "< " tcl-mode-syntax-table))
338 (defvar inferior-tcl-mode-map nil
339 "Keymap used in Inferior Tcl mode.")
340 (if inferior-tcl-mode-map
342 ;; FIXME Use keymap inheritance here? FIXME we override comint
343 ;; keybindings here. Maybe someone has a better set?
344 (setq inferior-tcl-mode-map (copy-keymap comint-mode-map))
345 (define-key inferior-tcl-mode-map "\e\C-a" 'tcl-beginning-of-defun)
346 (define-key inferior-tcl-mode-map "\e\C-e" 'tcl-end-of-defun)
347 (define-key inferior-tcl-mode-map "\177" 'backward-delete-char-untabify)
348 (define-key inferior-tcl-mode-map "\M-\C-x" 'tcl-eval-defun)
349 (define-key inferior-tcl-mode-map "\C-c\C-d" 'tcl-help-on-word)
350 (define-key inferior-tcl-mode-map "\C-c\C-e" 'tcl-eval-defun)
351 (define-key inferior-tcl-mode-map "\C-c\C-l" 'tcl-load-file)
352 (define-key inferior-tcl-mode-map "\C-c\C-p" 'inferior-tcl)
353 (define-key inferior-tcl-mode-map "\C-c\C-r" 'tcl-eval-region)
354 (define-key inferior-tcl-mode-map "\C-c\C-z" 'switch-to-tcl))
357 (defvar tcl-lucid-menu
359 ["Beginning of function" tcl-beginning-of-defun t]
360 ["End of function" tcl-end-of-defun t]
361 ["Mark function" mark-tcl-function t]
362 ["Indent region" indent-region t]
363 ["Comment region" comment-region t]
364 ["Uncomment region" tcl-uncomment-region t]
366 ["Show Tcl process buffer" inferior-tcl t]
367 ["Send function to Tcl process" tcl-eval-defun t]
368 ["Send region to Tcl process" tcl-eval-region t]
369 ["Send file to Tcl process" tcl-load-file t]
370 ["Restart Tcl process with file" tcl-restart-with-file t]
372 ["Tcl help" tcl-help-on-word t]))
374 (defvar inferior-tcl-buffer nil
375 "*The current inferior-tcl process buffer.
377 MULTIPLE PROCESS SUPPORT
378 ===========================================================================
379 To run multiple Tcl processes, you start the first up with
380 \\[inferior-tcl]. It will be in a buffer named `*inferior-tcl*'.
381 Rename this buffer with \\[rename-buffer]. You may now start up a new
382 process with another \\[inferior-tcl]. It will be in a new buffer,
383 named `*inferior-tcl*'. You can switch between the different process
384 buffers with \\[switch-to-buffer].
386 Commands that send text from source buffers to Tcl processes -- like
387 `tcl-eval-defun' or `tcl-load-file' -- have to choose a process to
388 send to, when you have more than one Tcl process around. This is
389 determined by the global variable `inferior-tcl-buffer'. Suppose you
390 have three inferior Lisps running:
394 *inferior-tcl* inferior-tcl<3>
395 If you do a \\[tcl-eval-defun] command on some Lisp source code, what
396 process do you send it to?
398 - If you're in a process buffer (foo, bar, or *inferior-tcl*),
399 you send it to that process.
400 - If you're in some other buffer (e.g., a source file), you
401 send it to the process attached to buffer `inferior-tcl-buffer'.
402 This process selection is performed by function `inferior-tcl-proc'.
404 Whenever \\[inferior-tcl] fires up a new process, it resets
405 `inferior-tcl-buffer' to be the new process's buffer. If you only run
406 one process, this does the right thing. If you run multiple
407 processes, you can change `inferior-tcl-buffer' to another process
408 buffer with \\[set-variable].")
411 ;; Hooks and other customization.
414 (defvar tcl-mode-hook nil
415 "Hook run on entry to Tcl mode.
417 Several functions exist which are useful to run from your
418 `tcl-mode-hook' (see each function's documentation for more
422 Puts a \"Tcl\" menu on the menubar. Doesn't work in Emacs 18.
423 tcl-guess-application
424 Guesses a default setting for `tcl-application' based on any
425 \"#!\" line at the top of the file.
427 Quotes all \"#\" characters that don't correspond to actual
428 Tcl comments. (Useful when editing code not originally created
431 Auto-filling of Tcl comments.
433 Emacs 19 users can add functions to the hook with `add-hook':
435 (add-hook 'tcl-mode-hook 'tcl-guess-application)
437 Emacs 18 users must use `setq':
439 (setq tcl-mode-hook (cons 'tcl-guess-application tcl-mode-hook))")
442 (defvar inferior-tcl-mode-hook nil
443 "Hook for customizing Inferior Tcl mode.")
445 (defvar tcl-proc-list
447 "List of commands whose first argument defines something.
448 This exists because some people (eg, me) use \"defvar\" et al.
449 Call `tcl-set-proc-regexp' and `tcl-set-font-lock-keywords'
450 after changing this list.")
452 (defvar tcl-proc-regexp nil
453 "Regexp to use when matching proc headers.")
455 (defvar tcl-typeword-list
457 "List of Tcl keywords deonting \"type\". Used only for highlighting.
458 Call `tcl-set-font-lock-keywords' after changing this list.")
460 ;; Generally I've picked control operators to be keywords.
461 (defvar tcl-keyword-list
462 '("if" "then" "else" "elseif" "for" "foreach" "break" "continue" "while"
463 "eval" "case" "in" "switch" "default" "exit" "error" "proc" "return"
464 "uplevel" "loop" "for_array_keys" "for_recursive_glob" "for_file")
465 "List of Tcl keywords. Used only for highlighting.
466 Default list includes some TclX keywords.
467 Call `tcl-set-font-lock-keywords' after changing this list.")
469 (defvar tcl-font-lock-keywords nil
470 "Keywords to highlight for Tcl. See variable `font-lock-keywords'.
471 This variable is generally set from `tcl-proc-regexp',
472 `tcl-typeword-list', and `tcl-keyword-list' by the function
473 `tcl-set-font-lock-keywords'.")
475 ;; FIXME need some way to recognize variables because array refs look
477 (defvar tcl-type-alist
480 ("catch" tcl-commands)
481 ("if" tcl-expr "then" tcl-commands)
482 ("elseif" tcl-expr "then" tcl-commands)
483 ("elseif" tcl-expr tcl-commands)
484 ("if" tcl-expr tcl-commands)
485 ("while" tcl-expr tcl-commands)
486 ("for" tcl-commands tcl-expr tcl-commands tcl-commands)
487 ("foreach" nil nil tcl-commands)
488 ("for_file" nil nil tcl-commands)
489 ("for_array_keys" nil nil tcl-commands)
490 ("for_recursive_glob" nil nil nil tcl-commands)
491 ;; Loop handling is not perfect, because the third argument can be
492 ;; either a command or an expr, and there is no real way to look
494 ("loop" nil tcl-expr tcl-expr tcl-commands)
495 ("loop" nil tcl-expr tcl-commands)
497 "Alist that controls indentation.
498 \(Actually, this really only controls what happens on continuation lines).
499 Each entry looks like `(KEYWORD TYPE ...)'.
500 Each type entry describes a sexp after the keyword, and can be one of:
501 * nil, meaning that this sexp has no particular type.
502 * tcl-expr, meaning that this sexp is an arithmetic expression.
503 * tcl-commands, meaning that this sexp holds Tcl commands.
504 * a string, which must exactly match the string at the corresponding
505 position for a match to be made.
507 For example, the entry for the \"loop\" command is:
509 (\"loop\" nil tcl-expr tcl-commands)
511 This means that the \"loop\" command has three arguments. The first
512 argument is ignored (for indentation purposes). The second argument
513 is a Tcl expression, and the last argument is Tcl commands.")
515 (defvar tcl-explain-indentation nil
516 "If not `nil', debugging message will be printed during indentation.")
521 ;; Work around differences between various versions of Emacs.
524 ;; We use this because Lemacs 19.9 has what we need.
525 (defconst tcl-pps-has-arg-6
526 (or tcl-using-emacs-19
527 (and tcl-using-lemacs-19
530 (parse-partial-sexp (point) (point) nil nil nil t)
533 "t if using an emacs which supports sixth (\"commentstop\") argument
534 to parse-partial-sexp.")
536 ;; Its pretty bogus to have to do this, but there is no easier way to
537 ;; say "match not syntax-1 and not syntax-2". Too bad you can't put
538 ;; \s in [...]. This sickness is used in Emacs 19 to match a defun
539 ;; starter. (It is used for this in v18 as well).
540 ;;(defconst tcl-omit-ws-regexp
542 ;; (mapconcat 'char-to-string "w_.()\"\\$'/" "\\|\\s")
544 ;; "Regular expression that matches everything except space, comment
545 ;;starter, and comment ender syntax codes.")
547 ;; FIXME? Instead of using the hairy regexp above, we just use a
549 ;;(defconst tcl-omit-ws-regexp "^[^] \t\n#}]\\S(*"
550 ;; "Regular expression used in locating function definitions.")
552 ;; Here's another stab. I think this one actually works. Now the
553 ;; problem seems to be that there is a bug in Emacs 19.22 where
554 ;; end-of-defun doesn't really use the brace matching the one that
555 ;; trails defun-prompt-regexp.
556 (defconst tcl-omit-ws-regexp "^[^ \t\n#}][^\n}]+}*[ \t]+")
558 (defun tcl-internal-beginning-of-defun (&optional arg)
559 "Move backward to next beginning-of-defun.
560 With argument, do this that many times.
561 Returns t unless search stops due to end of buffer."
563 (if (or (null arg) (= arg 0))
570 (re-search-backward tcl-omit-ws-regexp nil 'move 1))))
571 (while (and (looking-at "[]#}]")
573 (re-search-backward tcl-omit-ws-regexp nil 'move 1)))))
575 (not (null success))))
577 (defun tcl-internal-end-of-defun (&optional arg)
578 "Move forward to next end of defun.
579 An end of a defun is found by moving forward from the beginning of one."
581 (if (or (null arg) (= arg 0)) (setq arg 1))
582 (let ((start (point)))
583 ;; Was forward-char. I think this works a little better.
585 (tcl-beginning-of-defun)
587 (while (and (re-search-forward tcl-omit-ws-regexp nil 'move 1)
588 (progn (beginning-of-line) t)
590 (progn (forward-line) t)))
591 (let ((next-line (save-excursion
594 (while (< (point) next-line)
597 (if (> (point) start) (setq arg (1- arg))))))
599 ;; In Emacs 19, we can use begining-of-defun as long as we set up a
600 ;; certain regexp. In Emacs 18, we need our own function.
601 (fset 'tcl-beginning-of-defun
602 (if tcl-using-emacs-19
604 'tcl-internal-beginning-of-defun))
606 ;; Only FSF Emacs 19 works correctly using end-of-defun. Emacs 18 and
607 ;; Lucid need our own function.
608 (fset 'tcl-end-of-defun
609 (if (and tcl-using-emacs-19 (not tcl-using-lemacs-19))
611 'tcl-internal-end-of-defun))
616 ;; Some helper functions.
619 (defun tcl-set-proc-regexp ()
620 "Set `tcl-proc-regexp' from variable `tcl-proc-list'."
621 (setq tcl-proc-regexp (concat "^\\("
622 (mapconcat 'identity tcl-proc-list "\\|")
625 (defun tcl-set-font-lock-keywords ()
626 "Set `tcl-font-lock-keywords'.
627 Uses variables `tcl-proc-regexp' and `tcl-keyword-list'."
628 (setq tcl-font-lock-keywords
630 ;; Names of functions (and other "defining things").
631 (list (concat tcl-proc-regexp "\\([^ \t\n]+\\)")
632 2 'font-lock-function-name-face)
634 ;; Names of type-defining things.
635 (list (concat "\\(\\s-\\|^\\)\\("
636 ;; FIXME Use 'regexp-quote?
637 (mapconcat 'identity tcl-typeword-list "\\|")
639 2 'font-lock-type-face)
641 ;; Keywords. Only recognized if surrounded by whitespace.
642 ;; FIXME consider using "not word or symbol", not
644 (cons (concat "\\(\\s-\\|^\\)\\("
645 ;; FIXME Use regexp-quote?
646 (mapconcat 'identity tcl-keyword-list "\\|")
653 (tcl-set-proc-regexp))
655 (if tcl-font-lock-keywords
657 (tcl-set-font-lock-keywords))
666 "Major mode for editing Tcl code.
667 Expression and list commands understand all Tcl brackets.
668 Tab indents for Tcl code.
669 Paragraphs are separated by blank lines only.
670 Delete converts tabs to spaces as it moves back.
672 Variables controlling indentation style:
674 Indentation of Tcl statements within surrounding block.
675 tcl-continued-indent-level
676 Indentation of continuation line relative to first line of command.
678 Variables controlling user interaction with mode (see variable
679 documentation for details):
680 tcl-tab-always-indent
681 Controls action of TAB key.
683 Non-nil means automatically newline before and after braces, brackets,
684 and semicolons inserted in Tcl code.
685 tcl-electric-hash-style
686 Controls action of `#' key.
687 tcl-use-hairy-comment-detector
688 If t, use more complicated, but slower, comment detector.
689 This variable is only used in GNU Emacs 19.
691 Turning on Tcl mode calls the value of the variable `tcl-mode-hook'
692 with no args, if that value is non-nil. Read the documentation for
693 `tcl-mode-hook' to see what kinds of interesting hook functions
699 (kill-all-local-variables)
700 (use-local-map tcl-mode-map)
701 (setq major-mode 'tcl-mode)
702 (setq mode-name "Tcl")
703 (setq local-abbrev-table tcl-mode-abbrev-table)
704 (set-syntax-table tcl-mode-syntax-table)
705 (make-local-variable 'paragraph-start)
706 (setq paragraph-start (concat "^$\\|" page-delimiter))
707 (make-local-variable 'paragraph-separate)
708 (setq paragraph-separate paragraph-start)
709 (make-local-variable 'paragraph-ignore-fill-prefix)
710 (setq paragraph-ignore-fill-prefix t)
711 (make-local-variable 'indent-line-function)
712 (setq indent-line-function 'tcl-indent-line)
713 ;; Tcl doesn't require a final newline.
714 ;; (make-local-variable 'require-final-newline)
715 ;; (setq require-final-newline t)
716 (make-local-variable 'comment-start)
717 (setq comment-start "# ")
718 (make-local-variable 'comment-start-skip)
719 (setq comment-start-skip "#+ *")
720 (make-local-variable 'comment-column)
721 (setq comment-column 40)
722 (make-local-variable 'comment-end)
723 (setq comment-end "")
724 (make-local-variable 'font-lock-keywords)
725 (setq font-lock-keywords tcl-font-lock-keywords)
726 (setq imenu-create-index-function 'tcl-imenu-create-index-function)
727 (make-local-variable 'parse-sexp-ignore-comments)
728 (if tcl-using-emacs-19
730 ;; This can only be set to t in Emacs 19 and Lucid Emacs.
731 ;; Emacs 18 and Epoch lose.
732 (setq parse-sexp-ignore-comments t)
733 ;; Lucid Emacs has defun-prompt-regexp, but I don't believe
734 ;; that it works for end-of-defun -- only for
735 ;; beginning-of-defun.
736 (make-local-variable 'defun-prompt-regexp)
737 (setq defun-prompt-regexp tcl-omit-ws-regexp)
738 ;; The following doesn't work in Lucid Emacs 19.6, but maybe
739 ;; it will appear in later versions.
740 (make-local-variable 'add-log-current-defun-function)
741 (setq add-log-current-defun-function 'add-log-tcl-defun))
742 (setq parse-sexp-ignore-comments nil))
743 (run-hooks 'tcl-mode-hook))
747 ;; This is used for braces, brackets, and semi (except for closing
748 ;; braces, which are handled specially).
749 (defun tcl-electric-char (arg)
750 "Insert character and correct line's indentation."
752 ;; Indent line first; this looks better if parens blink.
754 (self-insert-command arg)
755 (if (and tcl-auto-newline (= last-command-char ?\;))
760 ;; This is used for closing braces. If tcl-auto-newline is set, can
761 ;; insert a newline both before and after the brace, depending on
762 ;; context. FIXME should this be configurable? Does anyone use this?
763 (defun tcl-electric-brace (arg)
764 "Insert character and correct line's indentation."
766 ;; If auto-newlining and there is stuff on the same line, insert a
771 (skip-chars-backward " \t")
776 ;; In auto-newline case, must insert a newline after each
777 ;; brace. So an explicit loop is needed.
779 (insert last-command-char)
782 (setq arg (1- arg))))
783 (self-insert-command arg))
788 (defun tcl-indent-command (&optional arg)
789 "Indent current line as Tcl code, or in some cases insert a tab character.
790 If tcl-tab-always-indent is t (the default), always indent current line.
791 If tcl-tab-always-indent is nil and point is not in the indentation
792 area at the beginning of the line, a TAB is inserted.
793 Other values of tcl-tab-always-indent cause the first possible action
794 from the following list to take place:
796 1. Move from beginning of line to correct indentation.
797 2. Delete an empty comment.
798 3. Move forward to start of comment, indenting if necessary.
799 4. Move forward to end of line, indenting if necessary.
800 5. Create an empty comment.
801 6. Move backward to start of comment, indenting if necessary."
804 ((not tcl-tab-always-indent)
805 ;; Indent if in identation area, otherwise insert TAB.
806 (if (<= (current-column) (current-indentation))
808 (self-insert-command arg)))
809 ((eq tcl-tab-always-indent t)
813 ;; "Perl-mode" style TAB command.
814 (let* ((ipoint (point))
818 (comment-p (tcl-in-comment)))
820 ((= ipoint (save-excursion
825 ;; If indenting didn't leave us in column 0, go to the
826 ;; indentation. Otherwise leave point at end of line. This
828 (if (= (point) (save-excursion
832 (back-to-indentation)))
833 ((and comment-p (looking-at "[ \t]*$"))
834 ;; Empty comment, so delete it. We also delete any ";"
835 ;; characters at the end of the line. I think this is
836 ;; friendlier, but I don't know how other people will feel.
838 (skip-chars-backward " \t;")
839 (delete-region (point) eolpoint))
840 ((and comment-p (< ipoint (point)))
841 ;; Before comment, so skip to it.
843 (indent-for-comment))
844 ((/= ipoint eolpoint)
845 ;; Go to end of line (since we're not there yet).
849 ;; Create an empty comment (since there isn't one on this
850 ;; line). If line is not blank, make sure we insert a ";"
853 (if (/= (point) eolpoint)
856 (or (tcl-real-command-p)
859 (indent-for-comment))
861 ;; Go to start of comment. We don't leave point where it is
862 ;; because we want to skip comment-start-skip.
864 (indent-for-comment)))))))
866 (defun tcl-indent-line ()
867 "Indent current line as Tcl code.
868 Return the amount the indentation changed by."
869 (let ((indent (calculate-tcl-indent nil))
871 (case-fold-search nil)
872 (pos (- (point-max) (point))))
875 (cond ((eq indent nil)
876 (setq indent (current-indentation)))
878 (skip-chars-forward " \t")
879 (if (listp indent) (setq indent (car indent)))
880 (cond ((= (following-char) ?})
881 (setq indent (- indent tcl-indent-level)))
882 ((= (following-char) ?\])
883 (setq indent (- indent 1))))))
884 (skip-chars-forward " \t")
885 (setq shift-amt (- indent (current-column)))
886 (if (zerop shift-amt)
887 (if (> (- (point-max) pos) (point))
888 (goto-char (- (point-max) pos)))
889 (delete-region beg (point))
891 ;; If initial point was within line's indentation,
892 ;; position after the indentation. Else stay at same point in text.
893 (if (> (- (point-max) pos) (point))
894 (goto-char (- (point-max) pos))))
897 (defun tcl-figure-type ()
898 "Determine type of sexp at point.
899 This is either 'tcl-expr, 'tcl-commands, or nil. Puts point at start
900 of sexp that indicates types.
902 See documentation for variable `tcl-type-alist' for more information."
906 (while (and (< count 5)
910 ;; FIXME should use "tcl-backward-sexp", which would skip
911 ;; over entire variables, etc.
913 (if (looking-at "[a-zA-Z_]+")
914 (let ((list tcl-type-alist)
916 (setq word-stack (cons (current-word) word-stack))
917 (while (and list (not result))
918 (setq entry (car list))
919 (setq list (cdr list))
921 (while (and entry (<= index count))
922 ;; Abort loop if string does not match word on
924 (and (stringp (car entry))
925 (not (string= (car entry)
926 (nth index word-stack)))
928 (setq entry (cdr entry))
929 (setq index (1+ index)))
931 (not (stringp (car entry)))
932 (setq result (car entry)))
934 (setq word-stack (cons nil word-stack))))
936 (setq count (1+ count)))
937 (and tcl-explain-indentation
938 (message "Indentation type %s" result))
941 (defun calculate-tcl-indent (&optional parse-start)
942 "Return appropriate indentation for current line as Tcl code.
943 In usual case returns an integer: the column to indent to.
944 Returns nil if line starts inside a string, t if in a comment."
947 (let* ((indent-point (point))
948 (case-fold-search nil)
954 (= ?\\ (preceding-char)))))
955 (continued-indent-value (if continued-line
956 tcl-continued-indent-level
962 (goto-char parse-start)
963 (tcl-beginning-of-defun))
964 (while (< (point) indent-point)
965 (setq parse-start (point))
966 (setq state (parse-partial-sexp (point) indent-point 0))
967 (setq containing-sexp (car (cdr state))))
968 (cond ((or (nth 3 state) (nth 4 state))
969 ;; Inside comment or string. Return nil or t if should
970 ;; not change this line
972 ((null containing-sexp)
973 ;; Line is at top level.
974 continued-indent-value)
976 ;; Set expr-p if we are looking at the expression part of
977 ;; an "if", "expr", etc statement. Set commands-p if we
978 ;; are looking at the body part of an if, while, etc
979 ;; statement. FIXME Should check for "for" loops here.
980 (goto-char containing-sexp)
981 (let* ((sexpr-type (tcl-figure-type))
982 (expr-p (eq sexpr-type 'tcl-expr))
983 (commands-p (eq sexpr-type 'tcl-commands))
984 (expr-start (point)))
985 ;; Find the first statement in the block and indent
986 ;; like it. The first statement in the block might be
987 ;; on the same line, so what we do is skip all
988 ;; "virtually blank" lines, looking for a non-blank
989 ;; one. A line is virtually blank if it only contains
990 ;; a comment and whitespace. FIXME continued comments
991 ;; aren't supported. They are a wart on Tcl anyway.
992 ;; We do it this funky way because we want to know if
993 ;; we've found a statement on some line _after_ the
994 ;; line holding the sexp opener.
995 (goto-char containing-sexp)
997 (if (and (< (point) indent-point)
998 (looking-at "[ \t]*\\(#.*\\)?$"))
1001 (while (and (< (point) indent-point)
1002 (looking-at "[ \t]*\\(#.*\\)?$"))
1003 (setq found-next-line t)
1005 (if (or continued-line
1006 (/= (char-after containing-sexp) ?{)
1009 ;; Line is continuation line, or the sexp opener
1010 ;; is not a curly brace, or we are are looking at
1011 ;; an `expr' expression (which must be split
1012 ;; specially). So indentation is column of first
1013 ;; good spot after sexp opener (with some added
1014 ;; in the continued-line case). If there is no
1015 ;; nonempty line before the indentation point, we
1016 ;; use the column of the character after the sexp
1018 (if (>= (point) indent-point)
1020 (goto-char containing-sexp)
1022 (skip-chars-forward " \t"))
1023 (+ (current-column) continued-indent-value))
1024 ;; After a curly brace, and not a continuation line.
1025 ;; So take indentation from first good line after
1026 ;; start of block, unless that line is on the same
1027 ;; line as the opening brace. In this case use the
1028 ;; indentation of the opening brace's line, plus
1029 ;; another indent step. If we are in the body part
1030 ;; of an "if" or "while" then the indentation is
1031 ;; taken from the line holding the start of the
1033 (if (and (< (point) indent-point)
1035 (current-indentation)
1037 (goto-char expr-start)
1038 (goto-char containing-sexp))
1039 (+ (current-indentation) tcl-indent-level)))))))))
1043 (defun mark-tcl-function ()
1044 "Put mark at end of Tcl function, point at beginning."
1048 (if tcl-using-emacs-19
1049 (push-mark (point) nil t)
1050 (push-mark (point)))
1051 (tcl-beginning-of-defun)
1052 (backward-paragraph))
1056 (defun indent-tcl-exp ()
1057 "Indent each line of the Tcl grouping following point."
1059 (let ((indent-stack (list nil))
1060 (contain-stack (list (point)))
1061 (case-fold-search nil)
1062 outer-loop-done inner-loop-done state ostate
1063 this-indent last-sexp continued-line
1069 (setq outer-loop-done nil)
1070 (while (and (not (eobp)) (not outer-loop-done))
1071 (setq last-depth next-depth)
1072 ;; Compute how depth changes over this line
1073 ;; plus enough other lines to get to one that
1074 ;; does not end inside a comment or string.
1075 ;; Meanwhile, do appropriate indentation on comment lines.
1076 (setq inner-loop-done nil)
1077 (while (and (not inner-loop-done)
1078 (not (and (eobp) (setq outer-loop-done t))))
1080 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
1082 (setq next-depth (car state))
1083 (if (and (car (cdr (cdr state)))
1084 (>= (car (cdr (cdr state))) 0))
1085 (setq last-sexp (car (cdr (cdr state)))))
1086 (if (or (nth 4 ostate))
1088 (if (or (nth 3 state))
1090 (setq inner-loop-done t)))
1091 (if (<= next-depth 0)
1092 (setq outer-loop-done t))
1095 ;; If this line had ..))) (((.. in it, pop out of the levels
1096 ;; that ended anywhere in this line, even if the final depth
1097 ;; doesn't indicate that they ended.
1098 (while (> last-depth (nth 6 state))
1099 (setq indent-stack (cdr indent-stack)
1100 contain-stack (cdr contain-stack)
1101 last-depth (1- last-depth)))
1102 (if (/= last-depth next-depth)
1103 (setq last-sexp nil))
1104 ;; Add levels for any parens that were started in this line.
1105 (while (< last-depth next-depth)
1106 (setq indent-stack (cons nil indent-stack)
1107 contain-stack (cons nil contain-stack)
1108 last-depth (1+ last-depth)))
1109 (if (null (car contain-stack))
1110 (setcar contain-stack
1111 (or (car (cdr state))
1116 (setq continued-line
1119 (= (preceding-char) ?\\)))
1120 (skip-chars-forward " \t")
1123 (if (and (car indent-stack)
1124 (>= (car indent-stack) 0))
1125 ;; Line is on an existing nesting level.
1126 (setq this-indent (car indent-stack))
1127 ;; Just started a new nesting level.
1128 ;; Compute the standard indent for this level.
1129 (let ((val (calculate-tcl-indent
1130 (if (car indent-stack)
1131 (- (car indent-stack))))))
1132 (setcar indent-stack
1133 (setq this-indent val))
1134 (setq continued-line nil)))
1135 (cond ((not (numberp this-indent)))
1136 ((= (following-char) ?})
1137 (setq this-indent (- this-indent tcl-indent-level)))
1138 ((= (following-char) ?\])
1139 (setq this-indent (- this-indent 1))))
1140 ;; Put chosen indentation into effect.
1141 (or (null this-indent)
1144 (+ this-indent tcl-indent-level)
1147 (delete-region (point) (progn (beginning-of-line) (point)))
1150 (+ this-indent tcl-indent-level)
1151 this-indent)))))))))
1157 ;; Interfaces to other packages.
1160 (defun tcl-imenu-create-index-function ()
1161 "Generate alist of indices for imenu."
1162 (let ((re (concat tcl-proc-regexp "\\([^ \t\n{]+\\)"))
1164 (imenu-progress-message 0)
1165 (goto-char (point-min))
1166 (while (re-search-forward re nil t)
1167 (imenu-progress-message nil)
1168 ;; Position on start of proc name, not beginning of line.
1170 (cons (buffer-substring (match-beginning 2) (match-end 2))
1171 (match-beginning 2))
1173 (imenu-progress-message 100)
1176 ;; FIXME Definition of function is very ad-hoc. Should use
1177 ;; tcl-beginning-of-defun. Also has incestuous knowledge about the
1178 ;; format of tcl-proc-regexp.
1179 (defun add-log-tcl-defun ()
1180 "Return name of Tcl function point is in, or nil."
1182 (if (re-search-backward
1183 (concat tcl-proc-regexp "\\([^ \t\n{]+\\)") nil t)
1184 (buffer-substring (match-beginning 2)
1190 ;; Helper functions for inferior Tcl mode.
1193 ;; This exists to let us delete the prompt when commands are sent
1194 ;; directly to the inferior Tcl. See gud.el for an explanation of how
1195 ;; it all works (I took it from there). This stuff doesn't really
1196 ;; work as well as I'd like it to. But I don't believe there is
1197 ;; anything useful that can be done.
1198 (defvar inferior-tcl-delete-prompt-marker nil)
1200 (defun tcl-filter (proc string)
1201 (let ((inhibit-quit t))
1203 (set-buffer (process-buffer proc))
1204 (goto-char (process-mark proc))
1205 ;; Delete prompt if requested.
1206 (if (marker-buffer inferior-tcl-delete-prompt-marker)
1208 (delete-region (point) inferior-tcl-delete-prompt-marker)
1209 (set-marker inferior-tcl-delete-prompt-marker nil)))))
1210 (comint-output-filter proc string))
1212 (defun tcl-send-string (proc string)
1214 (set-buffer (process-buffer proc))
1215 (goto-char (process-mark proc))
1217 (if (looking-at comint-prompt-regexp)
1218 (set-marker inferior-tcl-delete-prompt-marker (point))))
1219 (comint-send-string proc string))
1221 (defun tcl-send-region (proc start end)
1223 (set-buffer (process-buffer proc))
1224 (goto-char (process-mark proc))
1226 (if (looking-at comint-prompt-regexp)
1227 (set-marker inferior-tcl-delete-prompt-marker (point))))
1228 (comint-send-region proc start end))
1230 (defun switch-to-tcl (eob-p)
1231 "Switch to inferior Tcl process buffer.
1232 With argument, positions cursor at end of buffer."
1234 (if (get-buffer inferior-tcl-buffer)
1235 (pop-to-buffer inferior-tcl-buffer)
1236 (error "No current inferior Tcl buffer"))
1239 (goto-char (point-max)))))
1241 (defun inferior-tcl-proc ()
1242 "Return current inferior Tcl process.
1243 See variable `inferior-tcl-buffer'."
1244 (let ((proc (get-buffer-process (if (eq major-mode 'inferior-tcl-mode)
1246 inferior-tcl-buffer))))
1248 (error "No Tcl process; see variable `inferior-tcl-buffer'"))))
1250 (defun tcl-eval-region (start end &optional and-go)
1251 "Send the current region to the inferior Tcl process.
1252 Prefix argument means switch to the Tcl buffer afterwards."
1253 (interactive "r\nP")
1254 (let ((proc (inferior-tcl-proc)))
1255 (tcl-send-region proc start end)
1256 (tcl-send-string proc "\n")
1257 (if and-go (switch-to-tcl t))))
1259 (defun tcl-eval-defun (&optional and-go)
1260 "Send the current defun to the inferior Tcl process.
1261 Prefix argument means switch to the Tcl buffer afterwards."
1265 (let ((end (point)))
1266 (tcl-beginning-of-defun)
1267 (tcl-eval-region (point) end)))
1268 (if and-go (switch-to-tcl t)))
1273 ;; Inferior Tcl mode itself.
1276 (defun inferior-tcl-mode ()
1277 "Major mode for interacting with Tcl interpreter.
1279 A Tcl process can be started with M-x inferior-tcl.
1281 Entry to this mode runs the hooks comint-mode-hook and
1282 inferior-tcl-mode-hook, in that order.
1284 You can send text to the inferior Tcl process from other buffers
1285 containing Tcl source.
1287 Variables controlling Inferior Tcl mode:
1289 Name of program to run.
1290 tcl-command-switches
1291 Command line arguments to `tcl-application'.
1294 inferior-tcl-source-command
1295 Command to use to read Tcl file in running application.
1297 The current inferior Tcl process buffer. See variable
1298 documentation for details on multiple-process support.
1300 The following commands are available:
1301 \\{inferior-tcl-mode-map}"
1304 (setq comint-prompt-regexp (or tcl-prompt-regexp
1306 (regexp-quote tcl-application)
1308 (setq major-mode 'inferior-tcl-mode)
1309 (setq mode-name "Inferior Tcl")
1310 (setq mode-line-process '(": %s"))
1311 (use-local-map inferior-tcl-mode-map)
1312 (setq local-abbrev-table tcl-mode-abbrev-table)
1313 (set-syntax-table tcl-mode-syntax-table)
1314 (if tcl-using-emacs-19
1316 (make-local-variable 'defun-prompt-regexp)
1317 (setq defun-prompt-regexp tcl-omit-ws-regexp)))
1318 (make-local-variable 'inferior-tcl-delete-prompt-marker)
1319 (setq inferior-tcl-delete-prompt-marker (make-marker))
1320 (set-process-filter (get-buffer-process (current-buffer)) 'tcl-filter)
1321 (run-hooks 'inferior-tcl-mode-hook))
1323 (defun inferior-tcl (cmd)
1324 "Run inferior Tcl process.
1325 Prefix arg means enter program name interactively.
1326 See documentation for function `inferior-tcl-mode' for more information."
1328 (list (if current-prefix-arg
1329 (read-string "Run Tcl: " tcl-application)
1331 (if (not (comint-check-proc "*inferior-tcl*"))
1333 (set-buffer (apply (function make-comint) "inferior-tcl" cmd nil
1334 tcl-command-switches))
1335 (inferior-tcl-mode)))
1336 (make-local-variable 'tcl-application)
1337 (setq tcl-application cmd)
1338 (setq inferior-tcl-buffer "*inferior-tcl*")
1339 (switch-to-buffer "*inferior-tcl*"))
1341 (and (fboundp 'defalias)
1342 (defalias 'run-tcl 'inferior-tcl))
1347 ;; Auto-fill support.
1350 (defun tcl-real-command-p ()
1351 "Return nil if point is not at the beginning of a command.
1352 A command is the first word on an otherwise empty line, or the
1353 first word following a semicolon, opening brace, or opening bracket."
1355 (skip-chars-backward " \t")
1360 ;; Note -- continued comments are not supported here. I
1361 ;; consider those to be a wart on the language.
1362 (not (eq ?\\ (preceding-char))))
1364 (memq (preceding-char) '(?\; ?{ ?\[))))))
1366 ;; FIXME doesn't actually return t. See last case.
1367 (defun tcl-real-comment-p ()
1368 "Return t if point is just after the `#' beginning a real comment.
1369 Does not check to see if previous char is actually `#'.
1370 A real comment is either at the beginning of the buffer,
1371 preceeded only by whitespace on the line, or has a preceeding
1372 semicolon, opening brace, or opening bracket on the same line."
1375 (tcl-real-command-p)))
1377 (defun tcl-hairy-scan-for-comment (state end always-stop)
1378 "Determine if point is in a comment.
1379 Returns a list of the form `(FLAG . STATE)'. STATE can be used
1380 as input to future invocations. FLAG is nil if not in comment,
1381 t otherwise. If in comment, leaves point at beginning of comment.
1382 Only works in Emacs 19. See also `tcl-simple-scan-for-comment', a
1383 simpler version that is often right, and works in Emacs 18."
1384 (let ((bol (save-excursion
1390 (while (and (not last-cstart) (< (point) end))
1391 (setq real-comment nil) ;In case we've looped around and it is
1393 (setq state (parse-partial-sexp (point) end nil nil state t))
1396 ;; If ALWAYS-STOP is set, stop even if we don't have a
1397 ;; real comment, or if the comment isn't on the same line
1399 (if always-stop (setq last-cstart (point)))
1400 ;; If we have a real comment, then set the comment
1401 ;; starting point if we are on the same line as the ending
1403 (setq real-comment (tcl-real-comment-p))
1406 (and (> (point) bol) (setq last-cstart (point)))
1407 ;; NOTE Emacs 19 has a misfeature whereby calling
1408 ;; parse-partial-sexp with COMMENTSTOP set and with
1409 ;; an initial list that says point is in a comment
1410 ;; will cause an immediate return. So we must skip
1411 ;; over the comment ourselves.
1412 (beginning-of-line 2)))
1413 ;; Frob the state to make it look like we aren't in a
1415 (setcar (nthcdr 4 state) nil))))
1417 (goto-char last-cstart))
1418 (cons real-comment state)))
1420 (defun tcl-hairy-in-comment ()
1421 "Return t if point is in a comment, and leave point at beginning
1423 (let ((save (point)))
1424 (tcl-beginning-of-defun)
1425 (car (tcl-hairy-scan-for-comment nil save nil))))
1427 (defun tcl-simple-in-comment ()
1428 "Return t if point is in comment, and leave point at beginning
1429 of comment. This is faster that `tcl-hairy-in-comment', but is
1430 correct less often."
1431 (let ((save (point))
1434 (while (and (< (point) save) (not comment))
1435 (search-forward "#" save 'move)
1436 (setq comment (tcl-real-comment-p)))
1439 (defun tcl-in-comment ()
1440 "Return t if point is in comment, and leave point at beginning
1442 (if (and tcl-pps-has-arg-6
1443 tcl-use-hairy-comment-detector)
1444 (tcl-hairy-in-comment)
1445 (tcl-simple-in-comment)))
1447 (defun tcl-do-auto-fill ()
1448 "Auto-fill function for Tcl mode. Only auto-fills in a comment."
1452 (setq in-comment (tcl-in-comment))
1454 (setq col (1- (current-column)))))
1459 (back-to-indentation)
1460 (delete-region (point) (save-excursion
1463 (indent-to-column col))))))
1468 ;; Help-related code.
1471 (defvar tcl-help-saved-dir nil
1472 "Saved help directory. If `tcl-help-directory' changes, this allows
1473 tcl-help-on-word to update the alist")
1475 (defvar tcl-help-alist nil
1476 "Alist with command names as keys and filenames as values.")
1478 (defun tcl-help-snarf-commands (dir)
1479 "Build alist of commands and filenames. There is probably a much
1480 better implementation of this, but I'm too tired to think of it right
1482 (let ((files (directory-files dir t)))
1484 (if (and (file-directory-p (car files))
1486 (let ((fpart (file-name-nondirectory (car files))))
1487 (or (equal fpart ".")
1488 (equal fpart "..")))))
1489 (let ((matches (directory-files (car files) t)))
1491 (or (file-directory-p (car matches))
1492 (setq tcl-help-alist
1494 (cons (file-name-nondirectory (car matches))
1497 (setq matches (cdr matches)))))
1498 (setq files (cdr files)))))
1500 (defun tcl-reread-help-files ()
1501 "Set up to re-read files, and then do it."
1503 (message "Building Tcl help file index...")
1504 (setq tcl-help-saved-dir tcl-help-directory)
1505 (setq tcl-help-alist nil)
1506 (tcl-help-snarf-commands tcl-help-directory)
1507 (message "Building Tcl help file index...done"))
1509 (defun tcl-current-word (flag)
1510 "Return current command word, or nil.
1511 If FLAG is nil, just uses `current-word'.
1512 Otherwise scans backward for most likely Tcl command word."
1513 (if (and flag (eq major-mode 'tcl-mode))
1516 ;; Look backward for first word actually in alist.
1519 (while (and (not (bobp))
1520 (not (tcl-real-command-p)))
1522 (if (assoc (current-word) tcl-help-alist)
1527 (defun tcl-help-on-word (command &optional arg)
1528 "Get help on Tcl command. Default is word at point.
1529 Prefix argument means invert sense of `tcl-use-smart-word-finder'."
1533 (if (not (string= tcl-help-directory tcl-help-saved-dir))
1534 (tcl-reread-help-files))
1535 (let ((word (tcl-current-word
1536 (if current-prefix-arg
1537 (not tcl-use-smart-word-finder)
1538 tcl-use-smart-word-finder))))
1540 (if (or (null word) (string= word ""))
1541 "Help on Tcl command: "
1542 (format "Help on Tcl command (default %s): " word))
1543 tcl-help-alist nil t)))
1544 current-prefix-arg))
1545 (if (not (string= tcl-help-directory tcl-help-saved-dir))
1546 (tcl-reread-help-files))
1547 (if (string= command "")
1548 (setq command (tcl-current-word
1550 (not tcl-use-smart-word-finder)
1551 tcl-use-smart-word-finder))))
1552 (let* ((help (get-buffer-create "*Tcl help*"))
1553 (cell (assoc command tcl-help-alist))
1554 (file (and cell (cdr cell))))
1556 (delete-region (point-min) (point-max))
1559 (insert "*** " command "\n\n")
1560 (insert-file-contents file))
1561 (if (string= command "")
1562 (insert "Magical Pig!")
1563 (insert "Tcl command " command " not in help\n")))
1564 (set-buffer-modified-p nil)
1565 (goto-char (point-min))
1566 (display-buffer help)))
1571 ;; Other interactive stuff.
1574 (defvar tcl-previous-dir/file nil
1575 "Record last directory and file used in loading.
1576 This holds a cons cell of the form `(DIRECTORY . FILE)'
1577 describing the last `tcl-load-file' command.")
1579 (defun tcl-load-file (file &optional and-go)
1580 "Load a Tcl file into the inferior Tcl process.
1581 Prefix argument means switch to the Tcl buffer afterwards."
1584 ;; car because comint-get-source returns a list holding the
1586 (car (comint-get-source "Load Tcl file: " tcl-previous-dir/file
1588 current-prefix-arg))
1589 (comint-check-source file)
1590 (setq tcl-previous-dir/file (cons (file-name-directory file)
1591 (file-name-nondirectory file)))
1592 (tcl-send-string (inferior-tcl-proc)
1593 (format inferior-tcl-source-command (tcl-quote file)))
1594 (if and-go (switch-to-tcl t)))
1596 ;; Maybe this should work just like tcl-load-file. But I think what
1597 ;; I've implemented will turn out to be more useful.
1598 (defun tcl-restart-with-file (file &optional and-go)
1599 "Restart inferior Tcl with file.
1600 If an inferior Tcl process exists, it is killed first.
1601 Prefix argument means switch to the Tcl buffer afterwards."
1604 (car (comint-get-source "Restart with Tcl file: "
1606 (eq major-mode 'tcl-mode)
1608 tcl-previous-dir/file)
1610 current-prefix-arg))
1611 (let* ((buf (if (eq major-mode 'inferior-tcl-mode)
1613 inferior-tcl-buffer))
1614 (proc (and buf (get-process buf))))
1616 ((not (and buf (get-buffer buf)))
1617 ;; I think this will be ok.
1618 (inferior-tcl tcl-application)
1619 (tcl-load-file file and-go))
1621 (not (comint-check-proc buf))
1623 "A Tcl process is running, are you sure you want to reset it? "))
1625 (comint-check-source file)
1626 (setq tcl-previous-dir/file (cons (file-name-directory file)
1627 (file-name-nondirectory file)))
1628 (comint-exec (get-buffer-create buf)
1632 tcl-application file tcl-command-switches)
1633 (if and-go (switch-to-tcl t)))))))
1635 ;; FIXME I imagine you can do this under Emacs 18. I just don't know
1637 (defun tcl-auto-fill-mode (&optional arg)
1638 "Like `auto-fill-mode', but controls filling of Tcl comments."
1640 (and (not tcl-using-emacs-19)
1641 (error "You must use Emacs 19 to get this feature."))
1642 ;; Following code taken from "auto-fill-mode" (simple.el).
1644 (setq auto-fill-function
1646 (not auto-fill-function)
1647 (> (prefix-numeric-value arg) 0))
1650 ;; Update mode line. FIXME I'd use force-mode-line-update, but I
1651 ;; don't know if it exists in v18.
1652 (set-buffer-modified-p (buffer-modified-p))))
1654 (defun tcl-electric-hash (&optional count)
1655 "Insert a `#' and quote if it does not start a real comment.
1656 Prefix arg is number of `#'s to insert.
1657 See variable `tcl-electric-hash-style' for description of quoting
1660 (or count (setq count 1))
1663 (if (eq tcl-electric-hash-style 'smart)
1664 (if (> count 3) ; FIXME what is "smart"?
1667 tcl-electric-hash-style))
1673 (setq comment (tcl-in-comment)))
1675 (and tcl-explain-indentation (message "comment: %s" comment))
1680 ((eq type 'backslash)
1681 ;; The following will set count to 0, so the
1682 ;; insert-char can still be run.
1686 (setq count (1- count)))))
1688 (insert-char ?# count))))
1690 (defun tcl-hashify-buffer ()
1691 "Quote all `#'s in current buffer that aren't Tcl comments."
1694 (goto-char (point-min))
1695 (if (and tcl-pps-has-arg-6 tcl-use-hairy-comment-detector)
1698 (while (< (point) (point-max))
1699 (setq result (tcl-hairy-scan-for-comment state (point-max) t))
1701 (beginning-of-line 2)
1703 (if (eq ?# (following-char))
1706 (setq state (cdr result))))
1707 (while (and (< (point) (point-max))
1708 (search-forward "#" nil 'move))
1709 (if (tcl-real-comment-p)
1710 (beginning-of-line 2)
1711 ;; There's really no good way for the simple converter to
1712 ;; work. So we just quote # if it isn't already quoted.
1713 ;; Bogus, but it works.
1715 (if (not (eq ?\\ (preceding-char)))
1719 ;; The following was inspired by the Tcl editing mode written by
1720 ;; Gregor Schmid <schmid@fb3-s7.math.TU-Berlin.DE>. His version also
1721 ;; attempts to snarf the command line options from the command line,
1722 ;; but I didn't think that would really be that helpful (doesn't seem
1723 ;; like it owould be right enough. His version also looks for the
1724 ;; "#!/bin/csh ... exec" hack, but that seemed even less useful.
1725 (defun tcl-guess-application ()
1726 "Attempt to guess Tcl application by looking at first line.
1727 The first line is assumed to look like \"#!.../program ...\"."
1729 (goto-char (point-min))
1730 (if (looking-at "#![^ \t]*/\\([^ \t/]+\\)\\([ \t]\\|$\\)")
1732 (make-local-variable 'tcl-application)
1733 (setq tcl-application (buffer-substring (match-beginning 1)
1736 ;; This only exists to put on the menubar. I couldn't figure out any
1737 ;; other way to do it. FIXME should take "number of #-marks"
1739 (defun tcl-uncomment-region (beg end)
1742 (comment-region beg end -1))
1747 ;; Lucid menu support.
1748 ;; Taken from schmid@fb3-s7.math.TU-Berlin.DE (Gregor Schmid),
1749 ;; who wrote a different Tcl mode.
1750 ;; We also have simple support for menus in FSF. We do this by
1751 ;; loading the Lucid menu emulation code.
1754 ;; Put this into your tcl-mode-hook.
1755 (defun tcl-install-menubar ()
1756 (and tcl-using-emacs-19
1757 (not tcl-using-lemacs-19)
1758 (if tcl-using-emacs-19.23
1761 ;; * lmenu.el provides 'menubar, which is bogus.
1762 ;; * lmenu.el causes menubars to be turned on everywhere.
1764 ;; Both of these problems are fixed in Emacs 19.23. People
1765 ;; using an Emacs before that just suffer.
1766 (require 'menubar "lmenu")))
1767 (if (not (assoc "Tcl" current-menubar))
1769 (set-buffer-menubar (copy-sequence current-menubar))
1770 (add-menu nil "Tcl" (cdr tcl-lucid-menu))))
1771 ;; You might want to do something like the below. I have it
1772 ;; commented out because it overrides existing bindings.
1774 ;; (define-key tcl-mode-map 'button3 'tcl-popup-menu)
1776 ;; (define-key tcl-mode-map [down-mouse-3] 'tcl-popup-menu)
1779 (defun tcl-popup-menu (e)
1781 (and tcl-using-emacs-19
1782 (not tcl-using-lemacs-19)
1783 (if tcl-using-emacs-19.23
1786 ;; * lmenu.el provides 'menubar, which is bogus.
1787 ;; * lmenu.el causes menubars to be turned on everywhere.
1789 ;; Both of these problems are fixed in Emacs 19.23. People
1790 ;; using an Emacs before that just suffer.
1791 (require 'menubar "lmenu"))) ;; This is annoying
1792 ;;(mouse-set-point e)
1793 ;; IMHO popup-menu should be autoloaded. Oh well.
1794 (popup-menu tcl-lucid-menu))
1799 ;; Quoting and unquoting functions.
1802 ;; This quoting is sufficient to protect eg a filename from any sort
1803 ;; of expansion or splitting. Tcl quoting sure sucks.
1804 (defun tcl-quote (string)
1805 "Quote STRING according to Tcl rules."
1806 (mapconcat (function (lambda (char)
1807 (if (memq char '(?[ ?] ?{ ?} ?\\ ?\" ?$ ? ?\;))
1808 (concat "\\" (char-to-string char))
1809 (char-to-string char))))
1816 ;;; tcl.el ends here