2

I need to configure the behavior of elpy when editing a python src block in org-src-mode. Usually such configuration code should be added to org-src-mode-hook. In my code I need to check some header args and act appropriately: I need to make sure that the code is python (not, say ipython) and whether the :session parameter is set.

The only way I found to do this is to access a "private" and undocumented variable org-src--babel-info. Is this the suggested way? Should this be documented in org-mode?

In particular my default elpy iterpreter is jupyter but I need to set it to python when editing src code blocks and I need elpy to use the running session when pressing C-c C-c instead of opening a new one.

  • Thanks for asking such an interesting question. Have you looked at org-element-map and org-element-property functions to extract the header info? I suspect these functions would work but I’m still trying understand how to pass the header data back to hook. – Melioratus Nov 30 '19 at 15:47
1

The file ob-core.el has a section Public API that contains the macro org-src-do-at-code-block. You can use that macro to analyze the source code block associated with the source edit buffer.

The following example shows how it can be done. It returns the header arguments as an association list with the header argument names as keys and the values as strings.

(defun org+-edit-src-header-args ()
  "Get header args of the source block currently edited."
  (let* ((el (org-src-do-at-code-block (org-element-context)))
     (parameters-string (org-element-property :parameters el))
     (parameters-list (org-babel-parse-header-arguments parameters-string)))
    parameters-list))
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.