unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Select/highlight and *copy* matches of some regex
@ 2022-06-27 15:52 Joost
  2022-06-27 16:12 ` tomas
  2022-06-27 20:56 ` Stephen Berman
  0 siblings, 2 replies; 20+ messages in thread
From: Joost @ 2022-06-27 15:52 UTC (permalink / raw)
  To: help-gnu-emacs

Hi all,

I've been looking for at least an hour for something that my Sublime-using colleagues seem to be able to do out of the box... Highlight all matches of some regex in the current buffer and then copy/kill *all* of those matches so that you can yank them in a new buffer.

The first part, highlighting all matches, is not a problem. But I haven't found a way yet to then extract all matches.

I've also looked for third-party packages, but haven't been able to find anything. I read a few times that `multiple-cursors` can kill all matches as a rectangle, so that you can use `yank-rectangle`, but I haven't been able to make that work.

So what am I overlooking here? I mean, there *must* be a way, right?

TIA,

Joost


-- 
Joost Kremers
Life has its moments



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

* Re: Select/highlight and *copy* matches of some regex
  2022-06-27 15:52 Select/highlight and *copy* matches of some regex Joost
@ 2022-06-27 16:12 ` tomas
  2022-06-27 17:16   ` Joost
  2022-06-27 20:56 ` Stephen Berman
  1 sibling, 1 reply; 20+ messages in thread
From: tomas @ 2022-06-27 16:12 UTC (permalink / raw)
  To: help-gnu-emacs

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

On Mon, Jun 27, 2022 at 05:52:48PM +0200, Joost wrote:
> Hi all,
> 
> I've been looking for at least an hour for something that my Sublime-using colleagues seem to be able to do out of the box... Highlight all matches of some regex in the current buffer and then copy/kill *all* of those matches so that you can yank them in a new buffer.
> 
> The first part, highlighting all matches, is not a problem. But I haven't found a way yet to then extract all matches.
> 
> I've also looked for third-party packages, but haven't been able to find anything. I read a few times that `multiple-cursors` can kill all matches as a rectangle, so that you can use `yank-rectangle`, but I haven't been able to make that work.
> 
> So what am I overlooking here? I mean, there *must* be a way, right?

Not a direct answer to your question, but perhaps similar. If you
can live with selecting /lines/ containing a match, there's "occur"
(M-x occur).

Each match is a "live link" to the location. You can even edit the
matches in the occur buffer itself (key 'e').

I didnt try what happens when you do a query-replace in the occur
buffer while it is in "edit" mode. But worth a try ;-)

Cheers
-- 
t

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

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

* Re: Select/highlight and *copy* matches of some regex
  2022-06-27 16:12 ` tomas
@ 2022-06-27 17:16   ` Joost
  2022-06-27 17:44     ` tomas
                       ` (2 more replies)
  0 siblings, 3 replies; 20+ messages in thread
From: Joost @ 2022-06-27 17:16 UTC (permalink / raw)
  To: fatiparty---

On Mon, 27 Jun 2022, at 18:12, tomas@tuxteam.de wrote:
> Not a direct answer to your question, but perhaps similar. If you
> can live with selecting /lines/ containing a match, there's "occur"
> (M-x occur).

Sorry, I meant to mention that, but I forgot. No, selecting lines is not an option, because it's the non-matching parts of each line that I'm trying to get rid of.

In my particular use case, I'm trying to clean up a file that looks something like this:

```
blah blah lang=en bla
lang=de
lang=fr blah blah
blah lang=pt
```

The part I'm interested in is "lang=xx", the rest needs to be removed. Each line contains this part (with different language codes), but may contain more text, which is irrelevant to me. There's no pattern to the irrelevant part, it can basically be any text.

I can highlight them with `highlight-regexp`, search for them with isearch, but there doesn't seem to be a straightforward way to copy or kill them...


-- 
Joost Kremers
Life has its moments



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

* Re: Select/highlight and *copy* matches of some regex
  2022-06-27 17:16   ` Joost
@ 2022-06-27 17:44     ` tomas
  2022-06-28  4:40     ` Visuwesh
  2022-06-28  9:29     ` Daniel Martín
  2 siblings, 0 replies; 20+ messages in thread
From: tomas @ 2022-06-27 17:44 UTC (permalink / raw)
  To: help-gnu-emacs

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

On Mon, Jun 27, 2022 at 07:16:27PM +0200, Joost wrote:
> On Mon, 27 Jun 2022, at 18:12, tomas@tuxteam.de wrote:
> > Not a direct answer to your question, but perhaps similar. If you
> > can live with selecting /lines/ containing a match, there's "occur"
> > (M-x occur).
> 
> Sorry, I meant to mention that, but I forgot. No, selecting lines is not an option, because it's the non-matching parts of each line that I'm trying to get rid of.
> 
> In my particular use case, I'm trying to clean up a file that looks something like this:

[...]

I see. No, I don't know such a thing out of the box (that doesn't
mean much, though).

It shouldn't be that difficult to cobble something together.

Cheers
-- 
t

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

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

* Re: Select/highlight and *copy* matches of some regex
  2022-06-27 15:52 Select/highlight and *copy* matches of some regex Joost
  2022-06-27 16:12 ` tomas
@ 2022-06-27 20:56 ` Stephen Berman
  2022-06-27 21:09   ` Joost Kremers
  2022-06-28  4:42   ` tomas
  1 sibling, 2 replies; 20+ messages in thread
From: Stephen Berman @ 2022-06-27 20:56 UTC (permalink / raw)
  To: Joost; +Cc: help-gnu-emacs

On Mon, 27 Jun 2022 17:52:48 +0200 Joost <joostkremers@fastmail.fm> wrote:

> Hi all,
>
> I've been looking for at least an hour for something that my Sublime-using
> colleagues seem to be able to do out of the box... Highlight all matches of
> some regex in the current buffer and then copy/kill *all* of those matches so
> that you can yank them in a new buffer.
>
> The first part, highlighting all matches, is not a problem. But I haven't
> found a way yet to then extract all matches.

Is this (suitably tweaked) good enough?

(let ((matches ""))
  (while (re-search-forward "lang=.." nil t)
    (setq matches (concat matches (match-string 0) "\n")))
  (kill-new matches))

Steve Berman



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

* Re: Select/highlight and *copy* matches of some regex
  2022-06-27 20:56 ` Stephen Berman
@ 2022-06-27 21:09   ` Joost Kremers
  2022-06-27 22:26     ` Emanuel Berg
  2022-06-27 22:29     ` Emanuel Berg
  2022-06-28  4:42   ` tomas
  1 sibling, 2 replies; 20+ messages in thread
From: Joost Kremers @ 2022-06-27 21:09 UTC (permalink / raw)
  To: Stephen Berman; +Cc: help-gnu-emacs


On Mon, Jun 27 2022, Stephen Berman wrote:
> On Mon, 27 Jun 2022 17:52:48 +0200 Joost <joostkremers@fastmail.fm> wrote:
>> The first part, highlighting all matches, is not a problem. But I haven't
>> found a way yet to then extract all matches.
>
> Is this (suitably tweaked) good enough?
>
> (let ((matches ""))
>   (while (re-search-forward "lang=.." nil t)
>     (setq matches (concat matches (match-string 0) "\n")))
>   (kill-new matches))

Yeah, it's easy to code something up that collects all matches of some regex is
a buffer. I'm just a little surprised that this isn't already possible, from
isearch or hi-lock-mode or something...

-- 
Joost Kremers
Life has its moments



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

* Re: Select/highlight and *copy* matches of some regex
  2022-06-27 21:09   ` Joost Kremers
@ 2022-06-27 22:26     ` Emanuel Berg
  2022-06-27 22:29     ` Emanuel Berg
  1 sibling, 0 replies; 20+ messages in thread
From: Emanuel Berg @ 2022-06-27 22:26 UTC (permalink / raw)
  To: help-gnu-emacs

Joost Kremers wrote:

>>> The first part, highlighting all matches, is not
>>> a problem. But I haven't found a way yet to then extract
>>> all matches.
>>
>> Is this (suitably tweaked) good enough?
>>
>> (let ((matches ""))
>>   (while (re-search-forward "lang=.." nil t)
>>     (setq matches (concat matches (match-string 0) "\n")))
>>   (kill-new matches))
>
> Yeah, it's easy to code something up that collects all
> matches of some regex is a buffer. I'm just a little
> surprised that this isn't already possible, from isearch or
> hi-lock-mode or something...

You are a little surprised? It is like that all the time with
Elisp.

-- 
underground experts united
https://dataswamp.org/~incal




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

* Re: Select/highlight and *copy* matches of some regex
  2022-06-27 21:09   ` Joost Kremers
  2022-06-27 22:26     ` Emanuel Berg
@ 2022-06-27 22:29     ` Emanuel Berg
  2022-06-27 22:41       ` Emanuel Berg
  2022-06-27 22:44       ` Select/highlight and *copy* matches of some regex Emanuel Berg
  1 sibling, 2 replies; 20+ messages in thread
From: Emanuel Berg @ 2022-06-27 22:29 UTC (permalink / raw)
  To: help-gnu-emacs

>>> The first part, highlighting all matches, is not
>>> a problem. But I haven't found a way yet to then extract
>>> all matches.
>>
>> Is this (suitably tweaked) good enough?
>>
>> (let ((matches ""))
>>   (while (re-search-forward "lang=.." nil t)
>>     (setq matches (concat matches (match-string 0) "\n")))
>>   (kill-new matches))

I have something like that ...

;;; -*- lexical-binding: t -*-
;;
;; this file:
;;   https://dataswamp.org/~incal/emacs-init/re-make-list.el

(defun re-make-list-forward (re &optional beg end)
  (interactive
   (when (use-region-p)
     (list
      (read-regexp "re: ")
      (region-beginning)
      (region-end) )))
  (let ((b (or beg (point)))
        (e (or end (point-max))) )
    (save-excursion
      (let ((matches '()))
        (goto-char b)
        (while (re-search-forward re e t)
          (push (match-string-no-properties 0) matches) )
        matches) )))
(defalias 're-make-list #'re-make-list-forward)

(defun re-make-list-backward (re &optional beg end)
  (interactive
   (when (use-region-p)
     (list
      (read-regexp "re: ")
      (region-beginning)
      (region-end) )))
  (let ((b (or beg (point)))
        (e (or end (point-max))) )
    (save-excursion
      (let ((matches '()))
        (goto-char e)
        (while (re-search-backward re b t)
          (push (match-string-no-properties 0) matches) )
        matches) )))

;; Below are 139 colors, so the list should have 139 items ...
;; Colors: https://www.rapidtables.com/web/color/RGB_Color.html
;;
;; (length (re-make-list-forward  "#[0-9a-f]\\{6\\}")) ; 139
;; (length (re-make-list-backward "#[0-9a-f]\\{6\\}")) ; 139
;;
;; maroon                  #800000
;; dark red                #8b0000
;; brown                   #a52a2a
;; firebrick               #b22222
;; crimson                 #dc143c
;; red                     #ff0000
;; tomato                  #ff6347
;; coral                   #ff7f50
;; indian red              #cd5c5c
;; light coral             #f08080
;; dark salmon             #e9967a
;; salmon                  #fa8072
;; light salmon            #ffa07a
;; orange red              #ff4500
;; dark orange             #ff8c00
;; orange                  #ffa500
;; gold                    #ffd700
;; dark golden rod         #b8860b
;; golden rod              #daa520
;; pale golden rod         #eee8aa
;; dark khaki              #bdb76b
;; khaki                   #f0e68c
;; olive                   #808000
;; yellow                  #ffff00
;; yellow green            #9acd32
;; dark olive green        #556b2f
;; olive drab              #6b8e23
;; lawn green              #7cfc00
;; chart reuse             #7fff00
;; green yellow            #adff2f
;; dark green              #006400
;; green                   #008000
;; forest green            #228b22
;; lime                    #00ff00
;; lime green              #32cd32
;; light green             #90ee90
;; pale green              #98fb98
;; dark sea green          #8fbc8f
;; medium spring green     #00fa9a
;; spring green            #00ff7f
;; sea green               #2e8b57
;; medium aqua marine      #66cdaa
;; medium sea green        #3cb371
;; light sea green         #20b2aa
;; dark slate gray         #2f4f4f
;; teal                    #008080
;; dark cyan               #008b8b
;; aqua                    #00ffff
;; cyan                    #00ffff
;; light cyan              #e0ffff
;; dark turquoise          #00ced1
;; turquoise               #40e0d0
;; medium turquoise        #48d1cc
;; pale turquoise          #afeeee
;; aqua marine             #7fffd4
;; powder blue             #b0e0e6
;; cadet blue              #5f9ea0
;; steel blue              #4682b4
;; corn flower blue        #6495ed
;; deep sky blue           #00bfff
;; dodger blue             #1e90ff
;; light blue              #add8e6
;; sky blue                #87ceeb
;; light sky blue          #87cefa
;; midnight blue           #191970
;; navy                    #000080
;; dark blue               #00008b
;; medium blue             #0000cd
;; blue                    #0000ff
;; royal blue              #4169e1
;; blue violet             #8a2be2
;; indigo                  #4b0082
;; dark slate blue         #483d8b
;; slate blue              #6a5acd
;; medium slate blue       #7b68ee
;; medium purple           #9370db
;; dark magenta            #8b008b
;; dark violet             #9400d3
;; dark orchid             #9932cc
;; medium orchid           #ba55d3
;; purple                  #800080
;; thistle                 #d8bfd8
;; plum                    #dda0dd
;; violet                  #ee82ee
;; magenta / fuchsia       #ff00ff
;; orchid                  #da70d6
;; medium violet red       #c71585
;; pale violet red         #db7093
;; deep pink               #ff1493
;; hot pink                #ff69b4
;; light pink              #ffb6c1
;; pink                    #ffc0cb
;; antique white           #faebd7
;; beige                   #f5f5dc
;; bisque                  #ffe4c4
;; blanched almond         #ffebcd
;; wheat                   #f5deb3
;; corn silk               #fff8dc
;; lemon chiffon           #fffacd
;; light golden rod yellow #fafad2
;; light yellow            #ffffe0
;; saddle brown            #8b4513
;; sienna                  #a0522d
;; chocolate               #d2691e
;; peru                    #cd853f
;; sandy brown             #f4a460
;; burly wood              #deb887
;; tan                     #d2b48c
;; rosy brown              #bc8f8f
;; moccasin                #ffe4b5
;; navajo white            #ffdead
;; peach puff              #ffdab9
;; misty rose              #ffe4e1
;; lavender blush          #fff0f5
;; linen                   #faf0e6
;; old lace                #fdf5e6
;; papaya whip             #ffefd5
;; sea shell               #fff5ee
;; mint cream              #f5fffa
;; slate gray              #708090
;; light slate gray        #778899
;; light steel blue        #b0c4de
;; lavender                #e6e6fa
;; floral white            #fffaf0
;; alice blue              #f0f8ff
;; ghost white             #f8f8ff
;; honeydew                #f0fff0
;; ivory                   #fffff0
;; azure                   #f0ffff
;; snow                    #fffafa
;; black                   #000000
;; dim gray / dim grey     #696969
;; gray / grey             #808080
;; dark gray / dark grey   #a9a9a9
;; silver                  #c0c0c0
;; light gray / light grey #d3d3d3
;; gainsboro               #dcdcdc
;; white smoke             #f5f5f5
;; white                   #ffffff

-- 
underground experts united
https://dataswamp.org/~incal




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

* Re: Select/highlight and *copy* matches of some regex
  2022-06-27 22:29     ` Emanuel Berg
@ 2022-06-27 22:41       ` Emanuel Berg
  2022-06-27 23:11         ` DWIM interface (was: Re: Select/highlight and *copy* matches of some regex) Emanuel Berg
  2022-06-27 22:44       ` Select/highlight and *copy* matches of some regex Emanuel Berg
  1 sibling, 1 reply; 20+ messages in thread
From: Emanuel Berg @ 2022-06-27 22:41 UTC (permalink / raw)
  To: help-gnu-emacs

>>> The first part, highlighting all matches, is not
>>> a problem. But I haven't found a way yet to then extract
>>> all matches.
>>
>> Is this (suitably tweaked) good enough?
>>
>> (let ((matches ""))
>>   (while (re-search-forward "lang=.." nil t)
>>     (setq matches (concat matches (match-string 0) "\n")))
>>   (kill-new matches))
>
> I have something like that [...]
>
> (defun re-make-list-forward (re &optional beg end)
>   (interactive
>    (when (use-region-p)
>      (list
>       (read-regexp "re: ")
>       (region-beginning)
>       (region-end) )))

Oups, that's no good if called interactively without a region.
I changed that ...

But I don't know what's the right thing to do when called from
Lisp with 'beg' and 'end' implicitly nil _and_ there is
a region?

Either use the region or consider that an interactive feature
only and set 'beg' and 'end' to the default `point-min' and
`point-max'?

;;; -*- lexical-binding: t -*-
;;
;; this file:
;;   https://dataswamp.org/~incal/emacs-init/re-make-list.el

(defun re-make-list-forward (re &optional beg end)
  (interactive
   (list
    (read-regexp "re: ")
    (when (use-region-p) (region-beginning))
    (when (use-region-p) (region-end)) )
  (or beg (setq beg (point)))
  (or end (setq end (point-max)))
  (save-excursion
    (let ((matches))
      (goto-char beg)
      (while (re-search-forward re end t)
        (push (match-string-no-properties 0) matches) )
      matches) ))
(defalias 're-make-list #'re-make-list-forward)

(defun re-make-list-backward (re &optional beg end)
  (interactive
   (list
    (read-regexp "re: ")
    (when (use-region-p) (region-beginning))
    (when (use-region-p) (region-end)) )
  (or beg (setq beg (point)))
  (or end (setq end (point-max)))
  (save-excursion
    (let ((matches))
      (goto-char end)
      (while (re-search-backward re beg t)
        (push (match-string-no-properties 0) matches) )
      matches) ))

;; Below are 139 colors, so the list should have 139 items
;; <https://www.rapidtables.com/web/color/RGB_Color.html>
;;
;; (length (re-make-list-forward  "#[0-9a-f]\\{6\\}")) ; 139
;; (length (re-make-list-backward "#[0-9a-f]\\{6\\}")) ; 139
;;
;; maroon                  #800000
;; dark red                #8b0000
;; brown                   #a52a2a
;; firebrick               #b22222
;; crimson                 #dc143c
;; red                     #ff0000
;; tomato                  #ff6347
;; coral                   #ff7f50
;; indian red              #cd5c5c
;; light coral             #f08080
;; dark salmon             #e9967a
;; salmon                  #fa8072
;; light salmon            #ffa07a
;; orange red              #ff4500
;; dark orange             #ff8c00
;; orange                  #ffa500
;; gold                    #ffd700
;; dark golden rod         #b8860b
;; golden rod              #daa520
;; pale golden rod         #eee8aa
;; dark khaki              #bdb76b
;; khaki                   #f0e68c
;; olive                   #808000
;; yellow                  #ffff00
;; yellow green            #9acd32
;; dark olive green        #556b2f
;; olive drab              #6b8e23
;; lawn green              #7cfc00
;; chart reuse             #7fff00
;; green yellow            #adff2f
;; dark green              #006400
;; green                   #008000
;; forest green            #228b22
;; lime                    #00ff00
;; lime green              #32cd32
;; light green             #90ee90
;; pale green              #98fb98
;; dark sea green          #8fbc8f
;; medium spring green     #00fa9a
;; spring green            #00ff7f
;; sea green               #2e8b57
;; medium aqua marine      #66cdaa
;; medium sea green        #3cb371
;; light sea green         #20b2aa
;; dark slate gray         #2f4f4f
;; teal                    #008080
;; dark cyan               #008b8b
;; aqua                    #00ffff
;; cyan                    #00ffff
;; light cyan              #e0ffff
;; dark turquoise          #00ced1
;; turquoise               #40e0d0
;; medium turquoise        #48d1cc
;; pale turquoise          #afeeee
;; aqua marine             #7fffd4
;; powder blue             #b0e0e6
;; cadet blue              #5f9ea0
;; steel blue              #4682b4
;; corn flower blue        #6495ed
;; deep sky blue           #00bfff
;; dodger blue             #1e90ff
;; light blue              #add8e6
;; sky blue                #87ceeb
;; light sky blue          #87cefa
;; midnight blue           #191970
;; navy                    #000080
;; dark blue               #00008b
;; medium blue             #0000cd
;; blue                    #0000ff
;; royal blue              #4169e1
;; blue violet             #8a2be2
;; indigo                  #4b0082
;; dark slate blue         #483d8b
;; slate blue              #6a5acd
;; medium slate blue       #7b68ee
;; medium purple           #9370db
;; dark magenta            #8b008b
;; dark violet             #9400d3
;; dark orchid             #9932cc
;; medium orchid           #ba55d3
;; purple                  #800080
;; thistle                 #d8bfd8
;; plum                    #dda0dd
;; violet                  #ee82ee
;; magenta / fuchsia       #ff00ff
;; orchid                  #da70d6
;; medium violet red       #c71585
;; pale violet red         #db7093
;; deep pink               #ff1493
;; hot pink                #ff69b4
;; light pink              #ffb6c1
;; pink                    #ffc0cb
;; antique white           #faebd7
;; beige                   #f5f5dc
;; bisque                  #ffe4c4
;; blanched almond         #ffebcd
;; wheat                   #f5deb3
;; corn silk               #fff8dc
;; lemon chiffon           #fffacd
;; light golden rod yellow #fafad2
;; light yellow            #ffffe0
;; saddle brown            #8b4513
;; sienna                  #a0522d
;; chocolate               #d2691e
;; peru                    #cd853f
;; sandy brown             #f4a460
;; burly wood              #deb887
;; tan                     #d2b48c
;; rosy brown              #bc8f8f
;; moccasin                #ffe4b5
;; navajo white            #ffdead
;; peach puff              #ffdab9
;; misty rose              #ffe4e1
;; lavender blush          #fff0f5
;; linen                   #faf0e6
;; old lace                #fdf5e6
;; papaya whip             #ffefd5
;; sea shell               #fff5ee
;; mint cream              #f5fffa
;; slate gray              #708090
;; light slate gray        #778899
;; light steel blue        #b0c4de
;; lavender                #e6e6fa
;; floral white            #fffaf0
;; alice blue              #f0f8ff
;; ghost white             #f8f8ff
;; honeydew                #f0fff0
;; ivory                   #fffff0
;; azure                   #f0ffff
;; snow                    #fffafa
;; black                   #000000
;; dim gray / dim grey     #696969
;; gray / grey             #808080
;; dark gray / dark grey   #a9a9a9
;; silver                  #c0c0c0
;; light gray / light grey #d3d3d3
;; gainsboro               #dcdcdc
;; white smoke             #f5f5f5
;; white                   #ffffff

-- 
underground experts united
https://dataswamp.org/~incal




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

* Re: Select/highlight and *copy* matches of some regex
  2022-06-27 22:29     ` Emanuel Berg
  2022-06-27 22:41       ` Emanuel Berg
@ 2022-06-27 22:44       ` Emanuel Berg
  2022-06-28  4:43         ` tomas
  1 sibling, 1 reply; 20+ messages in thread
From: Emanuel Berg @ 2022-06-27 22:44 UTC (permalink / raw)
  To: help-gnu-emacs

Now:

(defun re-make-list-forward (re &optional beg end)
  (interactive
   (list
    (read-regexp "re: ")
    (when (use-region-p) (region-beginning))
    (when (use-region-p) (region-end)) ))
  (or beg (setq beg (point)))
  (or end (setq end (point-max)))
  (save-excursion
    (let ((matches))
      (goto-char beg)
      (while (re-search-forward re end t)
        (push (match-string-no-properties 0) matches) )
      matches) ))
(defalias 're-make-list #'re-make-list-forward)

(defun re-make-list-backward (re &optional beg end)
  (interactive
   (list
    (read-regexp "re: ")
    (when (use-region-p) (region-beginning))
    (when (use-region-p) (region-end)) ))
  (or beg (setq beg (point)))
  (or end (setq end (point-max)))
  (save-excursion
    (let ((matches))
      (goto-char end)
      (while (re-search-backward re beg t)
        (push (match-string-no-properties 0) matches) )
      matches) ))

-- 
underground experts united
https://dataswamp.org/~incal




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

* DWIM interface (was: Re: Select/highlight and *copy* matches of some regex)
  2022-06-27 22:41       ` Emanuel Berg
@ 2022-06-27 23:11         ` Emanuel Berg
  0 siblings, 0 replies; 20+ messages in thread
From: Emanuel Berg @ 2022-06-27 23:11 UTC (permalink / raw)
  To: help-gnu-emacs

> Either use the region or consider that an interactive
> feature only and set 'beg' and 'end' to the default
> `point-min' and `point-max'?

Or `point' (not `point-min'), that's another thing to think
about ... but that maybe depends on the particular function,
or more so at least, what to do from Lisp with no supplied
boundaries _but_ a region, if you have a good answer to that
I think it would hold for all such functions ...

Here, compare ... ?

;;; -*- lexical-binding: t -*-
;;
;; this file:
;;   https://dataswamp.org/~incal/emacs-init/dwim.el

(defun test-dwim (&optional beg end)
  (interactive (when (use-region-p)
                 (list (region-beginning) (region-end)) ))
  (or beg (setq beg (point-min))) ; or (point)
  (or end (setq end (point-max)))
  ;; insert code here
  ;; now let's just make a list to do something
  (list beg end) )

(defun test-dwim-2 (&optional beg end)
  (interactive (when (use-region-p)
                 (list (region-beginning) (region-end)) ))
  (or beg (setq beg (if (use-region-p) (region-beginning) (point-min))))
  (or end (setq end (if (use-region-p) (region-end)       (point-max))))
  (list beg end) )

-- 
underground experts united
https://dataswamp.org/~incal




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

* Re: Select/highlight and *copy* matches of some regex
  2022-06-27 17:16   ` Joost
  2022-06-27 17:44     ` tomas
@ 2022-06-28  4:40     ` Visuwesh
  2022-06-28 19:52       ` Emanuel Berg
  2022-06-29 21:40       ` Joost Kremers
  2022-06-28  9:29     ` Daniel Martín
  2 siblings, 2 replies; 20+ messages in thread
From: Visuwesh @ 2022-06-28  4:40 UTC (permalink / raw)
  To: Joost; +Cc: fatiparty---

[திங்கள் ஜூன் 27, 2022] Joost wrote:

> On Mon, 27 Jun 2022, at 18:12, tomas@tuxteam.de wrote:
>> Not a direct answer to your question, but perhaps similar. If you
>> can live with selecting /lines/ containing a match, there's "occur"
>> (M-x occur).
>
> Sorry, I meant to mention that, but I forgot. No, selecting lines is not an option, because it's the non-matching parts of each line that I'm trying to get rid of.
>
> In my particular use case, I'm trying to clean up a file that looks something like this:
>
> ```
> blah blah lang=en bla
> lang=de
> lang=fr blah blah
> blah lang=pt
> ```
>
> The part I'm interested in is "lang=xx", the rest needs to be
> removed. Each line contains this part (with different language codes),
> but may contain more text, which is irrelevant to me. There's no
> pattern to the irrelevant part, it can basically be any text.
>
> I can highlight them with `highlight-regexp`, search for them with isearch, but there doesn't seem to be a straightforward way to copy or kill them...

I wonder if you can use C-u M-s o for the job.  I did 
C-u M-s o lang=.. then it produced a buffer with the contents

    lang=en
    lang=de
    lang=fr
    lang=pt



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

* Re: Select/highlight and *copy* matches of some regex
  2022-06-27 20:56 ` Stephen Berman
  2022-06-27 21:09   ` Joost Kremers
@ 2022-06-28  4:42   ` tomas
  1 sibling, 0 replies; 20+ messages in thread
From: tomas @ 2022-06-28  4:42 UTC (permalink / raw)
  To: help-gnu-emacs

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

On Mon, Jun 27, 2022 at 10:56:46PM +0200, Stephen Berman wrote:
> On Mon, 27 Jun 2022 17:52:48 +0200 Joost <joostkremers@fastmail.fm> wrote:
> 
> > Hi all,
> >
> > I've been looking for at least an hour for something that my Sublime-using
> > colleagues seem to be able to do out of the box... Highlight all matches of
> > some regex in the current buffer and then copy/kill *all* of those matches so
> > that you can yank them in a new buffer.
> >
> > The first part, highlighting all matches, is not a problem. But I haven't
> > found a way yet to then extract all matches.
> 
> Is this (suitably tweaked) good enough?
> 
> (let ((matches ""))
>   (while (re-search-forward "lang=.." nil t)
>     (setq matches (concat matches (match-string 0) "\n")))
>   (kill-new matches))

Nifty :)

You'll end up with one big string of all concatenated matches.
But it's a start.

Cheers
-- 
t

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

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

* Re: Select/highlight and *copy* matches of some regex
  2022-06-27 22:44       ` Select/highlight and *copy* matches of some regex Emanuel Berg
@ 2022-06-28  4:43         ` tomas
  2022-06-28  5:37           ` Emanuel Berg
  0 siblings, 1 reply; 20+ messages in thread
From: tomas @ 2022-06-28  4:43 UTC (permalink / raw)
  To: help-gnu-emacs

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

On Tue, Jun 28, 2022 at 12:44:22AM +0200, Emanuel Berg wrote:
> Now:
> 
> (defun re-make-list-forward (re &optional beg end)

[...]

You're always honing your little programs until they shine :)

Cheers
-- 
t

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

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

* Re: Select/highlight and *copy* matches of some regex
  2022-06-28  4:43         ` tomas
@ 2022-06-28  5:37           ` Emanuel Berg
  0 siblings, 0 replies; 20+ messages in thread
From: Emanuel Berg @ 2022-06-28  5:37 UTC (permalink / raw)
  To: help-gnu-emacs

tomas wrote:

>> Now:
>> 
>> (defun re-make-list-forward (re &optional beg end)
>
> [...]
>
> You're always honing your little programs until they shine :)

He without an army sharpens his knife :)

-- 
underground experts united
https://dataswamp.org/~incal




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

* Re: Select/highlight and *copy* matches of some regex
  2022-06-27 17:16   ` Joost
  2022-06-27 17:44     ` tomas
  2022-06-28  4:40     ` Visuwesh
@ 2022-06-28  9:29     ` Daniel Martín
  2022-06-28 10:33       ` tomas
  2 siblings, 1 reply; 20+ messages in thread
From: Daniel Martín @ 2022-06-28  9:29 UTC (permalink / raw)
  To: Joost; +Cc: fatiparty---

Joost <joostkremers@fastmail.fm> writes:

> On Mon, 27 Jun 2022, at 18:12, tomas@tuxteam.de wrote:
>> Not a direct answer to your question, but perhaps similar. If you
>> can live with selecting /lines/ containing a match, there's "occur"
>> (M-x occur).
>
> Sorry, I meant to mention that, but I forgot. No, selecting lines is not an option, because it's the non-matching parts of each line that I'm trying to get rid of.
>
> In my particular use case, I'm trying to clean up a file that looks something like this:
>
> ```
> blah blah lang=en bla
> lang=de
> lang=fr blah blah
> blah lang=pt
> ```
>
> The part I'm interested in is "lang=xx", the rest needs to be
> removed. Each line contains this part (with different language codes),
> but may contain more text, which is irrelevant to me. There's no
> pattern to the irrelevant part, it can basically be any text.
>
> I can highlight them with `highlight-regexp`, search for them with isearch, but there doesn't seem to be a straightforward way to copy or kill them...

You can pass the universal argument to occur to only show the matches
for the regexp, excluding the rest of the line.  For example:

C-u C-s REGEXP C-u M-s o

This will show an *Occur* buffer with only the matching parts of REGEXP
in the current buffer, which you can save to a file, etc.



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

* Re: Select/highlight and *copy* matches of some regex
  2022-06-28  9:29     ` Daniel Martín
@ 2022-06-28 10:33       ` tomas
  0 siblings, 0 replies; 20+ messages in thread
From: tomas @ 2022-06-28 10:33 UTC (permalink / raw)
  To: help-gnu-emacs

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

On Tue, Jun 28, 2022 at 11:29:51AM +0200, Daniel Martín wrote:

[...]

> You can pass the universal argument to occur to only show the matches
> for the regexp, excluding the rest of the line.  For example:
> 
> C-u C-s REGEXP C-u M-s o
> 
> This will show an *Occur* buffer with only the matching parts of REGEXP
> in the current buffer, which you can save to a file, etc.

Woah. I didn't know that one, thanks. This seems to come close
to the OP's wishes, yes.

Cheers
-- 
t

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

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

* Re: Select/highlight and *copy* matches of some regex
  2022-06-28  4:40     ` Visuwesh
@ 2022-06-28 19:52       ` Emanuel Berg
  2022-06-29 21:40       ` Joost Kremers
  1 sibling, 0 replies; 20+ messages in thread
From: Emanuel Berg @ 2022-06-28 19:52 UTC (permalink / raw)
  To: help-gnu-emacs

Visuwesh wrote:

> I wonder if you can use C-u M-s o for the job. I did C-u M-s
> o lang=.. then it produced a buffer with the contents
>
>     lang=en
>     lang=de
>     lang=fr
>     lang=pt

(require 'cl-lib)

(cl-loop
 while (re-search-forward "lang=\\([[:alnum:]]*\\)" (point-max) t)
 with hits = ()
 do (push (match-string-no-properties 1) hits)
 finally return
   (sort (cl-delete-duplicates hits :test #'string=) #'string<) )

;; blah blah lang=en bla
;; lang=de
;; lang=fr blah blah
;; blah lang=pt

;; blah blah lang=en bla
;; lang=de
;; lang=fr blah blah
;; blah lang=pt

;; result: ("de" "en" "fr" "pt")

-- 
underground experts united
https://dataswamp.org/~incal




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

* Re: Select/highlight and *copy* matches of some regex
  2022-06-28  4:40     ` Visuwesh
  2022-06-28 19:52       ` Emanuel Berg
@ 2022-06-29 21:40       ` Joost Kremers
  2022-06-30  6:05         ` Emanuel Berg
  1 sibling, 1 reply; 20+ messages in thread
From: Joost Kremers @ 2022-06-29 21:40 UTC (permalink / raw)
  To: Visuwesh; +Cc: help-gnu-emacs


On Tue, Jun 28 2022, Visuwesh wrote:
> I wonder if you can use C-u M-s o for the job.  I did 
> C-u M-s o lang=.. then it produced a buffer with the contents
>
>     lang=en
>     lang=de
>     lang=fr
>     lang=pt

Yes, that works, even from within isearch. Thanks!

-- 
Joost Kremers
Life has its moments



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

* Re: Select/highlight and *copy* matches of some regex
  2022-06-29 21:40       ` Joost Kremers
@ 2022-06-30  6:05         ` Emanuel Berg
  0 siblings, 0 replies; 20+ messages in thread
From: Emanuel Berg @ 2022-06-30  6:05 UTC (permalink / raw)
  To: help-gnu-emacs

Joost Kremers wrote:

>> I wonder if you can use C-u M-s o for the job. I did C-u
>> M-s o lang=.. then it produced a buffer with the contents
>>
>>   lang=en
>>   lang=de
>>   lang=fr
>>   lang=pt
>
> Yes, that works, even from within isearch. Thanks!

Hold your horses ...

(occur "lang=[-[:alnum:]]+")

  lang=mas     Masai                                  language       
  lang=fr-CA   French as used in Canada               language+region
  lang=es-419  Spanish as used in Latin America       language+region
  lang=zh-Hans Chinese written with Simplified script language+script [1]

Notable software from the French speaking Canadians include,
according to the COMP-HIST file:

  BASICOIS - French BASIC dialect/translation, probably from Quebec [2]

(It's only "Québec" in French.)

Anyone knows when, even approximately so, this software was
written and distributed?

[1] https://www.w3.org/International/articles/language-tags/

[2] https://dataswamp.org/~incal/COMP-HIST

-- 
underground experts united
https://dataswamp.org/~incal




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

end of thread, other threads:[~2022-06-30  6:05 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-06-27 15:52 Select/highlight and *copy* matches of some regex Joost
2022-06-27 16:12 ` tomas
2022-06-27 17:16   ` Joost
2022-06-27 17:44     ` tomas
2022-06-28  4:40     ` Visuwesh
2022-06-28 19:52       ` Emanuel Berg
2022-06-29 21:40       ` Joost Kremers
2022-06-30  6:05         ` Emanuel Berg
2022-06-28  9:29     ` Daniel Martín
2022-06-28 10:33       ` tomas
2022-06-27 20:56 ` Stephen Berman
2022-06-27 21:09   ` Joost Kremers
2022-06-27 22:26     ` Emanuel Berg
2022-06-27 22:29     ` Emanuel Berg
2022-06-27 22:41       ` Emanuel Berg
2022-06-27 23:11         ` DWIM interface (was: Re: Select/highlight and *copy* matches of some regex) Emanuel Berg
2022-06-27 22:44       ` Select/highlight and *copy* matches of some regex Emanuel Berg
2022-06-28  4:43         ` tomas
2022-06-28  5:37           ` Emanuel Berg
2022-06-28  4:42   ` tomas

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).