From ed0f4a36e9846f7808d743759424019275f714fc Mon Sep 17 00:00:00 2001 From: Dan Davison Date: Sat, 24 Oct 2009 12:39:05 -0400 Subject: [PATCH 1/3] org-babel: Export code and/or results of #+lob function calls. Currently, python fontification is used for export of these lines, due to some similarity between python function call syntax and #+lob call syntax. This is implemented as a language type "babel", mapped to python in org-src-lang-modes. --- contrib/babel/lisp/org-babel-exp.el | 43 +++++++++++++++++++++++++---- contrib/lisp/org-babel-init.el | 3 ++ 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/contrib/babel/lisp/org-babel-exp.el b/contrib/babel/lisp/org-babel-exp.el index dd48c47f6..482cb04dc 100644 --- a/contrib/babel/lisp/org-babel-exp.el +++ b/contrib/babel/lisp/org-babel-exp.el @@ -33,6 +33,7 @@ (require 'org-exp-blocks) (org-export-blocks-add-block '(src org-babel-exp-src-blocks nil)) (add-to-list 'org-export-interblocks '(src org-babel-exp-inline-src-blocks)) +(add-to-list 'org-export-interblocks '(lob org-babel-exp-lob-one-liners)) (defun org-babel-exp-src-blocks (body &rest headers) "Process src block for export. Depending on the 'export' @@ -78,6 +79,30 @@ options and are taken from `org-babel-defualt-inline-header-args'." (+ 6 (length (first info)) (length (second info)))))) (replace-match replacement t t))))) +(defun org-babel-exp-lob-one-liners (start end) + "Process #+lob (Library of Babel) calls between START and END for export. +See `org-babel-exp-src-blocks' for export options. Currently the +options are taken from `org-babel-default-header-args'." + (interactive) + (let (replacement) + (save-excursion + (goto-char start) + (while (and (< (point) end) + (re-search-forward org-babel-lob-one-liner-regexp nil t)) + (setq replacement + (save-match-data + (org-babel-exp-do-export + "emacs-lisp" "results" + (org-babel-merge-params + org-babel-default-header-args + (org-babel-parse-header-arguments + (org-babel-clean-text-properties + (concat ":var results=" + (mapconcat #'identity (org-babel-lob-get-info) " "))))) + 'lob))) + (setq end (+ end (- (length replacement) (length (match-string 0))))) + (replace-match replacement t t))))) + (defun org-babel-exp-do-export (lang body params type) (case (intern (or (cdr (assoc :exports params)) "code")) ('none "") @@ -88,10 +113,13 @@ options and are taken from `org-babel-defualt-inline-header-args'." (org-babel-exp-results body lang params type))))) (defun org-babel-exp-code (body lang params type) - (case type - ('inline (format "=%s=" body)) - ('block (format "#+BEGIN_SRC %s\n%s%s\n#+END_SRC" lang body - (if (string-match "\n$" body) "" "\n"))))) + (case type + ('inline (format "=%s=" body)) + ('block (format "#+BEGIN_SRC %s\n%s%s\n#+END_SRC" lang body + (if (string-match "\n$" body) "" "\n"))) + ('lob (save-excursion + (re-search-backward org-babel-lob-one-liner-regexp) + (format "#+BEGIN_SRC babel\n%s\n#+END_SRC" (first (org-babel-lob-get-info))))))) (defun org-babel-exp-results (body lang params type) (let ((params @@ -122,7 +150,12 @@ options and are taken from `org-babel-defualt-inline-header-args'." (save-excursion ;; org-exp-blocks places us at the end of the block (re-search-backward org-babel-src-block-regexp nil t) (org-babel-execute-src-block - nil nil (org-babel-merge-params params '((:results . "replace")))) ""))))) + nil nil (org-babel-merge-params params '((:results . "replace")))) "")) + ('lob + (save-excursion + (re-search-backward org-babel-lob-one-liner-regexp nil t) + (org-babel-execute-src-block + nil (list lang body (org-babel-merge-params params '((:results . "replace"))))) ""))))) (provide 'org-babel-exp) ;;; org-babel-exp.el ends here diff --git a/contrib/lisp/org-babel-init.el b/contrib/lisp/org-babel-init.el index 51bfecd05..f71a5d0f3 100644 --- a/contrib/lisp/org-babel-init.el +++ b/contrib/lisp/org-babel-init.el @@ -65,5 +65,8 @@ (defun org-babel-load-library-of-babel () (org-babel-lob-ingest (expand-file-name "library-of-babel.org" org-babel-lob-dir)))) +(unless (assoc "babel" org-src-lang-modes) + (add-to-list 'org-src-lang-modes (cons "babel" 'python))) + (provide 'org-babel-init) ;;; org-babel-init.el ends here From b84540ec795695fadbe8dbef3b16b946d1e3a7b5 Mon Sep 17 00:00:00 2001 From: Dan Davison Date: Sat, 24 Oct 2009 12:49:53 -0400 Subject: [PATCH 2/3] org-exp-blocks: remove remnants of R support --- lisp/org-exp-blocks.el | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/lisp/org-exp-blocks.el b/lisp/org-exp-blocks.el index 5102f21a9..9829ab9a2 100644 --- a/lisp/org-exp-blocks.el +++ b/lisp/org-exp-blocks.el @@ -60,11 +60,6 @@ ;; their own divs with author-specific ids allowing for css ;; coloring of comments based on the author. ;; -;; R :: Implements Sweave type exporting, evaluates blocks of R code, -;; and also replaces \R{} chunks in the file with their result -;; when passed to R. This require the `R' command which is -;; provided by ESS (Emacs Speaks Statistics). -;; ;;; Adding new blocks ;; ;; When adding a new block type first define a formatting function @@ -76,17 +71,11 @@ (require 'cl)) (require 'org) -(defvar comint-last-input-end) -(defvar comint-prompt-regexp) -(defvar comint-last-input-end) (defvar htmlp) (defvar latexp) (defvar docbookp) (defvar asciip) -(declare-function comint-send-input "comint" (&optional no-newline artificial)) -(declare-function R "ext:ess" nil) - (defun org-export-blocks-set (var value) "Set the value of `org-export-blocks' and install fontification." (set var value) @@ -102,9 +91,7 @@ (defcustom org-export-blocks '((comment org-export-blocks-format-comment t) (ditaa org-export-blocks-format-ditaa nil) - (dot org-export-blocks-format-dot nil) - (r org-export-blocks-format-R nil) - (R org-export-blocks-format-R nil)) + (dot org-export-blocks-format-dot nil)) "Use this a-list to associate block types with block exporting functions. The type of a block is determined by the text immediately following the '#+BEGIN_' portion of the block header. From 6c6b9985acd35c414c634910b2066ac3ebab3244 Mon Sep 17 00:00:00 2001 From: Dan Davison Date: Sat, 24 Oct 2009 13:14:25 -0400 Subject: [PATCH 3/3] org-exp-blocks: remove unused variables and save-match-datas --- lisp/org-exp-blocks.el | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/lisp/org-exp-blocks.el b/lisp/org-exp-blocks.el index 9829ab9a2..646688d4a 100644 --- a/lisp/org-exp-blocks.el +++ b/lisp/org-exp-blocks.el @@ -164,34 +164,30 @@ specified in BLOCKS which default to the value of `org-export-blocks-witheld'." (interactive) (save-window-excursion - (let ((count 0) - (blocks org-export-blocks-witheld) - (case-fold-search t) + (let ((case-fold-search t) (types '()) - indentation type func start end) + indentation type func start) (flet ((interblock (start end) - (save-match-data - (mapcar (lambda (pair) (funcall (second pair) start end)) - org-export-interblocks)))) + (mapcar (lambda (pair) (funcall (second pair) start end)) + org-export-interblocks))) (goto-char (point-min)) - (setf start (point)) + (setq start (point)) (while (re-search-forward "^\\([ \t]*\\)#\\+begin_\\(\\S-+\\)[ \t]*\\(.*\\)?[\r\n]\\([^\000]*?\\)[\r\n][ \t]*#\\+end_\\S-+.*" nil t) - (save-match-data (setq indentation (length (match-string 1)))) - (save-match-data (setf type (intern (match-string 2)))) - (unless (memq type types) (setf types (cons type types))) - (setf end (save-match-data (match-beginning 0))) - (interblock start end) - (if (setf func (cadr (assoc type org-export-blocks))) + (setq indentation (length (match-string 1))) + (setq type (intern (match-string 2))) + (unless (memq type types) (setq types (cons type types))) + (save-match-data (interblock start (match-beginning 0))) + (if (setq func (cadr (assoc type org-export-blocks))) (progn (replace-match (save-match-data - (if (memq type blocks) + (if (memq type org-export-blocks-witheld) "" (apply func (save-match-data (org-remove-indentation (match-string 4))) (split-string (match-string 3) " ")))) t t) ;; indent block (indent-code-rigidly (match-beginning 0) (match-end 0) indentation))) - (setf start (save-match-data (match-end 0)))) + (setq start (match-end 0))) (interblock start (point-max)))))) (add-hook 'org-export-preprocess-hook 'org-export-blocks-preprocess)