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