1

I'm not very skilled in elisp, I have only a minimal knowledge of it.

I have to modify an existing function, as I'm trying to fix a problem in an obsolete emacs package that could fulfill my needs.?

(defun okk-tmpl-insert (template)
  "Insert template into buffer, performing tag expansions.
See `okk-tmpl-tag-alist' for list of predefined tags."
  (interactive (list (read-file-name "Template file to insert: " okk-tmpl-dir nil t)))
  (setq okk-tmpl-prompt-start-point nil)
  (setq okk-tmpl-prompted-strings '())
  (setq okk-tmpl-eval-start-point nil)
  (setq okk-tmpl-final-point nil)
  (save-restriction
    (narrow-to-region (point) (point))
    (insert-file-contents template)
    (let (char result)
      (while (search-forward "%" nil t)
        (delete-char -1)
        (setq char (char-after))
        (delete-char 1)
        (setq result (assoc (char-to-string char) okk-tmpl-tag-alist))
        (if (not result)
            (error "Unknown tag %%%c" char)
          (setq result (eval (cdr result)))
          (when (stringp result) (insert result))))
      (when okk-tmpl-final-point (goto-char okk-tmpl-final-point)))
    (run-hooks 'okk-tmpl-insert-hook)))

I have modified the parser to set a variable when it is finding a "tag". Now I have to terminate the loop: (while (search-forward "%" nil t).

when this variable is t to terminate the template expansion, that will occur mainly on the start of the code snippets (a real file template in reality).

The code is derived from file-template.el, from https://www.emacswiki.org/emacs/file-template.el but stripped to do a bare minimun work without any automation, I fire the template insertion, select a template and then manually save the modified file.

2

1 Answer 1

3

To terminate the loop when a variable is t, change the condition to

  (while (and (search-forward "%" nil t) (not variable))
4
  • Many Thanks it works as expected. For now it suffice to fulfill my needs, but I have another idea in mind, it is possible to "skip" the tag expansion using same variable until a different tag is found? As example %Z0 will 'suspend' expansion and %Z1 will reactivate expansion. But as it is not related to the original question, I could made another question if required by forum rules (If this could be considered a 'forum'). Commented Oct 20, 2023 at 6:32
  • Creating a new question is definitely the right way to go.
    – choroba
    Commented Oct 20, 2023 at 8:20
  • Also, "Many Thanks" = Accept ;-)
    – choroba
    Commented Oct 20, 2023 at 8:20
  • See emacs.stackexchange.com/help/someone-answers for what the previous comment means :-)
    – NickD
    Commented Oct 21, 2023 at 16:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.