/***************************************************************************
 *   copyright       : (C) 2003-2014 by Pascal Brachet                     *
 *   http://www.xm1math.net/texmaker/                                      *
 *                                                                         *
 *   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 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
 
function wordwrap( str, width, brk, cut ) {
 
    brk = brk || '\n';
    width = width || 75;
    cut = cut || false;
 
    if (!str) { return str; }
 
    var regex = '.{1,' +width+ '}(\\s|$)' + (cut ? '|.{' +width+ '}|.+$' : '|\\S+?(\\s|$)');
 
    return str.match( RegExp(regex, 'g') ).join( brk );
 
}

var textwidth=80;

var txt = TM.selection;
if (txt != "") {
  var pos = TM.selectionStart;
  var wraptext="";
  var lines = txt.split('\n');

  for (i = 0; i < lines.length; ++i) {
  var line = lines[i];
  wraptext+=wordwrap(line, textwidth, '\n')+"\n";
  }
  TM.insertText(wraptext);
  TM.selectRange(pos, wraptext.length);
}

undefined;



