1

Sometimes it would be handy to get the previously entered field values, and process them.

Example: the second field (function name) of this snippet is always the camelCase version of the first field (description) in my workflow.

describe('$1', function $2() {
  $0
});

To write lisp function to do this, one should know how to get the value of the first field.

1

According to the Yasnippet Documentation in the Mirrors with Transformations, you can have a $1 substitution multiple times in your snippet, but each one can be transformed. Try this:

describe('$1', function ${1:$(camelize yas-text)}() {
  $0
});

That said, you'll need a camelize function written in Lisp. Check out the examples on the Emacs Wiki, like:

(defun camelize (s)
  "Convert under_score string S to CamelCase string."
  (mapconcat 'identity (mapcar
                        '(lambda (word) (capitalize (downcase word)))
                        (split-string s "_")) ""))

Hope that helps.

  • Thanks, it worked nice. I modified it a bit to my specific case. (defun my/camelize (s) "Convert under_score string S to CamelCase string." (store-substring (mapconcat 'identity (mapcar (lambda (word) (capitalize (downcase word))) (split-string s "[-_ ]+")) "") 0 (if (> (length s) 0) (downcase (substring s 0 1))))) – atevm Oct 4 '16 at 9:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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