Guest User

Untitled

a guest
Aug 30th, 2021
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 2.02 KB | None | 0 0
  1. (defun setup-subscripts-display ()
  2.   "Make numbers next to identifiers (like x0 or y4) appear like
  3. subscripts. Uses `prettify-symbols-mode'."
  4.   (setf prettify-symbols-unprettify-at-point 'right-edge)
  5.   (mapc (lambda (x) (push x prettify-symbols-alist))
  6.         '(("0" . "₀")
  7.           ("1" . "₁")
  8.           ("2" . "₂")
  9.           ("3" . "₃")
  10.           ("4" . "₄")
  11.           ("5" . "₅")
  12.           ("6" . "₆")
  13.           ("7" . "₇")
  14.           ("8" . "₈")
  15.           ("9" . "₉")))
  16.   (cl-macrolet ((save-start (&rest body) `(save-excursion (goto-char start) ,@body)))
  17.     (setf prettify-symbols-compose-predicate
  18.           (lambda (start end _match)
  19.             (if (save-start (cl-digit-char-p (following-char)))
  20.                 (or
  21.                  ;; #nλ subscripts
  22.                  (and (save-start ; #
  23.                        (loop while (cl-digit-char-p (following-char))
  24.                              do (backward-char))
  25.                        (eql (following-char) ?#))
  26.                       (save-start ; λ
  27.                        (forward-char)
  28.                        (eql (following-char))))
  29.                  ;; regular subscribpts
  30.                  (and (save-start ; excepted symbols
  31.                        (not (member (sexp-at-point) '(prin1))))
  32.                       (save-start ; on the left, ensure there's some letter
  33.                        (loop while (cl-digit-char-p (following-char))
  34.                              do (backward-char))
  35.                        (string-match-p (rx letter) (char-to-string (following-char))))
  36.                       (save-start ; on the right, there must be not other letters
  37.                        (loop while (cl-digit-char-p (following-char))
  38.                              do (forward-char))
  39.                        (string-match-p (rx (or white ")" 10))
  40.                                        (char-to-string (following-char))))))
  41.               (prettify-symbols-default-compose-p start end _match)))))
  42.   (prettify-symbols-mode))
  43.  
  44. (setup-subscripts-display)
Advertisement
Add Comment
Please, Sign In to add comment