3

I would like to wrap some text around selected text in emacs.

From a selection of the lines:

First item
Second item

I would like to get :

\begin{itemize}
\item First item
\item Second item
\end{itemize}

Using C-c C-e in AucTeX collapses the selection into one line:

\begin{itemize}
\item First item Second item
\end{itemize}

The following snippet in yasnippet:

# -*- mode: snippet -*-
# name : wrap item
# expand-env : ((yas-wrap-around-region nil) (item-string "\item  "))
# binding : C-M-z
# --
\begin{itemize}
`(let ((text (yas-selected-text))) (when text (replace-regexp-in-string "^" item-string text)))` $0
\end{itemize}

gives:

\begin{itemize}
item First item
item Second item
\end{itemize}

I tried using (item-string "\\item ") instead, but that gives the error:

[yas] elisp error: Invalid use of '\' in replacement text

I would like to have the snippet work as I can modify it for use in other contexts also.

user1031565
  • 107
  • 6
  • Suggestion: Do what you need to do manually, but record what you do as a *keyboard macro*. Replay the macro as many times as you need to. From the looks of what you want (but that's not very clear) it doesn't seem like you need more than that. – Drew Jan 02 '15 at 04:34
  • 2
    Here is a link to a recent example of a Yasnippet containing an if/then `elisp` statement, with an insert, which required four backslashes to equal just one backslash -- the example is LaTeX related: http://emacs.stackexchange.com/a/5536/2287 It does *not* use anything that requires AUCTeX -- I am AUCTeX FREE :). – lawlist Jan 02 '15 at 04:56
  • @Drew Not exactly, the backslash of the `\item` is missing in the result. – Christoph Jan 02 '15 at 08:14
  • @lawlist The four backslashes did the trick. Thank you for a constructive response. With regard to some of the other responses, I obviously did not make it clear enough that I wanted a general solution, not just for the specific example given. – user1031565 Jan 02 '15 at 11:05

1 Answers1

2

I just wrote some starter code to solve this problem in a general way. It's at https://github.com/abo-abo/latex-wrap. It already works for the specific case that you describe, and you can help me extend it by posting issues.

abo-abo
  • 20,038
  • 3
  • 50
  • 71