/.emacs.d/el-get/nxhtml/related/smarty-mode.el

https://bitbucket.org/shuangxinyu/emacspack · Lisp · 2753 lines · 2230 code · 390 blank · 133 comment · 72 complexity · 6631c8e5de97ff1b6efec5bba142f6a4 MD5 · raw file

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