| 
					
				 | 
			
			
				@@ -1,4 +1,4 @@ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-;;; ob-julia.el --- org-babel functions for julia code evaluation 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+;;; ob-julia.el --- org-babel functions for julia code evaluation  -*- lexical-binding: t; -*- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 ;; Copyright (C) 2013-2021 Free Software Foundation, Inc. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 ;; Authors: G. Jay Kerns, based on ob-R.el by Eric Schulte and Dan Davison 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -44,7 +44,7 @@ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 			    (raw org html latex code pp wrap) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 			    (replace silent append prepend) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 			    (output value graphics)))) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  "julia-specific header arguments.") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  "Julia-specific header arguments.") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 (add-to-list 'org-babel-tangle-lang-exts '("julia" . "jl")) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -58,7 +58,8 @@ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   :type 'string) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 (defvar ess-current-process-name) ; dynamically scoped 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-(defvar ess-local-process-name) ; dynamically scoped 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+(defvar ess-local-process-name)   ; dynamically scoped 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+(defvar ess-eval-visibly-p)       ; dynamically scoped 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 (defun org-babel-edit-prep:julia (info) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   (let ((session (cdr (assq :session (nth 2 info))))) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     (when (and session 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -68,7 +69,7 @@ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 (defun org-babel-expand-body:julia (body params &optional _graphics-file) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   "Expand BODY according to PARAMS, return the expanded body." 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  (mapconcat 'identity 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  (mapconcat #'identity 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	     (append 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	      (when (cdr (assq :prologue params)) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		(list (cdr (assq :prologue params)))) 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -89,7 +90,7 @@ This function is called by `org-babel-execute-src-block'." 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	   (graphics-file (and (member "graphics" (assq :result-params params)) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 			       (org-babel-graphical-output-file params))) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	   (colnames-p (unless graphics-file (cdr (assq :colnames params)))) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	   (rownames-p (unless graphics-file (cdr (assq :rownames params)))) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	   ;; (rownames-p (unless graphics-file (cdr (assq :rownames params)))) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	   (full-body (org-babel-expand-body:julia body params graphics-file)) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	   (result 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	    (org-babel-julia-evaluate 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -97,9 +98,10 @@ This function is called by `org-babel-execute-src-block'." 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	     (or (equal "yes" colnames-p) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		 (org-babel-pick-name 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		  (cdr (assq :colname-names params)) colnames-p)) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	     (or (equal "yes" rownames-p) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		 (org-babel-pick-name 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		  (cdr (assq :rowname-names params)) rownames-p))))) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	     ;; (or (equal "yes" rownames-p) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	     ;;     (org-babel-pick-name 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	     ;;      (cdr (assq :rowname-names params)) rownames-p)) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	     ))) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				       (if graphics-file nil result)))) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 (defun org-babel-normalize-newline (result) 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -136,8 +138,9 @@ This function is called by `org-babel-execute-src-block'." 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				      (lambda (pair) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        (org-babel-julia-assign-elisp 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	(car pair) (cdr pair) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	(equal "yes" (cdr (assq :colnames params))) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	(equal "yes" (cdr (assq :rownames params))))) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	;; (equal "yes" (cdr (assq :colnames params))) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	;; (equal "yes" (cdr (assq :rownames params))) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	)) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				      (mapcar 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				       (lambda (i) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	(cons (car (nth i vars)) 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -150,21 +153,22 @@ This function is called by `org-babel-execute-src-block'." 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 (defun org-babel-julia-quote-csv-field (s) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   "Quote field S for export to julia." 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   (if (stringp s) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      (concat "\"" (mapconcat #'identity (split-string s "\"") "\"\"") "\"") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     (format "%S" s))) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-(defun org-babel-julia-assign-elisp (name value colnames-p rownames-p) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+(defun org-babel-julia-assign-elisp (name value) ;; colnames-p rownames-p 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   "Construct julia code assigning the elisp VALUE to a variable named NAME." 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   (if (listp value) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      (let* ((lengths (mapcar 'length (cl-remove-if-not 'sequencep value))) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-             (max (if lengths (apply 'max lengths) 0)) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-             (min (if lengths (apply 'min lengths) 0))) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      (let* ((lengths (mapcar #'length (cl-remove-if-not #'sequencep value))) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+             (max (if lengths (apply #'max lengths) 0)) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+             (min (if lengths (apply #'min lengths) 0))) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         ;; Ensure VALUE has an orgtbl structure (depth of at least 2). 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         (unless (listp (car value)) (setq value (list value))) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         (let ((file (orgtbl-to-csv value '(:fmt org-babel-julia-quote-csv-field))) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-              (header (if (or (eq (nth 1 value) 'hline) colnames-p) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                          "TRUE" "FALSE")) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-              (row-names (if rownames-p "1" "NULL"))) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+              ;; (header (if (or (eq (nth 1 value) 'hline) colnames-p) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+              ;;             "TRUE" "FALSE")) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+              ;; (row-names (if rownames-p "1" "NULL")) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+              ) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				           (if (= max min) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				               (format "%s = begin 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     using CSV 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -183,11 +187,15 @@ end" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   (unless (string= session "none") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     (let ((session (or session "*Julia*")) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	  (ess-ask-for-ess-directory 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	   (and (boundp 'ess-ask-for-ess-directory) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		ess-ask-for-ess-directory 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	   (and (bound-and-true-p ess-ask-for-ess-directory) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		(not (cdr (assq :dir params)))))) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				       (if (org-babel-comint-buffer-livep session) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	  session 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	;; FIXME: Depending on `display-buffer-alist', (julia) may end up 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        ;; popping up a new frame which `save-window-excursion' won't be able 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        ;; to "undo", so we really should call a kind of 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        ;; `julia-no-select' instead so we don't need to undo any 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        ;; window-changes afterwards. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	(save-window-excursion 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	  (when (get-buffer session) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	    ;; Session buffer exists, but with dead process 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -251,16 +259,16 @@ end" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 end") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 (defun org-babel-julia-evaluate 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    (session body result-type result-params column-names-p row-names-p) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    (session body result-type result-params column-names-p) ;; row-names-p 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   "Evaluate julia code in BODY." 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   (if session 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				       (org-babel-julia-evaluate-session 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-       session body result-type result-params column-names-p row-names-p) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+       session body result-type result-params column-names-p) ;; row-names-p 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     (org-babel-julia-evaluate-external-process 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-     body result-type result-params column-names-p row-names-p))) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     body result-type result-params column-names-p))) ;; row-names-p 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 (defun org-babel-julia-evaluate-external-process 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    (body result-type result-params column-names-p row-names-p) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    (body result-type result-params column-names-p) ;; row-names-p 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   "Evaluate BODY in external julia process. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 If RESULT-TYPE equals 'output then return standard output as a 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 string.  If RESULT-TYPE equals 'value then return the value of the 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -284,7 +292,7 @@ last statement in BODY, as elisp." 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     (output (org-babel-eval org-babel-julia-command body)))) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 (defun org-babel-julia-evaluate-session 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    (session body result-type result-params column-names-p row-names-p) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    (session body result-type result-params column-names-p) ;; row-names-p 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   "Evaluate BODY in SESSION. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 If RESULT-TYPE equals 'output then return standard output as a 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 string.  If RESULT-TYPE equals 'value then return the value of the 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -314,7 +322,7 @@ last statement in BODY, as elisp." 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	column-names-p))) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     (output 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				      (mapconcat 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      'org-babel-chomp 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      #'org-babel-chomp 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				       (butlast 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        (delq nil 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	     (mapcar 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -327,13 +335,14 @@ last statement in BODY, as elisp." 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		     (substring line (match-end 1)) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		   line)) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	       (org-babel-comint-with-output (session org-babel-julia-eoe-output) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		 (insert (mapconcat 'org-babel-chomp 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		 (insert (mapconcat #'org-babel-chomp 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 				    (list body org-babel-julia-eoe-indicator) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 				    "\n")) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                 (inferior-ess-send-input)))))) "\n")))) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                 (inferior-ess-send-input)))))) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      "\n")))) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 (defun org-babel-julia-process-value-result (result column-names-p) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  "julia-specific processing of return value. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  "Julia-specific processing of return value. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 Insert hline if column names in output have been requested." 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   (if column-names-p 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				       (cons (car result) (cons 'hline (cdr result))) 
			 |