table.el 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. ;;; srecode/table.el --- Tables of Semantic Recoders
  2. ;; Copyright (C) 2007-2012 Free Software Foundation, Inc.
  3. ;; Author: Eric M. Ludlam <eric@siege-engine.com>
  4. ;; This file is part of GNU Emacs.
  5. ;; GNU Emacs is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; GNU Emacs is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;;
  17. ;; Semantic Recoder tables manage lists of templates and the major
  18. ;; modes they are associated with.
  19. ;;
  20. (require 'eieio)
  21. (require 'eieio-base)
  22. (require 'mode-local)
  23. (require 'srecode)
  24. (declare-function srecode-load-tables-for-mode "srecode/find")
  25. (declare-function srecode-template-table-in-project-p "srecode/find")
  26. ;;; Code:
  27. ;;; TEMPLATE TABLE
  28. ;;
  29. (defclass srecode-template-table ()
  30. (;;
  31. ;; Raw file tracking
  32. ;;
  33. (file :initarg :file
  34. :type string
  35. :documentation
  36. "The name of the file this table was built from.")
  37. (filesize :initarg :filesize
  38. :type number
  39. :documentation
  40. "The size of the file when it was parsed.")
  41. (filedate :initarg :filedate
  42. :type cons
  43. :documentation
  44. "Date from the inode of the file when it was last edited.
  45. Format is from the `file-attributes' function.")
  46. (major-mode :initarg :major-mode
  47. :documentation
  48. "The major mode this table of templates is associated with.")
  49. ;;
  50. ;; Template file sorting data
  51. ;;
  52. (application :initarg :application
  53. :type symbol
  54. :documentation
  55. "Tracks the name of the application these templates belong to.
  56. If this is nil, then this template table belongs to a set of generic
  57. templates that can be used with no additional dictionary values.
  58. When it is non-nil, it is assumed the template macros need specialized
  59. Emacs Lisp code to fill in the dictionary.")
  60. (priority :initarg :priority
  61. :type number
  62. :documentation
  63. "For file of this Major Mode, what is the priority of this file.
  64. When there are multiple template files with similar names, templates with
  65. the highest priority are scanned last, allowing them to override values in
  66. previous template files.")
  67. (project :initarg :project
  68. :type (or null string)
  69. :documentation
  70. "Scope some project files to a specific project.
  71. The value is a directory which forms the root of a particular project,
  72. or a subset of a particular project.")
  73. ;;
  74. ;; Parsed Data from the template file
  75. ;;
  76. (templates :initarg :templates
  77. :type list
  78. :documentation
  79. "The list of templates compiled into this table.")
  80. (namehash :initarg :namehash
  81. :documentation
  82. "Hash table containing the names of all the templates.")
  83. (contexthash :initarg :contexthash
  84. :documentation
  85. "")
  86. (variables :initarg :variables
  87. :documentation
  88. "AList of variables.
  89. These variables are used to initialize dictionaries.")
  90. )
  91. "Semantic recoder template table.
  92. A Table contains all templates from a single .srt file.
  93. Tracks various lookup hash tables.")
  94. ;;; MODE TABLE
  95. ;;
  96. (defvar srecode-mode-table-list nil
  97. "List of all the SRecode mode table classes that have been built.")
  98. (defclass srecode-mode-table (eieio-instance-tracker)
  99. ((tracking-symbol :initform 'srecode-mode-table-list)
  100. (major-mode :initarg :major-mode
  101. :documentation
  102. "Table of template tables for this major-mode.")
  103. (tables :initarg :tables
  104. :documentation
  105. "All the tables that have been defined for this major mode.")
  106. )
  107. "Track template tables for a particular major mode.
  108. Tracks all the template-tables for a specific major mode.")
  109. (defun srecode-get-mode-table (mode)
  110. "Get the SRecoder mode table for the major mode MODE.
  111. Optional argument SOFT indicates to not make a new one if a table
  112. was not found."
  113. (let ((ans nil))
  114. (while (and (not ans) mode)
  115. (setq ans (eieio-instance-tracker-find
  116. mode 'major-mode 'srecode-mode-table-list)
  117. mode (get-mode-local-parent mode)))
  118. ans))
  119. (defun srecode-make-mode-table (mode)
  120. "Get the SRecoder mode table for the major mode MODE."
  121. (let ((old (eieio-instance-tracker-find
  122. mode 'major-mode 'srecode-mode-table-list)))
  123. (if old
  124. old
  125. (let* ((ms (if (stringp mode) mode (symbol-name mode)))
  126. (new (srecode-mode-table ms
  127. :major-mode mode
  128. :tables nil)))
  129. ;; Save this new mode table in that mode's variable.
  130. (eval `(setq-mode-local ,mode srecode-table ,new))
  131. new))))
  132. (defmethod srecode-mode-table-find ((mt srecode-mode-table) file)
  133. "Look in the mode table MT for a template table from FILE.
  134. Return nil if there was none."
  135. (object-assoc file 'file (oref mt tables)))
  136. (defun srecode-mode-table-new (mode file &rest init)
  137. "Create a new template table for MODE in FILE.
  138. INIT are the initialization parameters for the new template table."
  139. (let* ((mt (srecode-make-mode-table mode))
  140. (old (srecode-mode-table-find mt file))
  141. (attr (file-attributes file))
  142. (new (apply 'srecode-template-table
  143. (file-name-nondirectory file)
  144. :file file
  145. :filesize (nth 7 attr)
  146. :filedate (nth 5 attr)
  147. :major-mode mode
  148. init
  149. )))
  150. ;; Whack the old table.
  151. (when old (object-remove-from-list mt 'tables old))
  152. ;; Add the new table
  153. (object-add-to-list mt 'tables new)
  154. ;; Sort the list in reverse order. When other routines
  155. ;; go front-to-back, the highest priority items are put
  156. ;; into the search table first, allowing lower priority items
  157. ;; to be the items found in the search table.
  158. (object-sort-list mt 'tables (lambda (a b)
  159. (> (oref a :priority)
  160. (oref b :priority))))
  161. ;; Return it.
  162. new))
  163. (defun object-sort-list (object slot predicate)
  164. "Sort the items in OBJECT's SLOT.
  165. Use PREDICATE is the same as for the `sort' function."
  166. (when (slot-boundp object slot)
  167. (when (listp (eieio-oref object slot))
  168. (eieio-oset object slot (sort (eieio-oref object slot) predicate)))))
  169. ;;; DEBUG
  170. ;;
  171. ;; Dump out information about the current srecoder compiled templates.
  172. ;;
  173. (defun srecode-dump-templates (mode)
  174. "Dump a list of the current templates for MODE."
  175. (interactive "sMode: ")
  176. (require 'srecode/find)
  177. (let ((modesym (cond ((string= mode "")
  178. major-mode)
  179. ((not (string-match "-mode" mode))
  180. (intern-soft (concat mode "-mode")))
  181. (t
  182. (intern-soft mode)))))
  183. (srecode-load-tables-for-mode modesym)
  184. (let ((tmp (srecode-get-mode-table modesym))
  185. )
  186. (if (not tmp)
  187. (error "No table found for mode %S" modesym))
  188. (with-output-to-temp-buffer "*SRECODE DUMP*"
  189. (srecode-dump tmp))
  190. )))
  191. (defmethod srecode-dump ((tab srecode-mode-table))
  192. "Dump the contents of the SRecode mode table TAB."
  193. (princ "MODE TABLE FOR ")
  194. (princ (oref tab :major-mode))
  195. (princ "\n--------------------------------------------\n\nNumber of tables: ")
  196. (let ((subtab (oref tab :tables)))
  197. (princ (length subtab))
  198. (princ "\n\n")
  199. (while subtab
  200. (srecode-dump (car subtab))
  201. (setq subtab (cdr subtab)))
  202. ))
  203. (defmethod srecode-dump ((tab srecode-template-table))
  204. "Dump the contents of the SRecode template table TAB."
  205. (princ "Template Table for ")
  206. (princ (object-name-string tab))
  207. (princ "\nPriority: ")
  208. (prin1 (oref tab :priority))
  209. (when (oref tab :application)
  210. (princ "\nApplication: ")
  211. (princ (oref tab :application)))
  212. (when (oref tab :project)
  213. (require 'srecode/find) ; For srecode-template-table-in-project-p
  214. (princ "\nProject Directory: ")
  215. (princ (oref tab :project))
  216. (when (not (srecode-template-table-in-project-p tab))
  217. (princ "\n ** Not Usable in this file. **")))
  218. (princ "\n\nVariables:\n")
  219. (let ((vars (oref tab variables)))
  220. (while vars
  221. (princ (car (car vars)))
  222. (princ "\t")
  223. (if (< (length (car (car vars))) 9)
  224. (princ "\t"))
  225. (prin1 (cdr (car vars)))
  226. (princ "\n")
  227. (setq vars (cdr vars))))
  228. (princ "\n\nTemplates:\n")
  229. (let ((temp (oref tab templates)))
  230. (while temp
  231. (srecode-dump (car temp))
  232. (setq temp (cdr temp))))
  233. )
  234. (provide 'srecode/table)
  235. ;;; srecode/table.el ends here