emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* self-referencing babel shell code blocks
@ 2021-01-17 21:41 HJ
  2021-01-18  0:12 ` John Kitchin
  0 siblings, 1 reply; 5+ messages in thread
From: HJ @ 2021-01-17 21:41 UTC (permalink / raw)
  To: emacs-orgmode


  hi,

   I'm  trying to figure out how to write a block , header, PROPERTY or 
whatever is necessary so the output of a command in babel source block 
identifies it own position - the line (or block or something) in buffer 
(or at least the file) in which it is written. ( Plus the checked out 
git revision / branch. )


  I've been experimenting with the following, so far without much luck ...

  Which of these pieces of information (see the "echo" line below) is it 
possible to get? Which is easy? (variables VAR[0345] are most critical) 
How would one write a snippet which provides (some of) this information?

  Is it much harder with command 1 being SSH to a remote machine? Would 
":dir user@remotemachine:/some/dir" work better? Or at all?

  Perhaps I would know how to find out VAR6 and VAR7 by running first 
some `git cmd1` and `git cmd2` locally in a " command 0 " - but is there 
a way to get that info from magit somehow?

  But what I am most desperate for is how to cause orgmode block to set 
VAR0, VAR3, VAR4 and VAR5, and - also fairly important - how to use the 
combination of VAR[0345] do open / recall the file onto the screen

  Thanks a million for helping me out here ...

    HJ


This is the sample file :


* heading L1_12
   Hello and welcome, this is a file with self-referencing shell babel source code blocks
   ...
** heading L2_37
   ...
#+NAME: block220
#+BEGIN_SRC  bash  :session sess9
   : command 0 ;  cd _how-do-I-find-the-dir-of-currently-edited-file_ ; VAR6=`git cmd1` ; VAR7=`git cmd2`
   : command 1 ;  ssh -o "SetEnv VAR1=machine VAR2=user VAR3=..."    targetUser@targetMachine
#+END_SRC


...

#+NAME: block225
#+BEGIN_SRC  bash  :session sess9
   : command 2 ; : do some computation on targetMachine
   : command 3 ; echo This code has been launched from emacs process VAR0:_EMACS_SESSION_ID_ \
     on computer $VAR1:_SRC_HOSTNAME_ \
     run by user $VAR2:_USER_ \
     from file $VAR3:_FILENAME_ \
     code block named $VAR4:block225 \
     in this file positioned under headings: $VAR5:heading L1_12/heading L2_37/ \
     which is checked out from $VAR6:_GIT_COMMIT_ID_ \
     on branch $VAR7:_GIT_BRANCH_
   : command 3 continues ; echo To display the buffer which launched this code on the screen of the running emacs session, run the following command :    emacs  _What_the_heck_do_I_put_here?_  '&'
#+END_SRC








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

* Re: self-referencing babel shell code blocks
  2021-01-17 21:41 self-referencing babel shell code blocks HJ
@ 2021-01-18  0:12 ` John Kitchin
  2021-01-18  0:33   ` HJ
  2021-01-18  9:10   ` tomas
  0 siblings, 2 replies; 5+ messages in thread
From: John Kitchin @ 2021-01-18  0:12 UTC (permalink / raw)
  To: HJ; +Cc: org-mode-email

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

I guess this is what you mean:


* heading L1_12
** heading L2_37

#+name: this-block
#+header: :var VAR1=(getenv "HOSTNAME")
#+header: :var VAR2=(message user-login-name)
#+header: :var VAR3=(buffer-file-name)
#+header: :var VAR4=(org-element-property :name (org-element-context))
#+header: :var VAR5=(org-no-properties (org-display-outline-path nil t))
#+header: :var VAR5a=(line-number-at-pos (org-element-property :begin
(org-element-context)))
#+header: :var VAR6=(s-trim (shell-command-to-string "git rev-parse HEAD"))
#+header: :var VAR7=(s-trim (shell-command-to-string "git branch
--show-current"))
#+BEGIN_SRC sh :results raw
echo Run by $VAR2 from $VAR3
echo In a src-block named $VAR4 in $VAR5 at line $VAR5a
echo on commit $VAR6 in git branch $VAR7
#+END_SRC

#+RESULTS: this-block
Run by jkitchin from /Users/jkitchin/Dropbox/emacs/journal/2021/01/17/
2021-01-17.org
In a src-block named this-block in heading L1_12/heading L2_37 at line 140
on commit a07475addc3fdf557fb46a72b1100cf1c9c0805a in git branch master



John

-----------------------------------
Professor John Kitchin
Doherty Hall A207F
Department of Chemical Engineering
Carnegie Mellon University
Pittsburgh, PA 15213
412-268-7803
@johnkitchin
http://kitchingroup.cheme.cmu.edu



On Sun, Jan 17, 2021 at 4:42 PM HJ <hj-orgmode-1@hj.proberto.com> wrote:

>
>   hi,
>
>    I'm  trying to figure out how to write a block , header, PROPERTY or
> whatever is necessary so the output of a command in babel source block
> identifies it own position - the line (or block or something) in buffer
> (or at least the file) in which it is written. ( Plus the checked out
> git revision / branch. )
>
>
>   I've been experimenting with the following, so far without much luck ...
>
>   Which of these pieces of information (see the "echo" line below) is it
> possible to get? Which is easy? (variables VAR[0345] are most critical)
> How would one write a snippet which provides (some of) this information?
>
>   Is it much harder with command 1 being SSH to a remote machine? Would
> ":dir user@remotemachine:/some/dir" work better? Or at all?
>
>   Perhaps I would know how to find out VAR6 and VAR7 by running first
> some `git cmd1` and `git cmd2` locally in a " command 0 " - but is there
> a way to get that info from magit somehow?
>
>   But what I am most desperate for is how to cause orgmode block to set
> VAR0, VAR3, VAR4 and VAR5, and - also fairly important - how to use the
> combination of VAR[0345] do open / recall the file onto the screen
>
>   Thanks a million for helping me out here ...
>
>     HJ
>
>
> This is the sample file :
>
>
> * heading L1_12
>    Hello and welcome, this is a file with self-referencing shell babel
> source code blocks
>    ...
> ** heading L2_37
>    ...
> #+NAME: block220
> #+BEGIN_SRC  bash  :session sess9
>    : command 0 ;  cd _how-do-I-find-the-dir-of-currently-edited-file_ ;
> VAR6=`git cmd1` ; VAR7=`git cmd2`
>    : command 1 ;  ssh -o "SetEnv VAR1=machine VAR2=user VAR3=..."
> targetUser@targetMachine
> #+END_SRC
>
>
> ...
>
> #+NAME: block225
> #+BEGIN_SRC  bash  :session sess9
>    : command 2 ; : do some computation on targetMachine
>    : command 3 ; echo This code has been launched from emacs process
> VAR0:_EMACS_SESSION_ID_ \
>      on computer $VAR1:_SRC_HOSTNAME_ \
>      run by user $VAR2:_USER_ \
>      from file $VAR3:_FILENAME_ \
>      code block named $VAR4:block225 \
>      in this file positioned under headings: $VAR5:heading L1_12/heading
> L2_37/ \
>      which is checked out from $VAR6:_GIT_COMMIT_ID_ \
>      on branch $VAR7:_GIT_BRANCH_
>    : command 3 continues ; echo To display the buffer which launched this
> code on the screen of the running emacs session, run the following command
> :    emacs  _What_the_heck_do_I_put_here?_  '&'
> #+END_SRC
>
>
>
>
>
>
>
>

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

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

* Re: self-referencing babel shell code blocks
  2021-01-18  0:12 ` John Kitchin
@ 2021-01-18  0:33   ` HJ
  2021-01-18  1:22     ` John Kitchin
  2021-01-18  9:10   ` tomas
  1 sibling, 1 reply; 5+ messages in thread
From: HJ @ 2021-01-18  0:33 UTC (permalink / raw)
  To: John Kitchin; +Cc: org-mode-email


Dear Mr Kitchin,

   you are a genius !!!!!!

    A BIG THANK YOU!!!

   Wow. I couldn't imagine someone might be able to do this so quickly 
and perfectly. Wow.

    thank you so much

     HJ

PS: Now I'm off to figure out how to invoke emacs to tell it to open up 
the window and show this file at the place where this-block block 
appears ...


On 1/18/21 1:12 AM, John Kitchin wrote:
> I guess this is what you mean:
>
>
> * heading L1_12
> ** heading L2_37
>
> #+name: this-block
> #+header: :var VAR1=(getenv "HOSTNAME")
> #+header: :var VAR2=(message user-login-name)
> #+header: :var VAR3=(buffer-file-name)
> #+header: :var VAR4=(org-element-property :name (org-element-context))
> #+header: :var VAR5=(org-no-properties (org-display-outline-path nil t))
> #+header: :var VAR5a=(line-number-at-pos (org-element-property :begin 
> (org-element-context)))
> #+header: :var VAR6=(s-trim (shell-command-to-string "git rev-parse 
> HEAD"))
> #+header: :var VAR7=(s-trim (shell-command-to-string "git branch 
> --show-current"))
> #+BEGIN_SRC sh :results raw
> echo Run by $VAR2 from $VAR3
> echo In a src-block named $VAR4 in $VAR5 at line $VAR5a
> echo on commit $VAR6 in git branch $VAR7
> #+END_SRC
>
> #+RESULTS: this-block
> Run by jkitchin from 
> /Users/jkitchin/Dropbox/emacs/journal/2021/01/17/2021-01-17.org 
> <http://2021-01-17.org>
> In a src-block named this-block in heading L1_12/heading L2_37 at line 140
> on commit a07475addc3fdf557fb46a72b1100cf1c9c0805a in git branch master
>
>
>
> John
>
> -----------------------------------
> Professor John Kitchin
> Doherty Hall A207F
> Department of Chemical Engineering
> Carnegie Mellon University
> Pittsburgh, PA 15213
> 412-268-7803
> @johnkitchin
> http://kitchingroup.cheme.cmu.edu <http://kitchingroup.cheme.cmu.edu>
>
>
>
> On Sun, Jan 17, 2021 at 4:42 PM HJ <hj-orgmode-1@hj.proberto.com 
> <mailto:hj-orgmode-1@hj.proberto.com>> wrote:
>
>
>       hi,
>
>        I'm  trying to figure out how to write a block , header,
>     PROPERTY or
>     whatever is necessary so the output of a command in babel source
>     block
>     identifies it own position - the line (or block or something) in
>     buffer
>     (or at least the file) in which it is written. ( Plus the checked out
>     git revision / branch. )
>
>
>       I've been experimenting with the following, so far without much
>     luck ...
>
>       Which of these pieces of information (see the "echo" line below)
>     is it
>     possible to get? Which is easy? (variables VAR[0345] are most
>     critical)
>     How would one write a snippet which provides (some of) this
>     information?
>
>       Is it much harder with command 1 being SSH to a remote machine?
>     Would
>     ":dir user@remotemachine:/some/dir" work better? Or at all?
>
>       Perhaps I would know how to find out VAR6 and VAR7 by running first
>     some `git cmd1` and `git cmd2` locally in a " command 0 " - but is
>     there
>     a way to get that info from magit somehow?
>
>       But what I am most desperate for is how to cause orgmode block
>     to set
>     VAR0, VAR3, VAR4 and VAR5, and - also fairly important - how to
>     use the
>     combination of VAR[0345] do open / recall the file onto the screen
>
>       Thanks a million for helping me out here ...
>
>         HJ
>
>
>     This is the sample file :
>
>
>     * heading L1_12
>        Hello and welcome, this is a file with self-referencing shell
>     babel source code blocks
>        ...
>     ** heading L2_37
>        ...
>     #+NAME: block220
>     #+BEGIN_SRC  bash  :session sess9
>        : command 0 ;  cd
>     _how-do-I-find-the-dir-of-currently-edited-file_ ; VAR6=`git cmd1`
>     ; VAR7=`git cmd2`
>        : command 1 ;  ssh -o "SetEnv VAR1=machine VAR2=user VAR3=..." 
>       targetUser@targetMachine
>     #+END_SRC
>
>
>     ...
>
>     #+NAME: block225
>     #+BEGIN_SRC  bash  :session sess9
>        : command 2 ; : do some computation on targetMachine
>        : command 3 ; echo This code has been launched from emacs
>     process VAR0:_EMACS_SESSION_ID_ \
>          on computer $VAR1:_SRC_HOSTNAME_ \
>          run by user $VAR2:_USER_ \
>          from file $VAR3:_FILENAME_ \
>          code block named $VAR4:block225 \
>          in this file positioned under headings: $VAR5:heading
>     L1_12/heading L2_37/ \
>          which is checked out from $VAR6:_GIT_COMMIT_ID_ \
>          on branch $VAR7:_GIT_BRANCH_
>        : command 3 continues ; echo To display the buffer which
>     launched this code on the screen of the running emacs session, run
>     the following command :    emacs _What_the_heck_do_I_put_here?_  '&'
>     #+END_SRC
>
>
>
>
>
>
>


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

* Re: self-referencing babel shell code blocks
  2021-01-18  0:33   ` HJ
@ 2021-01-18  1:22     ` John Kitchin
  0 siblings, 0 replies; 5+ messages in thread
From: John Kitchin @ 2021-01-18  1:22 UTC (permalink / raw)
  To: HJ; +Cc: org-mode-email

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

see
https://stackoverflow.com/questions/3139970/open-a-file-at-line-with-filenameline-syntax

there are also ways to make fancier org-links, but I think you will find
what you need in that link.

John

-----------------------------------
Professor John Kitchin
Doherty Hall A207F
Department of Chemical Engineering
Carnegie Mellon University
Pittsburgh, PA 15213
412-268-7803
@johnkitchin
http://kitchingroup.cheme.cmu.edu



On Sun, Jan 17, 2021 at 7:33 PM HJ <hj-orgmode-1@hj.proberto.com> wrote:

>
> Dear Mr Kitchin,
>
>    you are a genius !!!!!!
>
>     A BIG THANK YOU!!!
>
>    Wow. I couldn't imagine someone might be able to do this so quickly
> and perfectly. Wow.
>
>     thank you so much
>
>      HJ
>
> PS: Now I'm off to figure out how to invoke emacs to tell it to open up
> the window and show this file at the place where this-block block
> appears ...
>
>
> On 1/18/21 1:12 AM, John Kitchin wrote:
> > I guess this is what you mean:
> >
> >
> > * heading L1_12
> > ** heading L2_37
> >
> > #+name: this-block
> > #+header: :var VAR1=(getenv "HOSTNAME")
> > #+header: :var VAR2=(message user-login-name)
> > #+header: :var VAR3=(buffer-file-name)
> > #+header: :var VAR4=(org-element-property :name (org-element-context))
> > #+header: :var VAR5=(org-no-properties (org-display-outline-path nil t))
> > #+header: :var VAR5a=(line-number-at-pos (org-element-property :begin
> > (org-element-context)))
> > #+header: :var VAR6=(s-trim (shell-command-to-string "git rev-parse
> > HEAD"))
> > #+header: :var VAR7=(s-trim (shell-command-to-string "git branch
> > --show-current"))
> > #+BEGIN_SRC sh :results raw
> > echo Run by $VAR2 from $VAR3
> > echo In a src-block named $VAR4 in $VAR5 at line $VAR5a
> > echo on commit $VAR6 in git branch $VAR7
> > #+END_SRC
> >
> > #+RESULTS: this-block
> > Run by jkitchin from
> > /Users/jkitchin/Dropbox/emacs/journal/2021/01/17/2021-01-17.org
> > <http://2021-01-17.org>
> > In a src-block named this-block in heading L1_12/heading L2_37 at line
> 140
> > on commit a07475addc3fdf557fb46a72b1100cf1c9c0805a in git branch master
> >
> >
> >
> > John
> >
> > -----------------------------------
> > Professor John Kitchin
> > Doherty Hall A207F
> > Department of Chemical Engineering
> > Carnegie Mellon University
> > Pittsburgh, PA 15213
> > 412-268-7803
> > @johnkitchin
> > http://kitchingroup.cheme.cmu.edu <http://kitchingroup.cheme.cmu.edu>
> >
> >
> >
> > On Sun, Jan 17, 2021 at 4:42 PM HJ <hj-orgmode-1@hj.proberto.com
> > <mailto:hj-orgmode-1@hj.proberto.com>> wrote:
> >
> >
> >       hi,
> >
> >        I'm  trying to figure out how to write a block , header,
> >     PROPERTY or
> >     whatever is necessary so the output of a command in babel source
> >     block
> >     identifies it own position - the line (or block or something) in
> >     buffer
> >     (or at least the file) in which it is written. ( Plus the checked out
> >     git revision / branch. )
> >
> >
> >       I've been experimenting with the following, so far without much
> >     luck ...
> >
> >       Which of these pieces of information (see the "echo" line below)
> >     is it
> >     possible to get? Which is easy? (variables VAR[0345] are most
> >     critical)
> >     How would one write a snippet which provides (some of) this
> >     information?
> >
> >       Is it much harder with command 1 being SSH to a remote machine?
> >     Would
> >     ":dir user@remotemachine:/some/dir" work better? Or at all?
> >
> >       Perhaps I would know how to find out VAR6 and VAR7 by running first
> >     some `git cmd1` and `git cmd2` locally in a " command 0 " - but is
> >     there
> >     a way to get that info from magit somehow?
> >
> >       But what I am most desperate for is how to cause orgmode block
> >     to set
> >     VAR0, VAR3, VAR4 and VAR5, and - also fairly important - how to
> >     use the
> >     combination of VAR[0345] do open / recall the file onto the screen
> >
> >       Thanks a million for helping me out here ...
> >
> >         HJ
> >
> >
> >     This is the sample file :
> >
> >
> >     * heading L1_12
> >        Hello and welcome, this is a file with self-referencing shell
> >     babel source code blocks
> >        ...
> >     ** heading L2_37
> >        ...
> >     #+NAME: block220
> >     #+BEGIN_SRC  bash  :session sess9
> >        : command 0 ;  cd
> >     _how-do-I-find-the-dir-of-currently-edited-file_ ; VAR6=`git cmd1`
> >     ; VAR7=`git cmd2`
> >        : command 1 ;  ssh -o "SetEnv VAR1=machine VAR2=user VAR3=..."
> >       targetUser@targetMachine
> >     #+END_SRC
> >
> >
> >     ...
> >
> >     #+NAME: block225
> >     #+BEGIN_SRC  bash  :session sess9
> >        : command 2 ; : do some computation on targetMachine
> >        : command 3 ; echo This code has been launched from emacs
> >     process VAR0:_EMACS_SESSION_ID_ \
> >          on computer $VAR1:_SRC_HOSTNAME_ \
> >          run by user $VAR2:_USER_ \
> >          from file $VAR3:_FILENAME_ \
> >          code block named $VAR4:block225 \
> >          in this file positioned under headings: $VAR5:heading
> >     L1_12/heading L2_37/ \
> >          which is checked out from $VAR6:_GIT_COMMIT_ID_ \
> >          on branch $VAR7:_GIT_BRANCH_
> >        : command 3 continues ; echo To display the buffer which
> >     launched this code on the screen of the running emacs session, run
> >     the following command :    emacs _What_the_heck_do_I_put_here?_  '&'
> >     #+END_SRC
> >
> >
> >
> >
> >
> >
> >
>

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

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

* Re: self-referencing babel shell code blocks
  2021-01-18  0:12 ` John Kitchin
  2021-01-18  0:33   ` HJ
@ 2021-01-18  9:10   ` tomas
  1 sibling, 0 replies; 5+ messages in thread
From: tomas @ 2021-01-18  9:10 UTC (permalink / raw)
  To: John Kitchin; +Cc: HJ, org-mode-email

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

On Sun, Jan 17, 2021 at 07:12:21PM -0500, John Kitchin wrote:
> I guess this is what you mean:
> 
> 
> * heading L1_12
> ** heading L2_37
> 
> #+name: this-block
> #+header: :var VAR1=(getenv "HOSTNAME")
> #+header: :var VAR2=(message user-login-name)

[more good stuff elided]

Not the OP here, but... thanks a bunch!

You just saved me a few hours of hair pulling.

Cheers
 - t

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

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

end of thread, other threads:[~2021-01-18  9:11 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-17 21:41 self-referencing babel shell code blocks HJ
2021-01-18  0:12 ` John Kitchin
2021-01-18  0:33   ` HJ
2021-01-18  1:22     ` John Kitchin
2021-01-18  9:10   ` tomas

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