5

I'm using some buttons, and want to arrange them in a row. That is, some will be directly next to each other. I don't want whitespace between them. This is easy enough to do:

However, when I do this, mousing over either of them highlights both. I would expect only the moused-over button to be highlighted.

Using this code:

(progn (insert ?\n)
       (insert-text-button "one")
       (insert " ")
       (insert-text-button "two")
       (insert " ")
       (insert-text-button "three")
       (insert-text-button "four"))

Here is a screenshot of what happens when I mouse over a button with whitespace after it. Notice that only "one" is highlighted.

enter image description here

And here is a screenshot of what happens when I mouse over a button with a button immediately after it. It highlights the button "three", which is expected, but also the button "four", which is not.

enter image description here

How can I make Emacs only highlight "three", the button being moused over?

1 Answer 1

7

Buttons are based on text properties. Each character in a buffer has its own properties. Emacs doesn't record start and end positions for properties: properties are not intervals. When it needs to know where a property starts and ends, it looks for the previous or next change in property. Likewise, buttons don't have a recorded start and end: instead all the characters that make up the button have the same button-related properties.

Buttons are highlighted when the mouse cursor hovers over them because they have the mouse-face property set to highlight. Emacs highlights the text extent around the character under the mouse cursor that has the same value for the mouse-face property. In your example, when the cursor is over the h, the whole text threefour gets highlighted since that's as far as the mouse-face property has the same value that it has for the h.

If you want to separate two buttons without having a visual separation, insert a zero-width space between them.

(progn (insert ?\n)
       (insert-text-button "one")
       (insert " ")
       (insert-text-button "two")
       (insert " ")
       (insert-text-button "three")
       (insert "\u200b")
       (insert-text-button "four"))
1
  • Perfect! Thanks.
    – zck
    Aug 15, 2020 at 17:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.