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.
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
Subreddit
Post Details
- Posted
- 6 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/emacs/comme...