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