emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Prevent creating empty lines with trailing spaces when tangling noweb
@ 2017-05-06  6:30 Kaushal Modi
  2017-05-06  6:54 ` Kaushal Modi
  2017-05-06  7:23 ` Nicolas Goaziou
  0 siblings, 2 replies; 4+ messages in thread
From: Kaushal Modi @ 2017-05-06  6:30 UTC (permalink / raw)
  To: emacs-org list


[-- Attachment #1.1: Type: text/plain, Size: 2800 bytes --]

Hello,

I have this MWE:

#+TITLE: Org babel tangle and noweb
#+PROPERTY: header-args:shell :shebang "#!/usr/bin/env bash"

* Shell Script
#+BEGIN_SRC shell :noweb yes :tangle code.sh
exec emacs -Q "$@" \
           --eval '(progn
                      <<elisp>>
                   )' 2>/dev/null </dev/tty
#+END_SRC
* Emacs Configuration
#+BEGIN_SRC emacs-lisp :noweb-ref elisp :noweb-sep "\\n\\n"
(menu-bar-mode -1)
#+END_SRC
Some comment.
#+BEGIN_SRC emacs-lisp :noweb-ref elisp
(line-number-mode 1)
#+END_SRC


When tangled (C-c C-v t), it generates:

[image: image.png]

Notice that the empty line is created between noweb segments as expected
because of ":noweb-sep "\\n\\n"". But what was unexpected to me was the
creation of empty line with spaces inserted to match the indentation.

Is this by design?

If it is by design, I would propose to have a new switch called something
like ":noweb-notrailingspc" (would like a better suggestion for this switch
name).

So the src block where noweb expansion needs to happen will look like this:

#+BEGIN_SRC shell :noweb yes :tangle code.sh :noweb-notrailingspc yes
exec emacs -Q "$@" \
           --eval '(progn
                      <<elisp>>
                   )' 2>/dev/null </dev/tty
#+END_SRC

Now, tangling will generate

[image: image.png]

Here is a diff (without whitespace differences) of the change I made to
ob-core locally to get this:

diff --git a/lisp/ob-core.el b/lisp/ob-core.el
index cb332ffea92..0f04d8b85e1 100644
--- a/lisp/ob-core.el
+++ b/lisp/ob-core.el
@@ -2730,9 +2730,7 @@ block but are passed literally to the
\"example-block\"."
  (funcall nb-add (buffer-substring index (point)))
  (goto-char (match-end 0))
  (setq index (point))
- (funcall
-         nb-add
-         (with-current-buffer parent-buffer
+ (let ((body-str (with-current-buffer parent-buffer
   (save-restriction
     (widen)
     (mapconcat ;; Interpose PREFIX between every line.
@@ -2794,6 +2792,9 @@ block but are passed literally to the
\"example-block\"."
   "`org-babel-noweb-error-langs')"))
    "")))
       "[\n\r]") (concat "\n" prefix))))))
+  (when (string= "yes" (cdr (assq :noweb-notrailingspc (nth 2 info))))
+    (setq body-str (replace-regexp-in-string "^[ \n\r]*\n" "\n" body-str)))
+  (funcall nb-add body-str)))
       (funcall nb-add (buffer-substring index (point-max))))
     new-body))


Questions:

1. If the above empty lines with prefix number of spaces are not expected,
the fix would be simpler.. to just throw in that (setq body-str
(replace-regexp-in-string "^[ \n\r]*\n" "\n" body-str)) form in-between.

2a. If the spacey empty lines are as per design, then does this diff look
good? Should I work on a proper patch?
2b. What would be a better/more intuitive switch name than
":noweb-notrailingspc"?
-- 

Kaushal Modi

[-- Attachment #1.2: Type: text/html, Size: 5195 bytes --]

[-- Attachment #2: image.png --]
[-- Type: image/png, Size: 17908 bytes --]

[-- Attachment #3: image.png --]
[-- Type: image/png, Size: 17794 bytes --]

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

end of thread, other threads:[~2017-05-06 12:59 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-05-06  6:30 Prevent creating empty lines with trailing spaces when tangling noweb Kaushal Modi
2017-05-06  6:54 ` Kaushal Modi
2017-05-06  7:23 ` Nicolas Goaziou
2017-05-06 12:59   ` Kaushal Modi

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