Writer that keeps track of when it hasn't seen a newline yet, will
auto-insert newlines except when lines start with a carriage return.
11856
@Override
  public void write(char[] cbuf, int off, int len) throws IOException {
    if(len <= 0) {
      return;
    }
    // if we havn't last seen a newline, and don't get a CR, insert a newline.
    if(charsSinceNewline > 0) {
      if(cbuf[off] != CARRIAGE_RETURN) {
        super.write(NEWLINEC, 0, NEWLINEC.length);
        charsSinceNewline = 0;
      }
      else {
        // length of this line:
        int nonnl = countNonNewline(cbuf, off + 1, len - 1);
        // clear the existing chars.
        if(nonnl < charsSinceNewline) {
          super.write(CARRIAGE_RETURN);
          while(charsSinceNewline > 0) {
            final int n = Math.min(charsSinceNewline, WHITESPACE.length());
            super.write(WHITESPACE, 0, n);
            charsSinceNewline -= n;
          }
        }
        else {
          charsSinceNewline = 0;
        }
      }
    }
    charsSinceNewline = tailingNonNewline(cbuf, off, len);
    super.write(cbuf, off, len);
    flush();
  }
169439
public void newLineIfNotEmpty() {
		for (int i = segments.size() - 1; i >= 0; i--) {
			String segment = segments.get(i);
			if (lineDelimiter.equals(segment)) {
				segments.subList(i + 1, segments.size()).clear();
				cachedToString = null;
				return;
			}
			for (int j = 0; j < segment.length(); j++) {
				if (!WhitespaceMatcher.isWhitespace(segment.charAt(j))) {
					newLine();
					return;
				}
			}
		}
		segments.clear();
		cachedToString = null;
	}
1163749
def insert_lines(self, count=None):
        
        count = count or 1
        top, bottom = self.margins or Margins(0, self.lines - 1)

        # If cursor is outside scrolling margins it -- do nothin'.
        if top <= self.cursor.y <= bottom:
            self.dirty.update(range(self.cursor.y, self.lines))
            for y in range(bottom, self.cursor.y - 1, -1):
                if y + count <= bottom and y in self.buffer:
                    self.buffer[y + count] = self.buffer[y]
                self.buffer.pop(y, None)

            self.carriage_return()
571538
@Override
    public StringWriter append(char c) {
		if (c == '\n') {
			if (!ignoreNewLine) {
				++line;
				column = 0;
			}
			ignoreNewLine = false;
		} else if (c == '\r'){
			// \r\n sequence should be treated as a single line feed so set a flag
			// to indicate that if the next character is a /n then we should ignore it
			ignoreNewLine = true;
			++line;
			column = 0;
		} else {
			ignoreNewLine = false;
			++column;
		}
        super.write(c);
        return this;
    }
453585
def newline(self):
        """"""
        self.write_str(self.eol)
        self.room = self.maxlinelen
1243575
def should_indent(code):
    """"""
    last = rem_comment(code.splitlines()[-1])
    return last.endswith(":") or last.endswith("\\") or paren_change(last) < 0
1001058
def line(self, text=''):
        ''''''
        self.out.write(text)
        self.out.write('\n')
1301816
function escapeCarriageReturn(txt) {
  if (!txt) return "";
  if (!/\r/.test(txt)) return txt;
  txt = txt.replace(/\r+\n/gm, "\n"); // \r followed by \n --> newline
  while (/\r[^$]/.test(txt)) {
    var base = /^(.*)\r+/m.exec(txt)[1];
    var insert = /\r+(.*)$/m.exec(txt)[1];
    insert = insert + base.slice(insert.length, base.length);
    txt = txt.replace(/\r+.*$/m, "\r").replace(/^.*\r/m, insert);
  }
  return txt;
}
843116
public static Writer appendLine(Writer writer, String line) {
        return append(writer, line + JMString.LINE_SEPARATOR);
    }
855876
function(editor) {
			if (!this.insertLineBreakOnlyAction(editor)) {
				var curPadding = editorUtils.getCurrentLinePadding(editor);
				var content = String(editor.getContent());
				var caretPos = editor.getCaretPos();
				var len = content.length;
				var nl = '\n';
					
				// check out next line padding
				var lineRange = editor.getCurrentLineRange();
				var nextPadding = '';
					
				for (var i = lineRange.end, ch; i < len; i++) {
					ch = content.charAt(i);
					if (ch == ' ' || ch == '\t')
						nextPadding += ch;
					else
						break;
				}
				
				if (nextPadding.length > curPadding.length) {
					editor.replaceContent(nl + nextPadding, caretPos, caretPos, true);
				} else {
					editor.replaceContent(nl, caretPos);
				}
			}
			
			return true;
		}
738859
def start_of_new_markdown_cell(self, line):
        """"""
        for empty_markdown_cell in ['""', "''"]:
            if line == empty_markdown_cell:
                return empty_markdown_cell

        for triple_quote in ['"""', "'''"]:
            if line.startswith(triple_quote):
                return triple_quote

        if self.twenty_hash.match(line):
            return line

        return None
863364
def _write_line(self, text=''):
        
        if text:
            self._code += str(text) + "\n"
        else:
            self._code += "\n"
1164785
def insert_line_below(self, copy_margin=True):
        
        if copy_margin:
            insert = '\n' + self.document.leading_whitespace_in_current_line
        else:
            insert = '\n'

        self.cursor_position += self.document.get_end_of_line_position()
        self.insert_text(insert)
1224036
@Override
    public boolean isNewLine(byte[]bytes, int p, int end) {
        return p < end ? bytes[p] == Encoding.NEW_LINE : false;
    }
941721
def insert_text_to(cursor, text, fmt):
    """"""
    while True:
        index = text.find(chr(8))  # backspace
        if index == -1:
            break
        cursor.insertText(text[:index], fmt)
        if cursor.positionInBlock() > 0:
            cursor.deletePreviousChar()
        text = text[index+1:]
    cursor.insertText(text, fmt)
90310
def newLine(self) :
		""""""
		l = CSVEntry(self)
		if self.keepInMemory :
			self.lines.append(l)
		return l
469306
def auto_undent(string):
    
    lines = string.splitlines()
    while lines[0].strip() == "":
        lines = lines[1:]
        if not lines:
            return []
    spaces = len(lines[0]) - len(lines[0].lstrip(" "))
    out = []
    for line in lines:
        num_spaces = len(line) - len(line.lstrip(" "))
        out.append(line[min(spaces, num_spaces) :])
    return out