From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: rvclayton@acm.org (R. Clayton) Newsgroups: gmane.emacs.help Subject: Custom query-replace. Date: Sun, 12 Jul 2015 16:47:56 -0400 Message-ID: <87y4ilnlvn.fsf@BanjaLuka.rclayton.net> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1436734205 11643 80.91.229.3 (12 Jul 2015 20:50:05 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sun, 12 Jul 2015 20:50:05 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sun Jul 12 22:49:51 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 1ZEOC4-0000no-Js for geh-help-gnu-emacs@m.gmane.org; Sun, 12 Jul 2015 22:49:48 +0200 Original-Received: from localhost ([::1]:51906 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZEOC3-0007as-Lz for geh-help-gnu-emacs@m.gmane.org; Sun, 12 Jul 2015 16:49:47 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:48060) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZEOBt-0007ai-L4 for help-gnu-emacs@gnu.org; Sun, 12 Jul 2015 16:49:38 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZEOBq-0005Sg-G2 for help-gnu-emacs@gnu.org; Sun, 12 Jul 2015 16:49:37 -0400 Original-Received: from plane.gmane.org ([80.91.229.3]:44093) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZEOBq-0005QE-96 for help-gnu-emacs@gnu.org; Sun, 12 Jul 2015 16:49:34 -0400 Original-Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1ZEOBn-0000g1-AL for help-gnu-emacs@gnu.org; Sun, 12 Jul 2015 22:49:31 +0200 Original-Received: from pool-74-105-65-207.nwrknj.fios.verizon.net ([74.105.65.207]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sun, 12 Jul 2015 22:49:31 +0200 Original-Received: from rvclayton by pool-74-105-65-207.nwrknj.fios.verizon.net with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sun, 12 Jul 2015 22:49:31 +0200 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 51 Original-X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: pool-74-105-65-207.nwrknj.fios.verizon.net User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux) Cancel-Lock: sha1:I4qf8Z55bwCcEn1Qw5WpTfS/Bug= X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 80.91.229.3 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:105664 Archived-At: 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. I had a hazy idea I could do this by flogging some keymap. However, once I got this far (defun rehyphenate () (interactive) (let (f r) (fset 'r (lambda (data count) (concat (match-string 1) "-" (match-string 2)))) (fset 'f (lambda () (perform-replace "\\([a-z]\\)- +\\([a-z]\\)" ; from-string (cons 'r "") ; replacements t ; query-flag t ; regexp-flag nil ; delimited-flag nil ; repeat-count nil ; keymap (point-min) ; start (point-max) ; end ))) (while (f) ))) I realized my hazy idea was hazier than I though. It seems to me I have two paths I can follow: Continue to flog the keymap by including commands in the keymap that in turn flog the replacement text, or otherwise communicate the change choice to the replacement function. Move the query from the match to the replacement function, which would forego standard query-replace behavior, such as quitting and skipping, which is an ok trade-off. Both of these paths seem unattractive to me. What alternatives are there available for me to do what I want? Is there some similarly-behaving code around I can steal from?