2 if (typeof exports
== "object" && typeof module
== "object") // CommonJS
3 mod(require("../../lib/codemirror"));
4 else if (typeof define
== "function" && define
.amd
) // AMD
5 define(["../../lib/codemirror"], mod
);
6 else // Plain browser env
8 })(function(CodeMirror
) {
11 var Pos
= CodeMirror
.Pos
;
13 function findParagraph(cm
, pos
, options
) {
14 var startRE
= options
.paragraphStart
|| cm
.getHelper(pos
, "paragraphStart");
15 for (var start
= pos
.line
, first
= cm
.firstLine(); start
> first
; --start
) {
16 var line
= cm
.getLine(start
);
17 if (startRE
&& startRE
.test(line
)) break;
18 if (!/\S/.test(line
)) { ++start
; break; }
20 var endRE
= options
.paragraphEnd
|| cm
.getHelper(pos
, "paragraphEnd");
21 for (var end
= pos
.line
+ 1, last
= cm
.lastLine(); end
<= last
; ++end
) {
22 var line
= cm
.getLine(end
);
23 if (endRE
&& endRE
.test(line
)) { ++end
; break; }
24 if (!/\S/.test(line
)) break;
26 return {from: start
, to
: end
};
29 function findBreakPoint(text
, column
, wrapOn
, killTrailingSpace
) {
30 for (var at
= column
; at
> 0; --at
)
31 if (wrapOn
.test(text
.slice(at
- 1, at
+ 1))) break;
32 if (at
== 0) at
= column
;
34 if (killTrailingSpace
)
35 while (text
.charAt(endOfText
- 1) == " ") --endOfText
;
36 return {from: endOfText
, to
: at
};
39 function wrapRange(cm
, from, to
, options
) {
40 from = cm
.clipPos(from); to
= cm
.clipPos(to
);
41 var column
= options
.column
|| 80;
42 var wrapOn
= options
.wrapOn
|| /\s\S|-[^\.\d]/;
43 var killTrailing
= options
.killTrailingSpace
!== false;
44 var changes
= [], curLine
= "", curNo
= from.line
;
45 var lines
= cm
.getRange(from, to
, false);
46 if (!lines
.length
) return null;
47 var leadingSpace
= lines
[0].match(/^[ \t]*/)[0];
49 for (var i
= 0; i
< lines
.length
; ++i
) {
50 var text
= lines
[i
], oldLen
= curLine
.length
, spaceInserted
= 0;
51 if (curLine
&& text
&& !wrapOn
.test(curLine
.charAt(curLine
.length
- 1) + text
.charAt(0))) {
55 var spaceTrimmed
= "";
57 spaceTrimmed
= text
.match(/^\s*/)[0];
58 text
= text
.slice(spaceTrimmed
.length
);
62 var firstBreak
= curLine
.length
> column
&& leadingSpace
== spaceTrimmed
&&
63 findBreakPoint(curLine
, column
, wrapOn
, killTrailing
);
64 // If this isn't broken, or is broken at a different point, remove old break
65 if (!firstBreak
|| firstBreak
.from != oldLen
|| firstBreak
.to
!= oldLen
+ spaceInserted
) {
66 changes
.push({text
: [spaceInserted
? " " : ""],
67 from: Pos(curNo
, oldLen
),
68 to
: Pos(curNo
+ 1, spaceTrimmed
.length
)});
70 curLine
= leadingSpace
+ text
;
74 while (curLine
.length
> column
) {
75 var bp
= findBreakPoint(curLine
, column
, wrapOn
, killTrailing
);
76 changes
.push({text
: ["", leadingSpace
],
77 from: Pos(curNo
, bp
.from),
78 to
: Pos(curNo
, bp
.to
)});
79 curLine
= leadingSpace
+ curLine
.slice(bp
.to
);
83 if (changes
.length
) cm
.operation(function() {
84 for (var i
= 0; i
< changes
.length
; ++i
) {
85 var change
= changes
[i
];
86 cm
.replaceRange(change
.text
, change
.from, change
.to
);
89 return changes
.length
? {from: changes
[0].from, to
: CodeMirror
.changeEnd(changes
[changes
.length
- 1])} : null;
92 CodeMirror
.defineExtension("wrapParagraph", function(pos
, options
) {
93 options
= options
|| {};
94 if (!pos
) pos
= this.getCursor();
95 var para
= findParagraph(this, pos
, options
);
96 return wrapRange(this, Pos(para
.from, 0), Pos(para
.to
- 1), options
);
99 CodeMirror
.commands
.wrapLines = function(cm
) {
100 cm
.operation(function() {
101 var ranges
= cm
.listSelections(), at
= cm
.lastLine() + 1;
102 for (var i
= ranges
.length
- 1; i
>= 0; i
--) {
103 var range
= ranges
[i
], span
;
105 var para
= findParagraph(cm
, range
.head
, {});
106 span
= {from: Pos(para
.from, 0), to
: Pos(para
.to
- 1)};
108 span
= {from: range
.from(), to
: range
.to()};
110 if (span
.to
.line
>= at
) continue;
112 wrapRange(cm
, span
.from, span
.to
, {});
117 CodeMirror
.defineExtension("wrapRange", function(from, to
, options
) {
118 return wrapRange(this, from, to
, options
|| {});
121 CodeMirror
.defineExtension("wrapParagraphsInRange", function(from, to
, options
) {
122 options
= options
|| {};
123 var cm
= this, paras
= [];
124 for (var line
= from.line
; line
<= to
.line
;) {
125 var para
= findParagraph(cm
, Pos(line
, 0), options
);
129 var madeChange
= false;
130 if (paras
.length
) cm
.operation(function() {
131 for (var i
= paras
.length
- 1; i
>= 0; --i
)
132 madeChange
= madeChange
|| wrapRange(cm
, Pos(paras
[i
].from, 0), Pos(paras
[i
].to
- 1), options
);