Hello,

Nicolas Goaziou wrote:
> Andreas Leha <andreas.l...@med.uni-goettingen.de> writes:
>> Grant Rettke <g...@wisdomandwonder.com> writes:
>>>
>>> My goal is to obtain the following behavior in org mode for
>>> a document:
>>> 1) Report an error if there is a source block without a language
>>>    specified
>>> 2) Report an error if there is a source block with a language
>>>    specified that is *not* present in `org-babel-load-languages'
>>>
>>> I've thought about ways to do this and come up with:
>>> 1) Visually inspect the document
>>> 2) Use `org-element' to parse and process the document
>>
>> I'd be interested in 2) if you come up with something here :-)
>
>   (defun my-src-block-check ()
>     (interactive)
>     (org-element-map (org-element-parse-buffer 'element) 'src-block
>       (lambda (src-block)
>         (let ((language (org-element-property :language src-block)))
>           (cond ((null language)
>                  (error "Missing language at position %d"
>                         (org-element-property :post-affiliated src-block)))
>                 ((not (assoc-string language org-babel-load-languages))
>                  (error "Unknown language at position %d"
>                         (org-element-property :post-affiliated 
> src-block)))))))
>     (message "Source blocks checked in %s." (buffer-name 
> (buffer-base-buffer))))

I've improved it in order to:

- Check as well for the language of inline code blocks,
- Report the line number instead of the char position.

--8<---------------cut here---------------start------------->8---
  (defun org-src-block-check ()
    (interactive)
    (org-element-map (org-element-parse-buffer)
      '(src-block inline-src-block)
      (lambda (sb)
        (let ((language (org-element-property :language sb)))
          (cond ((null language)
                 (error "Missing language at line %d in %s"
                        (org-current-line
                         (org-element-property :post-affiliated sb))
                        (buffer-name)))
                ((not (assoc-string language org-babel-load-languages))
                 (error "Unknown language `%s' at line %d in `%s'"
                        language
                        (org-current-line
                         (org-element-property :post-affiliated sb))
                        (buffer-name)))))))
    (message "Source blocks checked in %s." (buffer-name (buffer-base-buffer))))
--8<---------------cut here---------------end--------------->8---

Though, while the line number is often correct, it is not always!?  Is
`org-current-line' the right function to call?

Besides that, I think we should check for such unknown languages if (and
only if) the code block is supposed to be executed.

For example, while I don't have a python interpreter on my machine
(hence, I want to be sure I never have executable python blocks in my
documents), I can have Org documentation that includes python blocks (as
examples, of just for tangling -- to be run on another machine).  As
long as the code blocks don't have ":eval yes" somehow, this is OK, and
shouldn't be caught as an error.  WDYT?

Best regards,
  Seb

-- 
Sebastien Vauban

Reply via email to