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