all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Jean Louis <bugs@gnu.support>
To: uzibalqa <uzibalqa@proton.me>
Cc: "help-gnu-emacs@gnu.org" <help-gnu-emacs@gnu.org>
Subject: Re: Apply replacement on word occurring at point using a lisp function
Date: Fri, 12 Aug 2022 23:24:03 +0300	[thread overview]
Message-ID: <Yva24weuy9PqgpSK@protected.localdomain> (raw)
In-Reply-To: <fWc23eVrpYY9bu_sWs2GFOdmbpdYBopRL91g6-DPAAk5vdK9x3nzWdf6K4D_jJEJRYk6-4DTCXMG8dTQx8u2En86ioddag4y7QAvia9E_DA=@proton.me>

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



  reply	other threads:[~2022-08-12 20:24 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-12 19:48 Apply replacement on word occurring at point using a lisp function uzibalqa via Users list for the GNU Emacs text editor
2022-08-12 20:24 ` Jean Louis [this message]
2022-08-12 20:28   ` uzibalqa
2022-08-12 20:37     ` Jean Louis
2022-08-12 20:59       ` uzibalqa
2022-08-13 10:24         ` Jean Louis
2022-08-13 14:17           ` uzibalqa

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Yva24weuy9PqgpSK@protected.localdomain \
    --to=bugs@gnu.support \
    --cc=help-gnu-emacs@gnu.org \
    --cc=uzibalqa@proton.me \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.