From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by arlo.cworth.org (Postfix) with ESMTP id 00F4D6DE0F37 for ; Mon, 25 Mar 2019 08:33:12 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at cworth.org X-Spam-Flag: NO X-Spam-Score: -0.395 X-Spam-Level: X-Spam-Status: No, score=-0.395 tagged_above=-999 required=5 tests=[AWL=-0.195, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_PASS=-0.001] autolearn=disabled Received: from arlo.cworth.org ([127.0.0.1]) by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id djyG-e0jbJ26 for ; Mon, 25 Mar 2019 08:33:10 -0700 (PDT) Received: from mail-ed1-f53.google.com (mail-ed1-f53.google.com [209.85.208.53]) by arlo.cworth.org (Postfix) with ESMTPS id 900676DE0F2E for ; Mon, 25 Mar 2019 08:33:10 -0700 (PDT) Received: by mail-ed1-f53.google.com with SMTP id a25so7915476edc.8 for ; Mon, 25 Mar 2019 08:33:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version; bh=Z/4vqKfd0qeE0UiQG+NGrFf9+TNFM3dDel2+nkVPkEc=; b=FTHxlftwTJVRoUxa236C6Knu+ybF0sL6y7oit+GpLdrA64Tw1KqwE9IhdO5Rpjekza gJgoCH7y5av/nbg/v56fUGpQlrN6FAL5LnIhIPIKC5QlnCUMGDzoP3huD83UuN+QXM0g ZIRHLEp747DumaLsWkOZBf/AWPXnU4A9ZUtEQ+B4Zxy+NqGgB7S3fjPkRWABCPua7kgo WMfStUjDd2rLNUEfpT9FLfxEK3em74FE8FmcNAawhOaoxameaI5n924+GeyF0h/OYeP5 zGa4aBEKNm8/ybeKHFrj4hLzx4WIkI4cqdODvNbjaXStXBLoWXLHCK68vkxpdhQA5dNt /D2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version; bh=Z/4vqKfd0qeE0UiQG+NGrFf9+TNFM3dDel2+nkVPkEc=; b=akX1mI+4Phdl5XP9q+xQRx1MbPb3ueoTTxPs3PAj/gpEkcWZbKqogtMPOWDnqqJVlJ Kw+SrIUnVBlQbX3M/zm94LCAJANawpX0oF77S0gEk2oXDRBFkWZVSKMZzjldlZ82IKzn /hcgBQPASgI/PT12MfLO4rmBaKgxkFocQGbRPju/HhQisobslehdOXmwm57lEVSncGX0 VNKjsFe0Ah4cEKL2QsX7qyKSrHAWmWsbBWqqLm2TggceBX9BBXs8hn1OABgGuQT5kw7P ZehMKtaHv/Lz7z4DWGWpE/A5Bj1qw8R5/anGJ5MkU2Hk1WBXtOOzU2z3GnK1r4258d6f Wn7Q== X-Gm-Message-State: APjAAAUgoN8NW+TGJTo7Xk6EPVqXvR1TMVkwzm73YcgBZKWFSzkxrxES jovd8P9fLR0hfCbQhmNet1RzY0ILd8Y= X-Google-Smtp-Source: APXvYqx4mQ+kke6Ac2WsJy0Rv2W0vfTPdfReAZdUaUDVoeznq5NEM2RAN1G5FfdzmLdnpezbtjVuag== X-Received: by 2002:a50:b615:: with SMTP id b21mr16669425ede.175.1553527988406; Mon, 25 Mar 2019 08:33:08 -0700 (PDT) Received: from localhost ([2620:10d:c092:180::1:49b]) by smtp.gmail.com with ESMTPSA id f13sm3380624eda.38.2019.03.25.08.33.07 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 25 Mar 2019 08:33:07 -0700 (PDT) From: Julien Masson To: notmuch@notmuchmail.org Cc: Subject: [PATCH v2] emacs: tree: support fold/unfold thread or sub-thread Date: Mon, 25 Mar 2019 16:25:12 +0100 Message-ID: <861s2vrmtp.fsf@gmail.com> MIME-Version: 1.0 Content-Type: text/plain X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 25 Mar 2019 15:33:12 -0000 This patch allow the user to fold/unfold a thread or a sub-thread in the current tree buffer by pressing "t" key. By default a string is displayed at the beginning of the overlay to indicate that this thread/sub-thread is folded. Pressing again "t" on a folded thread/sub-thread will unfold it. This feature works accross all the queries which are in Tree View. Signed-off-by: Julien Masson --- Changes from V1: https://notmuchmail.org/pipermail/notmuch/2019/027571.html -> handle sub-thread folding -> With Universal Argument (C-u), the user can fold the whole thread when pressing "t" -> replace seq-{find,filter} functions with their corresponding cl functions emacs/notmuch-tree.el | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/emacs/notmuch-tree.el b/emacs/notmuch-tree.el index c00315e..8867542 100644 --- a/emacs/notmuch-tree.el +++ b/emacs/notmuch-tree.el @@ -71,6 +71,11 @@ Note the author string should not contain :type '(alist :key-type (string) :value-type (string)) :group 'notmuch-tree) +(defcustom notmuch-tree-overlay-string " [...]" + "String displayed at the beginning of the overlay" + :type 'string + :group 'notmuch-tree) + ;; Faces for messages that match the query. (defface notmuch-tree-match-face '((t :inherit default)) @@ -159,6 +164,13 @@ Note the author string should not contain :group 'notmuch-tree :group 'notmuch-faces) +;; Faces for overlays +(defface notmuch-tree-overlay-fold-face + '((t :inherit 'font-lock-keyword-face)) + "Default face used to display `notmuch-tree-overlay-string'" + :group 'notmuch-tree + :group 'notmuch-faces) + (defvar notmuch-tree-previous-subject "The subject of the most recent result shown during the async display") (make-variable-buffer-local 'notmuch-tree-previous-subject) @@ -196,6 +208,9 @@ if the user has loaded a different buffer in that window.") (make-variable-buffer-local 'notmuch-tree-message-buffer) (put 'notmuch-tree-message-buffer 'permanent-local t) +(defvar notmuch-tree-overlays nil + "List of overlays used to fold/unfold thread") + (defun notmuch-tree-to-message-pane (func) "Execute FUNC in message pane. @@ -294,6 +309,7 @@ FUNC." (define-key map " " 'notmuch-tree-scroll-or-next) (define-key map (kbd "DEL") 'notmuch-tree-scroll-message-window-back) (define-key map "e" 'notmuch-tree-resume-message) + (define-key map "t" 'notmuch-tree-toggle-folding-thread) map)) (fset 'notmuch-tree-mode-map notmuch-tree-mode-map) @@ -415,6 +431,80 @@ NOT change the database." (notmuch-draft-resume id) (message "No message to resume!")))) +(defun notmuch-tree-find-overlay (buffer start end) + "Return the first overlay found in `notmuch-tree-overlays'. + +The overlay found is located between START and END position in BUFFER." + (cl-find-if (lambda (ov) + (and (eq (overlay-buffer ov) buffer) + (<= (overlay-start ov) start) + (>= (overlay-end ov) end))) + notmuch-tree-overlays)) + +(defun notmuch-tree-clean-up-overlays () + "Remove overlays not referenced to any buffer" + (setq notmuch-tree-overlays (cl-remove-if #'overlay-buffer notmuch-tree-overlays))) + +(defun notmuch-tree-remove-overlay (overlay) + "Delete OVERLAY and remove it from `notmuch-tree-overlays' list" + (setq notmuch-tree-overlays (remove overlay notmuch-tree-overlays)) + (delete-overlay overlay)) + +(defun notmuch-tree-add-overlay (start end) + "Add an overlay from START to END in the current buffer. + +If non nil, `notmuch-tree-overlay-string' is added at the end of the line. +The overlay created is added to `notmuch-tree-overlays' list" + (let ((overlay (make-overlay start end))) + (add-to-list 'notmuch-tree-overlays overlay) + (overlay-put overlay 'invisible t) + (when notmuch-tree-overlay-string + (overlay-put overlay 'before-string + (propertize notmuch-tree-overlay-string + 'face 'notmuch-tree-overlay-fold-face))))) + +(defun notmuch-tree-thread-range () + "Return list of Start and End position of the current thread" + (let (start end) + (save-excursion + (while (not (or (notmuch-tree-get-prop :first) (eobp))) + (forward-line -1)) + (setq start (line-end-position)) + (notmuch-tree-next-thread) + (setq end (- (point) 1)) + (list start end)))) + +(defun notmuch-tree-sub-thread-range () + "Return list of Start and End position of the current sub-thread" + (if (notmuch-tree-get-prop :first) + (notmuch-tree-thread-range) + (let ((level (length (notmuch-tree-get-prop :tree-status))) + (start (line-end-position)) + end) + ;; find end position + (save-excursion + (forward-line) + (while (and (< level (length (notmuch-tree-get-prop :tree-status))) + (not (eobp))) + (forward-line)) + (setq end (- (point) 1))) + (list start end)))) + +(defun notmuch-tree-toggle-folding-thread (&optional arg) + "Fold / Unfold the current thread or sub-thread. + +With prefix arg (C-u) the whole thread is folded" + (interactive "p") + (cl-multiple-value-bind (start end) + (if (and arg (= arg 1)) + (notmuch-tree-sub-thread-range) + (notmuch-tree-thread-range)) + (unless (= start end) + (let ((overlay (notmuch-tree-find-overlay (current-buffer) start end))) + (if overlay + (notmuch-tree-remove-overlay overlay) + (notmuch-tree-add-overlay start end)))))) + ;; The next two functions close the message window before calling ;; notmuch-search or notmuch-tree but they do so after the user has ;; entered the query (in case the user was basing the query on @@ -966,6 +1056,9 @@ The arguments are: ;; Don't track undo information for this buffer (set 'buffer-undo-list t) + ;; clean-up overlays in case where some overlays reference to no buffer + (notmuch-tree-clean-up-overlays) + (notmuch-tree-worker query query-context target open-target) (setq truncate-lines t)) -- 2.7.4