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