Easily check src block correctness in org-mode

Thank you Nicolas Goaziou, for the beginnings of an org-lint. The goal here was to:

  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’

And, it does.

(defun gcr/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))))

Addendum: 2015-02-05
Sebastien Vauban just posted his improved version to:
– “Check as well for the language of inline code blocks,”
– “Report the line number instead of the char position.”

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

Not seeing this post on the web yet so no link.

One thought on “Easily check src block correctness in org-mode”

Leave a Reply

Your email address will not be published. Required fields are marked *