org-special-block-extras: Make your own blocks and links

Ξ
- 1. Make your own blocks with
org-defblock - 2. Make your own links with
org-deflink - 3. Tags are functions of Org sections:
org-deftag - 4.
C-c C-cfor your own blocks - 5. Your own Babel: a block, and its
C-c C-c - 6.
C-c 'to edit a block in its own buffer - 7.
C-c C-v C-dto split a block in two - 8.
C-c C-v C-jto insert a block's argument - 9. Hover for a block's arguments:
eldoc
⭐ The code lives at github.com/alhassy/org-special-block-extras —star it, or discuss it there!
Every block and link above is made with the very tools below, so a new one of our own is a
defun away.
1. Make your own blocks with org-defblock
We declare a block just as we declare a function with cl-defun: the block's main
argument —written right after #+begin_NAME— is its &optional argument, and its
header arguments are its &key arguments. After &context come the names the body
needs of the export: backend, the current export backend, and contents, the block's
Org-parsed contents; there are also raw-contents, args, link?, and anchor. The
body sees only what it so asks for, so none of ours shadows a variable of its own; a name
already taken may be had under another, as in &context ((:contents text)). See the
docstring of org-defblock for the lot, and why we prefer this to anaphora.
(org-defblock note (&optional (title "Note") &key (color "teal") &context backend contents) "Call out CONTENTS under TITLE, tinted COLOR." (pcase backend ('html (format "<div style=\"border-left: 4px solid %s; padding-left: 1em\"><b>%s</b>%s</div>" color (org-inline title) contents)) (_ (format "\\textbf{\\color{%s}%s} %s" color (org-inline title) contents))))
Now the following binds title to "Heads up" and color to red; omit either, and
its default is used. The main argument may also be given by its name, as a keyword:
#+begin_note :title "Heads up" is the same block.
#+begin_note "Heads up" :color red Mind the gap. #+end_note
As with cl-defun, a header argument the block does not declare —say, a misspelt
:colour red— is an export-time error, unless the arglist writes &allow-other-keys, as
the package's own blocks do; so, () takes no arguments at all.
For any backend derived from HTML or LaTeX —org-reveal's, ox-hugo's, ox-gfm's, beamer's, or
our own— backend is html or latex, so one rendering serves them all. To single one out,
we ask Org's own org-export-current-backend; by way of example, a docsy alert in Hugo:
(org-defblock noteblock (&optional (title "Note") &key (color "primary") &context backend raw-contents contents) "A note: a docsy alert in Hugo, a noteblock environment in LaTeX, a box elsewhere in HTML." (cond ((org-export-derived-backend-p org-export-current-backend 'hugo) (format "{{%% alert title=\"%s\" color=\"%s\" %%}}\n%s\n{{%% /alert %%}}" title color raw-contents)) ((equal backend 'latex) (format "\\begin{noteblock}\n%s\n\\end{noteblock}" contents)) (t (format "<div class=\"note\">%s</div>" contents))))
In similar spirit, (org-export-markdown-p) and (org-export-beamer-p) single out Markdown and
beamer.
Every block is also a link, with the label as main argument and the description as
contents: [[note:Heads up][Mind the gap.]].
Our markup works in every block, for free: contents is Org, exported as prose is, so red:fire,
[[tooltip:x][a tip]], $\alpha$, [[https://orgmode.org][Org]], [[file:notes.org][notes]],
[[*Heading]], [[target]], and <<target>> all render within it. An argument is a different matter:
only the block knows whether its title is prose, or whether its color is a color's name; so we
hand a prose argument to (org-inline title), as the note above does, and its markup renders too
—links to the document's sections and targets included, and its own <<target>> joins the document,
so a [[target]] elsewhere reaches the block. The package's own titles all do so.
Wherever the output is no prose, the same links mean the same things: in a math block, a proof tree,
and inline $…$, red:x is {\color{red}x} and [[tooltip:x][a tip]] hovers; in a graph, red:x
colors its node's label, a tooltip: is the node's tooltip, a web link its URL, and $\lambda_{x}$
shows as λ subscripted x. Each link says what it means once, in org-markup-links; a link of our own
joins that list to reach maths and graphs alike.
A block names what its document's head needs, added once, by :latex-head and :html-head; say,
(org-defblock thm (&optional title :latex-head ("amsthm" "\\newtheorem{theorem}{Theorem}")) …). A
LaTeX item is a package name or any preamble line; an HTML item is HTML, a symbol holding it, or
mathjax. An export that uses the block adds each item its head lacks; one that does not, adds none.
The lists go unquoted; e.g., :html-head (mathjax my-html-head "<style>.aside {color: gray}</style>").
Name a package, "amsthm", rather than write "\\usepackage{amsthm}": a name is left be when the
document loads it already, options and all —say, \usepackage[thmmarks]{amsthm}— whereas a line
is left be only when the document has that very line; else the package loads twice, and differing
options clash.
Even Org's own blocks ---quote, verse, center, example, src, export, and comment—
are fair game: an (org-defblock verse …) overrides every #+begin_verse, saying so in
*Messages*. To leave some to Org, a handler hands them back; by way of example, a verse
signed by its poet, else Org's own:
(org-defblock verse (&optional poet &context raw-contents contents) "A verse, signed by POET; sans one, Org's own verse." (if (org-string-nw-p poet) (format "\\begin{verse}\n%s\n\\end{verse}\n--- %s" contents poet) (org-parse (org-builtin-block "verse" raw-contents))))
Our own quote does just this: given a citation, it is ours; else, Org's own. Moreover, defaults may change long after the
block is defined, just as a src block's header-args do —there is nothing new to learn. A
NAME block reads them as a src block of language NAME would; from weakest to strongest:
(setq org-babel-default-header-args:note '((:color . "orange"))), then
#+property: header-args:note :color orange for the whole file, then :header-args:note: in
a heading's drawer for its subtree ---:header-args:note+: adds to it— then #+header:
lines just above the block, and finally the block's own line. By way of example, every note
in this section is orange, and the second is boxed too:
* Exercises :PROPERTIES: :header-args:note: :color orange :END: #+begin_note Hint Try induction. #+end_note #+header: :boxed t #+begin_note Answer By induction on n. #+end_note
A main argument is given by its name, as in :title Hint; and switches, such as :let and
:folded, are inherited alike —a src block of language LANG even takes them from Babel's
own header-args:LANG. See C-h f org--block-header-args.
One definition gives three uses: a block, a link, and a tag. A section tagged :note:
exports as a note block around its own text, up to the next heading; a main argument
named title is the heading's title, and the section's properties supply keyword arguments
by name. So all three of these agree:
#+begin_note "Heads up" :color red Mind the gap. #+end_note [[note:Heads up][Mind the gap.]] * Heads up :note: :PROPERTIES: :COLOR: red :END: Mind the gap.
The main argument may come from elsewhere, strongest first: the tag's underscore-separated items, as
in :details_Spoiler_alert: —tags cannot hold spaces— then a property of the argument's own name,
such as :TITLE:, and lastly the heading. A main argument that is no title, such as parallel's
column count, keeps its default.
Likewise, the package's own blocks are links and tags too; e.g., * Hint :details: folds a
section's text away under the title Hint. Try tagging a heading :red: —or :blue:,
:teal:, … any of the 19 colors— and its text exports in that color.
Should we not want all three, the options :link nil and :tag nil decline the link and
the tag, whereas :block nil keeps only the link and tag —e.g.,
(org-defblock note (&optional (title "Note") :tag nil) …). The variable
org-defblock-links-and-tags sets whether blocks come with links and tags by default; its
documentation weighs the trade-off —chiefly namespacing: every block claims the link type
NAME: and the tag :NAME:. So, should :red: already mean something else in our
headings, we set org-defblock-links-and-tags to nil before loading this package. For just a
link, or just a tag, there are org-deflink and org-deftag, below.
A tag wraps only its section's own text, since a block cannot hold a heading. By way of
example, in * Abstract :abstract: with a ** Aside beneath it, the aside stays outside
the box. Declaring :tag [:subsections MODE] picks what becomes of subsections:
ignore—the default, as with:tag t— leaves them outside the block;errorrefuses a tagged section that has any, naming both headings;recursewraps each subsection's own text in a block of its own, with the same arguments, unless the subsection carries the tag itself; andcontentshands the whole subtree, subsections and all, to the block as its contents; no block is written, the handler reading the subtree itself. A :graph: section's subheadings are so its nodes.
A block may also say how it looks in our Org buffers, via the option :fontify: below,
#+begin_proof shows as Proof., #+end_proof as ⏹, and the contents in italics too.
Each part is optional; the text itself is untouched, only displayed differently.
(org-defblock proof (&optional title :fontify [:begin "Proof." :end "⏹" :face 'italic]) …)
Moreover, :keywords shows patterns within the contents in a face of their choosing; e.g.,
a math block shows each blue:<<Fold>> as a tag, in blue, by
:fontify [:keywords '((org--math-match-tag 0 org--math-tag-face))]
where a REGEXP that is a function is a font-lock matcher, and a face that is a function is called, with the match data at hand, for the face.
The full story is at C-h o org-defblock.
2. Make your own links with org-deflink
An Org link [[NAME:label][description]] is a function of its label and description;
so, we declare one as we would a cl-defun, then say how it exports and how it looks
within Emacs.
(org-deflink shout (label &optional description backend) "Shout the description, else the label." :face '(:weight bold :foreground "red") :help-echo (format "I'll shout %s" (or description label)) :export (format (if (eq backend 'latex) "\\textbf{%s}" "<b>%s</b>") (upcase (or description label))))
Then shout:hello and [[shout:hello][hey there]] export as HELLO and HEY THERE.
The arglist names the label, description, and export backend, in that order; any may be
omitted from the end —a link that needs none has the arglist ()— and (description "…")
gives a default. Besides :export, the
keys :face, :help-echo, :follow, :keymap, :display and :let customise the link
within Emacs; C-h o org-deflink has the details.
4. C-c C-c for your own blocks
Org's C-c C-c does the obvious thing at point: it runs a src block, ticks a checkbox,
recalculates a table. Our special blocks may join in; one org-def-action gives every
#+begin_NAME … #+end_NAME block a C-c C-c action, as one org-defblock gives it an export.
For a single block, the :C-c_C-c switch suffices: #+begin_quiz :C-c_C-c my/check-answers
makes C-c C-c within that block call my/check-answers; a Lisp form, rather than a
function name, is evaluated instead. For every block of a kind, we define an action:
(org-defblock shout (&context contents) "Upcase the contents." (upcase contents)) (org-def-action shout (&optional greeting &key (volume 1)) "Announce the shout's GREETING, at VOLUME." (message "%s, at %s: %s" greeting volume raw-contents))
Then C-c C-c within #+begin_shout Hello :volume 11 … #+end_shout says Hello, at 11: ….
The arglist is the block's, in either of org-defblock's styles; the body sees the
arguments by name —those the block inherits, by header-args:shout, too— and the
block's text as raw-contents, with point within the block. A non-nil value claims the key
press, so Org does nothing else with it; nil declines it, leaving it to the block's
:C-c_C-c switch, if any.
For a complete worked example, we may mimic the graphRender an Org list + tables as a Graphviz dot graph.
TITLE is the graph’s label.
The header’s other keys are graph attributes, such as :bgcolor white; :node.KEY and :edge.KEY set node and edge defaults;
:direction right makes arcs flow rightwards ---else up, left, or down, the default---; #+edge: :undirected: makes
arcs arrowless, an undirected graph; :wrap N fills labels to N characters a line; :unflatten N staggers
wide rows of leaves over N ranks; and an unknown tag gets a made-up template, unless :unknown-tags error makes it
an error. Pass :file foo.png on the
#+begin_graph line to fix the output filename; else a #+name: foo line above the block makes it foo.png, and the graph
a figure that [[foo]] reaches and #+caption: captions; otherwise the name is graph-HASH.png, HASH being that of the
dot source, so re-exporting an unchanged graph rewrites one file rather than adding another.
For HTML export: embeds an inline <svg> element, whose nodes’ and arcs’ tooltips hover as prose’s do, by tooltips.js;
it fits the page’s width, a click toggling its full size.
For Markdown, it writes the image file and links it, as for a named graph.
For LaTeX, dot writes a PDF beside the .tex, which \includegraphics shows, shrunk only to fit the line’s width;
for other backends, or sans dot, it emits a #+begin_src dot block. In HTML, a node’s [[*Section]] link leads to that section.
For a fixed picture of the list ---a planet amid its satellites, a flow, a bubble--- LaTeX’s smartdiagram typesets it
instead, by #+begin_smartdiagram TITLE :type TYPE; see ‘org--smartdiagram-block’.
Within the block body:
#+graph: table ↦ global graph[ ] attributes
#+node: table ↦ default node[ ] attributes
#+edge: table ↦ default edge[ ] attributes
Under an item, #+graph:, #+node:, and #+edge: reach its whole subtree, as Org’s inherited properties do.
:NAME: … :END: ↦ a drawer of any name, folding such directives away
- :T: :: MEANING ↦ at the top, a legend entry: a sample of template T, captioned MEANING
A #+name: table outside any graph is a template every graph in the buffer may use; the graph’s own wins.
A section tagged :graph: is a graph of its text and subsections, its :PROPERTIES: drawer serving as the header line:
each subheading is an item, its title the item’s text, its drawer’s :KEY: v the item’s | key | v | rows, its body
paragraphs notes, its ORDERED property 1. 2. children, and a TODO keyword its checkbox; a list in its body is more
children. C-c C-c in the section’s body renders it; see ‘org--graph-read-heading’ and ‘org-graph--section-cc’.
Each list item declares a node. Nesting encodes edges (parent → child); ordered children, 1. 2. 3., share a rank
in the order written. An item’s text may hold a <<target>>, its id; [[TARGET]] or [[TARGET][LABEL]] links, extra arcs;
and *bold*, /italic/, _underline_, +strike+, =code=, or ~pre~, a braced x^{2} or H_{2}O, or a @@dot:…@@ snippet,
which make its label an HTML-like one; an entity, \alpha, is its character. A <<<radio target>>> is a node, every
item mentioning it having an arc to it. Other Org syntax in a block, such as a headline or #+begin_quote, is an error.
A description tag, ‘+ L :T: :: CHILD’, labels the arc into CHILD, its tags styling it; so does a link’s description,
[[TARGET][L :T:]]; trailing ‘:T:’ tags splice #+name: templates; trailing ‘:@X:’ tags align: ‘:@start:’ sets the node
on the first rank, rank=min; ‘:@end:’, on the last, rank=max; any other, on one rank with every node sharing it;
an inline footnote, [fn::NOTE], is the node’s tooltip, as is a note: a paragraph past the item’s first, or a
#+begin_note block ---with ‘#+begin_note :edge’, the arc’s---, shown beneath the label instead under :notes inline;
a web, file, or [[*Section]] link, its URL; an [[file:IMAGE.png]], its image;
a checkbox, [X], [-], or [ ], applies the template checked, partial, or unchecked, when one exists; and a ‘\\’
breaks the label’s line ---in a table value or record cell, too.
Org’s {{{macros}}} expand, by the buffer’s #+macro: lines.
A two-column inline table on a list item supplies node attributes.
| key | val | ↦ a Graphviz attribute of the node; values are quoted for you.
| :edge.KEY | val | ↦ attribute of the arc from the parent; a parallel arc is a second child item.
| :edge[T].KEY | val | ↦ attribute of this item’s arc to T, from a [[T]] link; "rec::port" reaches a record’s port.
A child item that is a lone [[T]] link is a reference to T: an arc to it, styled by the child’s :edge.KEY rows.
| shape | cluster | ↦ make this node an inline subgraph cluster_ID { … }, its children its members; as does the
:container: tag; a :container_TITLE: tag puts a node in the box TITLE.
| any | other | table | ↦ a record: the node’s <TABLE> label, as laid out; a cell’s <<p>> is port p, its [[T]] an
arc from there; ^ or an empty cell spans, a <l>/<c>/<r> row aligns, and hlines rule. A
table is a record unless each row is two cells, its key an attribute or :edge.ATTR.
Example:
#+begin_graph :direction right
+ A
+ calls :: B
+ returns :: C
#+end_graph
Regarding the Lisp function:
+ BACKEND refers to the current export backend.
+ RAW-CONTENTS refers to the text as the user wrote it, sans its least indentation.
⇒ Its body asks, by &context, for CONTENTS: the ‘org parsed’ version of user text.
⇒ CONTENTS and RAW-CONTENTS are identical for the description of a link; whence the LINK? of &context is non-nil.
For example, upon LaTeX export, the Org special block
#+begin_graph
Hello, world
#+end_graph
is rewritten to the result of the call
(org--graph-block ‘latex "Hello, world") block's handler,
org-graph–cc-handler. It
- finds its block via
(re-search-backward "^[ \t]*#\\+begin_graph\\b")then(org-special-block-after-point "graph"); - reads the block's keyword arguments, so
#+begin_graph :C-c_C-c dotchanges whatC-c C-cdoes; - honours
C-uviacurrent-prefix-arg—showing the dot source rather than rendering a PNG; - inserts a
#+RESULTS:section after#+end_graph, just as src blocks do; and - returns
t, claiming the keypress.
It is the graph's action, at the end of org-graph-block.el:
(org-def-action graph () "Render the graph at point, or show its dot source; see `org-graph--cc-handler'." (org-graph--cc-handler))
The dispatcher itself —a single function on org-ctrl-c-ctrl-c-hook— lives in
org-ctrl-c-ctrl-c-for-special-block.el.
5. Your own Babel: a block, and its C-c C-c
Each ob-LANG package teaches Babel to run a language: C-c C-c evaluates the block and
writes its #+RESULTS: below; export shows the code, its results, or both. We have both
halves already: org-defblock says what a block exports to, and org-def-action what
C-c C-c does within it. By way of example, a calc block whose every line is maths for
Emacs's own calculator:
(org-defblock calc (&context raw-contents) "Each line of maths, beside its value by Emacs's calc." (org-parse (format "#+begin_example\n%s\n#+end_example" (mapconcat (lambda (line) (format "%s = %s" line (calc-eval line))) (split-string raw-contents "\n" t) "\n")))) (org-def-action calc () "Write each line's value below the block, as Babel writes a src block's results." (let ((results (mapconcat (lambda (line) (concat ": " (calc-eval line))) (split-string raw-contents "\n" t) "\n"))) (re-search-forward "^[ \t]*#\\+end_calc") (forward-line) (when (looking-at-p "[ \t]*#\\+RESULTS:") (delete-region (point) (org-element-property :end (org-element-at-point)))) (insert "#+RESULTS:\n" results "\n") t))
Then C-c C-c within
#+begin_calc 2^10 sqrt(2) #+end_calc
writes : 1024 and : 1.41421356237 below it; export shows each line beside its value.
That is the whole of an ob-calc: a language's evaluator is a function of the block's
text, so the block's arguments are its header arguments ---#+begin_calc :precision 3—
inherited by :header-args:calc: as ever, and every switch, such as :folded or :tags,
applies for free. For a single block, rather than a kind, the :C-c_C-c switch suffices;
and :log t shows what a block exports to, when the export half is the one in doubt.
6. C-c ' to edit a block in its own buffer
Just as C-c ' edits a src block in a buffer of its language, so too it edits every special
block —ours, or any #+begin_𝒳 at all— in an Org buffer; C-c ' again writes the contents
back. Org keeps all it already does: a table, a link, or a src block within a special block is
edited as ever.
A block may instead choose its buffer's mode: the option :edit-indirect-mode MODE makes
C-c ' open the block's contents in a buffer in MODE. By way of example, the graphRender an Org list + tables as a Graphviz dot graph.
TITLE is the graph’s label.
The header’s other keys are graph attributes, such as :bgcolor white; :node.KEY and :edge.KEY set node and edge defaults;
:direction right makes arcs flow rightwards ---else up, left, or down, the default---; #+edge: :undirected: makes
arcs arrowless, an undirected graph; :wrap N fills labels to N characters a line; :unflatten N staggers
wide rows of leaves over N ranks; and an unknown tag gets a made-up template, unless :unknown-tags error makes it
an error. Pass :file foo.png on the
#+begin_graph line to fix the output filename; else a #+name: foo line above the block makes it foo.png, and the graph
a figure that [[foo]] reaches and #+caption: captions; otherwise the name is graph-HASH.png, HASH being that of the
dot source, so re-exporting an unchanged graph rewrites one file rather than adding another.
For HTML export: embeds an inline <svg> element, whose nodes’ and arcs’ tooltips hover as prose’s do, by tooltips.js;
it fits the page’s width, a click toggling its full size.
For Markdown, it writes the image file and links it, as for a named graph.
For LaTeX, dot writes a PDF beside the .tex, which \includegraphics shows, shrunk only to fit the line’s width;
for other backends, or sans dot, it emits a #+begin_src dot block. In HTML, a node’s [[*Section]] link leads to that section.
For a fixed picture of the list ---a planet amid its satellites, a flow, a bubble--- LaTeX’s smartdiagram typesets it
instead, by #+begin_smartdiagram TITLE :type TYPE; see ‘org--smartdiagram-block’.
Within the block body:
#+graph: table ↦ global graph[ ] attributes
#+node: table ↦ default node[ ] attributes
#+edge: table ↦ default edge[ ] attributes
Under an item, #+graph:, #+node:, and #+edge: reach its whole subtree, as Org’s inherited properties do.
:NAME: … :END: ↦ a drawer of any name, folding such directives away
- :T: :: MEANING ↦ at the top, a legend entry: a sample of template T, captioned MEANING
A #+name: table outside any graph is a template every graph in the buffer may use; the graph’s own wins.
A section tagged :graph: is a graph of its text and subsections, its :PROPERTIES: drawer serving as the header line:
each subheading is an item, its title the item’s text, its drawer’s :KEY: v the item’s | key | v | rows, its body
paragraphs notes, its ORDERED property 1. 2. children, and a TODO keyword its checkbox; a list in its body is more
children. C-c C-c in the section’s body renders it; see ‘org--graph-read-heading’ and ‘org-graph--section-cc’.
Each list item declares a node. Nesting encodes edges (parent → child); ordered children, 1. 2. 3., share a rank
in the order written. An item’s text may hold a <<target>>, its id; [[TARGET]] or [[TARGET][LABEL]] links, extra arcs;
and *bold*, /italic/, _underline_, +strike+, =code=, or ~pre~, a braced x^{2} or H_{2}O, or a @@dot:…@@ snippet,
which make its label an HTML-like one; an entity, \alpha, is its character. A <<<radio target>>> is a node, every
item mentioning it having an arc to it. Other Org syntax in a block, such as a headline or #+begin_quote, is an error.
A description tag, ‘+ L :T: :: CHILD’, labels the arc into CHILD, its tags styling it; so does a link’s description,
[[TARGET][L :T:]]; trailing ‘:T:’ tags splice #+name: templates; trailing ‘:@X:’ tags align: ‘:@start:’ sets the node
on the first rank, rank=min; ‘:@end:’, on the last, rank=max; any other, on one rank with every node sharing it;
an inline footnote, [fn::NOTE], is the node’s tooltip, as is a note: a paragraph past the item’s first, or a
#+begin_note block ---with ‘#+begin_note :edge’, the arc’s---, shown beneath the label instead under :notes inline;
a web, file, or [[*Section]] link, its URL; an [[file:IMAGE.png]], its image;
a checkbox, [X], [-], or [ ], applies the template checked, partial, or unchecked, when one exists; and a ‘\\’
breaks the label’s line ---in a table value or record cell, too.
Org’s {{{macros}}} expand, by the buffer’s #+macro: lines.
A two-column inline table on a list item supplies node attributes.
| key | val | ↦ a Graphviz attribute of the node; values are quoted for you.
| :edge.KEY | val | ↦ attribute of the arc from the parent; a parallel arc is a second child item.
| :edge[T].KEY | val | ↦ attribute of this item’s arc to T, from a [[T]] link; "rec::port" reaches a record’s port.
A child item that is a lone [[T]] link is a reference to T: an arc to it, styled by the child’s :edge.KEY rows.
| shape | cluster | ↦ make this node an inline subgraph cluster_ID { … }, its children its members; as does the
:container: tag; a :container_TITLE: tag puts a node in the box TITLE.
| any | other | table | ↦ a record: the node’s <TABLE> label, as laid out; a cell’s <<p>> is port p, its [[T]] an
arc from there; ^ or an empty cell spans, a <l>/<c>/<r> row aligns, and hlines rule. A
table is a record unless each row is two cells, its key an attribute or :edge.ATTR.
Example:
#+begin_graph :direction right
+ A
+ calls :: B
+ returns :: C
#+end_graph
Regarding the Lisp function:
+ BACKEND refers to the current export backend.
+ RAW-CONTENTS refers to the text as the user wrote it, sans its least indentation.
⇒ Its body asks, by &context, for CONTENTS: the ‘org parsed’ version of user text.
⇒ CONTENTS and RAW-CONTENTS are identical for the description of a link; whence the LINK? of &context is non-nil.
For example, upon LaTeX export, the Org special block
#+begin_graph
Hello, world
#+end_graph
is rewritten to the result of the call
(org--graph-block ‘latex "Hello, world") block
is declared with :edit-indirect-mode org-graph-edit-mode, an Org mode that completes Graphviz
attributes.
(org-defblock poem (&optional title :edit-indirect-mode text-mode &context contents) "Set CONTENTS in italics, under TITLE." (format "<h3>%s</h3><em>%s</em>" title contents))
For an action beyond an edit buffer, (org-defblock-add-edit-special-handler "poem" #'my-handler)
registers any function of no arguments, just as C-c C-c handlers are registered —see
org-special-block-indirect-edit.el.
7. C-c C-v C-d to split a block in two
Just as C-c C-v C-d splits a src block at point, so too it splits any special block, after
Kaushal Modi's trick; each piece keeps the block's whole #+begin_ line, header arguments
and all. By way of example, with point at the start of Two.,
#+begin_tip Mind One. Two. #+end_tip
becomes
#+begin_tip Mind One. #+end_tip #+begin_tip Mind Two. #+end_tip
At a line's end, the split falls after that line; an active region is split off as a piece of
its own; and C-u C-c C-v C-d puts a heading between the pieces. In a src block within a
special block, it is the src block that is split, as ever. The command is
org-special-block-split, should you want it on a key of its own.
8. C-c C-v C-j to insert a block's argument
In a src block, C-c C-v C-j completes a header argument, then its value, and appends both
to the #+begin_src line. Within a special block, it does the same for the block's own
keywords, as org-defblock declares them —offering each one's default— and for every
switch, such as :folded or :tags; an empty value, as for :folded, means t:
#+begin_details Hint C-c C-v C-j :title-color ⏎ red ⏎ body ↦ #+begin_details Hint :title-color red #+end_details
In a src block, our switches join Babel's own header arguments among those it completes.
9. Hover for a block's arguments: eldoc
With point on a #+begin_NAME or #+end_NAME line, or on a NAME: link, the echo area
—or eldoc-box, should we use it— shows how the block is called, defaults and all, along
with the first line of its docstring:
#+begin_details TITLE="Details" :background-color "#e5f5e5" :title-color "green": Enclose contents in a folded up box, for HTML.
This needs no setup beyond org-special-block-extras-mode, and works for our own blocks too.
A multi-line display, such as eldoc-box, shows more beneath that line: a representative example
of the block, fontified as Org, its # comments explaining each feature —enough for the common
uses, sans the manual—; the keys that act on the block —e.g., a math block's C-c C-c preview
and C-c ' edit—; the switches every block understands, :when, :folded, :let, and the
rest; and two buttons: Docs, for the full story, C-h o org--NAME-block; and See the manual,
for the section of this manual that documents it. Our own blocks declare these as org-defblock
options:
(org-defblock shy (:hover "Prose that eldoc shows beneath the docstring's first line."
:manual "https://example.com/shy") ; or "#ANCHOR", a section of this manual
"Be shy."
contents)
;; A long option reads best set apart, after the definition.
(org-defblock-attribute shy :example "
# Shy text is still Org: *bold* stays bold.
#+begin_shy
Hush, *now*.
#+end_shy")
Since org-defblock-attribute sets any option, after the fact, it serves us for blocks we did not
write too; e.g., (org-defblock-attribute graph :manual "https://example.com/my-notes").
An org-deflink takes :hover and :manual too.
Likewise, completing on a #+begin_ line ---M-TAB, or any completion popup— offers every
block's name beside the first line of its docstring, and Org's own blocks beside them; a popup's
documentation pane shows the block's whole docstring.
Life & Computing Science by Musa Al-hassy is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License
