0

I have this function:

(defun fix-mult-punct-before-footnote ()
  "Scope: FOOTNOTE
Description: Fixes repeated/multiple punctuation before the \\footnote macro."
  (interactive)
  ;; "..\footnote{" --> "..\footnote{"
  ;; ".,\footnote{" --> "?\footnote{"
  (query-replace-regexp
   "\\([,.:;!?]\\)\\([,.:;!?]\\)\\(\\\\footnote{\\)"
   ;; REPLACEMENT
   '((lambda (data count)
       (let ((M1 (match-string-no-properties 1))
             (M2 (match-string-no-properties 2)))
         (cond ((string= M1 M2)
                (format "%s%s" M1
                        (regexp-quote (match-string 3))))
               ((not (string= M1 M2))
                (format "%s%s"
                        (char-to-string
                         (read-char-choice
                          (format "The repeated punctuation contains heterogeneous symbols.
Choose which symbol to keep between \"%s\" and \"%s\""
                                  M1 M2)
                          (list (string-to-char M1)
                                (string-to-char M2))))
                        (regexp-quote (match-string 3))))))))
   nil (point-min) (point-max)))

In the read-char-choice point I'd like to add the option to skip the replacement like the n char does normally. For how the function is built now I need to choice the chars in any case and only than I can skip the replacement.

Is it possible?

I'll try to explain better my question.

For how the function is built now when the function processed the string:

.,\footnote{

the second cond case ask me which char, "," or "." use in the replacement using read-char-choice. But if I DON'T want to proceed with the replacement I can't skip it in this point. I need to choose a char and NEXT skip the replacement. I'd like to simplify the process adding a skipping solution in the read-char-choice point.

I mean somthing like:

(defun fix-mult-punct-before-footnote ()
  "Ambito: FOOTNOTE
Descrizione: Corregge la punteggiatura multipla/ripetuta prima della macro \\footnote"
  (interactive)
  ;; "..\footnote{" --> "..\footnote{"
  ;; ".,\footnote{" --> "?\footnote{"
  (query-replace-regexp
   "\\([,.:;!?]\\)\\([,.:;!?]\\)\\(\\\\footnote{\\)"
   ;; REPLACEMENT
   '((lambda (data count)
       (let ((M1 (match-string-no-properties 1))
         (M2 (match-string-no-properties 2)))
     (cond ((string= M1 M2)
        (format "%s%s" M1
            (regexp-quote (match-string 3))))
           ((not (string= M1 M2))
        (format "%s%s"
            (char-to-string
             (let ((user-choice (read-char-choice
                         (format "The repeated punctuation contains heterogeneous symbols.
Choose which symbol to keep between \"%s\" and \"%s\" OR type \"n\" to skip this replacement: "
                             M1 M2)
                         (list (string-to-char M1)
                           (string-to-char M2)
                           ?n))))
               
               (if (equal user-choice ?n)
                   SOMETHING_HERE
                 user-choice)))
            (regexp-quote (match-string 3))))))))
   nil (point-min) (point-max)))
2
  • It's not clear (to me) what you want to do. It sounds, from your last paragraph, like just using n should do what you want. Maybe rephrase that text to make what you want more clear?
    – Drew
    Nov 10, 2023 at 18:03
  • @Drew I tried to explain better the point.
    – Gabriele
    Nov 10, 2023 at 18:14

1 Answer 1

0

The solution is built-in in query-replace-regexp:

(defun fix-mult-punct-before-footnote ()
  "Scope: FOOTNOTE
Description: Fixes repeated/multiple punctuation before the \\footnote macro."
  (interactive)
  ;; "..\footnote{" --> "..\footnote{"
  ;; ".,\footnote{" --> "?\footnote{"
  (query-replace-regexp
   "\\([,.:;!?]\\)\\([,.:;!?]\\)\\(\\\\footnote{\\)"
   ;; REPLACEMENT
   '((lambda (data count)
       (let ((M1 (match-string-no-properties 1))
             (M2 (match-string-no-properties 2)))
         (cond ((string= M1 M2)
                (format "%s%s" M1
                        (regexp-quote (match-string 3))))
               ((not (string= M1 M2))
        (format "\\?%s" (regexp-quote (match-string 3))))))))
   nil (point-min) (point-max)))

I simply forgot about the \? option:

‘\?’ lets you edit the replacement text in the minibuffer
at the given position for each replacement.

But the possibility of add the skip in the lambda function of my original function is still interesting to me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.