From mboxrd@z Thu Jan 1 00:00:00 1970 From: =?utf-8?Q?G=C3=B6ktu=C4=9F_Kayaalp?= Subject: Re: Inheriting some local variables from source code block editing buffers Date: Mon, 14 May 2018 08:44:15 +0300 Message-ID: References: Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:53652) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fI6lT-0006l8-DI for emacs-orgmode@gnu.org; Mon, 14 May 2018 02:15:24 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fI6HV-00029R-KR for emacs-orgmode@gnu.org; Mon, 14 May 2018 01:44:25 -0400 Received: from relay3-d.mail.gandi.net ([217.70.183.195]:44367) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fI6HV-00024D-Bl for emacs-orgmode@gnu.org; Mon, 14 May 2018 01:44:21 -0400 Received: from alpha.alpha (unknown [176.218.82.110]) (Authenticated sender: self@gkayaalp.com) by relay3-d.mail.gandi.net (Postfix) with ESMTPSA id 101DE60013 for ; Mon, 14 May 2018 07:44:16 +0200 (CEST) In-Reply-To: (=?utf-8?B?IkfDtmt0dcSf?= Kayaalp"'s message of "Sun, 29 Apr 2018 20:19:03 +0300") List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: emacs-orgmode@gnu.org --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Hello, Sorry for the silence, I've finally got around to implementing this, and implemented it as an advice, which supports both an =E2=80=98:edit-bindings= =E2=80=99 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 =E2=80=98org-src--edit-element=E2=80=99 function. One =E2=80=98gotcha=E2=80=99 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 dat= um)))) (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 v= ar))) ;; 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)))) --=20 =C4=B0. G=C3=B6ktu=C4=9F Kayaalp 024C 30DD 597D 142B 49AC 40EB 465C D949 B101 2427 --=-=-= Content-Type: text/x-org; charset=utf-8 Content-Disposition: attachment; filename=edit-bindings.org Content-Transfer-Encoding: quoted-printable Content-Description: Testing instructions. # $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=C3=B6ktu=C4=9F 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 =3DC-c ' C-M-x=3D, and evaluating variables via =3DM-:=3D 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: --=-=-=--