Search occurrences of character at point

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Search occurrences of character at point

Guido Van Hoecke-9
Hi,

I sometimes have to edit some text where I want to find all occurences
of some non-standard character, such as Char: ­ (173, #o255, #xad, ...)

I'd like a way to have the cursor sit on such a character and hit a key
to position it at the next occurrence of said character.

I'm sure there's a way to do this?

Any ideas, anyone?

TIA,

Guido

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Search occurrences of character at point

Yassin Philip
Well, you could use a rx regexp function like this:

(defun find-nonascii ()
   (interactive)
   (if (re-search-forward (rx nonascii))
       (message "There it is: (%s)"
                (buffer-substring-no-properties
                 (match-beginning 0) (match-end 0)))
     (message "doh!")))

yPhil

On 04/08/17 12:31, Guido Van Hoecke wrote:

> Hi,
>
> I sometimes have to edit some text where I want to find all occurences
> of some non-standard character, such as Char: ­ (173, #o255, #xad, ...)
>
> I'd like a way to have the cursor sit on such a character and hit a key
> to position it at the next occurrence of said character.
>
> I'm sure there's a way to do this?
>
> Any ideas, anyone?
>
> TIA,
>
> Guido
>

--
Yassin Philip      New album NOW
http://yassinphilip.bitbucket.io


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Search occurrences of character at point

Drew Adams
In reply to this post by Guido Van Hoecke-9
> I sometimes have to edit some text where I want to find all occurences
> of some non-standard character, such as Char:  (173, #o255, #xad, ...)
>
> I'd like a way to have the cursor sit on such a character and hit a key
> to position it at the next occurrence of said character.
>
> I'm sure there's a way to do this?

1. `M-s o' (command `occur'), followed by typing `[' followed
by typing or pasting the character, followed by typing `]'.

E.g., `M-s o [w]' shows occurrences of character `w'.

After typing `[' you can also use `C-x RET' followed by
typing the code point or the Unicode name of the character
(followed by typing `]').

`M-s o' shows matches for a regexp.  A regexp of a character
enclosed in `[...]' matches that character.

2. `occur' shows the matches in a separate buffer, where
you can navigate among them.  This is slightly different
from what you request, which is to hit a key to immediately
go to the next occurrence.

To do that, just use `C-s' (`isearch-forward')
followed by typing the character.  Repeat `C-s' to go to
subsequent occurrences of the character.  Again, instead
of typing the character you can use `C-x RET' followed by
typing the code point or the Unicode name of the character.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Search occurrences of character at point

Yuri Khan-2
In reply to this post by Guido Van Hoecke-9
On Fri, Aug 4, 2017 at 6:31 PM, Guido Van Hoecke <[hidden email]> wrote:

> I sometimes have to edit some text where I want to find all occurences
> of some non-standard character, such as Char: ­ (173, #o255, #xad, ...)
>
> I'd like a way to have the cursor sit on such a character and hit a key
> to position it at the next occurrence of said character.
>
> I'm sure there's a way to do this?

C-s (isearch-forward) C-M-y (isearch-yank-char)?

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Search occurrences of character at point

John Ankarström
In reply to this post by Guido Van Hoecke-9
Hello!

Guido Van Hoecke <[hidden email]> writes:

> I'd like a way to have the cursor sit on such a character and hit a key
> to position it at the next occurrence of said character.

Tinkering around in the *scratch* buffer, I came up with the
following function:

--8<---------------cut here---------------start------------->8---
(defun my/next-char-at-point ()
  (interactive)
  (set-mark (point))
  (deactivate-mark)
  (search-forward (format "%c" (char-after)) nil nil 2)
  (forward-char -1))
--8<---------------cut here---------------end--------------->8---

It works as you'd expect. Call it and it will bring you to the
next occurrence of the character at point. Conveniently, it will
also leave a mark at the old character.

This means that if you bind it to, say, `C-c n', you could hit
`C-c n' to go through all future occurrences of the char at
point, and then repeatedly use `C-u <space>' to pop the mark and
cycle back through the occurrences.

Pretty nifty, no?

- John

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Search occurrences of character at point

Drew Adams
In reply to this post by Yuri Khan-2
> C-s (isearch-forward) C-M-y (isearch-yank-char)?

Yes, of course.  This is the best answer (`C-s C-M-y').

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Search occurrences of character at point

Guido Van Hoecke-9
Thank you all for your help

On 4 August 2017 at 19:08, Drew Adams <[hidden email]> wrote:

> > C-s (isearch-forward) C-M-y (isearch-yank-char)?
>
> Yes, of course.  This is the best answer (`C-s C-M-y').
>

It seems that both `C-s C-M-y' and `C-s C-w' do the job, where C-M-w can
than be used to remove the last selected character(s)

Wish you all a nice weekend,

Guido
Loading...