|
From: | Diego Zamboni |
Subject: | Re: How to intersperse commands with their output in RESULTS block? |
Date: | Fri, 7 Feb 2020 22:30:30 +0100 |
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)))
[Prev in Thread] | Current Thread | [Next in Thread] |