all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / 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

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

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.