/
2017-05-06-Enhanced-beginning--and-end-of-buffer-in-special-mode-buffers-(dired-etc.).html
272 lines (246 loc) · 20.4 KB
/
2017-05-06-Enhanced-beginning--and-end-of-buffer-in-special-mode-buffers-(dired-etc.).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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<?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>Enhanced beginning- and end-of-buffer in special mode buffers (dired etc.)</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">Enhanced beginning- and end-of-buffer in special mode buffers (dired etc.)</h1>
<p>
Two very useful commands are <code>beginning-of-buffer</code> (by default on <code>M-<</code>) and <code>end-of-buffer</code> (<code>M-></code>). They do exactly what they advertise: move the point (= jump) to the beginning or end of current buffer.
</p>
<p>
This works fine in buffers with text like buffers holding code (<code>emacs-lisp-mode</code>, <code>c-mode</code>…) or text (<code>org-mode</code>, <code>markdown-mode</code>…) because it does exactly what you want.
</p>
<p>
One of the beautiful and most clever things in Emacs design to me is that <b>every</b> buffer is just text. What this means is you can use all the multitude of functions that work on text to also navigate forms, special listings, tables et cetera. Indeed, the most infuriating feeling when I use something which isn't Emacs, e.g. a GUI program with a table in it, is the inability to just search the contents of the UI: there either is a special search for each control or you simply can't do it at all. Very annoying.
</p>
<p>
However, back in Emacs not everything is perfect :). When you are in special buffers like <code>dired</code>, <code>vc-dir</code> or <code>occur</code> it makes little sense for <code>beginning-of-buffer</code> to jump to the <i>actual</i> beginning. What you want 99% of the time is to go to the first logical entry, be it the first file or the first result of search.
</p>
<p>
Well, nothing is simpler than to just write a macro that would generate these "better" navigation functions and remap the default functions to the new variants, right?
</p>
<p>
The following two macros do just that. Basically, they wrap the <code>beginning-of-buffer</code> functionality by first going to the real beginning and then invoke provided forms to move the point to the logical beginning. Almost every special mode provides some sort of <code>...-next-line</code> or <code>...-next-item</code> function you can use to move from the beginning to the first logical item.
</p>
<p>
One clever additional functionality is that if the point already is at the logical beginning and you invoke the function again it will actually jump to the real beginning. Similarly it works for <code>end-of-buffer</code>. This adds a little bit of <i>dwim</i> flavoring to the commands.
</p>
<div class="org-src-container">
<pre class="src src-elisp"><span style="color: #888a85;">(</span><span style="color: #b4fa70;">defmacro</span> <span style="color: #fce94f;">my-special-beginning-of-buffer</span> <span style="color: #888a85;">(</span>mode <span style="color: #8cc4ff;">&rest</span> forms<span style="color: #888a85;">)</span>
<span style="color: #e9b96e;">"Define a special version of `</span><span style="color: #e6a8df;">beginning-of-buffer</span><span style="color: #e9b96e;">' in MODE.</span>
<span style="color: #e9b96e;">The special function is defined such that the point first moves</span>
<span style="color: #e9b96e;">to `</span><span style="color: #e6a8df;">point-min</span><span style="color: #e9b96e;">' and then FORMS are evaluated. If the point did</span>
<span style="color: #e9b96e;">not change because of the evaluation of FORMS, jump</span>
<span style="color: #e9b96e;">unconditionally to `</span><span style="color: #e6a8df;">point-min</span><span style="color: #e9b96e;">'. This way repeated invocations</span>
<span style="color: #e9b96e;">toggle between real beginning and logical beginning of the</span>
<span style="color: #e9b96e;">buffer."</span>
<span style="color: #888a85;">(</span><span style="color: #b4fa70;">declare</span> <span style="color: #888a85;">(</span>indent 1<span style="color: #888a85;">))</span>
<span style="color: #888a85;">(</span><span style="color: #b4fa70;">let</span> <span style="color: #888a85;">((</span>fname <span style="color: #888a85;">(</span>intern <span style="color: #888a85;">(</span>concat <span style="color: #e9b96e;">"my-"</span> <span style="color: #888a85;">(</span>symbol-name mode<span style="color: #888a85;">)</span> <span style="color: #e9b96e;">"-beginning-of-buffer"</span><span style="color: #888a85;">)))</span>
<span style="color: #888a85;">(</span>mode-map <span style="color: #888a85;">(</span>intern <span style="color: #888a85;">(</span>concat <span style="color: #888a85;">(</span>symbol-name mode<span style="color: #888a85;">)</span> <span style="color: #e9b96e;">"-mode-map"</span><span style="color: #888a85;">)))</span>
<span style="color: #888a85;">(</span>mode-hook <span style="color: #888a85;">(</span>intern <span style="color: #888a85;">(</span>concat <span style="color: #888a85;">(</span>symbol-name mode<span style="color: #888a85;">)</span> <span style="color: #e9b96e;">"-mode-hook"</span><span style="color: #888a85;">))))</span>
`<span style="color: #888a85;">(</span><span style="color: #b4fa70;">progn</span>
<span style="color: #888a85;">(</span><span style="color: #b4fa70;">defun</span> ,fname <span style="color: #888a85;">()</span>
<span style="color: #888a85;">(</span><span style="color: #b4fa70;">interactive</span><span style="color: #888a85;">)</span>
<span style="color: #888a85;">(</span><span style="color: #b4fa70;">let</span> <span style="color: #888a85;">((</span>p <span style="color: #888a85;">(</span>point<span style="color: #888a85;">)))</span>
<span style="color: #888a85;">(</span>goto-char <span style="color: #888a85;">(</span>point-min<span style="color: #888a85;">))</span>
,@forms
<span style="color: #888a85;">(</span><span style="color: #b4fa70;">when</span> <span style="color: #888a85;">(</span>= p <span style="color: #888a85;">(</span>point<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>add-hook ',mode-hook
<span style="color: #888a85;">(</span><span style="color: #b4fa70;">lambda</span> <span style="color: #888a85;">()</span>
<span style="color: #888a85;">(</span>define-key ,mode-map
[remap beginning-of-buffer] ',fname<span style="color: #888a85;">))))))</span>
</pre>
</div>
<p>
And a corresponding version for <code>end-of-buffer</code>.
</p>
<div class="org-src-container">
<pre class="src src-elisp"><span style="color: #888a85;">(</span><span style="color: #b4fa70;">defmacro</span> <span style="color: #fce94f;">my-special-end-of-buffer</span> <span style="color: #888a85;">(</span>mode <span style="color: #8cc4ff;">&rest</span> forms<span style="color: #888a85;">)</span>
<span style="color: #e9b96e;">"Define a special version of `</span><span style="color: #e6a8df;">end-of-buffer</span><span style="color: #e9b96e;">' in MODE.</span>
<span style="color: #e9b96e;">The special function is defined such that the point first moves</span>
<span style="color: #e9b96e;">to `</span><span style="color: #e6a8df;">point-max</span><span style="color: #e9b96e;">' and then FORMS are evaluated. If the point did</span>
<span style="color: #e9b96e;">not change because of the evaluation of FORMS, jump</span>
<span style="color: #e9b96e;">unconditionally to `</span><span style="color: #e6a8df;">point-max</span><span style="color: #e9b96e;">'. This way repeated invocations</span>
<span style="color: #e9b96e;">toggle between real end and logical end of the buffer."</span>
<span style="color: #888a85;">(</span><span style="color: #b4fa70;">declare</span> <span style="color: #888a85;">(</span>indent 1<span style="color: #888a85;">))</span>
<span style="color: #888a85;">(</span><span style="color: #b4fa70;">let</span> <span style="color: #888a85;">((</span>fname <span style="color: #888a85;">(</span>intern <span style="color: #888a85;">(</span>concat <span style="color: #e9b96e;">"my-"</span> <span style="color: #888a85;">(</span>symbol-name mode<span style="color: #888a85;">)</span> <span style="color: #e9b96e;">"-end-of-buffer"</span><span style="color: #888a85;">)))</span>
<span style="color: #888a85;">(</span>mode-map <span style="color: #888a85;">(</span>intern <span style="color: #888a85;">(</span>concat <span style="color: #888a85;">(</span>symbol-name mode<span style="color: #888a85;">)</span> <span style="color: #e9b96e;">"-mode-map"</span><span style="color: #888a85;">)))</span>
<span style="color: #888a85;">(</span>mode-hook <span style="color: #888a85;">(</span>intern <span style="color: #888a85;">(</span>concat <span style="color: #888a85;">(</span>symbol-name mode<span style="color: #888a85;">)</span> <span style="color: #e9b96e;">"-mode-hook"</span><span style="color: #888a85;">))))</span>
`<span style="color: #888a85;">(</span><span style="color: #b4fa70;">progn</span>
<span style="color: #888a85;">(</span><span style="color: #b4fa70;">defun</span> ,fname <span style="color: #888a85;">()</span>
<span style="color: #888a85;">(</span><span style="color: #b4fa70;">interactive</span><span style="color: #888a85;">)</span>
<span style="color: #888a85;">(</span><span style="color: #b4fa70;">let</span> <span style="color: #888a85;">((</span>p <span style="color: #888a85;">(</span>point<span style="color: #888a85;">)))</span>
<span style="color: #888a85;">(</span>goto-char <span style="color: #888a85;">(</span>point-max<span style="color: #888a85;">))</span>
,@forms
<span style="color: #888a85;">(</span><span style="color: #b4fa70;">when</span> <span style="color: #888a85;">(</span>= p <span style="color: #888a85;">(</span>point<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>add-hook ',mode-hook
<span style="color: #888a85;">(</span><span style="color: #b4fa70;">lambda</span> <span style="color: #888a85;">()</span>
<span style="color: #888a85;">(</span>define-key ,mode-map
[remap end-of-buffer] ',fname<span style="color: #888a85;">))))))</span>
</pre>
</div>
<p>
The key remapping works by remapping the <a href="https://www.gnu.org/software/emacs/manual/html_node/elisp/Remapping-Commands.html">command</a>; this is an amazing Emacs feature many people aren't aware of. What this means in short is that you do not bind a key to the command but rather remap a command to another command. That is, whatever key you bound to <code>beginning-of-buffer</code> will now instead invoke the special function generated above. When you remap <code>beginning-of-buffer</code> somewhere globally all the special functions will "automagically" remap as well.
</p>
<p>
The macros rely on the Emacs convention where the mode name ends in <code>-mode</code> and the map ends in <code>-mode-map</code>, so far it worked everywhere (but it wouldn't be difficult to change the macro to accept those as arguments—left as an exercise to the reader).
</p>
<p>
What follows is all the "overloads" I use in my config, you can use these as an inspiration to add your own. I'm thinking of how to package this code and provide some simple interface for people to enable these in their configs and contribute more for various other special buffers. If you want to help out with the packaging leave me a <a href="https://github.com/Fuco1/.emacs.d/issues/new">message</a>.
</p>
<div id="outline-container-orge4ddd96" class="outline-2">
<h2 id="orge4ddd96">Dired (<code>M-x dired</code>)</h2>
<div class="outline-text-2" id="text-orge4ddd96">
<div class="org-src-container">
<pre class="src src-elisp"><span style="color: #888a85;">(</span><span style="color: #b4fa70;">my-special-beginning-of-buffer</span> dired
<span style="color: #888a85;">(</span><span style="color: #b4fa70;">while</span> <span style="color: #888a85;">(</span>not <span style="color: #888a85;">(</span><span style="color: #b4fa70;">ignore-errors</span> <span style="color: #888a85;">(</span>dired-get-filename<span style="color: #888a85;">)))</span>
<span style="color: #888a85;">(</span>dired-next-line 1<span style="color: #888a85;">)))</span>
<span style="color: #888a85;">(</span><span style="color: #b4fa70;">my-special-end-of-buffer</span> dired
<span style="color: #888a85;">(</span>dired-previous-line 1<span style="color: #888a85;">))</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-orgcc5b3f4" class="outline-2">
<h2 id="orgcc5b3f4">Occur (<code>M-x occur</code>)</h2>
<div class="outline-text-2" id="text-orgcc5b3f4">
<div class="org-src-container">
<pre class="src src-elisp"><span style="color: #888a85;">(</span><span style="color: #b4fa70;">my-special-beginning-of-buffer</span> occur
<span style="color: #888a85;">(</span>occur-next 1<span style="color: #888a85;">))</span>
<span style="color: #888a85;">(</span><span style="color: #b4fa70;">my-special-end-of-buffer</span> occur
<span style="color: #888a85;">(</span>occur-prev 1<span style="color: #888a85;">))</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org8c62bfb" class="outline-2">
<h2 id="org8c62bfb">Ibuffer (<code>M-x ibuffer</code>)</h2>
<div class="outline-text-2" id="text-org8c62bfb">
<div class="org-src-container">
<pre class="src src-elisp"><span style="color: #888a85;">(</span><span style="color: #b4fa70;">my-special-beginning-of-buffer</span> ibuffer
<span style="color: #888a85;">(</span>ibuffer-forward-line 1<span style="color: #888a85;">))</span>
<span style="color: #888a85;">(</span><span style="color: #b4fa70;">my-special-end-of-buffer</span> ibuffer
<span style="color: #888a85;">(</span>ibuffer-backward-line 1<span style="color: #888a85;">))</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org5c04633" class="outline-2">
<h2 id="org5c04633">vc directory view (<code>M-x vc-dir</code> or <code>C-x v d</code>)</h2>
<div class="outline-text-2" id="text-org5c04633">
<div class="org-src-container">
<pre class="src src-elisp"><span style="color: #888a85;">(</span><span style="color: #b4fa70;">my-special-beginning-of-buffer</span> vc-dir
<span style="color: #888a85;">(</span>vc-dir-next-line 1<span style="color: #888a85;">))</span>
<span style="color: #888a85;">(</span><span style="color: #b4fa70;">my-special-end-of-buffer</span> vc-dir
<span style="color: #888a85;">(</span>vc-dir-previous-line 1<span style="color: #888a85;">))</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org67bd14b" class="outline-2">
<h2 id="org67bd14b">bs (<code>M-x bs-show</code>)</h2>
<div class="outline-text-2" id="text-org67bd14b">
<div class="org-src-container">
<pre class="src src-elisp"><span style="color: #888a85;">(</span><span style="color: #b4fa70;">my-special-beginning-of-buffer</span> bs
<span style="color: #888a85;">(</span>bs-down 2<span style="color: #888a85;">))</span>
<span style="color: #888a85;">(</span><span style="color: #b4fa70;">my-special-end-of-buffer</span> bs
<span style="color: #888a85;">(</span>bs-up 1<span style="color: #888a85;">)</span>
<span style="color: #888a85;">(</span>bs-down 1<span style="color: #888a85;">))</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org4408714" class="outline-2">
<h2 id="org4408714">Recentf (<code>M-x recentf-open-files</code>)</h2>
<div class="outline-text-2" id="text-org4408714">
<div class="org-src-container">
<pre class="src src-elisp"><span style="color: #888a85;">(</span><span style="color: #b4fa70;">my-special-beginning-of-buffer</span> recentf-dialog
<span style="color: #888a85;">(</span><span style="color: #b4fa70;">when</span> <span style="color: #888a85;">(</span>re-search-forward <span style="color: #e9b96e;">"^ \\["</span> nil t<span style="color: #888a85;">)</span>
<span style="color: #888a85;">(</span>goto-char <span style="color: #888a85;">(</span>match-beginning 0<span style="color: #888a85;">))))</span>
<span style="color: #888a85;">(</span><span style="color: #b4fa70;">my-special-end-of-buffer</span> recentf-dialog
<span style="color: #888a85;">(</span>re-search-backward <span style="color: #e9b96e;">"^ \\["</span> nil t<span style="color: #888a85;">))</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org5ae2372" class="outline-2">
<h2 id="org5ae2372">Org Agenda (<code>M-x org-agenda</code>)</h2>
<div class="outline-text-2" id="text-org5ae2372">
<div class="org-src-container">
<pre class="src src-elisp"><span style="color: #888a85;">(</span><span style="color: #b4fa70;">my-special-beginning-of-buffer</span> org-agenda
<span style="color: #888a85;">(</span>org-agenda-next-item 1<span style="color: #888a85;">))</span>
<span style="color: #888a85;">(</span><span style="color: #b4fa70;">my-special-end-of-buffer</span> org-agenda
<span style="color: #888a85;">(</span>org-agenda-previous-item 1<span style="color: #888a85;">))</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org1f5cc33" class="outline-2">
<h2 id="org1f5cc33">ag (from <code>ag.el</code> package, <code>M-x ag</code>)</h2>
<div class="outline-text-2" id="text-org1f5cc33">
<div class="org-src-container">
<pre class="src src-elisp"><span style="color: #888a85;">(</span><span style="color: #b4fa70;">my-special-beginning-of-buffer</span> ag
<span style="color: #888a85;">(</span>compilation-next-error 1<span style="color: #888a85;">))</span>
<span style="color: #888a85;">(</span><span style="color: #b4fa70;">my-special-end-of-buffer</span> ag
<span style="color: #888a85;">(</span>compilation-previous-error 1<span style="color: #888a85;">))</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-orgddf8341" class="outline-2">
<h2 id="orgddf8341">Notmuch (from <code>notmuch</code> package, <code>M-x notmuch-search</code>)</h2>
<div class="outline-text-2" id="text-orgddf8341">
<div class="org-src-container">
<pre class="src src-elisp"><span style="color: #888a85;">(</span><span style="color: #b4fa70;">my-special-beginning-of-buffer</span> notmuch-search
<span style="color: #888a85;">(</span>notmuch-search-first-thread<span style="color: #888a85;">)</span>
<span style="color: #888a85;">(</span>beginning-of-line<span style="color: #888a85;">))</span>
<span style="color: #888a85;">(</span><span style="color: #b4fa70;">my-special-end-of-buffer</span> notmuch-search
<span style="color: #888a85;">(</span>notmuch-search-last-thread<span style="color: #888a85;">)</span>
<span style="color: #888a85;">(</span>end-of-line<span style="color: #888a85;">))</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org44e3452" class="outline-2">
<h2 id="org44e3452">Elfeed (from <code>elfeed</code> package, <code>M-x elfeed</code>)</h2>
<div class="outline-text-2" id="text-org44e3452">
<div class="org-src-container">
<pre class="src src-elisp"><span style="color: #888a85;">(</span><span style="color: #b4fa70;">my-special-end-of-buffer</span> elfeed-search
<span style="color: #888a85;">(</span>forward-line -2<span style="color: #888a85;">))</span>
</pre>
</div>
</div>
</div>
</div>
<div id="postamble" class="status">
<hr />
<div style="text-align: left; float: left;">Published at: 2017-05-06 16:16 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-05-06-Enhanced-beginning--and-end-of-buffer-in-special-mode-buffers-(dired-etc.).org">Edit on GitHub!</a></div>
</div>
</body>
</html>