help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: replace deprecated function ?


From: B. T. Raven
Subject: Re: replace deprecated function ?
Date: Wed, 14 Feb 2018 17:31:34 -0600
User-agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0

On 2/13/2018 15:14, Kaushal Modi wrote:
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:

Thanks, Kaushal. That is impressive but too advanced for me. I will save it in order to understand unicode better. It looks like the defun takes a huge data gulp at (let ((char-hash (ucs-names))). I know that hash tables are somehow related to crc's, sha256, signatures etc but I don't know how to use them. I will probably have to wait for a w32 binary of 26.1 before I can try your defun. btw, I don't want to operate on an entire region here because there are too many numbers scattered about the texts that should not be superscripted. It's more efficient for me to inspect them in context one by one. I would need a rectangle region or maybe even a gerrymandered one. ;-)


(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 "⁰".

All this could eventually be useful for stripping diacritics in order to search on the base glyphs.


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

I've downloaded this and will try to build it on my Linux laptop but I don't know that OS well enough to get much done in Emacs on it. Are general Linux questions pertinent to getting the GUI and Emacs customized allowable in this ng?

Ed





reply via email to

[Prev in Thread] Current Thread [Next in Thread]