💾 Archived View for gemini.omarpolo.com › post › emacs-macros.gmi captured on 2021-12-04 at 18:04:22. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2021-11-30)

➡️ Next capture (2022-01-08)

-=-=-=-=-=-=-

↩ back to the index

Being lazy, the Emacs macro edition

Written while listening to “Wait” by The Beatles.

Published: 2021-02-10

Tagged with:

#emacs

I just recalled how cool macros are. I was helping to convert a manuscript for a book from LibreOffice to LaTeX, and to speed the conversion we used pandoc. The problem was that pandoc added a lot of noise to the generated code. Take for instance this bit:

\hypertarget{foo-bar-baz}{%
\subsubsection[foo bar baz]{\texorpdfstring{\protect\hypertarget{anchor-92}{}{}Foo Bar Baz}{Foo Bar Baz}}\label{foo-bar-baz}}

that needs to be converted to like

\section{Foo Bar Baz}

i.e. subsection → section and remove some crufts. If there were only a handful of those, I could have done it by hand, but given that were around 700 instance of those, it was unfeasible.

My first idea was to fire up sam and play with it a bit. Unfortunately, it’s been a while since I’ve used it extensively, so I’m a bit rusty. The plan was to use the command x to select every paragraph, then g to filter that type of paragraphs and then something with s, but I failed.

Given that I didn’t want to spend too much time on this, I tried to use an Emacs macro. Oh boy, it went so well.

The macro goes along the lines of

C-s                     ;; isearch-forward
\                       ;; self-insert-command
section                 ;; self-insert-command * 7
RET                     ;; newline
C-a                     ;; move-beginning-of-line
2*C-M-SPC               ;; mark-sexp
C-w                     ;; kill-region
C-p                     ;; previous-line
2*C-o                   ;; open-line
C-y                     ;; yank
C-n                     ;; next-line
M-h                     ;; mark-paragraph
C-w                     ;; kill-region

it goes to the next \section, select the LaTeX command and the text within the square brackets (using mark-sexp two times), move that bit before and then killing the paragraph.

Then ‘C-- C-x e’ and the whole file was fixed.

The bottom line is, I guess, use your editor and learn it, or something along those lines.

-- text: CC-BY-SA-4.0; code: public domain unless specified otherwise

For comments, write at < blog at omarpolo dot com > or @yumh@pleroma.libretux.com in the fediverse.

Capsule proudly assembled with Clojure