Implement functions for showing the complete articles.
[gnus] / lisp / gnus-art.el
1 ;;; gnus-art.el --- article mode commands for Gnus
2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;;   2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 ;; For Emacs < 22.2.
29 (eval-and-compile
30   (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
31 (eval-when-compile
32   (require 'cl))
33 (defvar tool-bar-map)
34 (defvar w3m-minor-mode-map)
35
36 (require 'gnus)
37 (require 'gnus-sum)
38 (require 'gnus-spec)
39 (require 'gnus-int)
40 (require 'gnus-win)
41 (require 'mm-bodies)
42 (require 'mail-parse)
43 (require 'mm-decode)
44 (require 'mm-view)
45 (require 'wid-edit)
46 (require 'mm-uu)
47 (require 'message)
48
49 (autoload 'gnus-msg-mail "gnus-msg" nil t)
50 (autoload 'gnus-button-mailto "gnus-msg")
51 (autoload 'gnus-button-reply "gnus-msg" nil t)
52 (autoload 'parse-time-string "parse-time" nil nil)
53 (autoload 'ansi-color-apply-on-region "ansi-color")
54 (autoload 'mm-url-insert-file-contents-external "mm-url")
55 (autoload 'mm-extern-cache-contents "mm-extern")
56
57 (defgroup gnus-article nil
58   "Article display."
59   :link '(custom-manual "(gnus)Article Buffer")
60   :group 'gnus)
61
62 (defgroup gnus-article-treat nil
63   "Treating article parts."
64   :link '(custom-manual "(gnus)Article Hiding")
65   :group 'gnus-article)
66
67 (defgroup gnus-article-hiding nil
68   "Hiding article parts."
69   :link '(custom-manual "(gnus)Article Hiding")
70   :group 'gnus-article)
71
72 (defgroup gnus-article-highlight nil
73   "Article highlighting."
74   :link '(custom-manual "(gnus)Article Highlighting")
75   :group 'gnus-article
76   :group 'gnus-visual)
77
78 (defgroup gnus-article-signature nil
79   "Article signatures."
80   :link '(custom-manual "(gnus)Article Signature")
81   :group 'gnus-article)
82
83 (defgroup gnus-article-headers nil
84   "Article headers."
85   :link '(custom-manual "(gnus)Hiding Headers")
86   :group 'gnus-article)
87
88 (defgroup gnus-article-washing nil
89   "Special commands on articles."
90   :link '(custom-manual "(gnus)Article Washing")
91   :group 'gnus-article)
92
93 (defgroup gnus-article-emphasis nil
94   "Fontisizing articles."
95   :link '(custom-manual "(gnus)Article Fontisizing")
96   :group 'gnus-article)
97
98 (defgroup gnus-article-saving nil
99   "Saving articles."
100   :link '(custom-manual "(gnus)Saving Articles")
101   :group 'gnus-article)
102
103 (defgroup gnus-article-mime nil
104   "Worshiping the MIME wonder."
105   :link '(custom-manual "(gnus)Using MIME")
106   :group 'gnus-article)
107
108 (defgroup gnus-article-buttons nil
109   "Pushable buttons in the article buffer."
110   :link '(custom-manual "(gnus)Article Buttons")
111   :group 'gnus-article)
112
113 (defgroup gnus-article-various nil
114   "Other article options."
115   :link '(custom-manual "(gnus)Misc Article")
116   :group 'gnus-article)
117
118 (defcustom gnus-ignored-headers
119   (mapcar
120    (lambda (header)
121      (concat "^" header ":"))
122    '("Path" "Expires" "Date-Received" "References" "Xref" "Lines"
123      "Relay-Version" "Message-ID" "Approved" "Sender" "Received"
124      "X-UIDL" "MIME-Version" "Return-Path" "In-Reply-To"
125      "Content-Type" "Content-Transfer-Encoding" "X-WebTV-Signature"
126      "X-MimeOLE" "X-MSMail-Priority" "X-Priority" "X-Loop"
127      "X-Authentication-Warning" "X-MIME-Autoconverted" "X-Face"
128      "X-Attribution" "X-Originating-IP" "Delivered-To"
129      "NNTP-[-A-Za-z]+" "Distribution" "X-no-archive" "X-Trace"
130      "X-Complaints-To" "X-NNTP-Posting-Host" "X-Orig.*"
131      "Abuse-Reports-To" "Cache-Post-Path" "X-Article-Creation-Date"
132      "X-Poster" "X-Mail2News-Path" "X-Server-Date" "X-Cache"
133      "Originator" "X-Problems-To" "X-Auth-User" "X-Post-Time"
134      "X-Admin" "X-UID" "Resent-[-A-Za-z]+" "X-Mailing-List"
135      "Precedence" "Original-[-A-Za-z]+" "X-filename" "X-Orcpt"
136      "Old-Received" "X-Pgp" "X-Auth" "X-From-Line"
137      "X-Gnus-Article-Number" "X-Majordomo" "X-Url" "X-Sender"
138      "MBOX-Line" "Priority" "X400-[-A-Za-z]+"
139      "Status" "X-Gnus-Mail-Source" "Cancel-Lock"
140      "X-FTN" "X-EXP32-SerialNo" "Encoding" "Importance"
141      "Autoforwarded" "Original-Encoded-Information-Types" "X-Ya-Pop3"
142      "X-Face-Version" "X-Vms-To" "X-ML-NAME" "X-ML-COUNT"
143      "Mailing-List" "X-finfo" "X-md5sum" "X-md5sum-Origin"
144      "X-Sun-Charset" "X-Accept-Language" "X-Envelope-Sender"
145      "List-[A-Za-z]+" "X-Listprocessor-Version"
146      "X-Received" "X-Distribute" "X-Sequence" "X-Juno-Line-Breaks"
147      "X-Notes-Item" "X-MS-TNEF-Correlator" "x-uunet-gateway"
148      "X-Received" "Content-length" "X-precedence"
149      "X-Authenticated-User" "X-Comment" "X-Report" "X-Abuse-Info"
150      "X-HTTP-Proxy" "X-Mydeja-Info" "X-Copyright" "X-No-Markup"
151      "X-Abuse-Info" "X-From_" "X-Accept-Language" "Errors-To"
152      "X-BeenThere" "X-Mailman-Version" "List-Help" "List-Post"
153      "List-Subscribe" "List-Id" "List-Unsubscribe" "List-Archive"
154      "X-Content-length" "X-Posting-Agent" "Original-Received"
155      "X-Request-PGP" "X-Fingerprint" "X-WRIEnvto" "X-WRIEnvfrom"
156      "X-Virus-Scanned" "X-Delivery-Agent" "Posted-Date" "X-Gateway"
157      "X-Local-Origin" "X-Local-Destination" "X-UserInfo1"
158      "X-Received-Date" "X-Hashcash" "Face" "X-DMCA-Notifications"
159      "X-Abuse-and-DMCA-Info" "X-Postfilter" "X-Gpg-.*" "X-Disclaimer"
160      "Envelope-To" "X-Spam-Score" "System-Type" "X-Injected-Via-Gmane"
161      "X-Gmane-NNTP-Posting-Host" "Jabber-ID" "Archived-At"
162      "Envelope-Sender" "Envelope-Recipients"))
163   "*All headers that start with this regexp will be hidden.
164 This variable can also be a list of regexps of headers to be ignored.
165 If `gnus-visible-headers' is non-nil, this variable will be ignored."
166   :type '(choice :custom-show nil
167                  regexp
168                  (repeat regexp))
169   :group 'gnus-article-hiding)
170
171 (defcustom gnus-visible-headers
172   "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^[BGF]?Cc:\\|^Posted-To:\\|^Mail-Copies-To:\\|^Mail-Followup-To:\\|^Apparently-To:\\|^Gnus-Warning:\\|^Resent-From:\\|^X-Sent:"
173   "*All headers that do not match this regexp will be hidden.
174 This variable can also be a list of regexp of headers to remain visible.
175 If this variable is non-nil, `gnus-ignored-headers' will be ignored."
176   :type '(choice
177           (repeat :value-to-internal (lambda (widget value)
178                                        (custom-split-regexp-maybe value))
179                   :match (lambda (widget value)
180                            (or (stringp value)
181                                (widget-editable-list-match widget value)))
182                   regexp)
183           (const :tag "Use gnus-ignored-headers" nil)
184           regexp)
185   :group 'gnus-article-hiding)
186
187 (defcustom gnus-sorted-header-list
188   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:"
189     "^Followup-To:" "^To:" "^Cc:" "^Date:" "^Organization:")
190   "*This variable is a list of regular expressions.
191 If it is non-nil, headers that match the regular expressions will
192 be placed first in the article buffer in the sequence specified by
193 this list."
194   :type '(repeat regexp)
195   :group 'gnus-article-hiding)
196
197 (defcustom gnus-boring-article-headers '(empty followup-to reply-to)
198   "Headers that are only to be displayed if they have interesting data.
199 Possible values in this list are:
200
201   'empty       Headers with no content.
202   'newsgroups  Newsgroup identical to Gnus group.
203   'to-address  To identical to To-address.
204   'to-list     To identical to To-list.
205   'cc-list     CC identical to To-list.
206   'followup-to Followup-to identical to Newsgroups.
207   'reply-to    Reply-to identical to From.
208   'date        Date less than four days old.
209   'long-to     To and/or Cc longer than 1024 characters.
210   'many-to     Multiple To and/or Cc."
211   :type '(set (const :tag "Headers with no content." empty)
212               (const :tag "Newsgroups identical to Gnus group." newsgroups)
213               (const :tag "To identical to To-address." to-address)
214               (const :tag "To identical to To-list." to-list)
215               (const :tag "CC identical to To-list." cc-list)
216               (const :tag "Followup-to identical to Newsgroups." followup-to)
217               (const :tag "Reply-to identical to From." reply-to)
218               (const :tag "Date less than four days old." date)
219               (const :tag "To and/or Cc longer than 1024 characters." long-to)
220               (const :tag "Multiple To and/or Cc headers." many-to))
221   :group 'gnus-article-hiding)
222
223 (defcustom gnus-article-skip-boring nil
224   "Skip over text that is not worth reading.
225 By default, if you set this t, then Gnus will display citations and
226 signatures, but will never scroll down to show you a page consisting
227 only of boring text.  Boring text is controlled by
228 `gnus-article-boring-faces'."
229   :version "22.1"
230   :type 'boolean
231   :group 'gnus-article-hiding)
232
233 (defcustom gnus-signature-separator '("^-- $" "^-- *$")
234   "Regexp matching signature separator.
235 This can also be a list of regexps.  In that case, it will be checked
236 from head to tail looking for a separator.  Searches will be done from
237 the end of the buffer."
238   :type '(choice :format "%{%t%}: %[Value Menu%]\n%v"
239                  (regexp)
240                  (repeat :tag "List of regexp" regexp))
241   :group 'gnus-article-signature)
242
243 (defcustom gnus-signature-limit nil
244   "Provide a limit to what is considered a signature.
245 If it is a number, no signature may not be longer (in characters) than
246 that number.  If it is a floating point number, no signature may be
247 longer (in lines) than that number.  If it is a function, the function
248 will be called without any parameters, and if it returns nil, there is
249 no signature in the buffer.  If it is a string, it will be used as a
250 regexp.  If it matches, the text in question is not a signature.
251
252 This can also be a list of the above values."
253   :type '(choice (const nil)
254                  (integer :value 200)
255                  (number :value 4.0)
256                  function
257                  (regexp :value ".*"))
258   :group 'gnus-article-signature)
259
260 (defcustom gnus-fetch-partial-articles nil
261   "If non-nil, Gnus will fetch partial articles.
262 If t, nnimap will fetch only the first part.  If a string, it
263 will fetch all parts that have types that match that string.  A
264 likely value would be \"text/\" to automatically fetch all
265 textual parts.
266
267 Currently only the nnimap backend actually supports partial
268 article fetching.  If the backend doesn't support it, it has no
269 effect."
270   :version "24.1"
271   :type '(choice (const nil)
272                  (const t)
273                  (regexp))
274   :group 'gnus-article)
275
276 (defcustom gnus-hidden-properties '(invisible t intangible t)
277   "Property list to use for hiding text."
278   :type 'sexp
279   :group 'gnus-article-hiding)
280
281 ;; Fixme: This isn't the right thing for mixed graphical and non-graphical
282 ;; frames in a session.
283 (defcustom gnus-article-x-face-command
284   (if (featurep 'xemacs)
285       (if (or (gnus-image-type-available-p 'xface)
286               (gnus-image-type-available-p 'pbm))
287           'gnus-display-x-face-in-from
288         "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | ee -")
289     (if (gnus-image-type-available-p 'pbm)
290         'gnus-display-x-face-in-from
291       "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | \
292 display -"))
293   "*String or function to be executed to display an X-Face header.
294 If it is a string, the command will be executed in a sub-shell
295 asynchronously.  The compressed face will be piped to this command."
296   :type `(choice string
297                  (function-item gnus-display-x-face-in-from)
298                  function)
299   :version "21.1"
300   :group 'gnus-picon
301   :group 'gnus-article-washing)
302
303 (defcustom gnus-article-x-face-too-ugly nil
304   "Regexp matching posters whose face shouldn't be shown automatically."
305   :type '(choice regexp (const nil))
306   :group 'gnus-article-washing)
307
308 (defcustom gnus-article-banner-alist nil
309   "Banner alist for stripping.
310 For example,
311      ((egroups . \"^[ \\t\\n]*-------------------+\\\\( \\\\(e\\\\|Yahoo! \\\\)Groups Sponsor -+\\\\)?....\\n\\\\(.+\\n\\\\)+\"))"
312   :version "21.1"
313   :type '(repeat (cons symbol regexp))
314   :group 'gnus-article-washing)
315
316 (gnus-define-group-parameter
317  banner
318  :variable-document
319  "Alist of regexps (to match group names) and banner."
320  :variable-group gnus-article-washing
321  :parameter-type
322  '(choice :tag "Banner"
323           :value nil
324           (const :tag "Remove signature" signature)
325           (symbol :tag "Item in `gnus-article-banner-alist'" none)
326           regexp
327           (const :tag "None" nil))
328  :parameter-document
329  "If non-nil, specify how to remove `banners' from articles.
330
331 Symbol `signature' means to remove signatures delimited by
332 `gnus-signature-separator'.  Any other symbol is used to look up a
333 regular expression to match the banner in `gnus-article-banner-alist'.
334 A string is used as a regular expression to match the banner
335 directly.")
336
337 (defcustom gnus-article-address-banner-alist nil
338   "Alist of mail addresses and banners.
339 Each element has the form (ADDRESS . BANNER), where ADDRESS is a regexp
340 to match a mail address in the From: header, BANNER is one of a symbol
341 `signature', an item in `gnus-article-banner-alist', a regexp and nil.
342 If ADDRESS matches author's mail address, it will remove things like
343 advertisements.  For example:
344
345 \((\"@yoo-hoo\\\\.co\\\\.jp\\\\'\" . \"\\n_+\\nDo You Yoo-hoo!\\\\?\\n.*\\n.*\\n\"))
346 "
347   :type '(repeat
348           (cons
349            (regexp :tag "Address")
350            (choice :tag "Banner" :value nil
351                    (const :tag "Remove signature" signature)
352                    (symbol :tag "Item in `gnus-article-banner-alist'" none)
353                    regexp
354                    (const :tag "None" nil))))
355   :version "22.1"
356   :group 'gnus-article-washing)
357
358 (defmacro gnus-emphasis-custom-with-format (&rest body)
359   `(let ((format "\
360 \\(\\s-\\|^\\|\\=\\|[-\"]\\|\\s(\\)\\(%s\\(\\w+\\(\\s-+\\w+\\)*[.,]?\\)%s\\)\
361 \\(\\([-,.;:!?\"]\\|\\s)\\)+\\s-\\|[?!.]\\s-\\|\\s)\\|\\s-\\)"))
362      ,@body))
363
364 (defun gnus-emphasis-custom-value-to-external (value)
365   (gnus-emphasis-custom-with-format
366    (if (consp (car value))
367        (list (format format (car (car value)) (cdr (car value)))
368              2
369              (if (nth 1 value) 2 3)
370              (nth 2 value))
371      value)))
372
373 (defun gnus-emphasis-custom-value-to-internal (value)
374   (gnus-emphasis-custom-with-format
375    (let ((regexp (concat "\\`"
376                          (format (regexp-quote format)
377                                  "\\([^()]+\\)" "\\([^()]+\\)")
378                          "\\'"))
379          pattern)
380      (if (string-match regexp (setq pattern (car value)))
381          (list (cons (match-string 1 pattern) (match-string 2 pattern))
382                (= (nth 2 value) 2)
383                (nth 3 value))
384        value))))
385
386 (defcustom gnus-emphasis-alist
387   (let ((types
388          '(("\\*" "\\*" bold nil 2)
389            ("_" "_" underline)
390            ("/" "/" italic)
391            ("_/" "/_" underline-italic)
392            ("_\\*" "\\*_" underline-bold)
393            ("\\*/" "/\\*" bold-italic)
394            ("_\\*/" "/\\*_" underline-bold-italic))))
395     (nconc
396      (gnus-emphasis-custom-with-format
397       (mapcar (lambda (spec)
398                 (list (format format (car spec) (cadr spec))
399                       (or (nth 3 spec) 2)
400                       (or (nth 4 spec) 3)
401                       (intern (format "gnus-emphasis-%s" (nth 2 spec)))))
402               types))
403      '(;; I've never seen anyone use this strikethru convention whereas I've
404        ;; several times seen it triggered by normal text.  --Stef
405        ;; Miles suggests that this form is sometimes used but for italics,
406        ;; so maybe we should map it to `italic'.
407        ;; ("\\(\\s-\\|^\\)\\(-\\(\\(\\w\\|-[^-]\\)+\\)-\\)\\(\\s-\\|[?!.,;]\\)"
408        ;; 2 3 gnus-emphasis-strikethru)
409        ("\\(\\s-\\|^\\)\\(_\\(\\(\\w\\|_[^_]\\)+\\)_\\)\\(\\s-\\|[?!.,;]\\)"
410         2 3 gnus-emphasis-underline))))
411   "*Alist that says how to fontify certain phrases.
412 Each item looks like this:
413
414   (\"_\\\\(\\\\w+\\\\)_\" 0 1 'underline)
415
416 The first element is a regular expression to be matched.  The second
417 is a number that says what regular expression grouping used to find
418 the entire emphasized word.  The third is a number that says what
419 regexp grouping should be displayed and highlighted.  The fourth
420 is the face used for highlighting."
421   :type
422   '(repeat
423     (menu-choice
424      :format "%[Customizing Style%]\n%v"
425      :indent 2
426      (group :tag "Default"
427             :value ("" 0 0 default)
428             :value-create
429             (lambda (widget)
430               (let ((value (widget-get
431                             (cadr (widget-get (widget-get widget :parent)
432                                               :args))
433                             :value)))
434                 (if (not (eq (nth 2 value) 'default))
435                     (widget-put
436                      widget
437                      :value
438                      (gnus-emphasis-custom-value-to-external value))))
439               (widget-group-value-create widget))
440             regexp
441             (integer :format "Match group: %v")
442             (integer :format "Emphasize group: %v")
443             face)
444      (group :tag "Simple"
445             :value (("_" . "_") nil default)
446             (cons :format "%v"
447                   (regexp :format "Start regexp: %v")
448                   (regexp :format "End regexp: %v"))
449             (boolean :format "Show start and end patterns: %[%v%]\n"
450                      :on " On " :off " Off ")
451             face)))
452   :get (lambda (symbol)
453          (mapcar 'gnus-emphasis-custom-value-to-internal
454                  (default-value symbol)))
455   :set (lambda (symbol value)
456          (set-default symbol (mapcar 'gnus-emphasis-custom-value-to-external
457                                      value)))
458   :group 'gnus-article-emphasis)
459
460 (defcustom gnus-emphasize-whitespace-regexp "^[ \t]+\\|[ \t]*\n"
461   "A regexp to describe whitespace which should not be emphasized.
462 Typical values are \"^[ \\t]+\\\\|[ \\t]*\\n\" and \"[ \\t]+\\\\|[ \\t]*\\n\".
463 The former avoids underlining of leading and trailing whitespace,
464 and the latter avoids underlining any whitespace at all."
465   :version "21.1"
466   :group 'gnus-article-emphasis
467   :type 'regexp)
468
469 (defface gnus-emphasis-bold '((t (:bold t)))
470   "Face used for displaying strong emphasized text (*word*)."
471   :group 'gnus-article-emphasis)
472
473 (defface gnus-emphasis-italic '((t (:italic t)))
474   "Face used for displaying italic emphasized text (/word/)."
475   :group 'gnus-article-emphasis)
476
477 (defface gnus-emphasis-underline '((t (:underline t)))
478   "Face used for displaying underlined emphasized text (_word_)."
479   :group 'gnus-article-emphasis)
480
481 (defface gnus-emphasis-underline-bold '((t (:bold t :underline t)))
482   "Face used for displaying underlined bold emphasized text (_*word*_)."
483   :group 'gnus-article-emphasis)
484
485 (defface gnus-emphasis-underline-italic '((t (:italic t :underline t)))
486   "Face used for displaying underlined italic emphasized text (_/word/_)."
487   :group 'gnus-article-emphasis)
488
489 (defface gnus-emphasis-bold-italic '((t (:bold t :italic t)))
490   "Face used for displaying bold italic emphasized text (/*word*/)."
491   :group 'gnus-article-emphasis)
492
493 (defface gnus-emphasis-underline-bold-italic
494   '((t (:bold t :italic t :underline t)))
495   "Face used for displaying underlined bold italic emphasized text.
496 Example: (_/*word*/_)."
497   :group 'gnus-article-emphasis)
498
499 (defface gnus-emphasis-strikethru (if (featurep 'xemacs)
500                                       '((t (:strikethru t)))
501                                     '((t (:strike-through t))))
502   "Face used for displaying strike-through text (-word-)."
503   :group 'gnus-article-emphasis)
504
505 (defface gnus-emphasis-highlight-words
506   '((t (:background "black" :foreground "yellow")))
507   "Face used for displaying highlighted words."
508   :group 'gnus-article-emphasis)
509
510 (defcustom gnus-article-time-format "%a, %d %b %Y %T %Z"
511   "Format for display of Date headers in article bodies.
512 See `format-time-string' for the possible values.
513
514 The variable can also be function, which should return a complete Date
515 header.  The function is called with one argument, the time, which can
516 be fed to `format-time-string'."
517   :type '(choice string function)
518   :link '(custom-manual "(gnus)Article Date")
519   :group 'gnus-article-washing)
520
521 (defcustom gnus-save-all-headers t
522   "*If non-nil, don't remove any headers before saving.
523 This will be overridden by the `:headers' property that the symbol of
524 the saver function, which is specified by `gnus-default-article-saver',
525 might have."
526   :group 'gnus-article-saving
527   :type 'boolean)
528
529 (defcustom gnus-prompt-before-saving 'always
530   "*This variable says how much prompting is to be done when saving articles.
531 If it is nil, no prompting will be done, and the articles will be
532 saved to the default files.  If this variable is `always', each and
533 every article that is saved will be preceded by a prompt, even when
534 saving large batches of articles.  If this variable is neither nil not
535 `always', there the user will be prompted once for a file name for
536 each invocation of the saving commands."
537   :group 'gnus-article-saving
538   :type '(choice (item always)
539                  (item :tag "never" nil)
540                  (sexp :tag "once" :format "%t\n" :value t)))
541
542 (defcustom gnus-saved-headers gnus-visible-headers
543   "Headers to keep if `gnus-save-all-headers' is nil.
544 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
545 If that variable is nil, however, all headers that match this regexp
546 will be kept while the rest will be deleted before saving.  This and
547 `gnus-save-all-headers' will be overridden by the `:headers' property
548 that the symbol of the saver function, which is specified by
549 `gnus-default-article-saver', might have."
550   :group 'gnus-article-saving
551   :type 'regexp)
552
553 ;; Note that "Rmail format" is mbox since Emacs 23, but Babyl before.
554 (defcustom gnus-default-article-saver 'gnus-summary-save-in-rmail
555   "A function to save articles in your favourite format.
556 The function will be called by way of the `gnus-summary-save-article'
557 command, and friends such as `gnus-summary-save-article-rmail'.
558
559 Gnus provides the following functions:
560
561 * gnus-summary-save-in-rmail (Rmail format)
562 * gnus-summary-save-in-mail (Unix mail format)
563 * gnus-summary-save-in-folder (MH folder)
564 * gnus-summary-save-in-file (article format)
565 * gnus-summary-save-body-in-file (article body)
566 * gnus-summary-save-in-vm (use VM's folder format)
567 * gnus-summary-write-to-file (article format -- overwrite)
568 * gnus-summary-write-body-to-file (article body -- overwrite)
569 * gnus-summary-save-in-pipe (article format)
570
571 The symbol of each function may have the following properties:
572
573 * :decode
574 The value non-nil means save decoded articles.  This is meaningful
575 only with `gnus-summary-save-in-file', `gnus-summary-save-body-in-file',
576 `gnus-summary-write-to-file', `gnus-summary-write-body-to-file', and
577 `gnus-summary-save-in-pipe'.
578
579 * :function
580 The value specifies an alternative function which appends, not
581 overwrites, articles to a file.  This implies that when saving many
582 articles at a time, `gnus-prompt-before-saving' is bound to t and all
583 articles are saved in a single file.  This is meaningful only with
584 `gnus-summary-write-to-file' and `gnus-summary-write-body-to-file'.
585
586 * :headers
587 The value specifies the symbol of a variable of which the value
588 specifies headers to be saved.  If it is omitted,
589 `gnus-save-all-headers' and `gnus-saved-headers' control what
590 headers should be saved."
591   :group 'gnus-article-saving
592   :type '(radio (function-item gnus-summary-save-in-rmail)
593                 (function-item gnus-summary-save-in-mail)
594                 (function-item gnus-summary-save-in-folder)
595                 (function-item gnus-summary-save-in-file)
596                 (function-item gnus-summary-save-body-in-file)
597                 (function-item gnus-summary-save-in-vm)
598                 (function-item gnus-summary-write-to-file)
599                 (function-item gnus-summary-write-body-to-file)
600                 (function-item gnus-summary-save-in-pipe)
601                 (function)))
602
603 (defcustom gnus-article-save-coding-system
604   (or (and (mm-coding-system-p 'utf-8) 'utf-8)
605       (and (mm-coding-system-p 'iso-2022-7bit) 'iso-2022-7bit)
606       (and (mm-coding-system-p 'emacs-mule) 'emacs-mule)
607       (and (mm-coding-system-p 'escape-quoted) 'escape-quoted))
608   "Coding system used to save decoded articles to a file.
609
610 The recommended coding systems are `utf-8', `iso-2022-7bit' and so on,
611 which can safely encode any characters in text.  This is used by the
612 commands including:
613
614 * gnus-summary-save-article-file
615 * gnus-summary-save-article-body-file
616 * gnus-summary-write-article-file
617 * gnus-summary-write-article-body-file
618
619 and the functions to which you may set `gnus-default-article-saver':
620
621 * gnus-summary-save-in-file
622 * gnus-summary-save-body-in-file
623 * gnus-summary-write-to-file
624 * gnus-summary-write-body-to-file
625
626 Those commands and functions save just text displayed in the article
627 buffer to a file if the value of this variable is non-nil.  Note that
628 buttonized MIME parts will be lost in a saved file in that case.
629 Otherwise, raw articles will be saved."
630   :group 'gnus-article-saving
631   :type `(choice
632           :format "%{%t%}:\n %[Value Menu%] %v"
633           (const :tag "Save raw articles" nil)
634           ,@(delq nil
635                   (mapcar
636                    (lambda (arg) (if (mm-coding-system-p (nth 3 arg)) arg))
637                    '((const :tag "UTF-8" utf-8)
638                      (const :tag "iso-2022-7bit" iso-2022-7bit)
639                      (const :tag "Emacs internal" emacs-mule)
640                      (const :tag "escape-quoted" escape-quoted))))
641           (symbol :tag "Coding system")))
642
643 (defcustom gnus-rmail-save-name 'gnus-plain-save-name
644   "A function generating a file name to save articles in Rmail format.
645 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
646   :group 'gnus-article-saving
647   :type 'function)
648
649 (defcustom gnus-mail-save-name 'gnus-plain-save-name
650   "A function generating a file name to save articles in Unix mail format.
651 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
652   :group 'gnus-article-saving
653   :type 'function)
654
655 (defcustom gnus-folder-save-name 'gnus-folder-save-name
656   "A function generating a file name to save articles in MH folder.
657 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER."
658   :group 'gnus-article-saving
659   :type 'function)
660
661 (defcustom gnus-file-save-name 'gnus-numeric-save-name
662   "A function generating a file name to save articles in article format.
663 The function is called with NEWSGROUP, HEADERS, and optional
664 LAST-FILE."
665   :group 'gnus-article-saving
666   :type 'function)
667
668 (defcustom gnus-split-methods
669   '((gnus-article-archive-name)
670     (gnus-article-nndoc-name))
671   "*Variable used to suggest where articles are to be saved.
672 For instance, if you would like to save articles related to Gnus in
673 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
674 you could set this variable to something like:
675
676  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
677    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
678
679 This variable is an alist where the key is the match and the
680 value is a list of possible files to save in if the match is
681 non-nil.
682
683 If the match is a string, it is used as a regexp match on the
684 article.  If the match is a symbol, that symbol will be funcalled
685 from the buffer of the article to be saved with the newsgroup as the
686 parameter.  If it is a list, it will be evaled in the same buffer.
687
688 If this form or function returns a string, this string will be used as a
689 possible file name; and if it returns a non-nil list, that list will be
690 used as possible file names."
691   :group 'gnus-article-saving
692   :type '(repeat (choice (list :value (fun) function)
693                          (cons :value ("" "") regexp (repeat string))
694                          (sexp :value nil))))
695
696 (defcustom gnus-page-delimiter "^\^L"
697   "*Regexp describing what to use as article page delimiters.
698 The default value is \"^\^L\", which is a form linefeed at the
699 beginning of a line."
700   :type 'regexp
701   :group 'gnus-article-various)
702
703 (defcustom gnus-article-mode-line-format "Gnus: %g [%w] %S%m"
704   "*The format specification for the article mode line.
705 See `gnus-summary-mode-line-format' for a closer description.
706
707 The following additional specs are available:
708
709 %w  The article washing status.
710 %m  The number of MIME parts in the article."
711   :type 'string
712   :group 'gnus-article-various)
713
714 (defcustom gnus-article-mode-hook nil
715   "*A hook for Gnus article mode."
716   :type 'hook
717   :group 'gnus-article-various)
718
719 (when (featurep 'xemacs)
720   ;; Extracted from gnus-xmas-define in order to preserve user settings
721   (when (fboundp 'turn-off-scroll-in-place)
722     (add-hook 'gnus-article-mode-hook 'turn-off-scroll-in-place))
723   ;; Extracted from gnus-xmas-redefine in order to preserve user settings
724   (add-hook 'gnus-article-mode-hook 'gnus-xmas-article-menu-add))
725
726 (defcustom gnus-article-menu-hook nil
727   "*Hook run after the creation of the article mode menu."
728   :type 'hook
729   :group 'gnus-article-various)
730
731 (defcustom gnus-article-prepare-hook nil
732   "*A hook called after an article has been prepared in the article buffer."
733   :type 'hook
734   :group 'gnus-article-various)
735
736 (defcustom gnus-copy-article-ignored-headers nil
737   "List of headers to be removed when copying an article.
738 Each element is a regular expression."
739   :version "23.1" ;; No Gnus
740   :type '(repeat regexp)
741   :group 'gnus-article-various)
742
743 (make-obsolete-variable 'gnus-article-hide-pgp-hook nil
744                         "Gnus 5.10 (Emacs-22.1)")
745
746 (defface gnus-button
747   '((t (:weight bold)))
748   "Face used for highlighting a button in the article buffer."
749   :group 'gnus-article-buttons)
750
751 (defcustom gnus-article-button-face 'gnus-button
752   "Face used for highlighting buttons in the article buffer.
753
754 An article button is a piece of text that you can activate by pressing
755 `RET' or `mouse-2' above it."
756   :type 'face
757   :group 'gnus-article-buttons)
758
759 (defcustom gnus-article-mouse-face 'highlight
760   "Face used for mouse highlighting in the article buffer.
761
762 Article buttons will be displayed in this face when the cursor is
763 above them."
764   :type 'face
765   :group 'gnus-article-buttons)
766
767 (defcustom gnus-signature-face 'gnus-signature
768   "Face used for highlighting a signature in the article buffer.
769 Obsolete; use the face `gnus-signature' for customizations instead."
770   :type 'face
771   :group 'gnus-article-highlight
772   :group 'gnus-article-signature)
773
774 (defface gnus-signature
775   '((t
776      (:italic t)))
777   "Face used for highlighting a signature in the article buffer."
778   :group 'gnus-article-highlight
779   :group 'gnus-article-signature)
780 ;; backward-compatibility alias
781 (put 'gnus-signature-face 'face-alias 'gnus-signature)
782 (put 'gnus-signature-face 'obsolete-face "22.1")
783
784 (defface gnus-header-from
785   '((((class color)
786       (background dark))
787      (:foreground "PaleGreen1"))
788     (((class color)
789       (background light))
790      (:foreground "red3"))
791     (t
792      (:italic t)))
793   "Face used for displaying from headers."
794   :group 'gnus-article-headers
795   :group 'gnus-article-highlight)
796 ;; backward-compatibility alias
797 (put 'gnus-header-from-face 'face-alias 'gnus-header-from)
798 (put 'gnus-header-from-face 'obsolete-face "22.1")
799
800 (defface gnus-header-subject
801   '((((class color)
802       (background dark))
803      (:foreground "SeaGreen1"))
804     (((class color)
805       (background light))
806      (:foreground "red4"))
807     (t
808      (:bold t :italic t)))
809   "Face used for displaying subject headers."
810   :group 'gnus-article-headers
811   :group 'gnus-article-highlight)
812 ;; backward-compatibility alias
813 (put 'gnus-header-subject-face 'face-alias 'gnus-header-subject)
814 (put 'gnus-header-subject-face 'obsolete-face "22.1")
815
816 (defface gnus-header-newsgroups
817   '((((class color)
818       (background dark))
819      (:foreground "yellow" :italic t))
820     (((class color)
821       (background light))
822      (:foreground "MidnightBlue" :italic t))
823     (t
824      (:italic t)))
825   "Face used for displaying newsgroups headers.
826 In the default setup this face is only used for crossposted
827 articles."
828   :group 'gnus-article-headers
829   :group 'gnus-article-highlight)
830 ;; backward-compatibility alias
831 (put 'gnus-header-newsgroups-face 'face-alias 'gnus-header-newsgroups)
832 (put 'gnus-header-newsgroups-face 'obsolete-face "22.1")
833
834 (defface gnus-header-name
835   '((((class color)
836       (background dark))
837      (:foreground "SpringGreen2"))
838     (((class color)
839       (background light))
840      (:foreground "maroon"))
841     (t
842      (:bold t)))
843   "Face used for displaying header names."
844   :group 'gnus-article-headers
845   :group 'gnus-article-highlight)
846 ;; backward-compatibility alias
847 (put 'gnus-header-name-face 'face-alias 'gnus-header-name)
848 (put 'gnus-header-name-face 'obsolete-face "22.1")
849
850 (defface gnus-header-content
851   '((((class color)
852       (background dark))
853      (:foreground "SpringGreen1" :italic t))
854     (((class color)
855       (background light))
856      (:foreground "indianred4" :italic t))
857     (t
858      (:italic t)))  "Face used for displaying header content."
859   :group 'gnus-article-headers
860   :group 'gnus-article-highlight)
861 ;; backward-compatibility alias
862 (put 'gnus-header-content-face 'face-alias 'gnus-header-content)
863 (put 'gnus-header-content-face 'obsolete-face "22.1")
864
865 (defcustom gnus-header-face-alist
866   '(("From" nil gnus-header-from)
867     ("Subject" nil gnus-header-subject)
868     ("Newsgroups:.*," nil gnus-header-newsgroups)
869     ("" gnus-header-name gnus-header-content))
870   "*Controls highlighting of article headers.
871
872 An alist of the form (HEADER NAME CONTENT).
873
874 HEADER is a regular expression which should match the name of a
875 header and NAME and CONTENT are either face names or nil.
876
877 The name of each header field will be displayed using the face
878 specified by the first element in the list where HEADER matches
879 the header name and NAME is non-nil.  Similarly, the content will
880 be displayed by the first non-nil matching CONTENT face."
881   :group 'gnus-article-headers
882   :group 'gnus-article-highlight
883   :type '(repeat (list (regexp :tag "Header")
884                        (choice :tag "Name"
885                                (item :tag "skip" nil)
886                                (face :value default))
887                        (choice :tag "Content"
888                                (item :tag "skip" nil)
889                                (face :value default)))))
890
891 (defcustom gnus-face-properties-alist (if (featurep 'xemacs)
892                                           '((xface . (:face gnus-x-face)))
893                                         '((pbm . (:face gnus-x-face))
894                                           (png . nil)))
895   "Alist of image types and properties applied to Face and X-Face images.
896 Here are examples:
897
898 ;; Specify the altitude of Face images in the From header.
899 \(setq gnus-face-properties-alist
900       '((pbm . (:face gnus-x-face :ascent 80))
901         (png . (:ascent 80))))
902
903 ;; Show Face images as pressed buttons.
904 \(setq gnus-face-properties-alist
905       '((pbm . (:face gnus-x-face :relief -2))
906         (png . (:relief -2))))
907
908 See the manual for the valid properties for various image types.
909 Currently, `pbm' is used for X-Face images and `png' is used for Face
910 images in Emacs.  Only the `:face' property is effective on the `xface'
911 image type in XEmacs if it is built with the libcompface library."
912   :version "23.1" ;; No Gnus
913   :group 'gnus-article-headers
914   :type '(repeat (cons :format "%v" (symbol :tag "Image type") plist)))
915
916 (defcustom gnus-article-decode-hook
917   '(article-decode-charset article-decode-encoded-words
918                            article-decode-group-name article-decode-idna-rhs)
919   "*Hook run to decode charsets in articles."
920   :group 'gnus-article-headers
921   :type 'hook)
922
923 (defcustom gnus-display-mime-function 'gnus-display-mime
924   "Function to display MIME articles."
925   :group 'gnus-article-mime
926   :type 'function)
927
928 (defvar gnus-decode-header-function 'mail-decode-encoded-word-region
929   "Function used to decode headers.")
930
931 (defvar gnus-decode-address-function 'mail-decode-encoded-address-region
932   "Function used to decode addresses.")
933
934 (defvar gnus-article-dumbquotes-map
935   '(("\200" "EUR")
936     ("\202" ",")
937     ("\203" "f")
938     ("\204" ",,")
939     ("\205" "...")
940     ("\213" "<")
941     ("\214" "OE")
942     ("\221" "`")
943     ("\222" "'")
944     ("\223" "``")
945     ("\224" "\"")
946     ("\225" "*")
947     ("\226" "-")
948     ("\227" "--")
949     ("\230" "~")
950     ("\231" "(TM)")
951     ("\233" ">")
952     ("\234" "oe")
953     ("\264" "'"))
954   "Table for MS-to-Latin1 translation.")
955
956 (defcustom gnus-ignored-mime-types nil
957   "List of MIME types that should be ignored by Gnus."
958   :version "21.1"
959   :group 'gnus-article-mime
960   :type '(repeat regexp))
961
962 (defcustom gnus-unbuttonized-mime-types '(".*/.*")
963   "List of MIME types that should not be given buttons when rendered inline.
964 See also `gnus-buttonized-mime-types' which may override this variable.
965 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
966   :version "21.1"
967   :group 'gnus-article-mime
968   :type '(repeat regexp))
969
970 (defcustom gnus-buttonized-mime-types nil
971   "List of MIME types that should be given buttons when rendered inline.
972 If set, this variable overrides `gnus-unbuttonized-mime-types'.
973 To see e.g. security buttons you could set this to
974 `(\"multipart/signed\")'.  You could also add \"multipart/alternative\" to
975 this list to display radio buttons that allow you to choose one of two
976 media types those mails include.  See also `mm-discouraged-alternatives'.
977 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
978   :version "22.1"
979   :group 'gnus-article-mime
980   :type '(repeat regexp))
981
982 (defcustom gnus-inhibit-mime-unbuttonizing nil
983   "If non-nil, all MIME parts get buttons.
984 When nil (the default value), then some MIME parts do not get buttons,
985 as described by the variables `gnus-buttonized-mime-types' and
986 `gnus-unbuttonized-mime-types'."
987   :version "22.1"
988   :group 'gnus-article-mime
989   :type 'boolean)
990
991 (defcustom gnus-body-boundary-delimiter "_"
992   "String used to delimit header and body.
993 This variable is used by `gnus-article-treat-body-boundary' which can
994 be controlled by `gnus-treat-body-boundary'."
995   :version "22.1"
996   :group 'gnus-article-various
997   :type '(choice (item :tag "None" :value nil)
998                  string))
999
1000 (defcustom gnus-picon-databases '("/usr/lib/picon" "/usr/local/faces"
1001                                   "/usr/share/picons")
1002   "Defines the location of the faces database.
1003 For information on obtaining this database of pretty pictures, please
1004 see http://www.cs.indiana.edu/picons/ftp/index.html"
1005   :version "22.1"
1006   :type '(repeat directory)
1007   :link '(url-link :tag "download"
1008                    "http://www.cs.indiana.edu/picons/ftp/index.html")
1009   :link '(custom-manual "(gnus)Picons")
1010   :group 'gnus-picon)
1011
1012 (defun gnus-picons-installed-p ()
1013   "Say whether picons are installed on your machine."
1014   (let ((installed nil))
1015     (dolist (database gnus-picon-databases)
1016       (when (file-exists-p database)
1017         (setq installed t)))
1018     installed))
1019
1020 (defcustom gnus-article-mime-part-function nil
1021   "Function called with a MIME handle as the argument.
1022 This is meant for people who want to do something automatic based
1023 on parts -- for instance, adding Vcard info to a database."
1024   :group 'gnus-article-mime
1025   :type '(choice (const nil)
1026                  function))
1027
1028 (defcustom gnus-mime-multipart-functions nil
1029   "An alist of MIME types to functions to display them."
1030   :version "21.1"
1031   :group 'gnus-article-mime
1032   :type '(repeat (cons :format "%v" (string :tag "MIME type") function)))
1033
1034 (defcustom gnus-article-date-lapsed-new-header nil
1035   "Whether the X-Sent and Date headers can coexist.
1036 When using `gnus-treat-date-lapsed', the \"X-Sent:\" header will
1037 either replace the old \"Date:\" header (if this variable is nil), or
1038 be added below it (otherwise)."
1039   :version "21.1"
1040   :group 'gnus-article-headers
1041   :type 'boolean)
1042
1043 (defcustom gnus-article-mime-match-handle-function 'undisplayed-alternative
1044   "Function called with a MIME handle as the argument.
1045 This is meant for people who want to view first matched part.
1046 For `undisplayed-alternative' (default), the first undisplayed
1047 part or alternative part is used.  For `undisplayed', the first
1048 undisplayed part is used.  For a function, the first part which
1049 the function return t is used.  For nil, the first part is
1050 used."
1051   :version "21.1"
1052   :group 'gnus-article-mime
1053   :type '(choice
1054           (item :tag "first" :value nil)
1055           (item :tag "undisplayed" :value undisplayed)
1056           (item :tag "undisplayed or alternative"
1057                 :value undisplayed-alternative)
1058           (function)))
1059
1060 (defcustom gnus-mime-action-alist
1061   '(("save to file" . gnus-mime-save-part)
1062     ("save and strip" . gnus-mime-save-part-and-strip)
1063     ("replace with file" . gnus-mime-replace-part)
1064     ("delete part" . gnus-mime-delete-part)
1065     ("display as text" . gnus-mime-inline-part)
1066     ("view the part" . gnus-mime-view-part)
1067     ("pipe to command" . gnus-mime-pipe-part)
1068     ("toggle display" . gnus-article-press-button)
1069     ("toggle display" . gnus-article-view-part-as-charset)
1070     ("view as type" . gnus-mime-view-part-as-type)
1071     ("view internally" . gnus-mime-view-part-internally)
1072     ("view externally" . gnus-mime-view-part-externally))
1073   "An alist of actions that run on the MIME attachment."
1074   :group 'gnus-article-mime
1075   :type '(repeat (cons (string :tag "name")
1076                        (function))))
1077
1078 (defcustom gnus-auto-select-part 1
1079   "Advance to next MIME part when deleting or stripping parts.
1080
1081 When 0, point will be placed on the same part as before.  When
1082 positive (negative), move point forward (backwards) this many
1083 parts.  When nil, redisplay article."
1084   :version "23.1" ;; No Gnus
1085   :group 'gnus-article-mime
1086   :type '(choice (const nil :tag "Redisplay article.")
1087                  (const 1 :tag "Next part.")
1088                  (const 0 :tag "Current part.")
1089                  integer))
1090
1091 ;;;
1092 ;;; The treatment variables
1093 ;;;
1094
1095 (defvar gnus-part-display-hook nil
1096   "Hook called on parts that are to receive treatment.")
1097
1098 (defvar gnus-article-treat-custom
1099   '(choice (const :tag "Off" nil)
1100            (const :tag "On" t)
1101            (const :tag "Header" head)
1102            (const :tag "First" first)
1103            (const :tag "Last" last)
1104            (integer :tag "Less")
1105            (repeat :tag "Groups" regexp)
1106            (sexp :tag "Predicate")))
1107
1108 (defvar gnus-article-treat-head-custom
1109   '(choice (const :tag "Off" nil)
1110            (const :tag "Header" head)))
1111
1112 (defvar gnus-article-treat-types '("text/plain" "text/x-verbatim"
1113                                    "text/x-patch")
1114   "Parts to treat.")
1115
1116 (defvar gnus-inhibit-treatment nil
1117   "Whether to inhibit treatment.")
1118
1119 (defcustom gnus-treat-highlight-signature '(or t (typep "text/x-vcard"))
1120   "Highlight the signature.
1121 Valid values are nil, t, `head', `first', `last', an integer or a
1122 predicate.  See Info node `(gnus)Customizing Articles'."
1123   :group 'gnus-article-treat
1124   :link '(custom-manual "(gnus)Customizing Articles")
1125   :type gnus-article-treat-custom)
1126 (put 'gnus-treat-highlight-signature 'highlight t)
1127
1128 (defcustom gnus-treat-buttonize 100000
1129   "Add buttons.
1130 Valid values are nil, t, `head', `first', `last', an integer or a
1131 predicate.  See Info node `(gnus)Customizing Articles'."
1132   :group 'gnus-article-treat
1133   :link '(custom-manual "(gnus)Customizing Articles")
1134   :type gnus-article-treat-custom)
1135 (put 'gnus-treat-buttonize 'highlight t)
1136
1137 (defcustom gnus-treat-buttonize-head 'head
1138   "Add buttons to the head.
1139 Valid values are nil, t, `head', `first', `last', an integer or a
1140 predicate.  See Info node `(gnus)Customizing Articles'."
1141   :group 'gnus-article-treat
1142   :link '(custom-manual "(gnus)Customizing Articles")
1143   :type gnus-article-treat-head-custom)
1144 (put 'gnus-treat-buttonize-head 'highlight t)
1145
1146 (defcustom gnus-treat-emphasize 50000
1147   "Emphasize text.
1148 Valid values are nil, t, `head', `first', `last', an integer or a
1149 predicate.  See Info node `(gnus)Customizing Articles'."
1150   :group 'gnus-article-treat
1151   :link '(custom-manual "(gnus)Customizing Articles")
1152   :type gnus-article-treat-custom)
1153 (put 'gnus-treat-emphasize 'highlight t)
1154
1155 (defcustom gnus-treat-strip-cr nil
1156   "Remove carriage returns.
1157 Valid values are nil, t, `head', `first', `last', an integer or a
1158 predicate.  See Info node `(gnus)Customizing Articles'."
1159   :version "22.1"
1160   :group 'gnus-article-treat
1161   :link '(custom-manual "(gnus)Customizing Articles")
1162   :type gnus-article-treat-custom)
1163
1164 (defcustom gnus-treat-unsplit-urls nil
1165   "Remove newlines from within URLs.
1166 Valid values are nil, t, `head', `first', `last', an integer or a
1167 predicate.  See Info node `(gnus)Customizing Articles'."
1168   :version "22.1"
1169   :group 'gnus-article-treat
1170   :link '(custom-manual "(gnus)Customizing Articles")
1171   :type gnus-article-treat-custom)
1172
1173 (defcustom gnus-treat-leading-whitespace nil
1174   "Remove leading whitespace in headers.
1175 Valid values are nil, t, `head', `first', `last', an integer or a
1176 predicate.  See Info node `(gnus)Customizing Articles'."
1177   :version "22.1"
1178   :group 'gnus-article-treat
1179   :link '(custom-manual "(gnus)Customizing Articles")
1180   :type gnus-article-treat-custom)
1181
1182 (defcustom gnus-treat-hide-headers 'head
1183   "Hide headers.
1184 Valid values are nil, t, `head', `first', `last', an integer or a
1185 predicate.  See Info node `(gnus)Customizing Articles'."
1186   :group 'gnus-article-treat
1187   :link '(custom-manual "(gnus)Customizing Articles")
1188   :type gnus-article-treat-head-custom)
1189
1190 (defcustom gnus-treat-hide-boring-headers nil
1191   "Hide boring headers.
1192 Valid values are nil, t, `head', `first', `last', an integer or a
1193 predicate.  See Info node `(gnus)Customizing Articles'."
1194   :group 'gnus-article-treat
1195   :link '(custom-manual "(gnus)Customizing Articles")
1196   :type gnus-article-treat-head-custom)
1197
1198 (defcustom gnus-treat-hide-signature nil
1199   "Hide the signature.
1200 Valid values are nil, t, `head', `first', `last', an integer or a
1201 predicate.  See Info node `(gnus)Customizing Articles'."
1202   :group 'gnus-article-treat
1203   :link '(custom-manual "(gnus)Customizing Articles")
1204   :type gnus-article-treat-custom)
1205
1206 (defcustom gnus-treat-fill-article nil
1207   "Fill the article.
1208 Valid values are nil, t, `head', `first', `last', an integer or a
1209 predicate.  See Info node `(gnus)Customizing Articles'."
1210   :group 'gnus-article-treat
1211   :link '(custom-manual "(gnus)Customizing Articles")
1212   :type gnus-article-treat-custom)
1213
1214 (defcustom gnus-treat-hide-citation nil
1215   "Hide cited text.
1216 Valid values are nil, t, `head', `first', `last', an integer or a
1217 predicate.  See Info node `(gnus)Customizing Articles'."
1218   :group 'gnus-article-treat
1219   :link '(custom-manual "(gnus)Customizing Articles")
1220   :type gnus-article-treat-custom)
1221
1222 (defcustom gnus-treat-hide-citation-maybe nil
1223   "Hide cited text.
1224 Valid values are nil, t, `head', `first', `last', an integer or a
1225 predicate.  See Info node `(gnus)Customizing Articles'."
1226   :group 'gnus-article-treat
1227   :link '(custom-manual "(gnus)Customizing Articles")
1228   :type gnus-article-treat-custom)
1229
1230 (defcustom gnus-treat-strip-list-identifiers 'head
1231   "Strip list identifiers from `gnus-list-identifiers`.
1232 Valid values are nil, t, `head', `first', `last', an integer or a
1233 predicate.  See Info node `(gnus)Customizing Articles'."
1234   :version "21.1"
1235   :group 'gnus-article-treat
1236   :link '(custom-manual "(gnus)Customizing Articles")
1237   :type gnus-article-treat-custom)
1238
1239 (make-obsolete-variable 'gnus-treat-strip-pgp nil
1240                         "Gnus 5.10 (Emacs 22.1)")
1241
1242 (defcustom gnus-treat-strip-pem nil
1243   "Strip PEM signatures.
1244 Valid values are nil, t, `head', `first', `last', an integer or a
1245 predicate.  See Info node `(gnus)Customizing Articles'."
1246   :group 'gnus-article-treat
1247   :link '(custom-manual "(gnus)Customizing Articles")
1248   :type gnus-article-treat-custom)
1249
1250 (defcustom gnus-treat-strip-banner t
1251   "Strip banners from articles.
1252 The banner to be stripped is specified in the `banner' group parameter.
1253 Valid values are nil, t, `head', `first', `last', an integer or a
1254 predicate.  See Info node `(gnus)Customizing Articles'."
1255   :group 'gnus-article-treat
1256   :link '(custom-manual "(gnus)Customizing Articles")
1257   :type gnus-article-treat-custom)
1258
1259 (defcustom gnus-treat-highlight-headers 'head
1260   "Highlight the headers.
1261 Valid values are nil, t, `head', `first', `last', an integer or a
1262 predicate.  See Info node `(gnus)Customizing Articles'."
1263   :group 'gnus-article-treat
1264   :link '(custom-manual "(gnus)Customizing Articles")
1265   :type gnus-article-treat-head-custom)
1266 (put 'gnus-treat-highlight-headers 'highlight t)
1267
1268 (defcustom gnus-treat-highlight-citation t
1269   "Highlight cited text.
1270 Valid values are nil, t, `head', `first', `last', an integer or a
1271 predicate.  See Info node `(gnus)Customizing Articles'."
1272   :group 'gnus-article-treat
1273   :link '(custom-manual "(gnus)Customizing Articles")
1274   :type gnus-article-treat-custom)
1275 (put 'gnus-treat-highlight-citation 'highlight t)
1276
1277 (defcustom gnus-treat-date-ut nil
1278   "Display the Date in UT (GMT).
1279 Valid values are nil, t, `head', `first', `last', an integer or a
1280 predicate.  See Info node `(gnus)Customizing Articles'."
1281   :group 'gnus-article-treat
1282   :link '(custom-manual "(gnus)Customizing Articles")
1283   :type gnus-article-treat-head-custom)
1284
1285 (defcustom gnus-treat-date-local nil
1286   "Display the Date in the local timezone.
1287 Valid values are nil, t, `head', `first', `last', an integer or a
1288 predicate.  See Info node `(gnus)Customizing Articles'."
1289   :group 'gnus-article-treat
1290   :link '(custom-manual "(gnus)Customizing Articles")
1291   :type gnus-article-treat-head-custom)
1292
1293 (defcustom gnus-treat-date-english nil
1294   "Display the Date in a format that can be read aloud in English.
1295 Valid values are nil, t, `head', `first', `last', an integer or a
1296 predicate.  See Info node `(gnus)Customizing Articles'."
1297   :version "22.1"
1298   :group 'gnus-article-treat
1299   :link '(custom-manual "(gnus)Customizing Articles")
1300   :type gnus-article-treat-head-custom)
1301
1302 (defcustom gnus-treat-date-lapsed nil
1303   "Display the Date header in a way that says how much time has elapsed.
1304 Valid values are nil, t, `head', `first', `last', an integer or a
1305 predicate.  See Info node `(gnus)Customizing Articles'."
1306   :group 'gnus-article-treat
1307   :link '(custom-manual "(gnus)Customizing Articles")
1308   :type gnus-article-treat-head-custom)
1309
1310 (defcustom gnus-treat-date-original nil
1311   "Display the date in the original timezone.
1312 Valid values are nil, t, `head', `first', `last', an integer or a
1313 predicate.  See Info node `(gnus)Customizing Articles'."
1314   :group 'gnus-article-treat
1315   :link '(custom-manual "(gnus)Customizing Articles")
1316   :type gnus-article-treat-head-custom)
1317
1318 (defcustom gnus-treat-date-iso8601 nil
1319   "Display the date in the ISO8601 format.
1320 Valid values are nil, t, `head', `first', `last', an integer or a
1321 predicate.  See Info node `(gnus)Customizing Articles'."
1322   :version "21.1"
1323   :group 'gnus-article-treat
1324   :link '(custom-manual "(gnus)Customizing Articles")
1325   :type gnus-article-treat-head-custom)
1326
1327 (defcustom gnus-treat-date-user-defined nil
1328   "Display the date in a user-defined format.
1329 The format is defined by the `gnus-article-time-format' variable.
1330 Valid values are nil, t, `head', `first', `last', an integer or a
1331 predicate.  See Info node `(gnus)Customizing Articles'."
1332   :group 'gnus-article-treat
1333   :link '(custom-manual "(gnus)Customizing Articles")
1334   :type gnus-article-treat-head-custom)
1335
1336 (defcustom gnus-treat-strip-headers-in-body t
1337   "Strip the X-No-Archive header line from the beginning of the body.
1338 Valid values are nil, t, `head', `first', `last', an integer or a
1339 predicate.  See Info node `(gnus)Customizing Articles'."
1340   :version "21.1"
1341   :group 'gnus-article-treat
1342   :link '(custom-manual "(gnus)Customizing Articles")
1343   :type gnus-article-treat-custom)
1344
1345 (defcustom gnus-treat-strip-trailing-blank-lines nil
1346   "Strip trailing blank lines.
1347 Valid values are nil, t, `head', `first', `last', an integer or a
1348 predicate.  See Info node `(gnus)Customizing Articles'.
1349
1350 When set to t, it also strips trailing blanks in all MIME parts.
1351 Consider to use `last' instead."
1352   :group 'gnus-article-treat
1353   :link '(custom-manual "(gnus)Customizing Articles")
1354   :type gnus-article-treat-custom)
1355
1356 (defcustom gnus-treat-strip-leading-blank-lines nil
1357   "Strip leading blank lines.
1358 Valid values are nil, t, `head', `first', `last', an integer or a
1359 predicate.  See Info node `(gnus)Customizing Articles'.
1360
1361 When set to t, it also strips trailing blanks in all MIME parts."
1362   :group 'gnus-article-treat
1363   :link '(custom-manual "(gnus)Customizing Articles")
1364   :type gnus-article-treat-custom)
1365
1366 (defcustom gnus-treat-strip-multiple-blank-lines nil
1367   "Strip multiple blank lines.
1368 Valid values are nil, t, `head', `first', `last', an integer or a
1369 predicate.  See Info node `(gnus)Customizing Articles'."
1370   :group 'gnus-article-treat
1371   :link '(custom-manual "(gnus)Customizing Articles")
1372   :type gnus-article-treat-custom)
1373
1374 (defcustom gnus-treat-unfold-headers 'head
1375   "Unfold folded header lines.
1376 Valid values are nil, t, `head', `first', `last', an integer or a
1377 predicate.  See Info node `(gnus)Customizing Articles'."
1378   :version "22.1"
1379   :group 'gnus-article-treat
1380   :link '(custom-manual "(gnus)Customizing Articles")
1381   :type gnus-article-treat-custom)
1382
1383 (defcustom gnus-article-unfold-long-headers nil
1384   "If non-nil, allow unfolding headers even if the header is long.
1385 If it is a regexp, only long headers matching this regexp are unfolded.
1386 If it is t, all long headers are unfolded.
1387
1388 This variable has no effect if `gnus-treat-unfold-headers' is nil."
1389   :version "23.1" ;; No Gnus
1390   :group 'gnus-article-treat
1391   :type '(choice (const nil)
1392                  (const :tag "all" t)
1393                  (regexp)))
1394
1395 (defcustom gnus-treat-fold-headers nil
1396   "Fold headers.
1397 Valid values are nil, t, `head', `first', `last', an integer or a
1398 predicate.  See Info node `(gnus)Customizing Articles'."
1399   :version "22.1"
1400   :group 'gnus-article-treat
1401   :link '(custom-manual "(gnus)Customizing Articles")
1402   :type gnus-article-treat-custom)
1403
1404 (defcustom gnus-treat-fold-newsgroups 'head
1405   "Fold the Newsgroups and Followup-To headers.
1406 Valid values are nil, t, `head', `first', `last', an integer or a
1407 predicate.  See Info node `(gnus)Customizing Articles'."
1408   :version "22.1"
1409   :group 'gnus-article-treat
1410   :link '(custom-manual "(gnus)Customizing Articles")
1411   :type gnus-article-treat-custom)
1412
1413 (defcustom gnus-treat-overstrike t
1414   "Treat overstrike highlighting.
1415 Valid values are nil, t, `head', `first', `last', an integer or a
1416 predicate.  See Info node `(gnus)Customizing Articles'."
1417   :group 'gnus-article-treat
1418   :link '(custom-manual "(gnus)Customizing Articles")
1419   :type gnus-article-treat-custom)
1420 (put 'gnus-treat-overstrike 'highlight t)
1421
1422 (defcustom gnus-treat-ansi-sequences (if (locate-library "ansi-color") t)
1423   "Treat ANSI SGR control sequences.
1424 Valid values are nil, t, `head', `first', `last', an integer or a
1425 predicate.  See Info node `(gnus)Customizing Articles'."
1426   :group 'gnus-article-treat
1427   :link '(custom-manual "(gnus)Customizing Articles")
1428   :type gnus-article-treat-custom)
1429
1430 (make-obsolete-variable 'gnus-treat-display-xface
1431                         'gnus-treat-display-x-face "22.1")
1432
1433 (defcustom gnus-treat-display-x-face
1434   (and (not noninteractive)
1435        (gnus-image-type-available-p 'xbm)
1436        (if (featurep 'xemacs)
1437            (featurep 'xface)
1438          (condition-case nil
1439              (and (string-match "^0x" (shell-command-to-string "uncompface"))
1440                   (executable-find "icontopbm"))
1441            ;; shell-command-to-string may signal an error, e.g. if
1442            ;; shell-file-name is not found.
1443            (error nil)))
1444        'head)
1445   "Display X-Face headers.
1446 Valid values are nil and `head'.
1447 See Info node `(gnus)Customizing Articles' and Info node
1448 `(gnus)X-Face' for details."
1449   :group 'gnus-article-treat
1450   :version "21.1"
1451   :link '(custom-manual "(gnus)Customizing Articles")
1452   :link '(custom-manual "(gnus)X-Face")
1453   :type gnus-article-treat-head-custom
1454   :set (lambda (symbol value)
1455          (set-default
1456           symbol
1457           (cond ((or (boundp symbol) (get symbol 'saved-value))
1458                  value)
1459                 ((boundp 'gnus-treat-display-xface)
1460                  (message "\
1461 ** gnus-treat-display-xface is an obsolete variable;\
1462  use gnus-treat-display-x-face instead")
1463                  (default-value 'gnus-treat-display-xface))
1464                 ((get 'gnus-treat-display-xface 'saved-value)
1465                  (message "\
1466 ** gnus-treat-display-xface is an obsolete variable;\
1467  use gnus-treat-display-x-face instead")
1468                  (eval (car (get 'gnus-treat-display-xface 'saved-value))))
1469                 (t
1470                  value)))))
1471 (put 'gnus-treat-display-x-face 'highlight t)
1472
1473 (defcustom gnus-treat-display-face
1474   (and (not noninteractive)
1475        (gnus-image-type-available-p 'png)
1476        'head)
1477   "Display Face headers.
1478 Valid values are nil, t, `head', `first', `last', an integer or a
1479 predicate.  See Info node `(gnus)Customizing Articles' and Info
1480 node `(gnus)Face' for details."
1481   :group 'gnus-article-treat
1482   :version "22.1"
1483   :link '(custom-manual "(gnus)Customizing Articles")
1484   :link '(custom-manual "(gnus)X-Face")
1485   :type gnus-article-treat-head-custom)
1486 (put 'gnus-treat-display-face 'highlight t)
1487
1488 (defcustom gnus-treat-display-smileys (gnus-image-type-available-p 'xpm)
1489   "Display smileys.
1490 Valid values are nil, t, `head', `first', `last', an integer or a
1491 predicate.  See Info node `(gnus)Customizing Articles' and Info
1492 node `(gnus)Smileys' for details."
1493   :group 'gnus-article-treat
1494   :version "21.1"
1495   :link '(custom-manual "(gnus)Customizing Articles")
1496   :link '(custom-manual "(gnus)Smileys")
1497   :type gnus-article-treat-custom)
1498 (put 'gnus-treat-display-smileys 'highlight t)
1499
1500 (defcustom gnus-treat-from-picon
1501   (if (and (gnus-image-type-available-p 'xpm)
1502            (gnus-picons-installed-p))
1503       'head nil)
1504   "Display picons in the From header.
1505 Valid values are nil, t, `head', `first', `last', an integer or a
1506 predicate.  See Info node `(gnus)Customizing Articles' and Info
1507 node `(gnus)Picons' for details."
1508   :version "22.1"
1509   :group 'gnus-article-treat
1510   :group 'gnus-picon
1511   :link '(custom-manual "(gnus)Customizing Articles")
1512   :link '(custom-manual "(gnus)Picons")
1513   :type gnus-article-treat-head-custom)
1514 (put 'gnus-treat-from-picon 'highlight t)
1515
1516 (defcustom gnus-treat-mail-picon
1517   (if (and (gnus-image-type-available-p 'xpm)
1518            (gnus-picons-installed-p))
1519       'head nil)
1520   "Display picons in To and Cc headers.
1521 Valid values are nil, t, `head', `first', `last', an integer or a
1522 predicate.  See Info node `(gnus)Customizing Articles' and Info
1523 node `(gnus)Picons' for details."
1524   :version "22.1"
1525   :group 'gnus-article-treat
1526   :group 'gnus-picon
1527   :link '(custom-manual "(gnus)Customizing Articles")
1528   :link '(custom-manual "(gnus)Picons")
1529   :type gnus-article-treat-head-custom)
1530 (put 'gnus-treat-mail-picon 'highlight t)
1531
1532 (defcustom gnus-treat-newsgroups-picon
1533   (if (and (gnus-image-type-available-p 'xpm)
1534            (gnus-picons-installed-p))
1535       'head nil)
1536   "Display picons in the Newsgroups and Followup-To headers.
1537 Valid values are nil, t, `head', `first', `last', an integer or a
1538 predicate.  See Info node `(gnus)Customizing Articles' and Info
1539 node `(gnus)Picons' for details."
1540   :version "22.1"
1541   :group 'gnus-article-treat
1542   :group 'gnus-picon
1543   :link '(custom-manual "(gnus)Customizing Articles")
1544   :link '(custom-manual "(gnus)Picons")
1545   :type gnus-article-treat-head-custom)
1546 (put 'gnus-treat-newsgroups-picon 'highlight t)
1547
1548 (defcustom gnus-treat-from-gravatar nil
1549   "Display gravatars in the From header.
1550 Valid values are nil, t, `head', `first', `last', an integer or a
1551 predicate.  See Info node `(gnus)Customizing Articles' and Info
1552 node `(gnus)Gravatars' for details."
1553   :version "24.1"
1554   :group 'gnus-article-treat
1555   :group 'gnus-gravatar
1556   :link '(custom-manual "(gnus)Customizing Articles")
1557   :link '(custom-manual "(gnus)Gravatars")
1558   :type gnus-article-treat-head-custom)
1559 (put 'gnus-treat-from-gravatar 'highlight t)
1560
1561 (defcustom gnus-treat-mail-gravatar nil
1562   "Display gravatars in To and Cc headers.
1563 Valid values are nil, t, `head', `first', `last', an integer or a
1564 predicate.  See Info node `(gnus)Customizing Articles' and Info
1565 node `(gnus)Gravatars' for details."
1566   :version "24.1"
1567   :group 'gnus-article-treat
1568   :group 'gnus-gravatar
1569   :link '(custom-manual "(gnus)Customizing Articles")
1570   :link '(custom-manual "(gnus)Gravatars")
1571   :type gnus-article-treat-head-custom)
1572 (put 'gnus-treat-mail-gravatar 'highlight t)
1573
1574 (defcustom gnus-treat-body-boundary
1575   (if (or gnus-treat-newsgroups-picon
1576           gnus-treat-mail-picon
1577           gnus-treat-from-picon
1578           gnus-treat-from-gravatar
1579           gnus-treat-mail-gravatar)
1580       ;; If there's much decoration, the user might prefer a boundery.
1581       'head
1582     nil)
1583   "Draw a boundary at the end of the headers.
1584 Valid values are nil and `head'.
1585 See Info node `(gnus)Customizing Articles' for details."
1586   :version "22.1"
1587   :group 'gnus-article-treat
1588   :link '(custom-manual "(gnus)Customizing Articles")
1589   :type gnus-article-treat-head-custom)
1590
1591 (defcustom gnus-treat-capitalize-sentences nil
1592   "Capitalize sentence-starting words.
1593 Valid values are nil, t, `head', `first', `last', an integer or a
1594 predicate.  See Info node `(gnus)Customizing Articles'."
1595   :version "21.1"
1596   :group 'gnus-article-treat
1597   :link '(custom-manual "(gnus)Customizing Articles")
1598   :type gnus-article-treat-custom)
1599
1600 (defcustom gnus-treat-wash-html nil
1601   "Format as HTML.
1602 Valid values are nil, t, `head', `first', `last', an integer or a
1603 predicate.  See Info node `(gnus)Customizing Articles'."
1604   :version "22.1"
1605   :group 'gnus-article-treat
1606   :link '(custom-manual "(gnus)Customizing Articles")
1607   :type gnus-article-treat-custom)
1608
1609 (defcustom gnus-treat-fill-long-lines nil
1610   "Fill long lines.
1611 Valid values are nil, t, `head', `first', `last', an integer or a
1612 predicate.  See Info node `(gnus)Customizing Articles'."
1613   :group 'gnus-article-treat
1614   :link '(custom-manual "(gnus)Customizing Articles")
1615   :type gnus-article-treat-custom)
1616
1617 (defcustom gnus-treat-play-sounds nil
1618   "Play sounds.
1619 Valid values are nil, t, `head', `first', `last', an integer or a
1620 predicate.  See Info node `(gnus)Customizing Articles'."
1621   :version "21.1"
1622   :group 'gnus-article-treat
1623   :link '(custom-manual "(gnus)Customizing Articles")
1624   :type gnus-article-treat-custom)
1625
1626 (defcustom gnus-treat-x-pgp-sig nil
1627   "Verify X-PGP-Sig.
1628 To automatically treat X-PGP-Sig, set it to head.
1629 Valid values are nil, t, `head', `first', `last', an integer or a
1630 predicate.  See Info node `(gnus)Customizing Articles'."
1631   :version "22.1"
1632   :group 'gnus-article-treat
1633   :group 'mime-security
1634   :link '(custom-manual "(gnus)Customizing Articles")
1635   :type gnus-article-treat-custom)
1636
1637 (defvar gnus-article-encrypt-protocol-alist
1638   '(("PGP" . mml2015-self-encrypt)))
1639
1640 ;; Set to nil if more than one protocol added to
1641 ;; gnus-article-encrypt-protocol-alist.
1642 (defcustom gnus-article-encrypt-protocol "PGP"
1643   "The protocol used for encrypt articles.
1644 It is a string, such as \"PGP\". If nil, ask user."
1645   :version "22.1"
1646   :type 'string
1647   :group 'mime-security)
1648
1649 (defvar gnus-article-wash-function nil
1650   "Function used for converting HTML into text.")
1651
1652 (defcustom gnus-use-idna (and (condition-case nil (require 'idna) (file-error))
1653                               (mm-coding-system-p 'utf-8)
1654                               (executable-find idna-program))
1655   "Whether IDNA decoding of headers is used when viewing messages.
1656 This requires GNU Libidn, and by default only enabled if it is found."
1657   :version "22.1"
1658   :group 'gnus-article-headers
1659   :type 'boolean)
1660
1661 (defcustom gnus-article-over-scroll nil
1662   "If non-nil, allow scrolling the article buffer even when there no more text."
1663   :version "22.1"
1664   :group 'gnus-article
1665   :type 'boolean)
1666
1667 ;;; Internal variables
1668
1669 (defvar gnus-english-month-names
1670   '("January" "February" "March" "April" "May" "June" "July" "August"
1671     "September" "October" "November" "December"))
1672
1673 (defvar article-goto-body-goes-to-point-min-p nil)
1674 (defvar gnus-article-wash-types nil)
1675 (defvar gnus-article-emphasis-alist nil)
1676 (defvar gnus-article-image-alist nil)
1677
1678 (defvar gnus-article-mime-handle-alist-1 nil)
1679 (defvar gnus-treatment-function-alist
1680   '((gnus-treat-x-pgp-sig gnus-article-verify-x-pgp-sig)
1681     (gnus-treat-strip-banner gnus-article-strip-banner)
1682     (gnus-treat-strip-headers-in-body gnus-article-strip-headers-in-body)
1683     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1684     (gnus-treat-buttonize gnus-article-add-buttons)
1685     (gnus-treat-fill-article gnus-article-fill-cited-article)
1686     (gnus-treat-fill-long-lines gnus-article-fill-long-lines)
1687     (gnus-treat-strip-cr gnus-article-remove-cr)
1688     (gnus-treat-unsplit-urls gnus-article-unsplit-urls)
1689     (gnus-treat-date-ut gnus-article-date-ut)
1690     (gnus-treat-date-local gnus-article-date-local)
1691     (gnus-treat-date-english gnus-article-date-english)
1692     (gnus-treat-date-original gnus-article-date-original)
1693     (gnus-treat-date-user-defined gnus-article-date-user)
1694     (gnus-treat-date-iso8601 gnus-article-date-iso8601)
1695     (gnus-treat-date-lapsed gnus-article-date-lapsed)
1696     (gnus-treat-display-x-face gnus-article-display-x-face)
1697     (gnus-treat-display-face gnus-article-display-face)
1698     (gnus-treat-hide-headers gnus-article-maybe-hide-headers)
1699     (gnus-treat-hide-boring-headers gnus-article-hide-boring-headers)
1700     (gnus-treat-hide-signature gnus-article-hide-signature)
1701     (gnus-treat-strip-list-identifiers gnus-article-hide-list-identifiers)
1702     (gnus-treat-leading-whitespace gnus-article-remove-leading-whitespace)
1703     (gnus-treat-strip-pem gnus-article-hide-pem)
1704     (gnus-treat-from-picon gnus-treat-from-picon)
1705     (gnus-treat-mail-picon gnus-treat-mail-picon)
1706     (gnus-treat-newsgroups-picon gnus-treat-newsgroups-picon)
1707     (gnus-treat-from-gravatar gnus-treat-from-gravatar)
1708     (gnus-treat-mail-gravatar gnus-treat-mail-gravatar)
1709     (gnus-treat-highlight-headers gnus-article-highlight-headers)
1710     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1711     (gnus-treat-strip-trailing-blank-lines
1712      gnus-article-remove-trailing-blank-lines)
1713     (gnus-treat-strip-leading-blank-lines
1714      gnus-article-strip-leading-blank-lines)
1715     (gnus-treat-strip-multiple-blank-lines
1716      gnus-article-strip-multiple-blank-lines)
1717     (gnus-treat-overstrike gnus-article-treat-overstrike)
1718     (gnus-treat-ansi-sequences gnus-article-treat-ansi-sequences)
1719     (gnus-treat-unfold-headers gnus-article-treat-unfold-headers)
1720     (gnus-treat-fold-newsgroups gnus-article-treat-fold-newsgroups)
1721     (gnus-treat-fold-headers gnus-article-treat-fold-headers)
1722     (gnus-treat-buttonize-head gnus-article-add-buttons-to-head)
1723     (gnus-treat-display-smileys gnus-treat-smiley)
1724     (gnus-treat-capitalize-sentences gnus-article-capitalize-sentences)
1725     (gnus-treat-wash-html gnus-article-wash-html)
1726     (gnus-treat-emphasize gnus-article-emphasize)
1727     (gnus-treat-hide-citation gnus-article-hide-citation)
1728     (gnus-treat-hide-citation-maybe gnus-article-hide-citation-maybe)
1729     (gnus-treat-highlight-citation gnus-article-highlight-citation)
1730     (gnus-treat-body-boundary gnus-article-treat-body-boundary)
1731     (gnus-treat-play-sounds gnus-earcon-display)))
1732
1733 (defvar gnus-article-mime-handle-alist nil)
1734 (defvar article-lapsed-timer nil)
1735 (defvar gnus-article-current-summary nil)
1736
1737 (defvar gnus-article-mode-syntax-table
1738   (let ((table (copy-syntax-table text-mode-syntax-table)))
1739     ;; This causes the citation match run O(2^n).
1740     ;; (modify-syntax-entry ?- "w" table)
1741     (modify-syntax-entry ?> ")<" table)
1742     (modify-syntax-entry ?< "(>" table)
1743     ;; make M-. in article buffers work for `foo' strings
1744     (modify-syntax-entry ?' " " table)
1745     (modify-syntax-entry ?` " " table)
1746     table)
1747   "Syntax table used in article mode buffers.
1748 Initialized from `text-mode-syntax-table.")
1749
1750 (defvar gnus-save-article-buffer nil)
1751
1752 (defvar gnus-number-of-articles-to-be-saved nil)
1753
1754 (defvar gnus-inhibit-hiding nil)
1755
1756 (defvar gnus-article-edit-mode nil)
1757
1758 ;;; Macros for dealing with the article buffer.
1759
1760 (defmacro gnus-with-article-headers (&rest forms)
1761   `(with-current-buffer gnus-article-buffer
1762      (save-restriction
1763        (let ((inhibit-read-only t)
1764              (inhibit-point-motion-hooks t)
1765              (case-fold-search t))
1766          (article-narrow-to-head)
1767          ,@forms))))
1768
1769 (put 'gnus-with-article-headers 'lisp-indent-function 0)
1770 (put 'gnus-with-article-headers 'edebug-form-spec '(body))
1771
1772 (defmacro gnus-with-article-buffer (&rest forms)
1773   `(with-current-buffer gnus-article-buffer
1774      (let ((inhibit-read-only t))
1775        ,@forms)))
1776
1777 (put 'gnus-with-article-buffer 'lisp-indent-function 0)
1778 (put 'gnus-with-article-buffer 'edebug-form-spec '(body))
1779
1780 (defun gnus-article-goto-header (header)
1781   "Go to HEADER, which is a regular expression."
1782   (re-search-forward (concat "^\\(" header "\\):") nil t))
1783
1784 (defsubst gnus-article-hide-text (b e props)
1785   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
1786   (gnus-add-text-properties-when 'article-type nil b e props)
1787   (when (memq 'intangible props)
1788     (put-text-property
1789      (max (1- b) (point-min))
1790      b 'intangible (cddr (memq 'intangible props)))))
1791
1792 (defsubst gnus-article-unhide-text (b e)
1793   "Remove hidden text properties from region between B and E."
1794   (remove-text-properties b e gnus-hidden-properties)
1795   (when (memq 'intangible gnus-hidden-properties)
1796     (put-text-property (max (1- b) (point-min))
1797                        b 'intangible nil)))
1798
1799 (defun gnus-article-hide-text-type (b e type)
1800   "Hide text of TYPE between B and E."
1801   (gnus-add-wash-type type)
1802   (gnus-article-hide-text
1803    b e (cons 'article-type (cons type gnus-hidden-properties))))
1804
1805 (defun gnus-article-unhide-text-type (b e type)
1806   "Unhide text of TYPE between B and E."
1807   (gnus-delete-wash-type type)
1808   (remove-text-properties
1809    b e (cons 'article-type (cons type gnus-hidden-properties)))
1810   (when (memq 'intangible gnus-hidden-properties)
1811     (put-text-property (max (1- b) (point-min))
1812                        b 'intangible nil)))
1813
1814 (defun gnus-article-hide-text-of-type (type)
1815   "Hide text of TYPE in the current buffer."
1816   (save-excursion
1817     (let ((b (point-min))
1818           (e (point-max)))
1819       (while (setq b (text-property-any b e 'article-type type))
1820         (add-text-properties b (incf b) gnus-hidden-properties)))))
1821
1822 (defun gnus-article-delete-text-of-type (type)
1823   "Delete text of TYPE in the current buffer."
1824   (save-excursion
1825     (let ((b (point-min)))
1826       (if (eq type 'multipart)
1827           ;; Remove MIME buttons associated with multipart/alternative parts.
1828           (progn
1829             (goto-char b)
1830             (while (if (get-text-property (point) 'gnus-part)
1831                        (setq b (point))
1832                      (when (setq b (next-single-property-change (point)
1833                                                                 'gnus-part))
1834                        (goto-char b)
1835                        t))
1836               (end-of-line)
1837               (skip-chars-forward "\n")
1838               (when (eq (get-text-property b 'article-type) 'multipart)
1839                 (delete-region b (point)))))
1840         (while (setq b (text-property-any b (point-max) 'article-type type))
1841           (delete-region
1842            b (or (text-property-not-all b (point-max) 'article-type type)
1843                  (point-max))))))))
1844
1845 (defun gnus-article-delete-invisible-text ()
1846   "Delete all invisible text in the current buffer."
1847   (save-excursion
1848     (let ((b (point-min)))
1849       (while (setq b (text-property-any b (point-max) 'invisible t))
1850         (delete-region
1851          b (or (text-property-not-all b (point-max) 'invisible t)
1852                (point-max)))))))
1853
1854 (defun gnus-article-text-type-exists-p (type)
1855   "Say whether any text of type TYPE exists in the buffer."
1856   (text-property-any (point-min) (point-max) 'article-type type))
1857
1858 (defsubst gnus-article-header-rank ()
1859   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
1860   (let ((list gnus-sorted-header-list)
1861         (i 1))
1862     (while list
1863       (if (looking-at (car list))
1864           (setq list nil)
1865         (setq list (cdr list))
1866         (incf i)))
1867       i))
1868
1869 (defun article-hide-headers (&optional arg delete)
1870   "Hide unwanted headers and possibly sort them as well."
1871   (interactive)
1872   ;; This function might be inhibited.
1873   (unless gnus-inhibit-hiding
1874     (let ((inhibit-read-only t)
1875           (case-fold-search t)
1876           (max (1+ (length gnus-sorted-header-list)))
1877           (inhibit-point-motion-hooks t)
1878           (cur (current-buffer))
1879           ignored visible beg)
1880       (save-excursion
1881         ;; `gnus-ignored-headers' and `gnus-visible-headers' may be
1882         ;; group parameters, so we should go to the summary buffer.
1883         (when (prog1
1884                   (condition-case nil
1885                       (progn (set-buffer gnus-summary-buffer) t)
1886                     (error nil))
1887                 (setq ignored (when (not gnus-visible-headers)
1888                                 (cond ((stringp gnus-ignored-headers)
1889                                        gnus-ignored-headers)
1890                                       ((listp gnus-ignored-headers)
1891                                        (mapconcat 'identity
1892                                                   gnus-ignored-headers
1893                                                   "\\|"))))
1894                       visible (cond ((stringp gnus-visible-headers)
1895                                      gnus-visible-headers)
1896                                     ((and gnus-visible-headers
1897                                           (listp gnus-visible-headers))
1898                                      (mapconcat 'identity
1899                                                 gnus-visible-headers
1900                                                 "\\|")))))
1901           (set-buffer cur))
1902         (save-restriction
1903           ;; First we narrow to just the headers.
1904           (article-narrow-to-head)
1905           ;; Hide any "From " lines at the beginning of (mail) articles.
1906           (while (looking-at "From ")
1907             (forward-line 1))
1908           (unless (bobp)
1909             (delete-region (point-min) (point)))
1910           ;; Then treat the rest of the header lines.
1911           ;; Then we use the two regular expressions
1912           ;; `gnus-ignored-headers' and `gnus-visible-headers' to
1913           ;; select which header lines is to remain visible in the
1914           ;; article buffer.
1915           (while (re-search-forward "^[^ \t:]*:" nil t)
1916             (beginning-of-line)
1917             ;; Mark the rank of the header.
1918             (put-text-property
1919              (point) (1+ (point)) 'message-rank
1920              (if (or (and visible (looking-at visible))
1921                      (and ignored
1922                           (not (looking-at ignored))))
1923                  (gnus-article-header-rank)
1924                (+ 2 max)))
1925             (forward-line 1))
1926           (message-sort-headers-1)
1927           (when (setq beg (text-property-any
1928                            (point-min) (point-max) 'message-rank (+ 2 max)))
1929             ;; We delete the unwanted headers.
1930             (gnus-add-wash-type 'headers)
1931             (add-text-properties (point-min) (+ 5 (point-min))
1932                                  '(article-type headers dummy-invisible t))
1933             (delete-region beg (point-max))))))))
1934
1935 (defun article-hide-boring-headers (&optional arg)
1936   "Toggle hiding of headers that aren't very interesting.
1937 If given a negative prefix, always show; if given a positive prefix,
1938 always hide."
1939   (interactive (gnus-article-hidden-arg))
1940   (when (and (not (gnus-article-check-hidden-text 'boring-headers arg))
1941              (not gnus-show-all-headers))
1942     (save-excursion
1943       (save-restriction
1944         (let ((inhibit-read-only t)
1945               (inhibit-point-motion-hooks t))
1946           (article-narrow-to-head)
1947           (dolist (elem gnus-boring-article-headers)
1948             (goto-char (point-min))
1949             (cond
1950              ;; Hide empty headers.
1951              ((eq elem 'empty)
1952               (while (re-search-forward "^[^: \t]+:[ \t]*\n[^ \t]" nil t)
1953                 (forward-line -1)
1954                 (gnus-article-hide-text-type
1955                  (point-at-bol)
1956                  (progn
1957                    (end-of-line)
1958                    (if (re-search-forward "^[^ \t]" nil t)
1959                        (match-beginning 0)
1960                      (point-max)))
1961                  'boring-headers)))
1962              ;; Hide boring Newsgroups header.
1963              ((eq elem 'newsgroups)
1964               (when (gnus-string-equal
1965                      (gnus-fetch-field "newsgroups")
1966                      (gnus-group-real-name
1967                       (if (boundp 'gnus-newsgroup-name)
1968                           gnus-newsgroup-name
1969                         "")))
1970                 (gnus-article-hide-header "newsgroups")))
1971              ((eq elem 'to-address)
1972               (let ((to (message-fetch-field "to"))
1973                     (to-address
1974                      (gnus-parameter-to-address
1975                       (if (boundp 'gnus-newsgroup-name)
1976                           gnus-newsgroup-name ""))))
1977                 (when (and to to-address
1978                            (ignore-errors
1979                              (gnus-string-equal
1980                               ;; only one address in To
1981                               (nth 1 (mail-extract-address-components to))
1982                               to-address)))
1983                   (gnus-article-hide-header "to"))))
1984              ((eq elem 'to-list)
1985               (let ((to (message-fetch-field "to"))
1986                     (to-list
1987                      (gnus-parameter-to-list
1988                       (if (boundp 'gnus-newsgroup-name)
1989                           gnus-newsgroup-name ""))))
1990                 (when (and to to-list
1991                            (ignore-errors
1992                              (gnus-string-equal
1993                               ;; only one address in To
1994                               (nth 1 (mail-extract-address-components to))
1995                               to-list)))
1996                   (gnus-article-hide-header "to"))))
1997              ((eq elem 'cc-list)
1998               (let ((cc (message-fetch-field "cc"))
1999                     (to-list
2000                      (gnus-parameter-to-list
2001                       (if (boundp 'gnus-newsgroup-name)
2002                           gnus-newsgroup-name ""))))
2003                 (when (and cc to-list
2004                            (ignore-errors
2005                              (gnus-string-equal
2006                               ;; only one address in CC
2007                               (nth 1 (mail-extract-address-components cc))
2008                               to-list)))
2009                   (gnus-article-hide-header "cc"))))
2010              ((eq elem 'followup-to)
2011               (when (gnus-string-equal
2012                      (message-fetch-field "followup-to")
2013                      (message-fetch-field "newsgroups"))
2014                 (gnus-article-hide-header "followup-to")))
2015              ((eq elem 'reply-to)
2016               (if (gnus-group-find-parameter
2017                    gnus-newsgroup-name 'broken-reply-to)
2018                   (gnus-article-hide-header "reply-to")
2019                 (let ((from (message-fetch-field "from"))
2020                       (reply-to (message-fetch-field "reply-to")))
2021                   (when
2022                       (and
2023                        from reply-to
2024                        (ignore-errors
2025                          (equal
2026                           (sort (mapcar
2027                                  (lambda (x) (downcase (cadr x)))
2028                                  (mail-extract-address-components from t))
2029                                 'string<)
2030                           (sort (mapcar
2031                                  (lambda (x) (downcase (cadr x)))
2032                                  (mail-extract-address-components reply-to t))
2033                                 'string<))))
2034                     (gnus-article-hide-header "reply-to")))))
2035              ((eq elem 'date)
2036               (let ((date (with-current-buffer gnus-original-article-buffer
2037                             ;; If date in `gnus-article-buffer' is localized
2038                             ;; (`gnus-treat-date-user-defined'),
2039                             ;; `days-between' might fail.
2040                             (message-fetch-field "date"))))
2041                 (when (and date
2042                            (< (days-between (current-time-string) date)
2043                               4))
2044                   (gnus-article-hide-header "date"))))
2045              ((eq elem 'long-to)
2046               (let ((to (message-fetch-field "to"))
2047                     (cc (message-fetch-field "cc")))
2048                 (when (> (length to) 1024)
2049                   (gnus-article-hide-header "to"))
2050                 (when (> (length cc) 1024)
2051                   (gnus-article-hide-header "cc"))))
2052              ((eq elem 'many-to)
2053               (let ((to-count 0)
2054                     (cc-count 0))
2055                 (goto-char (point-min))
2056                 (while (re-search-forward "^to:" nil t)
2057                   (setq to-count (1+ to-count)))
2058                 (when (> to-count 1)
2059                   (while (> to-count 0)
2060                     (goto-char (point-min))
2061                     (save-restriction
2062                       (re-search-forward "^to:" nil nil to-count)
2063                       (forward-line -1)
2064                       (narrow-to-region (point) (point-max))
2065                       (gnus-article-hide-header "to"))
2066                     (setq to-count (1- to-count))))
2067                 (goto-char (point-min))
2068                 (while (re-search-forward "^cc:" nil t)
2069                   (setq cc-count (1+ cc-count)))
2070                 (when (> cc-count 1)
2071                   (while (> cc-count 0)
2072                     (goto-char (point-min))
2073                     (save-restriction
2074                       (re-search-forward "^cc:" nil nil cc-count)
2075                       (forward-line -1)
2076                       (narrow-to-region (point) (point-max))
2077                       (gnus-article-hide-header "cc"))
2078                     (setq cc-count (1- cc-count)))))))))))))
2079
2080 (defun gnus-article-hide-header (header)
2081   (save-excursion
2082     (goto-char (point-min))
2083     (when (re-search-forward (concat "^" header ":") nil t)
2084       (gnus-article-hide-text-type
2085        (point-at-bol)
2086        (progn
2087          (end-of-line)
2088          (if (re-search-forward "^[^ \t]" nil t)
2089              (match-beginning 0)
2090            (point-max)))
2091        'boring-headers))))
2092
2093 (defvar gnus-article-normalized-header-length 40
2094   "Length of normalized headers.")
2095
2096 (defun article-normalize-headers ()
2097   "Make all header lines 40 characters long."
2098   (interactive)
2099   (let ((inhibit-read-only t)
2100         column)
2101     (save-excursion
2102       (save-restriction
2103         (article-narrow-to-head)
2104         (while (not (eobp))
2105           (cond
2106            ((< (setq column (- (point-at-eol) (point)))
2107                gnus-article-normalized-header-length)
2108             (end-of-line)
2109             (insert (make-string
2110                      (- gnus-article-normalized-header-length column)
2111                      ? )))
2112            ((> column gnus-article-normalized-header-length)
2113             (gnus-put-text-property
2114              (progn
2115                (forward-char gnus-article-normalized-header-length)
2116                (point))
2117              (point-at-eol)
2118              'invisible t))
2119            (t
2120             ;; Do nothing.
2121             ))
2122           (forward-line 1))))))
2123
2124 (defun article-treat-dumbquotes ()
2125   "Translate M****s*** sm*rtq**t*s and other symbols into proper text.
2126 Note that this function guesses whether a character is a sm*rtq**t* or
2127 not, so it should only be used interactively.
2128
2129 Sm*rtq**t*s are M****s***'s unilateral extension to the
2130 iso-8859-1 character map in an attempt to provide more quoting
2131 characters.  If you see something like \\222 or \\264 where
2132 you're expecting some kind of apostrophe or quotation mark, then
2133 try this wash."
2134   (interactive)
2135   (article-translate-strings gnus-article-dumbquotes-map))
2136
2137 (defun article-translate-characters (from to)
2138   "Translate all characters in the body of the article according to FROM and TO.
2139 FROM is a string of characters to translate from; to is a string of
2140 characters to translate to."
2141   (save-excursion
2142     (when (article-goto-body)
2143       (let ((inhibit-read-only t)
2144             (x (make-string 225 ?x))
2145             (i -1))
2146         (while (< (incf i) (length x))
2147           (aset x i i))
2148         (setq i 0)
2149         (while (< i (length from))
2150           (aset x (aref from i) (aref to i))
2151           (incf i))
2152         (translate-region (point) (point-max) x)))))
2153
2154 (defun article-translate-strings (map)
2155   "Translate all string in the body of the article according to MAP.
2156 MAP is an alist where the elements are on the form (\"from\" \"to\")."
2157   (save-excursion
2158     (when (article-goto-body)
2159       (let ((inhibit-read-only t))
2160         (dolist (elem map)
2161           (save-excursion
2162             (while (search-forward (car elem) nil t)
2163               (replace-match (cadr elem)))))))))
2164
2165 (defun article-treat-overstrike ()
2166   "Translate overstrikes into bold text."
2167   (interactive)
2168   (save-excursion
2169     (when (article-goto-body)
2170       (let ((inhibit-read-only t))
2171         (while (search-forward "\b" nil t)
2172           (let ((next (char-after))
2173                 (previous (char-after (- (point) 2))))
2174             ;; We do the boldification/underlining by hiding the
2175             ;; overstrikes and putting the proper text property
2176             ;; on the letters.
2177             (cond
2178              ((eq next previous)
2179               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2180               (put-text-property (point) (1+ (point)) 'face 'bold))
2181              ((eq next ?_)
2182               (gnus-article-hide-text-type
2183                (1- (point)) (1+ (point)) 'overstrike)
2184               (put-text-property
2185                (- (point) 2) (1- (point)) 'face 'underline))
2186              ((eq previous ?_)
2187               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2188               (put-text-property
2189                (point) (1+ (point)) 'face 'underline)))))))))
2190
2191 (defun article-treat-ansi-sequences ()
2192   "Translate ANSI SGR control sequences into overlays or extents."
2193   (interactive)
2194   (save-excursion
2195     (when (article-goto-body)
2196       (let ((inhibit-read-only t))
2197         (ansi-color-apply-on-region (point) (point-max))))))
2198
2199 (defun gnus-article-treat-unfold-headers ()
2200   "Unfold folded message headers.
2201 Only the headers that fit into the current window width will be
2202 unfolded."
2203   (interactive)
2204   (gnus-with-article-headers
2205     (let (length)
2206       (while (not (eobp))
2207         (save-restriction
2208           (mail-header-narrow-to-field)
2209           (let* ((header (buffer-string))
2210                  (unfoldable
2211                   (or (equal gnus-article-unfold-long-headers t)
2212                       (and (stringp gnus-article-unfold-long-headers)
2213                            (string-match gnus-article-unfold-long-headers header)))))
2214             (with-temp-buffer
2215               (insert header)
2216               (goto-char (point-min))
2217               (while (re-search-forward "\n[\t ]" nil t)
2218                 (replace-match " " t t)))
2219             (setq length (- (point-max) (point-min) 1))
2220             (when (or unfoldable
2221                       (< length (window-width)))
2222               (while (re-search-forward "\n[\t ]" nil t)
2223                 (replace-match " " t t))))
2224           (goto-char (point-max)))))))
2225
2226 (defun gnus-article-treat-fold-headers ()
2227   "Fold message headers."
2228   (interactive)
2229   (gnus-with-article-headers
2230     (while (not (eobp))
2231       (save-restriction
2232         (mail-header-narrow-to-field)
2233         (mail-header-fold-field)
2234         (goto-char (point-max))))))
2235
2236 (defun gnus-treat-smiley ()
2237   "Toggle display of textual emoticons (\"smileys\") as small graphical icons."
2238   (interactive)
2239   (gnus-with-article-buffer
2240     (if (memq 'smiley gnus-article-wash-types)
2241         (gnus-delete-images 'smiley)
2242       (article-goto-body)
2243       (let ((images (smiley-region (point) (point-max))))
2244         (when images
2245           (gnus-add-wash-type 'smiley)
2246           (dolist (image images)
2247             (gnus-add-image 'smiley image)))))))
2248
2249 (defun gnus-article-remove-images ()
2250   "Remove all images from the article buffer."
2251   (interactive)
2252   (gnus-with-article-buffer
2253     (dolist (elem gnus-article-image-alist)
2254       (gnus-delete-images (car elem)))))
2255
2256 (defun gnus-article-treat-fold-newsgroups ()
2257   "Unfold folded message headers.
2258 Only the headers that fit into the current window width will be
2259 unfolded."
2260   (interactive)
2261   (gnus-with-article-headers
2262     (while (gnus-article-goto-header "newsgroups\\|followup-to")
2263       (save-restriction
2264         (mail-header-narrow-to-field)
2265         (while (re-search-forward ", *" nil t)
2266           (replace-match ", " t t))
2267         (mail-header-fold-field)
2268         (goto-char (point-max))))))
2269
2270 (defcustom gnus-article-truncate-lines (default-value 'truncate-lines)
2271   "Value of `truncate-lines' in Gnus Article buffer.
2272 Valid values are nil, t, `head', `first', `last', an integer or a
2273 predicate.  See Info node `(gnus)Customizing Articles'."
2274   :version "23.1" ;; No Gnus
2275   :group 'gnus-article
2276   ;; :link '(custom-manual "(gnus)Customizing Articles")
2277   :type 'boolean)
2278
2279 (defun gnus-article-toggle-truncate-lines (&optional arg)
2280   "Toggle whether to fold or truncate long lines in article the buffer.
2281 If ARG is non-nil and not a number, toggle
2282 `gnus-article-truncate-lines' too.  If ARG is a number, truncate
2283 long lines if and only if arg is positive."
2284   (interactive "P")
2285   (cond
2286    ((and (numberp arg) (> arg 0))
2287     (setq gnus-article-truncate-lines t))
2288    ((numberp arg)
2289     (setq gnus-article-truncate-lines nil))
2290    (arg
2291     (setq gnus-article-truncate-lines
2292           (not gnus-article-truncate-lines))))
2293   (gnus-with-article-buffer
2294     (cond
2295      ((and (numberp arg) (> arg 0))
2296       (setq truncate-lines nil))
2297      ((numberp arg)
2298       (setq truncate-lines t)))
2299     ;; In versions of Emacs 22 (CVS) before 2006-05-26,
2300     ;; `toggle-truncate-lines' needs an argument.
2301     (toggle-truncate-lines)))
2302
2303 (defun gnus-article-treat-body-boundary ()
2304   "Place a boundary line at the end of the headers."
2305   (interactive)
2306   (when (and gnus-body-boundary-delimiter
2307              (> (length gnus-body-boundary-delimiter) 0))
2308     (gnus-with-article-headers
2309       (goto-char (point-max))
2310       (let ((start (point)))
2311         (insert "X-Boundary: ")
2312         (gnus-add-text-properties start (point) '(invisible t intangible t))
2313         (insert (let (str)
2314                   (while (>= (1- (window-width)) (length str))
2315                     (setq str (concat str gnus-body-boundary-delimiter)))
2316                   (substring str 0 (1- (window-width))))
2317                 "\n")
2318         (gnus-put-text-property start (point) 'gnus-decoration 'header)))))
2319
2320 (defun article-fill-long-lines ()
2321   "Fill lines that are wider than the window width."
2322   (interactive)
2323   (save-excursion
2324     (let ((inhibit-read-only t)
2325           (width (window-width (get-buffer-window (current-buffer)))))
2326       (save-restriction
2327         (article-goto-body)
2328         (let ((adaptive-fill-mode nil)) ;Why?  -sm
2329           (while (not (eobp))
2330             (end-of-line)
2331             (when (>= (current-column) (min fill-column width))
2332               (narrow-to-region (min (1+ (point)) (point-max))
2333                                 (point-at-bol))
2334               (let ((goback (point-marker)))
2335                 (fill-paragraph nil)
2336                 (goto-char (marker-position goback)))
2337               (widen))
2338             (forward-line 1)))))))
2339
2340 (defun article-capitalize-sentences ()
2341   "Capitalize the first word in each sentence."
2342   (interactive)
2343   (save-excursion
2344     (let ((inhibit-read-only t)
2345           (paragraph-start "^[\n\^L]"))
2346       (article-goto-body)
2347       (while (not (eobp))
2348         (capitalize-word 1)
2349         (forward-sentence)))))
2350
2351 (defun article-remove-cr ()
2352   "Remove trailing CRs and then translate remaining CRs into LFs."
2353   (interactive)
2354   (save-excursion
2355     (let ((inhibit-read-only t))
2356       (goto-char (point-min))
2357       (while (re-search-forward "\r+$" nil t)
2358         (replace-match "" t t))
2359       (goto-char (point-min))
2360       (while (search-forward "\r" nil t)
2361         (replace-match "\n" t t)))))
2362
2363 (defun article-remove-trailing-blank-lines ()
2364   "Remove all trailing blank lines from the article."
2365   (interactive)
2366   (save-excursion
2367     (let ((inhibit-read-only t))
2368       (goto-char (point-max))
2369       (delete-region
2370        (point)
2371        (progn
2372          (while (and (not (bobp))
2373                      (looking-at "^[ \t]*$")
2374                      (not (gnus-annotation-in-region-p
2375                            (point) (point-at-eol))))
2376            (forward-line -1))
2377          (forward-line 1)
2378          (point))))))
2379
2380 (defvar gnus-face-properties-alist)
2381
2382 (defun article-display-face (&optional force)
2383   "Display any Face headers in the header."
2384   (interactive (list 'force))
2385   (let ((wash-face-p buffer-read-only))
2386     (gnus-with-article-headers
2387       ;; When displaying parts, this function can be called several times on
2388       ;; the same article, without any intended toggle semantic (as typing `W
2389       ;; D d' would have). So face deletion must occur only when we come from
2390       ;; an interactive command, that is when the *Article* buffer is
2391       ;; read-only.
2392       (if (and wash-face-p (memq 'face gnus-article-wash-types))
2393           (gnus-delete-images 'face)
2394         (let ((from (message-fetch-field "from"))
2395               face faces)
2396           (save-current-buffer
2397             (when (and wash-face-p
2398                        (gnus-buffer-live-p gnus-original-article-buffer)
2399                        (not (re-search-forward "^Face:[\t ]*" nil t)))
2400               (set-buffer gnus-original-article-buffer))
2401             (save-restriction
2402               (mail-narrow-to-head)
2403               (when (or force
2404                         ;; Check whether this face is censored.
2405                         (not (and gnus-article-x-face-too-ugly
2406                                   (or from
2407                                       (setq from (message-fetch-field "from")))
2408                                   (string-match gnus-article-x-face-too-ugly
2409                                                 from))))
2410                 (while (gnus-article-goto-header "Face")
2411                   (push (mail-header-field-value) faces)))))
2412           (when faces
2413             (goto-char (point-min))
2414             (let (png image)
2415               (unless (setq from (gnus-article-goto-header "from"))
2416                 (insert "From:")
2417                 (setq from (point))
2418                 (insert " [no `from' set]\n"))
2419               (while faces
2420                 (when (setq png (gnus-convert-face-to-png (pop faces)))
2421                   (setq image
2422                         (apply 'gnus-create-image png 'png t
2423                                (cdr (assq 'png gnus-face-properties-alist))))
2424                   (goto-char from)
2425                   (gnus-add-wash-type 'face)
2426                   (gnus-add-image 'face image)
2427                   (gnus-put-image image nil 'face))))))))))
2428
2429 (defun article-display-x-face (&optional force)
2430   "Look for an X-Face header and display it if present."
2431   (interactive (list 'force))
2432   (let ((wash-face-p buffer-read-only)) ;; When type `W f'
2433     (gnus-with-article-headers
2434       ;; Delete the old process, if any.
2435       (when (process-status "article-x-face")
2436         (delete-process "article-x-face"))
2437       ;; See the comment in `article-display-face'.
2438       (if (and wash-face-p (memq 'xface gnus-article-wash-types))
2439           ;; We have already displayed X-Faces, so we remove them
2440           ;; instead.
2441           (gnus-delete-images 'xface)
2442         ;; Display X-Faces.
2443         (let ((from (message-fetch-field "from"))
2444               x-faces face)
2445           (save-current-buffer
2446             (when (and wash-face-p
2447                        (gnus-buffer-live-p gnus-original-article-buffer)
2448                        (not (re-search-forward "^X-Face:[\t ]*" nil t)))
2449               ;; If type `W f', use gnus-original-article-buffer,
2450               ;; otherwise use the current buffer because displaying
2451               ;; RFC822 parts calls this function too.
2452               (set-buffer gnus-original-article-buffer))
2453             (save-restriction
2454               (mail-narrow-to-head)
2455               (and gnus-article-x-face-command
2456                    (or force
2457                        ;; Check whether this face is censored.
2458                        (not (and gnus-article-x-face-too-ugly
2459                                  (or from
2460                                      (setq from (message-fetch-field "from")))
2461                                  (string-match gnus-article-x-face-too-ugly
2462                                                from))))
2463                    (while (gnus-article-goto-header "X-Face")
2464                      (push (mail-header-field-value) x-faces)))))
2465           (when x-faces
2466             ;; We display the face.
2467             (cond ((functionp gnus-article-x-face-command)
2468                    ;; The command is a lisp function, so we call it.
2469                    (mapc gnus-article-x-face-command x-faces))
2470                   ((stringp gnus-article-x-face-command)
2471                    ;; The command is a string, so we interpret the command
2472                    ;; as a, well, command, and fork it off.
2473                    (let ((process-connection-type nil))
2474                      (gnus-set-process-query-on-exit-flag
2475                       (start-process
2476                        "article-x-face" nil shell-file-name
2477                        shell-command-switch gnus-article-x-face-command)
2478                       nil)
2479                      ;; Sending multiple EOFs to xv doesn't work,
2480                      ;; so we only do a single external face.
2481                      (with-temp-buffer
2482                        (insert (car x-faces))
2483                        (process-send-region "article-x-face"
2484                                             (point-min) (point-max)))
2485                      (process-send-eof "article-x-face")))
2486                   (t
2487                    (error "`%s' set to `%s' is not a function"
2488                           gnus-article-x-face-command
2489                           'gnus-article-x-face-command)))))))))
2490
2491 (defun article-decode-mime-words ()
2492   "Decode all MIME-encoded words in the article."
2493   (interactive)
2494   (gnus-with-article-buffer
2495     (let ((inhibit-point-motion-hooks t)
2496           (mail-parse-charset gnus-newsgroup-charset)
2497           (mail-parse-ignored-charsets
2498            (with-current-buffer gnus-summary-buffer
2499              gnus-newsgroup-ignored-charsets)))
2500       (mail-decode-encoded-word-region (point-min) (point-max)))))
2501
2502 (defun article-decode-charset (&optional prompt)
2503   "Decode charset-encoded text in the article.
2504 If PROMPT (the prefix), prompt for a coding system to use."
2505   (interactive "P")
2506   (let ((inhibit-point-motion-hooks t) (case-fold-search t)
2507         (inhibit-read-only t)
2508         (mail-parse-charset gnus-newsgroup-charset)
2509         (mail-parse-ignored-charsets
2510          (save-excursion (condition-case nil
2511                              (set-buffer gnus-summary-buffer)
2512                            (error))
2513                          gnus-newsgroup-ignored-charsets))
2514         ct cte ctl charset format)
2515     (save-excursion
2516       (save-restriction
2517         (article-narrow-to-head)
2518         (setq ct (message-fetch-field "Content-Type" t)
2519               cte (message-fetch-field "Content-Transfer-Encoding" t)
2520               ctl (and ct (mail-header-parse-content-type ct))
2521               charset (cond
2522                        (prompt
2523                         (mm-read-coding-system "Charset to decode: "))
2524                        (ctl
2525                         (mail-content-type-get ctl 'charset)))
2526               format (and ctl (mail-content-type-get ctl 'format)))
2527         (when cte
2528           (setq cte (mail-header-strip cte)))
2529         (if (and ctl (not (string-match "/" (car ctl))))
2530             (setq ctl nil))
2531         (goto-char (point-max)))
2532       (forward-line 1)
2533       (save-restriction
2534         (narrow-to-region (point) (point-max))
2535         (when (and (eq mail-parse-charset 'gnus-decoded)
2536                    (eq (mm-body-7-or-8) '8bit))
2537           ;; The text code could have been decoded.
2538           (setq charset mail-parse-charset))
2539         (when (and (or (not ctl)
2540                        (equal (car ctl) "text/plain"))
2541                    (not format)) ;; article with format will decode later.
2542           (mm-decode-body
2543            charset (and cte (intern (downcase
2544                                      (gnus-strip-whitespace cte))))
2545            (car ctl)))))))
2546
2547 (defun article-decode-encoded-words ()
2548   "Remove encoded-word encoding from headers."
2549   (let ((inhibit-point-motion-hooks t)
2550         (mail-parse-charset gnus-newsgroup-charset)
2551         (mail-parse-ignored-charsets
2552          (save-excursion (condition-case nil
2553                              (set-buffer gnus-summary-buffer)
2554                            (error))
2555                          gnus-newsgroup-ignored-charsets))
2556         (inhibit-read-only t)
2557         end start)
2558     (goto-char (point-min))
2559     (when (search-forward "\n\n" nil 'move)
2560       (forward-line -1))
2561     (setq end (point))
2562     (while (not (bobp))
2563       (while (progn
2564                (forward-line -1)
2565                (and (not (bobp))
2566                     (memq (char-after) '(?\t ? )))))
2567       (setq start (point))
2568       (if (looking-at "\
2569 \\(?:Resent-\\)?\\(?:From\\|Cc\\|To\\|Bcc\\|\\(?:In-\\)?Reply-To\\|Sender\
2570 \\|Mail-Followup-To\\|Mail-Copies-To\\|Approved\\):")
2571           (funcall gnus-decode-address-function start end)
2572         (funcall gnus-decode-header-function start end))
2573       (goto-char (setq end start)))))
2574
2575 (defun article-decode-group-name ()
2576   "Decode group names in Newsgroups, Followup-To and Xref headers."
2577   (let ((inhibit-point-motion-hooks t)
2578         (inhibit-read-only t)
2579         (method (gnus-find-method-for-group gnus-newsgroup-name))
2580         regexp)
2581     (when (and (or gnus-group-name-charset-method-alist
2582                    gnus-group-name-charset-group-alist)
2583                (gnus-buffer-live-p gnus-original-article-buffer))
2584       (save-restriction
2585         (article-narrow-to-head)
2586         (dolist (header '("Newsgroups" "Followup-To" "Xref"))
2587           (with-current-buffer gnus-original-article-buffer
2588             (goto-char (point-min)))
2589           (setq regexp (concat "^" header
2590                                ":\\([^\n]*\\(?:\n[\t ]+[^\n]+\\)*\\)\n"))
2591           (while (re-search-forward regexp nil t)
2592             (replace-match (save-match-data
2593                              (gnus-decode-newsgroups
2594                               ;; XXX how to use data in article buffer?
2595                               (with-current-buffer gnus-original-article-buffer
2596                                 (re-search-forward regexp nil t)
2597                                 (match-string 1))
2598                               gnus-newsgroup-name method))
2599                            t t nil 1))
2600           (goto-char (point-min)))))))
2601
2602 (autoload 'idna-to-unicode "idna")
2603
2604 (defun article-decode-idna-rhs ()
2605   "Decode IDNA strings in RHS in various headers in current buffer.
2606 The following headers are decoded: From:, To:, Cc:, Reply-To:,
2607 Mail-Reply-To: and Mail-Followup-To:."
2608   (when gnus-use-idna
2609     (save-restriction
2610       (let ((inhibit-point-motion-hooks t)
2611             (inhibit-read-only t))
2612         (article-narrow-to-head)
2613         (goto-char (point-min))
2614         (while (re-search-forward "@[^ \t\n\r,>]*\\(xn--[-A-Za-z0-9.]*\\)[ \t\n\r,>]" nil t)
2615           (let (ace unicode)
2616             (when (save-match-data
2617                     (and (setq ace (match-string 1))
2618                          (save-excursion
2619                            (and (re-search-backward "^[^ \t]" nil t)
2620                                 (looking-at "From\\|To\\|Cc\\|Reply-To\\|Mail-Reply-To\\|Mail-Followup-To")))
2621                          (setq unicode (idna-to-unicode ace))))
2622               (unless (string= ace unicode)
2623                 (replace-match unicode nil nil nil 1)))))))))
2624
2625 (defun article-de-quoted-unreadable (&optional force read-charset)
2626   "Translate a quoted-printable-encoded article.
2627 If FORCE, decode the article whether it is marked as quoted-printable
2628 or not.
2629 If READ-CHARSET, ask for a coding system."
2630   (interactive (list 'force current-prefix-arg))
2631   (save-excursion
2632     (let ((inhibit-read-only t) type charset)
2633       (if (gnus-buffer-live-p gnus-original-article-buffer)
2634           (with-current-buffer gnus-original-article-buffer
2635             (setq type
2636                   (gnus-fetch-field "content-transfer-encoding"))
2637             (let* ((ct (gnus-fetch-field "content-type"))
2638                    (ctl (and ct (mail-header-parse-content-type ct))))
2639               (setq charset (and ctl
2640                                  (mail-content-type-get ctl 'charset)))
2641               (if (stringp charset)
2642                   (setq charset (intern (downcase charset)))))))
2643       (if read-charset
2644           (setq charset (mm-read-coding-system "Charset: " charset)))
2645       (unless charset
2646         (setq charset gnus-newsgroup-charset))
2647       (when (or force
2648                 (and type (let ((case-fold-search t))
2649                             (string-match "quoted-printable" type))))
2650         (article-goto-body)
2651         (quoted-printable-decode-region
2652          (point) (point-max) (mm-charset-to-coding-system charset))))))
2653
2654 (defun article-de-base64-unreadable (&optional force read-charset)
2655   "Translate a base64 article.
2656 If FORCE, decode the article whether it is marked as base64 not.
2657 If READ-CHARSET, ask for a coding system."
2658   (interactive (list 'force current-prefix-arg))
2659   (save-excursion
2660     (let ((inhibit-read-only t) type charset)
2661       (if (gnus-buffer-live-p gnus-original-article-buffer)
2662           (with-current-buffer gnus-original-article-buffer
2663             (setq type
2664                   (gnus-fetch-field "content-transfer-encoding"))
2665             (let* ((ct (gnus-fetch-field "content-type"))
2666                    (ctl (and ct (mail-header-parse-content-type ct))))
2667               (setq charset (and ctl
2668                                  (mail-content-type-get ctl 'charset)))
2669               (if (stringp charset)
2670                   (setq charset (intern (downcase charset)))))))
2671       (if read-charset
2672           (setq charset (mm-read-coding-system "Charset: " charset)))
2673       (unless charset
2674         (setq charset gnus-newsgroup-charset))
2675       (when (or force
2676                 (and type (let ((case-fold-search t))
2677                             (string-match "base64" type))))
2678         (article-goto-body)
2679         (save-restriction
2680           (narrow-to-region (point) (point-max))
2681           (base64-decode-region (point-min) (point-max))
2682           (mm-decode-coding-region
2683            (point-min) (point-max) (mm-charset-to-coding-system charset)))))))
2684
2685 (eval-when-compile
2686   (require 'rfc1843))
2687
2688 (defun article-decode-HZ ()
2689   "Translate a HZ-encoded article."
2690   (interactive)
2691   (require 'rfc1843)
2692   (save-excursion
2693     (let ((inhibit-read-only t))
2694       (rfc1843-decode-region (point-min) (point-max)))))
2695
2696 (defun article-unsplit-urls ()
2697   "Remove the newlines that some other mailers insert into URLs."
2698   (interactive)
2699   (save-excursion
2700     (let ((inhibit-read-only t))
2701       (goto-char (point-min))
2702       (while (re-search-forward
2703               "\\(\\(https?\\|ftp\\)://\\S-+\\) *\n\\(\\S-+\\)" nil t)
2704         (replace-match "\\1\\3" t)))
2705     (when (interactive-p)
2706       (gnus-treat-article nil))))
2707
2708
2709 (defun article-wash-html (&optional read-charset)
2710   "Format an HTML article.
2711 If READ-CHARSET, ask for a coding system.  If it is a number, the
2712 charset defined in `gnus-summary-show-article-charset-alist' is used."
2713   (interactive "P")
2714   (save-excursion
2715     (let ((inhibit-read-only t)
2716           charset)
2717       (if read-charset
2718           (if (or (and (numberp read-charset)
2719                        (setq charset
2720                              (cdr
2721                               (assq read-charset
2722                                     gnus-summary-show-article-charset-alist))))
2723                   (setq charset (mm-read-coding-system "Charset: ")))
2724               (let ((gnus-summary-show-article-charset-alist
2725                      (list (cons 1 charset))))
2726                 (with-current-buffer gnus-summary-buffer
2727                   (gnus-summary-show-article 1)))
2728             (error "No charset is given"))
2729         (when (gnus-buffer-live-p gnus-original-article-buffer)
2730           (with-current-buffer gnus-original-article-buffer
2731             (let* ((ct (gnus-fetch-field "content-type"))
2732                    (ctl (and ct (mail-header-parse-content-type ct))))
2733               (setq charset (and ctl
2734                                  (mail-content-type-get ctl 'charset)))
2735               (when (stringp charset)
2736                 (setq charset (intern (downcase charset)))))))
2737         (unless charset
2738           (setq charset gnus-newsgroup-charset)))
2739       (article-goto-body)
2740       (save-window-excursion
2741         (save-restriction
2742           (narrow-to-region (point) (point-max))
2743           (let* ((func (or gnus-article-wash-function mm-text-html-renderer))
2744                  (entry (assq func mm-text-html-washer-alist)))
2745             (when entry
2746               (setq func (cdr entry)))
2747             (cond
2748              ((functionp func)
2749               (funcall func))
2750              (t
2751               (apply (car func) (cdr func))))))))))
2752
2753 ;; External.
2754 (declare-function w3-region "ext:w3-display" (st nd))
2755
2756 (defun gnus-article-wash-html-with-w3 ()
2757   "Wash the current buffer with w3."
2758   (mm-setup-w3)
2759   (let ((w3-strict-width (window-width))
2760         (url-standalone-mode t)
2761         (url-gateway-unplugged t)
2762         (w3-honor-stylesheets nil))
2763     (condition-case ()
2764         (w3-region (point-min) (point-max))
2765       (error))))
2766
2767 ;; External.
2768 (declare-function w3m-region "ext:w3m" (start end &optional url charset))
2769
2770 (defun gnus-article-wash-html-with-w3m ()
2771   "Wash the current buffer with emacs-w3m."
2772   (mm-setup-w3m)
2773   (let ((w3m-safe-url-regexp mm-w3m-safe-url-regexp)
2774         w3m-force-redisplay)
2775     (w3m-region (point-min) (point-max)))
2776   ;; Put the mark meaning this part was rendered by emacs-w3m.
2777   (put-text-property (point-min) (point-max) 'mm-inline-text-html-with-w3m t)
2778   (when (and mm-inline-text-html-with-w3m-keymap
2779              (boundp 'w3m-minor-mode-map)
2780              w3m-minor-mode-map)
2781     (if (and (boundp 'w3m-link-map)
2782              w3m-link-map)
2783         (let* ((start (point-min))
2784                (end (point-max))
2785                (on (get-text-property start 'w3m-href-anchor))
2786                (map (copy-keymap w3m-link-map))
2787                next)
2788           (set-keymap-parent map w3m-minor-mode-map)
2789           (while (< start end)
2790             (if on
2791                 (progn
2792                   (setq next (or (text-property-any start end
2793                                                     'w3m-href-anchor nil)
2794                                  end))
2795                   (put-text-property start next 'keymap map))
2796               (setq next (or (text-property-not-all start end
2797                                                     'w3m-href-anchor nil)
2798                              end))
2799               (put-text-property start next 'keymap w3m-minor-mode-map))
2800             (setq start next
2801                   on (not on))))
2802       (put-text-property (point-min) (point-max) 'keymap w3m-minor-mode-map))))
2803
2804 (defvar charset) ;; Bound by `article-wash-html'.
2805
2806 (defun gnus-article-wash-html-with-w3m-standalone ()
2807   "Wash the current buffer with w3m."
2808   (if (mm-w3m-standalone-supports-m17n-p)
2809       (progn
2810         (unless (mm-coding-system-p charset) ;; Bound by `article-wash-html'.
2811           ;; The default.
2812           (setq charset 'iso-8859-1))
2813         (let ((coding-system-for-write charset)
2814               (coding-system-for-read charset))
2815           (call-process-region
2816            (point-min) (point-max)
2817            "w3m" t t nil "-dump" "-T" "text/html"
2818            "-I" (symbol-name charset) "-O" (symbol-name charset))))
2819     (mm-inline-wash-with-stdin nil "w3m" "-dump" "-T" "text/html")))
2820
2821 (defvar gnus-article-browse-html-temp-list nil
2822   "List of temporary files created by `gnus-article-browse-html-parts'.
2823 Internal variable.")
2824
2825 (defcustom gnus-article-browse-delete-temp 'ask
2826   "What to do with temporary files from `gnus-article-browse-html-parts'.
2827 If nil, don't delete temporary files.  If it is t, delete them on
2828 exit from the summary buffer.  If it is the symbol `file', query
2829 on each file, if it is `ask' ask once when exiting from the
2830 summary buffer."
2831   :group 'gnus-article
2832   :version "23.1" ;; No Gnus
2833   :type '(choice (const :tag "Don't delete" nil)
2834                  (const :tag "Don't ask" t)
2835                  (const :tag "Ask" ask)
2836                  (const :tag "Ask for each file" file)))
2837
2838 ;; Cf. mm-postponed-undisplay-list / mm-destroy-postponed-undisplay-list.
2839
2840 (defun gnus-article-browse-delete-temp-files (&optional how)
2841   "Delete temp-files created by `gnus-article-browse-html-parts'."
2842   (when (and gnus-article-browse-html-temp-list
2843              (progn
2844                (or how (setq how gnus-article-browse-delete-temp))
2845                (if (eq how 'ask)
2846                    (let ((files (length gnus-article-browse-html-temp-list)))
2847                      (gnus-y-or-n-p (format
2848                                      "Delete all %s temporary HTML file%s? "
2849                                      files
2850                                      (if (> files 1) "s" ""))))
2851                  how)))
2852     (dolist (file gnus-article-browse-html-temp-list)
2853       (cond ((file-directory-p file)
2854              (when (or (not (eq how 'file))
2855                        (gnus-y-or-n-p
2856                         (format
2857                          "Delete temporary HTML file(s) in directory `%s'? "
2858                          (file-name-as-directory file))))
2859                (gnus-delete-directory file)))
2860             ((file-exists-p file)
2861              (when (or (not (eq how 'file))
2862                        (gnus-y-or-n-p
2863                         (format "Delete temporary HTML file `%s'? " file)))
2864                (delete-file file)))))
2865     ;; Also remove file from the list when not deleted or if file doesn't
2866     ;; exist anymore.
2867     (setq gnus-article-browse-html-temp-list nil))
2868   gnus-article-browse-html-temp-list)
2869
2870 (defun gnus-article-browse-html-save-cid-content (cid handles directory)
2871   "Find CID content in HANDLES and save it in a file in DIRECTORY.
2872 Return file name."
2873   (save-match-data
2874     (let (file type)
2875       (catch 'found
2876         (dolist (handle handles)
2877           (cond
2878            ((not (listp handle)))
2879            ((equal (mm-handle-media-supertype handle) "multipart")
2880             (when (setq file (gnus-article-browse-html-save-cid-content
2881                               cid handle directory))
2882               (throw 'found file)))
2883            ((equal (concat "<" cid ">") (mm-handle-id handle))
2884             (setq file
2885                   (expand-file-name
2886                    (or (mail-content-type-get
2887                         (mm-handle-disposition handle) 'filename)
2888                        (mail-content-type-get
2889                         (setq type (mm-handle-type handle)) 'name)
2890                        (concat
2891                         (make-temp-name "cid")
2892                         (car (rassoc (car type) mailcap-mime-extensions))))
2893                    directory))
2894             (mm-save-part-to-file handle file)
2895             (throw 'found file))))))))
2896
2897 (defun gnus-article-browse-html-parts (list &optional header)
2898   "View all \"text/html\" parts from LIST.
2899 Recurse into multiparts.  The optional HEADER that should be a decoded
2900 message header will be added to the bodies of the \"text/html\" parts."
2901   ;; Internal function used by `gnus-article-browse-html-article'.
2902   (let (type file charset content cid-dir tmp-file showed)
2903     ;; Find and show the html-parts.
2904     (dolist (handle list)
2905       ;; If HTML, show it:
2906       (cond ((not (listp handle)))
2907             ((or (equal (car (setq type (mm-handle-type handle))) "text/html")
2908                  (and (equal (car type) "message/external-body")
2909                       (or header
2910                           (setq file (or (mail-content-type-get type 'name)
2911                                          (mail-content-type-get
2912                                           (mm-handle-disposition handle)
2913                                           'filename))))
2914                       (or (mm-handle-cache handle)
2915                           (condition-case code
2916                               (progn (mm-extern-cache-contents handle) t)
2917                             (error
2918                              (gnus-message 3 "%s" (error-message-string code))
2919                              (when (>= gnus-verbose 3) (sit-for 2))
2920                              nil)))
2921                       (progn
2922                         (setq handle (mm-handle-cache handle)
2923                               type (mm-handle-type handle))
2924                         (equal (car type) "text/html"))))
2925              (setq charset (mail-content-type-get type 'charset)
2926                    content (mm-get-part handle))
2927              (with-temp-buffer
2928                (if (eq charset 'gnus-decoded)
2929                    (mm-enable-multibyte)
2930                  (mm-disable-multibyte))
2931                (insert content)
2932                ;; resolve cid contents
2933                (let ((case-fold-search t)
2934                      cid-file)
2935                  (goto-char (point-min))
2936                  (while (re-search-forward "\
2937 <img[\t\n ]+\\(?:[^\t\n >]+[\t\n ]+\\)*src=\"\\(cid:\\([^\"]+\\)\\)\""
2938                                            nil t)
2939                    (unless cid-dir
2940                      (setq cid-dir (mm-make-temp-file "cid" t))
2941                      (add-to-list 'gnus-article-browse-html-temp-list cid-dir))
2942                    (setq file nil
2943                          content nil)
2944                    (when (setq cid-file
2945                                (gnus-article-browse-html-save-cid-content
2946                                 (match-string 2)
2947                                 (with-current-buffer gnus-article-buffer
2948                                   gnus-article-mime-handles)
2949                                 cid-dir))
2950                      (replace-match (concat "file://" cid-file)
2951                                     nil nil nil 1))))
2952                (unless content (setq content (buffer-string))))
2953              (when (or charset header (not file))
2954                (setq tmp-file (mm-make-temp-file
2955                                ;; Do we need to care for 8.3 filenames?
2956                                "mm-" nil ".html")))
2957              ;; Add a meta html tag to specify charset and a header.
2958              (cond
2959               (header
2960                (let (title eheader body hcharset coding force-charset)
2961                  (with-temp-buffer
2962                    (mm-enable-multibyte)
2963                    (setq case-fold-search t)
2964                    (insert header "\n")
2965                    (setq title (message-fetch-field "subject"))
2966                    (goto-char (point-min))
2967                    (while (re-search-forward "\\(<\\)\\|\\(>\\)\\|&" nil t)
2968                      (replace-match (cond ((match-beginning 1) "&lt;")
2969                                           ((match-beginning 2) "&gt;")
2970                                           (t "&amp;"))))
2971                    (goto-char (point-min))
2972                    (insert "<pre>\n")
2973                    (goto-char (point-max))
2974                    (insert "</pre>\n<hr>\n")
2975                    ;; We have to examine charset one by one since
2976                    ;; charset specified in parts might be different.
2977                    (if (eq charset 'gnus-decoded)
2978                        (setq charset 'utf-8
2979                              eheader (mm-encode-coding-string (buffer-string)
2980                                                               charset)
2981                              title (when title
2982                                      (mm-encode-coding-string title charset))
2983                              body (mm-encode-coding-string content charset)
2984                              force-charset t)
2985                      (setq hcharset (mm-find-mime-charset-region (point-min)
2986                                                                  (point-max)))
2987                      (cond ((= (length hcharset) 1)
2988                             (setq hcharset (car hcharset)
2989                                   coding (mm-charset-to-coding-system
2990                                           hcharset)))
2991                            ((> (length hcharset) 1)
2992                             (setq hcharset 'utf-8
2993                                   coding hcharset)))
2994                      (if coding
2995                          (if charset
2996                              (progn
2997                                (setq body
2998                                      (mm-charset-to-coding-system charset))
2999                                (if (eq coding body)
3000                                    (setq eheader (mm-encode-coding-string
3001                                                   (buffer-string) coding)
3002                                          title (when title
3003                                                  (mm-encode-coding-string
3004                                                   title coding))
3005                                          body content)
3006                                  (setq charset 'utf-8
3007                                        eheader (mm-encode-coding-string
3008                                                 (buffer-string) charset)
3009                                        title (when title
3010                                                (mm-encode-coding-string
3011                                                 title charset))
3012                                        body (mm-encode-coding-string
3013                                              (mm-decode-coding-string
3014                                               content body)
3015                                              charset)
3016                                        force-charset t)))
3017                            (setq charset hcharset
3018                                  eheader (mm-encode-coding-string
3019                                           (buffer-string) coding)
3020                                  title (when title
3021                                          (mm-encode-coding-string
3022                                           title coding))
3023                                  body content))
3024                        (setq eheader (mm-string-as-unibyte (buffer-string))
3025                              body content)))
3026                    (erase-buffer)
3027                    (mm-disable-multibyte)
3028                    (insert body)
3029                    (when charset
3030                      (mm-add-meta-html-tag handle charset force-charset))
3031                    (when title
3032                      (goto-char (point-min))
3033                      (unless (search-forward "<title>" nil t)
3034                        (re-search-forward "<head>\\s-*" nil t)
3035                        (insert "<title>" title "</title>\n")))
3036                    (goto-char (point-min))
3037                    (or (re-search-forward
3038                         "<body\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t)
3039                        (re-search-forward
3040                         "</head\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t))
3041                    (insert eheader)
3042                    (mm-write-region (point-min) (point-max)
3043                                     tmp-file nil nil nil 'binary t))))
3044               (charset
3045                (mm-with-unibyte-buffer
3046                  (insert (if (eq charset 'gnus-decoded)
3047                              (mm-encode-coding-string content
3048                                                       (setq charset 'utf-8))
3049                            content))
3050                  (if (or (mm-add-meta-html-tag handle charset)
3051                          (not file))
3052                      (mm-write-region (point-min) (point-max)
3053                                       tmp-file nil nil nil 'binary t)
3054                    (setq tmp-file nil))))
3055               (tmp-file
3056                (mm-save-part-to-file handle tmp-file)))
3057              (when tmp-file
3058                (add-to-list 'gnus-article-browse-html-temp-list tmp-file))
3059              (add-hook 'gnus-summary-prepare-exit-hook
3060                        'gnus-article-browse-delete-temp-files)
3061              (add-hook 'gnus-exit-gnus-hook
3062                        (lambda  ()
3063                          (gnus-article-browse-delete-temp-files t)))
3064              ;; FIXME: Warn if there's an <img> tag?
3065              (browse-url-of-file (or tmp-file (expand-file-name file)))
3066              (setq showed t))
3067             ;; If multipart, recurse
3068             ((equal (mm-handle-media-supertype handle) "multipart")
3069              (when (gnus-article-browse-html-parts handle header)
3070                (setq showed t)))
3071             ((equal (mm-handle-media-type handle) "message/rfc822")
3072              (mm-with-multibyte-buffer
3073                (mm-insert-part handle)
3074                (setq handle (mm-dissect-buffer t t))
3075                (when (and (bufferp (car handle))
3076                           (stringp (car (mm-handle-type handle))))
3077                  (setq handle (list handle)))
3078                (when header
3079                  (article-decode-encoded-words)
3080                  (let ((gnus-visible-headers
3081                         (or (get 'gnus-visible-headers 'standard-value)
3082                             gnus-visible-headers)))
3083                    (article-hide-headers))
3084                  (goto-char (point-min))
3085                  (search-forward "\n\n" nil 'move)
3086                  (skip-chars-backward "\t\n ")
3087                  (setq header (buffer-substring (point-min) (point)))))
3088              (when (prog1
3089                        (gnus-article-browse-html-parts handle header)
3090                      (mm-destroy-parts handle))
3091                (setq showed t)))))
3092     showed))
3093
3094 (defun gnus-article-browse-html-article (&optional arg)
3095   "View \"text/html\" parts of the current article with a WWW browser.
3096 Inline images embedded in a message using the cid scheme, as they are
3097 generally considered to be safe, will be processed properly.
3098 The message header is added to the beginning of every html part unless
3099 the prefix argument ARG is given.
3100
3101 Warning: Spammers use links to images (using the http scheme) in HTML
3102 articles to verify whether you have read the message.  As
3103 `gnus-article-browse-html-article' passes the HTML content to the
3104 browser without eliminating these \"web bugs\" you should only
3105 use it for mails from trusted senders.
3106
3107 If you always want to display HTML parts in the browser, set
3108 `mm-text-html-renderer' to nil.
3109
3110 This command creates temporary files to pass HTML contents including
3111 images if any to the browser, and deletes them when exiting the group
3112 \(if you want)."
3113   ;; Cf. `mm-w3m-safe-url-regexp'
3114   (interactive "P")
3115   (if arg
3116       (gnus-summary-show-article)
3117     (let ((gnus-visible-headers (or (get 'gnus-visible-headers 'standard-value)
3118                                     gnus-visible-headers))
3119           ;; As we insert a <hr>, there's no need for the body boundary.
3120           (gnus-treat-body-boundary nil))
3121       (gnus-summary-show-article)))
3122   (with-current-buffer gnus-article-buffer
3123     (let ((header (unless arg
3124                     (save-restriction
3125                       (widen)
3126                       (buffer-substring-no-properties
3127                        (goto-char (point-min))
3128                        (if (search-forward "\n\n" nil t)
3129                            (match-beginning 0)
3130                          (goto-char (point-max))
3131                          (skip-chars-backward "\t\n ")
3132                          (point))))))
3133           parts)
3134       (set-buffer gnus-original-article-buffer)
3135       (setq parts (mm-dissect-buffer t t))
3136       ;; If singlepart, enforce a list.
3137       (when (and (bufferp (car parts))
3138                  (stringp (car (mm-handle-type parts))))
3139         (setq parts (list parts)))
3140       ;; Process the list
3141       (unless (gnus-article-browse-html-parts parts header)
3142         (gnus-error 3 "Mail doesn't contain a \"text/html\" part!"))
3143       (mm-destroy-parts parts)
3144       (unless arg
3145         (gnus-summary-show-article)))))
3146
3147 (defun article-hide-list-identifiers ()
3148   "Remove list identifies from the Subject header.
3149 The `gnus-list-identifiers' variable specifies what to do."
3150   (interactive)
3151   (let ((inhibit-point-motion-hooks t)
3152         (regexp (if (consp gnus-list-identifiers)
3153                     (mapconcat 'identity gnus-list-identifiers " *\\|")
3154                   gnus-list-identifiers))
3155         (inhibit-read-only t))
3156     (when regexp
3157       (save-excursion
3158         (save-restriction
3159           (article-narrow-to-head)
3160           (goto-char (point-min))
3161           (while (re-search-forward
3162                   (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
3163                   nil t)
3164             (delete-region (match-beginning 2) (match-end 0))
3165             (beginning-of-line))
3166           (when (re-search-forward
3167                  "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" nil t)
3168             (delete-region (match-beginning 1) (match-end 1))))))))
3169
3170 (defun article-hide-pem (&optional arg)
3171   "Toggle hiding of any PEM headers and signatures in the current article.
3172 If given a negative prefix, always show; if given a positive prefix,
3173 always hide."
3174   (interactive (gnus-article-hidden-arg))
3175   (unless (gnus-article-check-hidden-text 'pem arg)
3176     (save-excursion
3177       (let ((inhibit-read-only t) end)
3178         (goto-char (point-min))
3179         ;; Hide the horrendously ugly "header".
3180         (when (and (search-forward
3181                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
3182                     nil t)
3183                    (setq end (1+ (match-beginning 0))))
3184           (gnus-add-wash-type 'pem)
3185           (gnus-article-hide-text-type
3186            end
3187            (if (search-forward "\n\n" nil t)
3188                (match-end 0)
3189              (point-max))
3190            'pem)
3191           ;; Hide the trailer as well
3192           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
3193                                 nil t)
3194             (gnus-article-hide-text-type
3195              (match-beginning 0) (match-end 0) 'pem)))))))
3196
3197 (defun article-strip-banner ()
3198   "Strip the banners specified by the `banner' group parameter and by
3199 `gnus-article-address-banner-alist'."
3200   (interactive)
3201   (save-excursion
3202     (save-restriction
3203       (let ((inhibit-point-motion-hooks t))
3204         (when (gnus-parameter-banner gnus-newsgroup-name)
3205           (article-really-strip-banner
3206            (gnus-parameter-banner gnus-newsgroup-name)))
3207         (when gnus-article-address-banner-alist
3208           ;; Note that the From header is decoded here, so it is
3209           ;; required that the *-extract-address-components function
3210           ;; supports non-ASCII text.
3211           (let ((from (save-restriction
3212                         (widen)
3213                         (article-narrow-to-head)
3214                         (mail-fetch-field "from"))))
3215             (when (and from
3216                        (setq from
3217                              (cadr (funcall gnus-extract-address-components
3218                                             from))))
3219               (catch 'found
3220                 (dolist (pair gnus-article-address-banner-alist)
3221                   (when (string-match (car pair) from)
3222                     (throw 'found
3223                            (article-really-strip-banner (cdr pair)))))))))))))
3224
3225 (defun article-really-strip-banner (banner)
3226   "Strip the banner specified by the argument."
3227   (save-excursion
3228     (save-restriction
3229       (let ((inhibit-point-motion-hooks t)
3230             (gnus-signature-limit nil)
3231             (inhibit-read-only t))
3232         (article-goto-body)
3233         (cond
3234          ((eq banner 'signature)
3235           (when (gnus-article-narrow-to-signature)
3236             (widen)
3237             (forward-line -1)
3238             (delete-region (point) (point-max))))
3239          ((symbolp banner)
3240           (if (setq banner (cdr (assq banner gnus-article-banner-alist)))
3241               (while (re-search-forward banner nil t)
3242                 (delete-region (match-beginning 0) (match-end 0)))))
3243          ((stringp banner)
3244           (while (re-search-forward banner nil t)
3245             (delete-region (match-beginning 0) (match-end 0)))))))))
3246
3247 (defun article-babel ()
3248   "Translate article using an online translation service."
3249   (interactive)
3250   (require 'babel)
3251   (gnus-with-article-buffer
3252     (when (article-goto-body)
3253       (let* ((start (point))
3254              (end (point-max))
3255              (orig (buffer-substring start end))
3256              (trans (babel-as-string orig)))
3257         (save-restriction
3258           (narrow-to-region start end)
3259           (delete-region start end)
3260           (insert trans))))))
3261
3262 (defun article-hide-signature (&optional arg)
3263   "Hide the signature in the current article.
3264 If given a negative prefix, always show; if given a positive prefix,
3265 always hide."
3266   (interactive (gnus-article-hidden-arg))
3267   (unless (gnus-article-check-hidden-text 'signature arg)
3268     (save-excursion
3269       (save-restriction
3270         (let ((inhibit-read-only t))
3271           (when (gnus-article-narrow-to-signature)
3272             (gnus-article-hide-text-type
3273              (point-min) (point-max) 'signature))))))
3274   (gnus-set-mode-line 'article))
3275
3276 (defun article-strip-headers-in-body ()
3277   "Strip offensive headers from bodies."
3278   (interactive)
3279   (save-excursion
3280     (article-goto-body)
3281     (let ((case-fold-search t))
3282       (when (looking-at "x-no-archive:")
3283         (gnus-delete-line)))))
3284
3285 (defun article-strip-leading-blank-lines ()
3286   "Remove all blank lines from the beginning of the article."
3287   (interactive)
3288   (save-excursion
3289     (let ((inhibit-point-motion-hooks t)
3290           (inhibit-read-only t))
3291       (when (article-goto-body)
3292         (while (and (not (eobp))
3293                     (looking-at "[ \t]*$"))
3294           (gnus-delete-line))))))
3295
3296 (defun article-narrow-to-head ()
3297   "Narrow the buffer to the head of the message.
3298 Point is left at the beginning of the narrowed-to region."
3299   (narrow-to-region
3300    (goto-char (point-min))
3301    (if (search-forward "\n\n" nil 1)
3302        (1- (point))
3303      (point-max)))
3304   (goto-char (point-min)))
3305
3306 (defun article-goto-body ()
3307   "Place point at the start of the body."
3308   (goto-char (point-min))
3309   (cond
3310    ;; This variable is only bound when dealing with separate
3311    ;; MIME body parts.
3312    (article-goto-body-goes-to-point-min-p
3313     t)
3314    ((search-forward "\n\n" nil t)
3315     t)
3316    (t
3317     (goto-char (point-max))
3318     nil)))
3319
3320 (defun article-strip-multiple-blank-lines ()
3321   "Replace consecutive blank lines with one empty line."
3322   (interactive)
3323   (save-excursion
3324     (let ((inhibit-point-motion-hooks t)
3325           (inhibit-read-only t))
3326       ;; First make all blank lines empty.
3327       (article-goto-body)
3328       (while (re-search-forward "^[ \t]+$" nil t)
3329         (unless (gnus-annotation-in-region-p
3330                  (match-beginning 0) (match-end 0))
3331           (replace-match "" nil t)))
3332       ;; Then replace multiple empty lines with a single empty line.
3333       (article-goto-body)
3334       (while (re-search-forward "\n\n\\(\n+\\)" nil t)
3335         (unless (gnus-annotation-in-region-p
3336                  (match-beginning 0) (match-end 0))
3337           (delete-region (match-beginning 1) (match-end 1)))))))
3338
3339 (defun article-strip-leading-space ()
3340   "Remove all white space from the beginning of the lines in the article."
3341   (interactive)
3342   (save-excursion
3343     (let ((inhibit-point-motion-hooks t)
3344           (inhibit-read-only t))
3345       (article-goto-body)
3346       (while (re-search-forward "^[ \t]+" nil t)
3347         (replace-match "" t t)))))
3348
3349 (defun article-strip-trailing-space ()
3350   "Remove all white space from the end of the lines in the article."
3351   (interactive)
3352   (save-excursion
3353     (let ((inhibit-point-motion-hooks t)
3354           (inhibit-read-only t))
3355       (article-goto-body)
3356       (while (re-search-forward "[ \t]+$" nil t)
3357         (replace-match "" t t)))))
3358
3359 (defun article-strip-blank-lines ()
3360   "Strip leading, trailing and multiple blank lines."
3361   (interactive)
3362   (article-strip-leading-blank-lines)
3363   (article-remove-trailing-blank-lines)
3364   (article-strip-multiple-blank-lines))
3365
3366 (defun article-strip-all-blank-lines ()
3367   "Strip all blank lines."
3368   (interactive)
3369   (save-excursion
3370     (let ((inhibit-point-motion-hooks t)
3371           (inhibit-read-only t))
3372       (article-goto-body)
3373       (while (re-search-forward "^[ \t]*\n" nil t)
3374         (replace-match "" t t)))))
3375
3376 (defun gnus-article-narrow-to-signature ()
3377   "Narrow to the signature; return t if a signature is found, else nil."
3378   (let ((inhibit-point-motion-hooks t))
3379     (when (gnus-article-search-signature)
3380       (forward-line 1)
3381       ;; Check whether we have some limits to what we consider
3382       ;; to be a signature.
3383       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
3384                       (list gnus-signature-limit)))
3385             limit limited)
3386         (while (setq limit (pop limits))
3387           (if (or (and (integerp limit)
3388                        (< (- (point-max) (point)) limit))
3389                   (and (floatp limit)
3390                        (< (count-lines (point) (point-max)) limit))
3391                   (and (functionp limit)
3392                        (funcall limit))
3393                   (and (stringp limit)
3394                        (not (re-search-forward limit nil t))))
3395               ()                        ; This limit did not succeed.
3396             (setq limited t
3397                   limits nil)))
3398         (unless limited
3399           (narrow-to-region (point) (point-max))
3400           t)))))
3401
3402 (defun gnus-article-search-signature ()
3403   "Search the current buffer for the signature separator.
3404 Put point at the beginning of the signature separator."
3405   (let ((cur (point)))
3406     (goto-char (point-max))
3407     (if (if (stringp gnus-signature-separator)
3408             (re-search-backward gnus-signature-separator nil t)
3409           (let ((seps gnus-signature-separator))
3410             (while (and seps
3411                         (not (re-search-backward (car seps) nil t)))
3412               (pop seps))
3413             seps))
3414         t
3415       (goto-char cur)
3416       nil)))
3417
3418 (defun gnus-article-hidden-arg ()
3419   "Return the current prefix arg as a number, or 0 if no prefix."
3420   (list (if current-prefix-arg
3421             (prefix-numeric-value current-prefix-arg)
3422           0)))
3423
3424 (defun gnus-article-check-hidden-text (type arg)
3425   "Return nil if hiding is necessary.
3426 Arg can be nil or a number.  nil and positive means hide, negative
3427 means show, 0 means toggle."
3428   (save-excursion
3429     (save-restriction
3430       (let ((hide (gnus-article-hidden-text-p type)))
3431         (cond
3432          ((or (null arg)
3433               (> arg 0))
3434           nil)
3435          ((< arg 0)
3436           (gnus-article-show-hidden-text type)
3437           t)
3438          (t
3439           (if (eq hide 'hidden)
3440               (progn
3441                 (gnus-article-show-hidden-text type)
3442                 t)
3443             nil)))))))
3444
3445 (defun gnus-article-hidden-text-p (type)
3446   "Say whether the current buffer contains hidden text of type TYPE."
3447   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
3448     (while (and pos
3449                 (not (get-text-property pos 'invisible))
3450                 (not (get-text-property pos 'dummy-invisible)))
3451       (setq pos
3452             (text-property-any (1+ pos) (point-max) 'article-type type)))
3453     (if pos
3454         'hidden
3455       nil)))
3456
3457 (defun gnus-article-show-hidden-text (type &optional dummy)
3458   "Show all hidden text of type TYPE.
3459 Originally it is hide instead of DUMMY."
3460   (let ((inhibit-read-only t)
3461         (inhibit-point-motion-hooks t))
3462     (gnus-remove-text-properties-when
3463      'article-type type
3464      (point-min) (point-max)
3465      (cons 'article-type (cons type
3466                                gnus-hidden-properties)))
3467     (gnus-delete-wash-type type)))
3468
3469 (defconst article-time-units
3470   `((year . ,(* 365.25 24 60 60))
3471     (week . ,(* 7 24 60 60))
3472     (day . ,(* 24 60 60))
3473     (hour . ,(* 60 60))
3474     (minute . 60)
3475     (second . 1))
3476   "Mapping from time units to seconds.")
3477
3478 (defun gnus-article-forward-header ()
3479   "Move point to the start of the next header.
3480 If the current header is a continuation header, this can be several
3481 lines forward."
3482   (let ((ended nil))
3483     (while (not ended)
3484       (forward-line 1)
3485       (if (looking-at "[ \t]+[^ \t]")
3486           (forward-line 1)
3487         (setq ended t)))))
3488
3489 (defun article-date-ut (&optional type highlight)
3490   "Convert DATE date to universal time in the current article.
3491 If TYPE is `local', convert to local time; if it is `lapsed', output
3492 how much time has lapsed since DATE.  For `lapsed', the value of
3493 `gnus-article-date-lapsed-new-header' says whether the \"X-Sent:\" header
3494 should replace the \"Date:\" one, or should be added below it."
3495   (interactive (list 'ut t))
3496   (let* ((tdate-regexp "^Date:[ \t]\\|^X-Sent:[ \t]")
3497          (date-regexp (cond ((not gnus-article-date-lapsed-new-header)
3498                              tdate-regexp)
3499                             ((eq type 'lapsed)
3500                              "^X-Sent:[ \t]")
3501                             (article-lapsed-timer
3502                              "^Date:[ \t]")
3503                             (t
3504                              tdate-regexp)))
3505          (case-fold-search t)
3506          (inhibit-read-only t)
3507          (inhibit-point-motion-hooks t)
3508          pos date bface eface)
3509     (save-excursion
3510       (save-restriction
3511         (widen)
3512         (goto-char (point-min))
3513         (while (or (setq date (get-text-property (setq pos (point))
3514                                                  'original-date))
3515                    (when (setq pos (next-single-property-change
3516                                     (point) 'original-date))
3517                      (setq date (get-text-property pos 'original-date))
3518                      t))
3519           (narrow-to-region
3520            pos (if (setq pos (text-property-any pos (point-max)
3521                                                 'original-date nil))
3522                    (progn
3523                      (goto-char pos)
3524                      (if (or (bolp) (eobp))
3525                          (point)
3526                        (1+ (point))))
3527                  (point-max)))
3528           (goto-char (point-min))
3529           (when (re-search-forward tdate-regexp nil t)
3530             (setq bface (get-text-property (point-at-bol) 'face)
3531                   eface (get-text-property (1- (point-at-eol)) 'face)))
3532           (goto-char (point-min))
3533           (setq pos nil)
3534           ;; Delete any old Date headers.
3535           (while (re-search-forward date-regexp nil t)
3536             (if pos
3537                 (delete-region (point-at-bol) (progn
3538                                                 (gnus-article-forward-header)
3539                                                 (point)))
3540               (delete-region (point-at-bol) (progn
3541                                               (gnus-article-forward-header)
3542                                               (forward-char -1)
3543                                               (point)))
3544               (setq pos (point))))
3545           (when (and (not pos)
3546                      (re-search-forward tdate-regexp nil t))
3547             (forward-line 1))
3548           (gnus-goto-char pos)
3549           (insert (article-make-date-line date (or type 'ut)))
3550           (unless pos
3551             (insert "\n")
3552             (forward-line -1))
3553           ;; Do highlighting.
3554           (beginning-of-line)
3555           (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
3556             (put-text-property (match-beginning 1) (1+ (match-end 1))
3557                                'face bface)
3558             (put-text-property (match-beginning 2) (match-end 2)
3559                                'face eface))
3560           (put-text-property (point-min) (1- (point-max)) 'original-date date)
3561           (goto-char (point-max))
3562           (widen))))))
3563
3564 (defun article-make-date-line (date type)
3565   "Return a DATE line of TYPE."
3566   (unless (memq type '(local ut original user iso8601 lapsed english))
3567     (error "Unknown conversion type: %s" type))
3568   (condition-case ()
3569       (let ((time (date-to-time date)))
3570         (cond
3571          ;; Convert to the local timezone.
3572          ((eq type 'local)
3573           (concat "Date: " (message-make-date time)))
3574          ;; Convert to Universal Time.
3575          ((eq type 'ut)
3576           (concat "Date: "
3577                   (substring
3578                    (message-make-date
3579                     (let* ((e (parse-time-string date))
3580                            (tm (apply 'encode-time e))
3581                            (ms (car tm))
3582                            (ls (- (cadr tm) (car (current-time-zone time)))))
3583                       (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
3584                             ((> ls 65535) (list (1+ ms) (- ls 65536)))
3585                             (t (list ms ls)))))
3586                    0 -5)
3587                   "UT"))
3588          ;; Get the original date from the article.
3589          ((eq type 'original)
3590           (concat "Date: " (if (string-match "\n+$" date)
3591                                (substring date 0 (match-beginning 0))
3592                              date)))
3593          ;; Let the user define the format.
3594          ((eq type 'user)
3595           (let ((format (or (condition-case nil
3596                                 (with-current-buffer gnus-summary-buffer
3597                                   gnus-article-time-format)
3598                               (error nil))
3599                             gnus-article-time-format)))
3600             (if (functionp format)
3601                 (funcall format time)
3602               (concat "Date: " (format-time-string format time)))))
3603          ;; ISO 8601.
3604          ((eq type 'iso8601)
3605           (let ((tz (car (current-time-zone time))))
3606             (concat
3607              "Date: "
3608              (format-time-string "%Y%m%dT%H%M%S" time)
3609              (format "%s%02d%02d"
3610                      (if (> tz 0) "+" "-") (/ (abs tz) 3600)
3611                      (/ (% (abs tz) 3600) 60)))))
3612          ;; Do an X-Sent lapsed format.
3613          ((eq type 'lapsed)
3614           ;; If the date is seriously mangled, the timezone functions are
3615           ;; liable to bug out, so we ignore all errors.
3616           (let* ((now (current-time))
3617                  (real-time (subtract-time now time))
3618                  (real-sec (and real-time
3619                                 (+ (* (float (car real-time)) 65536)
3620                                    (cadr real-time))))
3621                  (sec (and real-time (abs real-sec)))
3622                  num prev)
3623             (cond
3624              ((null real-time)
3625               "X-Sent: Unknown")
3626              ((zerop sec)
3627               "X-Sent: Now")
3628              (t
3629               (concat
3630                "X-Sent: "
3631                ;; This is a bit convoluted, but basically we go
3632                ;; through the time units for years, weeks, etc,
3633                ;; and divide things to see whether that results
3634                ;; in positive answers.
3635                (mapconcat
3636                 (lambda (unit)
3637                   (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
3638                       ;; The (remaining) seconds are too few to
3639                       ;; be divided into this time unit.
3640                       ""
3641                     ;; It's big enough, so we output it.
3642                     (setq sec (- sec (* num (cdr unit))))
3643                     (prog1
3644                         (concat (if prev ", " "") (int-to-string
3645                                                    (floor num))
3646                                 " " (symbol-name (car unit))
3647                                 (if (> num 1) "s" ""))
3648                       (setq prev t))))
3649                 article-time-units "")
3650                ;; If dates are odd, then it might appear like the
3651                ;; article was sent in the future.
3652                (if (> real-sec 0)
3653                    " ago"
3654                  " in the future"))))))
3655          ;; Display the date in proper English
3656          ((eq type 'english)
3657           (let ((dtime (decode-time time)))
3658             (concat
3659              "Date: the "
3660              (number-to-string (nth 3 dtime))
3661              (let ((digit (% (nth 3 dtime) 10)))
3662                (cond
3663                 ((memq (nth 3 dtime) '(11 12 13)) "th")
3664                 ((= digit 1) "st")
3665                 ((= digit 2) "nd")
3666                 ((= digit 3) "rd")
3667                 (t "th")))
3668              " of "
3669              (nth (1- (nth 4 dtime)) gnus-english-month-names)
3670              " "
3671              (number-to-string (nth 5 dtime))
3672              " at "
3673              (format "%02d" (nth 2 dtime))
3674              ":"
3675              (format "%02d" (nth 1 dtime)))))))
3676     (error
3677      (format "Date: %s (from Gnus)" date))))
3678
3679 (defun article-date-local (&optional highlight)
3680   "Convert the current article date to the local timezone."
3681   (interactive (list t))
3682   (article-date-ut 'local highlight))
3683
3684 (defun article-date-english (&optional highlight)
3685   "Convert the current article date to something that is proper English."
3686   (interactive (list t))
3687   (article-date-ut 'english highlight))
3688
3689 (defun article-date-original (&optional highlight)
3690   "Convert the current article date to what it was originally.
3691 This is only useful if you have used some other date conversion
3692 function and want to see what the date was before converting."
3693   (interactive (list t))
3694   (article-date-ut 'original highlight))
3695
3696 (defun article-date-lapsed (&optional highlight)
3697   "Convert the current article date to time lapsed since it was sent."
3698   (interactive (list t))
3699   (article-date-ut 'lapsed highlight))
3700
3701 (defun article-update-date-lapsed ()
3702   "Function to be run from a timer to update the lapsed time line."
3703   (save-match-data
3704     (let (deactivate-mark)
3705       (save-excursion
3706         (ignore-errors
3707          (walk-windows
3708           (lambda (w)
3709             (set-buffer (window-buffer w))
3710             (when (eq major-mode 'gnus-article-mode)
3711               (let ((mark (point-marker)))
3712                 (goto-char (point-min))
3713                 (when (re-search-forward "^X-Sent:" nil t)
3714                   (article-date-lapsed t))
3715                 (goto-char (marker-position mark))
3716                 (move-marker mark nil))))
3717           nil 'visible))))))
3718
3719 (defun gnus-start-date-timer (&optional n)
3720   "Start a timer to update the X-Sent header in the article buffers.
3721 The numerical prefix says how frequently (in seconds) the function
3722 is to run."
3723   (interactive "p")
3724   (unless n
3725     (setq n 1))
3726   (gnus-stop-date-timer)
3727   (setq article-lapsed-timer
3728         (run-at-time 1 n 'article-update-date-lapsed)))
3729
3730 (defun gnus-stop-date-timer ()
3731   "Stop the X-Sent timer."
3732   (interactive)
3733   (when article-lapsed-timer
3734     (nnheader-cancel-timer article-lapsed-timer)
3735     (setq article-lapsed-timer nil)))
3736
3737 (defun article-date-user (&optional highlight)
3738   "Convert the current article date to the user-defined format.
3739 This format is defined by the `gnus-article-time-format' variable."
3740   (interactive (list t))
3741   (article-date-ut 'user highlight))
3742
3743 (defun article-date-iso8601 (&optional highlight)
3744   "Convert the current article date to ISO8601."
3745   (interactive (list t))
3746   (article-date-ut 'iso8601 highlight))
3747
3748 (defmacro gnus-article-save-original-date (&rest forms)
3749   "Save the original date as a text property and evaluate FORMS."
3750   `(let* ((case-fold-search t)
3751           (start (progn
3752                    (goto-char (point-min))
3753                    (when (and (re-search-forward "^date:[\t\n ]+" nil t)
3754                               (not (bolp)))
3755                      (match-end 0))))
3756           (date (when (and start
3757                            (re-search-forward "[\t ]*\n\\(?:[^\t ]\\|\\'\\)"
3758                                               nil t))
3759                   (buffer-substring-no-properties start
3760                                                   (match-beginning 0)))))
3761      (goto-char (point-max))
3762      (skip-chars-backward "\n")
3763      (put-text-property (point-min) (point) 'original-date date)
3764      ,@forms
3765      (goto-char (point-max))
3766      (skip-chars-backward "\n")
3767      (put-text-property (point-min) (point) 'original-date date)))
3768
3769 ;; (defun article-show-all ()
3770 ;;   "Show all hidden text in the article buffer."
3771 ;;   (interactive)
3772 ;;   (save-excursion
3773 ;;     (let ((inhibit-read-only t))
3774 ;;       (gnus-article-unhide-text (point-min) (point-max)))))
3775
3776 (defun article-remove-leading-whitespace ()
3777   "Remove excessive whitespace from all headers."
3778   (interactive)
3779   (save-excursion
3780     (save-restriction
3781       (let ((inhibit-read-only t))
3782         (article-narrow-to-head)
3783         (goto-char (point-min))
3784         (while (re-search-forward "^[^ :]+: \\([ \t]+\\)" nil t)
3785           (delete-region (match-beginning 1) (match-end 1)))))))
3786
3787 (defun article-emphasize (&optional arg)
3788   "Emphasize text according to `gnus-emphasis-alist'."
3789   (interactive (gnus-article-hidden-arg))
3790   (unless (gnus-article-check-hidden-text 'emphasis arg)
3791     (save-excursion
3792       (let ((alist (or
3793                     (condition-case nil
3794                         (with-current-buffer gnus-summary-buffer
3795                           gnus-article-emphasis-alist)
3796                       (error))
3797                     gnus-emphasis-alist))
3798             (inhibit-read-only t)
3799             (props (append '(article-type emphasis)
3800                            gnus-hidden-properties))
3801             regexp elem beg invisible visible face)
3802         (article-goto-body)
3803         (setq beg (point))
3804         (while (setq elem (pop alist))
3805           (goto-char beg)
3806           (setq regexp (car elem)
3807                 invisible (nth 1 elem)
3808                 visible (nth 2 elem)
3809                 face (nth 3 elem))
3810           (while (re-search-forward regexp nil t)
3811             (when (and (match-beginning visible) (match-beginning invisible))
3812               (gnus-article-hide-text
3813                (match-beginning invisible) (match-end invisible) props)
3814               (gnus-article-unhide-text-type
3815                (match-beginning visible) (match-end visible) 'emphasis)
3816               (gnus-put-overlay-excluding-newlines
3817                (match-beginning visible) (match-end visible) 'face face)
3818               (gnus-add-wash-type 'emphasis)
3819               (goto-char (match-end invisible)))))))))
3820
3821 (defun gnus-article-setup-highlight-words (&optional highlight-words)
3822   "Setup newsgroup emphasis alist."
3823   (unless gnus-article-emphasis-alist
3824     (let ((name (and gnus-newsgroup-name
3825                      (gnus-group-real-name gnus-newsgroup-name))))
3826       (make-local-variable 'gnus-article-emphasis-alist)
3827       (setq gnus-article-emphasis-alist
3828             (nconc
3829              (let ((alist gnus-group-highlight-words-alist) elem highlight)
3830                (while (setq elem (pop alist))
3831                  (when (and name (string-match (car elem) name))
3832                    (setq alist nil
3833                          highlight (copy-sequence (cdr elem)))))
3834                highlight)
3835              (copy-sequence highlight-words)
3836              (if gnus-newsgroup-name
3837                  (copy-sequence (gnus-group-find-parameter
3838                                  gnus-newsgroup-name 'highlight-words t)))
3839              gnus-emphasis-alist)))))
3840
3841 (defvar gnus-summary-article-menu)
3842 (defvar gnus-summary-post-menu)
3843
3844 ;;; Saving functions.
3845
3846 (defun gnus-article-save (save-buffer file &optional num)
3847   "Save the currently selected article."
3848   (when (or (get gnus-default-article-saver :headers)
3849             (not gnus-save-all-headers))
3850     ;; Remove headers according to `gnus-saved-headers' or the value
3851     ;; of the `:headers' property that the saver function might have.
3852     (let ((gnus-visible-headers
3853            (or (symbol-value (get gnus-default-article-saver :headers))
3854                gnus-saved-headers gnus-visible-headers))
3855           ;; Ignore group parameter.  See `article-hide-headers'.
3856           (gnus-summary-buffer nil))
3857       (with-current-buffer save-buffer
3858         (article-hide-headers 1 t))))
3859   (save-window-excursion
3860     (if (not gnus-default-article-saver)
3861         (error "No default saver is defined")
3862       ;; !!! Magic!  The saving functions all save
3863       ;; `gnus-save-article-buffer' (or so they think), but we
3864       ;; bind that variable to our save-buffer.
3865       (set-buffer gnus-article-buffer)
3866       (let* ((gnus-save-article-buffer save-buffer)
3867              (filename
3868               (cond
3869                ((not gnus-prompt-before-saving) 'default)
3870                ((eq gnus-prompt-before-saving 'always) nil)
3871                (t file)))
3872              (gnus-number-of-articles-to-be-saved
3873               (when (eq gnus-prompt-before-saving t)
3874                 num)))                  ; Magic
3875         (set-buffer gnus-article-current-summary)
3876         (funcall gnus-default-article-saver filename)))))
3877
3878 (defun gnus-read-save-file-name (prompt &optional filename
3879                                         function group headers variable
3880                                         dir-var)
3881   (let ((default-name
3882           (funcall function group headers (symbol-value variable)))
3883         result)
3884     (setq result
3885           (expand-file-name
3886            (cond
3887             ((eq filename 'default)
3888              default-name)
3889             ((eq filename t)
3890              default-name)
3891             (filename filename)
3892             (t
3893              (when (symbol-value dir-var)
3894                (setq default-name (expand-file-name
3895                                    (file-name-nondirectory default-name)
3896                                    (symbol-value dir-var))))
3897              (let* ((split-name (gnus-get-split-value gnus-split-methods))
3898                     (prompt
3899                      (format prompt
3900                              (if (and gnus-number-of-articles-to-be-saved
3901                                       (> gnus-number-of-articles-to-be-saved 1))
3902                                  (format "these %d articles"
3903                                          gnus-number-of-articles-to-be-saved)
3904                                "this article")))
3905                     (file
3906                      ;; Let the split methods have their say.
3907                      (cond
3908                       ;; No split name was found.
3909                       ((null split-name)
3910                        (read-file-name
3911                         (concat prompt " (default "
3912                                 (file-name-nondirectory default-name) "): ")
3913                         (file-name-directory default-name)
3914                         default-name))
3915                       ;; A single group name is returned.
3916                       ((stringp split-name)
3917                        (setq default-name
3918                              (funcall function split-name headers
3919                                       (symbol-value variable)))
3920                        (read-file-name
3921                         (concat prompt " (default "
3922                                 (file-name-nondirectory default-name) "): ")
3923                         (file-name-directory default-name)
3924                         default-name))
3925                       ;; A single split name was found
3926                       ((= 1 (length split-name))
3927                        (let* ((name (expand-file-name
3928                                      (car split-name)
3929                                      gnus-article-save-directory))
3930                               (dir (cond ((file-directory-p name)
3931                                           (file-name-as-directory name))
3932                                          ((file-exists-p name) name)
3933                                          (t gnus-article-save-directory))))
3934                          (read-file-name
3935                           (concat prompt " (default " name "): ")
3936                           dir name)))
3937                       ;; A list of splits was found.
3938                       (t
3939                        (setq split-name (nreverse split-name))
3940                        (let (result)
3941                          (let ((file-name-history
3942                                 (nconc split-name file-name-history)))
3943                            (setq result
3944                                  (expand-file-name
3945                                   (read-file-name
3946                                    (concat prompt " (`M-p' for defaults): ")
3947                                    gnus-article-save-directory
3948                                    (car split-name))
3949                                   gnus-article-save-directory)))
3950                          (car (push result file-name-history)))))))
3951                ;; Create the directory.
3952                (gnus-make-directory (file-name-directory file))
3953                ;; If we have read a directory, we append the default file name.
3954                (when (file-directory-p file)
3955                  (setq file (expand-file-name (file-name-nondirectory
3956                                                default-name)
3957                                               (file-name-as-directory file))))
3958                ;; Possibly translate some characters.
3959                (nnheader-translate-file-chars file))))))
3960     (gnus-make-directory (file-name-directory result))
3961     (when variable
3962       (set variable result))
3963     (when dir-var
3964       (set dir-var (file-name-directory result)))
3965     result))
3966
3967 (defun gnus-article-archive-name (group)
3968   "Return the first instance of an \"Archive-name\" in the current buffer."
3969   (let ((case-fold-search t))
3970     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
3971       (nnheader-concat gnus-article-save-directory
3972                        (match-string 1)))))
3973
3974 (defun gnus-article-nndoc-name (group)
3975   "If GROUP is an nndoc group, return the name of the parent group."
3976   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
3977     (gnus-group-get-parameter group 'save-article-group)))
3978
3979 (defun gnus-summary-save-in-rmail (&optional filename)
3980   "Append this article to Rmail file.
3981 Optional argument FILENAME specifies file name.
3982 Directory to save to is default to `gnus-article-save-directory'."
3983   (setq filename (gnus-read-save-file-name
3984                   "Save %s in rmail file" filename
3985                   gnus-rmail-save-name gnus-newsgroup-name
3986                   gnus-current-headers 'gnus-newsgroup-last-rmail))
3987   (gnus-eval-in-buffer-window gnus-save-article-buffer
3988     (save-excursion
3989       (save-restriction
3990         (widen)
3991         ;; Note that unlike gnus-summary-save-in-mail, there is no
3992         ;; check to see if filename is Babyl.  Rmail in Emacs 23 does
3993         ;; not use Babyl.
3994         (gnus-output-to-rmail filename))))
3995   filename)
3996
3997 (defun gnus-summary-save-in-mail (&optional filename)
3998   "Append this article to Unix mail file.
3999 Optional argument FILENAME specifies file name.
4000 Directory to save to is default to `gnus-article-save-directory'."
4001   (setq filename (gnus-read-save-file-name
4002                   "Save %s in Unix mail file" filename
4003                   gnus-mail-save-name gnus-newsgroup-name
4004                   gnus-current-headers 'gnus-newsgroup-last-mail))
4005   (gnus-eval-in-buffer-window gnus-save-article-buffer
4006     (save-excursion
4007       (save-restriction
4008         (widen)
4009         (if (and (file-readable-p filename)
4010                  (file-regular-p filename)
4011                  (mail-file-babyl-p filename))
4012             (gnus-output-to-rmail filename)
4013           (gnus-output-to-mail filename)))))
4014   filename)
4015
4016 (put 'gnus-summary-save-in-file :decode t)
4017 (put 'gnus-summary-save-in-file :headers 'gnus-saved-headers)
4018 (defun gnus-summary-save-in-file (&optional filename overwrite)
4019   "Append this article to file.
4020 Optional argument FILENAME specifies file name.
4021 Directory to save to is default to `gnus-article-save-directory'."
4022   (setq filename (gnus-read-save-file-name
4023                   "Save %s in file" filename
4024                   gnus-file-save-name gnus-newsgroup-name
4025                   gnus-current-headers 'gnus-newsgroup-last-file))
4026   (gnus-eval-in-buffer-window gnus-save-article-buffer
4027     (save-excursion
4028       (save-restriction
4029         (widen)
4030         (when (and overwrite
4031                    (file-exists-p filename))
4032           (delete-file filename))
4033         (gnus-output-to-file filename))))
4034   filename)
4035
4036 (put 'gnus-summary-write-to-file :decode t)
4037 (put 'gnus-summary-write-to-file :function 'gnus-summary-save-in-file)
4038 (put 'gnus-summary-write-to-file :headers 'gnus-saved-headers)
4039 (defun gnus-summary-write-to-file (&optional filename)
4040   "Write this article to a file, overwriting it if the file exists.
4041 Optional argument FILENAME specifies file name.
4042 The directory to save in defaults to `gnus-article-save-directory'."
4043   (setq filename (gnus-read-save-file-name
4044                   "Save %s in file" filename
4045                   gnus-file-save-name gnus-newsgroup-name
4046                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
4047   (gnus-summary-save-in-file filename t))
4048
4049 (put 'gnus-summary-save-body-in-file :decode t)
4050 (defun gnus-summary-save-body-in-file (&optional filename overwrite)
4051   "Append this article body to a file.
4052 Optional argument FILENAME specifies file name.
4053 The directory to save in defaults to `gnus-article-save-directory'."
4054   (setq filename (gnus-read-save-file-name
4055                   "Save %s body in file" filename
4056                   gnus-file-save-name gnus-newsgroup-name
4057                   gnus-current-headers 'gnus-newsgroup-last-file))
4058   (gnus-eval-in-buffer-window gnus-save-article-buffer
4059     (save-excursion
4060       (save-restriction
4061         (widen)
4062         (when (article-goto-body)
4063           (narrow-to-region (point) (point-max)))
4064         (when (and overwrite
4065                    (file-exists-p filename))
4066           (delete-file filename))
4067         (gnus-output-to-file filename))))
4068   filename)
4069
4070 (put 'gnus-summary-write-body-to-file :decode t)
4071 (put 'gnus-summary-write-body-to-file
4072      :function 'gnus-summary-save-body-in-file)
4073 (defun gnus-summary-write-body-to-file (&optional filename)
4074   "Write this article body to a file, overwriting it if the file exists.
4075 Optional argument FILENAME specifies file name.
4076 The directory to save in defaults to `gnus-article-save-directory'."
4077   (setq filename (gnus-read-save-file-name
4078                   "Save %s body in file" filename
4079                   gnus-file-save-name gnus-newsgroup-name
4080                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
4081   (gnus-summary-save-body-in-file filename t))
4082
4083 (put 'gnus-summary-save-in-pipe :decode t)
4084 (put 'gnus-summary-save-in-pipe :headers 'gnus-saved-headers)
4085 (defun gnus-summary-save-in-pipe (&optional command raw)
4086   "Pipe this article to subprocess COMMAND.
4087 Valid values for COMMAND include:
4088   a string
4089     The executable command name and possibly arguments.
4090   nil
4091     You will be prompted for the command in the minibuffer.
4092   the symbol `default'
4093     It will be replaced with the command which the variable
4094     `gnus-summary-pipe-output-default-command' holds or the command
4095     last used for saving.
4096 Non-nil value for RAW overrides `:decode' and `:headers' properties
4097 and the raw article including all headers will be piped."
4098   (let ((article (gnus-summary-article-number))
4099         (decode (unless raw
4100                   (get 'gnus-summary-save-in-pipe :decode)))
4101         save-buffer default)
4102     (if article
4103         (if (vectorp (gnus-summary-article-header article))
4104             (save-current-buffer
4105               (gnus-summary-select-article decode decode nil article)
4106               (insert-buffer-substring
4107                (prog1
4108                    (if decode
4109                        gnus-article-buffer
4110                      gnus-original-article-buffer)
4111                  (setq save-buffer
4112                        (nnheader-set-temp-buffer " *Gnus Save*"))))
4113               ;; Remove unwanted headers.
4114               (when (and (not raw)
4115                          (or (get 'gnus-summary-save-in-pipe :headers)
4116                              (not gnus-save-all-headers)))
4117                 (let ((gnus-visible-headers
4118                        (or (symbol-value (get 'gnus-summary-save-in-pipe
4119                                               :headers))
4120                            gnus-saved-headers gnus-visible-headers))
4121                       (gnus-summary-buffer nil))
4122                   (article-hide-headers 1 t))))
4123           (error "%d is not a real article" article))
4124       (error "No article to pipe"))
4125     (setq default (or gnus-summary-pipe-output-default-command
4126                       gnus-last-shell-command))
4127     (unless (stringp command)
4128       (setq command
4129             (if (and (eq command 'default) default)
4130                 default
4131               (gnus-read-shell-command "Shell command on this article: "
4132                                        default))))
4133     (when (string-equal command "")
4134       (if default
4135           (setq command default)
4136         (error "A command is required")))
4137     (gnus-eval-in-buffer-window save-buffer
4138       (save-restriction
4139         (widen)
4140         (shell-command-on-region (point-min) (point-max) command nil)))
4141     (gnus-kill-buffer save-buffer))
4142   (setq gnus-summary-pipe-output-default-command command))
4143
4144 (defun gnus-summary-pipe-to-muttprint (&optional command)
4145   "Pipe this article to muttprint."
4146   (unless (stringp command)
4147     (setq command (read-string
4148                    "Print using command: " gnus-summary-muttprint-program
4149                    nil gnus-summary-muttprint-program)))
4150   (let ((gnus-summary-pipe-output-default-command
4151          gnus-summary-pipe-output-default-command))
4152     (gnus-summary-save-in-pipe command))
4153   (setq gnus-summary-muttprint-program command))
4154
4155 ;;; Article file names when saving.
4156
4157 (defun gnus-capitalize-newsgroup (newsgroup)
4158   "Capitalize NEWSGROUP name."
4159   (when (not (zerop (length newsgroup)))
4160     (concat (char-to-string (upcase (aref newsgroup 0)))
4161             (substring newsgroup 1))))
4162
4163 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
4164   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4165 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
4166 Otherwise, it is like ~/News/news/group/num."
4167   (let ((default
4168           (expand-file-name
4169            (concat (if (gnus-use-long-file-name 'not-save)
4170                        (gnus-capitalize-newsgroup newsgroup)
4171                      (gnus-newsgroup-directory-form newsgroup))
4172                    "/" (int-to-string (mail-header-number headers)))
4173            gnus-article-save-directory)))
4174     (if (and last-file
4175              (string-equal (file-name-directory default)
4176                            (file-name-directory last-file))
4177              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4178         default
4179       (or last-file default))))
4180
4181 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
4182   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4183 If variable `gnus-use-long-file-name' is non-nil, it is
4184 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
4185   (let ((default
4186           (expand-file-name
4187            (concat (if (gnus-use-long-file-name 'not-save)
4188                        newsgroup
4189                      (gnus-newsgroup-directory-form newsgroup))
4190                    "/" (int-to-string (mail-header-number headers)))
4191            gnus-article-save-directory)))
4192     (if (and last-file
4193              (string-equal (file-name-directory default)
4194                            (file-name-directory last-file))
4195              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4196         default
4197       (or last-file default))))
4198
4199 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
4200   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4201 If variable `gnus-use-long-file-name' is non-nil, it is
4202 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
4203   (or last-file
4204       (expand-file-name
4205        (if (gnus-use-long-file-name 'not-save)
4206            newsgroup
4207          (file-relative-name
4208           (expand-file-name "news" (gnus-newsgroup-directory-form newsgroup))
4209           default-directory))
4210        gnus-article-save-directory)))
4211
4212 (defun gnus-sender-save-name (newsgroup headers &optional last-file)
4213   "Generate file name from sender."
4214   (let ((from (mail-header-from headers)))
4215     (expand-file-name
4216      (if (and from (string-match "\\([^ <]+\\)@" from))
4217          (match-string 1 from)
4218        "nobody")
4219      gnus-article-save-directory)))
4220
4221 (defun article-verify-x-pgp-sig ()
4222   "Verify X-PGP-Sig."
4223   ;; <ftp://ftp.isc.org/pub/pgpcontrol/FORMAT>
4224   (interactive)
4225   (if (gnus-buffer-live-p gnus-original-article-buffer)
4226       (let ((sig (with-current-buffer gnus-original-article-buffer
4227                    (gnus-fetch-field "X-PGP-Sig")))
4228             items info headers)
4229         (when (and sig
4230                    mml2015-use
4231                    (mml2015-clear-verify-function))
4232           (with-temp-buffer
4233             (insert-buffer-substring gnus-original-article-buffer)
4234             (setq items (split-string sig))
4235             (message-narrow-to-head)
4236             (let ((inhibit-point-motion-hooks t)
4237                   (case-fold-search t))
4238               ;; Don't verify multiple headers.
4239               (setq headers (mapconcat (lambda (header)
4240                                          (concat header ": "
4241                                                  (mail-fetch-field header)
4242                                                  "\n"))
4243                                        (split-string (nth 1 items) ",") "")))
4244             (delete-region (point-min) (point-max))
4245             (insert "-----BEGIN PGP SIGNED MESSAGE-----\n\n")
4246             (insert "X-Signed-Headers: " (nth 1 items) "\n")
4247             (insert headers)
4248             (widen)
4249             (forward-line)
4250             (while (not (eobp))
4251               (if (looking-at "^-")
4252                   (insert "- "))
4253               (forward-line))
4254             (insert "\n-----BEGIN PGP SIGNATURE-----\n")
4255             (insert "Version: " (car items) "\n\n")
4256             (insert (mapconcat 'identity (cddr items) "\n"))
4257             (insert "\n-----END PGP SIGNATURE-----\n")
4258             (let ((mm-security-handle (list (format "multipart/signed"))))
4259               (mml2015-clean-buffer)
4260               (let ((coding-system-for-write (or gnus-newsgroup-charset
4261                                                  'iso-8859-1)))
4262                 (funcall (mml2015-clear-verify-function)))
4263               (setq info
4264                     (or (mm-handle-multipart-ctl-parameter
4265                          mm-security-handle 'gnus-details)
4266                         (mm-handle-multipart-ctl-parameter
4267                          mm-security-handle 'gnus-info)))))
4268           (when info
4269             (let ((inhibit-read-only t) bface eface)
4270               (save-restriction
4271                 (message-narrow-to-head)
4272                 (goto-char (point-max))
4273                 (forward-line -1)
4274                 (setq bface (get-text-property (point-at-bol) 'face)
4275                       eface (get-text-property (1- (point-at-eol)) 'face))
4276                 (message-remove-header "X-Gnus-PGP-Verify")
4277                 (if (re-search-forward "^X-PGP-Sig:" nil t)
4278                     (forward-line)
4279                   (goto-char (point-max)))
4280                 (narrow-to-region (point) (point))
4281                 (insert "X-Gnus-PGP-Verify: " info "\n")
4282                 (goto-char (point-min))
4283                 (forward-line)
4284                 (while (not (eobp))
4285                   (if (not (looking-at "^[ \t]"))
4286                       (insert " "))
4287                   (forward-line))
4288                 ;; Do highlighting.
4289                 (goto-char (point-min))
4290                 (when (looking-at "\\([^:]+\\): *")
4291                   (put-text-property (match-beginning 1) (1+ (match-end 1))
4292                                      'face bface)
4293                   (put-text-property (match-end 0) (point-max)
4294                                      'face eface)))))))))
4295
4296 (autoload 'canlock-verify "canlock" nil t) ;; for XEmacs.
4297
4298 (defun article-verify-cancel-lock ()
4299   "Verify Cancel-Lock header."
4300   (interactive)
4301   (if (gnus-buffer-live-p gnus-original-article-buffer)
4302       (canlock-verify gnus-original-article-buffer)))
4303
4304 (eval-and-compile
4305   (mapc
4306    (lambda (func)
4307      (let (afunc gfunc)
4308        (if (consp func)
4309            (setq afunc (car func)
4310                  gfunc (cdr func))
4311          (setq afunc func
4312                gfunc (intern (format "gnus-%s" func))))
4313        (defalias gfunc
4314          (when (fboundp afunc)
4315            `(lambda (&optional interactive &rest args)
4316               ,(documentation afunc t)
4317               (interactive (list t))
4318               (with-current-buffer gnus-article-buffer
4319                 (if interactive
4320                     (call-interactively ',afunc)
4321                   (apply ',afunc args))))))))
4322    '(article-hide-headers
4323      article-verify-x-pgp-sig
4324      article-verify-cancel-lock
4325      article-hide-boring-headers
4326      article-treat-overstrike
4327      article-treat-ansi-sequences
4328      article-fill-long-lines
4329      article-capitalize-sentences
4330      article-remove-cr
4331      article-remove-leading-whitespace
4332      article-display-x-face
4333      article-display-face
4334      article-de-quoted-unreadable
4335      article-de-base64-unreadable
4336      article-decode-HZ
4337      article-wash-html
4338      article-unsplit-urls
4339      article-hide-list-identifiers
4340      article-strip-banner
4341      article-babel
4342      article-hide-pem
4343      article-hide-signature
4344      article-strip-headers-in-body
4345      article-remove-trailing-blank-lines
4346      article-strip-leading-blank-lines
4347      article-strip-multiple-blank-lines
4348      article-strip-leading-space
4349      article-strip-trailing-space
4350      article-strip-blank-lines
4351      article-strip-all-blank-lines
4352      article-date-local
4353      article-date-english
4354      article-date-iso8601
4355      article-date-original
4356      article-date-ut
4357      article-decode-mime-words
4358      article-decode-charset
4359      article-decode-encoded-words
4360      article-date-user
4361      article-date-lapsed
4362      article-emphasize
4363      article-treat-dumbquotes
4364      article-normalize-headers
4365      ;;(article-show-all . gnus-article-show-all-headers)
4366      )))
4367 \f
4368 ;;;
4369 ;;; Gnus article mode
4370 ;;;
4371
4372 (put 'gnus-article-mode 'mode-class 'special)
4373
4374 (set-keymap-parent gnus-article-mode-map widget-keymap)
4375
4376 (gnus-define-keys gnus-article-mode-map
4377   " " gnus-article-goto-next-page
4378   "\177" gnus-article-goto-prev-page
4379   [delete] gnus-article-goto-prev-page
4380   [backspace] gnus-article-goto-prev-page
4381   "\C-c^" gnus-article-refer-article
4382   "h" gnus-article-show-summary
4383   "s" gnus-article-show-summary
4384   "\C-c\C-m" gnus-article-mail
4385   "?" gnus-article-describe-briefly
4386   "<" beginning-of-buffer
4387   ">" end-of-buffer
4388   "\C-c\C-i" gnus-info-find-node
4389   "\C-c\C-b" gnus-bug
4390   "R" gnus-article-reply-with-original
4391   "F" gnus-article-followup-with-original
4392   "\C-hk" gnus-article-describe-key
4393   "\C-hc" gnus-article-describe-key-briefly
4394   "\C-hb" gnus-article-describe-bindings
4395
4396   "e" gnus-article-read-summary-keys
4397   "\C-d" gnus-article-read-summary-keys
4398   "\M-*" gnus-article-read-summary-keys
4399   "\M-#" gnus-article-read-summary-keys
4400   "\M-^" gnus-article-read-summary-keys
4401   "\M-g" gnus-article-read-summary-keys)
4402
4403 (substitute-key-definition
4404  'undefined 'gnus-article-read-summary-keys gnus-article-mode-map)
4405
4406 (gnus-define-keys (gnus-article-send-map "S" gnus-article-mode-map)
4407   "W" gnus-article-wide-reply-with-original)
4408 (if (featurep 'xemacs)
4409     (set-keymap-default-binding gnus-article-send-map
4410                                 'gnus-article-read-summary-send-keys)
4411   (define-key gnus-article-send-map [t] 'gnus-article-read-summary-send-keys))
4412
4413 (defun gnus-article-make-menu-bar ()
4414   (unless (boundp 'gnus-article-commands-menu)
4415     (gnus-summary-make-menu-bar))
4416   (gnus-turn-off-edit-menu 'article)
4417   (unless (boundp 'gnus-article-article-menu)
4418     (easy-menu-define
4419      gnus-article-article-menu gnus-article-mode-map ""
4420      '("Article"
4421        ["Scroll forwards" gnus-article-goto-next-page t]
4422        ["Scroll backwards" gnus-article-goto-prev-page t]
4423        ["Show summary" gnus-article-show-summary t]
4424        ["Fetch Message-ID at point" gnus-article-refer-article t]
4425        ["Mail to address at point" gnus-article-mail t]
4426        ["Send a bug report" gnus-bug t]))
4427
4428     (easy-menu-define
4429      gnus-article-treatment-menu gnus-article-mode-map ""
4430      ;; Fixme: this should use :active (and maybe :visible).
4431      '("Treatment"
4432        ["Hide headers" gnus-article-hide-headers t]
4433        ["Hide signature" gnus-article-hide-signature t]
4434        ["Hide citation" gnus-article-hide-citation t]
4435        ["Treat overstrike" gnus-article-treat-overstrike t]
4436        ["Treat ANSI sequences" gnus-article-treat-ansi-sequences t]
4437        ["Remove carriage return" gnus-article-remove-cr t]
4438        ["Remove leading whitespace" gnus-article-remove-leading-whitespace t]
4439        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]
4440        ["Remove base64" gnus-article-de-base64-unreadable t]
4441        ["Treat html" gnus-article-wash-html t]
4442        ["Remove newlines from within URLs" gnus-article-unsplit-urls t]
4443        ["Decode HZ" gnus-article-decode-HZ t]))
4444
4445     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
4446
4447     ;; Note "Post" menu is defined in gnus-sum.el for consistency
4448
4449     (gnus-run-hooks 'gnus-article-menu-hook)))
4450
4451 (defvar bookmark-make-record-function)
4452
4453 (defun gnus-article-mode ()
4454   "Major mode for displaying an article.
4455
4456 All normal editing commands are switched off.
4457
4458 The following commands are available in addition to all summary mode
4459 commands:
4460 \\<gnus-article-mode-map>
4461 \\[gnus-article-next-page]\t Scroll the article one page forwards
4462 \\[gnus-article-prev-page]\t Scroll the article one page backwards
4463 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
4464 \\[gnus-article-show-summary]\t Display the summary buffer
4465 \\[gnus-article-mail]\t Send a reply to the address near point
4466 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
4467 \\[gnus-info-find-node]\t Go to the Gnus info node"
4468   (interactive)
4469   (kill-all-local-variables)
4470   (gnus-simplify-mode-line)
4471   (setq mode-name "Article")
4472   (setq major-mode 'gnus-article-mode)
4473   (make-local-variable 'minor-mode-alist)
4474   (use-local-map gnus-article-mode-map)
4475   (when (gnus-visual-p 'article-menu 'menu)
4476     (gnus-article-make-menu-bar)
4477     (when gnus-summary-tool-bar-map
4478       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
4479   (gnus-update-format-specifications nil 'article-mode)
4480   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
4481   (set (make-local-variable 'gnus-page-broken) nil)
4482   (make-local-variable 'gnus-button-marker-list)
4483   (make-local-variable 'gnus-article-current-summary)
4484   (make-local-variable 'gnus-article-mime-handles)
4485   (make-local-variable 'gnus-article-decoded-p)
4486   (make-local-variable 'gnus-article-mime-handle-alist)
4487   (make-local-variable 'gnus-article-wash-types)
4488   (make-local-variable 'gnus-article-image-alist)
4489   (make-local-variable 'gnus-article-charset)
4490   (make-local-variable 'gnus-article-ignored-charsets)
4491   (set (make-local-variable 'bookmark-make-record-function)
4492        'gnus-summary-bookmark-make-record)
4493   ;; Prevent Emacs 22 from displaying non-break space with `nobreak-space'
4494   ;; face.
4495   (set (make-local-variable 'nobreak-char-display) nil)
4496   (setq cursor-in-non-selected-windows nil)
4497   (setq truncate-lines gnus-article-truncate-lines)
4498   (gnus-set-default-directory)
4499   (buffer-disable-undo)
4500   (setq buffer-read-only t
4501         show-trailing-whitespace nil)
4502   (set-syntax-table gnus-article-mode-syntax-table)
4503   (mm-enable-multibyte)
4504   (gnus-run-mode-hooks 'gnus-article-mode-hook))
4505
4506 (defvar gnus-button-marker-list nil
4507   "Regexp matching any of the regexps from `gnus-button-alist'.
4508 Internal variable.")
4509
4510 (defun gnus-article-setup-buffer ()
4511   "Initialize the article buffer."
4512   (let* ((name (if gnus-single-article-buffer "*Article*"
4513                  (concat "*Article " gnus-newsgroup-name "*")))
4514          (original
4515           (progn (string-match "\\*Article" name)
4516                  (concat " *Original Article"
4517                          (substring name (match-end 0))))))
4518     (setq gnus-article-buffer name)
4519     (setq gnus-original-article-buffer original)
4520     (setq gnus-article-mime-handle-alist nil)
4521     (with-current-buffer gnus-summary-buffer
4522       ;; This might be a variable local to the summary buffer.
4523       (unless gnus-single-article-buffer
4524         (setq gnus-article-buffer name)
4525         (setq gnus-original-article-buffer original)
4526         (gnus-set-global-variables)))
4527     (gnus-article-setup-highlight-words)
4528     ;; Init original article buffer.
4529     (with-current-buffer (gnus-get-buffer-create gnus-original-article-buffer)
4530       (mm-enable-multibyte)
4531       (setq major-mode 'gnus-original-article-mode)
4532       (make-local-variable 'gnus-original-article))
4533     (if (and (get-buffer name)
4534              (with-current-buffer name
4535                (if gnus-article-edit-mode
4536                    (if (y-or-n-p "Article mode edit in progress; discard? ")
4537                        (progn
4538                          (set-buffer-modified-p nil)
4539                          (gnus-kill-buffer name)
4540                          (message "")
4541                          nil)
4542                      (error "Action aborted"))
4543                  t)))
4544         (with-current-buffer name
4545           (set (make-local-variable 'gnus-article-edit-mode) nil)
4546           (when gnus-article-mime-handles
4547             (mm-destroy-parts gnus-article-mime-handles)
4548             (setq gnus-article-mime-handles nil))
4549           ;; Set it to nil in article-buffer!
4550           (setq gnus-article-mime-handle-alist nil)
4551           (buffer-disable-undo)
4552           (setq buffer-read-only t)
4553           ;; This list just keeps growing if we don't reset it.
4554           (setq gnus-button-marker-list nil)
4555           (unless (eq major-mode 'gnus-article-mode)
4556             (gnus-article-mode))
4557           (current-buffer))
4558       (with-current-buffer (gnus-get-buffer-create name)
4559         (gnus-article-mode)
4560         (make-local-variable 'gnus-summary-buffer)
4561         (setq gnus-summary-buffer
4562               (gnus-summary-buffer-name gnus-newsgroup-name))
4563         (gnus-summary-set-local-parameters gnus-newsgroup-name)
4564         (current-buffer)))))
4565
4566 ;; Set article window start at LINE, where LINE is the number of lines
4567 ;; from the head of the article.
4568 (defun gnus-article-set-window-start (&optional line)
4569   (let ((article-window (gnus-get-buffer-window gnus-article-buffer t)))
4570     (when article-window
4571       (set-window-start
4572        article-window
4573        (with-current-buffer gnus-article-buffer
4574          (goto-char (point-min))
4575          (if (not line)
4576              (point-min)
4577            (gnus-message 6 "Moved to bookmark")
4578            (search-forward "\n\n" nil t)
4579            (forward-line line)
4580            (point)))))))
4581
4582 (defun gnus-article-prepare (article &optional all-headers header)
4583   "Prepare ARTICLE in article mode buffer.
4584 ARTICLE should either be an article number or a Message-ID.
4585 If ARTICLE is an id, HEADER should be the article headers.
4586 If ALL-HEADERS is non-nil, no headers are hidden."
4587   (save-excursion
4588     ;; Make sure we start in a summary buffer.
4589     (unless (eq major-mode 'gnus-summary-mode)
4590       (set-buffer gnus-summary-buffer))
4591     (setq gnus-summary-buffer (current-buffer))
4592     (let* ((gnus-article (if header (mail-header-number header) article))
4593            (summary-buffer (current-buffer))
4594            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
4595            (group gnus-newsgroup-name)
4596            result)
4597       (save-excursion
4598         (gnus-article-setup-buffer)
4599         (set-buffer gnus-article-buffer)
4600         ;; Deactivate active regions.
4601         (when (and (boundp 'transient-mark-mode)
4602                    transient-mark-mode)
4603           (setq mark-active nil))
4604         (if (not (setq result (let ((inhibit-read-only t))
4605                                 (gnus-request-article-this-buffer
4606                                  article group))))
4607             ;; There is no such article.
4608             (save-excursion
4609               (when (and (numberp article)
4610                          (not (memq article gnus-newsgroup-sparse)))
4611                 (setq gnus-article-current
4612                       (cons gnus-newsgroup-name article))
4613                 (set-buffer gnus-summary-buffer)
4614                 (setq gnus-current-article article)
4615                 (if (and (memq article gnus-newsgroup-undownloaded)
4616                          (not (gnus-online (gnus-find-method-for-group
4617                                             gnus-newsgroup-name))))
4618                     (progn
4619                       (gnus-summary-set-agent-mark article)
4620                       (message "Message marked for downloading"))
4621                   (gnus-summary-mark-article article gnus-canceled-mark)
4622                   (unless (memq article gnus-newsgroup-sparse)
4623                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
4624           (if (or (eq result 'pseudo)
4625                   (eq result 'nneething))
4626               (progn
4627                 (with-current-buffer summary-buffer
4628                   (push article gnus-newsgroup-history)
4629                   (setq gnus-last-article gnus-current-article
4630                         gnus-current-article 0
4631                         gnus-current-headers nil
4632                         gnus-article-current nil)
4633                   (if (eq result 'nneething)
4634                       (gnus-configure-windows 'summary)
4635                     (gnus-configure-windows 'article))
4636                   (gnus-set-global-variables))
4637                 (let ((gnus-article-mime-handle-alist-1
4638                        gnus-article-mime-handle-alist))
4639                   (gnus-set-mode-line 'article)))
4640             ;; The result from the `request' was an actual article -
4641             ;; or at least some text that is now displayed in the
4642             ;; article buffer.
4643             (when (and (numberp article)
4644                        (not (eq article gnus-current-article)))
4645               ;; Seems like a new article has been selected.
4646               ;; `gnus-current-article' must be an article number.
4647               (with-current-buffer summary-buffer
4648                 (push article gnus-newsgroup-history)
4649                 (setq gnus-last-article gnus-current-article
4650                       gnus-current-article article
4651                       gnus-current-headers
4652                       (gnus-summary-article-header gnus-current-article)
4653                       gnus-article-current
4654                       (cons gnus-newsgroup-name gnus-current-article))
4655                 (unless (vectorp gnus-current-headers)
4656                   (setq gnus-current-headers nil))
4657                 (gnus-summary-goto-subject gnus-current-article)
4658                 (when (gnus-summary-show-thread)
4659                   ;; If the summary buffer really was folded, the
4660                   ;; previous goto may not actually have gone to
4661                   ;; the right article, but the thread root instead.
4662                   ;; So we go again.
4663                   (gnus-summary-goto-subject gnus-current-article))
4664                 (gnus-run-hooks 'gnus-mark-article-hook)
4665                 (gnus-set-mode-line 'summary)
4666                 (when (gnus-visual-p 'article-highlight 'highlight)
4667                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
4668                 ;; Set the global newsgroup variables here.
4669                 (gnus-set-global-variables)
4670                 (setq gnus-have-all-headers
4671                       (or all-headers gnus-show-all-headers))))
4672             (save-excursion
4673               (gnus-configure-windows 'article))
4674             (when (or (numberp article)
4675                       (stringp article))
4676               (gnus-article-prepare-display)
4677               ;; Do page break.
4678               (goto-char (point-min))
4679               (when gnus-break-pages
4680                 (gnus-narrow-to-page)))
4681             (let ((gnus-article-mime-handle-alist-1
4682                    gnus-article-mime-handle-alist))
4683               (gnus-set-mode-line 'article))
4684             (article-goto-body)
4685             (unless (bobp)
4686               (forward-line -1))
4687             (set-window-point (get-buffer-window (current-buffer)) (point))
4688             (gnus-configure-windows 'article)
4689             t))))))
4690
4691 ;;;###autoload
4692 (defun gnus-article-prepare-display ()
4693   "Make the current buffer look like a nice article."
4694   ;; Hooks for getting information from the article.
4695   ;; This hook must be called before being narrowed.
4696   (let ((gnus-article-buffer (current-buffer))
4697         buffer-read-only
4698         (inhibit-read-only t))
4699     (unless (eq major-mode 'gnus-article-mode)
4700       (gnus-article-mode))
4701     (setq buffer-read-only nil
4702           gnus-article-wash-types nil
4703           gnus-article-image-alist nil)
4704     (gnus-run-hooks 'gnus-tmp-internal-hook)
4705     (when gnus-display-mime-function
4706       (funcall gnus-display-mime-function))
4707     (gnus-run-hooks 'gnus-article-prepare-hook)))
4708
4709 ;;;
4710 ;;; Gnus Sticky Article Mode
4711 ;;;
4712
4713 (define-derived-mode gnus-sticky-article-mode gnus-article-mode "StickyArticle"
4714   "Mode for sticky articles."
4715   ;; Release bindings that won't work.
4716   (substitute-key-definition 'gnus-article-read-summary-keys 'undefined
4717                              gnus-sticky-article-mode-map)
4718   (substitute-key-definition 'gnus-article-refer-article 'undefined
4719                              gnus-sticky-article-mode-map)
4720   (dolist (k '("e" "h" "s" "F" "R"))
4721     (define-key gnus-sticky-article-mode-map k nil))
4722   (define-key gnus-sticky-article-mode-map "k" 'gnus-kill-sticky-article-buffer)
4723   (define-key gnus-sticky-article-mode-map "q" 'bury-buffer)
4724   (define-key gnus-sticky-article-mode-map "\C-hc" 'describe-key-briefly)
4725   (define-key gnus-sticky-article-mode-map "\C-hk" 'describe-key))
4726
4727 (defun gnus-sticky-article (arg)
4728   "Make the current article sticky.
4729 If a prefix ARG is given, ask for a name for this sticky article buffer."
4730   (interactive "P")
4731   (gnus-summary-show-thread)
4732   (gnus-summary-select-article nil nil 'pseudo)
4733   (let (new-art-buf-name)
4734     (gnus-eval-in-buffer-window gnus-article-buffer
4735       (setq new-art-buf-name
4736             (concat
4737              "*Sticky Article: "
4738              (if arg
4739                  (read-from-minibuffer "Sticky article buffer name: ")
4740                (gnus-with-article-headers
4741                  (gnus-article-goto-header "subject")
4742                  (setq new-art-buf-name
4743                        (buffer-substring-no-properties
4744                         (line-beginning-position) (line-end-position)))
4745                  (goto-char (point-min))
4746                  (gnus-article-goto-header "from")
4747                  (setq new-art-buf-name
4748                        (concat
4749                         new-art-buf-name ", "
4750                         (buffer-substring-no-properties
4751                          (line-beginning-position) (line-end-position))))
4752                  (goto-char (point-min))
4753                  (gnus-article-goto-header "date")
4754                  (setq new-art-buf-name
4755                        (concat
4756                         new-art-buf-name ", "
4757                         (buffer-substring-no-properties
4758                          (line-beginning-position) (line-end-position))))))
4759              "*"))
4760       (if (and (gnus-buffer-live-p new-art-buf-name)
4761                (with-current-buffer new-art-buf-name
4762                  (eq major-mode 'gnus-sticky-article-mode)))
4763           (switch-to-buffer new-art-buf-name)
4764         (setq new-art-buf-name (rename-buffer new-art-buf-name t)))
4765       (gnus-sticky-article-mode))
4766     (setq gnus-article-buffer new-art-buf-name))
4767   (gnus-summary-recenter)
4768   (gnus-summary-position-point))
4769
4770 (defun gnus-kill-sticky-article-buffer (&optional buffer)
4771   "Kill the given sticky article BUFFER.
4772 If none is given, assume the current buffer and kill it if it has
4773 `gnus-sticky-article-mode'."
4774   (interactive)
4775   (unless buffer
4776     (setq buffer (current-buffer)))
4777   (with-current-buffer buffer
4778     (when (eq major-mode 'gnus-sticky-article-mode)
4779       (gnus-kill-buffer buffer))))
4780
4781 (defun gnus-kill-sticky-article-buffers (arg)
4782   "Kill all sticky article buffers.
4783 If a prefix ARG is given, ask for confirmation."
4784   (interactive "P")
4785   (dolist (buf (gnus-buffers))
4786     (with-current-buffer buf
4787       (when (eq major-mode 'gnus-sticky-article-mode)
4788         (if (not arg)
4789             (gnus-kill-buffer buf)
4790           (when (yes-or-no-p (concat "Kill buffer " (buffer-name buf) "? "))
4791             (gnus-kill-buffer buf)))))))
4792
4793 ;;;
4794 ;;; Gnus MIME viewing functions
4795 ;;;
4796
4797 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
4798   "Format of the MIME buttons.
4799
4800 Valid specifiers include:
4801 %t  The MIME type
4802 %T  MIME type, along with additional info
4803 %n  The `name' parameter
4804 %d  The description, if any
4805 %l  The length of the encoded part
4806 %p  The part identifier number
4807 %e  Dots if the part isn't displayed
4808
4809 General format specifiers can also be used.  See Info node
4810 `(gnus)Formatting Variables'.")
4811
4812 (defvar gnus-mime-button-line-format-alist
4813   '((?t gnus-tmp-type ?s)
4814     (?T gnus-tmp-type-long ?s)
4815     (?n gnus-tmp-name ?s)
4816     (?d gnus-tmp-description ?s)
4817     (?p gnus-tmp-id ?s)
4818     (?l gnus-tmp-length ?d)
4819     (?e gnus-tmp-dots ?s)))
4820
4821 (defvar gnus-mime-button-commands
4822   '((gnus-article-press-button "\r" "Toggle Display")
4823     (gnus-mime-view-part "v" "View Interactively...")
4824     (gnus-mime-view-part-as-type "t" "View As Type...")
4825     (gnus-mime-view-part-as-charset "C" "View As charset...")
4826     (gnus-mime-save-part "o" "Save...")
4827     (gnus-mime-save-part-and-strip "\C-o" "Save and Strip")
4828     (gnus-mime-replace-part "r" "Replace part")
4829     (gnus-mime-delete-part "d" "Delete part")
4830     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
4831     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
4832     (gnus-mime-view-part-internally "E" "View Internally") ;; Why `E'?
4833     (gnus-mime-view-part-externally "e" "View Externally")
4834     (gnus-mime-print-part "p" "Print")
4835     (gnus-mime-pipe-part "|" "Pipe To Command...")
4836     (gnus-mime-action-on-part "." "Take action on the part...")))
4837
4838 (defun gnus-article-mime-part-status ()
4839   (if gnus-article-mime-handle-alist-1
4840       (if (eq 1 (length gnus-article-mime-handle-alist-1))
4841           " (1 part)"
4842         (format " (%d parts)" (length gnus-article-mime-handle-alist-1)))
4843     ""))
4844
4845 (defvar gnus-mime-button-map
4846   (let ((map (make-sparse-keymap)))
4847     (define-key map gnus-mouse-2 'gnus-article-push-button)
4848     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
4849     (dolist (c gnus-mime-button-commands)
4850       (define-key map (cadr c) (car c)))
4851     map))
4852
4853 (easy-menu-define
4854   gnus-mime-button-menu gnus-mime-button-map "MIME button menu."
4855   `("MIME Part"
4856     ,@(mapcar (lambda (c)
4857                 (vector (caddr c) (car c) :active t))
4858               gnus-mime-button-commands)))
4859
4860 (defvar gnus-url-button-commands
4861   '((gnus-article-copy-string "u" "Copy URL to kill ring")))
4862
4863 (defvar gnus-url-button-map
4864   (let ((map (make-sparse-keymap)))
4865     (dolist (c gnus-url-button-commands)
4866       (define-key map (cadr c) (car c)))
4867     map))
4868
4869 (easy-menu-define
4870   gnus-url-button-menu gnus-url-button-map "URL button menu."
4871   `("Url Button"
4872     ,@(mapcar (lambda (c)
4873                 (vector (caddr c) (car c) :active t))
4874               gnus-url-button-commands)))
4875
4876 (defmacro gnus-bind-safe-url-regexp (&rest body)
4877   "Bind `mm-w3m-safe-url-regexp' according to `gnus-safe-html-newsgroups'."
4878   `(let ((mm-w3m-safe-url-regexp
4879           (let ((group (if (and (eq major-mode 'gnus-article-mode)
4880                                 (gnus-buffer-live-p
4881                                  gnus-article-current-summary))
4882                            (with-current-buffer gnus-article-current-summary
4883                              gnus-newsgroup-name)
4884                          gnus-newsgroup-name)))
4885             (if (cond ((not group)
4886                        ;; Maybe we're in a mml-preview buffer
4887                        ;; and no group is selected.
4888                        t)
4889                       ((stringp gnus-safe-html-newsgroups)
4890                        (string-match gnus-safe-html-newsgroups group))
4891                       ((consp gnus-safe-html-newsgroups)
4892                        (member group gnus-safe-html-newsgroups)))
4893                 nil
4894               mm-w3m-safe-url-regexp))))
4895      ,@body))
4896
4897 (defun gnus-mime-button-menu (event prefix)
4898  "Construct a context-sensitive menu of MIME commands."
4899  (interactive "e\nP")
4900  (save-window-excursion
4901    (let ((pos (event-start event)))
4902      (select-window (posn-window pos))
4903      (goto-char (posn-point pos))
4904      (gnus-article-check-buffer)
4905      (popup-menu gnus-mime-button-menu nil prefix))))
4906
4907 (defun gnus-mime-view-all-parts (&optional handles)
4908   "View all the MIME parts."
4909   (interactive)
4910   (with-current-buffer gnus-article-buffer
4911     (let ((handles (or handles gnus-article-mime-handles))
4912           (mail-parse-charset gnus-newsgroup-charset)
4913           (mail-parse-ignored-charsets
4914            (with-current-buffer gnus-summary-buffer
4915              gnus-newsgroup-ignored-charsets)))
4916       (when handles
4917         (mm-remove-parts handles)
4918         (goto-char (point-min))
4919         (or (search-forward "\n\n") (goto-char (point-max)))
4920         (let ((inhibit-read-only t))
4921           (delete-region (point) (point-max))
4922           (gnus-bind-safe-url-regexp (mm-display-parts handles)))))))
4923
4924 (defun gnus-article-jump-to-part (n)
4925   "Jump to MIME part N."
4926   (interactive "P")
4927   (pop-to-buffer gnus-article-buffer)
4928   ;; FIXME: why is it necessary?
4929   (sit-for 0)
4930   (let ((parts (length gnus-article-mime-handle-alist)))
4931     (or n (setq n (read-number (format "Jump to part (2..%s): " parts))))
4932     (unless (and (integerp n) (<= n parts) (>= n 1))
4933       (setq n
4934             (progn
4935               (gnus-message 7 "Invalid part `%s', using %s instead."
4936                             n parts)
4937               parts)))
4938     (gnus-message 9 "Jumping to part %s." n)
4939     (cond ((>= gnus-auto-select-part 1)
4940            (while (and (<= n parts)
4941                        (not (gnus-article-goto-part n)))
4942              (setq n (1+ n))))
4943           ((< gnus-auto-select-part 0)
4944            (while (and (>= n 1)
4945                        (not (gnus-article-goto-part n)))
4946              (setq n (1- n))))
4947           (t
4948            (gnus-article-goto-part n)))))
4949
4950 (defvar gnus-mime-buttonized-part-id nil
4951   "ID of a mime part that should be buttonized.
4952 `gnus-mime-save-part-and-strip' and `gnus-mime-delete-part' bind it.")
4953
4954 (eval-when-compile
4955   (defsubst gnus-article-edit-part (handles &optional current-id)
4956     "Edit an article in order to delete a mime part.
4957 This function is exclusively used by `gnus-mime-save-part-and-strip'
4958 and `gnus-mime-delete-part', and not provided at run-time normally."
4959     (gnus-article-edit-article
4960      `(lambda ()
4961         (buffer-disable-undo)
4962         (erase-buffer)
4963         (let ((mail-parse-charset (or gnus-article-charset
4964                                       ',gnus-newsgroup-charset))
4965               (mail-parse-ignored-charsets
4966                (or gnus-article-ignored-charsets
4967                    ',gnus-newsgroup-ignored-charsets))
4968               (mbl mml-buffer-list))
4969           (setq mml-buffer-list nil)
4970           (insert-buffer-substring gnus-original-article-buffer)
4971           (mime-to-mml ',handles)
4972           (setq gnus-article-mime-handles nil)
4973           (let ((mbl1 mml-buffer-list))
4974             (setq mml-buffer-list mbl)
4975             (set (make-local-variable 'mml-buffer-list) mbl1))
4976           (gnus-make-local-hook 'kill-buffer-hook)
4977           (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
4978      `(lambda (no-highlight)
4979         (let ((mail-parse-charset (or gnus-article-charset
4980                                       ',gnus-newsgroup-charset))
4981               (message-options message-options)
4982               (message-options-set-recipient)
4983               (mail-parse-ignored-charsets
4984                (or gnus-article-ignored-charsets
4985                    ',gnus-newsgroup-ignored-charsets)))
4986           (mml-to-mime)
4987           (mml-destroy-buffers)
4988           (remove-hook 'kill-buffer-hook
4989                        'mml-destroy-buffers t)
4990           (kill-local-variable 'mml-buffer-list))
4991         (gnus-summary-edit-article-done
4992          ,(or (mail-header-references gnus-current-headers) "")
4993          ,(gnus-group-read-only-p)
4994          ,gnus-summary-buffer no-highlight))
4995      t)
4996     ;; Force buttonizing this part.
4997     (let ((gnus-mime-buttonized-part-id current-id))
4998       (gnus-article-edit-done))
4999     (gnus-configure-windows 'article)
5000     (when (and current-id (integerp gnus-auto-select-part))
5001       (gnus-article-jump-to-part
5002        (min (max (+ current-id gnus-auto-select-part) 1)
5003             (with-current-buffer gnus-article-buffer
5004               (length gnus-article-mime-handle-alist)))))))
5005
5006 (defun gnus-mime-replace-part (file)
5007   "Replace MIME part under point with an external body."
5008   ;; Useful if file has already been saved to disk
5009   (interactive
5010    (list
5011     (read-file-name "Replace MIME part with file: "
5012                     (or mm-default-directory default-directory)
5013                     nil nil)))
5014   (gnus-mime-save-part-and-strip file))
5015
5016 (defun gnus-mime-save-part-and-strip (&optional file)
5017   "Save the MIME part under point then replace it with an external body.
5018 If FILE is given, use it for the external part."
5019   (interactive)
5020   (gnus-article-check-buffer)
5021   (when (gnus-group-read-only-p)
5022     (error "The current group does not support deleting of parts"))
5023   (when (mm-complicated-handles gnus-article-mime-handles)
5024     (error "\
5025 The current article has a complicated MIME structure, giving up..."))
5026   (let* ((data (get-text-property (point) 'gnus-data))
5027          (id (get-text-property (point) 'gnus-part))
5028          param
5029          (handles gnus-article-mime-handles))
5030     (unless file
5031       (setq file
5032             (and data (mm-save-part data "Delete MIME part and save to: "))))
5033     (when file
5034       (with-current-buffer (mm-handle-buffer data)
5035         (erase-buffer)
5036         (insert "Content-Type: " (mm-handle-media-type data))
5037         (mml-insert-parameter-string (cdr (mm-handle-type data))
5038                                      '(charset))
5039         ;; Add a filename for the sake of saving the part again.
5040         (mml-insert-parameter
5041          (mail-header-encode-parameter "name" (file-name-nondirectory file)))
5042         (insert "\n")
5043         (insert "Content-ID: " (message-make-message-id) "\n")
5044         (insert "Content-Transfer-Encoding: binary\n")
5045         (insert "\n"))
5046       (setcdr data
5047               (cdr (mm-make-handle nil
5048                                    `("message/external-body"
5049                                      (access-type . "LOCAL-FILE")
5050                                      (name . ,file)))))
5051       ;; (set-buffer gnus-summary-buffer)
5052       (gnus-article-edit-part handles id))))
5053
5054 ;; A function like `gnus-summary-save-parts' (`X m', `<MIME> <Extract all
5055 ;; parts...>') but with stripping would be nice.
5056
5057 (defun gnus-mime-delete-part ()
5058   "Delete the MIME part under point.
5059 Replace it with some information about the removed part."
5060   (interactive)
5061   (gnus-article-check-buffer)
5062   (when (gnus-group-read-only-p)
5063     (error "The current group does not support deleting of parts"))
5064   (when (mm-complicated-handles gnus-article-mime-handles)
5065     (error "\
5066 The current article has a complicated MIME structure, giving up..."))
5067   (when (or gnus-expert-user
5068             (gnus-yes-or-no-p "\
5069 Deleting parts may malfunction or destroy the article; continue? "))
5070     (let* ((data (get-text-property (point) 'gnus-data))
5071            (id (get-text-property (point) 'gnus-part))
5072            (handles gnus-article-mime-handles)
5073            (none "(none)")
5074            (description
5075             (let ((desc (mm-handle-description data)))
5076               (when desc
5077                 (mail-decode-encoded-word-string desc))))
5078            (filename
5079             (or (mail-content-type-get (mm-handle-disposition data) 'filename)
5080                 none))
5081            (type (mm-handle-media-type data)))
5082       (unless data
5083         (error "No MIME part under point"))
5084       (with-current-buffer (mm-handle-buffer data)
5085         (let ((bsize (format "%s" (buffer-size))))
5086           (erase-buffer)
5087           (insert
5088            (concat
5089             ",----\n"
5090             "| The following attachment has been deleted:\n"
5091             "|\n"
5092             "| Type:           " type "\n"
5093             "| Filename:       " filename "\n"
5094             "| Size (encoded): " bsize " Byte\n"
5095             (when description
5096               (concat    "| Description:    " description "\n"))
5097             "`----\n"))
5098           (setcdr data
5099                   (cdr (mm-make-handle
5100                         nil `("text/plain" (charset . gnus-decoded)) nil nil
5101                         (list "attachment")
5102                         (format "Deleted attachment (%s bytes)" bsize))))))
5103       ;; (set-buffer gnus-summary-buffer)
5104       (gnus-article-edit-part handles id))))
5105
5106 (defun gnus-mime-save-part ()
5107   "Save the MIME part under point."
5108   (interactive)
5109   (gnus-article-check-buffer)
5110   (let ((data (get-text-property (point) 'gnus-data)))
5111     (when data
5112       (mm-save-part data))))
5113
5114 (defun gnus-mime-pipe-part (&optional cmd)
5115   "Pipe the MIME part under point to a process.
5116 Use CMD as the process."
5117   (interactive)
5118   (gnus-article-check-buffer)
5119   (let ((data (get-text-property (point) 'gnus-data)))
5120     (when data
5121       (mm-pipe-part data cmd))))
5122
5123 (defun gnus-mime-view-part ()
5124   "Interactively choose a viewing method for the MIME part under point."
5125   (interactive)
5126   (gnus-article-check-buffer)
5127   (let ((data (get-text-property (point) 'gnus-data)))
5128     (when data
5129       (setq gnus-article-mime-handles
5130             (mm-merge-handles
5131              gnus-article-mime-handles (setq data (copy-sequence data))))
5132       (mm-interactively-view-part data))))
5133
5134 (defun gnus-mime-view-part-as-type-internal ()
5135   (gnus-article-check-buffer)
5136   (let* ((handle (get-text-property (point) 'gnus-data))
5137          (name (or
5138                 ;; Content-Type: foo/bar; name=...
5139                 (mail-content-type-get (mm-handle-type handle) 'name)
5140                 ;; Content-Disposition: attachment; filename=...
5141                 (cdr (assq 'filename (cdr (mm-handle-disposition handle))))))
5142          (def-type (and name (mm-default-file-encoding name))))
5143     (or (and def-type (cons def-type 0))
5144         (and handle
5145              (equal (mm-handle-media-supertype handle) "text")
5146              '("text/plain" . 0))
5147         '("application/octet-stream" . 0))))
5148
5149 (defun gnus-mime-view-part-as-type (&optional mime-type pred)
5150   "Choose a MIME media type, and view the part as such.
5151 If non-nil, PRED is a predicate to use during completion to limit the
5152 available media-types."
5153   (interactive)
5154   (unless mime-type
5155     (setq mime-type
5156           (let ((default (gnus-mime-view-part-as-type-internal)))
5157             (completing-read
5158              (format "View as MIME type (default %s): "
5159                      (car default))
5160              (mapcar #'list (mailcap-mime-types))
5161              pred nil nil nil
5162              (car default)))))
5163   (gnus-article-check-buffer)
5164   (let ((handle (get-text-property (point) 'gnus-data)))
5165     (when handle
5166       (when (equal (mm-handle-media-type handle) "message/external-body")
5167         (unless (mm-handle-cache handle)
5168           (mm-extern-cache-contents handle))
5169         (setq handle (mm-handle-cache handle)))
5170       (setq handle
5171             (mm-make-handle (mm-handle-buffer handle)
5172                             (cons mime-type (cdr (mm-handle-type handle)))
5173                             (mm-handle-encoding handle)
5174                             (mm-handle-undisplayer handle)
5175                             (mm-handle-disposition handle)
5176                             (mm-handle-description handle)
5177                             nil
5178                             (mm-handle-id handle)))
5179       (setq gnus-article-mime-handles
5180             (mm-merge-handles gnus-article-mime-handles handle))
5181       (when (mm-handle-displayed-p handle)
5182         (mm-remove-part handle))
5183       (gnus-mm-display-part handle))))
5184
5185 (defun gnus-mime-copy-part (&optional handle arg)
5186   "Put the MIME part under point into a new buffer.
5187 If `auto-compression-mode' is enabled, compressed files like .gz and .bz2
5188 are decompressed."
5189   (interactive (list nil current-prefix-arg))
5190   (gnus-article-check-buffer)
5191   (unless handle
5192     (setq handle (get-text-property (point) 'gnus-data)))
5193   (when handle
5194     (let ((filename (or (mail-content-type-get (mm-handle-type handle)
5195                                                'name)
5196                         (mail-content-type-get (mm-handle-disposition handle)
5197                                                'filename)))
5198           contents dont-decode charset coding-system)
5199       (mm-with-unibyte-buffer
5200         (mm-insert-part handle)
5201         (setq contents (or (condition-case nil
5202                                (mm-decompress-buffer filename nil 'sig)
5203                              (error
5204                               (setq dont-decode t)
5205                               nil))
5206                            (buffer-string))))
5207       (setq filename (cond (filename (file-name-nondirectory filename))
5208                            (dont-decode "*raw data*")
5209                            (t "*decoded*")))
5210       (cond
5211        (dont-decode)
5212        ((not arg)
5213         (unless (setq charset (mail-content-type-get
5214                                (mm-handle-type handle) 'charset))
5215           (unless (setq coding-system (mm-with-unibyte-buffer
5216                                         (insert contents)
5217                                         (mm-find-buffer-file-coding-system)))
5218             (setq charset gnus-newsgroup-charset))))
5219        ((numberp arg)
5220         (setq charset (or (cdr (assq arg
5221                                      gnus-summary-show-article-charset-alist))
5222                           (mm-read-coding-system "Charset: ")))))
5223       (switch-to-buffer (generate-new-buffer filename))
5224       (if (or coding-system
5225               (and charset
5226                    (setq coding-system (mm-charset-to-coding-system charset))
5227                    (not (eq charset 'ascii))))
5228           (progn
5229             (mm-enable-multibyte)
5230             (insert (mm-decode-coding-string contents coding-system))
5231             (setq buffer-file-coding-system
5232                   (if (boundp 'last-coding-system-used)
5233                       (symbol-value 'last-coding-system-used)
5234                     coding-system)))
5235         (mm-disable-multibyte)
5236         (insert contents)
5237         (setq buffer-file-coding-system mm-binary-coding-system))
5238       ;; We do it this way to make `normal-mode' set the appropriate mode.
5239       (unwind-protect
5240           (progn
5241             (setq buffer-file-name (expand-file-name filename))
5242             (normal-mode))
5243         (setq buffer-file-name nil))
5244       (goto-char (point-min)))))
5245
5246 (defun gnus-mime-print-part (&optional handle filename)
5247   "Print the MIME part under point."
5248   (interactive (list nil (ps-print-preprint current-prefix-arg)))
5249   (gnus-article-check-buffer)
5250   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5251          (contents (and handle (mm-get-part handle)))
5252          (file (mm-make-temp-file (expand-file-name "mm." mm-tmp-directory)))
5253          (printer (mailcap-mime-info (mm-handle-media-type handle) "print")))
5254     (when contents
5255         (if printer
5256             (unwind-protect
5257                 (progn
5258                   (mm-save-part-to-file handle file)
5259                   (call-process shell-file-name nil
5260                                 (generate-new-buffer " *mm*")
5261                                 nil
5262                                 shell-command-switch
5263                                 (mm-mailcap-command
5264                                  printer file (mm-handle-type handle))))
5265               (delete-file file))
5266           (with-temp-buffer
5267             (insert contents)
5268             (gnus-print-buffer))
5269           (ps-despool filename)))))
5270
5271 (defun gnus-mime-inline-part (&optional handle arg)
5272   "Insert the MIME part under point into the current buffer.
5273 Compressed files like .gz and .bz2 are decompressed."
5274   (interactive (list nil current-prefix-arg))
5275   (gnus-article-check-buffer)
5276   (unless handle
5277     (setq handle (get-text-property (point) 'gnus-data)))
5278   (when handle
5279     (let ((b (point))
5280           (inhibit-read-only t)
5281           contents charset coding-system)
5282       (if (and (not arg) (mm-handle-undisplayer handle))
5283           (mm-remove-part handle)
5284         (mm-with-unibyte-buffer
5285           (mm-insert-part handle)
5286           (setq contents
5287                 (or (mm-decompress-buffer
5288                      (or (mail-content-type-get (mm-handle-type handle)
5289                                                 'name)
5290                          (mail-content-type-get (mm-handle-disposition handle)
5291                                                 'filename))
5292                      nil t)
5293                     (buffer-string))))
5294         (cond
5295          ((not arg)
5296           (unless (setq charset (mail-content-type-get
5297                                  (mm-handle-type handle) 'charset))
5298             (unless (setq coding-system
5299                           (mm-with-unibyte-buffer
5300                             (insert contents)
5301                             (mm-find-buffer-file-coding-system)))
5302               (setq charset gnus-newsgroup-charset))))
5303          ((numberp arg)
5304           (if (mm-handle-undisplayer handle)
5305               (mm-remove-part handle))
5306           (setq charset
5307                 (or (cdr (assq arg
5308                                gnus-summary-show-article-charset-alist))
5309                     (mm-read-coding-system "Charset: "))))
5310          (t
5311           (if (mm-handle-undisplayer handle)
5312               (mm-remove-part handle))))
5313         (forward-line 2)
5314         (mm-insert-inline
5315          handle
5316          (if (or coding-system
5317                  (and charset
5318                       (setq coding-system
5319                             (mm-charset-to-coding-system charset))
5320                       (not (eq coding-system 'ascii))))
5321              (mm-decode-coding-string contents coding-system)
5322            (mm-string-to-multibyte contents)))
5323         (goto-char b)))))
5324
5325 (defun gnus-mime-set-charset-parameters (handle charset)
5326   "Set CHARSET to parameters in HANDLE.
5327 CHARSET may either be a string or a symbol."
5328   (unless (stringp charset)
5329     (setq charset (symbol-name charset)))
5330   (if (stringp (car handle))
5331       (dolist (h (cdr handle))
5332         (gnus-mime-set-charset-parameters h charset))
5333     (let* ((type (mm-handle-type (if (equal (mm-handle-media-type handle)
5334                                             "message/external-body")
5335                                      (progn
5336                                        (unless (mm-handle-cache handle)
5337                                          (mm-extern-cache-contents handle))
5338                                        (mm-handle-cache handle))
5339                                    handle)))
5340            (param (assq 'charset (cdr type))))
5341       (if param
5342           (setcdr param charset)
5343         (setcdr type (cons (cons 'charset charset) (cdr type)))))))
5344
5345 (defun gnus-mime-view-part-as-charset (&optional handle arg)
5346   "Insert the MIME part under point into the current buffer using the
5347 specified charset."
5348   (interactive (list nil current-prefix-arg))
5349   (gnus-article-check-buffer)
5350   (let ((handle (or handle (get-text-property (point) 'gnus-data)))
5351         (fun (get-text-property (point) 'gnus-callback))
5352         (gnus-newsgroup-ignored-charsets 'gnus-all)
5353         charset form preferred parts)
5354     (when handle
5355       (when (prog1
5356                 (and fun
5357                      (setq charset
5358                            (or (cdr (assq
5359                                      arg
5360                                      gnus-summary-show-article-charset-alist))
5361                                (mm-read-coding-system "Charset: "))))
5362               (if (mm-handle-undisplayer handle)
5363                   (mm-remove-part handle)))
5364         (gnus-mime-set-charset-parameters handle charset)
5365         (when (and (consp (setq form (cdr-safe fun)))
5366                    (setq form (ignore-errors
5367                                 (assq 'gnus-mime-display-alternative form)))
5368                    (setq preferred (caddr form))
5369                    (progn
5370                      (when (eq (car preferred) 'quote)
5371                        (setq preferred (cadr preferred)))
5372                      (not (equal preferred
5373                                  (get-text-property (point) 'gnus-data))))
5374                    (setq parts (get-text-property (point) 'gnus-part))
5375                    (setq parts (cdr (assq parts
5376                                           gnus-article-mime-handle-alist)))
5377                    (equal (mm-handle-media-type parts) "multipart/alternative")
5378                    (setq parts (reverse (cdr parts))))
5379           (setcar (cddr form)
5380                   (list 'quote (or (cadr (member preferred parts))
5381                                    (car parts)))))
5382         (funcall fun handle)))))
5383
5384 (defun gnus-mime-view-part-externally (&optional handle)
5385   "View the MIME part under point with an external viewer."
5386   (interactive)
5387   (gnus-article-check-buffer)
5388   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5389          (mm-user-display-methods nil)
5390          (mm-inlined-types nil)
5391          (mail-parse-charset gnus-newsgroup-charset)
5392          (mail-parse-ignored-charsets
5393           (with-current-buffer gnus-summary-buffer
5394             gnus-newsgroup-ignored-charsets))
5395          (type (mm-handle-media-type handle))
5396          (method (mailcap-mime-info type))
5397          (mm-enable-external t))
5398     (if (not (stringp method))
5399         (gnus-mime-view-part-as-type
5400          nil (lambda (types) (stringp (mailcap-mime-info (car types)))))
5401       (when handle
5402         (if (mm-handle-undisplayer handle)
5403             (mm-remove-part handle)
5404           (mm-display-part handle))))))
5405
5406 (defun gnus-mime-view-part-internally (&optional handle)
5407   "View the MIME part under point with an internal viewer.
5408 If no internal viewer is available, use an external viewer."
5409   (interactive)
5410   (gnus-article-check-buffer)
5411   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5412          (mm-inlined-types '(".*"))
5413          (mm-inline-large-images t)
5414          (mail-parse-charset gnus-newsgroup-charset)
5415          (mail-parse-ignored-charsets
5416           (with-current-buffer gnus-summary-buffer
5417             gnus-newsgroup-ignored-charsets))
5418          (inhibit-read-only t))
5419     (if (not (mm-inlinable-p handle))
5420         (gnus-mime-view-part-as-type
5421          nil (lambda (types) (mm-inlinable-p handle (car types))))
5422       (when handle
5423         (if (mm-handle-undisplayer handle)
5424             (mm-remove-part handle)
5425           (gnus-bind-safe-url-regexp (mm-display-part handle)))))))
5426
5427 (defun gnus-mime-action-on-part (&optional action)
5428   "Do something with the MIME attachment at \(point\)."
5429   (interactive
5430    (list (completing-read "Action: " gnus-mime-action-alist nil t)))
5431   (gnus-article-check-buffer)
5432   (let ((action-pair (assoc action gnus-mime-action-alist)))
5433     (if action-pair
5434         (funcall (cdr action-pair)))))
5435
5436 (defun gnus-article-part-wrapper (n function &optional no-handle interactive)
5437   "Call FUNCTION on MIME part N.
5438 Unless NO-HANDLE, call FUNCTION with N-th MIME handle as it's only argument.
5439 If INTERACTIVE, call FUNCTION interactivly."
5440   (let (window frame)
5441     ;; Check whether the article is displayed.
5442     (unless (and (gnus-buffer-live-p gnus-article-buffer)
5443                  (setq window (get-buffer-window gnus-article-buffer t))
5444                  (frame-visible-p (setq frame (window-frame window))))
5445       (error "No article is displayed"))
5446     (with-current-buffer gnus-article-buffer
5447       ;; Check whether the article displays the right contents.
5448       (unless (with-current-buffer gnus-summary-buffer
5449                 (eq gnus-current-article (gnus-summary-article-number)))
5450         (error "You should select the right article first"))
5451       (if n
5452           (setq n (prefix-numeric-value n))
5453         (let ((pt (point)))
5454           (setq n (or (get-text-property pt 'gnus-part)
5455                       (and (not (bobp))
5456                            (get-text-property (1- pt) 'gnus-part))
5457                       (get-text-property (prog2
5458                                              (forward-line 1)
5459                                              (point)
5460                                            (goto-char pt))
5461                                          'gnus-part)
5462                       (get-text-property
5463                        (or (and (setq pt (previous-single-property-change
5464                                           pt 'gnus-part))
5465                                 (1- pt))
5466                            (next-single-property-change (point) 'gnus-part)
5467                            (point))
5468                        'gnus-part)
5469                       1))))
5470       ;; Check whether the specified part exists.
5471       (when (> n (length gnus-article-mime-handle-alist))
5472         (error "No such part")))
5473     (unless
5474         (progn
5475           ;; To select the window is needed so that the cursor
5476           ;; might be visible on the MIME button.
5477           (select-window (prog1
5478                              window
5479                            (setq window (selected-window))
5480                            ;; Article may be displayed in the other frame.
5481                            (gnus-select-frame-set-input-focus
5482                             (prog1
5483                                 frame
5484                               (setq frame (selected-frame))))))
5485           (when (gnus-article-goto-part n)
5486             ;; We point the cursor and the arrow at the MIME button
5487             ;; when the `function' prompt the user for something.
5488             (let ((cursor-in-non-selected-windows t)
5489                   (overlay-arrow-string "=>")
5490                   (overlay-arrow-position (point-marker)))
5491               (unwind-protect
5492                   (cond
5493                    ((and no-handle interactive)
5494                     (call-interactively function))
5495                    (no-handle
5496                     (funcall function))
5497                    (interactive
5498                     (call-interactively
5499                      function
5500                      (cdr (assq n gnus-article-mime-handle-alist))))
5501                    (t
5502                     (funcall function
5503                              (cdr (assq n gnus-article-mime-handle-alist)))))
5504                 (set-marker overlay-arrow-position nil)
5505                 (unless gnus-auto-select-part
5506                   (gnus-select-frame-set-input-focus frame)
5507                   (select-window window))))
5508             t))
5509       (if gnus-inhibit-mime-unbuttonizing
5510           ;; This is the default though the program shouldn't reach here.
5511           (error "No such part")
5512         ;; The part which doesn't have the MIME button is selected.
5513         ;; So, we display all the buttons and redo it.
5514         (let ((gnus-inhibit-mime-unbuttonizing t))
5515           (gnus-summary-show-article)
5516           (gnus-article-part-wrapper n function no-handle))))))
5517
5518 (defun gnus-article-pipe-part (n)
5519   "Pipe MIME part N, which is the numerical prefix."
5520   (interactive "P")
5521   (gnus-article-part-wrapper n 'mm-pipe-part))
5522
5523 (defun gnus-article-save-part (n)
5524   "Save MIME part N, which is the numerical prefix."
5525   (interactive "P")
5526   (gnus-article-part-wrapper n 'mm-save-part))
5527
5528 (defun gnus-article-interactively-view-part (n)
5529   "View MIME part N interactively, which is the numerical prefix."
5530   (interactive "P")
5531   (gnus-article-part-wrapper n 'mm-interactively-view-part))
5532
5533 (defun gnus-article-copy-part (n)
5534   "Copy MIME part N, which is the numerical prefix."
5535   (interactive "P")
5536   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
5537
5538 (defun gnus-article-view-part-as-charset (n)
5539   "View MIME part N using a specified charset.
5540 N is the numerical prefix."
5541   (interactive "P")
5542   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
5543
5544 (defun gnus-article-view-part-externally (n)
5545   "View MIME part N externally, which is the numerical prefix."
5546   (interactive "P")
5547   (gnus-article-part-wrapper n 'gnus-mime-view-part-externally))
5548
5549 (defun gnus-article-inline-part (n)
5550   "Inline MIME part N, which is the numerical prefix."
5551   (interactive "P")
5552   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
5553
5554 (defun gnus-article-save-part-and-strip (n)
5555   "Save MIME part N and replace it with an external body.
5556 N is the numerical prefix."
5557   (interactive "P")
5558   (gnus-article-part-wrapper n 'gnus-mime-save-part-and-strip t))
5559
5560 (defun gnus-article-replace-part (n)
5561   "Replace MIME part N with an external body.
5562 N is the numerical prefix."
5563   (interactive "P")
5564   (gnus-article-part-wrapper n 'gnus-mime-replace-part t t))
5565
5566 (defun gnus-article-delete-part (n)
5567   "Delete MIME part N and add some information about the removed part.
5568 N is the numerical prefix."
5569   (interactive "P")
5570   (gnus-article-part-wrapper n 'gnus-mime-delete-part t))
5571
5572 (defun gnus-article-view-part-as-type (n)
5573   "Choose a MIME media type, and view part N as such.
5574 N is the numerical prefix."
5575   (interactive "P")
5576   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-type t))
5577
5578 (defun gnus-article-mime-match-handle-first (condition)
5579   (if condition
5580       (let (n)
5581         (dolist (ihandle gnus-article-mime-handle-alist)
5582           (if (and (cond
5583                     ((functionp condition)
5584                      (funcall condition (cdr ihandle)))
5585                     ((eq condition 'undisplayed)
5586                      (not (or (mm-handle-undisplayer (cdr ihandle))
5587                               (equal (mm-handle-media-type (cdr ihandle))
5588                                      "multipart/alternative"))))
5589                     ((eq condition 'undisplayed-alternative)
5590                      (not (mm-handle-undisplayer (cdr ihandle))))
5591                     (t t))
5592                    (gnus-article-goto-part (car ihandle))
5593                    (or (not n) (< (car ihandle) n)))
5594               (setq n (car ihandle))))
5595         (or n 1))
5596     1))
5597
5598 (defun gnus-article-view-part (&optional n)
5599   "View MIME part N, which is the numerical prefix.
5600 If the part is already shown, hide the part.  If N is nil, view
5601 all parts."
5602   (interactive "P")
5603   (with-current-buffer gnus-article-buffer
5604     (or (numberp n) (setq n (gnus-article-mime-match-handle-first
5605                              gnus-article-mime-match-handle-function)))
5606     (when (> n (length gnus-article-mime-handle-alist))
5607       (error "No such part"))
5608     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
5609       (when (gnus-article-goto-part n)
5610         (if (equal (car handle) "multipart/alternative")
5611             (gnus-article-press-button)
5612           (when (eq (gnus-mm-display-part handle) 'internal)
5613             (gnus-set-window-start)))))))
5614
5615 (defsubst gnus-article-mime-total-parts ()
5616   (if (bufferp (car gnus-article-mime-handles))
5617       1 ;; single part
5618     (1- (length gnus-article-mime-handles))))
5619
5620 (defun gnus-mm-display-part (handle)
5621   "Display HANDLE and fix MIME button."
5622   (let ((id (get-text-property (point) 'gnus-part))
5623         (point (point))
5624         (inhibit-read-only t))
5625     (forward-line 1)
5626     (prog1
5627         (let ((window (selected-window))
5628               (mail-parse-charset gnus-newsgroup-charset)
5629               (mail-parse-ignored-charsets
5630                (if (gnus-buffer-live-p gnus-summary-buffer)
5631                    (with-current-buffer gnus-summary-buffer
5632                      gnus-newsgroup-ignored-charsets)
5633                  nil)))
5634           (save-excursion
5635             (unwind-protect
5636                 (let ((win (gnus-get-buffer-window (current-buffer) t))
5637                       (beg (point)))
5638                   (when win
5639                     (select-window win))
5640                   (goto-char point)
5641                   (forward-line)
5642                   (if (mm-handle-displayed-p handle)
5643                       ;; This will remove the part.
5644                       (mm-display-part handle)
5645                     (save-restriction
5646                       (narrow-to-region (point)
5647                                         (if (eobp) (point) (1+ (point))))
5648                       (gnus-bind-safe-url-regexp (mm-display-part handle))
5649                       ;; We narrow to the part itself and
5650                       ;; then call the treatment functions.
5651                       (goto-char (point-min))
5652                       (forward-line 1)
5653                       (narrow-to-region (point) (point-max))
5654                       (gnus-treat-article
5655                        nil id
5656                        (gnus-article-mime-total-parts)
5657                        (mm-handle-media-type handle)))))
5658               (if (window-live-p window)
5659                   (select-window window)))))
5660       (goto-char point)
5661       (gnus-delete-line)
5662       (gnus-insert-mime-button
5663        handle id (list (mm-handle-displayed-p handle)))
5664       (goto-char point))))
5665
5666 (defun gnus-article-goto-part (n)
5667   "Go to MIME part N."
5668   (gnus-goto-char (text-property-any (point-min) (point-max) 'gnus-part n)))
5669
5670 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
5671   (let ((gnus-tmp-name
5672          (or (mail-content-type-get (mm-handle-type handle) 'name)
5673              (mail-content-type-get (mm-handle-disposition handle) 'filename)
5674              (mail-content-type-get (mm-handle-type handle) 'url)
5675              ""))
5676         (gnus-tmp-type (mm-handle-media-type handle))
5677         (gnus-tmp-description (or (mm-handle-description handle) ""))
5678         (gnus-tmp-dots
5679          (if (if displayed (car displayed)
5680                (mm-handle-displayed-p handle))
5681              "" "..."))
5682         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
5683                            (buffer-size)))
5684         gnus-tmp-type-long b e)
5685     (when (string-match ".*/" gnus-tmp-name)
5686       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
5687     (setq gnus-tmp-type-long (concat gnus-tmp-type
5688                                      (and (not (equal gnus-tmp-name ""))
5689                                           (concat "; " gnus-tmp-name))))
5690     (unless (equal gnus-tmp-description "")
5691       (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
5692     (unless (bolp)
5693       (insert "\n"))
5694     (setq b (point))
5695     (gnus-eval-format
5696      gnus-mime-button-line-format gnus-mime-button-line-format-alist
5697      `(keymap ,gnus-mime-button-map
5698               gnus-callback gnus-mm-display-part
5699               gnus-part ,gnus-tmp-id
5700               article-type annotation
5701               gnus-data ,handle))
5702     (setq e (if (bolp)
5703                 ;; Exclude a newline.
5704                 (1- (point))
5705               (point)))
5706     (when gnus-article-button-face
5707       (gnus-overlay-put (gnus-make-overlay b e nil t)
5708                         'face gnus-article-button-face))
5709     (widget-convert-button
5710      'link b e
5711      :mime-handle handle
5712      :action 'gnus-widget-press-button
5713      :button-keymap gnus-mime-button-map
5714      :help-echo
5715      (lambda (widget)
5716        ;; Needed to properly clear the message due to a bug in
5717        ;; wid-edit (XEmacs only).
5718        (if (boundp 'help-echo-owns-message)
5719            (setq help-echo-owns-message t))
5720        (format
5721         "%S: %s the MIME part; %S: more options"
5722         (aref gnus-mouse-2 0)
5723         (if (mm-handle-displayed-p (widget-get widget :mime-handle))
5724             "hide" "show")
5725         (aref gnus-down-mouse-3 0))))))
5726
5727 (defun gnus-widget-press-button (elems el)
5728   (goto-char (widget-get elems :from))
5729   (gnus-article-press-button))
5730
5731 (defvar gnus-displaying-mime nil)
5732
5733 (defun gnus-display-mime (&optional ihandles)
5734   "Display the MIME parts."
5735   (save-excursion
5736     (save-selected-window
5737       (let ((window (get-buffer-window gnus-article-buffer))
5738             (point (point)))
5739         (when window
5740           (select-window window)
5741           ;; We have to do this since selecting the window
5742           ;; may change the point.  So we set the window point.
5743           (set-window-point window point)))
5744       (let ((handles ihandles)
5745             (inhibit-read-only t)
5746             handle)
5747         (cond (handles)
5748               ((setq handles (mm-dissect-buffer nil gnus-article-loose-mime))
5749                (when gnus-article-emulate-mime
5750                  (mm-uu-dissect-text-parts handles)))
5751               (gnus-article-emulate-mime
5752                (setq handles (mm-uu-dissect))))
5753         (when (and (not ihandles)
5754                    (not gnus-displaying-mime))
5755           ;; Top-level call; we clean up.
5756           (when gnus-article-mime-handles
5757             (mm-destroy-parts gnus-article-mime-handles)
5758             (setq gnus-article-mime-handle-alist nil));; A trick.
5759           (setq gnus-article-mime-handles handles)
5760           ;; We allow users to glean info from the handles.
5761           (when gnus-article-mime-part-function
5762             (gnus-mime-part-function handles)))
5763         (if (and handles
5764                  (or (not (stringp (car handles)))
5765                      (cdr handles)))
5766             (progn
5767               (when (and (not ihandles)
5768                          (not gnus-displaying-mime))
5769                 ;; Clean up for mime parts.
5770                 (article-goto-body)
5771                 (delete-region (point) (point-max)))
5772               (let ((gnus-displaying-mime t))
5773                 (gnus-mime-display-part handles)))
5774           (save-restriction
5775             (article-goto-body)
5776             (narrow-to-region (point) (point-max))
5777             (gnus-treat-article nil 1 1)
5778             (widen)))
5779         (unless ihandles
5780           ;; Highlight the headers.
5781           (save-excursion
5782             (save-restriction
5783               (article-goto-body)
5784               (narrow-to-region (point-min) (point))
5785               (gnus-article-save-original-date
5786                (gnus-treat-article 'head)))))))
5787     ;; Cope with broken MIME messages.
5788     (goto-char (point-max))
5789     (unless (bolp)
5790       (insert "\n"))))
5791
5792 (defcustom gnus-mime-display-multipart-as-mixed nil
5793   "Display \"multipart\" parts as  \"multipart/mixed\".
5794
5795 If t, it overrides nil values of
5796 `gnus-mime-display-multipart-alternative-as-mixed' and
5797 `gnus-mime-display-multipart-related-as-mixed'."
5798   :group 'gnus-article-mime
5799   :type 'boolean)
5800
5801 (defcustom gnus-mime-display-multipart-alternative-as-mixed nil
5802   "Display \"multipart/alternative\" parts as  \"multipart/mixed\"."
5803   :version "22.1"
5804   :group 'gnus-article-mime
5805   :type 'boolean)
5806
5807 (defcustom gnus-mime-display-multipart-related-as-mixed nil
5808   "Display \"multipart/related\" parts as  \"multipart/mixed\".
5809
5810 If displaying \"text/html\" is discouraged \(see
5811 `mm-discouraged-alternatives'\) images or other material inside a
5812 \"multipart/related\" part might be overlooked when this variable is nil."
5813   :version "22.1"
5814   :group 'gnus-article-mime
5815   :type 'boolean)
5816
5817 (defun gnus-mime-display-part (handle)
5818   (cond
5819    ;; Maybe a broken MIME message.
5820    ((null handle))
5821    ;; Single part.
5822    ((not (stringp (car handle)))
5823     (gnus-mime-display-single handle))
5824    ;; User-defined multipart
5825    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
5826     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
5827              handle))
5828    ;; multipart/alternative
5829    ((and (equal (car handle) "multipart/alternative")
5830          (not (or gnus-mime-display-multipart-as-mixed
5831                   gnus-mime-display-multipart-alternative-as-mixed)))
5832     (let ((id (1+ (length gnus-article-mime-handle-alist))))
5833       (push (cons id handle) gnus-article-mime-handle-alist)
5834       (gnus-mime-display-alternative (cdr handle) nil nil id)))
5835    ;; multipart/related
5836    ((and (equal (car handle) "multipart/related")
5837          (not (or gnus-mime-display-multipart-as-mixed
5838                   gnus-mime-display-multipart-related-as-mixed)))
5839     ;;;!!!We should find the start part, but we just default
5840     ;;;!!!to the first part.
5841     ;;(gnus-mime-display-part (cadr handle))
5842     ;;;!!! Most multipart/related is an HTML message plus images.
5843     ;;;!!! Unfortunately we are unable to let W3 display those
5844     ;;;!!! included images, so we just display it as a mixed multipart.
5845     ;;(gnus-mime-display-mixed (cdr handle))
5846     ;;;!!! No, w3 can display everything just fine.
5847     (gnus-mime-display-part (cadr handle)))
5848    ((equal (car handle) "multipart/signed")
5849     (gnus-add-wash-type 'signed)
5850     (gnus-mime-display-security handle))
5851    ((equal (car handle) "multipart/encrypted")
5852     (gnus-add-wash-type 'encrypted)
5853     (gnus-mime-display-security handle))
5854    ;; Other multiparts are handled like multipart/mixed.
5855    (t
5856     (gnus-mime-display-mixed (cdr handle)))))
5857
5858 (defun gnus-mime-part-function (handles)
5859   (if (stringp (car handles))
5860       (mapcar 'gnus-mime-part-function (cdr handles))
5861     (funcall gnus-article-mime-part-function handles)))
5862
5863 (defun gnus-mime-display-mixed (handles)
5864   (mapcar 'gnus-mime-display-part handles))
5865
5866 (defun gnus-mime-display-single (handle)
5867   (let ((type (mm-handle-media-type handle))
5868         (ignored gnus-ignored-mime-types)
5869         (not-attachment t)
5870         (move nil)
5871         display text)
5872     (catch 'ignored
5873       (progn
5874         (while ignored
5875           (when (string-match (pop ignored) type)
5876             (throw 'ignored nil)))
5877         (if (and (setq not-attachment
5878                        (and (not (mm-inline-override-p handle))
5879                             (or (not (mm-handle-disposition handle))
5880                                 (equal (car (mm-handle-disposition handle))
5881                                        "inline")
5882                                 (mm-attachment-override-p handle))))
5883                  (mm-automatic-display-p handle)
5884                  (or (and
5885                       (mm-inlinable-p handle)
5886                       (mm-inlined-p handle))
5887                      (mm-automatic-external-display-p type)))
5888             (setq display t)
5889           (when (equal (mm-handle-media-supertype handle) "text")
5890             (setq text t)))
5891         (let ((id (1+ (length gnus-article-mime-handle-alist)))
5892               beg)
5893           (push (cons id handle) gnus-article-mime-handle-alist)
5894           (when (and display
5895                      (equal (mm-handle-media-supertype handle) "message"))
5896             (insert-char
5897              ?\n
5898              (cond ((not (bolp)) 2)
5899                    ((or (bobp) (eq (char-before (1- (point))) ?\n)) 0)
5900                    (t 1))))
5901           (when (or (not display)
5902                     (not (gnus-unbuttonized-mime-type-p type))
5903                     (eq id gnus-mime-buttonized-part-id))
5904             (gnus-insert-mime-button
5905              handle id (list (or display (and not-attachment text))))
5906             (gnus-article-insert-newline)
5907             ;; Remember modify the number of forward lines.
5908             (setq move t))
5909           (setq beg (point))
5910           (cond
5911            (display
5912             (when move
5913               (forward-line -1)
5914               (setq beg (point)))
5915             (let ((mail-parse-charset gnus-newsgroup-charset)
5916                   (mail-parse-ignored-charsets
5917                    (save-excursion (condition-case ()
5918                                        (set-buffer gnus-summary-buffer)
5919                                      (error))
5920                                    gnus-newsgroup-ignored-charsets)))
5921               (gnus-bind-safe-url-regexp (mm-display-part handle t)))
5922             (goto-char (point-max)))
5923            ((and text not-attachment)
5924             (when move
5925               (forward-line -1)
5926               (setq beg (point)))
5927             (gnus-article-insert-newline)
5928             (mm-insert-inline
5929              handle
5930              (let ((charset (or (mail-content-type-get (mm-handle-type handle)
5931                                                        'charset)
5932                                 (and (equal type "text/calendar") 'utf-8))))
5933                (cond ((not charset)
5934                       (mm-string-as-multibyte (mm-get-part handle)))
5935                      ((eq charset 'gnus-decoded)
5936                       (with-current-buffer (mm-handle-buffer handle)
5937                         (buffer-string)))
5938                      (t
5939                       (mm-decode-string (mm-get-part handle) charset)))))
5940             (goto-char (point-max))))
5941           ;; Do highlighting.
5942           (save-excursion
5943             (save-restriction
5944               (narrow-to-region beg (point))
5945               (if (eq handle gnus-article-mime-handles)
5946                   ;; The format=flowed case.
5947                   (gnus-treat-article nil 1 1 (mm-handle-media-type handle))
5948                 ;; Don't count signature parts that are never displayed.
5949                 ;; The part number should be re-calculated supposing this
5950                 ;; might be a message/rfc822 part.
5951                 (let (handles)
5952                   (dolist (part gnus-article-mime-handles)
5953                     (unless (or (stringp part)
5954                                 (equal (car (mm-handle-type part))
5955                                        "application/pgp-signature"))
5956                       (push part handles)))
5957                   (gnus-treat-article
5958                    nil (length (memq handle handles)) (length handles)
5959                    (mm-handle-media-type handle)))))))))))
5960
5961 (defun gnus-unbuttonized-mime-type-p (type)
5962   "Say whether TYPE is to be unbuttonized."
5963   (unless gnus-inhibit-mime-unbuttonizing
5964     (when (catch 'found
5965             (let ((types gnus-unbuttonized-mime-types))
5966               (while types
5967                 (when (string-match (pop types) type)
5968                   (throw 'found t)))))
5969       (not (catch 'found
5970              (let ((types gnus-buttonized-mime-types))
5971                (while types
5972                  (when (string-match (pop types) type)
5973                    (throw 'found t)))))))))
5974
5975 (defun gnus-article-insert-newline ()
5976   "Insert a newline, but mark it as undeletable."
5977   (gnus-put-text-property
5978    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
5979
5980 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
5981   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
5982          (ihandles handles)
5983          (point (point))
5984          handle (inhibit-read-only t) from props begend not-pref)
5985     (save-window-excursion
5986       (save-restriction
5987         (when ibegend
5988           (narrow-to-region (car ibegend)
5989                             (or (cdr ibegend)
5990                                 (progn
5991                                   (goto-char (car ibegend))
5992                                   (forward-line 2)
5993                                   (point))))
5994           (delete-region (point-min) (point-max))
5995           (mm-remove-parts handles))
5996         (setq begend (list (point-marker)))
5997         ;; Do the toggle.
5998         (unless (setq not-pref (cadr (member preferred ihandles)))
5999           (setq not-pref (car ihandles)))
6000         (when (or ibegend
6001                   (not preferred)
6002                   (not (gnus-unbuttonized-mime-type-p
6003                         "multipart/alternative")))
6004           (gnus-add-text-properties
6005            (setq from (point))
6006            (progn
6007              (insert (format "%d.  " id))
6008              (point))
6009            `(gnus-callback
6010              (lambda (handles)
6011                (unless ,(not ibegend)
6012                  (setq gnus-article-mime-handle-alist
6013                        ',gnus-article-mime-handle-alist))
6014                (gnus-mime-display-alternative
6015                 ',ihandles ',not-pref ',begend ,id))
6016              keymap ,gnus-mime-button-map
6017              ,gnus-mouse-face-prop ,gnus-article-mouse-face
6018              face ,gnus-article-button-face
6019              gnus-part ,id
6020              article-type multipart))
6021           (widget-convert-button 'link from (point)
6022                                  :action 'gnus-widget-press-button
6023                                  :button-keymap gnus-widget-button-keymap)
6024           ;; Do the handles
6025           (while (setq handle (pop handles))
6026             (gnus-add-text-properties
6027              (setq from (point))
6028              (progn
6029                (insert (format "(%c) %-18s"
6030                                (if (equal handle preferred) ?* ? )
6031                                (mm-handle-media-type handle)))
6032                (point))
6033              `(gnus-callback
6034                (lambda (handles)
6035                  (unless ,(not ibegend)
6036                    (setq gnus-article-mime-handle-alist
6037                          ',gnus-article-mime-handle-alist))
6038                  (gnus-mime-display-alternative
6039                   ',ihandles ',handle ',begend ,id))
6040                keymap ,gnus-mime-button-map
6041                ,gnus-mouse-face-prop ,gnus-article-mouse-face
6042                face ,gnus-article-button-face
6043                gnus-part ,id
6044                gnus-data ,handle))
6045             (widget-convert-button 'link from (point)
6046                                    :action 'gnus-widget-press-button
6047                                    :button-keymap gnus-widget-button-keymap)
6048             (insert "  "))
6049           (insert "\n\n"))
6050         (when preferred
6051           (if (stringp (car preferred))
6052               (gnus-display-mime preferred)
6053             (let ((mail-parse-charset gnus-newsgroup-charset)
6054                   (mail-parse-ignored-charsets
6055                    (with-current-buffer gnus-summary-buffer
6056                      gnus-newsgroup-ignored-charsets)))
6057               (gnus-bind-safe-url-regexp (mm-display-part preferred))
6058               ;; Do highlighting.
6059               (save-excursion
6060                 (save-restriction
6061                   (narrow-to-region (car begend) (point-max))
6062                   (gnus-treat-article
6063                    nil (length gnus-article-mime-handle-alist)
6064                    (gnus-article-mime-total-parts)
6065                    (mm-handle-media-type handle))))))
6066           (goto-char (point-max))
6067           (setcdr begend (point-marker)))))
6068     (when ibegend
6069       (goto-char point))))
6070
6071 (defconst gnus-article-wash-status-strings
6072   (let ((alist '((cite "c" "Possible hidden citation text"
6073                        " " "All citation text visible")
6074                  (headers "h" "Hidden headers"
6075                           " " "All headers visible.")
6076                  (pgp "p" "Encrypted or signed message status hidden"
6077                       " " "No hidden encryption nor digital signature status")
6078                  (signature "s" "Signature has been hidden"
6079                             " " "Signature is visible")
6080                  (overstrike "o" "Overstrike (^H) characters applied"
6081                              " " "No overstrike characters applied")
6082                  (emphasis "e" "/*_Emphasis_*/ characters applied"
6083                            " " "No /*_emphasis_*/ characters applied")))
6084         result)
6085     (dolist (entry alist result)
6086       (let ((key (nth 0 entry))
6087             (on (copy-sequence (nth 1 entry)))
6088             (on-help (nth 2 entry))
6089             (off (copy-sequence (nth 3 entry)))
6090             (off-help (nth 4 entry)))
6091         (put-text-property 0 1 'help-echo on-help on)
6092         (put-text-property 0 1 'help-echo off-help off)
6093         (push (list key on off) result))))
6094   "Alist of strings describing wash status in the mode line.
6095 Each entry has the form (KEY ON OF), where the KEY is a symbol
6096 representing the particular washing function, ON is the string to use
6097 in the article mode line when the washing function is active, and OFF
6098 is the string to use when it is inactive.")
6099
6100 (defun gnus-article-wash-status-entry (key value)
6101   (let ((entry (assoc key gnus-article-wash-status-strings)))
6102     (if value (nth 1 entry) (nth 2 entry))))
6103
6104 (defun gnus-article-wash-status ()
6105   "Return a string which display status of article washing."
6106   (with-current-buffer gnus-article-buffer
6107     (let ((cite (memq 'cite gnus-article-wash-types))
6108           (headers (memq 'headers gnus-article-wash-types))
6109           (boring (memq 'boring-headers gnus-article-wash-types))
6110           (pgp (memq 'pgp gnus-article-wash-types))
6111           (pem (memq 'pem gnus-article-wash-types))
6112           (signed (memq 'signed gnus-article-wash-types))
6113           (encrypted (memq 'encrypted gnus-article-wash-types))
6114           (signature (memq 'signature gnus-article-wash-types))
6115           (overstrike (memq 'overstrike gnus-article-wash-types))
6116           (emphasis (memq 'emphasis gnus-article-wash-types)))
6117       (concat
6118        (gnus-article-wash-status-entry 'cite cite)
6119        (gnus-article-wash-status-entry 'headers (or headers boring))
6120        (gnus-article-wash-status-entry 'pgp (or pgp pem signed encrypted))
6121        (gnus-article-wash-status-entry 'signature signature)
6122        (gnus-article-wash-status-entry 'overstrike overstrike)
6123        (gnus-article-wash-status-entry 'emphasis emphasis)))))
6124
6125 (defun gnus-add-wash-type (type)
6126   "Add a washing of TYPE to the current status."
6127   (add-to-list 'gnus-article-wash-types type))
6128
6129 (defun gnus-delete-wash-type (type)
6130   "Add a washing of TYPE to the current status."
6131   (setq gnus-article-wash-types (delq type gnus-article-wash-types)))
6132
6133 (defun gnus-add-image (category image)
6134   "Add IMAGE of CATEGORY to the list of displayed images."
6135   (let ((entry (assq category gnus-article-image-alist)))
6136     (unless entry
6137       (setq entry (list category))
6138       (push entry gnus-article-image-alist))
6139     (nconc entry (list image))))
6140
6141 (defun gnus-delete-images (category)
6142   "Delete all images in CATEGORY."
6143   (let ((entry (assq category gnus-article-image-alist)))
6144     (dolist (image (cdr entry))
6145       (gnus-remove-image image category))
6146     (setq gnus-article-image-alist (delq entry gnus-article-image-alist))
6147     (gnus-delete-wash-type category)))
6148
6149 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
6150
6151 (defun gnus-article-maybe-hide-headers ()
6152   "Hide unwanted headers if `gnus-have-all-headers' is nil.
6153 Provided for backwards compatibility."
6154   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
6155                  (not (with-current-buffer gnus-summary-buffer
6156                         gnus-have-all-headers)))
6157              (not gnus-inhibit-hiding))
6158     (gnus-article-hide-headers)))
6159
6160 ;;; Article savers.
6161
6162 (defun gnus-output-to-file (file-name)
6163   "Append the current article to a file named FILE-NAME.
6164 If `gnus-article-save-coding-system' is non-nil, it is used to encode
6165 text and used as the value of the coding cookie which is added to the
6166 top of a file.  Otherwise, this function saves a raw article without
6167 the coding cookie."
6168   (let* ((artbuf (current-buffer))
6169          (file-name-coding-system nnmail-pathname-coding-system)
6170          (coding gnus-article-save-coding-system)
6171          (coding-system-for-read (if coding
6172                                      nil ;; Rely on the coding cookie.
6173                                    mm-text-coding-system))
6174          (coding-system-for-write (or coding
6175                                       mm-text-coding-system-for-write
6176                                       mm-text-coding-system))
6177          (exists (file-exists-p file-name)))
6178     (with-temp-buffer
6179       (when exists
6180         (insert-file-contents file-name)
6181         (goto-char (point-min))
6182         ;; Remove the existing coding cookie.
6183         (when (looking-at "X-Gnus-Coding-System: .+\n\n")
6184           (delete-region (match-beginning 0) (match-end 0))))
6185       (goto-char (point-max))
6186       (insert-buffer-substring artbuf)
6187       ;; Append newline at end of the buffer as separator, and then
6188       ;; save it to file.
6189       (goto-char (point-max))
6190       (insert "\n")
6191       (when coding
6192         ;; If the coding system is not suitable to encode the text,
6193         ;; ask a user for a proper one.
6194         (when (fboundp 'select-safe-coding-system)
6195           (setq coding (coding-system-base
6196                         (save-window-excursion
6197                           (select-safe-coding-system (point-min) (point-max)
6198                                                      coding))))
6199           (setq coding-system-for-write
6200                 (or (cdr (assq coding '((mule-utf-8 . utf-8))))
6201                     coding)))
6202         (goto-char (point-min))
6203         ;; Add the coding cookie.
6204         (insert (format "X-Gnus-Coding-System: -*- coding: %s; -*-\n\n"
6205                         coding-system-for-write)))
6206       (if exists
6207           (progn
6208             (write-region (point-min) (point-max) file-name nil 'no-message)
6209             (message "Appended to %s" file-name))
6210         (write-region (point-min) (point-max) file-name))))
6211   t)
6212
6213 (defun gnus-narrow-to-page (&optional arg)
6214   "Narrow the article buffer to a page.
6215 If given a numerical ARG, move forward ARG pages."
6216   (interactive "P")
6217   (setq arg (if arg (prefix-numeric-value arg) 0))
6218   (with-current-buffer gnus-article-buffer
6219     (widen)
6220     ;; Remove any old next/prev buttons.
6221     (when (gnus-visual-p 'page-marker)
6222       (let ((inhibit-read-only t))
6223         (gnus-remove-text-with-property 'gnus-prev)
6224         (gnus-remove-text-with-property 'gnus-next)))
6225     (let (st nd pt)
6226       (when (save-excursion
6227               (cond ((< arg 0)
6228                      (if (re-search-backward page-delimiter nil 'move (abs arg))
6229                          (prog1
6230                              (setq nd (match-beginning 0)
6231                                    pt nd)
6232                            (when (re-search-backward page-delimiter nil t)
6233                              (setq st (match-end 0))))
6234                        (when (re-search-forward page-delimiter nil t)
6235                          (setq nd (match-beginning 0)
6236                                pt (point-min)))))
6237                     ((> arg 0)
6238                      (if (re-search-forward page-delimiter nil 'move arg)
6239                          (prog1
6240                              (setq st (match-end 0)
6241                                    pt st)
6242                            (when (re-search-forward page-delimiter nil t)
6243                              (setq nd (match-beginning 0))))
6244                        (when (re-search-backward page-delimiter nil t)
6245                          (setq st (match-end 0)
6246                                pt (point-max)))))
6247                     (t
6248                      (when (re-search-backward page-delimiter nil t)
6249                        (goto-char (setq st (match-end 0))))
6250                      (when (re-search-forward page-delimiter nil t)
6251                        (setq nd (match-beginning 0)))
6252                      (or st nd))))
6253         (setq gnus-page-broken t)
6254         (when pt (goto-char pt))
6255         (narrow-to-region (or st (point-min)) (or nd (point-max)))
6256         (when (gnus-visual-p 'page-marker)
6257           (save-excursion
6258             (when nd
6259               (goto-char nd)
6260               (gnus-insert-next-page-button))
6261             (when st
6262               (goto-char st)
6263               (gnus-insert-prev-page-button))))))))
6264
6265 ;; Article mode commands
6266
6267 (defun gnus-article-goto-next-page ()
6268   "Show the next page of the article."
6269   (interactive)
6270   (when (gnus-article-next-page)
6271     (goto-char (point-min))
6272     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
6273
6274
6275 (defun gnus-article-goto-prev-page ()
6276   "Show the previous page of the article."
6277   (interactive)
6278   (if (save-restriction (widen) (bobp)) ;; Real beginning-of-buffer?
6279       (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
6280     (gnus-article-prev-page nil)))
6281
6282 ;; This is cleaner but currently breaks `gnus-pick-mode':
6283 ;;
6284 ;; (defun gnus-article-goto-next-page ()
6285 ;;   "Show the next page of the article."
6286 ;;   (interactive)
6287 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6288 ;;     (gnus-summary-next-page)))
6289 ;;
6290 ;; (defun gnus-article-goto-prev-page ()
6291 ;;   "Show the next page of the article."
6292 ;;   (interactive)
6293 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6294 ;;     (gnus-summary-prev-page)))
6295
6296 (defun gnus-article-next-page (&optional lines)
6297   "Show the next page of the current article.
6298 If end of article, return non-nil.  Otherwise return nil.
6299 Argument LINES specifies lines to be scrolled up."
6300   (interactive "p")
6301   (move-to-window-line (if (featurep 'xemacs) -1 (- -1 scroll-margin)))
6302   (if (and (not (and gnus-article-over-scroll
6303                      (> (count-lines (window-start) (point-max))
6304                         (if (featurep 'xemacs)
6305                             (or lines (1- (window-height)))
6306                           (+ (or lines (1- (window-height))) scroll-margin)))))
6307            (save-excursion
6308              (end-of-line)
6309              (and (pos-visible-in-window-p)     ;Not continuation line.
6310                   (>= (1+ (point)) (point-max))))) ;Allow for trailing newline.
6311       ;; Nothing in this page.
6312       (if (or (not gnus-page-broken)
6313               (save-excursion
6314                 (save-restriction
6315                   (widen)
6316                   (forward-line)
6317                   (eobp)))) ;Real end-of-buffer?
6318           (progn
6319             (when gnus-article-over-scroll
6320               (gnus-article-next-page-1 lines))
6321             t)                  ;Nothing more.
6322         (gnus-narrow-to-page 1)         ;Go to next page.
6323         nil)
6324     ;; More in this page.
6325     (gnus-article-next-page-1 lines)
6326     nil))
6327
6328 (defun gnus-article-beginning-of-window ()
6329   "Move point to the beginning of the window.
6330 In Emacs, the point is placed at the line number which `scroll-margin'
6331 specifies."
6332   (if (featurep 'xemacs)
6333       (move-to-window-line 0)
6334     ;; There is an obscure bug in Emacs that makes it impossible to
6335     ;; scroll past big pictures in the article buffer.  Try to fix
6336     ;; this by adding a sanity check by counting the lines visible.
6337     (when (> (count-lines (window-start) (window-end)) 30)
6338       (move-to-window-line
6339        (min (max 0 scroll-margin)
6340             (max 1 (- (window-height)
6341                       (if mode-line-format 1 0)
6342                       (if header-line-format 1 0)
6343                       2)))))))
6344
6345 (defun gnus-article-next-page-1 (lines)
6346   (condition-case ()
6347       (let ((scroll-in-place nil))
6348         (scroll-up lines))
6349     (end-of-buffer
6350      ;; Long lines may cause an end-of-buffer error.
6351      (goto-char (point-max))))
6352   (gnus-article-beginning-of-window))
6353
6354 (defun gnus-article-prev-page (&optional lines)
6355   "Show previous page of current article.
6356 Argument LINES specifies lines to be scrolled down."
6357   (interactive "p")
6358   (move-to-window-line 0)
6359   (if (and gnus-page-broken
6360            (bobp)
6361            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
6362       (progn
6363         (gnus-narrow-to-page -1)        ;Go to previous page.
6364         (goto-char (point-max))
6365         (recenter (if gnus-article-over-scroll
6366                       (if lines
6367                           (max (if (featurep 'xemacs)
6368                                    lines
6369                                  (+ lines scroll-margin))
6370                                3)
6371                         (- (window-height) 2))
6372                     -1)))
6373     (prog1
6374         (condition-case ()
6375             (let ((scroll-in-place nil))
6376               (scroll-down lines))
6377           (beginning-of-buffer
6378            (goto-char (point-min))))
6379       (gnus-article-beginning-of-window))))
6380
6381 (defun gnus-article-only-boring-p ()
6382   "Decide whether there is only boring text remaining in the article.
6383 Something \"interesting\" is a word of at least two letters that does
6384 not have a face in `gnus-article-boring-faces'."
6385   (when (and gnus-article-skip-boring
6386              (boundp 'gnus-article-boring-faces)
6387              (symbol-value 'gnus-article-boring-faces))
6388     (save-excursion
6389       (let ((inhibit-point-motion-hooks t))
6390         (catch 'only-boring
6391           (while (re-search-forward "\\b\\w\\w" nil t)
6392             (forward-char -1)
6393             (when (not (gnus-intersection
6394                         (gnus-faces-at (point))
6395                         (symbol-value 'gnus-article-boring-faces)))
6396               (throw 'only-boring nil)))
6397           (throw 'only-boring t))))))
6398
6399 (defun gnus-article-refer-article ()
6400   "Read article specified by message-id around point."
6401   (interactive)
6402   (save-excursion
6403     (re-search-backward "[ \t]\\|^" (point-at-bol) t)
6404     (re-search-forward "<?news:<?\\|<" (point-at-eol) t)
6405     (if (re-search-forward "[^@ ]+@[^ \t>]+" (point-at-eol) t)
6406         (let ((msg-id (concat "<" (match-string 0) ">")))
6407           (set-buffer gnus-summary-buffer)
6408           (gnus-summary-refer-article msg-id))
6409       (error "No references around point"))))
6410
6411 (defun gnus-article-show-summary ()
6412   "Reconfigure windows to show summary buffer."
6413   (interactive)
6414   (if (not (gnus-buffer-live-p gnus-summary-buffer))
6415       (error "There is no summary buffer for this article buffer")
6416     (gnus-article-set-globals)
6417     (gnus-configure-windows 'article)
6418     (gnus-summary-goto-subject gnus-current-article)
6419     (gnus-summary-position-point)))
6420
6421 (defun gnus-article-describe-briefly ()
6422   "Describe article mode commands briefly."
6423   (interactive)
6424   (gnus-message 6 "%s" (substitute-command-keys "\\<gnus-article-mode-map>\\[gnus-article-goto-next-page]:Next page      \\[gnus-article-goto-prev-page]:Prev page  \\[gnus-article-show-summary]:Show summary  \\[gnus-info-find-node]:Run Info  \\[gnus-article-describe-briefly]:This help")))
6425
6426 (defun gnus-article-check-buffer ()
6427   "Beep if not in an article buffer."
6428   (unless (equal major-mode 'gnus-article-mode)
6429     (error "Command invoked outside of a Gnus article buffer")))
6430
6431 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
6432   "Read a summary buffer key sequence and execute it from the article buffer."
6433   (interactive "P")
6434   (gnus-article-check-buffer)
6435   (let ((nosaves
6436          '("q" "Q"  "c" "r" "\C-c\C-f" "m"  "a" "f"
6437            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
6438            "=" "^" "\M-^" "|"))
6439         (nosave-but-article
6440          '("A " "A<" "A>" "AM" "AP" "AR" "AT" "A\C-?" "A\M-\r" "A\r" "Ab" "Ae"
6441            "An" "Ap" [?A (meta return)] [?A delete]))
6442         (nosave-in-article
6443          '("AS" "\C-d"))
6444         (up-to-top
6445          '("n" "Gn" "p" "Gp"))
6446         keys new-sum-point)
6447     (with-current-buffer gnus-article-current-summary
6448       (let (gnus-pick-mode)
6449         (setq unread-command-events (nconc unread-command-events
6450                                            (list (or key last-command-event)))
6451               keys (if (featurep 'xemacs)
6452                        (events-to-keys (read-key-sequence nil t))
6453                      (read-key-sequence nil t)))))
6454
6455     (message "")
6456
6457     (cond
6458      ((eq (aref keys (1- (length keys))) ?\C-h)
6459       (gnus-article-describe-bindings (substring keys 0 -1)))
6460      ((or (member keys nosaves)
6461           (member keys nosave-but-article)
6462           (member keys nosave-in-article))
6463       (let (func)
6464         (save-window-excursion
6465           (pop-to-buffer gnus-article-current-summary)
6466           ;; We disable the pick minor mode commands.
6467           (let (gnus-pick-mode)
6468             (setq func (key-binding keys t))))
6469         (if (or (not func)
6470                 (numberp func))
6471             (ding)
6472           (unless (member keys nosave-in-article)
6473             (set-buffer gnus-article-current-summary))
6474           (call-interactively func)
6475           (setq new-sum-point (point)))
6476         (when (member keys nosave-but-article)
6477           (pop-to-buffer gnus-article-buffer))))
6478      (t
6479       ;; These commands should restore window configuration.
6480       (let ((obuf (current-buffer))
6481             (owin (current-window-configuration))
6482             win func in-buffer selected new-sum-start new-sum-hscroll err)
6483         (cond (not-restore-window
6484                (pop-to-buffer gnus-article-current-summary)
6485                (setq win (selected-window)))
6486               ((setq win (get-buffer-window gnus-article-current-summary))
6487                (select-window win))
6488               (t
6489                (let ((summary-buffer gnus-article-current-summary))
6490                  (gnus-configure-windows 'article)
6491                  (unless (setq win (get-buffer-window summary-buffer 'visible))
6492                    (let ((gnus-buffer-configuration
6493                           '((article ((vertical 1.0
6494                                                 (summary 0.25 point)
6495                                                 (article 1.0)))))))
6496                      (gnus-configure-windows 'article))
6497                    (setq win (get-buffer-window summary-buffer 'visible)))
6498                  (gnus-select-frame-set-input-focus (window-frame win))
6499                  (select-window win))))
6500         (setq in-buffer (current-buffer))
6501         ;; We disable the pick minor mode commands.
6502         (if (and (setq func (let (gnus-pick-mode)
6503                               (key-binding keys t)))
6504                  (functionp func)
6505                  (condition-case code
6506                      (progn
6507                        (call-interactively func)
6508                        t)
6509                    (error
6510                     (setq err code)
6511                     nil)))
6512             (progn
6513               (when (eq win (selected-window))
6514                 (setq new-sum-point (point)
6515                       new-sum-start (window-start win)
6516                       new-sum-hscroll (window-hscroll win)))
6517               (when (or (eq in-buffer (current-buffer))
6518                         (when (eq obuf (current-buffer))
6519                           (set-buffer in-buffer)
6520                           t))
6521                 (setq selected (gnus-summary-select-article))
6522                 (set-buffer obuf)
6523                 (unless not-restore-window
6524                   (set-window-configuration owin))
6525                 (when (and (eq selected 'old)
6526                            new-sum-point)
6527                   (set-window-start (get-buffer-window (current-buffer))
6528                                     1)
6529                   (set-window-point (get-buffer-window (current-buffer))
6530                                     (if (article-goto-body)
6531                                         (1- (point))
6532                                       (point))))
6533                 (when (and (not not-restore-window)
6534                            new-sum-point
6535                            (window-live-p win)
6536                            (with-current-buffer (window-buffer win)
6537                              (eq major-mode 'gnus-summary-mode)))
6538                   (set-window-point win new-sum-point)
6539                   (set-window-start win new-sum-start)
6540                   (set-window-hscroll win new-sum-hscroll))))
6541           (set-window-configuration owin)
6542           (if err
6543               (signal (car err) (cdr err))
6544             (ding))))))))
6545
6546 (defun gnus-article-read-summary-send-keys ()
6547   (interactive)
6548   (let ((unread-command-events (list (gnus-character-to-event ?S))))
6549     (gnus-article-read-summary-keys)))
6550
6551 (defun gnus-article-describe-key (key)
6552   "Display documentation of the function invoked by KEY.
6553 KEY is a string or a vector."
6554   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6555                        (read-key-sequence "Describe key: "))))
6556   (gnus-article-check-buffer)
6557   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6558                                   gnus-article-read-summary-send-keys))
6559       (with-current-buffer gnus-article-current-summary
6560         (setq unread-command-events
6561               (if (featurep 'xemacs)
6562                   (append key nil)
6563                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6564                                         (list 'meta (- x 128))
6565                                       x))
6566                         key)))
6567         (let ((cursor-in-echo-area t)
6568               gnus-pick-mode)
6569           (describe-key (read-key-sequence nil t))))
6570     (describe-key key)))
6571
6572 (defun gnus-article-describe-key-briefly (key &optional insert)
6573   "Display documentation of the function invoked by KEY.
6574 KEY is a string or a vector."
6575   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6576                        (read-key-sequence "Describe key: "))
6577                      current-prefix-arg))
6578   (gnus-article-check-buffer)
6579   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6580                                   gnus-article-read-summary-send-keys))
6581       (with-current-buffer gnus-article-current-summary
6582         (setq unread-command-events
6583               (if (featurep 'xemacs)
6584                   (append key nil)
6585                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6586                                         (list 'meta (- x 128))
6587                                       x))
6588                         key)))
6589         (let ((cursor-in-echo-area t)
6590               gnus-pick-mode)
6591           (describe-key-briefly (read-key-sequence nil t) insert)))
6592     (describe-key-briefly key insert)))
6593
6594 ;;`gnus-agent-mode' in gnus-agent.el will define it.
6595 (defvar gnus-agent-summary-mode)
6596 (defvar gnus-draft-mode)
6597 ;; Calling help-buffer will autoload help-mode.
6598 (defvar help-xref-stack-item)
6599 ;; Emacs 22 doesn't load it in the batch mode.
6600 (eval-when-compile
6601   (autoload 'help-buffer "help-mode"))
6602
6603 (defun gnus-article-describe-bindings (&optional prefix)
6604   "Show a list of all defined keys, and their definitions.
6605 The optional argument PREFIX, if non-nil, should be a key sequence;
6606 then we display only bindings that start with that prefix."
6607   (interactive)
6608   (gnus-article-check-buffer)
6609   (let ((keymap (copy-keymap gnus-article-mode-map))
6610         (map (copy-keymap gnus-article-send-map))
6611         (sumkeys (where-is-internal 'gnus-article-read-summary-keys))
6612         parent agent draft)
6613     (define-key keymap "S" map)
6614     (define-key map [t] nil)
6615     (with-current-buffer gnus-article-current-summary
6616       (set-keymap-parent
6617        keymap
6618        (if (setq parent (keymap-parent gnus-article-mode-map))
6619            (prog1
6620                (setq parent (copy-keymap parent))
6621              (set-keymap-parent parent (current-local-map)))
6622          (current-local-map)))
6623       (set-keymap-parent map (key-binding "S"))
6624       (let (key def gnus-pick-mode)
6625         (while sumkeys
6626           (setq key (pop sumkeys))
6627           (cond ((and (vectorp key) (= (length key) 1)
6628                       (consp (setq def (aref key 0)))
6629                       (numberp (car def)) (numberp (cdr def)))
6630                  (when (< (max (car def) (cdr def)) 128)
6631                    (setq sumkeys
6632                          (append (mapcar
6633                                   #'vector
6634                                   (nreverse (gnus-uncompress-range def)))
6635                                  sumkeys))))
6636                 ((setq def (key-binding key))
6637                  (unless (eq def 'undefined)
6638                    (define-key keymap key def))))))
6639       (when (boundp 'gnus-agent-summary-mode)
6640         (setq agent gnus-agent-summary-mode))
6641       (when (boundp 'gnus-draft-mode)
6642         (setq draft gnus-draft-mode)))
6643     (with-temp-buffer
6644       (use-local-map keymap)
6645       (set (make-local-variable 'gnus-agent-summary-mode) agent)
6646       (set (make-local-variable 'gnus-draft-mode) draft)
6647       (describe-bindings prefix))
6648     (let ((item `((lambda (prefix)
6649                     (with-current-buffer ,(current-buffer)
6650                       (gnus-article-describe-bindings prefix)))
6651                   ,prefix)))
6652       (with-current-buffer (let (help-xref-following) (help-buffer))
6653         (setq help-xref-stack-item item)))))
6654
6655 (defun gnus-article-reply-with-original (&optional wide)
6656   "Start composing a reply mail to the current message.
6657 The text in the region will be yanked.  If the region isn't active,
6658 the entire article will be yanked."
6659   (interactive)
6660   (let ((article (cdr gnus-article-current))
6661         contents)
6662     (if (not (gnus-region-active-p))
6663         (with-current-buffer gnus-summary-buffer
6664           (gnus-summary-reply (list (list article)) wide))
6665       (setq contents (buffer-substring (point) (mark t)))
6666       ;; Deactivate active regions.
6667       (when (and (boundp 'transient-mark-mode)
6668                  transient-mark-mode)
6669         (setq mark-active nil))
6670       (with-current-buffer gnus-summary-buffer
6671         (gnus-summary-reply
6672          (list (list article contents)) wide)))))
6673
6674 (defun gnus-article-wide-reply-with-original ()
6675   "Start composing a wide reply mail to the current message.
6676 The text in the region will be yanked.  If the region isn't active,
6677 the entire article will be yanked."
6678   (interactive)
6679   (gnus-article-reply-with-original t))
6680
6681 (defun gnus-article-followup-with-original ()
6682   "Compose a followup to the current article.
6683 The text in the region will be yanked.  If the region isn't active,
6684 the entire article will be yanked."
6685   (interactive)
6686   (let ((article (cdr gnus-article-current))
6687         contents)
6688       (if (not (gnus-region-active-p))
6689           (with-current-buffer gnus-summary-buffer
6690             (gnus-summary-followup (list (list article))))
6691         (setq contents (buffer-substring (point) (mark t)))
6692         ;; Deactivate active regions.
6693         (when (and (boundp 'transient-mark-mode)
6694                    transient-mark-mode)
6695           (setq mark-active nil))
6696         (with-current-buffer gnus-summary-buffer
6697           (gnus-summary-followup
6698            (list (list article contents)))))))
6699
6700 (defun gnus-article-hide (&optional arg force)
6701   "Hide all the gruft in the current article.
6702 This means that signatures, cited text and (some) headers will be
6703 hidden.
6704 If given a prefix, show the hidden text instead."
6705   (interactive (append (gnus-article-hidden-arg) (list 'force)))
6706   (gnus-article-hide-headers arg)
6707   (gnus-article-hide-list-identifiers arg)
6708   (gnus-article-hide-citation-maybe arg force)
6709   (gnus-article-hide-signature arg))
6710
6711 (defun gnus-article-maybe-highlight ()
6712   "Do some article highlighting if article highlighting is requested."
6713   (when (gnus-visual-p 'article-highlight 'highlight)
6714     (gnus-article-highlight-some)))
6715
6716 (defun gnus-check-group-server ()
6717   ;; Make sure the connection to the server is alive.
6718   (unless (gnus-server-opened
6719            (gnus-find-method-for-group gnus-newsgroup-name))
6720     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
6721     (gnus-request-group gnus-newsgroup-name t)))
6722
6723 (eval-when-compile
6724   (autoload 'nneething-get-file-name "nneething"))
6725
6726 (defun gnus-request-article-this-buffer (article group)
6727   "Get an article and insert it into this buffer."
6728   (let (do-update-line sparse-header)
6729     (prog1
6730         (save-excursion
6731           (erase-buffer)
6732           (gnus-kill-all-overlays)
6733           (setq group (or group gnus-newsgroup-name))
6734
6735           ;; Using `gnus-request-article' directly will insert the article into
6736           ;; `nntp-server-buffer' - so we'll save some time by not having to
6737           ;; copy it from the server buffer into the article buffer.
6738
6739           ;; We only request an article by message-id when we do not have the
6740           ;; headers for it, so we'll have to get those.
6741           (when (stringp article)
6742             (gnus-read-header article))
6743
6744           ;; If the article number is negative, that means that this article
6745           ;; doesn't belong in this newsgroup (possibly), so we find its
6746           ;; message-id and request it by id instead of number.
6747           (when (and (numberp article)
6748                      gnus-summary-buffer
6749                      (get-buffer gnus-summary-buffer)
6750                      (gnus-buffer-exists-p gnus-summary-buffer))
6751             (with-current-buffer gnus-summary-buffer
6752               (let ((header (gnus-summary-article-header article)))
6753                 (when (< article 0)
6754                   (cond
6755                    ((memq article gnus-newsgroup-sparse)
6756                     ;; This is a sparse gap article.
6757                     (setq do-update-line article)
6758                     (setq article (mail-header-id header))
6759                     (setq sparse-header (gnus-read-header article))
6760                     (setq gnus-newsgroup-sparse
6761                           (delq article gnus-newsgroup-sparse)))
6762                    ((vectorp header)
6763                     ;; It's a real article.
6764                     (setq article (mail-header-id header)))
6765                    (t
6766                     ;; It is an extracted pseudo-article.
6767                     (setq article 'pseudo)
6768                     (gnus-request-pseudo-article header))))
6769
6770                 (let ((method (gnus-find-method-for-group
6771                                gnus-newsgroup-name)))
6772                   (when (and (eq (car method) 'nneething)
6773                              (vectorp header))
6774                     (let ((dir (nneething-get-file-name
6775                                 (mail-header-id header))))
6776                       (when (and (stringp dir)
6777                                  (file-directory-p dir))
6778                         (setq article 'nneething)
6779                         (gnus-group-enter-directory dir))))))))
6780
6781           (cond
6782            ;; Refuse to select canceled articles.
6783            ((and (numberp article)
6784                  gnus-summary-buffer
6785                  (get-buffer gnus-summary-buffer)
6786                  (gnus-buffer-exists-p gnus-summary-buffer)
6787                  (eq (cdr (with-current-buffer gnus-summary-buffer
6788                             (assq article gnus-newsgroup-reads)))
6789                      gnus-canceled-mark))
6790             nil)
6791            ;; We first check `gnus-original-article-buffer'.
6792            ((and (get-buffer gnus-original-article-buffer)
6793                  (numberp article)
6794                  (with-current-buffer gnus-original-article-buffer
6795                    (and (equal (car gnus-original-article) group)
6796                         (eq (cdr gnus-original-article) article))))
6797             ;; `insert-buffer-substring' would incorrectly use the
6798             ;; equivalent of string-make-multibyte which amount to decoding
6799             ;; with locale-coding-system, causing failure of
6800             ;; subsequent decoding.
6801             (insert (mm-string-to-multibyte
6802                      (with-current-buffer gnus-original-article-buffer
6803                        (buffer-substring (point-min) (point-max)))))
6804             'article)
6805            ;; Check the backlog.
6806            ((and gnus-keep-backlog
6807                  (gnus-backlog-request-article group article (current-buffer)))
6808             'article)
6809            ;; Check asynchronous pre-fetch.
6810            ((gnus-async-request-fetched-article group article (current-buffer))
6811             (gnus-async-prefetch-next group article gnus-summary-buffer)
6812             (when (and (numberp article) gnus-keep-backlog)
6813               (gnus-backlog-enter-article group article (current-buffer)))
6814             'article)
6815            ;; Check the cache.
6816            ((and gnus-use-cache
6817                  (numberp article)
6818                  (gnus-cache-request-article article group))
6819             'article)
6820            ;; Check the agent cache.
6821            ((gnus-agent-request-article article group)
6822             'article)
6823            ;; Get the article and put into the article buffer.
6824            ((or (stringp article)
6825                 (numberp article))
6826             (let ((gnus-override-method gnus-override-method)
6827                   (methods (and (stringp article)
6828                                 gnus-refer-article-method))
6829                   (backend (car (gnus-find-method-for-group
6830                                  gnus-newsgroup-name)))
6831                   result
6832                   (inhibit-read-only t))
6833               (if (or (not (listp methods))
6834                       (and (symbolp (car methods))
6835                            (assq (car methods) nnoo-definition-alist)))
6836                   (setq methods (list methods)))
6837               (when (and (null gnus-override-method)
6838                          methods)
6839                 (setq gnus-override-method (pop methods)))
6840               (while (not result)
6841                 (when (eq gnus-override-method 'current)
6842                   (setq gnus-override-method
6843                         (with-current-buffer gnus-summary-buffer
6844                           gnus-current-select-method)))
6845                 (erase-buffer)
6846                 (gnus-kill-all-overlays)
6847                 (let ((gnus-newsgroup-name group))
6848                   (gnus-check-group-server))
6849                 (cond
6850                  ((gnus-request-article article group (current-buffer))
6851                   (when (numberp article)
6852                     (gnus-async-prefetch-next group article
6853                                               gnus-summary-buffer)
6854                     (when gnus-keep-backlog
6855                       (gnus-backlog-enter-article
6856                        group article (current-buffer))))
6857                   (setq result 'article))
6858                  (methods
6859                   (setq gnus-override-method (pop methods)))
6860                  ((not (string-match "^400 "
6861                                      (nnheader-get-report backend)))
6862                   ;; If we get 400 server disconnect, reconnect and
6863                   ;; retry; otherwise, assume the article has expired.
6864                   (setq result 'done))))
6865               (and (eq result 'article) 'article)))
6866            ;; It was a pseudo.
6867            (t article)))
6868
6869       ;; Associate this article with the current summary buffer.
6870       (setq gnus-article-current-summary gnus-summary-buffer)
6871
6872       ;; Take the article from the original article buffer
6873       ;; and place it in the buffer it's supposed to be in.
6874       (when (and (get-buffer gnus-article-buffer)
6875                  (equal (buffer-name (current-buffer))
6876                         (buffer-name (get-buffer gnus-article-buffer))))
6877         (save-excursion
6878           (if (get-buffer gnus-original-article-buffer)
6879               (set-buffer gnus-original-article-buffer)
6880             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
6881             (buffer-disable-undo)
6882             (setq major-mode 'gnus-original-article-mode)
6883             (setq buffer-read-only t))
6884           (let ((inhibit-read-only t))
6885             (erase-buffer)
6886             (insert-buffer-substring gnus-article-buffer))
6887           (setq gnus-original-article (cons group article)))
6888
6889         ;; Decode charsets.
6890         (run-hooks 'gnus-article-decode-hook)
6891         ;; Mark article as decoded or not.
6892         (setq gnus-article-decoded-p gnus-article-decode-hook))
6893
6894       ;; Update sparse articles.
6895       (when (and do-update-line
6896                  (or (numberp article)
6897                      (stringp article)))
6898         (let ((buf (current-buffer)))
6899           (set-buffer gnus-summary-buffer)
6900           (gnus-summary-update-article do-update-line sparse-header)
6901           (gnus-summary-goto-subject do-update-line nil t)
6902           (set-window-point (gnus-get-buffer-window (current-buffer) t)
6903                             (point))
6904           (set-buffer buf))))))
6905
6906 ;;;
6907 ;;; Article editing
6908 ;;;
6909
6910 (defcustom gnus-article-edit-mode-hook nil
6911   "Hook run in article edit mode buffers."
6912   :group 'gnus-article-various
6913   :type 'hook)
6914
6915 (defvar gnus-article-edit-done-function nil)
6916
6917 (defvar gnus-article-edit-mode-map nil)
6918
6919 ;; Should we be using derived.el for this?
6920 (unless gnus-article-edit-mode-map
6921   (setq gnus-article-edit-mode-map (make-keymap))
6922   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
6923
6924   (gnus-define-keys gnus-article-edit-mode-map
6925     "\C-c?"    describe-mode
6926     "\C-c\C-c" gnus-article-edit-done
6927     "\C-c\C-k" gnus-article-edit-exit
6928     "\C-c\C-f\C-t" message-goto-to
6929     "\C-c\C-f\C-o" message-goto-from
6930     "\C-c\C-f\C-b" message-goto-bcc
6931     ;;"\C-c\C-f\C-w" message-goto-fcc
6932     "\C-c\C-f\C-c" message-goto-cc
6933     "\C-c\C-f\C-s" message-goto-subject
6934     "\C-c\C-f\C-r" message-goto-reply-to
6935     "\C-c\C-f\C-n" message-goto-newsgroups
6936     "\C-c\C-f\C-d" message-goto-distribution
6937     "\C-c\C-f\C-f" message-goto-followup-to
6938     "\C-c\C-f\C-m" message-goto-mail-followup-to
6939     "\C-c\C-f\C-k" message-goto-keywords
6940     "\C-c\C-f\C-u" message-goto-summary
6941     "\C-c\C-f\C-i" message-insert-or-toggle-importance
6942     "\C-c\C-f\C-a" message-generate-unsubscribed-mail-followup-to
6943     "\C-c\C-b" message-goto-body
6944     "\C-c\C-i" message-goto-signature
6945
6946     "\C-c\C-t" message-insert-to
6947     "\C-c\C-n" message-insert-newsgroups
6948     "\C-c\C-o" message-sort-headers
6949     "\C-c\C-e" message-elide-region
6950     "\C-c\C-v" message-delete-not-region
6951     "\C-c\C-z" message-kill-to-signature
6952     "\M-\r" message-newline-and-reformat
6953     "\C-c\C-a" mml-attach-file
6954     "\C-a" message-beginning-of-line
6955     "\t" message-tab
6956     "\M-;" comment-region)
6957
6958   (gnus-define-keys (gnus-article-edit-wash-map
6959                      "\C-c\C-w" gnus-article-edit-mode-map)
6960     "f" gnus-article-edit-full-stops))
6961
6962 (easy-menu-define
6963   gnus-article-edit-mode-field-menu gnus-article-edit-mode-map ""
6964   '("Field"
6965     ["Fetch To" message-insert-to t]
6966     ["Fetch Newsgroups" message-insert-newsgroups t]
6967     "----"
6968     ["To" message-goto-to t]
6969     ["From" message-goto-from t]
6970     ["Subject" message-goto-subject t]
6971     ["Cc" message-goto-cc t]
6972     ["Reply-To" message-goto-reply-to t]
6973     ["Summary" message-goto-summary t]
6974     ["Keywords" message-goto-keywords t]
6975     ["Newsgroups" message-goto-newsgroups t]
6976     ["Followup-To" message-goto-followup-to t]
6977     ["Mail-Followup-To" message-goto-mail-followup-to t]
6978     ["Distribution" message-goto-distribution t]
6979     ["Body" message-goto-body t]
6980     ["Signature" message-goto-signature t]))
6981
6982 (define-derived-mode gnus-article-edit-mode message-mode "Article Edit"
6983   "Major mode for editing articles.
6984 This is an extended text-mode.
6985
6986 \\{gnus-article-edit-mode-map}"
6987   (make-local-variable 'gnus-article-edit-done-function)
6988   (make-local-variable 'gnus-prev-winconf)
6989   (set (make-local-variable 'font-lock-defaults)
6990        '(message-font-lock-keywords t))
6991   (set (make-local-variable 'mail-header-separator) "")
6992   (set (make-local-variable 'gnus-article-edit-mode) t)
6993   (easy-menu-add message-mode-field-menu message-mode-map)
6994   (mml-mode)
6995   (setq buffer-read-only nil)
6996   (buffer-enable-undo)
6997   (widen))
6998
6999 (defun gnus-article-edit (&optional force)
7000   "Edit the current article.
7001 This will have permanent effect only in mail groups.
7002 If FORCE is non-nil, allow editing of articles even in read-only
7003 groups."
7004   (interactive "P")
7005   (when (and (not force)
7006              (gnus-group-read-only-p))
7007     (error "The current newsgroup does not support article editing"))
7008   (gnus-article-date-original)
7009   (gnus-article-edit-article
7010    'ignore
7011    `(lambda (no-highlight)
7012       'ignore
7013       (gnus-summary-edit-article-done
7014        ,(or (mail-header-references gnus-current-headers) "")
7015        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
7016
7017 (defun gnus-article-edit-article (start-func exit-func &optional quiet)
7018   "Start editing the contents of the current article buffer."
7019   (let ((winconf (current-window-configuration)))
7020     (set-buffer gnus-article-buffer)
7021     (let ((message-auto-save-directory
7022            ;; Don't associate the article buffer with a draft file.
7023            nil))
7024       (gnus-article-edit-mode))
7025     (funcall start-func)
7026     (set-buffer-modified-p nil)
7027     (gnus-configure-windows 'edit-article)
7028     (setq gnus-article-edit-done-function exit-func)
7029     (setq gnus-prev-winconf winconf)
7030     (unless quiet
7031       (gnus-message 6 "C-c C-c to end edits"))))
7032
7033 (defun gnus-article-edit-done (&optional arg)
7034   "Update the article edits and exit."
7035   (interactive "P")
7036   (let ((func gnus-article-edit-done-function)
7037         (buf (current-buffer))
7038         (start (window-start))
7039         (p (point))
7040         (winconf gnus-prev-winconf))
7041     (widen) ;; Widen it in case that users narrowed the buffer.
7042     (funcall func arg)
7043     (set-buffer buf)
7044     ;; The cache and backlog have to be flushed somewhat.
7045     (when gnus-keep-backlog
7046       (gnus-backlog-remove-article
7047        (car gnus-article-current) (cdr gnus-article-current)))
7048     ;; Flush original article as well.
7049     (gnus-flush-original-article-buffer)
7050     (when gnus-use-cache
7051       (gnus-cache-update-article
7052        (car gnus-article-current) (cdr gnus-article-current)))
7053     ;; We remove all text props from the article buffer.
7054     (kill-all-local-variables)
7055     (set-text-properties (point-min) (point-max) nil)
7056     (gnus-article-mode)
7057     (set-window-configuration winconf)
7058     (set-buffer buf)
7059     (set-window-start (get-buffer-window buf) start)
7060     (set-window-point (get-buffer-window buf) (point)))
7061   (gnus-summary-show-article))
7062
7063 (defun gnus-flush-original-article-buffer ()
7064   (when (get-buffer gnus-original-article-buffer)
7065     (with-current-buffer gnus-original-article-buffer
7066       (setq gnus-original-article nil))))
7067
7068 (defun gnus-article-edit-exit ()
7069   "Exit the article editing without updating."
7070   (interactive)
7071   (when (or (not (buffer-modified-p))
7072             (yes-or-no-p "Article modified; kill anyway? "))
7073     (let ((curbuf (current-buffer))
7074           (p (point))
7075           (window-start (window-start)))
7076       (erase-buffer)
7077       (if (gnus-buffer-live-p gnus-original-article-buffer)
7078           (insert-buffer-substring gnus-original-article-buffer))
7079       (let ((winconf gnus-prev-winconf))
7080         (kill-all-local-variables)
7081         (gnus-article-mode)
7082         (set-window-configuration winconf)
7083         ;; Tippy-toe some to make sure that point remains where it was.
7084         (save-current-buffer
7085           (set-buffer curbuf)
7086           (set-window-start (get-buffer-window (current-buffer)) window-start)
7087           (goto-char p))))
7088     (gnus-summary-show-article)))
7089
7090 (defun gnus-article-edit-full-stops ()
7091   "Interactively repair spacing at end of sentences."
7092   (interactive)
7093   (save-excursion
7094     (goto-char (point-min))
7095     (search-forward-regexp "^$" nil t)
7096     (let ((case-fold-search nil))
7097       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
7098
7099 ;;;
7100 ;;; Article highlights
7101 ;;;
7102
7103 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
7104
7105 ;;; Internal Variables:
7106
7107 (defcustom gnus-button-url-regexp
7108   (concat
7109    "\\b\\(\\(www\\.\\|\\(s?https?\\|ftp\\|file\\|gopher\\|"
7110    "nntp\\|news\\|telnet\\|wais\\|mailto\\|info\\):\\)"
7111    "\\(//[-a-z0-9_.]+:[0-9]*\\)?"
7112    (if (string-match "[[:digit:]]" "1") ;; Support POSIX?
7113        (let ((chars "-a-z0-9_=#$@~%&*+\\/[:word:]")
7114              (punct "!?:;.,"))
7115          (concat
7116           "\\(?:"
7117           ;; Match paired parentheses, e.g. in Wikipedia URLs:
7118           ;; http://thread.gmane.org/47B4E3B2.3050402@gmail.com
7119           "[" chars punct "]+" "(" "[" chars punct "]+" "[" chars "]*)" "[" chars "]*"
7120           "\\|"
7121           "[" chars punct     "]+" "[" chars "]"
7122           "\\)"))
7123      (concat ;; XEmacs 21.4 doesn't support POSIX.
7124       "\\([-a-z0-9_=!?#$@~%&*+\\/:;.,]\\|\\w\\)+"
7125       "\\([-a-z0-9_=#$@~%&*+\\/]\\|\\w\\)"))
7126    "\\)")
7127   "Regular expression that matches URLs."
7128   :group 'gnus-article-buttons
7129   :type 'regexp)
7130
7131 (defcustom gnus-button-valid-fqdn-regexp
7132   message-valid-fqdn-regexp
7133   "Regular expression that matches a valid FQDN."
7134   :version "22.1"
7135   :group 'gnus-article-buttons
7136   :type 'regexp)
7137
7138 ;; Regexp suggested by Felix Wiemann in <87oeuomcz9.fsf@news2.ososo.de>
7139 (defcustom gnus-button-valid-localpart-regexp
7140   "[a-z0-9$%(*-=?[_][^<>\")!;:,{}\n\t @]*"
7141   "Regular expression that matches a localpart of mail addresses or MIDs."
7142   :version "22.1"
7143   :group 'gnus-article-buttons
7144   :type 'regexp)
7145
7146 (defcustom gnus-button-man-handler 'manual-entry
7147   "Function to use for displaying man pages.
7148 The function must take at least one argument with a string naming the
7149 man page."
7150   :version "22.1"
7151   :type '(choice (function-item :tag "Man" manual-entry)
7152                  (function-item :tag "Woman" woman)
7153                  (function :tag "Other"))
7154   :group 'gnus-article-buttons)
7155
7156 (defcustom gnus-ctan-url "http://tug.ctan.org/tex-archive/"
7157   "Top directory of a CTAN \(Comprehensive TeX Archive Network\) archive.
7158 If the default site is too slow, try to find a CTAN mirror, see
7159 <URL:http://tug.ctan.org/tex-archive/CTAN.sites?action=/index.html>.  See also
7160 the variable `gnus-button-handle-ctan'."
7161   :version "22.1"
7162   :group 'gnus-article-buttons
7163   :link '(custom-manual "(gnus)Group Parameters")
7164   :type '(choice (const "http://www.tex.ac.uk/tex-archive/")
7165                  (const "http://tug.ctan.org/tex-archive/")
7166                  (const "http://www.dante.de/CTAN/")
7167                  (string :tag "Other")))
7168
7169 (defcustom gnus-button-ctan-handler 'browse-url
7170   "Function to use for displaying CTAN links.
7171 The function must take one argument, the string naming the URL."
7172   :version "22.1"
7173   :type '(choice (function-item :tag "Browse Url" browse-url)
7174                  (function :tag "Other"))
7175   :group 'gnus-article-buttons)
7176
7177 (defcustom gnus-button-handle-ctan-bogus-regexp "^/?tex-archive/\\|^/"
7178   "Bogus strings removed from CTAN URLs."
7179   :version "22.1"
7180   :group 'gnus-article-buttons
7181   :type '(choice (const "^/?tex-archive/\\|/")
7182                  (regexp :tag "Other")))
7183
7184 (defcustom gnus-button-ctan-directory-regexp
7185   (regexp-opt
7186    (list "archive-tools" "biblio" "bibliography" "digests" "documentation"
7187          "dviware" "fonts" "graphics" "help" "indexing" "info" "language"
7188          "languages" "macros" "nonfree" "obsolete" "support" "systems"
7189          "tds" "tools" "usergrps" "web") t)
7190   "Regular expression for ctan directories.
7191 It should match all directories in the top level of `gnus-ctan-url'."
7192   :version "22.1"
7193   :group 'gnus-article-buttons
7194   :type 'regexp)
7195
7196 (defcustom gnus-button-mid-or-mail-regexp
7197   (concat "\\b\\(<?" gnus-button-valid-localpart-regexp "@"
7198           gnus-button-valid-fqdn-regexp
7199           ">?\\)\\b")
7200   "Regular expression that matches a message ID or a mail address."
7201   :version "22.1"
7202   :group 'gnus-article-buttons
7203   :type 'regexp)
7204
7205 (defcustom gnus-button-prefer-mid-or-mail 'gnus-button-mid-or-mail-heuristic
7206   "What to do when the button on a string as \"foo123@bar.invalid\" is pushed.
7207 Strings like this can be either a message ID or a mail address.  If it is one
7208 of the symbols `mid' or `mail', Gnus will always assume that the string is a
7209 message ID or a mail address, respectively.  If this variable is set to the
7210 symbol `ask', always query the user what do do.  If it is a function, this
7211 function will be called with the string as its only argument.  The function
7212 must return `mid', `mail', `invalid' or `ask'."
7213   :version "22.1"
7214   :group 'gnus-article-buttons
7215   :type '(choice (function-item :tag "Heuristic function"
7216                                 gnus-button-mid-or-mail-heuristic)
7217                  (const ask)
7218                  (const mid)
7219                  (const mail)))
7220
7221 (defcustom gnus-button-mid-or-mail-heuristic-alist
7222   '((-10.0 . ".+\\$.+@")
7223     (-10.0 . "#")
7224     (-10.0 . "\\*")
7225     (-5.0  . "\\+[^+]*\\+.*@") ;; # two plus signs
7226     (-5.0  . "@[Nn][Ee][Ww][Ss]") ;; /\@news/i
7227     (-5.0  . "@.*[Dd][Ii][Aa][Ll][Uu][Pp]") ;; /\@.*dialup/i;
7228     (-1.0  . "^[^a-z]+@")
7229     ;;
7230     (-5.0  . "\\.[0-9][0-9]+.*@") ;; "\.[0-9]{2,}.*\@"
7231     (-5.0  . "[a-z].*[A-Z].*[a-z].*[A-Z].*@") ;; "([a-z].*[A-Z].*){2,}\@"
7232     (-3.0  . "[A-Z][A-Z][a-z][a-z].*@")
7233     (-5.0  . "\\...?.?@") ;; (-5.0 . "\..{1,3}\@")
7234     ;;
7235     (-2.0  . "^[0-9]")
7236     (-1.0  . "^[0-9][0-9]")
7237     ;;
7238     ;; -3.0 /^[0-9][0-9a-fA-F]{2,2}/;
7239     (-3.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7240     ;; -5.0 /^[0-9][0-9a-fA-F]{3,3}/;
7241     (-5.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7242     ;;
7243     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][^0-9].*@") ;; "[0-9]{5,}.*\@"
7244     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][^0-9].*@")
7245     ;;       "[0-9]{8,}.*\@"
7246     (-3.0
7247      . "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].*@")
7248     ;; "[0-9]{12,}.*\@"
7249     ;; compensation for TDMA dated mail addresses:
7250     (25.0  . "-dated-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]+.*@")
7251     ;;
7252     (-20.0 . "\\.fsf@") ;; Gnus
7253     (-20.0 . "^slrn")
7254     (-20.0 . "^Pine")
7255     (-20.0 . "^alpine\\.")
7256     (-20.0 . "_-_") ;; Subject change in thread
7257     ;;
7258     (-20.0 . "\\.ln@") ;; leafnode
7259     (-30.0 . "@ID-[0-9]+\\.[a-zA-Z]+\\.dfncis\\.de")
7260     (-30.0 . "@4[Aa][Xx]\\.com") ;; Forte Agent
7261     ;;
7262     ;; (5.0 . "") ;; $local_part_len <= 7
7263     (10.0  . "^[^0-9]+@")
7264     (3.0   . "^[^0-9]+[0-9][0-9]?[0-9]?@")
7265     ;;      ^[^0-9]+[0-9]{1,3}\@ digits only at end of local part
7266     (3.0   . "\@stud")
7267     ;;
7268     (2.0   . "[a-z][a-z][._-][A-Z][a-z].*@")
7269     ;;
7270     (0.5   . "^[A-Z][a-z]")
7271     (0.5   . "^[A-Z][a-z][a-z]")
7272     (1.5   . "^[A-Z][a-z][A-Z][a-z][^a-z]") ;; ^[A-Z][a-z]{3,3}
7273     (2.0   . "^[A-Z][a-z][A-Z][a-z][a-z][^a-z]")) ;; ^[A-Z][a-z]{4,4}
7274   "An alist of \(RATE . REGEXP\) pairs for `gnus-button-mid-or-mail-heuristic'.
7275
7276 A negative RATE indicates a message IDs, whereas a positive indicates a mail
7277 address.  The REGEXP is processed with `case-fold-search' set to nil."
7278   :version "22.1"
7279   :group 'gnus-article-buttons
7280   :type '(repeat (cons (number :tag "Rate")
7281                        (regexp :tag "Regexp"))))
7282
7283 (defun gnus-button-mid-or-mail-heuristic (mid-or-mail)
7284   "Guess whether MID-OR-MAIL is a message ID or a mail address.
7285 Returns `mid' if MID-OR-MAIL is a message IDs, `mail' if it's a mail
7286 address, `ask' if unsure and `invalid' if the string is invalid."
7287   (let ((case-fold-search nil)
7288         (list gnus-button-mid-or-mail-heuristic-alist)
7289         (result 0) rate regexp lpartlen elem)
7290     (setq lpartlen
7291           (length (gnus-replace-in-string mid-or-mail "^\\(.*\\)@.*$" "\\1")))
7292     (gnus-message 8 "`%s', length of local part=`%s'." mid-or-mail lpartlen)
7293     ;; Certain special cases...
7294     (when (string-match
7295            (concat
7296             "^0[0-9]+-[0-9][0-9][0-9][0-9]@t-online\\.de$\\|"
7297             "^[0-9]+\\.[0-9]+@compuserve\\|"
7298             "@public\\.gmane\\.org")
7299            mid-or-mail)
7300       (gnus-message 8 "`%s' is a known mail address." mid-or-mail)
7301       (setq result 'mail))
7302     (when (string-match "@.*@\\| " mid-or-mail)
7303       (gnus-message 8 "`%s' is invalid." mid-or-mail)
7304       (setq result 'invalid))
7305     ;; Nothing more to do, if result is not a number here...
7306     (when (numberp result)
7307       (while list
7308         (setq elem (car list)
7309               rate (car elem)
7310               regexp (cdr elem)
7311               list (cdr list))
7312         (when (string-match regexp mid-or-mail)
7313           (setq result (+ result rate))
7314           (gnus-message
7315            9 "`%s' matched `%s', rate `%s', result `%s'."
7316            mid-or-mail regexp rate result)))
7317       (when (<= lpartlen 7)
7318         (setq result (+ result 5.0))
7319         (gnus-message 9 "`%s' matched (<= lpartlen 7), result `%s'."
7320                       mid-or-mail result))
7321       (when (>= lpartlen 12)
7322         (gnus-message 9 "`%s' matched (>= lpartlen 12)" mid-or-mail)
7323         (cond
7324          ((string-match "[0-9][^0-9]+[0-9].*@" mid-or-mail)
7325           ;; Long local part should contain realname if e-mail address,
7326           ;; too many digits: message-id.
7327           ;; $score -= 5.0 + 0.1 * $local_part_len;
7328           (setq rate (* -1.0 (+ 5.0 (* 0.1 lpartlen))))
7329           (setq result (+ result rate))
7330           (gnus-message
7331            9 "Many digits in `%s', rate `%s', result `%s'."
7332            mid-or-mail rate result))
7333          ((string-match "[^aeiouy][^aeiouy][^aeiouy][^aeiouy]+.*\@"
7334                         mid-or-mail)
7335           ;; Too few vowels [^aeiouy]{4,}.*\@
7336           (setq result (+ result -5.0))
7337           (gnus-message
7338            9 "Few vowels in `%s', rate `%s', result `%s'."
7339            mid-or-mail -5.0 result))
7340          (t
7341           (setq result (+ result 5.0))
7342           (gnus-message
7343            9 "`%s', rate `%s', result `%s'." mid-or-mail 5.0 result)))))
7344     (gnus-message 8 "`%s': Final rate is `%s'." mid-or-mail result)
7345     ;; Maybe we should make this a customizable alist: (condition . 'result)
7346     (cond
7347      ((symbolp result) result)
7348      ;; Now convert number into proper results:
7349      ((< result -10.0) 'mid)
7350      ((> result  10.0) 'mail)
7351      (t 'ask))))
7352
7353 (defun gnus-button-handle-mid-or-mail (mid-or-mail)
7354   (let* ((pref gnus-button-prefer-mid-or-mail) guessed
7355          (url-mid (concat "news" ":" mid-or-mail))
7356          (url-mailto (concat "mailto" ":" mid-or-mail)))
7357     (gnus-message 9 "mid-or-mail=%s" mid-or-mail)
7358     (when (fboundp pref)
7359       (setq guessed
7360             ;; get rid of surrounding angles...
7361             (funcall pref
7362                      (gnus-replace-in-string mid-or-mail "^<\\|>$" "")))
7363       (if (or (eq 'mid guessed) (eq 'mail guessed))
7364           (setq pref guessed)
7365         (setq pref 'ask)))
7366     (if (eq pref 'ask)
7367         (save-window-excursion
7368           (if (y-or-n-p (concat "Is <" mid-or-mail "> a mail address? "))
7369               (setq pref 'mail)
7370             (setq pref 'mid))))
7371     (cond ((eq pref 'mid)
7372            (gnus-message 8 "calling `gnus-button-handle-news' %s" url-mid)
7373            (gnus-button-handle-news url-mid))
7374           ((eq pref 'mail)
7375            (gnus-message 8 "calling `gnus-url-mailto'  %s" url-mailto)
7376            (gnus-url-mailto url-mailto))
7377           (t (gnus-message 3 "Invalid string.")))))
7378
7379 (defun gnus-button-handle-custom (fun arg)
7380   "Call function FUN on argument ARG.
7381 Both FUN and ARG are supposed to be strings.  ARG will be passed
7382 as a symbol to FUN."
7383   (funcall (intern fun)
7384            (if (string-match "^customize-apropos" fun)
7385                arg
7386              (intern arg))))
7387
7388 (defvar gnus-button-handle-describe-prefix "^\\(C-h\\|<?[Ff]1>?\\)")
7389
7390 ;; FIXME: Maybe we should merge some of the functions that do quite similar
7391 ;; stuff?
7392
7393 (defun gnus-button-handle-describe-function (url)
7394   "Call `describe-function' when pushing the corresponding URL button."
7395   (describe-function
7396    (intern
7397     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7398
7399 (defun gnus-button-handle-describe-variable (url)
7400   "Call `describe-variable' when pushing the corresponding URL button."
7401   (describe-variable
7402    (intern
7403     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7404
7405 (defun gnus-button-handle-symbol (url)
7406 "Display help on variable or function.
7407 Calls `describe-variable' or `describe-function'."
7408   (let ((sym (intern url)))
7409     (cond
7410      ((fboundp sym) (describe-function sym))
7411      ((boundp sym) (describe-variable sym))
7412      (t (gnus-message 3 "`%s' is not a known function of variable." url)))))
7413
7414 (defun gnus-button-handle-describe-key (url)
7415   "Call `describe-key' when pushing the corresponding URL button."
7416   (let* ((key-string
7417           (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))
7418          (keys (ignore-errors (eval `(kbd ,key-string)))))
7419     (if keys
7420         (describe-key keys)
7421       (gnus-message 3 "Invalid key sequence in button: %s" key-string))))
7422
7423 (defun gnus-button-handle-apropos (url)
7424   "Call `apropos' when pushing the corresponding URL button."
7425   (apropos (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7426
7427 (defun gnus-button-handle-apropos-command (url)
7428   "Call `apropos' when pushing the corresponding URL button."
7429   (apropos-command
7430    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7431
7432 (defun gnus-button-handle-apropos-variable (url)
7433   "Call `apropos' when pushing the corresponding URL button."
7434   (funcall
7435    (if (fboundp 'apropos-variable) 'apropos-variable 'apropos)
7436    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7437
7438 (defun gnus-button-handle-apropos-documentation (url)
7439   "Call `apropos' when pushing the corresponding URL button."
7440   (funcall
7441    (if (fboundp 'apropos-documentation) 'apropos-documentation 'apropos)
7442    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7443
7444 (defun gnus-button-handle-library (url)
7445   "Call `locate-library' when pushing the corresponding URL button."
7446   (gnus-message 9 "url=`%s'" url)
7447   (let* ((lib (locate-library url))
7448          (file (gnus-replace-in-string (or lib "") "\.elc" ".el")))
7449     (if (not lib)
7450         (gnus-message 1 "Cannot locale library `%s'." url)
7451       (find-file-read-only file))))
7452
7453 (defun gnus-button-handle-ctan (url)
7454   "Call `browse-url' when pushing a CTAN URL button."
7455   (funcall
7456    gnus-button-ctan-handler
7457    (concat
7458     gnus-ctan-url
7459     (gnus-replace-in-string url gnus-button-handle-ctan-bogus-regexp ""))))
7460
7461 (defcustom gnus-button-tex-level 5
7462   "*Integer that says how many TeX-related buttons Gnus will show.
7463 The higher the number, the more buttons will appear and the more false
7464 positives are possible.  Note that you can set this variable local to
7465 specific groups.  Setting it higher in TeX groups is probably a good idea.
7466 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
7467 how to set variables in specific groups."
7468   :version "22.1"
7469   :group 'gnus-article-buttons
7470   :link '(custom-manual "(gnus)Group Parameters")
7471   :type 'integer)
7472
7473 (defcustom gnus-button-man-level 5
7474   "*Integer that says how many man-related buttons Gnus will show.
7475 The higher the number, the more buttons will appear and the more false
7476 positives are possible.  Note that you can set this variable local to
7477 specific groups.  Setting it higher in Unix groups is probably a good idea.
7478 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
7479 how to set variables in specific groups."
7480   :version "22.1"
7481   :group 'gnus-article-buttons
7482   :link '(custom-manual "(gnus)Group Parameters")
7483   :type 'integer)
7484
7485 (defcustom gnus-button-emacs-level 5
7486   "*Integer that says how many emacs-related buttons Gnus will show.
7487 The higher the number, the more buttons will appear and the more false
7488 positives are possible.  Note that you can set this variable local to
7489 specific groups.  Setting it higher in Emacs or Gnus related groups is
7490 probably a good idea.  See Info node `(gnus)Group Parameters' and the variable
7491 `gnus-parameters' on how to set variables in specific groups."
7492   :version "22.1"
7493   :group 'gnus-article-buttons
7494   :link '(custom-manual "(gnus)Group Parameters")
7495   :type 'integer)
7496
7497 (defcustom gnus-button-message-level 5
7498   "*Integer that says how many buttons for news or mail messages will appear.
7499 The higher the number, the more buttons will appear and the more false
7500 positives are possible."
7501   ;; mail addresses, MIDs, URLs for news, ...
7502   :version "22.1"
7503   :group 'gnus-article-buttons
7504   :type 'integer)
7505
7506 (defcustom gnus-button-browse-level 5
7507   "*Integer that says how many buttons for browsing will appear.
7508 The higher the number, the more buttons will appear and the more false
7509 positives are possible."
7510   ;; stuff handled by `browse-url' or `gnus-button-embedded-url'
7511   :version "22.1"
7512   :group 'gnus-article-buttons
7513   :type 'integer)
7514
7515 (defcustom gnus-button-alist
7516   '(("<\\(url:[>\n\t ]*?\\)?\\(nntp\\|news\\):[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
7517      0 (>= gnus-button-message-level 0) gnus-button-handle-news 3)
7518     ((concat "\\b\\(nntp\\|news\\):\\("
7519              gnus-button-valid-localpart-regexp "@[a-z0-9.-]+[a-z]\\)")
7520      0 t gnus-button-handle-news 2)
7521     ("\\(\\b<\\(url:[>\n\t ]*\\)?\\(nntp\\|news\\):[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
7522      1 (>= gnus-button-message-level 0) gnus-button-fetch-group 5)
7523     ("\\b\\(nntp\\|news\\):\\(//\\)?\\([^'\">\n\t ]+\\)"
7524      0 (>= gnus-button-message-level 0) gnus-button-fetch-group 3)
7525     ;; RFC 2392 (Don't allow `/' in domain part --> CID)
7526     ("\\bmid:\\(//\\)?\\([^'\">\n\t ]+@[^'\">\n\t /]+\\)"
7527      0 (>= gnus-button-message-level 0) gnus-button-message-id 2)
7528     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7529      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7530     ("\\b\\(mid\\|message-id\\):? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7531      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7532     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>"
7533      0 (>= gnus-button-message-level 0) gnus-url-mailto 2)
7534     ;; RFC 2368 (The mailto URL scheme)
7535     ("\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7536      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7537     ("\\bmailto:\\([^ \n\t]+\\)"
7538      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7539     ;; CTAN
7540     ((concat "\\bCTAN:[ \t\n]?[^>)!;:,'\n\t ]*\\("
7541              gnus-button-ctan-directory-regexp
7542              "[^][>)!;:,'\n\t ]+\\)")
7543      0 (>= gnus-button-tex-level 1) gnus-button-handle-ctan 1)
7544     ((concat "\\btex-archive/\\("
7545              gnus-button-ctan-directory-regexp
7546              "/[-_.a-z0-9/]+[-_./a-z0-9]+[/a-z0-9]\\)")
7547      1 (>= gnus-button-tex-level 6) gnus-button-handle-ctan 1)
7548     ((concat
7549       "\\b\\("
7550       gnus-button-ctan-directory-regexp
7551       "/[-_.a-z0-9]+/[-_./a-z0-9]+[/a-z0-9]\\)")
7552      1 (>= gnus-button-tex-level 8) gnus-button-handle-ctan 1)
7553     ;; Info Konqueror style <info:/foo/bar baz>.
7554     ;; Must come before " Gnus home-grown style".
7555     ("\\binfo://?\\([^'\">\n\t]+\\)"
7556      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7557    ;; Info, Gnus home-grown style (deprecated) <info://foo/bar+baz>
7558     ("\\binfo://\\([^'\">\n\t ]+\\)"
7559      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7560     ;; Info GNOME style <info:foo#bar_baz>
7561     ("\\binfo:\\([^('\n\t\r \"><][^'\n\t\r \"><]*\\)"
7562      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-gnome 1)
7563     ;; Info KDE style <info:(foo)bar baz>
7564     ("<\\(info:\\(([^)]+)[^>\n\r]*\\)\\)>"
7565      1 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-kde 2)
7566     ("\\((Info-goto-node\\|(info\\)[ \t\n]*\\(\"[^\"]*\"\\))" 0
7567      (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 2)
7568     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+i[ \t\n]+d?[ \t\n]?m[ \t\n]+[^ ]+ ?[^ ]+[ \t\n]+RET\\([ \t\n]+i[ \t\n]+[^ ]+ ?[^ ]+[ \t\n]+RET\\([ \t\n,]*\\)\\)?"
7569      ;; Info links like `C-h i d m Gnus RET' or `C-h i d m Gnus RET i partial RET'
7570      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-keystrokes 0)
7571     ;; This is custom
7572     ("M-x[ \t\n]\\(customize-[^ ]+\\)[ \t\n]RET[ \t\n]\\([^ ]+\\)[ \t\n]RET" 0
7573      (>= gnus-button-emacs-level 1) gnus-button-handle-custom 1 2)
7574     ;; Emacs help commands
7575     ("M-x[ \t\n]+apropos[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7576      ;; regexp doesn't match arguments containing ` '.
7577      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos 1)
7578     ("M-x[ \t\n]+apropos-command[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7579      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-command 1)
7580     ("M-x[ \t\n]+apropos-variable[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7581      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-variable 1)
7582     ("M-x[ \t\n]+apropos-documentation[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7583      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-documentation 1)
7584     ;; The following entries may lead to many false positives so don't enable
7585     ;; them by default (use a high button level).
7586     ("/\\([a-z][-a-z0-9]+\\.el\\)\\>[^.?]"
7587      ;; Exclude [.?] for URLs in gmane.emacs.cvs
7588      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7589     ("`\\([a-z][-a-z0-9]+\\.el\\)'"
7590      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7591     ("`\\([a-z][a-z0-9]+-[a-z0-9]+-[-a-z0-9]*[a-z]\\|\\(gnus\\|message\\)-[-a-z]+\\)'"
7592      0 (>= gnus-button-emacs-level 8) gnus-button-handle-symbol 1)
7593     ("`\\([a-z][a-z0-9]+-[a-z]+\\)'"
7594      0 (>= gnus-button-emacs-level 9) gnus-button-handle-symbol 1)
7595     ("(setq[ \t\n]+\\([a-z][a-z0-9]+-[-a-z0-9]+\\)[ \t\n]+.+)"
7596      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-variable 1)
7597     ("\\bM-x[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7598      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-function 1)
7599     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+f[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7600      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-function 2)
7601     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+v[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7602      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-variable 2)
7603     ("`\\(\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+k[ \t\n]+\\([^']+\\)\\)'"
7604      ;; Unlike the other regexps we really have to require quoting
7605      ;; here to determine where it ends.
7606      1 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-key 3)
7607     ;; This is how URLs _should_ be embedded in text (RFC 1738, RFC 2396)...
7608     ("<URL: *\\([^\n<>]*\\)>"
7609      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7610     ;; RFC 2396 (2.4.3., delims) ...
7611     ("\"URL: *\\([^\n\"]*\\)\""
7612      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7613     ;; Raw URLs.
7614     (gnus-button-url-regexp
7615      0 (>= gnus-button-browse-level 0) browse-url 0)
7616     ;; man pages
7617     ("\\b\\([a-z][a-z]+([1-9])\\)\\W"
7618      0 (and (>= gnus-button-man-level 1) (< gnus-button-man-level 3))
7619      gnus-button-handle-man 1)
7620     ;; more man pages: resolv.conf(5), iso_8859-1(7), xterm(1x)
7621     ("\\b\\([a-z][-_.a-z0-9]+([1-9])\\)\\W"
7622      0 (and (>= gnus-button-man-level 3) (< gnus-button-man-level 5))
7623      gnus-button-handle-man 1)
7624     ;; even more: Apache::PerlRun(3pm), PDL::IO::FastRaw(3pm),
7625     ;; SoWWWAnchor(3iv), XSelectInput(3X11), X(1), X(7)
7626     ("\\b\\(\\(?:[a-z][-+_.:a-z0-9]+([1-9][X1a-z]*)\\)\\|\\b\\(?:X([1-9])\\)\\)\\W"
7627      0 (>= gnus-button-man-level 5) gnus-button-handle-man 1)
7628     ;; Recognizing patches to .el files.  This is somewhat obscure,
7629     ;; but considering the percentage of Gnus users who hack Emacs
7630     ;; Lisp files...
7631     ("^--- \\([^ .]+\\.el\\).*\n.*\n@@ -?\\([0-9]+\\)" 1
7632      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7633     ("^\\*\\*\\* \\([^ .]+\\.el\\).*\n.*\n\\*+\n\\*\\*\\* \\([0-9]+\\)" 1
7634      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7635     ;; MID or mail: To avoid too many false positives we don't try to catch
7636     ;; all kind of allowed MIDs or mail addresses.  Domain part must contain
7637     ;; at least one dot.  TLD must contain two or three chars or be a know TLD
7638     ;; (info|name|...).  Put this entry near the _end_ of `gnus-button-alist'
7639     ;; so that non-ambiguous entries (see above) match first.
7640     (gnus-button-mid-or-mail-regexp
7641      0 (>= gnus-button-message-level 5) gnus-button-handle-mid-or-mail 1))
7642   "*Alist of regexps matching buttons in article bodies.
7643
7644 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
7645 REGEXP: is the string (case insensitive) matching text around the button (can
7646 also be Lisp expression evaluating to a string),
7647 BUTTON: is the number of the regexp grouping actually matching the button,
7648 FORM: is a Lisp expression which must eval to true for the button to
7649 be added,
7650 CALLBACK: is the function to call when the user push this button, and each
7651 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
7652
7653 CALLBACK can also be a variable, in that case the value of that
7654 variable it the real callback function."
7655   :group 'gnus-article-buttons
7656   :type '(repeat (list (choice regexp variable sexp)
7657                        (integer :tag "Button")
7658                        (sexp :tag "Form")
7659                        (function :tag "Callback")
7660                        (repeat :tag "Par"
7661                                :inline t
7662                                (integer :tag "Regexp group")))))
7663 (put 'gnus-button-alist 'risky-local-variable t)
7664
7665 (defcustom gnus-header-button-alist
7666   '(("^\\(References\\|Message-I[Dd]\\|^In-Reply-To\\):" "<[^<>]+>"
7667      0 (>= gnus-button-message-level 0) gnus-button-message-id 0)
7668     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$"
7669      1 (>= gnus-button-message-level 0) gnus-button-reply 1)
7670     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
7671      0 (>= gnus-button-message-level 0) gnus-msg-mail 0)
7672     ("^X-[Uu][Rr][Ll]:" gnus-button-url-regexp
7673      0 (>= gnus-button-browse-level 0) browse-url 0)
7674     ("^Subject:" gnus-button-url-regexp
7675      0 (>= gnus-button-browse-level 0) browse-url 0)
7676     ("^[^:]+:" gnus-button-url-regexp
7677      0 (>= gnus-button-browse-level 0) browse-url 0)
7678     ("^OpenPGP:.*url=" gnus-button-url-regexp
7679      0 (>= gnus-button-browse-level 0) gnus-button-openpgp 0)
7680     ("^[^:]+:" "\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7681      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7682     ("^[^:]+:" "\\(<\\(url: \\)?\\(nntp\\|news\\):\\([^>\n ]*\\)>\\)"
7683      1 (>= gnus-button-message-level 0) gnus-button-message-id 4))
7684   "*Alist of headers and regexps to match buttons in article heads.
7685
7686 This alist is very similar to `gnus-button-alist', except that each
7687 alist has an additional HEADER element first in each entry:
7688
7689 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
7690
7691 HEADER is a regexp to match a header.  For a fuller explanation, see
7692 `gnus-button-alist'."
7693   :group 'gnus-article-buttons
7694   :group 'gnus-article-headers
7695   :type '(repeat (list (regexp :tag "Header")
7696                        (choice regexp variable)
7697                        (integer :tag "Button")
7698                        (sexp :tag "Form")
7699                        (function :tag "Callback")
7700                        (repeat :tag "Par"
7701                                :inline t
7702                                (integer :tag "Regexp group")))))
7703 (put 'gnus-header-button-alist 'risky-local-variable t)
7704
7705 ;;; Commands:
7706
7707 (defun gnus-article-push-button (event)
7708   "Check text under the mouse pointer for a callback function.
7709 If the text under the mouse pointer has a `gnus-callback' property,
7710 call it with the value of the `gnus-data' text property."
7711   (interactive "e")
7712   (set-buffer (window-buffer (posn-window (event-start event))))
7713   (let* ((pos (posn-point (event-start event)))
7714          (data (get-text-property pos 'gnus-data))
7715          (fun (get-text-property pos 'gnus-callback)))
7716     (goto-char pos)
7717     (when fun
7718       (funcall fun data))))
7719
7720 (defun gnus-article-press-button ()
7721   "Check text at point for a callback function.
7722 If the text at point has a `gnus-callback' property,
7723 call it with the value of the `gnus-data' text property."
7724   (interactive)
7725   (let ((data (get-text-property (point) 'gnus-data))
7726         (fun (get-text-property (point) 'gnus-callback)))
7727     (when fun
7728       (funcall fun data))))
7729
7730 (defun gnus-article-highlight (&optional force)
7731   "Highlight current article.
7732 This function calls `gnus-article-highlight-headers',
7733 `gnus-article-highlight-citation',
7734 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7735 do the highlighting.  See the documentation for those functions."
7736   (interactive (list 'force))
7737   (gnus-article-highlight-headers)
7738   (gnus-article-highlight-citation force)
7739   (gnus-article-highlight-signature)
7740   (gnus-article-add-buttons force)
7741   (gnus-article-add-buttons-to-head))
7742
7743 (defun gnus-article-highlight-some (&optional force)
7744   "Highlight current article.
7745 This function calls `gnus-article-highlight-headers',
7746 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7747 do the highlighting.  See the documentation for those functions."
7748   (interactive (list 'force))
7749   (gnus-article-highlight-headers)
7750   (gnus-article-highlight-signature)
7751   (gnus-article-add-buttons))
7752
7753 (defun gnus-article-highlight-headers ()
7754   "Highlight article headers as specified by `gnus-header-face-alist'."
7755   (interactive)
7756   (gnus-with-article-headers
7757     (let (regexp header-face field-face from hpoints fpoints)
7758       (dolist (entry gnus-header-face-alist)
7759         (goto-char (point-min))
7760         (setq regexp (concat "^\\("
7761                              (if (string-equal "" (nth 0 entry))
7762                                  "[^\t ]"
7763                                (nth 0 entry))
7764                              "\\)")
7765               header-face (nth 1 entry)
7766               field-face (nth 2 entry))
7767         (while (and (re-search-forward regexp nil t)
7768                     (not (eobp)))
7769           (beginning-of-line)
7770           (setq from (point))
7771           (unless (search-forward ":" nil t)
7772             (forward-char 1))
7773           (when (and header-face
7774                      (not (memq (point) hpoints)))
7775             (push (point) hpoints)
7776             (gnus-put-text-property from (point) 'face header-face))
7777           (when (and field-face
7778                      (not (memq (setq from (point)) fpoints)))
7779             (push from fpoints)
7780             (if (re-search-forward "^[^ \t]" nil t)
7781                 (forward-char -2)
7782               (goto-char (point-max)))
7783             (gnus-put-text-property from (point) 'face field-face)))))))
7784
7785 (defun gnus-article-highlight-signature ()
7786   "Highlight the signature in an article.
7787 It does this by highlighting everything after
7788 `gnus-signature-separator' using the face `gnus-signature'."
7789   (interactive)
7790   (gnus-with-article-buffer
7791     (let ((inhibit-point-motion-hooks t))
7792       (save-restriction
7793         (when (and gnus-signature-face
7794                    (gnus-article-narrow-to-signature))
7795           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max) nil t)
7796                             'face gnus-signature-face)
7797           (widen)
7798           (gnus-article-search-signature)
7799           (let ((start (match-beginning 0))
7800                 (end (set-marker (make-marker) (1+ (match-end 0)))))
7801             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
7802                                      end)))))))
7803
7804 (defun gnus-button-in-region-p (b e prop)
7805   "Say whether PROP exists in the region."
7806   (text-property-not-all b e prop nil))
7807
7808 (defun gnus-article-add-buttons (&optional force)
7809   "Find external references in the article and make buttons of them.
7810 \"External references\" are things like Message-IDs and URLs, as
7811 specified by `gnus-button-alist'."
7812   (interactive (list 'force))
7813   (gnus-with-article-buffer
7814     (let ((inhibit-point-motion-hooks t)
7815           (case-fold-search t)
7816           (alist gnus-button-alist)
7817           beg entry regexp)
7818       ;; Remove all old markers.
7819       (let (marker entry new-list)
7820         (while (setq marker (pop gnus-button-marker-list))
7821           (if (or (< marker (point-min)) (>= marker (point-max)))
7822               (push marker new-list)
7823             (goto-char marker)
7824             (when (setq entry (gnus-button-entry))
7825               (put-text-property (match-beginning (nth 1 entry))
7826                                  (match-end (nth 1 entry))
7827                                  'gnus-callback nil))
7828             (set-marker marker nil)))
7829         (setq gnus-button-marker-list new-list))
7830       ;; We skip the headers.
7831       (article-goto-body)
7832       (setq beg (point))
7833       (while (setq entry (pop alist))
7834         (setq regexp (eval (car entry)))
7835         (goto-char beg)
7836         (while (re-search-forward regexp nil t)
7837           (let ((start (match-beginning (nth 1 entry)))
7838                 (end (match-end (nth 1 entry)))
7839                 (from (match-beginning 0)))
7840             (when (and (or (eq t (nth 2 entry))
7841                            (eval (nth 2 entry)))
7842                        (not (gnus-button-in-region-p
7843                              start end 'gnus-callback)))
7844               ;; That optional form returned non-nil, so we add the
7845               ;; button.
7846               (setq from (set-marker (make-marker) from))
7847               (push from gnus-button-marker-list)
7848               (unless (and (eq (car entry) 'gnus-button-url-regexp)
7849                            (gnus-article-extend-url-button from start end))
7850                 (gnus-article-add-button start end
7851                                          'gnus-button-push from)
7852                 (gnus-put-text-property
7853                  start end
7854                  'gnus-string (buffer-substring-no-properties
7855                                start end))))))))))
7856
7857 (defun gnus-article-extend-url-button (beg start end)
7858   "Extend url button if url is folded into two or more lines.
7859 Return non-nil if button is extended.  BEG is a marker that points to
7860 the beginning position of a text containing url.  START and END are
7861 the endpoints of a url button before it is extended.  The concatenated
7862 url is put as the `gnus-button-url' overlay property on the button."
7863   (let ((opoint (point))
7864         (points (list start end))
7865         url delim regexp)
7866     (prog1
7867         (when (and (progn
7868                      (goto-char end)
7869                      (not (looking-at "[\t ]*[\">]")))
7870                    (progn
7871                      (goto-char start)
7872                      (string-match
7873                       "\\(?:\"\\|\\(<\\)\\)[\t ]*\\(?:url[\t ]*:[\t ]*\\)?\\'"
7874                       (buffer-substring (point-at-bol) start)))
7875                    (progn
7876                      (setq url (list (buffer-substring start end))
7877                            delim (if (match-beginning 1) ">" "\""))
7878                      (beginning-of-line)
7879                      (setq regexp (concat
7880                                    (when (and (looking-at
7881                                                message-cite-prefix-regexp)
7882                                               (< (match-end 0) start))
7883                                      (regexp-quote (match-string 0)))
7884                                    "\
7885 \[\t ]*\\(?:\\([^\t\n \">]+\\)[\t ]*$\\|\\([^\t\n \">]*\\)[\t ]*"
7886                                    delim "\\)"))
7887                      (while (progn
7888                               (forward-line 1)
7889                               (and (looking-at regexp)
7890                                    (prog1
7891                                        (match-beginning 1)
7892                                      (push (or (match-string 2)
7893                                                (match-string 1))
7894                                            url)
7895                                      (push (setq end (or (match-end 2)
7896                                                          (match-end 1)))
7897                                            points)
7898                                      (push (or (match-beginning 2)
7899                                                (match-beginning 1))
7900                                            points)))))
7901                      (match-beginning 2)))
7902           (let (gnus-article-mouse-face widget-mouse-face)
7903             (while points
7904               (gnus-article-add-button (pop points) (pop points)
7905                                        'gnus-button-push beg)))
7906           (let ((overlay (gnus-make-overlay start end)))
7907             (gnus-overlay-put overlay 'evaporate t)
7908             (gnus-overlay-put overlay 'gnus-button-url
7909                               (list (mapconcat 'identity (nreverse url) "")))
7910             (when gnus-article-mouse-face
7911               (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))
7912           t)
7913       (goto-char opoint))))
7914
7915 ;; Add buttons to the head of an article.
7916 (defun gnus-article-add-buttons-to-head ()
7917   "Add buttons to the head of the article."
7918   (interactive)
7919   (gnus-with-article-headers
7920     (let (beg end)
7921       (dolist (entry gnus-header-button-alist)
7922         ;; Each alist entry.
7923         (goto-char (point-min))
7924         (while (re-search-forward (car entry) nil t)
7925           ;; Each header matching the entry.
7926           (setq beg (match-beginning 0))
7927           (setq end (or (and (re-search-forward "^[^ \t]" nil t)
7928                              (match-beginning 0))
7929                         (point-max)))
7930           (goto-char beg)
7931           (while (re-search-forward (eval (nth 1 entry)) end t)
7932             ;; Each match within a header.
7933             (let* ((entry (cdr entry))
7934                    (start (match-beginning (nth 1 entry)))
7935                    (end (match-end (nth 1 entry)))
7936                    (form (nth 2 entry)))
7937               (goto-char (match-end 0))
7938               (when (eval form)
7939                 (gnus-article-add-button
7940                  start end (nth 3 entry)
7941                  (buffer-substring (match-beginning (nth 4 entry))
7942                                    (match-end (nth 4 entry)))))))
7943           (goto-char end))))))
7944
7945 ;;; External functions:
7946
7947 (defun gnus-article-add-button (from to fun &optional data text)
7948   "Create a button between FROM and TO with callback FUN and data DATA."
7949   (when gnus-article-button-face
7950     (gnus-overlay-put (gnus-make-overlay from to nil t)
7951                       'face gnus-article-button-face))
7952   (gnus-add-text-properties
7953    from to
7954    (nconc (and gnus-article-mouse-face
7955                (list gnus-mouse-face-prop gnus-article-mouse-face))
7956           (list 'gnus-callback fun)
7957           (and data (list 'gnus-data data))))
7958   (widget-convert-button 'link from to :action 'gnus-widget-press-button
7959                          :help-echo (or text "Follow the link")
7960                          :keymap gnus-url-button-map
7961                          :button-keymap gnus-widget-button-keymap))
7962
7963 (defun gnus-article-copy-string ()
7964   "Copy the string in the button to the kill ring."
7965   (interactive)
7966   (gnus-article-check-buffer)
7967   (let ((data (get-text-property (point) 'gnus-string)))
7968     (when data
7969       (with-temp-buffer
7970         (insert data)
7971         (copy-region-as-kill (point-min) (point-max))
7972         (message "Copied %s" data)))))
7973
7974 ;;; Internal functions:
7975
7976 (defun gnus-article-set-globals ()
7977   (with-current-buffer gnus-summary-buffer
7978     (gnus-set-global-variables)))
7979
7980 (defun gnus-signature-toggle (end)
7981   (gnus-with-article-buffer
7982     (let ((inhibit-point-motion-hooks t))
7983       (if (text-property-any end (point-max) 'article-type 'signature)
7984           (progn
7985             (gnus-delete-wash-type 'signature)
7986             (gnus-remove-text-properties-when
7987              'article-type 'signature end (point-max)
7988              (cons 'article-type (cons 'signature
7989                                        gnus-hidden-properties))))
7990         (gnus-add-wash-type 'signature)
7991         (gnus-add-text-properties-when
7992          'article-type nil end (point-max)
7993          (cons 'article-type (cons 'signature
7994                                    gnus-hidden-properties)))))
7995     (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
7996       (gnus-set-mode-line 'article))))
7997
7998 (defun gnus-button-entry ()
7999   ;; Return the first entry in `gnus-button-alist' matching this place.
8000   (let ((alist gnus-button-alist)
8001         (entry nil))
8002     (while alist
8003       (setq entry (pop alist))
8004       (if (looking-at (eval (car entry)))
8005           (setq alist nil)
8006         (setq entry nil)))
8007     entry))
8008
8009 (defun gnus-button-push (marker)
8010   ;; Push button starting at MARKER.
8011   (save-excursion
8012     (goto-char marker)
8013     (let* ((entry (gnus-button-entry))
8014            (inhibit-point-motion-hooks t)
8015            (fun (nth 3 entry))
8016            (args (or (and (eq (car entry) 'gnus-button-url-regexp)
8017                           (get-char-property marker 'gnus-button-url))
8018                      (mapcar (lambda (group)
8019                                (let ((string (match-string group)))
8020                                  (set-text-properties
8021                                   0 (length string) nil string)
8022                                  string))
8023                              (nthcdr 4 entry)))))
8024       (cond
8025        ((fboundp fun)
8026         (apply fun args))
8027        ((and (boundp fun)
8028              (fboundp (symbol-value fun)))
8029         (apply (symbol-value fun) args))
8030        (t
8031         (gnus-message 1 "You must define `%S' to use this button"
8032                       (cons fun args)))))))
8033
8034 (defun gnus-parse-news-url (url)
8035   (let (scheme server port group message-id articles)
8036     (with-temp-buffer
8037       (insert url)
8038       (goto-char (point-min))
8039       (when (looking-at "\\([A-Za-z]+\\):")
8040         (setq scheme (match-string 1))
8041         (goto-char (match-end 0)))
8042       (when (looking-at "//\\([^:/]+\\)\\(:?\\)\\([0-9]+\\)?/")
8043         (setq server (match-string 1))
8044         (setq port (if (stringp (match-string 3))
8045                        (string-to-number (match-string 3))
8046                      (match-string 3)))
8047         (goto-char (match-end 0)))
8048
8049       (cond
8050        ((looking-at "\\(.*@.*\\)")
8051         (setq message-id (match-string 1)))
8052        ((looking-at "\\([^/]+\\)/\\([-0-9]+\\)")
8053         (setq group (match-string 1)
8054               articles (split-string (match-string 2) "-")))
8055        ((looking-at "\\([^/]+\\)/?")
8056         (setq group (match-string 1)))
8057        (t
8058         (error "Unknown news URL syntax"))))
8059     (list scheme server port group message-id articles)))
8060
8061 (defun gnus-button-handle-news (url)
8062   "Fetch a news URL."
8063   (destructuring-bind (scheme server port group message-id articles)
8064       (gnus-parse-news-url url)
8065     (cond
8066      (message-id
8067       (with-current-buffer gnus-summary-buffer
8068         (if server
8069             (let ((gnus-refer-article-method
8070                    (nconc (list (list 'nntp server))
8071                           gnus-refer-article-method))
8072                   (nntp-port-number (or port "nntp")))
8073               (gnus-message 7 "Fetching %s with %s"
8074                             message-id gnus-refer-article-method)
8075               (gnus-summary-refer-article message-id))
8076           (gnus-summary-refer-article message-id))))
8077      (group
8078       (gnus-button-fetch-group url)))))
8079
8080 (defun gnus-button-patch (library line)
8081   "Visit an Emacs Lisp library LIBRARY on line LINE."
8082   (interactive)
8083   (let ((file (locate-library (file-name-nondirectory library))))
8084     (unless file
8085       (error "Couldn't find library %s" library))
8086     (find-file file)
8087     (goto-char (point-min))
8088     (forward-line (1- (string-to-number line)))))
8089
8090 (defun gnus-button-handle-man (url)
8091   "Fetch a man page."
8092   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
8093   (when (eq gnus-button-man-handler 'woman)
8094     (setq url (gnus-replace-in-string url "([1-9][X1a-z]*).*\\'" "")))
8095   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
8096   (funcall gnus-button-man-handler url))
8097
8098 (defun gnus-button-handle-info-url (url)
8099   "Fetch an info URL."
8100   (setq url (mm-subst-char-in-string ?+ ?\  url))
8101   (cond
8102    ((string-match "^\\([^:/]+\\)?/\\(.*\\)" url)
8103     (gnus-info-find-node
8104      (concat "(" (or (gnus-url-unhex-string (match-string 1 url))
8105                      "Gnus")
8106              ")" (gnus-url-unhex-string (match-string 2 url)))))
8107    ((string-match "([^)\"]+)[^\"]+" url)
8108     (setq url
8109           (gnus-replace-in-string
8110            (gnus-replace-in-string url "[\n\t ]+" " ") "\"" ""))
8111     (gnus-info-find-node url))
8112    (t (error "Can't parse %s" url))))
8113
8114 (defun gnus-button-handle-info-url-gnome (url)
8115   "Fetch GNOME style info URL."
8116   (setq url (mm-subst-char-in-string ?_ ?\  url))
8117   (if (string-match "\\([^#]+\\)#?\\(.*\\)" url)
8118       (gnus-info-find-node
8119        (concat "("
8120                (gnus-url-unhex-string
8121                  (match-string 1 url))
8122                ")"
8123                (or (gnus-url-unhex-string
8124                     (match-string 2 url))
8125                    "Top")))
8126     (error "Can't parse %s" url)))
8127
8128 (defun gnus-button-handle-info-url-kde (url)
8129   "Fetch KDE style info URL."
8130   (gnus-info-find-node (gnus-url-unhex-string url)))
8131
8132 ;; (info) will autoload info.el
8133 (declare-function Info-menu "info" (menu-item &optional fork))
8134 (declare-function Info-index-next "info" (num))
8135
8136 (defun gnus-button-handle-info-keystrokes (url)
8137   "Call `info' when pushing the corresponding URL button."
8138   ;; For links like `C-h i d m gnus RET part RET , ,', `C-h i d m CC Mode RET'.
8139   (let (node indx comma)
8140     (if (string-match
8141          (concat "\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+i[ \t\n]+d?[ \t\n]?m[ \t\n]+"
8142                  "\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET"
8143                  "\\([ \t\n]+i[ \t\n]+[^ ]+ ?[^ ]+[ \t\n]+RET"
8144                  "\\(?:[ \t\n,]*\\)\\)?")
8145          url)
8146         (setq node (match-string 2 url)
8147               indx (match-string 3 url))
8148       (error "Can't parse %s" url))
8149     (info)
8150     (Info-directory)
8151     (Info-menu node)
8152     (when (> (length indx) 0)
8153       (string-match (concat "[ \t\n]+i[ \t\n]+\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET"
8154                             "\\([ \t\n,]*\\)")
8155                     indx)
8156       (setq comma (match-string 2 indx))
8157       (setq indx  (match-string 1 indx))
8158       (Info-index indx)
8159       (when comma
8160         (dotimes (i (with-temp-buffer
8161                       (insert comma)
8162                       ;; Note: the XEmacs version of `how-many' takes
8163                       ;; no optional argument.
8164                       (goto-char (point-min))
8165                       (how-many ",")))
8166           (Info-index-next 1)))
8167       nil)))
8168
8169 ;; Called after pgg-snarf-keys-region, which autoloads pgg.el.
8170 (declare-function pgg-display-output-buffer "pgg" (start end status))
8171
8172 (defun gnus-button-openpgp (url)
8173   "Retrieve and add an OpenPGP key given URL from an OpenPGP header."
8174   (with-temp-buffer
8175     (mm-url-insert-file-contents-external url)
8176     (pgg-snarf-keys-region (point-min) (point-max))
8177     (pgg-display-output-buffer nil nil nil)))
8178
8179 (defun gnus-button-message-id (message-id)
8180   "Fetch MESSAGE-ID."
8181   (with-current-buffer gnus-summary-buffer
8182     (gnus-summary-refer-article message-id)))
8183
8184 (defun gnus-button-fetch-group (address &rest ignore)
8185   "Fetch GROUP specified by ADDRESS."
8186   (when (string-match "\\`\\(nntp\\|news\\):\\(//\\)?\\(.*\\)\\'"
8187                       address)
8188     ;; Allow to use `gnus-button-fetch-group' in `browse-url-browser-function'
8189     ;; for nntp:// and news://
8190     (setq address (match-string 3 address)))
8191   (if (not (string-match "[:/]" address))
8192       ;; This is just a simple group url.
8193       (gnus-group-read-ephemeral-group address gnus-select-method)
8194     (if (not
8195          (string-match
8196           "^\\([^:/]+\\)\\(:\\([^/]+\\)\\)?/\\([^/]+\\)\\(/\\([0-9]+\\)\\)?"
8197           address))
8198         (error "Can't parse %s" address)
8199       (gnus-group-read-ephemeral-group
8200        (match-string 4 address)
8201        `(nntp ,(match-string 1 address)
8202               (nntp-address ,(match-string 1 address))
8203               (nntp-port-number ,(if (match-end 3)
8204                                      (match-string 3 address)
8205                                    "nntp")))
8206        nil nil nil
8207        (and (match-end 6) (list (string-to-number (match-string 6 address))))))))
8208
8209 (defun gnus-url-parse-query-string (query &optional downcase)
8210   (let (retval pairs cur key val)
8211     (setq pairs (split-string query "&"))
8212     (while pairs
8213       (setq cur (car pairs)
8214             pairs (cdr pairs))
8215       (if (not (string-match "=" cur))
8216           nil                           ; Grace
8217         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
8218               val (gnus-url-unhex-string (substring cur (match-end 0) nil) t))
8219         (if downcase
8220             (setq key (downcase key)))
8221         (setq cur (assoc key retval))
8222         (if cur
8223             (setcdr cur (cons val (cdr cur)))
8224           (setq retval (cons (list key val) retval)))))
8225     retval))
8226
8227 (defun gnus-url-mailto (url)
8228   ;; Send mail to someone
8229   (when (string-match "mailto:/*\\(.*\\)" url)
8230     (setq url (substring url (match-beginning 1) nil)))
8231   (let (to args subject func)
8232     (setq args (gnus-url-parse-query-string
8233                 (if (string-match "^\\?" url)
8234                     (substring url 1)
8235                   (if (string-match "^\\([^?]+\\)\\?\\(.*\\)" url)
8236                       (concat "to=" (match-string 1 url) "&"
8237                               (match-string 2 url))
8238                     (concat "to=" url)))
8239                 t)
8240           subject (cdr-safe (assoc "subject" args)))
8241     (gnus-msg-mail)
8242     (while args
8243       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
8244       (if (fboundp func)
8245           (funcall func)
8246         (message-position-on-field (caar args)))
8247       (insert (gnus-replace-in-string
8248                (mapconcat 'identity (reverse (cdar args)) ", ")
8249                "\r\n" "\n" t))
8250       (setq args (cdr args)))
8251     (if subject
8252         (message-goto-body)
8253       (message-goto-subject))))
8254
8255 (defun gnus-button-embedded-url (address)
8256   "Activate ADDRESS with `browse-url'."
8257   (browse-url (gnus-strip-whitespace address)))
8258
8259 ;;; Next/prev buttons in the article buffer.
8260
8261 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
8262 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
8263
8264 (defvar gnus-prev-page-map
8265   (let ((map (make-sparse-keymap)))
8266     (define-key map gnus-mouse-2 'gnus-button-prev-page)
8267     (define-key map "\r" 'gnus-button-prev-page)
8268     map))
8269
8270 (defvar gnus-next-page-map
8271   (let ((map (make-sparse-keymap)))
8272     (unless (>= emacs-major-version 21)
8273       ;; XEmacs doesn't care.
8274       (set-keymap-parent map gnus-article-mode-map))
8275     (define-key map gnus-mouse-2 'gnus-button-next-page)
8276     (define-key map "\r" 'gnus-button-next-page)
8277     map))
8278
8279 (defun gnus-insert-prev-page-button ()
8280   (let ((b (point)) e
8281         (inhibit-read-only t))
8282     (gnus-eval-format
8283      gnus-prev-page-line-format nil
8284      `(keymap ,gnus-prev-page-map
8285               gnus-prev t
8286               gnus-callback gnus-article-button-prev-page
8287               article-type annotation))
8288     (setq e (if (bolp)
8289                 ;; Exclude a newline.
8290                 (1- (point))
8291               (point)))
8292     (when gnus-article-button-face
8293       (gnus-overlay-put (gnus-make-overlay b e nil t)
8294                         'face gnus-article-button-face))
8295     (widget-convert-button
8296      'link b e
8297      :action 'gnus-button-prev-page
8298      :button-keymap gnus-prev-page-map)))
8299
8300 (defun gnus-button-next-page (&optional args more-args)
8301   "Go to the next page."
8302   (interactive)
8303   (let ((win (selected-window)))
8304     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8305     (gnus-article-next-page)
8306     (select-window win)))
8307
8308 (defun gnus-button-prev-page (&optional args more-args)
8309   "Go to the prev page."
8310   (interactive)
8311   (let ((win (selected-window)))
8312     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8313     (gnus-article-prev-page)
8314     (select-window win)))
8315
8316 (defun gnus-insert-next-page-button ()
8317   (let ((b (point)) e
8318         (inhibit-read-only t))
8319     (gnus-eval-format gnus-next-page-line-format nil
8320                       `(keymap ,gnus-next-page-map
8321                                gnus-next t
8322                                gnus-callback gnus-article-button-next-page
8323                                article-type annotation))
8324     (setq e (if (bolp)
8325                 ;; Exclude a newline.
8326                 (1- (point))
8327               (point)))
8328     (when gnus-article-button-face
8329       (gnus-overlay-put (gnus-make-overlay b e nil t)
8330                         'face gnus-article-button-face))
8331     (widget-convert-button
8332      'link b e
8333      :action 'gnus-button-next-page
8334      :button-keymap gnus-next-page-map)))
8335
8336 (defun gnus-article-button-next-page (arg)
8337   "Go to the next page."
8338   (interactive "P")
8339   (let ((win (selected-window)))
8340     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8341     (gnus-article-next-page)
8342     (select-window win)))
8343
8344 (defun gnus-article-button-prev-page (arg)
8345   "Go to the prev page."
8346   (interactive "P")
8347   (let ((win (selected-window)))
8348     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8349     (gnus-article-prev-page)
8350     (select-window win)))
8351
8352 (defvar gnus-decode-header-methods
8353   '(mail-decode-encoded-word-region)
8354   "List of methods used to decode headers.
8355
8356 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
8357 is FUNCTION, FUNCTION will be applied to all newsgroups.  If item is a
8358 \(REGEXP . FUNCTION), FUNCTION will be only apply to the newsgroups
8359 whose names match REGEXP.
8360
8361 For example:
8362 \((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
8363  mail-decode-encoded-word-region
8364  (\"chinese\" . rfc1843-decode-region))
8365 ")
8366
8367 (defvar gnus-decode-header-methods-cache nil)
8368
8369 (defun gnus-multi-decode-header (start end)
8370   "Apply the functions from `gnus-encoded-word-methods' that match."
8371   (unless (and gnus-decode-header-methods-cache
8372                (eq gnus-newsgroup-name
8373                    (car gnus-decode-header-methods-cache)))
8374     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
8375     (dolist (x gnus-decode-header-methods)
8376       (if (symbolp x)
8377           (nconc gnus-decode-header-methods-cache (list x))
8378         (if (and gnus-newsgroup-name
8379                  (string-match (car x) gnus-newsgroup-name))
8380             (nconc gnus-decode-header-methods-cache
8381                    (list (cdr x)))))))
8382   (let ((xlist gnus-decode-header-methods-cache))
8383     (pop xlist)
8384     (save-restriction
8385       (narrow-to-region start end)
8386       (while xlist
8387         (funcall (pop xlist) (point-min) (point-max))))))
8388
8389 ;;;
8390 ;;; Treatment top-level handling.
8391 ;;;
8392
8393 (defun gnus-treat-article (condition &optional part-number total-parts type)
8394   (let ((length (- (point-max) (point-min)))
8395         (alist gnus-treatment-function-alist)
8396         (article-goto-body-goes-to-point-min-p t)
8397         (treated-type
8398          (or (not type)
8399              (catch 'found
8400                (let ((list gnus-article-treat-types))
8401                  (while list
8402                    (when (string-match (pop list) type)
8403                      (throw 'found t)))))))
8404         (highlightp (gnus-visual-p 'article-highlight 'highlight))
8405         val elem)
8406     (gnus-run-hooks 'gnus-part-display-hook)
8407     (dolist (elem alist)
8408       (setq val
8409             (save-excursion
8410               (when (gnus-buffer-live-p gnus-summary-buffer)
8411                 (set-buffer gnus-summary-buffer))
8412               (symbol-value (car elem))))
8413       (when (and (or (consp val)
8414                      treated-type)
8415                  (gnus-treat-predicate val)
8416                  (or (not (get (car elem) 'highlight))
8417                      highlightp))
8418         (save-restriction
8419           (funcall (cadr elem)))))))
8420
8421 ;; Dynamic variables.
8422 (defvar part-number)
8423 (defvar total-parts)
8424 (defvar type)
8425 (defvar condition)
8426 (defvar length)
8427
8428 (defun gnus-treat-predicate (val)
8429   (cond
8430    ((null val)
8431     nil)
8432    (condition
8433     (eq condition val))
8434    ((and (listp val)
8435          (stringp (car val)))
8436     (apply 'gnus-or (mapcar `(lambda (s)
8437                                (string-match s ,(or gnus-newsgroup-name "")))
8438                             val)))
8439    ((listp val)
8440     (let ((pred (pop val)))
8441       (cond
8442        ((eq pred 'or)
8443         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
8444        ((eq pred 'and)
8445         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
8446        ((eq pred 'not)
8447         (not (gnus-treat-predicate (car val))))
8448        ((eq pred 'typep)
8449         (equal (car val) type))
8450        (t
8451         (error "%S is not a valid predicate" pred)))))
8452    ((eq val t)
8453     t)
8454    ((eq val 'head)
8455     nil)
8456    ((eq val 'first)
8457     (eq part-number 1))
8458    ((eq val 'last)
8459     (eq part-number total-parts))
8460    ((numberp val)
8461     (< length val))
8462    (t
8463     (error "%S is not a valid value" val))))
8464
8465 (defun gnus-article-encrypt-body (protocol &optional n)
8466   "Encrypt the article body."
8467   (interactive
8468    (list
8469     (or gnus-article-encrypt-protocol
8470         (completing-read "Encrypt protocol: "
8471                          gnus-article-encrypt-protocol-alist
8472                          nil t))
8473     current-prefix-arg))
8474   ;; User might hit `K E' instead of `K e', so prompt once.
8475   (when (and gnus-article-encrypt-protocol
8476              gnus-novice-user)
8477     (unless (gnus-y-or-n-p "Really encrypt article(s)? ")
8478       (error "Encrypt aborted")))
8479   (let ((func (cdr (assoc protocol gnus-article-encrypt-protocol-alist))))
8480     (unless func
8481       (error "Can't find the encrypt protocol %s" protocol))
8482     (if (member gnus-newsgroup-name '("nndraft:delayed"
8483                                       "nndraft:drafts"
8484                                       "nndraft:queue"))
8485         (error "Can't encrypt the article in group %s"
8486                gnus-newsgroup-name))
8487     (gnus-summary-iterate n
8488       (with-current-buffer gnus-summary-buffer
8489         (let ((mail-parse-charset gnus-newsgroup-charset)
8490               (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
8491               (summary-buffer gnus-summary-buffer)
8492               references point)
8493           (gnus-set-global-variables)
8494           (when (gnus-group-read-only-p)
8495             (error "The current newsgroup does not support article encrypt"))
8496           (gnus-summary-show-article t)
8497           (setq references
8498               (or (mail-header-references gnus-current-headers) ""))
8499           (set-buffer gnus-article-buffer)
8500           (let* ((inhibit-read-only t)
8501                  (headers
8502                   (mapcar (lambda (field)
8503                             (and (save-restriction
8504                                    (message-narrow-to-head)
8505                                    (goto-char (point-min))
8506                                    (search-forward field nil t))
8507                                  (prog2
8508                                      (message-narrow-to-field)
8509                                      (buffer-string)
8510                                    (delete-region (point-min) (point-max))
8511                                    (widen))))
8512                           '("Content-Type:" "Content-Transfer-Encoding:"
8513                             "Content-Disposition:"))))
8514             (message-narrow-to-head)
8515             (message-remove-header "MIME-Version")
8516             (goto-char (point-max))
8517             (setq point (point))
8518             (insert (apply 'concat headers))
8519             (widen)
8520             (narrow-to-region point (point-max))
8521             (let ((message-options message-options))
8522               (message-options-set 'message-sender user-mail-address)
8523               (message-options-set 'message-recipients user-mail-address)
8524               (message-options-set 'message-sign-encrypt 'not)
8525               (funcall func))
8526             (goto-char (point-min))
8527             (insert "MIME-Version: 1.0\n")
8528             (widen)
8529             (gnus-summary-edit-article-done
8530              references nil summary-buffer t))
8531           (when gnus-keep-backlog
8532             (gnus-backlog-remove-article
8533              (car gnus-article-current) (cdr gnus-article-current)))
8534           (gnus-flush-original-article-buffer)
8535           (when gnus-use-cache
8536             (gnus-cache-update-article
8537              (car gnus-article-current) (cdr gnus-article-current))))))))
8538
8539 (defvar gnus-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n"
8540   "The following specs can be used:
8541 %t  The security MIME type
8542 %i  Additional info
8543 %d  Details
8544 %D  Details if button is pressed")
8545
8546 (defvar gnus-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n"
8547   "The following specs can be used:
8548 %t  The security MIME type
8549 %i  Additional info
8550 %d  Details
8551 %D  Details if button is pressed")
8552
8553 (defvar gnus-mime-security-button-line-format-alist
8554   '((?t gnus-tmp-type ?s)
8555     (?i gnus-tmp-info ?s)
8556     (?d gnus-tmp-details ?s)
8557     (?D gnus-tmp-pressed-details ?s)))
8558
8559 (defvar gnus-mime-security-button-commands
8560   '((gnus-article-press-button "\r" "Show Detail")
8561     (undefined "v")
8562     (undefined "t")
8563     (undefined "C")
8564     (gnus-mime-security-save-part "o" "Save...")
8565     (undefined "\C-o")
8566     (undefined "r")
8567     (undefined "d")
8568     (undefined "c")
8569     (undefined "i")
8570     (undefined "E")
8571     (undefined "e")
8572     (undefined "p")
8573     (gnus-mime-security-pipe-part "|" "Pipe To Command...")
8574     (undefined ".")))
8575
8576 (defvar gnus-mime-security-button-map
8577   (let ((map (make-sparse-keymap)))
8578     (define-key map gnus-mouse-2 'gnus-article-push-button)
8579     (define-key map gnus-down-mouse-3 'gnus-mime-security-button-menu)
8580     (dolist (c gnus-mime-security-button-commands)
8581       (define-key map (cadr c) (car c)))
8582     map))
8583
8584 (easy-menu-define
8585   gnus-mime-security-button-menu gnus-mime-security-button-map
8586   "Security button menu."
8587   `("Security Part"
8588     ,@(delq nil
8589             (mapcar (lambda (c)
8590                       (unless (eq (car c) 'undefined)
8591                         (vector (caddr c) (car c) :active t)))
8592                     gnus-mime-security-button-commands))))
8593
8594 (defun gnus-mime-security-button-menu (event prefix)
8595   "Construct a context-sensitive menu of security commands."
8596   (interactive "e\nP")
8597   (save-window-excursion
8598     (let ((pos (event-start event)))
8599       (select-window (posn-window pos))
8600       (goto-char (posn-point pos))
8601       (gnus-article-check-buffer)
8602       (popup-menu gnus-mime-security-button-menu nil prefix))))
8603
8604 (defvar gnus-mime-security-details-buffer nil)
8605
8606 (defvar gnus-mime-security-button-pressed nil)
8607
8608 (defvar gnus-mime-security-show-details-inline t
8609   "If non-nil, show details in the article buffer.")
8610
8611 (defun gnus-mime-security-verify-or-decrypt (handle)
8612   (mm-remove-parts (cdr handle))
8613   (let ((region (mm-handle-multipart-ctl-parameter handle 'gnus-region))
8614         point (inhibit-read-only t))
8615     (if region
8616         (goto-char (car region)))
8617     (setq point (point))
8618     (with-current-buffer (mm-handle-multipart-original-buffer handle)
8619       (let* ((mm-verify-option 'known)
8620              (mm-decrypt-option 'known)
8621              (nparts (mm-possibly-verify-or-decrypt (cdr handle) handle)))
8622         (unless (eq nparts (cdr handle))
8623           (mm-destroy-parts (cdr handle))
8624           (setcdr handle nparts))))
8625     (gnus-mime-display-security handle)
8626     (when region
8627       (delete-region (point) (cdr region))
8628       (set-marker (car region) nil)
8629       (set-marker (cdr region) nil))
8630     (goto-char point)))
8631
8632 (defun gnus-mime-security-show-details (handle)
8633   (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
8634     (if (not details)
8635         (gnus-message 5 "No details.")
8636       (if gnus-mime-security-show-details-inline
8637           (let ((gnus-mime-security-button-pressed
8638                  (not (get-text-property (point) 'gnus-mime-details)))
8639                 (gnus-mime-security-button-line-format
8640                  (get-text-property (point) 'gnus-line-format))
8641                 (inhibit-read-only t))
8642             (forward-char -1)
8643             (while (eq (get-text-property (point) 'gnus-line-format)
8644                        gnus-mime-security-button-line-format)
8645               (forward-char -1))
8646             (forward-char)
8647             (save-restriction
8648               (narrow-to-region (point) (point))
8649               (gnus-insert-mime-security-button handle))
8650             (delete-region (point)
8651                            (or (text-property-not-all
8652                                 (point) (point-max)
8653                                 'gnus-line-format
8654                                 gnus-mime-security-button-line-format)
8655                                (point-max))))
8656         ;; Not inlined.
8657         (if (gnus-buffer-live-p gnus-mime-security-details-buffer)
8658             (with-current-buffer gnus-mime-security-details-buffer
8659               (erase-buffer)
8660               t)
8661           (setq gnus-mime-security-details-buffer
8662                 (gnus-get-buffer-create "*MIME Security Details*")))
8663         (with-current-buffer gnus-mime-security-details-buffer
8664           (insert details)
8665           (goto-char (point-min)))
8666         (pop-to-buffer gnus-mime-security-details-buffer)))))
8667
8668 (defun gnus-mime-security-press-button (handle)
8669   (save-excursion
8670     (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8671         (gnus-mime-security-show-details handle)
8672       (gnus-mime-security-verify-or-decrypt handle))))
8673
8674 (defun gnus-insert-mime-security-button (handle &optional displayed)
8675   (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
8676          (gnus-tmp-type
8677           (concat
8678            (or (nth 2 (assoc protocol mm-verify-function-alist))
8679                (nth 2 (assoc protocol mm-decrypt-function-alist))
8680                "Unknown")
8681            (if (equal (car handle) "multipart/signed")
8682                " Signed" " Encrypted")
8683            " Part"))
8684          (gnus-tmp-info
8685           (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8686               "Undecided"))
8687          (gnus-tmp-details
8688           (mm-handle-multipart-ctl-parameter handle 'gnus-details))
8689          gnus-tmp-pressed-details
8690          b e)
8691     (setq gnus-tmp-details
8692           (if gnus-tmp-details
8693               (concat "\n" gnus-tmp-details)
8694             ""))
8695     (setq gnus-tmp-pressed-details
8696           (if gnus-mime-security-button-pressed gnus-tmp-details ""))
8697     (unless (bolp)
8698       (insert "\n"))
8699     (setq b (point))
8700     (gnus-eval-format
8701      gnus-mime-security-button-line-format
8702      gnus-mime-security-button-line-format-alist
8703      `(keymap ,gnus-mime-security-button-map
8704          gnus-callback gnus-mime-security-press-button
8705          gnus-line-format ,gnus-mime-security-button-line-format
8706          gnus-mime-details ,gnus-mime-security-button-pressed
8707          article-type annotation
8708          gnus-data ,handle))
8709     (setq e (if (bolp)
8710                 ;; Exclude a newline.
8711                 (1- (point))
8712               (point)))
8713     (when gnus-article-button-face
8714       (gnus-overlay-put (gnus-make-overlay b e nil t)
8715                         'face gnus-article-button-face))
8716     (widget-convert-button
8717      'link b e
8718      :mime-handle handle
8719      :action 'gnus-widget-press-button
8720      :button-keymap gnus-mime-security-button-map
8721      :help-echo
8722      (lambda (widget)
8723        ;; Needed to properly clear the message due to a bug in
8724        ;; wid-edit (XEmacs only).
8725        (when (boundp 'help-echo-owns-message)
8726          (setq help-echo-owns-message t))
8727        (format
8728         "%S: show detail; %S: more options"
8729         (aref gnus-mouse-2 0)
8730         (aref gnus-down-mouse-3 0))))))
8731
8732 (defun gnus-mime-display-security (handle)
8733   (save-restriction
8734     (narrow-to-region (point) (point))
8735     (unless (gnus-unbuttonized-mime-type-p (car handle))
8736       (gnus-insert-mime-security-button handle))
8737     (gnus-mime-display-part (cadr handle))
8738     (unless (bolp)
8739       (insert "\n"))
8740     (unless (gnus-unbuttonized-mime-type-p (car handle))
8741       (let ((gnus-mime-security-button-line-format
8742              gnus-mime-security-button-end-line-format))
8743         (gnus-insert-mime-security-button handle)))
8744     (mm-set-handle-multipart-parameter
8745      handle 'gnus-region
8746      (cons (set-marker (make-marker) (point-min))
8747            (set-marker (make-marker) (point-max))))
8748     (goto-char (point-max))))
8749
8750 (defun gnus-mime-security-run-function (function)
8751   "Run FUNCTION with the security part under point."
8752   (gnus-article-check-buffer)
8753   (let ((data (get-text-property (point) 'gnus-data))
8754         buffer handle)
8755     (when (and (stringp (car-safe data))
8756                (setq buffer (mm-handle-multipart-original-buffer data))
8757                (setq handle (cadr data)))
8758       (if (bufferp (mm-handle-buffer handle))
8759           (progn
8760             (setq handle (cons buffer (copy-sequence (cdr handle))))
8761             (mm-handle-set-undisplayer handle nil))
8762         (setq handle (mm-make-handle
8763                       buffer
8764                       (mm-handle-multipart-ctl-parameter handle 'protocol)
8765                       nil nil nil nil nil nil)))
8766       (funcall function handle))))
8767
8768 (defun gnus-mime-security-save-part ()
8769   "Save the security part under point."
8770   (interactive)
8771   (gnus-mime-security-run-function 'mm-save-part))
8772
8773 (defun gnus-mime-security-pipe-part ()
8774   "Pipe the security part under point to a process."
8775   (interactive)
8776   (gnus-mime-security-run-function 'mm-pipe-part))
8777
8778 (gnus-ems-redefine)
8779
8780 (provide 'gnus-art)
8781
8782 (run-hooks 'gnus-art-load-hook)
8783
8784 ;;; gnus-art.el ends here