Kaynağa Gözat

Merge branch 'master' of git+ssh://repo.or.cz/srv/git/org-mode

Carsten Dominik 15 yıl önce
ebeveyn
işleme
d003caa178

+ 31 - 1
contrib/babel/lisp/langs/org-babel-haskell.el

@@ -81,9 +81,39 @@ variables pre-set using `multiple-value-bind'.
       (match-string 1 string)
     string))
 
+(defun org-babel-haskell-initiate-session (&optional session)
+  "If there is not a current inferior-process-buffer in SESSION
+then create.  Return the initialized session."
+  ;; TODO: make it possible to have multiple sessions
+  (run-haskell) (current-buffer))
+
+(defun org-babel-load-session:haskell (session body params)
+  "Load BODY into SESSION."
+  (save-window-excursion
+    (let* ((buffer (org-babel-prep-session:haskell session params))
+           (load-file (concat (make-temp-file "org-babel-haskell-load") ".hs")))
+      (with-temp-buffer
+        (insert body) (write-file load-file)
+        (haskell-mode) (inferior-haskell-load-file))
+      buffer)))
+
 (defun org-babel-prep-session:haskell (session params)
   "Prepare SESSION according to the header arguments specified in PARAMS."
-  (save-window-excursion (run-haskell) (current-buffer)))
+  (save-window-excursion
+    (org-babel-haskell-initiate-session session)
+    (let* ((vars (org-babel-ref-variables params))
+           (var-lines (mapconcat ;; define any variables
+                       (lambda (pair)
+                         (format "%s=%s"
+                                 (car pair)
+                                 (org-babel-ruby-var-to-ruby (cdr pair))))
+                       vars "\n"))
+           (vars-file (concat (make-temp-file "org-babel-haskell-vars") ".hs")))
+      (when vars
+        (with-temp-buffer
+          (insert var-lines) (write-file vars-file)
+          (haskell-mode) (inferior-haskell-load-file)))
+      (current-buffer))))
 
 (defun org-babel-haskell-table-or-string (results)
   "If the results look like a table, then convert them into an

+ 25 - 0
contrib/babel/lisp/org-babel.el

@@ -61,6 +61,15 @@ to `org-open-at-point'."
   (interactive "P")
   (or (call-interactively #'org-babel-open-src-block-result) ad-do-it))
 
+(defun org-babel-load-in-session-maybe ()
+  "Detect if this is context for a org-babel src-block and if so
+then run `org-babel-load-in-session'."
+  (interactive)
+  (let ((info (org-babel-get-src-block-info)))
+    (if info (progn (org-babel-load-in-session current-prefix-arg info) t) nil)))
+
+(add-hook 'org-metaup-hook 'org-babel-load-in-session-maybe)
+
 (defun org-babel-pop-to-session-maybe ()
   "Detect if this is context for a org-babel src-block and if so
 then run `org-babel-pop-to-session'."
@@ -181,6 +190,22 @@ the header arguments specified at the source code block."
     (org-babel-insert-result result result-params)
     (case result-type (output nil) (value result))))
 
+(defun org-babel-load-in-session (&optional arg info)
+  "Load the body of the current source-code block.  Evaluate the
+header arguments for the source block before entering the
+session.  After loading the body this pops open the session."
+  (interactive)
+  (let* ((info (or info (org-babel-get-src-block-info)))
+         (lang (first info))
+         (body (second info))
+         (params (third info))
+         (session (cdr (assoc :session params))))
+    (unless (member lang org-babel-interpreters)
+      (error "Language is not in `org-babel-interpreters': %s" lang))
+    ;; if called with a prefix argument, then process header arguments
+    (pop-to-buffer (funcall (intern (concat "org-babel-load-session:" lang)) session body params))
+    (move-end-of-line 1)))
+
 (defun org-babel-pop-to-session (&optional arg info)
   "Pop to the session of the current source-code block.  If
 called with a prefix argument then evaluate the header arguments