5555from diffuse import constants
5656from diffuse .vcs .vcs_registry import VcsRegistry
5757
58- if not hasattr (__builtins__ , 'WindowsError' ):
59- # define 'WindowsError' so 'except' statements will work on all platforms
60- WindowsError = IOError
61-
6258# avoid some dictionary lookups when string.whitespace is used in loops
6359# this is sorted based upon frequency to speed up code for stripping whitespace
6460whitespace = ' \t \n \r \x0b \x0c '
@@ -131,38 +127,6 @@ def parse(self, state_name, s):
131127 start = end
132128 return state_name , blocks
133129
134- # split string into lines based upon DOS, Mac, and Unix line endings
135- def splitlines (s ):
136- # split on new line characters
137- temp , i , n = [], 0 , len (s )
138- while i < n :
139- j = s .find ('\n ' , i )
140- if j < 0 :
141- temp .append (s [i :])
142- break
143- j += 1
144- temp .append (s [i :j ])
145- i = j
146- # split on carriage return characters
147- ss = []
148- for s in temp :
149- i , n = 0 , len (s )
150- while i < n :
151- j = s .find ('\r ' , i )
152- if j < 0 :
153- ss .append (s [i :])
154- break
155- j += 1
156- if j < n and s [j ] == '\n ' :
157- j += 1
158- ss .append (s [i :j ])
159- i = j
160- return ss
161-
162- # also recognise old Mac OS line endings
163- def readlines (fd ):
164- return strip_eols (splitlines (fd .read ()))
165-
166130def readconfiglines (fd ):
167131 return fd .read ().replace ('\r ' , '' ).split ('\n ' )
168132
@@ -1141,33 +1105,12 @@ def getFormat(ss):
11411105 flags |= UNIX_FORMAT
11421106 return flags
11431107
1144- # returns the number of characters in the string excluding any line ending
1145- # characters
1146- def len_minus_line_ending (s ):
1147- if s is None :
1148- return 0
1149- n = len (s )
1150- if s .endswith ('\r \n ' ):
1151- n -= 2
1152- elif s .endswith ('\r ' ) or s .endswith ('\n ' ):
1153- n -= 1
1154- return n
1155-
1156- # returns the string without the line ending characters
1157- def strip_eol (s ):
1158- if s is not None :
1159- return s [:len_minus_line_ending (s )]
1160-
1161- # returns the list of strings without line ending characters
1162- def strip_eols (ss ):
1163- return [ strip_eol (s ) for s in ss ]
1164-
11651108# convenience method to change the line ending of a string
11661109def convert_to_format (s , format ):
11671110 if s is not None and format != 0 :
11681111 old_format = getFormat ([ s ])
11691112 if old_format != 0 and (old_format & format ) == 0 :
1170- s = strip_eol (s )
1113+ s = utils . strip_eol (s )
11711114 # prefer the host line ending style
11721115 if (format & DOS_FORMAT ) and os .linesep == '\r \n ' :
11731116 s += os .linesep
@@ -1913,7 +1856,7 @@ def setFont(self, font):
19131856 # This is an inline loop over self.characterWidth() for performance reasons.
19141857 def stringWidth (self , s ):
19151858 if not self .prefs .getBool ('display_show_whitespace' ):
1916- s = strip_eol (s )
1859+ s = utils . strip_eol (s )
19171860 col = 0
19181861 for c in s :
19191862 try :
@@ -1965,7 +1908,7 @@ def characterWidth(self, i, c):
19651908 def expand (self , s ):
19661909 visible = self .prefs .getBool ('display_show_whitespace' )
19671910 if not visible :
1968- s = strip_eol (s )
1911+ s = utils . strip_eol (s )
19691912 tab_width = self .prefs .getInt ('display_tab_width' )
19701913 col = 0
19711914 result = []
@@ -2563,7 +2506,7 @@ def _alignmentHash(self, line):
25632506 # hashes for non-null lines should start with '+' to distinguish
25642507 # them from blank lines
25652508 if pref ('align_ignore_endofline' ):
2566- text = strip_eol (text )
2509+ text = utils . strip_eol (text )
25672510 if pref ('align_ignore_blanklines' ) and isBlank (text ):
25682511 # consider all lines containing only white space as the same
25692512 return ''
@@ -2792,8 +2735,8 @@ def replaceText(self, text):
27922735 if text is None :
27932736 text = ''
27942737 # split the replacement text into lines
2795- ss = splitlines (text )
2796- if len (ss ) == 0 or len (ss [- 1 ]) != len_minus_line_ending (ss [- 1 ]):
2738+ ss = utils . splitlines (text )
2739+ if len (ss ) == 0 or len (ss [- 1 ]) != utils . len_minus_line_ending (ss [- 1 ]):
27972740 ss .append ('' )
27982741 # change the format to that of the target pane
27992742 if pane .format == 0 :
@@ -2807,7 +2750,7 @@ def replaceText(self, text):
28072750 lastcol = 0
28082751 if len (ss ) > 0 :
28092752 last = ss [- 1 ]
2810- if len (last ) == len_minus_line_ending (last ):
2753+ if len (last ) == utils . len_minus_line_ending (last ):
28112754 del ss [- 1 ]
28122755 lastcol = len (last )
28132756 cur_line = line0 + len (ss )
@@ -3200,7 +3143,7 @@ def button_press(self, f, x, y, extend):
32003143 x , y = - 1 , 0
32013144 i = min (y // self .font_height , len (self .panes [f ].lines ))
32023145 if self .mode == CHAR_MODE and f == self .current_pane :
3203- text = strip_eol (self .getLineText (f , i ))
3146+ text = utils . strip_eol (self .getLineText (f , i ))
32043147 j = self ._getPickedCharacter (text , x , True )
32053148 if extend :
32063149 si , sj = self .selection_line , self .selection_char
@@ -3241,7 +3184,7 @@ def darea_button_press_cb(self, widget, event, f):
32413184 self .emit ('mode_changed' )
32423185 elif self .mode == CHAR_MODE and self .current_pane == f :
32433186 # select word
3244- text = strip_eol (self .getLineText (f , i ))
3187+ text = utils . strip_eol (self .getLineText (f , i ))
32453188 if text is not None :
32463189 n = len (text )
32473190 j = self ._getPickedCharacter (text , x , False )
@@ -3323,7 +3266,7 @@ def getDiffRanges(self, f, i, idx, flag):
33233266 else :
33243267 s = s2
33253268 if self .prefs .getBool ('display_ignore_endofline' ):
3326- s = strip_eol (s )
3269+ s = utils . strip_eol (s )
33273270
33283271 s1 = nullToEmpty (self .getCompareString (f , i ))
33293272 s2 = nullToEmpty (self .getCompareString (f + 1 , i ))
@@ -3394,7 +3337,7 @@ def getCompareString(self, f, i):
33943337 s = line .getText ()
33953338 if s is not None :
33963339 if self .prefs .getBool ('display_ignore_endofline' ):
3397- s = strip_eol (s )
3340+ s = utils . strip_eol (s )
33983341 if self .prefs .getBool ('display_ignore_blanklines' ) and isBlank (s ):
33993342 return None
34003343 if self .prefs .getBool ('display_ignore_whitespace' ):
@@ -3906,7 +3849,7 @@ def diffmap_draw_cb(self, widget, cr):
39063849 # returns the maximum valid offset for a cursor position
39073850 # cursors cannot be moved to the right of line ending characters
39083851 def getMaxCharPosition (self , i ):
3909- return len_minus_line_ending (self .getLineText (self .current_pane , i ))
3852+ return utils . len_minus_line_ending (self .getLineText (self .current_pane , i ))
39103853
39113854 # 'enter_align_mode' keybinding action
39123855 def _line_mode_enter_align_mode (self ):
@@ -4132,7 +4075,7 @@ def key_press_cb(self, widget, event):
41324075 # move the cursor to column 'col' if possible
41334076 s = self .getLineText (f , i )
41344077 if s is not None :
4135- s = strip_eol (s )
4078+ s = utils . strip_eol (s )
41364079 idx = 0
41374080 for c in s :
41384081 w = self .characterWidth (idx , c )
@@ -4282,7 +4225,7 @@ def key_press_cb(self, widget, event):
42824225 self .recordEditMode ()
42834226 for i in range (start , end + 1 ):
42844227 text = self .getLineText (f , i )
4285- if text is not None and len_minus_line_ending (text ) > 0 :
4228+ if text is not None and utils . len_minus_line_ending (text ) > 0 :
42864229 # count spacing before the first non-whitespace character
42874230 j , w = 0 , 0
42884231 while j < len (text ) and text [j ] in ' \t ' :
@@ -4807,7 +4750,7 @@ def remove_trailing_white_space(self):
48074750 text = self .getLineText (f , i )
48084751 if text is not None :
48094752 # locate trailing whitespace
4810- old_n = n = len_minus_line_ending (text )
4753+ old_n = n = utils . len_minus_line_ending (text )
48114754 while n > 0 and text [n - 1 ] in whitespace :
48124755 n -= 1
48134756 # update line if it changed
@@ -4914,7 +4857,7 @@ def _adjust_indenting(self, offset):
49144857 self .recordEditMode ()
49154858 for i in range (start , end + 1 ):
49164859 text = self .getLineText (f , i )
4917- if text is not None and len_minus_line_ending (text ) > 0 :
4860+ if text is not None and utils . len_minus_line_ending (text ) > 0 :
49184861 # count spacing before the first non-whitespace character
49194862 j , w = 0 , 0
49204863 while j < len (text ) and text [j ] in ' \t ' :
@@ -5628,8 +5571,8 @@ def load(self, f, info):
56285571 s , encoding = self .prefs .convertToUnicode (s )
56295572 else :
56305573 s = str (s , encoding = encoding )
5631- ss = splitlines (s )
5632- except (IOError , OSError , UnicodeDecodeError , WindowsError , LookupError ):
5574+ ss = utils . splitlines (s )
5575+ except (IOError , OSError , UnicodeDecodeError , LookupError ):
56335576 # FIXME: this can occur before the toplevel window is drawn
56345577 if rev is not None :
56355578 msg = _ ('Error reading revision %(rev)s of %(file)s.' ) % { 'rev' : rev , 'file' : name }
@@ -6460,7 +6403,7 @@ def createCommitFileTabs(self, items, labels, options):
64606403 name , rev = spec
64616404 viewer .load (i , FileInfo (name , encoding , vcs , rev ))
64626405 viewer .setOptions (options )
6463- except (IOError , OSError , WindowsError ):
6406+ except (IOError , OSError ):
64646407 utils .logErrorAndDialog (_ ('Error retrieving commits for %s.' ) % (dn , ), self .get_toplevel ())
64656408
64666409 # create a new viewer for each modified file found in 'items'
@@ -6489,7 +6432,7 @@ def createModifiedFileTabs(self, items, labels, options):
64896432 name , rev = spec
64906433 viewer .load (i , FileInfo (name , encoding , vcs , rev ))
64916434 viewer .setOptions (options )
6492- except (IOError , OSError , WindowsError ):
6435+ except (IOError , OSError ):
64936436 utils .logErrorAndDialog (_ ('Error retrieving modifications for %s.' ) % (dn , ), self .get_toplevel ())
64946437
64956438 # close all tabs without differences
0 commit comments