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