!function(r){"object"==typeof exports&&"object"==typeof module?r(require("../../lib/codemirror")):"function"==typeof define&&define.amd?define(["../../lib/codemirror"],r):r(CodeMirror)}(function(r){"use strict";function t(r,t,e){for(var n=e.paragraphStart||r.getHelper(t,"paragraphStart"),o=t.line,a=r.firstLine();o>a;--o){var i=r.getLine(o);if(n&&n.test(i))break;if(!/\S/.test(i)){++o;break}}for(var f=e.paragraphEnd||r.getHelper(t,"paragraphEnd"),l=t.line+1,s=r.lastLine();l<=s;++l){var i=r.getLine(l);if(f&&f.test(i)){++l;break}if(!/\S/.test(i))break}return{from:o,to:l}}function e(r,t,e,n){for(var o=t;o>0&&!e.test(r.slice(o-1,o+1));--o);0==o&&(o=t);var a=o;if(n)for(;" "==r.charAt(a-1);)--a;return{from:a,to:o}}function n(t,n,a,i){n=t.clipPos(n),a=t.clipPos(a);var f=i.column||80,l=i.wrapOn||/\s\S|-[^\.\d]/,s=!1!==i.killTrailingSpace,h=[],c="",g=n.line,p=t.getRange(n,a,!1);if(!p.length)return null;for(var u=p[0].match(/^[ \t]*/)[0],m=0;m<p.length;++m){var v=p[m],d=c.length,b=0;c&&v&&!l.test(c.charAt(c.length-1)+v.charAt(0))&&(c+=" ",b=1);var x="";if(m&&(x=v.match(/^\s*/)[0],v=v.slice(x.length)),c+=v,m){var S=c.length>f&&u==x&&e(c,f,l,s);S&&S.from==d&&S.to==d+b?(c=u+v,++g):h.push({text:[b?" ":""],from:o(g,d),to:o(g+1,x.length)})}for(;c.length>f;){var E=e(c,f,l,s);h.push({text:["",u],from:o(g,E.from),to:o(g,E.to)}),c=u+c.slice(E.to),++g}}return h.length&&t.operation(function(){for(var r=0;r<h.length;++r){var e=h[r];t.replaceRange(e.text,e.from,e.to)}}),h.length?{from:h[0].from,to:r.changeEnd(h[h.length-1])}:null}var o=r.Pos;r.defineExtension("wrapParagraph",function(r,e){e=e||{},r||(r=this.getCursor());var a=t(this,r,e);return n(this,o(a.from,0),o(a.to-1),e)}),r.commands.wrapLines=function(r){r.operation(function(){for(var e=r.listSelections(),a=r.lastLine()+1,i=e.length-1;i>=0;i--){var f,l=e[i];if(l.empty()){var s=t(r,l.head,{});f={from:o(s.from,0),to:o(s.to-1)}}else f={from:l.from(),to:l.to()};f.to.line>=a||(a=f.from.line,n(r,f.from,f.to,{}))}})},r.defineExtension("wrapRange",function(r,t,e){return n(this,r,t,e||{})}),r.defineExtension("wrapParagraphsInRange",function(r,e,a){a=a||{};for(var i=this,f=[],l=r.line;l<=e.line;){var s=t(i,o(l,0),a);f.push(s),l=s.to}var h=!1;return f.length&&i.operation(function(){for(var r=f.length-1;r>=0;--r)h=h||n(i,o(f[r].from,0),o(f[r].to-1),a)}),h})});