I got really interested in org-entities (to deal with the case I mentioned in the first email in this thread like \ast{}shrug\ast{}) and came up with this:
(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 (orig-fun &rest args)
"When the universal prefix C-u is used before entering any character,
insert the character's `org-entity' name if available."
(let ((pressed-key (this-command-keys))
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)
"."))))
(when (null entity-name)
(apply orig-fun args))))
(advice-add 'org-self-insert-command :around #'modi/org-insert-org-entity-maybe)
=====
After evaluating the above, whenever you do C-u *, C-u /, C-u =, etc, that symbol's org-entity will be inserted (if available in either org-entities-user or org-entities).
If an org-entity match is not found, that symbol will be inserted 4 times, as C-u would usually do.
The message tells the user if the org-entity got inserted so that there is not confusion.
The advised portion gets executed only if the below 2 conditions match:
- User used C-u prefix (not M-4 or C-u C-u or anything else)
- The entered character has a match in the org entities lists.
Question to the list is: Does this advise mask any useful functionality of org-self-insert-command?