tcl.el 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548
  1. ;;; tcl.el --- Tcl code editing commands for Emacs
  2. ;; Copyright (C) 1994, 1998-2012 Free Software Foundation, Inc.
  3. ;; Maintainer: FSF
  4. ;; Author: Tom Tromey <tromey@redhat.com>
  5. ;; Chris Lindblad <cjl@lcs.mit.edu>
  6. ;; Keywords: languages tcl modes
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;; BEFORE USE:
  19. ;;
  20. ;; If you plan to use the interface to the TclX help files, you must
  21. ;; set the variable tcl-help-directory-list to point to the topmost
  22. ;; directories containing the TclX help files. Eg:
  23. ;;
  24. ;; (setq tcl-help-directory-list '("/usr/local/lib/tclx/help"))
  25. ;;
  26. ;;; Commentary:
  27. ;; CUSTOMIZATION NOTES:
  28. ;; * tcl-proc-list can be used to customize a list of things that
  29. ;; "define" other things. Eg in my project I put "defvar" in this
  30. ;; list.
  31. ;; * tcl-typeword-list is similar, but uses font-lock-type-face.
  32. ;; * tcl-keyword-list is a list of keywords. I've generally used this
  33. ;; for flow-control words. Eg I add "unwind_protect" to this list.
  34. ;; * tcl-builtin-list lists commands to be given font-lock-builtin-face.
  35. ;; * tcl-type-alist can be used to minimally customize indentation
  36. ;; according to context.
  37. ;; THANKS FOR CRITICISM AND SUGGESTIONS TO:
  38. ;; Guido Bosch <Guido.Bosch@loria.fr>
  39. ;; pgs1002@esc.cam.ac.uk (Dr P.G. Sjoerdsma)
  40. ;; Mike Scheidler <c23mts@kocrsv01.delcoelect.com>
  41. ;; Matt Newman <men@charney.colorado.edu>
  42. ;; rwhitby@research.canon.oz.au (Rod Whitby)
  43. ;; h9118101@hkuxa.hku.hk (Yip Chi Lap [Beta])
  44. ;; Pertti Tapio Kasanen <ptk@delta.hut.fi>
  45. ;; schmid@fb3-s7.math.TU-Berlin.DE (Gregor Schmid)
  46. ;; warsaw@nlm.nih.gov (Barry A. Warsaw)
  47. ;; Carl Witty <cwitty@ai.mit.edu>
  48. ;; T. V. Raman <raman@crl.dec.com>
  49. ;; Jesper Pedersen <blackie@imada.ou.dk>
  50. ;; dfarmer@evolving.com (Doug Farmer)
  51. ;; "Chris Alfeld" <calfeld@math.utah.edu>
  52. ;; Ben Wing <ben@xemacs.org>
  53. ;; KNOWN BUGS:
  54. ;; * In Tcl "#" is not always a comment character. This can confuse tcl.el
  55. ;; in certain circumstances. For now the only workaround is to use
  56. ;; font-lock which will mark the # chars accordingly or enclose offending
  57. ;; hash characters in quotes or precede them with a backslash. Note that
  58. ;; using braces won't work -- quotes change the syntax class of characters
  59. ;; between them, while braces do not. If you don't use font-lock, the
  60. ;; electric-# mode helps alleviate this problem somewhat.
  61. ;; * indent-tcl-exp is untested.
  62. ;; TODO:
  63. ;; * make add-log-tcl-defun smarter. should notice if we are in the
  64. ;; middle of a defun, or between defuns. should notice if point is
  65. ;; on first line of defun (or maybe even in comments before defun).
  66. ;; * Allow continuation lines to be indented under the first argument
  67. ;; of the preceding line, like this:
  68. ;; [list something \
  69. ;; something-else]
  70. ;; * There is a request that indentation work like this:
  71. ;; button .fred -label Fred \
  72. ;; -command {puts fred}
  73. ;; * Should have tcl-complete-symbol that queries the inferior process.
  74. ;; * Should have describe-symbol that works by sending the magic
  75. ;; command to a tclX process.
  76. ;; * Need C-x C-e binding (tcl-eval-last-exp).
  77. ;; * Write indent-region function that is faster than indenting each
  78. ;; line individually.
  79. ;; * tcl-figure-type should stop at "beginning of line" (only ws
  80. ;; before point, and no "\" on previous line). (see tcl-real-command-p).
  81. ;; * overrides some comint keybindings; fix.
  82. ;; * Trailing \ will eat blank lines. Should deal with this.
  83. ;; (this would help catch some potential bugs).
  84. ;; * Inferior should display in half the screen, not the whole screen.
  85. ;; * Indentation should deal with "switch".
  86. ;; * Consider writing code to find help files automatically (for
  87. ;; common cases).
  88. ;; * `#' shouldn't insert `\#' when point is in string.
  89. ;;; Code:
  90. (eval-when-compile
  91. (require 'imenu)
  92. (require 'outline)
  93. (require 'dabbrev)
  94. (require 'add-log))
  95. (require 'comint)
  96. ;;
  97. ;; User variables.
  98. ;;
  99. (defgroup tcl nil
  100. "Major mode for editing Tcl source in Emacs."
  101. :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
  102. :group 'languages)
  103. (defcustom tcl-indent-level 4
  104. "*Indentation of Tcl statements with respect to containing block."
  105. :type 'integer
  106. :group 'tcl)
  107. (put 'tcl-indent-level 'safe-local-variable 'integerp)
  108. (defcustom tcl-continued-indent-level 4
  109. "*Indentation of continuation line relative to first line of command."
  110. :type 'integer
  111. :group 'tcl)
  112. (put 'tcl-continued-indent-level 'safe-local-variable 'integerp)
  113. (defcustom tcl-auto-newline nil
  114. "*Non-nil means automatically newline before and after braces you insert."
  115. :type 'boolean
  116. :group 'tcl)
  117. (defcustom tcl-tab-always-indent tab-always-indent
  118. "*Control effect of TAB key.
  119. If t (the default), always indent current line.
  120. If nil and point is not in the indentation area at the beginning of
  121. the line, a TAB is inserted.
  122. Other values cause the first possible action from the following list
  123. to take place:
  124. 1. Move from beginning of line to correct indentation.
  125. 2. Delete an empty comment.
  126. 3. Move forward to start of comment, indenting if necessary.
  127. 4. Move forward to end of line, indenting if necessary.
  128. 5. Create an empty comment.
  129. 6. Move backward to start of comment, indenting if necessary."
  130. :type '(choice (const :tag "Always" t)
  131. (const :tag "Beginning only" nil)
  132. (const :tag "Maybe move or make or delete comment" 'tcl))
  133. :group 'tcl)
  134. (defcustom tcl-electric-hash-style nil ;; 'smart
  135. "*Style of electric hash insertion to use.
  136. Possible values are `backslash', meaning that `\\' quoting should be
  137. done; `quote', meaning that `\"' quoting should be done; `smart',
  138. meaning that the choice between `backslash' and `quote' should be
  139. made depending on the number of hashes inserted; or nil, meaning that
  140. no quoting should be done. Any other value for this variable is
  141. taken to mean `smart'. The default is nil."
  142. :type '(choice (const backslash) (const quote) (const smart) (const nil))
  143. :group 'tcl)
  144. (defcustom tcl-help-directory-list nil
  145. "*List of topmost directories containing TclX help files."
  146. :type '(repeat directory)
  147. :group 'tcl)
  148. (defcustom tcl-use-smart-word-finder t
  149. "*If not nil, use smart way to find current word, for Tcl help feature."
  150. :type 'boolean
  151. :group 'tcl)
  152. (defcustom tcl-application "wish"
  153. "*Name of Tcl program to run in inferior Tcl mode."
  154. :type 'string
  155. :group 'tcl)
  156. (defcustom tcl-command-switches nil
  157. "*List of switches to supply to the `tcl-application' program."
  158. :type '(repeat string)
  159. :group 'tcl)
  160. (defcustom tcl-prompt-regexp "^\\(% \\|\\)"
  161. "*If not nil, a regexp that will match the prompt in the inferior process.
  162. If nil, the prompt is the name of the application with \">\" appended.
  163. The default is \"^\\(% \\|\\)\", which will match the default primary
  164. and secondary prompts for tclsh and wish."
  165. :type 'regexp
  166. :group 'tcl)
  167. (defcustom inferior-tcl-source-command "source %s\n"
  168. "*Format-string for building a Tcl command to load a file.
  169. This format string should use `%s' to substitute a file name
  170. and should result in a Tcl expression that will command the
  171. inferior Tcl to load that file. The filename will be appropriately
  172. quoted for Tcl."
  173. :type 'string
  174. :group 'tcl)
  175. (defface tcl-escaped-newline '((t :inherit font-lock-string-face))
  176. "Face used for (non-escaped) backslash at end of a line in Tcl mode."
  177. :group 'tcl
  178. :version "22.1")
  179. ;;
  180. ;; Keymaps, abbrevs, syntax tables.
  181. ;;
  182. (defvar tcl-mode-map
  183. (let ((map (make-sparse-keymap)))
  184. (define-key map "{" 'tcl-electric-char)
  185. (define-key map "}" 'tcl-electric-brace)
  186. (define-key map "[" 'tcl-electric-char)
  187. (define-key map "]" 'tcl-electric-char)
  188. (define-key map ";" 'tcl-electric-char)
  189. (define-key map "#" 'tcl-electric-hash) ;Remove? -stef
  190. (define-key map "\e\C-q" 'tcl-indent-exp)
  191. (define-key map "\177" 'backward-delete-char-untabify)
  192. (define-key map "\t" 'tcl-indent-command)
  193. (define-key map "\M-\C-x" 'tcl-eval-defun)
  194. (define-key map "\C-c\C-i" 'tcl-help-on-word)
  195. (define-key map "\C-c\C-v" 'tcl-eval-defun)
  196. (define-key map "\C-c\C-f" 'tcl-load-file)
  197. (define-key map "\C-c\C-t" 'inferior-tcl)
  198. (define-key map "\C-c\C-x" 'tcl-eval-region)
  199. (define-key map "\C-c\C-s" 'switch-to-tcl)
  200. map)
  201. "Keymap used in `tcl-mode'.")
  202. (defvar tcl-mode-syntax-table
  203. (let ((st (make-syntax-table)))
  204. (modify-syntax-entry ?% "_" st)
  205. (modify-syntax-entry ?@ "_" st)
  206. (modify-syntax-entry ?& "_" st)
  207. (modify-syntax-entry ?* "_" st)
  208. (modify-syntax-entry ?+ "_" st)
  209. (modify-syntax-entry ?- "_" st)
  210. (modify-syntax-entry ?. "_" st)
  211. (modify-syntax-entry ?: "_" st)
  212. (modify-syntax-entry ?! "_" st)
  213. (modify-syntax-entry ?$ "_" st) ; FIXME use "'"?
  214. (modify-syntax-entry ?/ "_" st)
  215. (modify-syntax-entry ?~ "_" st)
  216. (modify-syntax-entry ?< "_" st)
  217. (modify-syntax-entry ?= "_" st)
  218. (modify-syntax-entry ?> "_" st)
  219. (modify-syntax-entry ?| "_" st)
  220. (modify-syntax-entry ?\( "()" st)
  221. (modify-syntax-entry ?\) ")(" st)
  222. (modify-syntax-entry ?\; "." st)
  223. (modify-syntax-entry ?\n ">" st)
  224. ;; (modify-syntax-entry ?\f ">" st)
  225. (modify-syntax-entry ?# "<" st)
  226. st)
  227. "Syntax table in use in `tcl-mode' buffers.")
  228. (defvar inferior-tcl-mode-map
  229. ;; FIXME we override comint keybindings here.
  230. ;; Maybe someone has a better set?
  231. (let ((map (make-sparse-keymap)))
  232. ;; Will inherit from `comint-mode-map' thanks to define-derived-mode.
  233. (define-key map "\t" 'comint-dynamic-complete)
  234. (define-key map "\M-?" 'comint-dynamic-list-filename-completions)
  235. (define-key map "\177" 'backward-delete-char-untabify)
  236. (define-key map "\M-\C-x" 'tcl-eval-defun)
  237. (define-key map "\C-c\C-i" 'tcl-help-on-word)
  238. (define-key map "\C-c\C-v" 'tcl-eval-defun)
  239. (define-key map "\C-c\C-f" 'tcl-load-file)
  240. (define-key map "\C-c\C-t" 'inferior-tcl)
  241. (define-key map "\C-c\C-x" 'tcl-eval-region)
  242. (define-key map "\C-c\C-s" 'switch-to-tcl)
  243. map)
  244. "Keymap used in `inferior-tcl-mode'.")
  245. (easy-menu-define tcl-mode-menu tcl-mode-map "Menu used in `tcl-mode'."
  246. '("Tcl"
  247. ["Beginning of function" beginning-of-defun t]
  248. ["End of function" end-of-defun t]
  249. ["Mark function" mark-defun t]
  250. ["Indent region" indent-region (mark t)]
  251. ["Comment region" comment-region (mark t)]
  252. ["Uncomment region" uncomment-region (mark t)]
  253. "----"
  254. ["Show Tcl process buffer" inferior-tcl t]
  255. ["Send function to Tcl process" tcl-eval-defun
  256. (and inferior-tcl-buffer (get-buffer inferior-tcl-buffer))]
  257. ["Send region to Tcl process" tcl-eval-region
  258. (and inferior-tcl-buffer (get-buffer inferior-tcl-buffer))]
  259. ["Send file to Tcl process" tcl-load-file
  260. (and inferior-tcl-buffer (get-buffer inferior-tcl-buffer))]
  261. ["Restart Tcl process with file" tcl-restart-with-file t]
  262. "----"
  263. ["Tcl help" tcl-help-on-word tcl-help-directory-list]))
  264. (defvar inferior-tcl-buffer nil
  265. "*The current inferior-tcl process buffer.
  266. MULTIPLE PROCESS SUPPORT
  267. ===========================================================================
  268. To run multiple Tcl processes, you start the first up with
  269. \\[inferior-tcl]. It will be in a buffer named `*inferior-tcl*'.
  270. Rename this buffer with \\[rename-buffer]. You may now start up a new
  271. process with another \\[inferior-tcl]. It will be in a new buffer,
  272. named `*inferior-tcl*'. You can switch between the different process
  273. buffers with \\[switch-to-buffer].
  274. Commands that send text from source buffers to Tcl processes -- like
  275. `tcl-eval-defun' or `tcl-load-file' -- have to choose a process to
  276. send to, when you have more than one Tcl process around. This is
  277. determined by the global variable `inferior-tcl-buffer'. Suppose you
  278. have three inferior Lisps running:
  279. Buffer Process
  280. foo inferior-tcl
  281. bar inferior-tcl<2>
  282. *inferior-tcl* inferior-tcl<3>
  283. If you do a \\[tcl-eval-defun] command on some Lisp source code, what
  284. process do you send it to?
  285. - If you're in a process buffer (foo, bar, or *inferior-tcl*),
  286. you send it to that process.
  287. - If you're in some other buffer (e.g., a source file), you
  288. send it to the process attached to buffer `inferior-tcl-buffer'.
  289. This process selection is performed by function `inferior-tcl-proc'.
  290. Whenever \\[inferior-tcl] fires up a new process, it resets
  291. `inferior-tcl-buffer' to be the new process's buffer. If you only run
  292. one process, this does the right thing. If you run multiple
  293. processes, you can change `inferior-tcl-buffer' to another process
  294. buffer with \\[set-variable].")
  295. ;;
  296. ;; Hooks and other customization.
  297. ;;
  298. (defvar tcl-mode-hook nil
  299. "Hook run on entry to Tcl mode.
  300. Several functions exist which are useful to run from your
  301. `tcl-mode-hook' (see each function's documentation for more
  302. information):
  303. `tcl-guess-application'
  304. Guesses a default setting for `tcl-application' based on any
  305. \"#!\" line at the top of the file.
  306. `tcl-hashify-buffer'
  307. Quotes all \"#\" characters that don't correspond to actual
  308. Tcl comments. (Useful when editing code not originally created
  309. with this mode).
  310. `tcl-auto-fill-mode'
  311. Auto-filling of Tcl comments.
  312. Add functions to the hook with `add-hook':
  313. (add-hook 'tcl-mode-hook 'tcl-guess-application)")
  314. (defvar tcl-proc-list
  315. '("proc" "method" "itcl_class" "body" "configbody" "class")
  316. "List of commands whose first argument defines something.
  317. This exists because some people (eg, me) use `defvar' et al.
  318. Call `tcl-set-proc-regexp' and `tcl-set-font-lock-keywords'
  319. after changing this list.")
  320. (defvar tcl-proc-regexp nil
  321. "Regexp to use when matching proc headers.")
  322. (defvar tcl-typeword-list
  323. '("global" "upvar" "inherit" "public" "protected" "private"
  324. "common" "itk_option" "variable")
  325. "List of Tcl keywords denoting \"type\". Used only for highlighting.
  326. Call `tcl-set-font-lock-keywords' after changing this list.")
  327. ;; Generally I've picked control operators to be keywords.
  328. (defvar tcl-keyword-list
  329. '("if" "then" "else" "elseif" "for" "foreach" "break" "continue" "while"
  330. "eval" "case" "in" "switch" "default" "exit" "error" "proc" "return"
  331. "uplevel" "constructor" "destructor" "itcl_class" "loop" "for_array_keys"
  332. "for_recursive_glob" "for_file" "method" "body" "configbody" "class"
  333. "chain")
  334. "List of Tcl keywords. Used only for highlighting.
  335. Default list includes some TclX keywords.
  336. Call `tcl-set-font-lock-keywords' after changing this list.")
  337. (defvar tcl-builtin-list
  338. '("after" "append" "array" "bgerror" "binary" "catch" "cd" "clock"
  339. "close" "concat" "console" "dde" "encoding" "eof" "exec" "expr"
  340. "fblocked" "fconfigure" "fcopy" "file" "fileevent" "flush"
  341. "format" "gets" "glob" "history" "incr" "info" "interp" "join"
  342. "lappend" "lindex" "linsert" "list" "llength" "load" "lrange"
  343. "lreplace" "lsort" "namespace" "open" "package" "pid" "puts" "pwd"
  344. "read" "regexp" "registry" "regsub" "rename" "scan" "seek" "set"
  345. "socket" "source" "split" "string" "subst" "tell" "time" "trace"
  346. "unknown" "unset" "vwait")
  347. "List of Tcl commands. Used only for highlighting.
  348. Call `tcl-set-font-lock-keywords' after changing this list.
  349. This list excludes those commands already found in `tcl-proc-list' and
  350. `tcl-keyword-list'.")
  351. (defvar tcl-font-lock-keywords nil
  352. "Keywords to highlight for Tcl. See variable `font-lock-keywords'.
  353. This variable is generally set from `tcl-proc-regexp',
  354. `tcl-typeword-list', and `tcl-keyword-list' by the function
  355. `tcl-set-font-lock-keywords'.")
  356. (defconst tcl-syntax-propertize-function
  357. (syntax-propertize-rules
  358. ;; Mark the few `#' that are not comment-markers.
  359. ("[^;[{ \t\n][ \t]*\\(#\\)" (1 ".")))
  360. "Syntactic keywords for `tcl-mode'.")
  361. ;; FIXME need some way to recognize variables because array refs look
  362. ;; like 2 sexps.
  363. (defvar tcl-type-alist
  364. '(("proc" nil tcl-expr tcl-commands)
  365. ("method" nil tcl-expr tcl-commands)
  366. ("destructor" tcl-commands)
  367. ("constructor" tcl-commands)
  368. ("expr" tcl-expr)
  369. ("catch" tcl-commands)
  370. ("if" tcl-expr "then" tcl-commands)
  371. ("elseif" tcl-expr "then" tcl-commands)
  372. ("elseif" tcl-expr tcl-commands)
  373. ("if" tcl-expr tcl-commands)
  374. ("while" tcl-expr tcl-commands)
  375. ("for" tcl-commands tcl-expr tcl-commands tcl-commands)
  376. ("foreach" nil nil tcl-commands)
  377. ("for_file" nil nil tcl-commands)
  378. ("for_array_keys" nil nil tcl-commands)
  379. ("for_recursive_glob" nil nil nil tcl-commands)
  380. ;; Loop handling is not perfect, because the third argument can be
  381. ;; either a command or an expr, and there is no real way to look
  382. ;; forward.
  383. ("loop" nil tcl-expr tcl-expr tcl-commands)
  384. ("loop" nil tcl-expr tcl-commands))
  385. "Alist that controls indentation.
  386. \(Actually, this really only controls what happens on continuation lines).
  387. Each entry looks like `(KEYWORD TYPE ...)'.
  388. Each type entry describes a sexp after the keyword, and can be one of:
  389. * nil, meaning that this sexp has no particular type.
  390. * tcl-expr, meaning that this sexp is an arithmetic expression.
  391. * tcl-commands, meaning that this sexp holds Tcl commands.
  392. * a string, which must exactly match the string at the corresponding
  393. position for a match to be made.
  394. For example, the entry for the \"loop\" command is:
  395. (\"loop\" nil tcl-expr tcl-commands)
  396. This means that the \"loop\" command has three arguments. The first
  397. argument is ignored (for indentation purposes). The second argument
  398. is a Tcl expression, and the last argument is Tcl commands.")
  399. (defvar tcl-explain-indentation nil
  400. "If non-nil, debugging message will be printed during indentation.")
  401. ;; Here's another stab. I think this one actually works.
  402. ;; We have to be careful that the open-brace following this regexp
  403. ;; is indeed the one corresponding to the function's body so
  404. ;; that end-of-defun works correctly. Tricky cases are:
  405. ;; proc foo { {arg1 def} arg2 } {
  406. ;; as well as
  407. ;; proc foo { \n {arg1 def} \n arg2 } {
  408. ;; The current setting handles the first case properly but not the second.
  409. ;; It also fails if `proc' is not in column-0 (e.g. it's in a namespace).
  410. (defconst tcl-omit-ws-regexp "^[^]\" \t\n#}][^\n\"#]+[ \t]+")
  411. ;;
  412. ;; Some helper functions.
  413. ;;
  414. (defun tcl-set-proc-regexp ()
  415. "Set `tcl-proc-regexp' from variable `tcl-proc-list'."
  416. (setq tcl-proc-regexp
  417. (concat "^\\s-*" (regexp-opt tcl-proc-list t) "[ \t]+")))
  418. (defun tcl-set-font-lock-keywords ()
  419. "Set `tcl-font-lock-keywords'.
  420. Uses variables `tcl-proc-regexp' and `tcl-keyword-list'."
  421. (setq tcl-font-lock-keywords
  422. (list
  423. ;; Names of functions (and other "defining things").
  424. (list (concat tcl-proc-regexp "\\([^ \t\n]+\\)")
  425. 2 'font-lock-function-name-face)
  426. ;; Names of type-defining things.
  427. (list (concat "\\(\\s-\\|^\\)"
  428. (regexp-opt tcl-typeword-list t)
  429. "\\(\\s-\\|$\\)")
  430. 2 'font-lock-type-face)
  431. (list (concat "\\_<" (regexp-opt tcl-builtin-list t) "\\_>")
  432. 1 'font-lock-builtin-face)
  433. ;; When variable names are enclosed in {} braces, any
  434. ;; character can be used. Otherwise just letters, digits,
  435. ;; underscores. Variable names can be prefixed with any
  436. ;; number of "namespace::" qualifiers. A leading "::" refers
  437. ;; to the global namespace.
  438. '("\\${\\([^}]+\\)}" 1 font-lock-variable-name-face)
  439. '("\\$\\(\\(?:::\\)?\\(?:[[:alnum:]_]+::\\)*[[:alnum:]_]+\\)"
  440. 1 font-lock-variable-name-face)
  441. '("\\(?:\\s-\\|^\\|\\[\\)set\\s-+{\\([^}]+\\)}"
  442. 1 font-lock-variable-name-face keep)
  443. '("\\(?:\\s-\\|^\\|\\[\\)set\\s-+\\(\\(?:::\\)?\
  444. \\(?:[[:alnum:]_]+::\\)*[[:alnum:]_]+\\)"
  445. 1 font-lock-variable-name-face keep)
  446. '("\\(^\\|[^\\]\\)\\(\\\\\\\\\\)*\\(\\\\\\)$" 3 'tcl-escaped-newline)
  447. ;; Keywords. Only recognized if surrounded by whitespace.
  448. ;; FIXME consider using "not word or symbol", not
  449. ;; "whitespace".
  450. (cons (concat "\\_<" (regexp-opt tcl-keyword-list t) "\\_>")
  451. 1))))
  452. (if tcl-proc-regexp
  453. ()
  454. (tcl-set-proc-regexp))
  455. (if tcl-font-lock-keywords
  456. ()
  457. (tcl-set-font-lock-keywords))
  458. (defvar tcl-imenu-generic-expression
  459. `((nil ,(concat tcl-proc-regexp "\\([-A-Za-z0-9_:+*]+\\)") 2))
  460. "Imenu generic expression for `tcl-mode'. See `imenu-generic-expression'.")
  461. ;;
  462. ;; The mode itself.
  463. ;;
  464. ;;;###autoload
  465. (define-derived-mode tcl-mode prog-mode "Tcl"
  466. "Major mode for editing Tcl code.
  467. Expression and list commands understand all Tcl brackets.
  468. Tab indents for Tcl code.
  469. Paragraphs are separated by blank lines only.
  470. Delete converts tabs to spaces as it moves back.
  471. Variables controlling indentation style:
  472. `tcl-indent-level'
  473. Indentation of Tcl statements within surrounding block.
  474. `tcl-continued-indent-level'
  475. Indentation of continuation line relative to first line of command.
  476. Variables controlling user interaction with mode (see variable
  477. documentation for details):
  478. `tcl-tab-always-indent'
  479. Controls action of TAB key.
  480. `tcl-auto-newline'
  481. Non-nil means automatically newline before and after braces, brackets,
  482. and semicolons inserted in Tcl code.
  483. `tcl-use-smart-word-finder'
  484. If not nil, use a smarter, Tcl-specific way to find the current
  485. word when looking up help on a Tcl command.
  486. Turning on Tcl mode runs `tcl-mode-hook'. Read the documentation for
  487. `tcl-mode-hook' to see what kinds of interesting hook functions
  488. already exist."
  489. (unless (and (boundp 'filladapt-mode) filladapt-mode)
  490. (set (make-local-variable 'paragraph-ignore-fill-prefix) t))
  491. (set (make-local-variable 'indent-line-function) 'tcl-indent-line)
  492. (set (make-local-variable 'comment-indent-function) 'tcl-comment-indent)
  493. ;; Tcl doesn't require a final newline.
  494. ;; (make-local-variable 'require-final-newline)
  495. ;; (setq require-final-newline t)
  496. (set (make-local-variable 'comment-start) "# ")
  497. (set (make-local-variable 'comment-start-skip)
  498. "\\(\\(^\\|[;{[]\\)\\s-*\\)#+ *")
  499. (set (make-local-variable 'comment-end) "")
  500. (set (make-local-variable 'outline-regexp) ".")
  501. (set (make-local-variable 'outline-level) 'tcl-outline-level)
  502. (set (make-local-variable 'font-lock-defaults)
  503. '(tcl-font-lock-keywords nil nil nil beginning-of-defun))
  504. (set (make-local-variable 'syntax-propertize-function)
  505. tcl-syntax-propertize-function)
  506. (set (make-local-variable 'imenu-generic-expression)
  507. tcl-imenu-generic-expression)
  508. ;; Settings for new dabbrev code.
  509. (set (make-local-variable 'dabbrev-case-fold-search) nil)
  510. (set (make-local-variable 'dabbrev-case-replace) nil)
  511. (set (make-local-variable 'dabbrev-abbrev-skip-leading-regexp) "[$!]")
  512. (set (make-local-variable 'dabbrev-abbrev-char-regexp) "\\sw\\|\\s_")
  513. (set (make-local-variable 'parse-sexp-ignore-comments) t)
  514. ;; XEmacs has defun-prompt-regexp, but I don't believe
  515. ;; that it works for end-of-defun -- only for
  516. ;; beginning-of-defun.
  517. (set (make-local-variable 'defun-prompt-regexp) tcl-omit-ws-regexp)
  518. (set (make-local-variable 'add-log-current-defun-function)
  519. 'tcl-add-log-defun)
  520. (easy-menu-add tcl-mode-menu)
  521. ;; Append Tcl menu to popup menu for XEmacs.
  522. (if (boundp 'mode-popup-menu)
  523. (setq mode-popup-menu
  524. (cons (concat mode-name " Mode Commands") tcl-mode-menu))))
  525. ;; This is used for braces, brackets, and semi (except for closing
  526. ;; braces, which are handled specially).
  527. (defun tcl-electric-char (arg)
  528. "Insert character and correct line's indentation."
  529. (interactive "p")
  530. ;; Indent line first; this looks better if parens blink.
  531. (tcl-indent-line)
  532. (self-insert-command arg)
  533. (if (and tcl-auto-newline (= last-command-event ?\;))
  534. (progn
  535. (newline)
  536. (tcl-indent-line))))
  537. ;; This is used for closing braces. If tcl-auto-newline is set, can
  538. ;; insert a newline both before and after the brace, depending on
  539. ;; context. FIXME should this be configurable? Does anyone use this?
  540. (defun tcl-electric-brace (arg)
  541. "Insert character and correct line's indentation."
  542. (interactive "p")
  543. ;; If auto-newlining and there is stuff on the same line, insert a
  544. ;; newline first.
  545. (if tcl-auto-newline
  546. (progn
  547. (if (save-excursion
  548. (skip-chars-backward " \t")
  549. (bolp))
  550. ()
  551. (tcl-indent-line)
  552. (newline))
  553. ;; In auto-newline case, must insert a newline after each
  554. ;; brace. So an explicit loop is needed.
  555. (while (> arg 0)
  556. (insert last-command-event)
  557. (tcl-indent-line)
  558. (newline)
  559. (setq arg (1- arg))))
  560. (self-insert-command arg))
  561. (tcl-indent-line))
  562. (defun tcl-indent-command (&optional _arg)
  563. "Indent current line as Tcl code, or in some cases insert a tab character.
  564. If `tcl-tab-always-indent' is t (the default), always indent current line.
  565. If `tcl-tab-always-indent' is nil and point is not in the indentation
  566. area at the beginning of the line, a TAB is inserted.
  567. Other values of `tcl-tab-always-indent' cause the first possible action
  568. from the following list to take place:
  569. 1. Move from beginning of line to correct indentation.
  570. 2. Delete an empty comment.
  571. 3. Move forward to start of comment, indenting if necessary.
  572. 4. Move forward to end of line, indenting if necessary.
  573. 5. Create an empty comment.
  574. 6. Move backward to start of comment, indenting if necessary."
  575. (interactive "p")
  576. (if (memq tcl-tab-always-indent '(nil t))
  577. (let ((tab-always-indent tcl-tab-always-indent))
  578. (call-interactively 'indent-for-tab-command))
  579. ;; "Perl-mode" style TAB command.
  580. (let* ((ipoint (point))
  581. (eolpoint (progn
  582. (end-of-line)
  583. (point)))
  584. (comment-p (tcl-in-comment)))
  585. (cond
  586. ((= ipoint (line-beginning-position))
  587. (beginning-of-line)
  588. (tcl-indent-line)
  589. ;; If indenting didn't leave us in column 0, go to the
  590. ;; indentation. Otherwise leave point at end of line. This
  591. ;; is a hack.
  592. (if (= (point) (line-beginning-position))
  593. (end-of-line)
  594. (back-to-indentation)))
  595. ((and comment-p (looking-at "[ \t]*$"))
  596. ;; Empty comment, so delete it. We also delete any ";"
  597. ;; characters at the end of the line. I think this is
  598. ;; friendlier, but I don't know how other people will feel.
  599. (backward-char)
  600. (skip-chars-backward " \t;")
  601. (delete-region (point) eolpoint))
  602. ((and comment-p (< ipoint (point)))
  603. ;; Before comment, so skip to it.
  604. (tcl-indent-line)
  605. (indent-for-comment))
  606. ((/= ipoint eolpoint)
  607. ;; Go to end of line (since we're not there yet).
  608. (goto-char eolpoint)
  609. (tcl-indent-line))
  610. ((not comment-p)
  611. (tcl-indent-line)
  612. (comment-indent))
  613. (t
  614. ;; Go to start of comment. We don't leave point where it is
  615. ;; because we want to skip comment-start-skip.
  616. (tcl-indent-line)
  617. (indent-for-comment))))))
  618. (defun tcl-indent-line ()
  619. "Indent current line as Tcl code.
  620. Return the amount the indentation changed by."
  621. (let ((indent (tcl-calculate-indent nil))
  622. beg shift-amt
  623. (case-fold-search nil)
  624. (pos (- (point-max) (point))))
  625. (if (null indent)
  626. 'noindent
  627. (beginning-of-line)
  628. (setq beg (point))
  629. (skip-chars-forward " \t")
  630. (if (listp indent) (setq indent (car indent)))
  631. (cond ((= (following-char) ?})
  632. (setq indent (- indent tcl-indent-level)))
  633. ((= (following-char) ?\])
  634. (setq indent (- indent 1))))
  635. (skip-chars-forward " \t")
  636. (setq shift-amt (- indent (current-column)))
  637. (if (zerop shift-amt)
  638. (if (> (- (point-max) pos) (point))
  639. (goto-char (- (point-max) pos)))
  640. (delete-region beg (point))
  641. (indent-to indent)
  642. ;; If initial point was within line's indentation,
  643. ;; position after the indentation. Else stay at same point in text.
  644. (if (> (- (point-max) pos) (point))
  645. (goto-char (- (point-max) pos))))
  646. shift-amt)))
  647. (defun tcl-figure-type ()
  648. "Determine type of sexp at point.
  649. This is either `tcl-expr', `tcl-commands', or nil. Puts point at start
  650. of sexp that indicates types.
  651. See documentation for variable `tcl-type-alist' for more information."
  652. (let ((count 0)
  653. result
  654. word-stack)
  655. (while (and (< count 5)
  656. (not result))
  657. (condition-case nil
  658. (progn
  659. ;; FIXME should use "tcl-backward-sexp", which would skip
  660. ;; over entire variables, etc.
  661. (backward-sexp)
  662. (if (looking-at "[a-zA-Z_]+")
  663. (let ((list tcl-type-alist)
  664. entry)
  665. (setq word-stack (cons (tcl-word-no-props) word-stack))
  666. (while (and list (not result))
  667. (setq entry (car list))
  668. (setq list (cdr list))
  669. (let ((index 0))
  670. (while (and entry (<= index count))
  671. ;; Abort loop if string does not match word on
  672. ;; stack.
  673. (and (stringp (car entry))
  674. (not (string= (car entry)
  675. (nth index word-stack)))
  676. (setq entry nil))
  677. (setq entry (cdr entry))
  678. (setq index (1+ index)))
  679. (and (> index count)
  680. (not (stringp (car entry)))
  681. (setq result (car entry)))
  682. )))
  683. (setq word-stack (cons nil word-stack))))
  684. (error nil))
  685. (setq count (1+ count)))
  686. (and tcl-explain-indentation
  687. (message "Indentation type %s" result))
  688. result))
  689. (defun tcl-calculate-indent (&optional parse-start)
  690. "Return appropriate indentation for current line as Tcl code.
  691. In usual case returns an integer: the column to indent to.
  692. Returns nil if line starts inside a string, t if in a comment."
  693. (save-excursion
  694. (beginning-of-line)
  695. (let* ((indent-point (point))
  696. (case-fold-search nil)
  697. (continued-line
  698. (save-excursion
  699. (if (bobp)
  700. nil
  701. (backward-char)
  702. (= ?\\ (preceding-char)))))
  703. (continued-indent-value (if continued-line
  704. tcl-continued-indent-level
  705. 0))
  706. state
  707. containing-sexp
  708. found-next-line)
  709. (if parse-start
  710. (goto-char parse-start)
  711. (beginning-of-defun))
  712. (while (< (point) indent-point)
  713. (setq parse-start (point))
  714. (setq state (parse-partial-sexp (point) indent-point 0))
  715. (setq containing-sexp (car (cdr state))))
  716. (cond ((or (nth 3 state) (nth 4 state))
  717. ;; Inside comment or string. Return nil or t if should
  718. ;; not change this line
  719. (nth 4 state))
  720. ((null containing-sexp)
  721. ;; Line is at top level.
  722. continued-indent-value)
  723. (t
  724. ;; Set expr-p if we are looking at the expression part of
  725. ;; an "if", "expr", etc statement. Set commands-p if we
  726. ;; are looking at the body part of an if, while, etc
  727. ;; statement. FIXME Should check for "for" loops here.
  728. (goto-char containing-sexp)
  729. (let* ((sexpr-type (tcl-figure-type))
  730. (expr-p (eq sexpr-type 'tcl-expr))
  731. (commands-p (eq sexpr-type 'tcl-commands))
  732. (expr-start (point)))
  733. ;; Find the first statement in the block and indent
  734. ;; like it. The first statement in the block might be
  735. ;; on the same line, so what we do is skip all
  736. ;; "virtually blank" lines, looking for a non-blank
  737. ;; one. A line is virtually blank if it only contains
  738. ;; a comment and whitespace. FIXME continued comments
  739. ;; aren't supported. They are a wart on Tcl anyway.
  740. ;; We do it this funky way because we want to know if
  741. ;; we've found a statement on some line _after_ the
  742. ;; line holding the sexp opener.
  743. (goto-char containing-sexp)
  744. (forward-char)
  745. (if (and (< (point) indent-point)
  746. (looking-at "[ \t]*\\(#.*\\)?$"))
  747. (progn
  748. (forward-line)
  749. (while (and (< (point) indent-point)
  750. (looking-at "[ \t]*\\(#.*\\)?$"))
  751. (setq found-next-line t)
  752. (forward-line))))
  753. (if (or continued-line
  754. (/= (char-after containing-sexp) ?{)
  755. expr-p)
  756. (progn
  757. ;; Line is continuation line, or the sexp opener
  758. ;; is not a curly brace, or we are looking at
  759. ;; an `expr' expression (which must be split
  760. ;; specially). So indentation is column of first
  761. ;; good spot after sexp opener (with some added
  762. ;; in the continued-line case). If there is no
  763. ;; nonempty line before the indentation point, we
  764. ;; use the column of the character after the sexp
  765. ;; opener.
  766. (if (>= (point) indent-point)
  767. (progn
  768. (goto-char containing-sexp)
  769. (forward-char))
  770. (skip-chars-forward " \t"))
  771. (+ (current-column) continued-indent-value))
  772. ;; After a curly brace, and not a continuation line.
  773. ;; So take indentation from first good line after
  774. ;; start of block, unless that line is on the same
  775. ;; line as the opening brace. In this case use the
  776. ;; indentation of the opening brace's line, plus
  777. ;; another indent step. If we are in the body part
  778. ;; of an "if" or "while" then the indentation is
  779. ;; taken from the line holding the start of the
  780. ;; statement.
  781. (if (and (< (point) indent-point)
  782. found-next-line)
  783. (current-indentation)
  784. (if commands-p
  785. (goto-char expr-start)
  786. (goto-char containing-sexp))
  787. (+ (current-indentation) tcl-indent-level)))))))))
  788. (defun tcl-indent-exp ()
  789. "Indent each line of the Tcl grouping following point."
  790. (interactive)
  791. (let ((indent-stack (list nil))
  792. (contain-stack (list (point)))
  793. (case-fold-search nil)
  794. outer-loop-done inner-loop-done state ostate
  795. this-indent continued-line
  796. (next-depth 0)
  797. last-depth)
  798. (save-excursion
  799. (forward-sexp 1))
  800. (save-excursion
  801. (setq outer-loop-done nil)
  802. (while (and (not (eobp)) (not outer-loop-done))
  803. (setq last-depth next-depth)
  804. ;; Compute how depth changes over this line
  805. ;; plus enough other lines to get to one that
  806. ;; does not end inside a comment or string.
  807. ;; Meanwhile, do appropriate indentation on comment lines.
  808. (setq inner-loop-done nil)
  809. (while (and (not inner-loop-done)
  810. (not (and (eobp) (setq outer-loop-done t))))
  811. (setq ostate state)
  812. (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
  813. nil nil state))
  814. (setq next-depth (car state))
  815. (if (or (nth 4 ostate))
  816. (tcl-indent-line))
  817. (if (or (nth 3 state))
  818. (forward-line 1)
  819. (setq inner-loop-done t)))
  820. (if (<= next-depth 0)
  821. (setq outer-loop-done t))
  822. (if outer-loop-done
  823. nil
  824. ;; If this line had ..))) (((.. in it, pop out of the levels
  825. ;; that ended anywhere in this line, even if the final depth
  826. ;; doesn't indicate that they ended.
  827. (while (> last-depth (nth 6 state))
  828. (setq indent-stack (cdr indent-stack)
  829. contain-stack (cdr contain-stack)
  830. last-depth (1- last-depth)))
  831. ;; Add levels for any parens that were started in this line.
  832. (while (< last-depth next-depth)
  833. (setq indent-stack (cons nil indent-stack)
  834. contain-stack (cons nil contain-stack)
  835. last-depth (1+ last-depth)))
  836. (if (null (car contain-stack))
  837. (setcar contain-stack
  838. (or (car (cdr state))
  839. (save-excursion
  840. (forward-sexp -1)
  841. (point)))))
  842. (forward-line 1)
  843. (setq continued-line
  844. (save-excursion
  845. (backward-char)
  846. (= (preceding-char) ?\\)))
  847. (skip-chars-forward " \t")
  848. (if (eolp)
  849. nil
  850. (if (and (car indent-stack)
  851. (>= (car indent-stack) 0))
  852. ;; Line is on an existing nesting level.
  853. (setq this-indent (car indent-stack))
  854. ;; Just started a new nesting level.
  855. ;; Compute the standard indent for this level.
  856. (let ((val (tcl-calculate-indent
  857. (if (car indent-stack)
  858. (- (car indent-stack))))))
  859. (setcar indent-stack
  860. (setq this-indent val))
  861. (setq continued-line nil)))
  862. (cond ((not (numberp this-indent)))
  863. ((= (following-char) ?})
  864. (setq this-indent (- this-indent tcl-indent-level)))
  865. ((= (following-char) ?\])
  866. (setq this-indent (- this-indent 1))))
  867. ;; Put chosen indentation into effect.
  868. (or (null this-indent)
  869. (= (current-column)
  870. (if continued-line
  871. (+ this-indent tcl-indent-level)
  872. this-indent))
  873. (progn
  874. (delete-region (point) (progn (beginning-of-line) (point)))
  875. (indent-to
  876. (if continued-line
  877. (+ this-indent tcl-indent-level)
  878. this-indent)))))))))
  879. )
  880. ;;
  881. ;; Interfaces to other packages.
  882. ;;
  883. ;; FIXME Definition of function is very ad-hoc. Should use
  884. ;; beginning-of-defun. Also has incestuous knowledge about the
  885. ;; format of tcl-proc-regexp.
  886. (defun tcl-add-log-defun ()
  887. "Return name of Tcl function point is in, or nil."
  888. (save-excursion
  889. (end-of-line)
  890. (if (re-search-backward (concat tcl-proc-regexp "\\([^ \t\n{]+\\)") nil t)
  891. (match-string 2))))
  892. (defun tcl-outline-level ()
  893. (save-excursion
  894. (skip-chars-forward " \t")
  895. (current-column)))
  896. ;;
  897. ;; Helper functions for inferior Tcl mode.
  898. ;;
  899. ;; This exists to let us delete the prompt when commands are sent
  900. ;; directly to the inferior Tcl. See gud.el for an explanation of how
  901. ;; it all works (I took it from there). This stuff doesn't really
  902. ;; work as well as I'd like it to. But I don't believe there is
  903. ;; anything useful that can be done.
  904. (defvar inferior-tcl-delete-prompt-marker nil)
  905. (defun tcl-filter (proc string)
  906. (let ((inhibit-quit t)) ;FIXME: Isn't that redundant?
  907. (with-current-buffer (process-buffer proc)
  908. ;; Delete prompt if requested.
  909. (when (marker-buffer inferior-tcl-delete-prompt-marker)
  910. (delete-region (process-mark proc) inferior-tcl-delete-prompt-marker)
  911. (set-marker inferior-tcl-delete-prompt-marker nil))))
  912. (comint-output-filter proc string))
  913. (defun tcl-send-string (proc string)
  914. (with-current-buffer (process-buffer proc)
  915. (goto-char (process-mark proc))
  916. (forward-line 0) ;Not (beginning-of-line) because of fields.
  917. (if (looking-at comint-prompt-regexp)
  918. (set-marker inferior-tcl-delete-prompt-marker (point))))
  919. (comint-send-string proc string))
  920. (defun tcl-send-region (proc start end)
  921. (with-current-buffer (process-buffer proc)
  922. (goto-char (process-mark proc))
  923. (forward-line 0) ;Not (beginning-of-line) because of fields.
  924. (if (looking-at comint-prompt-regexp)
  925. (set-marker inferior-tcl-delete-prompt-marker (point))))
  926. (comint-send-region proc start end))
  927. (defun switch-to-tcl (eob-p)
  928. "Switch to inferior Tcl process buffer.
  929. With argument, positions cursor at end of buffer."
  930. (interactive "P")
  931. (if (get-buffer inferior-tcl-buffer)
  932. (pop-to-buffer inferior-tcl-buffer)
  933. (error "No current inferior Tcl buffer"))
  934. (cond (eob-p
  935. (push-mark)
  936. (goto-char (point-max)))))
  937. (defun inferior-tcl-proc ()
  938. "Return current inferior Tcl process.
  939. See variable `inferior-tcl-buffer'."
  940. (let ((proc (get-buffer-process (if (derived-mode-p 'inferior-tcl-mode)
  941. (current-buffer)
  942. inferior-tcl-buffer))))
  943. (or proc
  944. (error "No Tcl process; see variable `inferior-tcl-buffer'"))))
  945. (defun tcl-eval-region (start end &optional and-go)
  946. "Send the current region to the inferior Tcl process.
  947. Prefix argument means switch to the Tcl buffer afterwards."
  948. (interactive "r\nP")
  949. (let ((proc (inferior-tcl-proc)))
  950. (tcl-send-region
  951. proc
  952. ;; Strip leading and trailing whitespace.
  953. (save-excursion (goto-char start) (skip-chars-forward " \t\n") (point))
  954. (save-excursion (goto-char end) (skip-chars-backward " \t\n") (point)))
  955. (tcl-send-string proc "\n")
  956. (if and-go (switch-to-tcl t))))
  957. (defun tcl-eval-defun (&optional and-go)
  958. "Send the current defun to the inferior Tcl process.
  959. Prefix argument means switch to the Tcl buffer afterwards."
  960. (interactive "P")
  961. (save-excursion
  962. (end-of-defun)
  963. (let ((end (point)))
  964. (beginning-of-defun)
  965. (tcl-eval-region (point) end)))
  966. (if and-go (switch-to-tcl t)))
  967. ;;
  968. ;; Inferior Tcl mode itself.
  969. ;;
  970. (define-derived-mode inferior-tcl-mode comint-mode "Inferior Tcl"
  971. "Major mode for interacting with Tcl interpreter.
  972. You can start a Tcl process with \\[inferior-tcl].
  973. Entry to this mode runs the normal hooks `comint-mode-hook' and
  974. `inferior-tcl-mode-hook', in that order.
  975. You can send text to the inferior Tcl process from other buffers
  976. containing Tcl source.
  977. Variables controlling Inferior Tcl mode:
  978. `tcl-application'
  979. Name of program to run.
  980. `tcl-command-switches'
  981. Command line arguments to `tcl-application'.
  982. `tcl-prompt-regexp'
  983. Matches prompt.
  984. `inferior-tcl-source-command'
  985. Command to use to read Tcl file in running application.
  986. `inferior-tcl-buffer'
  987. The current inferior Tcl process buffer. See variable
  988. documentation for details on multiple-process support.
  989. The following commands are available:
  990. \\{inferior-tcl-mode-map}"
  991. (set (make-local-variable 'comint-prompt-regexp)
  992. (or tcl-prompt-regexp
  993. (concat "^" (regexp-quote tcl-application) ">")))
  994. (setq mode-line-process '(": %s"))
  995. (setq local-abbrev-table tcl-mode-abbrev-table)
  996. (set-syntax-table tcl-mode-syntax-table)
  997. (set (make-local-variable 'defun-prompt-regexp) tcl-omit-ws-regexp)
  998. (set (make-local-variable 'inferior-tcl-delete-prompt-marker) (make-marker))
  999. (set-process-filter (get-buffer-process (current-buffer)) 'tcl-filter))
  1000. ;;;###autoload
  1001. (defun inferior-tcl (cmd)
  1002. "Run inferior Tcl process.
  1003. Prefix arg means enter program name interactively.
  1004. See documentation for function `inferior-tcl-mode' for more information."
  1005. (interactive
  1006. (list (if current-prefix-arg
  1007. (read-string "Run Tcl: " tcl-application)
  1008. tcl-application)))
  1009. (unless (comint-check-proc "*inferior-tcl*")
  1010. (set-buffer (apply (function make-comint) "inferior-tcl" cmd nil
  1011. tcl-command-switches))
  1012. (inferior-tcl-mode)
  1013. ;; Make tclsh display a prompt on ms-windows (or under Unix, when a tty
  1014. ;; wasn't used). Doesn't affect wish, unfortunately.
  1015. (unless (process-tty-name (inferior-tcl-proc))
  1016. (tcl-send-string (inferior-tcl-proc)
  1017. "set ::tcl_interactive 1; concat\n")))
  1018. (set (make-local-variable 'tcl-application) cmd)
  1019. (setq inferior-tcl-buffer "*inferior-tcl*")
  1020. (pop-to-buffer "*inferior-tcl*"))
  1021. (defalias 'run-tcl 'inferior-tcl)
  1022. ;;
  1023. ;; Auto-fill support.
  1024. ;;
  1025. (defun tcl-real-command-p ()
  1026. "Return nil if point is not at the beginning of a command.
  1027. A command is the first word on an otherwise empty line, or the
  1028. first word following a semicolon, opening brace, or opening bracket."
  1029. (save-excursion
  1030. (skip-chars-backward " \t")
  1031. (cond
  1032. ((bobp) t)
  1033. ((bolp)
  1034. (backward-char)
  1035. ;; Note -- continued comments are not supported here. I
  1036. ;; consider those to be a wart on the language.
  1037. (not (eq ?\\ (preceding-char))))
  1038. (t
  1039. (memq (preceding-char) '(?\; ?{ ?\[))))))
  1040. ;; FIXME doesn't actually return t. See last case.
  1041. (defun tcl-real-comment-p ()
  1042. "Return t if point is just after the `#' beginning a real comment.
  1043. Does not check to see if previous char is actually `#'.
  1044. A real comment is either at the beginning of the buffer,
  1045. preceded only by whitespace on the line, or has a preceding
  1046. semicolon, opening brace, or opening bracket on the same line."
  1047. (save-excursion
  1048. (backward-char)
  1049. (tcl-real-command-p)))
  1050. (defun tcl-hairy-scan-for-comment (state end always-stop)
  1051. "Determine if point is in a comment.
  1052. Returns a list of the form `(FLAG . STATE)'. STATE can be used
  1053. as input to future invocations. FLAG is nil if not in comment,
  1054. t otherwise. If in comment, leaves point at beginning of comment."
  1055. (let ((bol (save-excursion
  1056. (goto-char end)
  1057. (line-beginning-position)))
  1058. real-comment
  1059. last-cstart)
  1060. (while (and (not last-cstart) (< (point) end))
  1061. (setq real-comment nil) ;In case we've looped around and it is set.
  1062. (setq state (parse-partial-sexp (point) end nil nil state t))
  1063. (if (nth 4 state)
  1064. (progn
  1065. ;; If ALWAYS-STOP is set, stop even if we don't have a
  1066. ;; real comment, or if the comment isn't on the same line
  1067. ;; as the end.
  1068. (if always-stop (setq last-cstart (point)))
  1069. ;; If we have a real comment, then set the comment
  1070. ;; starting point if we are on the same line as the ending
  1071. ;; location.
  1072. (setq real-comment (tcl-real-comment-p))
  1073. (if real-comment
  1074. (progn
  1075. (and (> (point) bol) (setq last-cstart (point)))
  1076. ;; NOTE Emacs 19 has a misfeature whereby calling
  1077. ;; parse-partial-sexp with COMMENTSTOP set and with
  1078. ;; an initial list that says point is in a comment
  1079. ;; will cause an immediate return. So we must skip
  1080. ;; over the comment ourselves.
  1081. (beginning-of-line 2)))
  1082. ;; Frob the state to make it look like we aren't in a
  1083. ;; comment.
  1084. (setcar (nthcdr 4 state) nil))))
  1085. (and last-cstart
  1086. (goto-char last-cstart))
  1087. (cons real-comment state)))
  1088. (defun tcl-in-comment ()
  1089. "Return t if point is in a comment, and leave point at beginning of comment."
  1090. (let ((save (point)))
  1091. (beginning-of-defun)
  1092. (car (tcl-hairy-scan-for-comment nil save nil))))
  1093. ;;
  1094. ;; Help-related code.
  1095. ;;
  1096. (defvar tcl-help-saved-dirs nil
  1097. "Saved help directories.
  1098. If `tcl-help-directory-list' changes, this allows `tcl-help-on-word'
  1099. to update the alist.")
  1100. (defvar tcl-help-alist nil
  1101. "Alist with command names as keys and filenames as values.")
  1102. (defun tcl-files-alist (dir &optional alist)
  1103. "Recursively add all pairs (FILE . PATH) under DIR to ALIST."
  1104. (dolist (file (directory-files dir t) alist)
  1105. (cond
  1106. ((not (file-directory-p file))
  1107. (push (cons (file-name-nondirectory file) file) alist))
  1108. ((member (file-name-nondirectory file) '("." "..")))
  1109. (t (setq alist (tcl-files-alist file alist))))))
  1110. (defun tcl-help-snarf-commands (dirlist)
  1111. "Return alist of commands and filenames."
  1112. (let ((alist nil))
  1113. (dolist (dir dirlist alist)
  1114. (when (file-directory-p dir)
  1115. (setq alist (tcl-files-alist dir alist))))))
  1116. (defun tcl-reread-help-files ()
  1117. "Set up to re-read files, and then do it."
  1118. (interactive)
  1119. (message "Building Tcl help file index...")
  1120. (setq tcl-help-saved-dirs tcl-help-directory-list)
  1121. (setq tcl-help-alist (tcl-help-snarf-commands tcl-help-directory-list))
  1122. (message "Building Tcl help file index...done"))
  1123. (defun tcl-word-no-props ()
  1124. "Like `current-word', but strips properties."
  1125. (let ((word (current-word)))
  1126. (set-text-properties 0 (length word) nil word)
  1127. word))
  1128. (defun tcl-current-word (flag)
  1129. "Return current command word, or nil.
  1130. If FLAG is nil, just uses `current-word'.
  1131. Otherwise scans backward for most likely Tcl command word."
  1132. (if (and flag
  1133. (derived-mode-p 'tcl-mode 'inferior-tcl-mode))
  1134. (condition-case nil
  1135. (save-excursion
  1136. ;; Look backward for first word actually in alist.
  1137. (if (bobp)
  1138. ()
  1139. (while (and (not (bobp))
  1140. (not (tcl-real-command-p)))
  1141. (backward-sexp)))
  1142. (if (assoc (tcl-word-no-props) tcl-help-alist)
  1143. (tcl-word-no-props)))
  1144. (error nil))
  1145. (tcl-word-no-props)))
  1146. ;;;###autoload
  1147. (defun tcl-help-on-word (command &optional arg)
  1148. "Get help on Tcl command. Default is word at point.
  1149. Prefix argument means invert sense of `tcl-use-smart-word-finder'."
  1150. (interactive
  1151. (list
  1152. (progn
  1153. (if (not (equal tcl-help-directory-list tcl-help-saved-dirs))
  1154. (tcl-reread-help-files))
  1155. (let ((word (tcl-current-word
  1156. (if current-prefix-arg
  1157. (not tcl-use-smart-word-finder)
  1158. tcl-use-smart-word-finder))))
  1159. (completing-read
  1160. (if (or (null word) (string= word ""))
  1161. "Help on Tcl command: "
  1162. (format "Help on Tcl command (default %s): " word))
  1163. tcl-help-alist nil t nil nil word)))
  1164. current-prefix-arg))
  1165. (if (not (equal tcl-help-directory-list tcl-help-saved-dirs))
  1166. (tcl-reread-help-files))
  1167. (if (string= command "")
  1168. (setq command (tcl-current-word
  1169. (if arg
  1170. (not tcl-use-smart-word-finder)
  1171. tcl-use-smart-word-finder))))
  1172. (let* ((help (get-buffer-create "*Tcl help*"))
  1173. (cell (assoc command tcl-help-alist))
  1174. (file (and cell (cdr cell))))
  1175. (set-buffer help)
  1176. (delete-region (point-min) (point-max))
  1177. (if file
  1178. (progn
  1179. (insert "*** " command "\n\n")
  1180. (insert-file-contents file))
  1181. (if (string= command "")
  1182. (insert "Magical Pig!")
  1183. (insert "Tcl command " command " not in help\n")))
  1184. (set-buffer-modified-p nil)
  1185. (goto-char (point-min))
  1186. (display-buffer help)))
  1187. ;;
  1188. ;; Other interactive stuff.
  1189. ;;
  1190. (defvar tcl-previous-dir/file nil
  1191. "Record last directory and file used in loading.
  1192. This holds a cons cell of the form `(DIRECTORY . FILE)'
  1193. describing the last `tcl-load-file' command.")
  1194. (defun tcl-load-file (file &optional and-go)
  1195. "Load a Tcl file into the inferior Tcl process.
  1196. Prefix argument means switch to the Tcl buffer afterwards."
  1197. (interactive
  1198. (list
  1199. ;; car because comint-get-source returns a list holding the
  1200. ;; filename.
  1201. (car (comint-get-source "Load Tcl file: "
  1202. (or (and
  1203. (derived-mode-p 'tcl-mode)
  1204. (buffer-file-name))
  1205. tcl-previous-dir/file)
  1206. '(tcl-mode) t))
  1207. current-prefix-arg))
  1208. (comint-check-source file)
  1209. (setq tcl-previous-dir/file (cons (file-name-directory file)
  1210. (file-name-nondirectory file)))
  1211. (tcl-send-string (inferior-tcl-proc)
  1212. (format inferior-tcl-source-command (tcl-quote file)))
  1213. (if and-go (switch-to-tcl t)))
  1214. (defun tcl-restart-with-file (file &optional and-go)
  1215. "Restart inferior Tcl with file.
  1216. If an inferior Tcl process exists, it is killed first.
  1217. Prefix argument means switch to the Tcl buffer afterwards."
  1218. (interactive
  1219. (list
  1220. (car (comint-get-source "Restart with Tcl file: "
  1221. (or (and
  1222. (derived-mode-p 'tcl-mode)
  1223. (buffer-file-name))
  1224. tcl-previous-dir/file)
  1225. '(tcl-mode) t))
  1226. current-prefix-arg))
  1227. (let* ((buf (if (derived-mode-p 'inferior-tcl-mode)
  1228. (current-buffer)
  1229. inferior-tcl-buffer))
  1230. (proc (and buf (get-process buf))))
  1231. (cond
  1232. ((not (and buf (get-buffer buf)))
  1233. ;; I think this will be ok.
  1234. (inferior-tcl tcl-application)
  1235. (tcl-load-file file and-go))
  1236. ((or
  1237. (not (comint-check-proc buf))
  1238. (yes-or-no-p
  1239. "A Tcl process is running, are you sure you want to reset it? "))
  1240. (save-excursion
  1241. (comint-check-source file)
  1242. (setq tcl-previous-dir/file (cons (file-name-directory file)
  1243. (file-name-nondirectory file)))
  1244. (comint-exec (get-buffer-create buf)
  1245. (if proc
  1246. (process-name proc)
  1247. "inferior-tcl")
  1248. tcl-application file tcl-command-switches)
  1249. (if and-go (switch-to-tcl t)))))))
  1250. (defun tcl-auto-fill-mode (&optional arg)
  1251. "Like `auto-fill-mode', but sets `comment-auto-fill-only-comments'."
  1252. (interactive "P")
  1253. (auto-fill-mode arg)
  1254. (if auto-fill-function
  1255. (set (make-local-variable 'comment-auto-fill-only-comments) t)
  1256. (kill-local-variable 'comment-auto-fill-only-comments)))
  1257. (defun tcl-electric-hash (&optional count)
  1258. "Insert a `#' and quote if it does not start a real comment.
  1259. Prefix arg is number of `#'s to insert.
  1260. See variable `tcl-electric-hash-style' for description of quoting
  1261. styles."
  1262. (interactive "p")
  1263. (or count (setq count 1))
  1264. (if (> count 0)
  1265. (let ((type
  1266. (if (eq tcl-electric-hash-style 'smart)
  1267. (if (> count 3) ; FIXME what is "smart"?
  1268. 'quote
  1269. 'backslash)
  1270. tcl-electric-hash-style))
  1271. comment)
  1272. (if type
  1273. (progn
  1274. (save-excursion
  1275. (insert "#")
  1276. (setq comment (tcl-in-comment)))
  1277. (delete-char 1)
  1278. (and tcl-explain-indentation (message "comment: %s" comment))
  1279. (cond
  1280. ((eq type 'quote)
  1281. (if (not comment)
  1282. (insert "\"")))
  1283. ((eq type 'backslash)
  1284. ;; The following will set count to 0, so the
  1285. ;; insert-char can still be run.
  1286. (if (not comment)
  1287. (while (> count 0)
  1288. (insert "\\#")
  1289. (setq count (1- count)))))
  1290. (t nil))))
  1291. (insert-char ?# count))))
  1292. (defun tcl-hashify-buffer ()
  1293. "Quote all `#'s in current buffer that aren't Tcl comments."
  1294. (interactive)
  1295. (save-excursion
  1296. (goto-char (point-min))
  1297. (let (state
  1298. result)
  1299. (while (< (point) (point-max))
  1300. (setq result (tcl-hairy-scan-for-comment state (point-max) t))
  1301. (if (car result)
  1302. (beginning-of-line 2)
  1303. (backward-char)
  1304. (if (eq ?# (following-char))
  1305. (insert "\\"))
  1306. (forward-char))
  1307. (setq state (cdr result))))))
  1308. (defun tcl-comment-indent ()
  1309. "Return the desired indentation, but be careful to add a `;' if needed."
  1310. (save-excursion
  1311. ;; If line is not blank, make sure we insert a ";" first.
  1312. (skip-chars-backward " \t")
  1313. (unless (or (bolp) (tcl-real-command-p))
  1314. (insert ";")
  1315. ;; Try and erase a non-significant char to keep charpos identical.
  1316. (if (memq (char-after) '(?\t ?\s)) (delete-char 1))))
  1317. (funcall (default-value 'comment-indent-function)))
  1318. ;; The following was inspired by the Tcl editing mode written by
  1319. ;; Gregor Schmid <schmid@fb3-s7.math.TU-Berlin.DE>. His version also
  1320. ;; attempts to snarf the command line options from the command line,
  1321. ;; but I didn't think that would really be that helpful (doesn't seem
  1322. ;; like it would be right enough. His version also looks for the
  1323. ;; "#!/bin/csh ... exec" hack, but that seemed even less useful.
  1324. ;; FIXME should make sure that the application mentioned actually
  1325. ;; exists.
  1326. (defun tcl-guess-application ()
  1327. "Attempt to guess Tcl application by looking at first line.
  1328. The first line is assumed to look like \"#!.../program ...\"."
  1329. (save-excursion
  1330. (goto-char (point-min))
  1331. (if (looking-at "#![^ \t]*/\\([^ \t\n/]+\\)\\([ \t]\\|$\\)")
  1332. (set (make-local-variable 'tcl-application) (match-string 1)))))
  1333. ;;
  1334. ;; XEmacs menu support.
  1335. ;; Taken from schmid@fb3-s7.math.TU-Berlin.DE (Gregor Schmid),
  1336. ;; who wrote a different Tcl mode.
  1337. ;; We also have support for menus in Emacs. We do this by
  1338. ;; loading the XEmacs menu emulation code.
  1339. ;;
  1340. (defun tcl-popup-menu (_e)
  1341. (interactive "@e")
  1342. (popup-menu tcl-mode-menu))
  1343. ;;
  1344. ;; Quoting and unquoting functions.
  1345. ;;
  1346. ;; This quoting is sufficient to protect eg a filename from any sort
  1347. ;; of expansion or splitting. Tcl quoting sure sucks.
  1348. (defun tcl-quote (string)
  1349. "Quote STRING according to Tcl rules."
  1350. (mapconcat (lambda (char)
  1351. (if (memq char '(?[ ?] ?{ ?} ?\\ ?\" ?$ ?\s ?\;))
  1352. (concat "\\" (char-to-string char))
  1353. (char-to-string char)))
  1354. string ""))
  1355. ;;
  1356. ;; Bug reporting.
  1357. ;;
  1358. ;; These are relics kept "just in case".
  1359. (defalias 'tcl-uncomment-region 'uncomment-region)
  1360. (defalias 'tcl-indent-for-comment 'comment-indent)
  1361. (defalias 'add-log-tcl-defun 'tcl-add-log-defun)
  1362. (defalias 'indent-tcl-exp 'tcl-indent-exp)
  1363. (defalias 'calculate-tcl-indent 'tcl-calculate-indent)
  1364. (defalias 'tcl-beginning-of-defun 'beginning-of-defun)
  1365. (defalias 'tcl-end-of-defun 'end-of-defun)
  1366. (defalias 'tcl-mark-defun 'mark-defun)
  1367. (defun tcl-mark () (mark t))
  1368. (provide 'tcl)
  1369. ;;; tcl.el ends here