From mboxrd@z Thu Jan 1 00:00:00 1970 From: Suvayu Ali Subject: [PATCH] Implement history facility for org-occur searches Date: Wed, 24 Aug 2011 01:47:39 +0200 Message-ID: <20110824014739.7b7f230c@kuru.homelinux.net> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="MP_/em4kIpukdeEypXtdXdSna9W" Return-path: Received: from eggs.gnu.org ([140.186.70.92]:48167) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Qw0h7-0008Lr-4C for emacs-orgmode@gnu.org; Tue, 23 Aug 2011 19:47:46 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Qw0h5-00027F-Rp for emacs-orgmode@gnu.org; Tue, 23 Aug 2011 19:47:45 -0400 Received: from mail-ew0-f41.google.com ([209.85.215.41]:33392) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Qw0h5-00026r-K7 for emacs-orgmode@gnu.org; Tue, 23 Aug 2011 19:47:43 -0400 Received: by ewy9 with SMTP id 9so325395ewy.0 for ; Tue, 23 Aug 2011 16:47:42 -0700 (PDT) List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: Org mode mailing list --MP_/em4kIpukdeEypXtdXdSna9W Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Content-Disposition: inline Hi Bastien and everyone, I wanted to implement a history facility for sparse trees. Since sparse trees use org-occur and org-scan-tags any such facility would need to be aware of both functions. My lisp foo proved to be too weak to understand org-scan-tags, so I implemented this only for org-occur searches. With this patch one can traverse the sparse tree history for date, regular expression and TODO keyword searches with the org-occur-history-forward and org-occur-history-backward functions. One can also call the org-occur-history-next function interactively to jump to the nth history entry. This should also work for any other searches that uses org-occur as the backend. Please feel free to modify the patch if there are better ways to implement this facility. Thank you. -- Suvayu Open source is the future. It sets us free. --MP_/em4kIpukdeEypXtdXdSna9W Content-Type: text/x-patch Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename=0001-Implement-history-facility-for-org-occur-searches.patch >From 11c36f42d1471629fd783aaf1db4aaf3d1d01ef6 Mon Sep 17 00:00:00 2001 From: Suvayu Ali Date: Tue, 23 Aug 2011 14:25:08 +0200 Subject: [PATCH] Implement history facility for org-occur searches Changed function: * org.el (org-occur): Add history functionality and corresponding documentation New functions: * org.el (org-occur-history-next): Function for history traversal (org-occur-history-forward) and (org-occur-history-backward): User funtions for moving forward and backward in search history New variables: * org.el (org-occur-match-history): Search history variable (org-occur-match-history-len): Search history size --- lisp/org.el | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 63 insertions(+), 2 deletions(-) diff --git a/lisp/org.el b/lisp/org.el index bbc6a75..d419a04 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -12294,6 +12294,15 @@ (defvar org-occur-parameters nil as well.") (make-variable-buffer-local 'org-occur-parameters) +;;;; Org occur history variables +(defvar org-occur-match-history nil + "History list of `org-occur' searches.") +(make-variable-buffer-local 'org-occur-match-history) +(defcustom org-occur-match-history-len 10 + "Length of `org-occur-match-history'." + :type 'integer + :group 'org) + (defun org-occur (regexp &optional keep-previous callback) "Make a compact tree which shows all matches of REGEXP. The tree will show the lines where the regexp matches, and all higher @@ -12303,7 +12312,15 @@ (defun org-occur (regexp &optional keep-previous callback) call to `org-occur' will be kept, to allow stacking of calls to this command. If CALLBACK is non-nil, it is a function which is called to confirm -that the match should indeed be shown." +that the match should indeed be shown. + +The org-occur search history is stored in the variable +`org-occur-match-history'. The length of the history is determined by the +variable `org-occur-match-history-len'. The function +`org-occur-history-next' can be used to traverse the org-occur history. +Two user functions `org-occur-history-forward' and +`org-occur-history-backward' are provided to conveniently traverse the +org-occur history one step at a time." (interactive "sRegexp: \nP") (when (equal regexp "") (error "Regexp cannot be empty")) @@ -12322,7 +12339,17 @@ (defun org-occur (regexp &optional keep-previous callback) (save-match-data (funcall callback))) (setq cnt (1+ cnt)) (when org-highlight-sparse-tree-matches - (org-highlight-new-match (match-beginning 0) (match-end 0))) + (org-highlight-new-match (match-beginning 0) (match-end 0)) + ;; remove duplicate element + (setq org-occur-match-history + (delete regexp org-occur-match-history)) + ;; add last search at the front + (add-to-list 'org-occur-match-history regexp) + ;; curtail history to max history length + (if (eq org-occur-match-history-len + (length org-occur-match-history)) + (setq org-occur-match-history + (butlast org-occur-match-history)))) (org-show-context 'occur-tree)))) (when org-remove-highlights-with-change (org-add-hook 'before-change-functions 'org-remove-occur-highlights @@ -12360,6 +12387,40 @@ (defun org-occur-next-match (&optional n reset) (goto-char p1) (error "No more matches")))) +(defun org-occur-history-next (steps) + "Traverse org occur history by STEPS steps in the forward direction +with respect to the current position. The search history is saved after +removing any duplicate searches. It is reordered to reflect the most +recent search. Before traversing the history the following transformation +is applied: STEPS MODULO `org-occur-match-history-len'. + +For negative STEPS, history traversal is done in the backward direction +starting at the end of the history list. When called interactively it +prompts for the number of steps." + (interactive "NSteps: ") + (let (match) + (if (< steps 0) + (setq match + (car (last org-occur-match-history + (mod (- steps) org-occur-match-history-len)))) + (setq match (nth (mod steps org-occur-match-history-len) + org-occur-match-history))) + (org-occur match) + (setq org-occur-match-history + (delete match org-occur-match-history)) + (add-to-list 'org-occur-match-history match t))) + +(defun org-occur-history-forward () + "Repeat last org occur search." + (interactive) + (let ((steps (if org-occur-highlights 1 0))) + (org-occur-history-next steps))) + +(defun org-occur-history-backward () + "Repeat last org occur search." + (interactive) + (org-occur-history-next -1)) + (defun org-show-context (&optional key) "Make sure point and context are visible. How much context is shown depends upon the variables -- 1.7.4.4 --MP_/em4kIpukdeEypXtdXdSna9W--