Browse Source

babel: code cleaning -- `error' and `with-current-buffer' usage for Emacs inclusion

Eric Schulte 10 years ago
parent
commit
c32d773919
9 changed files with 43 additions and 57 deletions
  1. 23 33
      lisp/ob-C.el
  2. 3 4
      lisp/ob-clojure.el
  3. 1 1
      lisp/ob-comint.el
  4. 1 1
      lisp/ob-ditaa.el
  5. 1 2
      lisp/ob-eval.el
  6. 2 3
      lisp/ob-latex.el
  7. 2 3
      lisp/ob-mscgen.el
  8. 1 2
      lisp/ob-ref.el
  9. 9 8
      lisp/ob.el

+ 23 - 33
lisp/ob-C.el

@@ -87,45 +87,35 @@ or `org-babel-execute:c++'."
   (let* ((processed-params (org-babel-process-params params))
          (tmp-src-file (make-temp-file "org-babel-C-src" nil
                                        (cond
-                                         ((equal org-babel-c-variant 'c) ".c")
-                                         ((equal org-babel-c-variant 'cpp) ".cpp"))))
+					((equal org-babel-c-variant 'c) ".c")
+					((equal org-babel-c-variant 'cpp) ".cpp"))))
          (tmp-bin-file (make-temp-file "org-babel-C-bin"))
          (tmp-out-file (make-temp-file "org-babel-C-out"))
          (cmdline (cdr (assoc :cmdline params)))
          (flags (cdr (assoc :flags params)))
          (full-body (org-babel-C-expand body params))
-         (error-buf (get-buffer-create "*Org-Babel Error Output*"))
          (compile
-          (progn
-            (with-temp-file tmp-src-file (insert full-body))
-            (with-temp-buffer
-              (org-babel-shell-command-on-region
-               (point-min) (point-max)
-               (format "%s -o %s %s %s"
-		       (cond
-			((equal org-babel-c-variant 'c) org-babel-C-compiler)
-			((equal org-babel-c-variant 'cpp) org-babel-c++-compiler))
-                       tmp-bin-file
-                       (mapconcat 'identity
-                                  (if (listp flags) flags (list flags)) " ")
-                       tmp-src-file)
-               (current-buffer) 'replace error-buf)))))
-    (if (= compile 0)
-        (org-babel-reassemble-table
-         (org-babel-read
-          (org-babel-trim
-	   (org-babel-eval
-	    (concat tmp-bin-file (if cmdline (concat " " cmdline) "")) "")))
-         (org-babel-pick-name
-	  (nth 4 processed-params) (cdr (assoc :colnames params)))
-         (org-babel-pick-name
-	  (nth 5 processed-params) (cdr (assoc :rownames params))))
-      (progn
-        (with-current-buffer error-buf
-          (goto-char (point-max))
-          (insert (concat "\n\n--body--\n" full-body))
-          (goto-char (point-min)))
-        (display-buffer error-buf) nil))))
+	  (progn
+	    (with-temp-file tmp-src-file (insert full-body))
+	    (org-babel-eval
+	     (format "%s -o %s %s %s"
+		     (cond
+		      ((equal org-babel-c-variant 'c) org-babel-C-compiler)
+		      ((equal org-babel-c-variant 'cpp) org-babel-c++-compiler))
+		     tmp-bin-file
+		     (mapconcat 'identity
+				(if (listp flags) flags (list flags)) " ")
+		     tmp-src-file) ""))))
+    (org-babel-reassemble-table
+     (org-babel-read
+      (org-babel-trim
+       (org-babel-eval
+	(concat tmp-bin-file (if cmdline (concat " " cmdline) "")) "")))
+     (org-babel-pick-name
+      (nth 4 processed-params) (cdr (assoc :colnames params)))
+     (org-babel-pick-name
+      (nth 5 processed-params) (cdr (assoc :rownames params))))))
+
 
 (defun org-babel-C-expand (body params &optional processed-params)
   "Expand a block of C or C++ code with org-babel according to

+ 3 - 4
lisp/ob-clojure.el

@@ -93,8 +93,8 @@
 (defun org-babel-clojure-babel-clojure-cmd ()
   "Create the command to start clojure according to current settings."
   (if (and (not swank-clojure-binary) (not swank-clojure-classpath))
-      (error (concat "You must specifiy either a `swank-clojure-binary' "
-		     "or a `swank-clojure-jar-path'"))
+      (error "%s" (concat "You must specifiy either a `swank-clojure-binary' "
+			  "or a `swank-clojure-jar-path'"))
     (if swank-clojure-binary
         (if (listp swank-clojure-binary)
             swank-clojure-binary
@@ -269,8 +269,7 @@ repl buffer."
   (require 'slime) (require 'swank-clojure)
   (let ((raw nil)
         (results nil))
-    (save-window-excursion
-      (set-buffer buffer)
+    (with-current-buffer buffer
       (setq raw (org-babel-clojure-slime-eval-sync body))
       (setq results (reverse (mapcar #'org-babel-trim raw)))
       (cond

+ 1 - 1
lisp/ob-comint.el

@@ -48,7 +48,7 @@ body inside the protection of `save-window-excursion' and
   `(save-excursion
      (save-match-data
        (unless (org-babel-comint-buffer-livep ,buffer)
-         (error (format "buffer %s doesn't exist or has no process" ,buffer)))
+         (error "buffer %s doesn't exist or has no process" ,buffer))
        (set-buffer ,buffer)
        ,@body)))
 

+ 1 - 1
lisp/ob-ditaa.el

@@ -56,7 +56,7 @@ called by `org-babel-execute-src-block'."
         (cmdline (cdr (assoc :cmdline params)))
         (in-file (make-temp-file "org-babel-ditaa")))
     (unless (file-exists-p org-ditaa-jar-path)
-      (error (format "Could not find ditaa.jar at %s" org-ditaa-jar-path)))
+      (error "Could not find ditaa.jar at %s" org-ditaa-jar-path))
     (with-temp-file in-file (insert body))
     (message (concat "java -jar " org-ditaa-jar-path " " cmdline " " in-file " " out-file))
     (shell-command (concat "java -jar " org-ditaa-jar-path " " cmdline " " in-file " " out-file))

+ 1 - 2
lisp/ob-eval.el

@@ -187,8 +187,7 @@ specifies the value of ERROR-BUFFER."
 	      ;; Clear the output buffer, then run the command with
 	      ;; output there.
 	      (let ((directory default-directory))
-		(save-current-buffer
-		  (set-buffer buffer)
+		(with-current-buffer buffer
 		  (setq buffer-read-only nil)
 		  (if (not output-buffer)
 		      (setq default-directory directory))

+ 2 - 3
lisp/ob-latex.el

@@ -80,9 +80,8 @@ called by `org-babel-execute-src-block'."
           (when (file-exists-p out-file) (delete-file out-file))
           (rename-file (org-babel-latex-tex-to-pdf tex-file) out-file))
          ((string-match "\\.\\([^\\.]+\\)$" out-file)
-          (error
-           (message "can not create %s files, please specify a .png or .pdf file"
-                    (match-string 1 out-file)))))
+          (error "can not create %s files, please specify a .png or .pdf file"
+		 (match-string 1 out-file))))
         out-file)
     body))
 

+ 2 - 3
lisp/ob-mscgen.el

@@ -74,9 +74,8 @@ formats."
   (let* ((out-file (or (cdr (assoc :file params)) "output.png" ))
          (filetype (or (cdr (assoc :filetype params)) "png" )))
     (unless (cdr (assoc :file params))
-      (error (concat
-	      "\nERROR: no output file specified. "
-	      "Add \":file some_name.png\" to the src header")))
+      (error "
+ERROR: no output file specified. Add \":file name.png\" to the src header"))
     (org-babel-eval (concat "mscgen -T " filetype " -o " out-file) body)
     out-file))
 

+ 1 - 2
lisp/ob-ref.el

@@ -150,8 +150,7 @@ return nil."
 	  ;;       buffer (marker-buffer id-loc)
 	  ;;       loc (marker-position id-loc))
 	  ;; (move-marker id-loc nil)
-	  (progn (message (format "reference '%s' not found in this buffer" ref))
-		 (error (format "reference '%s' not found in this buffer" ref))))
+	  (error "reference '%s' not found in this buffer" ref))
 	(if lob-info
 	    (setq type 'lob)
 	  (while (not (setq type (org-babel-ref-at-ref-p)))

+ 9 - 8
lisp/ob.el

@@ -1323,9 +1323,8 @@ block but are passed literally to the \"example-block\"."
           (nb-add (buffer-substring index (point)))
           (goto-char (match-end 0))
           (setq index (point))
-          (nb-add (save-current-buffer
-                    (set-buffer parent-buffer)
-                    (mapconcat ;; interpose `prefix' between every line
+          (nb-add (with-current-buffer parent-buffer
+		    (mapconcat ;; interpose PREFIX between every line
                      #'identity
                      (split-string
                       (if evaluate
@@ -1334,7 +1333,8 @@ block but are passed literally to the \"example-block\"."
                             (if (stringp raw) raw (format "%S" raw)))
 			(save-restriction
 			  (widen)
-			  (let ((point (org-babel-find-named-block source-name)))
+			  (let ((point (org-babel-find-named-block
+					source-name)))
 			    (if point
 				(save-excursion
 				  (goto-char point)
@@ -1344,10 +1344,11 @@ block but are passed literally to the \"example-block\"."
 			      ;; optionally raise an error if named
 			      ;; source-block doesn't exist
 			      (if (member lang org-babel-noweb-error-langs)
-				  (error
-				   (concat "<<%s>> could not be resolved "
-					   "(see `org-babel-noweb-error-langs')")
-				   source-name)
+				  (error "%s"
+					 (concat
+					  "<<" source-name ">> "
+					  "could not be resolved (see "
+					  "`org-babel-noweb-error-langs')"))
 				"")))))
 		      "[\n\r]") (concat "\n" prefix)))))
         (nb-add (buffer-substring index (point-max)))))