1 ;;; srecode/table.el --- Tables of Semantic Recoders
3 ;; Copyright (C) 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5 ;; Author: Eric M. Ludlam <eric@siege-engine.com>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;; Semantic Recoder tables manage lists of templates and the major
25 ;; modes they are associated with.
33 (declare-function srecode-load-tables-for-mode
"srecode/find")
39 (defclass srecode-template-table
()
46 "The name of the file this table was built from.")
47 (filesize :initarg
:filesize
50 "The size of the file when it was parsed.")
51 (filedate :initarg
:filedate
54 "Date from the inode of the file when it was last edited.
55 Format is from the `file-attributes' function.")
56 (major-mode :initarg
:major-mode
58 "The major mode this table of templates is associated with.")
60 ;; Template file sorting data
62 (application :initarg
:application
65 "Tracks the name of the application these templates belong to.
66 If this is nil, then this template table belongs to a set of generic
67 templates that can be used with no additional dictionary values.
68 When it is non-nil, it is assumed the template macros need specialized
69 Emacs Lisp code to fill in the dictoinary.")
70 (priority :initarg
:priority
73 "For file of this Major Mode, what is the priority of this file.
74 When there are multiple template files with similar names, templates with
75 the highest priority are scanned last, allowing them to override values in
76 previous template files.")
78 ;; Parsed Data from the template file
80 (templates :initarg
:templates
83 "The list of templates compiled into this table.")
84 (namehash :initarg
:namehash
86 "Hash table containing the names of all the templates.")
87 (contexthash :initarg
:contexthash
90 (variables :initarg
:variables
93 These variables are used to initialize dictionaries.")
95 "Semantic recoder template table.
96 A Table contains all templates from a single .srt file.
97 Tracks various lookup hash tables.")
101 (defvar srecode-mode-table-list nil
102 "List of all the SRecode mode table classes that have been built.")
104 (defclass srecode-mode-table
(eieio-instance-tracker)
105 ((tracking-symbol :initform
'srecode-mode-table-list
)
106 (major-mode :initarg
:major-mode
108 "Table of template tables for this major-mode.")
109 (tables :initarg
:tables
111 "All the tables that have been defined for this major mode.")
113 "Track template tables for a particular major mode.
114 Tracks all the template-tables for a specific major mode.")
116 (defun srecode-get-mode-table (mode)
117 "Get the SRecoder mode table for the major mode MODE.
118 Optional argument SOFT indicates to not make a new one if a table
121 (while (and (not ans
) mode
)
122 (setq ans
(eieio-instance-tracker-find
123 mode
'major-mode
'srecode-mode-table-list
)
124 mode
(get-mode-local-parent mode
)))
127 (defun srecode-make-mode-table (mode)
128 "Get the SRecoder mode table for the major mode MODE."
129 (let ((old (eieio-instance-tracker-find
130 mode
'major-mode
'srecode-mode-table-list
)))
133 (let* ((ms (if (stringp mode
) mode
(symbol-name mode
)))
134 (new (srecode-mode-table ms
137 ;; Save this new mode table in that mode's variable.
138 (eval `(setq-mode-local ,mode srecode-table
,new
))
142 (defmethod srecode-mode-table-find ((mt srecode-mode-table
) file
)
143 "Look in the mode table MT for a template table from FILE.
144 Return nil if there was none."
145 (object-assoc file
'file
(oref mt tables
)))
147 (defun srecode-mode-table-new (mode file
&rest init
)
148 "Create a new template table for MODE in FILE.
149 INIT are the initialization parameters for the new template table."
150 (let* ((mt (srecode-make-mode-table mode
))
151 (old (srecode-mode-table-find mt file
))
152 (attr (file-attributes file
))
153 (new (apply 'srecode-template-table
154 (file-name-nondirectory file
)
156 :filesize
(nth 7 attr
)
157 :filedate
(nth 5 attr
)
161 ;; Whack the old table.
162 (when old
(object-remove-from-list mt
'tables old
))
164 (object-add-to-list mt
'tables new
)
165 ;; Sort the list in reverse order. When other routines
166 ;; go front-to-back, the highest priority items are put
167 ;; into the search table first, allowing lower priority items
168 ;; to be the items found in the search table.
169 (object-sort-list mt
'tables
(lambda (a b
)
170 (> (oref a
:priority
)
171 (oref b
:priority
))))
175 (defun object-sort-list (object slot predicate
)
176 "Sort the items in OBJECT's SLOT.
177 Use PREDICATE is the same as for the `sort' function."
178 (when (slot-boundp object slot
)
179 (when (listp (eieio-oref object slot
))
180 (eieio-oset object slot
(sort (eieio-oref object slot
) predicate
)))))
184 ;; Dump out information about the current srecoder compiled templates.
186 (defun srecode-dump-templates (mode)
187 "Dump a list of the current templates for MODE."
188 (interactive "sMode: ")
189 (require 'srecode
/find
)
190 (let ((modesym (cond ((string= mode
"")
192 ((not (string-match "-mode" mode
))
193 (intern-soft (concat mode
"-mode")))
195 (intern-soft mode
)))))
196 (srecode-load-tables-for-mode modesym
)
197 (let ((tmp (srecode-get-mode-table modesym
))
200 (error "No table found for mode %S" modesym
))
201 (with-output-to-temp-buffer "*SRECODE DUMP*"
205 (defmethod srecode-dump ((tab srecode-mode-table
))
206 "Dump the contents of the SRecode mode table TAB."
207 (princ "MODE TABLE FOR ")
208 (princ (oref tab
:major-mode
))
209 (princ "\n--------------------------------------------\n\nNumber of tables: ")
210 (let ((subtab (oref tab
:tables
)))
211 (princ (length subtab
))
214 (srecode-dump (car subtab
))
215 (setq subtab
(cdr subtab
)))
218 (defmethod srecode-dump ((tab srecode-template-table
))
219 "Dump the contents of the SRecode template table TAB."
220 (princ "Template Table for ")
221 (princ (object-name-string tab
))
222 (princ "\nPriority: ")
223 (prin1 (oref tab
:priority
))
224 (when (oref tab
:application
)
225 (princ "\nApplication: ")
226 (princ (oref tab
:application
)))
227 (princ "\n\nVariables:\n")
228 (let ((vars (oref tab variables
)))
230 (princ (car (car vars
)))
232 (if (< (length (car (car vars
))) 9)
234 (prin1 (cdr (car vars
)))
236 (setq vars
(cdr vars
))))
237 (princ "\n\nTemplates:\n")
238 (let ((temp (oref tab templates
)))
240 (srecode-dump (car temp
))
241 (setq temp
(cdr temp
))))
245 (provide 'srecode
/table
)
247 ;;; srecode/table.el ends here
249 ;; arch-tag: 547d2f1d-2694-49b3-ab13-b2cda6b25b4d