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