From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Rodolfo Medina Newsgroups: gmane.emacs.help Subject: Re: replace-regexp from A to B? Date: Tue, 28 Aug 2018 09:13:24 +0200 Message-ID: <87h8jfm0a3.fsf@gmail.com> References: <87bm9pi43a.fsf@gmail.com> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: blaine.gmane.org 1535440334 26558 195.159.176.226 (28 Aug 2018 07:12:14 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Tue, 28 Aug 2018 07:12:14 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.2 (gnu/linux) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue Aug 28 09:12:10 2018 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1fuYAc-0006mR-1h for geh-help-gnu-emacs@m.gmane.org; Tue, 28 Aug 2018 09:12:10 +0200 Original-Received: from localhost ([::1]:36695 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fuYCi-0006Wj-DM for geh-help-gnu-emacs@m.gmane.org; Tue, 28 Aug 2018 03:14:20 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:38480) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fuYCB-0006Wc-07 for help-gnu-emacs@gnu.org; Tue, 28 Aug 2018 03:13:47 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fuYC7-0001by-N2 for help-gnu-emacs@gnu.org; Tue, 28 Aug 2018 03:13:46 -0400 Original-Received: from [195.159.176.226] (port=41475 helo=blaine.gmane.org) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fuYC7-0001aj-FU for help-gnu-emacs@gnu.org; Tue, 28 Aug 2018 03:13:43 -0400 Original-Received: from list by blaine.gmane.org with local (Exim 4.84_2) (envelope-from ) id 1fuY9t-0005tr-KH for help-gnu-emacs@gnu.org; Tue, 28 Aug 2018 09:11:25 +0200 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 82 Original-X-Complaints-To: usenet@blaine.gmane.org Cancel-Lock: sha1:aJoJBS7BHMnM8Asn7DLDaGJGntU= X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 195.159.176.226 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.21 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" Xref: news.gmane.org gmane.emacs.help:117781 Archived-At: Yuri Khan writes: > On Sun, Aug 26, 2018 at 9:40 PM Rodolfo Medina 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