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