PageRenderTime 37ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 1ms

/plugins/nxhtml/related/smarty-mode.el

http://github.com/spastorino/my_emacs_for_rails
Emacs Lisp | 2745 lines | 2231 code | 387 blank | 127 comment | 73 complexity | d98779a1cf7c467fc8881c07c74c72f2 MD5 | raw file
Possible License(s): GPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. ;;; smarty-mode.el --- major mode for editing Smarty templates
  2. ;; Author: Vincent DEBOUT <deboutv@free.fr>
  3. ;; Maintainer: Vincent DEBOUT <deboutv@free.fr>
  4. ;; Keywords: languages smarty templates
  5. ;; WWW: http://deboutv.free.fr/lisp/smarty/
  6. ;;; License
  7. ;; This program is free software; you can redistribute it and/or
  8. ;; modify it under the terms of the GNU General Public License
  9. ;; as published by the Free Software Foundation; either version 2
  10. ;; of the License, or (at your option) any later version.
  11. ;; This program is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with this program; if not, write to the Free Software
  17. ;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. (defconst smarty-version "0.0.5"
  19. "Smarty Mode version number.")
  20. (defconst smarty-time-stamp "2007-11-01"
  21. "Smarty Mode time stamp for last update.")
  22. (defconst smarty-is-xemacs (string-match "XEmacs" emacs-version)
  23. "Non-nil if XEmacs is used.")
  24. (require 'font-lock)
  25. (when (not smarty-is-xemacs)
  26. (require 'cc-mode)
  27. (require 'custom)
  28. (require 'etags))
  29. (eval-when-compile
  30. (require 'regexp-opt))
  31. (when smarty-is-xemacs
  32. (require 'easymenu)
  33. (require 'hippie-exp))
  34. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  35. ;;; Customization
  36. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  37. (defgroup smarty nil
  38. "Customizations for Smarty mode."
  39. :prefix "smarty-"
  40. :group 'languages)
  41. (defgroup smarty-mode nil
  42. "Customizations for Smarty mode."
  43. :group 'smarty)
  44. (defcustom smarty-electric-mode t
  45. "*Non-nil enables electrification (automatic template generation).
  46. If nil, template generators can still be invoked through key bindings and
  47. menu. Is indicated in the modeline by \"/e\" after the mode name and can be
  48. toggled by `\\[smarty-electric-mode]'."
  49. :type 'boolean
  50. :group 'smarty-mode)
  51. (defcustom smarty-stutter-mode t
  52. "*Non-nil enables stuttering.
  53. Is indicated in the modeline by \"/s\" after the mode name and can be toggled
  54. by `\\[smarty-stutter-mode]'."
  55. :type 'boolean
  56. :group 'smarty-mode)
  57. (defgroup smarty-menu nil
  58. "Customizations for menues."
  59. :group 'smarty)
  60. (defcustom smarty-source-file-menu t
  61. "*Non-nil means add a menu of all source files in current directory."
  62. :type 'boolean
  63. :group 'smarty-menu)
  64. (defgroup smarty-highlight nil
  65. "Customizations for highlight."
  66. :group 'smarty)
  67. (defcustom smarty-highlight-plugin-functions t
  68. "*Non-nil means highlight the plugin functions in the buffer."
  69. :type 'boolean
  70. :group 'smarty-highlight)
  71. (defgroup smarty-template nil
  72. "Customizations for templates."
  73. :group 'smarty)
  74. (defgroup smarty-header nil
  75. "Customizations for header template."
  76. :group 'smarty-template)
  77. (defcustom smarty-file-header ""
  78. "*String or file to insert as file header.
  79. If the string specifies an existing file name, the contents of the file is
  80. inserted, otherwise the string itself is inserted as file header.
  81. Type `C-j' for newlines.
  82. If the header contains RCS keywords, they may be written as <RCS>Keyword<RCS>
  83. if the header needs to be version controlled.
  84. The following keywords for template generation are supported:
  85. <filename> : replaced by the name of the buffer
  86. <author> : replaced by the user name and email address
  87. \(`user-full-name',`mail-host-address', `user-mail-address')
  88. <login> : replaced by user login name (`user-login-name')
  89. <company> : replaced by contents of option `smarty-company-name'
  90. <date> : replaced by the current date
  91. <year> : replaced by the current year
  92. <copyright> : replaced by copyright string (`smarty-copyright-string')
  93. <cursor> : final cursor position."
  94. :type 'string
  95. :group 'smarty-header)
  96. (defcustom smarty-file-footer ""
  97. "*String or file to insert as file footer.
  98. If the string specifies an existing file name, the contents of the file is
  99. inserted, otherwise the string itself is inserted as file footer (i.e. at
  100. the end of the file).
  101. Type `C-j' for newlines.
  102. The same keywords as in option `smarty-file-header' can be used."
  103. :type 'string
  104. :group 'smarty-header)
  105. (defcustom smarty-company-name ""
  106. "*Name of company to insert in file header.
  107. See option `smarty-file-header'."
  108. :type 'string
  109. :group 'smarty-header)
  110. (defcustom smarty-copyright-string ""
  111. "*Copyright string to insert in file header.
  112. Can be multi-line string (type `C-j' for newline) and contain other file
  113. header keywords (see option `smarty-file-header')."
  114. :type 'string
  115. :group 'smarty-header)
  116. (defcustom smarty-date-format "%Y-%m-%d"
  117. "*Specifies the date format to use in the header.
  118. This string is passed as argument to the command `format-time-string'.
  119. For more information on format strings, see the documentation for the
  120. `format-time-string' command (C-h f `format-time-string')."
  121. :type 'string
  122. :group 'smarty-header)
  123. (defcustom smarty-modify-date-prefix-string ""
  124. "*Prefix string of modification date in Smarty file header.
  125. If actualization of the modification date is called (menu,
  126. `\\[smarty-template-modify]'), this string is searched and the rest
  127. of the line replaced by the current date."
  128. :type 'string
  129. :group 'smarty-header)
  130. (defcustom smarty-modify-date-on-saving nil
  131. "*Non-nil means update the modification date when the buffer is saved.
  132. Calls function `\\[smarty-template-modify]').
  133. NOTE: Activate the new setting in a Smarty buffer by using the menu entry
  134. \"Activate Options\"."
  135. :type 'boolean
  136. :group 'smarty-header)
  137. (defgroup smarty-misc nil
  138. "Miscellaneous customizations."
  139. :group 'smarty)
  140. (defcustom smarty-left-delimiter "{"
  141. "Left escaping delimiter."
  142. :type 'string
  143. :group 'smarty-misc)
  144. (defcustom smarty-right-delimiter "}"
  145. "Right escaping delimiter."
  146. :type 'string
  147. :group 'smarty-misc)
  148. (defcustom smarty-intelligent-tab t
  149. "*Non-nil means `TAB' does indentation, word completion and tab insertion.
  150. That is, if preceding character is part of a word then complete word,
  151. else if not at beginning of line then insert tab,
  152. else if last command was a `TAB' or `RET' then dedent one step,
  153. else indent current line (i.e. `TAB' is bound to `smarty-electric-tab').
  154. If nil, TAB always indents current line (i.e. `TAB' is bound to
  155. `indent-according-to-mode').
  156. NOTE: Activate the new setting in a Smarty buffer by using the menu entry
  157. \"Activate Options\"."
  158. :type 'boolean
  159. :group 'smarty-misc)
  160. (defcustom smarty-word-completion-in-minibuffer t
  161. "*Non-nil enables word completion in minibuffer (for template prompts).
  162. NOTE: Activate the new setting by restarting Emacs."
  163. :type 'boolean
  164. :group 'smarty-misc)
  165. (defcustom smarty-word-completion-case-sensitive nil
  166. "*Non-nil means word completion using `TAB' is case sensitive.
  167. That is, `TAB' completes words that start with the same letters and case.
  168. Otherwise, case is ignored."
  169. :type 'boolean
  170. :group 'smarty-misc)
  171. ;; Functions
  172. (defun smarty-customize ()
  173. "Call the customize function with `smarty' as argument."
  174. (interactive)
  175. (customize-browse 'smarty))
  176. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  177. ;; Variables
  178. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  179. (defvar smarty-menu-max-size 20
  180. "*Specifies the maximum size of a menu before splitting it into submenues.")
  181. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  182. ;; Menu tools functions
  183. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  184. (defun smarty-menu-split (list title)
  185. "Split menu LIST into several submenues, if number of
  186. elements > `smarty-menu-max-size'."
  187. (if (> (length list) smarty-menu-max-size)
  188. (let ((remain list)
  189. (result '())
  190. (sublist '())
  191. (menuno 1)
  192. (i 0))
  193. (while remain
  194. (setq sublist (cons (car remain) sublist))
  195. (setq remain (cdr remain))
  196. (setq i (+ i 1))
  197. (if (= i smarty-menu-max-size)
  198. (progn
  199. (setq result (cons (cons (format "%s %s" title menuno)
  200. (nreverse sublist)) result))
  201. (setq i 0)
  202. (setq menuno (+ menuno 1))
  203. (setq sublist '()))))
  204. (and sublist
  205. (setq result (cons (cons (format "%s %s" title menuno)
  206. (nreverse sublist)) result)))
  207. (nreverse result))
  208. list))
  209. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  210. ;; Source file menu
  211. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  212. (defvar smarty-sources-menu nil)
  213. ;; Create the source menu
  214. (defun smarty-add-source-files-menu ()
  215. "Scan directory for all Smarty source files and generate menu.
  216. The directory of the current source file is scanned."
  217. (interactive)
  218. (message "Scanning directory for source files ...")
  219. (let ((newmap (current-local-map))
  220. (file-list (smarty-get-source-files))
  221. menu-list found)
  222. ;; Create list for menu
  223. (setq found nil)
  224. (while file-list
  225. (setq found t)
  226. (setq menu-list (cons (vector (car file-list)
  227. (list 'find-file (car file-list)) t)
  228. menu-list))
  229. (setq file-list (cdr file-list)))
  230. (setq menu-list (smarty-menu-split menu-list "Sources"))
  231. (when found (setq menu-list (cons "--" menu-list)))
  232. (setq menu-list (cons ["*Rescan*" smarty-add-source-files-menu t] menu-list))
  233. (setq menu-list (cons "Sources" menu-list))
  234. ;; Create menu
  235. (easy-menu-add menu-list)
  236. (easy-menu-define smarty-sources-menu newmap
  237. "Smarty source files menu" menu-list))
  238. (message ""))
  239. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  240. ;; Smarty menu
  241. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  242. (defun smarty-create-mode-menu ()
  243. "Create Smarty Mode menu."
  244. `("Smarty"
  245. ("Templates"
  246. ("Built-in Functions"
  247. ["capture" smarty-template-capture t]
  248. ["config_load" smarty-template-config-load t]
  249. ["else" smarty-template-else t]
  250. ["elseif" smarty-template-elseif t]
  251. ["foreach" smarty-template-foreach t]
  252. ["foreachelse" smarty-template-foreachelse t]
  253. ["if" smarty-template-if t]
  254. ["include" smarty-template-include t]
  255. ["include_php" smarty-template-include-php t]
  256. ["insert" smarty-template-insert t]
  257. ["ldelim" smarty-template-ldelim t]
  258. ["literal" smarty-template-literal t]
  259. ["php" smarty-template-php t]
  260. ["rdelim" smarty-template-rdelim t]
  261. ["section" smarty-template-section t]
  262. ["sectionelse" smarty-template-sectionelse t]
  263. ["strip" smarty-template-strip t])
  264. ("Custom Functions"
  265. ["assign" smarty-template-assign t]
  266. ["counter" smarty-template-counter t]
  267. ["cycle" smarty-template-cycle t]
  268. ["debug" smarty-template-debug t]
  269. ["eval" smarty-template-eval t]
  270. ["fetch" smarty-template-fetch t]
  271. ["html_checkboxes" smarty-template-html-checkboxes t]
  272. ["html_image" smarty-template-html-image t]
  273. ["html_options" smarty-template-html-options t]
  274. ["html_radios" smarty-template-html-radios t]
  275. ["html_select_date" smarty-template-html-select-date t]
  276. ["html_select_time" smarty-template-html-select-time t]
  277. ["html_table" smarty-template-html-table t]
  278. ["mailto" smarty-template-mailto t]
  279. ["math" smarty-template-math t]
  280. ["popup" smarty-template-popup t]
  281. ["popup_init" smarty-template-popup-init t]
  282. ["textformat" smarty-template-textformat t])
  283. ("Variable Modifiers"
  284. ["capitalize" smarty-template-capitalize t]
  285. ["cat" smarty-template-cat t]
  286. ["count_characters" smarty-template-count-characters t]
  287. ["count_paragraphs" smarty-template-count-paragraphs t]
  288. ["count_sentences" smarty-template-count-sentences t]
  289. ["count_words" smarty-template-count-words t]
  290. ["date_format" smarty-template-date-format t]
  291. ["default" smarty-template-default t]
  292. ["escape" smarty-template-escape t]
  293. ["indent" smarty-template-indent t]
  294. ["lower" smarty-template-lower t]
  295. ["nl2br" smarty-template-nl2br t]
  296. ["regex_replace" smarty-template-regex-replace t]
  297. ["replace" smarty-template-replace t]
  298. ["spacify" smarty-template-spacify t]
  299. ["string_format" smarty-template-string-format t]
  300. ["strip" smarty-template-vstrip t]
  301. ["strip_tags" smarty-template-strip-tags t]
  302. ["truncate" smarty-template-truncate t]
  303. ["upper" smarty-template-upper t]
  304. ["wordwrap" smarty-template-wordwrap t])
  305. ("Plugins (Functions)"
  306. ("BlockRepeatPlugin"
  307. ["repeat" smarty-template-repeat t]
  308. ["str_repeat" smarty-template-str-repeat t])
  309. ("ClipCache"
  310. ["clipcache" smarty-template-clipcache t]
  311. ["include_clipcache" smarty-template-include-clipcache t])
  312. ("SmartyFormtool"
  313. ["formtool_checkall" smarty-template-formtool-checkall t]
  314. ["formtool_copy" smarty-template-formtool-copy t]
  315. ["formtool_count_chars" smarty-template-formtool-count-chars t]
  316. ["formtool_init" smarty-template-formtool-init t]
  317. ["formtool_move" smarty-template-formtool-move t]
  318. ["formtool_moveall" smarty-template-formtool-moveall t]
  319. ["formtool_movedown" smarty-template-formtool-movedown t]
  320. ["formtool_moveup" smarty-template-formtool-moveup t]
  321. ["formtool_remove" smarty-template-formtool-remove t]
  322. ["formtool_rename" smarty-template-formtool-rename t]
  323. ["formtool_save" smarty-template-formtool-save t]
  324. ["formtool_selectall" smarty-template-formtool-selectall t])
  325. ("SmartyPaginate"
  326. ["paginate_first" smarty-template-paginate-first t]
  327. ["paginate_last" smarty-template-paginate-last t]
  328. ["paginate_middle" smarty-template-paginate-middle t]
  329. ["paginate_next" smarty-template-paginate-next t]
  330. ["paginate_prev" smarty-template-paginate-prev t])
  331. ("SmartyValidate"
  332. ["validate" smarty-template-validate t]))
  333. ("Plugins (Variable Modifiers)"
  334. ("AlternativeDateModifierPlugin"
  335. ["date_format2" smarty-template-date-formatto t])
  336. ("B2Smilies"
  337. ["B2Smilies" smarty-template-btosmilies t])
  338. ("BBCodePlugin"
  339. ["bbcode2html" smarty-template-bbcodetohtml t])
  340. )
  341. "--"
  342. ["Insert Header" smarty-template-header t]
  343. ["Insert Footer" smarty-template-footer t]
  344. ["Insert Date" smarty-template-insert-date t]
  345. ["Modify Date" smarty-template-modify t])
  346. "--"
  347. ["Show Messages" smarty-show-messages :keys "C-c M-m"]
  348. ["Smarty Mode Documentation" smarty-doc-mode :keys "C-c C-h"]
  349. ["Version" smarty-version :keys "C-c C-v"]
  350. "--"
  351. ("Options"
  352. ("Mode"
  353. ["Electric Mode"
  354. (progn (customize-set-variable 'smarty-electric-mode
  355. (not smarty-electric-mode))
  356. (smarty-mode-line-update))
  357. :style toggle :selected smarty-electric-mode :keys "C-c C-m C-e"]
  358. ["Stutter Mode"
  359. (progn (customize-set-variable 'smarty-stutter-mode
  360. (not smarty-stutter-mode))
  361. (smarty-mode-line-update))
  362. :style toggle :selected smarty-stutter-mode :keys "C-c C-m C-s"]
  363. "--"
  364. ["Customize Group..." (customize-group 'smarty-mode) t])
  365. ("Menu"
  366. ["Source Menu"
  367. (customize-set-variable 'smarty-source-file-menu
  368. (not smarty-source-file-menu))
  369. :style toggle :selected smarty-source-file-menu]
  370. "--"
  371. ["Customize Group..." (customize-group 'smarty-menu) t])
  372. ("Highlight"
  373. ["Highlight plugin functions"
  374. (progn (customize-set-variable 'smarty-highlight-plugin-functions
  375. (not smarty-highlight-plugin-functions)))
  376. :style toggle :selected smarty-highlight-plugin-functions]
  377. "--"
  378. ["Customize Group..." (customize-group 'smarty-highlight) t])
  379. ("Template"
  380. ("Header"
  381. ["Header template..."
  382. (customize-option 'smarty-file-header) t]
  383. ["Footer template..."
  384. (customize-option 'smarty-file-footer) t]
  385. ["Company..."
  386. (customize-option 'smarty-company-name) t]
  387. ["Copyright..."
  388. (customize-option 'smarty-copyright-string) t]
  389. ["Date format..."
  390. (customize-option 'smarty-date-format) t]
  391. ["Modify date prefix..."
  392. (customize-option 'smarty-modify-date-prefix-string) t]
  393. ["Modify date on saving"
  394. (customize-set-variable 'smarty-modify-date-on-saving
  395. (not smarty-modify-date-on-saving))
  396. :style toggle :selected smarty-modify-date-on-saving]
  397. "--"
  398. ["Customize Group..." (customize-group 'smarty-header) t])
  399. "--"
  400. ["Customize Group..." (customize-group 'smarty-template) t])
  401. ("Miscellaneous"
  402. ["Left delimiter..."
  403. (customize-option 'smarty-left-delimiter) t]
  404. ["Right delimiter..."
  405. (customize-option 'smarty-right-delimiter) t]
  406. ["Use Intelligent Tab"
  407. (progn (customize-set-variable 'smarty-intelligent-tab
  408. (not smarty-intelligent-tab))
  409. (smarty-activate-customizations))
  410. :style toggle :selected smarty-intelligent-tab]
  411. ["Word Completion in Minibuffer"
  412. (progn (customize-set-variable 'smarty-word-completion-in-minibuffer
  413. (not smarty-word-completion-in-minibuffer))
  414. (message "Activate new setting by saving options and restarting Emacs"))
  415. :style toggle :selected smarty-word-completion-in-minibuffer]
  416. ["Completion is case sensitive"
  417. (customize-set-variable 'smarty-word-completion-case-sensitive
  418. (not smarty-word-completion-case-sensitive))
  419. :style toggle :selected smarty-word-completion-case-sensitive]
  420. "--"
  421. ["Customize Group..." (customize-group 'smarty-misc) t])
  422. "--"
  423. ["Save Options" customize-save-customized t]
  424. ["Activate Options" smarty-activate-customizations t]
  425. ["Browse Options..." smarty-customize t])))
  426. (defvar smarty-mode-menu-list (smarty-create-mode-menu)
  427. "Smarty Mode menu.")
  428. (defvar smarty-mode-map nil
  429. "Keymap for Smarty Mode.")
  430. (defun smarty-update-mode-menu ()
  431. "Update Smarty Mode menu."
  432. (interactive)
  433. (easy-menu-remove smarty-mode-menu-list)
  434. (setq smarty-mode-menu-list (smarty-create-mode-menu))
  435. (easy-menu-add smarty-mode-menu-list)
  436. (easy-menu-define smarty-mode-menu smarty-mode-map
  437. "Menu keymap for Smarty Mode." smarty-mode-menu-list))
  438. (defvar smarty-mode-hook nil)
  439. (defvar smarty-functions nil
  440. "List of Smarty functions.")
  441. (defvar smarty-functions-regexp nil
  442. "Regexp for Smarty functions.")
  443. (defconst smarty-01-functions
  444. '("capture" "config_load" "foreach" "foreachelse" "include"
  445. "include_php" "insert" "if" "elseif" "else" "ldelim" "rdelim"
  446. "literal" "php" "section" "sectionelse" "strip" "assign" "counter"
  447. "cycle" "debug" "eval" "fetch" "html_checkboxes" "html_image"
  448. "html_options" "html_radios" "html_select_date" "html_select_time"
  449. "html_table" "math" "mailto" "popup_init" "popup" "textformat")
  450. "Smarty built-in & custom functions.")
  451. (defvar smarty-modifiers nil
  452. "List of Smarty variable modifiers.")
  453. (defvar smarty-modifiers-regexp nil
  454. "Regexp for Smarty variable modifiers.")
  455. (defconst smarty-01-modifiers
  456. '("capitalize" "cat" "count_characters" "count_paragraphs"
  457. "count_sentences" "count_words" "date_format" "default"
  458. "escape" "indent" "lower" "nl2br" "regex_replace" "replace"
  459. "spacify" "string_format" "strip" "strip_tags" "truncate"
  460. "upper" "wordwrap")
  461. "Smarty variable modifiers.")
  462. (defvar smarty-plugins-functions nil
  463. "List of Smarty functions.")
  464. (defvar smarty-plugins-functions-regexp nil
  465. "Regexp for Smarty functions.")
  466. (defconst smarty-01-plugins-functions
  467. '("validate" "formtool_checkall" "formtool_copy" "formtool_count_chars"
  468. "formtool_init" "formtool_move" "formtool_moveall"
  469. "formtool_movedown" "formtool_moveup" "formtool_remove"
  470. "formtool_rename" "formtool_save" "formtool_selectall"
  471. "paginate_first" "paginate_last" "paginate_middle"
  472. "paginate_next" "paginate_prev" "clipcache" "include_clipcache"
  473. "repeat" "str_repeat")
  474. "Smarty plugins functions.")
  475. (defvar smarty-plugins-modifiers nil
  476. "List of Smarty variable modifiers.")
  477. (defvar smarty-plugins-modifiers-regexp nil
  478. "Regexp for Smarty functions.")
  479. (defconst smarty-01-plugins-modifiers
  480. '("B2Smilies" "bbcode2html" "date_format2")
  481. "Smarty plugins modifiers.")
  482. (defconst smarty-constants
  483. (eval-when-compile
  484. (regexp-opt
  485. '("TRUE" "FALSE" "NULL") t))
  486. "Smarty constants.")
  487. ;; Syntax table creation
  488. (defvar smarty-mode-syntax-table nil
  489. "Syntax table for smarty-mode.")
  490. (defvar smarty-mode-ext-syntax-table nil
  491. "Syntax table extended by `_' used in `smarty-mode' buffers.")
  492. (defun smarty-create-syntax-table ()
  493. (if smarty-mode-syntax-table
  494. ()
  495. (setq smarty-mode-syntax-table (make-syntax-table))
  496. ;; Make | a punctuation character
  497. (modify-syntax-entry ?| "." smarty-mode-syntax-table)
  498. ;; Make " a punctuation character so highlighing works withing html strings
  499. (modify-syntax-entry ?\" "." smarty-mode-syntax-table)
  500. ;; define parentheses to match
  501. (modify-syntax-entry ?\( "()" smarty-mode-syntax-table)
  502. (modify-syntax-entry ?\) ")(" smarty-mode-syntax-table)
  503. (modify-syntax-entry ?\[ "(]" smarty-mode-syntax-table)
  504. (modify-syntax-entry ?\] ")[" smarty-mode-syntax-table)
  505. (modify-syntax-entry ?\{ "(}" smarty-mode-syntax-table)
  506. (modify-syntax-entry ?\} "){" smarty-mode-syntax-table)
  507. )
  508. (set-syntax-table smarty-mode-syntax-table)
  509. ;; extended syntax table including '_' (for simpler search regexps)
  510. (setq smarty-mode-ext-syntax-table (copy-syntax-table smarty-mode-syntax-table))
  511. (modify-syntax-entry ?_ "w" smarty-mode-ext-syntax-table))
  512. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  513. ;; File/directory manipulation
  514. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  515. (defun smarty-directory-files (directory &optional full match)
  516. "Call `directory-files' if DIRECTORY exists, otherwise generate error
  517. message."
  518. (if (not (file-directory-p directory))
  519. (smarty-warning-when-idle "No such directory: \"%s\"" directory)
  520. (let ((dir (directory-files directory full match)))
  521. (setq dir (delete "." dir))
  522. (setq dir (delete ".." dir))
  523. dir)))
  524. (defun smarty-get-source-files (&optional full directory)
  525. "Get list of SMARTY source files in DIRECTORY or current directory."
  526. (let ((mode-alist auto-mode-alist)
  527. filename-regexp)
  528. ;; create regular expressions for matching file names
  529. (setq filename-regexp "\\`[^.].*\\(")
  530. (while mode-alist
  531. (when (eq (cdar mode-alist) 'smarty-mode)
  532. (setq filename-regexp
  533. (concat filename-regexp (caar mode-alist) "\\|")))
  534. (setq mode-alist (cdr mode-alist)))
  535. (setq filename-regexp
  536. (concat (substring filename-regexp 0
  537. (string-match "\\\\|$" filename-regexp)) "\\)"))
  538. ;; find files
  539. (smarty-directory-files
  540. (or directory default-directory) full filename-regexp)))
  541. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  542. ;; Messages reporting
  543. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  544. (defvar smarty-warnings nil
  545. "Warnings to tell the user during start up.")
  546. (defun smarty-run-when-idle (secs repeat function)
  547. "Wait until idle, then run FUNCTION."
  548. (if (fboundp 'start-itimer)
  549. (start-itimer "smarty-mode" function secs repeat t)
  550. ; (run-with-idle-timer secs repeat function)))
  551. ;; explicitely activate timer (necessary when Emacs is already idle)
  552. (aset (run-with-idle-timer secs repeat function) 0 nil)))
  553. (defun smarty-warning-when-idle (&rest args)
  554. "Wait until idle, then print out warning STRING and beep."
  555. (save-match-data ;; runs in timer
  556. (if noninteractive
  557. (smarty-warning (apply 'format args) t)
  558. (unless smarty-warnings
  559. (smarty-run-when-idle .1 nil 'smarty-print-warnings))
  560. (setq smarty-warnings (cons (apply 'format args) smarty-warnings)))))
  561. (defun smarty-warning (string &optional nobeep)
  562. "Print out warning STRING and beep."
  563. (message (concat "WARNING: " string))
  564. (unless (or nobeep noninteractive) (beep)))
  565. (defun smarty-print-warnings ()
  566. "Print out messages in variable `smarty-warnings'."
  567. (let ((no-warnings (length smarty-warnings)))
  568. (setq smarty-warnings (nreverse smarty-warnings))
  569. (while smarty-warnings
  570. (message (concat "WARNING: " (car smarty-warnings)))
  571. (setq smarty-warnings (cdr smarty-warnings)))
  572. (beep)
  573. (when (> no-warnings 1)
  574. (message "WARNING: See warnings in message buffer (type `C-c M-m')."))))
  575. (defun smarty-show-messages ()
  576. "Get *Messages* buffer to show recent messages."
  577. (interactive)
  578. (display-buffer " *Message-Log*"))
  579. (defun smarty-version ()
  580. "Echo the current version of Smarty Mode in the minibuffer."
  581. (interactive)
  582. (message "Smarty Mode %s (%s)" smarty-version smarty-time-stamp)
  583. (smarty-keep-region-active))
  584. ;; active regions
  585. (defun smarty-keep-region-active ()
  586. "Do whatever is necessary to keep the region active in XEmacs.
  587. Ignore byte-compiler warnings you might see."
  588. (and (boundp 'zmacs-region-stays)
  589. (setq zmacs-region-stays t)))
  590. (defmacro smarty-prepare-search-1 (&rest body)
  591. "Enable case insensitive search and switch to syntax table that includes '_',
  592. then execute BODY, and finally restore the old environment. Used for
  593. consistent searching."
  594. `(let ((case-fold-search t) ; case insensitive search
  595. (current-syntax-table (syntax-table))
  596. result
  597. (restore-prog ; program to restore enviroment
  598. '(progn
  599. ;; restore syntax table
  600. (set-syntax-table current-syntax-table))))
  601. ;; use extended syntax table
  602. (set-syntax-table smarty-mode-ext-syntax-table)
  603. ;; execute BODY safely
  604. (setq result
  605. (condition-case info
  606. (progn ,@body)
  607. (error (eval restore-prog) ; restore environment on error
  608. (error (cadr info))))) ; pass error up
  609. ;; restore environment
  610. (eval restore-prog)
  611. result))
  612. (defmacro smarty-prepare-search-2 (&rest body)
  613. "Enable case insensitive search, switch to syntax table that includes '_',
  614. and remove `intangible' overlays, then execute BODY, and finally restore the
  615. old environment. Used for consistent searching."
  616. `(let ((case-fold-search t) ; case insensitive search
  617. (current-syntax-table (syntax-table))
  618. result overlay-all-list overlay-intangible-list overlay
  619. (restore-prog ; program to restore enviroment
  620. '(progn
  621. ;; restore syntax table
  622. (set-syntax-table current-syntax-table)
  623. ;; restore `intangible' overlays
  624. (when (fboundp 'overlay-lists)
  625. (while overlay-intangible-list
  626. (overlay-put (car overlay-intangible-list) 'intangible t)
  627. (setq overlay-intangible-list
  628. (cdr overlay-intangible-list)))))))
  629. ;; use extended syntax table
  630. (set-syntax-table smarty-mode-ext-syntax-table)
  631. ;; remove `intangible' overlays
  632. (when (fboundp 'overlay-lists)
  633. (setq overlay-all-list (overlay-lists))
  634. (setq overlay-all-list
  635. (append (car overlay-all-list) (cdr overlay-all-list)))
  636. (while overlay-all-list
  637. (setq overlay (car overlay-all-list))
  638. (when (memq 'intangible (overlay-properties overlay))
  639. (setq overlay-intangible-list
  640. (cons overlay overlay-intangible-list))
  641. (overlay-put overlay 'intangible nil))
  642. (setq overlay-all-list (cdr overlay-all-list))))
  643. ;; execute BODY safely
  644. (setq result
  645. (condition-case info
  646. (progn ,@body)
  647. (error (eval restore-prog) ; restore environment on error
  648. (error (cadr info))))) ; pass error up
  649. ;; restore environment
  650. (eval restore-prog)
  651. result))
  652. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  653. ;; Enabling/disabling
  654. (defun smarty-mode-line-update ()
  655. "Update the modeline string for Smarty major mode."
  656. (setq mode-name (concat "Smarty"
  657. (and (or smarty-electric-mode smarty-stutter-mode) "/")
  658. (and smarty-electric-mode "e")
  659. (and smarty-stutter-mode "s")))
  660. (force-mode-line-update t))
  661. (defun smarty-electric-mode (arg)
  662. "Toggle Smarty electric mode.
  663. Turn on if ARG positive, turn off if ARG negative, toggle if ARG zero or nil."
  664. (interactive "P")
  665. (setq smarty-electric-mode
  666. (cond ((or (not arg) (zerop arg)) (not smarty-electric-mode))
  667. ((> arg 0) t) (t nil)))
  668. (smarty-mode-line-update))
  669. (defun smarty-stutter-mode (arg)
  670. "Toggle Smarty stuttering mode.
  671. Turn on if ARG positive, turn off if ARG negative, toggle if ARG zero or nil."
  672. (interactive "P")
  673. (setq smarty-stutter-mode
  674. (cond ((or (not arg) (zerop arg)) (not smarty-stutter-mode))
  675. ((> arg 0) t) (t nil)))
  676. (smarty-mode-line-update))
  677. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  678. ;;; Smarty code delimitation
  679. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  680. (defun smarty-in-literal ()
  681. "Determine if point is in a Smarty literal."
  682. (save-excursion
  683. (let ((here (point))
  684. start state)
  685. (beginning-of-line)
  686. (setq start (point))
  687. (goto-char here)
  688. (setq state (parse-partial-sexp start (point)))
  689. (cond
  690. ((nth 3 state) 'string)
  691. ((nth 4 state) 'comment)
  692. (t nil)))))
  693. (defun smarty-in-comment-p ()
  694. "Check if point is in a comment."
  695. (let ((result nil) (here (point-marker)) found)
  696. (save-excursion
  697. (setq found (re-search-backward (regexp-quote (concat smarty-left-delimiter "*")) nil t))
  698. (when found
  699. (setq result (re-search-forward (regexp-quote (concat "*" smarty-right-delimiter)) here t))
  700. (setq result (not result))))
  701. result))
  702. (defun smarty-after-ldelim ()
  703. "Check that the previous character is the left delimiter."
  704. (let ((here (point-marker)) ldelim-found ldelim-point)
  705. (save-excursion
  706. (setq ldelim-found (re-search-backward (regexp-quote smarty-left-delimiter) nil t))
  707. (re-search-forward (regexp-quote smarty-left-delimiter) here t)
  708. (setq ldelim-point (point-marker))
  709. (goto-char here)
  710. (if (and (= here ldelim-point) ldelim-found)
  711. t
  712. nil))))
  713. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  714. ;; Words to expand
  715. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  716. (defun smarty-words-init ()
  717. "Initialize reserved words."
  718. (setq smarty-functions smarty-01-functions)
  719. (setq smarty-modifiers smarty-01-modifiers)
  720. (setq smarty-plugins-functions smarty-01-plugins-functions)
  721. (setq smarty-plugins-modifiers smarty-01-plugins-modifiers)
  722. (setq smarty-functions-regexp (concat "\\<\\(" (regexp-opt smarty-functions) "\\)\\>"))
  723. (setq smarty-modifiers-regexp (concat "\\<\\(" (regexp-opt smarty-modifiers) "\\)\\>"))
  724. (setq smarty-plugins-functions-regexp (concat "\\<\\(" (regexp-opt smarty-plugins-functions) "\\)\\>"))
  725. (setq smarty-plugins-modifiers-regexp (concat "\\<\\(" (regexp-opt smarty-plugins-modifiers) "\\)\\>"))
  726. (smarty-abbrev-list-init))
  727. (defvar smarty-abbrev-list nil
  728. "Predefined abbreviations for Smarty.")
  729. (defun smarty-abbrev-list-init ()
  730. (setq smarty-abbrev-list
  731. (append
  732. (list nil) smarty-functions
  733. (list nil) smarty-modifiers
  734. (list nil) smarty-plugins-functions
  735. (list nil) smarty-plugins-modifiers)))
  736. (defvar smarty-expand-upper-case nil)
  737. (defun smarty-try-expand-abbrev (old)
  738. "Try expanding abbreviations from `smarty-abbrev-list'."
  739. (unless old
  740. (he-init-string (he-dabbrev-beg) (point))
  741. (setq he-expand-list
  742. (let ((abbrev-list smarty-abbrev-list)
  743. (sel-abbrev-list '()))
  744. (while abbrev-list
  745. ; (if (stringp (car abbrev-list))
  746. ; (insert (concat " " (car abbrev-list))))
  747. (when (or (not (stringp (car abbrev-list)))
  748. (string-match
  749. (concat "^" he-search-string) (car abbrev-list)))
  750. (setq sel-abbrev-list
  751. (cons (car abbrev-list) sel-abbrev-list)))
  752. (setq abbrev-list (cdr abbrev-list)))
  753. (nreverse sel-abbrev-list))))
  754. (while (and he-expand-list
  755. (or (not (stringp (car he-expand-list)))
  756. (he-string-member (car he-expand-list) he-tried-table t)))
  757. (unless (stringp (car he-expand-list))
  758. (setq smarty-expand-upper-case (car he-expand-list)))
  759. (setq he-expand-list (cdr he-expand-list)))
  760. (if (null he-expand-list)
  761. (progn (when old (he-reset-string))
  762. nil)
  763. (he-substitute-string
  764. (if smarty-expand-upper-case
  765. (upcase (car he-expand-list))
  766. (car he-expand-list))
  767. t)
  768. (setq he-expand-list (cdr he-expand-list))
  769. t))
  770. ;; initialize reserved words for Smarty Mode
  771. (smarty-words-init)
  772. ;; function for expanding abbrevs and dabbrevs
  773. (defun smarty-expand-abbrev (arg))
  774. (fset 'smarty-expand-abbrev (make-hippie-expand-function
  775. '(try-expand-dabbrev
  776. try-expand-dabbrev-all-buffers
  777. smarty-try-expand-abbrev)))
  778. ;; function for expanding parenthesis
  779. (defun smarty-expand-paren (arg))
  780. (fset 'smarty-expand-paren (make-hippie-expand-function
  781. '(try-expand-list
  782. try-expand-list-all-buffers)))
  783. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  784. ;;; Stuttering
  785. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  786. (defun smarty-electric-tab (&optional prefix-arg)
  787. "If preceding character is part of a word or a paren then hippie-expand,
  788. else if right of non whitespace on line then insert tab,
  789. else if last command was a tab or return then dedent one step or if a comment
  790. toggle between normal indent and inline comment indent,
  791. else indent `correctly'."
  792. (interactive "*P")
  793. (smarty-prepare-search-2
  794. (cond
  795. ;; expand word
  796. ((= (char-syntax (preceding-char)) ?w)
  797. (let ((case-fold-search (not smarty-word-completion-case-sensitive))
  798. (case-replace nil)
  799. (hippie-expand-only-buffers
  800. (or (and (boundp 'hippie-expand-only-buffers)
  801. hippie-expand-only-buffers)
  802. '(smarty-mode))))
  803. (smarty-expand-abbrev prefix-arg)))
  804. ;; expand parenthesis
  805. ((or (= (preceding-char) ?\() (= (preceding-char) ?\)))
  806. (let ((case-fold-search (not smarty-word-completion-case-sensitive))
  807. (case-replace nil))
  808. (smarty-expand-paren prefix-arg))))
  809. (setq this-command 'smarty-electric-tab)))
  810. (defun smarty-electric-space (count)
  811. "Expand abbreviations and self-insert space(s)."
  812. (interactive "p")
  813. (let ((here (point-marker)) ldelim-found ldelim-point rdelim-found rdelim-point
  814. delete-a)
  815. (setq ldelim-found (re-search-backward (regexp-quote smarty-left-delimiter) nil t))
  816. (re-search-forward (regexp-quote smarty-left-delimiter) here t)
  817. (setq ldelim-point (point-marker))
  818. (goto-char here)
  819. (setq rdelim-found (re-search-backward (regexp-quote (concat " " smarty-right-delimiter)) nil t))
  820. (re-search-forward (regexp-quote (concat " " smarty-right-delimiter)) here t)
  821. (setq rdelim-point (point-marker))
  822. (goto-char here)
  823. (cond ((and (= here ldelim-point) ldelim-found) (insert (concat "ldelim" smarty-right-delimiter)))
  824. ((and (= here rdelim-point) rdelim-found)
  825. (re-search-backward (regexp-quote (concat " " smarty-right-delimiter)) nil t)
  826. (delete-char 1)
  827. (insert (concat " " smarty-left-delimiter "rdelim"))
  828. (goto-char here))
  829. ((smarty-in-comment-p)
  830. (self-insert-command count)
  831. (cond ((>= (current-column) (+ 2 end-comment-column))
  832. (backward-char 1)
  833. (skip-chars-backward "^ \t\n")
  834. (indent-new-comment-line)
  835. (skip-chars-forward "^ \t\n")
  836. (forward-char 1))
  837. ((>= (current-column) end-comment-column)
  838. (indent-new-comment-line))
  839. (t nil)))
  840. ((or (and (>= (preceding-char) ?a) (<= (preceding-char) ?z))
  841. (and (>= (preceding-char) ?A) (<= (preceding-char) ?Z))
  842. (and (>= (preceding-char) ?0) (<= (preceding-char) ?9)))
  843. (progn
  844. (setq here (point-marker))
  845. (insert " ")
  846. (setq delete-a t)
  847. (if (re-search-backward "|" nil t)
  848. (progn
  849. (setq found (re-search-forward (regexp-quote "B2Smilies") here t))
  850. (if (and found (= here (point-marker)))
  851. (replace-match "btosmilies")
  852. (setq found (re-search-forward (regexp-quote "bbcode2html") here t))
  853. (if (and found (= here (point-marker)))
  854. (replace-match "bbcodetohtml")
  855. (setq found (re-search-forward (regexp-quote "date_format2") here t))
  856. (if (and found (= here (point-marker)))
  857. (replace-match "date_formatto")
  858. (goto-char here)
  859. (setq delete-a nil)
  860. (delete-char 1)))))
  861. (goto-char here)
  862. (setq delete-a nil)
  863. (delete-char 1)))
  864. (smarty-prepare-search-1 (expand-abbrev))
  865. (self-insert-command count)
  866. (if (and delete-a (looking-at " "))
  867. (delete-char 1)))
  868. (t (self-insert-command count)))))
  869. (defun smarty-electric-open-bracket (count)
  870. "'(' --> '(', '((' --> '[', '[(' --> '{'"
  871. (interactive "p")
  872. (if (and smarty-stutter-mode (= count 1) (not (smarty-in-literal)))
  873. (if (= (preceding-char) ?\()
  874. (progn (delete-char -1) (insert-char ?\[ 1))
  875. (if (= (preceding-char) ?\[)
  876. (progn (delete-char -1) (insert-char ?\{ 1))
  877. (insert-char ?\( 1)))
  878. (self-insert-command count)))
  879. (defun smarty-electric-close-bracket (count)
  880. "')' --> ')', '))' --> ']', '])' --> '}'"
  881. (interactive "p")
  882. (if (and smarty-stutter-mode (= count 1) (not (smarty-in-literal)))
  883. (progn
  884. (if (= (preceding-char) ?\))
  885. (progn (delete-char -1) (insert-char ?\] 1))
  886. (if (= (preceding-char) ?\])
  887. (progn (delete-char -1) (insert-char ?} 1))
  888. (insert-char ?\) 1)))
  889. (blink-matching-open))
  890. (self-insert-command count)))
  891. (defun smarty-electric-star (count)
  892. "After a left delimiter add a right delemiter to close the comment"
  893. (interactive "p")
  894. (let ((here (point-marker)) found)
  895. (if (and smarty-stutter-mode (= count 1) (not (smarty-in-literal)))
  896. (progn
  897. (setq found (re-search-backward (regexp-quote smarty-left-delimiter) nil t))
  898. (re-search-forward (regexp-quote smarty-left-delimiter) here t)
  899. (if (not (and (= here (point-marker)) found))
  900. (progn (goto-char here)
  901. (self-insert-command count))
  902. (self-insert-command count)
  903. (insert " ")
  904. (setq here (point-marker))
  905. (insert " *")
  906. (insert smarty-right-delimiter)
  907. (goto-char here)))
  908. (self-insert-command count))))
  909. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  910. ;;; Electrification
  911. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  912. (defconst smarty-template-prompt-syntax "[^ =<>][^<>@.\n]*[^ =<>]"
  913. "Syntax of prompt inserted by template generators.")
  914. (defvar smarty-template-invoked-by-hook nil
  915. "Indicates whether a template has been invoked by a hook or by key or menu.
  916. Used for undoing after template abortion.")
  917. (defun smarty-minibuffer-tab (&optional prefix-arg)
  918. "If preceding character is part of a word or a paren then hippie-expand,
  919. else insert tab (used for word completion in Smarty minibuffer)."
  920. (interactive "P")
  921. (cond
  922. ;; expand word
  923. ((= (char-syntax (preceding-char)) ?w)
  924. (let ((case-fold-search (not smarty-word-completion-case-sensitive))
  925. (case-replace nil)
  926. (hippie-expand-only-buffers
  927. (or (and (boundp 'hippie-expand-only-buffers)
  928. hippie-expand-only-buffers)
  929. '(smarty-mode))))
  930. (smarty-expand-abbrev prefix-arg)))
  931. ;; expand parenthesis
  932. ((or (= (preceding-char) ?\() (= (preceding-char) ?\)))
  933. (let ((case-fold-search (not smarty-word-completion-case-sensitive))
  934. (case-replace nil))
  935. (smarty-expand-paren prefix-arg)))
  936. ;; insert tab
  937. (t (insert-tab))))
  938. ;; correct different behavior of function `unread-command-events' in XEmacs
  939. (defun smarty-character-to-event (arg))
  940. (defalias 'smarty-character-to-event
  941. (if (fboundp 'character-to-event) 'character-to-event 'identity))
  942. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  943. ;; Abbrev ook bindings
  944. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  945. (defvar smarty-mode-abbrev-table nil
  946. "Abbrev table to use in `smarty-mode' buffers.")
  947. (defun smarty-mode-abbrev-table-init ()
  948. "Initialize `smarty-mode-abbrev-table'."
  949. (when smarty-mode-abbrev-table (clear-abbrev-table smarty-mode-abbrev-table))
  950. (define-abbrev-table 'smarty-mode-abbrev-table
  951. (append
  952. '(
  953. ("capture" "" smarty-template-capture-hook 0)
  954. ("config_load" "" smarty-template-config-load-hook 0)
  955. ("else" "" smarty-template-else-hook 0)
  956. ("elseif" "" smarty-template-elseif-hook 0)
  957. ("foreach" "" smarty-template-foreach-hook 0)
  958. ("foreachelse" "" smarty-template-foreachelse-hook 0)
  959. ("if" "" smarty-template-if-hook 0)
  960. ("include" "" smarty-template-include-hook 0)
  961. ("include_php" "" smarty-template-include-php-hook 0)
  962. ("insert" "" smarty-template-insert-hook 0)
  963. ("ldelim" "" smarty-template-ldelim-hook 0)
  964. ("literal" "" smarty-template-literal-hook 0)
  965. ("php" "" smarty-template-php-hook 0)
  966. ("rdelim" "" smarty-template-rdelim-hook 0)
  967. ("section" "" smarty-template-section-hook 0)
  968. ("sectionelse" "" smarty-template-sectionelse-hook 0)
  969. ("strip" "" smarty-template-strip-hook 0)
  970. ("assign" "" smarty-template-assign-hook 0)
  971. ("counter" "" smarty-template-counter-hook 0)
  972. ("cycle" "" smarty-template-cycle-hook 0)
  973. ("debug" "" smarty-template-debug-hook 0)
  974. ("eval" "" smarty-template-eval-hook 0)
  975. ("fetch" "" smarty-template-fetch-hook 0)
  976. ("html_checkboxes" "" smarty-template-html-checkboxes-hook 0)
  977. ("html_image" "" smarty-template-html-image-hook 0)
  978. ("html_options" "" smarty-template-html-options-hook 0)
  979. ("html_radios" "" smarty-template-html-radios-hook 0)
  980. ("html_select_date" "" smarty-template-html-select-date-hook 0)
  981. ("html_select_time" "" smarty-template-html-select-time-hook 0)
  982. ("html_table" "" smarty-template-html-table-hook 0)
  983. ("mailto" "" smarty-template-mailto-hook 0)
  984. ("math" "" smarty-template-math-hook 0)
  985. ("popup" "" smarty-template-popup-hook 0)
  986. ("popup_init" "" smarty-template-popup-init-hook 0)
  987. ("textformat" "" smarty-template-textformat-hook 0)
  988. ("capitalize" "" smarty-template-capitalize-hook 0)
  989. ("cat" "" smarty-template-cat-hook 0)
  990. ("count_characters" "" smarty-template-count-characters-hook 0)
  991. ("count_paragraphs" "" smarty-template-count-paragraphs-hook 0)
  992. ("count_sentences" "" smarty-template-count-sentences-hook 0)
  993. ("count_words" "" smarty-template-count-words-hook 0)
  994. ("date_format" "" smarty-template-date-format-hook 0)
  995. ("default" "" smarty-template-default-hook 0)
  996. ("escape" "" smarty-template-escape-hook 0)
  997. ("indent" "" smarty-template-indent-hook 0)
  998. ("lower" "" smarty-template-lower-hook 0)
  999. ("nl2br" "" smarty-template-nl2br-hook 0)
  1000. ("regex_replace" "" smarty-template-regex-replace-hook 0)
  1001. ("replace" "" smarty-template-replace-hook 0)
  1002. ("spacify" "" smarty-template-spacify-hook 0)
  1003. ("string_format" "" smarty-template-string-format-hook 0)
  1004. ("strip" "" smarty-template-vstrip-hook 0)
  1005. ("strip_tags" "" smarty-template-strip-tags-hook 0)
  1006. ("truncate" "" smarty-template-truncate-hook 0)
  1007. ("upper" "" smarty-template-upper-hook 0)
  1008. ("wordwrap" "" smarty-template-wordwrap-hook 0)
  1009. ("validate" "" smarty-template-validate-hook 0)
  1010. ("clipcache" "" smarty-template-clipcache-hook 0)
  1011. ("repeat" "" smarty-template-repeat-hook 0)
  1012. ("str_repeat" "" smarty-template-str-repeat-hook 0)
  1013. ("include_clipcache" "" smarty-template-include-clipcache-hook 0)
  1014. ("formtool_checkall" "" smarty-template-formtool-checkall-hook 0)
  1015. ("formtool_copy" "" smarty-template-formtool-copy-hook 0)
  1016. ("formtool_count_chars" "" smarty-template-formtool-count-chars-hook 0)
  1017. ("formtool_init" "" smarty-template-formtool-init-hook 0)
  1018. ("formtool_move" "" smarty-template-formtool-move-hook 0)
  1019. ("formtool_moveall" "" smarty-template-formtool-moveall-hook 0)
  1020. ("formtool_movedown" "" smarty-template-formtool-movedown-hook 0)
  1021. ("formtool_moveup" "" smarty-template-formtool-moveup-hook 0)
  1022. ("formtool_remove" "" smarty-template-formtool-remove-hook 0)
  1023. ("formtool_rename" "" smarty-template-formtool-rename-hook 0)
  1024. ("formtool_save" "" smarty-template-formtool-save-hook 0)
  1025. ("formtool_selectall" "" smarty-template-formtool-selectall-hook 0)
  1026. ("paginate_first" "" smarty-template-paginate-first-hook 0)
  1027. ("paginate_last" "" smarty-template-paginate-last-hook 0)
  1028. ("paginate_middle" "" smarty-template-paginate-middle-hook 0)
  1029. ("paginate_next" "" smarty-template-paginate-next-hook 0)
  1030. ("paginate_prev" "" smarty-template-paginate-prev-hook 0)
  1031. ("btosmilies" "" smarty-template-btosmilies-hook 0)
  1032. ("bbcodetohtml" "" smarty-template-bbcodetohtml-hook 0)
  1033. ("date_formatto" "" smarty-template-date-formatto-hook 0)))))
  1034. ;; initialize abbrev table for Smarty Mode
  1035. (smarty-mode-abbrev-table-init)
  1036. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1037. ;; Abbrev hooks
  1038. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1039. (defun smarty-hooked-abbrev (func)
  1040. "Do function, if syntax says abbrev is a keyword, invoked by hooked abbrev,
  1041. but not if inside a comment or quote)."
  1042. (if (or (smarty-in-literal)
  1043. (smarty-in-comment-p))
  1044. (progn
  1045. (insert " ")
  1046. (unexpand-abbrev)
  1047. (delete-char -1))
  1048. (if (not smarty-electric-mode)
  1049. (progn
  1050. (insert " ")
  1051. (unexpand-abbrev)
  1052. (backward-word 1)
  1053. (delete-char 1))
  1054. (let ((invoke-char last-command-char)
  1055. (abbrev-mode -1)
  1056. (smarty-template-invoked-by-hook t))
  1057. (let ((caught (catch 'abort
  1058. (funcall func))))
  1059. (when (stringp caught) (message caught)))
  1060. (when (= invoke-char ?-) (setq abbrev-start-location (point)))
  1061. ;; delete CR which is still in event queue
  1062. (if (fboundp 'enqueue-eval-event)
  1063. (enqueue-eval-event 'delete-char -1)
  1064. (setq unread-command-events ; push back a delete char
  1065. (list (smarty-character-to-event ?\177))))))))
  1066. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1067. ;;; Fontification
  1068. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1069. (defvar smarty-font-lock-keywords-1
  1070. (list
  1071. ;; Fontify built-in functions
  1072. (cons
  1073. (concat (regexp-quote smarty-left-delimiter) "[/]*" smarty-functions-regexp)
  1074. '(1 font-lock-keyword-face))
  1075. (cons
  1076. (concat "\\<\\(" smarty-constants "\\)\\>")
  1077. 'font-lock-constant-face)
  1078. (cons (concat "\\(" (regexp-quote (concat smarty-left-delimiter "*")) "\\(\\s-\\|\\w\\|\\s.\\|\\s_\\|\\s(\\|\\s)\\|\\s\\\\)*" (regexp-quote (concat "*" smarty-right-delimiter)) "\\)")
  1079. 'font-lock-comment-face)
  1080. )
  1081. "Subdued level highlighting for Smarty mode.")
  1082. (defconst smarty-font-lock-keywords-2
  1083. (append
  1084. smarty-font-lock-keywords-1
  1085. (list
  1086. ;; Fontify variable names (\\sw\\|\\s_\\) matches any word character +
  1087. ;; underscore
  1088. '("\\$\\(\\(?:\\sw\\|\\s_\\)+\\)" (1 font-lock-variable-name-face)) ; $variable
  1089. '("->\\(\\(?:\\sw\\|\\s_\\)+\\)" (1 font-lock-variable-name-face t t)) ; ->variable
  1090. '("\\.\\(\\(?:\\sw\\|\\s_\\)+\\)" (1 font-lock-variable-name-face t t)) ; .variable
  1091. '("->\\(\\(?:\\sw\\|\\s_\\)+\\)\\s-*(" (1 font-lock-function-name-face t t)) ; ->function_call
  1092. '("\\<\\(\\(?:\\sw\\|\\s_\\)+\\s-*\\)(" (1 font-lock-function-name-face)) ; word(
  1093. '("\\<\\(\\(?:\\sw\\|\\s_\\)+\\s-*\\)[[]" (1 font-lock-variable-name-face)) ; word[
  1094. '("\\<[0-9]+" . default) ; number (also matches word)
  1095. ;; Fontify strings
  1096. ;;'("\"\\([^\"]*\\)\"[^\"]+" (1 font-lock-string-face t t))
  1097. ))
  1098. "Medium level highlighting for Smarty mode.")
  1099. (defconst smarty-font-lock-keywords-3
  1100. (append
  1101. smarty-font-lock-keywords-2
  1102. (list
  1103. ;; Fontify modifiers
  1104. (cons (concat "|\\(" smarty-modifiers-regexp "\\)[:|]+") '(1 font-lock-function-name-face))
  1105. (cons (concat "|\\(" smarty-modifiers-regexp "\\)" (regexp-quote smarty-right-delimiter)) '(1 font-lock-function-name-face))
  1106. ;; Fontify config vars
  1107. (cons (concat (regexp-quote smarty-left-delimiter) "\\(#\\(?:\\sw\\|\\s_\\)+#\\)") '(1 font-lock-constant-face))))
  1108. "Balls-out highlighting for Smarty mode.")
  1109. (defconst smarty-font-lock-keywords-4
  1110. (append
  1111. smarty-font-lock-keywords-3
  1112. (list
  1113. ;; Fontify plugin functions
  1114. (cons
  1115. (concat (regexp-quote smarty-left-delimiter) "[/]*" smarty-plugins-functions-regexp)
  1116. '(1 font-lock-keyword-face))
  1117. (cons (concat "|\\(" smarty-plugins-modifiers-regexp "\\)[:|]+") '(1 font-lock-function-name-face))
  1118. (cons (concat "|\\(" smarty-plugins-modifiers-regexp "\\)" (regexp-quote smarty-right-delimiter)) '(1 font-lock-function-name-face)))))
  1119. (defvar smarty-font-lock-keywords smarty-font-lock-keywords-3
  1120. "Default highlighting level for Smarty mode")
  1121. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1122. ;;; Mode map
  1123. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1124. (defvar smarty-template-map nil
  1125. "Keymap for Smarty templates.")
  1126. (defun smarty-template-map-init ()
  1127. "Initialize `smarty-template-map'."
  1128. (setq smarty-template-map (make-sparse-keymap))
  1129. ;; key bindings for Smarty templates
  1130. (define-key smarty-template-map "\C-ba" 'smarty-template-capture)
  1131. (define-key smarty-template-map "\C-bc" 'smarty-template-config-load)
  1132. (define-key smarty-template-map "\C-b\M-e" 'smarty-template-else)
  1133. (define-key smarty-template-map "\C-b\C-e" 'smarty-template-elseif)
  1134. (define-key smarty-template-map "\C-b\C-f" 'smarty-template-foreach)
  1135. (define-key smarty-template-map "\C-b\M-f" 'smarty-template-foreachelse)
  1136. (define-key smarty-template-map "\C-bf" 'smarty-template-if)
  1137. (define-key smarty-template-map "\C-b\C-i" 'smarty-template-include)
  1138. (define-key smarty-template-map "\C-b\M-i" 'smarty-template-include-php)
  1139. (define-key smarty-template-map "\C-bi" 'smarty-template-insert)
  1140. (define-key smarty-template-

Large files files are truncated, but you can click here to view the full file