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