(eval-when-compile
(require 'imenu)
(require 'outline)
(require 'dabbrev)
(require 'add-log))
(require 'comint)
(defgroup tcl nil
"Major mode for editing Tcl source in Emacs."
:link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
:group 'languages)
(defcustom tcl-indent-level 4
"*Indentation of Tcl statements with respect to containing block."
:type 'integer
:group 'tcl)
(defcustom tcl-continued-indent-level 4
"*Indentation of continuation line relative to first line of command."
:type 'integer
:group 'tcl)
(defcustom tcl-auto-newline nil
"*Non-nil means automatically newline before and after braces you insert."
:type 'boolean
:group 'tcl)
(defcustom tcl-tab-always-indent tab-always-indent
"*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 of
the line, a TAB is inserted.
Other values cause the first possible action from 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."
:type '(choice (const :tag "Always" t)
(const :tag "Beginning only" nil)
(const :tag "Maybe move or make or delete comment" 'tcl))
:group 'tcl)
(defcustom tcl-electric-hash-style nil "*Style of electric hash insertion to use.
Possible values are `backslash', meaning that `\\' quoting should be
done; `quote', meaning that `\"' quoting should be done; `smart',
meaning that the choice between `backslash' and `quote' should be
made depending on the number of hashes inserted; or nil, meaning that
no quoting should be done. Any other value for this variable is
taken to mean `smart'. The default is nil."
:type '(choice (const backslash) (const quote) (const smart) (const nil))
:group 'tcl)
(defcustom tcl-help-directory-list nil
"*List of topmost directories containing TclX help files."
:type '(repeat directory)
:group 'tcl)
(defcustom tcl-use-smart-word-finder t
"*If not nil, use smart way to find current word, for Tcl help feature."
:type 'boolean
:group 'tcl)
(defcustom tcl-application "wish"
"*Name of Tcl program to run in inferior Tcl mode."
:type 'string
:group 'tcl)
(defcustom tcl-command-switches nil
"*List of switches to supply to the `tcl-application' program."
:type '(repeat string)
:group 'tcl)
(defcustom tcl-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 primary
and secondary prompts for tclsh and wish."
:type 'regexp
:group 'tcl)
(defcustom inferior-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 name
and should result in a Tcl expression that will command the
inferior Tcl to load that file. The filename will be appropriately
quoted for Tcl."
:type 'string
:group 'tcl)
(defface tcl-escaped-newline '((t :inherit font-lock-string-face))
"Face used for (non-escaped) backslash at end of a line in Tcl mode."
:group 'tcl
:version "22.1")
(defvar tcl-mode-map
(let ((map (make-sparse-keymap)))
(define-key map "{" 'tcl-electric-char)
(define-key map "}" 'tcl-electric-brace)
(define-key map "[" 'tcl-electric-char)
(define-key map "]" 'tcl-electric-char)
(define-key map ";" 'tcl-electric-char)
(define-key map "#" 'tcl-electric-hash) (define-key map "\e\C-q" 'tcl-indent-exp)
(define-key map "\177" 'backward-delete-char-untabify)
(define-key map "\t" 'tcl-indent-command)
(define-key map "\M-\C-x" 'tcl-eval-defun)
(define-key map "\C-c\C-i" 'tcl-help-on-word)
(define-key map "\C-c\C-v" 'tcl-eval-defun)
(define-key map "\C-c\C-f" 'tcl-load-file)
(define-key map "\C-c\C-t" 'inferior-tcl)
(define-key map "\C-c\C-x" 'tcl-eval-region)
(define-key map "\C-c\C-s" 'switch-to-tcl)
map)
"Keymap used in `tcl-mode'.")
(defvar tcl-mode-syntax-table
(let ((st (make-syntax-table)))
(modify-syntax-entry ?% "_" st)
(modify-syntax-entry ?@ "_" st)
(modify-syntax-entry ?& "_" st)
(modify-syntax-entry ?* "_" st)
(modify-syntax-entry ?+ "_" st)
(modify-syntax-entry ?- "_" st)
(modify-syntax-entry ?. "_" st)
(modify-syntax-entry ?: "_" st)
(modify-syntax-entry ?! "_" st)
(modify-syntax-entry ?$ "_" st) (modify-syntax-entry ?/ "_" st)
(modify-syntax-entry ?~ "_" st)
(modify-syntax-entry ?< "_" st)
(modify-syntax-entry ?= "_" st)
(modify-syntax-entry ?> "_" st)
(modify-syntax-entry ?| "_" st)
(modify-syntax-entry ?\( "()" st)
(modify-syntax-entry ?\) ")(" st)
(modify-syntax-entry ?\ (modify-syntax-entry ?\n ">" st)
(modify-syntax-entry ?# "<" st)
st)
"Syntax table in use in `tcl-mode' buffers.")
(defvar inferior-tcl-mode-map
(let ((map (make-sparse-keymap)))
(define-key map "\t" 'comint-dynamic-complete)
(define-key map "\M-?" 'comint-dynamic-list-filename-completions)
(define-key map "\177" 'backward-delete-char-untabify)
(define-key map "\M-\C-x" 'tcl-eval-defun)
(define-key map "\C-c\C-i" 'tcl-help-on-word)
(define-key map "\C-c\C-v" 'tcl-eval-defun)
(define-key map "\C-c\C-f" 'tcl-load-file)
(define-key map "\C-c\C-t" 'inferior-tcl)
(define-key map "\C-c\C-x" 'tcl-eval-region)
(define-key map "\C-c\C-s" 'switch-to-tcl)
map)
"Keymap used in `inferior-tcl-mode'.")
(easy-menu-define tcl-mode-menu tcl-mode-map "Menu used in `tcl-mode'."
'("Tcl"
["Beginning of function" beginning-of-defun t]
["End of function" end-of-defun t]
["Mark function" mark-defun t]
["Indent region" indent-region (mark t)]
["Comment region" comment-region (mark t)]
["Uncomment region" uncomment-region (mark t)]
"----"
["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]))
(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 new
process with another \\[inferior-tcl]. It will be in a new buffer,
named `*inferior-tcl*'. You can switch between the different process
buffers 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 to
send to, when you have more than one Tcl process around. This is
determined by the global variable `inferior-tcl-buffer'. Suppose you
have 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, what
process 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 run
one process, this does the right thing. If you run multiple
processes, you can change `inferior-tcl-buffer' to another process
buffer with \\[set-variable].")
(defvar 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 more
information):
`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)")
(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.")
(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"
"chain")
"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-builtin-list
'("after" "append" "array" "bgerror" "binary" "catch" "cd" "clock"
"close" "concat" "console" "dde" "encoding" "eof" "exec" "expr"
"fblocked" "fconfigure" "fcopy" "file" "fileevent" "flush"
"format" "gets" "glob" "history" "incr" "info" "interp" "join"
"lappend" "lindex" "linsert" "list" "llength" "load" "lrange"
"lreplace" "lsort" "namespace" "open" "package" "pid" "puts" "pwd"
"read" "regexp" "registry" "regsub" "rename" "scan" "seek" "set"
"socket" "source" "split" "string" "subst" "tell" "time" "trace"
"unknown" "unset" "vwait")
"List of Tcl commands. Used only for highlighting.
Call `tcl-set-font-lock-keywords' after changing this list.
This list excludes those commands already found in `tcl-proc-list' and
`tcl-keyword-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'.")
(defvar tcl-font-lock-syntactic-keywords
'(("[^;[{ \t\n][ \t]*\\(#\\)" (1 ".")))
"Syntactic keywords for `tcl-mode'.")
(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" 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 first
argument is ignored (for indentation purposes). The second argument
is a Tcl expression, and the last argument is Tcl commands.")
(defvar tcl-explain-indentation nil
"If non-nil, debugging message will be printed during indentation.")
(defconst tcl-omit-ws-regexp "^[^]\" \t\n#}][^\n\"#]+[ \t]+")
(defun tcl-set-proc-regexp ()
"Set `tcl-proc-regexp' from variable `tcl-proc-list'."
(setq tcl-proc-regexp
(concat "^\\s-*" (regexp-opt tcl-proc-list t) "[ \t]+")))
(defun tcl-set-font-lock-keywords ()
"Set `tcl-font-lock-keywords'.
Uses variables `tcl-proc-regexp' and `tcl-keyword-list'."
(setq tcl-font-lock-keywords
(list
(list (concat tcl-proc-regexp "\\([^ \t\n]+\\)")
2 'font-lock-function-name-face)
(list (concat "\\(\\s-\\|^\\)"
(regexp-opt tcl-typeword-list t)
"\\(\\s-\\|$\\)")
2 'font-lock-type-face)
(list (concat "\\_<" (regexp-opt tcl-builtin-list t) "\\_>")
1 'font-lock-builtin-face)
'("\\${\\([^}]+\\)}" 1 font-lock-variable-name-face)
'("\\$\\(\\(?:::\\)?\\(?:[[:alnum:]_]+::\\)*[[:alnum:]_]+\\)"
1 font-lock-variable-name-face)
'("\\(?:\\s-\\|^\\|\\[\\)set\\s-+{\\([^}]+\\)}"
1 font-lock-variable-name-face keep)
'("\\(?:\\s-\\|^\\|\\[\\)set\\s-+\\(\\(?:::\\)?\
\\(?:[[:alnum:]_]+::\\)*[[:alnum:]_]+\\)"
1 font-lock-variable-name-face keep)
'("\\(^\\|[^\\]\\)\\(\\\\\\\\\\)*\\(\\\\\\)$" 3 'tcl-escaped-newline)
(cons (concat "\\_<" (regexp-opt tcl-keyword-list t) "\\_>")
1))))
(if tcl-proc-regexp
()
(tcl-set-proc-regexp))
(if tcl-font-lock-keywords
()
(tcl-set-font-lock-keywords))
(defvar tcl-imenu-generic-expression
`((nil ,(concat tcl-proc-regexp "\\([-A-Za-z0-9_:+*]+\\)") 2))
"Imenu generic expression for `tcl-mode'. See `imenu-generic-expression'.")
(define-derived-mode tcl-mode nil "Tcl"
"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 variable
documentation 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-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 runs `tcl-mode-hook'. Read the documentation for
`tcl-mode-hook' to see what kinds of interesting hook functions
already exist.
Commands:
\\{tcl-mode-map}"
(unless (and (boundp 'filladapt-mode) filladapt-mode)
(set (make-local-variable 'paragraph-ignore-fill-prefix) t))
(set (make-local-variable 'indent-line-function) 'tcl-indent-line)
(set (make-local-variable 'comment-indent-function) 'tcl-comment-indent)
(set (make-local-variable 'comment-start) "# ")
(set (make-local-variable 'comment-start-skip)
"\\(\\(^\\|[;{[]\\)\\s-*\\)#+ *")
(set (make-local-variable 'comment-end) "")
(set (make-local-variable 'outline-regexp) ".")
(set (make-local-variable 'outline-level) 'tcl-outline-level)
(set (make-local-variable 'font-lock-defaults)
'(tcl-font-lock-keywords nil nil nil beginning-of-defun
(font-lock-syntactic-keywords . tcl-font-lock-syntactic-keywords)
(parse-sexp-lookup-properties . t)))
(set (make-local-variable 'imenu-generic-expression)
tcl-imenu-generic-expression)
(set (make-local-variable 'dabbrev-case-fold-search) nil)
(set (make-local-variable 'dabbrev-case-replace) nil)
(set (make-local-variable 'dabbrev-abbrev-skip-leading-regexp) "[$!]")
(set (make-local-variable 'dabbrev-abbrev-char-regexp) "\\sw\\|\\s_")
(set (make-local-variable 'parse-sexp-ignore-comments) t)
(set (make-local-variable 'defun-prompt-regexp) tcl-omit-ws-regexp)
(set (make-local-variable 'add-log-current-defun-function)
'tcl-add-log-defun)
(easy-menu-add tcl-mode-menu)
(if (boundp 'mode-popup-menu)
(setq mode-popup-menu
(cons (concat mode-name " Mode Commands") tcl-mode-menu))))
(defun tcl-electric-char (arg)
"Insert character and correct line's indentation."
(interactive "p")
(tcl-indent-line)
(self-insert-command arg)
(if (and tcl-auto-newline (= last-command-char ?\ (progn
(newline)
(tcl-indent-line))))
(defun tcl-electric-brace (arg)
"Insert character and correct line's indentation."
(interactive "p")
(if tcl-auto-newline
(progn
(if (save-excursion
(skip-chars-backward " \t")
(bolp))
()
(tcl-indent-line)
(newline))
(while (> arg 0)
(insert last-command-char)
(tcl-indent-line)
(newline)
(setq arg (1- arg))))
(self-insert-command arg))
(tcl-indent-line))
(defun tcl-indent-command (&optional arg)
"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 indentation
area at the beginning of the line, a TAB is inserted.
Other values of `tcl-tab-always-indent' cause the first possible action
from 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")
(if (memq tcl-tab-always-indent '(nil t))
(let ((tab-always-indent tcl-tab-always-indent))
(call-interactively 'indent-for-tab-command))
(let* ((ipoint (point))
(eolpoint (progn
(end-of-line)
(point)))
(comment-p (tcl-in-comment)))
(cond
((= ipoint (line-beginning-position))
(beginning-of-line)
(tcl-indent-line)
(if (= (point) (line-beginning-position))
(end-of-line)
(back-to-indentation)))
((and comment-p (looking-at "[ \t]*$"))
(backward-char)
(skip-chars-backward " \t;")
(delete-region (point) eolpoint))
((and comment-p (< ipoint (point)))
(tcl-indent-line)
(indent-for-comment))
((/= ipoint eolpoint)
(goto-char eolpoint)
(tcl-indent-line))
((not comment-p)
(tcl-indent-line)
(comment-indent))
(t
(tcl-indent-line)
(indent-for-comment))))))
(defun tcl-indent-line ()
"Indent current line as Tcl code.
Return the amount the indentation changed by."
(let ((indent (tcl-calculate-indent nil))
beg shift-amt
(case-fold-search nil)
(pos (- (point-max) (point))))
(if (null indent)
'noindent
(beginning-of-line)
(setq beg (point))
(skip-chars-forward " \t")
(if (listp indent) (setq indent (car indent)))
(cond ((= (following-char) ?})
(setq indent (- indent tcl-indent-level)))
((= (following-char) ?\])
(setq indent (- indent 1))))
(skip-chars-forward " \t")
(setq shift-amt (- indent (current-column)))
(if (zerop shift-amt)
(if (> (- (point-max) pos) (point))
(goto-char (- (point-max) pos)))
(delete-region beg (point))
(indent-to indent)
(if (> (- (point-max) pos) (point))
(goto-char (- (point-max) pos))))
shift-amt)))
(defun tcl-figure-type ()
"Determine type of sexp at point.
This is either `tcl-expr', `tcl-commands', or nil. Puts point at start
of sexp that indicates types.
See documentation for variable `tcl-type-alist' for more information."
(let ((count 0)
result
word-stack)
(while (and (< count 5)
(not result))
(condition-case nil
(progn
(backward-sexp)
(if (looking-at "[a-zA-Z_]+")
(let ((list tcl-type-alist)
entry)
(setq word-stack (cons (tcl-word-no-props) word-stack))
(while (and list (not result))
(setq entry (car list))
(setq list (cdr list))
(let ((index 0))
(while (and entry (<= index count))
(and (stringp (car entry))
(not (string= (car entry)
(nth index word-stack)))
(setq entry nil))
(setq entry (cdr entry))
(setq index (1+ index)))
(and (> index count)
(not (stringp (car entry)))
(setq result (car entry)))
)))
(setq word-stack (cons nil word-stack))))
(error nil))
(setq count (1+ count)))
(and tcl-explain-indentation
(message "Indentation type %s" result))
result))
(defun tcl-calculate-indent (&optional parse-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-search nil)
(continued-line
(save-excursion
(if (bobp)
nil
(backward-char)
(= ?\\ (preceding-char)))))
(continued-indent-value (if continued-line
tcl-continued-indent-level
0))
state
containing-sexp
found-next-line)
(if parse-start
(goto-char parse-start)
(beginning-of-defun))
(while (< (point) indent-point)
(setq parse-start (point))
(setq state (parse-partial-sexp (point) indent-point 0))
(setq containing-sexp (car (cdr state))))
(cond ((or (nth 3 state) (nth 4 state))
(nth 4 state))
((null containing-sexp)
continued-indent-value)
(t
(goto-char containing-sexp)
(let* ((sexpr-type (tcl-figure-type))
(expr-p (eq sexpr-type 'tcl-expr))
(commands-p (eq sexpr-type 'tcl-commands))
(expr-start (point)))
(goto-char containing-sexp)
(forward-char)
(if (and (< (point) indent-point)
(looking-at "[ \t]*\\(#.*\\)?$"))
(progn
(forward-line)
(while (and (< (point) indent-point)
(looking-at "[ \t]*\\(#.*\\)?$"))
(setq found-next-line t)
(forward-line))))
(if (or continued-line
(/= (char-after containing-sexp) ?{)
expr-p)
(progn
(if (>= (point) indent-point)
(progn
(goto-char containing-sexp)
(forward-char))
(skip-chars-forward " \t"))
(+ (current-column) continued-indent-value))
(if (and (< (point) indent-point)
found-next-line)
(current-indentation)
(if commands-p
(goto-char expr-start)
(goto-char containing-sexp))
(+ (current-indentation) tcl-indent-level)))))))))
(defun tcl-indent-exp ()
"Indent each line of the Tcl grouping following point."
(interactive)
(let ((indent-stack (list nil))
(contain-stack (list (point)))
(case-fold-search nil)
outer-loop-done inner-loop-done state ostate
this-indent continued-line
(next-depth 0)
last-depth)
(save-excursion
(forward-sexp 1))
(save-excursion
(setq outer-loop-done nil)
(while (and (not (eobp)) (not outer-loop-done))
(setq last-depth next-depth)
(setq inner-loop-done nil)
(while (and (not inner-loop-done)
(not (and (eobp) (setq outer-loop-done t))))
(setq ostate state)
(setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
nil nil state))
(setq next-depth (car state))
(if (or (nth 4 ostate))
(tcl-indent-line))
(if (or (nth 3 state))
(forward-line 1)
(setq inner-loop-done t)))
(if (<= next-depth 0)
(setq outer-loop-done t))
(if outer-loop-done
nil
(while (> last-depth (nth 6 state))
(setq indent-stack (cdr indent-stack)
contain-stack (cdr contain-stack)
last-depth (1- last-depth)))
(while (< last-depth next-depth)
(setq indent-stack (cons nil indent-stack)
contain-stack (cons nil contain-stack)
last-depth (1+ last-depth)))
(if (null (car contain-stack))
(setcar contain-stack
(or (car (cdr state))
(save-excursion
(forward-sexp -1)
(point)))))
(forward-line 1)
(setq continued-line
(save-excursion
(backward-char)
(= (preceding-char) ?\\)))
(skip-chars-forward " \t")
(if (eolp)
nil
(if (and (car indent-stack)
(>= (car indent-stack) 0))
(setq this-indent (car indent-stack))
(let ((val (tcl-calculate-indent
(if (car indent-stack)
(- (car indent-stack))))))
(setcar indent-stack
(setq this-indent val))
(setq continued-line nil)))
(cond ((not (numberp this-indent)))
((= (following-char) ?})
(setq this-indent (- this-indent tcl-indent-level)))
((= (following-char) ?\])
(setq this-indent (- this-indent 1))))
(or (null this-indent)
(= (current-column)
(if continued-line
(+ this-indent tcl-indent-level)
this-indent))
(progn
(delete-region (point) (progn (beginning-of-line) (point)))
(indent-to
(if continued-line
(+ this-indent tcl-indent-level)
this-indent)))))))))
)
(defun tcl-add-log-defun ()
"Return name of Tcl function point is in, or nil."
(save-excursion
(end-of-line)
(if (re-search-backward (concat tcl-proc-regexp "\\([^ \t\n{]+\\)") nil t)
(match-string 2))))
(defun tcl-outline-level ()
(save-excursion
(skip-chars-forward " \t")
(current-column)))
(defvar inferior-tcl-delete-prompt-marker nil)
(defun tcl-filter (proc string)
(let ((inhibit-quit t))
(with-current-buffer (process-buffer proc)
(goto-char (process-mark proc))
(if (marker-buffer inferior-tcl-delete-prompt-marker)
(progn
(delete-region (point) inferior-tcl-delete-prompt-marker)
(set-marker inferior-tcl-delete-prompt-marker nil)))))
(comint-output-filter proc string))
(defun tcl-send-string (proc string)
(with-current-buffer (process-buffer proc)
(goto-char (process-mark proc))
(forward-line 0) (if (looking-at comint-prompt-regexp)
(set-marker inferior-tcl-delete-prompt-marker (point))))
(comint-send-string proc string))
(defun tcl-send-region (proc start end)
(with-current-buffer (process-buffer proc)
(goto-char (process-mark proc))
(forward-line 0) (if (looking-at comint-prompt-regexp)
(set-marker inferior-tcl-delete-prompt-marker (point))))
(comint-send-region proc start end))
(defun switch-to-tcl (eob-p)
"Switch to inferior Tcl process buffer.
With argument, positions cursor at end of buffer."
(interactive "P")
(if (get-buffer inferior-tcl-buffer)
(pop-to-buffer inferior-tcl-buffer)
(error "No current inferior Tcl buffer"))
(cond (eob-p
(push-mark)
(goto-char (point-max)))))
(defun inferior-tcl-proc ()
"Return current inferior Tcl process.
See variable `inferior-tcl-buffer'."
(let ((proc (get-buffer-process (if (eq major-mode 'inferior-tcl-mode)
(current-buffer)
inferior-tcl-buffer))))
(or proc
(error "No Tcl process; see variable `inferior-tcl-buffer'"))))
(defun tcl-eval-region (start end &optional and-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-region
proc
(save-excursion (goto-char start) (skip-chars-forward " \t\n") (point))
(save-excursion (goto-char end) (skip-chars-backward " \t\n") (point)))
(tcl-send-string proc "\n")
(if and-go (switch-to-tcl t))))
(defun tcl-eval-defun (&optional and-go)
"Send the current defun to the inferior Tcl process.
Prefix argument means switch to the Tcl buffer afterwards."
(interactive "P")
(save-excursion
(end-of-defun)
(let ((end (point)))
(beginning-of-defun)
(tcl-eval-region (point) end)))
(if and-go (switch-to-tcl t)))
(define-derived-mode inferior-tcl-mode comint-mode "Inferior Tcl"
"Major mode for interacting with Tcl interpreter.
You can start a Tcl process 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 buffers
containing 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}"
(set (make-local-variable 'comint-prompt-regexp)
(or tcl-prompt-regexp
(concat "^" (regexp-quote tcl-application) ">")))
(setq mode-line-process '(": %s"))
(setq local-abbrev-table tcl-mode-abbrev-table)
(set-syntax-table tcl-mode-syntax-table)
(set (make-local-variable 'defun-prompt-regexp) tcl-omit-ws-regexp)
(set (make-local-variable 'inferior-tcl-delete-prompt-marker) (make-marker))
(set-process-filter (get-buffer-process (current-buffer)) 'tcl-filter))
(defun inferior-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 (if current-prefix-arg
(read-string "Run Tcl: " tcl-application)
tcl-application)))
(unless (comint-check-proc "*inferior-tcl*")
(set-buffer (apply (function make-comint) "inferior-tcl" cmd nil
tcl-command-switches))
(inferior-tcl-mode)
(unless (process-tty-name (inferior-tcl-proc))
(tcl-send-string (inferior-tcl-proc)
"set ::tcl_interactive 1; concat\n")))
(set (make-local-variable 'tcl-application) cmd)
(setq inferior-tcl-buffer "*inferior-tcl*")
(pop-to-buffer "*inferior-tcl*"))
(defalias 'run-tcl 'inferior-tcl)
(defun tcl-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 the
first word following a semicolon, opening brace, or opening bracket."
(save-excursion
(skip-chars-backward " \t")
(cond
((bobp) t)
((bolp)
(backward-char)
(not (eq ?\\ (preceding-char))))
(t
(memq (preceding-char) '(?\
(defun tcl-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 preceding
semicolon, opening brace, or opening bracket on the same line."
(save-excursion
(backward-char)
(tcl-real-command-p)))
(defun tcl-hairy-scan-for-comment (state end always-stop)
"Determine if point is in a comment.
Returns a list of the form `(FLAG . STATE)'. STATE can be used
as 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', a
simpler version that is often right, and works in Emacs 18."
(let ((bol (save-excursion
(goto-char end)
(beginning-of-line)
(point)))
real-comment
last-cstart)
(while (and (not last-cstart) (< (point) end))
(setq real-comment nil) (setq state (parse-partial-sexp (point) end nil nil state t))
(if (nth 4 state)
(progn
(if always-stop (setq last-cstart (point)))
(setq real-comment (tcl-real-comment-p))
(if real-comment
(progn
(and (> (point) bol) (setq last-cstart (point)))
(beginning-of-line 2)))
(setcar (nthcdr 4 state) nil))))
(and last-cstart
(goto-char last-cstart))
(cons real-comment state)))
(defun tcl-in-comment ()
"Return t if point is in a comment, and leave point at beginning of comment."
(let ((save (point)))
(beginning-of-defun)
(car (tcl-hairy-scan-for-comment nil save nil))))
(defvar tcl-help-saved-dirs nil
"Saved help directories.
If `tcl-help-directory-list' changes, this allows `tcl-help-on-word'
to update the alist.")
(defvar tcl-help-alist nil
"Alist with command names as keys and filenames as values.")
(defun tcl-files-alist (dir &optional alist)
"Recursively add all pairs (FILE . PATH) under DIR to ALIST."
(dolist (file (directory-files dir t) alist)
(cond
((not (file-directory-p file))
(push (cons (file-name-nondirectory file) file) alist))
((member (file-name-nondirectory file) '("." "..")))
(t (setq alist (tcl-files-alist file alist))))))
(defun tcl-help-snarf-commands (dirlist)
"Return alist of commands and filenames."
(let ((alist nil))
(dolist (dir dirlist alist)
(when (file-directory-p dir)
(setq alist (tcl-files-alist dir alist))))))
(defun tcl-reread-help-files ()
"Set up to re-read files, and then do it."
(interactive)
(message "Building Tcl help file index...")
(setq tcl-help-saved-dirs tcl-help-directory-list)
(setq tcl-help-alist (tcl-help-snarf-commands tcl-help-directory-list))
(message "Building Tcl help file index...done"))
(defun tcl-word-no-props ()
"Like `current-word', but strips properties."
(let ((word (current-word)))
(set-text-properties 0 (length word) nil word)
word))
(defun tcl-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 (and flag
(memq major-mode '(tcl-mode inferior-tcl-mode)))
(condition-case nil
(save-excursion
(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)))
(error nil))
(tcl-word-no-props)))
(defun tcl-help-on-word (command &optional arg)
"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 (equal tcl-help-directory-list tcl-help-saved-dirs))
(tcl-reread-help-files))
(let ((word (tcl-current-word
(if current-prefix-arg
(not tcl-use-smart-word-finder)
tcl-use-smart-word-finder))))
(completing-read
(if (or (null word) (string= word ""))
"Help on Tcl command: "
(format "Help on Tcl command (default %s): " word))
tcl-help-alist nil t nil nil word)))
current-prefix-arg))
(if (not (equal tcl-help-directory-list tcl-help-saved-dirs))
(tcl-reread-help-files))
(if (string= command "")
(setq command (tcl-current-word
(if arg
(not tcl-use-smart-word-finder)
tcl-use-smart-word-finder))))
(let* ((help (get-buffer-create "*Tcl help*"))
(cell (assoc command tcl-help-alist))
(file (and cell (cdr cell))))
(set-buffer help)
(delete-region (point-min) (point-max))
(if file
(progn
(insert "*** " command "\n\n")
(insert-file-contents file))
(if (string= command "")
(insert "Magical Pig!")
(insert "Tcl command " command " not in help\n")))
(set-buffer-modified-p nil)
(goto-char (point-min))
(display-buffer help)))
(defvar tcl-previous-dir/file nil
"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.")
(defun tcl-load-file (file &optional and-go)
"Load a Tcl file into the inferior Tcl process.
Prefix argument means switch to the Tcl buffer afterwards."
(interactive
(list
(car (comint-get-source "Load Tcl file: "
(or (and
(eq major-mode 'tcl-mode)
(buffer-file-name))
tcl-previous-dir/file)
'(tcl-mode) t))
current-prefix-arg))
(comint-check-source file)
(setq tcl-previous-dir/file (cons (file-name-directory file)
(file-name-nondirectory file)))
(tcl-send-string (inferior-tcl-proc)
(format inferior-tcl-source-command (tcl-quote file)))
(if and-go (switch-to-tcl t)))
(defun tcl-restart-with-file (file &optional and-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
(eq major-mode 'tcl-mode)
(buffer-file-name))
tcl-previous-dir/file)
'(tcl-mode) t))
current-prefix-arg))
(let* ((buf (if (eq major-mode 'inferior-tcl-mode)
(current-buffer)
inferior-tcl-buffer))
(proc (and buf (get-process buf))))
(cond
((not (and buf (get-buffer buf)))
(inferior-tcl tcl-application)
(tcl-load-file file and-go))
((or
(not (comint-check-proc buf))
(yes-or-no-p
"A Tcl process is running, are you sure you want to reset it? "))
(save-excursion
(comint-check-source file)
(setq tcl-previous-dir/file (cons (file-name-directory file)
(file-name-nondirectory file)))
(comint-exec (get-buffer-create buf)
(if proc
(process-name proc)
"inferior-tcl")
tcl-application file tcl-command-switches)
(if and-go (switch-to-tcl t)))))))
(defun tcl-auto-fill-mode (&optional arg)
"Like `auto-fill-mode', but sets `comment-auto-fill-only-comments'."
(interactive "P")
(auto-fill-mode arg)
(if auto-fill-function
(set (make-local-variable 'comment-auto-fill-only-comments) t)
(kill-local-variable 'comment-auto-fill-only-comments)))
(defun tcl-electric-hash (&optional count)
"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 quoting
styles."
(interactive "p")
(or count (setq count 1))
(if (> count 0)
(let ((type
(if (eq tcl-electric-hash-style 'smart)
(if (> count 3) 'quote
'backslash)
tcl-electric-hash-style))
comment)
(if type
(progn
(save-excursion
(insert "#")
(setq comment (tcl-in-comment)))
(delete-char 1)
(and tcl-explain-indentation (message "comment: %s" comment))
(cond
((eq type 'quote)
(if (not comment)
(insert "\"")))
((eq type 'backslash)
(if (not comment)
(while (> count 0)
(insert "\\#")
(setq count (1- count)))))
(t nil))))
(insert-char ?# count))))
(defun tcl-hashify-buffer ()
"Quote all `#'s in current buffer that aren't Tcl comments."
(interactive)
(save-excursion
(goto-char (point-min))
(let (state
result)
(while (< (point) (point-max))
(setq result (tcl-hairy-scan-for-comment state (point-max) t))
(if (car result)
(beginning-of-line 2)
(backward-char)
(if (eq ?# (following-char))
(insert "\\"))
(forward-char))
(setq state (cdr result))))))
(defun tcl-comment-indent ()
"Return the desired indentation, but be careful to add a `;' if needed."
(save-excursion
(skip-chars-backward " \t")
(unless (or (bolp) (tcl-real-command-p))
(insert ";")
(if (memq (char-after) '(?\t ?\s)) (delete-char 1))))
(funcall (default-value 'comment-indent-function)))
(defun tcl-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]\\|$\\)")
(set (make-local-variable 'tcl-application) (match-string 1)))))
(defun tcl-popup-menu (e)
(interactive "@e")
(popup-menu tcl-mode-menu))
(defun tcl-quote (string)
"Quote STRING according to Tcl rules."
(mapconcat (lambda (char)
(if (memq char '(?[ ?] ?{ ?} ?\\ ?\" ?$ ?\s ?\;))
(concat "\\" (char-to-string char))
(char-to-string char)))
string ""))
;;
;; Bug reporting.
;;
;; These are relics kept "just in case".
(defalias 'tcl-uncomment-region 'uncomment-region)
(defalias 'tcl-indent-for-comment 'comment-indent)
(defalias 'add-log-tcl-defun 'tcl-add-log-defun)
(defalias 'indent-tcl-exp 'tcl-indent-exp)
(defalias 'calculate-tcl-indent 'tcl-calculate-indent)
(defalias 'tcl-beginning-of-defun 'beginning-of-defun)
(defalias 'tcl-end-of-defun 'end-of-defun)
(defalias 'tcl-mark-defun 'mark-defun)
(defun tcl-mark () (mark t))
(provide 'tcl)
;; arch-tag: 8a032554-c3ef-422e-b84c-acec0522179d
;;; tcl.el ends here