[emacs-berlin] A riddle to be solved with Emacs

Zelphir Kaltstahl zelphirkaltstahl at posteo.de
Mon Jun 7 17:58:34 UTC 2021


This looks like it should be solvable using one or more keyboard macros [1].

Here is one I just created:

(fset 'experiment-to-json-macro
   (kmacro-lambda-form [?\M-m ?l ?a ?b ?e ?l ?: ?\S-  M-right ?\} ?\M-m ?\{ ?v ?a ?l ?u ?e ?: ?  ?\" ?\" ?, ?  ?\C-s ?\" return left M-S-right ?\M-w M-left M-left M-left M-S-right ?\C-y M-S-left ?\M-x ?d ?o ?w ?- ?r ?e ?g return M-S-right ?\M-% ?  return ?- return ?! home down] 0 "%d"))

> This inserts some Lisp code that, when executed later, will define the same
macro with the same definition it has now. -- [2]

(1) Basically you do the line manipulation for one line, (2) while always
keeping in mind to only use movements of the cursor, which are generally
applicable to all lines (4) move the cursor to the next line (5) and finish the
macro. (6) Then you can use that macro on multiple lines by using the universal
argument as follows:

C-u <enter num of lines here> C-x e

Which runs the last macro you number of lines times. It is important, that you
move the pointer to the next line at the end of your macro, so that the macro
can be run over and over again, moving to the next line by itself, not requiring
you to move to the next line after each macro run.

At the start of the macro I used M-m to go to the first character in the line. I
could have started recording the macro already with the pointer at that place
and not press M-m, but then whatever I store would be less general, as it would
depend on the pointer position, when the macro is invoked.

Hope this helps!


[1]: https://www.gnu.org/software/emacs/manual/html_node/emacs/Keyboard-Macros.html


On 6/7/21 2:00 PM, emacs-berlin-request at emacs-berlin.org wrote:
> Hello everyone,
> the other day I've failed to solve a small task consisting of text
> search and replace in a file. I'm asking for an opinion on how could I
> have solved this using Emacs. This could have been an interesting
> question during the meetup :-)
> Task: you have a text file of about 1000 lines in this format:
> ---snip-snip-snip-snip-snip---
> [
>   ...
> ]
> ---snip-snip-snip-snip-snip---
> You want to transform it into a JSON where each label goes into a `label`
> field, plus a `value` field with a "slug" of the label. A "slug" is a
> conversion of some text replacing puntuation and whitespaces with
> hyphens, example follows:
> ---snip-snip-snip-snip-snip---
> [
>  { value: "only-capital-case", label: "ONLY CAPITAL CASE"},
>  { value: "also-this-one", label: "ALSO THIS ONE"},
>  { value: "hands-are-typing", label: "HANDS ARE TYPING"},
>  { value: "only-letters-numbers-and-punctuation", label: "ONLY LETTERS, NUMBERS 
>  ...
> ]
> ---snip-snip-snip-snip-snip---
> I've tried either using the `multiple-cursors` package, I've tried
> learning my first macro, I've tried with `M-x query-replace-regexp`. All
> my attempts have failed. I could not learn how to use these by reading
> the documentation.
> My solution: ended up with 10 lines of Python :-/
> So, who's up for some Emacs-fu? :-) Thanks!
> p.s. I had to set Emacs in `so-long` mode because it struggled with a
> 1000 lines file (I'm on Emacs native-comp).
> Best,
> ------------------------------
> Subject: Digest Footer
> _______________________________________________
> emacs-berlin mailing list
> emacs-berlin at emacs-berlin.org
> https://mailb.org/mailman/listinfo/emacs-berlin
> ------------------------------
> End of emacs-berlin Digest, Vol 83, Issue 1
> *******************************************

repositories: https://notabug.org/ZelphirKaltstahl

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailb.org/pipermail/emacs-berlin/attachments/20210607/ce8b1ce6/attachment.htm>

More information about the emacs-berlin mailing list