From mboxrd@z Thu Jan 1 00:00:00 1970 From: Brent Goodrick Subject: org-src--contents-for-write-back : preserve original major-mode, and avoid mixing tabs and spaces in org-mode buffers Date: Sun, 2 Apr 2017 21:15:46 -0700 Message-ID: Mime-Version: 1.0 Content-Type: multipart/alternative; boundary=001a113ec618e38214054c3b6966 Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:37032) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cutPD-0000wm-2C for emacs-orgmode@gnu.org; Mon, 03 Apr 2017 00:15:52 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cutPB-00050i-Ij for emacs-orgmode@gnu.org; Mon, 03 Apr 2017 00:15:51 -0400 Received: from mail-io0-x230.google.com ([2607:f8b0:4001:c06::230]:34484) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cutPB-0004z8-C0 for emacs-orgmode@gnu.org; Mon, 03 Apr 2017 00:15:49 -0400 Received: by mail-io0-x230.google.com with SMTP id b140so68113213iof.1 for ; Sun, 02 Apr 2017 21:15:47 -0700 (PDT) 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 --001a113ec618e38214054c3b6966 Content-Type: text/plain; charset=UTF-8 Hi, I found a bug in org-mode where emacs-lisp code that is in a already-indented source block in an org-mode buffer is improperly indented when editing it via C-c '. Take the following contrived example emacs-lisp source code: 1. Here is a list item with a emacs-lisp source block: #+BEGIN_SRC emacs-lisp :results value (let ((uuid "c2327c73-6da3-4421-8bda-194783a00e8f")) (progn (let ((xxx 'yyy)) (let ((xxx 'yyy)) (while t (message "infinite loop")))))) #+END_SRC After C-c ', indenting it, and C-c ' again, it renders as follows (tabs converted to spaces for this email, since I have `indent-tabs-mode' set to t in my emacs-lisp mode, which is the Emacs default): 1. Here is a list item with a emacs-lisp source block: #+BEGIN_SRC emacs-lisp :results value (let ((uuid "c2327c73-6da3-4421-8bda-194783a00e8f")) (progn (let ((xxx 'yyy)) (let ((xxx 'yyy)) (while t (message "infinite loop")))))) #+END_SRC Notice how the indentation looks bad due to the mixture of tabs and spaces. The bug is in the `org-src--contents-for-write-back' function. It uses a temp buffer. The temp buffer's major-mode is left to be the default, which is fundamental-mode, which knows nothing about how to indent lisp code properly. So in the fix below, I run the major-mode function from the original buffer. But even with that fix, the indentation must also use spaces in order to avoid mixing tabs and spaces in the resulting Org buffer. My fix, shown below, is to initialize the major-mode, and set `indent-tabs-mode' to nil, before it runs the `write-back' code. See "CHANGE" comments for the changes made: (defun org-src--contents-for-write-back () "Return buffer contents in a format appropriate for write back. Assume point is in the corresponding edit buffer." (let ((indentation (or org-src--block-indentation 0)) (preserve-indentation org-src--preserve-indentation) (contents (org-with-wide-buffer (buffer-string))) (write-back org-src--allow-write-back)) ;; CHANGE: Save off the original mode into orig-major-mode: (let ((orig-major-mode major-mode)) (with-temp-buffer (insert (org-no-properties contents)) ;; CHANGE: Switch to the original mode to obtain mode-specific indentation behavior: (funcall orig-major-mode) ;; CHANGE: Do not mix tabs and spaces during indentation: (setq indent-tabs-mode nil) (goto-char (point-min)) (when (functionp write-back) (funcall write-back)) (unless (or preserve-indentation (= indentation 0)) (let ((ind (make-string indentation ?\s))) (goto-char (point-min)) (while (not (eobp)) (when (looking-at-p "[ \t]*\\S-") (insert ind)) (forward-line)))) (buffer-string))))) If the above change seems correct, can someone apply it to org-mode? Thanks, Brent Goodrick --001a113ec618e38214054c3b6966 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Hi,

I found a bug in org-mode where emacs-lisp code= that is in a
already-indented source block in an org-mode buffer is imp= roperly
indented when editing it via C-c '. Take the following contr= ived
example emacs-lisp source code:

=C2=A01. Here is a list item= with a emacs-lisp source block:
=C2=A0=C2=A0=C2=A0 #+BEGIN_SRC emacs-li= sp :results value
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (let ((uuid "c2327= c73-6da3-4421-8bda-194783a00e8f"))
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0 (progn
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0 (let ((xxx 'yyy))
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0 (let ((xxx 'yyy))
=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (while t
=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0 (message "infinite loop"))))))
=C2=A0=C2=A0=C2=A0 #+END= _SRC

After C-c ', indenting it, and C-c ' again, it renders = as
follows (tabs converted to spaces for this email, since I have
`in= dent-tabs-mode' set to t in my emacs-lisp mode, which
is the Emacs d= efault):

=C2=A01. Here is a list item with a emacs-lisp source block= :
=C2=A0=C2=A0=C2=A0 #+BEGIN_SRC emacs-lisp :results value
=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0 (let ((uuid "c2327c73-6da3-4421-8bda-194783a00e8= f"))
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (progn
=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (let ((xxx 'yyy))
=C2= =A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 (let ((xxx 'yyy))
=C2=A0=C2=A0=C2= =A0 =C2=A0=C2=A0=C2=A0 =C2=A0 (while t
=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0= =C2=A0 =C2=A0=C2=A0=C2=A0 (message "infinite loop"))))))
=C2= =A0=C2=A0=C2=A0 #+END_SRC

Notice how the indentation looks bad due t= o the mixture of tabs
and spaces.

The bug is in the `org-src--con= tents-for-write-back' function. It
uses a temp buffer. The temp buff= er's major-mode is left to be
the default, which is fundamental-mode= , which knows nothing about
how to indent lisp code properly. So in the = fix below, I run the
major-mode function from the original buffer. But e= ven with that
fix, the indentation must also use spaces in order to avoi= d
mixing tabs and spaces in the resulting Org buffer.=C2=A0 My fix,
s= hown below, is to initialize the major-mode, and set
`indent-tabs-mode&#= 39; to nil, before it runs the `write-back'
code.

See "C= HANGE" comments for the changes made:

(defun org-src--contents-= for-write-back ()
=C2=A0 "Return buffer contents in a format approp= riate for write back.
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 Assume = point is in the corresponding edit buffer."
=C2=A0 (let ((indentati= on (or org-src--block-indentation 0))
=C2=A0=C2=A0=C2=A0 (preserve-inden= tation org-src--preserve-indentation)
=C2=A0=C2=A0=C2=A0 (contents (org-= with-wide-buffer (buffer-string)))
=C2=A0=C2=A0=C2=A0 (write-back org-sr= c--allow-write-back))
=C2=A0=C2=A0=C2=A0 ;; CHANGE: Save off the origina= l mode into orig-major-mode:
=C2=A0=C2=A0=C2=A0 (let ((orig-major-mode m= ajor-mode))
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (with-temp-buffer
=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (insert (org-no-properties contents))<= br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ;; CHANGE: Switch to the orig= inal mode to obtain mode-specific indentation behavior:
=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0 (funcall orig-major-mode)
=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0 ;; CHANGE: Do not mix tabs and spaces during inden= tation:
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (setq indent-tabs-mod= e nil)
=C2=A0=C2=A0=C2=A0 (goto-char (point-min))
=C2=A0=C2=A0=C2=A0 = (when (functionp write-back) (funcall write-back))
=C2=A0=C2=A0=C2=A0 (u= nless (or preserve-indentation (=3D indentation 0))
=C2=A0=C2=A0=C2=A0 = =C2=A0 (let ((ind (make-string indentation ?\s)))
=C2=A0=C2=A0=C2=A0 =C2= =A0=C2=A0=C2=A0 (goto-char (point-min))
=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0= =C2=A0 (while (not (eobp))
=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0 (when (looking-at-p "[ \t]*\\S-") (insert ind))
=C2=A0= =C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (forward-line))))
=C2=A0=C2= =A0=C2=A0 (buffer-string)))))

If the above change seems correct, can= someone apply it to org-mode?

Thanks,
Brent Goodrick

--001a113ec618e38214054c3b6966--