all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "Pascal J. Bourguignon" <pjb@informatimago.com>
To: help-gnu-emacs@gnu.org
Subject: Re: A variant of match-end, but after replacement?
Date: Mon, 20 Jul 2015 02:33:13 +0200	[thread overview]
Message-ID: <87zj2r3bxy.fsf@kuiper.lan.informatimago.com> (raw)
In-Reply-To: mailman.7165.1437350800.904.help-gnu-emacs@gnu.org

Marcin Borkowski <mbork@mbork.pl> writes:
>> Not very much:
>>
>>     (let ((end        (match-end 1))
>>           (new-string (replace-match new-text t t string 1)))
>>       (do-something-from new-string
>>                          (+ end (- (length new-string) (length string)))))
>
> I give up.  It seems that a PhD in math is not enough to deal with
> string-length arithmetic;-).  Shame on me!  (OTOH, mails from you often
> teach me that when I have the impression that I'm smart, it's usually
> only an impression.)

Sorry, I was about to write out the equation, but if it's necessary,
I'll do.



   (length new-string)
   --------------------------------------------------------
   (length prefix)  (length new)           (length suffix)
   ---------------- ---------------------- ----------------
   pppppppppppppppp nnnnnnnnnnnnnnnnnnnnnn ssssssssssssssss
                                          ^
                                          |
                                       new-end

   (length old-string)
   --------------------------------------------------
   (length prefix)  (length old)     (length suffix)
   ---------------- ---------------- ----------------
   pppppppppppppppp oooooooooooooooo ssssssssssssssss
                                    ^
                                    |
                               (match-end)


So, we have:

    (= (length suffix) (- (length new-string) new-end))
    (= (length suffix) (- (length old-string) (match-end)))

Or, in math:

       s = ns-ne
       s = os-me
    
Therefore:

        0 = (ns-ne) - (os-me)
  <=>   0 = ns - ne - os + me
  <=>  ne = me + (ns - os)
  <=> (= new-end (+ (match-end) (- (length new-string) (length old-string))))
      
Hence:
           
     (let ((end        (match-end 1))
           (new-string (replace-match new-text t t old-string 1)))
       (do-something-from new-string
                          (+ end (- (length new-string) (length old-string)))))


-- 
__Pascal Bourguignon__                 http://www.informatimago.com/
“The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.” -- Carl Bass CEO Autodesk


  parent reply	other threads:[~2015-07-20  0:33 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <mailman.7146.1437306572.904.help-gnu-emacs@gnu.org>
2015-07-19 12:06 ` A variant of match-end, but after replacement? Pascal J. Bourguignon
2015-07-19 12:22   ` Marcin Borkowski
2015-07-19 13:25     ` Marcin Borkowski
2015-07-20 10:46     ` Nicolas Richard
     [not found]   ` <mailman.7147.1437308571.904.help-gnu-emacs@gnu.org>
2015-07-19 12:30     ` Pascal J. Bourguignon
2015-07-19 13:20       ` Marcin Borkowski
2015-07-19 13:26         ` Dmitry Gutov
2015-07-19 19:57           ` Marcin Borkowski
     [not found]       ` <mailman.7150.1437312076.904.help-gnu-emacs@gnu.org>
2015-07-19 13:41         ` Pascal J. Bourguignon
2015-07-19 19:58           ` Marcin Borkowski
     [not found]           ` <mailman.7160.1437335923.904.help-gnu-emacs@gnu.org>
2015-07-19 20:18             ` Barry Margolin
2015-07-19 22:45               ` Pascal J. Bourguignon
2015-07-20  0:06                 ` Marcin Borkowski
     [not found]                 ` <mailman.7165.1437350800.904.help-gnu-emacs@gnu.org>
2015-07-20  0:33                   ` Pascal J. Bourguignon [this message]
2015-07-19 11:49 Marcin Borkowski
2015-07-19 20:23 ` Dmitry Gutov

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=87zj2r3bxy.fsf@kuiper.lan.informatimago.com \
    --to=pjb@informatimago.com \
    --cc=help-gnu-emacs@gnu.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.