From mboxrd@z Thu Jan 1 00:00:00 1970 From: Dan Davison Subject: Re: Exporting blocks without removing indentation Date: Sat, 31 Oct 2009 21:36:58 -0400 Message-ID: <87iqdvyqit.fsf@stats.ox.ac.uk> References: <87ljivlk9s.fsf@stats.ox.ac.uk> <064DBD81-9D10-46D6-911B-67E83EE10A11@gmail.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Return-path: Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1N4PNV-0006qo-Gr for emacs-orgmode@gnu.org; Sat, 31 Oct 2009 21:37:09 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1N4PNR-0006q7-Qj for emacs-orgmode@gnu.org; Sat, 31 Oct 2009 21:37:09 -0400 Received: from [199.232.76.173] (port=51023 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1N4PNR-0006q4-LF for emacs-orgmode@gnu.org; Sat, 31 Oct 2009 21:37:05 -0400 Received: from markov.stats.ox.ac.uk ([163.1.210.1]:48020) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1N4PNR-0006IC-3T for emacs-orgmode@gnu.org; Sat, 31 Oct 2009 21:37:05 -0400 List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: Carsten Dominik Cc: emacs org-mode mailing list --=-=-= Carsten Dominik writes: > Hi Dan, > > maybe the easiest way to implement this would be a -i (or whatever) > switch a the src block. Thanks, I have gone ahead with your suggestion. As you suggest, I've added a -i switch that prevents the indentation of a block from being removed during export. If the variable org-src-preserve-indentation[1] is non-nil, then that is equivalent to having a -i switch on every source block. Then the issue remained that an org-exp-block plugin is unable to return a block with the original indentation if the indentation is removed before it receives it. So I have made that indentation removal contingent on the same criteria (non-nil variable or -i switch). Those 2 commits are in branch ded-indent at git://repo.or.cz/org-mode/babel.git, and also pasted below. Dan Footnotes: [1] org-src-preserve-indentation also acts in org-src-mode, where it prevents indentation from being removed when switching between org and language mode buffers. --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=0001-Allow-code-indentation-to-be-preserved-on-export.patch >From d9447ad7b08332cac005bcb602ac1e55cf0588cf Mon Sep 17 00:00:00 2001 From: Dan Davison Date: Sat, 31 Oct 2009 15:43:42 -0400 Subject: [PATCH 1/2] Allow code indentation to be preserved on export This is controlled on a per-block basis with a new src block flag -i. It can be controlled globally using the variable org-src-preserve-indentation. --- lisp/org-exp.el | 4 +++- lisp/org-src.el | 12 +++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/lisp/org-exp.el b/lisp/org-exp.el index f08ce41..c6ffe75 100644 --- a/lisp/org-exp.el +++ b/lisp/org-exp.el @@ -2273,6 +2273,8 @@ INDENT was the original indentation of the block." rpllbl (string-match "-r\\>" opts) keepp (string-match "-k\\>" opts) textareap (string-match "-t\\>" opts) + preserve-indentp (or org-src-preserve-indentation + (string-match "-i\\>" opts)) cols (if (string-match "-w[ \t]+\\([0-9]+\\)" opts) (string-to-number (match-string 1 opts)) 80) @@ -2285,7 +2287,7 @@ INDENT was the original indentation of the block." ;; we cannot use numbering or highlighting. (setq num nil cont nil lang nil)) (if keepp (setq rpllbl 'keep)) - (setq rtn (org-remove-indentation code)) + (setq rtn (if preserve-indentp code (org-remove-indentation code))) (when (string-match "^," rtn) (setq rtn (with-temp-buffer (insert rtn) diff --git a/lisp/org-src.el b/lisp/org-src.el index 463bfa8..de8f8a2 100644 --- a/lisp/org-src.el +++ b/lisp/org-src.el @@ -87,11 +87,13 @@ These are the regions where each line starts with a colon." (defcustom org-src-preserve-indentation nil "If non-nil, leading whitespace characters in source code - blocks are preserved. Otherwise, after editing with - \\[org-edit-src-code], the minimum (across-lines) number of - leading whitespace characters are removed from all lines, and - the code block is then uniformly indented according to the - value of `org-edit-src-content-indentation'." + blocks are preserved on export, and when switching between the + org buffer and the language mode edit buffer. If this variable + is nil then, after editing with \\[org-edit-src-code], the + minimum (across-lines) number of leading whitespace characters + are removed from all lines, and the code block is uniformly + indented according to the value of + `org-edit-src-content-indentation'." :group 'org-edit-structure :type 'boolean) -- 1.6.3.3 --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=0002-Allow-org-exp-blocks-plugins-to-return-block-with-in.patch >From 60a5a63e6c6d66d1925f8cfc1191502bc34a7269 Mon Sep 17 00:00:00 2001 From: Dan Davison Date: Sat, 31 Oct 2009 20:39:57 -0400 Subject: [PATCH 2/2] Allow org-exp-blocks plugins to return block with indentation unaltered. With these changes, if org-src-preserve-indentation is non-nil, or if the block has a -i switch, then the behaviour of org-exp-blocks is altered as follows: 1. Indentation is not removed before passing the block contents to the block-transforming plugin. 2. The result returned by the plugin is not re-indented. --- lisp/org-exp-blocks.el | 17 ++++++++++------- 1 files changed, 10 insertions(+), 7 deletions(-) diff --git a/lisp/org-exp-blocks.el b/lisp/org-exp-blocks.el index 646688d..c92a9fa 100644 --- a/lisp/org-exp-blocks.el +++ b/lisp/org-exp-blocks.el @@ -166,7 +166,7 @@ specified in BLOCKS which default to the value of (save-window-excursion (let ((case-fold-search t) (types '()) - indentation type func start) + indentation type func start body headers preserve-indent) (flet ((interblock (start end) (mapcar (lambda (pair) (funcall (second pair) start end)) org-export-interblocks))) @@ -176,17 +176,20 @@ specified in BLOCKS which default to the value of "^\\([ \t]*\\)#\\+begin_\\(\\S-+\\)[ \t]*\\(.*\\)?[\r\n]\\([^\000]*?\\)[\r\n][ \t]*#\\+end_\\S-+.*" nil t) (setq indentation (length (match-string 1))) (setq type (intern (match-string 2))) + (setq headers (save-match-data (split-string (match-string 3)))) + (setq body (match-string 4)) + (setq preserve-indent (or org-src-preserve-indentation (member "-i" headers))) + (unless preserve-indent + (setq body (save-match-data (org-remove-indentation body)))) (unless (memq type types) (setq types (cons type types))) (save-match-data (interblock start (match-beginning 0))) (if (setq func (cadr (assoc type org-export-blocks))) (progn (replace-match (save-match-data - (if (memq type org-export-blocks-witheld) - "" - (apply func (save-match-data (org-remove-indentation (match-string 4))) - (split-string (match-string 3) " ")))) t t) - ;; indent block - (indent-code-rigidly (match-beginning 0) (match-end 0) indentation))) + (if (memq type org-export-blocks-witheld) "" + (apply func body headers))) t t) + (unless preserve-indent + (indent-code-rigidly (match-beginning 0) (match-end 0) indentation)))) (setq start (match-end 0))) (interblock start (point-max)))))) -- 1.6.3.3 --=-=-= > > Switches are being processed already, so it will be simple to add one, > I think. > > - Carsten > > On Oct 28, 2009, at 2:29 PM, Dan Davison wrote: > >> Fairly late in the export process, org-exp removes the common >> indentation from lines inside blocks (line 2288 of org-exp.el): >> >> (setq rtn (org-remove-indentation code)) >> >> However, what do we do if >> >> 1. A user has a block that she wishes to export with the indentation >> intact? >> >> 2. An org-exp-blocks plugin has a block that it wishes to export with >> the indentation intact? >> >> I propose that we make (1) and (2) possible. An example where (1) >> arises >> is if, when documenting a moderately complex program, different >> parts of >> a class or function are contained in separate blocks. >> >> Q1. Is that aim approved of? >> >> (2) is a bit trickier than (1). To solve (1), we could introduce a fix >> in org-exp.el like >> >> - (setq rtn (org-remove-indentation code)) >> + (setq rtn (if org-src-preserve-indentation code >> + (org-remove-indentation code))) >> >> However in solving (2) the plugin cannot assume that the variable >> org-src-preserve-indentation is in effect (in fact it very likely >> isn't). So we need to come up with a way in which the block resulting >> from an org-exp-blocks plugin can have its indentation survive through >> to the export product, even if org-src-preserve-indentation is >> globally >> nil. Two possibilities are >> >> 1. We adopt a convention whereby if all lines start with some >> character >> (say ,) then that character is removed (but the remaining >> indentation >> is left intact.) >> >> 2. We transplant the indentation-removing work from org-exp.el into >> org-exp-blocks.el, and arrange that it is only performed if the >> block >> is *not* handled by an org-exp-blocks plugin. >> >> Q2. Has this made sense? What solution do you suggest? >> >> Q3. Should we use org-src-preserve-indentation in this way to affect >> export? (It currently has effect when switching between the org buffer >> and the code edit buffer.) >> >> Dan >> >> >> _______________________________________________ >> Emacs-orgmode mailing list >> Remember: use `Reply All' to send replies to the list. >> Emacs-orgmode@gnu.org >> http://lists.gnu.org/mailman/listinfo/emacs-orgmode > > - Carsten > > > > > > _______________________________________________ > Emacs-orgmode mailing list > Remember: use `Reply All' to send replies to the list. > Emacs-orgmode@gnu.org > http://lists.gnu.org/mailman/listinfo/emacs-orgmode --=-=-= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Emacs-orgmode mailing list Remember: use `Reply All' to send replies to the list. Emacs-orgmode@gnu.org http://lists.gnu.org/mailman/listinfo/emacs-orgmode --=-=-=--