On Wed, Sep 14, 2016 at 8:21 AM Clément Pit--Claudel <address@hidden
Back when I wrote this I was starting out with Org-mode, I was in a hurry, and I didn't know about org-export-before-processing-hook :) So instead I had a larger ELisp script that opened the file, ran this filter, then called org-latex-export-to-latex :)
Looks good. Maybe using the same syntax for both would be good though; something like "BEGIN_ONLY tex" and "BEGIN_ONLY :not tex" maybe?
Thanks. I was exactly my earlier syntax idea but I scrapped it!
We could probably get rid of the "tex" part after the END_ONLY, and change "tex" to "latex" for consistency with BEGIN_EXPORT :)
I thought of removing that too. But then thought of keeping it in case we want to support nesting of these blocks.. I haven't yet needed to have anything like this, but below is a rough idea.
I haven't tested this, but based on the code, nesting anyways doesn't work as of now I believe.
Below is a modification of your code.
(defun cpit/filter-begin-only (type)
"Remove BEGIN_ONLY %s blocks whose %s doesn't equal TYPE.
For those that match, only remove the delimiters.
On the flip side, for BEGIN_EXCEPT %s blocks, remove those if %s equals TYPE. "
(while (re-search-forward " *#\\+BEGIN_\\(ONLY\\|EXCEPT\\) +\\([a-z]+\\)\n" nil t)
(let ((only-or-export (match-string-no-properties 1))
(block-type (match-string-no-properties 2))
(begin-from (match-beginning 0))
(begin-to (match-end 0)))
(re-search-forward (format " *#\\+END_%s +%s\n" only-or-export block-type))
(let ((end-from (match-beginning 0))
(end-to (match-end 0)))
(if (or (and (string= "ONLY" only-or-export)
(string= type block-type))
(and (string= "EXCEPT" only-or-export)
(not (string= type block-type))))
(progn ; Keep the block,
; delete just the comment markers
(delete-region end-from end-to)
(delete-region begin-from begin-to))
;; Delete the block
(message "Removing %s block" block-type)
(delete-region begin-from end-to))))))
(add-hook 'org-export-before-process #'cpit/filter-begin-only)