From mboxrd@z Thu Jan 1 00:00:00 1970 From: Diego Zamboni Subject: Re: How to intersperse commands with their output in RESULTS block? Date: Fri, 7 Feb 2020 22:30:30 +0100 Message-ID: References: <871rr86w8p.fsf@ucl.ac.uk> <87a75ugz4j.fsf@alphapapa.net> Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="000000000000211b00059e031a5f" Return-path: Received: from eggs.gnu.org ([2001:470:142:3::10]:41896) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j0BD5-0005kx-Vl for emacs-orgmode@gnu.org; Fri, 07 Feb 2020 16:30:49 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j0BD4-0001HS-MJ for emacs-orgmode@gnu.org; Fri, 07 Feb 2020 16:30:47 -0500 Received: from mail-wr1-x42a.google.com ([2a00:1450:4864:20::42a]:33557) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1j0BD4-0001EL-8w for emacs-orgmode@gnu.org; Fri, 07 Feb 2020 16:30:46 -0500 Received: by mail-wr1-x42a.google.com with SMTP id u6so609493wrt.0 for ; Fri, 07 Feb 2020 13:30:45 -0800 (PST) In-Reply-To: <87a75ugz4j.fsf@alphapapa.net> List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane-mx.org@gnu.org Sender: "Emacs-orgmode" To: Adam Porter Cc: Org-mode --000000000000211b00059e031a5f Content-Type: text/plain; charset="UTF-8" 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 < wrote: > Diego Zamboni 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))) > > > --000000000000211b00059e031a5f Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi Adam and Eric for your further comment= s!

I had read a bit about = the =3Drx=3D package but not used it, thanks for the pointer to =3Dxr=3D, m= akes it a lot easier to figure out the syntax.

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

#+property: header-args:sh+ :exports output
#+property= : header-args:sh+ :results output
#+property: header-args:sh+ :wr= ap "src console"
#+property: header-args:sh+ :post clea= nup(data=3D*this*)
#+property: header-args:sh+ :prologue "sc= ript <<EOF" :epilogue "EOF"

=
Then I can write plain =3D#+begin_src sh=3D blocks and they will produ= ce 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 =3Dscript=3D command and produces a nicely formatted=
> session transcript:
>
> #+NAME: cleanup
> #+BEGIN_SRC emacs-lisp :var data=3D"" :results value :export= s none
>=C2=A0 =C2=A0(replace-regexp-in-string
>=C2=A0 =C2=A0 "\\$ exit\\(.\\|\n\\)*$" ""
>=C2=A0 =C2=A0 (replace-regexp-in-string
>=C2=A0 =C2=A0 =C2=A0"^bash-.*\\$" "$"
>=C2=A0 =C2=A0 =C2=A0(replace-regexp-in-string
>=C2=A0 =C2=A0 =C2=A0 "\\(\\(.\\|\n\\)*?\\)\\$\\(.\\|\n\\)*\\'&= quot; ""
>=C2=A0 =C2=A0 =C2=A0 (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<= br> > for how to simplify it).

Hi Diego,

A few suggestions:

1.=C2=A0 You can use `rx' to define regexps in a Lispy way, and the ELP= A
package `xr' converts existing regexp strings to the rx format, which can help in learning rx syntax.=C2=A0 For example:

=C2=A0 (xr "^bash-.*\\$" 'brief) ;;=3D> (seq bol "bas= h-" (0+ nonl) "$")

So you can use that regexp like:

=C2=A0 (replace-regexp-in-string (rx bol "bash-" (0+ nonl) "= $") ...)

This nasty one is much easier with rx:

=C2=A0 (xr "\\(\\(.\\|\n\\)*?\\)\\$\\(.\\|\n\\)*\\'" 'bri= ef)
=C2=A0 ;;=3D>
=C2=A0 ;; (seq (group (*? (group anything)))
=C2=A0 ;;=C2=A0 =C2=A0 =C2=A0 "$" (0+ (group anything)) eos)

2.=C2=A0 To avoid the nested calls, you can use a loop, like:

=C2=A0 (cl-loop for (match replace) in
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(list (list (rx foo bar) "rep= lacement"))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0do (setf string
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (repl= ace-regexp-in-string match replace
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 string)))


--000000000000211b00059e031a5f--