Skip to content

Align markup with Emacs org-element - #11

Merged
seflue merged 12 commits into
nvim-orgmode:feat/markupfrom
seflue:fix/markup-emacs-conformance
Oct 23, 2026
Merged

seflue merged 12 commits into
nvim-orgmode:feat/markupfrom
seflue:fix/markup-emacs-conformance

Conversation

@seflue

@seflue seflue commented Sep 20, 2026

Copy link
Copy Markdown

Related: nvim-orgmode/orgmode#452. Review of feat/markup against 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/markup fixes nvim-orgmode/orgmode#452 for both =...= and ~...~. Their nodes restrict the contents to expr, 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 bogus inline_math_block spanning two ~...~ spans as LaTeX and tectonic crashed with Missing $ inserted. With feat/markup the same line gives two clean spans. That line is a regression test in the corpus.

Findings

Reference throughout is org-element--parse-generic-emphasis in org-element.el (Org 9.8.7), not the font-lock variable. No correctness problems beyond the four below. markup_stack push/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* baz parses 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*c and FOO_BAR_ light up.

Fixed here for whitespace and start of line. The punctuation half needs lookbehind the external scanner does not have; str/sym consume those characters before the scanner runs. (*bold*) and "*bold*" stay unrecognised. The full Emacs rule means lexing plain text in the external scanner with a last_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. code and verbatim swapped (fixed here)

=...= produced a code node and ~...~ a verbatim node. org-emphasis-alist and org-element have it the other way round. Node names are public API for every highlights.scm, so this has to be right before feat/markup merges. 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 from org-emphasis-regexp-components, not the syntax. A wrapped paragraph in /.../ is valid Org, and feat/markup is 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:

  • Corpus: conformance tests for delimiter rules, span extent and composition with latex, math and literal spans. 228 tests, all green.
  • Fixes: code/verbatim mapping, whitespace or start of line before an opening delimiter, tab after a closing one. Each with its corpus test.
  • Refactors, no behaviour change: predicates named after the Emacs fields, with a comment mapping pre/border/body/post/newlines to the scanner. The delimiter scan split into close, open guard and open walk. The [ branch split into link and footnote helpers, this one touches pre-existing link code, leave it out if you prefer. Column read once per scan().
  • Comments: sentinel offsets in serialize, and why delimiters must stay out of the internal lexer.

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:

  1. Lookbehind. Plain text lexed in the external scanner with last_char, so the rule for the character before an opening delimiter matches Emacs in full. Own PR, corpus run against it.
  2. Paragraph-bounded spans, on top of 1.

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.
@seflue
seflue changed the base branch from main to feat/markup September 20, 2026 20:11
@seflue

seflue commented Oct 13, 2026 •

Copy link
Copy Markdown
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 kristijanhusak left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did some changes in the organization roles, so I think you should have permissions now.

@seflue
seflue merged commit 9dcb3ec into nvim-orgmode:feat/markup Oct 23, 2026
1 check passed
@seflue seflue mentioned this pull request Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improper syntax highlight for dollar sign $

2 participants