emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to intersperse commands with their output in RESULTS block?


From: Diego Zamboni
Subject: Re: How to intersperse commands with their output in RESULTS block?
Date: Fri, 7 Feb 2020 22:30:30 +0100

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="">
#+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 <address@hidden> wrote:
Diego Zamboni <address@hidden> 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)))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]