【老问题】无法通过 org-emphasis-regexp-components 进行 org 中文行内格式化

因为 org-mode 渲染用的是 org-do-emphasis-faces ,而 parse 用的是 org-element--parse-generic-emphasis (后者比较新), 所以旧的方法有点不适用,需要在原来的基础上对 org-element--parse-generic-emphasis 做点修改:

(defun eli/org-element--parse-generic-emphasis (mark type)
  "Parse emphasis object at point, if any.

MARK is the delimiter string used.  TYPE is a symbol among
`bold', `code', `italic', `strike-through', `underline', and
`verbatim'.

Assume point is at first MARK."
  (save-excursion
    (let ((origin (point)))
      (unless (bolp) (forward-char -1))
      (let ((opening-re
             (rx-to-string
              `(seq (or line-start (any space ?- ?\( ?' ?\" ?\{ nonascii))
                    ,mark
                    (not space)))))
        (when (looking-at opening-re)
          (goto-char (1+ origin))
          (let ((closing-re
                 (rx-to-string
                  `(seq
                    (not space)
                    (group ,mark)
                    (or (any space ?- ?. ?, ?\; ?: ?! ?? ?' ?\" ?\) ?\} ?\\ ?\[
                             nonascii)
                        line-end)))))
            (when (re-search-forward closing-re nil t)
              (let ((closing (match-end 1)))
                (goto-char closing)
                (let* ((post-blank (skip-chars-forward " \t"))
                       (contents-begin (1+ origin))
                       (contents-end (1- closing)))
                  (list type
                        (append
                         (list :begin origin
                               :end (point)
                               :post-blank post-blank)
                         (if (memq type '(code verbatim))
                             (list :value
                                   (and (memq type '(code verbatim))
                                        (buffer-substring
                                         contents-begin contents-end)))
                           (list :contents-begin contents-begin
                                 :contents-end contents-end)))))))))))))
(advice-add #'org-element--parse-generic-emphasis :override #'eli/org-element--parse-generic-emphasis)

这样就可以了

当然为了稳定可以使用零宽空格,虽然在 org 开发者内部对是否全面接受零宽空格还没有统一意见,不过零宽空格是在 [:space:] 范围内的,所以肯定没有兼容性问题。

4 个赞