!function(t){"object"==typeof exports&&"object"==typeof module?t(require("../../lib/codemirror"),require("./foldcode")):"function"==typeof define&&define.amd?define(["../../lib/codemirror","./foldcode"],t):t(CodeMirror)}((function(t){"use strict";t.defineOption("foldGutter",!1,(function(o,r,n){var i;n&&n!=t.Init&&(o.clearGutter(o.state.foldGutter.options.gutter),o.state.foldGutter=null,o.off("gutterClick",d),o.off("changes",u),o.off("viewportChange",l),o.off("fold",c),o.off("unfold",c),o.off("swapDoc",u)),r&&(o.state.foldGutter=new e((!0===(i=r)&&(i={}),null==i.gutter&&(i.gutter="CodeMirror-foldgutter"),null==i.indicatorOpen&&(i.indicatorOpen="CodeMirror-foldgutter-open"),null==i.indicatorFolded&&(i.indicatorFolded="CodeMirror-foldgutter-folded"),i)),a(o),o.on("gutterClick",d),o.on("changes",u),o.on("viewportChange",l),o.on("fold",c),o.on("unfold",c),o.on("swapDoc",u))}));var o=t.Pos;function e(t){this.options=t,this.from=this.to=0}function r(t,e){for(var r=t.findMarks(o(e,0),o(e+1,0)),n=0;n<r.length;++n)if(r[n].__isFold){var i=r[n].find(-1);if(i&&i.line===e)return r[n]}}function n(t){if("string"==typeof t){var o=document.createElement("div");return o.className=t+" CodeMirror-guttermarker-subtle",o}return t.cloneNode(!0)}function i(t,e,i){var a=t.state.foldGutter.options,d=e-1,u=t.foldOption(a,"minFoldSize"),l=t.foldOption(a,"rangeFinder"),c="string"==typeof a.indicatorFolded&&f(a.indicatorFolded),s="string"==typeof a.indicatorOpen&&f(a.indicatorOpen);t.eachLine(e,i,(function(e){++d;var i=null,f=e.gutterMarkers;if(f&&(f=f[a.gutter]),r(t,d)){if(c&&f&&c.test(f.className))return;i=n(a.indicatorFolded)}else{var p=o(d,0),m=l&&l(t,p);if(m&&m.to.line-m.from.line>=u){if(s&&f&&s.test(f.className))return;i=n(a.indicatorOpen)}}(i||f)&&t.setGutterMarker(e,a.gutter,i)}))}function f(t){return new RegExp("(^|\\s)"+t+"(?:$|\\s)\\s*")}function a(t){var o=t.getViewport(),e=t.state.foldGutter;e&&(t.operation((function(){i(t,o.from,o.to)})),e.from=o.from,e.to=o.to)}function d(t,e,n){var i=t.state.foldGutter;if(i){var f=i.options;if(n==f.gutter){var a=r(t,e);a?a.clear():t.foldCode(o(e,0),f)}}}function u(t){var o=t.state.foldGutter;if(o){var e=o.options;o.from=o.to=0,clearTimeout(o.changeUpdate),o.changeUpdate=setTimeout((function(){a(t)}),e.foldOnChangeTimeSpan||600)}}function l(t){var o=t.state.foldGutter;if(o){var e=o.options;clearTimeout(o.changeUpdate),o.changeUpdate=setTimeout((function(){var e=t.getViewport();o.from==o.to||e.from-o.to>20||o.from-e.to>20?a(t):t.operation((function(){e.from<o.from&&(i(t,e.from,o.from),o.from=e.from),e.to>o.to&&(i(t,o.to,e.to),o.to=e.to)}))}),e.updateViewportTimeSpan||400)}}function c(t,o){var e=t.state.foldGutter;if(e){var r=o.line;r>=e.from&&r<e.to&&i(t,r,r+1)}}}));