Yue Yi <[email protected]> writes: > After testing, I noticed that there are still some issues with markers > in Chinese text where there are no spaces. For example: > > 你好*世界*现在我有*冰淇凌*。 (Hello *world* now I have *ice cream*.) > > In this case, `世界' and `冰淇凌' are expected to be highlighted, but > `现在我有' is also being highlighted as part of the markup. I'm not sure > what the test results look like on your end. > > Of course, if you don't have suitable Chinese fonts installed, I hope > the following Japanese test case helps: > > あ*い*うえ*お* > > You can observe that everything except for the first character "あ" is > being highlighted. > > Another interesting phenomenon is that after applying the patch, the > combination `*+ab+' is rendered in bold style within the Org buffer, even > though the asterisk * is not paired.
Try the attached. This variant is slower (uses parser), but more accurate.
>From 098c8e1ad14a2c9f459a04cbcc3e8b69002903d6 Mon Sep 17 00:00:00 2001 Message-ID: <098c8e1ad14a2c9f459a04cbcc3e8b69002903d6.1766666303.git.yanta...@posteo.net> From: Ihor Radchenko <[email protected]> Date: Sat, 20 Dec 2025 11:58:16 +0100 Subject: [PATCH] WIP: Org markup: Allow Unicode punctuation and breakable symbols around emphasis * lisp/org-element.el (org-element-category-table): Define custom category table adding opening/closing punctuation, opening/closing quotes, dashes, and auxiliary punctuation. (org-element--parse-generic-emphasis): Extend allowed characters around emphasis to generic opening/closing punctuation, quote punctuation, dash-likes, and auxiliary ,-like punctuation. Also, allow breakable characters, like Chinese/Japanese symbols for languages that do not use spaces. * lisp/org.el (org-element-emphasis-pre-re): (org-element-emphasis-post-re): Add new regexps defining emphasis boundaries. * lisp/org.el (org-mode): Setup category table. (org-emphasis-regexp-components): Allow pre/post to be nil to follow the new defaults. Change the default values of pre/past to nil. (org-set-emph-re): (org-do-emphasis-faces): (org-emphasize): Fall back to parser defaults when pre/past in `org-emphasis-regexp-components' is nil. --- lisp/org-element.el | 37 ++++++++++++++++-- lisp/org.el | 91 ++++++++++++++++++++++++++++++--------------- 2 files changed, 96 insertions(+), 32 deletions(-) diff --git a/lisp/org-element.el b/lisp/org-element.el index 6abccd001..c22b68695 100644 --- a/lisp/org-element.el +++ b/lisp/org-element.el @@ -3318,6 +3318,38 @@ ;;; Objects ;;;; Bold +(defvar org-element-category-table + (let ((category-table (copy-category-table)) + (uniprop-table (unicode-property-table-internal 'general-category))) + ;; Define categories + (define-category ?{ "Opening punctuation" category-table) + (define-category ?} "Closing punctuation" category-table) + (define-category ?\[ "Initial quote" category-table) + (define-category ?\] "Final quote" category-table) + (define-category ?- "Dash" category-table) + (define-category ?, "Other punctuation" category-table) + ;; Map characters to categories according to their general-category + (map-char-table + (lambda (key val) + (pcase val + ('Ps (modify-category-entry key ?{ category-table)) + ('Pe (modify-category-entry key ?} category-table)) + ('Pi (modify-category-entry key ?\[ category-table)) + ('Pf (modify-category-entry key ?\] category-table)) + ('Pd (modify-category-entry key ?- category-table)) + ('Po (modify-category-entry key ?, category-table)))) + uniprop-table) + category-table) + "Category table for Org buffers. +The table defines additional Unicode categories: +- ?{ for opening punctuation +- ?} for closing punctuation +- ?[ for opening quote +- ?] for closing quote +- ?- for dash-like +- ?, for other punctuation. +These categories are necessary for parsing emphasis.") + (defun org-element--parse-generic-emphasis (mark type) "Parse emphasis object at point, if any. @@ -3331,7 +3363,7 @@ (defun org-element--parse-generic-emphasis (mark type) (unless (bolp) (forward-char -1)) (let ((opening-re (rx-to-string - `(seq (or line-start (any space ?- ?\( ?' ?\" ?\{)) + `(seq (regexp ,org-element-emphasis-pre-re) ,mark (not space))))) (when (looking-at-p opening-re) @@ -3341,8 +3373,7 @@ (defun org-element--parse-generic-emphasis (mark type) `(seq (not space) (group ,mark) - (or (any space ?- ?. ?, ?\; ?: ?! ?? ?' ?\" ?\) ?\} ?\\ ?\[) - line-end))))) + (regexp ,org-element-emphasis-post-re))))) (when (re-search-forward closing-re nil t) (let ((closing (match-end 1))) (goto-char closing) diff --git a/lisp/org.el b/lisp/org.el index 67d9679fe..04d2bf210 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -3823,6 +3823,31 @@ (defcustom org-pretty-entities-include-sub-superscripts t :version "24.1" :type 'boolean) +(defconst org-element-emphasis-pre-re + (rx (or line-start space + ;; opening punctuation + (category ?{) (category ?\[) + ;; dashes, other punctuation + (category ?-) (category ?,) + ;; Chinese, Japanese, and other breakable + ;; characters + (category ?|))) + "Regexp matching character before opening emphasis marker. +Assumes that `org-element-category-table' is active.") + +(defconst org-element-emphasis-post-re + (rx (or space + ;; closing punctuation + (category ?}) (category ?\]) + ;; dashes, other punctuation + (category ?-) (category ?,) + ;; Chinese, Japanese, and other breakable + ;; characters + (category ?|) + line-end)) + "Regexp matching character after opening emphasis marker. +Assumes that `org-element-category-table' is active.") + (defvar org-emph-re nil "Regular expression for matching emphasis. After a match, the match groups contain these elements: @@ -3850,10 +3875,13 @@ (defun org-set-emph-re (var val) (body (if (<= nl 0) body (format "%s*?\\(?:\n%s*?\\)\\{0,%d\\}" body body nl))) (template - (format (concat "\\([%s]\\|^\\)" ;before markers + ;; See `org-element--parse-generic-emphasis' + (format (concat "\\(%s\\)" ;before markers "\\(\\([%%s]\\)\\([^%s]\\|[^%s]%s[^%s]\\)\\3\\)" "\\([%s]\\|$\\)") ;after markers - pre border border body border post))) + (if pre (format "[%s]\\|^" pre) org-element-emphasis-pre-re) + border border body border + (if post (format "[%s]\\|$" post) org-element-emphasis-post-re)))) (setq org-emph-re (format template "*/_+")) (setq org-verbatim-re (format template "=~"))))) @@ -3861,7 +3889,7 @@ (defun org-set-emph-re (var val) ;; set this option proved cumbersome. See this message/thread: ;; https://orgmode.org/list/[email protected] (defvar org-emphasis-regexp-components - '("-[:space:]('\"{" "-[:space:].,:!?;'\")}\\[" "[:space:]" "." 1) + '(nil nil "[:space:]" "." 1) "Components used to build the regular expression for FONTIFYING emphasis. WARNING: This variable only affects visual fontification, but does not change Org markup. For example, it does not affect how emphasis markup @@ -3874,7 +3902,9 @@ (defvar org-emphasis-regexp-components specify what is allowed/forbidden in each part: pre Chars allowed as prematch. Beginning of line will be allowed too. + nil means use parser defaults. post Chars allowed as postmatch. End of line will be allowed too. + nil means use parser defaults. border The chars *forbidden* as border characters. body-regexp A regexp like \".\" to match a body character. Don't use non-shy groups here, and don't allow newline here. @@ -5106,6 +5136,9 @@ (define-derived-mode org-mode outline-mode "Org" (org-install-agenda-files-menu)) (setq-local outline-regexp org-outline-regexp) (setq-local outline-level 'org-outline-level) + (require 'org-element) + (defvar org-element-category-table) ; org-element.el + (set-category-table org-element-category-table) ;; Initialize cache. (org-element-cache-reset) (when (and org-element-cache-persistent @@ -5385,35 +5418,27 @@ (defsubst org-rear-nonsticky-at (pos) (defun org-do-emphasis-faces (limit) "Run through the buffer and emphasize strings." - (let ((quick-re (format "\\([%s]\\|^\\)\\([~=*/_+]\\)" - (car org-emphasis-regexp-components)))) + (let ((quick-re (format "\\(%s\\)\\([~=*/_+]\\)" + (if (car org-emphasis-regexp-components) + (format "[%s]\\|^" (car org-emphasis-regexp-components)) + org-element-emphasis-pre-re)))) (catch :exit (while (re-search-forward quick-re limit t) (let* ((marker (match-string 2)) - (verbatim? (member marker '("~" "=")))) + (verbatim? (member marker '("~" "="))) + (context (save-match-data + (save-excursion + (goto-char (match-beginning 2)) + (org-element-context))))) (when (save-excursion (goto-char (match-beginning 0)) (and - ;; Do not match table hlines. - (not (and (equal marker "+") - (org-match-line - "[ \t]*\\(|[-+]+|?\\|\\+[-+]+\\+\\)[ \t]*$"))) - ;; Do not match headline stars. Do not consider - ;; stars of a headline as closing marker for bold - ;; markup either. - (not (and (equal marker "*") - (save-excursion - (forward-char) - (skip-chars-backward "*") - (looking-at-p org-outline-regexp-bol)))) + (org-element-type-p + context + '(bold code italic strike-through underline verbatim)) + (equal (match-beginning 2) (org-element-begin context)) ;; Match full emphasis markup regexp. - (looking-at (if verbatim? org-verbatim-re org-emph-re)) - ;; Do not span over paragraph boundaries. - (not (string-match-p org-element-paragraph-separate - (match-string 2))) - ;; Do not span over cells in table rows. - (not (and (save-match-data (org-match-line "[ \t]*|")) - (string-match-p "|" (match-string 4)))))) + (looking-at (if verbatim? org-verbatim-re org-emph-re)))) (pcase-let ((`(,_ ,face ,_) (assoc marker org-emphasis-alist)) (m (if org-hide-emphasis-markers 4 2))) (font-lock-prepend-text-property @@ -5478,12 +5503,20 @@ (defun org-emphasize (&optional char) (setq string (concat s string s)) (when beg (delete-region beg end)) (unless (or (bolp) - (string-match (concat "[" (nth 0 erc) "\n]") - (char-to-string (char-before (point))))) + (string-match + (if (nth 0 erc) (concat "[" (nth 0 erc) "\n]") + ;; See `org-element--parse-generic-emphasis' + (rx (or (regexp org-element-emphasis-pre-re) + "\n"))) + (char-to-string (char-before (point))))) (insert " ")) (unless (or (eobp) - (string-match (concat "[" (nth 1 erc) "\n]") - (char-to-string (char-after (point))))) + (string-match + ;; See `org-element--parse-generic-emphasis' + (if (nth 1 erc) (concat "[" (nth 1 erc) "\n]") + (rx (or (regexp org-element-emphasis-post-re) + "\n"))) + (char-to-string (char-after (point))))) (insert " ") (backward-char 1)) (insert string) (and move (backward-char 1)))) -- 2.52.0
-- Ihor Radchenko // yantar92, Org mode maintainer, Learn more about Org mode at <https://orgmode.org/>. Support Org development at <https://liberapay.com/org-mode>, or support my work at <https://liberapay.com/yantar92>
