From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Tim Landscheidt Newsgroups: gmane.emacs.help Subject: Re: String replace with lisp code Date: Fri, 22 Jul 2011 00:39:52 +0000 Organization: Message-ID: References: <4E23CF23.4050908@easy-emacs.de> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Trace: dough.gmane.org 1311295219 1738 80.91.229.12 (22 Jul 2011 00:40:19 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Fri, 22 Jul 2011 00:40:19 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri Jul 22 02:40:15 2011 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([140.186.70.17]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1Qk3mp-0002to-7L for geh-help-gnu-emacs@m.gmane.org; Fri, 22 Jul 2011 02:40:15 +0200 Original-Received: from localhost ([::1]:45661 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Qk3mo-0007rw-Q2 for geh-help-gnu-emacs@m.gmane.org; Thu, 21 Jul 2011 20:40:14 -0400 Original-Received: from eggs.gnu.org ([140.186.70.92]:56731) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Qk3mj-0007qR-VE for help-gnu-emacs@gnu.org; Thu, 21 Jul 2011 20:40:11 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Qk3mi-000631-H1 for help-gnu-emacs@gnu.org; Thu, 21 Jul 2011 20:40:09 -0400 Original-Received: from lo.gmane.org ([80.91.229.12]:59445) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Qk3mi-00062b-Ad for help-gnu-emacs@gnu.org; Thu, 21 Jul 2011 20:40:08 -0400 Original-Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1Qk3mh-0002rM-By for help-gnu-emacs@gnu.org; Fri, 22 Jul 2011 02:40:07 +0200 Original-Received: from f054053195.adsl.alicedsl.de ([78.54.53.195]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 22 Jul 2011 02:40:07 +0200 Original-Received: from tim by f054053195.adsl.alicedsl.de with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 22 Jul 2011 02:40:07 +0200 X-Injected-Via-Gmane: http://gmane.org/ Mail-Followup-To: help-gnu-emacs@gnu.org Original-Lines: 35 Original-X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: f054053195.adsl.alicedsl.de Mail-Copies-To: never User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:xUrQfAlAQ6mjKZnz49AdhyUpWP4= X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) X-Received-From: 80.91.229.12 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 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.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:81717 Archived-At: Andreas Röhler wrote: >> During search/replace, how can I use the evaluation of a lisp expression as >> the substitution string? >> An example could be ... say I want to replace the occurrence of a certain >> word with the word prefixed with its occurrence count - >> For example - If I'd like to replace the occurrence of sea in - >> She sells sea shells on the sea shore >> with >> She sells (1)sea shells on the (2)sea shore. >> Or perhaps a more frequent use case of adding line number to each line >> (while I was typing this, I got the idea of using the keyboard macro to >> achieve this .... is there a better way though?) > M-x query-replace-regexp, > \(sea\) > RET > \,(progn (or count (setq count 1))(setq count (1+ > count))(concat "(" (number-to-string count) ")" \1)) > RET > ;;;;;;;;;;;; > \,EXPR > evaluates the expression and the takes the result for replacement. BTW, the number of previous replacements is available as "\#". Tim