/
2017-04-20-Archive-subtrees-under-the-same-hierarchy-as-original-in-the-archive-files.html
97 lines (91 loc) · 9.36 KB
/
2017-04-20-Archive-subtrees-under-the-same-hierarchy-as-original-in-the-archive-files.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
<?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 15:59 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Archive subtrees under the same hierarchy as original in the archive files</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">Archive subtrees under the same hierarchy as original in the archive files</h1>
<p>
The way archiving works in <code>org-mode</code> always annoyed me. Specifically the fact that the tree structure gets flattened when you archive your tasks. Sure, there is the property <code>ARCHIVE_OLPATH</code> but it is not as visual as having the tasks in trees. Plus it makes things like clock reports very difficult.
</p>
<p>
If your tree equals a project, clockreport with <code>C-c C-x C-r</code> will sum
the times properly… well not anymore with flattened structure. This works so long as you archive the entire project at once, but I often have projects so huge (e.g. <i>Emacs Configuration</i>) that I archive subtress or parts or just single tasks on a regular basis, with the base project being practically never-ending.
</p>
<p>
Well, no more, I said, and wrote this handy advice that uses the aforementioned property to reconstruct the tree in the archive file. If the parent headings don't exist they are created on-demand. There is one caveat: if you have non-unique headings, this can sometimes file things under wrong tree; the search is done from top to bottom following the hierarchy.
</p>
<div class="org-src-container">
<pre class="src src-elisp"><span style="color: #888a85;">(</span><span style="color: #b4fa70;">defadvice</span> <span style="color: #fce94f;">org-archive-subtree</span> <span style="color: #888a85;">(</span>around fix-hierarchy activate<span style="color: #888a85;">)</span>
<span style="color: #888a85;">(</span><span style="color: #b4fa70;">let*</span> <span style="color: #888a85;">((</span>fix-archive-p <span style="color: #888a85;">(</span><span style="color: #b4fa70;">and</span> <span style="color: #888a85;">(</span>not current-prefix-arg<span style="color: #888a85;">)</span>
<span style="color: #888a85;">(</span>not <span style="color: #888a85;">(</span>use-region-p<span style="color: #888a85;">))))</span>
<span style="color: #888a85;">(</span>afile <span style="color: #888a85;">(</span>org-extract-archive-file <span style="color: #888a85;">(</span>org-get-local-archive-location<span style="color: #888a85;">)))</span>
<span style="color: #888a85;">(</span>buffer <span style="color: #888a85;">(</span><span style="color: #b4fa70;">or</span> <span style="color: #888a85;">(</span>find-buffer-visiting afile<span style="color: #888a85;">)</span> <span style="color: #888a85;">(</span>find-file-noselect afile<span style="color: #888a85;">))))</span>
ad-do-it
<span style="color: #888a85;">(</span><span style="color: #b4fa70;">when</span> fix-archive-p
<span style="color: #888a85;">(</span><span style="color: #b4fa70;">with-current-buffer</span> buffer
<span style="color: #888a85;">(</span>goto-char <span style="color: #888a85;">(</span>point-max<span style="color: #888a85;">))</span>
<span style="color: #888a85;">(</span><span style="color: #b4fa70;">while</span> <span style="color: #888a85;">(</span>org-up-heading-safe<span style="color: #888a85;">))</span>
<span style="color: #888a85;">(</span><span style="color: #b4fa70;">let*</span> <span style="color: #888a85;">((</span>olpath <span style="color: #888a85;">(</span>org-entry-get <span style="color: #888a85;">(</span>point<span style="color: #888a85;">)</span> <span style="color: #e9b96e;">"ARCHIVE_OLPATH"</span><span style="color: #888a85;">))</span>
<span style="color: #888a85;">(</span>path <span style="color: #888a85;">(</span><span style="color: #b4fa70;">and</span> olpath <span style="color: #888a85;">(</span>split-string olpath <span style="color: #e9b96e;">"/"</span><span style="color: #888a85;">)))</span>
<span style="color: #888a85;">(</span>level 1<span style="color: #888a85;">)</span>
tree-text<span style="color: #888a85;">)</span>
<span style="color: #888a85;">(</span><span style="color: #b4fa70;">when</span> olpath
<span style="color: #888a85;">(</span>org-mark-subtree<span style="color: #888a85;">)</span>
<span style="color: #888a85;">(</span><span style="color: #b4fa70;">setq</span> tree-text <span style="color: #888a85;">(</span>buffer-substring <span style="color: #888a85;">(</span>region-beginning<span style="color: #888a85;">)</span> <span style="color: #888a85;">(</span>region-end<span style="color: #888a85;">)))</span>
<span style="color: #888a85;">(</span><span style="color: #b4fa70;">let</span> <span style="color: #888a85;">(</span>this-command<span style="color: #888a85;">)</span> <span style="color: #888a85;">(</span>org-cut-subtree<span style="color: #888a85;">))</span>
<span style="color: #888a85;">(</span>goto-char <span style="color: #888a85;">(</span>point-min<span style="color: #888a85;">))</span>
<span style="color: #888a85;">(</span><span style="color: #b4fa70;">save-restriction</span>
<span style="color: #888a85;">(</span>widen<span style="color: #888a85;">)</span>
<span style="color: #888a85;">(</span>-each path
<span style="color: #888a85;">(</span><span style="color: #b4fa70;">lambda</span> <span style="color: #888a85;">(</span>heading<span style="color: #888a85;">)</span>
<span style="color: #888a85;">(</span><span style="color: #b4fa70;">if</span> <span style="color: #888a85;">(</span>re-search-forward
<span style="color: #888a85;">(</span>rx-to-string
`<span style="color: #888a85;">(</span>: bol <span style="color: #888a85;">(</span>repeat ,level <span style="color: #e9b96e;">"*"</span><span style="color: #888a85;">)</span> <span style="color: #888a85;">(</span>1+ <span style="color: #e9b96e;">" "</span><span style="color: #888a85;">)</span> ,heading<span style="color: #888a85;">))</span> <span style="color: #ff4b4b;">nil t</span><span style="color: #ff4b4b;">)</span>
<span style="color: #888a85;">(</span>org-narrow-to-subtree<span style="color: #888a85;">)</span>
<span style="color: #888a85;">(</span>goto-char <span style="color: #888a85;">(</span>point-max<span style="color: #888a85;">))</span>
<span style="color: #888a85;">(</span><span style="color: #b4fa70;">unless</span> <span style="color: #888a85;">(</span>looking-at <span style="color: #e9b96e;">"^"</span><span style="color: #888a85;">)</span>
<span style="color: #888a85;">(</span>insert <span style="color: #e9b96e;">"\n"</span><span style="color: #888a85;">))</span>
<span style="color: #888a85;">(</span>insert <span style="color: #888a85;">(</span>make-string level ?*<span style="color: #888a85;">)</span>
<span style="color: #e9b96e;">" "</span>
heading
<span style="color: #e9b96e;">"\n"</span><span style="color: #888a85;">))</span>
<span style="color: #888a85;">(</span><span style="color: #b4fa70;">cl-incf</span> level<span style="color: #888a85;">)))</span>
<span style="color: #888a85;">(</span>widen<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>org-paste-subtree level tree-text<span style="color: #888a85;">))))))))</span>
</pre>
</div>
<p>
The code uses the old advice system because I like it better (it reads much better, even if it isn't so powerful), plus I still use Emacs 24. If you test this on new versions and find out it doesn't work, open an <a href="https://github.com/Fuco1/.emacs.d/issues/new">issue</a> or a <a href="https://github.com/Fuco1/.emacs.d/compare">pull request</a>. It also depends on <a href="https://github.com/magnars/dash.el">dash.el</a> so make sure you have that installed.
</p>
</div>
<div id="postamble" class="status">
<hr />
<div style="text-align: left; float: left;">Published at: 2017-04-20 18:51 Last updated at: 2023-02-08 15:59</div>
<div style="text-align: right;">Found a typo? <a href="https://github.com/Fuco1/fuco1.github.io/blob/master/posts/2017-04-20-Archive-subtrees-under-the-same-hierarchy-as-original-in-the-archive-files.org">Edit on GitHub!</a></div>
</div>
</body>
</html>