5
if (typeof exports == "object" && typeof module == "object")
6
mod(require("../../lib/codemirror"));
7
else if (typeof define == "function" && define.amd)
8
define(["../../lib/codemirror"], mod);
11
})(function(CodeMirror) {
14
var Pos = CodeMirror.Pos;
16
function findParagraph(cm, pos, options) {
17
var startRE = options.paragraphStart || cm.getHelper(pos, "paragraphStart");
18
for (var start = pos.line, first = cm.firstLine(); start > first; --start) {
19
var line = cm.getLine(start);
20
if (startRE && startRE.test(line)) break;
21
if (!/\S/.test(line)) { ++start; break; }
23
var endRE = options.paragraphEnd || cm.getHelper(pos, "paragraphEnd");
24
for (var end = pos.line + 1, last = cm.lastLine(); end <= last; ++end) {
25
var line = cm.getLine(end);
26
if (endRE && endRE.test(line)) { ++end; break; }
27
if (!/\S/.test(line)) break;
29
return {from: start, to: end};
32
function findBreakPoint(text, column, wrapOn, killTrailingSpace, forceBreak) {
34
while (at < text.length && text.charAt(at) == " ") at++
36
if (wrapOn.test(text.slice(at - 1, at + 1))) break;
38
if (!forceBreak && at <= text.match(/^[ \t]*/)[0].length) {
41
for (at = column + 1; at < text.length - 1; ++at) {
42
if (wrapOn.test(text.slice(at - 1, at + 1))) break;
46
for (var first = true;; first = false) {
48
if (killTrailingSpace)
49
while (text.charAt(endOfText - 1) == " ") --endOfText;
50
if (endOfText == 0 && first) at = column;
51
else return {from: endOfText, to: at};
55
function wrapRange(cm, from, to, options) {
56
from = cm.clipPos(from); to = cm.clipPos(to);
57
var column = options.column || 80;
58
var wrapOn = options.wrapOn || /\s\S|-[^\.\d]/;
59
var forceBreak = options.forceBreak !== false;
60
var killTrailing = options.killTrailingSpace !== false;
61
var changes = [], curLine = "", curNo = from.line;
62
var lines = cm.getRange(from, to, false);
63
if (!lines.length) return null;
64
var leadingSpace = lines[0].match(/^[ \t]*/)[0];
65
if (leadingSpace.length >= column) column = leadingSpace.length + 1
67
for (var i = 0; i < lines.length; ++i) {
68
var text = lines[i], oldLen = curLine.length, spaceInserted = 0;
69
if (curLine && text && !wrapOn.test(curLine.charAt(curLine.length - 1) + text.charAt(0))) {
73
var spaceTrimmed = "";
75
spaceTrimmed = text.match(/^\s*/)[0];
76
text = text.slice(spaceTrimmed.length);
80
var firstBreak = curLine.length > column && leadingSpace == spaceTrimmed &&
81
findBreakPoint(curLine, column, wrapOn, killTrailing, forceBreak);
83
if (!firstBreak || firstBreak.from != oldLen || firstBreak.to != oldLen + spaceInserted) {
84
changes.push({text: [spaceInserted ? " " : ""],
85
from: Pos(curNo, oldLen),
86
to: Pos(curNo + 1, spaceTrimmed.length)});
88
curLine = leadingSpace + text;
92
while (curLine.length > column) {
93
var bp = findBreakPoint(curLine, column, wrapOn, killTrailing, forceBreak);
94
if (bp.from != bp.to ||
95
forceBreak && leadingSpace !== curLine.slice(0, bp.to)) {
96
changes.push({text: ["", leadingSpace],
97
from: Pos(curNo, bp.from),
98
to: Pos(curNo, bp.to)});
99
curLine = leadingSpace + curLine.slice(bp.to);
106
if (changes.length) cm.operation(function() {
107
for (var i = 0; i < changes.length; ++i) {
108
var change = changes[i];
109
if (change.text || CodeMirror.cmpPos(change.from, change.to))
110
cm.replaceRange(change.text, change.from, change.to);
113
return changes.length ? {from: changes[0].from, to: CodeMirror.changeEnd(changes[changes.length - 1])} : null;
116
CodeMirror.defineExtension("wrapParagraph", function(pos, options) {
117
options = options || {};
118
if (!pos) pos = this.getCursor();
119
var para = findParagraph(this, pos, options);
120
return wrapRange(this, Pos(para.from, 0), Pos(para.to - 1), options);
123
CodeMirror.commands.wrapLines = function(cm) {
124
cm.operation(function() {
125
var ranges = cm.listSelections(), at = cm.lastLine() + 1;
126
for (var i = ranges.length - 1; i >= 0; i--) {
127
var range = ranges[i], span;
129
var para = findParagraph(cm, range.head, {});
130
span = {from: Pos(para.from, 0), to: Pos(para.to - 1)};
132
span = {from: range.from(), to: range.to()};
134
if (span.to.line >= at) continue;
136
wrapRange(cm, span.from, span.to, {});
141
CodeMirror.defineExtension("wrapRange", function(from, to, options) {
142
return wrapRange(this, from, to, options || {});
145
CodeMirror.defineExtension("wrapParagraphsInRange", function(from, to, options) {
146
options = options || {};
147
var cm = this, paras = [];
148
for (var line = from.line; line <= to.line;) {
149
var para = findParagraph(cm, Pos(line, 0), options);
153
var madeChange = false;
154
if (paras.length) cm.operation(function() {
155
for (var i = paras.length - 1; i >= 0; --i)
156
madeChange = madeChange || wrapRange(cm, Pos(paras[i].from, 0), Pos(paras[i].to - 1), options);