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