all messages for Emacs-related lists mirrored at yhetil.org
 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

* Re: Prevent creating empty lines with trailing spaces when tangling noweb
  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
  1 sibling, 0 replies; 4+ messages in thread
From: Kaushal Modi @ 2017-05-06  6:54 UTC (permalink / raw)
  To: emacs-org list


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

On Sat, May 6, 2017 at 2:30 AM Kaushal Modi <kaushal.modi@gmail.com> wrote:

> +    (setq body-str (replace-regexp-in-string "^[ \n\r]*\n" "\n"
> body-str)))
>
>
Just one correction to this regexp so that two or more consecutive empty
lines in the source block get retained:

(setq body-str (replace-regexp-in-string "^[ ]+\n" "\n" body-str))

Here's the draft diff again:

diff --git a/lisp/ob-core.el b/lisp/ob-core.el
index cb332ffea92..96628f44ab4 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" "\n" body-str)))
+  (funcall nb-add body-str)))
       (funcall nb-add (buffer-substring index (point-max))))
     new-body))


-- 

Kaushal Modi

[-- Attachment #1.2: Type: text/html, Size: 3219 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

* Re: Prevent creating empty lines with trailing spaces when tangling noweb
  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
  1 sibling, 1 reply; 4+ messages in thread
From: Nicolas Goaziou @ 2017-05-06  7:23 UTC (permalink / raw)
  To: Kaushal Modi; +Cc: emacs-org list

Hello,

Kaushal Modi <kaushal.modi@gmail.com> writes:

> 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?

I lean towards "yes".

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

I don't think this is necessary. This is but a corner-case. That doesn't
require a dedicated switch.

I'd rather clean every empty line upon tangling, or let the user do it
with `org-babel-post-tangle-hook'.

Regards,

-- 
Nicolas Goaziou

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

* Re: Prevent creating empty lines with trailing spaces when tangling noweb
  2017-05-06  7:23 ` Nicolas Goaziou
@ 2017-05-06 12:59   ` Kaushal Modi
  0 siblings, 0 replies; 4+ messages in thread
From: Kaushal Modi @ 2017-05-06 12:59 UTC (permalink / raw)
  To: Nicolas Goaziou; +Cc: emacs-org list

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

On Sat, May 6, 2017, 3:23 AM Nicolas Goaziou <mail@nicolasgoaziou.fr> wrote:

> Hello,
>
> Kaushal Modi <kaushal.modi@gmail.com> writes:
>

> I'd rather clean every empty line upon tangling, or let the user do it
> with `org-babel-post-tangle-hook'.


I just tried this again and it worked this time!

    (add-hook 'org-babel-post-tangle-hook #'delete-trailing-whitespace)
    (add-hook 'org-babel-post-tangle-hook #'save-buffer :append)

Thanks!

Some back-story on what led me to modify ob-core.el:

I did try changing the hook earlier, but that did not work. So this was my
next attempt[1], and that led me to try this approach of preventing white
spaces in the first place.

Earlier I was also trying to tangle using ox-publish with this element in
org-publish-project-alist:

        ("foo"
         :base-directory "/some/dir"
         :publishing-function org-babel-tangle-publish
         :publishing-directory "/some/dir")

Probably the base directory and publishing directory being the same caused
some issue with saving the buffer after delete-trailing-whitespace. So I
ended up having to manually save the tangled file after each publish step.

Or probably I somehow didn't have the hooks right.

After that I switched to calling just the below separately instead of
tangling through publish step:

    (org-babel-tangle file FILE)

And now with the above hook modifications, the deletion of whitespaces is
working exactly as I wanted.

Well, that was an interesting journey.

[1]:
https://github.com/kaushalmodi/eless/blob/aa060eaede906e6bd4205f997074e7048c0c810c/build/build.el#L28-L47
-- 

Kaushal Modi

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

^ permalink raw reply	[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 external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.