def indentBody(self, event=None): ''' The indent-region command indents each line of the selected body text, or each line of a node if there is no selected text. The @tabwidth directive in effect determines amount of indentation. (not yet) A numeric argument specifies the column to indent to. ''' c, undoType = self, 'Indent Region' w = c.frame.body.wrapper sel_1, sel_2 = w.getSelectionRange() ins = w.getInsertPoint() tab_width = c.getTabWidth(c.p) head, lines, tail, oldSel, oldYview = self.getBodyLines() changed, result = False, [] for line in lines: i, width = g.skip_leading_ws_with_indent(line, 0, tab_width) s = g.computeLeadingWhitespace(width + abs(tab_width), tab_width) + line[i:] if s != line: changed = True result.append(s) if changed: # Leo 5.6: preserve insert point. preserveSel = sel_1 == sel_2 if preserveSel: ins += tab_width oldSel = ins, ins result = ''.join(result) c.updateBodyPane(head, result, tail, undoType, oldSel, oldYview, preserveSel)
def regularize_whitespace(self, lines): """ Regularize leading whitespace in s: Convert tabs to blanks or vice versa depending on the @tabwidth in effect. """ kind = 'tabs' if self.tab_width > 0 else 'blanks' kind2 = 'blanks' if self.tab_width > 0 else 'tabs' fn = g.shortFileName(self.root.h) count, result, tab_width = 0, [], self.tab_width self.ws_error = False # 2016/11/23 if tab_width < 0: # Convert tabs to blanks. for n, line in enumerate(lines): i, w = g.skip_leading_ws_with_indent(line, 0, tab_width) # Use negative width. s = g.computeLeadingWhitespace(w, -abs(tab_width)) + line[i:] if s != line: count += 1 result.append(s) elif tab_width > 0: # Convert blanks to tabs. for n, line in enumerate(lines): # Use positive width. s = g.optimizeLeadingWhitespace(line, abs(tab_width)) if s != line: count += 1 result.append(s) if count: self.ws_error = True # A flag to check. if not g.unitTesting: # g.es_print('Warning: Intermixed tabs and blanks in', fn) # g.es_print('Perfect import test will ignoring leading whitespace.') g.es('changed leading %s to %s in %s line%s in %s' % ( kind2, kind, count, g.plural(count), fn)) if g.unitTesting: # Sets flag for unit tests. self.report('changed %s lines' % count) return result
def convertTabs(self, event=None): '''Convert all tabs to blanks in the selected node.''' c = self changed = False dirtyVnodeList = [] head, lines, tail, oldSel, oldYview = self.getBodyLines( expandSelection=True) # Use the relative @tabwidth, not the global one. theDict = c.scanAllDirectives() tabWidth = theDict.get("tabwidth") if tabWidth: result = [] for line in lines: i, w = g.skip_leading_ws_with_indent(line, 0, tabWidth) s = g.computeLeadingWhitespace( w, -abs(tabWidth)) + line[i:] # use negative width. if s != line: changed = True result.append(s) if changed: undoType = 'Convert Tabs' result = ''.join(result) oldSel = None dirtyVnodeList = c.updateBodyPane(head, result, tail, undoType, oldSel, oldYview) # Handles undo return changed, dirtyVnodeList
def regularizeWhitespace(self, s): '''Regularize leading whitespace in s: Convert tabs to blanks or vice versa depending on the @tabwidth in effect. This is only called for strict languages.''' changed = False lines = g.splitLines(s) result = [] tab_width = self.tab_width if tab_width < 0: # Convert tabs to blanks. for line in lines: i, w = g.skip_leading_ws_with_indent(line, 0, tab_width) s = g.computeLeadingWhitespace( w, -abs(tab_width)) + line[i:] # Use negative width. if s != line: changed = True result.append(s) elif tab_width > 0: # Convert blanks to tabs. for line in lines: s = g.optimizeLeadingWhitespace( line, abs(tab_width)) # Use positive width. if s != line: changed = True result.append(s) if changed: action = 'tabs converted to blanks' if self.tab_width < 0 else 'blanks converted to tabs' message = 'inconsistent leading whitespace. %s' % action self.report(message) return ''.join(result)
def indentBody(self, event=None): ''' The indent-region command indents each line of the selected body text, or each line of a node if there is no selected text. The @tabwidth directive in effect determines amount of indentation. (not yet) A numeric argument specifies the column to indent to. ''' c, undoType = self, 'Indent Region' w = c.frame.body.wrapper sel_1, sel_2 = w.getSelectionRange() ins = w.getInsertPoint() tab_width = c.getTabWidth(c.p) head, lines, tail, oldSel, oldYview = self.getBodyLines() changed, result = False, [] for line in lines: i, width = g.skip_leading_ws_with_indent(line, 0, tab_width) s = g.computeLeadingWhitespace(width + abs(tab_width), tab_width) + line[i:] if s != line: changed = True result.append(s) if changed: # Leo 5.6: preserve insert point. preserveSel = sel_1 == sel_2 if preserveSel: ins += tab_width oldSel = ins, ins result = ''.join(result) c.updateBodyPane(head, result, tail, undoType, oldSel, oldYview, preserveSel)
def alwaysIndentBody(self, event=None): """ The always-indent-region command indents each line of the selected body text. The @tabwidth directive in effect determines amount of indentation. """ c, p, u, w = self, self.p, self.undoer, self.frame.body.wrapper # # #1801: Don't rely on bindings to ensure that we are editing the body. event_w = event and event.w if event_w != w: c.insertCharFromEvent(event) return # # "Before" snapshot. bunch = u.beforeChangeBody(p) # # Initial data. sel_1, sel_2 = w.getSelectionRange() tab_width = c.getTabWidth(p) head, lines, tail, oldSel, oldYview = self.getBodyLines() # # Calculate the result. changed, result = False, [] for line in lines: if line.strip(): i, width = g.skip_leading_ws_with_indent(line, 0, tab_width) s = g.computeLeadingWhitespace(width + abs(tab_width), tab_width) + line[i:] result.append(s) if s != line: changed = True else: result.append('\n') # #2418 if not changed: return # # Set p.b and w's text first. middle = ''.join(result) all = head + middle + tail p.b = all # Sets dirty and changed bits. w.setAllText(all) # # Calculate the proper selection range (i, j, ins). if sel_1 == sel_2: line = result[0] i, width = g.skip_leading_ws_with_indent(line, 0, tab_width) i = j = len(head) + i else: i = len(head) j = len(head) + len(middle) if middle.endswith('\n'): # #1742. j -= 1 # # Set the selection range and scroll position. w.setSelectionRange(i, j, insert=j) w.setYScrollPosition(oldYview) # # "after" snapshot. u.afterChangeBody(p, 'Indent Region', bunch)
def convertAllBlanks(self, event=None): '''Convert all blanks to tabs in the selected outline.''' c = self u = c.undoer undoType = 'Convert All Blanks' current = c.p if g.app.batchMode: c.notValidInBatchMode(undoType) return d = c.scanAllDirectives() tabWidth = d.get("tabwidth") count = 0 dirtyVnodeList = [] u.beforeChangeGroup(current, undoType) for p in current.self_and_subtree(): # g.trace(p.h,tabWidth) innerUndoData = u.beforeChangeNodeContents(p) if p == current: changed, dirtyVnodeList2 = c.convertBlanks(event) if changed: count += 1 dirtyVnodeList.extend(dirtyVnodeList2) else: changed = False result = [] text = p.v.b lines = text.split('\n') for line in lines: i, w = g.skip_leading_ws_with_indent(line, 0, tabWidth) s = g.computeLeadingWhitespace( w, abs(tabWidth)) + line[i:] # use positive width. if s != line: changed = True result.append(s) if changed: count += 1 dirtyVnodeList2 = p.setDirty() dirtyVnodeList.extend(dirtyVnodeList2) result = '\n'.join(result) p.setBodyString(result) u.afterChangeNodeContents(p, undoType, innerUndoData) u.afterChangeGroup(current, undoType, dirtyVnodeList=dirtyVnodeList) if not g.unitTesting: g.es("blanks converted to tabs in", count, "nodes") # Must come before c.redraw(). if count > 0: c.redraw_after_icons_changed()
def convertTabs(self, event=None): """Convert all tabs to blanks in the selected node.""" c, p, u, w = self, self.p, self.undoer, self.frame.body.wrapper # # "Before" snapshot. bunch = u.beforeChangeBody(p) # # Data... w.selectAllText() head, lines, tail, oldSel, oldYview = self.getBodyLines() # Use the relative @tabwidth, not the global one. theDict = c.scanAllDirectives(p) tabWidth = theDict.get("tabwidth") if not tabWidth: return False # # Calculate the result. changed, result = False, [] for line in lines: i, width = g.skip_leading_ws_with_indent(line, 0, tabWidth) s = g.computeLeadingWhitespace( width, -abs(tabWidth)) + line[i:] # use negative width. if s != line: changed = True result.append(s) if not changed: return False # # Set p.b and w's text first. middle = ''.join(result) p.b = head + middle + tail # Sets dirty and changed bits. w.setAllText(head + middle + tail) # # Calculate the proper selection range (i, j, ins). i = len(head) j = max(i, len(head) + len(middle) - 1) # # Set the selection range and scroll position. w.setSelectionRange(i, j, insert=j) w.setYScrollPosition(oldYview) # # "after" snapshot. u.afterChangeBody(p, 'Add Comments', bunch) return True
def dedentBody(self, event=None): """Remove one tab's worth of indentation from all presently selected lines.""" c, p, u, w = self, self.p, self.undoer, self.frame.body.wrapper # # Initial data. sel_1, sel_2 = w.getSelectionRange() tab_width = c.getTabWidth(c.p) head, lines, tail, oldSel, oldYview = self.getBodyLines() bunch = u.beforeChangeBody(p) # # Calculate the result. changed, result = False, [] for line in lines: i, width = g.skip_leading_ws_with_indent(line, 0, tab_width) s = g.computeLeadingWhitespace(width - abs(tab_width), tab_width) + line[i:] if s != line: changed = True result.append(s) if not changed: return # # Set p.b and w's text first. middle = ''.join(result) all = head + middle + tail p.b = all # Sets dirty and changed bits. w.setAllText(all) # # Calculate the proper selection range (i, j, ins). if sel_1 == sel_2: line = result[0] ins, width = g.skip_leading_ws_with_indent(line, 0, tab_width) i = j = len(head) + ins else: i = len(head) j = len(head) + len(middle) if middle.endswith('\n'): # #1742. j -= 1 # # Set the selection range and scroll position. w.setSelectionRange(i, j, insert=j) w.setYScrollPosition(oldYview) u.afterChangeBody(p, 'Unindent Region', bunch)
def convertTabs(self, event=None): '''Convert all tabs to blanks in the selected node.''' c = self; changed = False; dirtyVnodeList = [] head, lines, tail, oldSel, oldYview = self.getBodyLines(expandSelection=True) # Use the relative @tabwidth, not the global one. theDict = c.scanAllDirectives() tabWidth = theDict.get("tabwidth") if tabWidth: result = [] for line in lines: i, w = g.skip_leading_ws_with_indent(line, 0, tabWidth) s = g.computeLeadingWhitespace(w, -abs(tabWidth)) + line[i:] # use negative width. if s != line: changed = True result.append(s) if changed: undoType = 'Convert Tabs' result = ''.join(result) oldSel = None dirtyVnodeList = c.updateBodyPane(head, result, tail, undoType, oldSel, oldYview) # Handles undo return changed, dirtyVnodeList
def convertAllTabs(self, event=None): """Convert all tabs to blanks in the selected outline.""" c = self u = c.undoer undoType = 'Convert All Tabs' current = c.p if g.app.batchMode: c.notValidInBatchMode(undoType) return theDict = c.scanAllDirectives(c.p) tabWidth = theDict.get("tabwidth") count = 0 u.beforeChangeGroup(current, undoType) for p in current.self_and_subtree(): undoData = u.beforeChangeNodeContents(p) if p == current: changed = self.convertTabs(event) if changed: count += 1 else: result = [] changed = False text = p.v.b lines = text.split('\n') for line in lines: i, w = g.skip_leading_ws_with_indent(line, 0, tabWidth) s = g.computeLeadingWhitespace( w, -abs(tabWidth)) + line[i:] # use negative width. if s != line: changed = True result.append(s) if changed: count += 1 p.setDirty() p.setBodyString('\n'.join(result)) u.afterChangeNodeContents(p, undoType, undoData) u.afterChangeGroup(current, undoType) if not g.unitTesting: g.es("tabs converted to blanks in", count, "nodes") if count > 0: c.redraw_after_icons_changed()
def regularizeWhitespace(self, s): '''Regularize leading whitespace in s: Convert tabs to blanks or vice versa depending on the @tabwidth in effect. This is only called for strict languages.''' changed = False; lines = g.splitLines(s); result = []; tab_width = self.tab_width if tab_width < 0: # Convert tabs to blanks. for line in lines: i, w = g.skip_leading_ws_with_indent(line, 0, tab_width) s = g.computeLeadingWhitespace(w, -abs(tab_width)) + line[i:] # Use negative width. if s != line: changed = True result.append(s) elif tab_width > 0: # Convert blanks to tabs. for line in lines: s = g.optimizeLeadingWhitespace(line, abs(tab_width)) # Use positive width. if s != line: changed = True result.append(s) if changed: action = 'tabs converted to blanks' if self.tab_width < 0 else 'blanks converted to tabs' message = 'inconsistent leading whitespace. %s' % action self.report(message) return ''.join(result)
def convertAllBlanks(self, event=None): '''Convert all blanks to tabs in the selected outline.''' c = self; u = c.undoer; undoType = 'Convert All Blanks' current = c.p if g.app.batchMode: c.notValidInBatchMode(undoType) return d = c.scanAllDirectives() tabWidth = d.get("tabwidth") count = 0; dirtyVnodeList = [] u.beforeChangeGroup(current, undoType) for p in current.self_and_subtree(): innerUndoData = u.beforeChangeNodeContents(p) if p == current: changed, dirtyVnodeList2 = c.convertBlanks(event) if changed: count += 1 dirtyVnodeList.extend(dirtyVnodeList2) else: changed = False; result = [] text = p.v.b lines = text.split('\n') for line in lines: i, w = g.skip_leading_ws_with_indent(line, 0, tabWidth) s = g.computeLeadingWhitespace(w, abs(tabWidth)) + line[i:] # use positive width. if s != line: changed = True result.append(s) if changed: count += 1 dirtyVnodeList2 = p.setDirty() dirtyVnodeList.extend(dirtyVnodeList2) result = '\n'.join(result) p.setBodyString(result) u.afterChangeNodeContents(p, undoType, innerUndoData) u.afterChangeGroup(current, undoType, dirtyVnodeList=dirtyVnodeList) if not g.unitTesting: g.es("blanks converted to tabs in", count, "nodes") # Must come before c.redraw(). if count > 0: c.redraw_after_icons_changed()
def dedentBody(self, event=None): '''Remove one tab's worth of indentation from all presently selected lines.''' c, undoType = self, 'Unindent' w = c.frame.body.wrapper sel_1, sel_2 = w.getSelectionRange() ins = w.getInsertPoint() tab_width = c.getTabWidth(c.p) head, lines, tail, oldSel, oldYview = self.getBodyLines() changed, result = False, [] for line in lines: i, width = g.skip_leading_ws_with_indent(line, 0, tab_width) s = g.computeLeadingWhitespace(width - abs(tab_width), tab_width) + line[i:] if s != line: changed = True result.append(s) if changed: result = ''.join(result) # Leo 5.6: preserve insert point. preserveSel = sel_1 == sel_2 if preserveSel: ins = max(0, ins - abs(tab_width)) oldSel = ins, ins c.updateBodyPane(head, result, tail, undoType, oldSel, oldYview, preserveSel)
def dedentBody(self, event=None): '''Remove one tab's worth of indentation from all presently selected lines.''' c, undoType = self, 'Unindent' w = c.frame.body.wrapper sel_1, sel_2 = w.getSelectionRange() ins = w.getInsertPoint() tab_width = c.getTabWidth(c.p) head, lines, tail, oldSel, oldYview = self.getBodyLines() changed, result = False, [] for line in lines: i, width = g.skip_leading_ws_with_indent(line, 0, tab_width) s = g.computeLeadingWhitespace(width - abs(tab_width), tab_width) + line[i:] if s != line: changed = True result.append(s) if changed: # Leo 5.6: preserve insert point. preserveSel = sel_1 == sel_2 if preserveSel: ins = max(len(head), len(result[0]) - len(lines[0]) + ins) oldSel = ins, ins result = ''.join(result) c.updateBodyPane(head, result, tail, undoType, oldSel, oldYview, preserveSel)
def regularize_whitespace(self, s): ''' Regularize leading whitespace in s: Convert tabs to blanks or vice versa depending on the @tabwidth in effect. ''' kind = 'tabs' if self.tab_width > 0 else 'blanks' kind2 = 'blanks' if self.tab_width > 0 else 'tabs' fn = g.shortFileName(self.root.h) lines = g.splitLines(s) count, result, tab_width = 0, [], self.tab_width self.ws_error = False # 2016/11/23 if tab_width < 0: # Convert tabs to blanks. for n, line in enumerate(lines): i, w = g.skip_leading_ws_with_indent(line, 0, tab_width) s = g.computeLeadingWhitespace(w, -abs(tab_width)) + line[i:] # Use negative width. if s != line: count += 1 result.append(s) elif tab_width > 0: # Convert blanks to tabs. for n, line in enumerate(lines): s = g.optimizeLeadingWhitespace(line, abs(tab_width)) # Use positive width. if s != line: count += 1 result.append(s) if count: self.ws_error = True # A flag to check. if not g.unitTesting: # g.es_print('Warning: Intermixed tabs and blanks in', fn) # g.es_print('Perfect import test will ignoring leading whitespace.') g.es('changed leading %s to %s in %s line%s in %s' % ( kind2, kind, count, g.plural(count), fn)) if g.unitTesting: # Sets flag for unit tests. self.report('changed %s lines' % count) return ''.join(result)