快速提出我的问题:列表中org-emphasis-regexp-components
条目是否有一些不成文的要求,特别是前两个条目pre
和post
(分别列出了强调文本之前和强调文本之后允许的字符)?
我以前看过关于这个列表的讨论,特别是在 https://emacs.stackexchange.com/a/54715, 但我似乎遇到了某种未记录的边缘情况,阻止我的编辑起作用。
现在,我的具体情况。我正在尝试找到一种易于阅读的方式来允许
- 空格、左分隔符和强调文本前的短划线,以及
- 强调文本后的空格、右分隔符、破折号和标点。
为此,我设定了
"[:space:]({[“-–—"
pre
和"[:space:])}]”-–—.?!,;:"
post
, 以我认为是可读的方式(见下文)。
使用这些设置时,在强调的文本之前放置空格、左括号(
、方括号[
或大括号{
不会干扰强调。但是放置任何其他角色都可以。我希望左双引号“
、破折号-
、endash–
和 emdash—
应该有效,因为它们包含在pre
中,但它们没有。
将任何字符放在强调的文本之后都会破坏强调。
这是设置值的实际代码。
(setcar org-emphasis-regexp-components
(concat
"[:space:]"
(string
;; Opening delimiters; the comments prevent match-parens from getting mad .
?( ;;)
?{ ;;}
?[ ;;]
?“ ;;”
;; Dashes
?- ?– ?—)))
(setcar (nthcdr 1 org-emphasis-regexp-components)
(concat
"[:space:]"
(string
;; Closing delimiters, with matching comments as above.
;;(
?) ;;{
?} ;;[
?] ;;“
?”
;; Dashes
?- ?– ?—
;; Punctuation
?. ?? ?! ?, ?; ?:)))
作为参考,这里是org-emphasis-regexp-components
的文档。它没有提到定义pre
和post
所需的任何布局。
org-emphasis-regexp-components is a variable defined in ‘org.el’.
Documentation:
Components used to build the regular expression for emphasis.
This is a list with five entries. Terminology: In an emphasis string
like " *strong word* ", we call the initial space PREMATCH, the final
space POSTMATCH, the stars MARKERS, "s" and "d" are BORDER characters
and "trong wor" is the body. The different components in this variable
specify what is allowed/forbidden in each part:
pre Chars allowed as prematch. Beginning of line will be allowed too.
post Chars allowed as postmatch. End of line will be allowed too.
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.
newline The maximum number of newlines allowed in an emphasis exp.
我错过了什么导致它不起作用?
写完这个问题后,我立即做了我应该做的事情,并尝试在pre
和post
中添加更少的字符。你瞧,是[
、]
和-
条目导致了问题。
仔细观察前面对org-emphasis-regexp-components
(https://emacs.stackexchange.com/a/54715)的讨论,这些字符串以某种方式组合成一个庞大而复杂的正则表达式。而且,错误的括号和破折号似乎可能会被误认为是该正则表达式中的特殊字符,从而导致问题。
根据 NickD 的评论,实际上需要的是遵循正则表达式的约定,因此
- 如果包含短划线
-
,它必须是字符串的第一个或最后一个元素,并且 - 如果包含右大括号
]
,则它必须是字符串的第一个元素。
完成所有操作后,以下是问题中字符集的适当pre
和post
值。
pre
:"[:space:]({“[—–-"
post
:"][:space:])}”.?!,;:–—-"
以及(我相信相当可读的)代码来生成这些字符串。
(setcar org-emphasis-regexp-components
(concat
;; All whitespace characters.
"[:space:]"
(string
;; Opening delimiters; the comments prevent check-parens from getting mad .
?( ;;)
?{ ;;}
?“ ;;”
?[ ;]
;; Dashes
?— ?– ?-))) ;; Do not move the dash. It will break the regexp.
(setcar (nthcdr 1 org-emphasis-regexp-components)
(concat
(string ;[
?]) ;; Do not move the bracket. It will break the regexp.
;; All whitespace characters.
"[:space:]"
(string
;; Closing delimiters, with matching comments as above.
;;(
?) ;;{
?} ;;“
?”
;; Punctuation
?. ?? ?! ?, ?; ?:
;; Dashes
?– ?— ?-))) ;; Do not move the dash. It will break the regexp.