[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: replace-regexp from A to B?
From: |
Rodolfo Medina |
Subject: |
Re: replace-regexp from A to B? |
Date: |
Tue, 28 Aug 2018 09:13:24 +0200 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/25.2 (gnu/linux) |
Yuri Khan <yurivkhan@gmail.com> writes:
> 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’).
Thanks... In my case, `C-M-h' does not select all the music piece but only
portions of it... Nevertheless, your second solution:
(defun my-musixtex-mark-piece ()
(interactive)
(let* ((end (re-search-forward "\\\\Endpiece\\>"))
(begin (search-backward "\\startpiece")))
(push-mark end nil t)
(goto-char begin)))
(with only two backslashes, instead of 4, before `startpiece', and nothing else
after `startpiece') seems to well select my music piece... Your first
solution, in particular
(defun my-musixtex-beginning-of-piece (arg)
(if (> arg 0)
(dotimes arg (re-search-backward "\\\\startpiece\\>"))
(dotimes (- arg) (re-search-forward "\\\\startpiece\\>"))))
gives error when evaluated with `C-x C-e'...
Rodolfo
- Re: replace-regexp from A to B?, Emanuel Berg, 2018/08/26
- Re: replace-regexp from A to B?, Rodolfo Medina, 2018/08/26
- Message not available
- Re: replace-regexp from A to B?, Emanuel Berg, 2018/08/26
- mixing argument types (was: Re: replace-regexp from A to B?), Emanuel Berg, 2018/08/26
- Re: mixing argument types, Stefan Monnier, 2018/08/26
- Message not available
- Re: mixing argument types, Emanuel Berg, 2018/08/26
- Re: mixing argument types, Rodolfo Medina, 2018/08/27
- Message not available
- Re: mixing argument types, Emanuel Berg, 2018/08/27
- Message not available
- Re: mixing argument types, Emanuel Berg, 2018/08/27