EmacsGolf Challenge 2

Xah Lee sent me another suggestion for an EmacsGolf challenge. While in a whimsical mood, Lee decided to write some Elisp to convert Latin text to Fraktur (Gothic font). That's an interesting Elisp challenge in its own right but Lee decided to use a simple table lookup and to provide the facility to go back from Fraktur to Latin. His table, as you can see at the link, is a vector of vectors and a bit of a pain to construct. Having built the first table, Lee wants to make the second as easily as possible. Given the starting table

(setq latin-to-gothic [ ["A" "𝔄"] ["B" "𝔅"] ["C" "ℭ"] ["D" "𝔇"] ["E" "𝔈"]
["F" "𝔉"] ["G" "𝔊"] ["H" "ℌ"] ["I" "ℑ"] ["J" "𝔍"] ["K" "𝔎"] ["L" "𝔏"]
["M" "𝔐"] ["N" "𝔑"] ["O" "𝔒"] ["P" "𝔓"] ["Q" "𝔔"] ["R" "ℜ"] ["S" "𝔖"]
["T" "𝔗"] ["U" "𝔘"] ["V" "𝔙"] ["W" "𝔚"] ["X" "𝔛"] ["Y" "𝔜"] ["Z" "ℨ"]
["a" "𝔞"] ["b" "𝔟"] ["c" "𝔠"] ["d" "𝔡"] ["e" "𝔢"] ["f" "𝔣"] ["g" "𝔤"]
["h" "𝔥"] ["i" "𝔦"] ["j" "𝔧"] ["k" "𝔨"] ["l" "𝔩"] ["m" "𝔪"] ["n" "𝔫"]
["o" "𝔬"] ["p" "𝔭"] ["q" "𝔮"] ["r" "𝔯"] ["s" "𝔰"] ["t" "𝔱"] ["u" "𝔲"]
["v" "𝔳"] ["w" "𝔴"] ["x" "𝔵"] ["y" "𝔶"] ["z" "𝔷"]])

we want to make a second table to map Fraktur to Latin.

(setq latin-to-gothic [ ["A" "𝔄"] ["B" "𝔅"] ["C" "ℭ"] ["D" "𝔇"] ["E" "𝔈"]
["F" "𝔉"] ["G" "𝔊"] ["H" "ℌ"] ["I" "ℑ"] ["J" "𝔍"] ["K" "𝔎"] ["L" "𝔏"]
["M" "𝔐"] ["N" "𝔑"] ["O" "𝔒"] ["P" "𝔓"] ["Q" "𝔔"] ["R" "ℜ"] ["S" "𝔖"]
["T" "𝔗"] ["U" "𝔘"] ["V" "𝔙"] ["W" "𝔚"] ["X" "𝔛"] ["Y" "𝔜"] ["Z" "ℨ"]
["a" "𝔞"] ["b" "𝔟"] ["c" "𝔠"] ["d" "𝔡"] ["e" "𝔢"] ["f" "𝔣"] ["g" "𝔤"]
["h" "𝔥"] ["i" "𝔦"] ["j" "𝔧"] ["k" "𝔨"] ["l" "𝔩"] ["m" "𝔪"] ["n" "𝔫"]
["o" "𝔬"] ["p" "𝔭"] ["q" "𝔮"] ["r" "𝔯"] ["s" "𝔰"] ["t" "𝔱"] ["u" "𝔲"]
["v" "𝔳"] ["w" "𝔴"] ["x" "𝔵"] ["y" "𝔶"] ["z" "𝔷"] ])

(setq gothic-to-latin [ ["𝔄" "A"] ["𝔅" "B"] ["ℭ" "C"] ["𝔇" "D"] ["𝔈" "E"]
["𝔉" "F"] ["𝔊" "G"] ["ℌ" "H"] ["ℑ" "I"] ["𝔍" "J"] ["𝔎" "K"] ["𝔏" "L"]
["𝔐" "M"] ["𝔑" "N"] ["𝔒" "O"] ["𝔓" "P"] ["𝔔" "Q"] ["ℜ" "R"] ["𝔖" "S"]
["𝔗" "T"] ["𝔘" "U"] ["𝔙" "V"] ["𝔚" "W"] ["𝔛" "X"] ["𝔜" "Y"] ["ℨ" "Z"]
["𝔞" "a"] ["𝔟" "b"] ["𝔠" "c"] ["𝔡" "d"] ["𝔢" "e"] ["𝔣" "f"] ["𝔤" "g"]
["𝔥" "h"] ["𝔦" "i"] ["𝔧" "j"] ["𝔨" "k"] ["𝔩" "l"] ["𝔪" "m"] ["𝔫" "n"]
["𝔬" "o"] ["𝔭" "p"] ["𝔮" "q"] ["𝔯" "r"] ["𝔰" "s"] ["𝔱" "t"] ["𝔲" "u"]
["𝔳" "v"] ["𝔴" "w"] ["𝔵" "x"] ["𝔶" "y"] ["𝔷" "z"] ])

Lee says he did this with a simple replace-regexp. Can you do better? Since this is EmacsGolf, you can use anything in stock Emacs, ELPA, or an external tool called from Emacs.

This entry was posted in General and tagged , . Bookmark the permalink.
  • Jonathan

    Hopefully the formatting comes out correctly.

    C-M-SPC ;; Mark sexp
    M-w ;; Copy sexp
    C-M-f ;; Forward sexp
    RET ;; Add new line
    RET ;; Add new line
    C-y ;; Yank sexp
    C-M-b ;; Backward sexp
    C-M-% ;; Query-regexp-replace
    \w+-to-\|\w+ ;; Find any two sets of word characters with -to- or " " between them
    \3\2\1 ;; Reverse the order
    RET ! ;; Perform on all matches

    44 keystrokes.

  • This one is pretty simple, this is a first thing that came to my mind (=likely what I would really do)

    C-] ;; sp-select-next-thing
    M-w ;; kill-ring-save
    C-y ;; yank
    C-u C-M-d ;; sp-down-sexp, C-u makes it go as much as possible. This leaves me on [|"A"...
    C-M-f ;; sp-forward-sexp
    C-M-t ;; transpose-sexp
    C-M-e ;; sp-up-sexp
    C-M-d ;; sp-down-sexp
    C-56 F4 ;; execute macro
    C-M-d ;; sp-down-sexp, being at the end of sexp it jumps to the beginning
    ;; now fix the latin-to-gothic
    M-f ;; forward-word
    C-SPC ;; activate mark
    M-2 M-f ;; two words forward
    M-0 M-t ;; transpose words at mark and point

    Done. 24 if I count right.

    Now here's a super-boss solution using multiple cursors :P

    C-2 C-M-d ;; sp-down-sexp twice
    C-SPC ;; set mark
    C-M-a ;; sp-backward-down-sexp - this jumps to the end of sexp
    M-s-r ;; mc/mark-all-in-region (I use s and M-s prefixes for mc)
    C-q [ ;; this prevents smartparens from inserting []. Now we have a cursor at each [ of the pair.
    C-M-f ;; sp-forward-sexp
    C-M-t ;; transpose-sexp
    C-g ;; quit mc
    C-2 C-M-u ;; twice sp-backward-up-sexp
    M-- M-2 M-t

    Done. That's 17 :)

    • Uh, sorry. I forgot to copy the form in the 2nd example. So add +5 for copying. That leaves it at 22.

      (by the way I have an account but still can't edit posts :/)

      • jcs

        Darn. OK, let me investigate and see if there is some way of enabling user edits. Reading the WP documentation is a bit painful so I've been hoping that setting up the account would solve the problem.

    • super-boss solution :D

  • Jorge

    C-space M-e M-w C-m C-m C-y C-r - M-t M-b M-b M-t M-t M-f F3 M-t M-f F4 M-5 M-1 F4

    21 keystrokes with standard emacs

    • Phil

      Keeping my assumption that there's a newline at the end of the original text, I can reduce your approach to 17 keystrokes:

      C-SPC M-%gt; M-w RET C-y C-r - M-t M-b M-b M-t M-t <f3%gt; M-f M-t C-0 <f4%gt;

      • Phil

        Wow, I'm having a bad day with markup, clearly...

        C-SPC M-> M-w RET C-y C-r - M-t M-b M-b M-t M-t <f3> M-f M-t C-0 <f4>

        • Jorge

          Nice! =)

  • i did with 18 keystrokes.

    hard to give the exact keys, because i have many customization, but the solution should be doable in gnu emacs with similar number of keys. Here's my major steps.

    • call query-replace-regexp

    • give 「." ".

    • type the first part the 「.」, then press a key to copy (there's a convenience that it'll copy the line when there's no selection).

    • also, i have abbrev that auto types 「[^」 . If i mod my abbrev first to type 「」, that'll save me 3 keys ☺. electric-pair-mode is also useful, but i have my own setup to type pairs.

    • for replacement, type 「\2" "\1」. Again, i have setup to type quote pairs too, so that can deduct one key.

    • then type ! to replace all.

    but if we actually count keystrokes, mine is probably 30 or 40, because all key combinations such as Ctrl+a for me is sequence of single keys. e.g. my query-replace-regex is 【 r u】 on dvorak, 3 keys.

  • oops, several critical text got auto-converted in WordPress i think. The curly quote above should be straight double quotes. The last line, about query replace key, should be 【menu r u】.

  • Phil

    My first attempt was 23 keystrokes, but I've reduced it to 20:

    C-SPC M-w C-y
    C-SPC C-0 M-t
    C-s " SPC M-t C-0

    • Phil

      Oops, I didn't escape the <s. That should have been:

      C-SPC <C-end> M-w <return> C-y <C-M-left>
      <C-right> <right> C-SPC <C-M-right> <left> C-0 M-t
      <f3> C-s " SPC M-t C-0 <f4>

      • Phil

        FWIW it turns out that the <right> is redundant, so that goes down to 19 (but that's still not as good as the modification of Jorge's solution).