2006-03-02 Hynek Schlawack <hynek@ularx.de>
[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 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 2, or (at your option)
14 ;; 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; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile
31   (require 'cl)
32   (defvar tool-bar-map)
33   (defvar w3m-minor-mode-map))
34
35 (require 'gnus)
36 ;; Avoid the "Recursive load suspected" error in Emacs 21.1.
37 (eval-and-compile
38   (let ((recursive-load-depth-limit 100))
39     (require 'gnus-sum)))
40 (require 'gnus-spec)
41 (require 'gnus-int)
42 (require 'gnus-win)
43 (require 'mm-bodies)
44 (require 'mail-parse)
45 (require 'mm-decode)
46 (require 'mm-view)
47 (require 'wid-edit)
48 (require 'mm-uu)
49 (require 'message)
50
51 (autoload 'gnus-msg-mail "gnus-msg" nil t)
52 (autoload 'gnus-button-mailto "gnus-msg")
53 (autoload 'gnus-button-reply "gnus-msg" nil t)
54 (autoload 'parse-time-string "parse-time" nil nil)
55 (autoload 'ansi-color-apply-on-region "ansi-color")
56 (autoload 'mm-url-insert-file-contents-external "mm-url")
57
58 (defgroup gnus-article nil
59   "Article display."
60   :link '(custom-manual "(gnus)Article Buffer")
61   :group 'gnus)
62
63 (defgroup gnus-article-treat nil
64   "Treating article parts."
65   :link '(custom-manual "(gnus)Article Hiding")
66   :group 'gnus-article)
67
68 (defgroup gnus-article-hiding nil
69   "Hiding article parts."
70   :link '(custom-manual "(gnus)Article Hiding")
71   :group 'gnus-article)
72
73 (defgroup gnus-article-highlight nil
74   "Article highlighting."
75   :link '(custom-manual "(gnus)Article Highlighting")
76   :group 'gnus-article
77   :group 'gnus-visual)
78
79 (defgroup gnus-article-signature nil
80   "Article signatures."
81   :link '(custom-manual "(gnus)Article Signature")
82   :group 'gnus-article)
83
84 (defgroup gnus-article-headers nil
85   "Article headers."
86   :link '(custom-manual "(gnus)Hiding Headers")
87   :group 'gnus-article)
88
89 (defgroup gnus-article-washing nil
90   "Special commands on articles."
91   :link '(custom-manual "(gnus)Article Washing")
92   :group 'gnus-article)
93
94 (defgroup gnus-article-emphasis nil
95   "Fontisizing articles."
96   :link '(custom-manual "(gnus)Article Fontisizing")
97   :group 'gnus-article)
98
99 (defgroup gnus-article-saving nil
100   "Saving articles."
101   :link '(custom-manual "(gnus)Saving Articles")
102   :group 'gnus-article)
103
104 (defgroup gnus-article-mime nil
105   "Worshiping the MIME wonder."
106   :link '(custom-manual "(gnus)Using MIME")
107   :group 'gnus-article)
108
109 (defgroup gnus-article-buttons nil
110   "Pushable buttons in the article buffer."
111   :link '(custom-manual "(gnus)Article Buttons")
112   :group 'gnus-article)
113
114 (defgroup gnus-article-various nil
115   "Other article options."
116   :link '(custom-manual "(gnus)Misc Article")
117   :group 'gnus-article)
118
119 (defcustom gnus-ignored-headers
120   (mapcar
121    (lambda (header)
122      (concat "^" header ":"))
123    '("Path" "Expires" "Date-Received" "References" "Xref" "Lines"
124      "Relay-Version" "Message-ID" "Approved" "Sender" "Received"
125      "X-UIDL" "MIME-Version" "Return-Path" "In-Reply-To"
126      "Content-Type" "Content-Transfer-Encoding" "X-WebTV-Signature"
127      "X-MimeOLE" "X-MSMail-Priority" "X-Priority" "X-Loop"
128      "X-Authentication-Warning" "X-MIME-Autoconverted" "X-Face"
129      "X-Attribution" "X-Originating-IP" "Delivered-To"
130      "NNTP-[-A-Za-z]+" "Distribution" "X-no-archive" "X-Trace"
131      "X-Complaints-To" "X-NNTP-Posting-Host" "X-Orig.*"
132      "Abuse-Reports-To" "Cache-Post-Path" "X-Article-Creation-Date"
133      "X-Poster" "X-Mail2News-Path" "X-Server-Date" "X-Cache"
134      "Originator" "X-Problems-To" "X-Auth-User" "X-Post-Time"
135      "X-Admin" "X-UID" "Resent-[-A-Za-z]+" "X-Mailing-List"
136      "Precedence" "Original-[-A-Za-z]+" "X-filename" "X-Orcpt"
137      "Old-Received" "X-Pgp" "X-Auth" "X-From-Line"
138      "X-Gnus-Article-Number" "X-Majordomo" "X-Url" "X-Sender"
139      "MBOX-Line" "Priority" "X400-[-A-Za-z]+"
140      "Status" "X-Gnus-Mail-Source" "Cancel-Lock"
141      "X-FTN" "X-EXP32-SerialNo" "Encoding" "Importance"
142      "Autoforwarded" "Original-Encoded-Information-Types" "X-Ya-Pop3"
143      "X-Face-Version" "X-Vms-To" "X-ML-NAME" "X-ML-COUNT"
144      "Mailing-List" "X-finfo" "X-md5sum" "X-md5sum-Origin"
145      "X-Sun-Charset" "X-Accept-Language" "X-Envelope-Sender"
146      "List-[A-Za-z]+" "X-Listprocessor-Version"
147      "X-Received" "X-Distribute" "X-Sequence" "X-Juno-Line-Breaks"
148      "X-Notes-Item" "X-MS-TNEF-Correlator" "x-uunet-gateway"
149      "X-Received" "Content-length" "X-precedence"
150      "X-Authenticated-User" "X-Comment" "X-Report" "X-Abuse-Info"
151      "X-HTTP-Proxy" "X-Mydeja-Info" "X-Copyright" "X-No-Markup"
152      "X-Abuse-Info" "X-From_" "X-Accept-Language" "Errors-To"
153      "X-BeenThere" "X-Mailman-Version" "List-Help" "List-Post"
154      "List-Subscribe" "List-Id" "List-Unsubscribe" "List-Archive"
155      "X-Content-length" "X-Posting-Agent" "Original-Received"
156      "X-Request-PGP" "X-Fingerprint" "X-WRIEnvto" "X-WRIEnvfrom"
157      "X-Virus-Scanned" "X-Delivery-Agent" "Posted-Date" "X-Gateway"
158      "X-Local-Origin" "X-Local-Destination" "X-UserInfo1"
159      "X-Received-Date" "X-Hashcash" "Face" "X-DMCA-Notifications"
160      "X-Abuse-and-DMCA-Info" "X-Postfilter" "X-Gpg-.*" "X-Disclaimer"))
161   "*All headers that start with this regexp will be hidden.
162 This variable can also be a list of regexps of headers to be ignored.
163 If `gnus-visible-headers' is non-nil, this variable will be ignored."
164   :type '(choice :custom-show nil
165                  regexp
166                  (repeat regexp))
167   :group 'gnus-article-hiding)
168
169 (defcustom gnus-visible-headers
170   "^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:"
171   "*All headers that do not match this regexp will be hidden.
172 This variable can also be a list of regexp of headers to remain visible.
173 If this variable is non-nil, `gnus-ignored-headers' will be ignored."
174   :type '(repeat :value-to-internal (lambda (widget value)
175                                       (custom-split-regexp-maybe value))
176                  :match (lambda (widget value)
177                           (or (stringp value)
178                               (widget-editable-list-match widget value)))
179                  regexp)
180   :group 'gnus-article-hiding)
181
182 (defcustom gnus-sorted-header-list
183   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:"
184     "^Followup-To:" "^To:" "^Cc:" "^Date:" "^Organization:")
185   "*This variable is a list of regular expressions.
186 If it is non-nil, headers that match the regular expressions will
187 be placed first in the article buffer in the sequence specified by
188 this list."
189   :type '(repeat regexp)
190   :group 'gnus-article-hiding)
191
192 (defcustom gnus-boring-article-headers '(empty followup-to reply-to)
193   "Headers that are only to be displayed if they have interesting data.
194 Possible values in this list are:
195
196   'empty       Headers with no content.
197   'newsgroups  Newsgroup identical to Gnus group.
198   'to-address  To identical to To-address.
199   'to-list     To identical to To-list.
200   'cc-list     CC identical to To-list.
201   'followup-to Followup-to identical to Newsgroups.
202   'reply-to    Reply-to identical to From.
203   'date        Date less than four days old.
204   'long-to     To and/or Cc longer than 1024 characters.
205   'many-to     Multiple To and/or Cc."
206   :type '(set (const :tag "Headers with no content." empty)
207               (const :tag "Newsgroups identical to Gnus group." newsgroups)
208               (const :tag "To identical to To-address." to-address)
209               (const :tag "To identical to To-list." to-list)
210               (const :tag "CC identical to To-list." cc-list)
211               (const :tag "Followup-to identical to Newsgroups." followup-to)
212               (const :tag "Reply-to identical to From." reply-to)
213               (const :tag "Date less than four days old." date)
214               (const :tag "To and/or Cc longer than 1024 characters." long-to)
215               (const :tag "Multiple To and/or Cc headers." many-to))
216   :group 'gnus-article-hiding)
217
218 (defcustom gnus-article-skip-boring nil
219   "Skip over text that is not worth reading.
220 By default, if you set this t, then Gnus will display citations and
221 signatures, but will never scroll down to show you a page consisting
222 only of boring text.  Boring text is controlled by
223 `gnus-article-boring-faces'."
224   :version "22.1"
225   :type 'boolean
226   :group 'gnus-article-hiding)
227
228 (defcustom gnus-signature-separator '("^-- $" "^-- *$")
229   "Regexp matching signature separator.
230 This can also be a list of regexps.  In that case, it will be checked
231 from head to tail looking for a separator.  Searches will be done from
232 the end of the buffer."
233   :type '(choice :format "%{%t%}: %[Value Menu%]\n%v"
234                  (regexp)
235                  (repeat :tag "List of regexp" regexp))
236   :group 'gnus-article-signature)
237
238 (defcustom gnus-signature-limit nil
239   "Provide a limit to what is considered a signature.
240 If it is a number, no signature may not be longer (in characters) than
241 that number.  If it is a floating point number, no signature may be
242 longer (in lines) than that number.  If it is a function, the function
243 will be called without any parameters, and if it returns nil, there is
244 no signature in the buffer.  If it is a string, it will be used as a
245 regexp.  If it matches, the text in question is not a signature.
246
247 This can also be a list of the above values."
248   :type '(choice (const nil)
249                  (integer :value 200)
250                  (number :value 4.0)
251                  (function :value fun)
252                  (regexp :value ".*"))
253   :group 'gnus-article-signature)
254
255 (defcustom gnus-hidden-properties '(invisible t intangible t)
256   "Property list to use for hiding text."
257   :type 'sexp
258   :group 'gnus-article-hiding)
259
260 ;; Fixme: This isn't the right thing for mixed graphical and non-graphical
261 ;; frames in a session.
262 (defcustom gnus-article-x-face-command
263   (if (featurep 'xemacs)
264       (if (or (gnus-image-type-available-p 'xface)
265               (gnus-image-type-available-p 'pbm))
266           'gnus-display-x-face-in-from
267         "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | ee -")
268     (if (gnus-image-type-available-p 'pbm)
269         'gnus-display-x-face-in-from
270       "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | \
271 display -"))
272   "*String or function to be executed to display an X-Face header.
273 If it is a string, the command will be executed in a sub-shell
274 asynchronously.  The compressed face will be piped to this command."
275   :type `(choice string
276                  (function-item gnus-display-x-face-in-from)
277                  function)
278   :version "21.1"
279   :group 'gnus-picon
280   :group 'gnus-article-washing)
281
282 (defcustom gnus-article-x-face-too-ugly nil
283   "Regexp matching posters whose face shouldn't be shown automatically."
284   :type '(choice regexp (const nil))
285   :group 'gnus-article-washing)
286
287 (defcustom gnus-article-banner-alist nil
288   "Banner alist for stripping.
289 For example,
290      ((egroups . \"^[ \\t\\n]*-------------------+\\\\( \\\\(e\\\\|Yahoo! \\\\)Groups Sponsor -+\\\\)?....\\n\\\\(.+\\n\\\\)+\"))"
291   :version "21.1"
292   :type '(repeat (cons symbol regexp))
293   :group 'gnus-article-washing)
294
295 (gnus-define-group-parameter
296  banner
297  :variable-document
298  "Alist of regexps (to match group names) and banner."
299  :variable-group gnus-article-washing
300  :parameter-type
301  '(choice :tag "Banner"
302           :value nil
303           (const :tag "Remove signature" signature)
304           (symbol :tag "Item in `gnus-article-banner-alist'" none)
305           regexp
306           (const :tag "None" nil))
307  :parameter-document
308  "If non-nil, specify how to remove `banners' from articles.
309
310 Symbol `signature' means to remove signatures delimited by
311 `gnus-signature-separator'.  Any other symbol is used to look up a
312 regular expression to match the banner in `gnus-article-banner-alist'.
313 A string is used as a regular expression to match the banner
314 directly.")
315
316 (defcustom gnus-article-address-banner-alist nil
317   "Alist of mail addresses and banners.
318 Each element has the form (ADDRESS . BANNER), where ADDRESS is a regexp
319 to match a mail address in the From: header, BANNER is one of a symbol
320 `signature', an item in `gnus-article-banner-alist', a regexp and nil.
321 If ADDRESS matches author's mail address, it will remove things like
322 advertisements.  For example:
323
324 \((\"@yoo-hoo\\\\.co\\\\.jp\\\\'\" . \"\\n_+\\nDo You Yoo-hoo!\\\\?\\n.*\\n.*\\n\"))
325 "
326   :type '(repeat
327           (cons
328            (regexp :tag "Address")
329            (choice :tag "Banner" :value nil
330                    (const :tag "Remove signature" signature)
331                    (symbol :tag "Item in `gnus-article-banner-alist'" none)
332                    regexp
333                    (const :tag "None" nil))))
334   :version "22.1"
335   :group 'gnus-article-washing)
336
337 (defmacro gnus-emphasis-custom-with-format (&rest body)
338   `(let ((format "\
339 \\(\\s-\\|^\\|\\=\\|[-\"]\\|\\s(\\)\\(%s\\(\\w+\\(\\s-+\\w+\\)*[.,]?\\)%s\\)\
340 \\(\\([-,.;:!?\"]\\|\\s)\\)+\\s-\\|[?!.]\\s-\\|\\s)\\|\\s-\\)"))
341      ,@body))
342
343 (defun gnus-emphasis-custom-value-to-external (value)
344   (gnus-emphasis-custom-with-format
345    (if (consp (car value))
346        (list (format format (car (car value)) (cdr (car value)))
347              2
348              (if (nth 1 value) 2 3)
349              (nth 2 value))
350      value)))
351
352 (defun gnus-emphasis-custom-value-to-internal (value)
353   (gnus-emphasis-custom-with-format
354    (let ((regexp (concat "\\`"
355                          (format (regexp-quote format)
356                                  "\\([^()]+\\)" "\\([^()]+\\)")
357                          "\\'"))
358          pattern)
359      (if (string-match regexp (setq pattern (car value)))
360          (list (cons (match-string 1 pattern) (match-string 2 pattern))
361                (= (nth 2 value) 2)
362                (nth 3 value))
363        value))))
364
365 (defcustom gnus-emphasis-alist
366   (let ((types
367          '(("\\*" "\\*" bold nil 2)
368            ("_" "_" underline)
369            ("/" "/" italic)
370            ("_/" "/_" underline-italic)
371            ("_\\*" "\\*_" underline-bold)
372            ("\\*/" "/\\*" bold-italic)
373            ("_\\*/" "/\\*_" underline-bold-italic))))
374     (nconc
375      (gnus-emphasis-custom-with-format
376       (mapcar (lambda (spec)
377                 (list (format format (car spec) (cadr spec))
378                       (or (nth 3 spec) 2)
379                       (or (nth 4 spec) 3)
380                       (intern (format "gnus-emphasis-%s" (nth 2 spec)))))
381               types))
382      '(;; I've never seen anyone use this strikethru convention whereas I've
383        ;; several times seen it triggered by normal text.  --Stef
384        ;; Miles suggests that this form is sometimes used but for italics,
385        ;; so maybe we should map it to `italic'.
386        ;; ("\\(\\s-\\|^\\)\\(-\\(\\(\\w\\|-[^-]\\)+\\)-\\)\\(\\s-\\|[?!.,;]\\)"
387        ;; 2 3 gnus-emphasis-strikethru)
388        ("\\(\\s-\\|^\\)\\(_\\(\\(\\w\\|_[^_]\\)+\\)_\\)\\(\\s-\\|[?!.,;]\\)"
389         2 3 gnus-emphasis-underline))))
390   "*Alist that says how to fontify certain phrases.
391 Each item looks like this:
392
393   (\"_\\\\(\\\\w+\\\\)_\" 0 1 'underline)
394
395 The first element is a regular expression to be matched.  The second
396 is a number that says what regular expression grouping used to find
397 the entire emphasized word.  The third is a number that says what
398 regexp grouping should be displayed and highlighted.  The fourth
399 is the face used for highlighting."
400   :type
401   '(repeat
402     (menu-choice
403      :format "%[Customizing Style%]\n%v"
404      :indent 2
405      (group :tag "Default"
406             :value ("" 0 0 default)
407             :value-create
408             (lambda (widget)
409               (let ((value (widget-get
410                             (cadr (widget-get (widget-get widget :parent)
411                                               :args))
412                             :value)))
413                 (if (not (eq (nth 2 value) 'default))
414                     (widget-put
415                      widget
416                      :value
417                      (gnus-emphasis-custom-value-to-external value))))
418               (widget-group-value-create widget))
419             regexp
420             (integer :format "Match group: %v")
421             (integer :format "Emphasize group: %v")
422             face)
423      (group :tag "Simple"
424             :value (("_" . "_") nil default)
425             (cons :format "%v"
426                   (regexp :format "Start regexp: %v")
427                   (regexp :format "End regexp: %v"))
428             (boolean :format "Show start and end patterns: %[%v%]\n"
429                      :on " On " :off " Off ")
430             face)))
431   :get (lambda (symbol)
432          (mapcar 'gnus-emphasis-custom-value-to-internal
433                  (default-value symbol)))
434   :set (lambda (symbol value)
435          (set-default symbol (mapcar 'gnus-emphasis-custom-value-to-external
436                                      value)))
437   :group 'gnus-article-emphasis)
438
439 (defcustom gnus-emphasize-whitespace-regexp "^[ \t]+\\|[ \t]*\n"
440   "A regexp to describe whitespace which should not be emphasized.
441 Typical values are \"^[ \\t]+\\\\|[ \\t]*\\n\" and \"[ \\t]+\\\\|[ \\t]*\\n\".
442 The former avoids underlining of leading and trailing whitespace,
443 and the latter avoids underlining any whitespace at all."
444   :version "21.1"
445   :group 'gnus-article-emphasis
446   :type 'regexp)
447
448 (defface gnus-emphasis-bold '((t (:bold t)))
449   "Face used for displaying strong emphasized text (*word*)."
450   :group 'gnus-article-emphasis)
451
452 (defface gnus-emphasis-italic '((t (:italic t)))
453   "Face used for displaying italic emphasized text (/word/)."
454   :group 'gnus-article-emphasis)
455
456 (defface gnus-emphasis-underline '((t (:underline t)))
457   "Face used for displaying underlined emphasized text (_word_)."
458   :group 'gnus-article-emphasis)
459
460 (defface gnus-emphasis-underline-bold '((t (:bold t :underline t)))
461   "Face used for displaying underlined bold emphasized text (_*word*_)."
462   :group 'gnus-article-emphasis)
463
464 (defface gnus-emphasis-underline-italic '((t (:italic t :underline t)))
465   "Face used for displaying underlined italic emphasized text (_/word/_)."
466   :group 'gnus-article-emphasis)
467
468 (defface gnus-emphasis-bold-italic '((t (:bold t :italic t)))
469   "Face used for displaying bold italic emphasized text (/*word*/)."
470   :group 'gnus-article-emphasis)
471
472 (defface gnus-emphasis-underline-bold-italic
473   '((t (:bold t :italic t :underline t)))
474   "Face used for displaying underlined bold italic emphasized text.
475 Example: (_/*word*/_)."
476   :group 'gnus-article-emphasis)
477
478 (defface gnus-emphasis-strikethru (if (featurep 'xemacs)
479                                       '((t (:strikethru t)))
480                                     '((t (:strike-through t))))
481   "Face used for displaying strike-through text (-word-)."
482   :group 'gnus-article-emphasis)
483
484 (defface gnus-emphasis-highlight-words
485   '((t (:background "black" :foreground "yellow")))
486   "Face used for displaying highlighted words."
487   :group 'gnus-article-emphasis)
488
489 (defcustom gnus-article-time-format "%a, %d %b %Y %T %Z"
490   "Format for display of Date headers in article bodies.
491 See `format-time-string' for the possible values.
492
493 The variable can also be function, which should return a complete Date
494 header.  The function is called with one argument, the time, which can
495 be fed to `format-time-string'."
496   :type '(choice string function)
497   :link '(custom-manual "(gnus)Article Date")
498   :group 'gnus-article-washing)
499
500 (defcustom gnus-save-all-headers t
501   "*If non-nil, don't remove any headers before saving."
502   :group 'gnus-article-saving
503   :type 'boolean)
504
505 (defcustom gnus-prompt-before-saving 'always
506   "*This variable says how much prompting is to be done when saving articles.
507 If it is nil, no prompting will be done, and the articles will be
508 saved to the default files.  If this variable is `always', each and
509 every article that is saved will be preceded by a prompt, even when
510 saving large batches of articles.  If this variable is neither nil not
511 `always', there the user will be prompted once for a file name for
512 each invocation of the saving commands."
513   :group 'gnus-article-saving
514   :type '(choice (item always)
515                  (item :tag "never" nil)
516                  (sexp :tag "once" :format "%t\n" :value t)))
517
518 (defcustom gnus-saved-headers gnus-visible-headers
519   "Headers to keep if `gnus-save-all-headers' is nil.
520 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
521 If that variable is nil, however, all headers that match this regexp
522 will be kept while the rest will be deleted before saving."
523   :group 'gnus-article-saving
524   :type 'regexp)
525
526 (defcustom gnus-default-article-saver 'gnus-summary-save-in-rmail
527   "A function to save articles in your favourite format.
528 The function must be interactively callable (in other words, it must
529 be an Emacs command).
530
531 Gnus provides the following functions:
532
533 * gnus-summary-save-in-rmail (Rmail format)
534 * gnus-summary-save-in-mail (Unix mail format)
535 * gnus-summary-save-in-folder (MH folder)
536 * gnus-summary-save-in-file (article format)
537 * gnus-summary-save-body-in-file (article body)
538 * gnus-summary-save-in-vm (use VM's folder format)
539 * gnus-summary-write-to-file (article format -- overwrite)."
540   :group 'gnus-article-saving
541   :type '(radio (function-item gnus-summary-save-in-rmail)
542                 (function-item gnus-summary-save-in-mail)
543                 (function-item gnus-summary-save-in-folder)
544                 (function-item gnus-summary-save-in-file)
545                 (function-item gnus-summary-save-body-in-file)
546                 (function-item gnus-summary-save-in-vm)
547                 (function-item gnus-summary-write-to-file)
548                 (function)))
549
550 (defcustom gnus-rmail-save-name 'gnus-plain-save-name
551   "A function generating a file name to save articles in Rmail format.
552 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
553   :group 'gnus-article-saving
554   :type 'function)
555
556 (defcustom gnus-mail-save-name 'gnus-plain-save-name
557   "A function generating a file name to save articles in Unix mail format.
558 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
559   :group 'gnus-article-saving
560   :type 'function)
561
562 (defcustom gnus-folder-save-name 'gnus-folder-save-name
563   "A function generating a file name to save articles in MH folder.
564 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER."
565   :group 'gnus-article-saving
566   :type 'function)
567
568 (defcustom gnus-file-save-name 'gnus-numeric-save-name
569   "A function generating a file name to save articles in article format.
570 The function is called with NEWSGROUP, HEADERS, and optional
571 LAST-FILE."
572   :group 'gnus-article-saving
573   :type 'function)
574
575 (defcustom gnus-split-methods
576   '((gnus-article-archive-name)
577     (gnus-article-nndoc-name))
578   "*Variable used to suggest where articles are to be saved.
579 For instance, if you would like to save articles related to Gnus in
580 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
581 you could set this variable to something like:
582
583  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
584    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
585
586 This variable is an alist where the key is the match and the
587 value is a list of possible files to save in if the match is
588 non-nil.
589
590 If the match is a string, it is used as a regexp match on the
591 article.  If the match is a symbol, that symbol will be funcalled
592 from the buffer of the article to be saved with the newsgroup as
593 the parameter.  If it is a list, it will be evaled in the same
594 buffer.
595
596 If this form or function returns a string, this string will be
597 used as a possible file name; and if it returns a non-nil list,
598 that list will be used as possible file names."
599   :group 'gnus-article-saving
600   :type '(repeat (choice (list :value (fun) function)
601                          (cons :value ("" "") regexp (repeat string))
602                          (sexp :value nil))))
603
604 (defcustom gnus-page-delimiter "^\^L"
605   "*Regexp describing what to use as article page delimiters.
606 The default value is \"^\^L\", which is a form linefeed at the
607 beginning of a line."
608   :type 'regexp
609   :group 'gnus-article-various)
610
611 (defcustom gnus-article-mode-line-format "Gnus: %g [%w] %S%m"
612   "*The format specification for the article mode line.
613 See `gnus-summary-mode-line-format' for a closer description.
614
615 The following additional specs are available:
616
617 %w  The article washing status.
618 %m  The number of MIME parts in the article."
619   :type 'string
620   :group 'gnus-article-various)
621
622 (defcustom gnus-article-mode-hook nil
623   "*A hook for Gnus article mode."
624   :type 'hook
625   :group 'gnus-article-various)
626
627 (when (featurep 'xemacs)
628   ;; Extracted from gnus-xmas-define in order to preserve user settings
629   (when (fboundp 'turn-off-scroll-in-place)
630     (add-hook 'gnus-article-mode-hook 'turn-off-scroll-in-place))
631   ;; Extracted from gnus-xmas-redefine in order to preserve user settings
632   (add-hook 'gnus-article-mode-hook 'gnus-xmas-article-menu-add))
633
634 (defcustom gnus-article-menu-hook nil
635   "*Hook run after the creation of the article mode menu."
636   :type 'hook
637   :group 'gnus-article-various)
638
639 (defcustom gnus-article-prepare-hook nil
640   "*A hook called after an article has been prepared in the article buffer."
641   :type 'hook
642   :group 'gnus-article-various)
643
644 (defcustom gnus-copy-article-ignored-headers nil
645   "List of headers to be removed when copying an article.
646 Each element is a regular expression."
647   :version "23.0" ;; No Gnus
648   :type '(repeat regexp)
649   :group 'gnus-article-various)
650
651 (make-obsolete-variable 'gnus-article-hide-pgp-hook
652                         "This variable is obsolete in Gnus 5.10.")
653
654 (defface gnus-button
655   '((t (:weight bold)))
656   "Face used for highlighting a button in the article buffer."
657   :group 'gnus-article-buttons)
658
659 (defcustom gnus-article-button-face 'gnus-button
660   "Face used for highlighting buttons in the article buffer.
661
662 An article button is a piece of text that you can activate by pressing
663 `RET' or `mouse-2' above it."
664   :type 'face
665   :group 'gnus-article-buttons)
666
667 (defcustom gnus-article-mouse-face 'highlight
668   "Face used for mouse highlighting in the article buffer.
669
670 Article buttons will be displayed in this face when the cursor is
671 above them."
672   :type 'face
673   :group 'gnus-article-buttons)
674
675 (defcustom gnus-signature-face 'gnus-signature
676   "Face used for highlighting a signature in the article buffer.
677 Obsolete; use the face `gnus-signature' for customizations instead."
678   :type 'face
679   :group 'gnus-article-highlight
680   :group 'gnus-article-signature)
681
682 (defface gnus-signature
683   '((t
684      (:italic t)))
685   "Face used for highlighting a signature in the article buffer."
686   :group 'gnus-article-highlight
687   :group 'gnus-article-signature)
688 ;; backward-compatibility alias
689 (put 'gnus-signature-face 'face-alias 'gnus-signature)
690
691 (defface gnus-header-from
692   '((((class color)
693       (background dark))
694      (:foreground "spring green"))
695     (((class color)
696       (background light))
697      (:foreground "red3"))
698     (t
699      (:italic t)))
700   "Face used for displaying from headers."
701   :group 'gnus-article-headers
702   :group 'gnus-article-highlight)
703 ;; backward-compatibility alias
704 (put 'gnus-header-from-face 'face-alias 'gnus-header-from)
705
706 (defface gnus-header-subject
707   '((((class color)
708       (background dark))
709      (:foreground "SeaGreen3"))
710     (((class color)
711       (background light))
712      (:foreground "red4"))
713     (t
714      (:bold t :italic t)))
715   "Face used for displaying subject headers."
716   :group 'gnus-article-headers
717   :group 'gnus-article-highlight)
718 ;; backward-compatibility alias
719 (put 'gnus-header-subject-face 'face-alias 'gnus-header-subject)
720
721 (defface gnus-header-newsgroups
722   '((((class color)
723       (background dark))
724      (:foreground "yellow" :italic t))
725     (((class color)
726       (background light))
727      (:foreground "MidnightBlue" :italic t))
728     (t
729      (:italic t)))
730   "Face used for displaying newsgroups headers.
731 In the default setup this face is only used for crossposted
732 articles."
733   :group 'gnus-article-headers
734   :group 'gnus-article-highlight)
735 ;; backward-compatibility alias
736 (put 'gnus-header-newsgroups-face 'face-alias 'gnus-header-newsgroups)
737
738 (defface gnus-header-name
739   '((((class color)
740       (background dark))
741      (:foreground "SeaGreen"))
742     (((class color)
743       (background light))
744      (:foreground "maroon"))
745     (t
746      (:bold t)))
747   "Face used for displaying header names."
748   :group 'gnus-article-headers
749   :group 'gnus-article-highlight)
750 ;; backward-compatibility alias
751 (put 'gnus-header-name-face 'face-alias 'gnus-header-name)
752
753 (defface gnus-header-content
754   '((((class color)
755       (background dark))
756      (:foreground "forest green" :italic t))
757     (((class color)
758       (background light))
759      (:foreground "indianred4" :italic t))
760     (t
761      (:italic t)))  "Face used for displaying header content."
762   :group 'gnus-article-headers
763   :group 'gnus-article-highlight)
764 ;; backward-compatibility alias
765 (put 'gnus-header-content-face 'face-alias 'gnus-header-content)
766
767 (defcustom gnus-header-face-alist
768   '(("From" nil gnus-header-from)
769     ("Subject" nil gnus-header-subject)
770     ("Newsgroups:.*," nil gnus-header-newsgroups)
771     ("" gnus-header-name gnus-header-content))
772   "*Controls highlighting of article headers.
773
774 An alist of the form (HEADER NAME CONTENT).
775
776 HEADER is a regular expression which should match the name of a
777 header and NAME and CONTENT are either face names or nil.
778
779 The name of each header field will be displayed using the face
780 specified by the first element in the list where HEADER matches
781 the header name and NAME is non-nil.  Similarly, the content will
782 be displayed by the first non-nil matching CONTENT face."
783   :group 'gnus-article-headers
784   :group 'gnus-article-highlight
785   :type '(repeat (list (regexp :tag "Header")
786                        (choice :tag "Name"
787                                (item :tag "skip" nil)
788                                (face :value default))
789                        (choice :tag "Content"
790                                (item :tag "skip" nil)
791                                (face :value default)))))
792
793 (defcustom gnus-article-decode-hook
794   '(article-decode-charset article-decode-encoded-words
795                            article-decode-group-name article-decode-idna-rhs)
796   "*Hook run to decode charsets in articles."
797   :group 'gnus-article-headers
798   :type 'hook)
799
800 (defcustom gnus-display-mime-function 'gnus-display-mime
801   "Function to display MIME articles."
802   :group 'gnus-article-mime
803   :type 'function)
804
805 (defvar gnus-decode-header-function 'mail-decode-encoded-word-region
806   "Function used to decode headers.")
807
808 (defvar gnus-article-dumbquotes-map
809   '(("\200" "EUR")
810     ("\202" ",")
811     ("\203" "f")
812     ("\204" ",,")
813     ("\205" "...")
814     ("\213" "<")
815     ("\214" "OE")
816     ("\221" "`")
817     ("\222" "'")
818     ("\223" "``")
819     ("\224" "\"")
820     ("\225" "*")
821     ("\226" "-")
822     ("\227" "--")
823     ("\230" "~")
824     ("\231" "(TM)")
825     ("\233" ">")
826     ("\234" "oe")
827     ("\264" "'"))
828   "Table for MS-to-Latin1 translation.")
829
830 (defcustom gnus-ignored-mime-types nil
831   "List of MIME types that should be ignored by Gnus."
832   :version "21.1"
833   :group 'gnus-article-mime
834   :type '(repeat regexp))
835
836 (defcustom gnus-unbuttonized-mime-types '(".*/.*")
837   "List of MIME types that should not be given buttons when rendered inline.
838 See also `gnus-buttonized-mime-types' which may override this variable.
839 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
840   :version "21.1"
841   :group 'gnus-article-mime
842   :type '(repeat regexp))
843
844 (defcustom gnus-buttonized-mime-types nil
845   "List of MIME types that should be given buttons when rendered inline.
846 If set, this variable overrides `gnus-unbuttonized-mime-types'.
847 To see e.g. security buttons you could set this to
848 `(\"multipart/signed\")'.  You could also add \"multipart/alternative\" to
849 this list to display radio buttons that allow you to choose one of two
850 media types those mails include.  See also `mm-discouraged-alternatives'.
851 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
852   :version "22.1"
853   :group 'gnus-article-mime
854   :type '(repeat regexp))
855
856 (defcustom gnus-inhibit-mime-unbuttonizing nil
857   "If non-nil, all MIME parts get buttons.
858 When nil (the default value), then some MIME parts do not get buttons,
859 as described by the variables `gnus-buttonized-mime-types' and
860 `gnus-unbuttonized-mime-types'."
861   :version "22.1"
862   :group 'gnus-article-mime
863   :type 'boolean)
864
865 (defcustom gnus-body-boundary-delimiter "_"
866   "String used to delimit header and body.
867 This variable is used by `gnus-article-treat-body-boundary' which can
868 be controlled by `gnus-treat-body-boundary'."
869   :version "22.1"
870   :group 'gnus-article-various
871   :type '(choice (item :tag "None" :value nil)
872                  string))
873
874 (defcustom gnus-picon-databases '("/usr/lib/picon" "/usr/local/faces"
875                                   "/usr/share/picons")
876   "Defines the location of the faces database.
877 For information on obtaining this database of pretty pictures, please
878 see http://www.cs.indiana.edu/picons/ftp/index.html"
879   :version "22.1"
880   :type '(repeat directory)
881   :link '(url-link :tag "download"
882                    "http://www.cs.indiana.edu/picons/ftp/index.html")
883   :link '(custom-manual "(gnus)Picons")
884   :group 'gnus-picon)
885
886 (defun gnus-picons-installed-p ()
887   "Say whether picons are installed on your machine."
888   (let ((installed nil))
889     (dolist (database gnus-picon-databases)
890       (when (file-exists-p database)
891         (setq installed t)))
892     installed))
893
894 (defcustom gnus-article-mime-part-function nil
895   "Function called with a MIME handle as the argument.
896 This is meant for people who want to do something automatic based
897 on parts -- for instance, adding Vcard info to a database."
898   :group 'gnus-article-mime
899   :type '(choice (const nil)
900                  function))
901
902 (defcustom gnus-mime-multipart-functions nil
903   "An alist of MIME types to functions to display them."
904   :version "21.1"
905   :group 'gnus-article-mime
906   :type '(repeat (cons :format "%v" (string :tag "MIME type") function)))
907
908 (defcustom gnus-article-date-lapsed-new-header nil
909   "Whether the X-Sent and Date headers can coexist.
910 When using `gnus-treat-date-lapsed', the \"X-Sent:\" header will
911 either replace the old \"Date:\" header (if this variable is nil), or
912 be added below it (otherwise)."
913   :version "21.1"
914   :group 'gnus-article-headers
915   :type 'boolean)
916
917 (defcustom gnus-article-mime-match-handle-function 'undisplayed-alternative
918   "Function called with a MIME handle as the argument.
919 This is meant for people who want to view first matched part.
920 For `undisplayed-alternative' (default), the first undisplayed
921 part or alternative part is used.  For `undisplayed', the first
922 undisplayed part is used.  For a function, the first part which
923 the function return t is used.  For nil, the first part is
924 used."
925   :version "21.1"
926   :group 'gnus-article-mime
927   :type '(choice
928           (item :tag "first" :value nil)
929           (item :tag "undisplayed" :value undisplayed)
930           (item :tag "undisplayed or alternative"
931                 :value undisplayed-alternative)
932           (function)))
933
934 (defcustom gnus-mime-action-alist
935   '(("save to file" . gnus-mime-save-part)
936     ("save and strip" . gnus-mime-save-part-and-strip)
937     ("replace with file" . gnus-mime-replace-part)
938     ("delete part" . gnus-mime-delete-part)
939     ("display as text" . gnus-mime-inline-part)
940     ("view the part" . gnus-mime-view-part)
941     ("pipe to command" . gnus-mime-pipe-part)
942     ("toggle display" . gnus-article-press-button)
943     ("toggle display" . gnus-article-view-part-as-charset)
944     ("view as type" . gnus-mime-view-part-as-type)
945     ("view internally" . gnus-mime-view-part-internally)
946     ("view externally" . gnus-mime-view-part-externally))
947   "An alist of actions that run on the MIME attachment."
948   :group 'gnus-article-mime
949   :type '(repeat (cons (string :tag "name")
950                        (function))))
951
952 (defcustom gnus-auto-select-part 1
953   "Advance to next MIME part when deleting or stripping parts.
954
955 When 0, point will be placed on the same part as before.  When
956 positive (negative), move point forward (backwards) this many
957 parts.  When nil, redisplay article."
958   :version "23.0" ;; No Gnus
959   :group 'gnus-article-mime
960   :type '(choice (const nil :tag "Redisplay article.")
961                  (const 1 :tag "Next part.")
962                  (const 0 :tag "Current part.")
963                  integer))
964
965 ;;;
966 ;;; The treatment variables
967 ;;;
968
969 (defvar gnus-part-display-hook nil
970   "Hook called on parts that are to receive treatment.")
971
972 (defvar gnus-article-treat-custom
973   '(choice (const :tag "Off" nil)
974            (const :tag "On" t)
975            (const :tag "Header" head)
976            (const :tag "First" first)
977            (const :tag "Last" last)
978            (integer :tag "Less")
979            (repeat :tag "Groups" regexp)
980            (sexp :tag "Predicate")))
981
982 (defvar gnus-article-treat-head-custom
983   '(choice (const :tag "Off" nil)
984            (const :tag "Header" head)))
985
986 (defvar gnus-article-treat-types '("text/plain")
987   "Parts to treat.")
988
989 (defvar gnus-inhibit-treatment nil
990   "Whether to inhibit treatment.")
991
992 (defcustom gnus-treat-highlight-signature '(or t (typep "text/x-vcard"))
993   "Highlight the signature.
994 Valid values are nil, t, `head', `first', `last', an integer or a
995 predicate.  See Info node `(gnus)Customizing Articles'."
996   :group 'gnus-article-treat
997   :link '(custom-manual "(gnus)Customizing Articles")
998   :type gnus-article-treat-custom)
999 (put 'gnus-treat-highlight-signature 'highlight t)
1000
1001 (defcustom gnus-treat-buttonize 100000
1002   "Add buttons.
1003 Valid values are nil, t, `head', `first', `last', an integer or a
1004 predicate.  See Info node `(gnus)Customizing Articles'."
1005   :group 'gnus-article-treat
1006   :link '(custom-manual "(gnus)Customizing Articles")
1007   :type gnus-article-treat-custom)
1008 (put 'gnus-treat-buttonize 'highlight t)
1009
1010 (defcustom gnus-treat-buttonize-head 'head
1011   "Add buttons to the head.
1012 Valid values are nil, t, `head', `first', `last', an integer or a
1013 predicate.  See Info node `(gnus)Customizing Articles'."
1014   :group 'gnus-article-treat
1015   :link '(custom-manual "(gnus)Customizing Articles")
1016   :type gnus-article-treat-head-custom)
1017 (put 'gnus-treat-buttonize-head 'highlight t)
1018
1019 (defcustom gnus-treat-emphasize
1020   (and (or window-system
1021            (featurep 'xemacs))
1022        50000)
1023   "Emphasize text.
1024 Valid values are nil, t, `head', `first', `last', an integer or a
1025 predicate.  See Info node `(gnus)Customizing Articles'."
1026   :group 'gnus-article-treat
1027   :link '(custom-manual "(gnus)Customizing Articles")
1028   :type gnus-article-treat-custom)
1029 (put 'gnus-treat-emphasize 'highlight t)
1030
1031 (defcustom gnus-treat-strip-cr nil
1032   "Remove carriage returns.
1033 Valid values are nil, t, `head', `first', `last', an integer or a
1034 predicate.  See Info node `(gnus)Customizing Articles'."
1035   :version "22.1"
1036   :group 'gnus-article-treat
1037   :link '(custom-manual "(gnus)Customizing Articles")
1038   :type gnus-article-treat-custom)
1039
1040 (defcustom gnus-treat-unsplit-urls nil
1041   "Remove newlines from within URLs.
1042 Valid values are nil, t, `head', `first', `last', an integer or a
1043 predicate.  See Info node `(gnus)Customizing Articles'."
1044   :version "22.1"
1045   :group 'gnus-article-treat
1046   :link '(custom-manual "(gnus)Customizing Articles")
1047   :type gnus-article-treat-custom)
1048
1049 (defcustom gnus-treat-leading-whitespace nil
1050   "Remove leading whitespace in headers.
1051 Valid values are nil, t, `head', `first', `last', an integer or a
1052 predicate.  See Info node `(gnus)Customizing Articles'."
1053   :version "22.1"
1054   :group 'gnus-article-treat
1055   :link '(custom-manual "(gnus)Customizing Articles")
1056   :type gnus-article-treat-custom)
1057
1058 (defcustom gnus-treat-hide-headers 'head
1059   "Hide headers.
1060 Valid values are nil, t, `head', `first', `last', an integer or a
1061 predicate.  See Info node `(gnus)Customizing Articles'."
1062   :group 'gnus-article-treat
1063   :link '(custom-manual "(gnus)Customizing Articles")
1064   :type gnus-article-treat-head-custom)
1065
1066 (defcustom gnus-treat-hide-boring-headers nil
1067   "Hide boring headers.
1068 Valid values are nil, t, `head', `first', `last', an integer or a
1069 predicate.  See Info node `(gnus)Customizing Articles'."
1070   :group 'gnus-article-treat
1071   :link '(custom-manual "(gnus)Customizing Articles")
1072   :type gnus-article-treat-head-custom)
1073
1074 (defcustom gnus-treat-hide-signature nil
1075   "Hide the signature.
1076 Valid values are nil, t, `head', `first', `last', an integer or a
1077 predicate.  See Info node `(gnus)Customizing Articles'."
1078   :group 'gnus-article-treat
1079   :link '(custom-manual "(gnus)Customizing Articles")
1080   :type gnus-article-treat-custom)
1081
1082 (defcustom gnus-treat-fill-article nil
1083   "Fill the article.
1084 Valid values are nil, t, `head', `first', `last', an integer or a
1085 predicate.  See Info node `(gnus)Customizing Articles'."
1086   :group 'gnus-article-treat
1087   :link '(custom-manual "(gnus)Customizing Articles")
1088   :type gnus-article-treat-custom)
1089
1090 (defcustom gnus-treat-hide-citation nil
1091   "Hide cited text.
1092 Valid values are nil, t, `head', `first', `last', an integer or a
1093 predicate.  See Info node `(gnus)Customizing Articles'."
1094   :group 'gnus-article-treat
1095   :link '(custom-manual "(gnus)Customizing Articles")
1096   :type gnus-article-treat-custom)
1097
1098 (defcustom gnus-treat-hide-citation-maybe nil
1099   "Hide cited text.
1100 Valid values are nil, t, `head', `first', `last', an integer or a
1101 predicate.  See Info node `(gnus)Customizing Articles'."
1102   :group 'gnus-article-treat
1103   :link '(custom-manual "(gnus)Customizing Articles")
1104   :type gnus-article-treat-custom)
1105
1106 (defcustom gnus-treat-strip-list-identifiers 'head
1107   "Strip list identifiers from `gnus-list-identifiers`.
1108 Valid values are nil, t, `head', `first', `last', an integer or a
1109 predicate.  See Info node `(gnus)Customizing Articles'."
1110   :version "21.1"
1111   :group 'gnus-article-treat
1112   :link '(custom-manual "(gnus)Customizing Articles")
1113   :type gnus-article-treat-custom)
1114
1115 (make-obsolete-variable 'gnus-treat-strip-pgp
1116                         "This option is obsolete in Gnus 5.10.")
1117
1118 (defcustom gnus-treat-strip-pem nil
1119   "Strip PEM signatures.
1120 Valid values are nil, t, `head', `first', `last', an integer or a
1121 predicate.  See Info node `(gnus)Customizing Articles'."
1122   :group 'gnus-article-treat
1123   :link '(custom-manual "(gnus)Customizing Articles")
1124   :type gnus-article-treat-custom)
1125
1126 (defcustom gnus-treat-strip-banner t
1127   "Strip banners from articles.
1128 The banner to be stripped is specified in the `banner' group parameter.
1129 Valid values are nil, t, `head', `first', `last', an integer or a
1130 predicate.  See Info node `(gnus)Customizing Articles'."
1131   :group 'gnus-article-treat
1132   :link '(custom-manual "(gnus)Customizing Articles")
1133   :type gnus-article-treat-custom)
1134
1135 (defcustom gnus-treat-highlight-headers 'head
1136   "Highlight the headers.
1137 Valid values are nil, t, `head', `first', `last', an integer or a
1138 predicate.  See Info node `(gnus)Customizing Articles'."
1139   :group 'gnus-article-treat
1140   :link '(custom-manual "(gnus)Customizing Articles")
1141   :type gnus-article-treat-head-custom)
1142 (put 'gnus-treat-highlight-headers 'highlight t)
1143
1144 (defcustom gnus-treat-highlight-citation t
1145   "Highlight cited text.
1146 Valid values are nil, t, `head', `first', `last', an integer or a
1147 predicate.  See Info node `(gnus)Customizing Articles'."
1148   :group 'gnus-article-treat
1149   :link '(custom-manual "(gnus)Customizing Articles")
1150   :type gnus-article-treat-custom)
1151 (put 'gnus-treat-highlight-citation 'highlight t)
1152
1153 (defcustom gnus-treat-date-ut nil
1154   "Display the Date in UT (GMT).
1155 Valid values are nil, t, `head', `first', `last', an integer or a
1156 predicate.  See Info node `(gnus)Customizing Articles'."
1157   :group 'gnus-article-treat
1158   :link '(custom-manual "(gnus)Customizing Articles")
1159   :type gnus-article-treat-head-custom)
1160
1161 (defcustom gnus-treat-date-local nil
1162   "Display the Date in the local timezone.
1163 Valid values are nil, t, `head', `first', `last', an integer or a
1164 predicate.  See Info node `(gnus)Customizing Articles'."
1165   :group 'gnus-article-treat
1166   :link '(custom-manual "(gnus)Customizing Articles")
1167   :type gnus-article-treat-head-custom)
1168
1169 (defcustom gnus-treat-date-english nil
1170   "Display the Date in a format that can be read aloud in English.
1171 Valid values are nil, t, `head', `first', `last', an integer or a
1172 predicate.  See Info node `(gnus)Customizing Articles'."
1173   :version "22.1"
1174   :group 'gnus-article-treat
1175   :link '(custom-manual "(gnus)Customizing Articles")
1176   :type gnus-article-treat-head-custom)
1177
1178 (defcustom gnus-treat-date-lapsed nil
1179   "Display the Date header in a way that says how much time has elapsed.
1180 Valid values are nil, t, `head', `first', `last', an integer or a
1181 predicate.  See Info node `(gnus)Customizing Articles'."
1182   :group 'gnus-article-treat
1183   :link '(custom-manual "(gnus)Customizing Articles")
1184   :type gnus-article-treat-head-custom)
1185
1186 (defcustom gnus-treat-date-original nil
1187   "Display the date in the original timezone.
1188 Valid values are nil, t, `head', `first', `last', an integer or a
1189 predicate.  See Info node `(gnus)Customizing Articles'."
1190   :group 'gnus-article-treat
1191   :link '(custom-manual "(gnus)Customizing Articles")
1192   :type gnus-article-treat-head-custom)
1193
1194 (defcustom gnus-treat-date-iso8601 nil
1195   "Display the date in the ISO8601 format.
1196 Valid values are nil, t, `head', `first', `last', an integer or a
1197 predicate.  See Info node `(gnus)Customizing Articles'."
1198   :version "21.1"
1199   :group 'gnus-article-treat
1200   :link '(custom-manual "(gnus)Customizing Articles")
1201   :type gnus-article-treat-head-custom)
1202
1203 (defcustom gnus-treat-date-user-defined nil
1204   "Display the date in a user-defined format.
1205 The format is defined by the `gnus-article-time-format' variable.
1206 Valid values are nil, t, `head', `first', `last', an integer or a
1207 predicate.  See Info node `(gnus)Customizing Articles'."
1208   :group 'gnus-article-treat
1209   :link '(custom-manual "(gnus)Customizing Articles")
1210   :type gnus-article-treat-head-custom)
1211
1212 (defcustom gnus-treat-strip-headers-in-body t
1213   "Strip the X-No-Archive header line from the beginning of the body.
1214 Valid values are nil, t, `head', `first', `last', an integer or a
1215 predicate.  See Info node `(gnus)Customizing Articles'."
1216   :version "21.1"
1217   :group 'gnus-article-treat
1218   :link '(custom-manual "(gnus)Customizing Articles")
1219   :type gnus-article-treat-custom)
1220
1221 (defcustom gnus-treat-strip-trailing-blank-lines nil
1222   "Strip trailing blank lines.
1223 Valid values are nil, t, `head', `first', `last', an integer or a
1224 predicate.  See Info node `(gnus)Customizing Articles'.
1225
1226 When set to t, it also strips trailing blanks in all MIME parts.
1227 Consider to use `last' instead."
1228   :group 'gnus-article-treat
1229   :link '(custom-manual "(gnus)Customizing Articles")
1230   :type gnus-article-treat-custom)
1231
1232 (defcustom gnus-treat-strip-leading-blank-lines nil
1233   "Strip leading blank lines.
1234 Valid values are nil, t, `head', `first', `last', an integer or a
1235 predicate.  See Info node `(gnus)Customizing Articles'.
1236
1237 When set to t, it also strips trailing blanks in all MIME parts."
1238   :group 'gnus-article-treat
1239   :link '(custom-manual "(gnus)Customizing Articles")
1240   :type gnus-article-treat-custom)
1241
1242 (defcustom gnus-treat-strip-multiple-blank-lines nil
1243   "Strip multiple blank lines.
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-unfold-headers 'head
1251   "Unfold folded header lines.
1252 Valid values are nil, t, `head', `first', `last', an integer or a
1253 predicate.  See Info node `(gnus)Customizing Articles'."
1254   :version "22.1"
1255   :group 'gnus-article-treat
1256   :link '(custom-manual "(gnus)Customizing Articles")
1257   :type gnus-article-treat-custom)
1258
1259 (defcustom gnus-treat-fold-headers nil
1260   "Fold headers.
1261 Valid values are nil, t, `head', `first', `last', an integer or a
1262 predicate.  See Info node `(gnus)Customizing Articles'."
1263   :version "22.1"
1264   :group 'gnus-article-treat
1265   :link '(custom-manual "(gnus)Customizing Articles")
1266   :type gnus-article-treat-custom)
1267
1268 (defcustom gnus-treat-fold-newsgroups 'head
1269   "Fold the Newsgroups and Followup-To headers.
1270 Valid values are nil, t, `head', `first', `last', an integer or a
1271 predicate.  See Info node `(gnus)Customizing Articles'."
1272   :version "22.1"
1273   :group 'gnus-article-treat
1274   :link '(custom-manual "(gnus)Customizing Articles")
1275   :type gnus-article-treat-custom)
1276
1277 (defcustom gnus-treat-overstrike t
1278   "Treat overstrike highlighting.
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-custom)
1284 (put 'gnus-treat-overstrike 'highlight t)
1285
1286 (defcustom gnus-treat-ansi-sequences (if (locate-library "ansi-color") t)
1287   "Treat ANSI SGR control sequences.
1288 Valid values are nil, t, `head', `first', `last', an integer or a
1289 predicate.  See Info node `(gnus)Customizing Articles'."
1290   :group 'gnus-article-treat
1291   :link '(custom-manual "(gnus)Customizing Articles")
1292   :type gnus-article-treat-custom)
1293
1294 (make-obsolete-variable 'gnus-treat-display-xface
1295                         'gnus-treat-display-x-face)
1296
1297 (defcustom gnus-treat-display-x-face
1298   (and (not noninteractive)
1299        (or (and (fboundp 'image-type-available-p)
1300                 (image-type-available-p 'xbm)
1301                 (string-match "^0x" (shell-command-to-string "uncompface"))
1302                 (executable-find "icontopbm"))
1303            (and (featurep 'xemacs)
1304                 (featurep 'xface)))
1305        'head)
1306   "Display X-Face headers.
1307 Valid values are nil, t, `head', `first', `last', an integer or a
1308 predicate.  See Info node `(gnus)Customizing Articles' and Info
1309 node `(gnus)X-Face' for details."
1310   :group 'gnus-article-treat
1311   :version "21.1"
1312   :link '(custom-manual "(gnus)Customizing Articles")
1313   :link '(custom-manual "(gnus)X-Face")
1314   :type gnus-article-treat-head-custom
1315   :set (lambda (symbol value)
1316          (set-default
1317           symbol
1318           (cond ((or (boundp symbol) (get symbol 'saved-value))
1319                  value)
1320                 ((boundp 'gnus-treat-display-xface)
1321                  (message "\
1322 ** gnus-treat-display-xface is an obsolete variable;\
1323  use gnus-treat-display-x-face instead")
1324                  (default-value 'gnus-treat-display-xface))
1325                 ((get 'gnus-treat-display-xface 'saved-value)
1326                  (message "\
1327 ** gnus-treat-display-xface is an obsolete variable;\
1328  use gnus-treat-display-x-face instead")
1329                  (eval (car (get 'gnus-treat-display-xface 'saved-value))))
1330                 (t
1331                  value)))))
1332 (put 'gnus-treat-display-x-face 'highlight t)
1333
1334 (defcustom gnus-treat-display-face
1335   (and (not noninteractive)
1336        (or (and (fboundp 'image-type-available-p)
1337                 (image-type-available-p 'png))
1338            (and (featurep 'xemacs)
1339                 (featurep 'png)))
1340        'head)
1341   "Display Face headers.
1342 Valid values are nil, t, `head', `first', `last', an integer or a
1343 predicate.  See Info node `(gnus)Customizing Articles' and Info
1344 node `(gnus)X-Face' for details."
1345   :group 'gnus-article-treat
1346   :version "22.1"
1347   :link '(custom-manual "(gnus)Customizing Articles")
1348   :link '(custom-manual "(gnus)X-Face")
1349   :type gnus-article-treat-head-custom)
1350 (put 'gnus-treat-display-face 'highlight t)
1351
1352 (defcustom gnus-treat-display-smileys
1353   (if (or (and (featurep 'xemacs)
1354                (featurep 'xpm))
1355           (and (fboundp 'image-type-available-p)
1356                (image-type-available-p 'pbm)))
1357       t nil)
1358   "Display smileys.
1359 Valid values are nil, t, `head', `first', `last', an integer or a
1360 predicate.  See Info node `(gnus)Customizing Articles' and Info
1361 node `(gnus)Smileys' for details."
1362   :group 'gnus-article-treat
1363   :version "21.1"
1364   :link '(custom-manual "(gnus)Customizing Articles")
1365   :link '(custom-manual "(gnus)Smileys")
1366   :type gnus-article-treat-custom)
1367 (put 'gnus-treat-display-smileys 'highlight t)
1368
1369 (defcustom gnus-treat-from-picon
1370   (if (and (gnus-image-type-available-p 'xpm)
1371            (gnus-picons-installed-p))
1372       'head nil)
1373   "Display picons in the From header.
1374 Valid values are nil, t, `head', `first', `last', an integer or a
1375 predicate.  See Info node `(gnus)Customizing Articles' and Info
1376 node `(gnus)Picons' for details."
1377   :version "22.1"
1378   :group 'gnus-article-treat
1379   :group 'gnus-picon
1380   :link '(custom-manual "(gnus)Customizing Articles")
1381   :link '(custom-manual "(gnus)Picons")
1382   :type gnus-article-treat-head-custom)
1383 (put 'gnus-treat-from-picon 'highlight t)
1384
1385 (defcustom gnus-treat-mail-picon
1386   (if (and (gnus-image-type-available-p 'xpm)
1387            (gnus-picons-installed-p))
1388       'head nil)
1389   "Display picons in To and Cc headers.
1390 Valid values are nil, t, `head', `first', `last', an integer or a
1391 predicate.  See Info node `(gnus)Customizing Articles' and Info
1392 node `(gnus)Picons' for details."
1393   :version "22.1"
1394   :group 'gnus-article-treat
1395   :group 'gnus-picon
1396   :link '(custom-manual "(gnus)Customizing Articles")
1397   :link '(custom-manual "(gnus)Picons")
1398   :type gnus-article-treat-head-custom)
1399 (put 'gnus-treat-mail-picon 'highlight t)
1400
1401 (defcustom gnus-treat-newsgroups-picon
1402   (if (and (gnus-image-type-available-p 'xpm)
1403            (gnus-picons-installed-p))
1404       'head nil)
1405   "Display picons in 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' and Info
1408 node `(gnus)Picons' for details."
1409   :version "22.1"
1410   :group 'gnus-article-treat
1411   :group 'gnus-picon
1412   :link '(custom-manual "(gnus)Customizing Articles")
1413   :link '(custom-manual "(gnus)Picons")
1414   :type gnus-article-treat-head-custom)
1415 (put 'gnus-treat-newsgroups-picon 'highlight t)
1416
1417 (defcustom gnus-treat-body-boundary
1418   (if (and (eq window-system 'x)
1419            (or gnus-treat-newsgroups-picon
1420                gnus-treat-mail-picon
1421                gnus-treat-from-picon))
1422       'head nil)
1423   "Draw a boundary at the end of the headers.
1424 Valid values are nil and `head'.
1425 See Info node `(gnus)Customizing Articles' for details."
1426   :version "22.1"
1427   :group 'gnus-article-treat
1428   :link '(custom-manual "(gnus)Customizing Articles")
1429   :type gnus-article-treat-head-custom)
1430
1431 (defcustom gnus-treat-capitalize-sentences nil
1432   "Capitalize sentence-starting words.
1433 Valid values are nil, t, `head', `first', `last', an integer or a
1434 predicate.  See Info node `(gnus)Customizing Articles'."
1435   :version "21.1"
1436   :group 'gnus-article-treat
1437   :link '(custom-manual "(gnus)Customizing Articles")
1438   :type gnus-article-treat-custom)
1439
1440 (defcustom gnus-treat-wash-html nil
1441   "Format as HTML.
1442 Valid values are nil, t, `head', `first', `last', an integer or a
1443 predicate.  See Info node `(gnus)Customizing Articles'."
1444   :version "22.1"
1445   :group 'gnus-article-treat
1446   :link '(custom-manual "(gnus)Customizing Articles")
1447   :type gnus-article-treat-custom)
1448
1449 (defcustom gnus-treat-fill-long-lines nil
1450   "Fill long lines.
1451 Valid values are nil, t, `head', `first', `last', an integer or a
1452 predicate.  See Info node `(gnus)Customizing Articles'."
1453   :group 'gnus-article-treat
1454   :link '(custom-manual "(gnus)Customizing Articles")
1455   :type gnus-article-treat-custom)
1456
1457 (defcustom gnus-treat-play-sounds nil
1458   "Play sounds.
1459 Valid values are nil, t, `head', `first', `last', an integer or a
1460 predicate.  See Info node `(gnus)Customizing Articles'."
1461   :version "21.1"
1462   :group 'gnus-article-treat
1463   :link '(custom-manual "(gnus)Customizing Articles")
1464   :type gnus-article-treat-custom)
1465
1466 (defcustom gnus-treat-translate nil
1467   "Translate articles from one language to another.
1468 Valid values are nil, t, `head', `first', `last', an integer or a
1469 predicate.  See Info node `(gnus)Customizing Articles'."
1470   :version "21.1"
1471   :group 'gnus-article-treat
1472   :link '(custom-manual "(gnus)Customizing Articles")
1473   :type gnus-article-treat-custom)
1474
1475 (defcustom gnus-treat-x-pgp-sig nil
1476   "Verify X-PGP-Sig.
1477 To automatically treat X-PGP-Sig, set it to head.
1478 Valid values are nil, t, `head', `first', `last', an integer or a
1479 predicate.  See Info node `(gnus)Customizing Articles'."
1480   :version "22.1"
1481   :group 'gnus-article-treat
1482   :group 'mime-security
1483   :link '(custom-manual "(gnus)Customizing Articles")
1484   :type gnus-article-treat-custom)
1485
1486 (defvar gnus-article-encrypt-protocol-alist
1487   '(("PGP" . mml2015-self-encrypt)))
1488
1489 ;; Set to nil if more than one protocol added to
1490 ;; gnus-article-encrypt-protocol-alist.
1491 (defcustom gnus-article-encrypt-protocol "PGP"
1492   "The protocol used for encrypt articles.
1493 It is a string, such as \"PGP\". If nil, ask user."
1494   :version "22.1"
1495   :type 'string
1496   :group 'mime-security)
1497
1498 (defvar gnus-article-wash-function nil
1499   "Function used for converting HTML into text.")
1500
1501 (defcustom gnus-use-idna (and (condition-case nil (require 'idna) (file-error))
1502                               (mm-coding-system-p 'utf-8)
1503                               (executable-find idna-program))
1504   "Whether IDNA decoding of headers is used when viewing messages.
1505 This requires GNU Libidn, and by default only enabled if it is found."
1506   :version "22.1"
1507   :group 'gnus-article-headers
1508   :type 'boolean)
1509
1510 (defcustom gnus-article-over-scroll nil
1511   "If non-nil, allow scrolling the article buffer even when there no more text."
1512   :version "22.1"
1513   :group 'gnus-article
1514   :type 'boolean)
1515
1516 ;;; Internal variables
1517
1518 (defvar gnus-english-month-names
1519   '("January" "February" "March" "April" "May" "June" "July" "August"
1520     "September" "October" "November" "December"))
1521
1522 (defvar gnus-button-regexp nil)
1523 (defvar gnus-button-marker-list nil)
1524 ;; Regexp matching any of the regexps from `gnus-button-alist'.
1525
1526 (defvar gnus-button-last nil)
1527 ;; The value of `gnus-button-alist' when `gnus-button-regexp' was build.
1528
1529 (defvar article-goto-body-goes-to-point-min-p nil)
1530 (defvar gnus-article-wash-types nil)
1531 (defvar gnus-article-emphasis-alist nil)
1532 (defvar gnus-article-image-alist nil)
1533
1534 (defvar gnus-article-mime-handle-alist-1 nil)
1535 (defvar gnus-treatment-function-alist
1536   '((gnus-treat-x-pgp-sig gnus-article-verify-x-pgp-sig)
1537     (gnus-treat-strip-banner gnus-article-strip-banner)
1538     (gnus-treat-strip-headers-in-body gnus-article-strip-headers-in-body)
1539     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1540     (gnus-treat-buttonize gnus-article-add-buttons)
1541     (gnus-treat-fill-article gnus-article-fill-cited-article)
1542     (gnus-treat-fill-long-lines gnus-article-fill-long-lines)
1543     (gnus-treat-strip-cr gnus-article-remove-cr)
1544     (gnus-treat-unsplit-urls gnus-article-unsplit-urls)
1545     (gnus-treat-date-ut gnus-article-date-ut)
1546     (gnus-treat-date-local gnus-article-date-local)
1547     (gnus-treat-date-english gnus-article-date-english)
1548     (gnus-treat-date-original gnus-article-date-original)
1549     (gnus-treat-date-user-defined gnus-article-date-user)
1550     (gnus-treat-date-iso8601 gnus-article-date-iso8601)
1551     (gnus-treat-date-lapsed gnus-article-date-lapsed)
1552     (gnus-treat-display-x-face gnus-article-display-x-face)
1553     (gnus-treat-display-face gnus-article-display-face)
1554     (gnus-treat-hide-headers gnus-article-maybe-hide-headers)
1555     (gnus-treat-hide-boring-headers gnus-article-hide-boring-headers)
1556     (gnus-treat-hide-signature gnus-article-hide-signature)
1557     (gnus-treat-strip-list-identifiers gnus-article-hide-list-identifiers)
1558     (gnus-treat-leading-whitespace gnus-article-remove-leading-whitespace)
1559     (gnus-treat-strip-pem gnus-article-hide-pem)
1560     (gnus-treat-from-picon gnus-treat-from-picon)
1561     (gnus-treat-mail-picon gnus-treat-mail-picon)
1562     (gnus-treat-newsgroups-picon gnus-treat-newsgroups-picon)
1563     (gnus-treat-highlight-headers gnus-article-highlight-headers)
1564     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1565     (gnus-treat-strip-trailing-blank-lines
1566      gnus-article-remove-trailing-blank-lines)
1567     (gnus-treat-strip-leading-blank-lines
1568      gnus-article-strip-leading-blank-lines)
1569     (gnus-treat-strip-multiple-blank-lines
1570      gnus-article-strip-multiple-blank-lines)
1571     (gnus-treat-overstrike gnus-article-treat-overstrike)
1572     (gnus-treat-ansi-sequences gnus-article-treat-ansi-sequences)
1573     (gnus-treat-unfold-headers gnus-article-treat-unfold-headers)
1574     (gnus-treat-fold-headers gnus-article-treat-fold-headers)
1575     (gnus-treat-fold-newsgroups gnus-article-treat-fold-newsgroups)
1576     (gnus-treat-buttonize-head gnus-article-add-buttons-to-head)
1577     (gnus-treat-display-smileys gnus-treat-smiley)
1578     (gnus-treat-capitalize-sentences gnus-article-capitalize-sentences)
1579     (gnus-treat-wash-html gnus-article-wash-html)
1580     (gnus-treat-emphasize gnus-article-emphasize)
1581     (gnus-treat-hide-citation gnus-article-hide-citation)
1582     (gnus-treat-hide-citation-maybe gnus-article-hide-citation-maybe)
1583     (gnus-treat-highlight-citation gnus-article-highlight-citation)
1584     (gnus-treat-body-boundary gnus-article-treat-body-boundary)
1585     (gnus-treat-play-sounds gnus-earcon-display)))
1586
1587 (defvar gnus-article-mime-handle-alist nil)
1588 (defvar article-lapsed-timer nil)
1589 (defvar gnus-article-current-summary nil)
1590
1591 (defvar gnus-article-mode-syntax-table
1592   (let ((table (copy-syntax-table text-mode-syntax-table)))
1593     ;; This causes the citation match run O(2^n).
1594     ;; (modify-syntax-entry ?- "w" table)
1595     (modify-syntax-entry ?> ")<" table)
1596     (modify-syntax-entry ?< "(>" table)
1597     ;; make M-. in article buffers work for `foo' strings
1598     (modify-syntax-entry ?' " " table)
1599     (modify-syntax-entry ?` " " table)
1600     table)
1601   "Syntax table used in article mode buffers.
1602 Initialized from `text-mode-syntax-table.")
1603
1604 (defvar gnus-save-article-buffer nil)
1605
1606 (defvar gnus-article-mode-line-format-alist
1607   (nconc '((?w (gnus-article-wash-status) ?s)
1608            (?m (gnus-article-mime-part-status) ?s))
1609          gnus-summary-mode-line-format-alist))
1610
1611 (defvar gnus-number-of-articles-to-be-saved nil)
1612
1613 (defvar gnus-inhibit-hiding nil)
1614
1615 (defvar gnus-article-edit-mode nil)
1616
1617 ;;; Macros for dealing with the article buffer.
1618
1619 (defmacro gnus-with-article-headers (&rest forms)
1620   `(save-excursion
1621      (set-buffer gnus-article-buffer)
1622      (save-restriction
1623        (let ((inhibit-read-only t)
1624              (inhibit-point-motion-hooks t)
1625              (case-fold-search t))
1626          (article-narrow-to-head)
1627          ,@forms))))
1628
1629 (put 'gnus-with-article-headers 'lisp-indent-function 0)
1630 (put 'gnus-with-article-headers 'edebug-form-spec '(body))
1631
1632 (defmacro gnus-with-article-buffer (&rest forms)
1633   `(save-excursion
1634      (set-buffer gnus-article-buffer)
1635      (let ((inhibit-read-only t))
1636        ,@forms)))
1637
1638 (put 'gnus-with-article-buffer 'lisp-indent-function 0)
1639 (put 'gnus-with-article-buffer 'edebug-form-spec '(body))
1640
1641 (defun gnus-article-goto-header (header)
1642   "Go to HEADER, which is a regular expression."
1643   (re-search-forward (concat "^\\(" header "\\):") nil t))
1644
1645 (defsubst gnus-article-hide-text (b e props)
1646   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
1647   (gnus-add-text-properties-when 'article-type nil b e props)
1648   (when (memq 'intangible props)
1649     (put-text-property
1650      (max (1- b) (point-min))
1651      b 'intangible (cddr (memq 'intangible props)))))
1652
1653 (defsubst gnus-article-unhide-text (b e)
1654   "Remove hidden text properties from region between B and E."
1655   (remove-text-properties b e gnus-hidden-properties)
1656   (when (memq 'intangible gnus-hidden-properties)
1657     (put-text-property (max (1- b) (point-min))
1658                        b 'intangible nil)))
1659
1660 (defun gnus-article-hide-text-type (b e type)
1661   "Hide text of TYPE between B and E."
1662   (gnus-add-wash-type type)
1663   (gnus-article-hide-text
1664    b e (cons 'article-type (cons type gnus-hidden-properties))))
1665
1666 (defun gnus-article-unhide-text-type (b e type)
1667   "Unhide text of TYPE between B and E."
1668   (gnus-delete-wash-type type)
1669   (remove-text-properties
1670    b e (cons 'article-type (cons type gnus-hidden-properties)))
1671   (when (memq 'intangible gnus-hidden-properties)
1672     (put-text-property (max (1- b) (point-min))
1673                        b 'intangible nil)))
1674
1675 (defun gnus-article-hide-text-of-type (type)
1676   "Hide text of TYPE in the current buffer."
1677   (save-excursion
1678     (let ((b (point-min))
1679           (e (point-max)))
1680       (while (setq b (text-property-any b e 'article-type type))
1681         (add-text-properties b (incf b) gnus-hidden-properties)))))
1682
1683 (defun gnus-article-delete-text-of-type (type)
1684   "Delete text of TYPE in the current buffer."
1685   (save-excursion
1686     (let ((b (point-min)))
1687       (if (eq type 'multipart)
1688           ;; Remove MIME buttons associated with multipart/alternative parts.
1689           (progn
1690             (goto-char b)
1691             (while (if (get-text-property (point) 'gnus-part)
1692                        (setq b (point))
1693                      (when (setq b (next-single-property-change (point)
1694                                                                 'gnus-part))
1695                        (goto-char b)
1696                        t))
1697               (end-of-line)
1698               (skip-chars-forward "\n")
1699               (when (eq (get-text-property b 'article-type) 'multipart)
1700                 (delete-region b (point)))))
1701         (while (setq b (text-property-any b (point-max) 'article-type type))
1702           (delete-region
1703            b (or (text-property-not-all b (point-max) 'article-type type)
1704                  (point-max))))))))
1705
1706 (defun gnus-article-delete-invisible-text ()
1707   "Delete all invisible text in the current buffer."
1708   (save-excursion
1709     (let ((b (point-min)))
1710       (while (setq b (text-property-any b (point-max) 'invisible t))
1711         (delete-region
1712          b (or (text-property-not-all b (point-max) 'invisible t)
1713                (point-max)))))))
1714
1715 (defun gnus-article-text-type-exists-p (type)
1716   "Say whether any text of type TYPE exists in the buffer."
1717   (text-property-any (point-min) (point-max) 'article-type type))
1718
1719 (defsubst gnus-article-header-rank ()
1720   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
1721   (let ((list gnus-sorted-header-list)
1722         (i 1))
1723     (while list
1724       (if (looking-at (car list))
1725           (setq list nil)
1726         (setq list (cdr list))
1727         (incf i)))
1728       i))
1729
1730 (defun article-hide-headers (&optional arg delete)
1731   "Hide unwanted headers and possibly sort them as well."
1732   (interactive)
1733   ;; This function might be inhibited.
1734   (unless gnus-inhibit-hiding
1735     (let ((inhibit-read-only nil)
1736           (case-fold-search t)
1737           (max (1+ (length gnus-sorted-header-list)))
1738           (inhibit-point-motion-hooks t)
1739           (cur (current-buffer))
1740           ignored visible beg)
1741       (save-excursion
1742         ;; `gnus-ignored-headers' and `gnus-visible-headers' may be
1743         ;; group parameters, so we should go to the summary buffer.
1744         (when (prog1
1745                   (condition-case nil
1746                       (progn (set-buffer gnus-summary-buffer) t)
1747                     (error nil))
1748                 (setq ignored (when (not gnus-visible-headers)
1749                                 (cond ((stringp gnus-ignored-headers)
1750                                        gnus-ignored-headers)
1751                                       ((listp gnus-ignored-headers)
1752                                        (mapconcat 'identity
1753                                                   gnus-ignored-headers
1754                                                   "\\|"))))
1755                       visible (cond ((stringp gnus-visible-headers)
1756                                      gnus-visible-headers)
1757                                     ((and gnus-visible-headers
1758                                           (listp gnus-visible-headers))
1759                                      (mapconcat 'identity
1760                                                 gnus-visible-headers
1761                                                 "\\|")))))
1762           (set-buffer cur))
1763         (save-restriction
1764           ;; First we narrow to just the headers.
1765           (article-narrow-to-head)
1766           ;; Hide any "From " lines at the beginning of (mail) articles.
1767           (while (looking-at "From ")
1768             (forward-line 1))
1769           (unless (bobp)
1770             (delete-region (point-min) (point)))
1771           ;; Then treat the rest of the header lines.
1772           ;; Then we use the two regular expressions
1773           ;; `gnus-ignored-headers' and `gnus-visible-headers' to
1774           ;; select which header lines is to remain visible in the
1775           ;; article buffer.
1776           (while (re-search-forward "^[^ \t:]*:" nil t)
1777             (beginning-of-line)
1778             ;; Mark the rank of the header.
1779             (put-text-property
1780              (point) (1+ (point)) 'message-rank
1781              (if (or (and visible (looking-at visible))
1782                      (and ignored
1783                           (not (looking-at ignored))))
1784                  (gnus-article-header-rank)
1785                (+ 2 max)))
1786             (forward-line 1))
1787           (message-sort-headers-1)
1788           (when (setq beg (text-property-any
1789                            (point-min) (point-max) 'message-rank (+ 2 max)))
1790             ;; We delete the unwanted headers.
1791             (gnus-add-wash-type 'headers)
1792             (add-text-properties (point-min) (+ 5 (point-min))
1793                                  '(article-type headers dummy-invisible t))
1794             (delete-region beg (point-max))))))))
1795
1796 (defun article-hide-boring-headers (&optional arg)
1797   "Toggle hiding of headers that aren't very interesting.
1798 If given a negative prefix, always show; if given a positive prefix,
1799 always hide."
1800   (interactive (gnus-article-hidden-arg))
1801   (when (and (not (gnus-article-check-hidden-text 'boring-headers arg))
1802              (not gnus-show-all-headers))
1803     (save-excursion
1804       (save-restriction
1805         (let ((inhibit-read-only t)
1806               (inhibit-point-motion-hooks t))
1807           (article-narrow-to-head)
1808           (dolist (elem gnus-boring-article-headers)
1809             (goto-char (point-min))
1810             (cond
1811              ;; Hide empty headers.
1812              ((eq elem 'empty)
1813               (while (re-search-forward "^[^: \t]+:[ \t]*\n[^ \t]" nil t)
1814                 (forward-line -1)
1815                 (gnus-article-hide-text-type
1816                  (point-at-bol)
1817                  (progn
1818                    (end-of-line)
1819                    (if (re-search-forward "^[^ \t]" nil t)
1820                        (match-beginning 0)
1821                      (point-max)))
1822                  'boring-headers)))
1823              ;; Hide boring Newsgroups header.
1824              ((eq elem 'newsgroups)
1825               (when (gnus-string-equal
1826                      (gnus-fetch-field "newsgroups")
1827                      (gnus-group-real-name
1828                       (if (boundp 'gnus-newsgroup-name)
1829                           gnus-newsgroup-name
1830                         "")))
1831                 (gnus-article-hide-header "newsgroups")))
1832              ((eq elem 'to-address)
1833               (let ((to (message-fetch-field "to"))
1834                     (to-address
1835                      (gnus-parameter-to-address
1836                       (if (boundp 'gnus-newsgroup-name)
1837                           gnus-newsgroup-name ""))))
1838                 (when (and to to-address
1839                            (ignore-errors
1840                              (gnus-string-equal
1841                               ;; only one address in To
1842                               (nth 1 (mail-extract-address-components to))
1843                               to-address)))
1844                   (gnus-article-hide-header "to"))))
1845              ((eq elem 'to-list)
1846               (let ((to (message-fetch-field "to"))
1847                     (to-list
1848                      (gnus-parameter-to-list
1849                       (if (boundp 'gnus-newsgroup-name)
1850                           gnus-newsgroup-name ""))))
1851                 (when (and to to-list
1852                            (ignore-errors
1853                              (gnus-string-equal
1854                               ;; only one address in To
1855                               (nth 1 (mail-extract-address-components to))
1856                               to-list)))
1857                   (gnus-article-hide-header "to"))))
1858              ((eq elem 'cc-list)
1859               (let ((cc (message-fetch-field "cc"))
1860                     (to-list
1861                      (gnus-parameter-to-list
1862                       (if (boundp 'gnus-newsgroup-name)
1863                           gnus-newsgroup-name ""))))
1864                 (when (and cc to-list
1865                            (ignore-errors
1866                              (gnus-string-equal
1867                               ;; only one address in CC
1868                               (nth 1 (mail-extract-address-components cc))
1869                               to-list)))
1870                   (gnus-article-hide-header "cc"))))
1871              ((eq elem 'followup-to)
1872               (when (gnus-string-equal
1873                      (message-fetch-field "followup-to")
1874                      (message-fetch-field "newsgroups"))
1875                 (gnus-article-hide-header "followup-to")))
1876              ((eq elem 'reply-to)
1877               (if (gnus-group-find-parameter
1878                    gnus-newsgroup-name 'broken-reply-to)
1879                   (gnus-article-hide-header "reply-to")
1880                 (let ((from (message-fetch-field "from"))
1881                       (reply-to (message-fetch-field "reply-to")))
1882                   (when
1883                       (and
1884                        from reply-to
1885                        (ignore-errors
1886                          (equal
1887                           (sort (mapcar
1888                                  (lambda (x) (downcase (cadr x)))
1889                                  (mail-extract-address-components from t))
1890                                 'string<)
1891                           (sort (mapcar
1892                                  (lambda (x) (downcase (cadr x)))
1893                                  (mail-extract-address-components reply-to t))
1894                                 'string<))))
1895                     (gnus-article-hide-header "reply-to")))))
1896              ((eq elem 'date)
1897               (let ((date (message-fetch-field "date")))
1898                 (when (and date
1899                            (< (days-between (current-time-string) date)
1900                               4))
1901                   (gnus-article-hide-header "date"))))
1902              ((eq elem 'long-to)
1903               (let ((to (message-fetch-field "to"))
1904                     (cc (message-fetch-field "cc")))
1905                 (when (> (length to) 1024)
1906                   (gnus-article-hide-header "to"))
1907                 (when (> (length cc) 1024)
1908                   (gnus-article-hide-header "cc"))))
1909              ((eq elem 'many-to)
1910               (let ((to-count 0)
1911                     (cc-count 0))
1912                 (goto-char (point-min))
1913                 (while (re-search-forward "^to:" nil t)
1914                   (setq to-count (1+ to-count)))
1915                 (when (> to-count 1)
1916                   (while (> to-count 0)
1917                     (goto-char (point-min))
1918                     (save-restriction
1919                       (re-search-forward "^to:" nil nil to-count)
1920                       (forward-line -1)
1921                       (narrow-to-region (point) (point-max))
1922                       (gnus-article-hide-header "to"))
1923                     (setq to-count (1- to-count))))
1924                 (goto-char (point-min))
1925                 (while (re-search-forward "^cc:" nil t)
1926                   (setq cc-count (1+ cc-count)))
1927                 (when (> cc-count 1)
1928                   (while (> cc-count 0)
1929                     (goto-char (point-min))
1930                     (save-restriction
1931                       (re-search-forward "^cc:" nil nil cc-count)
1932                       (forward-line -1)
1933                       (narrow-to-region (point) (point-max))
1934                       (gnus-article-hide-header "cc"))
1935                     (setq cc-count (1- cc-count)))))))))))))
1936
1937 (defun gnus-article-hide-header (header)
1938   (save-excursion
1939     (goto-char (point-min))
1940     (when (re-search-forward (concat "^" header ":") nil t)
1941       (gnus-article-hide-text-type
1942        (point-at-bol)
1943        (progn
1944          (end-of-line)
1945          (if (re-search-forward "^[^ \t]" nil t)
1946              (match-beginning 0)
1947            (point-max)))
1948        'boring-headers))))
1949
1950 (defvar gnus-article-normalized-header-length 40
1951   "Length of normalized headers.")
1952
1953 (defun article-normalize-headers ()
1954   "Make all header lines 40 characters long."
1955   (interactive)
1956   (let ((inhibit-read-only t)
1957         column)
1958     (save-excursion
1959       (save-restriction
1960         (article-narrow-to-head)
1961         (while (not (eobp))
1962           (cond
1963            ((< (setq column (- (point-at-eol) (point)))
1964                gnus-article-normalized-header-length)
1965             (end-of-line)
1966             (insert (make-string
1967                      (- gnus-article-normalized-header-length column)
1968                      ? )))
1969            ((> column gnus-article-normalized-header-length)
1970             (gnus-put-text-property
1971              (progn
1972                (forward-char gnus-article-normalized-header-length)
1973                (point))
1974              (point-at-eol)
1975              'invisible t))
1976            (t
1977             ;; Do nothing.
1978             ))
1979           (forward-line 1))))))
1980
1981 (defun article-treat-dumbquotes ()
1982   "Translate M****s*** sm*rtq**t*s and other symbols into proper text.
1983 Note that this function guesses whether a character is a sm*rtq**t* or
1984 not, so it should only be used interactively.
1985
1986 Sm*rtq**t*s are M****s***'s unilateral extension to the
1987 iso-8859-1 character map in an attempt to provide more quoting
1988 characters.  If you see something like \\222 or \\264 where
1989 you're expecting some kind of apostrophe or quotation mark, then
1990 try this wash."
1991   (interactive)
1992   (article-translate-strings gnus-article-dumbquotes-map))
1993
1994 (defun article-translate-characters (from to)
1995   "Translate all characters in the body of the article according to FROM and TO.
1996 FROM is a string of characters to translate from; to is a string of
1997 characters to translate to."
1998   (save-excursion
1999     (when (article-goto-body)
2000       (let ((inhibit-read-only t)
2001             (x (make-string 225 ?x))
2002             (i -1))
2003         (while (< (incf i) (length x))
2004           (aset x i i))
2005         (setq i 0)
2006         (while (< i (length from))
2007           (aset x (aref from i) (aref to i))
2008           (incf i))
2009         (translate-region (point) (point-max) x)))))
2010
2011 (defun article-translate-strings (map)
2012   "Translate all string in the body of the article according to MAP.
2013 MAP is an alist where the elements are on the form (\"from\" \"to\")."
2014   (save-excursion
2015     (when (article-goto-body)
2016       (let ((inhibit-read-only t))
2017         (dolist (elem map)
2018           (save-excursion
2019             (while (search-forward (car elem) nil t)
2020               (replace-match (cadr elem)))))))))
2021
2022 (defun article-treat-overstrike ()
2023   "Translate overstrikes into bold text."
2024   (interactive)
2025   (save-excursion
2026     (when (article-goto-body)
2027       (let ((inhibit-read-only t))
2028         (while (search-forward "\b" nil t)
2029           (let ((next (char-after))
2030                 (previous (char-after (- (point) 2))))
2031             ;; We do the boldification/underlining by hiding the
2032             ;; overstrikes and putting the proper text property
2033             ;; on the letters.
2034             (cond
2035              ((eq next previous)
2036               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2037               (put-text-property (point) (1+ (point)) 'face 'bold))
2038              ((eq next ?_)
2039               (gnus-article-hide-text-type
2040                (1- (point)) (1+ (point)) 'overstrike)
2041               (put-text-property
2042                (- (point) 2) (1- (point)) 'face 'underline))
2043              ((eq previous ?_)
2044               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2045               (put-text-property
2046                (point) (1+ (point)) 'face 'underline)))))))))
2047
2048 (defun article-treat-ansi-sequences ()
2049   "Translate ANSI SGR control sequences into overlays or extents."
2050   (interactive)
2051   (save-excursion
2052     (when (article-goto-body)
2053       (let ((inhibit-read-only t))
2054         (ansi-color-apply-on-region (point) (point-max))))))
2055
2056 (defun gnus-article-treat-unfold-headers ()
2057   "Unfold folded message headers.
2058 Only the headers that fit into the current window width will be
2059 unfolded."
2060   (interactive)
2061   (gnus-with-article-headers
2062     (let (length)
2063       (while (not (eobp))
2064         (save-restriction
2065           (mail-header-narrow-to-field)
2066           (let ((header (buffer-string)))
2067             (with-temp-buffer
2068               (insert header)
2069               (goto-char (point-min))
2070               (while (re-search-forward "\n[\t ]" nil t)
2071                 (replace-match " " t t)))
2072             (setq length (- (point-max) (point-min) 1)))
2073           (when (< length (window-width))
2074             (while (re-search-forward "\n[\t ]" nil t)
2075               (replace-match " " t t)))
2076           (goto-char (point-max)))))))
2077
2078 (defun gnus-article-treat-fold-headers ()
2079   "Fold message headers."
2080   (interactive)
2081   (gnus-with-article-headers
2082     (while (not (eobp))
2083       (save-restriction
2084         (mail-header-narrow-to-field)
2085         (mail-header-fold-field)
2086         (goto-char (point-max))))))
2087
2088 (defun gnus-treat-smiley ()
2089   "Toggle display of textual emoticons (\"smileys\") as small graphical icons."
2090   (interactive)
2091   (gnus-with-article-buffer
2092     (if (memq 'smiley gnus-article-wash-types)
2093         (gnus-delete-images 'smiley)
2094       (article-goto-body)
2095       (let ((images (smiley-region (point) (point-max))))
2096         (when images
2097           (gnus-add-wash-type 'smiley)
2098           (dolist (image images)
2099             (gnus-add-image 'smiley image)))))))
2100
2101 (defun gnus-article-remove-images ()
2102   "Remove all images from the article buffer."
2103   (interactive)
2104   (gnus-with-article-buffer
2105     (dolist (elem gnus-article-image-alist)
2106       (gnus-delete-images (car elem)))))
2107
2108 (defun gnus-article-treat-fold-newsgroups ()
2109   "Unfold folded message headers.
2110 Only the headers that fit into the current window width will be
2111 unfolded."
2112   (interactive)
2113   (gnus-with-article-headers
2114     (while (gnus-article-goto-header "newsgroups\\|followup-to")
2115       (save-restriction
2116         (mail-header-narrow-to-field)
2117         (while (re-search-forward ", *" nil t)
2118           (replace-match ", " t t))
2119         (mail-header-fold-field)
2120         (goto-char (point-max))))))
2121
2122 (defun gnus-article-treat-body-boundary ()
2123   "Place a boundary line at the end of the headers."
2124   (interactive)
2125   (when (and gnus-body-boundary-delimiter
2126              (> (length gnus-body-boundary-delimiter) 0))
2127     (gnus-with-article-headers
2128       (goto-char (point-max))
2129       (let ((start (point)))
2130         (insert "X-Boundary: ")
2131         (gnus-add-text-properties start (point) '(invisible t intangible t))
2132         (insert (let (str)
2133                   (while (>= (1- (window-width)) (length str))
2134                     (setq str (concat str gnus-body-boundary-delimiter)))
2135                   (substring str 0 (1- (window-width))))
2136                 "\n")
2137         (gnus-put-text-property start (point) 'gnus-decoration 'header)))))
2138
2139 (defun article-fill-long-lines ()
2140   "Fill lines that are wider than the window width."
2141   (interactive)
2142   (save-excursion
2143     (let ((inhibit-read-only t)
2144           (width (window-width (get-buffer-window (current-buffer)))))
2145       (save-restriction
2146         (article-goto-body)
2147         (let ((adaptive-fill-mode nil)) ;Why?  -sm
2148           (while (not (eobp))
2149             (end-of-line)
2150             (when (>= (current-column) (min fill-column width))
2151               (narrow-to-region (min (1+ (point)) (point-max))
2152                                 (point-at-bol))
2153               (let ((goback (point-marker)))
2154                 (fill-paragraph nil)
2155                 (goto-char (marker-position goback)))
2156               (widen))
2157             (forward-line 1)))))))
2158
2159 (defun article-capitalize-sentences ()
2160   "Capitalize the first word in each sentence."
2161   (interactive)
2162   (save-excursion
2163     (let ((inhibit-read-only t)
2164           (paragraph-start "^[\n\^L]"))
2165       (article-goto-body)
2166       (while (not (eobp))
2167         (capitalize-word 1)
2168         (forward-sentence)))))
2169
2170 (defun article-remove-cr ()
2171   "Remove trailing CRs and then translate remaining CRs into LFs."
2172   (interactive)
2173   (save-excursion
2174     (let ((inhibit-read-only t))
2175       (goto-char (point-min))
2176       (while (re-search-forward "\r+$" nil t)
2177         (replace-match "" t t))
2178       (goto-char (point-min))
2179       (while (search-forward "\r" nil t)
2180         (replace-match "\n" t t)))))
2181
2182 (defun article-remove-trailing-blank-lines ()
2183   "Remove all trailing blank lines from the article."
2184   (interactive)
2185   (save-excursion
2186     (let ((inhibit-read-only t))
2187       (goto-char (point-max))
2188       (delete-region
2189        (point)
2190        (progn
2191          (while (and (not (bobp))
2192                      (looking-at "^[ \t]*$")
2193                      (not (gnus-annotation-in-region-p
2194                            (point) (point-at-eol))))
2195            (forward-line -1))
2196          (forward-line 1)
2197          (point))))))
2198
2199 (eval-when-compile
2200   (defvar gnus-face-properties-alist))
2201
2202 (defun article-display-face ()
2203   "Display any Face headers in the header."
2204   (interactive)
2205   (let ((wash-face-p buffer-read-only))
2206     (gnus-with-article-headers
2207       ;; When displaying parts, this function can be called several times on
2208       ;; the same article, without any intended toggle semantic (as typing `W
2209       ;; D d' would have). So face deletion must occur only when we come from
2210       ;; an interactive command, that is when the *Article* buffer is
2211       ;; read-only.
2212       (if (and wash-face-p (memq 'face gnus-article-wash-types))
2213           (gnus-delete-images 'face)
2214         (let (face faces from)
2215           (save-current-buffer
2216             (when (and wash-face-p
2217                        (gnus-buffer-live-p gnus-original-article-buffer)
2218                        (not (re-search-forward "^Face:[\t ]*" nil t)))
2219               (set-buffer gnus-original-article-buffer))
2220             (save-restriction
2221               (mail-narrow-to-head)
2222               (while (gnus-article-goto-header "Face")
2223                 (push (mail-header-field-value) faces))))
2224           (when faces
2225             (goto-char (point-min))
2226             (let ((from (gnus-article-goto-header "from"))
2227                   png image)
2228               (unless from
2229                 (insert "From:")
2230                 (setq from (point))
2231                 (insert "[no `from' set]\n"))
2232               (while faces
2233                 (when (setq png (gnus-convert-face-to-png (pop faces)))
2234                   (setq image
2235                         (apply 'gnus-create-image png 'png t
2236                                (cdr (assq 'png gnus-face-properties-alist))))
2237                   (goto-char from)
2238                   (gnus-add-wash-type 'face)
2239                   (gnus-add-image 'face image)
2240                   (gnus-put-image image nil 'face))))))))))
2241
2242 (defun article-display-x-face (&optional force)
2243   "Look for an X-Face header and display it if present."
2244   (interactive (list 'force))
2245   (let ((wash-face-p buffer-read-only)) ;; When type `W f'
2246     (gnus-with-article-headers
2247       ;; Delete the old process, if any.
2248       (when (process-status "article-x-face")
2249         (delete-process "article-x-face"))
2250       ;; See the comment in `article-display-face'.
2251       (if (and wash-face-p (memq 'xface gnus-article-wash-types))
2252           ;; We have already displayed X-Faces, so we remove them
2253           ;; instead.
2254           (gnus-delete-images 'xface)
2255         ;; Display X-Faces.
2256         (let (x-faces from face)
2257           (save-current-buffer
2258             (when (and wash-face-p
2259                        (gnus-buffer-live-p gnus-original-article-buffer)
2260                        (not (re-search-forward "^X-Face:[\t ]*" nil t)))
2261               ;; If type `W f', use gnus-original-article-buffer,
2262               ;; otherwise use the current buffer because displaying
2263               ;; RFC822 parts calls this function too.
2264               (set-buffer gnus-original-article-buffer))
2265             (save-restriction
2266               (mail-narrow-to-head)
2267               (while (gnus-article-goto-header "X-Face")
2268                 (push (mail-header-field-value) x-faces))
2269               (setq from (message-fetch-field "from"))))
2270           ;; Sending multiple EOFs to xv doesn't work, so we only do a
2271           ;; single external face.
2272           (when (stringp gnus-article-x-face-command)
2273             (setq x-faces (list (car x-faces))))
2274           (when (and x-faces
2275                      gnus-article-x-face-command
2276                      (or force
2277                          ;; Check whether this face is censored.
2278                          (not gnus-article-x-face-too-ugly)
2279                          (and from
2280                               (not (string-match gnus-article-x-face-too-ugly
2281                                                  from)))))
2282             (while (setq face (pop x-faces))
2283               ;; We display the face.
2284               (cond ((stringp gnus-article-x-face-command)
2285                      ;; The command is a string, so we interpret the command
2286                      ;; as a, well, command, and fork it off.
2287                      (let ((process-connection-type nil))
2288                        (gnus-set-process-query-on-exit-flag
2289                         (start-process
2290                          "article-x-face" nil shell-file-name
2291                          shell-command-switch gnus-article-x-face-command)
2292                         nil)
2293                        (with-temp-buffer
2294                          (insert face)
2295                          (process-send-region "article-x-face"
2296                                               (point-min) (point-max)))
2297                        (process-send-eof "article-x-face")))
2298                     ((functionp gnus-article-x-face-command)
2299                      ;; The command is a lisp function, so we call it.
2300                      (funcall gnus-article-x-face-command face))
2301                     (t
2302                      (error "%s is not a function"
2303                             gnus-article-x-face-command))))))))))
2304
2305 (defun article-decode-mime-words ()
2306   "Decode all MIME-encoded words in the article."
2307   (interactive)
2308   (gnus-with-article-buffer
2309     (let ((inhibit-point-motion-hooks t)
2310           (mail-parse-charset gnus-newsgroup-charset)
2311           (mail-parse-ignored-charsets
2312            (with-current-buffer gnus-summary-buffer
2313              gnus-newsgroup-ignored-charsets)))
2314       (mail-decode-encoded-word-region (point-min) (point-max)))))
2315
2316 (defun article-decode-charset (&optional prompt)
2317   "Decode charset-encoded text in the article.
2318 If PROMPT (the prefix), prompt for a coding system to use."
2319   (interactive "P")
2320   (let ((inhibit-point-motion-hooks t) (case-fold-search t)
2321         (inhibit-read-only t)
2322         (mail-parse-charset gnus-newsgroup-charset)
2323         (mail-parse-ignored-charsets
2324          (save-excursion (condition-case nil
2325                              (set-buffer gnus-summary-buffer)
2326                            (error))
2327                          gnus-newsgroup-ignored-charsets))
2328         ct cte ctl charset format)
2329     (save-excursion
2330       (save-restriction
2331         (article-narrow-to-head)
2332         (setq ct (message-fetch-field "Content-Type" t)
2333               cte (message-fetch-field "Content-Transfer-Encoding" t)
2334               ctl (and ct (mail-header-parse-content-type ct))
2335               charset (cond
2336                        (prompt
2337                         (mm-read-coding-system "Charset to decode: "))
2338                        (ctl
2339                         (mail-content-type-get ctl 'charset)))
2340               format (and ctl (mail-content-type-get ctl 'format)))
2341         (when cte
2342           (setq cte (mail-header-strip cte)))
2343         (if (and ctl (not (string-match "/" (car ctl))))
2344             (setq ctl nil))
2345         (goto-char (point-max)))
2346       (forward-line 1)
2347       (save-restriction
2348         (narrow-to-region (point) (point-max))
2349         (when (and (eq mail-parse-charset 'gnus-decoded)
2350                    (eq (mm-body-7-or-8) '8bit))
2351           ;; The text code could have been decoded.
2352           (setq charset mail-parse-charset))
2353         (when (and (or (not ctl)
2354                        (equal (car ctl) "text/plain"))
2355                    (not format)) ;; article with format will decode later.
2356           (mm-decode-body
2357            charset (and cte (intern (downcase
2358                                      (gnus-strip-whitespace cte))))
2359            (car ctl)))))))
2360
2361 (defun article-decode-encoded-words ()
2362   "Remove encoded-word encoding from headers."
2363   (let ((inhibit-point-motion-hooks t)
2364         (mail-parse-charset gnus-newsgroup-charset)
2365         (mail-parse-ignored-charsets
2366          (save-excursion (condition-case nil
2367                              (set-buffer gnus-summary-buffer)
2368                            (error))
2369                          gnus-newsgroup-ignored-charsets))
2370         (inhibit-read-only t))
2371     (save-restriction
2372       (article-narrow-to-head)
2373       (funcall gnus-decode-header-function (point-min) (point-max)))))
2374
2375 (defun article-decode-group-name ()
2376   "Decode group names in `Newsgroups:'."
2377   (let ((inhibit-point-motion-hooks t)
2378         (inhibit-read-only t)
2379         (method (gnus-find-method-for-group gnus-newsgroup-name)))
2380     (when (and (or gnus-group-name-charset-method-alist
2381                    gnus-group-name-charset-group-alist)
2382                (gnus-buffer-live-p gnus-original-article-buffer))
2383       (save-restriction
2384         (article-narrow-to-head)
2385         (with-current-buffer gnus-original-article-buffer
2386           (goto-char (point-min)))
2387         (while (re-search-forward
2388                 "^Newsgroups:\\(\\(.\\|\n[\t ]\\)*\\)\n[^\t ]" nil t)
2389           (replace-match (save-match-data
2390                            (gnus-decode-newsgroups
2391                             ;; XXX how to use data in article buffer?
2392                             (with-current-buffer gnus-original-article-buffer
2393                               (re-search-forward
2394                                "^Newsgroups:\\(\\(.\\|\n[\t ]\\)*\\)\n[^\t ]"
2395                                nil t)
2396                               (match-string 1))
2397                             gnus-newsgroup-name method))
2398                          t t nil 1))
2399         (goto-char (point-min))
2400         (with-current-buffer gnus-original-article-buffer
2401           (goto-char (point-min)))
2402         (while (re-search-forward
2403                 "^Followup-To:\\(\\(.\\|\n[\t ]\\)*\\)\n[^\t ]" nil t)
2404           (replace-match (save-match-data
2405                            (gnus-decode-newsgroups
2406                             ;; XXX how to use data in article buffer?
2407                             (with-current-buffer gnus-original-article-buffer
2408                               (re-search-forward
2409                                "^Followup-To:\\(\\(.\\|\n[\t ]\\)*\\)\n[^\t ]"
2410                                nil t)
2411                               (match-string 1))
2412                             gnus-newsgroup-name method))
2413                          t t nil 1))))))
2414
2415 (autoload 'idna-to-unicode "idna")
2416
2417 (defun article-decode-idna-rhs ()
2418   "Decode IDNA strings in RHS in various headers in current buffer.
2419 The following headers are decoded: From:, To:, Cc:, Reply-To:,
2420 Mail-Reply-To: and Mail-Followup-To:."
2421   (when gnus-use-idna
2422     (save-restriction
2423       (let ((inhibit-point-motion-hooks t)
2424             (inhibit-read-only t))
2425         (article-narrow-to-head)
2426         (goto-char (point-min))
2427         (while (re-search-forward "@[^ \t\n\r,>]*\\(xn--[-A-Za-z0-9.]*\\)[ \t\n\r,>]" nil t)
2428           (let (ace unicode)
2429             (when (save-match-data
2430                     (and (setq ace (match-string 1))
2431                          (save-excursion
2432                            (and (re-search-backward "^[^ \t]" nil t)
2433                                 (looking-at "From\\|To\\|Cc\\|Reply-To\\|Mail-Reply-To\\|Mail-Followup-To")))
2434                          (setq unicode (idna-to-unicode ace))))
2435               (unless (string= ace unicode)
2436                 (replace-match unicode nil nil nil 1)))))))))
2437
2438 (defun article-de-quoted-unreadable (&optional force read-charset)
2439   "Translate a quoted-printable-encoded article.
2440 If FORCE, decode the article whether it is marked as quoted-printable
2441 or not.
2442 If READ-CHARSET, ask for a coding system."
2443   (interactive (list 'force current-prefix-arg))
2444   (save-excursion
2445     (let ((inhibit-read-only t) type charset)
2446       (if (gnus-buffer-live-p gnus-original-article-buffer)
2447           (with-current-buffer gnus-original-article-buffer
2448             (setq type
2449                   (gnus-fetch-field "content-transfer-encoding"))
2450             (let* ((ct (gnus-fetch-field "content-type"))
2451                    (ctl (and ct (mail-header-parse-content-type ct))))
2452               (setq charset (and ctl
2453                                  (mail-content-type-get ctl 'charset)))
2454               (if (stringp charset)
2455                   (setq charset (intern (downcase charset)))))))
2456       (if read-charset
2457           (setq charset (mm-read-coding-system "Charset: " charset)))
2458       (unless charset
2459         (setq charset gnus-newsgroup-charset))
2460       (when (or force
2461                 (and type (let ((case-fold-search t))
2462                             (string-match "quoted-printable" type))))
2463         (article-goto-body)
2464         (quoted-printable-decode-region
2465          (point) (point-max) (mm-charset-to-coding-system charset))))))
2466
2467 (defun article-de-base64-unreadable (&optional force read-charset)
2468   "Translate a base64 article.
2469 If FORCE, decode the article whether it is marked as base64 not.
2470 If READ-CHARSET, ask for a coding system."
2471   (interactive (list 'force current-prefix-arg))
2472   (save-excursion
2473     (let ((inhibit-read-only t) type charset)
2474       (if (gnus-buffer-live-p gnus-original-article-buffer)
2475           (with-current-buffer gnus-original-article-buffer
2476             (setq type
2477                   (gnus-fetch-field "content-transfer-encoding"))
2478             (let* ((ct (gnus-fetch-field "content-type"))
2479                    (ctl (and ct (mail-header-parse-content-type ct))))
2480               (setq charset (and ctl
2481                                  (mail-content-type-get ctl 'charset)))
2482               (if (stringp charset)
2483                   (setq charset (intern (downcase charset)))))))
2484       (if read-charset
2485           (setq charset (mm-read-coding-system "Charset: " charset)))
2486       (unless charset
2487         (setq charset gnus-newsgroup-charset))
2488       (when (or force
2489                 (and type (let ((case-fold-search t))
2490                             (string-match "base64" type))))
2491         (article-goto-body)
2492         (save-restriction
2493           (narrow-to-region (point) (point-max))
2494           (base64-decode-region (point-min) (point-max))
2495           (mm-decode-coding-region
2496            (point-min) (point-max) (mm-charset-to-coding-system charset)))))))
2497
2498 (eval-when-compile
2499   (require 'rfc1843))
2500
2501 (defun article-decode-HZ ()
2502   "Translate a HZ-encoded article."
2503   (interactive)
2504   (require 'rfc1843)
2505   (save-excursion
2506     (let ((inhibit-read-only t))
2507       (rfc1843-decode-region (point-min) (point-max)))))
2508
2509 (defun article-unsplit-urls ()
2510   "Remove the newlines that some other mailers insert into URLs."
2511   (interactive)
2512   (save-excursion
2513     (let ((inhibit-read-only t))
2514       (goto-char (point-min))
2515       (while (re-search-forward
2516               "\\(\\(https?\\|ftp\\)://\\S-+\\) *\n\\(\\S-+\\)" nil t)
2517         (replace-match "\\1\\3" t)))
2518     (when (interactive-p)
2519       (gnus-treat-article nil))))
2520
2521
2522 (defun article-wash-html (&optional read-charset)
2523   "Format an HTML article.
2524 If READ-CHARSET, ask for a coding system.  If it is a number, the
2525 charset defined in `gnus-summary-show-article-charset-alist' is used."
2526   (interactive "P")
2527   (save-excursion
2528     (let ((inhibit-read-only t)
2529           charset)
2530       (if read-charset
2531           (if (or (and (numberp read-charset)
2532                        (setq charset
2533                              (cdr
2534                               (assq read-charset
2535                                     gnus-summary-show-article-charset-alist))))
2536                   (setq charset (mm-read-coding-system "Charset: ")))
2537               (let ((gnus-summary-show-article-charset-alist
2538                      (list (cons 1 charset))))
2539                 (with-current-buffer gnus-summary-buffer
2540                   (gnus-summary-show-article 1)))
2541             (error "No charset is given"))
2542         (when (gnus-buffer-live-p gnus-original-article-buffer)
2543           (with-current-buffer gnus-original-article-buffer
2544             (let* ((ct (gnus-fetch-field "content-type"))
2545                    (ctl (and ct (mail-header-parse-content-type ct))))
2546               (setq charset (and ctl
2547                                  (mail-content-type-get ctl 'charset)))
2548               (when (stringp charset)
2549                 (setq charset (intern (downcase charset)))))))
2550         (unless charset
2551           (setq charset gnus-newsgroup-charset)))
2552       (article-goto-body)
2553       (save-window-excursion
2554         (save-restriction
2555           (narrow-to-region (point) (point-max))
2556           (let* ((func (or gnus-article-wash-function mm-text-html-renderer))
2557                  (entry (assq func mm-text-html-washer-alist)))
2558             (when entry
2559               (setq func (cdr entry)))
2560             (cond
2561              ((functionp func)
2562               (funcall func))
2563              (t
2564               (apply (car func) (cdr func))))))))))
2565
2566 (defun gnus-article-wash-html-with-w3 ()
2567   "Wash the current buffer with w3."
2568   (mm-setup-w3)
2569   (let ((w3-strict-width (window-width))
2570         (url-standalone-mode t)
2571         (url-gateway-unplugged t)
2572         (w3-honor-stylesheets nil))
2573     (condition-case ()
2574         (w3-region (point-min) (point-max))
2575       (error))))
2576
2577 (defun gnus-article-wash-html-with-w3m ()
2578   "Wash the current buffer with emacs-w3m."
2579   (mm-setup-w3m)
2580   (let ((w3m-safe-url-regexp mm-w3m-safe-url-regexp)
2581         w3m-force-redisplay)
2582     (w3m-region (point-min) (point-max)))
2583   (when (and mm-inline-text-html-with-w3m-keymap
2584              (boundp 'w3m-minor-mode-map)
2585              w3m-minor-mode-map)
2586     (add-text-properties
2587      (point-min) (point-max)
2588      (list 'keymap w3m-minor-mode-map
2589            ;; Put the mark meaning this part was rendered by emacs-w3m.
2590            'mm-inline-text-html-with-w3m t))))
2591
2592 (eval-when-compile (defvar charset)) ;; Bound by `article-wash-html'.
2593
2594 (defun gnus-article-wash-html-with-w3m-standalone ()
2595   "Wash the current buffer with w3m."
2596   (if (mm-w3m-standalone-supports-m17n-p)
2597       (progn
2598         (unless (mm-coding-system-p charset) ;; Bound by `article-wash-html'.
2599           ;; The default.
2600           (setq charset 'iso-8859-1))
2601         (let ((coding-system-for-write charset)
2602               (coding-system-for-read charset))
2603           (call-process-region
2604            (point-min) (point-max)
2605            "w3m" t t nil "-dump" "-T" "text/html"
2606            "-I" (symbol-name charset) "-O" (symbol-name charset))))
2607     (mm-inline-wash-with-stdin nil "w3m" "-dump" "-T" "text/html")))
2608
2609 (defvar gnus-article-browse-html-temp nil
2610   "Temporary files created by `gnus-article-browse-html-parts'")
2611
2612 (defvar gnus-article-browse-delete-temp 'ask
2613   "Defines how to deal with temporary files created by
2614 `gnus-article-browse-html-parts':
2615 Possible values: t: Delete
2616                  ask: Ask before delete
2617                  nil: Don't delete")
2618
2619 (defun gnus-article-browse-delete-temp-files ()
2620   "Delete temp-files created by `gnus-article-browse-html-parts'."
2621   (when (and gnus-article-browse-html-temp
2622              (or (and (equal gnus-article-browse-delete-temp 'ask)
2623                       (y-or-n-p "Delete temporary files from showing HTML-mails? "))
2624                  (eq gnus-article-browse-delete-temp t)))
2625     (dolist (tmp-file gnus-article-browse-html-temp)
2626       (when (file-exists-p tmp-file)
2627         (delete-file tmp-file)))
2628     (setq gnus-article-browse-html-temp nil)))
2629
2630 (defun gnus-article-browse-html-parts (list)
2631   "View all \"text/html\" parts from LIST.
2632 Recurse into multiparts."
2633   ;; Internal function used by `gnus-article-browse-html-article'.
2634   (let ((showed))
2635     ;; Find and show the html-parts.
2636     (dolist (handle list)
2637       ;; If HTML, show it:
2638       (when (listp handle)
2639         (cond ((and (bufferp (car handle))
2640                     (string-match "text/html" (car (mm-handle-type handle))))
2641                (let ((tmp-file (mm-make-temp-file
2642                                 ;; Do we need to care for 8.3 filenames?
2643                                 "mm-" nil ".html")))
2644                  (mm-save-part-to-file handle tmp-file)
2645                  (add-to-list 'gnus-article-browse-html-temp tmp-file)
2646                  (browse-url tmp-file)
2647                  (setq showed t)))
2648               ;; If multipart, recurse
2649               ((and (stringp (car handle))
2650                     (string-match "^multipart/" (car handle))
2651                     (setq showed
2652                           (or showed
2653                               (gnus-article-browse-html-parts handle))))))))
2654     showed))
2655
2656 ;; TODO: Key binding
2657 (defun gnus-article-browse-html-article ()
2658   "View \"text/html\" parts of the current article with a WWW browser."
2659   (interactive)
2660   (save-window-excursion
2661     ;; Open raw article and select the buffer
2662     (gnus-summary-show-article t)
2663     (gnus-summary-select-article-buffer)
2664     (let ((parts (mm-dissect-buffer t t)))
2665       ;; If singlepart, enforce a list.
2666       (when (and (bufferp (car parts))
2667                  (stringp (car (mm-handle-type parts))))
2668         (setq parts (list parts)))
2669       ;; Process the list
2670       (unless (gnus-article-browse-html-parts parts)
2671         (gnus-error 3 "Mail doesn't contain a \"text/html\" part!"))
2672       (gnus-summary-show-article))))
2673
2674 (defun article-hide-list-identifiers ()
2675   "Remove list identifies from the Subject header.
2676 The `gnus-list-identifiers' variable specifies what to do."
2677   (interactive)
2678   (let ((inhibit-point-motion-hooks t)
2679         (regexp (if (consp gnus-list-identifiers)
2680                     (mapconcat 'identity gnus-list-identifiers " *\\|")
2681                   gnus-list-identifiers))
2682         (inhibit-read-only t))
2683     (when regexp
2684       (save-excursion
2685         (save-restriction
2686           (article-narrow-to-head)
2687           (goto-char (point-min))
2688           (while (re-search-forward
2689                   (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
2690                   nil t)
2691             (delete-region (match-beginning 2) (match-end 0))
2692             (beginning-of-line))
2693           (when (re-search-forward
2694                  "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" nil t)
2695             (delete-region (match-beginning 1) (match-end 1))))))))
2696
2697 (defun article-hide-pem (&optional arg)
2698   "Toggle hiding of any PEM headers and signatures in the current article.
2699 If given a negative prefix, always show; if given a positive prefix,
2700 always hide."
2701   (interactive (gnus-article-hidden-arg))
2702   (unless (gnus-article-check-hidden-text 'pem arg)
2703     (save-excursion
2704       (let ((inhibit-read-only t) end)
2705         (goto-char (point-min))
2706         ;; Hide the horrendously ugly "header".
2707         (when (and (search-forward
2708                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
2709                     nil t)
2710                    (setq end (1+ (match-beginning 0))))
2711           (gnus-add-wash-type 'pem)
2712           (gnus-article-hide-text-type
2713            end
2714            (if (search-forward "\n\n" nil t)
2715                (match-end 0)
2716              (point-max))
2717            'pem)
2718           ;; Hide the trailer as well
2719           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
2720                                 nil t)
2721             (gnus-article-hide-text-type
2722              (match-beginning 0) (match-end 0) 'pem)))))))
2723
2724 (defun article-strip-banner ()
2725   "Strip the banners specified by the `banner' group parameter and by
2726 `gnus-article-address-banner-alist'."
2727   (interactive)
2728   (save-excursion
2729     (save-restriction
2730       (let ((inhibit-point-motion-hooks t))
2731         (when (gnus-parameter-banner gnus-newsgroup-name)
2732           (article-really-strip-banner
2733            (gnus-parameter-banner gnus-newsgroup-name)))
2734         (when gnus-article-address-banner-alist
2735           ;; Note that the From header is decoded here, so it is
2736           ;; required that the *-extract-address-components function
2737           ;; supports non-ASCII text.
2738           (let ((from (save-restriction
2739                         (widen)
2740                         (article-narrow-to-head)
2741                         (mail-fetch-field "from"))))
2742             (when (and from
2743                        (setq from
2744                              (cadr (funcall gnus-extract-address-components
2745                                             from))))
2746               (catch 'found
2747                 (dolist (pair gnus-article-address-banner-alist)
2748                   (when (string-match (car pair) from)
2749                     (throw 'found
2750                            (article-really-strip-banner (cdr pair)))))))))))))
2751
2752 (defun article-really-strip-banner (banner)
2753   "Strip the banner specified by the argument."
2754   (save-excursion
2755     (save-restriction
2756       (let ((inhibit-point-motion-hooks t)
2757             (gnus-signature-limit nil)
2758             (inhibit-read-only t))
2759         (article-goto-body)
2760         (cond
2761          ((eq banner 'signature)
2762           (when (gnus-article-narrow-to-signature)
2763             (widen)
2764             (forward-line -1)
2765             (delete-region (point) (point-max))))
2766          ((symbolp banner)
2767           (if (setq banner (cdr (assq banner gnus-article-banner-alist)))
2768               (while (re-search-forward banner nil t)
2769                 (delete-region (match-beginning 0) (match-end 0)))))
2770          ((stringp banner)
2771           (while (re-search-forward banner nil t)
2772             (delete-region (match-beginning 0) (match-end 0)))))))))
2773
2774 (defun article-babel ()
2775   "Translate article using an online translation service."
2776   (interactive)
2777   (require 'babel)
2778   (gnus-with-article-buffer
2779     (when (article-goto-body)
2780       (let* ((start (point))
2781              (end (point-max))
2782              (orig (buffer-substring start end))
2783              (trans (babel-as-string orig)))
2784         (save-restriction
2785           (narrow-to-region start end)
2786           (delete-region start end)
2787           (insert trans))))))
2788
2789 (defun article-hide-signature (&optional arg)
2790   "Hide the signature in the current article.
2791 If given a negative prefix, always show; if given a positive prefix,
2792 always hide."
2793   (interactive (gnus-article-hidden-arg))
2794   (unless (gnus-article-check-hidden-text 'signature arg)
2795     (save-excursion
2796       (save-restriction
2797         (let ((inhibit-read-only t))
2798           (when (gnus-article-narrow-to-signature)
2799             (gnus-article-hide-text-type
2800              (point-min) (point-max) 'signature))))))
2801   (gnus-set-mode-line 'article))
2802
2803 (defun article-strip-headers-in-body ()
2804   "Strip offensive headers from bodies."
2805   (interactive)
2806   (save-excursion
2807     (article-goto-body)
2808     (let ((case-fold-search t))
2809       (when (looking-at "x-no-archive:")
2810         (gnus-delete-line)))))
2811
2812 (defun article-strip-leading-blank-lines ()
2813   "Remove all blank lines from the beginning of the article."
2814   (interactive)
2815   (save-excursion
2816     (let ((inhibit-point-motion-hooks t)
2817           (inhibit-read-only t))
2818       (when (article-goto-body)
2819         (while (and (not (eobp))
2820                     (looking-at "[ \t]*$"))
2821           (gnus-delete-line))))))
2822
2823 (defun article-narrow-to-head ()
2824   "Narrow the buffer to the head of the message.
2825 Point is left at the beginning of the narrowed-to region."
2826   (narrow-to-region
2827    (goto-char (point-min))
2828    (if (search-forward "\n\n" nil 1)
2829        (1- (point))
2830      (point-max)))
2831   (goto-char (point-min)))
2832
2833 (defun article-goto-body ()
2834   "Place point at the start of the body."
2835   (goto-char (point-min))
2836   (cond
2837    ;; This variable is only bound when dealing with separate
2838    ;; MIME body parts.
2839    (article-goto-body-goes-to-point-min-p
2840     t)
2841    ((search-forward "\n\n" nil t)
2842     t)
2843    (t
2844     (goto-char (point-max))
2845     nil)))
2846
2847 (defun article-strip-multiple-blank-lines ()
2848   "Replace consecutive blank lines with one empty line."
2849   (interactive)
2850   (save-excursion
2851     (let ((inhibit-point-motion-hooks t)
2852           (inhibit-read-only t))
2853       ;; First make all blank lines empty.
2854       (article-goto-body)
2855       (while (re-search-forward "^[ \t]+$" nil t)
2856         (unless (gnus-annotation-in-region-p
2857                  (match-beginning 0) (match-end 0))
2858           (replace-match "" nil t)))
2859       ;; Then replace multiple empty lines with a single empty line.
2860       (article-goto-body)
2861       (while (re-search-forward "\n\n\\(\n+\\)" nil t)
2862         (unless (gnus-annotation-in-region-p
2863                  (match-beginning 0) (match-end 0))
2864           (delete-region (match-beginning 1) (match-end 1)))))))
2865
2866 (defun article-strip-leading-space ()
2867   "Remove all white space from the beginning of the lines in the article."
2868   (interactive)
2869   (save-excursion
2870     (let ((inhibit-point-motion-hooks t)
2871           (inhibit-read-only t))
2872       (article-goto-body)
2873       (while (re-search-forward "^[ \t]+" nil t)
2874         (replace-match "" t t)))))
2875
2876 (defun article-strip-trailing-space ()
2877   "Remove all white space from the end of the lines in the article."
2878   (interactive)
2879   (save-excursion
2880     (let ((inhibit-point-motion-hooks t)
2881           (inhibit-read-only t))
2882       (article-goto-body)
2883       (while (re-search-forward "[ \t]+$" nil t)
2884         (replace-match "" t t)))))
2885
2886 (defun article-strip-blank-lines ()
2887   "Strip leading, trailing and multiple blank lines."
2888   (interactive)
2889   (article-strip-leading-blank-lines)
2890   (article-remove-trailing-blank-lines)
2891   (article-strip-multiple-blank-lines))
2892
2893 (defun article-strip-all-blank-lines ()
2894   "Strip all blank lines."
2895   (interactive)
2896   (save-excursion
2897     (let ((inhibit-point-motion-hooks t)
2898           (inhibit-read-only t))
2899       (article-goto-body)
2900       (while (re-search-forward "^[ \t]*\n" nil t)
2901         (replace-match "" t t)))))
2902
2903 (defun gnus-article-narrow-to-signature ()
2904   "Narrow to the signature; return t if a signature is found, else nil."
2905   (let ((inhibit-point-motion-hooks t))
2906     (when (gnus-article-search-signature)
2907       (forward-line 1)
2908       ;; Check whether we have some limits to what we consider
2909       ;; to be a signature.
2910       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
2911                       (list gnus-signature-limit)))
2912             limit limited)
2913         (while (setq limit (pop limits))
2914           (if (or (and (integerp limit)
2915                        (< (- (point-max) (point)) limit))
2916                   (and (floatp limit)
2917                        (< (count-lines (point) (point-max)) limit))
2918                   (and (functionp limit)
2919                        (funcall limit))
2920                   (and (stringp limit)
2921                        (not (re-search-forward limit nil t))))
2922               ()                        ; This limit did not succeed.
2923             (setq limited t
2924                   limits nil)))
2925         (unless limited
2926           (narrow-to-region (point) (point-max))
2927           t)))))
2928
2929 (defun gnus-article-search-signature ()
2930   "Search the current buffer for the signature separator.
2931 Put point at the beginning of the signature separator."
2932   (let ((cur (point)))
2933     (goto-char (point-max))
2934     (if (if (stringp gnus-signature-separator)
2935             (re-search-backward gnus-signature-separator nil t)
2936           (let ((seps gnus-signature-separator))
2937             (while (and seps
2938                         (not (re-search-backward (car seps) nil t)))
2939               (pop seps))
2940             seps))
2941         t
2942       (goto-char cur)
2943       nil)))
2944
2945 (defun gnus-article-hidden-arg ()
2946   "Return the current prefix arg as a number, or 0 if no prefix."
2947   (list (if current-prefix-arg
2948             (prefix-numeric-value current-prefix-arg)
2949           0)))
2950
2951 (defun gnus-article-check-hidden-text (type arg)
2952   "Return nil if hiding is necessary.
2953 Arg can be nil or a number.  nil and positive means hide, negative
2954 means show, 0 means toggle."
2955   (save-excursion
2956     (save-restriction
2957       (let ((hide (gnus-article-hidden-text-p type)))
2958         (cond
2959          ((or (null arg)
2960               (> arg 0))
2961           nil)
2962          ((< arg 0)
2963           (gnus-article-show-hidden-text type)
2964           t)
2965          (t
2966           (if (eq hide 'hidden)
2967               (progn
2968                 (gnus-article-show-hidden-text type)
2969                 t)
2970             nil)))))))
2971
2972 (defun gnus-article-hidden-text-p (type)
2973   "Say whether the current buffer contains hidden text of type TYPE."
2974   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
2975     (while (and pos
2976                 (not (get-text-property pos 'invisible))
2977                 (not (get-text-property pos 'dummy-invisible)))
2978       (setq pos
2979             (text-property-any (1+ pos) (point-max) 'article-type type)))
2980     (if pos
2981         'hidden
2982       nil)))
2983
2984 (defun gnus-article-show-hidden-text (type &optional dummy)
2985   "Show all hidden text of type TYPE.
2986 Originally it is hide instead of DUMMY."
2987   (let ((inhibit-read-only t)
2988         (inhibit-point-motion-hooks t))
2989     (gnus-remove-text-properties-when
2990      'article-type type
2991      (point-min) (point-max)
2992      (cons 'article-type (cons type
2993                                gnus-hidden-properties)))
2994     (gnus-delete-wash-type type)))
2995
2996 (defconst article-time-units
2997   `((year . ,(* 365.25 24 60 60))
2998     (week . ,(* 7 24 60 60))
2999     (day . ,(* 24 60 60))
3000     (hour . ,(* 60 60))
3001     (minute . 60)
3002     (second . 1))
3003   "Mapping from time units to seconds.")
3004
3005 (defun gnus-article-forward-header ()
3006   "Move point to the start of the next header.
3007 If the current header is a continuation header, this can be several
3008 lines forward."
3009   (let ((ended nil))
3010     (while (not ended)
3011       (forward-line 1)
3012       (if (looking-at "[ \t]+[^ \t]")
3013           (forward-line 1)
3014         (setq ended t)))))
3015
3016 (defun article-date-ut (&optional type highlight)
3017   "Convert DATE date to universal time in the current article.
3018 If TYPE is `local', convert to local time; if it is `lapsed', output
3019 how much time has lapsed since DATE.  For `lapsed', the value of
3020 `gnus-article-date-lapsed-new-header' says whether the \"X-Sent:\" header
3021 should replace the \"Date:\" one, or should be added below it."
3022   (interactive (list 'ut t))
3023   (let* ((tdate-regexp "^Date:[ \t]\\|^X-Sent:[ \t]")
3024          (date-regexp (cond ((not gnus-article-date-lapsed-new-header)
3025                              tdate-regexp)
3026                             ((eq type 'lapsed)
3027                              "^X-Sent:[ \t]")
3028                             (article-lapsed-timer
3029                              "^Date:[ \t]")
3030                             (t
3031                              tdate-regexp)))
3032          (case-fold-search t)
3033          (inhibit-read-only t)
3034          (inhibit-point-motion-hooks t)
3035          pos date bface eface)
3036     (save-excursion
3037       (save-restriction
3038         (widen)
3039         (goto-char (point-min))
3040         (while (or (setq date (get-text-property (setq pos (point))
3041                                                  'original-date))
3042                    (when (setq pos (next-single-property-change
3043                                     (point) 'original-date))
3044                      (setq date (get-text-property pos 'original-date))
3045                      t))
3046           (narrow-to-region pos (or (text-property-any pos (point-max)
3047                                                        'original-date nil)
3048                                     (point-max)))
3049           (goto-char (point-min))
3050           (when (re-search-forward tdate-regexp nil t)
3051             (setq bface (get-text-property (point-at-bol) 'face)
3052                   eface (get-text-property (1- (point-at-eol)) 'face)))
3053           (goto-char (point-min))
3054           (setq pos nil)
3055           ;; Delete any old Date headers.
3056           (while (re-search-forward date-regexp nil t)
3057             (if pos
3058                 (delete-region (point-at-bol) (progn
3059                                                 (gnus-article-forward-header)
3060                                                 (point)))
3061               (delete-region (point-at-bol) (progn
3062                                               (gnus-article-forward-header)
3063                                               (forward-char -1)
3064                                               (point)))
3065               (setq pos (point))))
3066           (when (and (not pos)
3067                      (re-search-forward tdate-regexp nil t))
3068             (forward-line 1))
3069           (gnus-goto-char pos)
3070           (insert (article-make-date-line date (or type 'ut)))
3071           (unless pos
3072             (insert "\n")
3073             (forward-line -1))
3074           ;; Do highlighting.
3075           (beginning-of-line)
3076           (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
3077             (put-text-property (match-beginning 1) (1+ (match-end 1))
3078                                'face bface)
3079             (put-text-property (match-beginning 2) (match-end 2)
3080                                'face eface))
3081           (put-text-property (point-min) (1- (point-max)) 'original-date date)
3082           (goto-char (point-max))
3083           (widen))))))
3084
3085 (defun article-make-date-line (date type)
3086   "Return a DATE line of TYPE."
3087   (unless (memq type '(local ut original user iso8601 lapsed english))
3088     (error "Unknown conversion type: %s" type))
3089   (condition-case ()
3090       (let ((time (date-to-time date)))
3091         (cond
3092          ;; Convert to the local timezone.
3093          ((eq type 'local)
3094           (concat "Date: " (message-make-date time)))
3095          ;; Convert to Universal Time.
3096          ((eq type 'ut)
3097           (concat "Date: "
3098                   (substring
3099                    (message-make-date
3100                     (let* ((e (parse-time-string date))
3101                            (tm (apply 'encode-time e))
3102                            (ms (car tm))
3103                            (ls (- (cadr tm) (car (current-time-zone time)))))
3104                       (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
3105                             ((> ls 65535) (list (1+ ms) (- ls 65536)))
3106                             (t (list ms ls)))))
3107                    0 -5)
3108                   "UT"))
3109          ;; Get the original date from the article.
3110          ((eq type 'original)
3111           (concat "Date: " (if (string-match "\n+$" date)
3112                                (substring date 0 (match-beginning 0))
3113                              date)))
3114          ;; Let the user define the format.
3115          ((eq type 'user)
3116           (let ((format (or (condition-case nil
3117                                 (with-current-buffer gnus-summary-buffer
3118                                   gnus-article-time-format)
3119                               (error nil))
3120                             gnus-article-time-format)))
3121             (if (functionp format)
3122                 (funcall format time)
3123               (concat "Date: " (format-time-string format time)))))
3124          ;; ISO 8601.
3125          ((eq type 'iso8601)
3126           (let ((tz (car (current-time-zone time))))
3127             (concat
3128              "Date: "
3129              (format-time-string "%Y%m%dT%H%M%S" time)
3130              (format "%s%02d%02d"
3131                      (if (> tz 0) "+" "-") (/ (abs tz) 3600)
3132                      (/ (% (abs tz) 3600) 60)))))
3133          ;; Do an X-Sent lapsed format.
3134          ((eq type 'lapsed)
3135           ;; If the date is seriously mangled, the timezone functions are
3136           ;; liable to bug out, so we ignore all errors.
3137           (let* ((now (current-time))
3138                  (real-time (subtract-time now time))
3139                  (real-sec (and real-time
3140                                 (+ (* (float (car real-time)) 65536)
3141                                    (cadr real-time))))
3142                  (sec (and real-time (abs real-sec)))
3143                  num prev)
3144             (cond
3145              ((null real-time)
3146               "X-Sent: Unknown")
3147              ((zerop sec)
3148               "X-Sent: Now")
3149              (t
3150               (concat
3151                "X-Sent: "
3152                ;; This is a bit convoluted, but basically we go
3153                ;; through the time units for years, weeks, etc,
3154                ;; and divide things to see whether that results
3155                ;; in positive answers.
3156                (mapconcat
3157                 (lambda (unit)
3158                   (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
3159                       ;; The (remaining) seconds are too few to
3160                       ;; be divided into this time unit.
3161                       ""
3162                     ;; It's big enough, so we output it.
3163                     (setq sec (- sec (* num (cdr unit))))
3164                     (prog1
3165                         (concat (if prev ", " "") (int-to-string
3166                                                    (floor num))
3167                                 " " (symbol-name (car unit))
3168                                 (if (> num 1) "s" ""))
3169                       (setq prev t))))
3170                 article-time-units "")
3171                ;; If dates are odd, then it might appear like the
3172                ;; article was sent in the future.
3173                (if (> real-sec 0)
3174                    " ago"
3175                  " in the future"))))))
3176          ;; Display the date in proper English
3177          ((eq type 'english)
3178           (let ((dtime (decode-time time)))
3179             (concat
3180              "Date: the "
3181              (number-to-string (nth 3 dtime))
3182              (let ((digit (% (nth 3 dtime) 10)))
3183                (cond
3184                 ((memq (nth 3 dtime) '(11 12 13)) "th")
3185                 ((= digit 1) "st")
3186                 ((= digit 2) "nd")
3187                 ((= digit 3) "rd")
3188                 (t "th")))
3189              " of "
3190              (nth (1- (nth 4 dtime)) gnus-english-month-names)
3191              " "
3192              (number-to-string (nth 5 dtime))
3193              " at "
3194              (format "%02d" (nth 2 dtime))
3195              ":"
3196              (format "%02d" (nth 1 dtime)))))))
3197     (error
3198      (format "Date: %s (from Gnus)" date))))
3199
3200 (defun article-date-local (&optional highlight)
3201   "Convert the current article date to the local timezone."
3202   (interactive (list t))
3203   (article-date-ut 'local highlight))
3204
3205 (defun article-date-english (&optional highlight)
3206   "Convert the current article date to something that is proper English."
3207   (interactive (list t))
3208   (article-date-ut 'english highlight))
3209
3210 (defun article-date-original (&optional highlight)
3211   "Convert the current article date to what it was originally.
3212 This is only useful if you have used some other date conversion
3213 function and want to see what the date was before converting."
3214   (interactive (list t))
3215   (article-date-ut 'original highlight))
3216
3217 (defun article-date-lapsed (&optional highlight)
3218   "Convert the current article date to time lapsed since it was sent."
3219   (interactive (list t))
3220   (article-date-ut 'lapsed highlight))
3221
3222 (defun article-update-date-lapsed ()
3223   "Function to be run from a timer to update the lapsed time line."
3224   (save-match-data
3225     (let (deactivate-mark)
3226       (save-excursion
3227         (ignore-errors
3228          (walk-windows
3229           (lambda (w)
3230             (set-buffer (window-buffer w))
3231             (when (eq major-mode 'gnus-article-mode)
3232               (let ((mark (point-marker)))
3233                 (goto-char (point-min))
3234                 (when (re-search-forward "^X-Sent:" nil t)
3235                   (article-date-lapsed t))
3236                 (goto-char (marker-position mark))
3237                 (move-marker mark nil))))
3238           nil 'visible))))))
3239
3240 (defun gnus-start-date-timer (&optional n)
3241   "Start a timer to update the X-Sent header in the article buffers.
3242 The numerical prefix says how frequently (in seconds) the function
3243 is to run."
3244   (interactive "p")
3245   (unless n
3246     (setq n 1))
3247   (gnus-stop-date-timer)
3248   (setq article-lapsed-timer
3249         (run-at-time 1 n 'article-update-date-lapsed)))
3250
3251 (defun gnus-stop-date-timer ()
3252   "Stop the X-Sent timer."
3253   (interactive)
3254   (when article-lapsed-timer
3255     (nnheader-cancel-timer article-lapsed-timer)
3256     (setq article-lapsed-timer nil)))
3257
3258 (defun article-date-user (&optional highlight)
3259   "Convert the current article date to the user-defined format.
3260 This format is defined by the `gnus-article-time-format' variable."
3261   (interactive (list t))
3262   (article-date-ut 'user highlight))
3263
3264 (defun article-date-iso8601 (&optional highlight)
3265   "Convert the current article date to ISO8601."
3266   (interactive (list t))
3267   (article-date-ut 'iso8601 highlight))
3268
3269 (defmacro gnus-article-save-original-date (&rest forms)
3270   "Save the original date as a text property and evaluate FORMS."
3271   `(let* ((case-fold-search t)
3272           (start (progn
3273                    (goto-char (point-min))
3274                    (when (and (re-search-forward "^date:[\t\n ]+" nil t)
3275                               (not (bolp)))
3276                      (match-end 0))))
3277           (date (when (and start
3278                            (re-search-forward "[\t ]*\n\\(?:[^\t ]\\|\\'\\)"
3279                                               nil t))
3280                   (buffer-substring-no-properties start
3281                                                   (match-beginning 0)))))
3282      (goto-char (point-max))
3283      (skip-chars-backward "\n")
3284      (put-text-property (point-min) (point) 'original-date date)
3285      ,@forms
3286      (goto-char (point-max))
3287      (skip-chars-backward "\n")
3288      (put-text-property (point-min) (point) 'original-date date)))
3289
3290 ;; (defun article-show-all ()
3291 ;;   "Show all hidden text in the article buffer."
3292 ;;   (interactive)
3293 ;;   (save-excursion
3294 ;;     (let ((inhibit-read-only t))
3295 ;;       (gnus-article-unhide-text (point-min) (point-max)))))
3296
3297 (defun article-remove-leading-whitespace ()
3298   "Remove excessive whitespace from all headers."
3299   (interactive)
3300   (save-excursion
3301     (save-restriction
3302       (let ((inhibit-read-only t))
3303         (article-narrow-to-head)
3304         (goto-char (point-min))
3305         (while (re-search-forward "^[^ :]+: \\([ \t]+\\)" nil t)
3306           (delete-region (match-beginning 1) (match-end 1)))))))
3307
3308 (defun article-emphasize (&optional arg)
3309   "Emphasize text according to `gnus-emphasis-alist'."
3310   (interactive (gnus-article-hidden-arg))
3311   (unless (gnus-article-check-hidden-text 'emphasis arg)
3312     (save-excursion
3313       (let ((alist (or
3314                     (condition-case nil
3315                         (with-current-buffer gnus-summary-buffer
3316                           gnus-article-emphasis-alist)
3317                       (error))
3318                     gnus-emphasis-alist))
3319             (inhibit-read-only t)
3320             (props (append '(article-type emphasis)
3321                            gnus-hidden-properties))
3322             regexp elem beg invisible visible face)
3323         (article-goto-body)
3324         (setq beg (point))
3325         (while (setq elem (pop alist))
3326           (goto-char beg)
3327           (setq regexp (car elem)
3328                 invisible (nth 1 elem)
3329                 visible (nth 2 elem)
3330                 face (nth 3 elem))
3331           (while (re-search-forward regexp nil t)
3332             (when (and (match-beginning visible) (match-beginning invisible))
3333               (gnus-article-hide-text
3334                (match-beginning invisible) (match-end invisible) props)
3335               (gnus-article-unhide-text-type
3336                (match-beginning visible) (match-end visible) 'emphasis)
3337               (gnus-put-overlay-excluding-newlines
3338                (match-beginning visible) (match-end visible) 'face face)
3339               (gnus-add-wash-type 'emphasis)
3340               (goto-char (match-end invisible)))))))))
3341
3342 (defun gnus-article-setup-highlight-words (&optional highlight-words)
3343   "Setup newsgroup emphasis alist."
3344   (unless gnus-article-emphasis-alist
3345     (let ((name (and gnus-newsgroup-name
3346                      (gnus-group-real-name gnus-newsgroup-name))))
3347       (make-local-variable 'gnus-article-emphasis-alist)
3348       (setq gnus-article-emphasis-alist
3349             (nconc
3350              (let ((alist gnus-group-highlight-words-alist) elem highlight)
3351                (while (setq elem (pop alist))
3352                  (when (and name (string-match (car elem) name))
3353                    (setq alist nil
3354                          highlight (copy-sequence (cdr elem)))))
3355                highlight)
3356              (copy-sequence highlight-words)
3357              (if gnus-newsgroup-name
3358                  (copy-sequence (gnus-group-find-parameter
3359                                  gnus-newsgroup-name 'highlight-words t)))
3360              gnus-emphasis-alist)))))
3361
3362 (eval-when-compile
3363   (defvar gnus-summary-article-menu)
3364   (defvar gnus-summary-post-menu))
3365
3366 ;;; Saving functions.
3367
3368 (defun gnus-article-save (save-buffer file &optional num)
3369   "Save the currently selected article."
3370   (unless gnus-save-all-headers
3371     ;; Remove headers according to `gnus-saved-headers'.
3372     (let ((gnus-visible-headers
3373            (or gnus-saved-headers gnus-visible-headers))
3374           (gnus-article-buffer save-buffer))
3375       (save-excursion
3376         (set-buffer save-buffer)
3377         (article-hide-headers 1 t))))
3378   (save-window-excursion
3379     (if (not gnus-default-article-saver)
3380         (error "No default saver is defined")
3381       ;; !!! Magic!  The saving functions all save
3382       ;; `gnus-save-article-buffer' (or so they think), but we
3383       ;; bind that variable to our save-buffer.
3384       (set-buffer gnus-article-buffer)
3385       (let* ((gnus-save-article-buffer save-buffer)
3386              (filename
3387               (cond
3388                ((not gnus-prompt-before-saving) 'default)
3389                ((eq gnus-prompt-before-saving 'always) nil)
3390                (t file)))
3391              (gnus-number-of-articles-to-be-saved
3392               (when (eq gnus-prompt-before-saving t)
3393                 num)))                  ; Magic
3394         (set-buffer gnus-article-current-summary)
3395         (funcall gnus-default-article-saver filename)))))
3396
3397 (defun gnus-read-save-file-name (prompt &optional filename
3398                                         function group headers variable)
3399   (let ((default-name
3400           (funcall function group headers (symbol-value variable)))
3401         result)
3402     (setq result
3403           (expand-file-name
3404            (cond
3405             ((eq filename 'default)
3406              default-name)
3407             ((eq filename t)
3408              default-name)
3409             (filename filename)
3410             (t
3411              (let* ((split-name (gnus-get-split-value gnus-split-methods))
3412                     (prompt
3413                      (format prompt
3414                              (if (and gnus-number-of-articles-to-be-saved
3415                                       (> gnus-number-of-articles-to-be-saved 1))
3416                                  (format "these %d articles"
3417                                          gnus-number-of-articles-to-be-saved)
3418                                "this article")))
3419                     (file
3420                      ;; Let the split methods have their say.
3421                      (cond
3422                       ;; No split name was found.
3423                       ((null split-name)
3424                        (read-file-name
3425                         (concat prompt " (default "
3426                                 (file-name-nondirectory default-name) "): ")
3427                         (file-name-directory default-name)
3428                         default-name))
3429                       ;; A single group name is returned.
3430                       ((stringp split-name)
3431                        (setq default-name
3432                              (funcall function split-name headers
3433                                       (symbol-value variable)))
3434                        (read-file-name
3435                         (concat prompt " (default "
3436                                 (file-name-nondirectory default-name) "): ")
3437                         (file-name-directory default-name)
3438                         default-name))
3439                       ;; A single split name was found
3440                       ((= 1 (length split-name))
3441                        (let* ((name (expand-file-name
3442                                      (car split-name)
3443                                      gnus-article-save-directory))
3444                               (dir (cond ((file-directory-p name)
3445                                           (file-name-as-directory name))
3446                                          ((file-exists-p name) name)
3447                                          (t gnus-article-save-directory))))
3448                          (read-file-name
3449                           (concat prompt " (default " name "): ")
3450                           dir name)))
3451                       ;; A list of splits was found.
3452                       (t
3453                        (setq split-name (nreverse split-name))
3454                        (let (result)
3455                          (let ((file-name-history
3456                                 (nconc split-name file-name-history)))
3457                            (setq result
3458                                  (expand-file-name
3459                                   (read-file-name
3460                                    (concat prompt " (`M-p' for defaults): ")
3461                                    gnus-article-save-directory
3462                                    (car split-name))
3463                                   gnus-article-save-directory)))
3464                          (car (push result file-name-history)))))))
3465                ;; Create the directory.
3466                (gnus-make-directory (file-name-directory file))
3467                ;; If we have read a directory, we append the default file name.
3468                (when (file-directory-p file)
3469                  (setq file (expand-file-name (file-name-nondirectory
3470                                                default-name)
3471                                               (file-name-as-directory file))))
3472                ;; Possibly translate some characters.
3473                (nnheader-translate-file-chars file))))))
3474     (gnus-make-directory (file-name-directory result))
3475     (set variable result)))
3476
3477 (defun gnus-article-archive-name (group)
3478   "Return the first instance of an \"Archive-name\" in the current buffer."
3479   (let ((case-fold-search t))
3480     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
3481       (nnheader-concat gnus-article-save-directory
3482                        (match-string 1)))))
3483
3484 (defun gnus-article-nndoc-name (group)
3485   "If GROUP is an nndoc group, return the name of the parent group."
3486   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
3487     (gnus-group-get-parameter group 'save-article-group)))
3488
3489 (defun gnus-summary-save-in-rmail (&optional filename)
3490   "Append this article to Rmail file.
3491 Optional argument FILENAME specifies file name.
3492 Directory to save to is default to `gnus-article-save-directory'."
3493   (setq filename (gnus-read-save-file-name
3494                   "Save %s in rmail file" filename
3495                   gnus-rmail-save-name gnus-newsgroup-name
3496                   gnus-current-headers 'gnus-newsgroup-last-rmail))
3497   (gnus-eval-in-buffer-window gnus-save-article-buffer
3498     (save-excursion
3499       (save-restriction
3500         (widen)
3501         (gnus-output-to-rmail filename))))
3502   filename)
3503
3504 (defun gnus-summary-save-in-mail (&optional filename)
3505   "Append this article to Unix mail file.
3506 Optional argument FILENAME specifies file name.
3507 Directory to save to is default to `gnus-article-save-directory'."
3508   (setq filename (gnus-read-save-file-name
3509                   "Save %s in Unix mail file" filename
3510                   gnus-mail-save-name gnus-newsgroup-name
3511                   gnus-current-headers 'gnus-newsgroup-last-mail))
3512   (gnus-eval-in-buffer-window gnus-save-article-buffer
3513     (save-excursion
3514       (save-restriction
3515         (widen)
3516         (if (and (file-readable-p filename)
3517                  (file-regular-p filename)
3518                  (mail-file-babyl-p filename))
3519             (rmail-output-to-rmail-file filename t)
3520           (gnus-output-to-mail filename)))))
3521   filename)
3522
3523 (defun gnus-summary-save-in-file (&optional filename overwrite)
3524   "Append this article to file.
3525 Optional argument FILENAME specifies file name.
3526 Directory to save to is default to `gnus-article-save-directory'."
3527   (setq filename (gnus-read-save-file-name
3528                   "Save %s in file" filename
3529                   gnus-file-save-name gnus-newsgroup-name
3530                   gnus-current-headers 'gnus-newsgroup-last-file))
3531   (gnus-eval-in-buffer-window gnus-save-article-buffer
3532     (save-excursion
3533       (save-restriction
3534         (widen)
3535         (when (and overwrite
3536                    (file-exists-p filename))
3537           (delete-file filename))
3538         (gnus-output-to-file filename))))
3539   filename)
3540
3541 (defun gnus-summary-write-to-file (&optional filename)
3542   "Write this article to a file, overwriting it if the file exists.
3543 Optional argument FILENAME specifies file name.
3544 The directory to save in defaults to `gnus-article-save-directory'."
3545   (gnus-summary-save-in-file nil t))
3546
3547 (defun gnus-summary-save-body-in-file (&optional filename)
3548   "Append this article body to a file.
3549 Optional argument FILENAME specifies file name.
3550 The directory to save in defaults to `gnus-article-save-directory'."
3551   (setq filename (gnus-read-save-file-name
3552                   "Save %s body in file" filename
3553                   gnus-file-save-name gnus-newsgroup-name
3554                   gnus-current-headers 'gnus-newsgroup-last-file))
3555   (gnus-eval-in-buffer-window gnus-save-article-buffer
3556     (save-excursion
3557       (save-restriction
3558         (widen)
3559         (when (article-goto-body)
3560           (narrow-to-region (point) (point-max)))
3561         (gnus-output-to-file filename))))
3562   filename)
3563
3564 (defun gnus-summary-save-in-pipe (&optional command)
3565   "Pipe this article to subprocess."
3566   (setq command
3567         (cond ((and (eq command 'default)
3568                     gnus-last-shell-command)
3569                gnus-last-shell-command)
3570               ((stringp command)
3571                command)
3572               (t (read-string
3573                   (format
3574                    "Shell command on %s: "
3575                    (if (and gnus-number-of-articles-to-be-saved
3576                             (> gnus-number-of-articles-to-be-saved 1))
3577                        (format "these %d articles"
3578                                gnus-number-of-articles-to-be-saved)
3579                      "this article"))
3580                   gnus-last-shell-command))))
3581   (when (string-equal command "")
3582     (if gnus-last-shell-command
3583         (setq command gnus-last-shell-command)
3584       (error "A command is required")))
3585   (gnus-eval-in-buffer-window gnus-article-buffer
3586     (save-restriction
3587       (widen)
3588       (shell-command-on-region (point-min) (point-max) command nil)))
3589   (setq gnus-last-shell-command command))
3590
3591 (defun gnus-summary-pipe-to-muttprint (&optional command)
3592   "Pipe this article to muttprint."
3593   (setq command (read-string
3594                  "Print using command: " gnus-summary-muttprint-program
3595                  nil gnus-summary-muttprint-program))
3596   (gnus-summary-save-in-pipe command))
3597
3598 ;;; Article file names when saving.
3599
3600 (defun gnus-capitalize-newsgroup (newsgroup)
3601   "Capitalize NEWSGROUP name."
3602   (when (not (zerop (length newsgroup)))
3603     (concat (char-to-string (upcase (aref newsgroup 0)))
3604             (substring newsgroup 1))))
3605
3606 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
3607   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
3608 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
3609 Otherwise, it is like ~/News/news/group/num."
3610   (let ((default
3611           (expand-file-name
3612            (concat (if (gnus-use-long-file-name 'not-save)
3613                        (gnus-capitalize-newsgroup newsgroup)
3614                      (gnus-newsgroup-directory-form newsgroup))
3615                    "/" (int-to-string (mail-header-number headers)))
3616            gnus-article-save-directory)))
3617     (if (and last-file
3618              (string-equal (file-name-directory default)
3619                            (file-name-directory last-file))
3620              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
3621         default
3622       (or last-file default))))
3623
3624 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
3625   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
3626 If variable `gnus-use-long-file-name' is non-nil, it is
3627 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
3628   (let ((default
3629           (expand-file-name
3630            (concat (if (gnus-use-long-file-name 'not-save)
3631                        newsgroup
3632                      (gnus-newsgroup-directory-form newsgroup))
3633                    "/" (int-to-string (mail-header-number headers)))
3634            gnus-article-save-directory)))
3635     (if (and last-file
3636              (string-equal (file-name-directory default)
3637                            (file-name-directory last-file))
3638              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
3639         default
3640       (or last-file default))))
3641
3642 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
3643   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
3644 If variable `gnus-use-long-file-name' is non-nil, it is
3645 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
3646   (or last-file
3647       (expand-file-name
3648        (if (gnus-use-long-file-name 'not-save)
3649            newsgroup
3650          (file-relative-name
3651           (expand-file-name "news" (gnus-newsgroup-directory-form newsgroup))
3652           default-directory))
3653        gnus-article-save-directory)))
3654
3655 (defun gnus-sender-save-name (newsgroup headers &optional last-file)
3656   "Generate file name from sender."
3657   (let ((from (mail-header-from headers)))
3658     (expand-file-name
3659      (if (and from (string-match "\\([^ <]+\\)@" from))
3660          (match-string 1 from)
3661        "nobody")
3662      gnus-article-save-directory)))
3663
3664 (defun article-verify-x-pgp-sig ()
3665   "Verify X-PGP-Sig."
3666   (interactive)
3667   (if (gnus-buffer-live-p gnus-original-article-buffer)
3668       (let ((sig (with-current-buffer gnus-original-article-buffer
3669                    (gnus-fetch-field "X-PGP-Sig")))
3670             items info headers)
3671         (when (and sig
3672                    mml2015-use
3673                    (mml2015-clear-verify-function))
3674           (with-temp-buffer
3675             (insert-buffer-substring gnus-original-article-buffer)
3676             (setq items (split-string sig))
3677             (message-narrow-to-head)
3678             (let ((inhibit-point-motion-hooks t)
3679                   (case-fold-search t))
3680               ;; Don't verify multiple headers.
3681               (setq headers (mapconcat (lambda (header)
3682                                          (concat header ": "
3683                                                  (mail-fetch-field header)
3684                                                  "\n"))
3685                                        (split-string (nth 1 items) ",") "")))
3686             (delete-region (point-min) (point-max))
3687             (insert "-----BEGIN PGP SIGNED MESSAGE-----\n\n")
3688             (insert "X-Signed-Headers: " (nth 1 items) "\n")
3689             (insert headers)
3690             (widen)
3691             (forward-line)
3692             (while (not (eobp))
3693               (if (looking-at "^-")
3694                   (insert "- "))
3695               (forward-line))
3696             (insert "\n-----BEGIN PGP SIGNATURE-----\n")
3697             (insert "Version: " (car items) "\n\n")
3698             (insert (mapconcat 'identity (cddr items) "\n"))
3699             (insert "\n-----END PGP SIGNATURE-----\n")
3700             (let ((mm-security-handle (list (format "multipart/signed"))))
3701               (mml2015-clean-buffer)
3702               (let ((coding-system-for-write (or gnus-newsgroup-charset
3703                                                  'iso-8859-1)))
3704                 (funcall (mml2015-clear-verify-function)))
3705               (setq info
3706                     (or (mm-handle-multipart-ctl-parameter
3707                          mm-security-handle 'gnus-details)
3708                         (mm-handle-multipart-ctl-parameter
3709                          mm-security-handle 'gnus-info)))))
3710           (when info
3711             (let ((inhibit-read-only t) bface eface)
3712               (save-restriction
3713                 (message-narrow-to-head)
3714                 (goto-char (point-max))
3715                 (forward-line -1)
3716                 (setq bface (get-text-property (point-at-bol) 'face)
3717                       eface (get-text-property (1- (point-at-eol)) 'face))
3718                 (message-remove-header "X-Gnus-PGP-Verify")
3719                 (if (re-search-forward "^X-PGP-Sig:" nil t)
3720                     (forward-line)
3721                   (goto-char (point-max)))
3722                 (narrow-to-region (point) (point))
3723                 (insert "X-Gnus-PGP-Verify: " info "\n")
3724                 (goto-char (point-min))
3725                 (forward-line)
3726                 (while (not (eobp))
3727                   (if (not (looking-at "^[ \t]"))
3728                       (insert " "))
3729                   (forward-line))
3730                 ;; Do highlighting.
3731                 (goto-char (point-min))
3732                 (when (looking-at "\\([^:]+\\): *")
3733                   (put-text-property (match-beginning 1) (1+ (match-end 1))
3734                                      'face bface)
3735                   (put-text-property (match-end 0) (point-max)
3736                                      'face eface)))))))))
3737
3738 (defun article-verify-cancel-lock ()
3739   "Verify Cancel-Lock header."
3740   (interactive)
3741   (if (gnus-buffer-live-p gnus-original-article-buffer)
3742       (canlock-verify gnus-original-article-buffer)))
3743
3744 (eval-and-compile
3745   (mapcar
3746    (lambda (func)
3747      (let (afunc gfunc)
3748        (if (consp func)
3749            (setq afunc (car func)
3750                  gfunc (cdr func))
3751          (setq afunc func
3752                gfunc (intern (format "gnus-%s" func))))
3753        (defalias gfunc
3754          (when (fboundp afunc)
3755            `(lambda (&optional interactive &rest args)
3756               ,(documentation afunc t)
3757               (interactive (list t))
3758               (save-excursion
3759                 (set-buffer gnus-article-buffer)
3760                 (if interactive
3761                     (call-interactively ',afunc)
3762                   (apply ',afunc args))))))))
3763    '(article-hide-headers
3764      article-verify-x-pgp-sig
3765      article-verify-cancel-lock
3766      article-hide-boring-headers
3767      article-treat-overstrike
3768      article-treat-ansi-sequences
3769      article-fill-long-lines
3770      article-capitalize-sentences
3771      article-remove-cr
3772      article-remove-leading-whitespace
3773      article-display-x-face
3774      article-display-face
3775      article-de-quoted-unreadable
3776      article-de-base64-unreadable
3777      article-decode-HZ
3778      article-wash-html
3779      article-unsplit-urls
3780      article-hide-list-identifiers
3781      article-strip-banner
3782      article-babel
3783      article-hide-pem
3784      article-hide-signature
3785      article-strip-headers-in-body
3786      article-remove-trailing-blank-lines
3787      article-strip-leading-blank-lines
3788      article-strip-multiple-blank-lines
3789      article-strip-leading-space
3790      article-strip-trailing-space
3791      article-strip-blank-lines
3792      article-strip-all-blank-lines
3793      article-date-local
3794      article-date-english
3795      article-date-iso8601
3796      article-date-original
3797      article-date-ut
3798      article-decode-mime-words
3799      article-decode-charset
3800      article-decode-encoded-words
3801      article-date-user
3802      article-date-lapsed
3803      article-emphasize
3804      article-treat-dumbquotes
3805      article-normalize-headers
3806 ;;     (article-show-all . gnus-article-show-all-headers)
3807      )))
3808 \f
3809 ;;;
3810 ;;; Gnus article mode
3811 ;;;
3812
3813 (put 'gnus-article-mode 'mode-class 'special)
3814
3815 (set-keymap-parent gnus-article-mode-map widget-keymap)
3816
3817 (gnus-define-keys gnus-article-mode-map
3818   " " gnus-article-goto-next-page
3819   "\177" gnus-article-goto-prev-page
3820   [delete] gnus-article-goto-prev-page
3821   [backspace] gnus-article-goto-prev-page
3822   "\C-c^" gnus-article-refer-article
3823   "h" gnus-article-show-summary
3824   "s" gnus-article-show-summary
3825   "\C-c\C-m" gnus-article-mail
3826   "?" gnus-article-describe-briefly
3827   "e" gnus-summary-edit-article
3828   "<" beginning-of-buffer
3829   ">" end-of-buffer
3830   "\C-c\C-i" gnus-info-find-node
3831   "\C-c\C-b" gnus-bug
3832   "R" gnus-article-reply-with-original
3833   "F" gnus-article-followup-with-original
3834   "\C-hk" gnus-article-describe-key
3835   "\C-hc" gnus-article-describe-key-briefly
3836
3837   "\C-d" gnus-article-read-summary-keys
3838   "\M-*" gnus-article-read-summary-keys
3839   "\M-#" gnus-article-read-summary-keys
3840   "\M-^" gnus-article-read-summary-keys
3841   "\M-g" gnus-article-read-summary-keys)
3842
3843 (substitute-key-definition
3844  'undefined 'gnus-article-read-summary-keys gnus-article-mode-map)
3845
3846 (defun gnus-article-make-menu-bar ()
3847   (unless (boundp 'gnus-article-commands-menu)
3848     (gnus-summary-make-menu-bar))
3849   (gnus-turn-off-edit-menu 'article)
3850   (unless (boundp 'gnus-article-article-menu)
3851     (easy-menu-define
3852      gnus-article-article-menu gnus-article-mode-map ""
3853      '("Article"
3854        ["Scroll forwards" gnus-article-goto-next-page t]
3855        ["Scroll backwards" gnus-article-goto-prev-page t]
3856        ["Show summary" gnus-article-show-summary t]
3857        ["Fetch Message-ID at point" gnus-article-refer-article t]
3858        ["Mail to address at point" gnus-article-mail t]
3859        ["Send a bug report" gnus-bug t]))
3860
3861     (easy-menu-define
3862      gnus-article-treatment-menu gnus-article-mode-map ""
3863      ;; Fixme: this should use :active (and maybe :visible).
3864      '("Treatment"
3865        ["Hide headers" gnus-article-hide-headers t]
3866        ["Hide signature" gnus-article-hide-signature t]
3867        ["Hide citation" gnus-article-hide-citation t]
3868        ["Treat overstrike" gnus-article-treat-overstrike t]
3869        ["Treat ANSI sequences" gnus-article-treat-ansi-sequences t]
3870        ["Remove carriage return" gnus-article-remove-cr t]
3871        ["Remove leading whitespace" gnus-article-remove-leading-whitespace t]
3872        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]
3873        ["Remove base64" gnus-article-de-base64-unreadable t]
3874        ["Treat html" gnus-article-wash-html t]
3875        ["Remove newlines from within URLs" gnus-article-unsplit-urls t]
3876        ["Decode HZ" gnus-article-decode-HZ t]))
3877
3878     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
3879
3880     ;; Note "Post" menu is defined in gnus-sum.el for consistency
3881
3882     (gnus-run-hooks 'gnus-article-menu-hook)))
3883
3884 (defun gnus-article-mode ()
3885   "Major mode for displaying an article.
3886
3887 All normal editing commands are switched off.
3888
3889 The following commands are available in addition to all summary mode
3890 commands:
3891 \\<gnus-article-mode-map>
3892 \\[gnus-article-next-page]\t Scroll the article one page forwards
3893 \\[gnus-article-prev-page]\t Scroll the article one page backwards
3894 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
3895 \\[gnus-article-show-summary]\t Display the summary buffer
3896 \\[gnus-article-mail]\t Send a reply to the address near point
3897 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
3898 \\[gnus-info-find-node]\t Go to the Gnus info node"
3899   (interactive)
3900   (kill-all-local-variables)
3901   (gnus-simplify-mode-line)
3902   (setq mode-name "Article")
3903   (setq major-mode 'gnus-article-mode)
3904   (make-local-variable 'minor-mode-alist)
3905   (use-local-map gnus-article-mode-map)
3906   (when (gnus-visual-p 'article-menu 'menu)
3907     (gnus-article-make-menu-bar)
3908     (when gnus-summary-tool-bar-map
3909       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
3910   (gnus-update-format-specifications nil 'article-mode)
3911   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
3912   (set (make-local-variable 'gnus-page-broken) nil)
3913   (make-local-variable 'gnus-button-marker-list)
3914   (make-local-variable 'gnus-article-current-summary)
3915   (make-local-variable 'gnus-article-mime-handles)
3916   (make-local-variable 'gnus-article-decoded-p)
3917   (make-local-variable 'gnus-article-mime-handle-alist)
3918   (make-local-variable 'gnus-article-wash-types)
3919   (make-local-variable 'gnus-article-image-alist)
3920   (make-local-variable 'gnus-article-charset)
3921   (make-local-variable 'gnus-article-ignored-charsets)
3922   ;; Prevent recent Emacsen from displaying non-break space as "\ ".
3923   (set (make-local-variable 'nobreak-char-display) nil)
3924   (gnus-set-default-directory)
3925   (buffer-disable-undo)
3926   (setq buffer-read-only t
3927         show-trailing-whitespace nil)
3928   (set-syntax-table gnus-article-mode-syntax-table)
3929   (mm-enable-multibyte)
3930   (gnus-run-mode-hooks 'gnus-article-mode-hook))
3931
3932 (defun gnus-article-setup-buffer ()
3933   "Initialize the article buffer."
3934   (let* ((name (if gnus-single-article-buffer "*Article*"
3935                  (concat "*Article " gnus-newsgroup-name "*")))
3936          (original
3937           (progn (string-match "\\*Article" name)
3938                  (concat " *Original Article"
3939                          (substring name (match-end 0))))))
3940     (setq gnus-article-buffer name)
3941     (setq gnus-original-article-buffer original)
3942     (setq gnus-article-mime-handle-alist nil)
3943     ;; This might be a variable local to the summary buffer.
3944     (unless gnus-single-article-buffer
3945       (save-excursion
3946         (set-buffer gnus-summary-buffer)
3947         (setq gnus-article-buffer name)
3948         (setq gnus-original-article-buffer original)
3949         (gnus-set-global-variables)))
3950     (gnus-article-setup-highlight-words)
3951     ;; Init original article buffer.
3952     (save-excursion
3953       (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
3954       (mm-enable-multibyte)
3955       (setq major-mode 'gnus-original-article-mode)
3956       (make-local-variable 'gnus-original-article))
3957     (if (and (get-buffer name)
3958              (with-current-buffer name
3959                (if gnus-article-edit-mode
3960                    (if (y-or-n-p "Article mode edit in progress; discard? ")
3961                        (progn
3962                          (set-buffer-modified-p nil)
3963                          (gnus-kill-buffer name)
3964                          (message "")
3965                          nil)
3966                      (error "Action aborted"))
3967                  t)))
3968         (save-excursion
3969           (set-buffer name)
3970           (set (make-local-variable 'gnus-article-edit-mode) nil)
3971           (when gnus-article-mime-handles
3972             (mm-destroy-parts gnus-article-mime-handles)
3973             (setq gnus-article-mime-handles nil))
3974           ;; Set it to nil in article-buffer!
3975           (setq gnus-article-mime-handle-alist nil)
3976           (buffer-disable-undo)
3977           (setq buffer-read-only t)
3978           ;; This list just keeps growing if we don't reset it.
3979           (setq gnus-button-marker-list nil)
3980           (unless (eq major-mode 'gnus-article-mode)
3981             (gnus-article-mode))
3982           (current-buffer))
3983       (save-excursion
3984         (set-buffer (gnus-get-buffer-create name))
3985         (gnus-article-mode)
3986         (make-local-variable 'gnus-summary-buffer)
3987         (gnus-summary-set-local-parameters gnus-newsgroup-name)
3988         (current-buffer)))))
3989
3990 ;; Set article window start at LINE, where LINE is the number of lines
3991 ;; from the head of the article.
3992 (defun gnus-article-set-window-start (&optional line)
3993   (set-window-start
3994    (gnus-get-buffer-window gnus-article-buffer t)
3995    (save-excursion
3996      (set-buffer gnus-article-buffer)
3997      (goto-char (point-min))
3998      (if (not line)
3999          (point-min)
4000        (gnus-message 6 "Moved to bookmark")
4001        (search-forward "\n\n" nil t)
4002        (forward-line line)
4003        (point)))))
4004
4005 (defun gnus-article-prepare (article &optional all-headers header)
4006   "Prepare ARTICLE in article mode buffer.
4007 ARTICLE should either be an article number or a Message-ID.
4008 If ARTICLE is an id, HEADER should be the article headers.
4009 If ALL-HEADERS is non-nil, no headers are hidden."
4010   (save-excursion
4011     ;; Make sure we start in a summary buffer.
4012     (unless (eq major-mode 'gnus-summary-mode)
4013       (set-buffer gnus-summary-buffer))
4014     (setq gnus-summary-buffer (current-buffer))
4015     (let* ((gnus-article (if header (mail-header-number header) article))
4016            (summary-buffer (current-buffer))
4017            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
4018            (group gnus-newsgroup-name)
4019            result)
4020       (save-excursion
4021         (gnus-article-setup-buffer)
4022         (set-buffer gnus-article-buffer)
4023         ;; Deactivate active regions.
4024         (when (and (boundp 'transient-mark-mode)
4025                    transient-mark-mode)
4026           (setq mark-active nil))
4027         (if (not (setq result (let ((inhibit-read-only t))
4028                                 (gnus-request-article-this-buffer
4029                                  article group))))
4030             ;; There is no such article.
4031             (save-excursion
4032               (when (and (numberp article)
4033                          (not (memq article gnus-newsgroup-sparse)))
4034                 (setq gnus-article-current
4035                       (cons gnus-newsgroup-name article))
4036                 (set-buffer gnus-summary-buffer)
4037                 (setq gnus-current-article article)
4038                 (if (and (memq article gnus-newsgroup-undownloaded)
4039                          (not (gnus-online (gnus-find-method-for-group
4040                                             gnus-newsgroup-name))))
4041                     (progn
4042                       (gnus-summary-set-agent-mark article)
4043                       (message "Message marked for downloading"))
4044                   (gnus-summary-mark-article article gnus-canceled-mark)
4045                   (unless (memq article gnus-newsgroup-sparse)
4046                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
4047           (if (or (eq result 'pseudo)
4048                   (eq result 'nneething))
4049               (progn
4050                 (save-excursion
4051                   (set-buffer summary-buffer)
4052                   (push article gnus-newsgroup-history)
4053                   (setq gnus-last-article gnus-current-article
4054                         gnus-current-article 0
4055                         gnus-current-headers nil
4056                         gnus-article-current nil)
4057                   (if (eq result 'nneething)
4058                       (gnus-configure-windows 'summary)
4059                     (gnus-configure-windows 'article))
4060                   (gnus-set-global-variables))
4061                 (let ((gnus-article-mime-handle-alist-1
4062                        gnus-article-mime-handle-alist))
4063                   (gnus-set-mode-line 'article)))
4064             ;; The result from the `request' was an actual article -
4065             ;; or at least some text that is now displayed in the
4066             ;; article buffer.
4067             (when (and (numberp article)
4068                        (not (eq article gnus-current-article)))
4069               ;; Seems like a new article has been selected.
4070               ;; `gnus-current-article' must be an article number.
4071               (save-excursion
4072                 (set-buffer summary-buffer)
4073                 (push article gnus-newsgroup-history)
4074                 (setq gnus-last-article gnus-current-article
4075                       gnus-current-article article
4076                       gnus-current-headers
4077                       (gnus-summary-article-header gnus-current-article)
4078                       gnus-article-current
4079                       (cons gnus-newsgroup-name gnus-current-article))
4080                 (unless (vectorp gnus-current-headers)
4081                   (setq gnus-current-headers nil))
4082                 (gnus-summary-goto-subject gnus-current-article)
4083                 (when (gnus-summary-show-thread)
4084                   ;; If the summary buffer really was folded, the
4085                   ;; previous goto may not actually have gone to
4086                   ;; the right article, but the thread root instead.
4087                   ;; So we go again.
4088                   (gnus-summary-goto-subject gnus-current-article))
4089                 (gnus-run-hooks 'gnus-mark-article-hook)
4090                 (gnus-set-mode-line 'summary)
4091                 (when (gnus-visual-p 'article-highlight 'highlight)
4092                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
4093                 ;; Set the global newsgroup variables here.
4094                 (gnus-set-global-variables)
4095                 (setq gnus-have-all-headers
4096                       (or all-headers gnus-show-all-headers))))
4097             (save-excursion
4098               (gnus-configure-windows 'article))
4099             (when (or (numberp article)
4100                       (stringp article))
4101               (gnus-article-prepare-display)
4102               ;; Do page break.
4103               (goto-char (point-min))
4104               (when gnus-break-pages
4105                 (gnus-narrow-to-page)))
4106             (let ((gnus-article-mime-handle-alist-1
4107                    gnus-article-mime-handle-alist))
4108               (gnus-set-mode-line 'article))
4109             (article-goto-body)
4110             (unless (bobp)
4111               (forward-line -1))
4112             (set-window-point (get-buffer-window (current-buffer)) (point))
4113             (gnus-configure-windows 'article)
4114             t))))))
4115
4116 ;;;###autoload
4117 (defun gnus-article-prepare-display ()
4118   "Make the current buffer look like a nice article."
4119   ;; Hooks for getting information from the article.
4120   ;; This hook must be called before being narrowed.
4121   (let ((gnus-article-buffer (current-buffer))
4122         buffer-read-only
4123         (inhibit-read-only t))
4124     (unless (eq major-mode 'gnus-article-mode)
4125       (gnus-article-mode))
4126     (setq buffer-read-only nil
4127           gnus-article-wash-types nil
4128           gnus-article-image-alist nil)
4129     (gnus-run-hooks 'gnus-tmp-internal-hook)
4130     (when gnus-display-mime-function
4131       (funcall gnus-display-mime-function))
4132     (gnus-run-hooks 'gnus-article-prepare-hook)))
4133
4134 ;;;
4135 ;;; Gnus MIME viewing functions
4136 ;;;
4137
4138 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
4139   "Format of the MIME buttons.
4140
4141 Valid specifiers include:
4142 %t  The MIME type
4143 %T  MIME type, along with additional info
4144 %n  The `name' parameter
4145 %d  The description, if any
4146 %l  The length of the encoded part
4147 %p  The part identifier number
4148 %e  Dots if the part isn't displayed
4149
4150 General format specifiers can also be used.  See Info node
4151 `(gnus)Formatting Variables'.")
4152
4153 (defvar gnus-mime-button-line-format-alist
4154   '((?t gnus-tmp-type ?s)
4155     (?T gnus-tmp-type-long ?s)
4156     (?n gnus-tmp-name ?s)
4157     (?d gnus-tmp-description ?s)
4158     (?p gnus-tmp-id ?s)
4159     (?l gnus-tmp-length ?d)
4160     (?e gnus-tmp-dots ?s)))
4161
4162 (defvar gnus-mime-button-commands
4163   '((gnus-article-press-button "\r" "Toggle Display")
4164     (gnus-mime-view-part "v" "View Interactively...")
4165     (gnus-mime-view-part-as-type "t" "View As Type...")
4166     (gnus-mime-view-part-as-charset "C" "View As charset...")
4167     (gnus-mime-save-part "o" "Save...")
4168     (gnus-mime-save-part-and-strip "\C-o" "Save and Strip")
4169     (gnus-mime-replace-part "r" "Replace part")
4170     (gnus-mime-delete-part "d" "Delete part")
4171     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
4172     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
4173     (gnus-mime-view-part-internally "E" "View Internally") ;; Why `E'?
4174     (gnus-mime-view-part-externally "e" "View Externally")
4175     (gnus-mime-print-part "p" "Print")
4176     (gnus-mime-pipe-part "|" "Pipe To Command...")
4177     (gnus-mime-action-on-part "." "Take action on the part...")))
4178
4179 (defun gnus-article-mime-part-status ()
4180   (if gnus-article-mime-handle-alist-1
4181       (if (eq 1 (length gnus-article-mime-handle-alist-1))
4182           " (1 part)"
4183         (format " (%d parts)" (length gnus-article-mime-handle-alist-1)))
4184     ""))
4185
4186 (defvar gnus-mime-button-map
4187   (let ((map (make-sparse-keymap)))
4188     (define-key map gnus-mouse-2 'gnus-article-push-button)
4189     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
4190     (dolist (c gnus-mime-button-commands)
4191       (define-key map (cadr c) (car c)))
4192     map))
4193
4194 (easy-menu-define
4195   gnus-mime-button-menu gnus-mime-button-map "MIME button menu."
4196   `("MIME Part"
4197     ,@(mapcar (lambda (c)
4198                 (vector (caddr c) (car c) :enable t))
4199               gnus-mime-button-commands)))
4200
4201 (defun gnus-mime-button-menu (event prefix)
4202  "Construct a context-sensitive menu of MIME commands."
4203  (interactive "e\nP")
4204  (save-window-excursion
4205    (let ((pos (event-start event)))
4206      (select-window (posn-window pos))
4207      (goto-char (posn-point pos))
4208      (gnus-article-check-buffer)
4209      (popup-menu gnus-mime-button-menu nil prefix))))
4210
4211 (defun gnus-mime-view-all-parts (&optional handles)
4212   "View all the MIME parts."
4213   (interactive)
4214   (with-current-buffer gnus-article-buffer
4215     (let ((handles (or handles gnus-article-mime-handles))
4216           (mail-parse-charset gnus-newsgroup-charset)
4217           (mail-parse-ignored-charsets
4218            (with-current-buffer gnus-summary-buffer
4219              gnus-newsgroup-ignored-charsets)))
4220       (when handles
4221         (mm-remove-parts handles)
4222         (goto-char (point-min))
4223         (or (search-forward "\n\n") (goto-char (point-max)))
4224         (let ((inhibit-read-only t))
4225           (delete-region (point) (point-max))
4226           (mm-display-parts handles))))))
4227
4228 (defun gnus-article-jump-to-part (n)
4229   "Jump to MIME part N."
4230   (interactive "P")
4231   (pop-to-buffer gnus-article-buffer)
4232   ;; FIXME: why is it necessary?
4233   (sit-for 0)
4234   (let ((parts (length gnus-article-mime-handle-alist)))
4235     (or n (setq n
4236                 (string-to-number
4237                  (read-string ;; Emacs 21 doesn't have `read-number'.
4238                   (format "Jump to part (2..%s): " parts)))))
4239     (unless (and (integerp n) (<= n parts) (>= n 1))
4240       (setq n
4241             (progn
4242               (gnus-message 7 "Invalid part `%s', using %s instead."
4243                             n parts)
4244               parts)))
4245     (gnus-message 9 "Jumping to part %s." n)
4246     (cond ((>= gnus-auto-select-part 1)
4247            (while (and (<= n parts)
4248                        (not (gnus-article-goto-part n)))
4249              (setq n (1+ n))))
4250           ((< gnus-auto-select-part 0)
4251            (while (and (>= n 1)
4252                        (not (gnus-article-goto-part n)))
4253              (setq n (1- n))))
4254           (t
4255            (gnus-article-goto-part n)))))
4256
4257 (eval-when-compile
4258   (defsubst gnus-article-edit-part (handles &optional current-id)
4259     "Edit an article in order to delete a mime part.
4260 This function is exclusively used by `gnus-mime-save-part-and-strip'
4261 and `gnus-mime-delete-part', and not provided at run-time normally."
4262     (gnus-article-edit-article
4263      `(lambda ()
4264         (buffer-disable-undo)
4265         (erase-buffer)
4266         (let ((mail-parse-charset (or gnus-article-charset
4267                                       ',gnus-newsgroup-charset))
4268               (mail-parse-ignored-charsets
4269                (or gnus-article-ignored-charsets
4270                    ',gnus-newsgroup-ignored-charsets))
4271               (mbl mml-buffer-list))
4272           (setq mml-buffer-list nil)
4273           (insert-buffer-substring gnus-original-article-buffer)
4274           (mime-to-mml ',handles)
4275           (setq gnus-article-mime-handles nil)
4276           (let ((mbl1 mml-buffer-list))
4277             (setq mml-buffer-list mbl)
4278             (set (make-local-variable 'mml-buffer-list) mbl1))
4279           (gnus-make-local-hook 'kill-buffer-hook)
4280           (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
4281      `(lambda (no-highlight)
4282         (let ((mail-parse-charset (or gnus-article-charset
4283                                       ',gnus-newsgroup-charset))
4284               (message-options message-options)
4285               (message-options-set-recipient)
4286               (mail-parse-ignored-charsets
4287                (or gnus-article-ignored-charsets
4288                    ',gnus-newsgroup-ignored-charsets)))
4289           (mml-to-mime)
4290           (mml-destroy-buffers)
4291           (remove-hook 'kill-buffer-hook
4292                        'mml-destroy-buffers t)
4293           (kill-local-variable 'mml-buffer-list))
4294         (gnus-summary-edit-article-done
4295          ,(or (mail-header-references gnus-current-headers) "")
4296          ,(gnus-group-read-only-p)
4297          ,gnus-summary-buffer no-highlight))
4298      t)
4299     (gnus-article-edit-done)
4300     (gnus-summary-expand-window)
4301     (gnus-summary-show-article)
4302     (when (and current-id (integerp gnus-auto-select-part))
4303       (gnus-article-jump-to-part
4304        (+ current-id gnus-auto-select-part)))))
4305
4306 (defun gnus-mime-replace-part (file)
4307   "Replace MIME part under point with an external body."
4308   ;; Useful if file has already been saved to disk
4309   (interactive
4310    (list
4311     (mm-with-multibyte
4312       (read-file-name "Replace MIME part with file: "
4313                       (or mm-default-directory default-directory)
4314                       nil nil))))
4315   (gnus-mime-save-part-and-strip file))
4316
4317 (defun gnus-mime-save-part-and-strip (&optional file)
4318   "Save the MIME part under point then replace it with an external body.
4319 If FILE is given, use it for the external part."
4320   (interactive)
4321   (gnus-article-check-buffer)
4322   (when (gnus-group-read-only-p)
4323     (error "The current group does not support deleting of parts"))
4324   (when (mm-complicated-handles gnus-article-mime-handles)
4325     (error "\
4326 The current article has a complicated MIME structure, giving up..."))
4327   (let* ((data (get-text-property (point) 'gnus-data))
4328          (id (get-text-property (point) 'gnus-part))
4329          param
4330          (handles gnus-article-mime-handles))
4331     (unless file
4332       (setq file
4333             (and data (mm-save-part data "Delete MIME part and save to: "))))
4334     (when file
4335       (with-current-buffer (mm-handle-buffer data)
4336         (erase-buffer)
4337         (insert "Content-Type: " (mm-handle-media-type data))
4338         (mml-insert-parameter-string (cdr (mm-handle-type data))
4339                                      '(charset))
4340         (insert "\n")
4341         (insert "Content-ID: " (message-make-message-id) "\n")
4342         (insert "Content-Transfer-Encoding: binary\n")
4343         (insert "\n"))
4344       (setcdr data
4345               (cdr (mm-make-handle nil
4346                                    `("message/external-body"
4347                                      (access-type . "LOCAL-FILE")
4348                                      (name . ,file)))))
4349       ;; (set-buffer gnus-summary-buffer)
4350       (gnus-article-edit-part handles id))))
4351
4352 ;; A function like `gnus-summary-save-parts' (`X m', `<MIME> <Extract all
4353 ;; parts...>') but with stripping would be nice.
4354
4355 (defun gnus-mime-delete-part ()
4356   "Delete the MIME part under point.
4357 Replace it with some information about the removed part."
4358   (interactive)
4359   (gnus-article-check-buffer)
4360   (when (gnus-group-read-only-p)
4361     (error "The current group does not support deleting of parts"))
4362   (when (mm-complicated-handles gnus-article-mime-handles)
4363     (error "\
4364 The current article has a complicated MIME structure, giving up..."))
4365   (when (or gnus-expert-user
4366             (gnus-yes-or-no-p "\
4367 Deleting parts may malfunction or destroy the article; continue? "))
4368     (let* ((data (get-text-property (point) 'gnus-data))
4369            (id (get-text-property (point) 'gnus-part))
4370            (handles gnus-article-mime-handles)
4371            (none "(none)")
4372            (description
4373             (or
4374              (mail-decode-encoded-word-string (or (mm-handle-description data)
4375                                                   none))))
4376            (filename
4377             (or (mail-content-type-get (mm-handle-disposition data) 'filename)
4378                 none))
4379            (type (mm-handle-media-type data)))
4380       (unless data
4381         (error "No MIME part under point"))
4382       (with-current-buffer (mm-handle-buffer data)
4383         (let ((bsize (format "%s" (buffer-size))))
4384           (erase-buffer)
4385           (insert
4386            (concat
4387             ",----\n"
4388             "| The following attachment has been deleted:\n"
4389             "|\n"
4390             "| Type:           " type "\n"
4391             "| Filename:       " filename "\n"
4392             "| Size (encoded): " bsize " Byte\n"
4393             "| Description:    " description "\n"
4394             "`----\n"))
4395           (setcdr data
4396                   (cdr (mm-make-handle
4397                         nil `("text/plain") nil nil
4398                         (list "attachment")
4399                         (format "Deleted attachment (%s bytes)" bsize))))))
4400       ;; (set-buffer gnus-summary-buffer)
4401       (gnus-article-edit-part handles id))))
4402
4403 (defun gnus-mime-save-part ()
4404   "Save the MIME part under point."
4405   (interactive)
4406   (gnus-article-check-buffer)
4407   (let ((data (get-text-property (point) 'gnus-data)))
4408     (when data
4409       (mm-save-part data))))
4410
4411 (defun gnus-mime-pipe-part ()
4412   "Pipe the MIME part under point to a process."
4413   (interactive)
4414   (gnus-article-check-buffer)
4415   (let ((data (get-text-property (point) 'gnus-data)))
4416     (when data
4417       (mm-pipe-part data))))
4418
4419 (defun gnus-mime-view-part ()
4420   "Interactively choose a viewing method for the MIME part under point."
4421   (interactive)
4422   (gnus-article-check-buffer)
4423   (let ((data (get-text-property (point) 'gnus-data)))
4424     (when data
4425       (setq gnus-article-mime-handles
4426             (mm-merge-handles
4427              gnus-article-mime-handles (setq data (copy-sequence data))))
4428       (mm-interactively-view-part data))))
4429
4430 (defun gnus-mime-view-part-as-type-internal ()
4431   (gnus-article-check-buffer)
4432   (let* ((handle (get-text-property (point) 'gnus-data))
4433          (name (or
4434                 ;; Content-Type: foo/bar; name=...
4435                 (mail-content-type-get (mm-handle-type handle) 'name)
4436                 ;; Content-Disposition: attachment; filename=...
4437                 (cdr (assq 'filename (cdr (mm-handle-disposition handle))))))
4438          (def-type (and name (mm-default-file-encoding name))))
4439     (and def-type (cons def-type 0))))
4440
4441 (defun gnus-mime-view-part-as-type (&optional mime-type)
4442   "Choose a MIME media type, and view the part as such."
4443   (interactive)
4444   (unless mime-type
4445     (setq mime-type
4446           (let ((default (gnus-mime-view-part-as-type-internal)))
4447             (completing-read
4448              (format "View as MIME type (default %s): "
4449                      (car default))
4450              (mapcar #'list (mailcap-mime-types))
4451              nil nil nil nil
4452              (car default)))))
4453   (gnus-article-check-buffer)
4454   (let ((handle (get-text-property (point) 'gnus-data)))
4455     (when handle
4456       (setq handle
4457             (mm-make-handle (mm-handle-buffer handle)
4458                             (cons mime-type (cdr (mm-handle-type handle)))
4459                             (mm-handle-encoding handle)
4460                             (mm-handle-undisplayer handle)
4461                             (mm-handle-disposition handle)
4462                             (mm-handle-description handle)
4463                             nil
4464                             (mm-handle-id handle)))
4465       (setq gnus-article-mime-handles
4466             (mm-merge-handles gnus-article-mime-handles handle))
4467       (gnus-mm-display-part handle))))
4468
4469 (defun gnus-mime-copy-part (&optional handle arg)
4470   "Put the MIME part under point into a new buffer.
4471 If `auto-compression-mode' is enabled, compressed files like .gz and .bz2
4472 are decompressed."
4473   (interactive (list nil current-prefix-arg))
4474   (gnus-article-check-buffer)
4475   (unless handle
4476     (setq handle (get-text-property (point) 'gnus-data)))
4477   (when handle
4478     (let ((filename (or (mail-content-type-get (mm-handle-disposition handle)
4479                                                'name)
4480                         (mail-content-type-get (mm-handle-disposition handle)
4481                                                'filename)))
4482           contents dont-decode charset coding-system)
4483       (mm-with-unibyte-buffer
4484         (mm-insert-part handle)
4485         (setq contents (or (condition-case nil
4486                                (mm-decompress-buffer filename nil 'sig)
4487                              (error
4488                               (setq dont-decode t)
4489                               nil))
4490                            (buffer-string))))
4491       (setq filename (cond (filename (file-name-nondirectory filename))
4492                            (dont-decode "*raw data*")
4493                            (t "*decoded*")))
4494       (cond
4495        (dont-decode)
4496        ((not arg)
4497         (unless (setq charset (mail-content-type-get
4498                                (mm-handle-type handle) 'charset))
4499           (unless (setq coding-system (mm-with-unibyte-buffer
4500                                         (insert contents)
4501                                         (mm-find-buffer-file-coding-system)))
4502             (setq charset gnus-newsgroup-charset))))
4503        ((numberp arg)
4504         (setq charset (or (cdr (assq arg
4505                                      gnus-summary-show-article-charset-alist))
4506                           (mm-read-coding-system "Charset: ")))))
4507       (switch-to-buffer (generate-new-buffer filename))
4508       (if (or coding-system
4509               (and charset
4510                    (setq coding-system (mm-charset-to-coding-system charset))
4511                    (not (eq charset 'ascii))))
4512           (progn
4513             (mm-enable-multibyte)
4514             (insert (mm-decode-coding-string contents coding-system))
4515             (setq buffer-file-coding-system
4516                   (if (boundp 'last-coding-system-used)
4517                       (symbol-value 'last-coding-system-used)
4518                     coding-system)))
4519         (mm-disable-multibyte)
4520         (insert contents)
4521         (setq buffer-file-coding-system mm-binary-coding-system))
4522       ;; We do it this way to make `normal-mode' set the appropriate mode.
4523       (unwind-protect
4524           (progn
4525             (setq buffer-file-name (expand-file-name filename))
4526             (normal-mode))
4527         (setq buffer-file-name nil))
4528       (goto-char (point-min)))))
4529
4530 (defun gnus-mime-print-part (&optional handle filename)
4531   "Print the MIME part under point."
4532   (interactive (list nil (ps-print-preprint current-prefix-arg)))
4533   (gnus-article-check-buffer)
4534   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4535          (contents (and handle (mm-get-part handle)))
4536          (file (mm-make-temp-file (expand-file-name "mm." mm-tmp-directory)))
4537          (printer (mailcap-mime-info (mm-handle-media-type handle) "print")))
4538     (when contents
4539         (if printer
4540             (unwind-protect
4541                 (progn
4542                   (mm-save-part-to-file handle file)
4543                   (call-process shell-file-name nil
4544                                 (generate-new-buffer " *mm*")
4545                                 nil
4546                                 shell-command-switch
4547                                 (mm-mailcap-command
4548                                  printer file (mm-handle-type handle))))
4549               (delete-file file))
4550           (with-temp-buffer
4551             (insert contents)
4552             (gnus-print-buffer))
4553           (ps-despool filename)))))
4554
4555 (defun gnus-mime-inline-part (&optional handle arg)
4556   "Insert the MIME part under point into the current buffer.
4557 Compressed files like .gz and .bz2 are decompressed."
4558   (interactive (list nil current-prefix-arg))
4559   (gnus-article-check-buffer)
4560   (unless handle
4561     (setq handle (get-text-property (point) 'gnus-data)))
4562   (when handle
4563     (let ((b (point))
4564           (inhibit-read-only t)
4565           contents charset coding-system)
4566       (if (and (not arg) (mm-handle-undisplayer handle))
4567           (mm-remove-part handle)
4568         (mm-with-unibyte-buffer
4569           (mm-insert-part handle)
4570           (setq contents
4571                 (or (mm-decompress-buffer
4572                      (or (mail-content-type-get (mm-handle-disposition handle)
4573                                                 'name)
4574                          (mail-content-type-get (mm-handle-disposition handle)
4575                                                 'filename))
4576                      nil t)
4577                     (buffer-string))))
4578         (cond
4579          ((not arg)
4580           (unless (setq charset (mail-content-type-get
4581                                  (mm-handle-type handle) 'charset))
4582             (unless (setq coding-system
4583                           (mm-with-unibyte-buffer
4584                             (insert contents)
4585                             (mm-find-buffer-file-coding-system)))
4586               (setq charset gnus-newsgroup-charset))))
4587          ((numberp arg)
4588           (if (mm-handle-undisplayer handle)
4589               (mm-remove-part handle))
4590           (setq charset
4591                 (or (cdr (assq arg
4592                                gnus-summary-show-article-charset-alist))
4593                     (mm-read-coding-system "Charset: "))))
4594          (t
4595           (if (mm-handle-undisplayer handle)
4596               (mm-remove-part handle))))
4597         (forward-line 2)
4598         (mm-insert-inline
4599          handle
4600          (if (or coding-system
4601                  (and charset
4602                       (setq coding-system
4603                             (mm-charset-to-coding-system charset))
4604                       (not (eq coding-system 'ascii))))
4605              (mm-decode-coding-string contents coding-system)
4606            (mm-string-to-multibyte contents)))
4607         (goto-char b)))))
4608
4609 (defun gnus-mime-view-part-as-charset (&optional handle arg)
4610   "Insert the MIME part under point into the current buffer using the
4611 specified charset."
4612   (interactive (list nil current-prefix-arg))
4613   (gnus-article-check-buffer)
4614   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4615          contents charset
4616          (b (point))
4617          (inhibit-read-only t))
4618     (when handle
4619       (if (mm-handle-undisplayer handle)
4620           (mm-remove-part handle))
4621       (let ((gnus-newsgroup-charset
4622              (or (cdr (assq arg
4623                             gnus-summary-show-article-charset-alist))
4624                  (mm-read-coding-system "Charset: ")))
4625           (gnus-newsgroup-ignored-charsets 'gnus-all))
4626         (gnus-article-press-button)))))
4627
4628 (defun gnus-mime-view-part-externally (&optional handle)
4629   "View the MIME part under point with an external viewer."
4630   (interactive)
4631   (gnus-article-check-buffer)
4632   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4633          (mm-user-display-methods nil)
4634          (mm-inlined-types nil)
4635          (mail-parse-charset gnus-newsgroup-charset)
4636          (mail-parse-ignored-charsets
4637           (with-current-buffer gnus-summary-buffer
4638             gnus-newsgroup-ignored-charsets)))
4639     (when handle
4640       (if (mm-handle-undisplayer handle)
4641           (mm-remove-part handle)
4642         (mm-display-part handle)))))
4643
4644 (defun gnus-mime-view-part-internally (&optional handle)
4645   "View the MIME part under point with an internal viewer.
4646 If no internal viewer is available, use an external viewer."
4647   (interactive)
4648   (gnus-article-check-buffer)
4649   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4650          (mm-inlined-types '(".*"))
4651          (mm-inline-large-images t)
4652          (mail-parse-charset gnus-newsgroup-charset)
4653          (mail-parse-ignored-charsets
4654           (with-current-buffer gnus-summary-buffer
4655             gnus-newsgroup-ignored-charsets))
4656          (inhibit-read-only t))
4657     (when handle
4658       (if (mm-handle-undisplayer handle)
4659           (mm-remove-part handle)
4660         (mm-display-part handle)))))
4661
4662 (defun gnus-mime-action-on-part (&optional action)
4663   "Do something with the MIME attachment at \(point\)."
4664   (interactive
4665    (list (completing-read "Action: " gnus-mime-action-alist nil t)))
4666   (gnus-article-check-buffer)
4667   (let ((action-pair (assoc action gnus-mime-action-alist)))
4668     (if action-pair
4669         (funcall (cdr action-pair)))))
4670
4671 (defun gnus-article-part-wrapper (n function &optional no-handle interactive)
4672   "Call FUNCTION on MIME part N.
4673 Unless NO-HANDLE, call FUNCTION with N-th MIME handle as it's only argument.
4674 If INTERACTIVE, call FUNCTION interactivly."
4675   (let (window frame)
4676     ;; Check whether the article is displayed.
4677     (unless (and (gnus-buffer-live-p gnus-article-buffer)
4678                  (setq window (get-buffer-window gnus-article-buffer t))
4679                  (frame-visible-p (setq frame (window-frame window))))
4680       (error "No article is displayed"))
4681     (with-current-buffer gnus-article-buffer
4682       ;; Check whether the article displays the right contents.
4683       (unless (with-current-buffer gnus-summary-buffer
4684                 (eq gnus-current-article (gnus-summary-article-number)))
4685         (error "You should select the right article first"))
4686       ;; Check whether the specified part exists.
4687       (when (> n (length gnus-article-mime-handle-alist))
4688         (error "No such part")))
4689     (unless
4690         (progn
4691           ;; To select the window is needed so that the cursor
4692           ;; might be visible on the MIME button.
4693           (select-window (prog1
4694                              window
4695                            (setq window (selected-window))
4696                            ;; Article may be displayed in the other frame.
4697                            (gnus-select-frame-set-input-focus
4698                             (prog1
4699                                 frame
4700                               (setq frame (selected-frame))))))
4701           (when (gnus-article-goto-part n)
4702             ;; We point the cursor and the arrow at the MIME button
4703             ;; when the `function' prompt the user for something.
4704             (let ((cursor-in-non-selected-windows t)
4705                   (overlay-arrow-string "=>")
4706                   (overlay-arrow-position (point-marker)))
4707               (unwind-protect
4708                   (cond
4709                    ((and no-handle interactive)
4710                     (call-interactively function))
4711                    (no-handle
4712                     (funcall function))
4713                    (interactive
4714                     (call-interactively
4715                      function
4716                      (cdr (assq n gnus-article-mime-handle-alist))))
4717                    (t
4718                     (funcall function
4719                              (cdr (assq n gnus-article-mime-handle-alist)))))
4720                 (set-marker overlay-arrow-position nil)
4721                 (unless gnus-auto-select-part
4722                   (gnus-select-frame-set-input-focus frame)
4723                   (select-window window))))
4724             t))
4725       (if gnus-inhibit-mime-unbuttonizing
4726           ;; This is the default though the program shouldn't reach here.
4727           (error "No such part")
4728         ;; The part which doesn't have the MIME button is selected.
4729         ;; So, we display all the buttons and redo it.
4730         (let ((gnus-inhibit-mime-unbuttonizing t))
4731           (gnus-summary-show-article)
4732           (gnus-article-part-wrapper n function no-handle))))))
4733
4734 (defun gnus-article-pipe-part (n)
4735   "Pipe MIME part N, which is the numerical prefix."
4736   (interactive "p")
4737   (gnus-article-part-wrapper n 'mm-pipe-part))
4738
4739 (defun gnus-article-save-part (n)
4740   "Save MIME part N, which is the numerical prefix."
4741   (interactive "p")
4742   (gnus-article-part-wrapper n 'mm-save-part))
4743
4744 (defun gnus-article-interactively-view-part (n)
4745   "View MIME part N interactively, which is the numerical prefix."
4746   (interactive "p")
4747   (gnus-article-part-wrapper n 'mm-interactively-view-part))
4748
4749 (defun gnus-article-copy-part (n)
4750   "Copy MIME part N, which is the numerical prefix."
4751   (interactive "p")
4752   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
4753
4754 (defun gnus-article-view-part-as-charset (n)
4755   "View MIME part N using a specified charset.
4756 N is the numerical prefix."
4757   (interactive "p")
4758   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
4759
4760 (defun gnus-article-view-part-externally (n)
4761   "View MIME part N externally, which is the numerical prefix."
4762   (interactive "p")
4763   (gnus-article-part-wrapper n 'gnus-mime-view-part-externally))
4764
4765 (defun gnus-article-inline-part (n)
4766   "Inline MIME part N, which is the numerical prefix."
4767   (interactive "p")
4768   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
4769
4770 (defun gnus-article-save-part-and-strip (n)
4771   "Save MIME part N and replace it with an external body.
4772 N is the numerical prefix."
4773   (interactive "p")
4774   (gnus-article-part-wrapper n 'gnus-mime-save-part-and-strip t))
4775
4776 (defun gnus-article-replace-part (n)
4777   "Replace MIME part N with an external body.
4778 N is the numerical prefix."
4779   (interactive "p")
4780   (gnus-article-part-wrapper n 'gnus-mime-replace-part t t))
4781
4782 (defun gnus-article-delete-part (n)
4783   "Delete MIME part N and add some information about the removed part.
4784 N is the numerical prefix."
4785   (interactive "p")
4786   (gnus-article-part-wrapper n 'gnus-mime-delete-part t))
4787
4788 (defun gnus-article-mime-match-handle-first (condition)
4789   (if condition
4790       (let (n)
4791         (dolist (ihandle gnus-article-mime-handle-alist)
4792           (if (and (cond
4793                     ((functionp condition)
4794                      (funcall condition (cdr ihandle)))
4795                     ((eq condition 'undisplayed)
4796                      (not (or (mm-handle-undisplayer (cdr ihandle))
4797                               (equal (mm-handle-media-type (cdr ihandle))
4798                                      "multipart/alternative"))))
4799                     ((eq condition 'undisplayed-alternative)
4800                      (not (mm-handle-undisplayer (cdr ihandle))))
4801                     (t t))
4802                    (gnus-article-goto-part (car ihandle))
4803                    (or (not n) (< (car ihandle) n)))
4804               (setq n (car ihandle))))
4805         (or n 1))
4806     1))
4807
4808 (defun gnus-article-view-part (&optional n)
4809   "View MIME part N, which is the numerical prefix."
4810   (interactive "P")
4811   (with-current-buffer gnus-article-buffer
4812     (or (numberp n) (setq n (gnus-article-mime-match-handle-first
4813                              gnus-article-mime-match-handle-function)))
4814     (when (> n (length gnus-article-mime-handle-alist))
4815       (error "No such part"))
4816     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
4817       (when (gnus-article-goto-part n)
4818         (if (equal (car handle) "multipart/alternative")
4819             (gnus-article-press-button)
4820           (when (eq (gnus-mm-display-part handle) 'internal)
4821             (gnus-set-window-start)))))))
4822
4823 (defsubst gnus-article-mime-total-parts ()
4824   (if (bufferp (car gnus-article-mime-handles))
4825       1 ;; single part
4826     (1- (length gnus-article-mime-handles))))
4827
4828 (defun gnus-mm-display-part (handle)
4829   "Display HANDLE and fix MIME button."
4830   (let ((id (get-text-property (point) 'gnus-part))
4831         (point (point))
4832         (inhibit-read-only t))
4833     (forward-line 1)
4834     (prog1
4835         (let ((window (selected-window))
4836               (mail-parse-charset gnus-newsgroup-charset)
4837               (mail-parse-ignored-charsets
4838                (if (gnus-buffer-live-p gnus-summary-buffer)
4839                    (with-current-buffer gnus-summary-buffer
4840                      gnus-newsgroup-ignored-charsets)
4841                  nil)))
4842           (save-excursion
4843             (unwind-protect
4844                 (let ((win (gnus-get-buffer-window (current-buffer) t))
4845                       (beg (point)))
4846                   (when win
4847                     (select-window win))
4848                   (goto-char point)
4849                   (forward-line)
4850                   (if (mm-handle-displayed-p handle)
4851                       ;; This will remove the part.
4852                       (mm-display-part handle)
4853                     (save-restriction
4854                       (narrow-to-region (point)
4855                                         (if (eobp) (point) (1+ (point))))
4856                       (mm-display-part handle)
4857                       ;; We narrow to the part itself and
4858                       ;; then call the treatment functions.
4859                       (goto-char (point-min))
4860                       (forward-line 1)
4861                       (narrow-to-region (point) (point-max))
4862                       (gnus-treat-article
4863                        nil id
4864                        (gnus-article-mime-total-parts)
4865                        (mm-handle-media-type handle)))))
4866               (if (window-live-p window)
4867                   (select-window window)))))
4868       (goto-char point)
4869       (gnus-delete-line)
4870       (gnus-insert-mime-button
4871        handle id (list (mm-handle-displayed-p handle)))
4872       (goto-char point))))
4873
4874 (defun gnus-article-goto-part (n)
4875   "Go to MIME part N."
4876   (gnus-goto-char (text-property-any (point-min) (point-max) 'gnus-part n)))
4877
4878 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
4879   (let ((gnus-tmp-name
4880          (or (mail-content-type-get (mm-handle-type handle) 'name)
4881              (mail-content-type-get (mm-handle-disposition handle) 'filename)
4882              (mail-content-type-get (mm-handle-type handle) 'url)
4883              ""))
4884         (gnus-tmp-type (mm-handle-media-type handle))
4885         (gnus-tmp-description
4886          (mail-decode-encoded-word-string (or (mm-handle-description handle)
4887                                               "")))
4888         (gnus-tmp-dots
4889          (if (if displayed (car displayed)
4890                (mm-handle-displayed-p handle))
4891              "" "..."))
4892         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
4893                            (buffer-size)))
4894         gnus-tmp-type-long b e)
4895     (when (string-match ".*/" gnus-tmp-name)
4896       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
4897     (setq gnus-tmp-type-long (concat gnus-tmp-type
4898                                      (and (not (equal gnus-tmp-name ""))
4899                                           (concat "; " gnus-tmp-name))))
4900     (unless (equal gnus-tmp-description "")
4901       (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
4902     (unless (bolp)
4903       (insert "\n"))
4904     (setq b (point))
4905     (gnus-eval-format
4906      gnus-mime-button-line-format gnus-mime-button-line-format-alist
4907      `(keymap ,gnus-mime-button-map
4908               gnus-callback gnus-mm-display-part
4909               gnus-part ,gnus-tmp-id
4910               article-type annotation
4911               gnus-data ,handle))
4912     (setq e (if (bolp)
4913                 ;; Exclude a newline.
4914                 (1- (point))
4915               (point)))
4916     (widget-convert-button
4917      'link b e
4918      :mime-handle handle
4919      :action 'gnus-widget-press-button
4920      :button-keymap gnus-mime-button-map
4921      :help-echo
4922      (lambda (widget/window &optional overlay pos)
4923        ;; Needed to properly clear the message due to a bug in
4924        ;; wid-edit (XEmacs only).
4925        (if (boundp 'help-echo-owns-message)
4926            (setq help-echo-owns-message t))
4927        (format
4928         "%S: %s the MIME part; %S: more options"
4929         (aref gnus-mouse-2 0)
4930         ;; XEmacs will get a single widget arg; Emacs 21 will get
4931         ;; window, overlay, position.
4932         (if (mm-handle-displayed-p
4933              (if overlay
4934                  (with-current-buffer (gnus-overlay-buffer overlay)
4935                    (widget-get (widget-at (gnus-overlay-start overlay))
4936                                :mime-handle))
4937                (widget-get widget/window :mime-handle)))
4938             "hide" "show")
4939         (aref gnus-down-mouse-3 0))))))
4940
4941 (defun gnus-widget-press-button (elems el)
4942   (goto-char (widget-get elems :from))
4943   (gnus-article-press-button))
4944
4945 (defvar gnus-displaying-mime nil)
4946
4947 (defun gnus-display-mime (&optional ihandles)
4948   "Display the MIME parts."
4949   (save-excursion
4950     (save-selected-window
4951       (let ((window (get-buffer-window gnus-article-buffer))
4952             (point (point)))
4953         (when window
4954           (select-window window)
4955           ;; We have to do this since selecting the window
4956           ;; may change the point.  So we set the window point.
4957           (set-window-point window point)))
4958       (let ((handles ihandles)
4959             (inhibit-read-only t)
4960             handle)
4961         (cond (handles)
4962               ((setq handles (mm-dissect-buffer nil gnus-article-loose-mime))
4963                (when gnus-article-emulate-mime
4964                  (mm-uu-dissect-text-parts handles)))
4965               (gnus-article-emulate-mime
4966                (setq handles (mm-uu-dissect))))
4967         (when (and (not ihandles)
4968                    (not gnus-displaying-mime))
4969           ;; Top-level call; we clean up.
4970           (when gnus-article-mime-handles
4971             (mm-destroy-parts gnus-article-mime-handles)
4972             (setq gnus-article-mime-handle-alist nil));; A trick.
4973           (setq gnus-article-mime-handles handles)
4974           ;; We allow users to glean info from the handles.
4975           (when gnus-article-mime-part-function
4976             (gnus-mime-part-function handles)))
4977         (if (and handles
4978                  (or (not (stringp (car handles)))
4979                      (cdr handles)))
4980             (progn
4981               (when (and (not ihandles)
4982                          (not gnus-displaying-mime))
4983                 ;; Clean up for mime parts.
4984                 (article-goto-body)
4985                 (delete-region (point) (point-max)))
4986               (let ((gnus-displaying-mime t))
4987                 (gnus-mime-display-part handles)))
4988           (save-restriction
4989             (article-goto-body)
4990             (narrow-to-region (point) (point-max))
4991             (gnus-treat-article nil 1 1)
4992             (widen)))
4993         (unless ihandles
4994           ;; Highlight the headers.
4995           (save-excursion
4996             (save-restriction
4997               (article-goto-body)
4998               (narrow-to-region (point-min) (point))
4999               (gnus-article-save-original-date
5000                (gnus-treat-article 'head)))))))))
5001
5002 (defcustom gnus-mime-display-multipart-as-mixed nil
5003   "Display \"multipart\" parts as  \"multipart/mixed\".
5004
5005 If t, it overrides nil values of
5006 `gnus-mime-display-multipart-alternative-as-mixed' and
5007 `gnus-mime-display-multipart-related-as-mixed'."
5008   :group 'gnus-article-mime
5009   :type 'boolean)
5010
5011 (defcustom gnus-mime-display-multipart-alternative-as-mixed nil
5012   "Display \"multipart/alternative\" parts as  \"multipart/mixed\"."
5013   :version "22.1"
5014   :group 'gnus-article-mime
5015   :type 'boolean)
5016
5017 (defcustom gnus-mime-display-multipart-related-as-mixed nil
5018   "Display \"multipart/related\" parts as  \"multipart/mixed\".
5019
5020 If displaying \"text/html\" is discouraged \(see
5021 `mm-discouraged-alternatives'\) images or other material inside a
5022 \"multipart/related\" part might be overlooked when this variable is nil."
5023   :version "22.1"
5024   :group 'gnus-article-mime
5025   :type 'boolean)
5026
5027 (defun gnus-mime-display-part (handle)
5028   (cond
5029    ;; Maybe a broken MIME message.
5030    ((null handle))
5031    ;; Single part.
5032    ((not (stringp (car handle)))
5033     (gnus-mime-display-single handle))
5034    ;; User-defined multipart
5035    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
5036     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
5037              handle))
5038    ;; multipart/alternative
5039    ((and (equal (car handle) "multipart/alternative")
5040          (not (or gnus-mime-display-multipart-as-mixed
5041                   gnus-mime-display-multipart-alternative-as-mixed)))
5042     (let ((id (1+ (length gnus-article-mime-handle-alist))))
5043       (push (cons id handle) gnus-article-mime-handle-alist)
5044       (gnus-mime-display-alternative (cdr handle) nil nil id)))
5045    ;; multipart/related
5046    ((and (equal (car handle) "multipart/related")
5047          (not (or gnus-mime-display-multipart-as-mixed
5048                   gnus-mime-display-multipart-related-as-mixed)))
5049     ;;;!!!We should find the start part, but we just default
5050     ;;;!!!to the first part.
5051     ;;(gnus-mime-display-part (cadr handle))
5052     ;;;!!! Most multipart/related is an HTML message plus images.
5053     ;;;!!! Unfortunately we are unable to let W3 display those
5054     ;;;!!! included images, so we just display it as a mixed multipart.
5055     ;;(gnus-mime-display-mixed (cdr handle))
5056     ;;;!!! No, w3 can display everything just fine.
5057     (gnus-mime-display-part (cadr handle)))
5058    ((equal (car handle) "multipart/signed")
5059     (gnus-add-wash-type 'signed)
5060     (gnus-mime-display-security handle))
5061    ((equal (car handle) "multipart/encrypted")
5062     (gnus-add-wash-type 'encrypted)
5063     (gnus-mime-display-security handle))
5064    ;; Other multiparts are handled like multipart/mixed.
5065    (t
5066     (gnus-mime-display-mixed (cdr handle)))))
5067
5068 (defun gnus-mime-part-function (handles)
5069   (if (stringp (car handles))
5070       (mapcar 'gnus-mime-part-function (cdr handles))
5071     (funcall gnus-article-mime-part-function handles)))
5072
5073 (defun gnus-mime-display-mixed (handles)
5074   (mapcar 'gnus-mime-display-part handles))
5075
5076 (defun gnus-mime-display-single (handle)
5077   (let ((type (mm-handle-media-type handle))
5078         (ignored gnus-ignored-mime-types)
5079         (not-attachment t)
5080         (move nil)
5081         display text)
5082     (catch 'ignored
5083       (progn
5084         (while ignored
5085           (when (string-match (pop ignored) type)
5086             (throw 'ignored nil)))
5087         (if (and (setq not-attachment
5088                        (and (not (mm-inline-override-p handle))
5089                             (or (not (mm-handle-disposition handle))
5090                                 (equal (car (mm-handle-disposition handle))
5091                                        "inline")
5092                                 (mm-attachment-override-p handle))))
5093                  (mm-automatic-display-p handle)
5094                  (or (and
5095                       (mm-inlinable-p handle)
5096                       (mm-inlined-p handle))
5097                      (mm-automatic-external-display-p type)))
5098             (setq display t)
5099           (when (equal (mm-handle-media-supertype handle) "text")
5100             (setq text t)))
5101         (let ((id (1+ (length gnus-article-mime-handle-alist)))
5102               beg)
5103           (push (cons id handle) gnus-article-mime-handle-alist)
5104           (when (or (not display)
5105                     (not (gnus-unbuttonized-mime-type-p type)))
5106             (gnus-insert-mime-button
5107              handle id (list (or display (and not-attachment text))))
5108             (gnus-article-insert-newline)
5109             ;; Remember modify the number of forward lines.
5110             (setq move t))
5111           (setq beg (point))
5112           (cond
5113            (display
5114             (when move
5115               (forward-line -1)
5116               (setq beg (point)))
5117             (let ((mail-parse-charset gnus-newsgroup-charset)
5118                   (mail-parse-ignored-charsets
5119                    (save-excursion (condition-case ()
5120                                        (set-buffer gnus-summary-buffer)
5121                                      (error))
5122                                    gnus-newsgroup-ignored-charsets)))
5123               (mm-display-part handle t))
5124             (goto-char (point-max)))
5125            ((and text not-attachment)
5126             (when move
5127               (forward-line -1)
5128               (setq beg (point)))
5129             (gnus-article-insert-newline)
5130             (mm-insert-inline
5131              handle
5132              (let ((charset (mail-content-type-get (mm-handle-type handle)
5133                                                    'charset)))
5134                (cond ((not charset)
5135                       (mm-string-as-multibyte (mm-get-part handle)))
5136                      ((eq charset 'gnus-decoded)
5137                       (with-current-buffer (mm-handle-buffer handle)
5138                         (buffer-string)))
5139                      (t
5140                       (mm-decode-string (mm-get-part handle) charset)))))
5141             (goto-char (point-max))))
5142           ;; Do highlighting.
5143           (save-excursion
5144             (save-restriction
5145               (narrow-to-region beg (point))
5146               (gnus-treat-article
5147                nil id
5148                (gnus-article-mime-total-parts)
5149                (mm-handle-media-type handle)))))))))
5150
5151 (defun gnus-unbuttonized-mime-type-p (type)
5152   "Say whether TYPE is to be unbuttonized."
5153   (unless gnus-inhibit-mime-unbuttonizing
5154     (when (catch 'found
5155             (let ((types gnus-unbuttonized-mime-types))
5156               (while types
5157                 (when (string-match (pop types) type)
5158                   (throw 'found t)))))
5159       (not (catch 'found
5160              (let ((types gnus-buttonized-mime-types))
5161                (while types
5162                  (when (string-match (pop types) type)
5163                    (throw 'found t)))))))))
5164
5165 (defun gnus-article-insert-newline ()
5166   "Insert a newline, but mark it as undeletable."
5167   (gnus-put-text-property
5168    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
5169
5170 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
5171   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
5172          (ihandles handles)
5173          (point (point))
5174          handle (inhibit-read-only t) from props begend not-pref)
5175     (save-window-excursion
5176       (save-restriction
5177         (when ibegend
5178           (narrow-to-region (car ibegend)
5179                             (or (cdr ibegend)
5180                                 (progn
5181                                   (goto-char (car ibegend))
5182                                   (forward-line 2)
5183                                   (point))))
5184           (delete-region (point-min) (point-max))
5185           (mm-remove-parts handles))
5186         (setq begend (list (point-marker)))
5187         ;; Do the toggle.
5188         (unless (setq not-pref (cadr (member preferred ihandles)))
5189           (setq not-pref (car ihandles)))
5190         (when (or ibegend
5191                   (not preferred)
5192                   (not (gnus-unbuttonized-mime-type-p
5193                         "multipart/alternative")))
5194           (gnus-add-text-properties
5195            (setq from (point))
5196            (progn
5197              (insert (format "%d.  " id))
5198              (point))
5199            `(gnus-callback
5200              (lambda (handles)
5201                (unless ,(not ibegend)
5202                  (setq gnus-article-mime-handle-alist
5203                        ',gnus-article-mime-handle-alist))
5204                (gnus-mime-display-alternative
5205                 ',ihandles ',not-pref ',begend ,id))
5206              keymap ,gnus-mime-button-map
5207              ,gnus-mouse-face-prop ,gnus-article-mouse-face
5208              face ,gnus-article-button-face
5209              gnus-part ,id
5210              article-type multipart))
5211           (widget-convert-button 'link from (point)
5212                                  :action 'gnus-widget-press-button
5213                                  :button-keymap gnus-widget-button-keymap)
5214           ;; Do the handles
5215           (while (setq handle (pop handles))
5216             (gnus-add-text-properties
5217              (setq from (point))
5218              (progn
5219                (insert (format "(%c) %-18s"
5220                                (if (equal handle preferred) ?* ? )
5221                                (mm-handle-media-type handle)))
5222                (point))
5223              `(gnus-callback
5224                (lambda (handles)
5225                  (unless ,(not ibegend)
5226                    (setq gnus-article-mime-handle-alist
5227                          ',gnus-article-mime-handle-alist))
5228                  (gnus-mime-display-alternative
5229                   ',ihandles ',handle ',begend ,id))
5230                keymap ,gnus-mime-button-map
5231                ,gnus-mouse-face-prop ,gnus-article-mouse-face
5232                face ,gnus-article-button-face
5233                gnus-part ,id
5234                gnus-data ,handle))
5235             (widget-convert-button 'link from (point)
5236                                    :action 'gnus-widget-press-button
5237                                    :button-keymap gnus-widget-button-keymap)
5238             (insert "  "))
5239           (insert "\n\n"))
5240         (when preferred
5241           (if (stringp (car preferred))
5242               (gnus-display-mime preferred)
5243             (let ((mail-parse-charset gnus-newsgroup-charset)
5244                   (mail-parse-ignored-charsets
5245                    (with-current-buffer gnus-summary-buffer
5246                      gnus-newsgroup-ignored-charsets)))
5247               (mm-display-part preferred)
5248               ;; Do highlighting.
5249               (save-excursion
5250                 (save-restriction
5251                   (narrow-to-region (car begend) (point-max))
5252                   (gnus-treat-article
5253                    nil (length gnus-article-mime-handle-alist)
5254                    (gnus-article-mime-total-parts)
5255                    (mm-handle-media-type handle))))))
5256           (goto-char (point-max))
5257           (setcdr begend (point-marker)))))
5258     (when ibegend
5259       (goto-char point))))
5260
5261 (defconst gnus-article-wash-status-strings
5262   (let ((alist '((cite "c" "Possible hidden citation text"
5263                        " " "All citation text visible")
5264                  (headers "h" "Hidden headers"
5265                           " " "All headers visible.")
5266                  (pgp "p" "Encrypted or signed message status hidden"
5267                       " " "No hidden encryption nor digital signature status")
5268                  (signature "s" "Signature has been hidden"
5269                             " " "Signature is visible")
5270                  (overstrike "o" "Overstrike (^H) characters applied"
5271                              " " "No overstrike characters applied")
5272                  (emphasis "e" "/*_Emphasis_*/ characters applied"
5273                            " " "No /*_emphasis_*/ characters applied")))
5274         result)
5275     (dolist (entry alist result)
5276       (let ((key (nth 0 entry))
5277             (on (copy-sequence (nth 1 entry)))
5278             (on-help (nth 2 entry))
5279             (off (copy-sequence (nth 3 entry)))
5280             (off-help (nth 4 entry)))
5281         (put-text-property 0 1 'help-echo on-help on)
5282         (put-text-property 0 1 'help-echo off-help off)
5283         (push (list key on off) result))))
5284   "Alist of strings describing wash status in the mode line.
5285 Each entry has the form (KEY ON OF), where the KEY is a symbol
5286 representing the particular washing function, ON is the string to use
5287 in the article mode line when the washing function is active, and OFF
5288 is the string to use when it is inactive.")
5289
5290 (defun gnus-article-wash-status-entry (key value)
5291   (let ((entry (assoc key gnus-article-wash-status-strings)))
5292     (if value (nth 1 entry) (nth 2 entry))))
5293
5294 (defun gnus-article-wash-status ()
5295   "Return a string which display status of article washing."
5296   (with-current-buffer gnus-article-buffer
5297     (let ((cite (memq 'cite gnus-article-wash-types))
5298           (headers (memq 'headers gnus-article-wash-types))
5299           (boring (memq 'boring-headers gnus-article-wash-types))
5300           (pgp (memq 'pgp gnus-article-wash-types))
5301           (pem (memq 'pem gnus-article-wash-types))
5302           (signed (memq 'signed gnus-article-wash-types))
5303           (encrypted (memq 'encrypted gnus-article-wash-types))
5304           (signature (memq 'signature gnus-article-wash-types))
5305           (overstrike (memq 'overstrike gnus-article-wash-types))
5306           (emphasis (memq 'emphasis gnus-article-wash-types)))
5307       (concat
5308        (gnus-article-wash-status-entry 'cite cite)
5309        (gnus-article-wash-status-entry 'headers (or headers boring))
5310        (gnus-article-wash-status-entry 'pgp (or pgp pem signed encrypted))
5311        (gnus-article-wash-status-entry 'signature signature)
5312        (gnus-article-wash-status-entry 'overstrike overstrike)
5313        (gnus-article-wash-status-entry 'emphasis emphasis)))))
5314
5315 (defun gnus-add-wash-type (type)
5316   "Add a washing of TYPE to the current status."
5317   (add-to-list 'gnus-article-wash-types type))
5318
5319 (defun gnus-delete-wash-type (type)
5320   "Add a washing of TYPE to the current status."
5321   (setq gnus-article-wash-types (delq type gnus-article-wash-types)))
5322
5323 (defun gnus-add-image (category image)
5324   "Add IMAGE of CATEGORY to the list of displayed images."
5325   (let ((entry (assq category gnus-article-image-alist)))
5326     (unless entry
5327       (setq entry (list category))
5328       (push entry gnus-article-image-alist))
5329     (nconc entry (list image))))
5330
5331 (defun gnus-delete-images (category)
5332   "Delete all images in CATEGORY."
5333   (let ((entry (assq category gnus-article-image-alist)))
5334     (dolist (image (cdr entry))
5335       (gnus-remove-image image category))
5336     (setq gnus-article-image-alist (delq entry gnus-article-image-alist))
5337     (gnus-delete-wash-type category)))
5338
5339 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
5340
5341 (defun gnus-article-maybe-hide-headers ()
5342   "Hide unwanted headers if `gnus-have-all-headers' is nil.
5343 Provided for backwards compatibility."
5344   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
5345                  (not (with-current-buffer gnus-summary-buffer
5346                         gnus-have-all-headers)))
5347              (not gnus-inhibit-hiding))
5348     (gnus-article-hide-headers)))
5349
5350 ;;; Article savers.
5351
5352 (defun gnus-output-to-file (file-name)
5353   "Append the current article to a file named FILE-NAME."
5354   (let ((artbuf (current-buffer)))
5355     (with-temp-buffer
5356       (insert-buffer-substring artbuf)
5357       ;; Append newline at end of the buffer as separator, and then
5358       ;; save it to file.
5359       (goto-char (point-max))
5360       (insert "\n")
5361       (let ((file-name-coding-system nnmail-pathname-coding-system))
5362         (mm-append-to-file (point-min) (point-max) file-name))
5363       t)))
5364
5365 (defun gnus-narrow-to-page (&optional arg)
5366   "Narrow the article buffer to a page.
5367 If given a numerical ARG, move forward ARG pages."
5368   (interactive "P")
5369   (setq arg (if arg (prefix-numeric-value arg) 0))
5370   (save-excursion
5371     (set-buffer gnus-article-buffer)
5372     (goto-char (point-min))
5373     (widen)
5374     ;; Remove any old next/prev buttons.
5375     (when (gnus-visual-p 'page-marker)
5376       (let ((inhibit-read-only t))
5377         (gnus-remove-text-with-property 'gnus-prev)
5378         (gnus-remove-text-with-property 'gnus-next)))
5379     (if
5380         (cond ((< arg 0)
5381                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
5382               ((> arg 0)
5383                (re-search-forward page-delimiter nil 'move arg)))
5384         (goto-char (match-end 0))
5385       (save-excursion
5386         (goto-char (point-min))
5387         (setq gnus-page-broken
5388               (and (re-search-forward page-delimiter nil t) t))))
5389     (when gnus-page-broken
5390       (narrow-to-region
5391        (point)
5392        (if (re-search-forward page-delimiter nil 'move)
5393            (match-beginning 0)
5394          (point)))
5395       (when (and (gnus-visual-p 'page-marker)
5396                  (> (point-min) (save-restriction (widen) (point-min))))
5397         (save-excursion
5398           (goto-char (point-min))
5399           (gnus-insert-prev-page-button)))
5400       (when (and (gnus-visual-p 'page-marker)
5401                  (< (point-max) (save-restriction (widen) (point-max))))
5402         (save-excursion
5403           (goto-char (point-max))
5404           (gnus-insert-next-page-button))))))
5405
5406 ;; Article mode commands
5407
5408 (defun gnus-article-goto-next-page ()
5409   "Show the next page of the article."
5410   (interactive)
5411   (when (gnus-article-next-page)
5412     (goto-char (point-min))
5413     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
5414
5415
5416 (defun gnus-article-goto-prev-page ()
5417   "Show the previous page of the article."
5418   (interactive)
5419   (if (bobp)
5420       (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
5421     (gnus-article-prev-page nil)))
5422
5423 ;; This is cleaner but currently breaks `gnus-pick-mode':
5424 ;;
5425 ;; (defun gnus-article-goto-next-page ()
5426 ;;   "Show the next page of the article."
5427 ;;   (interactive)
5428 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
5429 ;;     (gnus-summary-next-page)))
5430 ;;
5431 ;; (defun gnus-article-goto-prev-page ()
5432 ;;   "Show the next page of the article."
5433 ;;   (interactive)
5434 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
5435 ;;     (gnus-summary-prev-page)))
5436
5437 (defun gnus-article-next-page (&optional lines)
5438   "Show the next page of the current article.
5439 If end of article, return non-nil.  Otherwise return nil.
5440 Argument LINES specifies lines to be scrolled up."
5441   (interactive "p")
5442   (move-to-window-line -1)
5443   (if (save-excursion
5444         (end-of-line)
5445         (and (pos-visible-in-window-p)  ;Not continuation line.
5446              (>= (1+ (point)) (point-max)))) ;Allow for trailing newline.
5447       ;; Nothing in this page.
5448       (if (or (not gnus-page-broken)
5449               (save-excursion
5450                 (save-restriction
5451                   (widen)
5452                   (forward-line)
5453                   (eobp)))) ;Real end-of-buffer?
5454           (progn
5455             (when gnus-article-over-scroll
5456               (gnus-article-next-page-1 lines))
5457             t)                  ;Nothing more.
5458         (gnus-narrow-to-page 1)         ;Go to next page.
5459         nil)
5460     ;; More in this page.
5461     (gnus-article-next-page-1 lines)
5462     nil))
5463
5464 (defmacro gnus-article-beginning-of-window ()
5465   "Move point to the beginning of the window.
5466 In Emacs, the point is placed at the line number which `scroll-margin'
5467 specifies."
5468   (if (featurep 'xemacs)
5469       '(move-to-window-line 0)
5470     '(move-to-window-line
5471       (min (max 0 scroll-margin)
5472            (max 1 (- (window-height)
5473                      (if mode-line-format 1 0)
5474                      (if header-line-format 1 0)))))))
5475
5476 (defun gnus-article-next-page-1 (lines)
5477   (when (and (not (featurep 'xemacs))
5478              (numberp lines)
5479              (> lines 0)
5480              (numberp (symbol-value 'scroll-margin))
5481              (> (symbol-value 'scroll-margin) 0))
5482     ;; Protect against the bug that Emacs 21.x hangs up when scrolling up for
5483     ;; too many number of lines if `scroll-margin' is set as two or greater.
5484     (setq lines (min lines
5485                      (max 0 (- (count-lines (window-start) (point-max))
5486                                (symbol-value 'scroll-margin))))))
5487   (condition-case ()
5488       (let ((scroll-in-place nil))
5489         (scroll-up lines))
5490     (end-of-buffer
5491      ;; Long lines may cause an end-of-buffer error.
5492      (goto-char (point-max))))
5493   (gnus-article-beginning-of-window))
5494
5495 (defun gnus-article-prev-page (&optional lines)
5496   "Show previous page of current article.
5497 Argument LINES specifies lines to be scrolled down."
5498   (interactive "p")
5499   (move-to-window-line 0)
5500   (if (and gnus-page-broken
5501            (bobp)
5502            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
5503       (progn
5504         (gnus-narrow-to-page -1)        ;Go to previous page.
5505         (goto-char (point-max))
5506         (recenter -1))
5507     (prog1
5508         (condition-case ()
5509             (let ((scroll-in-place nil))
5510               (scroll-down lines))
5511           (beginning-of-buffer
5512            (goto-char (point-min))))
5513       (gnus-article-beginning-of-window))))
5514
5515 (defun gnus-article-only-boring-p ()
5516   "Decide whether there is only boring text remaining in the article.
5517 Something \"interesting\" is a word of at least two letters that does
5518 not have a face in `gnus-article-boring-faces'."
5519   (when (and gnus-article-skip-boring
5520              (boundp 'gnus-article-boring-faces)
5521              (symbol-value 'gnus-article-boring-faces))
5522     (save-excursion
5523       (catch 'only-boring
5524         (while (re-search-forward "\\b\\w\\w" nil t)
5525           (forward-char -1)
5526           (when (not (gnus-intersection
5527                       (gnus-faces-at (point))
5528                       (symbol-value 'gnus-article-boring-faces)))
5529             (throw 'only-boring nil)))
5530         (throw 'only-boring t)))))
5531
5532 (defun gnus-article-refer-article ()
5533   "Read article specified by message-id around point."
5534   (interactive)
5535   (save-excursion
5536     (re-search-backward "[ \t]\\|^" (point-at-bol) t)
5537     (re-search-forward "<?news:<?\\|<" (point-at-eol) t)
5538     (if (re-search-forward "[^@ ]+@[^ \t>]+" (point-at-eol) t)
5539         (let ((msg-id (concat "<" (match-string 0) ">")))
5540           (set-buffer gnus-summary-buffer)
5541           (gnus-summary-refer-article msg-id))
5542       (error "No references around point"))))
5543
5544 (defun gnus-article-show-summary ()
5545   "Reconfigure windows to show summary buffer."
5546   (interactive)
5547   (if (not (gnus-buffer-live-p gnus-summary-buffer))
5548       (error "There is no summary buffer for this article buffer")
5549     (gnus-article-set-globals)
5550     (gnus-configure-windows 'article)
5551     (gnus-summary-goto-subject gnus-current-article)
5552     (gnus-summary-position-point)))
5553
5554 (defun gnus-article-describe-briefly ()
5555   "Describe article mode commands briefly."
5556   (interactive)
5557   (gnus-message 6 (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")))
5558
5559 (defun gnus-article-summary-command ()
5560   "Execute the last keystroke in the summary buffer."
5561   (interactive)
5562   (let ((obuf (current-buffer))
5563         (owin (current-window-configuration))
5564         func)
5565     (switch-to-buffer gnus-article-current-summary 'norecord)
5566     (setq func (lookup-key (current-local-map) (this-command-keys)))
5567     (call-interactively func)
5568     (set-buffer obuf)
5569     (set-window-configuration owin)
5570     (set-window-point (get-buffer-window (current-buffer)) (point))))
5571
5572 (defun gnus-article-summary-command-nosave ()
5573   "Execute the last keystroke in the summary buffer."
5574   (interactive)
5575   (let (func)
5576     (pop-to-buffer gnus-article-current-summary 'norecord)
5577     (setq func (lookup-key (current-local-map) (this-command-keys)))
5578     (call-interactively func)))
5579
5580 (defun gnus-article-check-buffer ()
5581   "Beep if not in an article buffer."
5582   (unless (equal major-mode 'gnus-article-mode)
5583     (error "Command invoked outside of a Gnus article buffer")))
5584
5585 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
5586   "Read a summary buffer key sequence and execute it from the article buffer."
5587   (interactive "P")
5588   (gnus-article-check-buffer)
5589   (let ((nosaves
5590          '("q" "Q"  "c" "r" "\C-c\C-f" "m"  "a" "f"
5591            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
5592            "=" "^" "\M-^" "|"))
5593         (nosave-but-article
5594          '("A\r"))
5595         (nosave-in-article
5596          '("\C-d"))
5597         (up-to-top
5598          '("n" "Gn" "p" "Gp"))
5599         keys new-sum-point)
5600     (save-excursion
5601       (set-buffer gnus-article-current-summary)
5602       (let (gnus-pick-mode)
5603         (push (or key last-command-event) unread-command-events)
5604         (setq keys (if (featurep 'xemacs)
5605                        (events-to-keys (read-key-sequence nil))
5606                      (read-key-sequence nil)))))
5607
5608     (message "")
5609
5610     (if (or (member keys nosaves)
5611             (member keys nosave-but-article)
5612             (member keys nosave-in-article))
5613         (let (func)
5614           (save-window-excursion
5615             (pop-to-buffer gnus-article-current-summary 'norecord)
5616             ;; We disable the pick minor mode commands.
5617             (let (gnus-pick-mode)
5618               (setq func (lookup-key (current-local-map) keys))))
5619           (if (or (not func)
5620                   (numberp func))
5621               (ding)
5622             (unless (member keys nosave-in-article)
5623               (set-buffer gnus-article-current-summary))
5624             (call-interactively func)
5625             (setq new-sum-point (point)))
5626           (when (member keys nosave-but-article)
5627             (pop-to-buffer gnus-article-buffer 'norecord)))
5628       ;; These commands should restore window configuration.
5629       (let ((obuf (current-buffer))
5630             (owin (current-window-configuration))
5631             (opoint (point))
5632             win func in-buffer selected new-sum-start new-sum-hscroll)
5633         (cond (not-restore-window
5634                (pop-to-buffer gnus-article-current-summary 'norecord))
5635               ((setq win (get-buffer-window gnus-article-current-summary))
5636                (select-window win))
5637               (t
5638                (switch-to-buffer gnus-article-current-summary 'norecord)))
5639         (setq in-buffer (current-buffer))
5640         ;; We disable the pick minor mode commands.
5641         (if (and (setq func (let (gnus-pick-mode)
5642                               (lookup-key (current-local-map) keys)))
5643                  (functionp func))
5644             (progn
5645               (call-interactively func)
5646               (when (eq win (selected-window))
5647                 (setq new-sum-point (point)
5648                       new-sum-start (window-start win)
5649                       new-sum-hscroll (window-hscroll win)))
5650               (when (eq in-buffer (current-buffer))
5651                 (setq selected (gnus-summary-select-article))
5652                 (set-buffer obuf)
5653                 (unless not-restore-window
5654                   (set-window-configuration owin))
5655                 (when (eq selected 'old)
5656                   (article-goto-body)
5657                   (set-window-start (get-buffer-window (current-buffer))
5658                                     1)
5659                   (set-window-point (get-buffer-window (current-buffer))
5660                                     (point)))
5661                 (when (and (not not-restore-window)
5662                            new-sum-point)
5663                   (set-window-point win new-sum-point)
5664                   (set-window-start win new-sum-start)
5665                   (set-window-hscroll win new-sum-hscroll))))
5666           (set-window-configuration owin)
5667           (ding))))))
5668
5669 (defun gnus-article-describe-key (key)
5670   "Display documentation of the function invoked by KEY.  KEY is a string."
5671   (interactive "kDescribe key: ")
5672   (gnus-article-check-buffer)
5673   (if (eq (key-binding key) 'gnus-article-read-summary-keys)
5674       (save-excursion
5675         (set-buffer gnus-article-current-summary)
5676         (let (gnus-pick-mode)
5677           (if (featurep 'xemacs)
5678               (progn
5679                 (push (elt key 0) unread-command-events)
5680                 (setq key (events-to-keys
5681                            (read-key-sequence "Describe key: "))))
5682             (setq unread-command-events
5683                   (mapcar
5684                    (lambda (x) (if (>= x 128) (list 'meta (- x 128)) x))
5685                    (string-to-list key)))
5686             (setq key (read-key-sequence "Describe key: "))))
5687         (describe-key key))
5688     (describe-key key)))
5689
5690 (defun gnus-article-describe-key-briefly (key &optional insert)
5691   "Display documentation of the function invoked by KEY.  KEY is a string."
5692   (interactive "kDescribe key: \nP")
5693   (gnus-article-check-buffer)
5694   (if (eq (key-binding key) 'gnus-article-read-summary-keys)
5695       (save-excursion
5696         (set-buffer gnus-article-current-summary)
5697         (let (gnus-pick-mode)
5698           (if (featurep 'xemacs)
5699               (progn
5700                 (push (elt key 0) unread-command-events)
5701                 (setq key (events-to-keys
5702                            (read-key-sequence "Describe key: "))))
5703             (setq unread-command-events
5704                   (mapcar
5705                    (lambda (x) (if (>= x 128) (list 'meta (- x 128)) x))
5706                    (string-to-list key)))
5707             (setq key (read-key-sequence "Describe key: "))))
5708         (describe-key-briefly key insert))
5709     (describe-key-briefly key insert)))
5710
5711 (defun gnus-article-reply-with-original (&optional wide)
5712   "Start composing a reply mail to the current message.
5713 The text in the region will be yanked.  If the region isn't active,
5714 the entire article will be yanked."
5715   (interactive "P")
5716   (let ((article (cdr gnus-article-current))
5717         contents)
5718     (if (not (gnus-region-active-p))
5719         (with-current-buffer gnus-summary-buffer
5720           (gnus-summary-reply (list (list article)) wide))
5721       (setq contents (buffer-substring (point) (mark t)))
5722       ;; Deactivate active regions.
5723       (when (and (boundp 'transient-mark-mode)
5724                  transient-mark-mode)
5725         (setq mark-active nil))
5726       (with-current-buffer gnus-summary-buffer
5727         (gnus-summary-reply
5728          (list (list article contents)) wide)))))
5729
5730 (defun gnus-article-followup-with-original ()
5731   "Compose a followup to the current article.
5732 The text in the region will be yanked.  If the region isn't active,
5733 the entire article will be yanked."
5734   (interactive)
5735   (let ((article (cdr gnus-article-current))
5736         contents)
5737       (if (not (gnus-region-active-p))
5738           (with-current-buffer gnus-summary-buffer
5739             (gnus-summary-followup (list (list article))))
5740         (setq contents (buffer-substring (point) (mark t)))
5741         ;; Deactivate active regions.
5742         (when (and (boundp 'transient-mark-mode)
5743                    transient-mark-mode)
5744           (setq mark-active nil))
5745         (with-current-buffer gnus-summary-buffer
5746           (gnus-summary-followup
5747            (list (list article contents)))))))
5748
5749 (defun gnus-article-hide (&optional arg force)
5750   "Hide all the gruft in the current article.
5751 This means that signatures, cited text and (some) headers will be
5752 hidden.
5753 If given a prefix, show the hidden text instead."
5754   (interactive (append (gnus-article-hidden-arg) (list 'force)))
5755   (gnus-article-hide-headers arg)
5756   (gnus-article-hide-list-identifiers arg)
5757   (gnus-article-hide-citation-maybe arg force)
5758   (gnus-article-hide-signature arg))
5759
5760 (defun gnus-article-maybe-highlight ()
5761   "Do some article highlighting if article highlighting is requested."
5762   (when (gnus-visual-p 'article-highlight 'highlight)
5763     (gnus-article-highlight-some)))
5764
5765 (defun gnus-check-group-server ()
5766   ;; Make sure the connection to the server is alive.
5767   (unless (gnus-server-opened
5768            (gnus-find-method-for-group gnus-newsgroup-name))
5769     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
5770     (gnus-request-group gnus-newsgroup-name t)))
5771
5772 (eval-when-compile
5773   (autoload 'nneething-get-file-name "nneething"))
5774
5775 (defun gnus-request-article-this-buffer (article group)
5776   "Get an article and insert it into this buffer."
5777   (let (do-update-line sparse-header)
5778     (prog1
5779         (save-excursion
5780           (erase-buffer)
5781           (gnus-kill-all-overlays)
5782           (setq group (or group gnus-newsgroup-name))
5783
5784           ;; Using `gnus-request-article' directly will insert the article into
5785           ;; `nntp-server-buffer' - so we'll save some time by not having to
5786           ;; copy it from the server buffer into the article buffer.
5787
5788           ;; We only request an article by message-id when we do not have the
5789           ;; headers for it, so we'll have to get those.
5790           (when (stringp article)
5791             (gnus-read-header article))
5792
5793           ;; If the article number is negative, that means that this article
5794           ;; doesn't belong in this newsgroup (possibly), so we find its
5795           ;; message-id and request it by id instead of number.
5796           (when (and (numberp article)
5797                      gnus-summary-buffer
5798                      (get-buffer gnus-summary-buffer)
5799                      (gnus-buffer-exists-p gnus-summary-buffer))
5800             (save-excursion
5801               (set-buffer gnus-summary-buffer)
5802               (let ((header (gnus-summary-article-header article)))
5803                 (when (< article 0)
5804                   (cond
5805                    ((memq article gnus-newsgroup-sparse)
5806                     ;; This is a sparse gap article.
5807                     (setq do-update-line article)
5808                     (setq article (mail-header-id header))
5809                     (setq sparse-header (gnus-read-header article))
5810                     (setq gnus-newsgroup-sparse
5811                           (delq article gnus-newsgroup-sparse)))
5812                    ((vectorp header)
5813                     ;; It's a real article.
5814                     (setq article (mail-header-id header)))
5815                    (t
5816                     ;; It is an extracted pseudo-article.
5817                     (setq article 'pseudo)
5818                     (gnus-request-pseudo-article header))))
5819
5820                 (let ((method (gnus-find-method-for-group
5821                                gnus-newsgroup-name)))
5822                   (when (and (eq (car method) 'nneething)
5823                              (vectorp header))
5824                     (let ((dir (nneething-get-file-name
5825                                 (mail-header-id header))))
5826                       (when (and (stringp dir)
5827                                  (file-directory-p dir))
5828                         (setq article 'nneething)
5829                         (gnus-group-enter-directory dir))))))))
5830
5831           (cond
5832            ;; Refuse to select canceled articles.
5833            ((and (numberp article)
5834                  gnus-summary-buffer
5835                  (get-buffer gnus-summary-buffer)
5836                  (gnus-buffer-exists-p gnus-summary-buffer)
5837                  (eq (cdr (with-current-buffer gnus-summary-buffer
5838                             (assq article gnus-newsgroup-reads)))
5839                      gnus-canceled-mark))
5840             nil)
5841            ;; We first check `gnus-original-article-buffer'.
5842            ((and (get-buffer gnus-original-article-buffer)
5843                  (numberp article)
5844                  (with-current-buffer gnus-original-article-buffer
5845                    (and (equal (car gnus-original-article) group)
5846                         (eq (cdr gnus-original-article) article))))
5847             (insert-buffer-substring gnus-original-article-buffer)
5848             'article)
5849            ;; Check the backlog.
5850            ((and gnus-keep-backlog
5851                  (gnus-backlog-request-article group article (current-buffer)))
5852             'article)
5853            ;; Check asynchronous pre-fetch.
5854            ((gnus-async-request-fetched-article group article (current-buffer))
5855             (gnus-async-prefetch-next group article gnus-summary-buffer)
5856             (when (and (numberp article) gnus-keep-backlog)
5857               (gnus-backlog-enter-article group article (current-buffer)))
5858             'article)
5859            ;; Check the cache.
5860            ((and gnus-use-cache
5861                  (numberp article)
5862                  (gnus-cache-request-article article group))
5863             'article)
5864            ;; Check the agent cache.
5865            ((gnus-agent-request-article article group)
5866             'article)
5867            ;; Get the article and put into the article buffer.
5868            ((or (stringp article)
5869                 (numberp article))
5870             (let ((gnus-override-method gnus-override-method)
5871                   (methods (and (stringp article)
5872                                 gnus-refer-article-method))
5873                   (backend (car (gnus-find-method-for-group
5874                                  gnus-newsgroup-name)))
5875                   result
5876                   (inhibit-read-only t))
5877               (if (or (not (listp methods))
5878                       (and (symbolp (car methods))
5879                            (assq (car methods) nnoo-definition-alist)))
5880                   (setq methods (list methods)))
5881               (when (and (null gnus-override-method)
5882                          methods)
5883                 (setq gnus-override-method (pop methods)))
5884               (while (not result)
5885                 (when (eq gnus-override-method 'current)
5886                   (setq gnus-override-method
5887                         (with-current-buffer gnus-summary-buffer
5888                           gnus-current-select-method)))
5889                 (erase-buffer)
5890                 (gnus-kill-all-overlays)
5891                 (let ((gnus-newsgroup-name group))
5892                   (gnus-check-group-server))
5893                 (cond
5894                  ((gnus-request-article article group (current-buffer))
5895                   (when (numberp article)
5896                     (gnus-async-prefetch-next group article
5897                                               gnus-summary-buffer)
5898                     (when gnus-keep-backlog
5899                       (gnus-backlog-enter-article
5900                        group article (current-buffer))))
5901                   (setq result 'article))
5902                  (methods
5903                   (setq gnus-override-method (pop methods)))
5904                  ((not (string-match "^400 "
5905                                      (nnheader-get-report backend)))
5906                   ;; If we get 400 server disconnect, reconnect and
5907                   ;; retry; otherwise, assume the article has expired.
5908                   (setq result 'done))))
5909               (and (eq result 'article) 'article)))
5910            ;; It was a pseudo.
5911            (t article)))
5912
5913       ;; Associate this article with the current summary buffer.
5914       (setq gnus-article-current-summary gnus-summary-buffer)
5915
5916       ;; Take the article from the original article buffer
5917       ;; and place it in the buffer it's supposed to be in.
5918       (when (and (get-buffer gnus-article-buffer)
5919                  (equal (buffer-name (current-buffer))
5920                         (buffer-name (get-buffer gnus-article-buffer))))
5921         (save-excursion
5922           (if (get-buffer gnus-original-article-buffer)
5923               (set-buffer gnus-original-article-buffer)
5924             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
5925             (buffer-disable-undo)
5926             (setq major-mode 'gnus-original-article-mode)
5927             (setq buffer-read-only t))
5928           (let ((inhibit-read-only t))
5929             (erase-buffer)
5930             (insert-buffer-substring gnus-article-buffer))
5931           (setq gnus-original-article (cons group article)))
5932
5933         ;; Decode charsets.
5934         (run-hooks 'gnus-article-decode-hook)
5935         ;; Mark article as decoded or not.
5936         (setq gnus-article-decoded-p gnus-article-decode-hook))
5937
5938       ;; Update sparse articles.
5939       (when (and do-update-line
5940                  (or (numberp article)
5941                      (stringp article)))
5942         (let ((buf (current-buffer)))
5943           (set-buffer gnus-summary-buffer)
5944           (gnus-summary-update-article do-update-line sparse-header)
5945           (gnus-summary-goto-subject do-update-line nil t)
5946           (set-window-point (gnus-get-buffer-window (current-buffer) t)
5947                             (point))
5948           (set-buffer buf))))))
5949
5950 ;;;
5951 ;;; Article editing
5952 ;;;
5953
5954 (defcustom gnus-article-edit-mode-hook nil
5955   "Hook run in article edit mode buffers."
5956   :group 'gnus-article-various
5957   :type 'hook)
5958
5959 (defvar gnus-article-edit-done-function nil)
5960
5961 (defvar gnus-article-edit-mode-map nil)
5962
5963 ;; Should we be using derived.el for this?
5964 (unless gnus-article-edit-mode-map
5965   (setq gnus-article-edit-mode-map (make-keymap))
5966   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
5967
5968   (gnus-define-keys gnus-article-edit-mode-map
5969     "\C-c?"    describe-mode
5970     "\C-c\C-c" gnus-article-edit-done
5971     "\C-c\C-k" gnus-article-edit-exit
5972     "\C-c\C-f\C-t" message-goto-to
5973     "\C-c\C-f\C-o" message-goto-from
5974     "\C-c\C-f\C-b" message-goto-bcc
5975     ;;"\C-c\C-f\C-w" message-goto-fcc
5976     "\C-c\C-f\C-c" message-goto-cc
5977     "\C-c\C-f\C-s" message-goto-subject
5978     "\C-c\C-f\C-r" message-goto-reply-to
5979     "\C-c\C-f\C-n" message-goto-newsgroups
5980     "\C-c\C-f\C-d" message-goto-distribution
5981     "\C-c\C-f\C-f" message-goto-followup-to
5982     "\C-c\C-f\C-m" message-goto-mail-followup-to
5983     "\C-c\C-f\C-k" message-goto-keywords
5984     "\C-c\C-f\C-u" message-goto-summary
5985     "\C-c\C-f\C-i" message-insert-or-toggle-importance
5986     "\C-c\C-f\C-a" message-generate-unsubscribed-mail-followup-to
5987     "\C-c\C-b" message-goto-body
5988     "\C-c\C-i" message-goto-signature
5989
5990     "\C-c\C-t" message-insert-to
5991     "\C-c\C-n" message-insert-newsgroups
5992     "\C-c\C-o" message-sort-headers
5993     "\C-c\C-e" message-elide-region
5994     "\C-c\C-v" message-delete-not-region
5995     "\C-c\C-z" message-kill-to-signature
5996     "\M-\r" message-newline-and-reformat
5997     "\C-c\C-a" mml-attach-file
5998     "\C-a" message-beginning-of-line
5999     "\t" message-tab
6000     "\M-;" comment-region)
6001
6002   (gnus-define-keys (gnus-article-edit-wash-map
6003                      "\C-c\C-w" gnus-article-edit-mode-map)
6004     "f" gnus-article-edit-full-stops))
6005
6006 (easy-menu-define
6007   gnus-article-edit-mode-field-menu gnus-article-edit-mode-map ""
6008   '("Field"
6009     ["Fetch To" message-insert-to t]
6010     ["Fetch Newsgroups" message-insert-newsgroups t]
6011     "----"
6012     ["To" message-goto-to t]
6013     ["From" message-goto-from t]
6014     ["Subject" message-goto-subject t]
6015     ["Cc" message-goto-cc t]
6016     ["Reply-To" message-goto-reply-to t]
6017     ["Summary" message-goto-summary t]
6018     ["Keywords" message-goto-keywords t]
6019     ["Newsgroups" message-goto-newsgroups t]
6020     ["Followup-To" message-goto-followup-to t]
6021     ["Mail-Followup-To" message-goto-mail-followup-to t]
6022     ["Distribution" message-goto-distribution t]
6023     ["Body" message-goto-body t]
6024     ["Signature" message-goto-signature t]))
6025
6026 (define-derived-mode gnus-article-edit-mode message-mode "Article Edit"
6027   "Major mode for editing articles.
6028 This is an extended text-mode.
6029
6030 \\{gnus-article-edit-mode-map}"
6031   (make-local-variable 'gnus-article-edit-done-function)
6032   (make-local-variable 'gnus-prev-winconf)
6033   (set (make-local-variable 'font-lock-defaults)
6034        '(message-font-lock-keywords t))
6035   (set (make-local-variable 'mail-header-separator) "")
6036   (set (make-local-variable 'gnus-article-edit-mode) t)
6037   (easy-menu-add message-mode-field-menu message-mode-map)
6038   (mml-mode)
6039   (setq buffer-read-only nil)
6040   (buffer-enable-undo)
6041   (widen))
6042
6043 (defun gnus-article-edit (&optional force)
6044   "Edit the current article.
6045 This will have permanent effect only in mail groups.
6046 If FORCE is non-nil, allow editing of articles even in read-only
6047 groups."
6048   (interactive "P")
6049   (when (and (not force)
6050              (gnus-group-read-only-p))
6051     (error "The current newsgroup does not support article editing"))
6052   (gnus-article-date-original)
6053   (gnus-article-edit-article
6054    'ignore
6055    `(lambda (no-highlight)
6056       'ignore
6057       (gnus-summary-edit-article-done
6058        ,(or (mail-header-references gnus-current-headers) "")
6059        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
6060
6061 (defun gnus-article-edit-article (start-func exit-func &optional quiet)
6062   "Start editing the contents of the current article buffer."
6063   (let ((winconf (current-window-configuration)))
6064     (set-buffer gnus-article-buffer)
6065     (let ((message-auto-save-directory
6066            ;; Don't associate the article buffer with a draft file.
6067            nil))
6068       (gnus-article-edit-mode))
6069     (funcall start-func)
6070     (set-buffer-modified-p nil)
6071     (gnus-configure-windows 'edit-article)
6072     (setq gnus-article-edit-done-function exit-func)
6073     (setq gnus-prev-winconf winconf)
6074     (unless quiet
6075       (gnus-message 6 "C-c C-c to end edits"))))
6076
6077 (defun gnus-article-edit-done (&optional arg)
6078   "Update the article edits and exit."
6079   (interactive "P")
6080   (let ((func gnus-article-edit-done-function)
6081         (buf (current-buffer))
6082         (start (window-start))
6083         (p (point))
6084         (winconf gnus-prev-winconf))
6085     (widen) ;; Widen it in case that users narrowed the buffer.
6086     (funcall func arg)
6087     (set-buffer buf)
6088     ;; The cache and backlog have to be flushed somewhat.
6089     (when gnus-keep-backlog
6090       (gnus-backlog-remove-article
6091        (car gnus-article-current) (cdr gnus-article-current)))
6092     ;; Flush original article as well.
6093     (save-excursion
6094       (when (get-buffer gnus-original-article-buffer)
6095         (set-buffer gnus-original-article-buffer)
6096         (setq gnus-original-article nil)))
6097     (when gnus-use-cache
6098       (gnus-cache-update-article
6099        (car gnus-article-current) (cdr gnus-article-current)))
6100     ;; We remove all text props from the article buffer.
6101     (kill-all-local-variables)
6102     (set-text-properties (point-min) (point-max) nil)
6103     (gnus-article-mode)
6104     (set-window-configuration winconf)
6105     (set-buffer buf)
6106     (set-window-start (get-buffer-window buf) start)
6107     (set-window-point (get-buffer-window buf) (point)))
6108   (gnus-summary-show-article))
6109
6110 (defun gnus-article-edit-exit ()
6111   "Exit the article editing without updating."
6112   (interactive)
6113   (when (or (not (buffer-modified-p))
6114             (yes-or-no-p "Article modified; kill anyway? "))
6115     (let ((curbuf (current-buffer))
6116           (p (point))
6117           (window-start (window-start)))
6118       (erase-buffer)
6119       (if (gnus-buffer-live-p gnus-original-article-buffer)
6120           (insert-buffer-substring gnus-original-article-buffer))
6121       (let ((winconf gnus-prev-winconf))
6122         (kill-all-local-variables)
6123         (gnus-article-mode)
6124         (set-window-configuration winconf)
6125         ;; Tippy-toe some to make sure that point remains where it was.
6126         (save-current-buffer
6127           (set-buffer curbuf)
6128           (set-window-start (get-buffer-window (current-buffer)) window-start)
6129           (goto-char p))))
6130     (gnus-summary-show-article)))
6131
6132 (defun gnus-article-edit-full-stops ()
6133   "Interactively repair spacing at end of sentences."
6134   (interactive)
6135   (save-excursion
6136     (goto-char (point-min))
6137     (search-forward-regexp "^$" nil t)
6138     (let ((case-fold-search nil))
6139       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
6140
6141 ;;;
6142 ;;; Article highlights
6143 ;;;
6144
6145 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
6146
6147 ;;; Internal Variables:
6148
6149 (defcustom gnus-button-url-regexp
6150   (if (string-match "[[:digit:]]" "1") ;; support POSIX?
6151       "\\b\\(\\(www\\.\\|\\(s?https?\\|ftp\\|file\\|gopher\\|nntp\\|news\\|telnet\\|wais\\|mailto\\|info\\):\\)\\(//[-a-z0-9_.]+:[0-9]*\\)?[-a-z0-9_=!?#$@~%&*+\\/:;.,[:word:]]+[-a-z0-9_=#$@~%&*+\\/[:word:]]\\)"
6152     "\\b\\(\\(www\\.\\|\\(s?https?\\|ftp\\|file\\|gopher\\|nntp\\|news\\|telnet\\|wais\\|mailto\\|info\\):\\)\\(//[-a-z0-9_.]+:[0-9]*\\)?\\([-a-z0-9_=!?#$@~%&*+\\/:;.,]\\|\\w\\)+\\([-a-z0-9_=#$@~%&*+\\/]\\|\\w\\)\\)")
6153   "Regular expression that matches URLs."
6154   :group 'gnus-article-buttons
6155   :type 'regexp)
6156
6157 (defcustom gnus-button-valid-fqdn-regexp
6158   message-valid-fqdn-regexp
6159   "Regular expression that matches a valid FQDN."
6160   :version "22.1"
6161   :group 'gnus-article-buttons
6162   :type 'regexp)
6163
6164 ;; Regexp suggested by Felix Wiemann in <87oeuomcz9.fsf@news2.ososo.de>
6165 (defcustom gnus-button-valid-localpart-regexp
6166   "[a-z0-9$%(*-=?[_][^<>\")!;:,{}\n\t ]*"
6167   "Regular expression that matches a localpart of mail addresses or MIDs."
6168   :version "22.1"
6169   :group 'gnus-article-buttons
6170   :type 'regexp)
6171
6172 (defcustom gnus-button-man-handler 'manual-entry
6173   "Function to use for displaying man pages.
6174 The function must take at least one argument with a string naming the
6175 man page."
6176   :version "22.1"
6177   :type '(choice (function-item :tag "Man" manual-entry)
6178                  (function-item :tag "Woman" woman)
6179                  (function :tag "Other"))
6180   :group 'gnus-article-buttons)
6181
6182 (defcustom gnus-ctan-url "http://tug.ctan.org/tex-archive/"
6183   "Top directory of a CTAN \(Comprehensive TeX Archive Network\) archive.
6184 If the default site is too slow, try to find a CTAN mirror, see
6185 <URL:http://tug.ctan.org/tex-archive/CTAN.sites?action=/index.html>.  See also
6186 the variable `gnus-button-handle-ctan'."
6187   :version "22.1"
6188   :group 'gnus-article-buttons
6189   :link '(custom-manual "(gnus)Group Parameters")
6190   :type '(choice (const "http://www.tex.ac.uk/tex-archive/")
6191                  (const "http://tug.ctan.org/tex-archive/")
6192                  (const "http://www.dante.de/CTAN/")
6193                  (string :tag "Other")))
6194
6195 (defcustom gnus-button-ctan-handler 'browse-url
6196   "Function to use for displaying CTAN links.
6197 The function must take one argument, the string naming the URL."
6198   :version "22.1"
6199   :type '(choice (function-item :tag "Browse Url" browse-url)
6200                  (function :tag "Other"))
6201   :group 'gnus-article-buttons)
6202
6203 (defcustom gnus-button-handle-ctan-bogus-regexp "^/?tex-archive/\\|^/"
6204   "Bogus strings removed from CTAN URLs."
6205   :version "22.1"
6206   :group 'gnus-article-buttons
6207   :type '(choice (const "^/?tex-archive/\\|/")
6208                  (regexp :tag "Other")))
6209
6210 (defcustom gnus-button-ctan-directory-regexp
6211   (regexp-opt
6212    (list "archive-tools" "biblio" "bibliography" "digests" "documentation"
6213          "dviware" "fonts" "graphics" "help" "indexing" "info" "language"
6214          "languages" "macros" "nonfree" "obsolete" "support" "systems"
6215          "tds" "tools" "usergrps" "web") t)
6216   "Regular expression for ctan directories.
6217 It should match all directories in the top level of `gnus-ctan-url'."
6218   :version "22.1"
6219   :group 'gnus-article-buttons
6220   :type 'regexp)
6221
6222 (defcustom gnus-button-mid-or-mail-regexp
6223   (concat "\\b\\(<?" gnus-button-valid-localpart-regexp "@"
6224           gnus-button-valid-fqdn-regexp
6225           ">?\\)\\b")
6226   "Regular expression that matches a message ID or a mail address."
6227   :version "22.1"
6228   :group 'gnus-article-buttons
6229   :type 'regexp)
6230
6231 (defcustom gnus-button-prefer-mid-or-mail 'gnus-button-mid-or-mail-heuristic
6232   "What to do when the button on a string as \"foo123@bar.invalid\" is pushed.
6233 Strings like this can be either a message ID or a mail address.  If it is one
6234 of the symbols `mid' or `mail', Gnus will always assume that the string is a
6235 message ID or a mail address, respectively.  If this variable is set to the
6236 symbol `ask', always query the user what do do.  If it is a function, this
6237 function will be called with the string as it's only argument.  The function
6238 must return `mid', `mail', `invalid' or `ask'."
6239   :version "22.1"
6240   :group 'gnus-article-buttons
6241   :type '(choice (function-item :tag "Heuristic function"
6242                                 gnus-button-mid-or-mail-heuristic)
6243                  (const ask)
6244                  (const mid)
6245                  (const mail)))
6246
6247 (defcustom gnus-button-mid-or-mail-heuristic-alist
6248   '((-10.0 . ".+\\$.+@")
6249     (-10.0 . "#")
6250     (-10.0 . "\\*")
6251     (-5.0  . "\\+[^+]*\\+.*@") ;; # two plus signs
6252     (-5.0  . "@[Nn][Ee][Ww][Ss]") ;; /\@news/i
6253     (-5.0  . "@.*[Dd][Ii][Aa][Ll][Uu][Pp]") ;; /\@.*dialup/i;
6254     (-1.0  . "^[^a-z]+@")
6255     ;;
6256     (-5.0  . "\\.[0-9][0-9]+.*@") ;; "\.[0-9]{2,}.*\@"
6257     (-5.0  . "[a-z].*[A-Z].*[a-z].*[A-Z].*@") ;; "([a-z].*[A-Z].*){2,}\@"
6258     (-3.0  . "[A-Z][A-Z][a-z][a-z].*@")
6259     (-5.0  . "\\...?.?@") ;; (-5.0 . "\..{1,3}\@")
6260     ;;
6261     (-2.0  . "^[0-9]")
6262     (-1.0  . "^[0-9][0-9]")
6263     ;;
6264     ;; -3.0 /^[0-9][0-9a-fA-F]{2,2}/;
6265     (-3.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
6266     ;; -5.0 /^[0-9][0-9a-fA-F]{3,3}/;
6267     (-5.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
6268     ;;
6269     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][^0-9].*@") ;; "[0-9]{5,}.*\@"
6270     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][^0-9].*@")
6271     ;;       "[0-9]{8,}.*\@"
6272     (-3.0
6273      . "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].*@")
6274     ;; "[0-9]{12,}.*\@"
6275     ;; compensation for TDMA dated mail addresses:
6276     (25.0  . "-dated-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]+.*@")
6277     ;;
6278     (-20.0 . "\\.fsf@") ;; Gnus
6279     (-20.0 . "^slrn")
6280     (-20.0 . "^Pine")
6281     (-20.0 . "_-_") ;; Subject change in thread
6282     ;;
6283     (-20.0 . "\\.ln@") ;; leafnode
6284     (-30.0 . "@ID-[0-9]+\\.[a-zA-Z]+\\.dfncis\\.de")
6285     (-30.0 . "@4[Aa][Xx]\\.com") ;; Forte Agent
6286     ;;
6287     ;; (5.0 . "") ;; $local_part_len <= 7
6288     (10.0  . "^[^0-9]+@")
6289     (3.0   . "^[^0-9]+[0-9][0-9]?[0-9]?@")
6290     ;;      ^[^0-9]+[0-9]{1,3}\@ digits only at end of local part
6291     (3.0   . "\@stud")
6292     ;;
6293     (2.0   . "[a-z][a-z][._-][A-Z][a-z].*@")
6294     ;;
6295     (0.5   . "^[A-Z][a-z]")
6296     (0.5   . "^[A-Z][a-z][a-z]")
6297     (1.5   . "^[A-Z][a-z][A-Z][a-z][^a-z]") ;; ^[A-Z][a-z]{3,3}
6298     (2.0   . "^[A-Z][a-z][A-Z][a-z][a-z][^a-z]")) ;; ^[A-Z][a-z]{4,4}
6299   "An alist of \(RATE . REGEXP\) pairs for `gnus-button-mid-or-mail-heuristic'.
6300
6301 A negative RATE indicates a message IDs, whereas a positive indicates a mail
6302 address.  The REGEXP is processed with `case-fold-search' set to nil."
6303   :version "22.1"
6304   :group 'gnus-article-buttons
6305   :type '(repeat (cons (number :tag "Rate")
6306                        (regexp :tag "Regexp"))))
6307
6308 (defun gnus-button-mid-or-mail-heuristic (mid-or-mail)
6309   "Guess whether MID-OR-MAIL is a message ID or a mail address.
6310 Returns `mid' if MID-OR-MAIL is a message IDs, `mail' if it's a mail
6311 address, `ask' if unsure and `invalid' if the string is invalid."
6312   (let ((case-fold-search nil)
6313         (list gnus-button-mid-or-mail-heuristic-alist)
6314         (result 0) rate regexp lpartlen elem)
6315     (setq lpartlen
6316           (length (gnus-replace-in-string mid-or-mail "^\\(.*\\)@.*$" "\\1")))
6317     (gnus-message 8 "`%s', length of local part=`%s'." mid-or-mail lpartlen)
6318     ;; Certain special cases...
6319     (when (string-match
6320            (concat
6321             "^0[0-9]+-[0-9][0-9][0-9][0-9]@t-online\\.de$\\|"
6322             "^[0-9]+\\.[0-9]+@compuserve\\|"
6323             "@public\\.gmane\\.org")
6324            mid-or-mail)
6325       (gnus-message 8 "`%s' is a known mail address." mid-or-mail)
6326       (setq result 'mail))
6327     (when (string-match "@.*@\\| " mid-or-mail)
6328       (gnus-message 8 "`%s' is invalid." mid-or-mail)
6329       (setq result 'invalid))
6330     ;; Nothing more to do, if result is not a number here...
6331     (when (numberp result)
6332       (while list
6333         (setq elem (car list)
6334               rate (car elem)
6335               regexp (cdr elem)
6336               list (cdr list))
6337         (when (string-match regexp mid-or-mail)
6338           (setq result (+ result rate))
6339           (gnus-message
6340            9 "`%s' matched `%s', rate `%s', result `%s'."
6341            mid-or-mail regexp rate result)))
6342       (when (<= lpartlen 7)
6343         (setq result (+ result 5.0))
6344         (gnus-message 9 "`%s' matched (<= lpartlen 7), result `%s'."
6345                       mid-or-mail result))
6346       (when (>= lpartlen 12)
6347         (gnus-message 9 "`%s' matched (>= lpartlen 12)" mid-or-mail)
6348         (cond
6349          ((string-match "[0-9][^0-9]+[0-9].*@" mid-or-mail)
6350           ;; Long local part should contain realname if e-mail address,
6351           ;; too many digits: message-id.
6352           ;; $score -= 5.0 + 0.1 * $local_part_len;
6353           (setq rate (* -1.0 (+ 5.0 (* 0.1 lpartlen))))
6354           (setq result (+ result rate))
6355           (gnus-message
6356            9 "Many digits in `%s', rate `%s', result `%s'."
6357            mid-or-mail rate result))
6358          ((string-match "[^aeiouy][^aeiouy][^aeiouy][^aeiouy]+.*\@"
6359                         mid-or-mail)
6360           ;; Too few vowels [^aeiouy]{4,}.*\@
6361           (setq result (+ result -5.0))
6362           (gnus-message
6363            9 "Few vowels in `%s', rate `%s', result `%s'."
6364            mid-or-mail -5.0 result))
6365          (t
6366           (setq result (+ result 5.0))
6367           (gnus-message
6368            9 "`%s', rate `%s', result `%s'." mid-or-mail 5.0 result)))))
6369     (gnus-message 8 "`%s': Final rate is `%s'." mid-or-mail result)
6370     ;; Maybe we should make this a customizable alist: (condition . 'result)
6371     (cond
6372      ((symbolp result) result)
6373      ;; Now convert number into proper results:
6374      ((< result -10.0) 'mid)
6375      ((> result  10.0) 'mail)
6376      (t 'ask))))
6377
6378 (defun gnus-button-handle-mid-or-mail (mid-or-mail)
6379   (let* ((pref gnus-button-prefer-mid-or-mail) guessed
6380          (url-mid (concat "news" ":" mid-or-mail))
6381          (url-mailto (concat "mailto" ":" mid-or-mail)))
6382     (gnus-message 9 "mid-or-mail=%s" mid-or-mail)
6383     (when (fboundp pref)
6384       (setq guessed
6385             ;; get rid of surrounding angles...
6386             (funcall pref
6387                      (gnus-replace-in-string mid-or-mail "^<\\|>$" "")))
6388       (if (or (eq 'mid guessed) (eq 'mail guessed))
6389           (setq pref guessed)
6390         (setq pref 'ask)))
6391     (if (eq pref 'ask)
6392         (save-window-excursion
6393           (if (y-or-n-p (concat "Is <" mid-or-mail "> a mail address? "))
6394               (setq pref 'mail)
6395             (setq pref 'mid))))
6396     (cond ((eq pref 'mid)
6397            (gnus-message 8 "calling `gnus-button-handle-news' %s" url-mid)
6398            (gnus-button-handle-news url-mid))
6399           ((eq pref 'mail)
6400            (gnus-message 8 "calling `gnus-url-mailto'  %s" url-mailto)
6401            (gnus-url-mailto url-mailto))
6402           (t (gnus-message 3 "Invalid string.")))))
6403
6404 (defun gnus-button-handle-custom (fun arg)
6405   "Call function FUN on argument ARG.
6406 Both FUN and ARG are supposed to be strings.  ARG will be passed
6407 as a symbol to FUN."
6408   (funcall (intern fun) (intern arg)))
6409
6410 (defvar gnus-button-handle-describe-prefix "^\\(C-h\\|<?[Ff]1>?\\)")
6411
6412 ;; FIXME: Maybe we should merge some of the functions that do quite similar
6413 ;; stuff?
6414
6415 (defun gnus-button-handle-describe-function (url)
6416   "Call `describe-function' when pushing the corresponding URL button."
6417   (describe-function
6418    (intern
6419     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
6420
6421 (defun gnus-button-handle-describe-variable (url)
6422   "Call `describe-variable' when pushing the corresponding URL button."
6423   (describe-variable
6424    (intern
6425     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
6426
6427 (defun gnus-button-handle-symbol (url)
6428 "Display help on variable or function.
6429 Calls `describe-variable' or `describe-function'."
6430   (let ((sym (intern url)))
6431     (cond
6432      ((fboundp sym) (describe-function sym))
6433      ((boundp sym) (describe-variable sym))
6434      (t (gnus-message 3 "`%s' is not a known function of variable." url)))))
6435
6436 (defun gnus-button-handle-describe-key (url)
6437   "Call `describe-key' when pushing the corresponding URL button."
6438   (let* ((key-string
6439           (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))
6440          (keys (ignore-errors (eval `(kbd ,key-string)))))
6441     (if keys
6442         (describe-key keys)
6443       (gnus-message 3 "Invalid key sequence in button: %s" key-string))))
6444
6445 (defun gnus-button-handle-apropos (url)
6446   "Call `apropos' when pushing the corresponding URL button."
6447   (apropos (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
6448
6449 (defun gnus-button-handle-apropos-command (url)
6450   "Call `apropos' when pushing the corresponding URL button."
6451   (apropos-command
6452    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
6453
6454 (defun gnus-button-handle-apropos-variable (url)
6455   "Call `apropos' when pushing the corresponding URL button."
6456   (funcall
6457    (if (fboundp 'apropos-variable) 'apropos-variable 'apropos)
6458    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
6459
6460 (defun gnus-button-handle-apropos-documentation (url)
6461   "Call `apropos' when pushing the corresponding URL button."
6462   (funcall
6463    (if (fboundp 'apropos-documentation) 'apropos-documentation 'apropos)
6464    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
6465
6466 (defun gnus-button-handle-library (url)
6467   "Call `locate-library' when pushing the corresponding URL button."
6468   (gnus-message 9 "url=`%s'" url)
6469   (let* ((lib (locate-library url))
6470          (file (gnus-replace-in-string (or lib "") "\.elc" ".el")))
6471     (if (not lib)
6472         (gnus-message 1 "Cannot locale library `%s'." url)
6473       (find-file-read-only file))))
6474
6475 (defun gnus-button-handle-ctan (url)
6476   "Call `browse-url' when pushing a CTAN URL button."
6477   (funcall
6478    gnus-button-ctan-handler
6479    (concat
6480     gnus-ctan-url
6481     (gnus-replace-in-string url gnus-button-handle-ctan-bogus-regexp ""))))
6482
6483 (defcustom gnus-button-tex-level 5
6484   "*Integer that says how many TeX-related buttons Gnus will show.
6485 The higher the number, the more buttons will appear and the more false
6486 positives are possible.  Note that you can set this variable local to
6487 specific groups.  Setting it higher in TeX groups is probably a good idea.
6488 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
6489 how to set variables in specific groups."
6490   :version "22.1"
6491   :group 'gnus-article-buttons
6492   :link '(custom-manual "(gnus)Group Parameters")
6493   :type 'integer)
6494
6495 (defcustom gnus-button-man-level 5
6496   "*Integer that says how many man-related buttons Gnus will show.
6497 The higher the number, the more buttons will appear and the more false
6498 positives are possible.  Note that you can set this variable local to
6499 specific groups.  Setting it higher in Unix groups is probably a good idea.
6500 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
6501 how to set variables in specific groups."
6502   :version "22.1"
6503   :group 'gnus-article-buttons
6504   :link '(custom-manual "(gnus)Group Parameters")
6505   :type 'integer)
6506
6507 (defcustom gnus-button-emacs-level 5
6508   "*Integer that says how many emacs-related buttons Gnus will show.
6509 The higher the number, the more buttons will appear and the more false
6510 positives are possible.  Note that you can set this variable local to
6511 specific groups.  Setting it higher in Emacs or Gnus related groups is
6512 probably a good idea.  See Info node `(gnus)Group Parameters' and the variable
6513 `gnus-parameters' on how to set variables in specific groups."
6514   :version "22.1"
6515   :group 'gnus-article-buttons
6516   :link '(custom-manual "(gnus)Group Parameters")
6517   :type 'integer)
6518
6519 (defcustom gnus-button-message-level 5
6520   "*Integer that says how many buttons for news or mail messages will appear.
6521 The higher the number, the more buttons will appear and the more false
6522 positives are possible."
6523   ;; mail addresses, MIDs, URLs for news, ...
6524   :version "22.1"
6525   :group 'gnus-article-buttons
6526   :type 'integer)
6527
6528 (defcustom gnus-button-browse-level 5
6529   "*Integer that says how many buttons for browsing will appear.
6530 The higher the number, the more buttons will appear and the more false
6531 positives are possible."
6532   ;; stuff handled by `browse-url' or `gnus-button-embedded-url'
6533   :version "22.1"
6534   :group 'gnus-article-buttons
6535   :type 'integer)
6536
6537 (defcustom gnus-button-alist
6538   '(("<\\(url:[>\n\t ]*?\\)?\\(nntp\\|news\\):[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
6539      0 (>= gnus-button-message-level 0) gnus-button-handle-news 3)
6540     ((concat "\\b\\(nntp\\|news\\):\\("
6541              gnus-button-valid-localpart-regexp "@[a-z0-9.-]+[a-z]\\)")
6542      0 t gnus-button-handle-news 2)
6543     ("\\(\\b<\\(url:[>\n\t ]*\\)?\\(nntp\\|news\\):[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
6544      1 (>= gnus-button-message-level 0) gnus-button-fetch-group 5)
6545     ("\\b\\(nntp\\|news\\):\\(//\\)?\\([^'\">\n\t ]+\\)"
6546      0 (>= gnus-button-message-level 0) gnus-button-fetch-group 3)
6547     ;; RFC 2392 (Don't allow `/' in domain part --> CID)
6548     ("\\bmid:\\(//\\)?\\([^'\">\n\t ]+@[^'\">\n\t /]+\\)"
6549      0 (>= gnus-button-message-level 0) gnus-button-message-id 2)
6550     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
6551      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
6552     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>"
6553      0 (>= gnus-button-message-level 0) gnus-url-mailto 2)
6554     ;; RFC 2368 (The mailto URL scheme)
6555     ("\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
6556      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
6557     ("\\bmailto:\\([^ \n\t]+\\)"
6558      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
6559     ;; CTAN
6560     ((concat "\\bCTAN:[ \t\n]?[^>)!;:,'\n\t ]*\\("
6561              gnus-button-ctan-directory-regexp
6562              "[^][>)!;:,'\n\t ]+\\)")
6563      0 (>= gnus-button-tex-level 1) gnus-button-handle-ctan 1)
6564     ((concat "\\btex-archive/\\("
6565              gnus-button-ctan-directory-regexp
6566              "/[-_.a-z0-9/]+[-_./a-z0-9]+[/a-z0-9]\\)")
6567      1 (>= gnus-button-tex-level 6) gnus-button-handle-ctan 1)
6568     ((concat
6569       "\\b\\("
6570       gnus-button-ctan-directory-regexp
6571       "/[-_.a-z0-9]+/[-_./a-z0-9]+[/a-z0-9]\\)")
6572      1 (>= gnus-button-tex-level 8) gnus-button-handle-ctan 1)
6573     ;; This is info (home-grown style) <info://foo/bar+baz>
6574     ("\\binfo://\\([^'\">\n\t ]+\\)"
6575      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
6576     ;; Info GNOME style <info:foo#bar_baz>
6577     ("\\binfo:\\([^('\n\t\r \"><][^'\n\t\r \"><]*\\)"
6578      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-gnome 1)
6579     ;; Info KDE style <info:(foo)bar baz>
6580     ("<\\(info:\\(([^)]+)[^>\n\r]*\\)\\)>"
6581      1 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-kde 2)
6582     ("\\((Info-goto-node\\|(info\\)[ \t\n]*\\(\"[^\"]*\"\\))" 0
6583      (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 2)
6584     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+i[ \t\n]+d?[ \t\n]?m[ \t\n]+\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET"
6585      ;; Info links like `C-h i d m CC Mode RET'
6586      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-keystrokes 2)
6587     ;; This is custom
6588     ("M-x[ \t\n]\\(customize-[^ ]+\\)[ \t\n]RET[ \t\n]\\([^ ]+\\)[ \t\n]RET" 0
6589      (>= gnus-button-emacs-level 1) gnus-button-handle-custom 1 2)
6590     ;; Emacs help commands
6591     ("M-x[ \t\n]+apropos[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6592      ;; regexp doesn't match arguments containing ` '.
6593      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos 1)
6594     ("M-x[ \t\n]+apropos-command[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6595      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-command 1)
6596     ("M-x[ \t\n]+apropos-variable[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6597      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-variable 1)
6598     ("M-x[ \t\n]+apropos-documentation[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6599      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-documentation 1)
6600     ;; The following entries may lead to many false positives so don't enable
6601     ;; them by default (use a high button level).
6602     ("/\\([a-z][-a-z0-9]+\\.el\\)\\>[^.?]"
6603      ;; Exclude [.?] for URLs in gmane.emacs.cvs
6604      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
6605     ("`\\([a-z][-a-z0-9]+\\.el\\)'"
6606      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
6607     ("`\\([a-z][a-z0-9]+-[a-z]+-[-a-z]+\\|\\(gnus\\|message\\)-[-a-z]+\\)'"
6608      0 (>= gnus-button-emacs-level 8) gnus-button-handle-symbol 1)
6609     ("`\\([a-z][a-z0-9]+-[a-z]+\\)'"
6610      0 (>= gnus-button-emacs-level 9) gnus-button-handle-symbol 1)
6611     ("(setq[ \t\n]+\\([a-z][a-z0-9]+-[-a-z0-9]+\\)[ \t\n]+.+)"
6612      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-variable 1)
6613     ("\\bM-x[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6614      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-function 1)
6615     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+f[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6616      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-function 2)
6617     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+v[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6618      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-variable 2)
6619     ("`\\(\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+k[ \t\n]+\\([^']+\\)\\)'"
6620      ;; Unlike the other regexps we really have to require quoting
6621      ;; here to determine where it ends.
6622      1 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-key 3)
6623     ;; This is how URLs _should_ be embedded in text (RFC 1738, RFC 2396)...
6624     ("<URL: *\\([^<>]*\\)>"
6625      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
6626     ;; RFC 2396 (2.4.3., delims) ...
6627     ("\"URL: *\\([^\"]*\\)\""
6628      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
6629     ;; RFC 2396 (2.4.3., delims) ...
6630     ("\"URL: *\\([^\"]*\\)\""
6631      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
6632     ;; Raw URLs.
6633     (gnus-button-url-regexp
6634      0 (>= gnus-button-browse-level 0) browse-url 0)
6635     ;; man pages
6636     ("\\b\\([a-z][a-z]+([1-9])\\)\\W"
6637      0 (and (>= gnus-button-man-level 1) (< gnus-button-man-level 3))
6638      gnus-button-handle-man 1)
6639     ;; more man pages: resolv.conf(5), iso_8859-1(7), xterm(1x)
6640     ("\\b\\([a-z][-_.a-z0-9]+([1-9])\\)\\W"
6641      0 (and (>= gnus-button-man-level 3) (< gnus-button-man-level 5))
6642      gnus-button-handle-man 1)
6643     ;; even more: Apache::PerlRun(3pm), PDL::IO::FastRaw(3pm),
6644     ;; SoWWWAnchor(3iv), XSelectInput(3X11), X(1), X(7)
6645     ("\\b\\(\\(?:[a-z][-+_.:a-z0-9]+([1-9][X1a-z]*)\\)\\|\\b\\(?:X([1-9])\\)\\)\\W"
6646      0 (>= gnus-button-man-level 5) gnus-button-handle-man 1)
6647     ;; MID or mail: To avoid too many false positives we don't try to catch
6648     ;; all kind of allowed MIDs or mail addresses.  Domain part must contain
6649     ;; at least one dot.  TLD must contain two or three chars or be a know TLD
6650     ;; (info|name|...).  Put this entry near the _end_ of `gnus-button-alist'
6651     ;; so that non-ambiguous entries (see above) match first.
6652     (gnus-button-mid-or-mail-regexp
6653      0 (>= gnus-button-message-level 5) gnus-button-handle-mid-or-mail 1))
6654   "*Alist of regexps matching buttons in article bodies.
6655
6656 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
6657 REGEXP: is the string (case insensitive) matching text around the button (can
6658 also be Lisp expression evaluating to a string),
6659 BUTTON: is the number of the regexp grouping actually matching the button,
6660 FORM: is a Lisp expression which must eval to true for the button to
6661 be added,
6662 CALLBACK: is the function to call when the user push this button, and each
6663 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
6664
6665 CALLBACK can also be a variable, in that case the value of that
6666 variable it the real callback function."
6667   :group 'gnus-article-buttons
6668   :type '(repeat (list (choice regexp variable sexp)
6669                        (integer :tag "Button")
6670                        (sexp :tag "Form")
6671                        (function :tag "Callback")
6672                        (repeat :tag "Par"
6673                                :inline t
6674                                (integer :tag "Regexp group")))))
6675
6676 (defcustom gnus-header-button-alist
6677   '(("^\\(References\\|Message-I[Dd]\\|^In-Reply-To\\):" "<[^<>]+>"
6678      0 (>= gnus-button-message-level 0) gnus-button-message-id 0)
6679     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$"
6680      1 (>= gnus-button-message-level 0) gnus-button-reply 1)
6681     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
6682      0 (>= gnus-button-message-level 0) gnus-msg-mail 0)
6683     ("^X-[Uu][Rr][Ll]:" gnus-button-url-regexp
6684      0 (>= gnus-button-browse-level 0) browse-url 0)
6685     ("^Subject:" gnus-button-url-regexp
6686      0 (>= gnus-button-browse-level 0) browse-url 0)
6687     ("^[^:]+:" gnus-button-url-regexp
6688      0 (>= gnus-button-browse-level 0) browse-url 0)
6689     ("^OpenPGP:.*url=" gnus-button-url-regexp
6690      0 (>= gnus-button-browse-level 0) gnus-button-openpgp 0)
6691     ("^[^:]+:" "\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
6692      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
6693     ("^[^:]+:" "\\(<\\(url: \\)?\\(nntp\\|news\\):\\([^>\n ]*\\)>\\)"
6694      1 (>= gnus-button-message-level 0) gnus-button-message-id 4))
6695   "*Alist of headers and regexps to match buttons in article heads.
6696
6697 This alist is very similar to `gnus-button-alist', except that each
6698 alist has an additional HEADER element first in each entry:
6699
6700 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
6701
6702 HEADER is a regexp to match a header.  For a fuller explanation, see
6703 `gnus-button-alist'."
6704   :group 'gnus-article-buttons
6705   :group 'gnus-article-headers
6706   :type '(repeat (list (regexp :tag "Header")
6707                        (choice regexp variable)
6708                        (integer :tag "Button")
6709                        (sexp :tag "Form")
6710                        (function :tag "Callback")
6711                        (repeat :tag "Par"
6712                                :inline t
6713                                (integer :tag "Regexp group")))))
6714
6715 ;;; Commands:
6716
6717 (defun gnus-article-push-button (event)
6718   "Check text under the mouse pointer for a callback function.
6719 If the text under the mouse pointer has a `gnus-callback' property,
6720 call it with the value of the `gnus-data' text property."
6721   (interactive "e")
6722   (set-buffer (window-buffer (posn-window (event-start event))))
6723   (let* ((pos (posn-point (event-start event)))
6724          (data (get-text-property pos 'gnus-data))
6725          (fun (get-text-property pos 'gnus-callback)))
6726     (goto-char pos)
6727     (when fun
6728       (funcall fun data))))
6729
6730 (defun gnus-article-press-button ()
6731   "Check text at point for a callback function.
6732 If the text at point has a `gnus-callback' property,
6733 call it with the value of the `gnus-data' text property."
6734   (interactive)
6735   (let ((data (get-text-property (point) 'gnus-data))
6736         (fun (get-text-property (point) 'gnus-callback)))
6737     (when fun
6738       (funcall fun data))))
6739
6740 (defun gnus-article-highlight (&optional force)
6741   "Highlight current article.
6742 This function calls `gnus-article-highlight-headers',
6743 `gnus-article-highlight-citation',
6744 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
6745 do the highlighting.  See the documentation for those functions."
6746   (interactive (list 'force))
6747   (gnus-article-highlight-headers)
6748   (gnus-article-highlight-citation force)
6749   (gnus-article-highlight-signature)
6750   (gnus-article-add-buttons force)
6751   (gnus-article-add-buttons-to-head))
6752
6753 (defun gnus-article-highlight-some (&optional force)
6754   "Highlight current article.
6755 This function calls `gnus-article-highlight-headers',
6756 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
6757 do the highlighting.  See the documentation for those functions."
6758   (interactive (list 'force))
6759   (gnus-article-highlight-headers)
6760   (gnus-article-highlight-signature)
6761   (gnus-article-add-buttons))
6762
6763 (defun gnus-article-highlight-headers ()
6764   "Highlight article headers as specified by `gnus-header-face-alist'."
6765   (interactive)
6766   (gnus-with-article-headers
6767     (let (regexp header-face field-face from hpoints fpoints)
6768       (dolist (entry gnus-header-face-alist)
6769         (goto-char (point-min))
6770         (setq regexp (concat "^\\("
6771                              (if (string-equal "" (nth 0 entry))
6772                                  "[^\t ]"
6773                                (nth 0 entry))
6774                              "\\)")
6775               header-face (nth 1 entry)
6776               field-face (nth 2 entry))
6777         (while (and (re-search-forward regexp nil t)
6778                     (not (eobp)))
6779           (beginning-of-line)
6780           (setq from (point))
6781           (unless (search-forward ":" nil t)
6782             (forward-char 1))
6783           (when (and header-face
6784                      (not (memq (point) hpoints)))
6785             (push (point) hpoints)
6786             (gnus-put-text-property from (point) 'face header-face))
6787           (when (and field-face
6788                      (not (memq (setq from (point)) fpoints)))
6789             (push from fpoints)
6790             (if (re-search-forward "^[^ \t]" nil t)
6791                 (forward-char -2)
6792               (goto-char (point-max)))
6793             (gnus-put-text-property from (point) 'face field-face)))))))
6794
6795 (defun gnus-article-highlight-signature ()
6796   "Highlight the signature in an article.
6797 It does this by highlighting everything after
6798 `gnus-signature-separator' using the face `gnus-signature'."
6799   (interactive)
6800   (gnus-with-article-buffer
6801     (let ((inhibit-point-motion-hooks t))
6802       (save-restriction
6803         (when (and gnus-signature-face
6804                    (gnus-article-narrow-to-signature))
6805           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max))
6806                             'face gnus-signature-face)
6807           (widen)
6808           (gnus-article-search-signature)
6809           (let ((start (match-beginning 0))
6810                 (end (set-marker (make-marker) (1+ (match-end 0)))))
6811             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
6812                                      end)))))))
6813
6814 (defun gnus-button-in-region-p (b e prop)
6815   "Say whether PROP exists in the region."
6816   (text-property-not-all b e prop nil))
6817
6818 (defun gnus-article-add-buttons (&optional force)
6819   "Find external references in the article and make buttons of them.
6820 \"External references\" are things like Message-IDs and URLs, as
6821 specified by `gnus-button-alist'."
6822   (interactive (list 'force))
6823   (gnus-with-article-buffer
6824     (let ((inhibit-point-motion-hooks t)
6825           (case-fold-search t)
6826           (alist gnus-button-alist)
6827           beg entry regexp)
6828       ;; Remove all old markers.
6829       (let (marker entry new-list)
6830         (while (setq marker (pop gnus-button-marker-list))
6831           (if (or (< marker (point-min)) (>= marker (point-max)))
6832               (push marker new-list)
6833             (goto-char marker)
6834             (when (setq entry (gnus-button-entry))
6835               (put-text-property (match-beginning (nth 1 entry))
6836                                  (match-end (nth 1 entry))
6837                                  'gnus-callback nil))
6838             (set-marker marker nil)))
6839         (setq gnus-button-marker-list new-list))
6840       ;; We skip the headers.
6841       (article-goto-body)
6842       (setq beg (point))
6843       (while (setq entry (pop alist))
6844         (setq regexp (eval (car entry)))
6845         (goto-char beg)
6846         (while (re-search-forward regexp nil t)
6847           (let* ((start (and entry (match-beginning (nth 1 entry))))
6848                  (end (and entry (match-end (nth 1 entry))))
6849                  (from (match-beginning 0)))
6850             (when (and (or (eq t (nth 2 entry))
6851                            (eval (nth 2 entry)))
6852                        (not (gnus-button-in-region-p
6853                              start end 'gnus-callback)))
6854               ;; That optional form returned non-nil, so we add the
6855               ;; button.
6856               (gnus-article-add-button
6857                start end 'gnus-button-push
6858                (car (push (set-marker (make-marker) from)
6859                           gnus-button-marker-list))))))))))
6860
6861 ;; Add buttons to the head of an article.
6862 (defun gnus-article-add-buttons-to-head ()
6863   "Add buttons to the head of the article."
6864   (interactive)
6865   (gnus-with-article-headers
6866     (let (beg end)
6867       (dolist (entry gnus-header-button-alist)
6868         ;; Each alist entry.
6869         (goto-char (point-min))
6870         (while (re-search-forward (car entry) nil t)
6871           ;; Each header matching the entry.
6872           (setq beg (match-beginning 0))
6873           (setq end (or (and (re-search-forward "^[^ \t]" nil t)
6874                              (match-beginning 0))
6875                         (point-max)))
6876           (goto-char beg)
6877           (while (re-search-forward (eval (nth 1 entry)) end t)
6878             ;; Each match within a header.
6879             (let* ((entry (cdr entry))
6880                    (start (match-beginning (nth 1 entry)))
6881                    (end (match-end (nth 1 entry)))
6882                    (form (nth 2 entry)))
6883               (goto-char (match-end 0))
6884               (when (eval form)
6885                 (gnus-article-add-button
6886                  start end (nth 3 entry)
6887                  (buffer-substring (match-beginning (nth 4 entry))
6888                                    (match-end (nth 4 entry)))))))
6889           (goto-char end))))))
6890
6891 ;;; External functions:
6892
6893 (defun gnus-article-add-button (from to fun &optional data)
6894   "Create a button between FROM and TO with callback FUN and data DATA."
6895   (when gnus-article-button-face
6896     (gnus-overlay-put (gnus-make-overlay from to)
6897                       'face gnus-article-button-face))
6898   (gnus-add-text-properties
6899    from to
6900    (nconc (and gnus-article-mouse-face
6901                (list gnus-mouse-face-prop gnus-article-mouse-face))
6902           (list 'gnus-callback fun)
6903           (and data (list 'gnus-data data))))
6904   (widget-convert-button 'link from to :action 'gnus-widget-press-button
6905                          :button-keymap gnus-widget-button-keymap))
6906
6907 ;;; Internal functions:
6908
6909 (defun gnus-article-set-globals ()
6910   (with-current-buffer gnus-summary-buffer
6911     (gnus-set-global-variables)))
6912
6913 (defun gnus-signature-toggle (end)
6914   (gnus-with-article-buffer
6915     (let ((inhibit-point-motion-hooks t))
6916       (if (text-property-any end (point-max) 'article-type 'signature)
6917           (progn
6918             (gnus-delete-wash-type 'signature)
6919             (gnus-remove-text-properties-when
6920              'article-type 'signature end (point-max)
6921              (cons 'article-type (cons 'signature
6922                                        gnus-hidden-properties))))
6923         (gnus-add-wash-type 'signature)
6924         (gnus-add-text-properties-when
6925          'article-type nil end (point-max)
6926          (cons 'article-type (cons 'signature
6927                                    gnus-hidden-properties)))))
6928     (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
6929       (gnus-set-mode-line 'article))))
6930
6931 (defun gnus-button-entry ()
6932   ;; Return the first entry in `gnus-button-alist' matching this place.
6933   (let ((alist gnus-button-alist)
6934         (entry nil))
6935     (while alist
6936       (setq entry (pop alist))
6937       (if (looking-at (eval (car entry)))
6938           (setq alist nil)
6939         (setq entry nil)))
6940     entry))
6941
6942 (defun gnus-button-push (marker)
6943   ;; Push button starting at MARKER.
6944   (save-excursion
6945     (goto-char marker)
6946     (let* ((entry (gnus-button-entry))
6947            (inhibit-point-motion-hooks t)
6948            (fun (nth 3 entry))
6949            (args (mapcar (lambda (group)
6950                            (let ((string (match-string group)))
6951                              (set-text-properties
6952                               0 (length string) nil string)
6953                              string))
6954                          (nthcdr 4 entry))))
6955       (cond
6956        ((fboundp fun)
6957         (apply fun args))
6958        ((and (boundp fun)
6959              (fboundp (symbol-value fun)))
6960         (apply (symbol-value fun) args))
6961        (t
6962         (gnus-message 1 "You must define `%S' to use this button"
6963                       (cons fun args)))))))
6964
6965 (defun gnus-parse-news-url (url)
6966   (let (scheme server port group message-id articles)
6967     (with-temp-buffer
6968       (insert url)
6969       (goto-char (point-min))
6970       (when (looking-at "\\([A-Za-z]+\\):")
6971         (setq scheme (match-string 1))
6972         (goto-char (match-end 0)))
6973       (when (looking-at "//\\([^:/]+\\)\\(:?\\)\\([0-9]+\\)?/")
6974         (setq server (match-string 1))
6975         (setq port (if (stringp (match-string 3))
6976                        (string-to-number (match-string 3))
6977                      (match-string 3)))
6978         (goto-char (match-end 0)))
6979
6980       (cond
6981        ((looking-at "\\(.*@.*\\)")
6982         (setq message-id (match-string 1)))
6983        ((looking-at "\\([^/]+\\)/\\([-0-9]+\\)")
6984         (setq group (match-string 1)
6985               articles (split-string (match-string 2) "-")))
6986        ((looking-at "\\([^/]+\\)/?")
6987         (setq group (match-string 1)))
6988        (t
6989         (error "Unknown news URL syntax"))))
6990     (list scheme server port group message-id articles)))
6991
6992 (defun gnus-button-handle-news (url)
6993   "Fetch a news URL."
6994   (destructuring-bind (scheme server port group message-id articles)
6995       (gnus-parse-news-url url)
6996     (cond
6997      (message-id
6998       (save-excursion
6999         (set-buffer gnus-summary-buffer)
7000         (if server
7001             (let ((gnus-refer-article-method
7002                    (nconc (list (list 'nntp server))
7003                           gnus-refer-article-method))
7004                   (nntp-port-number (or port "nntp")))
7005               (gnus-message 7 "Fetching %s with %s"
7006                             message-id gnus-refer-article-method)
7007               (gnus-summary-refer-article message-id))
7008           (gnus-summary-refer-article message-id))))
7009      (group
7010       (gnus-button-fetch-group url)))))
7011
7012 (defun gnus-button-handle-man (url)
7013   "Fetch a man page."
7014   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
7015   (when (eq gnus-button-man-handler 'woman)
7016     (setq url (gnus-replace-in-string url "([1-9][X1a-z]*).*\\'" "")))
7017   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
7018   (funcall gnus-button-man-handler url))
7019
7020 (defun gnus-button-handle-info-url (url)
7021   "Fetch an info URL."
7022   (setq url (mm-subst-char-in-string ?+ ?\  url))
7023   (cond
7024    ((string-match "^\\([^:/]+\\)?/\\(.*\\)" url)
7025     (gnus-info-find-node
7026      (concat "(" (or (gnus-url-unhex-string (match-string 1 url))
7027                      "Gnus")
7028              ")" (gnus-url-unhex-string (match-string 2 url)))))
7029    ((string-match "([^)\"]+)[^\"]+" url)
7030     (setq url
7031           (gnus-replace-in-string
7032            (gnus-replace-in-string url "[\n\t ]+" " ") "\"" ""))
7033     (gnus-info-find-node url))
7034    (t (error "Can't parse %s" url))))
7035
7036 (defun gnus-button-handle-info-url-gnome (url)
7037   "Fetch GNOME style info URL."
7038   (setq url (mm-subst-char-in-string ?_ ?\  url))
7039   (if (string-match "\\([^#]+\\)#?\\(.*\\)" url)
7040       (gnus-info-find-node
7041        (concat "("
7042                (gnus-url-unhex-string
7043                  (match-string 1 url))
7044                ")"
7045                (or (gnus-url-unhex-string
7046                     (match-string 2 url))
7047                    "Top")))
7048     (error "Can't parse %s" url)))
7049
7050 (defun gnus-button-handle-info-url-kde (url)
7051   "Fetch KDE style info URL."
7052   (gnus-info-find-node (gnus-url-unhex-string url)))
7053
7054 (defun gnus-button-handle-info-keystrokes (url)
7055   "Call `info' when pushing the corresponding URL button."
7056   ;; For links like `C-h i d m gnus RET', `C-h i d m CC Mode RET'.
7057   (info)
7058   (Info-directory)
7059   (Info-menu url))
7060
7061 (defun gnus-button-openpgp (url)
7062   "Retrieve and add an OpenPGP key given URL from an OpenPGP header."
7063   (with-temp-buffer
7064     (mm-url-insert-file-contents-external url)
7065     (pgg-snarf-keys-region (point-min) (point-max))
7066     (pgg-display-output-buffer nil nil nil)))
7067
7068 (defun gnus-button-message-id (message-id)
7069   "Fetch MESSAGE-ID."
7070   (with-current-buffer gnus-summary-buffer
7071     (gnus-summary-refer-article message-id)))
7072
7073 (defun gnus-button-fetch-group (address)
7074   "Fetch GROUP specified by ADDRESS."
7075   (if (not (string-match "[:/]" address))
7076       ;; This is just a simple group url.
7077       (gnus-group-read-ephemeral-group address gnus-select-method)
7078     (if (not
7079          (string-match
7080           "^\\([^:/]+\\)\\(:\\([^/]+\\)\\)?/\\([^/]+\\)\\(/\\([0-9]+\\)\\)?"
7081           address))
7082         (error "Can't parse %s" address)
7083       (gnus-group-read-ephemeral-group
7084        (match-string 4 address)
7085        `(nntp ,(match-string 1 address)
7086               (nntp-address ,(match-string 1 address))
7087               (nntp-port-number ,(if (match-end 3)
7088                                      (match-string 3 address)
7089                                    "nntp")))
7090        nil nil nil
7091        (and (match-end 6) (list (string-to-number (match-string 6 address))))))))
7092
7093 (defun gnus-url-parse-query-string (query &optional downcase)
7094   (let (retval pairs cur key val)
7095     (setq pairs (split-string query "&"))
7096     (while pairs
7097       (setq cur (car pairs)
7098             pairs (cdr pairs))
7099       (if (not (string-match "=" cur))
7100           nil                           ; Grace
7101         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
7102               val (gnus-url-unhex-string (substring cur (match-end 0) nil) t))
7103         (if downcase
7104             (setq key (downcase key)))
7105         (setq cur (assoc key retval))
7106         (if cur
7107             (setcdr cur (cons val (cdr cur)))
7108           (setq retval (cons (list key val) retval)))))
7109     retval))
7110
7111 (defun gnus-url-mailto (url)
7112   ;; Send mail to someone
7113   (when (string-match "mailto:/*\\(.*\\)" url)
7114     (setq url (substring url (match-beginning 1) nil)))
7115   (let (to args subject func)
7116     (setq args (gnus-url-parse-query-string
7117                 (if (string-match "^\\?" url)
7118                     (substring url 1)
7119                   (if (string-match "^\\([^?]+\\)\\?\\(.*\\)" url)
7120                       (concat "to=" (match-string 1 url) "&"
7121                               (match-string 2 url))
7122                     (concat "to=" url)))
7123                 t)
7124           subject (cdr-safe (assoc "subject" args)))
7125     (gnus-msg-mail)
7126     (while args
7127       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
7128       (if (fboundp func)
7129           (funcall func)
7130         (message-position-on-field (caar args)))
7131       (insert (gnus-replace-in-string
7132                (mapconcat 'identity (reverse (cdar args)) ", ")
7133                "\r\n" "\n" t))
7134       (setq args (cdr args)))
7135     (if subject
7136         (message-goto-body)
7137       (message-goto-subject))))
7138
7139 (defun gnus-button-embedded-url (address)
7140   "Activate ADDRESS with `browse-url'."
7141   (browse-url (gnus-strip-whitespace address)))
7142
7143 ;;; Next/prev buttons in the article buffer.
7144
7145 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
7146 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
7147
7148 (defvar gnus-prev-page-map
7149   (let ((map (make-sparse-keymap)))
7150     (define-key map gnus-mouse-2 'gnus-button-prev-page)
7151     (define-key map "\r" 'gnus-button-prev-page)
7152     map))
7153
7154 (defvar gnus-next-page-map
7155   (let ((map (make-sparse-keymap)))
7156     (unless (>= emacs-major-version 21)
7157       ;; XEmacs doesn't care.
7158       (set-keymap-parent map gnus-article-mode-map))
7159     (define-key map gnus-mouse-2 'gnus-button-next-page)
7160     (define-key map "\r" 'gnus-button-next-page)
7161     map))
7162
7163 (defun gnus-insert-prev-page-button ()
7164   (let ((b (point))
7165         (inhibit-read-only t))
7166     (gnus-eval-format
7167      gnus-prev-page-line-format nil
7168      `(keymap ,gnus-prev-page-map
7169          gnus-prev t
7170          gnus-callback gnus-article-button-prev-page
7171          article-type annotation))
7172     (widget-convert-button
7173      'link b (if (bolp)
7174                  ;; Exclude a newline.
7175                  (1- (point))
7176                (point))
7177      :action 'gnus-button-prev-page
7178      :button-keymap gnus-prev-page-map)))
7179
7180 (defun gnus-button-next-page (&optional args more-args)
7181   "Go to the next page."
7182   (interactive)
7183   (let ((win (selected-window)))
7184     (select-window (gnus-get-buffer-window gnus-article-buffer t))
7185     (gnus-article-next-page)
7186     (select-window win)))
7187
7188 (defun gnus-button-prev-page (&optional args more-args)
7189   "Go to the prev page."
7190   (interactive)
7191   (let ((win (selected-window)))
7192     (select-window (gnus-get-buffer-window gnus-article-buffer t))
7193     (gnus-article-prev-page)
7194     (select-window win)))
7195
7196 (defun gnus-insert-next-page-button ()
7197   (let ((b (point))
7198         (inhibit-read-only t))
7199     (gnus-eval-format gnus-next-page-line-format nil
7200                       `(keymap ,gnus-next-page-map
7201                           gnus-next t
7202                           gnus-callback gnus-article-button-next-page
7203                           article-type annotation))
7204     (widget-convert-button
7205      'link b (if (bolp)
7206                  ;; Exclude a newline.
7207                  (1- (point))
7208                (point))
7209      :action 'gnus-button-next-page
7210      :button-keymap gnus-next-page-map)))
7211
7212 (defun gnus-article-button-next-page (arg)
7213   "Go to the next page."
7214   (interactive "P")
7215   (let ((win (selected-window)))
7216     (select-window (gnus-get-buffer-window gnus-article-buffer t))
7217     (gnus-article-next-page)
7218     (select-window win)))
7219
7220 (defun gnus-article-button-prev-page (arg)
7221   "Go to the prev page."
7222   (interactive "P")
7223   (let ((win (selected-window)))
7224     (select-window (gnus-get-buffer-window gnus-article-buffer t))
7225     (gnus-article-prev-page)
7226     (select-window win)))
7227
7228 (defvar gnus-decode-header-methods
7229   '(mail-decode-encoded-word-region)
7230   "List of methods used to decode headers.
7231
7232 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
7233 is FUNCTION, FUNCTION will be applied to all newsgroups.  If item is a
7234 \(REGEXP . FUNCTION), FUNCTION will be only apply to the newsgroups
7235 whose names match REGEXP.
7236
7237 For example:
7238 \((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
7239  mail-decode-encoded-word-region
7240  (\"chinese\" . rfc1843-decode-region))
7241 ")
7242
7243 (defvar gnus-decode-header-methods-cache nil)
7244
7245 (defun gnus-multi-decode-header (start end)
7246   "Apply the functions from `gnus-encoded-word-methods' that match."
7247   (unless (and gnus-decode-header-methods-cache
7248                (eq gnus-newsgroup-name
7249                    (car gnus-decode-header-methods-cache)))
7250     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
7251     (mapcar (lambda (x)
7252               (if (symbolp x)
7253                   (nconc gnus-decode-header-methods-cache (list x))
7254                 (if (and gnus-newsgroup-name
7255                          (string-match (car x) gnus-newsgroup-name))
7256                     (nconc gnus-decode-header-methods-cache
7257                            (list (cdr x))))))
7258           gnus-decode-header-methods))
7259   (let ((xlist gnus-decode-header-methods-cache))
7260     (pop xlist)
7261     (save-restriction
7262       (narrow-to-region start end)
7263       (while xlist
7264         (funcall (pop xlist) (point-min) (point-max))))))
7265
7266 ;;;
7267 ;;; Treatment top-level handling.
7268 ;;;
7269
7270 (defun gnus-treat-article (condition &optional part-number total-parts type)
7271   (let ((length (- (point-max) (point-min)))
7272         (alist gnus-treatment-function-alist)
7273         (article-goto-body-goes-to-point-min-p t)
7274         (treated-type
7275          (or (not type)
7276              (catch 'found
7277                (let ((list gnus-article-treat-types))
7278                  (while list
7279                    (when (string-match (pop list) type)
7280                      (throw 'found t)))))))
7281         (highlightp (gnus-visual-p 'article-highlight 'highlight))
7282         val elem)
7283     (gnus-run-hooks 'gnus-part-display-hook)
7284     (dolist (elem alist)
7285       (setq val
7286             (save-excursion
7287               (when (gnus-buffer-live-p gnus-summary-buffer)
7288                 (set-buffer gnus-summary-buffer))
7289               (symbol-value (car elem))))
7290       (when (and (or (consp val)
7291                      treated-type)
7292                  (gnus-treat-predicate val)
7293                  (or (not (get (car elem) 'highlight))
7294                      highlightp))
7295         (save-restriction
7296           (funcall (cadr elem)))))))
7297
7298 ;; Dynamic variables.
7299 (eval-when-compile
7300   (defvar part-number)
7301   (defvar total-parts)
7302   (defvar type)
7303   (defvar condition)
7304   (defvar length))
7305
7306 (defun gnus-treat-predicate (val)
7307   (cond
7308    ((null val)
7309     nil)
7310    (condition
7311     (eq condition val))
7312    ((and (listp val)
7313          (stringp (car val)))
7314     (apply 'gnus-or (mapcar `(lambda (s)
7315                                (string-match s ,(or gnus-newsgroup-name "")))
7316                             val)))
7317    ((listp val)
7318     (let ((pred (pop val)))
7319       (cond
7320        ((eq pred 'or)
7321         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
7322        ((eq pred 'and)
7323         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
7324        ((eq pred 'not)
7325         (not (gnus-treat-predicate (car val))))
7326        ((eq pred 'typep)
7327         (equal (car val) type))
7328        (t
7329         (error "%S is not a valid predicate" pred)))))
7330    ((eq val t)
7331     t)
7332    ((eq val 'head)
7333     nil)
7334    ((eq val 'first)
7335     (eq part-number 1))
7336    ((eq val 'last)
7337     (eq part-number total-parts))
7338    ((numberp val)
7339     (< length val))
7340    (t
7341     (error "%S is not a valid value" val))))
7342
7343 (defun gnus-article-encrypt-body (protocol &optional n)
7344   "Encrypt the article body."
7345   (interactive
7346    (list
7347     (or gnus-article-encrypt-protocol
7348         (completing-read "Encrypt protocol: "
7349                          gnus-article-encrypt-protocol-alist
7350                          nil t))
7351     current-prefix-arg))
7352   (let ((func (cdr (assoc protocol gnus-article-encrypt-protocol-alist))))
7353     (unless func
7354       (error "Can't find the encrypt protocol %s" protocol))
7355     (if (member gnus-newsgroup-name '("nndraft:delayed"
7356                                       "nndraft:drafts"
7357                                       "nndraft:queue"))
7358         (error "Can't encrypt the article in group %s"
7359                gnus-newsgroup-name))
7360     (gnus-summary-iterate n
7361       (save-excursion
7362         (set-buffer gnus-summary-buffer)
7363         (let ((mail-parse-charset gnus-newsgroup-charset)
7364               (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
7365               (summary-buffer gnus-summary-buffer)
7366               references point)
7367           (gnus-set-global-variables)
7368           (when (gnus-group-read-only-p)
7369             (error "The current newsgroup does not support article encrypt"))
7370           (gnus-summary-show-article t)
7371           (setq references
7372               (or (mail-header-references gnus-current-headers) ""))
7373           (set-buffer gnus-article-buffer)
7374           (let* ((inhibit-read-only t)
7375                  (headers
7376                   (mapcar (lambda (field)
7377                             (and (save-restriction
7378                                    (message-narrow-to-head)
7379                                    (goto-char (point-min))
7380                                    (search-forward field nil t))
7381                                  (prog2
7382                                      (message-narrow-to-field)
7383                                      (buffer-string)
7384                                    (delete-region (point-min) (point-max))
7385                                    (widen))))
7386                           '("Content-Type:" "Content-Transfer-Encoding:"
7387                             "Content-Disposition:"))))
7388             (message-narrow-to-head)
7389             (message-remove-header "MIME-Version")
7390             (goto-char (point-max))
7391             (setq point (point))
7392             (insert (apply 'concat headers))
7393             (widen)
7394             (narrow-to-region point (point-max))
7395             (let ((message-options message-options))
7396               (message-options-set 'message-sender user-mail-address)
7397               (message-options-set 'message-recipients user-mail-address)
7398               (message-options-set 'message-sign-encrypt 'not)
7399               (funcall func))
7400             (goto-char (point-min))
7401             (insert "MIME-Version: 1.0\n")
7402             (widen)
7403             (gnus-summary-edit-article-done
7404              references nil summary-buffer t))
7405           (when gnus-keep-backlog
7406             (gnus-backlog-remove-article
7407              (car gnus-article-current) (cdr gnus-article-current)))
7408           (save-excursion
7409             (when (get-buffer gnus-original-article-buffer)
7410               (set-buffer gnus-original-article-buffer)
7411               (setq gnus-original-article nil)))
7412           (when gnus-use-cache
7413             (gnus-cache-update-article
7414              (car gnus-article-current) (cdr gnus-article-current))))))))
7415
7416 (defvar gnus-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n"
7417   "The following specs can be used:
7418 %t  The security MIME type
7419 %i  Additional info
7420 %d  Details
7421 %D  Details if button is pressed")
7422
7423 (defvar gnus-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n"
7424   "The following specs can be used:
7425 %t  The security MIME type
7426 %i  Additional info
7427 %d  Details
7428 %D  Details if button is pressed")
7429
7430 (defvar gnus-mime-security-button-line-format-alist
7431   '((?t gnus-tmp-type ?s)
7432     (?i gnus-tmp-info ?s)
7433     (?d gnus-tmp-details ?s)
7434     (?D gnus-tmp-pressed-details ?s)))
7435
7436 (defvar gnus-mime-security-button-commands
7437   '((gnus-article-press-button "\r" "Show Detail")
7438     (undefined "v")
7439     (undefined "t")
7440     (undefined "C")
7441     (gnus-mime-security-save-part "o" "Save...")
7442     (undefined "\C-o")
7443     (undefined "r")
7444     (undefined "d")
7445     (undefined "c")
7446     (undefined "i")
7447     (undefined "E")
7448     (undefined "e")
7449     (undefined "p")
7450     (gnus-mime-security-pipe-part "|" "Pipe To Command...")
7451     (undefined ".")))
7452
7453 (defvar gnus-mime-security-button-map
7454   (let ((map (make-sparse-keymap)))
7455     (define-key map gnus-mouse-2 'gnus-article-push-button)
7456     (define-key map gnus-down-mouse-3 'gnus-mime-security-button-menu)
7457     (dolist (c gnus-mime-security-button-commands)
7458       (define-key map (cadr c) (car c)))
7459     map))
7460
7461 (easy-menu-define
7462   gnus-mime-security-button-menu gnus-mime-security-button-map
7463   "Security button menu."
7464   `("Security Part"
7465     ,@(delq nil
7466             (mapcar (lambda (c)
7467                       (unless (eq (car c) 'undefined)
7468                         (vector (caddr c) (car c) :enable t)))
7469                     gnus-mime-security-button-commands))))
7470
7471 (defun gnus-mime-security-button-menu (event prefix)
7472   "Construct a context-sensitive menu of security commands."
7473   (interactive "e\nP")
7474   (save-window-excursion
7475     (let ((pos (event-start event)))
7476       (select-window (posn-window pos))
7477       (goto-char (posn-point pos))
7478       (gnus-article-check-buffer)
7479       (popup-menu gnus-mime-security-button-menu nil prefix))))
7480
7481 (defvar gnus-mime-security-details-buffer nil)
7482
7483 (defvar gnus-mime-security-button-pressed nil)
7484
7485 (defvar gnus-mime-security-show-details-inline t
7486   "If non-nil, show details in the article buffer.")
7487
7488 (defun gnus-mime-security-verify-or-decrypt (handle)
7489   (mm-remove-parts (cdr handle))
7490   (let ((region (mm-handle-multipart-ctl-parameter handle 'gnus-region))
7491         point (inhibit-read-only t))
7492     (if region
7493         (goto-char (car region)))
7494     (save-restriction
7495       (narrow-to-region (point) (point))
7496       (with-current-buffer (mm-handle-multipart-original-buffer handle)
7497         (let* ((mm-verify-option 'known)
7498                (mm-decrypt-option 'known)
7499                (nparts (mm-possibly-verify-or-decrypt (cdr handle) handle)))
7500           (unless (eq nparts (cdr handle))
7501             (mm-destroy-parts (cdr handle))
7502             (setcdr handle nparts))))
7503       (setq point (point))
7504       (gnus-mime-display-security handle)
7505       (goto-char (point-max)))
7506     (when region
7507       (delete-region (point) (cdr region))
7508       (set-marker (car region) nil)
7509       (set-marker (cdr region) nil))
7510     (goto-char point)))
7511
7512 (defun gnus-mime-security-show-details (handle)
7513   (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
7514     (if (not details)
7515         (gnus-message 5 "No details.")
7516       (if gnus-mime-security-show-details-inline
7517           (let ((gnus-mime-security-button-pressed
7518                  (not (get-text-property (point) 'gnus-mime-details)))
7519                 (gnus-mime-security-button-line-format
7520                  (get-text-property (point) 'gnus-line-format))
7521                 (inhibit-read-only t))
7522             (forward-char -1)
7523             (while (eq (get-text-property (point) 'gnus-line-format)
7524                        gnus-mime-security-button-line-format)
7525               (forward-char -1))
7526             (forward-char)
7527             (save-restriction
7528               (narrow-to-region (point) (point))
7529               (gnus-insert-mime-security-button handle))
7530             (delete-region (point)
7531                            (or (text-property-not-all
7532                                 (point) (point-max)
7533                                 'gnus-line-format
7534                                 gnus-mime-security-button-line-format)
7535                                (point-max))))
7536         ;; Not inlined.
7537         (if (gnus-buffer-live-p gnus-mime-security-details-buffer)
7538             (with-current-buffer gnus-mime-security-details-buffer
7539               (erase-buffer)
7540               t)
7541           (setq gnus-mime-security-details-buffer
7542                 (gnus-get-buffer-create "*MIME Security Details*")))
7543         (with-current-buffer gnus-mime-security-details-buffer
7544           (insert details)
7545           (goto-char (point-min)))
7546         (pop-to-buffer gnus-mime-security-details-buffer)))))
7547
7548 (defun gnus-mime-security-press-button (handle)
7549   (save-excursion
7550     (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
7551         (gnus-mime-security-show-details handle)
7552       (gnus-mime-security-verify-or-decrypt handle))))
7553
7554 (defun gnus-insert-mime-security-button (handle &optional displayed)
7555   (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
7556          (gnus-tmp-type
7557           (concat
7558            (or (nth 2 (assoc protocol mm-verify-function-alist))
7559                (nth 2 (assoc protocol mm-decrypt-function-alist))
7560                "Unknown")
7561            (if (equal (car handle) "multipart/signed")
7562                " Signed" " Encrypted")
7563            " Part"))
7564          (gnus-tmp-info
7565           (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
7566               "Undecided"))
7567          (gnus-tmp-details
7568           (mm-handle-multipart-ctl-parameter handle 'gnus-details))
7569          gnus-tmp-pressed-details
7570          b e)
7571     (setq gnus-tmp-details
7572           (if gnus-tmp-details
7573               (concat "\n" gnus-tmp-details)
7574             ""))
7575     (setq gnus-tmp-pressed-details
7576           (if gnus-mime-security-button-pressed gnus-tmp-details ""))
7577     (unless (bolp)
7578       (insert "\n"))
7579     (setq b (point))
7580     (gnus-eval-format
7581      gnus-mime-security-button-line-format
7582      gnus-mime-security-button-line-format-alist
7583      `(keymap ,gnus-mime-security-button-map
7584          gnus-callback gnus-mime-security-press-button
7585          gnus-line-format ,gnus-mime-security-button-line-format
7586          gnus-mime-details ,gnus-mime-security-button-pressed
7587          article-type annotation
7588          gnus-data ,handle))
7589     (setq e (if (bolp)
7590                 ;; Exclude a newline.
7591                 (1- (point))
7592               (point)))
7593     (widget-convert-button
7594      'link b e
7595      :mime-handle handle
7596      :action 'gnus-widget-press-button
7597      :button-keymap gnus-mime-security-button-map
7598      :help-echo
7599      (lambda (widget/window &optional overlay pos)
7600        ;; Needed to properly clear the message due to a bug in
7601        ;; wid-edit (XEmacs only).
7602        (when (boundp 'help-echo-owns-message)
7603          (setq help-echo-owns-message t))
7604        (format
7605         "%S: show detail; %S: more options"
7606         (aref gnus-mouse-2 0)
7607         (aref gnus-down-mouse-3 0))))))
7608
7609 (defun gnus-mime-display-security (handle)
7610   (save-restriction
7611     (narrow-to-region (point) (point))
7612     (unless (gnus-unbuttonized-mime-type-p (car handle))
7613       (gnus-insert-mime-security-button handle))
7614     (gnus-mime-display-part (cadr handle))
7615     (unless (bolp)
7616       (insert "\n"))
7617     (unless (gnus-unbuttonized-mime-type-p (car handle))
7618       (let ((gnus-mime-security-button-line-format
7619              gnus-mime-security-button-end-line-format))
7620         (gnus-insert-mime-security-button handle)))
7621     (mm-set-handle-multipart-parameter
7622      handle 'gnus-region
7623      (cons (set-marker (make-marker) (point-min))
7624            (set-marker (make-marker) (point-max))))))
7625
7626 (defun gnus-mime-security-run-function (function)
7627   "Run FUNCTION with the security part under point."
7628   (gnus-article-check-buffer)
7629   (let ((data (get-text-property (point) 'gnus-data))
7630         buffer handle)
7631     (when (and (stringp (car-safe data))
7632                (setq buffer (mm-handle-multipart-original-buffer data))
7633                (setq handle (cadr data)))
7634       (if (bufferp (mm-handle-buffer handle))
7635           (progn
7636             (setq handle (cons buffer (copy-sequence (cdr handle))))
7637             (mm-handle-set-undisplayer handle nil))
7638         (setq handle (mm-make-handle
7639                       buffer
7640                       (mm-handle-multipart-ctl-parameter handle 'protocol)
7641                       nil nil nil nil nil nil)))
7642       (funcall function handle))))
7643
7644 (defun gnus-mime-security-save-part ()
7645   "Save the security part under point."
7646   (interactive)
7647   (gnus-mime-security-run-function 'mm-save-part))
7648
7649 (defun gnus-mime-security-pipe-part ()
7650   "Pipe the security part under point to a process."
7651   (interactive)
7652   (gnus-mime-security-run-function 'mm-pipe-part))
7653
7654 (gnus-ems-redefine)
7655
7656 (provide 'gnus-art)
7657
7658 (run-hooks 'gnus-art-load-hook)
7659
7660 ;;; arch-tag: 2654516f-6279-48f9-a83b-05c1fa450c33
7661 ;;; gnus-art.el ends here