I modified g.es to automatically handle the accumulation of calls.
Every time you call it, the args and kwargs are added to a buffer and
a QTimer is set.  When the QTimer goes off, the buffered es calls are
concat'ed together and sent as one call.  It makes my posted example
work in its original form.

The use of QTimer makes this gui specific.  It probably wouldn't be
hard to abstract away the calls, but I am not sure what the best way
to do that would be.

There is a small bug where some lines have an initial space inserted
in front.  I think it has something to do how the put function handles
newlines, but I didn't care enough to figure it out.

I thought I'd try submitting this as a bzr diff to see how that
goes...  To do this, I updated a clean version of leo I had checked
out earlier, did a commit (which had no changes), made my changes, and
then did a diff.

=== modified file 'leo/core/leoGlobals.py'
--- leo/core/leoGlobals.py      2009-04-23 16:38:51 +0000
+++ leo/core/leoGlobals.py      2009-04-27 07:19:44 +0000
@@ -3093,18 +3093,113 @@
         log.putnl(tabName)
 #...@-node:ekr.20031218072017.1474:g.enl, ecnl & ecnls
 #...@+node:ekr.20070626132332:g.es & minitest
-def es(*args,**keys):
-
+#@<< class: EsObject >>
+...@+node:jca.20090427013120.1:<< class: EsObject >>
+class EsObject:
+    def __init__(self, args, kwargs):
+        self.args = args
+        self.kwargs = kwargs
+
+    def equals(self, other):
+        return self.kwargs == other.kwargs
+
+    def append(self, other):
+        self.args += other.args
+...@-node:jca.20090427013120.1:<< class: EsObject >>
+...@nl
+
+#@<< buffered es >>
+...@+node:jca.20090427013120.2:<< buffered es >>
+def es(*args, **keys):
+    #@    << handle no log or no app >>
+    #...@+node:jca.20090427013120.3:<< handle no log or no app >>
+    log = app.log
+    if app.killed: return
+
+    if not log or log.isNull:
+        return
+    #...@nonl
+    #...@-node:jca.20090427013120.3:<< handle no log or no app >>
+    #...@nl
+
+    #@    << create buffer and flush timer >>
+    #...@+node:jca.20090427013120.4:<< create buffer and flush timer >>
+    global es_buffer, flush_timer
+
+    if "es_buffer" not in globals():
+        es_buffer = []
+
+        #@    << create flush timer >>
+        #...@+node:jca.20090427013120.5:<< create flush timer >>
+        from PyQt4.QtCore import QTimer, SIGNAL
+        t = flush_timer = QTimer()
+        t.setSingleShot(True)
+        t.connect(t, SIGNAL("timeout()"), g.flush)
+        #...@nonl
+        #...@-node:jca.20090427013120.5:<< create flush timer >>
+        #...@nl
+    #...@-node:jca.20090427013120.4:<< create buffer and flush timer >>
+    #...@nl
+
+    es_buffer.append(EsObject(args, keys))
+
+    t = flush_timer
+    if not t.isActive():
+        t.start()
+...@-node:jca.20090427013120.2:<< buffered es >>
+...@nl
+
+#@<< def: flush >>
+...@+node:jca.20090427013120.6:<< def: flush >>
+
+def flush():
+    #@    << Break into chunks >>
+    #...@+node:jca.20090427013120.7:<< Break into chunks >>
+
+    global es_buffer
+    chunks = []
+    first_item = es_buffer.pop(0)
+    while es_buffer:
+        second_item = es_buffer.pop(0)
+        if first_item.equals(second_item):
+            first_item.append(second_item)
+        else:
+            chunks.append(first_item)
+            first_item = second_item
+    chunks.append(first_item)
+    #...@nonl
+    #...@-node:jca.20090427013120.7:<< Break into chunks >>
+    #...@nl
+
+    for chunk in chunks:
+        args = []
+        for arg in chunk.args:
+            arg = str(arg)
+            if arg.strip() == "":
+                continue
+            if not arg.endswith('\n'):
+                arg += '\n'
+            args.append(arg)
+        g.original_es(*args, **chunk.kwargs)
+
+    global flush_timer
+    flush_timer.stop()
+...@-node:jca.20090427013120.6:<< def: flush >>
+...@nl
+
+#@<< def: original_es >>
+...@+middle:jca.20090427013120.6:<< def: flush >>
+...@+node:jca.20090427013120.8:<< def: original_es >>
+def original_es(*args, **keys):
     '''Put all non-keyword args to the log pane.
     The first, third, fifth, etc. arg translated by
g.translateString.
-    Supports color, comma, newline, spaces and tabName keyword
arguments.
-    '''
+    Supports color, comma, newline, spaces and tabName keyword
arguments.'''
     log = app.log
-    if app.killed: return
     # Compute the effective args.
-    d =
{'color':'black','commas':False,'newline':True,'spaces':True,'tabName':'Log'}
+    d = {'color':'black', 'commas':False, 'newline':True,
'spaces':True,
+           'tabName':'Log'}
     d = g.doKeywordArgs(keys,d)
     color = d.get('color')
     if color == 'suppress': return # New in 4.3.
@@ -3135,6 +3230,9 @@
             app.logWaiting.append((s+'\n',color),)
         else:
             app.logWaiting.append((s,color),)
+...@-node:jca.20090427013120.8:<< def: original_es >>
+...@-middle:jca.20090427013120.6:<< def: flush >>
+...@nl
 #...@+node:ekr.20071024101611:mini test of es
 #@@nocolor
 #@@first

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/leo-editor?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to