/
2021-05-29-Restrict-agenda-to-multiple-subtrees.html
124 lines (113 loc) · 8.11 KB
/
2021-05-29-Restrict-agenda-to-multiple-subtrees.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2023-02-08 Wed 16:00 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Restrict agenda to multiple subtrees</title>
<meta name="author" content="Matúš Goljer" />
<meta name="generator" content="Org Mode" />
<link rel="stylesheet" href="css/blog.css" />
</head>
<body>
<div id="preamble" class="status">
<div style="text-align: left; float: left;">
<a href="./index.html">Home</a>
<a href="https://github.com/Fuco1/">GitHub</a>
<a href="https://www.patreon.com/user?u=3282358">Patreon</a>
</div>
<div style="text-align: right;">
<a href="https://github.com/Fuco1/Fuco1.github.io/discussions">Discussions</a>
<a href="https://fuco1.github.io/rss.xml">RSS</a>
<a href="https://twitter.com/Fuco1337">Twitter</a>
</div>
<hr />
</div>
<div id="content" class="content">
<h1 class="title">Restrict agenda to multiple subtrees</h1>
<p>
The function <code>org-agenda-set-restriction-lock</code> is very useful for
speeding up agenda when working on a specific project (implemented as
a file or an Orgmode subtree). Personally, I use two agenda views,
one "quick" with 5 simple sections and one "full" with 10 rather
complicated sections.
</p>
<p>
The quick one lists all the actionable tasks, all the stuck tasks or
notes that need to be processed and refiled. The full one lists all
the tasks from the project, including hierarchical project
dependencies, tasks on hold, bugs, waiting tasks and so on. The full
view takes a lot more processing power and is not useful maybe 80% of
the time when I simply want to find work to do next.
</p>
<p>
For the times when I want to get a complete overview over a project
and do some light management or planning, I use the full agenda view.
</p>
<p>
One thing that kept bothering me was that the only option was to
restrict to a file or a subtree, but nothing in
between<sup><a id="fnr.1" class="footref" href="#fn.1" role="doc-backlink">1</a></sup>, such as a
region spanning multiple subtrees. Since I'm not a huge fan of
nesting headers just for the sake of nesting (flatter structures and
<a href="https://github.com/Fuco1/org-node-graph">graphs</a> are much nicer for organization).
</p>
<p>
Luckily, the function <code>org-agenda-set-restriction-lock</code> is fairly
hackable. It uses overlays and markers for managing the restriction,
so all we need to do is grab the current active region's bounds and
set the org variables appropriately.
</p>
<div class="org-src-container">
<pre class="src src-elisp"><span style="color: #888a85;">(</span><span style="color: #b4fa70;">defun</span> <span style="color: #fce94f;">my-org-agenda-set-restriction-lock</span> <span style="color: #888a85;">(</span>orig-fun <span style="color: #8cc4ff;">&optional</span> type<span style="color: #888a85;">)</span>
<span style="color: #888a85;">(</span><span style="color: #b4fa70;">if</span> <span style="color: #888a85;">(</span>not <span style="color: #888a85;">(</span>use-region-p<span style="color: #888a85;">))</span>
<span style="color: #73d216;">;; </span><span style="color: #73d216;">unless a region is active, use the original function for</span>
<span style="color: #73d216;">;; </span><span style="color: #73d216;">cancel/file/subtree</span>
<span style="color: #888a85;">(</span>funcall orig-fun type<span style="color: #888a85;">)</span>
<span style="color: #73d216;">;; </span><span style="color: #73d216;">here we do approximately the same as subtree except find the</span>
<span style="color: #73d216;">;; </span><span style="color: #73d216;">beginning of subtree at region's beginning and end of subtree</span>
<span style="color: #73d216;">;; </span><span style="color: #73d216;">at region's end (could span multiple subtrees)</span>
<span style="color: #888a85;">(</span><span style="color: #b4fa70;">setq</span> org-agenda-restrict <span style="color: #888a85;">(</span>current-buffer<span style="color: #888a85;">))</span>
<span style="color: #73d216;">;; </span><span style="color: #73d216;">use 'my-region to avoid potential future conflict</span>
<span style="color: #888a85;">(</span><span style="color: #b4fa70;">setq</span> org-agenda-overriding-restriction <span style="color: #e6a8df;">'my-region</span><span style="color: #888a85;">)</span>
<span style="color: #888a85;">(</span>put <span style="color: #e6a8df;">'org-agenda-files</span> <span style="color: #e6a8df;">'org-restrict</span>
<span style="color: #888a85;">(</span>list <span style="color: #888a85;">(</span>buffer-file-name <span style="color: #888a85;">(</span>buffer-base-buffer<span style="color: #888a85;">))))</span>
<span style="color: #888a85;">(</span><span style="color: #b4fa70;">let</span> <span style="color: #888a85;">((</span>beg <span style="color: #888a85;">(</span>region-beginning<span style="color: #888a85;">))</span>
<span style="color: #888a85;">(</span>end <span style="color: #888a85;">(</span>region-end<span style="color: #888a85;">)))</span>
<span style="color: #888a85;">(</span><span style="color: #b4fa70;">save-excursion</span>
<span style="color: #888a85;">(</span>goto-char beg<span style="color: #888a85;">)</span>
<span style="color: #888a85;">(</span>org-back-to-heading t<span style="color: #888a85;">)</span>
<span style="color: #888a85;">(</span><span style="color: #b4fa70;">setq</span> beg <span style="color: #888a85;">(</span>point<span style="color: #888a85;">)))</span>
<span style="color: #888a85;">(</span><span style="color: #b4fa70;">save-excursion</span>
<span style="color: #888a85;">(</span>goto-char end<span style="color: #888a85;">)</span>
<span style="color: #888a85;">(</span>org-end-of-subtree t t<span style="color: #888a85;">)</span>
<span style="color: #888a85;">(</span><span style="color: #b4fa70;">setq</span> end <span style="color: #888a85;">(</span>point<span style="color: #888a85;">)))</span>
<span style="color: #888a85;">(</span>move-overlay org-agenda-restriction-lock-overlay
beg
<span style="color: #888a85;">(</span><span style="color: #b4fa70;">if</span> org-agenda-restriction-lock-highlight-subtree
end
<span style="color: #888a85;">(</span>point-at-eol<span style="color: #888a85;">)))</span>
<span style="color: #888a85;">(</span>move-marker org-agenda-restrict-begin beg<span style="color: #888a85;">)</span>
<span style="color: #888a85;">(</span>move-marker org-agenda-restrict-end end<span style="color: #888a85;">))</span>
<span style="color: #888a85;">(</span>message <span style="color: #e9b96e;">"Locking agenda restriction to region"</span><span style="color: #888a85;">)</span>
<span style="color: #888a85;">(</span>org-agenda-maybe-redo<span style="color: #888a85;">)))</span>
<span style="color: #888a85;">(</span>advice-add <span style="color: #e6a8df;">'org-agenda-set-restriction-lock</span> <span style="color: #ad7fa8;">:around</span> <span style="color: #8cc4ff;">#'my-org-agenda-set-restriction-lock</span><span style="color: #888a85;">)</span>
</pre>
</div>
<div id="footnotes">
<h2 class="footnotes">Footnotes: </h2>
<div id="text-footnotes">
<div class="footdef"><sup><a id="fn.1" class="footnum" href="#fnr.1" role="doc-backlink">1</a></sup> <div class="footpara" role="doc-footnote"><p class="footpara">While it is possible to restrict to a
region from the org-agenda speed dial, I find it quite impractical and
prefer to do the restrictions from the project's buffer</p></div></div>
</div>
</div></div>
<div id="postamble" class="status">
<hr />
<div style="text-align: left; float: left;">Published at: 2021-05-29 15:09 Last updated at: 2023-02-08 16:00</div>
<div style="text-align: right;">Found a typo? <a href="https://github.com/Fuco1/fuco1.github.io/blob/master/posts/2021-05-29-Restrict-agenda-to-multiple-subtrees.org">Edit on GitHub!</a></div>
</div>
</body>
</html>