[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: replace deprecated function ?
From: |
Kaushal Modi |
Subject: |
Re: replace deprecated function ? |
Date: |
Tue, 13 Feb 2018 21:14:28 +0000 |
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 "⁰".
So, if I have:
123 abc 456 def 78 90
and select just the "56 def 78" portion, and do M-x num-to-supnum, I end up
with:
123 abc 4⁵⁶ def ⁷⁸ 90
[1]: http://lists.gnu.org/r/emacs-devel/2018-01/msg00641.html
--
Kaushal Modi
- Re: replace deprecated function ?, (continued)
- Re: replace deprecated function ?, Emanuel Berg, 2018/02/13
- Re: replace deprecated function ?, B. T. Raven, 2018/02/13
- Re: replace deprecated function ?, Joost Kremers, 2018/02/13
- Re: replace deprecated function ?, Emanuel Berg, 2018/02/13
- Re: replace deprecated function ?, Emanuel Berg, 2018/02/13
- Re: replace deprecated function ?, Yuri Khan, 2018/02/13
- Message not available
- Re: replace deprecated function ?, Emanuel Berg, 2018/02/13
- Message not available
- Re: replace deprecated function ?, B. T. Raven, 2018/02/14
- Re: replace deprecated function ?, Emanuel Berg, 2018/02/16
- Re: replace deprecated function ?, B. T. Raven, 2018/02/16
Re: replace deprecated function ?,
Kaushal Modi <=
Message not available