From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Fren Zeee Newsgroups: gmane.emacs.help Subject: Re: How can I enter query-replace in GNU Emacs using a repeatable function based on values in the line currently at point. - Super User Date: Mon, 28 Jun 2010 00:32:18 -0700 (PDT) Organization: http://groups.google.com Message-ID: References: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: dough.gmane.org 1291839787 21042 80.91.229.12 (8 Dec 2010 20:23:07 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Wed, 8 Dec 2010 20:23:07 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed Dec 08 21:23:00 2010 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1PQQXT-0008A8-N6 for geh-help-gnu-emacs@m.gmane.org; Wed, 08 Dec 2010 21:23:00 +0100 Original-Received: from localhost ([127.0.0.1]:60099 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PQQXT-0004d1-1o for geh-help-gnu-emacs@m.gmane.org; Wed, 08 Dec 2010 15:22:59 -0500 Original-Path: usenet.stanford.edu!postnews.google.com!w12g2000yqj.googlegroups.com!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 111 Original-NNTP-Posting-Host: 75.28.137.98 Original-X-Trace: posting.google.com 1277710339 5545 127.0.0.1 (28 Jun 2010 07:32:19 GMT) Original-X-Complaints-To: groups-abuse@google.com Original-NNTP-Posting-Date: Mon, 28 Jun 2010 07:32:19 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: w12g2000yqj.googlegroups.com; posting-host=75.28.137.98; posting-account=Xy1x0QoAAAC1_8XWVbTILoO5VL5JMCDm User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6,gzip(gfe) Original-Xref: usenet.stanford.edu gnu.emacs.help:179294 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:75973 Archived-At: On Jun 24, 12:38=A0am, bolega wrote: > On Jun 21, 9:18=A0am, Tim Visher wrote: > > > > > On Mon, Jun 21, 2010 at 11:00 AM, Tim Landscheidt > > > wrote: > > > Tim Visher wrote: > > > >> I'd like to take the following data and query-replace occurences of > > >> each word identifier with the corresponding numeric identifier using > > >> some sort of repeatable function. > > > >> =A0 =A0 -1 ACT/CNS > > >> =A0 =A0 -2 AG NFC > > >> =A0 =A0 -3 AID > > >> =A0 =A0 -4 BBG > > >> =A0 =A0 -5 BIA > > >> =A0 =A0 -6 BLM > > >> =A0 =A0 -7 BOC > > >> =A0 =A0 -8 BPD > > >> =A0 =A0 -9 CCC > > >> =A0 =A0 -10 CDC > > >> =A0 =A0 -11 Census > > > >> In other words, with point at > > > >> =A0 =A0 -1 ACT/CNS > > >> =A0 =A0 ^ > > > >> I'd like to be able to hit a key and launch into the following comma= nd > > > >> =A0 =A0 query-replace RET ACT/CNS RET -1 RET > > >> [...] > > > > From my experience, the easiest way to do that is to copy > > > the lines to a temporary buffer, hack up a small lisplet: > > > > | (dolist (line '(("-1" =A0"ACT/CNS") > > > | =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 ("-2" =A0"AG NFC") > > > | =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 ("-3" =A0"AID") > > > | =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 ("-4" =A0"BBG") > > > | =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 ("-5" =A0"BIA") > > > | =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 ("-6" =A0"BLM") > > > | =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 ("-7" =A0"BOC") > > > | =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 ("-8" =A0"BPD") > > > | =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 ("-9" =A0"CCC") > > > | =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 ("-10" "CDC") > > > | =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 ("-11" "Census"))) > > > | =A0 (query-replace (car line) (cdr line) nil (point-min) (point-max= ))) > > > > and then evaluate that in the original buffer with M-x :. > > > > =A0While it is possible to create a proper "one-key command" > > > as you intended, chances are that it is very cumbersome and > > > that you will have to change it soon afterwards when your > > > requirements shift just a little bit. Directly programming > > > in Emacs Lisp is much easier and more flexible. > > > > Tim > > > That worked perfectly. Thanks, Tim. > > > -- > > > In Christ, > > Its new for me so I write the documentation here > > (defmacro dolist (spec &rest body) > =A0 "(dolist (VAR LIST [RESULT]) BODY...): loop over a list. > Evaluate BODY with VAR bound to each `car' from LIST, in turn. > Then evaluate RESULT to get return value, default nil." > =A0 (let ((temp (gensym "--dolist-temp--"))) > =A0 =A0 (list 'block nil > =A0 =A0 =A0 =A0 =A0 (list* 'let (list (list temp (nth 1 spec)) (car spec)= ) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0(list* 'while temp (list 'setq (car sp= ec) (list 'car temp)) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 (append body (list (list = 'setq temp > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0(list 'cdr temp))))) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0(if (cdr (cdr spec)) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0(cons (list 'setq (car spec) n= il) (cdr (cdr spec))) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0'(nil)))))) > > I think the readability and rememberability would be improved by > writing it as > > (dolist > =A0(foreach > =A0 '( ("" "") > =A0 =A0 =A0("" "") > =A0 =A0 =A0("" ""))) > =A0 (save-excursion (replace-string (car foreach) (cdr foreach)))) > > Bolega better to use eachpair than foreach