1 ;;; srecode/mode.el --- Minor mode for managing and using SRecode templates
3 ;; Copyright (C) 2008, 2009 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
)
34 (require 'semantic
/decorate
)
35 (require 'semantic
/wisent
)
37 (eval-when-compile (require 'semantic
/find
))
41 (defcustom global-srecode-minor-mode nil
42 "Non-nil in buffers with Semantic Recoder macro keybindings."
45 :require
'srecode
/mode
46 :initialize
'custom-initialize-default
47 :set
(lambda (sym val
)
48 (global-srecode-minor-mode (if val
1 -
1))))
50 (defvar srecode-minor-mode nil
51 "Non-nil in buffers with Semantic Recoder macro keybindings.")
52 (make-variable-buffer-local 'srecode-minor-mode
)
54 (defcustom srecode-minor-mode-hook nil
55 "Hook run at the end of the function `srecode-minor-mode'."
59 ;; We don't want to waste space. There is a menu after all.
60 ;;(add-to-list 'minor-mode-alist '(srecode-minor-mode ""))
62 (defvar srecode-prefix-key
[(control ?c
) ?
/]
63 "The common prefix key in srecode minor mode.")
65 (defvar srecode-prefix-map
66 (let ((km (make-sparse-keymap)))
67 ;; Basic template codes
68 (define-key km
"/" 'srecode-insert
)
69 (define-key km
[insert] 'srecode-insert)
70 (define-key km "." 'srecode-insert-again)
71 (define-key km "E" 'srecode-edit)
72 ;; Template indirect binding
75 (define-key km (format "%c" k) 'srecode-bind-insert)
78 "Keymap used behind the srecode prefix key in in srecode minor mode.")
80 (defvar srecode-menu-bar
87 :help "Insert a template by name."
90 ["Insert Template Again"
93 :help "Run the same template as last time again."
99 :help "Edit a template for this language by name."
102 '( "Insert ..." :filter srecode-minor-mode-templates-menu )
103 `( "Generate ..." :filter srecode-minor-mode-generate-menu )
107 (customize-group "srecode")
109 :help "Customize SRecode options"
117 :help "Calculate (if needed) and display the current template file map."
121 srecode-dump-templates
123 :help "Dump the current template table."
127 srecode-dictionary-dump
129 :help "Calculate a dump a dictionary for point."
133 "Menu for srecode minor mode.")
135 (defvar srecode-minor-menu nil
136 "Menu keymap build from `srecode-menu-bar'.")
138 (defcustom srecode-takeover-INS-key nil
139 "Use the insert key for inserting templates."
143 (defvar srecode-mode-map
144 (let ((km (make-sparse-keymap)))
145 (define-key km srecode-prefix-key srecode-prefix-map)
146 (easy-menu-define srecode-minor-menu km "Srecode Minor Mode Menu"
148 (when srecode-takeover-INS-key
149 (define-key km [insert] srecode-prefix-map
))
151 "Keymap for srecode minor mode.")
154 (defun srecode-minor-mode (&optional arg
)
155 "Toggle srecode minor mode.
156 With prefix argument ARG, turn on if positive, otherwise off. The
157 minor mode can be turned on only if semantic feature is available and
158 the current buffer was set up for parsing. Return non-nil if the
159 minor mode is enabled.
161 \\{srecode-mode-map}"
163 (list (or current-prefix-arg
164 (if srecode-minor-mode
0 1))))
166 (setq srecode-minor-mode
169 (prefix-numeric-value arg
)
171 (not srecode-minor-mode
)))
172 ;; If we are turning things on, make sure we have templates for
174 (when srecode-minor-mode
177 (mapcar (lambda (map)
178 (srecode-map-entries-for-mode map major-mode
))
179 (srecode-get-maps))))
180 (setq srecode-minor-mode nil
))
182 ;; Run hooks if we are turning this on.
183 (when srecode-minor-mode
184 (run-hooks 'srecode-minor-mode-hook
))
188 (defun global-srecode-minor-mode (&optional arg
)
189 "Toggle global use of srecode minor mode.
190 If ARG is positive, enable, if it is negative, disable.
191 If ARG is nil, then toggle."
193 (setq global-srecode-minor-mode
194 (semantic-toggle-minor-mode-globally
195 'srecode-minor-mode arg
)))
197 ;; Use the semantic minor mode magic stuff.
198 (semantic-add-minor-mode 'srecode-minor-mode
"" srecode-mode-map
)
202 (defun srecode-minor-mode-templates-menu (menu-def)
203 "Create a menu item of cascading filters active for this mode.
204 MENU-DEF is the menu to bind this into."
205 ;; Doing this SEGVs Emacs on windows.
206 ;;(srecode-load-tables-for-mode major-mode)
208 (let* ((modetable (srecode-get-mode-table major-mode
))
209 (subtab (when modetable
(oref modetable
:tables
)))
217 ;; No tables, show a "load the tables" option.
218 (list (vector "Load Mode Tables..."
221 (srecode-load-tables-for-mode major-mode
))
224 (setq context
(car-safe (srecode-calculate-context)))
227 (setq ltab
(oref (car subtab
) templates
))
229 (setq temp
(car ltab
))
231 ;; Do something with this template.
233 (let* ((ctxt (oref temp context
))
234 (ctxtcons (assoc ctxt alltabs
))
235 (bind (if (slot-boundp temp
'binding
)
236 (oref temp binding
)))
237 (name (object-name-string temp
)))
240 (if (string= context ctxt
)
241 ;; If this context is not in the current list of contexts
242 ;; is equal to the current context, then manage the
243 ;; active list instead
245 (setq ctxtcons
(or active
(cons ctxt nil
))))
246 ;; This is not an active context, add it to alltabs.
247 (setq ctxtcons
(cons ctxt nil
))
248 (setq alltabs
(cons ctxtcons alltabs
))))
252 (concat name
" (" bind
")")
254 `(lambda () (interactive)
255 (srecode-insert (concat ,ctxt
":" ,name
)))
258 (setcdr ctxtcons
(cons
262 (setq ltab
(cdr ltab
)))
263 (setq subtab
(cdr subtab
)))
265 ;; Now create the menu
266 (easy-menu-filter-return
267 (easy-menu-create-menu
268 "Semantic Recoder Filters"
274 (defvar srecode-minor-mode-generators nil
275 "List of code generators to be displayed in the srecoder menu.")
277 (defun srecode-minor-mode-generate-menu (menu-def)
278 "Create a menu item of cascading filters active for this mode.
279 MENU-DEF is the menu to bind this into."
280 ;; Doing this SEGVs Emacs on windows.
281 ;;(srecode-load-tables-for-mode major-mode)
282 (let ((allgeneratorapps nil
))
284 (dolist (gen srecode-minor-mode-generators
)
285 (setq allgeneratorapps
286 (cons (vector (cdr gen
) (car gen
))
288 (message "Adding %S to srecode menu" (car gen
))
291 (easy-menu-filter-return
292 (easy-menu-create-menu
293 "Semantic Recoder Generate Filters"
297 ;;; Minor Mode commands
299 (defun srecode-bind-insert ()
300 "Bound insert for Srecode macros.
301 This command will insert whichever srecode template has a binding
304 (let* ((k last-command-event
)
305 (ctxt (srecode-calculate-context))
306 ;; Find the template with the binding K
307 (template (srecode-template-get-table-for-binding
308 (srecode-table) k ctxt
)))
311 (error "No template bound to %c" k
))
313 (srecode-insert template
)
316 (defun srecode-edit (template-name)
317 "Switch to the template buffer for TEMPLATE-NAME.
318 Template is chosen based on the mode of the starting buffer."
319 ;; @todo - Get a template stack from the last run template, and show
321 (interactive (list (srecode-read-template-name
323 (car srecode-read-template-name-history
))))
324 (if (not (srecode-table))
325 (error "No template table found for mode %s" major-mode
))
326 (let ((temp (srecode-template-get-table (srecode-table) template-name
)))
328 (error "No Template named %s" template-name
))
329 ;; We need a template specific table, since tables chain.
330 (let ((tab (oref temp
:table
))
333 (find-file (oref tab
:file
))
334 (setq names
(semantic-find-tags-by-name (oref temp
:object-name
)
336 (cond ((= (length names
) 1)
337 (semantic-go-to-tag (car names
))
338 (semantic-momentary-highlight-tag (car names
)))
339 ((> (length names
) 1)
340 (let* ((ctxt (semantic-find-tags-by-name (oref temp
:context
)
342 (cls (semantic-find-tags-by-class 'context ctxt
))
345 (< (semantic-tag-start (car names
))
346 (semantic-tag-start (car cls
))))
347 (setq names
(cdr names
)))
350 (semantic-go-to-tag (car names
))
351 (semantic-momentary-highlight-tag (car names
)))
352 (error "Can't find template %s" template-name
))
354 (t (error "Can't find template %s" template-name
)))
357 (defun srecode-add-code-generator (function name
&optional binding
)
358 "Add the srecoder code generator FUNCTION with NAME to the menu.
359 Optional BINDING specifies the keybinding to use in the srecoder map.
360 BINDING should be a capital letter. Lower case letters are reserved
361 for individual templates.
362 Optional MODE specifies a major mode this function applies to.
363 Do not specify a mode if this function could be applied to most
365 ;; Update the menu generating part.
367 (while (setq remloop
(assoc function srecode-minor-mode-generators
))
368 (setq srecode-minor-mode-generators
369 (remove remloop srecode-minor-mode-generators
))))
371 (add-to-list 'srecode-minor-mode-generators
372 (cons function name
))
374 ;; Remove this function from any old bindings.
376 (let ((oldkey (where-is-internal function
377 (list srecode-prefix-map
)
380 (and (= (length oldkey
) 1)
381 (= (length binding
) 1)
382 (= (aref oldkey
0) (aref binding
0))))
385 ;; Remove the old binding
386 (define-key srecode-prefix-map oldkey nil
)
390 (let ((oldbinding (lookup-key srecode-prefix-map binding
)))
392 ;; During development, allow overrides.
393 (when (and oldbinding
394 (not (eq oldbinding function
))
395 (or (eq this-command
'eval-defun
) (eq this-command
'checkdoc-eval-defun
))
396 (y-or-n-p (format "Override old binding %s? " oldbinding
)))
397 (setq oldbinding nil
))
400 (define-key srecode-prefix-map binding function
)
401 (if (eq function oldbinding
)
404 (message "Conflict binding %S binding to srecode map."
408 ;; Add default code generators:
409 (srecode-add-code-generator 'srecode-document-insert-comment
"Comments" "C")
410 (srecode-add-code-generator 'srecode-insert-getset
"Get/Set" "G")
412 (provide 'srecode
/mode
)
415 ;; generated-autoload-file: "loaddefs.el"
416 ;; generated-autoload-feature: srecode/loaddefs
417 ;; generated-autoload-load-name: "srecode/mode"
420 ;;; srecode/mode.el ends here