Hi,

Let's say I have an org file structured like this

* Project 1
** Tasks
   foo
** Ideas

* Project 2
** Tasks
   bar

When I search 'foo' I'd like the agenda search-view to display 'Project
1' instead of the closer, but less informative 'Tasks'

The attached patch solves this by adding a configuration parameter that
sets the maximum outline level for the headings displayed in the results
of a search-view. This way, I can have a custom search on the above org
file with org-agenda-search-view-max-outline-level set to 1.

Abdó Roig.

>From 830acc3fe6e7235618df080f7b6878234cb831c7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Abd=C3=B3=20Roig-Maranges?= <abdo.r...@gmail.com>
Date: Wed, 12 Dec 2012 20:52:40 +0100
Subject: [PATCH] set max level to display in search-view results

* lisp/org-agenda.el (org-search-view) Adds the config setting
  org-agenda-search-view-max-outline-level that limits the depth of
  items on an agenda-search-view results. When a match is found,
  displays the innermost outline heading containing the match such
  that its level does not exceed
  org-agenda-search-view-max-outline-level.
---
 lisp/org-agenda.el | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

diff --git a/lisp/org-agenda.el b/lisp/org-agenda.el
index 5343887..0b85b9c 100644
--- a/lisp/org-agenda.el
+++ b/lisp/org-agenda.el
@@ -1320,6 +1320,12 @@ When nil, they may also match part of a word."
   :version "24.1"
   :type 'boolean)
 
+(defcustom org-agenda-search-view-max-outline-level nil
+  "Maximum outline level to display in search view."
+  :group 'org-agenda-search-view
+  :version "24.3"
+  :type 'integer)
+
 (defgroup org-agenda-time-grid nil
   "Options concerning the time grid in the Org-mode Agenda."
   :tag "Org Agenda Time Grid"
@@ -4397,10 +4403,22 @@ in `org-agenda-text-search-extra-files'."
 		    (goto-char (max (point-min) (1- (point))))
 		    (while (re-search-forward regexp nil t)
 		      (org-back-to-heading t)
+		      (while (and org-agenda-search-view-max-outline-level
+			(> (org-reduced-level (org-outline-level)) org-agenda-search-view-max-outline-level)
+			(forward-line -1)
+			(outline-back-to-heading t)))
+
 		      (skip-chars-forward "* ")
 		      (setq beg (point-at-bol)
 			    beg1 (point)
-			    end (progn (outline-next-heading) (point)))
+			    end (progn
+			      (outline-next-heading)
+			      (while (and org-agenda-search-view-max-outline-level
+			        (> (org-reduced-level (org-outline-level)) org-agenda-search-view-max-outline-level)
+				(forward-line 1)
+				(outline-next-heading)))
+			      (point)))
+
 		      (catch :skip
 			(goto-char beg)
 			(org-agenda-skip)
-- 
1.8.0.2

Reply via email to