From: Yuri Khan <yurivkhan@gmail.com>
To: rodolfo.medina@gmail.com
Cc: help-gnu-emacs <help-gnu-emacs@gnu.org>
Subject: Re: replace-regexp from A to B?
Date: Sun, 26 Aug 2018 22:50:31 +0700 [thread overview]
Message-ID: <CAP_d_8U8sOtPFUb5h71zVfBXdOk8rAp+X9gbEPQ-wFZELnD-_w@mail.gmail.com> (raw)
In-Reply-To: <87bm9pi43a.fsf@gmail.com>
On Sun, Aug 26, 2018 at 9:40 PM Rodolfo Medina <rodolfo.medina@gmail.com> wrote:
> Is it possible, and how?, to perform a replace-regexp from a certain point,
> e.g. the current one, or from a certain word/expression, up to the next
> occurrence of a certain other word/expression...? In my case, with MusiXTeX
> documents, the starting point should be the TeX command `\startpiece' and the
> final one `\Endpiece'. So I could replace strings/expressions within a single
> musical piece without going out of it.
I’d decompose the problem into two.
1. Mark the piece enclosing the point as a region.
2. Do an ordinary ‘replace-regexp’ or ‘query-replace-regexp’ in the region.
To solve (1), I’d first look if the major mode recognizes pieces as a
construct analogous to a function in a programming language, by
invoking ‘mark-defun’ and looking if it marks the whole piece. If it
does, problem solved; you can query-and-replace in current piece by
doing ‘C-M-h M-%’ or ‘C-M-h C-M-%’; or, if you want to do multiple
replacements in a piece, first narrow to it with ‘C-x n d’, then do
all your replacements, then widen back with ‘C-x n w’.
If the major mode does not have any useful notion of defun, I’d define
my own and arrange for it to be used by the major mode:
(defun my-musixtex-beginning-of-piece (arg)
(if (> arg 0)
(dotimes arg (re-search-backward "\\\\startpiece\\>"))
(dotimes (- arg) (re-search-forward "\\\\startpiece\\>"))))
(defun my-musixtex-end-of-piece ()
(re-search-forward "\\\\Endpiece\\>"))
(defun my-musixtex-init-defun ()
(setq-local beginning-of-defun-function
#'my-musixtex-beginning-of-piece)
(setq-local end-of-defun-function
#'my-musixtex-end-of-piece))
;; change the mode hook variable name to suit your major mode
(add-hook 'musixtex-mode-hook #'my-musixtex-init-defun)
If the major mode does have a useful idea of defun, I’d then just
write a function like this:
(defun my-musixtex-mark-piece ()
(interactive)
(let* ((end (re-search-forward "\\\\Endpiece\\>"))
(begin (search-backward "\\\\startpiece\\>")))
(push-mark end nil t)
(goto-char begin)))
and bind it to a convenient key probably involving the letter ‘h’ and
a few modifiers and/or prefix keys (because ‘mark-paragraph’ is on
‘M-h’, ‘mark-defun’ on ‘C-M-h’ and ‘mark-whole-buffer’ on ‘C-x h’).
next prev parent reply other threads:[~2018-08-26 15:50 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-08-26 14:40 replace-regexp from A to B? Rodolfo Medina
2018-08-26 15:50 ` Yuri Khan [this message]
2018-08-28 7:13 ` Rodolfo Medina
[not found] ` <87wosd0yoe.fsf@himinbjorg.adminart.net>
2018-08-28 7:15 ` Rodolfo Medina
[not found] <mailman.5556.1535294433.1292.help-gnu-emacs@gnu.org>
2018-08-26 16:02 ` Emanuel Berg
2018-08-26 17:54 ` Rodolfo Medina
[not found] ` <mailman.5566.1535306070.1292.help-gnu-emacs@gnu.org>
2018-08-26 19:35 ` Emanuel Berg
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
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CAP_d_8U8sOtPFUb5h71zVfBXdOk8rAp+X9gbEPQ-wFZELnD-_w@mail.gmail.com \
--to=yurivkhan@gmail.com \
--cc=help-gnu-emacs@gnu.org \
--cc=rodolfo.medina@gmail.com \
/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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).