view lisp/org/org-exp-blocks.el @ 104810:86b7fe7d1d8f

2009-09-02 Carsten Dominik <carsten.dominik@gmail.com> * org-protocol.el (org-protocol-store-link) (org-protocol-remember, org-protocol-open-source): Add autoloads. * org-compat.el (org-float-time): New function. * org.el (org-clock-update-time-maybe) (org-sort-entries-or-items, org-do-sort) (org-evaluate-time-range, org-time-string-to-seconds) (org-closed-in-range): Use `org-float-time'. * org-timer.el (org-timer-start, org-timer-pause-or-continue) (org-timer-seconds): Use `org-float-time'. * org-clock.el (org-clock-get-clocked-time, org-clock-out) (org-clock-sum, org-dblock-write:clocktable) (org-clocktable-steps): Use `org-float-time'. * org-agenda.el (org-agenda-last-marker-time) (org-agenda-new-marker, org-diary): Use `org-float-time'. * org-compat.el (w32-focus-frame): Declare the w32-focus-frame function. * org-exp.el (org-get-file-contents): Only protect lines that really need it. * org-html.el (require): Require cl for compilation. * org.el: Avoid using `default-major-mode'. * org-plot.el (require): Require CL only at compile time. * org-exp.el (require): Require CL only at compile time. * org-agenda.el (org-agenda-quit): When the agenda window is dedicated, remove other windows before exiting, so that the frame really will be killed. * org-exp.el (org-export-handle-include-files): Reset START and END for each loop cycle. 2009-09-02 Carsten Dominik <carsten.dominik@gmail.com> * org.el (org-eval-in-calendar): Use `org-select-frame-set-input-focus'. * org-compat.el (org-select-frame-set-input-focus): New function. * org.el (org-update-statistics-cookies): New function. (org-mode-map): Bind `C-c #' to `org-update-statistics-cookies'. 2009-09-02 Carsten Dominik <carsten.dominik@gmail.com> * org-src.el (org-edit-fixed-width-region): Set org-src-mode only after the local variables are set. * org-latex.el (org-export-latex-protect-amp): New function. (org-export-latex-links): Protect link ampersands in tables. * org-exp.el (org-export-select-backend-specific-text): Match in two steps, to avoid regexp problems. * org.el (org-offer-links-in-entry): Improve working with many and duplicate links. * org-agenda.el (org-agenda-show-1): Make more consistent with normal cycling. (org-agenda-cycle-show): Make more consistent with normal cycling. * org-gnus.el (org-gnus-store-link): Restore the linking to a website. 2009-09-02 Bastien Guerry <bzg@altern.org> * org-latex.el (org-export-latex-first-lines): Bugfix. 2009-09-02 Carsten Dominik <carsten.dominik@gmail.com> * org-clock.el (org-clock-modify-effort-estimate): Emit message about new effort. * org.el (org-set-effort): New function. (org-mode-map): New key for effort setting command. * org-agenda.el (org-agenda): Keep window setup when calling agenda from within agenda window. (org-agenda-mode-map): New keys for effort setting commands. (org-agenda-menu): Add effort setting commands to menu. (org-agenda-set-property, org-agenda-set-effort): New functions. * org-latex.el (org-export-latex-tables): Fix `org-table-last-alignment' and `org-table-last-column-widths' if the first column has been removed. 2009-09-02 Carsten Dominik <carsten.dominik@gmail.com> * org.el (org-remove-timestamp-with-keyword): Only remove in entry, not in subtree. * org-src.el (org-src-lang-modes): Add abbreviation elisp for emacs lisp. * org.el (org-open-at-point): When on headline, offer all strings in entry. * org-remember.el (org-remember-templates): Documentation fix. * org.el (org-move-subtree-down): Use `org-get-next-sibling' and `org-get-last-sibling' instead of the outline versions of these functions. (org-get-last-sibling): New function. (org-refile): Use `org-get-next-sibling' instead of the outline version of this function. (org-clean-visibility-after-subtree-move): Use `org-get-next-sibling' and `org-get-last-sibling' instead of the outline versions of these functions. 2009-09-02 Carsten Dominik <carsten.dominik@gmail.com> * org-agenda.el (org-prepare-agenda): When creating a new frame for the agenda, make the window dedicated. * org-agenda.el (org-agenda-mode-map): New keys for time motion. * org-table.el (org-table-align): Change the order of reinsertion and deletion, to avoid problems with overlays following the table. * org.el (org-parse-time-string): Better error message. (org-show-subtree): Use org-end-of-subtree. * org-macs.el (org-goto-line): New defsubst. * org.el (org-open-file, org-change-tag-in-region) (org-fast-tag-show-exit): Don't use `goto-line'. * org-table.el (org-table-align, org-table-insert-column) (org-table-delete-column, org-table-move-column) (org-table-sort-lines, org-table-copy-region) (org-table-paste-rectangle, org-table-wrap-region) (org-table-get-specials, org-table-rotate-recalc-marks) (org-table-get-range, org-table-recalculate) (org-table-edit-formulas, org-table-fedit-convert-buffer) (org-table-show-reference, org-table-highlight-rectangle): Don't use `goto-line'. * org-src.el (org-edit-src-code, org-edit-fixed-width-region) (org-edit-src-exit): Don't use `goto-line'. * org-macs.el (org-preserve-lc): Don't use `goto-line'. * org-list.el (org-renumber-ordered-list, org-fix-bullet-type): Don't use `goto-line'. * org-exp.el (org-export-number-lines): Don't use `goto-line'. * org-colview.el (org-columns, org-columns-redo) (org-agenda-columns): Don't use `goto-line'. * org-colview-xemacs.el (org-columns, org-agenda-columns): Don't use `goto-line'. * org-agenda.el (org-agenda-mode): Force visual line motion off. (org-agenda-add-entry-text-maxlines): Improve docstring. (org-agenda-start-with-entry-text-mode): New option. (org-agenda-entry-text-maxlines): New option. (org-agenda-entry-text-mode): New variable. (org-agenda-mode): Set initial value of `org-agenda-entry-text-mode'. (org-agenda-mode-map): Add the `E' key. (org-agenda-menu): Add entry text mode to the menu. (org-agenda-get-some-entry-text): Fix line count bug. (org-finalize-agenda): Apply entry text mode if appropriate. (org-agenda-entry-text-show-here): New function. (org-agenda-entry-text-show): New function. (org-agenda-entry-text-hide): New function. (org-agenda-view-mode-dispatch): Add entry text mode to the view key menu. (org-agenda-entry-text-mode): New command. (org-agenda-set-mode-name): Add entry text mode to the mode line string. (org-agenda-undo, org-agenda-get-restriction-and-command) (org-agenda-get-some-entry-text, org-agenda-redo): Don't use `goto-line'. 2009-09-02 Bernt Hansen <bernt@norang.ca> * org-clock.el (org-notify): Bugfix. 2009-09-02 Carsten Dominik <carsten.dominik@gmail.com> * org-agenda.el (org-agenda-open-link): Handle multiple links and check for after-string. * org-gnus.el (org-gnus-store-link): Simplify. * org.el (org-latex-regexps): Don't add extra empty lines for display formulas. 2009-09-02 Carsten Dominik <carsten.dominik@gmail.com> * org-agenda.el (org-agenda-get-some-entry-text): New function. (org-agenda-add-entry-text): Use `org-agenda-get-some-entry-text'. * org.el (org-cycle-separator-lines): Update docstring. (org-cycle-show-empty-lines): Handle negative values for `org-cycle-show-empty-lines'. * org-exp.el (org-export-protect-sub-super): New function. (org-export-normalize-links): Protect the url of plain links from supscript and superscript processing. * org-remember.el (org-remember-escaped-%): New function. (org-remember-apply-template): Use `org-remember-escaped-%' to detect escaped % signs. 2009-09-02 Bastien Guerry <bzg@altern.org> * org-timer.el (org-timer-set-timer): Use `org-notify' and play a sound when showing the notification. * org-clock.el (org-notify): New function. (org-clock-notify-once-if-expired): Use `org-notify'. * org-gnus.el (org-gnus-store-link): Handle `gnus-summary-mode' and `gnus-article-mode' separately. (gnus-summary-article-header): Fix the declare-function. 2009-09-02 Carsten Dominik <carsten.dominik@gmail.com> * org-exp.el (org-export-format-source-code-or-example): Translate language. * org-src.el (org-src-lang-modes): New variable (org-edit-src-code): Translate language. * org-exp.el (org-export-format-source-code-or-example): Deal wit the new structure of the `org-export-latex-listings-langs' variable. * org-latex.el (org-export-latex-listings-langs): Change structure of the variable from plist to alist. 2009-09-02 Carsten Dominik <carsten.dominik@gmail.com> * org.el (org-in-commented-line): New function. 2009-09-02 Carsten Dominik <carsten.dominik@gmail.com> * org.el (org-hide-block-toggle): Make folded blocks searchable. 2009-09-02 Friedrich Delgado Friedrichs <friedel@nomaden.org> (tiny change) * org.el (org-flag-drawer): More useful error. 2009-09-02 Carsten Dominik <carsten.dominik@gmail.com> * org-remember.el (org-remember-apply-template): Use org-icompleting-read. * org-publish.el (org-publish): Use org-icompleting-read. * org-colview.el (org-columns-edit-value, org-columns-new) (org-insert-columns-dblock): Use org-icompleting-read. * org-colview-xemacs.el (org-columns-edit-value) (org-columns-new, org-insert-columns-dblock): Use org-icompleting-read. * org-attach.el (org-attach-delete-one, org-attach-open): Use org-icompleting-read. 2009-09-02 Carsten Dominik <carsten.dominik@gmail.com> * org.el (org-hierarchical-todo-statistics): Improve docstring. (org-version): Return the version text. (org-org-menu): Add a menu entry for the new bug reporter. (org-submit-bug-report): New command. * org-list.el (org-hierarchical-checkbox-statistics): Improve docstring. * org.el (org-emphasis-regexp-components): Add "`" to set of pre-emphasis characters. * org-latex.el (org-export-latex-classes): Always include the soul package. (org-export-latex-emphasis-alist): Use \st for strikethough. * org-exp-blocks.el (org-export-blocks-preprocess): Use `indent-code-rigidly' to indent. * org-agenda.el (org-agenda-get-restriction-and-command): Remove properties only if MATCH really is a string. 2009-09-02 Carsten Dominik <carsten.dominik@gmail.com> * org-latex.el (org-export-latex-packages-alist): Fix customization type. * org.el (org-create-formula-image): Also use `org-export-latex-packages-alist'. * org-html.el (org-export-as-html): Fix bug in footnote regexp. (org-export-as-html): Format footnotes correctly. 2009-09-02 Carsten Dominik <carsten.dominik@gmail.com> * org.el (org-fast-tag-selection): Avoid text properties on tags in the alist. * org-agenda.el (org-agenda-get-restriction-and-command): Avoid text properties on the match element. 2009-09-02 Carsten Dominik <carsten.dominik@gmail.com> * org.el (org-set-regexps-and-options): Make sure the list of done keywords is not invalid. * org-exp.el (org-export-interpolate-newlines): New function. 2009-09-02 Carsten Dominik <carsten.dominik@gmail.com> * org.el (org-format-latex): Avoid nested overlays. * org-latex.el (org-export-latex-listings-langs): Add a few more languages. * org-exp.el (org-export-preprocess-apply-macros): Make sure to ignore newlines and space before the first macro argument. * org-latex.el (org-export-latex-tables): Remove save-excursion around `org-table-align'. 2009-09-02 Carsten Dominik <carsten.dominik@gmail.com> * org.el (org-export-html-special-string-regexps): Definition moved into org.el * org-exp.el (org-export-preprocess-apply-macros): Allow newlines in macro calls. 2009-09-02 Carsten Dominik <carsten.dominik@gmail.com> * org-latex.el (org-export-latex-listings) (org-export-latex-listings-langs): New options. * org-exp.el (org-export-format-source-code-or-example): Use listing package if requested by the user. 2009-09-02 Bastien Guerry <bzg@altern.org> * org.el (org-iswitchb): Fix bug when aborting the `org-iswitchb' command before actually switching to a buffer. 2009-09-02 Carsten Dominik <carsten.dominik@gmail.com> * org-exp.el (org-get-file-contents): Only quote org lines when the markup is src or example. * org-agenda.el (org-agenda-skip-scheduled-if-deadline-is-shown): New option (org-agenda-get-day-entries): Remember deadline results and pass them on into the function getting the scheduling information. (org-agenda-get-scheduled): Accept deadline results as parameters and maybe skip some entries. (org-agenda-skip-scheduled-if-deadline-is-shown): New option. * org.el (org-insert-heading): When respecting content, do not convert current line to headline. * org-clock.el (org-clock-save-markers-for-cut-and-paste): Also cheeeeeck the hd marker (org-clock-in): Also set the hd marker. (org-clock-out): Also set the hd marker. (org-clock-cancel): Reset markers. * org.el (org-clock-hd-marker): New marker. * org-faces.el (org-agenda-clocking): New face. * org-agenda.el (org-agenda-mark-clocking-task): New function. (org-finalize-agenda): call `org-agenda-mark-clocking-task'. * org.el (org-modules): Add org-track.el. * org-agenda.el (org-agenda-bulk-marked-p): New function. (org-agenda-bulk-mark, org-agenda-bulk-unmark): Use `org-agenda-bulk-marked-p'. (org-agenda-bulk-toggle): New command. 2009-09-02 Carsten Dominik <carsten.dominik@gmail.com> * org.el (org-move-subtree-down): Hide subtree if it was folded, not just the body. * org-remember.el (org-remember-finalize): Avoid buffer-modified messages.
author Carsten Dominik <dominik@science.uva.nl>
date Wed, 02 Sep 2009 12:59:52 +0000
parents 7c433d89bd4c
children c3c96e15571c
line wrap: on
line source

;;; org-exp-blocks.el --- pre-process blocks when exporting org files

;; Copyright (C) 2009
;;   Free Software Foundation, Inc.

;; Author: Eric Schulte

;; This file is part of GNU Emacs.
;;
;; GNU Emacs 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 of the License, or
;; (at your option) any later version.

;; GNU Emacs 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.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:
;;
;; This is a utility for pre-processing blocks in org files before
;; export using the `org-export-preprocess-hook'.  It can be used for
;; exporting new types of blocks from org-mode files and also for
;; changing the default export behavior of existing org-mode blocks.
;; The `org-export-blocks' and `org-export-interblocks' variables can
;; be used to control how blocks and the spaces between blocks
;; respectively are processed upon export.
;;
;; The type of a block is defined as the string following =#+begin_=,
;; so for example the following block would be of type ditaa.  Note
;; that both upper or lower case are allowed in =#+BEGIN_= and
;; =#+END_=.
;;
;; #+begin_ditaa blue.png -r -S
;; +---------+
;; | cBLU    |
;; |         |
;; |    +----+
;; |    |cPNK|
;; |    |    |
;; +----+----+
;; #+end_ditaa
;;
;;; Currently Implemented Block Types
;;
;; ditaa :: Convert ascii pictures to actual images using ditaa
;;          http://ditaa.sourceforge.net/.  To use this set
;;          `org-ditaa-jar-path' to the path to ditaa.jar on your
;;          system (should be set automatically in most cases) .
;;
;; dot :: Convert graphs defined using the dot graphing language to
;;        images using the dot utility.  For information on dot see
;;        http://www.graphviz.org/
;;
;; comment :: Wrap comments with titles and author information, in
;;            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
;; along the same lines as `org-export-blocks-format-dot' and then use
;; `org-export-blocks-add-block' to add your block type to
;; `org-export-blocks'.

(eval-when-compile
  (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 "ess" nil)

(defun org-export-blocks-set (var value)
  "Set the value of `org-export-blocks' and install fontification."
  (set var value)
  (mapc (lambda (spec)
	  (if (nth 2 spec)
	      (setq org-protecting-blocks
		    (delete (symbol-name (car spec))
			    org-protecting-blocks))
	    (add-to-list 'org-protecting-blocks
			 (symbol-name (car spec)))))
	value))

(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))
  "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.
Each block export function should accept three argumets..."
  :group 'org-export-general
  :type '(repeat
	  (list
	   (symbol :tag "Block name")
	   (function :tag "Block formatter")
	   (boolean :tag "Fontify content as Org syntax")))
  :set 'org-export-blocks-set)

(defun org-export-blocks-add-block (block-spec)
  "Add a new block type to `org-export-blocks'.  BLOCK-SPEC
should be a three element list the first element of which should
indicate the name of the block, the second element should be the
formatting function called by `org-export-blocks-preprocess' and
the third element a flag indicating whether these types of blocks
should be fontified in org-mode buffers (see
`org-protecting-blocks').  For example the BLOCK-SPEC for ditaa
blocks is as follows...

  (ditaa org-export-blocks-format-ditaa nil)"
  (unless (member block-spec org-export-blocks)
    (setq org-export-blocks (cons block-spec org-export-blocks))
    (org-export-blocks-set 'org-export-blocks org-export-blocks)))

(defcustom org-export-interblocks
  '((r org-export-interblocks-format-R)
    (R org-export-interblocks-format-R))
  "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.
Each block export function should accept three argumets..."
  :group 'org-export-general
  :type 'alist)

(defcustom org-export-blocks-witheld
  '(hidden)
  "List of block types (see `org-export-blocks') which should not
be exported."
  :group 'org-export-general
  :type 'list)

(defvar org-export-blocks-postblock-hooks nil "")

(defun org-export-blocks-html-quote (body &optional open close)
  "Protext BODY from org html export.  The optional OPEN and
CLOSE tags will be inserted around BODY."
  (concat
   "\n#+BEGIN_HTML\n"
   (or open "")
   body (if (string-match "\n$" body) "" "\n")
   (or close "")
   "#+END_HTML\n"))

(defun org-export-blocks-latex-quote (body &optional open close)
  "Protext BODY from org latex export.  The optional OPEN and
CLOSE tags will be inserted around BODY."
  (concat
   "\n#+BEGIN_LaTeX\n"
   (or open "")
   body (if (string-match "\n$" body) "" "\n")
   (or close "")
   "#+END_LaTeX\n"))

(defun org-export-blocks-preprocess ()
  "Export all blocks acording to the `org-export-blocks' block
exportation alist.  Does not export block types specified in
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)
	  (types '())
	  indentation type func start end)
      (flet ((interblock (start end type)
			 (save-match-data
			   (when (setf func (cadr (assoc type org-export-interblocks)))
			     (funcall func start end)))))
	(goto-char (point-min))
	(setf 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 type)
	  (if (setf func (cadr (assoc type org-export-blocks)))
	      (progn
                (replace-match (save-match-data
                                 (if (memq type blocks)
                                     ""
                                   (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))))
	(mapcar (lambda (type)
		  (interblock start (point-max) type))
		types)))))

(add-hook 'org-export-preprocess-hook 'org-export-blocks-preprocess)

;;================================================================================
;; type specific functions

;;--------------------------------------------------------------------------------
;; ditaa: create images from ASCII art using the ditaa utility
(defvar org-ditaa-jar-path (expand-file-name
			    "ditaa.jar"
			    (file-name-as-directory
			     (expand-file-name
			      "scripts"
			      (file-name-as-directory
			       (expand-file-name
				"../contrib"
				(file-name-directory (or load-file-name buffer-file-name)))))))
  "Path to the ditaa jar executable")

(defun org-export-blocks-format-ditaa (body &rest headers)
  "Pass block BODY to the ditaa utility creating an image.
Specify the path at which the image should be saved as the first
element of headers, any additional elements of headers will be
passed to the ditaa utility as command line arguments."
  (message "ditaa-formatting...")
  (let ((out-file (if headers (car headers)))
	(args (if (cdr headers) (mapconcat 'identity (cdr headers) " ")))
	(data-file (make-temp-file "org-ditaa")))
    (unless (file-exists-p org-ditaa-jar-path)
      (error (format "Could not find ditaa.jar at %s" org-ditaa-jar-path)))
    (setq body (if (string-match "^\\([^:\\|:[^ ]\\)" body)
		   body
		 (mapconcat (lambda (x) (substring x (if (> (length x) 1) 2 1)))
			    (org-split-string body "\n")
			    "\n")))
    (cond
     ((or htmlp latexp docbookp)
      (with-temp-file data-file (insert body))
      (message (concat "java -jar " org-ditaa-jar-path " " args " " data-file " " out-file))
      (shell-command (concat "java -jar " org-ditaa-jar-path " " args " " data-file " " out-file))
      (format "\n[[file:%s]]\n" out-file))
     (t (concat
	 "\n#+BEGIN_EXAMPLE\n"
	 body (if (string-match "\n$" body) "" "\n")
	 "#+END_EXAMPLE\n")))))

;;--------------------------------------------------------------------------------
;; dot: create graphs using the dot graphing language
;;      (require the dot executable to be in your path)
(defun org-export-blocks-format-dot (body &rest headers)
  "Pass block BODY to the dot graphing utility creating an image.
Specify the path at which the image should be saved as the first
element of headers, any additional elements of headers will be
passed to the dot utility as command line arguments.  Don't
forget to specify the output type for the dot command, so if you
are exporting to a file with a name like 'image.png' you should
include a '-Tpng' argument, and your block should look like the
following.

#+begin_dot models.png -Tpng
digraph data_relationships {
  \"data_requirement\" [shape=Mrecord, label=\"{DataRequirement|description\lformat\l}\"]
  \"data_product\" [shape=Mrecord, label=\"{DataProduct|name\lversion\lpoc\lformat\l}\"]
  \"data_requirement\" -> \"data_product\"
}
#+end_dot"
  (message "dot-formatting...")
  (let ((out-file (if headers (car headers)))
	(args (if (cdr headers) (mapconcat 'identity (cdr headers) " ")))
	(data-file (make-temp-file "org-ditaa")))
    (cond
     ((or htmlp latexp docbookp)
      (with-temp-file data-file (insert body))
      (message (concat "dot " data-file " " args " -o " out-file))
      (shell-command (concat "dot " data-file " " args " -o " out-file))
      (format "\n[[file:%s]]\n" out-file))
     (t (concat
	 "\n#+BEGIN_EXAMPLE\n"
	 body (if (string-match "\n$" body) "" "\n")
	 "#+END_EXAMPLE\n")))))

;;--------------------------------------------------------------------------------
;; comment: export comments in author-specific css-stylable divs
(defun org-export-blocks-format-comment (body &rest headers)
  "Format comment BODY by OWNER and return it formatted for export.
Currently, this only does something for HTML export, for all
other backends, it converts the comment into an EXAMPLE segment."
  (let ((owner (if headers (car headers)))
	(title (if (cdr headers) (mapconcat 'identity (cdr headers) " "))))
    (cond
     (htmlp ;; We are exporting to HTML
      (concat "#+BEGIN_HTML\n"
	      "<div class=\"org-comment\""
	      (if owner (format " id=\"org-comment-%s\" " owner))
	      ">\n"
	      (if owner (concat "<b>" owner "</b> ") "")
	      (if (and title (> (length title) 0)) (concat " -- " title "</br>\n") "</br>\n")
	      "<p>\n"
	      "#+END_HTML\n"
	      body
	      "#+BEGIN_HTML\n"
	      "</p>\n"
	      "</div>\n"
	      "#+END_HTML\n"))
     (t ;; This is not HTML, so just make it an example.
      (concat "#+BEGIN_EXAMPLE\n"
	      (if title (concat "Title:" title "\n") "")
	      (if owner (concat "By:" owner "\n") "")
	      body
	      (if (string-match "\n\\'" body) "" "\n")
	      "#+END_EXAMPLE\n")))))

;;--------------------------------------------------------------------------------
;; R: Sweave-type functionality
(defvar interblock-R-buffer nil
  "Holds the buffer for the current R process")

(defvar count) ; dynamicaly scoped from `org-export-blocks-preprocess'?
(defun org-export-blocks-format-R (body &rest headers)
  "Process R blocks and replace \R{} forms outside the blocks
with their values as determined by R."
  (interactive)
  (message "R processing...")
  (let ((image-path (or (and (car headers)
			     (string-match "\\(.?\\)\.\\(EPS\\|eps\\)" (car headers))
			     (match-string 1 (car headers)))
			(and (> (length (car headers)) 0)
			     (car headers))
			;; create the default filename
			(format "Rplot-%03d" count)))
	(plot (string-match "plot" body))
	R-proc)
    (setf count (+ count 1))
    (interblock-initiate-R-buffer)
    (setf R-proc (get-buffer-process interblock-R-buffer))
    ;; send strings to the ESS process using `comint-send-string'
    (setf body (mapconcat (lambda (line)
			    (interblock-R-input-command line) (concat "> " line))
			  (butlast (split-string body "[\r\n]"))
			  "\n"))
    ;; if there is a plot command, then create the images
    (when plot
      (interblock-R-input-command (format "dev.copy2eps(file=\"%s.eps\")" image-path)))
    (concat (cond
	     (htmlp (org-export-blocks-html-quote body
						  (format "<div id=\"R-%d\">\n<pre>\n" count)
						  "</pre>\n</div>\n"))
	     (latexp (org-export-blocks-latex-quote body
						    "\\begin{Schunk}\n\\begin{Sinput}\n"
						    "\\end{Sinput}\n\\end{Schunk}\n"))
	     (t (insert ;; default export
		 "#+begin_R " (mapconcat 'identity headers " ") "\n"
		 body (if (string-match "\n$" body) "" "\n")
		 "#+end_R\n")))
	    (if plot
		(format "[[file:%s.eps]]\n" image-path)
	      ""))))

(defun org-export-interblocks-format-R (start end)
  "This is run over parts of the org-file which are between R
blocks.  Its main use is to expand the \R{stuff} chunks for
export."
  (save-excursion
    (goto-char start)
    (interblock-initiate-R-buffer)
    (let (code replacement)
      (while (and (< (point) end) (re-search-forward "\\\\R{\\(.*\\)}" end t))
	(save-match-data (setf code (match-string 1)))
	(setf replacement (interblock-R-command-to-string code))
	(setf replacement (cond
			   (htmlp replacement)
			   (latexp replacement)
			   (t replacement)))
	(setf end (+ end (- (length replacement) (length code))))
	(replace-match replacement t t)))))

(defun interblock-initiate-R-buffer ()
  "If there is not a current R process then create one."
  (unless (and (buffer-live-p interblock-R-buffer) (get-buffer interblock-R-buffer))
    (save-excursion
      (R)
      (setf interblock-R-buffer (current-buffer))
      (interblock-R-wait-for-output)
      (interblock-R-input-command ""))))

(defun interblock-R-command-to-string (command)
  "Send a command to R, and return the results as a string."
  (interblock-R-input-command command)
  (interblock-R-last-output))

(defun interblock-R-input-command (command)
  "Pass COMMAND to the R process running in `interblock-R-buffer'."
  (save-excursion
    (save-match-data
      (set-buffer interblock-R-buffer)
      (goto-char (process-mark (get-buffer-process (current-buffer))))
      (insert command)
      (comint-send-input)
      (interblock-R-wait-for-output))))

(defun interblock-R-wait-for-output ()
  "Wait until output arrives"
  (save-excursion
    (save-match-data
      (set-buffer interblock-R-buffer)
      (while (progn
	       (goto-char comint-last-input-end)
	       (not (re-search-forward comint-prompt-regexp nil t)))
	(accept-process-output (get-buffer-process (current-buffer)))))))

(defun interblock-R-last-output ()
  "Return the last R output as a string"
  (save-excursion
    (save-match-data
      (set-buffer interblock-R-buffer)
      (goto-char (process-mark (get-buffer-process (current-buffer))))
      (forward-line 0)
      (let ((raw (buffer-substring comint-last-input-end (- (point) 1))))
	(if (string-match "\n" raw)
	    raw
	  (and (string-match "\\[[[:digit:]+]\\] *\\(.*\\)$" raw)
	       (message raw)
	       (message (match-string 1 raw))
	       (match-string 1 raw)))))))

(provide 'org-exp-blocks)

;;; org-exp-blocks.el ends here