diff options
Diffstat (limited to 'lisp/quality-of-life.el')
| -rw-r--r-- | lisp/quality-of-life.el | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/lisp/quality-of-life.el b/lisp/quality-of-life.el index bd4d66a..09bba46 100644 --- a/lisp/quality-of-life.el +++ b/lisp/quality-of-life.el @@ -100,3 +100,56 @@ ;; :init (ace-link-setup-default) ;; :bind (:map org-mode-map ;; ("C-c C-o" . ace-link-org))) + + + +;; Hack so that fill-paragraph is aware of the width of in-line LaTeX previews. + +(defvar standard-fill-column + (symbol-function #'current-fill-column)) + +(defun extract-rest-of-paragraph () + "Return, as a string, the region from POINT to the end of the +paragraph." + (save-excursion + (let ((beg (point))) + (forward-paragraph) + (buffer-substring beg (point))))) + +(defun tex-aware-current-fill-column () + "Fill without breaking over LaTeX previews." + (interactive "d") + ;; This only really applies in `org-mode', so hop out if that's not the case. + (if (eq 'org-mode major-mode) + ;; Extract paragraph to temporary buffer. + (let ((rest (extract-rest-of-paragraph)) + res) + (with-temp-buffer + (insert rest) + (goto-char (point-min)) + + ;; "Flatten" out the region. + (org-latex-preview) + + (cl-flet ((end-position () (save-excursion (end-of-paragraph-text) (point)))) + (let ((char-width-read 0) + last-position) + (while (/= (point) (end-position)) + (setq last-position (point)) + (forward-sexp) + (setq char-width-read + (+ char-width-read + (- (point) last-position) + (cl-reduce #'+ + (mapcar #'(lambda (ol) + (car (image-size (overlay-get ol 'display)))) + (overlays-in last-position (point)))))) + (when (and (>= char-width-read fill-column) + (not res)) + (setq res char-width-read))))) + + (round (or res fill-column)))) + (funcall standard-fill-column))) + +(fset 'current-fill-column #'tex-aware-current-fill-column) +(fset 'current-fill-column standard-fill-column) |