From mboxrd@z Thu Jan 1 00:00:00 1970 From: Sebastien Vauban 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 Message-ID: <86wq3z8gl2.fsf@example.com> References: <87egsmnvf7.fsf@nicolasgoaziou.fr> Mime-Version: 1.0 Content-Type: text/plain Return-path: List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org-mXXj517/zsQ@public.gmane.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org-mXXj517/zsQ@public.gmane.org To: Nicolas Goaziou , Andreas Leha Cc: emacs-orgmode-mXXj517/zsQ@public.gmane.org Hello, Nicolas Goaziou wrote: > Andreas Leha writes: >> Grant Rettke 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