From mboxrd@z Thu Jan 1 00:00:00 1970 From: Andreas Leha Subject: Re: named python session Date: Thu, 13 Aug 2015 14:12:32 +0100 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:50152) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZPsJR-0003B9-UA for emacs-orgmode@gnu.org; Thu, 13 Aug 2015 09:12:55 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZPsJM-0000bm-QV for emacs-orgmode@gnu.org; Thu, 13 Aug 2015 09:12:53 -0400 Received: from plane.gmane.org ([80.91.229.3]:34263) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZPsJM-0000bT-Bf for emacs-orgmode@gnu.org; Thu, 13 Aug 2015 09:12:48 -0400 Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1ZPsJ9-0007Et-EU for emacs-orgmode@gnu.org; Thu, 13 Aug 2015 15:12:35 +0200 Received: from 193.63.222.18 ([193.63.222.18]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 13 Aug 2015 15:12:35 +0200 Received: from andreas.leha by 193.63.222.18 with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 13 Aug 2015 15:12:35 +0200 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-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: emacs-orgmode@gnu.org Hi Ken, Ken Mankoff writes: > Hi Andreas, > > On 2015-08-12 at 08:06, Andreas Leha wrote: >> How do I associate that with the python process in *mypy*? >> I am asked to start python when I run python-shell-send-region. > > > I have set up my system so that Org asks for a buffer name every time =org-edit-special= is called. This may be helpful to you. > > -k. > > > *** Custom Python Session Names > > https://github.com/jorgenschaefer/elpy/issues/383 > > I want each python session to optionally have a unique name, so that I > can run multiple sessions in multiple windows/buffers/directories and > not have them interact/interfere. > > Here I've copied =elpy-shell-get-or-create-process= from =elpy.el= and > modified it. I also have to modify =python-shell-get-process-name=. > This all is fairly easy, except when executing from Org it gets more > complicated, hence the special case if a function called from > =org-ctrl-c-ctrl-c=. > > #+BEGIN_SRC emacs-lisp :results none > (defun elpy-shell-get-or-create-process () > "Get or create an inferior Python process for current buffer and return it. > Customized by KDM to make dedicated sessions" > (let* ((bufname (format "*%s*" (python-shell-get-process-name t))) > (proc (get-buffer-process bufname))) > (if proc > proc > (run-python (python-shell-parse-command) t nil) ;; DEDICATED! > (get-buffer-process bufname)))) > > (defun python-shell-get-process-name (dedicated) > (if (equal this-command 'org-ctrl-c-ctrl-c) > (kdm/orig-py-sh-get-proc-name dedicated) > (kdm/my-py-sh-get-proc-name dedicated))) > > (defun kdm/orig-py-sh-get-proc-name (dedicated) > "Calculate the appropriate process name for inferior Python process. > If DEDICATED is t returns a string with the form > `python-shell-buffer-name'[variable `buffer-name']." > (let ((process-name > (if (and dedicated > (buffer-name)) > (format "%s[%s]" python-shell-buffer-name (buffer-name)) > (format "%s" python-shell-buffer-name)))) > process-name)) > > (defun kdm/my-py-sh-get-proc-name (dedicated) > (if (boundp 'py-buf-proc-name) > (format "%s" py-buf-proc-name) > (setq-local py-buf-proc-name > (format "%s" > (completing-read "Python session name: " > nil nil nil (buffer-name) nil (buffer-name))) > ))) > > #+END_SRC Thanks a lot for setting me on the right track! I am quite surprised (coming from ess as my interface to R) that multiple named sessions aren't supported out of the box for python. I use your code now, which in my use-case works great. Only slight variation: I set the default python process-name to the session name using these two functions: #+BEGIN_SRC emacs-lisp :results none (defun al-org-babel-get-session (&optional arg info) "Return the :session of the current source-code block. Will return - nil if :session is given but not named - \"none\" if no :session argument is specified - the session name otherwise" (interactive) (if (org-src-edit-buffer-p) (let ((beg org-src--beg-marker)) (with-current-buffer (org-src--source-buffer) (goto-char beg) (al-org-babel-get-session))) (let* ((info (or info (org-babel-get-src-block-info))) (params (nth 2 info)) (session (cdr (assoc :session params)))) session))) (defun kdm/my-py-sh-get-proc-name (dedicated) (let* ((sessionname (al-org-babel-get-session)) (defaultname (or sessionname (if (equalp sessionname "none") (buffer-name) sessionname)))) (if (boundp 'py-buf-proc-name) (format "%s" py-buf-proc-name) (setq-local py-buf-proc-name (format "%s" (completing-read "Python session name: " nil nil nil defaultname nil defaultname)))))) #+END_SRC Thanks again, Andreas