unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Function to replace some strings from region and save result in kill-ring
@ 2023-10-07 17:38 PierGianLuca
  2023-10-08  6:26 ` Marcin Borkowski
  0 siblings, 1 reply; 3+ messages in thread
From: PierGianLuca @ 2023-10-07 17:38 UTC (permalink / raw)
  To: help-gnu-emacs

Hi everyone!

For some time I've wished to write a function that does the following:

1. Takes a string from (a) a region, if one is selected, or (b) user prompt, if a region is not selected.

2. Internally replaces a couple of regexp in the string with other strings. Specifically, the regexp "[ _]+" with "_", and "[.,;:?!'"`]" with "" (nothing).

3. Saves the new string obtained from the replacement into the kill-ring


Note that the original region is *not* changed.


I have basically no knowledge of elisp, so I tried to reverse-engineer and combine some functions with similar features that I have in my init file (origins forgotten in the depths of time). There is also an old post in this mailing list <https://lists.gnu.org/archive/html/help-gnu-emacs/2013-09/msg00219.html> with a partially similar request, yet with important differences.


My starting point is this, which should take care of 1.:


(defun changestring (&optional arg)
   "Replace some character in string and save to kill-ring."
   (interactive "p")
   (if (use-region-p)
       (let ((region (buffer-substring-no-properties (region-beginning) (region-end))))
*** the replacing part should go here ***
	)
     (let ((region (read-string "String to be converted: ")))
*** the replacing part should go here, again ***
       )
     )
   )


I thought I should use something like "(kill-region ...)" somewhere; but not quite, because the actual region should be untouched. I'm also having difficulties using several query-replace-regexp within a lisp function.

I'd appreciate any help!

Cheers,
Luca



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Function to replace some strings from region and save result in kill-ring
  2023-10-07 17:38 Function to replace some strings from region and save result in kill-ring PierGianLuca
@ 2023-10-08  6:26 ` Marcin Borkowski
  2023-10-08  9:39   ` PierGianLuca
  0 siblings, 1 reply; 3+ messages in thread
From: Marcin Borkowski @ 2023-10-08  6:26 UTC (permalink / raw)
  To: PierGianLuca; +Cc: help-gnu-emacs


On 2023-10-07, at 19:38, PierGianLuca <luca@magnaspesmeretrix.org> wrote:

> Hi everyone!
>
> For some time I've wished to write a function that does the following:
>
> 1. Takes a string from (a) a region, if one is selected, or (b) user prompt, if a region is not selected.
>
> 2. Internally replaces a couple of regexp in the string with other strings. Specifically, the regexp "[ _]+" with "_", and "[.,;:?!'"`]" with "" (nothing).
>
> 3. Saves the new string obtained from the replacement into the kill-ring
>
>
> Note that the original region is *not* changed.
>
>
> I have basically no knowledge of elisp, so I tried to reverse-engineer and combine some functions with similar features that I have in my init file (origins forgotten in the depths of time). There is also an old post in this mailing list <https://lists.gnu.org/archive/html/help-gnu-emacs/2013-09/msg00219.html> with a partially similar request, yet with important differences.
>
>
> My starting point is this, which should take care of 1.:
>
>
> (defun changestring (&optional arg)
>   "Replace some character in string and save to kill-ring."
>   (interactive "p")
>   (if (use-region-p)
>       (let ((region (buffer-substring-no-properties (region-beginning) (region-end))))
> *** the replacing part should go here ***
> 	)
>     (let ((region (read-string "String to be converted: ")))
> *** the replacing part should go here, again ***
>       )
>     )
>   )
>
>
> I thought I should use something like "(kill-region ...)" somewhere; but not quite, because the actual region should be untouched. I'm also having difficulties using several query-replace-regexp within a lisp function.
>
> I'd appreciate any help!

First of all, you seem to want to repeat the replacing code, which is
not the best idea - better to first assign the text to be manipulated to
a variable (using `let'), then manipulate it.

While at that, if you want more than one expression in the "then" branch
if an `if', use `progn' or some similar construct.

Then, don't use `query-replace-regexp' in Elisp - use a `while' loop
with a `replace-regexp-in-string', for example.

Then, you're right that `kill-region' is not what you want.  But if you
skim the source of `kill-region' (you don't even have to analyze or
fully understand it!), you'll find `kill-new', whose docstring says:

--8<---------------cut here---------------start------------->8---
Make STRING the latest kill in the kill ring.
--8<---------------cut here---------------end--------------->8---

Have you read the excellent "Intro to Emacs Lisp" by Robert Chassel?

Hth,

-- 
Marcin Borkowski
http://mbork.pl



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Function to replace some strings from region and save result in kill-ring
  2023-10-08  6:26 ` Marcin Borkowski
@ 2023-10-08  9:39   ` PierGianLuca
  0 siblings, 0 replies; 3+ messages in thread
From: PierGianLuca @ 2023-10-08  9:39 UTC (permalink / raw)
  To: Marcin Borkowski; +Cc: help-gnu-emacs

Thank you for the tips, Marcin!

> Have you read the excellent "Intro to Emacs Lisp" by Robert Chassel?

Unfortunately not (yet), thank you for the recommendation. Learning Emacs lisp has been on my to-do list for years, but I've simply not found the time yet :( Really just a sad matter of priority. Generally I like learning anything I use as in-depth as possible, and solve problems on my own, but time is a mean beast... Another topic I'd like to learn about is how the Linux kernel works, for example.

I'll try to look up the functions you mention and see how to use them!

Cheers,
Luca

> 
> First of all, you seem to want to repeat the replacing code, which is
> not the best idea - better to first assign the text to be manipulated to
> a variable (using `let'), then manipulate it.
> 
> While at that, if you want more than one expression in the "then" branch
> if an `if', use `progn' or some similar construct.
> 
> Then, don't use `query-replace-regexp' in Elisp - use a `while' loop
> with a `replace-regexp-in-string', for example.
> 
> Then, you're right that `kill-region' is not what you want.  But if you
> skim the source of `kill-region' (you don't even have to analyze or
> fully understand it!), you'll find `kill-new', whose docstring says:
> 
> --8<---------------cut here---------------start------------->8---
> Make STRING the latest kill in the kill ring.
> --8<---------------cut here---------------end--------------->8---
> 
> Have you read the excellent "Intro to Emacs Lisp" by Robert Chassel?



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-10-08  9:39 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-07 17:38 Function to replace some strings from region and save result in kill-ring PierGianLuca
2023-10-08  6:26 ` Marcin Borkowski
2023-10-08  9:39   ` PierGianLuca

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).