I wanted dalanicolai to fix his answer but he didn't do it (see my first and my second comment).
So, I give here my own answer.
One surpresses filling if one finds the tag starter in a backward
search without finding the tag ender at first:
(defun my-in-dd-display ()
"Don't break lines in Inline Stata Dynamic Tags."
(and (re-search-backward ">>\\|\\(<<dd_display:\\)" (line-beginning-position) t)
(add-hook 'fill-nobreak-predicate #'my-in-dd-display)
re-search-backward does the backward search that matches both, the tag-starter and the tag-end. The search stops if such a match is found. But only the tag-starter is grouped. The form
(and ... (match-beginning 1)) makes sure that the function returns non-nil if the tag-starter-group is matched and nil if the tag-end is matched.
Since the function is only a predicate it should not move point.
Avoiding a modification of the
match-data is not required by the doc string of
fill-nobreak-predicate but it is added as a precaution.
It is not clear from the question in which major mode you want to set up
my-in-dd-display as fill predicate.
The standard way to add
my-in-dd-display locally in certain major mode
my-major-mode would be:
(defun my-local-setup-in-dd-display ()
"Activate `my-in-dd-display' locally in major modes except Orgmode."
(add-hook 'fill-nobreak-predicate 'my-in-dd-display nil t))
(add-hook 'my-major-mode #'my-local-setup-in-dd-display)
Alternatively one could globally add
fill-nobreak-predicate for all modes except Orgmode as it is done in the first code snippet:
(add-hook 'fill-nobreak-predicate 'my-in-dd-display)
Orgmode has a non-standard way setting up
org-setup-filling. It uses
setq-local instead of
So, to activate
my-in-dd-display in Orgmode you need to set it with
(defun my-org-setup-in-dd-display ()
"Activate `my-in-dd-display' by `setq-local' as it is required for Orgmode."
(unless (memq 'my-in-dd-display fill-nobreak-predicate)
(setq-local fill-nobreak-predicate (cons 'my-in-dd-display fill-nobreak-predicate))))
(add-hook 'org-mode-hook #'my-org-setup-in-dd-display)