Search code examples
emacselisp

When do variables output properly in skeletons functions?


I'm trying to write a skeleton-function to output expressions in a loop. Out of a loop I can do,

(define-skeleton test
    ""
    > "a")

When I evaluate this function it outputs "a" into the working buffer as desired. However, I'm having issues when inserting this into a loop. I now have,

(define-skeleton test
  "A test skeleton"
  (let ((i 1))
  (while (< i 5)
   >"a"
   (setq i (1+ i)))))

I would expect this to output "aaaaa". However, instead nothing is outputted into the working buffer in this case. What is happening when I insert the loop?


Solution

  • The > somestring skeleton dsl does not work inside lisp forms. You can however concatenate the string inside a loop:

    (define-skeleton barbaz
        ""
      ""
      (let ((s ""))
        (dotimes (i 5)
          (setq s (concat s "a")))
        s)
      )
    

    My understanding is that code such as > "a"

    only works at the first nesting level inside a skeleton.

    [EDIT] Regarding your question

    What is happening when I insert the loop?

    The return value of the let form (that is, the return value of the while form)is inserted. I do not know why it does not raise an error when evaluating > "a", but the return value of a while form is nil, so nothing is inserted.