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

# $Id: edit-bindings.org,v 1.2 2018/05/14 05:23:48 g Exp $
#+title: :edit-bindings and #+ATTR_EDIT test file
#+author: Göktuğ Kayaalp

We use the variables ~lexical-binding~, ~truncate-lines~ and
~word-wrap~ for this test/demo.  We assume the first is ~nil~, the
second is set from a file local variable, and for the third we have no
assumptions.

#+BEGIN_SRC elisp :results verbatim
(list lexical-binding truncate-lines word-wrap)
#+END_SRC

#+RESULTS:
: (nil 62 t)

Now we set up the default ~:edit-bindings~ property.

#+PROPERTY: header-args :edit-bindings '((lexical-binding t))

>From now on, we'll do our observations evaluating the forms in source
code blocks via =C-c ' C-M-x=, and evaluating variables via =M-:= in
the edit special buffer.

In edit-special buffer, when editing, we observe that
~lexical-binding~ is set to ~t~:

#+BEGIN_SRC elisp
(eq lexical-binding t)
#+END_SRC

But ~truncate-lines~ is not set:

#+BEGIN_SRC elisp
(eq truncate-lines 62)
#+END_SRC

Copy ~truncate-lines~ over from this file's buffer local variables:

#+BEGIN_SRC elisp :edit-bindings '(truncate-lines)
(list lexical-binding truncate-lines)
#+END_SRC

We observe that now the buffer-wide value is shadoved, and while
truncate lines is passed through, ~lexical-binding~ is not, it's nil.

So we pass both through:

#+BEGIN_SRC elisp :edit-bindings '((lexical-binding t) truncate-lines)
(list (eq truncate-lines 62)
      (eq lexical-binding t))
#+END_SRC

Export blocks can not use header arguments.  Thus we use an element
property called ~ATTR_EDIT~:

#+ATTR_EDIT: ((fill-column 30) truncate-lines)
#+BEGIN_EXPORT latex
Nullam eu ante vel est
convallis dignissim.  Fusce
suscipit, wisi nec facilisis
facilisis, est dui fermentum
leo, quis tempor ligula erat
quis odio.  Nunc porta
vulputate tellus.  Nunc rutrum
turpis sed pede.  Sed
bibendum.
#+END_EXPORT

When we observe the value of ~truncate-lines~, we see that it is 62,
and when we use ~fill-paragraph~, that it wraps according to the new
value of ~fill-column~.

We get an error if we supply a bad varlist, but editing continues
anyways:

#+BEGIN_SRC python :edit-bindings '(("monty" "python"))
print(None)
#+END_SRC

#+ATTR_EDIT: Tea?
#+BEGIN_EXPORT latex
\textit{nope}
#+END_EXPORT

# Local Variables:
# truncate-lines: 62
# End:

Reply via email to