emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH] Display a count of items next to each list (or block)
@ 2013-09-25 20:09 Sebastien Vauban
  2013-09-26  7:34 ` Achim Gratz
  2013-09-28 15:35 ` Bernt Hansen
  0 siblings, 2 replies; 6+ messages in thread
From: Sebastien Vauban @ 2013-09-25 20:09 UTC (permalink / raw)
  To: emacs-orgmode-mXXj517/zsQ

Hello,

In order to make Org much nicer to use, I felt we missed a count of items next
to the lists (or blocks, for multi-block agenda views). Here is a patch to add
this, depending on the new variable `org-agenda-display-count-of-items'
(enabled by default).

The count of items must be updated when you apply tag filtering on lists. The
patch does it as well.

Please enjoy (or be scared by the real number of items you have on your TODO
lists)!!

Best regards,
  Seb

From: "Sebastien Vauban" <sva-news-D0wtAvR13HarG/iDocfnWg@public.gmane.org>
Date: Wed, 25 Sep 2013 21:56:01 +0200
Subject: [PATCH] Display a count of items next to each list (or block)

* org-agenda.el (org-agenda-display-count-of-items): New variable.
	(org-agenda-insert-count-of-items)
	(org-agenda-count-visible-lines-block)
	(org-agenda-remove-filtered-count): New helper functions.
	(org-search-view, org-todo-list, org-tags-view): Add count of
	items.
	(org-agenda-filter-by-tag, org-agenda-filter-apply): Add or
	remove filtered count of items.
	(org-agenda-goto-block-beginning): Fix problems for position
	of point.
	(org-agenda-goto-next-block): New command.

---
 lisp/org-agenda.el |   89 ++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 83 insertions(+), 6 deletions(-)

diff --git a/lisp/org-agenda.el b/lisp/org-agenda.el
index c48da91..336991f 100644
--- a/lisp/org-agenda.el
+++ b/lisp/org-agenda.el
@@ -4652,6 +4652,8 @@ in `org-agenda-text-search-extra-files'."
       (when rtnall
 	(insert (org-agenda-finalize-entries rtnall 'search) "\n"))
       (goto-char (point-min))
+      (when org-agenda-display-count-of-items
+	(org-agenda-insert-count-of-items rtnall))
       (or org-agenda-multi (org-agenda-fit-window-to-buffer))
       (add-text-properties (point-min) (point-max)
 			   `(org-agenda-type search
@@ -4663,6 +4665,46 @@ in `org-agenda-text-search-extra-files'."
 
 ;;; Agenda TODO list
 
+(defvar org-agenda-display-count-of-items t
+  "Display count of items next to each list.")
+
+(defun org-agenda-insert-count-of-items (count)
+  "Insert count of items at the end of current line."
+  (save-excursion
+    (end-of-line)
+    (insert " "
+	    (org-add-props (format "(%s)" (length rtnall)) nil
+	      'face 'org-agenda-block-count))))
+
+(defun org-agenda-count-visible-lines-block ()
+  "Count the number of items visible in the current block."
+  (interactive)
+  (let ((count 0))
+    (save-excursion
+      (org-agenda-goto-block-beginning)
+      (while (equal (get-char-property (point) 'face) 'org-agenda-structure) ; header line(s)
+	(forward-visible-line 1))
+      (while (or (equal (get-char-property (point) 'face) 'default)
+		 (equal (get-char-property (point) 'type) "tagsmatch")
+		 (equal (get-char-property (point) 'type) "todo")) ; entry line
+	(unless (get-char-property (point) 'invisible)
+	  (setq count (1+ count)))
+	(forward-visible-line 1))
+      count)))
+
+(defun org-agenda-remove-filtered-count ()
+  "Remove `X/' from filtered count string `(X/Y)'.
+
+Leaves point at total count."
+  (org-agenda-goto-block-beginning)
+  (while (not (equal (get-text-property (point) 'face)
+		     'org-agenda-block-count))
+    (forward-char))
+  (forward-char)                        ; for "("
+  (when (looking-at "[0-9]*/")
+    (kill-word 1)                       ; digits
+    (delete-char 1)))                   ; slash
+
 (defun org-agenda-propertize-selected-todo-keywords (keywords)
   "Use `org-todo-keyword-faces' for the selected todo KEYWORDS."
   (concat
@@ -4753,6 +4795,8 @@ for a keyword.  A numeric prefix directly selects the Nth keyword in
       (when rtnall
 	(insert (org-agenda-finalize-entries rtnall 'todo) "\n"))
       (goto-char (point-min))
+      (when org-agenda-display-count-of-items
+	(org-agenda-insert-count-of-items rtnall))
       (or org-agenda-multi (org-agenda-fit-window-to-buffer))
       (add-text-properties (point-min) (point-max)
 			   `(org-agenda-type todo
@@ -4840,6 +4884,8 @@ The prefix arg TODO-ONLY limits the search to TODO entries."
       (when rtnall
 	(insert (org-agenda-finalize-entries rtnall 'tags) "\n"))
       (goto-char (point-min))
+      (when org-agenda-display-count-of-items
+	(org-agenda-insert-count-of-items rtnall))
       (or org-agenda-multi (org-agenda-fit-window-to-buffer))
       (add-text-properties (point-min) (point-max)
 			   `(org-agenda-type tags
@@ -7448,6 +7494,13 @@ to switch to narrowing."
       (org-agenda-filter-show-all-tag)
       (when (get 'org-agenda-tag-filter :preset-filter)
 	(org-agenda-filter-apply org-agenda-tag-filter 'tag))
+      ;; count of items
+      (when org-agenda-display-count-of-items
+	(save-excursion
+	  (goto-char (point-min)) ; beginning of first block
+	  (while (or (equal (point) (point-min))
+		     (ignore-errors (org-agenda-goto-next-block)))
+	    (org-agenda-remove-filtered-count))))
       (setq maybe-refresh t))
      ((equal char ?. )
       (setq org-agenda-tag-filter
@@ -7613,12 +7666,21 @@ When NO-OPERATOR is non-nil, do not add the + operator to returned tags."
 				   (org-get-at-bol 'tags)))
 		    cat (get-text-property (point) 'org-category)
 		    txt (get-text-property (point) 'txt))
-	      (if (not (eval org-agenda-filter-form))
-		  (org-agenda-filter-hide-line type))
+	      (when (not (eval org-agenda-filter-form))
+		(org-agenda-filter-hide-line type))
 	      (beginning-of-line 2))
 	  (beginning-of-line 2))))
     (if (get-char-property (point) 'invisible)
-	(ignore-errors (org-agenda-previous-line)))))
+	(ignore-errors (org-agenda-previous-line)))
+    ;; count of items
+    (when org-agenda-display-count-of-items
+      (save-excursion
+	(goto-char (point-min)) ; beginning of first block
+	(while (or (equal (point) (point-min))
+		   (ignore-errors (org-agenda-goto-next-block)))
+	  (org-agenda-remove-filtered-count)
+	  (insert (org-add-props (format "%s/" (org-agenda-count-visible-lines-block)) nil
+		    'face 'warning)))))))
 
 (defun org-agenda-filter-top-headline-apply (hl &optional negative)
   "Filter by top headline HL."
@@ -7803,15 +7865,30 @@ Negative selection means regexp must not match for selection of an entry."
 	(unless (looking-at "\\'")
 	  (forward-char))
 	(let* ((prop 'org-agenda-structural-header)
-	       (p (previous-single-property-change (point) prop))
+	       (p (or (previous-single-property-change (point) prop) 1))
+	       ;; fix for `C-c a t' when on first char of buffer
 	       (n (next-single-property-change (or (and (looking-at "\\`") 1)
 						   (1- (point))) prop)))
-	  (setq dest (cond ((eq n (point-at-eol)) (1- n)) (p (1- p))))))
+	  (setq dest (cond ((eq n (point-at-eol)) (1- n)) (p p)))))
       (if (not dest)
-	  (error "Cannot find the beginning of the blog")
+	  (error "Cannot find the beginning of the block")
 	(goto-char dest)
 	(move-beginning-of-line 1)))))
 
+(defun org-agenda-goto-next-block ()
+  "Go the next agenda block beginning."
+  (interactive)
+  (if (not (derived-mode-p 'org-agenda-mode))
+      (error "Cannot execute this command outside of org-agenda-mode buffers")
+    (let ((prop 'org-agenda-structural-header) dest)
+      (save-excursion
+	(org-agenda-goto-block-beginning)
+	(forward-visible-line 1)
+	(setq dest (next-single-property-change (point) prop)))
+      (if (not dest)
+	  (error "Cannot find the next block")
+	(goto-char dest)))))
+
 (defun org-agenda-later (arg)
   "Go forward in time by the current span.
 With prefix ARG, go forward that many times the current span."
-- 
1.7.9

^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2013-10-01 16:40 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-09-25 20:09 [PATCH] Display a count of items next to each list (or block) Sebastien Vauban
2013-09-26  7:34 ` Achim Gratz
2013-09-26  7:39   ` Sebastien Vauban
2013-09-28 15:35 ` Bernt Hansen
2013-09-30  9:05   ` Sebastien Vauban
2013-10-01 16:39   ` Bernt Hansen

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).