Skip to main content
added 340 characters in body
Source Link
Tobias
  • 33.7k
  • 1
  • 38
  • 78

The 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.

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.

The 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.

Bounty Ended with 50 reputation awarded by Josh
added 44 characters in body
Source Link
Tobias
  • 33.7k
  • 1
  • 38
  • 78

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."
  (save-excursion
    (save-match-data
      (and (re-search-backward ">>\\|\\(<<dd_display:\\)" (line-beginning-position) t)
       (match-beginning 1)))))

(add-hook 'fill-nobreak-predicate #'my-in-dd-display)

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-orglocal-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-orglocal-setup-in-dd-display)

Alternatively one could globally add my-in-dd-display to 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 fill-nobreak-predicate in org-setup-filling. It uses setq-local instead of add-hook.

So, to activate my-in-dd-display in Orgmode you need to set it with setq or setq-local in org-mode-hook:

(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)

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."
  (save-excursion
    (save-match-data
      (and (re-search-backward ">>\\|\\(<<dd_display:\\)" (line-beginning-position) t)
       (match-beginning 1)))))

(add-hook 'fill-nobreak-predicate #'my-in-dd-display)

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-org-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-org-setup-in-dd-display)

Alternatively one could globally add my-in-dd-display to fill-nobreak-predicate for all modes except Orgmode:

(add-hook 'fill-nobreak-predicate 'my-in-dd-display)

Orgmode has a non-standard way setting up fill-nobreak-predicate in org-setup-filling. It uses setq-local instead of add-hook.

So, to activate my-in-dd-display in Orgmode you need to set it with setq or setq-local in org-mode-hook:

(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)

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."
  (save-excursion
    (save-match-data
      (and (re-search-backward ">>\\|\\(<<dd_display:\\)" (line-beginning-position) t)
       (match-beginning 1)))))

(add-hook 'fill-nobreak-predicate #'my-in-dd-display)

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 my-in-dd-display to 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 fill-nobreak-predicate in org-setup-filling. It uses setq-local instead of add-hook.

So, to activate my-in-dd-display in Orgmode you need to set it with setq or setq-local in org-mode-hook:

(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)
Source Link
Tobias
  • 33.7k
  • 1
  • 38
  • 78

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."
  (save-excursion
    (save-match-data
      (and (re-search-backward ">>\\|\\(<<dd_display:\\)" (line-beginning-position) t)
       (match-beginning 1)))))

(add-hook 'fill-nobreak-predicate #'my-in-dd-display)

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-org-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-org-setup-in-dd-display)

Alternatively one could globally add my-in-dd-display to fill-nobreak-predicate for all modes except Orgmode:

(add-hook 'fill-nobreak-predicate 'my-in-dd-display)

Orgmode has a non-standard way setting up fill-nobreak-predicate in org-setup-filling. It uses setq-local instead of add-hook.

So, to activate my-in-dd-display in Orgmode you need to set it with setq or setq-local in org-mode-hook:

(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)