From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by yquem.inria.fr (Postfix) with ESMTP id CF775BBAF for ; Thu, 8 Apr 2010 19:53:28 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Ar8BAOC0vUtCbwQakWdsb2JhbACPaYtPFQEBAQEJCwoHEQQeuzsFhQmDKA X-IronPort-AV: E=Sophos;i="4.52,171,1270418400"; d="scan'208";a="48584949" Received: from out2.smtp.messagingengine.com ([66.111.4.26]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/ADH-AES256-SHA; 08 Apr 2010 19:53:28 +0200 Received: from compute2.internal (compute2.internal [10.202.2.42]) by gateway1.messagingengine.com (Postfix) with ESMTP id B72D5EAEC9; Thu, 8 Apr 2010 13:53:26 -0400 (EDT) Received: from heartbeat2.messagingengine.com ([10.202.2.161]) by compute2.internal (MEProxy); Thu, 08 Apr 2010 13:53:26 -0400 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=messagingengine.com; h=message-id:date:from:mime-version:to:cc:subject:references:in-reply-to:content-type:content-transfer-encoding; s=smtpout; bh=iQyEEtUeBQSjGdYZtxjg8QKDLRU=; b=ppTJp0uARPXJmd8XPaZMXuL5ENW8y5rh3Ch79kCUYLbNyXMtFnTpmbpC0b2L5ZrytsuhCLkvjB+O9robOOf2M5MqX7UWaoWOUoWLTWrr4Boov7PzCV+a2fFXwBwQ11HTUqCoVxyZtTp/LeOPE0xDfyK8XqzIcMRVZRw9oNHnnUM= X-Sasl-enc: 7YVBZVynj+RnziaXmPyluXMd7HcQRKYdyIbMzu5V4K8I 1270749206 Received: from [192.168.1.61] (64-71-1-162.static.wiline.com [64.71.1.162]) by mail.messagingengine.com (Postfix) with ESMTPSA id 40DE310D4D; Thu, 8 Apr 2010 13:53:26 -0400 (EDT) Message-ID: <4BBE1815.3070403@ens-lyon.org> Date: Thu, 08 Apr 2010 10:53:25 -0700 From: Martin Jambon User-Agent: Thunderbird 2.0.0.22 (X11/20090908) MIME-Version: 1.0 To: ohl@physik.uni-wuerzburg.de Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Format: Fortran (&c.) style continuation lines? References: <20100408105828.3626347A7A@wthp058.physik.uni-wuerzburg.de> In-Reply-To: <20100408105828.3626347A7A@wthp058.physik.uni-wuerzburg.de> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Spam: no; 0.00; ens-lyon:01 thorsten:01 ohl:01 foo:01 printf:01 foo:01 printf:01 programmer's:01 lexical:01 wrote:01 caml-list:01 functions:01 functions:01 imperative:01 bin:01 Thorsten Ohl (TP2) wrote: > Hi, > > for ages, I've been using the following (somewhat hackish) approach to > pretty printing source code that requires special lexical markers to > allow statements that continue over more than one line. (e.g. in > Fortran > > foo = 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 & > + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 & > + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 & > + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 & > + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 & > + 1 + 1 + 1 + 1 + 1 + 1 + 1 > > and in /bin/sh we have the same with "\\" instead of "&"): > > open Format;; > > let continuing = ref true;; > > let wrap_newline () = > let out, flush, newline, space = get_all_formatter_output_functions > () in > let newline' () = > if !continuing then > out " &" 0 2; > newline () in > set_all_formatter_output_functions out flush newline' space;; > > let nl () = > continuing := false; > print_newline (); > continuing := true;; > > let _ = > wrap_newline ();; > > (* Nonsensical example: *) > for statement = 1 to 3 do > printf " @[<2>foo = 1"; > for i = 1 to 100 do > printf "@, + 1" > done; > nl () > done;; > > The requirement to end each statement with "nl ()" is tedious in real > world applications and the use of the global variable "continuing" > violates my sense of aesthetics... > > Is there a more idiomatic approach that I'm missing? Given the imperative nature of the Format module interface, your solution seems right to me. Maybe you'll output up to 2 bytes beyond the margin but I guess that's ok. You may also consider using easy-format, which offers a functional interface on top of Format. The programmer's job is to create a tree containing strings and parameters while the actual printing is done by a single function call. That said, easy-format does not support custom newline strings although I could add the feature. Link: http://martin.jambon.free.fr/easy-format.html Martin -- http://mjambon.com/