1 ;;; srecode/insert.el --- Insert srecode templates to an output stream.
3 ;; Copyright (C) 2005, 2007-2013 Free Software Foundation, Inc.
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
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 ;; Define and implements specific inserter objects.
26 ;; Manage the insertion process for a template.
30 (require 'cl
)) ;; for `lexical-let'
32 (require 'srecode
/compile
)
33 (require 'srecode
/find
)
34 (require 'srecode
/dictionary
)
35 (require 'srecode
/args
)
36 (require 'srecode
/filters
)
38 (defvar srecode-template-inserter-point
)
39 (declare-function srecode-overlaid-activate
"srecode/fields")
40 (declare-function srecode-template-inserted-region
"srecode/fields")
44 (defcustom srecode-insert-ask-variable-method
'ask
45 "Determine how to ask for a dictionary value when inserting a template.
46 Only the ASK style inserter will query the user for a value.
47 Dictionary value references that ask begin with the ? character.
49 'ask - Prompt in the minibuffer as the value is inserted.
50 'field - Use the dictionary macro name as the inserted value,
51 and place a field there. Matched fields change together.
53 NOTE: The field feature does not yet work with XEmacs."
55 :type
'(choice (const :tag
"Ask" ask
)
56 (const :tag
"Field" field
)))
58 (defvar srecode-insert-with-fields-in-progress nil
59 "Non-nil means that we are actively inserting a template with fields.")
61 ;;; INSERTION COMMANDS
63 ;; User level commands for inserting stuff.
64 (defvar srecode-insertion-start-context nil
65 "The context that was at point at the beginning of the template insertion.")
67 (defun srecode-insert-again ()
68 "Insert the previously inserted template (by name) again."
70 (let ((prev (car srecode-read-template-name-history
)))
73 (call-interactively 'srecode-insert
))))
76 (defun srecode-insert (template-name &rest dict-entries
)
77 "Insert the template TEMPLATE-NAME into the current buffer at point.
78 DICT-ENTRIES are additional dictionary values to add."
79 (interactive (list (srecode-read-template-name "Template Name: ")))
80 (if (not (srecode-table))
81 (error "No template table found for mode %s" major-mode
))
82 (let ((newdict (srecode-create-dictionary))
83 (temp (srecode-template-get-table (srecode-table) template-name
))
84 (srecode-insertion-start-context (srecode-calculate-context))
87 (error "No Template named %s" template-name
))
89 (srecode-dictionary-set-value newdict
91 (car (cdr dict-entries
)))
92 (setq dict-entries
(cdr (cdr dict-entries
))))
93 (srecode-insert-fcn temp newdict
)
94 ;; Don't put code here. We need to return the end-mark
95 ;; for this insertion step.
98 (defun srecode-insert-fcn (template dictionary
&optional stream skipresolver
)
99 "Insert TEMPLATE using DICTIONARY into STREAM.
100 Optional SKIPRESOLVER means to avoid refreshing the tag list,
101 or resolving any template arguments. It is assumed the caller
102 has set everything up already."
103 ;; Perform the insertion.
104 (let ((standard-output (or stream
(current-buffer)))
106 ;; Merge any template entries into the input dictionary.
107 (when (slot-boundp template
'dictionary
)
108 (srecode-dictionary-merge dictionary
(oref template dictionary
)))
111 ;; Make sure the semantic tags are up to date.
112 (semantic-fetch-tags)
113 ;; Resolve the arguments
114 (srecode-resolve-arguments template dictionary
))
116 (if (bufferp standard-output
)
117 ;; If there is a buffer, turn off various hooks. This will cause
118 ;; the mod hooks to be buffered up during the insert, but
119 ;; prevent tools like font-lock from fontifying mid-template.
120 ;; Especially important during insertion of complex comments that
121 ;; cause the new font-lock to comment-color stuff after the inserted
124 ;; I'm not sure about the motion hooks. It seems like a good
127 ;; Borrowed these concepts out of font-lock.
129 ;; I tried `combine-after-change-calls', but it did not have
130 ;; the effect I wanted.
131 (let ((start (point)))
132 (let ((inhibit-point-motion-hooks t
)
133 (inhibit-modification-hooks t
)
135 (srecode--insert-into-buffer template dictionary
)
137 ;; Now call those after change functions.
138 (run-hook-with-args 'after-change-functions
141 (srecode-insert-method template dictionary
))
142 ;; Handle specialization of the POINT inserter.
143 (when (and (bufferp standard-output
)
144 (slot-boundp 'srecode-template-inserter-point
'point
)
146 (set-buffer standard-output
)
147 (setq end-mark
(point-marker))
148 (goto-char (oref srecode-template-inserter-point point
)))
149 (oset-default 'srecode-template-inserter-point point eieio-unbound
)
151 ;; Return the end-mark.
152 (or end-mark
(point)))
155 (defun srecode--insert-into-buffer (template dictionary
)
156 "Insert a TEMPLATE with DICTIONARY into a buffer.
157 Do not call this function yourself. Instead use:
158 `srecode-insert' - Inserts by name.
159 `srecode-insert-fcn' - Insert with objects.
160 This function handles the case from one of the above functions when
161 the template is inserted into a buffer. It looks
162 at `srecode-insert-ask-variable-method' to decide if unbound dictionary
163 entries ask questions or insert editable fields.
165 Buffer based features related to change hooks is handled one level up."
166 ;; This line prevents the field archive from being let bound
167 ;; while the field insert tool is loaded via autoloads during
169 (when (eq srecode-insert-ask-variable-method
'field
)
170 (require 'srecode
/fields
))
172 (let ((srecode-field-archive nil
) ; Prevent field leaks during insert
173 (start (point)) ; Beginning of the region.
175 ;; This sub-let scopes the 'in-progress' piece so we know
176 ;; when to setup the end-template.
177 (let ((srecode-insert-with-fields-in-progress
178 (if (eq srecode-insert-ask-variable-method
'field
) t nil
))
180 (srecode-insert-method template dictionary
)
182 ;; If we are not in-progress, and we insert fields, then
183 ;; create the end-template with fields editable area.
184 (when (and (not srecode-insert-with-fields-in-progress
)
185 (eq srecode-insert-ask-variable-method
'field
) ; Only if user asked
186 srecode-field-archive
; Only if there were fields created
189 ;; Create the field-driven editable area.
190 (srecode-template-inserted-region
191 "TEMPLATE" :start start
:end
(point))))
192 (srecode-overlaid-activate reg
))
194 ;; We return with 'point being the end of the template insertion
195 ;; area. Return value is not important.
198 (declare-function data-debug-new-buffer
"data-debug")
199 (declare-function data-debug-insert-stuff-list
"data-debug")
200 (declare-function data-debug-insert-thing dictionary
"data-debug")
202 (defun srecode-insert-show-error-report (dictionary format
&rest args
)
203 "Display an error report based on DICTIONARY, FORMAT and ARGS.
204 This is intended to diagnose problems with failed template
206 (with-current-buffer (data-debug-new-buffer "*SRECODE INSERTION ERROR*")
208 ;; Insert the stack of templates that are currently being
210 (insert (propertize "Template Stack" 'face
'(:weight bold
))
211 (propertize " (most recent at bottom)" 'face
'(:slant italic
))
213 (data-debug-insert-stuff-list
214 (reverse (oref srecode-template active
)) "> ")
215 ;; Show the current dictionary.
216 (insert (propertize "Dictionary" 'face
'(:weight bold
)) "\n")
217 (data-debug-insert-thing dictionary
"" "> ")
218 ;; Show the error message.
219 (insert (propertize "Error" 'face
'(:weight bold
)) "\n")
220 (insert (apply #'format format args
))
221 (pop-to-buffer (current-buffer))))
223 (defun srecode-insert-report-error (dictionary format
&rest args
)
224 ;; TODO only display something when inside an interactive call?
225 (srecode-insert-show-error-report dictionary format args
)
226 (apply #'error format args
))
228 ;;; TEMPLATE ARGUMENTS
230 ;; Some templates have arguments. Each argument is associated with
231 ;; a function that can resolve the inputs needed.
232 (defun srecode-resolve-arguments (temp dict
)
233 "Resolve all the arguments needed by the template TEMP.
234 Apply anything learned to the dictionary DICT."
235 (srecode-resolve-argument-list (oref temp args
) dict temp
))
237 (defun srecode-resolve-argument-list (args dict
&optional temp
)
238 "Resolve arguments in the argument list ARGS.
239 ARGS is a list of symbols, such as :blank, or :file.
240 Apply values to DICT.
241 Optional argument TEMP is the template that is getting its arguments resolved."
244 (setq fcn
(intern-soft (concat "srecode-semantic-handle-"
245 (symbol-name (car args
)))))
247 (error "Error resolving template argument %S" (car args
)))
250 ;; Allow some to accept a 2nd argument optionally.
251 ;; They throw an error if not available, so try again.
252 (funcall fcn dict temp
)
253 (wrong-number-of-arguments (funcall fcn dict
)))
255 (setq args
(cdr args
)))
258 ;;; INSERTION STACK & METHOD
260 ;; Code managing the top-level insert method and the current
263 (defmethod srecode-push ((st srecode-template
))
264 "Push the srecoder template ST onto the active stack."
265 (oset st active
(cons st
(oref st active
))))
267 (defmethod srecode-pop :STATIC
((st srecode-template
))
268 "Pop the srecoder template ST onto the active stack.
269 ST can be a class, or an object."
270 (oset st active
(cdr (oref st active
))))
272 (defmethod srecode-peek :STATIC
((st srecode-template
))
273 "Fetch the topmost active template record. ST can be a class."
274 (car (oref st active
)))
276 (defmethod srecode-insert-method ((st srecode-template
) dictionary
)
277 "Insert the srecoder template ST."
278 ;; Merge any template entries into the input dictionary.
279 ;; This may happen twice since some templates arguments need
280 ;; these dictionary values earlier, but these values always
281 ;; need merging for template inserting in other templates.
282 (when (slot-boundp st
'dictionary
)
283 (srecode-dictionary-merge dictionary
(oref st dictionary
)))
286 (let ((c (oref st code
)))
288 (srecode-insert-code-stream c dictionary
))
289 ;; Popping the stack is protected.
292 (defun srecode-insert-code-stream (code dictionary
)
293 "Insert the CODE from a template into `standard-output'.
294 Use DICTIONARY to resolve any macros."
296 (cond ((stringp (car code
))
299 (srecode-insert-method (car code
) dictionary
)))
300 (setq code
(cdr code
))))
304 ;; Specific srecode inserters.
305 ;; The base class is from srecode-compile.
307 ;; Each inserter handles various macro codes from the template.
308 ;; The `code' slot specifies a character used to identify which
309 ;; inserter is to be created.
311 (defclass srecode-template-inserter-newline
(srecode-template-inserter)
315 "The character code used to identify inserters of this style.")
319 "Is this a hard newline (always inserted) or optional?
320 Optional newlines don't insert themselves if they are on a blank line
323 "Insert a newline, and possibly do indenting.
324 Specify the :indent argument to enable automatic indentation when newlines
325 occur in your template.")
327 (defmethod srecode-insert-method ((sti srecode-template-inserter-newline
)
329 "Insert the STI inserter."
330 ;; To be safe, indent the previous line since the template will
331 ;; change what is there to indent
332 (let ((i (srecode-dictionary-lookup-name dictionary
"INDENT"))
333 (inbuff (bufferp standard-output
))
336 (when (and inbuff
(not (oref sti hard
)))
337 ;; If this is not a hard newline, we need do the calculation
338 ;; and set "doit" to nil.
341 (narrow-to-region (point) pm
)
342 (when (looking-at "\\s-*$")
346 ;; Do indentation regardless of the newline.
347 (when (and (eq i t
) inbuff
)
348 (indent-according-to-mode)
353 ;; Indent after the newline, particularly for numeric indents.
354 (cond ((and (eq i t
) (bufferp standard-output
))
355 ;; WARNING - indent according to mode requires that standard-output
357 ;; @todo - how to indent in a string???
358 (setq pm
(point-marker))
359 (indent-according-to-mode)
362 (princ (make-string i
" ")))
366 (defmethod srecode-dump ((ins srecode-template-inserter-newline
) indent
)
367 "Dump the state of the SRecode template inserter INS."
369 (when (oref ins hard
)
373 (defclass srecode-template-inserter-blank
(srecode-template-inserter)
377 "The character representing this inserter style.
378 Can't be blank, or it might be used by regular variable insertion.")
379 (where :initform
'begin
382 "This should be 'begin or 'end, indicating where to insert a CR.
383 When set to 'begin, it will insert a CR if we are not at 'bol'.
384 When set to 'end it will insert a CR if we are not at 'eol'.")
385 ;; @TODO - Add slot and control for the number of blank
386 ;; lines before and after point.
388 "Insert a newline before and after a template, and possibly do indenting.
389 Specify the :blank argument to enable this inserter.")
391 (defmethod srecode-insert-method ((sti srecode-template-inserter-blank
)
393 "Make sure there is no text before or after point."
394 (let ((i (srecode-dictionary-lookup-name dictionary
"INDENT"))
395 (inbuff (bufferp standard-output
))
398 ;; Don't do this if we are not the active template.
399 (= (length (oref srecode-template active
)) 1))
401 (when (and (eq i t
) inbuff
(not (eq (oref sti where
) 'begin
)))
402 (indent-according-to-mode)
405 (cond ((and (eq (oref sti where
) 'begin
) (not (bolp)))
407 ((eq (oref sti where
) 'end
)
408 ;; If there is whitespace after pnt, then clear it out.
409 (when (looking-at "\\s-*$")
410 (delete-region (point) (point-at-eol)))
414 (setq pm
(point-marker))
415 (when (and (eq i t
) inbuff
(not (eq (oref sti where
) 'end
)))
416 (indent-according-to-mode)
420 (defclass srecode-template-inserter-comment
(srecode-template-inserter)
424 "The character code used to identify inserters of this style.")
426 "Allow comments within template coding. This inserts nothing.")
428 (defmethod srecode-inserter-prin-example :STATIC
((ins srecode-template-inserter-comment
)
429 escape-start escape-end
)
430 "Insert an example using inserter INS.
431 Arguments ESCAPE-START and ESCAPE-END are the current escape sequences in use."
434 (princ "! Miscellaneous text commenting in your template. ")
439 (defmethod srecode-insert-method ((sti srecode-template-inserter-comment
)
441 "Don't insert anything for comment macros in STI."
445 (defclass srecode-template-inserter-variable
(srecode-template-inserter)
449 "The character code used to identify inserters of this style."))
450 "Insert the value of a dictionary entry.
451 If there is no entry, insert nothing.")
453 (defvar srecode-inserter-variable-current-dictionary nil
454 "The active dictionary when calling a variable filter.")
456 (defmethod srecode-insert-variable-secondname-handler
457 ((sti srecode-template-inserter-variable
) dictionary value secondname
)
458 "For VALUE handle SECONDNAME behaviors for this variable inserter.
459 Return the result as a string.
460 By default, treat as a function name.
461 If SECONDNAME is nil, return VALUE."
463 (let ((fcnpart (read secondname
)))
464 (if (fboundp fcnpart
)
465 (let ((srecode-inserter-variable-current-dictionary dictionary
))
466 (funcall fcnpart value
))
468 (srecode-insert-report-error
470 "Variable inserter %s: second argument `%s' is not a function"
471 (object-print sti
) secondname
)))
474 (defmethod srecode-insert-method ((sti srecode-template-inserter-variable
)
476 "Insert the STI inserter."
477 ;; Convert the name into a name/fcn pair
478 (let* ((name (oref sti
:object-name
))
479 (fcnpart (oref sti
:secondname
))
480 (val (srecode-dictionary-lookup-name
484 ;; Alert if a macro wasn't found.
486 (message "Warning: macro %S was not found in the dictionary." name
)
488 ;; If there was a functional part, call that function.
491 (setq val
(srecode-insert-variable-secondname-handler
492 sti dictionary val fcnpart
)))
493 ;; Compound data value
494 ((srecode-dictionary-compound-value-child-p val
)
495 ;; Force FCN to be a symbol
496 (when fcnpart
(setq fcnpart
(read fcnpart
)))
497 ;; Convert compound value to a string with the fcn.
498 (setq val
(srecode-compound-toString val fcnpart dictionary
))
499 ;; If the value returned is nil, then it may be a special
500 ;; field inserter that requires us to set do-princ to nil.
502 (setq do-princ nil
)))
504 ;; Dictionaries... not allowed in this style
505 ((srecode-dictionary-child-p val
)
506 (srecode-insert-report-error
508 "Macro %s cannot insert a dictionary - use section macros instead"
511 ;; Other stuff... convert
513 (srecode-insert-report-error
515 "Macro %s cannot insert arbitrary data" name
)))
516 ;; Output the dumb thing unless the type of thing specifically
517 ;; did the inserting for us.
521 (defclass srecode-template-inserter-ask
(srecode-template-inserter-variable)
525 "The character code used to identify inserters of this style.")
526 (prompt :initarg
:prompt
529 "The prompt used to query for this dictionary value.")
530 (defaultfcn :initarg
:defaultfcn
533 "The function which can calculate a default value.")
534 (read-fcn :initarg
:read-fcn
535 :initform
'read-string
537 "The function used to read in the text for this prompt.")
539 "Insert the value of a dictionary entry.
540 If there is no entry, prompt the user for the value to use.
541 The prompt text used is derived from the previous PROMPT command in the
544 (defmethod srecode-inserter-apply-state
545 ((ins srecode-template-inserter-ask
) STATE
)
546 "For the template inserter INS, apply information from STATE.
547 Loop over the prompts to see if we have a match."
548 (let ((prompts (oref STATE prompts
))
551 (when (string= (semantic-tag-name (car prompts
))
552 (oref ins
:object-name
))
554 (semantic-tag-get-attribute (car prompts
) :text
))
555 (oset ins
:defaultfcn
556 (semantic-tag-get-attribute (car prompts
) :default
))
558 (or (semantic-tag-get-attribute (car prompts
) :read
)
561 (setq prompts
(cdr prompts
)))
564 (defmethod srecode-insert-method ((sti srecode-template-inserter-ask
)
566 "Insert the STI inserter."
567 (let ((val (srecode-dictionary-lookup-name
568 dictionary
(oref sti
:object-name
))))
570 ;; Does some extra work. Oh well.
573 ;; How is our -ask value determined?
574 (if srecode-insert-with-fields-in-progress
575 ;; Setup editable fields.
576 (setq val
(srecode-insert-method-field sti dictionary
))
577 ;; Ask the question...
578 (setq val
(srecode-insert-method-ask sti dictionary
)))
580 ;; After asking, save in the dictionary so that
581 ;; the user can use the same name again later.
582 (srecode-dictionary-set-value
583 (srecode-root-dictionary dictionary
)
584 (oref sti
:object-name
) val
)
586 ;; Now that this value is safely stowed in the dictionary,
587 ;; we can do what regular inserters do.
588 (call-next-method))))
590 (defmethod srecode-insert-ask-default ((sti srecode-template-inserter-ask
)
592 "Derive the default value for an askable inserter STI.
593 DICTIONARY is used to derive some values."
594 (let ((defaultfcn (oref sti
:defaultfcn
)))
596 ((stringp defaultfcn
)
599 ((functionp defaultfcn
)
600 (funcall defaultfcn
))
602 ((and (listp defaultfcn
)
603 (eq (car defaultfcn
) 'macro
))
604 (srecode-dictionary-lookup-name
605 dictionary
(cdr defaultfcn
)))
611 (srecode-insert-report-error
613 "Unknown default for prompt: %S" defaultfcn
)))))
615 (defmethod srecode-insert-method-ask ((sti srecode-template-inserter-ask
)
617 "Do the \"asking\" for the template inserter STI.
618 Use DICTIONARY to resolve values."
619 (let* ((prompt (oref sti prompt
))
620 (default (srecode-insert-ask-default sti dictionary
))
621 (reader (oref sti
:read-fcn
))
624 (cond ((eq reader
'y-or-n-p
)
625 (if (y-or-n-p (or prompt
627 (oref sti
:object-name
))))
630 ((eq reader
'read-char
)
633 (read-char (or prompt
634 (format "Char for %s: "
635 (oref sti
:object-name
))))))
639 (setq val
(funcall reader
641 (format "Specify %s: "
642 (oref sti
:object-name
)))
645 ;; Return our derived value.
649 (defmethod srecode-insert-method-field ((sti srecode-template-inserter-ask
)
651 "Create an editable field for the template inserter STI.
652 Use DICTIONARY to resolve values."
653 (let* ((default (srecode-insert-ask-default sti dictionary
))
655 (srecode-field-value (oref sti
:object-name
)
657 :defaultvalue default
))
659 ;; Return this special compound value as the thing to insert.
660 ;; This special compound value will repeat our asked question
661 ;; across multiple locations.
664 (defmethod srecode-dump ((ins srecode-template-inserter-ask
) indent
)
665 "Dump the state of the SRecode template inserter INS."
668 (princ (oref ins prompt
))
672 (defclass srecode-template-inserter-width
(srecode-template-inserter-variable)
676 "The character code used to identify inserters of this style.")
678 "Inserts the value of a dictionary variable with a specific width.
679 The second argument specifies the width, and a pad, separated by a colon.
680 Thus a specification of `10:left' will insert the value of A
681 to 10 characters, with spaces added to the left. Use `right' for adding
682 spaces to the right.")
684 (defmethod srecode-insert-variable-secondname-handler
685 ((sti srecode-template-inserter-width
) dictionary value width
)
686 "For VALUE handle WIDTH behaviors for this variable inserter.
687 Return the result as a string.
688 By default, treat as a function name."
689 ;; Cannot work without width.
691 (srecode-insert-report-error
693 "Width not specified for variable/width inserter"))
695 ;; Trim or pad to new length
696 (let* ((split (split-string width
":"))
697 (width (string-to-number (nth 0 split
)))
698 (second (nth 1 split
))
700 ((or (null second
) (string= "right" second
))
702 ((string= "left" second
)
705 (srecode-insert-report-error
707 "Unknown pad type %s" second
)))))
708 (if (>= (length value
) width
)
709 ;; Simple case - too long.
710 (substring value
0 width
)
711 ;; We need to pad on one side or the other.
712 (let ((padchars (make-string (- width
(length value
)) ?
)))
714 (concat padchars value
)
715 (concat value padchars
))))))
717 (defmethod srecode-inserter-prin-example :STATIC
((ins srecode-template-inserter-width
)
718 escape-start escape-end
)
719 "Insert an example using inserter INS.
720 Arguments ESCAPE-START and ESCAPE-END are the current escape sequences in use."
723 (princ "|A:10:right")
728 (defvar srecode-template-inserter-point-override nil
729 "Point-positioning method for the SRecode template inserter.
730 When nil, perform normal point-positioning behavior.
731 When the value is a cons cell (DEPTH . FUNCTION), call FUNCTION
732 instead, unless the template nesting depth, measured
733 by (length (oref srecode-template active)), is greater than
737 (defclass srecode-template-inserter-point
(srecode-template-inserter)
741 "The character code used to identify inserters of this style.")
742 (point :type
(or null marker
)
745 "Record the value of (point) in this class slot.
746 It is the responsibility of the inserter algorithm to clear this
747 after a successful insertion."))
748 "Record the value of (point) when inserted.
749 The cursor is placed at the ^ macro after insertion.
750 Some inserter macros, such as `srecode-template-inserter-include-wrap'
751 will place text at the ^ macro from the included macro.")
753 (defmethod srecode-inserter-prin-example :STATIC
((ins srecode-template-inserter-point
)
754 escape-start escape-end
)
755 "Insert an example using inserter INS.
756 Arguments ESCAPE-START and ESCAPE-END are the current escape sequences in use."
764 (defmethod srecode-insert-method ((sti srecode-template-inserter-point
)
766 "Insert the STI inserter.
767 Save point in the class allocated 'point' slot.
768 If `srecode-template-inserter-point-override' non-nil then this
769 generalized marker will do something else. See
770 `srecode-template-inserter-include-wrap' as an example."
771 ;; If `srecode-template-inserter-point-override' is non-nil, its car
772 ;; is the maximum template nesting depth for which the override is
773 ;; valid. Compare this to the actual template nesting depth and
774 ;; maybe use the override function which is stored in the cdr.
775 (if (and srecode-template-inserter-point-override
776 (<= (length (oref srecode-template active
))
777 (car srecode-template-inserter-point-override
)))
778 ;; Disable the old override while we do this.
779 (let ((over (cdr srecode-template-inserter-point-override
))
780 (srecode-template-inserter-point-override nil
))
781 (funcall over dictionary
))
782 (oset sti point
(point-marker))
785 (defclass srecode-template-inserter-subtemplate
(srecode-template-inserter)
787 "Wrap a section of a template under the control of a macro."
790 (defmethod srecode-inserter-prin-example :STATIC
((ins srecode-template-inserter-subtemplate
)
791 escape-start escape-end
)
792 "Insert an example using inserter INS.
793 Arguments ESCAPE-START and ESCAPE-END are the current escape sequences in use."
795 (princ " Template Text to control")
804 (defmethod srecode-insert-subtemplate ((sti srecode-template-inserter-subtemplate
)
806 "Insert a subtemplate for the inserter STI with dictionary DICT."
807 ;; Make sure that only dictionaries are used.
808 (unless (srecode-dictionary-child-p dict
)
809 (srecode-insert-report-error
811 "Only section dictionaries allowed for `%s'"
812 (eieio-object-name-string sti
)))
814 ;; Output the code from the sub-template.
815 (srecode-insert-method (slot-value sti slot
) dict
))
817 (defmethod srecode-insert-method-helper ((sti srecode-template-inserter-subtemplate
)
819 "Do the work for inserting the STI inserter.
820 Loops over the embedded CODE which was saved here during compilation.
821 The template to insert is stored in SLOT."
822 (let ((dicts (srecode-dictionary-lookup-name
823 dictionary
(oref sti
:object-name
))))
824 (when (not (listp dicts
))
825 (srecode-insert-report-error
827 "Cannot insert section %S from non-section variable."
828 (oref sti
:object-name
)))
829 ;; If there is no section dictionary, then don't output anything
830 ;; from this section.
832 (when (not (srecode-dictionary-p (car dicts
)))
833 (srecode-insert-report-error
835 "Cannot insert section %S from non-section variable."
836 (oref sti
:object-name
)))
837 (srecode-insert-subtemplate sti
(car dicts
) slot
)
838 (setq dicts
(cdr dicts
)))))
840 (defmethod srecode-insert-method ((sti srecode-template-inserter-subtemplate
)
842 "Insert the STI inserter.
843 Calls back to `srecode-insert-method-helper' for this class."
844 (srecode-insert-method-helper sti dictionary
'template
))
847 (defclass srecode-template-inserter-section-start
(srecode-template-inserter-subtemplate)
851 "The character code used to identify inserters of this style.")
852 (template :initarg
:template
854 "A template used to frame the codes from this inserter.")
856 "Apply values from a sub-dictionary to a template section.
857 The dictionary saved at the named dictionary entry will be
858 applied to the text between the section start and the
859 `srecode-template-inserter-section-end' macro.")
861 (defmethod srecode-parse-input ((ins srecode-template-inserter-section-start
)
863 "For the section inserter INS, parse INPUT.
864 Shorten input until the END token is found.
865 Return the remains of INPUT."
866 (let* ((out (srecode-compile-split-code tag input STATE
867 (oref ins
:object-name
))))
868 (oset ins template
(srecode-template
869 (eieio-object-name-string ins
)
875 (defmethod srecode-dump ((ins srecode-template-inserter-section-start
) indent
)
876 "Dump the state of the SRecode template inserter INS."
879 (srecode-dump-code-list (oref (oref ins template
) code
)
883 (defclass srecode-template-inserter-section-end
(srecode-template-inserter)
887 "The character code used to identify inserters of this style.")
889 "All template segments between the section-start and section-end
890 are treated specially.")
892 (defmethod srecode-insert-method ((sti srecode-template-inserter-section-end
)
894 "Insert the STI inserter."
897 (defmethod srecode-match-end ((ins srecode-template-inserter-section-end
) name
)
899 "For the template inserter INS, do I end a section called NAME?"
900 (string= name
(oref ins
:object-name
)))
902 (defclass srecode-template-inserter-include
(srecode-template-inserter-subtemplate)
906 "The character code used to identify inserters of this style.")
908 :initarg
:includedtemplate
910 "The template included for this inserter."))
911 "Include a different template into this one.
912 The included template will have additional dictionary entries from the subdictionary
913 stored specified by this macro.")
915 (defmethod srecode-inserter-prin-example :STATIC
((ins srecode-template-inserter-include
)
916 escape-start escape-end
)
917 "Insert an example using inserter INS.
918 Arguments ESCAPE-START and ESCAPE-END are the current escape sequences in use."
921 (princ ">DICTNAME:contextname:templatename")
926 (defmethod srecode-insert-include-lookup ((sti srecode-template-inserter-include
)
928 "For the template inserter STI, lookup the template to include.
929 Finds the template with this macro function part and stores it in
930 this template instance."
931 (let ((templatenamepart (oref sti
:secondname
)))
932 ;; If there was no template name, throw an error.
933 (unless templatenamepart
934 (srecode-insert-report-error
936 "Include macro `%s' needs a template name"
937 (oref sti
:object-name
)))
939 ;; NOTE: We used to cache the template and not look it up a second time,
940 ;; but changes in the template tables can change which template is
941 ;; eventually discovered, so now we always lookup that template.
943 ;; Calculate and store the discovered template
944 (let ((tmpl (srecode-template-get-table (srecode-table)
946 (active (oref srecode-template active
))
949 ;; If it isn't just available, scan back through
950 ;; the active template stack, searching for a matching
952 (while (and (not tmpl
) active
)
953 (setq ctxt
(oref (car active
) context
))
954 (setq tmpl
(srecode-template-get-table (srecode-table)
958 (when (slot-boundp (car active
) 'table
)
959 (let ((app (oref (oref (car active
) table
) application
)))
961 (setq tmpl
(srecode-template-get-table
966 (setq active
(cdr active
)))
968 ;; If it wasn't in this context, look to see if it
969 ;; defines its own context
970 (setq tmpl
(srecode-template-get-table (srecode-table)
974 ;; Store the found template into this object for later use.
975 (oset sti
:includedtemplate tmpl
))
977 (unless (oref sti includedtemplate
)
978 ;; @todo - Call into a debugger to help find the template in question.
979 (srecode-insert-report-error
981 "No template \"%s\" found for include macro `%s'"
982 templatenamepart
(oref sti
:object-name
)))))
984 (defmethod srecode-insert-method ((sti srecode-template-inserter-include
)
986 "Insert the STI inserter.
987 Finds the template with this macro function part, and inserts it
988 with the dictionaries found in the dictionary."
989 (srecode-insert-include-lookup sti dictionary
)
990 ;; Insert the template.
991 ;; Our baseclass has a simple way to do this.
992 (if (srecode-dictionary-lookup-name dictionary
(oref sti
:object-name
))
993 ;; If we have a value, then call the next method
994 (srecode-insert-method-helper sti dictionary
'includedtemplate
)
995 ;; If we don't have a special dictionary, then just insert with the
996 ;; current dictionary.
997 (srecode-insert-subtemplate sti dictionary
'includedtemplate
))
1001 ;; This template combines the include template and the sectional template.
1002 ;; It will first insert the included template, then insert the embedded
1003 ;; template wherever the $^$ in the included template was.
1005 ;; Since it uses dual inheritance, it will magically get the end-matching
1006 ;; behavior of #, with the including feature of >.
1008 (defclass srecode-template-inserter-include-wrap
(srecode-template-inserter-include srecode-template-inserter-section-start
)
1012 "The character code used to identify inserters of this style.")
1014 "Include a different template into this one, and add text at the ^ macro.
1015 The included template will have additional dictionary entries from the subdictionary
1016 stored specified by this macro. If the included macro includes a ^ macro,
1017 then the text between this macro and the end macro will be inserted at
1020 (defmethod srecode-inserter-prin-example :STATIC
((ins srecode-template-inserter-include-wrap
)
1021 escape-start escape-end
)
1022 "Insert an example using inserter INS.
1023 Arguments ESCAPE-START and ESCAPE-END are the current escape sequences in use."
1025 (princ escape-start
)
1026 (princ "<DICTNAME:contextname:templatename")
1029 (princ " Template Text to insert at ^ macro")
1032 (princ escape-start
)
1038 (defmethod srecode-insert-method ((sti srecode-template-inserter-include-wrap
)
1040 "Insert the template STI.
1041 This will first insert the include part via inheritance, then
1042 insert the section it wraps into the location in the included
1043 template where a ^ inserter occurs."
1044 ;; Step 1: Look up the included inserter
1045 (srecode-insert-include-lookup sti dictionary
)
1046 ;; Step 2: Temporarily override the point inserter.
1047 ;; We bind `srecode-template-inserter-point-override' to a cons cell
1048 ;; (DEPTH . FUNCTION) that has the maximum template nesting depth,
1049 ;; for which the override is valid, in DEPTH and a lambda function
1050 ;; which implements the wrap insertion behavior in FUNCTION. The
1051 ;; maximum valid nesting depth is just the current depth + 1.
1052 (let ((srecode-template-inserter-point-override
1053 (lexical-let ((inserter1 sti
))
1056 (+ (length (oref srecode-template active
)) 1)
1059 (let ((srecode-template-inserter-point-override nil
))
1060 (if (srecode-dictionary-lookup-name
1061 dict
(oref inserter1
:object-name
))
1062 ;; Insert our sectional part with looping.
1063 (srecode-insert-method-helper
1064 inserter1 dict
'template
)
1065 ;; Insert our sectional part just once.
1066 (srecode-insert-subtemplate
1067 inserter1 dict
'template
))))))))
1068 ;; Do a regular insertion for an include, but with our override in
1070 (call-next-method)))
1072 (provide 'srecode
/insert
)
1075 ;; generated-autoload-file: "loaddefs.el"
1076 ;; generated-autoload-load-name: "srecode/insert"
1079 ;;; srecode/insert.el ends here