[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: |
Jean Louis |
Subject: |
Re: Apply replacement on word occurring at point using a lisp function |
Date: |
Fri, 12 Aug 2022 23:24:03 +0300 |
User-agent: |
Mutt/+ () (2022-06-11) |
* 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)))
(goto-char (car bounds))
(delete-char (length word))
(insert (replace-regexp-in-string "\\<\\(co[glmnr]\\|coun\\|cum\\)" "k"
word))
(goto-char point)))
But your regular expression is incorrect in the above function,
as you are replacing it with "k" only. Maybe you try your best to
tell what exactly you wish to replace with what.
--
Jean
Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns
In support of Richard M. Stallman
https://stallmansupport.org/