* _FOLD
* FOLD() function recursive code
* Copyright (c) 2006 Ladybridge Systems, All Rights Reserved
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
* 
* Ladybridge Systems can be contacted via the www.openqm.com web site.
* 
* START-HISTORY:
* 07 Jul 06  2.4-9 Added delim argument and mutlivalued width handling.
* 16 Sep 04  2.0-1 OpenQM launch. Earlier history details suppressed.
* END-HISTORY
*
* START-DESCRIPTION:
*
* END-DESCRIPTION
*
* START-CODE

$internal
$no.symbols
$no.xref
$recursive

subroutine fold(string, (width), (delim))

   * If the delimiter is a null string (must be three argument format for
   * this to happen), use a value mark by default.

   delim = if delim = '' then @vm else delim[1,1]

   * The Pick style definition of FOLD() allows a multivalued width
   * argument where each element corresponds to the length of successive
   * lines, reusing the final value if we run out. Extract the first
   * width value.

   w = remove(width, more.w)


   if len(string) <= w then
      result = string
   end else
      in = string
      result = ''
      loop
         i = index(in, ' ', count(in[1,w+1], ' '))
         if i and len(in) > w then
            result := in[1,i-1]
            in = in[i+1, 999999]
         end else
            result := in[1,w]
            in = in[w+1, 999999]
         end
      while len(in)
         result := delim
         if more.w then w = remove(width, more.w) ;* Width for next line
      repeat
   end

   return value result
end
