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