From mboxrd@z Thu Jan 1 00:00:00 1970 From: Adam Porter Subject: Re: How to intersperse commands with their output in RESULTS block? Date: Fri, 07 Feb 2020 10:07:08 -0600 Message-ID: <87a75ugz4j.fsf@alphapapa.net> References: <871rr86w8p.fsf@ucl.ac.uk> Mime-Version: 1.0 Content-Type: text/plain Return-path: Received: from eggs.gnu.org ([2001:470:142:3::10]:49079) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j06A6-0003Aj-GS for emacs-orgmode@gnu.org; Fri, 07 Feb 2020 11:07:24 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j06A4-0005g8-Jt for emacs-orgmode@gnu.org; Fri, 07 Feb 2020 11:07:21 -0500 Received: from ciao.gmane.io ([159.69.161.202]:44462) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1j06A3-0005dP-BA for emacs-orgmode@gnu.org; Fri, 07 Feb 2020 11:07:20 -0500 Received: from list by ciao.gmane.io with local (Exim 4.92) (envelope-from ) id 1j06A0-000Hes-Gu for emacs-orgmode@gnu.org; Fri, 07 Feb 2020 17:07:16 +0100 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: emacs-orgmode@gnu.org 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)))