On Tue, Feb 13, 2018 at 1:21 PM B. T. Raven <btraven@nihilo.net> wrote:
But I notice that query-replace-regexp-eval is no longer considered
kosher. How should this be rewritten for Emacs ver. 25? Especially I
would like a function that is more immediately understandable than what
I have now.
Hello,
First of all, I know that you asked for a replacement function for emacs
25.x, but I have an alternative that needs emacs 26.x (because `ucs-names'
returns a hash instead of an alist in emacs 26).
It was fun to come up with this elisp function. Emacs 26 is nearing
release, so it might be a good time to try out its pretest version[1].
If you are still interested.. here's the function:
(defun num-to-supnum (beg end)
"Replace digits with superscript digits in the selected region."
(interactive "r")
(save-restriction
(save-excursion
(narrow-to-region beg end)
(goto-char (point-min))
(let ((char-hash (ucs-names)))
(while (re-search-forward "[0-9]" nil :noerror)
(let* ((matched-char-name (get-char-code-property (string-to-char
(match-string-no-properties 0)) 'name))
(new-name (replace-regexp-in-string "DIGIT" "SUPERSCRIPT"
matched-char-name nil :literal))
(str (char-to-string (gethash new-name char-hash))))
(replace-match str)))))))
It looks wordy, but simply does this:
- First narrows the buffer to your selected region, so that replacements
don't happen outside that.
- Replacements happen only for characters 0 through 9 (if present).
- Let's say "0" is found.. it then finds the 'name property for that char..
which is "DIGIT ZERO".
- Interestingly, the 'name property for ⁰ (superscript zero) is..
"SUPERSCRIPT ZERO". So I simply derive the name of the to-be-replaced-with
string by replacing DIGIT with SUPERSCRIPT.
- Then using "SUPERSCRIPT ZERO" as the key in the hash table, I find the
character code for superscript zero, and replace "0" with "⁰".