all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* How to automatically escape regex characters for regex search?
@ 2022-11-06  9:10 Jean Louis
  2022-11-06  9:17 ` Yuri Khan
  2022-11-06  9:19 ` tomas
  0 siblings, 2 replies; 6+ messages in thread
From: Jean Louis @ 2022-11-06  9:10 UTC (permalink / raw)
  To: Help GNU Emacs


Is there any Emacs function to automatically escape regex characters
for regex searches?

For example I wish to search for "- [ ]" but I wish to automatically
escape that, not in hard coded way, but in such way that I can use any
string and that regex character like "[ ]" get escaped.

--
Jean

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

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



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How to automatically escape regex characters for regex search?
  2022-11-06  9:10 How to automatically escape regex characters for regex search? Jean Louis
@ 2022-11-06  9:17 ` Yuri Khan
  2022-11-06 17:40   ` Marcin Borkowski
  2022-11-06  9:19 ` tomas
  1 sibling, 1 reply; 6+ messages in thread
From: Yuri Khan @ 2022-11-06  9:17 UTC (permalink / raw)
  To: Jean Louis; +Cc: Help GNU Emacs

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?



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How to automatically escape regex characters for regex search?
  2022-11-06  9:10 How to automatically escape regex characters for regex search? Jean Louis
  2022-11-06  9:17 ` Yuri Khan
@ 2022-11-06  9:19 ` tomas
  2022-11-06  9:24   ` Jean Louis
  1 sibling, 1 reply; 6+ messages in thread
From: tomas @ 2022-11-06  9:19 UTC (permalink / raw)
  To: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 403 bytes --]

On Sun, Nov 06, 2022 at 12:10:47PM +0300, Jean Louis wrote:
> 
> Is there any Emacs function to automatically escape regex characters
> for regex searches?
> 
> For example I wish to search for "- [ ]" but I wish to automatically
> escape that, not in hard coded way, but in such way that I can use any
> string and that regex character like "[ ]" get escaped.

regexp-quote

Cheers
-- 
t

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How to automatically escape regex characters for regex search?
  2022-11-06  9:19 ` tomas
@ 2022-11-06  9:24   ` Jean Louis
  0 siblings, 0 replies; 6+ messages in thread
From: Jean Louis @ 2022-11-06  9:24 UTC (permalink / raw)
  To: tomas; +Cc: help-gnu-emacs

Thank you much.

Though I found in mean time `rx-to-string' and it works well. Maybe I
am using it wrong. Do I?

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


-- 
Jean

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

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



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How to automatically escape regex characters for regex search?
  2022-11-06  9:17 ` Yuri Khan
@ 2022-11-06 17:40   ` Marcin Borkowski
  2022-11-07  5:58     ` Jean Louis
  0 siblings, 1 reply; 6+ messages in thread
From: Marcin Borkowski @ 2022-11-06 17:40 UTC (permalink / raw)
  To: Yuri Khan; +Cc: Jean Louis, Help GNU Emacs


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.

Best,

-- 
Marcin Borkowski
http://mbork.pl



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How to automatically escape regex characters for regex search?
  2022-11-06 17:40   ` Marcin Borkowski
@ 2022-11-07  5:58     ` Jean Louis
  0 siblings, 0 replies; 6+ messages in thread
From: Jean Louis @ 2022-11-07  5:58 UTC (permalink / raw)
  To: Marcin Borkowski; +Cc: Yuri Khan, Help GNU Emacs

* 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/



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2022-11-07  5:58 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-11-06  9:10 How to automatically escape regex characters for regex search? Jean Louis
2022-11-06  9:17 ` Yuri Khan
2022-11-06 17:40   ` Marcin Borkowski
2022-11-07  5:58     ` Jean Louis
2022-11-06  9:19 ` tomas
2022-11-06  9:24   ` Jean Louis

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.