Function: org-babel-tangle-collect-blocks

org-babel-tangle-collect-blocks is a byte-compiled function defined in ob-tangle.el.gz.

Signature

(org-babel-tangle-collect-blocks &optional LANG-RE TANGLE-FILE)

Documentation

Collect source blocks in the current Org file.

Return an association list of language and source-code block specifications of the form used by org-babel-spec-to-string grouped by tangled file name.

Optional argument LANG-RE can be used to limit the collected source code blocks by languages matching a regular expression.

Optional argument TANGLE-FILE can be used to limit the collected code blocks by target file.

Source Code

;; Defined in /usr/src/emacs/lisp/org/ob-tangle.el.gz
(defun org-babel-tangle-collect-blocks (&optional lang-re tangle-file)
  "Collect source blocks in the current Org file.
Return an association list of language and source-code block
specifications of the form used by `org-babel-spec-to-string'
grouped by tangled file name.

Optional argument LANG-RE can be used to limit the collected
source code blocks by languages matching a regular expression.

Optional argument TANGLE-FILE can be used to limit the collected
code blocks by target file."
  (let ((counter 0)
        (buffer-fn (buffer-file-name (buffer-base-buffer)))
        last-heading-pos blocks)
    (org-babel-map-src-blocks (buffer-file-name)
      (let ((current-heading-pos
             (or (org-element-begin
                  (org-element-lineage
                   (org-element-at-point)
                   'headline t))
                 1)))
	(if (eq last-heading-pos current-heading-pos) (cl-incf counter)
	  (setq counter 1)
	  (setq last-heading-pos current-heading-pos)))
      (unless (or (org-in-commented-heading-p)
		  (org-in-archived-heading-p))
	(let* ((info (org-babel-get-src-block-info 'no-eval))
	       (src-lang (nth 0 info))
	       (src-tfile (cdr (assq :tangle (nth 2 info)))))
	  (unless (or (string= src-tfile "no")
                      (not src-lang) ;; src block without lang
		      (and tangle-file (not (equal tangle-file src-tfile)))
		      (and lang-re (not (string-match-p lang-re src-lang))))
	    ;; Add the spec for this block to blocks under its tangled
	    ;; file name.
	    (let* ((block (org-babel-tangle-single-block counter))
                   (src-tfile (cdr (assq :tangle (nth 4 block))))
		   (file-name (org-babel-effective-tangled-filename
                               buffer-fn src-lang src-tfile))
		   (by-fn (assoc file-name blocks)))
	      (if by-fn (setcdr by-fn (cons (cons src-lang block) (cdr by-fn)))
		(push (cons file-name (list (cons src-lang block))) blocks)))))))
    ;; Ensure blocks are in the correct order.
    (mapcar (lambda (b) (cons (car b) (nreverse (cdr b))))
	    (nreverse blocks))))