emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Sebastien Vauban <sva-news-D0wtAvR13HarG/iDocfnWg@public.gmane.org>
To: Nicolas Goaziou
	<mail-Gpy5sJQTEQHwkn9pgDnJRVAUjnlXr6A1@public.gmane.org>,
	Andreas Leha
	<andreas.leha-A1rZ2h3LdSKGMSlLMZIubhS11BummzK+@public.gmane.org>
Cc: emacs-orgmode-mXXj517/zsQ@public.gmane.org
Subject: Re: How to inspect a document and check for the presence of source block language names and support
Date: Tue, 03 Feb 2015 10:55:21 +0100	[thread overview]
Message-ID: <86wq3z8gl2.fsf@example.com> (raw)
In-Reply-To: 87egsmnvf7.fsf@nicolasgoaziou.fr

Hello,

Nicolas Goaziou wrote:
> Andreas Leha <andreas.leha-A1rZ2h3LdSKGMSlLMZIubhS11BummzK+@public.gmane.org> writes:
>> Grant Rettke <gcr-8CdBUMNg/Lm6M07oP3eFjEEOCMrvLtNR@public.gmane.org> 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

      parent reply	other threads:[~2015-02-03  9:55 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-11-29  2:08 How to inspect a document and check for the presence of source block language names and support Grant Rettke
2014-11-29  2:22 ` Andreas Leha
2014-11-29 10:38   ` Nicolas Goaziou
2014-11-29 14:42     ` Grant Rettke
2014-11-29 20:40       ` Andreas Leha
2015-02-03  9:55     ` Sebastien Vauban [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=86wq3z8gl2.fsf@example.com \
    --to=sva-news-d0wtavr13harg/idocfnwg@public.gmane.org \
    --cc=andreas.leha-A1rZ2h3LdSKGMSlLMZIubhS11BummzK+@public.gmane.org \
    --cc=emacs-orgmode-mXXj517/zsQ@public.gmane.org \
    --cc=mail-Gpy5sJQTEQHwkn9pgDnJRVAUjnlXr6A1@public.gmane.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).