unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: phillip.lord@newcastle.ac.uk (Phillip Lord)
To: Stefan Monnier <monnier@iro.umontreal.ca>
Cc: emacs-devel@gnu.org
Subject: Re: [Request for Mentor] subst-char-in-region
Date: Tue, 16 Dec 2014 11:30:15 +0000	[thread overview]
Message-ID: <87tx0vg7co.fsf@newcastle.ac.uk> (raw)
In-Reply-To: <jwv61ddm2p0.fsf-monnier+emacs@gnu.org> (Stefan Monnier's message of "Mon, 15 Dec 2014 09:29:07 -0500")

Stefan Monnier <monnier@iro.umontreal.ca> writes:

>> Initially, because I wasn't sure that the a-c-f gave me all the
>> information that I needed. I thought it might do, but I was confused by
>> the third argument which is "length in bytes of the pre-change text". Is
>> "length in bytes" the same as "char position". I presumed note.
>
> Ha, nicely spotted, that's a bug in the docstring.  It passes (or
> should anyway) the length in characters.


Ah, okay. Can I fix it? This started of as a RFM thread, and this would
give me my first commit to Emacs core, even if it is just documentation.


>
>> The difficulty is that in some cases the conversion function uses the
>> contents of this buffer to work out the equivalent location in that
>> buffer.  When the b-c-f is called the two buffers are in sync, so this
>> conversion works.  But when the a-c-f is called, the two buffers are not
>> in sync because we haven't percolated the change yet.
>
> So, rather than the length of the previous text, you'd need to actually
> know the previous text?  


Potentially, yes.

For example, imagine I have a buffer like this

====
Here is some documentation
====

which maps to another buffer where the documentation is commented out

====
;; Here is some documentation
====

How to map between "equivalent" positions in the two? I do this by
counting backward from the end of the line in both buffers; this works
because the two lines differ only at the beginning.

With this technique, to calculate the equivalent position for the end of
a change, I need to know whether there are any new lines in changed region.

> Of course, you can use b-c-f to stash the "previous text", but indeed
> it won't necessarily always be exactly right, in the sense that the
> previous text stashed in b-c-f may not have the exact same boundaries,
> so you'll need to massage it a bit.
>
> The way I see it, you'll need to do something like
>
>    (defvar-local my-before-change-text nil)
>
>    (defun my-bcf (beg end)
>      (setq my-before-change-text
>            (if (null my-before-change-text)
>                (cons beg (buffer-substring beg end))
>              (let* ((oldbeg (car my-before-change-text))
>                     (oldtext (cdr my-before-change-text))
>                     (oldend (+ oldbeg (length oldtext)))
>                     (newbeg (min beg oldbeg))
>                     (newend (max end oldend)))
>                (cl-assert (equal oldtext (buffer-substring oldbeg oldend)))
>                (if (or (< newbeg oldbeg) (> newend oldend))
>                    (cons newbeg (buffer-substring newbeg newend))
>                  my-before-change-text)))))
>    (add-hook 'before-change-functions #'my-bcf)
>
> And then in after-change-functions, you'll need something like
>
>    (defun my-acf (beg end oldlen)
>      (let ((oldtext (substring (cdr my-before-change-text)
>                                (- beg (car my-before-change-text))
>                                (+ oldlen (- beg (car my-before-change-text))))))
>        (setq my-before-change-text nil)
>        ...))


That's possible, I guess, but it's still messy. The problem is I now
need to analyse both the buffer and the text which has been removed. I
also use the same function (to convert locations in the two buffers)
outside of changes (on the post-command-hook) where there is no changed
text. It adds considerable complexity the function. Still I will give it
a try.

For the moment, though, I have a workable solution (which is only make
incremental changes after white-listing commands); I have this working
with diff-mode now (nice example, incidentally, cause I found another,
unrelated, bug in my code).

Would you accept a commit to subst-char-in-region, though, which signals
the correct values on b-c-f? As I outlined in my previous email, it can
be done with no increase in computational complexity. Then I could
white-list fill-paragraph (at least when emacs 25 comes out!).

Phil




  reply	other threads:[~2014-12-16 11:30 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-12-12 11:43 [Request for Mentor] subst-char-in-region Phillip Lord
2014-12-12 14:35 ` Stefan Monnier
2014-12-12 15:05   ` Phillip Lord
2014-12-12 16:17     ` Stefan Monnier
2014-12-15 12:15       ` Phillip Lord
2014-12-15 14:29         ` Stefan Monnier
2014-12-16 11:30           ` Phillip Lord [this message]
2014-12-16 14:07             ` Stefan Monnier
2014-12-16 16:05               ` Phillip Lord

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=87tx0vg7co.fsf@newcastle.ac.uk \
    --to=phillip.lord@newcastle.ac.uk \
    --cc=emacs-devel@gnu.org \
    --cc=monnier@iro.umontreal.ca \
    /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 public inbox

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

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).