Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make text in Emacs-Lisp-overlay overwritable

Tags:

emacs

elisp

I have file where some parts (lines) are fixed width, and other parts are free text. The format of the fixed width lines behaves somewhat like a table. Each line has a certain number of field, each field have a fixed length (in characters). When editing these lines one should use something similar to overwrite-mode.

I am trying figure out how to solve this in Emacs Lisp.

Here is what I have so far:

(defun test-overlay ()
  (interactive)
  (let ((ovl (make-overlay 1 5 (current-buffer) t t)))
    (overlay-put ovl 'face '(:background "grey50"))
    (overlay-put ovl 'intangible t)
    ))
  • How can I make the overlay behave like the minor mode overwrite-mode?

That is, the overlay should not expand when text is added at the right edge. When entering text, the text in the overlay should move to the left, the text at the left edge being chopped off (deleted). Pressing Delete at the right edge should delete characters at the right, moving in spaces at the left edge..

like image 951
Håkon Hægland Avatar asked Feb 26 '26 03:02

Håkon Hægland


2 Answers

Instead of using overlays, use text properties --- specifically the text property read-only. Set its value to nil for the text you want to be writable, and to non-nil for text you want to be read-only. (There is no read-only overlay property.)

From (elisp) Special Properties:

`read-only'

 If a character has the property `read-only', then modifying that
 character is not allowed.  Any command that would do so gets an
 error, `text-read-only'.  If the property value is a string, that
 string is used as the error message.

 Insertion next to a read-only character is an error if inserting
 ordinary text there would inherit the `read-only' property due to
 stickiness.  Thus, you can control permission to insert next to
 read-only text by controlling the stickiness.  *Note Sticky
 Properties::.

 Since changing properties counts as modifying the buffer, it is not
 possible to remove a `read-only' property unless you know the
 special trick: bind `inhibit-read-only' to a non-`nil' value and
 then remove the property.  *Note Read Only Buffers::.
like image 64
Drew Avatar answered Feb 27 '26 20:02

Drew


You need to use the modification-hooks property. That let you do the same kind of things as with after-change-functions and hence specify the exact behavior you're looking for.

like image 35
Stefan Avatar answered Feb 27 '26 20:02

Stefan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!