emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* src block indentation causing problems due to tab/space mixing - proposed fix
@ 2018-07-27 13:22 Derek Feichtinger
  2018-08-19  8:40 ` Nicolas Goaziou
  0 siblings, 1 reply; 2+ messages in thread
From: Derek Feichtinger @ 2018-07-27 13:22 UTC (permalink / raw)
  To: emacs-orgmode

Dear Org developers

I think that the current way of indenting source blocks
(with org-src-preserve-indentation is set to nil) is not ideal in regard
to languages that dislike mixing of tabs and spaces.

I'm currently switching to python3 and this forbids the mixing.
Setting org-src-preserve-indentation to t would basically create
problems with many of my older files, and I also dislike the reduced
readability resulting from the code block contents being aligned at
column 0.

I just checked out the org mode master f79545f and made sure
that the relevant lines match my current active version of
org-plus-contrib-20180709.

Looking at the code:

org-edit-src-exit calls org-src--contents-for-write-back for writing the
contents of the source buffer. Inside of that function, all leading whitespace
of each line is replaced using indent-to. This leads to mixing tabs and
spaces and causes problems with python3.

in org-src--contents-for-write-back:

     #+BEGIN_SRC elisp
       (when (> indentation-offset 0)
	 (while (not (eobp))
	   (skip-chars-forward " \t")
	   (unless (eolp)		;ignore blank lines
	     (let ((i (current-column)))
	       (delete-region (line-beginning-position) (point))
	       (indent-to (+ i indentation-offset))))
	   (forward-line)))
     #+END_SRC

I tested this simple alternative which just uses indent-to to fill up
the columns needed for the source blocks basic indentation offset, but
which does not affect any leading whitespace from the code's buffer.

     #+BEGIN_SRC elisp
      (when (> indentation-offset 0)
	(while (not (eobp))
	  (unless (eolp)
	    (indent-to indentation-offset))
	  (forward-line)))
     #+END_SRC

This still allows org mode to use tabs for the block's basic indentation
offset, so that the block is nicely aligned with the rest of the org
buffer, but it prevents modification of the whitespace which is relevant
to the block itself.

I just tested it also with some tangled files, and it seems to work
nicely for my test cases. Still, I may have overlooked something, and
there is a deeper reason for the original implementation.

Best regards,
Derek

--
Paul Scherrer Institut
Dr. Derek Feichtinger                   Phone:   +41 56 310 47 33
Section Head Science-IT                 Email: derek.feichtinger@psi.ch
Building/Room No. WHGA/U126
CH-5232 Villigen PSI

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

end of thread, other threads:[~2018-08-19  8:40 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-07-27 13:22 src block indentation causing problems due to tab/space mixing - proposed fix Derek Feichtinger
2018-08-19  8:40 ` Nicolas Goaziou

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).