emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [O] Inheriting some local variables from source code block editing b


From: Göktuğ Kayaalp
Subject: Re: [O] Inheriting some local variables from source code block editing buffers
Date: Mon, 14 May 2018 08:44:15 +0300
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux)

Hello,

Sorry for the silence, I've finally got around to implementing this, and
implemented it as an advice, which supports both an ‘:edit-bindings’
Babel header argument for source code blocks, and an #+ATTR_EDIT:
element property for export blocks, etc.  Find the code below, and
attached an Org mode file to help with testing.

This advice can easily be made into a patch to the
‘org-src--edit-element’ function.

One ‘gotcha’ is that :edit-bindings requires a quoted list whereas the
explicit quote is not necessary with ATTR_EDIT:

#+BEGIN_SRC elisp :edit-bindings '((lexical-binding t))
#+ATTR_EDIT: ((lexical-binding t))

Another problem is that I was not able to define a new element property
named EDIT_BINDINGS, and had to take the shortcut with naming it as an
ATTR_* variable.  Preferably, it'd be EDIT_BINDINGS instead:

#+BEGIN_SRC elisp :edit-bindings '((lexical-binding t))
#+EDIT_BINDINGS: ((lexical-binding t))

But personally I don't think it's that big of a problem.


The advice:

(define-advice org-src--edit-element
    (:around
     (fn datum &rest args)
     attr-edit&edit-bindings)
  "Apply edit-special bindings."
  (let ((attr-edit (org-element-property :attr_edit datum))
        (edit-bindings
         (assoc :edit-bindings (caddr (org-babel-get-src-block-info nil 
datum))))
        (source-buffer (current-buffer))
        (sub (lambda (varlist source-buffer)
               (let (var val)
                 (dolist (form varlist)
                   ;; If it's a symbol, inherit from the Org mode buffer.
                   (if (symbolp form)
                       (setq var form
                             val (with-current-buffer source-buffer (eval var)))
                     ;; Else, apply the specified value.
                     (setq var (car form) val (cadr form)))
                   (unless (symbolp var)
                     ;; XXX: maybe tell where it is?
                     (user-error "Bad varlist at ATTR_EDIT"))
                   (set (make-local-variable var) val))))))

    ;; Apply function
    (apply fn datum args)

    ;; Apply edit attributes (ATTR_EDIT).
    (dolist (attr attr-edit)
      (let ((varlist (car (read-from-string attr))))
        (unless (listp varlist)
          (user-error "Value of ATTR_EDIT must be a varlist."))
        (funcall sub varlist source-buffer)))
    ;; Apply edit bindings (:edit-bindings header argument).
    ;; These override attr-edit values.
    (when edit-bindings
      (funcall sub (cdr edit-bindings) source-buffer))))

-- 
İ. Göktuğ Kayaalp       <https://www.gkayaalp.com/>
                         024C 30DD 597D 142B 49AC
                         40EB 465C D949 B101 2427

Attachment: edit-bindings.org
Description: Testing instructions.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]