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

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

Re: How to automatically escape regex characters for regex search?


From: Jean Louis
Subject: Re: How to automatically escape regex characters for regex search?
Date: Mon, 7 Nov 2022 08:58:09 +0300
User-agent: Mutt/2.2.7+37 (a90f69b) (2022-09-02)

* Marcin Borkowski <mbork@mbork.pl> [2022-11-06 20:42]:
> 
> On 2022-11-06, at 10:17, Yuri Khan <yuri.v.khan@gmail.com> wrote:
> 
> > On Sun, 6 Nov 2022 at 16:13, Jean Louis <bugs@gnu.support> wrote:
> >
> >> Is there any Emacs function to automatically escape regex characters
> >> for regex searches?
> >
> > regexp-quote?
> 
> While this is not what OP asked for, `regexp-opt' is also worth knowing
> about.

That one is even better as I have to check for multiple strings,
thanks.

(regexp-opt '("- [ ]" "- [x]")) ⇒ "\\(?:- \\[\\(?:[ x]]\\)\\)"

I have made functions to universally check the item in or out,
similarly to Org mode. 

Org mode has nice C-c C-c to mark the list item done.

But what about adoc-mode for Asciidoc?

(when (fboundp 'adoc-mode-map)
  (keymap-set adoc-mode-map "C-c C-c" 'rcd-adoc-check))

(defun rcd-adoc-check ()
  "Toggle DONE and TODO states in `adoc-mode-map' from `[ ]' to `[x]'."
  (interactive)
  (let ((rcd-check-in "- [ ]")
        (rcd-check-out "- [x]"))
    (rcd-check rcd-check-in rcd-check-out)))

If line is empty like this one...

- [ ] If line is empty like this one... by pressing C-c C-c it gets
  its "- [ ]" at front of the list.

- [x] And by pressing again C-c C-c on the line, the item get "DONE".


(defun rcd-check (&optional check-in check-out)
  "Replace matches of CHECK-IN with CHECK-OUT in a line.

It is useful to toggle [ ] to [✔]"
  (interactive)
  (let* ((start (line-beginning-position))
         (end (line-end-position))
         (line (buffer-substring start end))
         (check-in (or check-in "❰    ❱"))
         (check-out (or check-out "❰DONE❱")))
    (rcd-check-start check-in check-out)
    (save-excursion
      (cond ((string-match (regexp-quote check-in) line) 
             (replace-regexp (regexp-quote check-in) check-out nil start end))
            ((string-match (regexp-quote check-out) line)
             (replace-regexp (regexp-quote check-out) check-in nil start 
end))))))

(defun rcd-check-start (&optional check-in check-out)
  "Insert variable `check-in' at the beginning of line."
  (interactive)
  (let* ((start (line-beginning-position))
         (end (line-end-position))
         (line (buffer-substring start end))
         (check-in (or check-in "❰    ❱"))
         (check-out (or check-out "❰DONE❱")))
    (when (not (string-match (regexp-opt (list check-in check-out)) line))
      (save-excursion
        (goto-char start)
        (insert check-in " "))
      (when (= start end)
        (goto-char (line-end-position))))))

And I personally like ❰    ❱ and ❰DONE❱ which I use in any mode.

-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



reply via email to

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