1 ;;; srecode/mode.el --- Minor mode for managing and using SRecode templates
3 ;; Copyright (C) 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 ;; 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 a dump a dictionary for point."
119 "Menu for srecode minor mode.")
121 (defvar srecode-minor-menu nil
122 "Menu keymap build from `srecode-menu-bar'.")
124 (defcustom srecode-takeover-INS-key nil
125 "Use the insert key for inserting templates."
129 (defvar srecode-mode-map
130 (let ((km (make-sparse-keymap)))
131 (define-key km srecode-prefix-key srecode-prefix-map)
132 (easy-menu-define srecode-minor-menu km "Srecode Minor Mode Menu"
134 (when srecode-takeover-INS-key
135 (define-key km [insert] srecode-prefix-map))
137 "Keymap for srecode minor mode.")
140 (define-minor-mode srecode-minor-mode
141 "Toggle srecode minor mode.
142 With prefix argument ARG, turn on if positive, otherwise off. The
143 minor mode can be turned on only if semantic feature is available and
144 the current buffer was set up for parsing. Return non-nil if the
145 minor mode is enabled.
147 \\{srecode-mode-map}"
148 :keymap srecode-mode-map
149 ;; If we are turning things on, make sure we have templates for
151 (when srecode-minor-mode
154 (mapcar (lambda (map)
155 (srecode-map-entries-for-mode map major-mode))
156 (srecode-get-maps))))
157 (setq srecode-minor-mode nil))))
160 (define-minor-mode global-srecode-minor-mode
161 "Toggle global use of srecode minor mode.
162 If ARG is positive or nil, enable, if it is negative, disable."
163 :global t :group 'srecode
164 ;; Not needed because it's autoloaded instead.
165 ;; :require 'srecode/mode
166 (semantic-toggle-minor-mode-globally
167 'srecode-minor-mode (if global-srecode-minor-mode 1 -1)))
169 ;; Use the semantic minor mode magic stuff.
170 (semantic-add-minor-mode 'srecode-minor-mode "")
174 (defun srecode-minor-mode-templates-menu (menu-def)
175 "Create a menu item of cascading filters active for this mode.
176 MENU-DEF is the menu to bind this into."
177 ;; Doing this SEGVs Emacs on windows.
178 ;;(srecode-load-tables-for-mode major-mode)
180 (let* ((modetable (srecode-get-mode-table major-mode))
181 (subtab (when modetable (oref modetable :tables)))
189 ;; No tables, show a "load the tables" option.
190 (list (vector "Load Mode Tables..."
193 (srecode-load-tables-for-mode major-mode))
196 (setq context (car-safe (srecode-calculate-context)))
199 (setq ltab (oref (car subtab) templates))
201 (setq temp (car ltab))
203 ;; Do something with this template.
205 (let* ((ctxt (oref temp context))
206 (ctxtcons (assoc ctxt alltabs))
207 (bind (if (slot-boundp temp 'binding)
208 (oref temp binding)))
209 (name (object-name-string temp)))
212 (if (string= context ctxt)
213 ;; If this context is not in the current list of contexts
214 ;; is equal to the current context, then manage the
215 ;; active list instead
217 (setq ctxtcons (or active (cons ctxt nil))))
218 ;; This is not an active context, add it to alltabs.
219 (setq ctxtcons (cons ctxt nil))
220 (setq alltabs (cons ctxtcons alltabs))))
224 (concat name " (" bind ")")
226 `(lambda () (interactive)
227 (srecode-insert (concat ,ctxt ":" ,name)))
230 (setcdr ctxtcons (cons
234 (setq ltab (cdr ltab)))
235 (setq subtab (cdr subtab)))
237 ;; Now create the menu
238 (easy-menu-filter-return
239 (easy-menu-create-menu
240 "Semantic Recoder Filters"
246 (defvar srecode-minor-mode-generators nil
247 "List of code generators to be displayed in the srecoder menu.")
249 (defun srecode-minor-mode-generate-menu (menu-def)
250 "Create a menu item of cascading filters active for this mode.
251 MENU-DEF is the menu to bind this into."
252 ;; Doing this SEGVs Emacs on windows.
253 ;;(srecode-load-tables-for-mode major-mode)
254 (let ((allgeneratorapps nil))
256 (dolist (gen srecode-minor-mode-generators)
257 (setq allgeneratorapps
258 (cons (vector (cdr gen) (car gen))
260 (message "Adding %S to srecode menu" (car gen))
263 (easy-menu-filter-return
264 (easy-menu-create-menu
265 "Semantic Recoder Generate Filters"
269 ;;; Minor Mode commands
271 (defun srecode-bind-insert ()
272 "Bound insert for Srecode macros.
273 This command will insert whichever srecode template has a binding
276 (let* ((k last-command-event)
277 (ctxt (srecode-calculate-context))
278 ;; Find the template with the binding K
279 (template (srecode-template-get-table-for-binding
280 (srecode-table) k ctxt)))
283 (error "No template bound to %c" k))
285 (srecode-insert template)
288 (defun srecode-edit (template-name)
289 "Switch to the template buffer for TEMPLATE-NAME.
290 Template is chosen based on the mode of the starting buffer."
291 ;; @todo - Get a template stack from the last run template, and show
293 (interactive (list (srecode-read-template-name
295 (car srecode-read-template-name-history))))
296 (if (not (srecode-table))
297 (error "No template table found for mode %s" major-mode))
298 (let ((temp (srecode-template-get-table (srecode-table) template-name)))
300 (error "No Template named %s" template-name))
301 ;; We need a template specific table, since tables chain.
302 (let ((tab (oref temp :table))
305 (find-file (oref tab :file))
306 (setq names (semantic-find-tags-by-name (oref temp :object-name)
308 (cond ((= (length names) 1)
309 (semantic-go-to-tag (car names))
310 (semantic-momentary-highlight-tag (car names)))
311 ((> (length names) 1)
312 (let* ((ctxt (semantic-find-tags-by-name (oref temp :context)
314 (cls (semantic-find-tags-by-class 'context ctxt))
317 (< (semantic-tag-start (car names))
318 (semantic-tag-start (car cls))))
319 (setq names (cdr names)))
322 (semantic-go-to-tag (car names))
323 (semantic-momentary-highlight-tag (car names)))
324 (error "Can't find template %s" template-name))
326 (t (error "Can't find template %s" template-name)))
329 (defun srecode-add-code-generator (function name &optional binding)
330 "Add the srecoder code generator FUNCTION with NAME to the menu.
331 Optional BINDING specifies the keybinding to use in the srecoder map.
332 BINDING should be a capital letter. Lower case letters are reserved
333 for individual templates.
334 Optional MODE specifies a major mode this function applies to.
335 Do not specify a mode if this function could be applied to most
337 ;; Update the menu generating part.
339 (while (setq remloop (assoc function srecode-minor-mode-generators))
340 (setq srecode-minor-mode-generators
341 (remove remloop srecode-minor-mode-generators))))
343 (add-to-list 'srecode-minor-mode-generators
344 (cons function name))
346 ;; Remove this function from any old bindings.
348 (let ((oldkey (where-is-internal function
349 (list srecode-prefix-map)
352 (and (= (length oldkey) 1)
353 (= (length binding) 1)
354 (= (aref oldkey 0) (aref binding 0))))
357 ;; Remove the old binding
358 (define-key srecode-prefix-map oldkey nil)
362 (let ((oldbinding (lookup-key srecode-prefix-map binding)))
364 ;; During development, allow overrides.
365 (when (and oldbinding
366 (not (eq oldbinding function))
367 (or (eq this-command 'eval-defun) (eq this-command 'checkdoc-eval-defun))
368 (y-or-n-p (format "Override old binding %s? " oldbinding)))
369 (setq oldbinding nil))
372 (define-key srecode-prefix-map binding function)
373 (if (eq function oldbinding)
376 (message "Conflict binding %S binding to srecode map."
380 ;; Add default code generators:
381 (srecode-add-code-generator 'srecode-document-insert-comment "Comments" "C")
382 (srecode-add-code-generator 'srecode-insert-getset "Get/Set" "G")
384 (provide 'srecode/mode)
387 ;; generated-autoload-file: "loaddefs.el"
388 ;; generated-autoload-load-name: "srecode/mode"
391 ;; arch-tag: 56ad9d6b-899b-4a68-8636-1432b6bc149b
392 ;;; srecode/mode.el ends here