| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 | ;;; org-babel-tangle.el --- Extract source code from org-mode files;; Copyright (C) 2009 Dan Davison, Eric Schulte;; Author: Dan Davison, Eric Schulte;; Keywords: literate programming, reproducible research;; Homepage: http://orgmode.org;; Version: 0.01;;; License:;; This program is free software; you can redistribute it and/or modify;; it under the terms of the GNU General Public License as published by;; the Free Software Foundation; either version 3, or (at your option);; any later version.;;;; This program is distributed in the hope that it will be useful,;; but WITHOUT ANY WARRANTY; without even the implied warranty of;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the;; GNU General Public License for more details.;;;; You should have received a copy of the GNU General Public License;; along with GNU Emacs; see the file COPYING.  If not, write to the;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,;; Boston, MA 02110-1301, USA.;;; Commentary:;; Extract the code from source blocks out into raw source-code files.;;; Code:(require 'org-babel)(defvar org-babel-tangle-langs nil  "Association list matching source-block languages.  The car ofeach element should be a string indicating the source blocklanguage, and the cdr should be a list containing the extensionand shebang(#!) line to use when writing out the language tofile.")(defun org-babel-tangle ()  "Extract the bodies of all source code blocks from the currentfile into their own source-specific files."  (interactive)  (save-excursion    (let ((base-name (file-name-sans-extension (buffer-file-name)))          (block-counter 0))      (mapc ;; for every language create a file       (lambda (by-lang)         (let* ((lang (car by-lang))                (lang-f (intern (concat lang "-mode")))                (lang-specs (cdr (assoc lang org-babel-tangle-langs)))                (ext (first lang-specs))                (she-bang (second lang-specs))                (by-session (cdr by-lang)))           (flet ((to-file (filename specs)                           (with-temp-file filename                             (funcall lang-f)                             (when she-bang (insert (concat she-bang "\n")))                             (comment-region			      (point) (progn (insert "generated by org-babel-tangle") (point)))                             (mapc #'org-babel-spec-to-string (reverse specs)))))             ;; if there are multiple sessions then break out by session             (if (> (length by-session) 1)                 (mapc (lambda (session-pair)                         (setq block-counter (+ block-counter (length (cdr session-pair))))                         (to-file (format				   "%s-%s.%s" base-name (car session-pair) ext) (cdr session-pair)))                       by-session)               (setq block-counter (+ block-counter (length (cdr (car by-session)))))               (to-file (format "%s.%s" base-name ext) (cdr (car by-session)))))))       (org-babel-collect-blocks))      (message "tangled %d source-code blocks" block-counter))))(defun org-babel-collect-blocks ()  "Collect all source blocks in the current org-mode file.Return two nested association lists, first grouped by language,then by session, the contents will be source-code blockspecifications of the form used by `org-babel-spec-to-string'."  (let ((block-counter 0) blocks)    (org-babel-map-source-blocks (buffer-file-name)      (setq block-counter (+ 1 block-counter))      (let* ((link (progn (call-interactively 'org-store-link)                          (org-babel-clean-text-properties (car (pop org-stored-links)))))             (source-name (intern (or (org-babel-get-src-block-name)                                      (format "block-%d" block-counter))))             (info (org-babel-get-src-block-info))             (lang (first info))             (body (second info))             (params (third info))             (spec (list link source-name params body))             (session (cdr (assoc :session params)))             by-lang by-session)        ;; add the spec for this block to blocks under it's lang and session        (setq by-lang (cdr (assoc lang blocks)))        (setq blocks (delq (assoc lang blocks) blocks))        (setq by-session (cdr (assoc session by-lang)))        (setq by-lang (delq (assoc session by-lang) by-lang))        (setq blocks (cons ;; by-language                      (cons lang (cons ;; by-session                                  (cons session (cons spec by-session)) by-lang))                      blocks))))    ;; blocks should contain all source-blocks organized by language and session    ;; (message "blocks=%S" blocks) ;; debugging    blocks))(defun org-babel-spec-to-string (spec)  "Insert the source-code specified by SPEC into the currentsource code file.  This function uses `comment-region' whichassumes that the appropriate major-mode is set.  SPEC has theform  (link source-name params body)"  (flet ((insert-comment (text)                         (comment-region (point) (progn (insert text) (point)))))    (let ((link (first spec))          (source-name (second spec))          (body (fourth spec)))      (insert "\n\n")      (insert-comment (format "[[%s][%s]]" (org-link-escape link) source-name))      (insert (format "\n%s\n" (org-babel-chomp body)))      (insert-comment (format "%s ends here" source-name))      (insert "\n"))))(provide 'org-babel-tangle);;; org-babel-tangle.el ends here
 |