From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Marcin Borkowski Newsgroups: gmane.emacs.help Subject: Re: Function to replace some strings from region and save result in kill-ring Date: Sun, 08 Oct 2023 08:26:44 +0200 Message-ID: <87fs2lhbob.fsf@mbork.pl> References: Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="12988"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: mu4e 1.1.0; emacs 30.0.50 Cc: help-gnu-emacs@gnu.org To: PierGianLuca Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Sun Oct 08 08:28:00 2023 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1qpNGi-00035r-Cn for geh-help-gnu-emacs@m.gmane-mx.org; Sun, 08 Oct 2023 08:28:00 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qpNFt-00072K-DU; Sun, 08 Oct 2023 02:27:09 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qpNFp-0006tr-BZ for help-gnu-emacs@gnu.org; Sun, 08 Oct 2023 02:27:05 -0400 Original-Received: from mail.mojserwer.eu ([195.110.48.8]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qpNFn-0000O5-C5 for help-gnu-emacs@gnu.org; Sun, 08 Oct 2023 02:27:05 -0400 Original-Received: from localhost (localhost [127.0.0.1]) by mail.mojserwer.eu (Postfix) with ESMTP id 354851A00885; Sun, 8 Oct 2023 08:26:53 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at mail.mojserwer.eu Original-Received: from mail.mojserwer.eu ([127.0.0.1]) by localhost (mail.mojserwer.eu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id sb4HTTd56ZRD; Sun, 8 Oct 2023 08:26:49 +0200 (CEST) Original-Received: from localhost (178235147157.dynamic-3-poz-k-0-1-0.vectranet.pl [178.235.147.157]) by mail.mojserwer.eu (Postfix) with ESMTPSA id D8E011A00882; Sun, 8 Oct 2023 08:26:47 +0200 (CEST) In-reply-to: Received-SPF: pass client-ip=195.110.48.8; envelope-from=mbork@mbork.pl; helo=mail.mojserwer.eu X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.help:145251 Archived-At: On 2023-10-07, at 19:38, PierGianLuca 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 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