emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Code, Sessions, and org-edit-special
@ 2014-11-21 23:38 Ken Mankoff
  2014-11-23  0:17 ` Ken Mankoff
  0 siblings, 1 reply; 3+ messages in thread
From: Ken Mankoff @ 2014-11-21 23:38 UTC (permalink / raw)
  To: emacs-orgmode

Hi List,

I'm trying to improve my code workflow. I use python and sessions. I'd
like to be able to run code in the session even if I'm editing/viewing
the code via `org-edit-special`. Furthermore, when I edit code outside
of Org, I'd like to have sessions, that is, optionally one Python
session per source file. This allows me to edit different code for
different projects in different folders on virtual desktops, etc.

I've solved the non-Org part of this with the following one re-defined
and one new function:

#+BEGIN_SRC emacs-lisp
  (defun elpy-shell-get-or-create-process ()
    "Get or create an inferior Python process for current buffer and return it."
    (let* ((bufname (format "*%s*" (kdm/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 kdm/python-shell-get-process-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

Now when I C-c C-c in a buffer without an associated Python session, I'm
prompted for a name (defaulting to the buffer-name). I provide a session
name, and then all future C-c C-c's for that buffer are executed there.

This is good for Org. If I name a session "foo", I can edit code in
org-edit-special, assign Python session "foo", and execute code in a
session either from the org buffer or from the temp .py file. In theory.

But since I've call kdm/pythons-shell-get-process-name, if I try to C-c
C-c in a #+BEGIN_SRC section, it no longer works. I'm prompted for a
session name (I should not be). Whatever I enter is ignored, and the
code is executed either in a temporary "*Python*" if no :session, or in
the :session name as it should be.

How can I find out inside my `kdm/python-shell-get-process-name` if it
was called from Org, in which case I would implement the original
version.

Thanks,

  -k.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Code, Sessions, and org-edit-special
  2014-11-21 23:38 Code, Sessions, and org-edit-special Ken Mankoff
@ 2014-11-23  0:17 ` Ken Mankoff
  2014-11-23 19:50   ` Ken Mankoff
  0 siblings, 1 reply; 3+ messages in thread
From: Ken Mankoff @ 2014-11-23  0:17 UTC (permalink / raw)
  To: emacs-orgmode

Hi List,

A simpler phrasing of my previous question.

How do I find out if an Org function, `org-ctrl-c-ctrl-c`, was called
somewhere up the stack. I'm trying to modify a function
"python-shell-get-process-name", but I don't want it to be modified when
somewhere, possibly far upstream, Org is happening.

Perhaps this is the wrong approach? Rather than examining the stack,
should I just be checking if Org is the active mode? But I think when a
code segment is executed, a temp file is created and Org isn't the
active mode?

Thanks,

  -k.


* On 2014-11-21 at 18:38, Ken Mankoff wrote:
> Hi List,
>
> I'm trying to improve my code workflow. I use python and sessions. I'd
> like to be able to run code in the session even if I'm editing/viewing
> the code via `org-edit-special`. Furthermore, when I edit code outside
> of Org, I'd like to have sessions, that is, optionally one Python
> session per source file. This allows me to edit different code for
> different projects in different folders on virtual desktops, etc.
>
> I've solved the non-Org part of this with the following one re-defined
> and one new function:
>
> #+BEGIN_SRC emacs-lisp
>   (defun elpy-shell-get-or-create-process ()
>     "Get or create an inferior Python process for current buffer and return it."
>     (let* ((bufname (format "*%s*" (kdm/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 kdm/python-shell-get-process-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
>
> Now when I C-c C-c in a buffer without an associated Python session, I'm
> prompted for a name (defaulting to the buffer-name). I provide a session
> name, and then all future C-c C-c's for that buffer are executed there.
>
> This is good for Org. If I name a session "foo", I can edit code in
> org-edit-special, assign Python session "foo", and execute code in a
> session either from the org buffer or from the temp .py file. In theory.
>
> But since I've call kdm/pythons-shell-get-process-name, if I try to C-c
> C-c in a #+BEGIN_SRC section, it no longer works. I'm prompted for a
> session name (I should not be). Whatever I enter is ignored, and the
> code is executed either in a temporary "*Python*" if no :session, or in
> the :session name as it should be.
>
> How can I find out inside my `kdm/python-shell-get-process-name` if it
> was called from Org, in which case I would implement the original
> version.
>
> Thanks,
>
>   -k.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Code, Sessions, and org-edit-special
  2014-11-23  0:17 ` Ken Mankoff
@ 2014-11-23 19:50   ` Ken Mankoff
  0 siblings, 0 replies; 3+ messages in thread
From: Ken Mankoff @ 2014-11-23 19:50 UTC (permalink / raw)
  To: emacs-orgmode


* On 2014-11-22 at 19:17, Ken Mankoff wrote:
> Hi List,
>
> A simpler phrasing of my previous question.
>
> How do I find out if an Org function, `org-ctrl-c-ctrl-c`, was called
> somewhere up the stack. I'm trying to modify a function
> "python-shell-get-process-name", but I don't want it to be modified
> when somewhere, possibly far upstream, Org is happening.


 (equal this-command 'org-ctrl-c-ctrl-c)

Tells me if a function is in a call stack initiated by C-c C-c from Org.

I now have Org behaving as it always has with un-named sessions or named
sessions. Importantly, something Org was lacking, I can edit a file via
C-' (org-edit-special), and when I execute the code from there, which is
not part of Org, I can send it to the named session, rather than to the
standard executable as before.

If anyone wants this feature for Python let me know and I can share the
code.

  -k.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2014-11-23 19:51 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-11-21 23:38 Code, Sessions, and org-edit-special Ken Mankoff
2014-11-23  0:17 ` Ken Mankoff
2014-11-23 19:50   ` Ken Mankoff

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).