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