jpayne@69: """Format all or a selected region (line slice) of text. jpayne@69: jpayne@69: Region formatting options: paragraph, comment block, indent, deindent, jpayne@69: comment, uncomment, tabify, and untabify. jpayne@69: jpayne@69: File renamed from paragraph.py with functions added from editor.py. jpayne@69: """ jpayne@69: import re jpayne@69: from tkinter.messagebox import askyesno jpayne@69: from tkinter.simpledialog import askinteger jpayne@69: from idlelib.config import idleConf jpayne@69: jpayne@69: jpayne@69: class FormatParagraph: jpayne@69: """Format a paragraph, comment block, or selection to a max width. jpayne@69: jpayne@69: Does basic, standard text formatting, and also understands Python jpayne@69: comment blocks. Thus, for editing Python source code, this jpayne@69: extension is really only suitable for reformatting these comment jpayne@69: blocks or triple-quoted strings. jpayne@69: jpayne@69: Known problems with comment reformatting: jpayne@69: * If there is a selection marked, and the first line of the jpayne@69: selection is not complete, the block will probably not be detected jpayne@69: as comments, and will have the normal "text formatting" rules jpayne@69: applied. jpayne@69: * If a comment block has leading whitespace that mixes tabs and jpayne@69: spaces, they will not be considered part of the same block. jpayne@69: * Fancy comments, like this bulleted list, aren't handled :-) jpayne@69: """ jpayne@69: def __init__(self, editwin): jpayne@69: self.editwin = editwin jpayne@69: jpayne@69: @classmethod jpayne@69: def reload(cls): jpayne@69: cls.max_width = idleConf.GetOption('extensions', 'FormatParagraph', jpayne@69: 'max-width', type='int', default=72) jpayne@69: jpayne@69: def close(self): jpayne@69: self.editwin = None jpayne@69: jpayne@69: def format_paragraph_event(self, event, limit=None): jpayne@69: """Formats paragraph to a max width specified in idleConf. jpayne@69: jpayne@69: If text is selected, format_paragraph_event will start breaking lines jpayne@69: at the max width, starting from the beginning selection. jpayne@69: jpayne@69: If no text is selected, format_paragraph_event uses the current jpayne@69: cursor location to determine the paragraph (lines of text surrounded jpayne@69: by blank lines) and formats it. jpayne@69: jpayne@69: The length limit parameter is for testing with a known value. jpayne@69: """ jpayne@69: limit = self.max_width if limit is None else limit jpayne@69: text = self.editwin.text jpayne@69: first, last = self.editwin.get_selection_indices() jpayne@69: if first and last: jpayne@69: data = text.get(first, last) jpayne@69: comment_header = get_comment_header(data) jpayne@69: else: jpayne@69: first, last, comment_header, data = \ jpayne@69: find_paragraph(text, text.index("insert")) jpayne@69: if comment_header: jpayne@69: newdata = reformat_comment(data, limit, comment_header) jpayne@69: else: jpayne@69: newdata = reformat_paragraph(data, limit) jpayne@69: text.tag_remove("sel", "1.0", "end") jpayne@69: jpayne@69: if newdata != data: jpayne@69: text.mark_set("insert", first) jpayne@69: text.undo_block_start() jpayne@69: text.delete(first, last) jpayne@69: text.insert(first, newdata) jpayne@69: text.undo_block_stop() jpayne@69: else: jpayne@69: text.mark_set("insert", last) jpayne@69: text.see("insert") jpayne@69: return "break" jpayne@69: jpayne@69: jpayne@69: FormatParagraph.reload() jpayne@69: jpayne@69: def find_paragraph(text, mark): jpayne@69: """Returns the start/stop indices enclosing the paragraph that mark is in. jpayne@69: jpayne@69: Also returns the comment format string, if any, and paragraph of text jpayne@69: between the start/stop indices. jpayne@69: """ jpayne@69: lineno, col = map(int, mark.split(".")) jpayne@69: line = text.get("%d.0" % lineno, "%d.end" % lineno) jpayne@69: jpayne@69: # Look for start of next paragraph if the index passed in is a blank line jpayne@69: while text.compare("%d.0" % lineno, "<", "end") and is_all_white(line): jpayne@69: lineno = lineno + 1 jpayne@69: line = text.get("%d.0" % lineno, "%d.end" % lineno) jpayne@69: first_lineno = lineno jpayne@69: comment_header = get_comment_header(line) jpayne@69: comment_header_len = len(comment_header) jpayne@69: jpayne@69: # Once start line found, search for end of paragraph (a blank line) jpayne@69: while get_comment_header(line)==comment_header and \ jpayne@69: not is_all_white(line[comment_header_len:]): jpayne@69: lineno = lineno + 1 jpayne@69: line = text.get("%d.0" % lineno, "%d.end" % lineno) jpayne@69: last = "%d.0" % lineno jpayne@69: jpayne@69: # Search back to beginning of paragraph (first blank line before) jpayne@69: lineno = first_lineno - 1 jpayne@69: line = text.get("%d.0" % lineno, "%d.end" % lineno) jpayne@69: while lineno > 0 and \ jpayne@69: get_comment_header(line)==comment_header and \ jpayne@69: not is_all_white(line[comment_header_len:]): jpayne@69: lineno = lineno - 1 jpayne@69: line = text.get("%d.0" % lineno, "%d.end" % lineno) jpayne@69: first = "%d.0" % (lineno+1) jpayne@69: jpayne@69: return first, last, comment_header, text.get(first, last) jpayne@69: jpayne@69: # This should perhaps be replaced with textwrap.wrap jpayne@69: def reformat_paragraph(data, limit): jpayne@69: """Return data reformatted to specified width (limit).""" jpayne@69: lines = data.split("\n") jpayne@69: i = 0 jpayne@69: n = len(lines) jpayne@69: while i < n and is_all_white(lines[i]): jpayne@69: i = i+1 jpayne@69: if i >= n: jpayne@69: return data jpayne@69: indent1 = get_indent(lines[i]) jpayne@69: if i+1 < n and not is_all_white(lines[i+1]): jpayne@69: indent2 = get_indent(lines[i+1]) jpayne@69: else: jpayne@69: indent2 = indent1 jpayne@69: new = lines[:i] jpayne@69: partial = indent1 jpayne@69: while i < n and not is_all_white(lines[i]): jpayne@69: # XXX Should take double space after period (etc.) into account jpayne@69: words = re.split(r"(\s+)", lines[i]) jpayne@69: for j in range(0, len(words), 2): jpayne@69: word = words[j] jpayne@69: if not word: jpayne@69: continue # Can happen when line ends in whitespace jpayne@69: if len((partial + word).expandtabs()) > limit and \ jpayne@69: partial != indent1: jpayne@69: new.append(partial.rstrip()) jpayne@69: partial = indent2 jpayne@69: partial = partial + word + " " jpayne@69: if j+1 < len(words) and words[j+1] != " ": jpayne@69: partial = partial + " " jpayne@69: i = i+1 jpayne@69: new.append(partial.rstrip()) jpayne@69: # XXX Should reformat remaining paragraphs as well jpayne@69: new.extend(lines[i:]) jpayne@69: return "\n".join(new) jpayne@69: jpayne@69: def reformat_comment(data, limit, comment_header): jpayne@69: """Return data reformatted to specified width with comment header.""" jpayne@69: jpayne@69: # Remove header from the comment lines jpayne@69: lc = len(comment_header) jpayne@69: data = "\n".join(line[lc:] for line in data.split("\n")) jpayne@69: # Reformat to maxformatwidth chars or a 20 char width, jpayne@69: # whichever is greater. jpayne@69: format_width = max(limit - len(comment_header), 20) jpayne@69: newdata = reformat_paragraph(data, format_width) jpayne@69: # re-split and re-insert the comment header. jpayne@69: newdata = newdata.split("\n") jpayne@69: # If the block ends in a \n, we don't want the comment prefix jpayne@69: # inserted after it. (Im not sure it makes sense to reformat a jpayne@69: # comment block that is not made of complete lines, but whatever!) jpayne@69: # Can't think of a clean solution, so we hack away jpayne@69: block_suffix = "" jpayne@69: if not newdata[-1]: jpayne@69: block_suffix = "\n" jpayne@69: newdata = newdata[:-1] jpayne@69: return '\n'.join(comment_header+line for line in newdata) + block_suffix jpayne@69: jpayne@69: def is_all_white(line): jpayne@69: """Return True if line is empty or all whitespace.""" jpayne@69: jpayne@69: return re.match(r"^\s*$", line) is not None jpayne@69: jpayne@69: def get_indent(line): jpayne@69: """Return the initial space or tab indent of line.""" jpayne@69: return re.match(r"^([ \t]*)", line).group() jpayne@69: jpayne@69: def get_comment_header(line): jpayne@69: """Return string with leading whitespace and '#' from line or ''. jpayne@69: jpayne@69: A null return indicates that the line is not a comment line. A non- jpayne@69: null return, such as ' #', will be used to find the other lines of jpayne@69: a comment block with the same indent. jpayne@69: """ jpayne@69: m = re.match(r"^([ \t]*#*)", line) jpayne@69: if m is None: return "" jpayne@69: return m.group(1) jpayne@69: jpayne@69: jpayne@69: # Copied from editor.py; importing it would cause an import cycle. jpayne@69: _line_indent_re = re.compile(r'[ \t]*') jpayne@69: jpayne@69: def get_line_indent(line, tabwidth): jpayne@69: """Return a line's indentation as (# chars, effective # of spaces). jpayne@69: jpayne@69: The effective # of spaces is the length after properly "expanding" jpayne@69: the tabs into spaces, as done by str.expandtabs(tabwidth). jpayne@69: """ jpayne@69: m = _line_indent_re.match(line) jpayne@69: return m.end(), len(m.group().expandtabs(tabwidth)) jpayne@69: jpayne@69: jpayne@69: class FormatRegion: jpayne@69: "Format selected text (region)." jpayne@69: jpayne@69: def __init__(self, editwin): jpayne@69: self.editwin = editwin jpayne@69: jpayne@69: def get_region(self): jpayne@69: """Return line information about the selected text region. jpayne@69: jpayne@69: If text is selected, the first and last indices will be jpayne@69: for the selection. If there is no text selected, the jpayne@69: indices will be the current cursor location. jpayne@69: jpayne@69: Return a tuple containing (first index, last index, jpayne@69: string representation of text, list of text lines). jpayne@69: """ jpayne@69: text = self.editwin.text jpayne@69: first, last = self.editwin.get_selection_indices() jpayne@69: if first and last: jpayne@69: head = text.index(first + " linestart") jpayne@69: tail = text.index(last + "-1c lineend +1c") jpayne@69: else: jpayne@69: head = text.index("insert linestart") jpayne@69: tail = text.index("insert lineend +1c") jpayne@69: chars = text.get(head, tail) jpayne@69: lines = chars.split("\n") jpayne@69: return head, tail, chars, lines jpayne@69: jpayne@69: def set_region(self, head, tail, chars, lines): jpayne@69: """Replace the text between the given indices. jpayne@69: jpayne@69: Args: jpayne@69: head: Starting index of text to replace. jpayne@69: tail: Ending index of text to replace. jpayne@69: chars: Expected to be string of current text jpayne@69: between head and tail. jpayne@69: lines: List of new lines to insert between head jpayne@69: and tail. jpayne@69: """ jpayne@69: text = self.editwin.text jpayne@69: newchars = "\n".join(lines) jpayne@69: if newchars == chars: jpayne@69: text.bell() jpayne@69: return jpayne@69: text.tag_remove("sel", "1.0", "end") jpayne@69: text.mark_set("insert", head) jpayne@69: text.undo_block_start() jpayne@69: text.delete(head, tail) jpayne@69: text.insert(head, newchars) jpayne@69: text.undo_block_stop() jpayne@69: text.tag_add("sel", head, "insert") jpayne@69: jpayne@69: def indent_region_event(self, event=None): jpayne@69: "Indent region by indentwidth spaces." jpayne@69: head, tail, chars, lines = self.get_region() jpayne@69: for pos in range(len(lines)): jpayne@69: line = lines[pos] jpayne@69: if line: jpayne@69: raw, effective = get_line_indent(line, self.editwin.tabwidth) jpayne@69: effective = effective + self.editwin.indentwidth jpayne@69: lines[pos] = self.editwin._make_blanks(effective) + line[raw:] jpayne@69: self.set_region(head, tail, chars, lines) jpayne@69: return "break" jpayne@69: jpayne@69: def dedent_region_event(self, event=None): jpayne@69: "Dedent region by indentwidth spaces." jpayne@69: head, tail, chars, lines = self.get_region() jpayne@69: for pos in range(len(lines)): jpayne@69: line = lines[pos] jpayne@69: if line: jpayne@69: raw, effective = get_line_indent(line, self.editwin.tabwidth) jpayne@69: effective = max(effective - self.editwin.indentwidth, 0) jpayne@69: lines[pos] = self.editwin._make_blanks(effective) + line[raw:] jpayne@69: self.set_region(head, tail, chars, lines) jpayne@69: return "break" jpayne@69: jpayne@69: def comment_region_event(self, event=None): jpayne@69: """Comment out each line in region. jpayne@69: jpayne@69: ## is appended to the beginning of each line to comment it out. jpayne@69: """ jpayne@69: head, tail, chars, lines = self.get_region() jpayne@69: for pos in range(len(lines) - 1): jpayne@69: line = lines[pos] jpayne@69: lines[pos] = '##' + line jpayne@69: self.set_region(head, tail, chars, lines) jpayne@69: return "break" jpayne@69: jpayne@69: def uncomment_region_event(self, event=None): jpayne@69: """Uncomment each line in region. jpayne@69: jpayne@69: Remove ## or # in the first positions of a line. If the comment jpayne@69: is not in the beginning position, this command will have no effect. jpayne@69: """ jpayne@69: head, tail, chars, lines = self.get_region() jpayne@69: for pos in range(len(lines)): jpayne@69: line = lines[pos] jpayne@69: if not line: jpayne@69: continue jpayne@69: if line[:2] == '##': jpayne@69: line = line[2:] jpayne@69: elif line[:1] == '#': jpayne@69: line = line[1:] jpayne@69: lines[pos] = line jpayne@69: self.set_region(head, tail, chars, lines) jpayne@69: return "break" jpayne@69: jpayne@69: def tabify_region_event(self, event=None): jpayne@69: "Convert leading spaces to tabs for each line in selected region." jpayne@69: head, tail, chars, lines = self.get_region() jpayne@69: tabwidth = self._asktabwidth() jpayne@69: if tabwidth is None: jpayne@69: return jpayne@69: for pos in range(len(lines)): jpayne@69: line = lines[pos] jpayne@69: if line: jpayne@69: raw, effective = get_line_indent(line, tabwidth) jpayne@69: ntabs, nspaces = divmod(effective, tabwidth) jpayne@69: lines[pos] = '\t' * ntabs + ' ' * nspaces + line[raw:] jpayne@69: self.set_region(head, tail, chars, lines) jpayne@69: return "break" jpayne@69: jpayne@69: def untabify_region_event(self, event=None): jpayne@69: "Expand tabs to spaces for each line in region." jpayne@69: head, tail, chars, lines = self.get_region() jpayne@69: tabwidth = self._asktabwidth() jpayne@69: if tabwidth is None: jpayne@69: return jpayne@69: for pos in range(len(lines)): jpayne@69: lines[pos] = lines[pos].expandtabs(tabwidth) jpayne@69: self.set_region(head, tail, chars, lines) jpayne@69: return "break" jpayne@69: jpayne@69: def _asktabwidth(self): jpayne@69: "Return value for tab width." jpayne@69: return askinteger( jpayne@69: "Tab width", jpayne@69: "Columns per tab? (2-16)", jpayne@69: parent=self.editwin.text, jpayne@69: initialvalue=self.editwin.indentwidth, jpayne@69: minvalue=2, jpayne@69: maxvalue=16) jpayne@69: jpayne@69: jpayne@69: class Indents: jpayne@69: "Change future indents." jpayne@69: jpayne@69: def __init__(self, editwin): jpayne@69: self.editwin = editwin jpayne@69: jpayne@69: def toggle_tabs_event(self, event): jpayne@69: editwin = self.editwin jpayne@69: usetabs = editwin.usetabs jpayne@69: if askyesno( jpayne@69: "Toggle tabs", jpayne@69: "Turn tabs " + ("on", "off")[usetabs] + jpayne@69: "?\nIndent width " + jpayne@69: ("will be", "remains at")[usetabs] + " 8." + jpayne@69: "\n Note: a tab is always 8 columns", jpayne@69: parent=editwin.text): jpayne@69: editwin.usetabs = not usetabs jpayne@69: # Try to prevent inconsistent indentation. jpayne@69: # User must change indent width manually after using tabs. jpayne@69: editwin.indentwidth = 8 jpayne@69: return "break" jpayne@69: jpayne@69: def change_indentwidth_event(self, event): jpayne@69: editwin = self.editwin jpayne@69: new = askinteger( jpayne@69: "Indent width", jpayne@69: "New indent width (2-16)\n(Always use 8 when using tabs)", jpayne@69: parent=editwin.text, jpayne@69: initialvalue=editwin.indentwidth, jpayne@69: minvalue=2, jpayne@69: maxvalue=16) jpayne@69: if new and new != editwin.indentwidth and not editwin.usetabs: jpayne@69: editwin.indentwidth = new jpayne@69: return "break" jpayne@69: jpayne@69: jpayne@69: class Rstrip: # 'Strip Trailing Whitespace" on "Format" menu. jpayne@69: def __init__(self, editwin): jpayne@69: self.editwin = editwin jpayne@69: jpayne@69: def do_rstrip(self, event=None): jpayne@69: text = self.editwin.text jpayne@69: undo = self.editwin.undo jpayne@69: undo.undo_block_start() jpayne@69: jpayne@69: end_line = int(float(text.index('end'))) jpayne@69: for cur in range(1, end_line): jpayne@69: txt = text.get('%i.0' % cur, '%i.end' % cur) jpayne@69: raw = len(txt) jpayne@69: cut = len(txt.rstrip()) jpayne@69: # Since text.delete() marks file as changed, even if not, jpayne@69: # only call it when needed to actually delete something. jpayne@69: if cut < raw: jpayne@69: text.delete('%i.%i' % (cur, cut), '%i.end' % cur) jpayne@69: jpayne@69: if (text.get('end-2c') == '\n' # File ends with at least 1 newline; jpayne@69: and not hasattr(self.editwin, 'interp')): # & is not Shell. jpayne@69: # Delete extra user endlines. jpayne@69: while (text.index('end-1c') > '1.0' # Stop if file empty. jpayne@69: and text.get('end-3c') == '\n'): jpayne@69: text.delete('end-3c') jpayne@69: # Because tk indexes are slice indexes and never raise, jpayne@69: # a file with only newlines will be emptied. jpayne@69: # patchcheck.py does the same. jpayne@69: jpayne@69: undo.undo_block_stop() jpayne@69: jpayne@69: jpayne@69: if __name__ == "__main__": jpayne@69: from unittest import main jpayne@69: main('idlelib.idle_test.test_format', verbosity=2, exit=False)