[PATCH] WIP: sort top level messages in thread

2018-08-26 Thread David Bremner
this needs a test, and  memory de-allocation of the replaced
lists. Currently it creates a fair amount of garbage.
---

Hi Gregor;

Sorry for the lack of reply, some vacation intervened. Can you test
this patch? I think it fixes the second thread also.

lib/message.cc| 43 +++
 lib/notmuch-private.h |  5 +++--
 lib/thread.cc |  9 ++---
 3 files changed, 28 insertions(+), 29 deletions(-)

diff --git a/lib/message.cc b/lib/message.cc
index 3c547298..f329be20 100644
--- a/lib/message.cc
+++ b/lib/message.cc
@@ -639,34 +639,37 @@ _cmpmsg (const void *pa, const void *pb)
 return (int) difftime (time_a, time_b);
 }
 
-void
-_notmuch_message_sort_subtree (notmuch_message_t *root)
-{
-size_t child_count = 0;
-size_t child_capacity = 16;
-notmuch_message_t **children = talloc_zero_array (root, notmuch_message_t 
*, child_capacity);
+notmuch_message_list_t *
+_notmuch_message_sort_subtrees (notmuch_message_list_t *list) {
+
+size_t count = 0;
+size_t capacity = 16;
 
-for (notmuch_messages_t *messages = _notmuch_messages_create 
(root->replies);
+if (!list)
+   return list;
+
+notmuch_message_t **message_array = talloc_zero_array (list, 
notmuch_message_t *, capacity);
+
+for (notmuch_messages_t *messages = _notmuch_messages_create (list);
 notmuch_messages_valid (messages);
 notmuch_messages_move_to_next (messages)) {
-   notmuch_message_t *child = notmuch_messages_get (messages);
-   if (child_count >= child_capacity) {
-   child_capacity *= 2;
-   children = talloc_realloc (root, children, notmuch_message_t *, 
child_capacity);
+   notmuch_message_t *root = notmuch_messages_get (messages);
+   if (count >= capacity) {
+   capacity *= 2;
+   message_array = talloc_realloc (root, message_array, 
notmuch_message_t *, capacity);
}
-   children[child_count++] = child;
-   _notmuch_message_sort_subtree (child);
+   message_array[count++] = root;
+   root->replies = _notmuch_message_sort_subtrees (root->replies);
 }
 
-notmuch_message_list_t *new_replies = _notmuch_message_list_create (root);
+notmuch_message_list_t *new_list = _notmuch_message_list_create (list);
 
-qsort (children, child_count, sizeof (notmuch_message_t *), _cmpmsg);
-for (size_t i=0; ireplies);
-root->replies = new_replies;
-talloc_free (children);
+talloc_free (message_array);
+return new_list;
 }
 
 notmuch_messages_t *
diff --git a/lib/notmuch-private.h b/lib/notmuch-private.h
index 67fd4990..64f4e982 100644
--- a/lib/notmuch-private.h
+++ b/lib/notmuch-private.h
@@ -558,8 +558,9 @@ size_t _notmuch_message_get_thread_depth (notmuch_message_t 
*message);
 void
 _notmuch_message_label_depths (notmuch_message_t *message,
   size_t depth);
-void
-_notmuch_message_sort_subtree (notmuch_message_t *message);
+
+notmuch_message_list_t *
+_notmuch_message_sort_subtrees (notmuch_message_list_t *list);
 
 /* sha1.c */
 
diff --git a/lib/thread.cc b/lib/thread.cc
index a5047103..e9e15a5c 100644
--- a/lib/thread.cc
+++ b/lib/thread.cc
@@ -505,13 +505,8 @@ _resolve_thread_relationships (notmuch_thread_t *thread)
_notmuch_message_list_add_message (thread->toplevel_list, message);
 }
 
-for (notmuch_messages_t *messages = _notmuch_messages_create 
(thread->toplevel_list);
-notmuch_messages_valid (messages);
-notmuch_messages_move_to_next (messages))
-{
-   notmuch_message_t *message = notmuch_messages_get (messages);
-   _notmuch_message_sort_subtree (message);
-}
+/* XXX this creates garbage */
+thread->toplevel_list = _notmuch_message_sort_subtrees 
(thread->toplevel_list);
 
 talloc_free (local);
 }
-- 
2.18.0

___
notmuch mailing list
notmuch@notmuchmail.org
https://notmuchmail.org/mailman/listinfo/notmuch


Re: [PATCH v1 1/1] emacs: Kill the stderr buffer when an async process completes

2018-08-26 Thread David Edmondson
On Sunday, 2018-08-26 at 08:30:41 -03, David Bremner wrote:

> David Edmondson  writes:
>>  
>> +(defun notmuch-start-notmuch-error-sentinel (proc event)
>> +  (let* ((err-file (process-get proc 'err-file))
>> + (err-buffer (or (process-get proc 'err-buffer)
>> + (find-file-noselect err-file
> Is the second case here (find-file-noselect) for the non-make-process
> code path, or something else? It might help to have a comment.

For non-make-process.  Updated patch sent.

>> +(when err-buffer (kill-buffer err-buffer
>> +

dme.
-- 
I'm not the reason you're looking for redemption.
___
notmuch mailing list
notmuch@notmuchmail.org
https://notmuchmail.org/mailman/listinfo/notmuch


[PATCH v2 1/1] emacs: Kill the stderr buffer when an async process completes

2018-08-26 Thread David Edmondson
On some platforms (e.g. macOS), it is necessary to add a real sentinel
process for the error buffer used by `notmuch-start-notmuch' rather
than a no-op sentinel.
---
 emacs/notmuch-lib.el | 21 +++--
 1 file changed, 15 insertions(+), 6 deletions(-)

diff --git a/emacs/notmuch-lib.el b/emacs/notmuch-lib.el
index a7e02710..25d83fd6 100644
--- a/emacs/notmuch-lib.el
+++ b/emacs/notmuch-lib.el
@@ -909,7 +909,7 @@ invoke `set-process-sentinel' directly on the returned 
process,
 as that will interfere with the handling of stderr and the exit
 status."
 
-  (let (err-file err-buffer proc
+  (let (err-file err-buffer proc err-proc
;; Find notmuch using Emacs' `exec-path'
(command (or (executable-find notmuch-command)
 (error "Command not found: %s" notmuch-command
@@ -926,11 +926,13 @@ status."
  :buffer buffer
  :command (cons command args)
  :connection-type 'pipe
- :stderr err-buffer))
+ :stderr err-buffer)
+   err-proc (get-buffer-process err-buffer))
  (process-put proc 'err-buffer err-buffer)
- ;; Silence "Process NAME stderr finished" in stderr by adding a
- ;; no-op sentinel to the fake stderr process object
- (set-process-sentinel (get-buffer-process err-buffer) #'ignore))
+
+ (process-put err-proc 'err-file err-file)
+ (process-put err-proc 'err-buffer err-buffer)
+ (set-process-sentinel err-proc 
#'notmuch-start-notmuch-error-sentinel))
 
   ;; On Emacs versions before 25, there is no way to capture
   ;; stdout and stderr separately for asynchronous processes, or
@@ -990,9 +992,16 @@ status."
;; Emacs behaves strangely if an error escapes from a sentinel,
;; so turn errors into messages.
(message "%s" (error-message-string err
-(when err-buffer (kill-buffer err-buffer))
 (when err-file (ignore-errors (delete-file err-file)
 
+(defun notmuch-start-notmuch-error-sentinel (proc event)
+  (let* ((err-file (process-get proc 'err-file))
+;; When `make-process' is available, use the error buffer
+;; associated with the process, otherwise the error file.
+(err-buffer (or (process-get proc 'err-buffer)
+(find-file-noselect err-file
+(when err-buffer (kill-buffer err-buffer
+
 ;; This variable is used only buffer local, but it needs to be
 ;; declared globally first to avoid compiler warnings.
 (defvar notmuch-show-process-crypto nil)
-- 
2.17.1 (Apple Git-112)

___
notmuch mailing list
notmuch@notmuchmail.org
https://notmuchmail.org/mailman/listinfo/notmuch


[PATCH v2 0/1] emacs: Kill the stderr buffer when an async process completes

2018-08-26 Thread David Edmondson

emacs: Kill the stderr buffer when an async process completes

v2: Explain the make-process distinction in the error sentinel.


David Edmondson (1):
  emacs: Kill the stderr buffer when an async process completes

 emacs/notmuch-lib.el | 21 +++--
 1 file changed, 15 insertions(+), 6 deletions(-)

-- 
2.17.1 (Apple Git-112)

___
notmuch mailing list
notmuch@notmuchmail.org
https://notmuchmail.org/mailman/listinfo/notmuch


Re: [PATCH v1 1/1] emacs: Kill the stderr buffer when an async process completes

2018-08-26 Thread David Bremner
David Edmondson  writes:
>  
> +(defun notmuch-start-notmuch-error-sentinel (proc event)
> +  (let* ((err-file (process-get proc 'err-file))
> +  (err-buffer (or (process-get proc 'err-buffer)
> +  (find-file-noselect err-file
Is the second case here (find-file-noselect) for the non-make-process
code path, or something else? It might help to have a comment.
> +(when err-buffer (kill-buffer err-buffer
> +

___
notmuch mailing list
notmuch@notmuchmail.org
https://notmuchmail.org/mailman/listinfo/notmuch