实现类似 tex 的上下标
默认的实现会隐藏 {}, _, ^ 编辑起来很不舒服
(defconst org-match-substring-regexp
(concat
"\\(\\S-\\)\\([_^]\\)\\("
"\\(?:" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
"\\|"
"\\(?:" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
"\\|"
"\\(?:.\\)"
"\\|"
"\\(?:\\\\[[:alnum:].,\\]*[[:alnum:]]\\)"
"\\)")
"The regular expression matching a sub- or superscript.")
(defun +org-raise-scripts (limit)
"Add raise properties to sub/superscripts."
(when (and org-pretty-entities org-pretty-entities-include-sub-superscripts
(re-search-forward org-match-substring-regexp limit t))
(let* ((pos (point)) table-p comment-p
(mpos (match-beginning 3))
(emph-p (get-text-property mpos 'org-emphasis))
(link-p (get-text-property mpos 'mouse-face))
(keyw-p (eq 'org-special-keyword (get-text-property mpos 'face)))
(tex-p (eq 'org-latex-and-related (get-text-property mpos 'face))))
(goto-char (line-beginning-position))
(setq table-p (looking-at-p org-table-dataline-regexp)
comment-p (looking-at-p "^[ \t]*#[ +]"))
(goto-char pos)
;; Handle a_b^c
(when (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
(if (not (or comment-p emph-p link-p keyw-p))
(put-text-property (match-beginning 3) (match-end 0)
'display
(if (equal (char-after (match-beginning 2)) ?^)
(nth (if table-p 3 1) org-script-display)
(nth (if table-p 2 0) org-script-display)))
(put-text-property (match-beginning 2) (match-end 3) 'org-emphasis t))
t)))
(advice-add #'org-raise-scripts :override #'+org-raise-scripts)
缺点:暂时没法做到只处理公式,但是不影响导出
另一种方法:
(add-hook! 'org-mode-hook
(defun +org-enable-sub-superscript ()
(require 'tex-mode)
(font-lock-add-keywords nil tex-font-lock-keywords-3)))
我觉得有点脏,所以自己实现了一下