all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Yuri Khan <yuri.v.khan@gmail.com>
To: "R. Clayton" <rvclayton@acm.org>
Cc: "help-gnu-emacs@gnu.org" <help-gnu-emacs@gnu.org>
Subject: Re: Custom query-replace.
Date: Mon, 13 Jul 2015 13:19:31 +0600	[thread overview]
Message-ID: <CAP_d_8V4esgz+duk2xRfOvA+hb=wp1VXQGLPvxWCdG7oX=jQeQ@mail.gmail.com> (raw)
In-Reply-To: <87y4ilnlvn.fsf@BanjaLuka.rclayton.net>

On Mon, Jul 13, 2015 at 2:47 AM, R. Clayton <rvclayton@acm.org> 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’t know of a ready-made solution for that. Here’s 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 (“delete-hyphen-and-find-next”) deletes the preceding
whitespace and the hyphen;
1b. another (“unspace-hyphen-and-find-next”) deletes the preceding whitespace;
1c. yet another (“space-hyphen-and-find-next”) does a backwards
isearch for the hyphen, inserts a single space before it and pops the
mark saved by the isearch;
1d. the fourth (“find-next-hyphen”) does nothing at this point;

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 “find-next-hyphen”.

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.
(“find-next-hyphen” 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 “q”) to disable the mode.



  reply	other threads:[~2015-07-13  7:19 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-07-12 20:47 Custom query-replace R. Clayton
2015-07-13  7:19 ` Yuri Khan [this message]
2015-07-13 10:55   ` Marcin Borkowski
2015-07-14 17:32   ` R. Clayton

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAP_d_8V4esgz+duk2xRfOvA+hb=wp1VXQGLPvxWCdG7oX=jQeQ@mail.gmail.com' \
    --to=yuri.v.khan@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    --cc=rvclayton@acm.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.