elemakil
elemakil

Reputation: 3811

Replacing yasnippet fields by entries from a list

Recently I stumbled across the question "A quick way to repeatedly enter a variable name in Emacs?" which asks for a way to create and expand snippets "on-the-fly". I also found the package auto-yasnippet which was created by the author of the question and is probably the result of his question. I like this package very much, but I would like to get expanded functionality. The package allows to do the following:

You write the following snippet:

convert img$1.jpg -monochrome -resize 50% -rotate 180 img$1_mono.pdf

call aya-create, enter a new line and call aya-expand: The above code is pasted and you can replace the placeholders ($1, $2, ...) just as you would do for an ordinary yasnippet.

This is a great feature, however, if you have to create many copies of the snippet and enter the expansion string it becomes not so fun. It would be nice to pass a list (or list of lists if you have multiple placeholders) which are used to expand the placeholders. Lets say I have to call the above thing for files img_1.jpg to img_9.jpg and the results should be called img_a_mono.jpg to img_k_mono.jpg, thus one would create the list:

(setq foo-list '( '( 1 2 3 4 5 6 7 8 9 ) '( a b c d e f g h i j k ) )

write the snippet

convert img$1.jpg -monochrome -resize 50% -rotate 180 img$2_mono.pdf

and call the expansion function specifying foo-list as argument. [Of course one might create these lists on the fly by a function like number-sequence]

I am afraid I'm not proficient enough with elips to code this by myself, however, maybe someone has the skills and sees the use of this feature.

Upvotes: 1

Views: 317

Answers (1)

abo-abo
abo-abo

Reputation: 20352

I've written another package that now does the job that you describe. The relevant snippet is:

m1\n10&convert img&s.jpg -monochrome -resize 50% -rotate 180 img&s_mono.pdf

It expands to

convert img1.jpg -monochrome -resize 50% -rotate 180 img1_mono.pdf
convert img2.jpg -monochrome -resize 50% -rotate 180 img2_mono.pdf
convert img3.jpg -monochrome -resize 50% -rotate 180 img3_mono.pdf
convert img4.jpg -monochrome -resize 50% -rotate 180 img4_mono.pdf
convert img5.jpg -monochrome -resize 50% -rotate 180 img5_mono.pdf
convert img6.jpg -monochrome -resize 50% -rotate 180 img6_mono.pdf
convert img7.jpg -monochrome -resize 50% -rotate 180 img7_mono.pdf
convert img8.jpg -monochrome -resize 50% -rotate 180 img8_mono.pdf
convert img9.jpg -monochrome -resize 50% -rotate 180 img9_mono.pdf
convert img10.jpg -monochrome -resize 50% -rotate 180 img10_mono.pdf

The syntax is as follows:

m[<range start:=0>][<separator:= >]<range end>[lisp expr][&][format expr]

x is the default var in the elisp expression. It will take one by one the value of all numbers in the range.

& means that elisp expr has ended and format expr has begun. It can be used as part of the format expr if there's only one. The keys are the same as for format: I just translate & to %.

You can find more snippets in the comment section of tiny.el.

UPD: expr can now return a list

Following your suggestions, here's the next improvement. This one is without auxiliary var(96 is the value of (- ?a 1)):

m1\n10listx+x96&convert img&s.jpg -monochrome -resize 50% -rotate 180 img&c_mono.pdf

This one is with auxiliary var. First you define it (using tiny, of course:):

(setq foo-list '(m 10+x97&?&c))

expand this to

(setq foo-list '(?a ?b ?c ?d ?e ?f ?g ?h ?i ?j ?k))

And here's how you use it:

m1\n10listxnthxfoo-list&convert img&s.jpg -monochrome -resize 50% -rotate 180 img&c_mono.pdf

The expansion will be

convert img1.jpg -monochrome -resize 50% -rotate 180 imga_mono.pdf
convert img2.jpg -monochrome -resize 50% -rotate 180 imgb_mono.pdf
convert img3.jpg -monochrome -resize 50% -rotate 180 imgc_mono.pdf
convert img4.jpg -monochrome -resize 50% -rotate 180 imgd_mono.pdf
convert img5.jpg -monochrome -resize 50% -rotate 180 imge_mono.pdf
convert img6.jpg -monochrome -resize 50% -rotate 180 imgf_mono.pdf
convert img7.jpg -monochrome -resize 50% -rotate 180 imgg_mono.pdf
convert img8.jpg -monochrome -resize 50% -rotate 180 imgh_mono.pdf
convert img9.jpg -monochrome -resize 50% -rotate 180 imgi_mono.pdf
convert img10.jpg -monochrome -resize 50% -rotate 180 imgj_mono.pdf

Upvotes: 1

Related Questions