From mboxrd@z Thu Jan 1 00:00:00 1970 From: stardiviner Subject: Re: [PATCH] ob-clojure-literate.el add some new functions Date: Mon, 26 Mar 2018 09:59:10 +0800 Message-ID: <7ab04864-666d-b84b-b9ea-6a26868f5cdd@gmail.com> References: <4d7d38a2-4a7b-c22a-3d5c-fc05a99aabb8@gmail.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------E64368852B0A63D449B77E1F" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:59444) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1f0HPs-0005MQ-GQ for emacs-orgmode@gnu.org; Sun, 25 Mar 2018 21:59:22 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1f0HPn-0003hF-Nc for emacs-orgmode@gnu.org; Sun, 25 Mar 2018 21:59:20 -0400 Received: from mail-pg0-x22f.google.com ([2607:f8b0:400e:c05::22f]:37313) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1f0HPn-0003gw-96 for emacs-orgmode@gnu.org; Sun, 25 Mar 2018 21:59:15 -0400 Received: by mail-pg0-x22f.google.com with SMTP id n11so6714526pgp.4 for ; Sun, 25 Mar 2018 18:59:15 -0700 (PDT) Received: from [0.0.0.0] ([119.28.89.243]) by smtp.gmail.com with ESMTPSA id f64sm10084962pfa.154.2018.03.25.18.59.12 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 25 Mar 2018 18:59:13 -0700 (PDT) In-Reply-To: <4d7d38a2-4a7b-c22a-3d5c-fc05a99aabb8@gmail.com> Content-Language: en-US List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: org-mode This is a multi-part message in MIME format. --------------E64368852B0A63D449B77E1F Content-Type: multipart/alternative; boundary="------------3912F4DFE4087D6E147914D1" --------------3912F4DFE4087D6E147914D1 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit - support :ns header (as discussed at here https://github.com/clojure-emacs/clojure-mode/pull/465#discussion_r158009538) - supoort inject CIDER outside of project - support :varinitialization when :session initialization prepare. On 03/22/2018 01:49 AM, stardiviner wrote: > > Two patches > > One is to support ob-clojure.el future coming header argument :ns. > > The second is to support jack-in CIDER outside of Clojure project by > default. Not no need to ob-clojure project to be created anymore. > --------------3912F4DFE4087D6E147914D1 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 7bit

- support :ns header (as discussed at here https://github.com/clojure-emacs/clojure-mode/pull/465#discussion_r158009538)

- supoort inject CIDER outside of project

- support :var initialization when :session initialization prepare.


On 03/22/2018 01:49 AM, stardiviner wrote:

Two patches

One is to support ob-clojure.el future coming header argument :ns.

The second is to support jack-in CIDER outside of Clojure project by default. Not no need to ob-clojure project to be created anymore.


--------------3912F4DFE4087D6E147914D1-- --------------E64368852B0A63D449B77E1F Content-Type: text/x-patch; name="0001-ob-clojure-literate.el-CIDER-jack-in-outside-of-proj.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename*0="0001-ob-clojure-literate.el-CIDER-jack-in-outside-of-proj.pa"; filename*1="tch" >From bc575705704ebff7f7630cd35ff936844c89f66a Mon Sep 17 00:00:00 2001 From: stardiviner Date: Thu, 22 Mar 2018 01:21:22 +0800 Subject: [PATCH 1/3] * ob-clojure-literate.el: CIDER jack-in outside of project by default. --- contrib/lisp/ob-clojure-literate.el | 50 ++++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/contrib/lisp/ob-clojure-literate.el b/contrib/lisp/ob-clojure-literate.el index a7ab1894d..8d419b0b4 100644 --- a/contrib/lisp/ob-clojure-literate.el +++ b/contrib/lisp/ob-clojure-literate.el @@ -35,8 +35,10 @@ Don't auto jack in by default for not rude." :type 'boolean :group 'ob-clojure-literate) -(defcustom ob-clojure-literate-project-location (concat user-emacs-directory "Org-mode/") - "The location for `ob-clojure-literate' scaffold project." +(defcustom ob-clojure-literate-project-location nil + "The location for `ob-clojure-literate' scaffold project. +If it is nil, then `cider-jack-in' will jack-in outside of Clojure project. +If it is a directory, `ob-clojure-literate' will try to create Clojure project automatically." :type 'string :group 'ob-clojure-literate) @@ -45,7 +47,7 @@ Don't auto jack in by default for not rude." (defvar ob-clojure-literate-session-ns nil) (defvar ob-clojure-literate-cider-connections nil) -(defcustom ob-clojure-literate-default-session "*cider-repl ob-clojure*" +(defcustom ob-clojure-literate-default-session "*cider-repl localhost*" "The default session name for `ob-clojure-literate'." :type 'string :group 'ob-clojure-literate) @@ -104,23 +106,31 @@ Don't auto jack in by default for not rude." (defun ob-clojure-literate-auto-jackin () "Auto setup ob-clojure-literate scaffold and jack-in Clojure project." (interactive) - (unless (file-directory-p (expand-file-name ob-clojure-literate-project-location)) - (make-directory ob-clojure-literate-project-location t) - (let ((default-directory ob-clojure-literate-project-location)) - (shell-command "lein new ob-clojure"))) - (unless (or - (and (cider-connected-p) - (if (not (null ob-clojure-literate-session)) - (seq-contains cider-connections (get-buffer ob-clojure-literate-session)))) - cider-connections - (not (null ob-clojure-literate-session))) - ;; return back to original file. - (if (not (and (= (length (ob-clojure-literate-get-session-list)) 1) - (-contains-p (ob-clojure-literate-get-session-list) ob-clojure-literate-default-session))) - (save-window-excursion - (find-file (expand-file-name (concat ob-clojure-literate-project-location "ob-clojure/src/ob_clojure/core.clj"))) - (with-current-buffer "core.clj" - (cider-jack-in)))))) + (cond + ;; jack-in outside of Clojure project. + ((null ob-clojure-literate-project-location) + (if (member (get-buffer "*cider-repl localhost*") cider-connections) + (message "CIDER default session already launched.") + (cider-jack-in nil))) + ((not (null ob-clojure-literate-project-location)) + (unless (file-directory-p (expand-file-name ob-clojure-literate-project-location)) + (make-directory ob-clojure-literate-project-location t) + (let ((default-directory ob-clojure-literate-project-location)) + (shell-command "lein new ob-clojure"))) + (unless (or + (and (cider-connected-p) + (if (not (null ob-clojure-literate-session)) + (seq-contains cider-connections (get-buffer ob-clojure-literate-session)))) + cider-connections + (not (null ob-clojure-literate-session))) + ;; return back to original file. + (if (not (and (= (length (ob-clojure-literate-get-session-list)) 1) + (-contains-p (ob-clojure-literate-get-session-list) ob-clojure-literate-default-session))) + (save-window-excursion + (find-file (expand-file-name (concat ob-clojure-literate-project-location "ob-clojure/src/ob_clojure/core.clj"))) + (with-current-buffer "core.clj" + (cider-jack-in)))))) + )) (defun ob-clojure-literate-set-local-cider-connections (toggle?) "Set buffer local `cider-connections' for `ob-clojure-literate-mode' `TOGGLE?'." -- 2.16.2 --------------E64368852B0A63D449B77E1F Content-Type: text/x-patch; name="0002-ob-clojure-literate.el-support-use-ns-header-argumen.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename*0="0002-ob-clojure-literate.el-support-use-ns-header-argumen.pa"; filename*1="tch" >From 25e75ded1466b413a0d098041ad40f52e1707432 Mon Sep 17 00:00:00 2001 From: stardiviner Date: Thu, 22 Mar 2018 01:43:18 +0800 Subject: [PATCH 2/3] * ob-clojure-literate.el: support use :ns header argument. - rename ob-clojure-literate-cider-do-not-find-ns to ob-clojure-literate-set-ns --- contrib/lisp/ob-clojure-literate.el | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/contrib/lisp/ob-clojure-literate.el b/contrib/lisp/ob-clojure-literate.el index 8d419b0b4..71ea909ba 100644 --- a/contrib/lisp/ob-clojure-literate.el +++ b/contrib/lisp/ob-clojure-literate.el @@ -151,14 +151,15 @@ If it is a directory, `ob-clojure-literate' will try to create Clojure project a ;; (setq-local cider-connections '()) )) -(defun ob-clojure-literate-cider-do-not-find-ns (body params) +(defun ob-clojure-literate-set-ns (body params) "Fix the issue that `cider-current-ns' try to invoke `clojure-find-ns' to extract ns from buffer." ;; TODO: Is it possible to find ns in `body'? (when (ob-clojure-literate-any-connection-p) (setq ob-clojure-literate-original-ns (cider-current-ns)) (with-current-buffer ob-clojure-literate-session (setq ob-clojure-literate-session-ns cider-buffer-ns)) - (setq-local cider-buffer-ns ob-clojure-literate-session-ns)) + (setq-local cider-buffer-ns (or (cdr (assq :ns params)) + ob-clojure-literate-session-ns))) (message (format "ob-clojure-literate: current CIDER ns is [%s]." cider-buffer-ns))) (defun ob-clojure-literate-set-local-session (toggle?) @@ -302,7 +303,7 @@ reset `RESULT' to `nil'." (equal major-mode 'org-mode)) ; `ob-clojure-literate-mode' only works in `org-mode'. (ob-clojure-literate-set-local-cider-connections ob-clojure-literate-mode) (ob-clojure-literate-set-local-session ob-clojure-literate-mode) - (advice-add 'org-babel-execute:clojure :before #'ob-clojure-literate-cider-do-not-find-ns) + (advice-add 'org-babel-execute:clojure :before #'ob-clojure-literate-set-ns) (advice-add 'org-babel-expand-body:clojure :filter-args #'ob-clojure-literate-inject-code) (advice-add 'org-babel-execute:clojure :filter-return #'ob-clojure-literate-support-graphics-result) (message "ob-clojure-literate minor mode enabled."))) @@ -310,7 +311,7 @@ reset `RESULT' to `nil'." ;;;###autoload (defun ob-clojure-literate-disable () "Disable Org-mode buffer locally for `ob-clojure-literate'." - (advice-remove 'org-babel-execute:clojure #'ob-clojure-literate-cider-do-not-find-ns) + (advice-remove 'org-babel-execute:clojure #'ob-clojure-literate-set-ns) (advice-remove 'org-babel-expand-body:clojure #'ob-clojure-literate-inject-code) (advice-remove 'org-babel-execute:clojure #'ob-clojure-literate-support-graphics-result) (setq-local cider-buffer-ns ob-clojure-literate-original-ns) -- 2.16.2 --------------E64368852B0A63D449B77E1F Content-Type: text/x-patch; name="0003-ob-clojure-literate.el-support-vars-initialization-w.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename*0="0003-ob-clojure-literate.el-support-vars-initialization-w.pa"; filename*1="tch" >From fc8a9082712704f70eabe60c6c612600f16e58d1 Mon Sep 17 00:00:00 2001 From: stardiviner Date: Mon, 26 Mar 2018 09:47:54 +0800 Subject: [PATCH 3/3] * ob-clojure-literate.el: support vars initialization when prepare session. - org-babel-clojure-var-to-clojure --- contrib/lisp/ob-clojure-literate.el | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/contrib/lisp/ob-clojure-literate.el b/contrib/lisp/ob-clojure-literate.el index 71ea909ba..2d1d30db8 100644 --- a/contrib/lisp/ob-clojure-literate.el +++ b/contrib/lisp/ob-clojure-literate.el @@ -221,7 +221,14 @@ If it is a directory, `ob-clojure-literate' will try to create Clojure project a (defun org-babel-clojure-var-to-clojure (var) "Convert src block's `VAR' to Clojure variable." - ;; TODO: reference `org-babel-python-var-to-python' + (if (listp var) + (replace-regexp-in-string "(" "'(" var) + (cond + ((stringp var) + ;; wrap org-babel passed in header argument value with quote in Clojure. + (format "\"%s\"" var)) + (t + (format "%s" var)))) ) (defun org-babel-variable-assignments:clojure (params) @@ -230,8 +237,7 @@ If it is a directory, `ob-clojure-literate' will try to create Clojure project a (lambda (pair) (format "(def %s %s)" (car pair) - ;; (org-babel-clojure-var-to-clojure (cdr pair)) - (cdr pair))) + (org-babel-clojure-var-to-clojure (cdr pair)))) (org-babel--get-vars params))) ;;; Support header arguments :results graphics :file "image.png" by inject Clojure code. -- 2.16.2 --------------E64368852B0A63D449B77E1F--