在emacs-org-mode GNU列表上的集体建议使用org-entity
(C-h i g
(org) Special symbols
)\ast{}
而不是零宽度的空格字符(我在此处的其他答案中建议)后,得出了这个答案。
以下是一种通用解决方案,用户无需记住各种符号的组织实体。用户执行操作时,它将插入组织实体(如果有)C-u SYMBOL
;适用于C-u *
,C-u /
,C-u =
,等。
(defun modi/org-entity-get-name (char)
"Return the entity name for CHAR. For example, return \"ast\" for *."
(let ((ll (append org-entities-user
org-entities))
e name utf8)
(catch 'break
(while ll
(setq e (pop ll))
(when (not (stringp e))
(setq utf8 (nth 6 e))
(when (string= char utf8)
(setq name (car e))
(throw 'break name)))))))
(defun modi/org-insert-org-entity-maybe (&rest args)
"When the universal prefix C-u is used before entering any character,
insert the character's `org-entity' name if available.
If C-u prefix is not used and if `org-entity' name is not available, the
returned value `entity-name' will be nil."
;; It would be fine to use just (this-command-keys) instead of
;; (substring (this-command-keys) -1) below in emacs 25+.
;; But if the user pressed "C-u *", then
;; - in emacs 24.5, (this-command-keys) would return "^U*", and
;; - in emacs 25.x, (this-command-keys) would return "*".
;; But in both versions, (substring (this-command-keys) -1) will return
;; "*", which is what we want.
;; http://thread.gmane.org/gmane.emacs.orgmode/106974/focus=106996
(let ((pressed-key (substring (this-command-keys) -1))
entity-name)
(when (and (listp args) (eq 4 (car args)))
(setq entity-name (modi/org-entity-get-name pressed-key))
(when entity-name
(setq entity-name (concat "\\" entity-name "{}"))
(insert entity-name)
(message (concat "Inserted `org-entity' "
(propertize entity-name
'face 'font-lock-function-name-face)
" for the symbol "
(propertize pressed-key
'face 'font-lock-function-name-face)
"."))))
entity-name))
;; Run `org-self-insert-command' only if `modi/org-insert-org-entity-maybe'
;; returns nil.
(advice-add 'org-self-insert-command :before-until #'modi/org-insert-org-entity-maybe)
因此,当用户键入C-u *
,\ast{}
并在回显区域中显示以下消息时,
为符号*插入'org-entity'\ ast {}。
如果org-pretty-entities
在t
上方设置,插入的\ast{}
将显示为*
。
如果org-entity
字符X不存在,并且用户使用C-u
X,则X会被简单地插入4次(C-u
通常是这样)。
\ast{}shrug\ast{}
或\star{}shrug\star{}
,类似的方法,使用实体编码:a;
,或者如果在星号之前或之后添加空格也是可行的。