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

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

Re: Apply replacement on word occurring at point using a lisp function


From: uzibalqa
Subject: Re: Apply replacement on word occurring at point using a lisp function
Date: Fri, 12 Aug 2022 20:28:42 +0000




------- Original Message -------
On Friday, August 12th, 2022 at 8:24 PM, Jean Louis <bugs@gnu.support> wrote:


> * uzibalqa via Users list for the GNU Emacs text editor 
> help-gnu-emacs@gnu.org [2022-08-12 22:49]:
>
> > I want to insert the letter `k' for words with initial` cog', `col',` com', 
> > `con',` cor', `coun',` cum'."
> >
> > For this I have written
> >
> > (replace-regexp "\\<\\(co[glmnr]\\|coun\\|cum\\)" "k")
> >
> > What I want to do is apply the replacement on the word occurring at
> > point using a lisp function. How can this be achieved?
>
>
> (thing-at-point 'word) is to find word at point
>
> Then you may find cursor position and replace the region with new word.
>
> This is the way to go:
>
> (defun change-word-at-point ()
> (interactive)
> (let* ((bounds (bounds-of-thing-at-point 'word))
> (word (buffer-substring (car bounds) (cdr bounds)))
> (point (point)))

This is what I want to do

"cognize" changed to "knize"
"collect" to "klect"
"corrupt" to "krupt"
"cumulatively" to "kulatively"

Meaning that the initial matches of words as defined in regex are replaced with 
the letter `k'.



reply via email to

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