@@ -2627,7 +2627,6 @@ def set(self, *args):
26272627
26282628class Text (Widget ):
26292629 """Text widget which can display text in various forms."""
2630- # XXX Add dump()
26312630 def __init__ (self , master = None , cnf = {}, ** kw ):
26322631 """Construct a text widget with the parent MASTER.
26332632
@@ -2671,6 +2670,44 @@ def dlineinfo(self, index):
26712670 and baseline position of the visible part of the line containing
26722671 the character at INDEX."""
26732672 return self ._getints (self .tk .call (self ._w , 'dlineinfo' , index ))
2673+ def dump (self , index1 , index2 = None , command = None , ** kw ):
2674+ """Return the contents of the widget between index1 and index2.
2675+
2676+ The type of contents returned in filtered based on the keyword
2677+ parameters; if 'all', 'image', 'mark', 'tag', 'text', or 'window' are
2678+ given and true, then the corresponding items are returned. The result
2679+ is a list of triples of the form (key, value, index). If none of the
2680+ keywords are true then 'all' is used by default.
2681+
2682+ If the 'command' argument is given, it is called once for each element
2683+ of the list of triples, with the values of each triple serving as the
2684+ arguments to the function. In this case the list is not returned."""
2685+ args = []
2686+ func_name = None
2687+ result = None
2688+ if not command :
2689+ # Never call the dump command without the -command flag, since the
2690+ # output could involve Tcl quoting and would be a pain to parse
2691+ # right. Instead just set the command to build a list of triples
2692+ # as if we had done the parsing.
2693+ result = []
2694+ def append_triple (key , value , index , result = result ):
2695+ result .append ((key , value , index ))
2696+ command = append_triple
2697+ try :
2698+ if not isinstance (command , str ):
2699+ func_name = command = self ._register (command )
2700+ args += ["-command" , command ]
2701+ for key in kw :
2702+ if kw [key ]: args .append ("-" + key )
2703+ args .append (index1 )
2704+ if index2 :
2705+ args .append (index2 )
2706+ self .tk .call (self ._w , "dump" , * args )
2707+ return result
2708+ finally :
2709+ if func_name :
2710+ self .deletecommand (func_name )
26742711 def get (self , index1 , index2 = None ):
26752712 """Return the text from INDEX1 to INDEX2 (not included)."""
26762713 return self .tk .call (self ._w , 'get' , index1 , index2 )
0 commit comments