]> code.delx.au - gnu-emacs/blobdiff - lisp/org/org-exp-blocks.el
Auto-commit of generated files.
[gnu-emacs] / lisp / org / org-exp-blocks.el
index 57087e1dfc7aa295a30d314915243d135236e29a..d3789ad3aa83a65d950fb65994c5da63b83cbbce 100644 (file)
@@ -1,10 +1,8 @@
 ;;; org-exp-blocks.el --- pre-process blocks when exporting org files
 
-;; Copyright (C) 2009, 2010
-;;   Free Software Foundation, Inc.
+;; Copyright (C) 2009-2013 Free Software Foundation, Inc.
 
 ;; Author: Eric Schulte
-;; Version: 7.4
 
 ;; 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
-;;            their own divs with author-specific ids allowing for css
-;;            coloring of comments based on the author.
+;; export-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.
 ;;
 ;;; Adding new blocks
 ;;
 
 (eval-when-compile
   (require 'cl))
-(require 'org)
+(require 'find-func)
+(require 'org-compat)
 
-(defvar htmlp)
-(defvar latexp)
-(defvar docbookp)
-(defvar asciip)
+(declare-function org-split-string "org" (string &optional separators))
+(declare-function org-remove-indentation "org" (code &optional n))
+
+(defvar org-protecting-blocks nil) ; From org.el
 
 (defun org-export-blocks-set (var value)
   "Set the value of `org-export-blocks' and install fontification."
@@ -92,7 +93,7 @@
        value))
 
 (defcustom org-export-blocks
-  '((comment org-export-blocks-format-comment t)
+  '((export-comment org-export-blocks-format-comment t)
     (ditaa org-export-blocks-format-ditaa nil)
     (dot org-export-blocks-format-dot nil))
   "Use this alist to associate block types with block exporting functions.
@@ -140,12 +141,12 @@ export function should accept three arguments."
 (defcustom org-export-blocks-postblock-hook nil
   "Run after blocks have been processed with `org-export-blocks-preprocess'."
   :group 'org-export-general
+  :version "24.1"
   :type 'hook)
 
 (defun org-export-blocks-html-quote (body &optional open close)
   "Protect BODY from org html export.
 The optional OPEN and CLOSE tags will be inserted around BODY."
-
   (concat
    "\n#+BEGIN_HTML\n"
    (or open "")
@@ -163,6 +164,7 @@ The optional OPEN and CLOSE tags will be inserted around BODY."
    (or close "")
    "#+END_LaTeX\n"))
 
+(defvar org-src-preserve-indentation)     ; From org-src.el
 (defun org-export-blocks-preprocess ()
   "Export all blocks according to the `org-export-blocks' block export alist.
 Does not export block types specified in specified in BLOCKS
@@ -170,59 +172,98 @@ 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)
-      (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-+.*[\r\n]?" 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))
-       (run-hooks 'org-export-blocks-postblock-hook)))))
+         (interblock (lambda (start end)
+                       (mapcar (lambda (pair) (funcall (second pair) start end))
+                               org-export-interblocks)))
+         matched indentation type types func
+         start end body headers preserve-indent progress-marker)
+      (goto-char (point-min))
+      (setq start (point))
+      (let ((beg-re "^\\([ \t]*\\)#\\+begin_\\(\\S-+\\)[ \t]*\\(.*\\)?[\r\n]"))
+       (while (re-search-forward beg-re nil t)
+         (let* ((match-start (copy-marker (match-beginning 0)))
+                (body-start (copy-marker (match-end 0)))
+                (indentation (length (match-string 1)))
+                (inner-re (format "^[ \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 (copy-marker (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 (funcall 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)))))
+               ;; ;; un-comment this code after the org-element merge
+               ;; (save-match-data
+               ;;   (when (and replacement (string= replacement ""))
+               ;;     (delete-region
+               ;;      (car (org-element-collect-affiliated-keyword))
+               ;;      match-start)))
+               (when replacement
+                 (delete-region match-start match-end)
+                 (goto-char match-start) (insert replacement)
+                 (if preserve-indent
+                     ;; indent only the code block markers
+                     (save-excursion
+                       (indent-line-to indentation) ; indent end_block
+                       (goto-char match-start)
+                       (indent-line-to indentation))   ; indent begin_block
+                   ;; indent everything
+                   (indent-code-rigidly match-start (point) indentation)))))
+           ;; cleanup markers
+           (set-marker match-start nil)
+           (set-marker body-start nil)
+           (set-marker match-end nil))
+         (setq start (point))))
+      (funcall interblock start (point-max))
+      (run-hooks 'org-export-blocks-postblock-hook))))
 
 ;;================================================================================
 ;; 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.")
+(defcustom 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 (org-find-library-dir "org")))))))
+  "Path to the ditaa jar executable."
+  :group 'org-babel
+  :type 'string)
 
+(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 (progn
@@ -241,34 +282,38 @@ 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")))
-    (cond
-     ((or htmlp latexp docbookp)
-      (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 "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")))))
+    (prog1
+       (cond
+        ((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 "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")))
+      (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
@@ -284,7 +329,7 @@ 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 (progn
@@ -296,28 +341,30 @@ digraph data_relationships {
                                   (match-string 2 raw-out-file))
                           (cons raw-out-file "png")))
         (out-file (concat (car out-file-parts) "_" hash "." (cdr out-file-parts))))
-    (cond
-     ((or htmlp latexp docbookp)
-      (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)))
-      (format "\n[[file:%s]]\n" out-file))
-     (t (concat
-        "\n#+BEGIN_EXAMPLE\n"
-        body (if (string-match "\n$" body) "" "\n")
-        "#+END_EXAMPLE\n")))))
+    (prog1
+       (cond
+        ((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)))
+         (format "\n[[file:%s]]\n" out-file))
+        (t (concat
+            "\n#+BEGIN_EXAMPLE\n"
+            body (if (string-match "\n$" body) "" "\n")
+            "#+END_EXAMPLE\n")))
+      (message "begin_dot blocks are DEPRECATED, use begin_src blocks"))))
 
 ;;--------------------------------------------------------------------------------
 ;; comment: export comments in author-specific css-stylable divs
@@ -328,17 +375,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"))
@@ -352,5 +399,4 @@ 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