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