1'Provides "Strip trailing whitespace" under the "Format" menu.' 2 3class RstripExtension: 4 5 menudefs = [ 6 ('format', [None, ('Strip trailing whitespace', '<<do-rstrip>>'), ] ), ] 7 8 def __init__(self, editwin): 9 self.editwin = editwin 10 self.editwin.text.bind("<<do-rstrip>>", self.do_rstrip) 11 12 def do_rstrip(self, event=None): 13 14 text = self.editwin.text 15 undo = self.editwin.undo 16 17 undo.undo_block_start() 18 19 end_line = int(float(text.index('end'))) 20 for cur in range(1, end_line): 21 txt = text.get('%i.0' % cur, '%i.end' % cur) 22 raw = len(txt) 23 cut = len(txt.rstrip()) 24 # Since text.delete() marks file as changed, even if not, 25 # only call it when needed to actually delete something. 26 if cut < raw: 27 text.delete('%i.%i' % (cur, cut), '%i.end' % cur) 28 29 undo.undo_block_stop() 30 31if __name__ == "__main__": 32 import unittest 33 unittest.main('idlelib.idle_test.test_rstrip', verbosity=2, exit=False) 34