blob: 2ce3c7eafe5a817f9e93d5149bf47bd2c8047109 [file] [log] [blame]
Raymond Hettingereb816952009-05-29 01:22:18 +00001'Provides "Strip trailing whitespace" under the "Format" menu.'
2
Raymond Hettingereb816952009-05-29 01:22:18 +00003class RstripExtension:
4
5 menudefs = [
Terry Jan Reedy4f133e22013-07-13 02:34:43 -04006 ('format', [None, ('Strip trailing whitespace', '<<do-rstrip>>'), ] ), ]
Raymond Hettingereb816952009-05-29 01:22:18 +00007
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
Terry Jan Reedy4f133e22013-07-13 02:34:43 -040019 end_line = int(float(text.index('end')))
Raymond Hettingereb816952009-05-29 01:22:18 +000020 for cur in range(1, end_line):
Terry Jan Reedy4f133e22013-07-13 02:34:43 -040021 txt = text.get('%i.0' % cur, '%i.end' % cur)
22 raw = len(txt)
Raymond Hettingereb816952009-05-29 01:22:18 +000023 cut = len(txt.rstrip())
Terry Jan Reedy4f133e22013-07-13 02:34:43 -040024 # 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)
Raymond Hettingereb816952009-05-29 01:22:18 +000028
29 undo.undo_block_stop()
Terry Jan Reedy4f133e22013-07-13 02:34:43 -040030
31if __name__ == "__main__":
32 import unittest
33 unittest.main('idlelib.idle_test.test_rstrip', verbosity=2, exit=False)