Merge.
[bpt/emacs.git] / lisp / org / org-exp-blocks.el
index 90cb610..398da38 100644 (file)
@@ -1,10 +1,9 @@
 ;;; org-exp-blocks.el --- pre-process blocks when exporting org files
 
-;; Copyright (C) 2009, 2010
-;;   Free Software Foundation, Inc.
+;; Copyright (C) 2009-2011  Free Software Foundation, Inc.
 
 ;; Author: Eric Schulte
-;; Version: 6.35i
+;; Version: 7.7
 
 ;; This file is part of GNU Emacs.
 ;;
 ;;
 ;;; Currently Implemented Block Types
 ;;
-;; ditaa :: Convert ascii pictures to actual images using ditaa
+;; ditaa :: (DEPRECATED--use "#+begin_src ditaa" code blocks) 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
+;; dot :: (DEPRECATED--use "#+begin_src dot" code blocks) 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
 ;; `org-export-blocks-add-block' to add your block type to
 ;; `org-export-blocks'.
 
+;;; Code:
+
 (eval-when-compile
   (require 'cl))
 (require 'org)
 
-(defvar htmlp)
-(defvar latexp)
-(defvar docbookp)
-(defvar asciip)
-
 (defun org-export-blocks-set (var value)
   "Set the value of `org-export-blocks' and install fontification."
   (set var value)
   '((comment org-export-blocks-format-comment t)
     (ditaa org-export-blocks-format-ditaa 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.
-Each block export function should accept three argumets..."
+  "Use this alist 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 arguments."
   :group 'org-export-general
   :type '(repeat
          (list
@@ -106,14 +104,14 @@ Each block export function should accept three argumets..."
   :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...
+  "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)
@@ -122,25 +120,28 @@ blocks is as follows...
 
 (defcustom org-export-interblocks
   '()
-  "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..."
+  "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 arguments."
   :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."
+  "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 "")
+(defcustom org-export-blocks-postblock-hook nil
+  "Run after blocks have been processed with `org-export-blocks-preprocess'."
+  :group 'org-export-general
+  :type 'hook)
 
 (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."
+  "Protect BODY from org html export.
+The optional OPEN and CLOSE tags will be inserted around BODY."
+
   (concat
    "\n#+BEGIN_HTML\n"
    (or open "")
@@ -149,8 +150,8 @@ CLOSE tags will be inserted around BODY."
    "#+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."
+  "Protect BODY from org latex export.
+The optional OPEN and CLOSE tags will be inserted around BODY."
   (concat
    "\n#+BEGIN_LaTeX\n"
    (or open "")
@@ -159,44 +160,61 @@ CLOSE tags will be inserted around BODY."
    "#+END_LaTeX\n"))
 
 (defun org-export-blocks-preprocess ()
-  "Export all blocks according 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'."
+  "Export all blocks according to the `org-export-blocks' block export alist.
+Does not export block types specified in specified in BLOCKS
+which defaults to the value of `org-export-blocks-witheld'."
   (interactive)
   (save-window-excursion
     (let ((case-fold-search t)
          (types '())
-         indentation type func start body headers preserve-indent progress-marker)
+         matched indentation type func
+         start end body headers preserve-indent progress-marker)
       (flet ((interblock (start end)
                         (mapcar (lambda (pair) (funcall (second pair) start end))
                                 org-export-interblocks)))
        (goto-char (point-min))
        (setq start (point))
-       (while (re-search-forward
-               "^\\([ \t]*\\)#\\+begin_\\(\\S-+\\)[ \t]*\\(.*\\)?[\r\n]\\([^\000]*?\\)[\r\n][ \t]*#\\+end_\\S-+.*" nil t)
-          (setq indentation (length (match-string 1)))
-         (setq type (intern (downcase (match-string 2))))
-         (setq headers (save-match-data (org-split-string (match-string 3) "[ \t]+")))
-         (setq body (match-string 4))
-         (setq preserve-indent (or org-src-preserve-indentation (member "-i" headers)))
-         (unless preserve-indent
-           (setq body (save-match-data (org-remove-indentation body))))
-         (unless (memq type types) (setq types (cons type types)))
-         (save-match-data (interblock start (match-beginning 0)))
-         (when (setq func (cadr (assoc type org-export-blocks)))
-            (let ((replacement (save-match-data
-                                 (if (memq type org-export-blocks-witheld) ""
-                                   (apply func body headers)))))
-              (when replacement
-                (replace-match replacement t t)
-                (unless preserve-indent
-                  (indent-code-rigidly
-                   (match-beginning 0) (match-end 0) indentation)))))
-         (setq start (match-end 0)))
-       (interblock start (point-max))))))
-
-(add-hook 'org-export-preprocess-hook 'org-export-blocks-preprocess)
+       (let ((beg-re "^\\([ \t]*\\)#\\+begin_\\(\\S-+\\)[ \t]*\\(.*\\)?[\r\n]"))
+         (while (re-search-forward beg-re nil t)
+           (let* ((match-start (match-beginning 0))
+                  (body-start (match-end 0))
+                  (indentation (length (match-string 1)))
+                  (inner-re (format "[\r\n]*[ \t]*#\\+\\(begin\\|end\\)_%s"
+                                    (regexp-quote (downcase (match-string 2)))))
+                  (type (intern (downcase (match-string 2))))
+                  (headers (save-match-data
+                             (org-split-string (match-string 3) "[ \t]+")))
+                  (balanced 1)
+                  (preserve-indent (or org-src-preserve-indentation
+                                       (member "-i" headers)))
+                  match-end)
+             (while (and (not (zerop balanced))
+                         (re-search-forward inner-re nil t))
+               (if (string= (downcase (match-string 1)) "end")
+                   (decf balanced)
+                 (incf balanced)))
+             (when (not (zerop balanced))
+               (error "unbalanced begin/end_%s blocks with %S"
+                      type (buffer-substring match-start (point))))
+             (setq match-end (match-end 0))
+             (unless preserve-indent
+               (setq body (save-match-data (org-remove-indentation
+                                            (buffer-substring
+                                             body-start (match-beginning 0))))))
+             (unless (memq type types) (setq types (cons type types)))
+             (save-match-data (interblock start match-start))
+             (when (setq func (cadr (assoc type org-export-blocks)))
+               (let ((replacement (save-match-data
+                                    (if (memq type org-export-blocks-witheld) ""
+                                      (apply func body headers)))))
+                 (when replacement
+                   (delete-region match-start match-end)
+                   (goto-char match-start) (insert replacement)
+                   (unless preserve-indent
+                     (indent-code-rigidly match-start (point) indentation))))))
+           (setq start (point))))
+       (interblock start (point-max))
+       (run-hooks 'org-export-blocks-postblock-hook)))))
 
 ;;================================================================================
 ;; type specific functions
@@ -212,23 +230,28 @@ specified in BLOCKS which default to the value of
                               (expand-file-name
                                "../contrib"
                                (file-name-directory (or load-file-name buffer-file-name)))))))
-  "Path to the ditaa jar executable")
+  "Path to the ditaa jar executable.")
 
+(defvar org-export-current-backend) ; dynamically bound in org-exp.el
 (defun org-export-blocks-format-ditaa (body &rest headers)
-  "Pass block BODY to the ditaa utility creating an image.
+  "DEPRECATED: use begin_src ditaa code blocks
+
+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...")
+  (message "begin_ditaa blocks are DEPRECATED, use begin_src blocks")
   (let* ((args (if (cdr headers) (mapconcat 'identity (cdr headers) " ")))
          (data-file (make-temp-file "org-ditaa"))
-         (hash (sha1 (prin1-to-string (list body args))))
-         (raw-out-file (if headers (car headers)))
-         (out-file-parts (if (string-match "\\(.+\\)\\.\\([^\\.]+\\)$" raw-out-file)
-                             (cons (match-string 1 raw-out-file)
-                                   (match-string 2 raw-out-file))
-                           (cons raw-out-file "png")))
-         (out-file (concat (car out-file-parts) "_" hash "." (cdr out-file-parts))))
+        (hash (progn
+                (set-text-properties 0 (length body) nil body)
+                (sha1 (prin1-to-string (list body args)))))
+        (raw-out-file (if headers (car headers)))
+        (out-file-parts (if (string-match "\\(.+\\)\\.\\([^\\.]+\\)$" raw-out-file)
+                            (cons (match-string 1 raw-out-file)
+                                  (match-string 2 raw-out-file))
+                          (cons raw-out-file "png")))
+        (out-file (concat (car out-file-parts) "_" hash "." (cdr out-file-parts))))
     (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)
@@ -236,8 +259,9 @@ passed to the ditaa utility as command line arguments."
                 (mapconcat (lambda (x) (substring x (if (> (length x) 1) 2 1)))
                            (org-split-string body "\n")
                            "\n")))
+    (prog1
     (cond
-     ((or htmlp latexp docbookp)
+     ((member org-export-current-backend '(html latex docbook))
       (unless (file-exists-p out-file)
         (mapc ;; remove old hashed versions of this file
          (lambda (file)
@@ -257,13 +281,16 @@ passed to the ditaa utility as command line arguments."
      (t (concat
         "\n#+BEGIN_EXAMPLE\n"
         body (if (string-match "\n$" body) "" "\n")
-        "#+END_EXAMPLE\n")))))
+        "#+END_EXAMPLE\n")))
+    (message "begin_ditaa blocks are DEPRECATED, use begin_src blocks"))))
 
 ;;--------------------------------------------------------------------------------
 ;; 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.
+  "DEPRECATED: use \"#+begin_src dot\" code blocks
+
+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
@@ -279,38 +306,42 @@ digraph data_relationships {
   \"data_requirement\" -> \"data_product\"
 }
 #+end_dot"
-  (message "dot-formatting...")
+  (message "begin_dot blocks are DEPRECATED, use begin_src blocks")
   (let* ((args (if (cdr headers) (mapconcat 'identity (cdr headers) " ")))
          (data-file (make-temp-file "org-ditaa"))
-         (hash (sha1 (prin1-to-string (list body args))))
-         (raw-out-file (if headers (car headers)))
-         (out-file-parts (if (string-match "\\(.+\\)\\.\\([^\\.]+\\)$" raw-out-file)
-                             (cons (match-string 1 raw-out-file)
-                                   (match-string 2 raw-out-file))
-                           (cons raw-out-file "png")))
-         (out-file (concat (car out-file-parts) "_" hash "." (cdr out-file-parts))))
+        (hash (progn
+                (set-text-properties 0 (length body) nil body)
+                (sha1 (prin1-to-string (list body args)))))
+        (raw-out-file (if headers (car headers)))
+        (out-file-parts (if (string-match "\\(.+\\)\\.\\([^\\.]+\\)$" raw-out-file)
+                            (cons (match-string 1 raw-out-file)
+                                  (match-string 2 raw-out-file))
+                          (cons raw-out-file "png")))
+        (out-file (concat (car out-file-parts) "_" hash "." (cdr out-file-parts))))
+    (prog1
     (cond
-     ((or htmlp latexp docbookp)
+     ((member org-export-current-backend '(html latex docbook))
       (unless (file-exists-p out-file)
-        (mapc ;; remove old hashed versions of this file
-         (lambda (file)
-           (when (and (string-match (concat (regexp-quote (car out-file-parts))
-                                            "_\\([[:alnum:]]+\\)\\."
-                                            (regexp-quote (cdr out-file-parts)))
-                                    file)
-                      (= (length (match-string 1 out-file)) 40))
-             (delete-file (expand-file-name file
-                                            (file-name-directory out-file)))))
-         (directory-files (or (file-name-directory out-file)
-                              default-directory)))
-        (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)))
+       (mapc ;; remove old hashed versions of this file
+        (lambda (file)
+          (when (and (string-match (concat (regexp-quote (car out-file-parts))
+                                           "_\\([[:alnum:]]+\\)\\."
+                                           (regexp-quote (cdr out-file-parts)))
+                                   file)
+                     (= (length (match-string 1 out-file)) 40))
+            (delete-file (expand-file-name file
+                                           (file-name-directory out-file)))))
+        (directory-files (or (file-name-directory out-file)
+                             default-directory)))
+       (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")))))
+        "#+END_EXAMPLE\n")))
+    (message "begin_dot blocks are DEPRECATED, use begin_src blocks"))))
 
 ;;--------------------------------------------------------------------------------
 ;; comment: export comments in author-specific css-stylable divs
@@ -321,17 +352,17 @@ 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
+     ((eq org-export-current-backend 'html) ;; 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")
+             (if (and title (> (length title) 0)) (concat " -- " title "<br/>\n") "<br/>\n")
              "<p>\n"
              "#+END_HTML\n"
              body
-             "#+BEGIN_HTML\n"
+             "\n#+BEGIN_HTML\n"
              "</p>\n"
              "</div>\n"
              "#+END_HTML\n"))
@@ -345,5 +376,5 @@ other backends, it converts the comment into an EXAMPLE segment."
 
 (provide 'org-exp-blocks)
 
-;; arch-tag: 1c365fe9-8808-4f72-bb15-0b00f36d8024
+
 ;;; org-exp-blocks.el ends here