Skip to content

Commit

Permalink
Improve behavior entering fragments from the end
Browse files Browse the repository at this point in the history
If a LaTeX environment is disabled by the point entering from the end of
the fragment, then the point is moved to the last line of the fragment
and the column of the point is preserved. Previously, the point always
moved to the start of the LaTeX environment.

Co-authored-by: Benjamin Levy <[email protected]>
  • Loading branch information
wosined and io12 committed Jul 8, 2022
1 parent e763ea5 commit ede4ac6
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions org-fragtog.el
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ return nil."
;; The fragment must be disabled before `org-latex-preview', since
;; `org-latex-preview' only toggles, leaving no guarantee that it's enabled
;; afterwards.
(org-fragtog--disable-frag frag)
(save-excursion
(org-fragtog--disable-frag frag))

;; Move to fragment and enable
(save-excursion
Expand All @@ -210,6 +211,27 @@ return nil."
(re-search-forward "[^ \t]")
(ignore-errors (org-latex-preview))))

(defun org-fragtog--set-point-after-disable-frag (frag)
"Set point to where it should be after FRAG was disabled.
If point decreases and enters a fragment from the end, disabling it, then point
should move to the end of the fragment. If point moved up one line, its column
should be maintained."
(when (and ;; There has to be a prev-point.
org-fragtog--prev-point
;; Only move to the end of the fragment if it's closer to the
;; prev-point location than the start of the fragment is.
(< (abs (- org-fragtog--prev-point (org-fragtog--frag-end frag)))
(abs (- org-fragtog--prev-point (org-fragtog--frag-start frag)))))
(let ((prev-point-column (save-excursion
(goto-char org-fragtog--prev-point)
(current-column))))
;; Move to the line where the fragment ends while preserving the point
;; column.
(goto-char (1- (org-fragtog--frag-end frag)))
(when (/= (line-number-at-pos org-fragtog--prev-point)
(line-number-at-pos))
(move-to-column prev-point-column)))))

(defun org-fragtog--disable-frag (frag &optional renew)
"Disable the Org LaTeX fragment preview for the fragment FRAG.
If RENEW is non-nil, renew the fragment at point."
Expand All @@ -224,7 +246,8 @@ If RENEW is non-nil, renew the fragment at point."
;; There may be nothing at the adjusted point
(when frag
(org-clear-latex-preview (org-fragtog--frag-start frag)
(org-fragtog--frag-end frag))))
(org-fragtog--frag-end frag))
(org-fragtog--set-point-after-disable-frag frag)))

(provide 'org-fragtog)

Expand Down

1 comment on commit ede4ac6

@clsty
Copy link

@clsty clsty commented on ede4ac6 Mar 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit causes very weird cursor-auto-jumping behavior when using with yasnippet.
I don't have time to submit an issue now (maybe someday later), just a quick reminder here.

Please sign in to comment.