* Apply replacement on word occurring at point using a lisp function
@ 2022-08-12 19:48 uzibalqa via Users list for the GNU Emacs text editor
2022-08-12 20:24 ` Jean Louis
0 siblings, 1 reply; 7+ messages in thread
From: uzibalqa via Users list for the GNU Emacs text editor @ 2022-08-12 19:48 UTC (permalink / raw)
To: help-gnu-emacs@gnu.org
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?
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Apply replacement on word occurring at point using a lisp function
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
2022-08-12 20:28 ` uzibalqa
0 siblings, 1 reply; 7+ messages in thread
From: Jean Louis @ 2022-08-12 20:24 UTC (permalink / raw)
To: uzibalqa; +Cc: help-gnu-emacs@gnu.org
* 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/
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Apply replacement on word occurring at point using a lisp function
2022-08-12 20:24 ` Jean Louis
@ 2022-08-12 20:28 ` uzibalqa
2022-08-12 20:37 ` Jean Louis
0 siblings, 1 reply; 7+ messages in thread
From: uzibalqa @ 2022-08-12 20:28 UTC (permalink / raw)
To: Jean Louis; +Cc: help-gnu-emacs@gnu.org
------- 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'.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Apply replacement on word occurring at point using a lisp function
2022-08-12 20:28 ` uzibalqa
@ 2022-08-12 20:37 ` Jean Louis
2022-08-12 20:59 ` uzibalqa
0 siblings, 1 reply; 7+ messages in thread
From: Jean Louis @ 2022-08-12 20:37 UTC (permalink / raw)
To: uzibalqa; +Cc: help-gnu-emacs@gnu.org
* uzibalqa <uzibalqa@proton.me> [2022-08-12 23:29]:
> This is what I want to do
>
> "cognize" changed to "knize"
> "collect" to "klect"
> "corrupt" to "krupt"
> "cumulatively" to "kulatively"
How I read it, you wish to change some words, I understand above statement.
> Meaning that the initial matches of words as defined in regex are
> replaced with the letter `k'.
The last above statement is contradictory to your explanation above.
If there are just few words, then you could use this:
(defun my-get-them ()
(let* ((point (point)))
(goto-char 1)
(replace-regexp "cognize" "knize")
(replace-regexp "collect" "klect")
(replace-regexp "corrupt" "krup")
(replace-regexp "cumulatively" "kulatively")
(goto-char point)))
--
Jean
Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns
In support of Richard M. Stallman
https://stallmansupport.org/
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Apply replacement on word occurring at point using a lisp function
2022-08-12 20:37 ` Jean Louis
@ 2022-08-12 20:59 ` uzibalqa
2022-08-13 10:24 ` Jean Louis
0 siblings, 1 reply; 7+ messages in thread
From: uzibalqa @ 2022-08-12 20:59 UTC (permalink / raw)
To: Jean Louis; +Cc: help-gnu-emacs@gnu.org
------- Original Message -------
On Friday, August 12th, 2022 at 8:37 PM, Jean Louis <bugs@gnu.support> wrote:
> * uzibalqa uzibalqa@proton.me [2022-08-12 23:29]:
>
> > This is what I want to do
> >
> > "cognize" changed to "knize"
> > "collect" to "klect"
> > "corrupt" to "krupt"
> > "cumulatively" to "kulatively"
>
>
> How I read it, you wish to change some words, I understand above statement.
>
> > Meaning that the initial matches of words as defined in regex are
> > replaced with the letter `k'.
>
>
> The last above statement is contradictory to your explanation above.
>
> If there are just few words, then you could use this:
>
> (defun my-get-them ()
> (let* ((point (point)))
> (goto-char 1)
> (replace-regexp "cognize" "knize")
> (replace-regexp "collect" "klect")
> (replace-regexp "corrupt" "krup")
> (replace-regexp "cumulatively" "kulatively")
> (goto-char point)))
Jean, I have written the words as examples. But really wanted a general scheme for
replacements based on specified rules (e.g. initial matches of cog changed to k),
rather than using actual words.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Apply replacement on word occurring at point using a lisp function
2022-08-12 20:59 ` uzibalqa
@ 2022-08-13 10:24 ` Jean Louis
2022-08-13 14:17 ` uzibalqa
0 siblings, 1 reply; 7+ messages in thread
From: Jean Louis @ 2022-08-13 10:24 UTC (permalink / raw)
To: uzibalqa; +Cc: help-gnu-emacs@gnu.org
* uzibalqa <uzibalqa@proton.me> [2022-08-13 00:00]:
> Jean, I have written the words as examples. But really wanted a
> general scheme for replacements based on specified rules
> (e.g. initial matches of cog changed to k), rather than using actual
> words.
You have not presented definition of your general rule and I do not
have the library M-x telepathy-mode to know what you mean.
--
Jean
Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns
In support of Richard M. Stallman
https://stallmansupport.org/
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Apply replacement on word occurring at point using a lisp function
2022-08-13 10:24 ` Jean Louis
@ 2022-08-13 14:17 ` uzibalqa
0 siblings, 0 replies; 7+ messages in thread
From: uzibalqa @ 2022-08-13 14:17 UTC (permalink / raw)
To: Jean Louis; +Cc: help-gnu-emacs@gnu.org
------- Original Message -------
On Saturday, August 13th, 2022 at 10:24 AM, Jean Louis <bugs@gnu.support> wrote:
> * uzibalqa uzibalqa@proton.me [2022-08-13 00:00]:
>
> > Jean, I have written the words as examples. But really wanted a
> > general scheme for replacements based on specified rules
> > (e.g. initial matches of cog changed to k), rather than using actual
> > words.
>
>
> You have not presented definition of your general rule and I do not
> have the library M-x telepathy-mode to know what you mean.
>
> --
> Jean
Your original defun "change-word-at-point" does the job. Using a regex as you
did was good but did not want to go along your second path of matching exact
words, as matching words would entail going through the contents of the dictionary
and finding the words that match a rule (words starting with "cor").
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2022-08-13 14:17 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
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
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).