1 ;;; srecode/mode.el --- Minor mode for managing and using SRecode templates
3 ;; Copyright (C) 2008-2011 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 ;; Minor mode for working with SRecode template files.
26 ;; Depends on Semantic for minor-mode convenience functions.
30 (require 'srecode
/insert
)
31 (require 'srecode
/find
)
32 (require 'srecode
/map
)
33 (require 'semantic
/decorate
)
34 (require 'semantic
/wisent
)
36 (eval-when-compile (require 'semantic
/find
))
40 (defcustom srecode-minor-mode-hook nil
41 "Hook run at the end of the function `srecode-minor-mode'."
45 ;; We don't want to waste space. There is a menu after all.
46 ;;(add-to-list 'minor-mode-alist '(srecode-minor-mode ""))
48 (defvar srecode-prefix-key
[(control ?c
) ?
/]
49 "The common prefix key in srecode minor mode.")
51 (defvar srecode-prefix-map
52 (let ((km (make-sparse-keymap)))
53 ;; Basic template codes
54 (define-key km
"/" 'srecode-insert
)
55 (define-key km
[insert] 'srecode-insert)
56 (define-key km "." 'srecode-insert-again)
57 (define-key km "E" 'srecode-edit)
58 ;; Template indirect binding
61 (define-key km (format "%c" k) 'srecode-bind-insert)
64 "Keymap used behind the srecode prefix key in in srecode minor mode.")
66 (defvar srecode-menu-bar
73 :help "Insert a template by name."
76 ["Insert Template Again"
79 :help "Run the same template as last time again."
85 :help "Edit a template for this language by name."
88 '( "Insert ..." :filter srecode-minor-mode-templates-menu )
89 `( "Generate ..." :filter srecode-minor-mode-generate-menu )
93 (customize-group "srecode")
95 :help "Customize SRecode options"
103 :help "Calculate (if needed) and display the current template file map."
107 srecode-dump-templates
109 :help "Dump the current template table."
113 srecode-dictionary-dump
115 :help "Calculate and dump a dictionary for point."
121 :help "Display the different types of macros available."
125 "Menu for srecode minor mode.")
127 (defvar srecode-minor-menu nil
128 "Menu keymap build from `srecode-menu-bar'.")
130 (defcustom srecode-takeover-INS-key nil
131 "Use the insert key for inserting templates."
135 (defvar srecode-mode-map
136 (let ((km (make-sparse-keymap)))
137 (define-key km srecode-prefix-key srecode-prefix-map)
138 (easy-menu-define srecode-minor-menu km "Srecode Minor Mode Menu"
140 (when srecode-takeover-INS-key
141 (define-key km [insert] srecode-prefix-map
))
143 "Keymap for srecode minor mode.")
146 (define-minor-mode srecode-minor-mode
147 "Toggle srecode minor mode.
148 With prefix argument ARG, turn on if positive, otherwise off. The
149 minor mode can be turned on only if semantic feature is available and
150 the current buffer was set up for parsing. Return non-nil if the
151 minor mode is enabled.
153 \\{srecode-mode-map}"
154 :keymap srecode-mode-map
155 ;; If we are turning things on, make sure we have templates for
157 (when srecode-minor-mode
160 (mapcar (lambda (map)
161 (srecode-map-entries-for-mode map major-mode
))
162 (srecode-get-maps))))
163 (setq srecode-minor-mode nil
))))
166 (define-minor-mode global-srecode-minor-mode
167 "Toggle global use of srecode minor mode.
168 If ARG is positive or nil, enable, if it is negative, disable."
169 :global t
:group
'srecode
170 ;; Not needed because it's autoloaded instead.
171 ;; :require 'srecode/mode
172 (semantic-toggle-minor-mode-globally
173 'srecode-minor-mode
(if global-srecode-minor-mode
1 -
1)))
175 ;; Use the semantic minor mode magic stuff.
176 (semantic-add-minor-mode 'srecode-minor-mode
"")
180 (defun srecode-minor-mode-templates-menu (menu-def)
181 "Create a menu item of cascading filters active for this mode.
182 MENU-DEF is the menu to bind this into."
183 ;; Doing this SEGVs Emacs on windows.
184 ;;(srecode-load-tables-for-mode major-mode)
186 (let* ((modetable (srecode-get-mode-table major-mode
))
187 (subtab (when modetable
(oref modetable
:tables
)))
195 ;; No tables, show a "load the tables" option.
196 (list (vector "Load Mode Tables..."
199 (srecode-load-tables-for-mode major-mode
))
202 (setq context
(car-safe (srecode-calculate-context)))
205 (when (srecode-template-table-in-project-p (car subtab
))
206 (setq ltab
(oref (car subtab
) templates
))
208 (setq temp
(car ltab
))
210 ;; Do something with this template.
212 (let* ((ctxt (oref temp context
))
213 (ctxtcons (assoc ctxt alltabs
))
214 (bind (if (slot-boundp temp
'binding
)
215 (oref temp binding
)))
216 (name (object-name-string temp
)))
219 (if (string= context ctxt
)
220 ;; If this context is not in the current list of contexts
221 ;; is equal to the current context, then manage the
222 ;; active list instead
224 (setq ctxtcons
(or active
(cons ctxt nil
))))
225 ;; This is not an active context, add it to alltabs.
226 (setq ctxtcons
(cons ctxt nil
))
227 (setq alltabs
(cons ctxtcons alltabs
))))
231 (concat name
" (" bind
")")
233 `(lambda () (interactive)
234 (srecode-insert (concat ,ctxt
":" ,name
)))
237 (setcdr ctxtcons
(cons
241 (setq ltab
(cdr ltab
))))
242 (setq subtab
(cdr subtab
)))
244 ;; Now create the menu
245 (easy-menu-filter-return
246 (easy-menu-create-menu
247 "Semantic Recoder Filters"
253 (defvar srecode-minor-mode-generators nil
254 "List of code generators to be displayed in the srecoder menu.")
256 (defun srecode-minor-mode-generate-menu (menu-def)
257 "Create a menu item of cascading filters active for this mode.
258 MENU-DEF is the menu to bind this into."
259 ;; Doing this SEGVs Emacs on windows.
260 ;;(srecode-load-tables-for-mode major-mode)
261 (let ((allgeneratorapps nil
))
263 (dolist (gen srecode-minor-mode-generators
)
264 (setq allgeneratorapps
265 (cons (vector (cdr gen
) (car gen
))
267 (message "Adding %S to srecode menu" (car gen
))
270 (easy-menu-filter-return
271 (easy-menu-create-menu
272 "Semantic Recoder Generate Filters"
276 ;;; Minor Mode commands
278 (defun srecode-bind-insert ()
279 "Bound insert for Srecode macros.
280 This command will insert whichever srecode template has a binding
283 (srecode-load-tables-for-mode major-mode
)
284 (let* ((k last-command-event
)
285 (ctxt (srecode-calculate-context))
286 ;; Find the template with the binding K
287 (template (srecode-template-get-table-for-binding
288 (srecode-table) k ctxt
)))
291 (error "No template bound to %c" k
))
293 (srecode-insert template
)
296 (defun srecode-edit (template-name)
297 "Switch to the template buffer for TEMPLATE-NAME.
298 Template is chosen based on the mode of the starting buffer."
299 ;; @todo - Get a template stack from the last run template, and show
301 (interactive (list (srecode-read-template-name
303 (car srecode-read-template-name-history
))))
304 (if (not (srecode-table))
305 (error "No template table found for mode %s" major-mode
))
306 (let ((temp (srecode-template-get-table (srecode-table) template-name
)))
308 (error "No Template named %s" template-name
))
309 ;; We need a template specific table, since tables chain.
310 (let ((tab (oref temp
:table
))
313 (find-file (oref tab
:file
))
314 (setq names
(semantic-find-tags-by-name (oref temp
:object-name
)
316 (cond ((= (length names
) 1)
317 (semantic-go-to-tag (car names
))
318 (semantic-momentary-highlight-tag (car names
)))
319 ((> (length names
) 1)
320 (let* ((ctxt (semantic-find-tags-by-name (oref temp
:context
)
322 (cls (semantic-find-tags-by-class 'context ctxt
))
325 (< (semantic-tag-start (car names
))
326 (semantic-tag-start (car cls
))))
327 (setq names
(cdr names
)))
330 (semantic-go-to-tag (car names
))
331 (semantic-momentary-highlight-tag (car names
)))
332 (error "Can't find template %s" template-name
))
334 (t (error "Can't find template %s" template-name
)))
337 (defun srecode-add-code-generator (function name
&optional binding
)
338 "Add the srecoder code generator FUNCTION with NAME to the menu.
339 Optional BINDING specifies the keybinding to use in the srecoder map.
340 BINDING should be a capital letter. Lower case letters are reserved
341 for individual templates.
342 Optional MODE specifies a major mode this function applies to.
343 Do not specify a mode if this function could be applied to most
345 ;; Update the menu generating part.
347 (while (setq remloop
(assoc function srecode-minor-mode-generators
))
348 (setq srecode-minor-mode-generators
349 (remove remloop srecode-minor-mode-generators
))))
351 (add-to-list 'srecode-minor-mode-generators
352 (cons function name
))
354 ;; Remove this function from any old bindings.
356 (let ((oldkey (where-is-internal function
357 (list srecode-prefix-map
)
360 (and (= (length oldkey
) 1)
361 (= (length binding
) 1)
362 (= (aref oldkey
0) (aref binding
0))))
365 ;; Remove the old binding
366 (define-key srecode-prefix-map oldkey nil
)
370 (let ((oldbinding (lookup-key srecode-prefix-map binding
)))
372 ;; During development, allow overrides.
373 (when (and oldbinding
374 (not (eq oldbinding function
))
375 (or (eq this-command
'eval-defun
) (eq this-command
'checkdoc-eval-defun
))
376 (y-or-n-p (format "Override old binding %s? " oldbinding
)))
377 (setq oldbinding nil
))
380 (define-key srecode-prefix-map binding function
)
381 (if (eq function oldbinding
)
384 (message "Conflict binding %S binding to srecode map."
388 ;; Add default code generators:
389 (srecode-add-code-generator 'srecode-document-insert-comment
"Comments" "C")
390 (srecode-add-code-generator 'srecode-insert-getset
"Get/Set" "G")
392 (provide 'srecode
/mode
)
395 ;; generated-autoload-file: "loaddefs.el"
396 ;; generated-autoload-load-name: "srecode/mode"
399 ;;; srecode/mode.el ends here