0

Consider the following org-file.

#+NAME: example-src-block
#+BEGIN_SRC emacs-lisp
(defun my-message ()
  "Issue my message."
  (message "Within the emacs-lisp source block."))
#+END_SRC

How can the buffer local setting

(setq-local flycheck-disabled-checkers '(emacs-lisp-checkdoc))

be specified for the edit buffer of source block example-src-block.

Note: For opening the edit buffer place point on BEGIN_SRC and press C-c '.

The user J. Ricardo tried in a deleted question the following approach:

#+NAME: example-src-block
#+BEGIN_SRC emacs-lisp
(and (boundp 'flycheck-disabled-checkers)
     flycheck-disabled-checkers)
#+END_SRC

#+RESULTS: example-src-block
| emacs-lisp-checkdoc |

# Local Variables:
# eval: (setq-local org-babel-default-header-args:elisp '((:var . "flycheck-disabled-checkers='(emacs-lisp-checkdoc)")))
# End:

That does not set flycheck-disabled-checkers in source edit buffers for the source block. It only sets flycheck-disabled-checkers for the evaluation of the source block. as the printed result of the source block in the above example suggests.

  • @j-ricardo You deleted your question really fast. I think your question was really of general interest! You are absolutely right. An org buffer is like a directory for its source blocks. Why shouldn't we have "file variables" for the source blocks and "directory local" variables for the org buffer in the figurative sense? I am very sorry if I motivated you to delete your question with my comment. That was not my intention! Maybe your question only needed a (very) small bit of re-work. I tried to re-formulate it here and hope that I hit your actual problem with it. I added also my solution... – Tobias Jan 20 '18 at 9:35
1

Put (add-hook 'org-src-mode-hook 'hack-local-variables) into your init file. This enables the automatic evaluation of buffer local variables when you edit org source blocks with org-edit-src-code. You specify the buffer local variables for source blocks in the same manner as file local variables for files.

See the following example for a source block in an org file. The characters ^L actually stand for an inserted form-feed character. That character avoids that the buffer local variables for the source block are interpreted as file local variables for the org-file.

#+NAME: example-src-block
#+BEGIN_SRC emacs-lisp
(and (boundp 'flycheck-disabled-checkers)
     flycheck-disabled-checkers)
# Local Variables:
# flycheck-disabled-checkers: (emacs-lisp-checkdoc)
# End:
#+END_SRC

^L

The above solution still does not yet set buffer local variables for all elisp source blocks in an org file as J. Ricardo attempted to do.

For that purpose one can introduce a new header argument :edit-prep-form for emacs-lisp source blocks with help of the following function. That function can also be defined in your init file.

(defun org-babel-edit-prep:emacs-lisp (info)
  "Add parsing of :edit-prep-form for emacs-lisp source blocks."
  (let* ((header-argument-alist (nth 2 info))
     (edit-prep-form (assoc :edit-prep-form header-argument-alist)))
    (when edit-prep-form
      (eval (cdr edit-prep-form)))))

With that function installed a similar approach as that one proposed by J. Ricardo works. The only difference is that the header argument :var needs to be replaced by :edit-prep-form.

#+NAME: example-src-block
#+BEGIN_SRC emacs-lisp
  (and (boundp 'flycheck-disabled-checkers)
       flycheck-disabled-checkers)
#+END_SRC

#+RESULTS: example-src-block

# Local Variables:
# org-babel-default-header-args:emacs-lisp: ((:edit-prep-form . (setq-local flycheck-disabled-checkers '(emacs-lisp-checkdoc))))
# End:

The last approach works for source blocks with one certain language. The following elisp code adds a hook variable org-edit-src-code-hook. Hook functions added buffer-locally within the org buffer to that hook variable are run in all newly generated source edit buffers for source blocks of that org buffer.

(defvar org-edit-src-code-hook nil
  "Hook run after the setup of the edit buffer for an org source block.
The hook functions added buffer-locally within the org buffer
are run in the source edit buffers for the source blocks within the org file.")

(defun org-edit-src-code-hook-run (return-value)
  "Run `org-edit-src-code-hook' after `org-edit-src-code' with non-nil RETURN-VALUE."
  (when (and return-value
         (buffer-live-p (org-src--source-buffer)))
    (let ((hook-value (with-current-buffer (org-src--source-buffer)
            org-edit-src-code-hook)))
      (cl-loop for local-hook in hook-value
           if (eq local-hook t) do
           (cl-loop for global-hook in (default-value 'org-edit-src-code-hook) do
            (funcall global-hook))
           else do (funcall local-hook))))
  return-value)

(advice-add 'org-edit-src-code :filter-return #'org-edit-src-code-hook-run)

In the following org file we add buffer-locally an anonymous hook function to org-edit-src-code-hook that prints out the current buffer.

#+BEGIN_SRC emacs-lisp :results silent
(message "Running source block.")
#+END_SRC

# Local Variables:
# eval: (add-hook 'org-edit-src-code-hook (lambda () (message "Local src code hook for current buffer: %S" (current-buffer))) nil t)
# End:

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.