Repository navigation
Align markup with Emacs org-element - #11
Merged
seflue merged 12 commits intoOct 23, 2026
Merged
Conversation
Reference for feat/markup against Emacs org-element behaviour: - markup_delimiter_rules.tst: pre and post char, body border, headline disambiguation - markup_span_extent.tst: newline handling, paragraph break terminates - markup_composition.tst: markup with latex, math and literal spans
Two verbatim spans on one line each contain an unpaired $. The verbatim contents are restricted to expr, so the $ characters must not be paired into an inline_math_block crossing span boundaries.
Emacs org-element--parse-generic-emphasis requires the char before an
opening emphasis/verbatim/code delimiter to be whitespace, one of
- ( ' " {, or beginning-of-line. The scanner accepted any non-space
pre-char, so mid-word delimiters (foo*bar*) wrongly opened markup.
Only whitespace/BOL is checked, not the - ( ' " { subset: the external
scanner has no lookbehind into characters consumed by the ordinary
expr/str lexing, so those specific punctuation pre-chars can't be
distinguished from an ordinary word character. Whitespace/BOL is
derived from the indent-skip loop already in scan(), which does see
those characters directly.
Fixes DelimiterRules.1 in the markup conformance corpus.
org-element--parse-generic-emphasis accepts [ after a closing delimiter and forbids only whitespace at the border. The two tests asserted the correct behavior under titles calling it a divergence.
Emacs org-emphasis-alist and org-element define the equals sign as verbatim and the tilde as code. The scanner mapped them the other way round, so the equals span parsed as a code node and the tilde span as a verbatim node. Node names are public API for highlight and fold queries. The fix swaps the two delimiters in the scanner's delimiter to markup mapping and its inverse. The grammar rules, external token order and scanner enums keep their names and order, so the diff stays limited to the two mapping functions. Renaming every enum member and grammar rule would produce the same parse tree with a larger diff. Corpus expectations, test titles and sample text follow the swap.
Emacs org-emphasis-regexp-components lists the post set as - \t.,:!?;'")}\[ or end of line. tree-sitter-org enumerated the same punctuation but only space, newline and carriage return as whitespace, so a closing delimiter followed by a tab was rejected. The post predicate now tests whitespace with iswspace and the Emacs punctuation with strchr against a string constant. Delimiter chaining stays a separate term: it is not in the Emacs post set and remains a deliberate deviation.
Emacs org-emphasis-regexp-components splits emphasis into the fields pre, border, body, post and newlines. The scanner implements the same rules but named its predicates after content and marker, which hides the correspondence. The border and post predicates now carry the Emacs field names and a comment block maps all five fields to their places in the scanner. The border predicate drops the explicit newline and carriage return tests, both are covered by iswspace. No behaviour change.
The markup delimiter scan decided close, open and plain text in one function whose precondition (delimiter already consumed) was implicit, and it derived the delimiter char back from the markup kind although every caller already knows the char. Split it into a close step, an open guard and an open walk behind one entry point that states the precondition. The delimiter char is passed down instead of derived, which leaves the reverse lookup table unused, so it is removed. The line end test and the closer test are named predicates. No behaviour change.
Emacs restricts inline emphasis to a paragraph, the markup stack in this scanner is cleared at the beginning of every line instead. The comment on that clear now states why the stack goes stale at all: a closer swallowed by a link or math token is never requested as a close, so its entry would otherwise stay forever. The column was read three times per scan: once for the markup stack clear, once for the headline check and once inside the bracket branch, where a second variable shadowed the first. The column is now read once at the top and reused. The two later reads happened after the indent skip, so they are replaced by the combination of the initial read and a zero indent length, which is equivalent at both sites.
A [ can open a link, a footnote reference or a footnote definition. The scanner handled all three in one nested branch: a link attempt that found no closing ]] left the lexer at the end of the line and fell through into the footnote check, which then failed on the line end. Harmless, since tree-sitter discards the advance calls of a scan that returns false, but the control flow hid that the fall-through can never match. The branch is now a dispatcher that consumes the [ and hands over to one helper per token kind. The link helper returns false after its walk, so the footnote helper is only entered when the character after the [ is an f. The footnote label loop drops the explicit newline checks, iswspace already covers them. The column-zero test for the definition is passed in from the caller, which reads the column once.
Deserialize pushes one sentinel entry onto the indent, bullet and section stacks before it reads the buffer. Serialize therefore writes those counts as length minus one, while the markup stack has no sentinel and is written at full length. The asymmetry was unexplained in the code. Add a comment stating the rule. Change the loop counter in serialize to size_t so it matches deserialize and compares unsigned against the size_t counts instead of mixing signedness.
The external scanner decides whether a markup delimiter opens a span, closes one or is plain text. It only gets that chance while the delimiter is still unconsumed: once the internal lexer has emitted the character as a sym token, tree-sitter never asks the scanner about it. The expr helper therefore drops the delimiters from both its single character tokens and the sym class, and a separate low precedence token in the expr rule collects a lone delimiter the scanner rejected. Both places now carry a comment stating that rule. No grammar change, the generated parser is identical.
Author
|
@kristijanhusak I haven't heard feedback from you on this for a while. Any objections? Unfortunately I don't have the permissions to request a review from you in this repo. Would you mind giving me also merge permissions for this project too? To improve on nvim-orgmode, I think it is crucial for me to also work on the grammar. |
kristijanhusak
approved these changes
Oct 14, 2026
kristijanhusak
left a comment
Member
There was a problem hiding this comment.
I did some changes in the organization roles, so I think you should have permissions now.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Related: nvim-orgmode/orgmode#452. Review of
feat/markupagainst an Emacs conformance corpus, with the fixes and cleanups that came out of it. The corpus is the first commit; every fix has its test in the commit before it.The original bug is fixed
feat/markupfixes nvim-orgmode/orgmode#452 for both=...=and~...~. Their nodes restrict the contents toexpr, so the$math token is never offered inside them. I hit the old behaviour for real this week. snacks.nvim's org math preview took a bogusinline_math_blockspanning two~...~spans as LaTeX and tectonic crashed withMissing $ inserted. Withfeat/markupthe same line gives two clean spans. That line is a regression test in the corpus.Findings
Reference throughout is
org-element--parse-generic-emphasisinorg-element.el(Org 9.8.7), not the font-lock variable. No correctness problems beyond the four below.markup_stackpush/pop with the serialization budget, the headline versus bold decision, the checks around the delimiters all hold up.1. No check of the character before an opening delimiter (fixed here, partially)
foo*bar* bazparses as bold. Emacs requires the char before an opening delimiter to be whitespace, one of- ( ' " {, or the start of the line. With highlighting on,a*b*candFOO_BAR_light up.Fixed here for whitespace and start of line. The punctuation half needs lookbehind the external scanner does not have;
str/symconsume those characters before the scanner runs.(*bold*)and"*bold*"stay unrecognised. The full Emacs rule means lexing plain text in the external scanner with alast_char, as tree-sitter-markdown does. That touches all plain text lexing and serialization, so it is a PR of its own (first follow-up under Next steps).2.
codeandverbatimswapped (fixed here)=...=produced acodenode and~...~averbatimnode.org-emphasis-alistandorg-elementhave it the other way round. Node names are public API for everyhighlights.scm, so this has to be right beforefeat/markupmerges. The fix is the delimiter lookup in the scanner; rule and enum names were already correct.3. After a closing delimiter: a superset of Emacs (one addition here, one deviation kept)
After a closing delimiter the scanner accepts everything Emacs accepts, plus another markup delimiter, which Emacs does not.
*bold*/italic/gives bold and italic here and nothing in Emacs. I would keep that, it is the friendlier reading, and the predicate comment names it as a deviation. What was missing from the Emacs set is tab; added here.4. Single-line only (stricter than Emacs, second follow-up)
*bold across\nthe line*gives no bold. Emacs's parser has no newline limit; it searches for the closing delimiter up to the end of the paragraph. The "one newline" people quote is the font-lock limit fromorg-emphasis-regexp-components, not the syntax. A wrapped paragraph in/.../is valid Org, andfeat/markupis the only place stricter than Emacs. Three places enforce single-line at once: grammar, open lookahead, the stack clear at every line start. With plain text lexed in the external scanner the newline crossing is part of the same walk, so this comes after the lookbehind. The corpus test for it sits on a follow-up branch, not here.Commits
12 commits, in review order. They are independent of each other; leaving one out does not break the others:
code/verbatimmapping, whitespace or start of line before an opening delimiter, tab after a closing one. Each with its corpus test.[branch split into link and footnote helpers, this one touches pre-existing link code, leave it out if you prefer. Column read once perscan().Next steps
This PR first. It fixes nvim-orgmode/orgmode#452 and the mid-word case, which is most of what users run into, and it gets the markup nodes in front of them.
Then two follow-ups, which I could write:
last_char, so the rule for the character before an opening delimiter matches Emacs in full. Own PR, corpus run against it.