emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH] ob-ocaml: Support for pretty-printed outputs
@ 2019-10-04 16:52 Robert M. Kovacsics
  2019-10-13  7:48 ` Nicolas Goaziou
  0 siblings, 1 reply; 2+ messages in thread
From: Robert M. Kovacsics @ 2019-10-04 16:52 UTC (permalink / raw)
  To: Org-mode

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

E.g. the following snippet produces multiple lines of output, due to
pretty-printing

  #+BEGIN_SRC ocaml :exports code :eval no-export :results verbatim
(* Note, no need to have parentheses around sqrt, as
application binds the tightest *)
let rec gamma n = if n = 0
                   then (1.0 +. sqrt 5.0) /. 2.0
                   else 1.0 /. (gamma (n-1) -. 1.0)

let nums = List.map gamma (List.init 5 (fun x -> x))
  #+END_SRC

  #+NAME: gamma-tbl
  #+RESULTS[1fd6a3e846afdef51350eb6d7ba15c6844ccc14e]:
: val gamma : int -> float = <fun>
: val nums : float list =
:   [1.6180339887498949; 1.61803398874989468; 1.61803398874989535;
:    1.61803398874989357; 1.61803398874989823]

(In reality this would have 50 or so outputs, point being mathematically
it shouldn't change, but due to floating point errors it does.)

The regexp has the following problems:

- The "." in group 5 (as-was) doesn't match new-lines, so it only
   matches the first line, e.g. as a list
   : - [1.6180339887498949, 1.61803398874989468, 1.61803398874989535,

- When using "\\(.\\|\n\\)", it includes the starting indentation of the
   list, which makes org-babel-script-escape choke (called from
   org-babel-ocaml-read-list, from org-babel-ocaml-parse-output, from
   org-babel-execute:ocaml, the code being modified.

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-ob-ocaml-Support-for-pretty-printed-outputs.patch --]
[-- Type: text/x-diff; name=0001-ob-ocaml-Support-for-pretty-printed-outputs.patch, Size: 2243 bytes --]

From 281de8e248fdd70ecfc69237df83a155c9491f2e Mon Sep 17 00:00:00 2001
From: Kovacsics Robert <rmk35@cam.ac.uk>
Date: Fri, 4 Oct 2019 17:29:00 +0100
Subject: [PATCH] ob-ocaml: Support for pretty-printed outputs

E.g. the following snippet produces multiple lines of output, due to
pretty-printing

 #+BEGIN_SRC ocaml :exports code :eval no-export :results verbatim
(* Note, no need to have parentheses around sqrt, as
application binds the tightest *)
let rec gamma n = if n = 0
                  then (1.0 +. sqrt 5.0) /. 2.0
                  else 1.0 /. (gamma (n-1) -. 1.0)

let nums = List.map gamma (List.init 5 (fun x -> x))
 #+END_SRC

 #+NAME: gamma-tbl
 #+RESULTS[1fd6a3e846afdef51350eb6d7ba15c6844ccc14e]:
: val gamma : int -> float = <fun>
: val nums : float list =
:   [1.6180339887498949; 1.61803398874989468; 1.61803398874989535;
:    1.61803398874989357; 1.61803398874989823]

(In reality this would have 50 or so outputs, point being mathematically
it shouldn't change, but due to floating point errors it does.)

The regexp has the following problems:

- The "." in group 5 (as-was) doesn't match new-lines, so it only
  matches the first line, e.g. as a list
  : - [1.6180339887498949, 1.61803398874989468, 1.61803398874989535,

- When using "\\(.\\|\n\\)", it includes the starting indentation of the
  list, which makes org-babel-script-escape choke (called from
  org-babel-ocaml-read-list, from org-babel-ocaml-parse-output, from
  org-babel-execute:ocaml, the code being modified.
---
 lisp/ob-ocaml.el | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lisp/ob-ocaml.el b/lisp/ob-ocaml.el
index 0c0ffe442..54bc49a2f 100644
--- a/lisp/ob-ocaml.el
+++ b/lisp/ob-ocaml.el
@@ -83,11 +83,11 @@
 	 (raw (org-trim clean))
 	 (result-params (cdr (assq :result-params params))))
     (string-match
-     "\\(\\(.*\n\\)*\\)[^:\n]+ : \\([^=\n]+\\) =\\(\n\\| \\)\\(.+\\)$"
+     "\\(\\(.*\n\\)*\\)[^:\n]+ : \\([^=\n]+\\) =[[:space:]]+\\(\\(.\\|\n\\)+\\)$"
      raw)
     (let ((output (match-string 1 raw))
 	  (type (match-string 3 raw))
-	  (value (match-string 5 raw)))
+	  (value (match-string 4 raw)))
       (org-babel-reassemble-table
        (org-babel-result-cond result-params
 	 (cond
-- 
2.19.2


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] ob-ocaml: Support for pretty-printed outputs
  2019-10-04 16:52 [PATCH] ob-ocaml: Support for pretty-printed outputs Robert M. Kovacsics
@ 2019-10-13  7:48 ` Nicolas Goaziou
  0 siblings, 0 replies; 2+ messages in thread
From: Nicolas Goaziou @ 2019-10-13  7:48 UTC (permalink / raw)
  To: Robert M. Kovacsics; +Cc: Org-mode

Hello,

"Robert M. Kovacsics" <rmk35@cam.ac.uk> writes:

> Subject: [PATCH] ob-ocaml: Support for pretty-printed outputs
>
> E.g. the following snippet produces multiple lines of output, due to
> pretty-printing
>
>  #+BEGIN_SRC ocaml :exports code :eval no-export :results verbatim
> (* Note, no need to have parentheses around sqrt, as
> application binds the tightest *)
> let rec gamma n = if n = 0
>                   then (1.0 +. sqrt 5.0) /. 2.0
>                   else 1.0 /. (gamma (n-1) -. 1.0)
>
> let nums = List.map gamma (List.init 5 (fun x -> x))
>  #+END_SRC
>
>  #+NAME: gamma-tbl
>  #+RESULTS[1fd6a3e846afdef51350eb6d7ba15c6844ccc14e]:
> : val gamma : int -> float = <fun>
> : val nums : float list =
> :   [1.6180339887498949; 1.61803398874989468; 1.61803398874989535;
> :    1.61803398874989357; 1.61803398874989823]
>
> (In reality this would have 50 or so outputs, point being mathematically
> it shouldn't change, but due to floating point errors it does.)
>
> The regexp has the following problems:
>
> - The "." in group 5 (as-was) doesn't match new-lines, so it only
>   matches the first line, e.g. as a list
>   : - [1.6180339887498949, 1.61803398874989468, 1.61803398874989535,
>
> - When using "\\(.\\|\n\\)", it includes the starting indentation of the
>   list, which makes org-babel-script-escape choke (called from
>   org-babel-ocaml-read-list, from org-babel-ocaml-parse-output, from
>   org-babel-execute:ocaml, the code being modified.

Since no-one commented your patch, I applied it. I added TINYCHANGE
cookie at the end of the commit message, since I don't know if you
signed the FSF papers already.

Thank you for your contribution!

Regards,

-- 
Nicolas Goaziou

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2019-10-13  7:48 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-10-04 16:52 [PATCH] ob-ocaml: Support for pretty-printed outputs Robert M. Kovacsics
2019-10-13  7:48 ` Nicolas Goaziou

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).