emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* org-src--contents-for-write-back : preserve original major-mode, and avoid mixing tabs and spaces in org-mode buffers
@ 2017-04-03  4:15 Brent Goodrick
  2017-04-22  8:41 ` Nicolas Goaziou
  0 siblings, 1 reply; 7+ messages in thread
From: Brent Goodrick @ 2017-04-03  4:15 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 2972 bytes --]

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

[-- Attachment #2: Type: text/html, Size: 3574 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2017-04-29 16:41 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-04-03  4:15 org-src--contents-for-write-back : preserve original major-mode, and avoid mixing tabs and spaces in org-mode buffers Brent Goodrick
2017-04-22  8:41 ` Nicolas Goaziou
2017-04-23  3:21   ` Brent Goodrick
2017-04-27 23:04     ` Nicolas Goaziou
2017-04-28 15:29       ` Brent Goodrick
2017-04-29  9:59         ` Nicolas Goaziou
2017-04-29 16:41           ` Brent Goodrick

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).