emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [babel] Need different beginning for tangled script
@ 2010-02-03 14:14 Rainer M Krug
  2010-02-03 15:42 ` Dan Davison
  0 siblings, 1 reply; 4+ messages in thread
From: Rainer M Krug @ 2010-02-03 14:14 UTC (permalink / raw)
  To: emacs-orgmode


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

Hi

I want to use org-mode to create a submit script for a cluster, and would
need the first lines to be as follow:

#!/bin/bash
#$ -cwd -j y
#$ -t 1-5
#$ -l mem_free=4G

at the moment I get:

#!/usr/bin/env sh
# generated by org-babel-tangle
#
[[file:~/Documents/Projects/BiocontrolAndAlienDynamics/HakeaLandscape/trunc/R/spreadSim.org::*submit%20scripts][block-19]]
#$ -cwd -j y
#$ -t 1-5
#$ -l mem_free=4G

1) Is the first line customizable, or do I have to change org-babel-sh.el to
use #!/bin/bash instead?
2) Can I ommit the second and third line (the comments added by tangle)?

Thanks, and I must say org-mode is getting better and better

Cheers,

Rainer
-- 
NEW GERMAN FAX NUMBER!!!

Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation Biology,
UCT), Dipl. Phys. (Germany)

Centre of Excellence for Invasion Biology
Natural Sciences Building
Office Suite 2039
Stellenbosch University
Main Campus, Merriman Avenue
Stellenbosch
South Africa

Cell:           +27 - (0)83 9479 042
Fax:            +27 - (0)86 516 2782
Fax:            +49 - (0)321 2125 2244
email:          Rainer@krugs.de

Skype:          RMkrug
Google:         R.M.Krug@gmail.com

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

[-- Attachment #2: Type: text/plain, Size: 201 bytes --]

_______________________________________________
Emacs-orgmode mailing list
Please use `Reply All' to send replies to the list.
Emacs-orgmode@gnu.org
http://lists.gnu.org/mailman/listinfo/emacs-orgmode

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

* Re: [babel] Need different beginning for tangled script
  2010-02-03 14:14 [babel] Need different beginning for tangled script Rainer M Krug
@ 2010-02-03 15:42 ` Dan Davison
  2010-02-03 17:19   ` Eric Schulte
  2010-02-03 17:36   ` Dan Davison
  0 siblings, 2 replies; 4+ messages in thread
From: Dan Davison @ 2010-02-03 15:42 UTC (permalink / raw)
  To: Rainer M Krug; +Cc: emacs-orgmode

Rainer M Krug <r.m.krug@gmail.com> writes:

> Hi
>
> I want to use org-mode to create a submit script for a cluster, and would need
> the first lines to be as follow:
>
> #!/bin/bash
> #$ -cwd -j y
> #$ -t 1-5
> #$ -l mem_free=4G
>
> at the moment I get:
>
> #!/usr/bin/env sh
> # generated by org-babel-tangle
> # [[file:~/Documents/Projects/BiocontrolAndAlienDynamics/HakeaLandscape/trunc/R
> /spreadSim.org::*submit%20scripts][block-19]]
> #$ -cwd -j y
> #$ -t 1-5
> #$ -l mem_free=4G
>
> 1) Is the first line customizable, or do I have to change org-babel-sh.el to
> use #!/bin/bash instead?
> 2) Can I ommit the second and third line (the comments added by tangle)?

Hi Rainer,

Yes, I've also wanted control over these things from time to
time. Here's a patch. It introduces two new variables; you would turn
off both behaviours as follows:

(setq org-babel-tangle-include-shebang nil)
(setq org-babel-tangle-include-org-coordinates nil)

If Eric approves, he'll apply the patch to the main org-mode git repo. I
suspect you're using git already, but if not then you probably do want
to use it for org-mode.

Dan

From ebc119693ca9daf104f935e794b5e2122127fa05 Mon Sep 17 00:00:00 2001
From: Dan Davison <davison@stats.ox.ac.uk>
Date: Wed, 3 Feb 2010 10:41:10 -0500
Subject: [PATCH] babel: provide control over additional content in tangled files

New variables:
* org-babel-tangle-include-shebang
  Whether to include shebang
* org-babel-tangle-include-org-coordinates
  Whether to include links to org file etc.
---
 contrib/babel/lisp/org-babel-tangle.el |   32 ++++++++++++++++++++++++++------
 1 files changed, 26 insertions(+), 6 deletions(-)

diff --git a/contrib/babel/lisp/org-babel-tangle.el b/contrib/babel/lisp/org-babel-tangle.el
index 66e2c26..c019c43 100644
--- a/contrib/babel/lisp/org-babel-tangle.el
+++ b/contrib/babel/lisp/org-babel-tangle.el
@@ -40,6 +40,22 @@ to use when writing out the language to file, and an optional
 fourth element is a flag which when true indicates that the
 language does not support comments.")
 
+(defvar org-babel-tangle-include-shebang t
+  "If non-nil the first line of tangled output will be a
+'shebang'-style line, for example,
+
+#!/bin/sh
+
+On UNIX/linux-like operating systems these lines serve to specify
+an interpreter for the code contained in the file.")
+
+(defvar org-babel-tangle-include-org-coordinates t
+  "If non-nil (and if a comment syntax is defined), each block in
+the tangled output will be (a) preceded by a commented org-style
+link back to the source block in the source org file, and (b)
+followed by a commented statement indicating which block has just
+ended.")
+
 (defun org-babel-load-file (file)
   "Load the contents of the Emacs Lisp source code blocks in the
 org-mode formatted FILE.  This function will first export the
@@ -117,12 +133,12 @@ exported source code blocks by language."
                   ;; drop source-block to file
                   (with-temp-buffer
                     (funcall lang-f)
-                    (when (and she-bang (not (member file-name she-banged)))
+                    (when (and org-babel-tangle-include-shebang she-bang (not (member file-name she-banged)))
                       (insert (concat she-bang "\n"))
                       (setq she-banged (cons file-name she-banged)))
-                    (when commentable
+                    (when (and commentable org-babel-tangle-include-org-coordinates)
                       (comment-region
-                       (point) (progn (insert "generated by org-babel-tangle") (point)))
+                       (point) (progn (insert "Generated by org-babel-tangle") (point)))
                       (move-end-of-line nil))
                     (org-babel-spec-to-string spec)
                     (append-to-file nil nil file-name))
@@ -188,8 +204,8 @@ form
 
   (link source-name params body)"
   (flet ((insert-comment (text)
+			 (insert "\n")
                          (when commentable
-                           (insert "\n")
                            (comment-region (point) (progn (insert text) (point)))
                            (move-end-of-line nil)
                            (insert "\n"))))
@@ -197,9 +213,13 @@ form
           (source-name (second spec))
           (body (fourth spec))
           (commentable (not (fifth spec))))
-      (insert-comment (format "[[%s][%s]]" (org-link-escape link) source-name))
+      (if org-babel-tangle-include-org-coordinates
+	  (insert-comment (format "[[%s][%s]]" (org-link-escape link) source-name))
+	(insert "\n"))
       (insert (format "%s" (org-babel-chomp body)))
-      (insert-comment (format "%s ends here" source-name)))))
+      (if org-babel-tangle-include-org-coordinates
+	  (insert-comment (format "%s ends here" source-name))
+	(insert "\n")))))
 
 (provide 'org-babel-tangle)
 ;;; org-babel-tangle.el ends here
-- 
1.6.3.3

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

* Re: [babel] Need different beginning for tangled script
  2010-02-03 15:42 ` Dan Davison
@ 2010-02-03 17:19   ` Eric Schulte
  2010-02-03 17:36   ` Dan Davison
  1 sibling, 0 replies; 4+ messages in thread
From: Eric Schulte @ 2010-02-03 17:19 UTC (permalink / raw)
  To: Dan Davison; +Cc: emacs-orgmode, Rainer M Krug

Hi Rainer and Dan,

I have comments below, I believe this behavior should be possible
through manipulation of some existing org-babel variables.

Dan Davison <davison@stats.ox.ac.uk> writes:

> Rainer M Krug <r.m.krug@gmail.com> writes:
>
>> Hi
>>
>> I want to use org-mode to create a submit script for a cluster, and would need
>> the first lines to be as follow:
>>
>> #!/bin/bash
>> #$ -cwd -j y
>> #$ -t 1-5
>> #$ -l mem_free=4G
>>
>> at the moment I get:
>>
>> #!/usr/bin/env sh
>> # generated by org-babel-tangle
>> # [[file:~/Documents/Projects/BiocontrolAndAlienDynamics/HakeaLandscape/trunc/R
>> /spreadSim.org::*submit%20scripts][block-19]]
>> #$ -cwd -j y
>> #$ -t 1-5
>> #$ -l mem_free=4G
>>
>> 1) Is the first line customizable, or do I have to change org-babel-sh.el to
>> use #!/bin/bash instead?

The first line is customizable through adjusting the value of the
org-babel-tangle-langs variable as show below.

>>
>> 2) Can I ommit the second and third line (the comments added by tangle)?
>

The commenting behavior can also be changed using the
org-babel-tangle-langs variable.

The following non-trivial elisp snippet could be added to your emacs
init to make both of these changes.

--8<---------------cut here---------------start------------->8---

(setq org-babel-tangle-langs
      (cons
       '("sh" "sh" "#!/bin/bash" t)
       (remove-if (lambda (el) (string= (car el) "sh")) org-babel-tangle-langs)))

--8<---------------cut here---------------end--------------->8---

the third element of the '("sh" "sh" "#!/bin/bash" t) list will appear
at the top of your tangled files, and the t at the end of the list
should suppress all org-babel comments.

Perhaps we should change `org-babel-tangle-langs' to an a-list and
declare it behind a defvar to make these sorts of customizations easier
in the future.

Best -- Eric

>
> Hi Rainer,
>
> Yes, I've also wanted control over these things from time to
> time. Here's a patch. It introduces two new variables; you would turn
> off both behaviours as follows:
>
> (setq org-babel-tangle-include-shebang nil)
> (setq org-babel-tangle-include-org-coordinates nil)
>
> If Eric approves, he'll apply the patch to the main org-mode git repo. I
> suspect you're using git already, but if not then you probably do want
> to use it for org-mode.
>
> Dan
>
> From ebc119693ca9daf104f935e794b5e2122127fa05 Mon Sep 17 00:00:00 2001
> From: Dan Davison <davison@stats.ox.ac.uk>
> Date: Wed, 3 Feb 2010 10:41:10 -0500
> Subject: [PATCH] babel: provide control over additional content in tangled files
>
> New variables:
> * org-babel-tangle-include-shebang
>   Whether to include shebang
> * org-babel-tangle-include-org-coordinates
>   Whether to include links to org file etc.
> ---
>  contrib/babel/lisp/org-babel-tangle.el |   32 ++++++++++++++++++++++++++------
>  1 files changed, 26 insertions(+), 6 deletions(-)
>
> diff --git a/contrib/babel/lisp/org-babel-tangle.el b/contrib/babel/lisp/org-babel-tangle.el
> index 66e2c26..c019c43 100644
> --- a/contrib/babel/lisp/org-babel-tangle.el
> +++ b/contrib/babel/lisp/org-babel-tangle.el
> @@ -40,6 +40,22 @@ to use when writing out the language to file, and an optional
>  fourth element is a flag which when true indicates that the
>  language does not support comments.")
>  
> +(defvar org-babel-tangle-include-shebang t
> +  "If non-nil the first line of tangled output will be a
> +'shebang'-style line, for example,
> +
> +#!/bin/sh
> +
> +On UNIX/linux-like operating systems these lines serve to specify
> +an interpreter for the code contained in the file.")
> +
> +(defvar org-babel-tangle-include-org-coordinates t
> +  "If non-nil (and if a comment syntax is defined), each block in
> +the tangled output will be (a) preceded by a commented org-style
> +link back to the source block in the source org file, and (b)
> +followed by a commented statement indicating which block has just
> +ended.")
> +
>  (defun org-babel-load-file (file)
>    "Load the contents of the Emacs Lisp source code blocks in the
>  org-mode formatted FILE.  This function will first export the
> @@ -117,12 +133,12 @@ exported source code blocks by language."
>                    ;; drop source-block to file
>                    (with-temp-buffer
>                      (funcall lang-f)
> -                    (when (and she-bang (not (member file-name she-banged)))
> +                    (when (and org-babel-tangle-include-shebang she-bang (not (member file-name she-banged)))
>                        (insert (concat she-bang "\n"))
>                        (setq she-banged (cons file-name she-banged)))
> -                    (when commentable
> +                    (when (and commentable org-babel-tangle-include-org-coordinates)
>                        (comment-region
> -                       (point) (progn (insert "generated by org-babel-tangle") (point)))
> +                       (point) (progn (insert "Generated by org-babel-tangle") (point)))
>                        (move-end-of-line nil))
>                      (org-babel-spec-to-string spec)
>                      (append-to-file nil nil file-name))
> @@ -188,8 +204,8 @@ form
>  
>    (link source-name params body)"
>    (flet ((insert-comment (text)
> +			 (insert "\n")
>                           (when commentable
> -                           (insert "\n")
>                             (comment-region (point) (progn (insert text) (point)))
>                             (move-end-of-line nil)
>                             (insert "\n"))))
> @@ -197,9 +213,13 @@ form
>            (source-name (second spec))
>            (body (fourth spec))
>            (commentable (not (fifth spec))))
> -      (insert-comment (format "[[%s][%s]]" (org-link-escape link) source-name))
> +      (if org-babel-tangle-include-org-coordinates
> +	  (insert-comment (format "[[%s][%s]]" (org-link-escape link) source-name))
> +	(insert "\n"))
>        (insert (format "%s" (org-babel-chomp body)))
> -      (insert-comment (format "%s ends here" source-name)))))
> +      (if org-babel-tangle-include-org-coordinates
> +	  (insert-comment (format "%s ends here" source-name))
> +	(insert "\n")))))
>  
>  (provide 'org-babel-tangle)
>  ;;; org-babel-tangle.el ends here

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

* Re: [babel] Need different beginning for tangled script
  2010-02-03 15:42 ` Dan Davison
  2010-02-03 17:19   ` Eric Schulte
@ 2010-02-03 17:36   ` Dan Davison
  1 sibling, 0 replies; 4+ messages in thread
From: Dan Davison @ 2010-02-03 17:36 UTC (permalink / raw)
  To: Rainer M Krug; +Cc: emacs-orgmode

Dan Davison <davison@stats.ox.ac.uk> writes:

> Rainer M Krug <r.m.krug@gmail.com> writes:
>
>> Hi
>>
>> I want to use org-mode to create a submit script for a cluster, and would need
>> the first lines to be as follow:
>>
>> #!/bin/bash
>> #$ -cwd -j y
>> #$ -t 1-5
>> #$ -l mem_free=4G
>>
>> at the moment I get:
>>
>> #!/usr/bin/env sh
>> # generated by org-babel-tangle
>> # [[file:~/Documents/Projects/BiocontrolAndAlienDynamics/HakeaLandscape/trunc/R
>> /spreadSim.org::*submit%20scripts][block-19]]
>> #$ -cwd -j y
>> #$ -t 1-5
>> #$ -l mem_free=4G
>>
>> 1) Is the first line customizable, or do I have to change org-babel-sh.el to
>> use #!/bin/bash instead?

Hi Rainer,

I see that my patch in fact answered your question (2) and your other
question from your other email. But as for question (1), yes, see

C-h v org-babel-tangle-langs

The value of this variable for me is

(("makefile")
 ("C" "c")
 ("sql" "sql")
 ("perl" "pl" "#!/usr/bin/env perl")
 ("sass" "sass")
 ("dot" "dot")
 ("asymptote" "asymptote")
 ("latex" "tex")
 ("ditaa" "ditaa")
 ("ruby" "rb" "#!/usr/bin/env ruby")
 ("python" "py" "#!/usr/bin/env python")
 ("R" "R" "#!/usr/bin/env Rscript")
 ("sh" "sh" "#!/usr/bin/env sh")
 ("emacs-lisp" "el"))


One solution would be to change it so that the entry for sh is

 ("sh" "sh" "#!/bin/bash")


[Having taken some time off, my elisp is now failing me. What's the
right way to do this? If the cars of this list were symbols then we
could use assq-delete-all but as they're strings we can't. This seems
cumbersome

(setq org-babel-tangle-langs
      (cons '("sh" "sh" "#!/bin/bash")
	    (delete (assoc "sh" org-babel-tangle-langs) org-babel-tangle-langs)))

]

Dan



>> 2) Can I ommit the second and third line (the comments added by tangle)?
>
> Hi Rainer,
>
> Yes, I've also wanted control over these things from time to
> time. Here's a patch. It introduces two new variables; you would turn
> off both behaviours as follows:
>
> (setq org-babel-tangle-include-shebang nil)
> (setq org-babel-tangle-include-org-coordinates nil)
>
> If Eric approves, he'll apply the patch to the main org-mode git repo. I
> suspect you're using git already, but if not then you probably do want
> to use it for org-mode.
>
> Dan
>
>From ebc119693ca9daf104f935e794b5e2122127fa05 Mon Sep 17 00:00:00 2001
> From: Dan Davison <davison@stats.ox.ac.uk>
> Date: Wed, 3 Feb 2010 10:41:10 -0500
> Subject: [PATCH] babel: provide control over additional content in tangled files
>
> New variables:
> * org-babel-tangle-include-shebang
>   Whether to include shebang
> * org-babel-tangle-include-org-coordinates
>   Whether to include links to org file etc.
> ---
>  contrib/babel/lisp/org-babel-tangle.el |   32 ++++++++++++++++++++++++++------
>  1 files changed, 26 insertions(+), 6 deletions(-)
>
> diff --git a/contrib/babel/lisp/org-babel-tangle.el b/contrib/babel/lisp/org-babel-tangle.el
> index 66e2c26..c019c43 100644
> --- a/contrib/babel/lisp/org-babel-tangle.el
> +++ b/contrib/babel/lisp/org-babel-tangle.el
> @@ -40,6 +40,22 @@ to use when writing out the language to file, and an optional
>  fourth element is a flag which when true indicates that the
>  language does not support comments.")
>  
> +(defvar org-babel-tangle-include-shebang t
> +  "If non-nil the first line of tangled output will be a
> +'shebang'-style line, for example,
> +
> +#!/bin/sh
> +
> +On UNIX/linux-like operating systems these lines serve to specify
> +an interpreter for the code contained in the file.")
> +
> +(defvar org-babel-tangle-include-org-coordinates t
> +  "If non-nil (and if a comment syntax is defined), each block in
> +the tangled output will be (a) preceded by a commented org-style
> +link back to the source block in the source org file, and (b)
> +followed by a commented statement indicating which block has just
> +ended.")
> +
>  (defun org-babel-load-file (file)
>    "Load the contents of the Emacs Lisp source code blocks in the
>  org-mode formatted FILE.  This function will first export the
> @@ -117,12 +133,12 @@ exported source code blocks by language."
>                    ;; drop source-block to file
>                    (with-temp-buffer
>                      (funcall lang-f)
> -                    (when (and she-bang (not (member file-name she-banged)))
> +                    (when (and org-babel-tangle-include-shebang she-bang (not (member file-name she-banged)))
>                        (insert (concat she-bang "\n"))
>                        (setq she-banged (cons file-name she-banged)))
> -                    (when commentable
> +                    (when (and commentable org-babel-tangle-include-org-coordinates)
>                        (comment-region
> -                       (point) (progn (insert "generated by org-babel-tangle") (point)))
> +                       (point) (progn (insert "Generated by org-babel-tangle") (point)))
>                        (move-end-of-line nil))
>                      (org-babel-spec-to-string spec)
>                      (append-to-file nil nil file-name))
> @@ -188,8 +204,8 @@ form
>  
>    (link source-name params body)"
>    (flet ((insert-comment (text)
> +			 (insert "\n")
>                           (when commentable
> -                           (insert "\n")
>                             (comment-region (point) (progn (insert text) (point)))
>                             (move-end-of-line nil)
>                             (insert "\n"))))
> @@ -197,9 +213,13 @@ form
>            (source-name (second spec))
>            (body (fourth spec))
>            (commentable (not (fifth spec))))
> -      (insert-comment (format "[[%s][%s]]" (org-link-escape link) source-name))
> +      (if org-babel-tangle-include-org-coordinates
> +	  (insert-comment (format "[[%s][%s]]" (org-link-escape link) source-name))
> +	(insert "\n"))
>        (insert (format "%s" (org-babel-chomp body)))
> -      (insert-comment (format "%s ends here" source-name)))))
> +      (if org-babel-tangle-include-org-coordinates
> +	  (insert-comment (format "%s ends here" source-name))
> +	(insert "\n")))))
>  
>  (provide 'org-babel-tangle)
>  ;;; org-babel-tangle.el ends here

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

end of thread, other threads:[~2010-02-03 17:36 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-02-03 14:14 [babel] Need different beginning for tangled script Rainer M Krug
2010-02-03 15:42 ` Dan Davison
2010-02-03 17:19   ` Eric Schulte
2010-02-03 17:36   ` Dan Davison

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