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