Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

4
Byte-offsets in after-change-functions
Post Body

So I'm working on something I think will be cool, but I've run up against a road block and was hoping someone here might be able to point me in the right direction before I invoke higher powers (e.g. emacs-devel).

I'm trying to interface with a library that needs to be notified about changes in the document. This is what after-change-functions is for. Such a function receives the endpoints of the changed region (after the change), and the length of the region before the change, in terms of characters. This is good, but I need it in terms of bytes.

The endpoints can be converted to bytes with position-bytes, but I can't convert the length before the change happened because characters may have been deleted, and I need to know which characters those were to compute their length in bytes.

So the only workaround I've figured out so far is this:

  • In before-change-functions, record the beginning of the region and the actual contents of the prior region as a string.
  • In after-change-functions, use the stored information to compute the actual number of bytes in the region that was changed.

This seems to work but it looks brittle. One call to before-change-functions may be followed by several calls to after-change-functions, so the latter needs to update the recorded region info every time it runs. This is rife with opportunities for off-by-one errors, and maybe expensive: we're looking at something like two calls to buffer-substring-no-properties, three calls to substring and one call to concat per change. That feels like overkill.

Also, the manual warns against using both before-change-functions and after-change-functions in tandem, presumably for this very reason.

Are there better ways to do this?

My current code, for reference: https://gist.github.com/TheBB/3cb185b75d826a67c31d09983e12eedc

Author
Account Strength
100%
Account Age
14 years
Verified Email
Yes
Verified Flair
No
Total Karma
141,685
Link Karma
2,184
Comment Karma
138,153
Profile updated: 2 days ago
Posts updated: 6 months ago
Evil maintainer

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
6 years ago