From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Yuri Khan Newsgroups: gmane.emacs.help Subject: Re: Custom query-replace. Date: Mon, 13 Jul 2015 13:19:31 +0600 Message-ID: References: <87y4ilnlvn.fsf@BanjaLuka.rclayton.net> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Trace: ger.gmane.org 1436772008 12286 80.91.229.3 (13 Jul 2015 07:20:08 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 13 Jul 2015 07:20:08 +0000 (UTC) Cc: "help-gnu-emacs@gnu.org" To: "R. Clayton" Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Mon Jul 13 09:20:06 2015 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1ZEY21-0000LN-RG for geh-help-gnu-emacs@m.gmane.org; Mon, 13 Jul 2015 09:20:05 +0200 Original-Received: from localhost ([::1]:53236 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZEY20-0002SC-ST for geh-help-gnu-emacs@m.gmane.org; Mon, 13 Jul 2015 03:20:04 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:59017) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZEY1p-0002S6-Mo for help-gnu-emacs@gnu.org; Mon, 13 Jul 2015 03:19:55 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZEY1o-0004q3-Oz for help-gnu-emacs@gnu.org; Mon, 13 Jul 2015 03:19:53 -0400 Original-Received: from mail-la0-x229.google.com ([2a00:1450:4010:c03::229]:35858) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZEY1o-0004pq-GZ for help-gnu-emacs@gnu.org; Mon, 13 Jul 2015 03:19:52 -0400 Original-Received: by lagw2 with SMTP id w2so9161180lag.3 for ; Mon, 13 Jul 2015 00:19:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc:content-type:content-transfer-encoding; bh=IkQjbTbdAtxu19tym2MY22OYA4uuvVuuob8PzzhjGDM=; b=QqTBXL78Rj0Yavc1XTBsCtowEAB0xxA4B8J/lrNtgGyn0fjXNCu4c59A52vdfCruvq yEFZD/IyBDkLZcxQx8SgoRcCPgC/SU64phULXuoAbiKhGCL+H1HGjrcxqzx0PTgm2qro zcGWckceaeciLoIMBkd4wbKQ433YqauI6bCREcL2a9DOodzBO8B1wbmTzcdt5Er/i+Ug RNrnYmOZ2vqRl2oV06XIKY4bYsnic+30PDI+U7mmHuEOVx7OGyrbKriSOJJ47trrfrrX xdRAsRZTAFAUoUWNhxat+jiwyPDff+O9fRM0igtPOVZsdBCM68ism0jsQKwZkDCoAmij NGfA== X-Received: by 10.152.121.42 with SMTP id lh10mr31552054lab.0.1436771991781; Mon, 13 Jul 2015 00:19:51 -0700 (PDT) Original-Received: by 10.25.43.65 with HTTP; Mon, 13 Jul 2015 00:19:31 -0700 (PDT) In-Reply-To: <87y4ilnlvn.fsf@BanjaLuka.rclayton.net> X-Google-Sender-Auth: 16t5W74AjEOZOAMWiOHYXWanppo X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2a00:1450:4010:c03::229 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:105674 Archived-At: On Mon, Jul 13, 2015 at 2:47 AM, R. Clayton wrote: > I'm trying to write a query-replace command to deal with hyphens in text.= For > each match, I want to perform one of three changes: > > delete hyphen; go from "for- got" to "forgot" > unspace hyphen; go from "red- headed" to "red-headed" > space hyphen; go from "stop- it" to "stop - it" > > as well as the usual query-replace options (quit, skip, and so on). For > example, typing 'D' at the query-replace prompt would delete the hyphen. So you want to search for a pattern and interactively optionally replace each occurrence with one of several possible replacements. I don=E2=80=99t know of a ready-made solution for that. Here=E2=80=99s what I= would do instead. Record and save four macros that: 0. all assume the starting position where the point is immediately after the hyphen and its following whitespace if any; 1a. one (=E2=80=9Cdelete-hyphen-and-find-next=E2=80=9D) deletes the precedi= ng whitespace and the hyphen; 1b. another (=E2=80=9Cunspace-hyphen-and-find-next=E2=80=9D) deletes the pr= eceding whitespace; 1c. yet another (=E2=80=9Cspace-hyphen-and-find-next=E2=80=9D) does a backw= ards isearch for the hyphen, inserts a single space before it and pops the mark saved by the isearch; 1d. the fourth (=E2=80=9Cfind-next-hyphen=E2=80=9D) does nothing at this po= int; 2. after which all four macros skip (using regexp isearch) to the next occurrence of the search pattern, preparing for the next replacement. Bind them to sufficiently easy keys. Go to the start of buffer (M-<); if it is not an occurrence of the search pattern, prime the loop by invoking =E2=80=9Cfind-next-hyphen=E2=80= =9D. Repeatedly invoke one of the four macros, until the buffer ends. In case of mistake, undo. To return to the place of last occurrence in case of doubt, pop the mark (C-u C-SPC). Between the replacements, you can make any other edits, provided that you leave the point at a position suitable for the next replacement. (=E2=80=9Cfind-next-hyphen=E2=80=9D can be used at any position.) Possible improvement: Rewrite the macros as elisp functions. Check preconditions. Instead of isearch, use non-interactive elisp search facilities. (This way, you protect against document corruption when running macros at unsuitable positions, and stop clobbering the last isearch pattern.) Possible further improvement: If you encounter this task frequently and the need for other edits is low, you might want to make it a minor mode that (1) primes the loop on entry; (2) binds super-easy (basically, single-character) keys; (3) binds another super-easy key (such as =E2=80=9Cq=E2=80=9D) to disable the mode.