Re: [PATCH] emacs: wash: make word-wrap bound message width
[notmuch-archives.git] / 2c / f7b2fa9356596df3af1af20b28fdc593c8742b
1 Return-Path: <m.walters@qmul.ac.uk>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5         by olra.theworths.org (Postfix) with ESMTP id 1F649431FC0\r
6         for <notmuch@notmuchmail.org>; Mon, 14 Jul 2014 14:22:24 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: 0.502\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=0.502 tagged_above=-999 required=5\r
12         tests=[DKIM_ADSP_CUSTOM_MED=0.001, FREEMAIL_FROM=0.001,\r
13         NML_ADSP_CUSTOM_MED=1.2, RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
14 Received: from olra.theworths.org ([127.0.0.1])\r
15         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
16         with ESMTP id tq4PVK8H2tz6 for <notmuch@notmuchmail.org>;\r
17         Mon, 14 Jul 2014 14:22:16 -0700 (PDT)\r
18 Received: from mail2.qmul.ac.uk (mail2.qmul.ac.uk [138.37.6.6])\r
19         (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))\r
20         (No client certificate requested)\r
21         by olra.theworths.org (Postfix) with ESMTPS id A5884431FBC\r
22         for <notmuch@notmuchmail.org>; Mon, 14 Jul 2014 14:22:15 -0700 (PDT)\r
23 Received: from smtp.qmul.ac.uk ([138.37.6.40])\r
24         by mail2.qmul.ac.uk with esmtp (Exim 4.71)\r
25         (envelope-from <m.walters@qmul.ac.uk>)\r
26         id 1X6nhH-0007jw-St; Mon, 14 Jul 2014 22:22:09 +0100\r
27 Received: from [194.42.225.51] (helo=localhost)\r
28         by smtp.qmul.ac.uk with esmtpsa (TLSv1:AES128-SHA:128) (Exim 4.71)\r
29         (envelope-from <m.walters@qmul.ac.uk>)\r
30         id 1X6nhH-0007on-Ad; Mon, 14 Jul 2014 22:22:07 +0100\r
31 From: Mark Walters <markwalters1009@gmail.com>\r
32 To: Austin Clements <amdragon@MIT.EDU>, notmuch@notmuchmail.org\r
33 Subject: Re: [PATCH 1/2] emacs: Introduce notmuch-jump: shortcut keys to saved\r
34         searches\r
35 In-Reply-To: <1405353735-26244-2-git-send-email-amdragon@mit.edu>\r
36 References: <1405353735-26244-1-git-send-email-amdragon@mit.edu>\r
37         <1405353735-26244-2-git-send-email-amdragon@mit.edu>\r
38 User-Agent: Notmuch/0.15.2+615~g78e3a93 (http://notmuchmail.org) Emacs/23.4.1\r
39         (x86_64-pc-linux-gnu)\r
40 Date: Mon, 14 Jul 2014 22:22:05 +0100\r
41 Message-ID: <87egxnd4aq.fsf@qmul.ac.uk>\r
42 MIME-Version: 1.0\r
43 Content-Type: text/plain; charset=utf-8\r
44 Content-Transfer-Encoding: quoted-printable\r
45 X-Sender-Host-Address: 194.42.225.51\r
46 X-QM-Geographic: According to ripencc,\r
47         this message was delivered by a machine in Britain (UK) (GB).\r
48 X-QM-SPAM-Info: Sender has good ham record.  :)\r
49 X-QM-Body-MD5: 5bdb0a34a3b8e8204b35fee859a54db1 (of first 20000 bytes)\r
50 X-SpamAssassin-Score: 0.0\r
51 X-SpamAssassin-SpamBar: /\r
52 X-SpamAssassin-Report: The QM spam filters have analysed this message to\r
53         determine if it is\r
54         spam. We require at least 5.0 points to mark a message as spam.\r
55         This message scored 0.0 points. Summary of the scoring: \r
56         * 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail\r
57         provider *      (markwalters1009[at]gmail.com)\r
58 X-QM-Scan-Virus: ClamAV says the message is clean\r
59 X-BeenThere: notmuch@notmuchmail.org\r
60 X-Mailman-Version: 2.1.13\r
61 Precedence: list\r
62 List-Id: "Use and development of the notmuch mail system."\r
63         <notmuch.notmuchmail.org>\r
64 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
65         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
66 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
67 List-Post: <mailto:notmuch@notmuchmail.org>\r
68 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
69 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
70         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
71 X-List-Received-Date: Mon, 14 Jul 2014 21:22:24 -0000\r
72 \r
73 \r
74 On Mon, 14 Jul 2014, Austin Clements <amdragon@MIT.EDU> wrote:\r
75 > This introduces notmuch-jump, which is like a user-friendly,\r
76 > user-configurable global prefix map for saved searches.  This provides\r
77 > a non-modal and much faster way to access saved searches than\r
78 > notmuch-hello.\r
79 >\r
80 > A user configures shortcut keys in notmuch-saved-searches, which are\r
81 > immediately accessible from anywhere in Notmuch under the "j" key (for\r
82 > "jump").  When the user hits "j", the minibuffer immediately shows a\r
83 > helpful table of bindings reminiscent of a completions buffer.\r
84 \r
85 I am basically happy with this: the only downside compared to dme's\r
86 patch is that this is quite substantially bigger. However, since this is\r
87 all in it's own file that is not really a problem.\r
88 \r
89 I have a few comments below. In all cases I am happy to go with the\r
90 current code if you think it's better than my suggestion.\r
91 \r
92 > This code is a combination of work from myself (originally,\r
93 > "notmuch-go"), David Edmondson, and modifications from Mark Walters.\r
94 > ---\r
95 >  emacs/Makefile.local   |   3 +-\r
96 >  emacs/notmuch-hello.el |   2 +\r
97 >  emacs/notmuch-jump.el  | 189 +++++++++++++++++++++++++++++++++++++++++++=\r
98 ++++++\r
99 >  emacs/notmuch-lib.el   |   3 +\r
100 >  4 files changed, 196 insertions(+), 1 deletion(-)\r
101 >  create mode 100644 emacs/notmuch-jump.el\r
102 >\r
103 > diff --git a/emacs/Makefile.local b/emacs/Makefile.local\r
104 > index c0d6b19..1109cfa 100644\r
105 > --- a/emacs/Makefile.local\r
106 > +++ b/emacs/Makefile.local\r
107 > @@ -18,7 +18,8 @@ emacs_sources :=3D \\r
108 >       $(dir)/notmuch-tag.el \\r
109 >       $(dir)/coolj.el \\r
110 >       $(dir)/notmuch-print.el \\r
111 > -     $(dir)/notmuch-version.el\r
112 > +     $(dir)/notmuch-version.el \\r
113 > +     $(dir)/notmuch-jump.el \\r
114 >=20=20\r
115 >  $(dir)/notmuch-version.el: $(dir)/Makefile.local version.stamp\r
116 >  $(dir)/notmuch-version.el: $(srcdir)/$(dir)/notmuch-version.el.tmpl\r
117 > diff --git a/emacs/notmuch-hello.el b/emacs/notmuch-hello.el\r
118 > index 3de5238..061b27d 100644\r
119 > --- a/emacs/notmuch-hello.el\r
120 > +++ b/emacs/notmuch-hello.el\r
121 > @@ -85,6 +85,7 @@ (define-widget 'notmuch-saved-search-plist 'list\r
122 >               (group :format "%v" :inline t (const :format "  Query: " :query) (stri=\r
123 ng :format "%v")))\r
124 >         (checklist :inline t\r
125 >                    :format "%v"\r
126 > +                  (group :format "%v" :inline t (const :format "Shortcut key: " :ke=\r
127 y) (key-sequence :format "%v"))\r
128 >                    (group :format "%v" :inline t (const :format "Count-Query: " :cou=\r
129 nt-query) (string :format "%v"))\r
130 >                    (group :format "%v" :inline t (const :format "" :sort-order)\r
131 >                           (choice :tag " Sort Order"\r
132 > @@ -101,6 +102,7 @@ (defcustom notmuch-saved-searches '((:name "inbox" :q=\r
133 uery "tag:inbox")\r
134 >=20=20\r
135 >    :name            Name of the search (required).\r
136 >    :query           Search to run (required).\r
137 > +  :key             Optional shortcut key for `notmuch-jump-search'.\r
138 >    :count-query     Optional extra query to generate the count\r
139 >                     shown. If not present then the :query property\r
140 >                     is used.\r
141 > diff --git a/emacs/notmuch-jump.el b/emacs/notmuch-jump.el\r
142 > new file mode 100644\r
143 > index 0000000..cb1ae10\r
144 > --- /dev/null\r
145 > +++ b/emacs/notmuch-jump.el\r
146 > @@ -0,0 +1,189 @@\r
147 > +;; notmuch-jump.el --- User-friendly shortcut keys\r
148 > +;;\r
149 > +;; Copyright =C2=A9 Austin Clements\r
150 > +;;\r
151 > +;; This file is part of Notmuch.\r
152 > +;;\r
153 > +;; Notmuch is free software: you can redistribute it and/or modify it\r
154 > +;; under the terms of the GNU General Public License as published by\r
155 > +;; the Free Software Foundation, either version 3 of the License, or\r
156 > +;; (at your option) any later version.\r
157 > +;;\r
158 > +;; Notmuch is distributed in the hope that it will be useful, but\r
159 > +;; WITHOUT ANY WARRANTY; without even the implied warranty of\r
160 > +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
161 > +;; General Public License for more details.\r
162 > +;;\r
163 > +;; You should have received a copy of the GNU General Public License\r
164 > +;; along with Notmuch.  If not, see <http://www.gnu.org/licenses/>.\r
165 > +;;\r
166 > +;; Authors: Austin Clements <aclements@csail.mit.edu>\r
167 > +;;          David Edmondson <dme@dme.org>\r
168 > +\r
169 > +(eval-when-compile (require 'cl))\r
170 > +\r
171 > +(require 'notmuch-hello)\r
172 > +\r
173 > +;;;###autoload\r
174 > +(defun notmuch-jump-search ()\r
175 > +  "Jump to a saved search by shortcut key.\r
176 > +\r
177 > +This prompts for and performs a saved search using the shortcut\r
178 > +keys configured in the :key property of `notmuch-saved-searches'.\r
179 > +Typically these shortcuts are a single key long, so this is a\r
180 > +fast way to jump to a saved search from anywhere in Notmuch."\r
181 > +  (interactive)\r
182 > +\r
183 > +  ;; Build the action map\r
184 > +  (let (action-map)\r
185 > +    (dolist (saved-search notmuch-saved-searches)\r
186 > +      (let* ((saved-search (notmuch-hello-saved-search-to-plist saved-se=\r
187 arch))\r
188 > +          (key (plist-get saved-search :key)))\r
189 > +     (when key\r
190 > +       (let ((name (plist-get saved-search :name))\r
191 > +             (query (plist-get saved-search :query))\r
192 > +             (oldest-first\r
193 > +              (case (plist-get saved-search :sort-order)\r
194 \r
195 I would probably not do the saved-search-to-plist bit and just use\r
196 notmuch-saved-search-get each time.\r
197 \r
198 > +                (newest-first nil)\r
199 > +                (oldest-first t)\r
200 > +                (otherwise (default-value notmuch-search-oldest-first)))))\r
201 > +         (push (list key name\r
202 > +                     `(lambda () (notmuch-search ',query ',oldest-first)))\r
203 > +               action-map)))))\r
204 > +    (setq action-map (nreverse action-map))\r
205 > +\r
206 > +    (if action-map\r
207 > +     (notmuch-jump action-map "Search ")\r
208 > +      (error "No shortcut keys for saved searches.  Please customize not=\r
209 much-saved-searches."))))\r
210 \r
211 I would slightly rephrase the error: something more like "... To use\r
212 notmuch-jump please customize notmuch-saved-searches."  So that the user\r
213 who doesn't want to use notmuch-jump doesn't think they are at fault.\r
214 \r
215 > +(defvar notmuch-jump--action nil)\r
216 > +\r
217 > +(defun notmuch-jump (action-map prompt)\r
218 > +  "Interactively prompt for one of the keys in ACTION-MAP.\r
219 > +\r
220 > +Displays a pop-up temporary buffer with a summary of all bindings\r
221 > +in ACTION-MAP, reads a key from the minibuffer, and performs the\r
222 > +corresponding action.  The prompt can be canceled with C-g.\r
223 \r
224 Maybe say the prompt can be canceled with RET too?\r
225 \r
226 > +PROMPT must be a string to use for the prompt if this command was\r
227 > +not invoked directly by a key binding (e.g., it was invoked\r
228 > +through M-x).  PROMPT should include a space at the end.\r
229 \r
230 I find the "j-" prompt a bit weird and would prefer something more like\r
231 "Jump to search: " to be used however the user enters the function.\r
232 \r
233 > +ACTION-MAP must be a list of triples of the form\r
234 > +  (KEY LABEL ACTION)\r
235 > +where KEY is a key binding, LABEL is a string label to display in\r
236 > +the buffer, and ACTION is a nullary function to call.  LABEL may\r
237 > +be null, in which case the action will still be bound, but will\r
238 > +not appear in the pop-up buffer.\r
239 > +"\r
240 > +\r
241 > +  (let* ((items (notmuch-jump--format-actions action-map))\r
242 > +      ;; Format the table of bindings and the full prompt\r
243 > +      (table\r
244 > +       (with-temp-buffer\r
245 > +         (notmuch-jump--insert-items (window-body-width) items)\r
246 > +         (buffer-string)))\r
247 > +      (prompt-text\r
248 > +       (if (eq this-original-command this-command)\r
249 > +           ;; Make it look like we're just part of any regular\r
250 > +           ;; submap prompt (like C-x, C-c, etc.)\r
251 > +           (concat (format-kbd-macro (this-command-keys)) "-")\r
252 > +         ;; We were invoked through something like M-x\r
253 > +         prompt))\r
254 > +      (full-prompt\r
255 > +       (concat table "\n\n"\r
256 > +               (propertize prompt-text 'face 'minibuffer-prompt)))\r
257 > +      ;; By default, the minibuffer applies the minibuffer face to\r
258 > +      ;; the entire prompt.  However, we want to clearly\r
259 > +      ;; distinguish bindings (which we put in the prompt face\r
260 > +      ;; ourselves) from their labels, so disable the minibuffer's\r
261 > +      ;; own re-face-ing.\r
262 > +      (minibuffer-prompt-properties\r
263 > +       (notmuch-jump--plist-delete\r
264 > +        (copy-sequence minibuffer-prompt-properties)\r
265 > +        'face))\r
266 > +      ;; Build the keymap with our bindings\r
267 > +      (minibuffer-map (notmuch-jump--make-keymap action-map))\r
268 > +      ;; The bindings save the the action in notmuch-jump--action\r
269 > +      (notmuch-jump--action nil))\r
270 > +    ;; Read the action\r
271 > +    (read-from-minibuffer full-prompt nil minibuffer-map)\r
272 > +\r
273 > +    ;; If we got an action, do it\r
274 > +    (when notmuch-jump--action\r
275 > +      (funcall notmuch-jump--action))))\r
276 > +\r
277 > +(defun notmuch-jump--format-actions (action-map)\r
278 > +  "Format the actions in ACTION-MAP.\r
279 > +\r
280 > +Returns a list of strings, one for each item with a label in\r
281 > +ACTION-MAP.  These strings can be inserted into a tabular\r
282 > +buffer."\r
283 > +\r
284 > +  ;; Compute the maximum key description width\r
285 > +  (let ((key-width 1))\r
286 > +    (dolist (action action-map)\r
287 \r
288 The name "action" is slightly unfortunate when you use ACTION as the\r
289 third item of each element of action-map when describing it\r
290 above. However, no better name springs to mind. (Maybe "triple"?)\r
291 \r
292 > +      (setq key-width\r
293 > +         (max key-width\r
294 > +              (string-width (format-kbd-macro (first action))))))\r
295 > +    ;; Format each action\r
296 > +    (mapcar (lambda (action)\r
297 > +           (let ((key (format-kbd-macro (first action)))\r
298 > +                 (desc (second action)))\r
299 > +             (concat\r
300 > +              (propertize key 'face 'minibuffer-prompt)\r
301 > +              (make-string (- key-width (length key)) ? )\r
302 > +              " " desc)))\r
303 > +         action-map)))\r
304 > +\r
305 > +(defun notmuch-jump--insert-items (width items)\r
306 > +  "Make a table of ITEMS up to WIDTH wide in the current buffer."\r
307 > +  (let* ((nitems (length items))\r
308 > +      (col-width (+ 3 (apply #'max (mapcar #'string-width items))))\r
309 > +      (ncols (if (> (* col-width nitems) width)\r
310 > +                 (max 1 (/ width col-width))\r
311 > +               ;; Items fit on one line.  Space them out\r
312 > +               (setq col-width (/ width nitems))\r
313 > +               (length items))))\r
314 > +    (while items\r
315 > +      (dotimes (col ncols)\r
316 > +     (when items\r
317 > +       (let ((item (pop items)))\r
318 > +         (insert item)\r
319 > +         (when (and items (< col (- ncols 1)))\r
320 > +           (insert (make-string (- col-width (string-width item)) ? ))))))\r
321 > +      (when items\r
322 > +     (insert "\n")))))\r
323 > +\r
324 > +(defvar notmuch-jump-minibuffer-map\r
325 > +  (let ((map (make-sparse-keymap)))\r
326 > +    (set-keymap-parent map minibuffer-local-map)\r
327 > +    ;; Make this like a special-mode keymap, with no self-insert-command\r
328 > +    (suppress-keymap map)\r
329 > +    map)\r
330 > +  "Base keymap for notmuch-jump's minibuffer keymap.")\r
331 > +\r
332 > +(defun notmuch-jump--make-keymap (action-map)\r
333 > +  "Translate ACTION-MAP into a minibuffer keymap."\r
334 > +  (let ((map (make-sparse-keymap)))\r
335 > +    (set-keymap-parent map notmuch-jump-minibuffer-map)\r
336 > +    (dolist (action action-map)\r
337 > +      (define-key map (first action)\r
338 > +     `(lambda () (interactive)\r
339 > +        (setq notmuch-jump--action ',(third action))\r
340 > +        (exit-minibuffer))))\r
341 > +    map))\r
342 > +\r
343 > +(defun notmuch-jump--plist-delete (plist property)\r
344 > +  (let* ((xplist (cons nil plist))\r
345 > +      (pred xplist))\r
346 > +    (while (cdr pred)\r
347 > +      (when (eq (cadr pred) property)\r
348 > +     (setcdr pred (cdddr pred)))\r
349 > +      (setq pred (cddr pred)))\r
350 > +    (cdr xplist)))\r
351 > +\r
352 > +(unless (fboundp 'window-body-width)\r
353 > +  ;; Compatibility for Emacs pre-24\r
354 > +  (defun window-body-width (&optional window)\r
355 > +    (let ((edges (window-inside-edges window)))\r
356 > +      (- (caddr edges) (car edges)))))\r
357 > diff --git a/emacs/notmuch-lib.el b/emacs/notmuch-lib.el\r
358 > index 2941da3..135422d 100644\r
359 > --- a/emacs/notmuch-lib.el\r
360 > +++ b/emacs/notmuch-lib.el\r
361 > @@ -130,9 +130,12 @@ (defvar notmuch-common-keymap\r
362 >      (define-key map "m" 'notmuch-mua-new-mail)\r
363 >      (define-key map "=3D" 'notmuch-refresh-this-buffer)\r
364 >      (define-key map "G" 'notmuch-poll-and-refresh-this-buffer)\r
365 > +    (define-key map "j" 'notmuch-jump-search)\r
366 >      map)\r
367 >    "Keymap shared by all notmuch modes.")\r
368 >=20=20\r
369 > +(autoload 'notmuch-jump-search "notmuch-jump" "Jump to a saved search by=\r
370  shortcut key." t)\r
371 \r
372 We don't normally seem to use autoload but instead use\r
373 declare-function. It might be worth being consistent (I am not very sure\r
374 of the pros and cons of autoload). May also be worth having it with the\r
375 other declare-functions to keep it clear how things are loaded.\r
376 \r
377 Best wishes\r
378 \r
379 Mark\r
380 \r
381 \r
382 > +\r
383 >  ;; By default clicking on a button does not select the window\r
384 >  ;; containing the button (as opposed to clicking on a widget which\r
385 >  ;; does). This means that the button action is then executed in the\r
386 > --=20\r
387 > 2.0.0\r