emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Diego Zamboni <diego@zzamboni.org>
To: Adam Porter <adam@alphapapa.net>
Cc: Org-mode <emacs-orgmode@gnu.org>
Subject: Re: How to intersperse commands with their output in RESULTS block?
Date: Fri, 7 Feb 2020 22:30:30 +0100	[thread overview]
Message-ID: <CAGY83EcNEbLosAZSJ6ENtDAVsmO=7YqDCAXFgPXZJWD88wJrdA@mail.gmail.com> (raw)
In-Reply-To: <87a75ugz4j.fsf@alphapapa.net>

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

Hi Adam and Eric for your further comments!

I had read a bit about the =rx= package but not used it, thanks for the
pointer to =xr=, makes it a lot easier to figure out the syntax.

One more thing I realized is that I can make the desired settings the
defaults within my document, and even automatically wrap all blocks with
the =script= command, by setting the corresponding properties:

#+property: header-args:sh+ :exports output
#+property: header-args:sh+ :results output
#+property: header-args:sh+ :wrap "src console"
#+property: header-args:sh+ :post cleanup(data=*this*)
#+property: header-args:sh+ :prologue "script <<EOF" :epilogue "EOF"

Then I can write plain =#+begin_src sh= blocks and they will produce the
result I want.

I love org and its community - thanks again!

--Diego


On Fri, Feb 7, 2020 at 5:07 PM Adam Porter <adam@alphapapa.net> wrote:

> Diego Zamboni <diego@zzamboni.org> writes:
>
> > I came up with the following block, which cleans up all the cruft from
> > the output of the =script= command and produces a nicely formatted
> > session transcript:
> >
> > #+NAME: cleanup
> > #+BEGIN_SRC emacs-lisp :var data="" :results value :exports none
> >   (replace-regexp-in-string
> >    "\\$ exit\\(.\\|\n\\)*$" ""
> >    (replace-regexp-in-string
> >     "^bash-.*\\$" "$"
> >     (replace-regexp-in-string
> >      "\\(\\(.\\|\n\\)*?\\)\\$\\(.\\|\n\\)*\\'" ""
> >      (replace-regexp-in-string "
> > " "" data) nil nil 1)))
> > #+END_SRC
> >
> > (I am not happy with the regexp nesting and repetition above, I am not
> > an expert yet in emacs-lisp regex facilities. Suggestions appreciated
> > for how to simplify it).
>
> Hi Diego,
>
> A few suggestions:
>
> 1.  You can use `rx' to define regexps in a Lispy way, and the ELPA
> package `xr' converts existing regexp strings to the rx format, which
> can help in learning rx syntax.  For example:
>
>   (xr "^bash-.*\\$" 'brief) ;;=> (seq bol "bash-" (0+ nonl) "$")
>
> So you can use that regexp like:
>
>   (replace-regexp-in-string (rx bol "bash-" (0+ nonl) "$") ...)
>
> This nasty one is much easier with rx:
>
>   (xr "\\(\\(.\\|\n\\)*?\\)\\$\\(.\\|\n\\)*\\'" 'brief)
>   ;;=>
>   ;; (seq (group (*? (group anything)))
>   ;;      "$" (0+ (group anything)) eos)
>
> 2.  To avoid the nested calls, you can use a loop, like:
>
>   (cl-loop for (match replace) in
>            (list (list (rx foo bar) "replacement"))
>            do (setf string
>                     (replace-regexp-in-string match replace
>                                               string)))
>
>
>

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

  reply	other threads:[~2020-02-07 21:30 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-05 17:25 How to intersperse commands with their output in RESULTS block? Diego Zamboni
2020-02-06  6:55 ` Fraga, Eric
2020-02-06 20:45   ` Diego Zamboni
2020-02-07 15:26     ` Diego Zamboni
2020-02-07 15:49       ` Fraga, Eric
2020-02-07 16:07       ` Adam Porter
2020-02-07 21:30         ` Diego Zamboni [this message]
2020-02-08 10:04           ` Fraga, Eric

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAGY83EcNEbLosAZSJ6ENtDAVsmO=7YqDCAXFgPXZJWD88wJrdA@mail.gmail.com' \
    --to=diego@zzamboni.org \
    --cc=adam@alphapapa.net \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).