* replace deprecated function ? @ 2018-02-13 18:16 B. T. Raven 2018-02-13 19:27 ` Emanuel Berg ` (2 more replies) 0 siblings, 3 replies; 14+ messages in thread From: B. T. Raven @ 2018-02-13 18:16 UTC (permalink / raw) To: help-gnu-emacs In order to convert arabic numerals to superscripts (for example in plaintext to refer to footnotes)I have been using the following function for a few years: (defun num-to-supnum () ;; M-x sup "Replace digits with superscript digits." (interactive) (let ((start (point))) (save-excursion (query-replace-regexp-eval "[1234567890]" '(cdr (assoc (match-string-no-properties 0) '(("1" . "¹") ("2" . "²") ("3" . "³") ("4" . "⁴") ("5" . "⁵") ("6" . "⁶") ("7" . "⁷") ("8" . "⁸") ("9" . "⁹") ("0" . "⁰")) )) nil start (point-max)) ))) 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. Thanks, Ed ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: replace deprecated function ? 2018-02-13 18:16 replace deprecated function ? B. T. Raven @ 2018-02-13 19:27 ` Emanuel Berg 2018-02-13 20:00 ` Emanuel Berg 2018-02-13 21:14 ` Kaushal Modi [not found] ` <mailman.9061.1518556487.27995.help-gnu-emacs@gnu.org> 2 siblings, 1 reply; 14+ messages in thread From: Emanuel Berg @ 2018-02-13 19:27 UTC (permalink / raw) To: help-gnu-emacs B. T. Raven 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. In general, you do this with a while loop: in the condition, you search, and in the body, you replace. Note: `search-forward-regexp'/`replace-match' is recommended but I don't see (right now) why you can't do it with `re-search-forward' just as well. So an example would be, to replace all REGEXP matches with REPLACE: (defun replace-regexp-buffer (regexp replace) (save-excursion (goto-char (point-min)) (while (re-search-forward regexp nil t) (replace-match replace) ))) For your situation, 1) Do the simplest possibly such loop-and-replace (like my example) 2) Do a function that keeps a list, just as you have, and then iterates the list and feed it to the function from step 1. -- underground experts united http://user.it.uu.se/~embe8573 ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: replace deprecated function ? 2018-02-13 19:27 ` Emanuel Berg @ 2018-02-13 20:00 ` Emanuel Berg 2018-02-13 22:41 ` B. T. Raven 0 siblings, 1 reply; 14+ messages in thread From: Emanuel Berg @ 2018-02-13 20:00 UTC (permalink / raw) To: help-gnu-emacs (require 'cl-lib) (defun replace-regexp-buffer (regexp replace) (save-excursion (goto-char (point-min)) (while (re-search-forward regexp nil t) (replace-match replace) ))) (defun replace-list (list-pairs) (cl-loop for (search . replace) in list-pairs do (replace-regexp-buffer search replace) )) (replace-list '(("james" . "James") ("blish" . "Blish"))) ;; james blish james blish -- underground experts united http://user.it.uu.se/~embe8573 ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: replace deprecated function ? 2018-02-13 20:00 ` Emanuel Berg @ 2018-02-13 22:41 ` B. T. Raven 2018-02-13 23:19 ` Joost Kremers ` (2 more replies) 0 siblings, 3 replies; 14+ messages in thread From: B. T. Raven @ 2018-02-13 22:41 UTC (permalink / raw) To: help-gnu-emacs On 2/13/2018 14:00, Emanuel Berg wrote: > (require 'cl-lib) > > (defun replace-regexp-buffer (regexp replace) > (save-excursion > (goto-char (point-min)) > (while (re-search-forward regexp nil t) > (replace-match replace) ))) > > (defun replace-list (list-pairs) > (cl-loop for (search . replace) in list-pairs do > (replace-regexp-buffer search replace) )) > > (replace-list '(("james" . "James") ("blish" . "Blish"))) > > ;; james blish james blish > Thanks Emanuel but I need it to be more interactive. I want to look at each number that the query finds and press either space bar or n. The following produces argument "Args out of range" for no reason that I can ascertain. query-replace-regex has 5 args rexexp string, string, nil, int and int. Whence the error? Try my original function with query-replace-regexp-eval (the function I want to ditch) to see how it should work. (defun num-to-supnum ( ) ;; M-x sup "Replace digits with superscript digits." (interactive) (let ((start (point))) (save-excursion (query-replace-regexp "[1234567890]" (cdr (assoc (match-string 0) '(("1" . "¹") ("2" . "²") ("3" . "³") ("4" . "⁴") ("5" . "⁵") ("6" . "⁶") ("7" . "⁷") ("8" . "⁸") ("9" . "⁹") ("0" . "⁰")) )) nil start (point-max)) ))) Mutatis mutandis, it seems like I should be able to use query-replace-regexp but I can't get around the various errors it produces by trying different lisp idioms. If I evaluate different parts of the defun step-wise I see everything returned just as I expect. Could the fact that the superscripts are multi-byte cause a problem? Do I need backslashes someewhere? Thanks anyway, Ed ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: replace deprecated function ? 2018-02-13 22:41 ` B. T. Raven @ 2018-02-13 23:19 ` Joost Kremers 2018-02-13 23:26 ` Emanuel Berg [not found] ` <mailman.9067.1518563981.27995.help-gnu-emacs@gnu.org> 2 siblings, 0 replies; 14+ messages in thread From: Joost Kremers @ 2018-02-13 23:19 UTC (permalink / raw) To: B. T. Raven; +Cc: help-gnu-emacs On Tue, Feb 13 2018, B. T. Raven wrote: > Thanks Emanuel but I need it to be more interactive. I want to > look at each > number that the query finds and press either space bar or n. The > following > produces argument "Args out of range" for no reason that I can > ascertain. > query-replace-regex has 5 args rexexp string, string, nil, int > and int. Whence > the error? I suspect it's because you cannot use `match-string' to refer to the match data of the regexp search still being executed. The doc string of `query-replace-regexp-eval' says that in non-interactive use, you should use a loop of `search-regexp-forward' plus `replace-match'. You may be able to wrap the `replace-match' call in y-or-n-p. HTH -- Joost Kremers Life has its moments ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: replace deprecated function ? 2018-02-13 22:41 ` B. T. Raven 2018-02-13 23:19 ` Joost Kremers @ 2018-02-13 23:26 ` Emanuel Berg 2018-02-13 23:33 ` Emanuel Berg ` (2 more replies) [not found] ` <mailman.9067.1518563981.27995.help-gnu-emacs@gnu.org> 2 siblings, 3 replies; 14+ messages in thread From: Emanuel Berg @ 2018-02-13 23:26 UTC (permalink / raw) To: help-gnu-emacs B. T. Raven wrote: > Thanks Emanuel but I need it to be more > interactive. (defun replace-list-ask (list-pairs) (save-excursion (cl-loop for (search . replace) in list-pairs do (goto-char (point-min)) (query-replace-regexp search replace) ))) ;; (replace-list-ask '(("james" . "James") ("blish" . "Blish"))) ;; james blish james blish -- underground experts united http://user.it.uu.se/~embe8573 ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: replace deprecated function ? 2018-02-13 23:26 ` Emanuel Berg @ 2018-02-13 23:33 ` Emanuel Berg 2018-02-14 2:36 ` Yuri Khan [not found] ` <mailman.9079.1518575794.27995.help-gnu-emacs@gnu.org> 2 siblings, 0 replies; 14+ messages in thread From: Emanuel Berg @ 2018-02-13 23:33 UTC (permalink / raw) To: help-gnu-emacs > (defun replace-list-ask (list-pairs) > (save-excursion > (cl-loop for (search . replace) in list-pairs do > (goto-char (point-min)) > (query-replace-regexp search replace) ))) Perhaps better looking: (defun replace-list-ask-2 (list-pairs) (cl-loop for (search . replace) in list-pairs do (query-replace-regexp search replace nil (point-min) (point-max)) )) ;; (replace-list-ask-2 '(("james" . "James") ("blish" . "Blish"))) ;; james blish james blish -- underground experts united http://user.it.uu.se/~embe8573 ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: replace deprecated function ? 2018-02-13 23:26 ` Emanuel Berg 2018-02-13 23:33 ` Emanuel Berg @ 2018-02-14 2:36 ` Yuri Khan [not found] ` <mailman.9079.1518575794.27995.help-gnu-emacs@gnu.org> 2 siblings, 0 replies; 14+ messages in thread From: Yuri Khan @ 2018-02-14 2:36 UTC (permalink / raw) To: Emanuel Berg; +Cc: help-gnu-emacs On Wed, Feb 14, 2018 at 6:26 AM, Emanuel Berg <moasen@zoho.com> wrote: >> Thanks Emanuel but I need it to be more >> interactive. > > (defun replace-list-ask (list-pairs) > (save-excursion > (cl-loop for (search . replace) in list-pairs do > (goto-char (point-min)) > (query-replace-regexp search replace) ))) > > ;; (replace-list-ask '(("james" . "James") ("blish" . "Blish"))) This will ask for all jameses first, and then all blishes. The original snippet probably intended to do them all in document order. ^ permalink raw reply [flat|nested] 14+ messages in thread
[parent not found: <mailman.9079.1518575794.27995.help-gnu-emacs@gnu.org>]
* Re: replace deprecated function ? [not found] ` <mailman.9079.1518575794.27995.help-gnu-emacs@gnu.org> @ 2018-02-14 3:34 ` Emanuel Berg 0 siblings, 0 replies; 14+ messages in thread From: Emanuel Berg @ 2018-02-14 3:34 UTC (permalink / raw) To: help-gnu-emacs Yuri Khan wrote: > This will ask for all jameses first, and then > all blishes. The original snippet probably > intended to do them all in document order. (defun replace-list-ask-3 (re dict) (save-excursion (goto-char (point-min)) (while (re-search-forward re nil t) (let*((match-string (match-string-no-properties 0)) (default (cdr (assoc match-string dict)) )) (when (and default (yes-or-no-p (format "Replace \"%s\" with \"%s\" ? " match-string default))) (replace-match default) ))))) ;; (replace-list-ask-3 "james\\|blish" '(("james" . "James" ) ("blish" . "Blish")) ) -- underground experts united http://user.it.uu.se/~embe8573 ^ permalink raw reply [flat|nested] 14+ messages in thread
[parent not found: <mailman.9067.1518563981.27995.help-gnu-emacs@gnu.org>]
* Re: replace deprecated function ? [not found] ` <mailman.9067.1518563981.27995.help-gnu-emacs@gnu.org> @ 2018-02-14 23:04 ` B. T. Raven 2018-02-16 16:57 ` Emanuel Berg 0 siblings, 1 reply; 14+ messages in thread From: B. T. Raven @ 2018-02-14 23:04 UTC (permalink / raw) To: help-gnu-emacs Thanks, Joost and Emanuel. See below. On 2/13/2018 17:19, Joost Kremers wrote: > > On Tue, Feb 13 2018, B. T. Raven wrote: >> Thanks Emanuel but I need it to be more interactive. I want to look at >> each >> number that the query finds and press either space bar or n. The >> following >> produces argument "Args out of range" for no reason that I can ascertain. >> query-replace-regex has 5 args rexexp string, string, nil, int and >> int. Whence >> the error? > > I suspect it's because you cannot use `match-string' to refer to the > match data of the regexp search still being executed. This seems to be true of query-replace-regexp but not query-replace-regexp-eval. The second argument of the deprecated function is to-expr and it does work in my old defun. (I copy-pasted that snippet from some code a few years ago. The second argument of query-replace-regexp is a string. This one works the same a my defun using query-replace-regexp-eval: (defun num-to-supnum ( ) ;; M-x sup alias "Replace digits with superscript digits." (interactive) (save-excursion (setq pm (point-max)) (while (> pm (point)) (progn (search-forward-regexp "[0-9]") (backward-char) (if (= (read-key) 32) ;; spacebar to preserve query-replace-regexp look and feel (replace-match (cdr (assoc (match-string 0) '(("1" . "¹") ("2" . "²") ("3" . "³") ("4" . "⁴") ("5" . "⁵") ("6" . "⁶") ("7" . "⁷") ("8" . "⁸") ("9" . "⁹") ("0" . "⁰"))))) (forward-char)) ) ))) Ed p.s. This defun betrays my Quick-BASIC cognitive style but it seems to work. What's mainly wrong with it? > > The doc string of `query-replace-regexp-eval' says that in > non-interactive use, you should use a loop of `search-regexp-forward' is this new synonym for search-forward.... ^ > plus `replace-match'. You may be able to wrap the `replace-match' call > in y-or-n-p. > > HTH > ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: replace deprecated function ? 2018-02-14 23:04 ` B. T. Raven @ 2018-02-16 16:57 ` Emanuel Berg 2018-02-16 20:19 ` B. T. Raven 0 siblings, 1 reply; 14+ messages in thread From: Emanuel Berg @ 2018-02-16 16:57 UTC (permalink / raw) To: help-gnu-emacs B. T. Raven wrote: > Thanks, Joost and Emanuel. See below. Here is the best version [1] so far because I got away with the redundant input data and you then use the `query-replace' interface (indeed the very function) which is better than the crude `yes-or-no-p'. (defun replace-list-ask-3 (dict) (let ((re (string-join (mapcar #'car dict) "\\|"))) (save-excursion (goto-char (point-min)) (while (re-search-forward re nil t) (let*((match-string (match-string-no-properties 0)) (default (cdr (assoc match-string dict))) ) (when default (query-replace match-string default nil (match-beginning 0) (match-end 0) ))))))) ;; (replace-list-ask-3 '(("james" . "James") ("blish" . "Blish"))) [1] http://user.it.uu.se/~embe8573/emacs-init/replace-list.el -- underground experts united http://user.it.uu.se/~embe8573 ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: replace deprecated function ? 2018-02-16 16:57 ` Emanuel Berg @ 2018-02-16 20:19 ` B. T. Raven 0 siblings, 0 replies; 14+ messages in thread From: B. T. Raven @ 2018-02-16 20:19 UTC (permalink / raw) To: help-gnu-emacs On 2/16/2018 10:57, Emanuel Berg wrote: > B. T. Raven wrote: > >> Thanks, Joost and Emanuel. See below. > > Here is the best version [1] so far because > I got away with the redundant input data and > you then use the `query-replace' interface > (indeed the very function) which is better than > the crude `yes-or-no-p'. > > (defun replace-list-ask-3 (dict) > (let ((re (string-join (mapcar #'car dict) "\\|"))) > (save-excursion > (goto-char (point-min)) > (while (re-search-forward re nil t) > (let*((match-string (match-string-no-properties 0)) > (default (cdr (assoc match-string dict))) ) > (when default > (query-replace match-string default nil > (match-beginning 0) > (match-end 0) ))))))) > > ;; (replace-list-ask-3 '(("james" . "James") ("blish" . "Blish"))) > > [1] http://user.it.uu.se/~embe8573/emacs-init/replace-list.el > Thanks, Emanuel. In order to conserve muscle memory I used the (read-key) kludge instead of y-or-n-p. My only goal was to get rid of query-replace-regexp-eval so the only thing I used from your code was re-search-forward (C function) instead of search-forward-regexp. It looks like your solution is more general but I don't currently have any application for it other than superscripting numerals (footnotes) here and there. Ed ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: replace deprecated function ? 2018-02-13 18:16 replace deprecated function ? B. T. Raven 2018-02-13 19:27 ` Emanuel Berg @ 2018-02-13 21:14 ` Kaushal Modi [not found] ` <mailman.9061.1518556487.27995.help-gnu-emacs@gnu.org> 2 siblings, 0 replies; 14+ messages in thread From: Kaushal Modi @ 2018-02-13 21:14 UTC (permalink / raw) To: B. T. Raven; +Cc: help-gnu-emacs 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 ^ permalink raw reply [flat|nested] 14+ messages in thread
[parent not found: <mailman.9061.1518556487.27995.help-gnu-emacs@gnu.org>]
* Re: replace deprecated function ? [not found] ` <mailman.9061.1518556487.27995.help-gnu-emacs@gnu.org> @ 2018-02-14 23:31 ` B. T. Raven 0 siblings, 0 replies; 14+ messages in thread From: B. T. Raven @ 2018-02-14 23:31 UTC (permalink / raw) To: help-gnu-emacs 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 > ^ permalink raw reply [flat|nested] 14+ messages in thread
end of thread, other threads:[~2018-02-16 20:19 UTC | newest] Thread overview: 14+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2018-02-13 18:16 replace deprecated function ? B. T. Raven 2018-02-13 19:27 ` Emanuel Berg 2018-02-13 20:00 ` Emanuel Berg 2018-02-13 22:41 ` B. T. Raven 2018-02-13 23:19 ` Joost Kremers 2018-02-13 23:26 ` Emanuel Berg 2018-02-13 23:33 ` Emanuel Berg 2018-02-14 2:36 ` Yuri Khan [not found] ` <mailman.9079.1518575794.27995.help-gnu-emacs@gnu.org> 2018-02-14 3:34 ` Emanuel Berg [not found] ` <mailman.9067.1518563981.27995.help-gnu-emacs@gnu.org> 2018-02-14 23:04 ` B. T. Raven 2018-02-16 16:57 ` Emanuel Berg 2018-02-16 20:19 ` B. T. Raven 2018-02-13 21:14 ` Kaushal Modi [not found] ` <mailman.9061.1518556487.27995.help-gnu-emacs@gnu.org> 2018-02-14 23:31 ` B. T. Raven
Code repositories for project(s) associated with this external index https://git.savannah.gnu.org/cgit/emacs.git https://git.savannah.gnu.org/cgit/emacs/org-mode.git This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.