;;; tcl.el --- Tcl code editing commands for Emacs;; Copyright (C) 1994, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.;; Maintainer: FSF;; Author: Tom Tromey <tromey@busco.lanl.gov>;; Chris Lindblad <cjl@lcs.mit.edu>;; Keywords: languages tcl modes;; Version: Revision: 1.64.4.1;; This file is part of XEmacs.;; XEmacs is free software; you can redistribute it and/or modify;; it under the terms of the GNU General Public License as published by;; the Free Software Foundation; either version 2, or (at your option);; any later version.;; XEmacs is distributed in the hope that it will be useful,;; but WITHOUT ANY WARRANTY; without even the implied warranty of;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the;; GNU General Public License for more details.;; You should have received a copy of the GNU General Public License;; along with XEmacs; see the file COPYING. If not, write to the;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,;; Boston, MA 02111-1307, USA.;;; Synched up with: Mostly with GNU Emacs 21.2. The differences are a;;; couple of defcustom<->defvars, we use `tcl-imenu-generic-expression';;; instead of `tcl-imenu-create-index-function' and some typo fixes.;; BEFORE USE:;;;; If you plan to use the interface to the TclX help files, you must;; set the variable tcl-help-directory-list to point to the topmost;; directories containing the TclX help files. Eg:;;;; (setq tcl-help-directory-list '("/usr/local/lib/tclx/help"));;;;; Commentary:;; CUSTOMIZATION NOTES:;; * tcl-proc-list can be used to customize a list of things that;; "define" other things. Eg in my project I put "defvar" in this;; list.;; * tcl-typeword-list is similar, but uses font-lock-type-face.;; * tcl-keyword-list is a list of keywords. I've generally used this;; for flow-control words. Eg I add "unwind_protect" to this list.;; * tcl-type-alist can be used to minimally customize indentation;; according to context.;; THANKS FOR CRITICISM AND SUGGESTIONS TO:;; Guido Bosch <Guido.Bosch@loria.fr>;; pgs1002@esc.cam.ac.uk (Dr P.G. Sjoerdsma);; Mike Scheidler <c23mts@kocrsv01.delcoelect.com>;; Matt Newman <men@charney.colorado.edu>;; rwhitby@research.canon.oz.au (Rod Whitby);; h9118101@hkuxa.hku.hk (Yip Chi Lap [Beta]);; Pertti Tapio Kasanen <ptk@delta.hut.fi>;; schmid@fb3-s7.math.TU-Berlin.DE (Gregor Schmid);; warsaw@nlm.nih.gov (Barry A. Warsaw);; Carl Witty <cwitty@ai.mit.edu>;; T. V. Raman <raman@crl.dec.com>;; Jesper Pedersen <blackie@imada.ou.dk>;; dfarmer@evolving.com (Doug Farmer);; "Chris Alfeld" <calfeld@math.utah.edu>;; Ben Wing <ben@xemacs.org>;; KNOWN BUGS:;; * In Tcl "#" is not always a comment character. This can confuse;; tcl.el in certain circumstances. For now the only workaround is;; to enclose offending hash characters in quotes or precede it with;; a backslash. Note that using braces won't work -- quotes change;; the syntax class of characters between them, while braces do not.;; The electric-# mode helps alleviate this problem somewhat.;; * indent-tcl-exp is untested.;; TODO:;; * make add-log-tcl-defun smarter. should notice if we are in the;; middle of a defun, or between defuns. should notice if point is;; on first line of defun (or maybe even in comments before defun).;; * Allow continuation lines to be indented under the first argument;; of the preceding line, like this:;; [list something \;; something-else];; * There is a request that indentation work like this:;; button .fred -label Fred \;; -command {puts fred};; * Should have tcl-complete-symbol that queries the inferior process.;; * Should have describe-symbol that works by sending the magic;; command to a tclX process.;; * Need C-x C-e binding (tcl-eval-last-exp).;; * Write indent-region function that is faster than indenting each;; line individually.;; * tcl-figure-type should stop at "beginning of line" (only ws;; before point, and no "\" on previous line). (see tcl-real-command-p).;; * overrides some comint keybindings; fix.;; * Trailing \ will eat blank lines. Should deal with this.;; (this would help catch some potential bugs).;; * Inferior should display in half the screen, not the whole screen.;; * Indentation should deal with "switch".;; * Consider writing code to find help files automatically (for;; common cases).;; * `#' shouldn't insert `\#' when point is in string.;;; Code:(eval-when-compile(require'imenu)(require'outline)(require'dabbrev)(require'add-log));; I sure wish Emacs had a package that made it easy to extract this;; sort of information. Strange definition works with XEmacs 20.0.(defconsttcl-using-emacs-19(not(string-match"18\\."emacs-version))"Non-nil if using Emacs 19 or later.")(defconsttcl-using-emacs-19-23(or(string-match"19\\.\\(2[3-9]\\|[3-9][0-9]\\)"emacs-version)(string-match"^[2-9][0-9]\\."emacs-version))"Non-nil if using Emacs 19-23 or later.")(defconsttcl-using-xemacs-19(string-match"XEmacs"emacs-version)"Non-nil if using XEmacs.")(require'comint);; When compiling under Emacs, load imenu during compilation. If;; you have 19.22 or earlier, comment this out, or get imenu.(and(fboundp'eval-when-compile)(eval-when-compile(if(and(not(string<emacs-version"19.23"))(not(string-match"XEmacs"emacs-version)))(require'imenu))()))(defconsttcl-version"Revision: 1.64.4.1 ")(defconsttcl-maintainer"Tom Tromey <tromey@drip.colorado.edu>");;;; User variables.;;(defgrouptclnil"Major mode for editing Tcl source in Emacs":group'languages)(defcustomtcl-indent-level4"*Indentation of Tcl statements with respect to containing block.":group'tcl:type'integer)(defcustomtcl-continued-indent-level4"*Indentation of continuation line relative to first line of command.":group'tcl:type'integer)(defcustomtcl-auto-newlinenil"*Non-nil means automatically newline before and after braces you insert.":group'tcl:type'boolean)(defcustomtcl-tab-always-indentt"*Control effect of TAB key.If t (the default), always indent current line.If nil and point is not in the indentation area at the beginning ofthe line, a TAB is inserted.Other values cause the first possible action from the following listto take place: 1. Move from beginning of line to correct indentation. 2. Delete an empty comment. 3. Move forward to start of comment, indenting if necessary. 4. Move forward to end of line, indenting if necessary. 5. Create an empty comment. 6. Move backward to start of comment, indenting if necessary.":group'tcl:type'(choice(const:tag"Always"t)(const:tag"Beginning only"nil)(const:tag"Maybe move or make or delete comment"'tcl)))(defcustomtcl-use-hairy-comment-detectort"*If not nil, use the more sophisticated, but slower, comment-delete method.This variable is not effective in Emacs 18;the fast function is always used in that version.":group'tcl:type'boolean)(defcustomtcl-electric-hash-style'smart"*Style of electric hash insertion to use.Possible values are `backslash', meaning that `\\' quoting should bedone; `quote', meaning that `\"' quoting should be done; `smart',meaning that the choice between `backslash' and `quote' should bemade depending on the number of hashes inserted; or nil, meaning thatno quoting should be done. Any other value for this variable istaken to mean `smart'. The default is `smart'.":group'tcl:type'(choice(constbackslash)(constquote)(constsmart)(constnil)))(defcustomtcl-help-directory-listnil"*List of topmost directories containing TclX help files.":group'tcl:type'(repeatdirectory))(defcustomtcl-use-smart-word-findert"*If not nil, use smart way to find current word, for Tcl help feature.":group'tcl:type'boolean)(defcustomtcl-application"wish""*Name of Tcl program to run in inferior Tcl mode.":group'tcl:type'string)(defcustomtcl-command-switchesnil"*List of switches to supply to the `tcl-application' program.":group'tcl:type'(repeatstring))(defcustomtcl-prompt-regexp"^\\(% \\|\\)""*If not nil, a regexp that will match the prompt in the inferior process.If nil, the prompt is the name of the application with \">\" appended.The default is \"^\\(% \\|\\)\", which will match the default primaryand secondary prompts for tclsh and wish.":group'tcl:type'regexp)(defcustominferior-tcl-source-command"source %s\n""*Format-string for building a Tcl command to load a file.This format string should use `%s' to substitute a file nameand should result in a Tcl expression that will command theinferior Tcl to load that file. The filename will be appropriatelyquoted for Tcl.":group'tcl:type'string);;;; Keymaps, abbrevs, syntax tables.;;(defvartcl-mode-abbrev-tablenil"Abbrev table in use in Tcl-mode buffers.")(iftcl-mode-abbrev-table()(define-abbrev-table'tcl-mode-abbrev-table()))(defvartcl-mode-map()"Keymap used in Tcl mode.")(defvartcl-mode-syntax-tablenil"Syntax table in use in Tcl-mode buffers.")(iftcl-mode-syntax-table()(setqtcl-mode-syntax-table(make-syntax-table))(modify-syntax-entry?%"_"tcl-mode-syntax-table)(modify-syntax-entry?@"_"tcl-mode-syntax-table)(modify-syntax-entry?&"_"tcl-mode-syntax-table)(modify-syntax-entry?*"_"tcl-mode-syntax-table)(modify-syntax-entry?+"_"tcl-mode-syntax-table)(modify-syntax-entry?-"_"tcl-mode-syntax-table)(modify-syntax-entry?."_"tcl-mode-syntax-table)(modify-syntax-entry?:"_"tcl-mode-syntax-table)(modify-syntax-entry?!"_"tcl-mode-syntax-table)(modify-syntax-entry?$"_"tcl-mode-syntax-table); FIXME use "'"?(modify-syntax-entry?/"_"tcl-mode-syntax-table)(modify-syntax-entry?~"_"tcl-mode-syntax-table)(modify-syntax-entry?<"_"tcl-mode-syntax-table)(modify-syntax-entry?="_"tcl-mode-syntax-table)(modify-syntax-entry?>"_"tcl-mode-syntax-table)(modify-syntax-entry?| "_" tcl-mode-syntax-table) (modify-syntax-entry ?\( "()" tcl-mode-syntax-table) (modify-syntax-entry ?\) ")(" tcl-mode-syntax-table) (modify-syntax-entry ?\; "." tcl-mode-syntax-table) (modify-syntax-entry ?\n "> " tcl-mode-syntax-table) (modify-syntax-entry ?\f "> " tcl-mode-syntax-table) (modify-syntax-entry ?# "< " tcl-mode-syntax-table))(defvar inferior-tcl-mode-map nil "Keymap used in Inferior Tcl mode.");; XEmacs menu.(defvar tcl-xemacs-menu '(["Beginning of function" tcl-beginning-of-defun t] ["End of function" tcl-end-of-defun t] ["Mark function" tcl-mark-defun t] ["Indent region" indent-region (tcl-mark)] ["Comment region" comment-region (tcl-mark)] ["Uncomment region" tcl-uncomment-region (tcl-mark)] "----" ["Show Tcl process buffer" inferior-tcl t] ["Send function to Tcl process" tcl-eval-defun (and inferior-tcl-buffer (get-buffer inferior-tcl-buffer))] ["Send region to Tcl process" tcl-eval-region (and inferior-tcl-buffer (get-buffer inferior-tcl-buffer))] ["Send file to Tcl process" tcl-load-file (and inferior-tcl-buffer (get-buffer inferior-tcl-buffer))] ["Restart Tcl process with file" tcl-restart-with-file t] "----" ["Tcl help" tcl-help-on-word tcl-help-directory-list] ["Send bug report" tcl-submit-bug-report t]) "XEmacs menu for Tcl mode.");; Emacs does menus via keymaps. Do it in a function in case we;; later decide to add it to inferior Tcl mode as well.(defun tcl-add-emacs-menu (map) (define-key map [menu-bar] (make-sparse-keymap "Tcl")) ;; This fails in Emacs 19.22 and earlier. (require 'lmenu) (let ((menu (make-lucid-menu-keymap "Tcl" tcl-xemacs-menu))) (define-key map [menu-bar tcl] (cons "Tcl" menu)) ;; The following is intended to compute the key sequence ;; information for the menu. It doesn't work. (x-popup-menu nil menu)))(defun tcl-fill-mode-map () (define-key tcl-mode-map "{" 'tcl-electric-char) (define-key tcl-mode-map "}" 'tcl-electric-brace) (define-key tcl-mode-map "[" 'tcl-electric-char) (define-key tcl-mode-map "]" 'tcl-electric-char) (define-key tcl-mode-map ";" 'tcl-electric-char) (define-key tcl-mode-map "#" 'tcl-electric-hash) ;; FIXME. (define-key tcl-mode-map "\e\C-a" 'tcl-beginning-of-defun) ;; FIXME. (define-key tcl-mode-map "\e\C-e" 'tcl-end-of-defun) ;; FIXME. (define-key tcl-mode-map "\e\C-h" 'tcl-mark-defun) (define-key tcl-mode-map "\e\C-q" 'indent-tcl-exp) (define-key tcl-mode-map "\177" 'backward-delete-char-untabify) (define-key tcl-mode-map "\t" 'tcl-indent-command) (define-key tcl-mode-map "\M-;" 'tcl-indent-for-comment) (define-key tcl-mode-map "\M-\C-x" 'tcl-eval-defun) (define-key tcl-mode-map "\C-c\C-b" 'tcl-submit-bug-report) (and (fboundp 'comment-region) (define-key tcl-mode-map "\C-c\C-c" 'comment-region)) (define-key tcl-mode-map "\C-c\C-i" 'tcl-help-on-word) (define-key tcl-mode-map "\C-c\C-v" 'tcl-eval-defun) (define-key tcl-mode-map "\C-c\C-f" 'tcl-load-file) (define-key tcl-mode-map "\C-c\C-t" 'inferior-tcl) (define-key tcl-mode-map "\C-c\C-x" 'tcl-eval-region) (define-key tcl-mode-map "\C-c\C-s" 'switch-to-tcl) ;; Make menus. (if (and tcl-using-emacs-19 (not tcl-using-xemacs-19)) (progn (tcl-add-emacs-menu tcl-mode-map))))(defun tcl-fill-inferior-map () (define-key inferior-tcl-mode-map "\t" 'comint-dynamic-complete) (define-key inferior-tcl-mode-map "\M-?" 'comint-dynamic-list-filename-completions) (define-key inferior-tcl-mode-map "\e\C-a" 'tcl-beginning-of-defun) (define-key inferior-tcl-mode-map "\e\C-e" 'tcl-end-of-defun) (define-key inferior-tcl-mode-map "\177" 'backward-delete-char-untabify) (define-key inferior-tcl-mode-map "\M-\C-x" 'tcl-eval-defun) (define-key inferior-tcl-mode-map "\C-c\C-b" 'tcl-submit-bug-report) (define-key inferior-tcl-mode-map "\C-c\C-i" 'tcl-help-on-word) (define-key inferior-tcl-mode-map "\C-c\C-v" 'tcl-eval-defun) (define-key inferior-tcl-mode-map "\C-c\C-f" 'tcl-load-file) (define-key inferior-tcl-mode-map "\C-c\C-t" 'inferior-tcl) (define-key inferior-tcl-mode-map "\C-c\C-x" 'tcl-eval-region) (define-key inferior-tcl-mode-map "\C-c\C-s" 'switch-to-tcl))(if tcl-mode-map () (setq tcl-mode-map (make-sparse-keymap)) (tcl-fill-mode-map))(if inferior-tcl-mode-map () ;; FIXME Use keymap inheritance here? FIXME we override comint ;; keybindings here. Maybe someone has a better set? (setq inferior-tcl-mode-map (copy-keymap comint-mode-map)) (tcl-fill-inferior-map))(defvar inferior-tcl-buffer nil "*The current inferior-tcl process buffer.MULTIPLE PROCESS SUPPORT===========================================================================To run multiple Tcl processes, you start the first up with\\[inferior-tcl]. It will be in a buffer named `*inferior-tcl*'.Rename this buffer with \\[rename-buffer]. You may now start up a newprocess with another \\[inferior-tcl]. It will be in a new buffer,named `*inferior-tcl*'. You can switch between the different processbuffers with \\[switch-to-buffer].Commands that send text from source buffers to Tcl processes -- like`tcl-eval-defun' or `tcl-load-file' -- have to choose a process tosend to, when you have more than one Tcl process around. This isdetermined by the global variable `inferior-tcl-buffer'. Suppose youhave three inferior Lisps running: Buffer Process foo inferior-tcl bar inferior-tcl<2> *inferior-tcl* inferior-tcl<3>If you do a \\[tcl-eval-defun] command on some Lisp source code, whatprocess do you send it to?- If you're in a process buffer (foo, bar, or *inferior-tcl*), you send it to that process.- If you're in some other buffer (e.g., a source file), you send it to the process attached to buffer `inferior-tcl-buffer'.This process selection is performed by function `inferior-tcl-proc'.Whenever \\[inferior-tcl] fires up a new process, it resets`inferior-tcl-buffer' to be the new process's buffer. If you only runone process, this does the right thing. If you run multipleprocesses, you can change `inferior-tcl-buffer' to another processbuffer with \\[set-variable].");;;; Hooks and other customization.;;(defcustom tcl-mode-hook nil "Hook run on entry to Tcl mode.Several functions exist which are useful to run from your`tcl-mode-hook' (see each function's documentation for moreinformation): `tcl-guess-application' Guesses a default setting for `tcl-application' based on any \"#!\" line at the top of the file. `tcl-hashify-buffer' Quotes all \"#\" characters that don't correspond to actual Tcl comments. (Useful when editing code not originally created with this mode). `tcl-auto-fill-mode' Auto-filling of Tcl comments.Add functions to the hook with `add-hook': (add-hook 'tcl-mode-hook 'tcl-guess-application)Emacs 18 users must use `setq' instead: (setq tcl-mode-hook (cons 'tcl-guess-application tcl-mode-hook))" :type 'hook :group 'tcl)(defcustom inferior-tcl-mode-hook nil "Hook for customizing Inferior Tcl mode." :type 'hook :group 'tcl)(defvar tcl-proc-list '("proc" "method" "itcl_class" "body" "configbody" "class") "List of commands whose first argument defines something.This exists because some people (eg, me) use `defvar' et al.Call `tcl-set-proc-regexp' and `tcl-set-font-lock-keywords'after changing this list.")(defvar tcl-proc-regexp nil "Regexp to use when matching proc headers.")(defvar tcl-typeword-list '("global" "upvar" "inherit" "public" "protected" "private" "common" "itk_option" "variable") "List of Tcl keywords denoting \"type\". Used only for highlighting.Call `tcl-set-font-lock-keywords' after changing this list.");; Generally I've picked control operators to be keywords.(defvar tcl-keyword-list '("if" "then" "else" "elseif" "for" "foreach" "break" "continue" "while" "eval" "case" "in" "switch" "default" "exit" "error" "proc" "return" "uplevel" "constructor" "destructor" "itcl_class" "loop" "for_array_keys" "for_recursive_glob" "for_file" "method" "body" "configbody" "class") "List of Tcl keywords. Used only for highlighting.Default list includes some TclX keywords.Call `tcl-set-font-lock-keywords' after changing this list.")(defvar tcl-font-lock-keywords nil "Keywords to highlight for Tcl. See variable `font-lock-keywords'.This variable is generally set from `tcl-proc-regexp',`tcl-typeword-list', and `tcl-keyword-list' by the function`tcl-set-font-lock-keywords'.");; FIXME need some way to recognize variables because array refs look;; like 2 sexps.(defvar tcl-type-alist '( ("proc" nil tcl-expr tcl-commands) ("method" nil tcl-expr tcl-commands) ("destructor" tcl-commands) ("constructor" tcl-commands) ("expr" tcl-expr) ("catch" tcl-commands) ("if" tcl-expr "then" tcl-commands) ("elseif" tcl-expr "then" tcl-commands) ("elseif" tcl-expr tcl-commands) ("if" tcl-expr tcl-commands) ("while" tcl-expr tcl-commands) ("for" tcl-commands tcl-expr tcl-commands tcl-commands) ("foreach" nil nil tcl-commands) ("for_file" nil nil tcl-commands) ("for_array_keys" nil nil tcl-commands) ("for_recursive_glob" nil nil nil tcl-commands) ;; Loop handling is not perfect, because the third argument can be ;; either a command or an expr, and there is no real way to look ;; forward. ("loop" nil tcl-expr tcl-expr tcl-commands) ("loop" nil tcl-expr tcl-commands) ) "Alist that controls indentation.\(Actually, this really only controls what happens on continuation lines).Each entry looks like `(KEYWORD TYPE ...)'.Each type entry describes a sexp after the keyword, and can be one of:* nil, meaning that this sexp has no particular type.* tcl-expr, meaning that this sexp is an arithmetic expression.* tcl-commands, meaning that this sexp holds Tcl commands.* a string, which must exactly match the string at the corresponding position for a match to be made.For example, the entry for the \"loop\" command is: (\"loop\" nil tcl-expr tcl-commands)This means that the \"loop\" command has three arguments. The firstargument is ignored (for indentation purposes). The second argumentis a Tcl expression, and the last argument is Tcl commands.")(defvar tcl-explain-indentation nil "If not `nil', debugging message will be printed during indentation.");;;; Work around differences between various versions of Emacs.;;(defconst tcl-pps-has-arg-6 (or tcl-using-emacs-19 (and tcl-using-xemacs-19 (condition-case nil (progn (parse-partial-sexp (point) (point) nil nil nil t) t) (error nil)))) "t if Emacs supports COMMENTSTOP argument to `parse-partial-sexp'.");; Its pretty bogus to have to do this, but there is no easier way to;; say "match not syntax-1 and not syntax-2". Too bad you can't put;; \s in [...]. This sickness is used in Emacs 19 to match a defun;; starter. (It is used for this in v18 as well).;;(defconst tcl-omit-ws-regexp;; (concat "^\\(\\s";; (mapconcat 'char-to-string "w_.()\"\\$'/" "\\|\\s");; "\\)\\S(*");; "Regularexpressionthatmatcheseverythingexceptspace,comment;;starter, and comment ender syntax codes.");; FIXME? Instead of using the hairy regexp above, we just use a;; simple one.;;(defconst tcl-omit-ws-regexp "^[^] \t\n#}]\\S(*";; "Regular expression used in locating function definitions.");; Here's another stab. I think this one actually works. Now the;; problem seems to be that there is a bug in Emacs 19.22 where;; end-of-defun doesn't really use the brace matching the one that;; trails defun-prompt-regexp.;; ?? Is there a bug now ??(defconsttcl-omit-ws-regexp"^[^ \t\n#}][^\n}]+}*[ \t]+")(defuntcl-internal-beginning-of-defun(&optionalarg)"Move backward to next beginning of defun.With argument, do this that many times.Returns t unless search stops due to end of buffer."(interactive"p")(if(or(nullarg)(=arg0))(setqarg1))(let(success)(while(progn(setqarg(1-arg))(and(>=arg0)(setqsuccess(re-search-backwardtcl-omit-ws-regexpnil'move1))))(while(and(looking-at"[]#}]")(setqsuccess(re-search-backwardtcl-omit-ws-regexpnil'move1)))))(beginning-of-line)(not(nullsuccess))))(defuntcl-internal-end-of-defun(&optionalarg)"Move forward to next end of defun.An end of a defun is found by moving forward from the beginning of one."(interactive"p")(if(or(nullarg)(=arg0))(setqarg1))(let((start(point)));; Was forward-char. I think this works a little better.(forward-line)(tcl-beginning-of-defun)(while(>arg0)(while(and(re-search-forwardtcl-omit-ws-regexpnil'move1)(progn(beginning-of-line)t)(looking-at"[]#}]")(progn(forward-line)t)))(let((next-line(save-excursion(forward-line)(point))))(while(<(point)next-line)(forward-sexp)))(forward-line)(if(>(point)start)(setqarg(1-arg))))));; We can now use begining-of-defun as long as we set up a;; certain regexp. In Emacs 18, we need our own function.(defalias'tcl-beginning-of-defun(iftcl-using-emacs-19'beginning-of-defun'tcl-internal-beginning-of-defun));; Ditto end-of-defun.(defalias'tcl-end-of-defun(if(andtcl-using-emacs-19(nottcl-using-xemacs-19))'end-of-defun'tcl-internal-end-of-defun));; Internal mark-defun that is used for losing Emacsen.(defuntcl-internal-mark-defun()"Put mark at end of Tcl function, point at beginning."(interactive)(push-mark(point))(tcl-end-of-defun)(iftcl-using-emacs-19(push-mark(point)nilt)(push-mark(point)))(tcl-beginning-of-defun)(backward-paragraph));; In Emacs 19.23 and later, mark-defun works as advertised. I;; don't know about XEmacs, so for now it and Emacs 18 just lose.(fset'tcl-mark-defun(iftcl-using-emacs-19-23'mark-defun'tcl-internal-mark-defun));; In Emacs 19, mark takes an additional "force" argument. I;; don't know about XEmacs, so I'm just assuming it is the same.;; Emacs 18 doesn't have this argument.(defuntcl-mark()"Return mark, or nil if none."(iftcl-using-emacs-19(markt)(mark)));;;; Some helper functions.;;(defuntcl-set-proc-regexp()"Set `tcl-proc-regexp' from variable `tcl-proc-list'."(setqtcl-proc-regexp(concat"^\\s-*\\("(mapconcat'identitytcl-proc-list"\\|")"\\)[ \t]+")))(defuntcl-set-font-lock-keywords()"Set `tcl-font-lock-keywords'.Uses variables `tcl-proc-regexp' and `tcl-keyword-list'."(setqtcl-font-lock-keywords(list;; Names of functions (and other "defining things").(list(concattcl-proc-regexp"\\([^ \t\n]+\\)")2'font-lock-function-name-face);; Names of type-defining things.(list(concat"\\(\\s-\\|^\\)\\(";; FIXME Use 'regexp-quote?(mapconcat'identitytcl-typeword-list"\\|")"\\)\\(\\s-\\|$\\)")2'font-lock-type-face);; Keywords. Only recognized if surrounded by whitespace.;; FIXME consider using "not word or symbol", not;; "whitespace".(cons(concat"\\(\\s-\\|^\\)\\(";; FIXME Use regexp-quote? (mapconcat'identitytcl-keyword-list"\\|")"\\)\\(\\s-\\|$\\)")2))))(iftcl-proc-regexp()(tcl-set-proc-regexp))(iftcl-font-lock-keywords()(tcl-set-font-lock-keywords));; XEmacs change.(defvartcl-imenu-generic-expression'((nil"^proc[ \t]+\\([-A-Za-z0-9_:+*]+\\)"1))"Imenu generic expression for `tcl-mode'. See `imenu-generic-expression'.");;;; The mode itself.;;;;;###autoload(defuntcl-mode()"Major mode for editing Tcl code.Expression and list commands understand all Tcl brackets.Tab indents for Tcl code.Paragraphs are separated by blank lines only.Delete converts tabs to spaces as it moves back.Variables controlling indentation style: `tcl-indent-level' Indentation of Tcl statements within surrounding block. `tcl-continued-indent-level' Indentation of continuation line relative to first line of command.Variables controlling user interaction with mode (see variabledocumentation for details): `tcl-tab-always-indent' Controls action of TAB key. `tcl-auto-newline' Non-nil means automatically newline before and after braces, brackets, and semicolons inserted in Tcl code. `tcl-electric-hash-style' Controls action of `#' key. `tcl-use-hairy-comment-detector' If t, use more complicated, but slower, comment detector. This variable is only used in Emacs 19. `tcl-use-smart-word-finder' If not nil, use a smarter, Tcl-specific way to find the current word when looking up help on a Tcl command.Turning on Tcl mode calls the value of the variable `tcl-mode-hook'with no args, if that value is non-nil. Read the documentation for`tcl-mode-hook' to see what kinds of interesting hook functionsalready exist.Commands:\\{tcl-mode-map}"(interactive)(kill-all-local-variables)(use-local-maptcl-mode-map)(setqmajor-mode'tcl-mode)(setqmode-name"Tcl")(setqlocal-abbrev-tabletcl-mode-abbrev-table)(set-syntax-tabletcl-mode-syntax-table)(make-local-variable'paragraph-start)(make-local-variable'paragraph-separate)(if(andtcl-using-emacs-19-23(>=emacs-minor-version29))(progn;; In Emacs 19.29, you aren't supposed to start these with a ^.(setqparagraph-start"$\\|")(setqparagraph-separateparagraph-start))(setqparagraph-start(concat"^$\\|"page-delimiter))(setqparagraph-separateparagraph-start))(make-local-variable'paragraph-ignore-fill-prefix)(setqparagraph-ignore-fill-prefixt);; XEmacs change: check for filladapt-mode.(if(not(and(boundp'filladapt-mode)filladapt-mode))(progn(make-local-variable'fill-paragraph-function)(setqfill-paragraph-function'tcl-do-fill-paragraph)))(make-local-variable'indent-line-function)(setqindent-line-function'tcl-indent-line);; Tcl doesn't require a final newline.;; (make-local-variable 'require-final-newline);; (setq require-final-newline t)(make-local-variable'comment-start)(setqcomment-start"# ")(make-local-variable'comment-start-skip)(setqcomment-start-skip"#+ *")(make-local-variable'comment-column)(setqcomment-column40)(make-local-variable'comment-end)(setqcomment-end"")(make-local-variable'outline-regexp)(setqoutline-regexp"[^\n\^M]")(make-local-variable'outline-level)(setqoutline-level'tcl-outline-level)(make-local-variable'font-lock-defaults)(setqfont-lock-defaults'(tcl-font-lock-keywords));; XEmacs change.(make-local-variable'imenu-generic-expression)(setqimenu-generic-expressiontcl-imenu-generic-expression);; (add-hook 'tcl-mode-hook 'imenu-add-menubar-index)(make-local-variable'parse-sexp-ignore-comments);; Settings for new dabbrev code.(make-local-variable'dabbrev-case-fold-search)(setqdabbrev-case-fold-searchnil)(make-local-variable'dabbrev-case-replace)(setqdabbrev-case-replacenil)(make-local-variable'dabbrev-abbrev-skip-leading-regexp)(setqdabbrev-abbrev-skip-leading-regexp"[$!]")(make-local-variable'dabbrev-abbrev-char-regexp)(setqdabbrev-abbrev-char-regexp"\\sw\\|\\s_")(iftcl-using-emacs-19(progn;; This can only be set to t in Emacs 19 and XEmacs.;; Emacs 18 and Epoch lose.(setqparse-sexp-ignore-commentst);; XEmacs has defun-prompt-regexp, but I don't believe;; that it works for end-of-defun -- only for;; beginning-of-defun.(make-local-variable'defun-prompt-regexp)(setqdefun-prompt-regexptcl-omit-ws-regexp);; The following doesn't work in Lucid Emacs 19.6, but maybe;; it will appear in later versions.(make-local-variable'add-log-current-defun-function)(setqadd-log-current-defun-function'add-log-tcl-defun))(setqparse-sexp-ignore-commentsnil));; Put Tcl menu into menubar for XEmacs. This happens;; automatically in Emacs.(if(andtcl-using-xemacs-19current-menubar(not(assoc"Tcl"current-menubar)))(progn(set-buffer-menubar(copy-sequencecurrent-menubar))(add-menunil"Tcl"tcl-xemacs-menu)));; Append Tcl menu to popup menu for XEmacs.(if(andtcl-using-xemacs-19(boundp'mode-popup-menu))(setqmode-popup-menu(cons(concatmode-name" Mode Commands")tcl-xemacs-menu)));; If hilit19 is loaded, add our stuff.(if(featurep'hilit19)(tcl-hilit))(run-hooks'tcl-mode-hook));; This is used for braces, brackets, and semi (except for closing;; braces, which are handled specially).(defuntcl-electric-char(arg)"Insert character and correct line's indentation."(interactive"p");; Indent line first; this looks better if parens blink.(tcl-indent-line)(self-insert-commandarg)(if(andtcl-auto-newline(=last-command-char?\;))(progn(newline)(tcl-indent-line))));; This is used for closing braces. If tcl-auto-newline is set, can;; insert a newline both before and after the brace, depending on;; context. FIXME should this be configurable? Does anyone use this?(defuntcl-electric-brace(arg)"Insert character and correct line's indentation."(interactive"p");; If auto-newlining and there is stuff on the same line, insert a;; newline first.(iftcl-auto-newline(progn(if(save-excursion(skip-chars-backward" \t")(bolp))()(tcl-indent-line)(newline));; In auto-newline case, must insert a newline after each;; brace. So an explicit loop is needed.(while(>arg0)(insertlast-command-char)(tcl-indent-line)(newline)(setqarg(1-arg))))(self-insert-commandarg))(tcl-indent-line))(defuntcl-indent-command(&optionalarg)"Indent current line as Tcl code, or in some cases insert a tab character.If `tcl-tab-always-indent' is t (the default), always indent current line.If `tcl-tab-always-indent' is nil and point is not in the indentationarea at the beginning of the line, a TAB is inserted.Other values of `tcl-tab-always-indent' cause the first possible actionfrom the following list to take place: 1. Move from beginning of line to correct indentation. 2. Delete an empty comment. 3. Move forward to start of comment, indenting if necessary. 4. Move forward to end of line, indenting if necessary. 5. Create an empty comment. 6. Move backward to start of comment, indenting if necessary."(interactive"p")(cond((nottcl-tab-always-indent);; Indent if in indentation area, otherwise insert TAB.(if(<=(current-column)(current-indentation))(tcl-indent-line)(insert-tabarg)))((eqtcl-tab-always-indentt);; Always indent.(tcl-indent-line))(t;; "Perl-mode" style TAB command.(let*((ipoint(point))(eolpoint(progn(end-of-line)(point)))(comment-p(tcl-in-comment)))(cond((=ipoint(save-excursion(beginning-of-line)(point)))(beginning-of-line)(tcl-indent-line);; If indenting didn't leave us in column 0, go to the;; indentation. Otherwise leave point at end of line. This;; is a hack.(if(=(point)(save-excursion(beginning-of-line)(point)))(end-of-line)(back-to-indentation)))((andcomment-p(looking-at"[ \t]*$"));; Empty comment, so delete it. We also delete any ";";; characters at the end of the line. I think this is;; friendlier, but I don't know how other people will feel.(backward-char)(skip-chars-backward" \t;")(delete-region(point)eolpoint))((andcomment-p(<ipoint(point)));; Before comment, so skip to it.(tcl-indent-line)(indent-for-comment))((/=ipointeolpoint);; Go to end of line (since we're not there yet).(goto-chareolpoint)(tcl-indent-line))((notcomment-p)(tcl-indent-line)(tcl-indent-for-comment))(t;; Go to start of comment. We don't leave point where it is;; because we want to skip comment-start-skip.(tcl-indent-line)(indent-for-comment)))))))(defuntcl-indent-line()"Indent current line as Tcl code.Return the amount the indentation changed by."(let((indent(calculate-tcl-indentnil))begshift-amt(case-fold-searchnil)(pos(-(point-max)(point))))(beginning-of-line)(setqbeg(point))(cond((eqindentnil)(setqindent(current-indentation)))(t(skip-chars-forward" \t")(if(listpindent)(setqindent(carindent)))(cond((=(following-char)?})(setqindent(-indenttcl-indent-level)))((=(following-char)?\])(setqindent(-indent1))))))(skip-chars-forward" \t")(setqshift-amt(-indent(current-column)))(if(zeropshift-amt)(if(>(-(point-max)pos)(point))(goto-char(-(point-max)pos)))(delete-regionbeg(point))(indent-toindent);; If initial point was within line's indentation,;; position after the indentation. Else stay at same point in text.(if(>(-(point-max)pos)(point))(goto-char(-(point-max)pos))))shift-amt))(defuntcl-figure-type()"Determine type of sexp at point.This is either `tcl-expr', `tcl-commands', or nil. Puts point at startof sexp that indicates types.See documentation for variable `tcl-type-alist' for more information."(let((count0)resultword-stack)(while(and(<count5)(notresult))(condition-casenil(progn;; FIXME should use "tcl-backward-sexp", which would skip;; over entire variables, etc.(backward-sexp)(if(looking-at"[a-zA-Z_]+")(let((listtcl-type-alist)entry)(setqword-stack(cons(tcl-word-no-props)word-stack))(while(andlist(notresult))(setqentry(carlist))(setqlist(cdrlist))(let((index0))(while(andentry(<=indexcount));; Abort loop if string does not match word on;; stack.(and(stringp(carentry))(not(string=(carentry)(nthindexword-stack)))(setqentrynil))(setqentry(cdrentry))(setqindex(1+index)))(and(>indexcount)(not(stringp(carentry)))(setqresult(carentry))))))(setqword-stack(consnilword-stack))))(errornil))(setqcount(1+count)))(andtcl-explain-indentation(message"Indentation type %s"result))result))(defuncalculate-tcl-indent(&optionalparse-start)"Return appropriate indentation for current line as Tcl code.In usual case returns an integer: the column to indent to.Returns nil if line starts inside a string, t if in a comment."(save-excursion(beginning-of-line)(let*((indent-point(point))(case-fold-searchnil)(continued-line(save-excursion(if(bobp)nil(backward-char)(=?\\(preceding-char)))))(continued-indent-value(ifcontinued-linetcl-continued-indent-level0))statecontaining-sexpfound-next-line)(ifparse-start(goto-charparse-start)(tcl-beginning-of-defun))(while(<(point)indent-point)(setqparse-start(point))(setqstate(parse-partial-sexp(point)indent-point0))(setqcontaining-sexp(car(cdrstate))))(cond((or(nth3state)(nth4state));; Inside comment or string. Return nil or t if should;; not change this line(nth4state))((nullcontaining-sexp);; Line is at top level.continued-indent-value)(t;; Set expr-p if we are looking at the expression part of;; an "if", "expr", etc statement. Set commands-p if we;; are looking at the body part of an if, while, etc;; statement. FIXME Should check for "for" loops here.(goto-charcontaining-sexp)(let*((sexpr-type(tcl-figure-type))(expr-p(eqsexpr-type'tcl-expr))(commands-p(eqsexpr-type'tcl-commands))(expr-start(point)));; Find the first statement in the block and indent;; like it. The first statement in the block might be;; on the same line, so what we do is skip all;; "virtually blank" lines, looking for a non-blank;; one. A line is virtually blank if it only contains;; a comment and whitespace. FIXME continued comments;; aren't supported. They are a wart on Tcl anyway.;; We do it this funky way because we want to know if;; we've found a statement on some line _after_ the;; line holding the sexp opener.(goto-charcontaining-sexp)(forward-char)(if(and(<(point)indent-point)(looking-at"[ \t]*\\(#.*\\)?$"))(progn(forward-line)(while(and(<(point)indent-point)(looking-at"[ \t]*\\(#.*\\)?$"))(setqfound-next-linet)(forward-line))))(if(orcontinued-line(/=(char-aftercontaining-sexp)?{)expr-p)(progn;; Line is continuation line, or the sexp opener;; is not a curly brace, or we are are looking at;; an `expr' expression (which must be split;; specially). So indentation is column of first;; good spot after sexp opener (with some added;; in the continued-line case). If there is no;; nonempty line before the indentation point, we;; use the column of the character after the sexp;; opener.(if(>=(point)indent-point)(progn(goto-charcontaining-sexp)(forward-char))(skip-chars-forward" \t"))(+(current-column)continued-indent-value));; After a curly brace, and not a continuation line.;; So take indentation from first good line after;; start of block, unless that line is on the same;; line as the opening brace. In this case use the;; indentation of the opening brace's line, plus;; another indent step. If we are in the body part;; of an "if" or "while" then the indentation is;; taken from the line holding the start of the;; statement.(if(and(<(point)indent-point)found-next-line)(current-indentation)(ifcommands-p(goto-charexpr-start)(goto-charcontaining-sexp))(+(current-indentation)tcl-indent-level)))))))))(defunindent-tcl-exp()"Indent each line of the Tcl grouping following point."(interactive)(let((indent-stack(listnil))(contain-stack(list(point)))(case-fold-searchnil)outer-loop-doneinner-loop-donestateostatethis-indentlast-sexpcontinued-line(next-depth0)last-depth)(save-excursion(forward-sexp1))(save-excursion(setqouter-loop-donenil)(while(and(not(eobp))(notouter-loop-done))(setqlast-depthnext-depth);; Compute how depth changes over this line;; plus enough other lines to get to one that;; does not end inside a comment or string.;; Meanwhile, do appropriate indentation on comment lines.(setqinner-loop-donenil)(while(and(notinner-loop-done)(not(and(eobp)(setqouter-loop-donet))))(setqostatestate)(setqstate(parse-partial-sexp(point)(progn(end-of-line)(point))nilnilstate))(setqnext-depth(carstate))(if(and(car(cdr(cdrstate)))(>=(car(cdr(cdrstate)))0))(setqlast-sexp(car(cdr(cdrstate)))))(if(or(nth4ostate))(tcl-indent-line))(if(or(nth3state))(forward-line1)(setqinner-loop-donet)))(if(<=next-depth0)(setqouter-loop-donet))(ifouter-loop-donenil;; If this line had ..))) (((.. in it, pop out of the levels;; that ended anywhere in this line, even if the final depth;; doesn't indicate that they ended.(while(>last-depth(nth6state))(setqindent-stack(cdrindent-stack)contain-stack(cdrcontain-stack)last-depth(1-last-depth)))(if(/=last-depthnext-depth)(setqlast-sexpnil));; Add levels for any parens that were started in this line.(while(<last-depthnext-depth)(setqindent-stack(consnilindent-stack)contain-stack(consnilcontain-stack)last-depth(1+last-depth)))(if(null(carcontain-stack))(setcarcontain-stack(or(car(cdrstate))(save-excursion(forward-sexp-1)(point)))))(forward-line1)(setqcontinued-line(save-excursion(backward-char)(=(preceding-char)?\\)))(skip-chars-forward" \t")(if(eolp)nil(if(and(carindent-stack)(>=(carindent-stack)0));; Line is on an existing nesting level.(setqthis-indent(carindent-stack));; Just started a new nesting level.;; Compute the standard indent for this level.(let((val(calculate-tcl-indent(if(carindent-stack)(-(carindent-stack))))))(setcarindent-stack(setqthis-indentval))(setqcontinued-linenil)))(cond((not(numberpthis-indent)))((=(following-char)?})(setqthis-indent(-this-indenttcl-indent-level)))((=(following-char)?\])(setqthis-indent(-this-indent1))));; Put chosen indentation into effect.(or(nullthis-indent)(=(current-column)(ifcontinued-line(+this-indenttcl-indent-level)this-indent))(progn(delete-region(point)(progn(beginning-of-line)(point)))(indent-to(ifcontinued-line(+this-indenttcl-indent-level)this-indent))))))))));;;; Interfaces to other packages.;;;; FIXME Definition of function is very ad-hoc. Should use;; tcl-beginning-of-defun. Also has incestuous knowledge about the;; format of tcl-proc-regexp.(defunadd-log-tcl-defun()"Return name of Tcl function point is in, or nil."(save-excursion(end-of-line)(if(re-search-backward(concattcl-proc-regexp"\\([^ \t\n{]+\\)")nilt)(buffer-substring(match-beginning2)(match-end2)))))(defuntcl-outline-level()(save-excursion(skip-chars-forward" \t")(current-column)));;;; Helper functions for inferior Tcl mode.;;;; This exists to let us delete the prompt when commands are sent;; directly to the inferior Tcl. See gud.el for an explanation of how;; it all works (I took it from there). This stuff doesn't really;; work as well as I'd like it to. But I don't believe there is;; anything useful that can be done.(defvarinferior-tcl-delete-prompt-markernil)(defuntcl-filter(procstring)(let((inhibit-quitt))(save-excursion(set-buffer(process-bufferproc))(goto-char(process-markproc));; Delete prompt if requested.(if(marker-bufferinferior-tcl-delete-prompt-marker)(progn(delete-region(point)inferior-tcl-delete-prompt-marker)(set-markerinferior-tcl-delete-prompt-markernil)))))(iftcl-using-emacs-19(comint-output-filterprocstring)(funcallcomint-output-filterstring)))(defuntcl-send-string(procstring)(save-excursion(set-buffer(process-bufferproc))(goto-char(process-markproc))(beginning-of-line)(if(looking-atcomint-prompt-regexp)(set-markerinferior-tcl-delete-prompt-marker(point))))(comint-send-stringprocstring))(defuntcl-send-region(procstartend)(save-excursion(set-buffer(process-bufferproc))(goto-char(process-markproc))(beginning-of-line)(if(looking-atcomint-prompt-regexp)(set-markerinferior-tcl-delete-prompt-marker(point))))(comint-send-regionprocstartend))(defunswitch-to-tcl(eob-p)"Switch to inferior Tcl process buffer.With argument, positions cursor at end of buffer."(interactive"P")(if(get-bufferinferior-tcl-buffer)(pop-to-bufferinferior-tcl-buffer)(error"No current inferior Tcl buffer"))(cond(eob-p(push-mark)(goto-char(point-max)))))(defuninferior-tcl-proc()"Return current inferior Tcl process.See variable `inferior-tcl-buffer'."(let((proc(get-buffer-process(if(eqmajor-mode'inferior-tcl-mode)(current-buffer)inferior-tcl-buffer))))(orproc(error"No Tcl process; see variable `inferior-tcl-buffer'"))))(defuntcl-eval-region(startend&optionaland-go)"Send the current region to the inferior Tcl process.Prefix argument means switch to the Tcl buffer afterwards."(interactive"r\nP")(let((proc(inferior-tcl-proc)))(tcl-send-regionprocstartend)(tcl-send-stringproc"\n")(ifand-go(switch-to-tclt))))(defuntcl-eval-defun(&optionaland-go)"Send the current defun to the inferior Tcl process.Prefix argument means switch to the Tcl buffer afterwards."(interactive"P")(save-excursion(tcl-end-of-defun)(let((end(point)))(tcl-beginning-of-defun)(tcl-eval-region(point)end)))(ifand-go(switch-to-tclt)));;;; Inferior Tcl mode itself.;;(defuninferior-tcl-mode()"Major mode for interacting with Tcl interpreter.A Tcl process can be started with \\[inferior-tcl].Entry to this mode runs the normal hooks `comint-mode-hook' and`inferior-tcl-mode-hook', in that order.You can send text to the inferior Tcl process from other bufferscontaining Tcl source.Variables controlling Inferior Tcl mode: `tcl-application' Name of program to run. `tcl-command-switches' Command line arguments to `tcl-application'. `tcl-prompt-regexp' Matches prompt. `inferior-tcl-source-command' Command to use to read Tcl file in running application. `inferior-tcl-buffer' The current inferior Tcl process buffer. See variable documentation for details on multiple-process support.The following commands are available:\\{inferior-tcl-mode-map}"(interactive)(comint-mode)(setqcomint-prompt-regexp(ortcl-prompt-regexp(concat"^"(regexp-quotetcl-application)">")))(setqmajor-mode'inferior-tcl-mode)(setqmode-name"Inferior Tcl")(if(boundp'modeline-process)(setqmodeline-process'(": %s")); For XEmacs.(setqmode-line-process'(": %s")))(use-local-mapinferior-tcl-mode-map)(setqlocal-abbrev-tabletcl-mode-abbrev-table)(set-syntax-tabletcl-mode-syntax-table)(iftcl-using-emacs-19(progn(make-local-variable'defun-prompt-regexp)(setqdefun-prompt-regexptcl-omit-ws-regexp)))(make-local-variable'inferior-tcl-delete-prompt-marker)(setqinferior-tcl-delete-prompt-marker(make-marker))(set-process-filter(get-buffer-process(current-buffer))'tcl-filter)(run-hooks'inferior-tcl-mode-hook));;;###autoload(defuninferior-tcl(cmd)"Run inferior Tcl process.Prefix arg means enter program name interactively.See documentation for function `inferior-tcl-mode' for more information."(interactive(list(ifcurrent-prefix-arg(read-string"Run Tcl: "tcl-application)tcl-application)))(if(not(comint-check-proc"*inferior-tcl*"))(progn(set-buffer(apply(functionmake-comint)"inferior-tcl"cmdniltcl-command-switches))(inferior-tcl-mode)))(make-local-variable'tcl-application)(setqtcl-applicationcmd)(setqinferior-tcl-buffer"*inferior-tcl*")(switch-to-buffer"*inferior-tcl*"))(and(fboundp'defalias)(defalias'run-tcl'inferior-tcl));;;; Auto-fill support.;;(defuntcl-real-command-p()"Return nil if point is not at the beginning of a command.A command is the first word on an otherwise empty line, or thefirst word following a semicolon, opening brace, or opening bracket."(save-excursion(skip-chars-backward" \t")(cond((bobp)t)((bolp)(backward-char);; Note -- continued comments are not supported here. I;; consider those to be a wart on the language.(not(eq?\\(preceding-char))))(t(memq(preceding-char)'(?\;?{?\[))))));; FIXME doesn't actually return t. See last case.(defuntcl-real-comment-p()"Return t if point is just after the `#' beginning a real comment.Does not check to see if previous char is actually `#'.A real comment is either at the beginning of the buffer,preceded only by whitespace on the line, or has a precedingsemicolon, opening brace, or opening bracket on the same line."(save-excursion(backward-char)(tcl-real-command-p)))(defuntcl-hairy-scan-for-comment(stateendalways-stop)"Determine if point is in a comment.Returns a list of the form `(FLAG . STATE)'. STATE can be usedas input to future invocations. FLAG is nil if not in comment,t otherwise. If in comment, leaves point at beginning of comment.This function does not work in Emacs 18.See also `tcl-simple-scan-for-comment', asimpler version that is often right, and works in Emacs 18."(let((bol(save-excursion(goto-charend)(beginning-of-line)(point)))real-commentlast-cstart)(while(and(notlast-cstart)(<(point)end))(setqreal-commentnil);In case we've looped around and it is;set.(setqstate(parse-partial-sexp(point)endnilnilstatet))(if(nth4state)(progn;; If ALWAYS-STOP is set, stop even if we don't have a;; real comment, or if the comment isn't on the same line;; as the end.(ifalways-stop(setqlast-cstart(point)));; If we have a real comment, then set the comment;; starting point if we are on the same line as the ending;; location.(setqreal-comment(tcl-real-comment-p))(ifreal-comment(progn(and(>(point)bol)(setqlast-cstart(point)));; NOTE Emacs 19 has a misfeature whereby calling;; parse-partial-sexp with COMMENTSTOP set and with;; an initial list that says point is in a comment;; will cause an immediate return. So we must skip;; over the comment ourselves.(beginning-of-line2)));; Frob the state to make it look like we aren't in a;; comment.(setcar(nthcdr4state)nil))))(andlast-cstart(goto-charlast-cstart))(consreal-commentstate)))(defuntcl-hairy-in-comment()"Return t if point is in a comment, and leave point at beginning of comment."(let((save(point)))(tcl-beginning-of-defun)(car(tcl-hairy-scan-for-commentnilsavenil))))(defuntcl-simple-in-comment()"Return t if point is in comment, and leave point at beginning of comment.This is faster that `tcl-hairy-in-comment', but is correct less often."(let((save(point))comment)(beginning-of-line)(while(and(<(point)save)(notcomment))(search-forward"#"save'move)(setqcomment(tcl-real-comment-p)))comment))(defuntcl-in-comment()"Return t if point is in comment, and leave point at beginning of comment."(if(andtcl-pps-has-arg-6tcl-use-hairy-comment-detector)(tcl-hairy-in-comment)(tcl-simple-in-comment)))(defuntcl-do-fill-paragraph(ignore)"`fill-paragraph' function for Tcl mode. Only fills in a comment."(let(in-commentcolwhere)(save-excursion(end-of-line)(setqin-comment(tcl-in-comment))(ifin-comment(progn(setqwhere(1+(point)))(setqcol(1-(current-column))))))(andin-comment(save-excursion(back-to-indentation)(=col(current-column)));; In a comment. Set the fill prefix, and find the paragraph;; boundaries by searching for lines that look like;; comment-only lines.(let((fill-prefix(buffer-substring(progn(beginning-of-line)(point))where))p-startp-end);; Search backwards.(save-excursion(while(looking-at"^[ \t]*#")(forward-line-1))(forward-line)(setqp-start(point)));; Search forwards.(save-excursion(while(looking-at"^[ \t]*#")(forward-line))(setqp-end(point)));; Narrow and do the fill.(save-restriction(narrow-to-regionp-startp-end)(fill-paragraphignore)))))t)(defuntcl-do-auto-fill()"Auto-fill function for Tcl mode. Only auto-fills in a comment."(if(>(current-column)fill-column)(let((fill-prefix"# ")in-commentcol)(save-excursion(setqin-comment(tcl-in-comment))(ifin-comment(setqcol(1-(current-column)))))(ifin-comment(progn(do-auto-fill)(save-excursion(back-to-indentation)(delete-region(point)(save-excursion(beginning-of-line)(point)))(indent-to-columncol)))))));;;; Help-related code.;;(defvartcl-help-saved-dirsnil"Saved help directories.If `tcl-help-directory-list' changes, this allows `tcl-help-on-word'to update the alist.")(defvartcl-help-alistnil"Alist with command names as keys and filenames as values.")(defuntcl-help-snarf-commands(dirlist)"Build alist of commands and filenames."(whiledirlist(let((files(directory-files(cardirlist)t)))(whilefiles(if(and(file-directory-p(carfiles))(not(let((fpart(file-name-nondirectory(carfiles))))(or(equalfpart".")(equalfpart"..")))))(let((matches(directory-files(carfiles)t)))(whilematches(or(file-directory-p(carmatches))(setqtcl-help-alist(cons(cons(file-name-nondirectory(carmatches))(carmatches))tcl-help-alist)))(setqmatches(cdrmatches)))))(setqfiles(cdrfiles))))(setqdirlist(cdrdirlist))))(defuntcl-reread-help-files()"Set up to re-read files, and then do it."(interactive)(message"Building Tcl help file index...")(setqtcl-help-saved-dirstcl-help-directory-list)(setqtcl-help-alistnil)(tcl-help-snarf-commandstcl-help-directory-list)(message"Building Tcl help file index...done"))(defuntcl-word-no-props()"Like `current-word', but strips properties."(let((word(current-word)))(and(fboundp'set-text-properties)(set-text-properties0(lengthword)nilword))word))(defuntcl-current-word(flag)"Return current command word, or nil.If FLAG is nil, just uses `current-word'.Otherwise scans backward for most likely Tcl command word."(if(andflag(memqmajor-mode'(tcl-modeinferior-tcl-mode)))(condition-casenil(save-excursion;; Look backward for first word actually in alist.(if(bobp)()(while(and(not(bobp))(not(tcl-real-command-p)))(backward-sexp)))(if(assoc(tcl-word-no-props)tcl-help-alist)(tcl-word-no-props)))(errornil))(tcl-word-no-props)));;;###autoload(defuntcl-help-on-word(command&optionalarg)"Get help on Tcl command. Default is word at point.Prefix argument means invert sense of `tcl-use-smart-word-finder'."(interactive(list(progn(if(not(equaltcl-help-directory-listtcl-help-saved-dirs))(tcl-reread-help-files))(let((word(tcl-current-word(ifcurrent-prefix-arg(nottcl-use-smart-word-finder)tcl-use-smart-word-finder))))(completing-read(if(or(nullword)(string=word""))"Help on Tcl command: "(format"Help on Tcl command (default %s): "word))tcl-help-alistnilt)))current-prefix-arg))(if(not(equaltcl-help-directory-listtcl-help-saved-dirs))(tcl-reread-help-files))(if(string=command"")(setqcommand(tcl-current-word(ifarg(nottcl-use-smart-word-finder)tcl-use-smart-word-finder))))(let*((help(get-buffer-create"*Tcl help*"))(cell(assoccommandtcl-help-alist))(file(andcell(cdrcell))))(set-bufferhelp)(delete-region(point-min)(point-max))(iffile(progn(insert"*** "command"\n\n")(insert-file-contentsfile))(if(string=command"")(insert"Magical Pig!")(insert"Tcl command "command" not in help\n")))(set-buffer-modified-pnil)(goto-char(point-min))(display-bufferhelp)));;;; Other interactive stuff.;;(defvartcl-previous-dir/filenil"Record last directory and file used in loading.This holds a cons cell of the form `(DIRECTORY . FILE)'describing the last `tcl-load-file' command.")(defuntcl-load-file(file&optionaland-go)"Load a Tcl file into the inferior Tcl process.Prefix argument means switch to the Tcl buffer afterwards."(interactive(list;; car because comint-get-source returns a list holding the;; filename.(car(comint-get-source"Load Tcl file: "(or(and(eqmajor-mode'tcl-mode)(buffer-file-name))tcl-previous-dir/file)'(tcl-mode)t))current-prefix-arg))(comint-check-sourcefile)(setqtcl-previous-dir/file(cons(file-name-directoryfile)(file-name-nondirectoryfile)))(tcl-send-string(inferior-tcl-proc)(formatinferior-tcl-source-command(tcl-quotefile)))(ifand-go(switch-to-tclt)))(defuntcl-restart-with-file(file&optionaland-go)"Restart inferior Tcl with file.If an inferior Tcl process exists, it is killed first.Prefix argument means switch to the Tcl buffer afterwards."(interactive(list(car(comint-get-source"Restart with Tcl file: "(or(and(eqmajor-mode'tcl-mode)(buffer-file-name))tcl-previous-dir/file)'(tcl-mode)t))current-prefix-arg))(let*((buf(if(eqmajor-mode'inferior-tcl-mode)(current-buffer)inferior-tcl-buffer))(proc(andbuf(get-processbuf))))(cond((not(andbuf(get-bufferbuf)));; I think this will be ok.(inferior-tcltcl-application)(tcl-load-filefileand-go))((or(not(comint-check-procbuf))(yes-or-no-p"A Tcl process is running, are you sure you want to reset it? "))(save-excursion(comint-check-sourcefile)(setqtcl-previous-dir/file(cons(file-name-directoryfile)(file-name-nondirectoryfile)))(comint-exec(get-buffer-createbuf)(ifproc(process-nameproc)"inferior-tcl")tcl-applicationfiletcl-command-switches)(ifand-go(switch-to-tclt)))))))(defuntcl-auto-fill-mode(&optionalarg)"Like `auto-fill-mode', but controls filling of Tcl comments."(interactive"P")(and(nottcl-using-emacs-19)(error"This feature is not supported in Emacs 18"));; Following code taken from "auto-fill-mode" (simple.el).(prog1(setqauto-fill-function(if(if(nullarg)(notauto-fill-function)(>(prefix-numeric-valuearg)0))'tcl-do-auto-fillnil))(force-mode-line-update)));; hilit19 support from "Chris Alfeld" <calfeld@math.utah.edu>(defuntcl-hilit()(hilit-set-mode-patterns'(tcl-mode)'(("\\(^ *\\|\; *\\)#.*$"nilcomment)("[^\\]\\(\\$[A-Za-z0-9\\-\\_./\\(\\)]+\\)"1label)("[^_]\\<\\(append\\|array\\|auto_execok\\|auto_load\\|auto_mkindex\\|auto_reset\\|break\\|case\\|catch\\|cd\\|close\\|concat\\|continue\\|eof\\|error\\|eval\\|exec\\|exit\\|expr\\|file\\|flush\\|for\\|foreach\\|format\\|gets\\|glob\\|global\\|history\\|if\\|incr\\|info\\|join\\|lappend\\|lindex\\|linsert\\|list\\|llength\\|lrange\\|lreplace\\|lsearch\\|lsort\\|open\\|pid\\|proc\\|puts\\|pwd\\|read\\|regexp\\|regsub\\|rename\\|return\\|scan\\|seek\\|set\\|source\\|split\\|string\\|switch\\|tell\\|time\\|trace\\|unknown\\|unset\\|uplevel\\|upvar\\|while\\)\\>[^_]"1keyword); tcl keywords("[^_]\\<\\(after\\|bell\\|bind\\|bindtags\\|clipboard\\|destroy\\|fileevent\\|focus\\|grab\\|image\\|lower\\|option\\|pack\\|place\\|raise\\|scale\\|selection\\|send\\|subst\\|tk\\|tk_popup\\|tkwait\\|update\\|winfo\\|wm\\)\\>[^_]"1define); tk keywords("[^_]\\<\\(button\\|canvas\\|checkbutton\\|entry\\|frame\\|label\\|listbox\\|menu\\|menubutton\\|message\\|radiobutton\\|scrollbar\\|text\\|toplevel\\)\\>[^_]"1decl); tk widgets("[^_]\\<\\(tix\\((ButtonBox\\|Baloon\\|Control\\|DirList\\|ExFileSelectBox\\|ExFileSelectDialog\\|FileEntry\\|HList\\|LabelEntry\\|LabelFrame\\|NoteBook\\|OptionMenu\\|PanedWindow\\|PopupMenu\\|ScrolledHList\\|ScrolledText\\|ScrolledWindow\\|Select\\|StdButtonBox\\)\\)\\>[^_]"1defun); tix widgets("[{}\\\"\\(\\)]"nilinclude); misc punctuation)))(defuntcl-electric-hash(&optionalcount)"Insert a `#' and quote if it does not start a real comment.Prefix arg is number of `#'s to insert.See variable `tcl-electric-hash-style' for description of quotingstyles."(interactive"p")(orcount(setqcount1))(if(>count0)(let((type(if(eqtcl-electric-hash-style'smart)(if(>count3); FIXME what is "smart"?'quote'backslash)tcl-electric-hash-style))comment)(iftype(progn(save-excursion(insert"#")(setqcomment(tcl-in-comment)))(delete-char1)(andtcl-explain-indentation(message"comment: %s"comment))(cond((eqtype'quote)(if(notcomment)(insert"\"")))((eqtype'backslash);; The following will set count to 0, so the;; insert-char can still be run.(if(notcomment)(while(>count0)(insert"\\#")(setqcount(1-count)))))(tnil))))(insert-char?#count))))(defuntcl-hashify-buffer()"Quote all `#'s in current buffer that aren't Tcl comments."(interactive)(save-excursion(goto-char(point-min))(if(andtcl-pps-has-arg-6tcl-use-hairy-comment-detector)(let(stateresult)(while(<(point)(point-max))(setqresult(tcl-hairy-scan-for-commentstate(point-max)t))(if(carresult)(beginning-of-line2)(backward-char)(if(eq?#(following-char))(insert"\\"))(forward-char))(setqstate(cdrresult))))(while(and(<(point)(point-max))(search-forward"#"nil'move))(if(tcl-real-comment-p)(beginning-of-line2);; There's really no good way for the simple converter to;; work. So we just quote # if it isn't already quoted.;; Bogus, but it works.(backward-char)(if(not(eq?\\(preceding-char)))(insert"\\"))(forward-char))))))(defuntcl-indent-for-comment()"Indent this line's comment to comment column, or insert an empty comment.Is smart about syntax of Tcl comments.Parts of this were taken from `indent-for-comment'."(interactive"*")(end-of-line)(or(tcl-in-comment)(progn;; Not in a comment, so we have to insert one. Create an;; empty comment (since there isn't one on this line). If;; line is not blank, make sure we insert a ";" first.(skip-chars-backward" \t")(let((eolpoint(point)))(beginning-of-line)(if(/=(point)eolpoint)(progn(goto-chareolpoint)(insert(if(tcl-real-command-p)""";")"# ")(backward-char))))));; Point is just after the "#" starting a comment. Move it as;; appropriate.(let*((indent(or(funcallcomment-indent-function)comment-column))(begpos(progn(backward-char)(point))))(if(/=begposindent)(progn(skip-chars-backward" \t"(save-excursion(beginning-of-line)(point)))(delete-region(point)begpos)(indent-toindent)))(looking-atcomment-start-skip); Always true.(goto-char(match-end0));; I don't like the effect of the next two.;;(skip-chars-backward " \t" (match-beginning 0));;(skip-chars-backward "^ \t" (match-beginning 0))));; The following was inspired by the Tcl editing mode written by;; Gregor Schmid <schmid@fb3-s7.math.TU-Berlin.DE>. His version also;; attempts to snarf the command line options from the command line,;; but I didn't think that would really be that helpful (doesn't seem;; like it owould be right enough. His version also looks for the;; "#!/bin/csh ... exec" hack, but that seemed even less useful.;; FIXME should make sure that the application mentioned actually;; exists.(defuntcl-guess-application()"Attempt to guess Tcl application by looking at first line.The first line is assumed to look like \"#!.../program ...\"."(save-excursion(goto-char(point-min))(if(looking-at"#![^ \t]*/\\([^ \t\n/]+\\)\\([ \t]\\|$\\)")(progn(make-local-variable'tcl-application)(setqtcl-application(buffer-substring(match-beginning1)(match-end1)))))));; This only exists to put on the menubar. I couldn't figure out any;; other way to do it. FIXME should take "number of #-marks";; argument.(defuntcl-uncomment-region(begend)"Uncomment region."(interactive"r")(comment-regionbegend-1));;;; XEmacs menu support.;; Taken from schmid@fb3-s7.math.TU-Berlin.DE (Gregor Schmid),;; who wrote a different Tcl mode.;; We also have support for menus in Emacs. We do this by;; loading the XEmacs menu emulation code.;;(defuntcl-popup-menu(e)(interactive"@e")(andtcl-using-emacs-19(nottcl-using-xemacs-19)(iftcl-using-emacs-19-23(require'lmenu);; CAVEATS:;; * lmenu.el provides 'menubar, which is bogus.;; * lmenu.el causes menubars to be turned on everywhere.;; Doubly bogus!;; Both of these problems are fixed in Emacs 19.23. People;; using an Emacs before that just suffer.(require'menubar"lmenu")));; This is annoying;; IMHO popup-menu should be autoloaded. Oh well.(popup-menutcl-xemacs-menu));;;; Quoting and unquoting functions.;;;; This quoting is sufficient to protect eg a filename from any sort;; of expansion or splitting. Tcl quoting sure sucks.(defuntcl-quote(string)"Quote STRING according to Tcl rules."(mapconcat(function(lambda(char)(if(memqchar'(?[?]?{?}?\\?\"?$??\;))(concat"\\"(char-to-stringchar))(char-to-stringchar))))string""));;;; Bug reporting.;;(and(fboundp'eval-when-compile)(eval-when-compile(require'reporter)))(defuntcl-submit-bug-report()"Submit via mail a bug report on Tcl mode."(interactive)(require'reporter)(and(y-or-n-p"Do you really want to submit a bug report on Tcl mode? ")(reporter-submit-bug-reporttcl-maintainer(concat"Tcl mode "tcl-version)'(tcl-indent-leveltcl-continued-indent-leveltcl-auto-newlinetcl-tab-always-indenttcl-use-hairy-comment-detectortcl-electric-hash-styletcl-help-directory-listtcl-use-smart-word-findertcl-applicationtcl-command-switchestcl-prompt-regexpinferior-tcl-source-commandtcl-using-emacs-19tcl-using-emacs-19-23tcl-using-xemacs-19tcl-proc-listtcl-proc-regexptcl-typeword-listtcl-keyword-listtcl-font-lock-keywordstcl-pps-has-arg-6))))(provide'tcl);;; tcl.el ends here