tcl.el 54 KB

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