Re: Arg out of range error when tagging on the search view
[notmuch-archives.git] / 08 / c1e25d79fcb844994ae0a8e0a56a123974fcda
1 Return-Path: <bremner@pivot.cs.unb.ca>\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 1DD6A431FBC\r
6         for <notmuch@notmuchmail.org>; Thu, 24 Dec 2009 06:39:18 -0800 (PST)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 Received: from olra.theworths.org ([127.0.0.1])\r
9         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
10         with ESMTP id lsCPVCYfQt41 for <notmuch@notmuchmail.org>;\r
11         Thu, 24 Dec 2009 06:39:17 -0800 (PST)\r
12 Received: from pivot.cs.unb.ca (pivot.cs.unb.ca [131.202.240.57])\r
13         by olra.theworths.org (Postfix) with ESMTP id 6850F431FAE\r
14         for <notmuch@notmuchmail.org>; Thu, 24 Dec 2009 06:39:17 -0800 (PST)\r
15 Received: from\r
16         fctnnbsc30w-142167182194.pppoe-dynamic.high-speed.nb.bellaliant.net\r
17         ([142.167.182.194] helo=localhost)\r
18         by pivot.cs.unb.ca with esmtpsa (TLS1.0:RSA_AES_256_CBC_SHA1:32)\r
19         (Exim 4.69) (envelope-from <bremner@pivot.cs.unb.ca>)\r
20         id 1NNoqR-00086Y-Se; Thu, 24 Dec 2009 10:39:16 -0400\r
21 Received: from bremner by localhost with local (Exim 4.71)\r
22         (envelope-from <bremner@pivot.cs.unb.ca>)\r
23         id 1NNoqM-0000eM-DJ; Thu, 24 Dec 2009 10:39:10 -0400\r
24 From: david@tethera.net\r
25 To: notmuch@notmuchmail.org\r
26 Date: Thu, 24 Dec 2009 10:38:54 -0400\r
27 Message-Id: <1261665534-2445-1-git-send-email-david@tethera.net>\r
28 X-Mailer: git-send-email 1.6.5.7\r
29 In-Reply-To: <87ws0jydx0.fsf@yoom.home.cworth.org>\r
30 References: <87ws0jydx0.fsf@yoom.home.cworth.org>\r
31 X-Sender-Verified: bremner@pivot.cs.unb.ca\r
32 Cc: David Bremner <bremner@unb.ca>\r
33 Subject: [notmuch] [PATCH] notmuch.el: Refactor citation markup. Variables\r
34         for minimum size, button text.\r
35 X-BeenThere: notmuch@notmuchmail.org\r
36 X-Mailman-Version: 2.1.12\r
37 Precedence: list\r
38 List-Id: "Use and development of the notmuch mail system."\r
39         <notmuch.notmuchmail.org>\r
40 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
41         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
42 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
43 List-Post: <mailto:notmuch@notmuchmail.org>\r
44 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
45 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
46         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
47 X-List-Received-Date: Thu, 24 Dec 2009 14:39:18 -0000\r
48 \r
49 From: David Bremner <bremner@unb.ca>\r
50 \r
51 This is a fairly intrusive rewrite.\r
52 \r
53 - I pulled the common code for the signature and citation case out\r
54   into a separate function. This is not so much shorter, but I think it\r
55   will be easier to maintain.\r
56 \r
57 - I replaced the sequence of (looking-at blah) (forward-line)  with a single\r
58   re-search-forward per citation.\r
59 \r
60 New variables\r
61 \r
62 - notmuch-show-signature-button-format, notmuch-show-citation-button-format\r
63   Allow customization of button text.\r
64 \r
65 - notmuch-show-citation-lines-min\r
66   Do not buttonize citations below the given threshold.\r
67 ---\r
68 \r
69 I decided to start with a simple threshold, and only convert\r
70 sufficiently long citations to buttons.  Once I started messing with\r
71 that code, I decided to rework it to be more maintainable and\r
72 hopefully faster.\r
73 \r
74 This patch also fixes the problem of merging cites separated by a\r
75 single line of whitespace This is also fixed by the much simpler\r
76 <1260769295-12924-3-git-send-email-kanru@kanru.info>, but unlike that\r
77 patch there _shouldn't_ be a problem with eating the blank line after\r
78 a citation.  I'm not sure about the other bugs fixed by Kan-Ru's\r
79 series; perhaps Kan-Ru can comment.\r
80 \r
81  notmuch.el | 130\r
82  ++++++++++++++++++++++++++++++++++++++---------------------- 1 files\r
83  changed, 83 insertions(+), 47 deletions(-)\r
84 \r
85 diff --git a/notmuch.el b/notmuch.el\r
86 index 97914f2..6a5ceea 100644\r
87 --- a/notmuch.el\r
88 +++ b/notmuch.el\r
89 @@ -92,9 +92,24 @@ for indentation at the beginning of the line. But notmuch will\r
90  move past the indentation when testing this pattern, (so that the\r
91  pattern can still test against the entire line).")\r
92  \r
93 +(defvar notmuch-show-signature-button-format \r
94 +  "[ %d-line hidden signature. Click/Enter to show ]"\r
95 +  "String used to construct button text for hidden signatures\r
96 +\r
97 +Can use up to one integer format parameter, i.e. %d")\r
98 +\r
99 +(defvar notmuch-show-citation-button-format \r
100 +  "[ %d-line hidden citation. Click/Enter to show ]"\r
101 +  "String used to construct button text for hidden citations.\r
102 +\r
103 +Can use up to one integer format parameter, i.e. %d")\r
104 +\r
105  (defvar notmuch-show-signature-lines-max 12\r
106    "Maximum length of signature that will be hidden by default.")\r
107  \r
108 +(defvar notmuch-show-citation-lines-min 4\r
109 +  "Minimum length of citation that will be hidden.")\r
110 +\r
111  (defvar notmuch-command "notmuch"\r
112    "Command to run the notmuch binary.")\r
113  \r
114 @@ -593,54 +608,75 @@ which this thread was originally shown."\r
115    'face 'notmuch-message-summary-face\r
116    :supertype 'notmuch-button-invisibility-toggle-type)\r
117  \r
118 +(defun notmuch-show-citation-regexp (depth)\r
119 +  "Build a regexp for matching citations at a given DEPTH (indent)"\r
120 +  (let ((line-regexp (format "[[:space:]]\\{%d\\}*>.*\n" depth)))\r
121 +    (concat "\\(?:^" line-regexp \r
122 +           "\\(?:[[:space:]]*\n" line-regexp\r
123 +           "\\)?\\)+")))\r
124 +\r
125 +(defun notmuch-show-region-to-button (beg end type prefix button-text)\r
126 +  "Auxilary function to do the actual making of overlays and buttons\r
127 +\r
128 +BEG and END are buffer locations. TYPE should a string, either\r
129 +\"citation\" or \"signature\". PREFIX is some arbitrary text to\r
130 +insert before the button, probably for indentation.  BUTTON-TEXT\r
131 +is what to put on the button."\r
132 +\r
133 +;; This uses some slightly tricky conversions between strings and\r
134 +;; symbols because of the way the button code works. Note that\r
135 +;; replacing intern-soft with make-symbol will cause this to fail, \r
136 +;; since the newly created symbol has no plist.\r
137 +\r
138 +  (let ((overlay (make-overlay beg end))\r
139 +       (invis-spec (make-symbol (concat "notmuch-" type "-region")))\r
140 +       (button-type (intern-soft (concat "notmuch-button-" \r
141 +                                         type "-toggle-type"))))\r
142 +    (add-to-invisibility-spec invis-spec)\r
143 +    (overlay-put overlay 'invisible invis-spec)\r
144 +    (goto-char (1+ end))\r
145 +    (save-excursion \r
146 +      (goto-char (1- beg))\r
147 +      (insert prefix)\r
148 +      (insert-button button-text\r
149 +                    'invisibility-spec invis-spec\r
150 +                    :type button-type)\r
151 +      )))\r
152 +\r
153 +                                                \r
154  (defun notmuch-show-markup-citations-region (beg end depth)\r
155 -  (goto-char beg)\r
156 -  (beginning-of-line)\r
157 -  (while (< (point) end)\r
158 -    (let ((beg-sub (point-marker))\r
159 -         (indent (make-string depth ? ))\r
160 -         (citation ">"))\r
161 -      (move-to-column depth)\r
162 -      (if (looking-at citation)\r
163 -         (progn\r
164 -           (while (looking-at citation)\r
165 -             (forward-line)\r
166 -             (move-to-column depth))\r
167 -           (let ((overlay (make-overlay beg-sub (point)))\r
168 -                  (invis-spec (make-symbol "notmuch-citation-region")))\r
169 -              (add-to-invisibility-spec invis-spec)\r
170 -             (overlay-put overlay 'invisible invis-spec)\r
171 -              (let ((p (point-marker))\r
172 -                    (cite-button-text\r
173 -                     (concat "["  (number-to-string (count-lines beg-sub (point)))\r
174 -                             "-line citation. Click/Enter to show.]")))\r
175 -                (goto-char (- beg-sub 1))\r
176 -                (insert (concat "\n" indent))\r
177 -                (insert-button cite-button-text\r
178 -                               'invisibility-spec invis-spec\r
179 -                               :type 'notmuch-button-citation-toggle-type)\r
180 -                (forward-line)\r
181 -              ))))\r
182 -      (move-to-column depth)\r
183 -      (if (looking-at notmuch-show-signature-regexp)\r
184 -         (let ((sig-lines (- (count-lines beg-sub end) 1)))\r
185 -           (if (<= sig-lines notmuch-show-signature-lines-max)\r
186 -               (progn\r
187 -                  (let ((invis-spec (make-symbol "notmuch-signature-region")))\r
188 -                    (add-to-invisibility-spec invis-spec)\r
189 -                    (overlay-put (make-overlay beg-sub end)\r
190 -                                 'invisible invis-spec)\r
191 -                  \r
192 -                    (goto-char (- beg-sub 1))\r
193 -                    (insert (concat "\n" indent))\r
194 -                    (let ((sig-button-text (concat "[" (number-to-string sig-lines)\r
195 -                                                   "-line signature. Click/Enter to show.]")))\r
196 -                      (insert-button sig-button-text 'invisibility-spec invis-spec\r
197 -                                     :type 'notmuch-button-signature-toggle-type)\r
198 -                     )\r
199 -                    (insert "\n")\r
200 -                    (goto-char end))))))\r
201 -      (forward-line))))\r
202 +  "Markup citations, and up to one signature in the given region"\r
203 +  \r
204 +  (let ((citation-regexp (notmuch-show-citation-regexp depth))\r
205 +       (signature-regexp (concat (format "^[[:space:]]\\{%d\\}" depth) \r
206 +                                 notmuch-show-signature-regexp))\r
207 +       (indent (make-string depth ? )))\r
208 +    (goto-char beg)\r
209 +    (beginning-of-line)\r
210 +    (while (and (< (point) end) \r
211 +               (re-search-forward citation-regexp end t))\r
212 +      (let* ((cite-start (match-beginning 0))\r
213 +            (cite-end  (match-end 0))\r
214 +            (cite-lines (count-lines cite-start cite-end)))\r
215 +       (if (>= cite-lines notmuch-show-citation-lines-min)\r
216 +           (notmuch-show-region-to-button \r
217 +            cite-start cite-end\r
218 +            "citation"\r
219 +            indent\r
220 +            (format notmuch-show-citation-button-format cite-lines)\r
221 +            ))))\r
222 +    (if (re-search-forward signature-regexp end t)\r
223 +       (let* ((sig-start (match-beginning 0))\r
224 +              (sig-end (match-end 0))\r
225 +              (sig-lines (1- (count-lines sig-start end))))\r
226 +         (if (<= sig-lines notmuch-show-signature-lines-max)\r
227 +             (notmuch-show-region-to-button \r
228 +              sig-start\r
229 +              end\r
230 +              "signature"\r
231 +              indent\r
232 +              (format notmuch-show-signature-button-format sig-lines)\r
233 +              ))))))\r
234  \r
235  (defun notmuch-show-markup-part (beg end depth)\r
236    (if (re-search-forward notmuch-show-part-begin-regexp nil t)\r
237 -- \r
238 1.6.5.7\r
239 \r