Last active
April 1, 2026 18:19
-
-
Save jdtsmith/87ce361830466dd9ab2e304d17ddc2e4 to your computer and use it in GitHub Desktop.
org hidden emphasis: change cursor when "inside" emphasized org text
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;; `org-hide-emphasis-markers' is great, but when point is at the ends | |
| ;; of an emphasized piece of text, it's often not clear if you're in | |
| ;; position [A] or [B] (or, [X] or [Y]): | |
| ;; | |
| ;; [A]_[B]italicized[X]_[Y] | |
| ;; | |
| ;; This is frustrating, because sometimes you want to add text inside, | |
| ;; and sometimes you want to add adjacent text outside. The code below | |
| ;; tweaks `org' to help with with that problem, in two ways: | |
| ;; | |
| ;; 1. By adding `rear-nonsticky' properties, point will "approach" from | |
| ;; either side. I.e. if you move in from the left, point will be | |
| ;; at [A], if you move from the right, point will be at [B], and | |
| ;; similarly for the right boundary. | |
| ;; | |
| ;; 2. Cursor sensor functions change the cursor from a box to a bar | |
| ;; when point is "inside" the emphasis, so you always know where | |
| ;; edits will land. | |
| (defun my/emphasis-cursor (_win _pos type) | |
| "Indicate position inside hidden emphasis with bar cursor." | |
| (setq cursor-type (if (eq type 'entered) 'bar 'box))) | |
| (defun my/org-remove-cursor-text-properties (beg end &rest _r) | |
| "Remove cursor sensor properties." | |
| (with-silent-modifications | |
| (remove-text-properties beg end '(cursor-sensor-functions t)))) | |
| (defun my/org-do-emphasis-active-cursor (fun limit) | |
| (when-let ((ret (funcall fun limit))) | |
| (put-text-property (match-beginning 4) (match-end 2) | |
| 'cursor-sensor-functions '(my/emphasis-cursor)) | |
| (org-rear-nonsticky-at (match-end 3)) | |
| ret)) | |
| (when org-hide-emphasis-markers | |
| (advice-add 'org-unfontify-region :after 'my/org-remove-cursor-text-properties) | |
| (advice-add 'org-do-emphasis-faces :around 'my/org-do-emphasis-active-cursor)) | |
| ;; and turn on cursor-sensor-mode in org, e.g. in a use-package stanza: | |
| :hook | |
| (org-mode . (lambda () (when org-hide-emphasis-markers (cursor-sensor-mode 1)))) | |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In action:
org_emphasis.mov