home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / packages / rnews.el < prev    next >
Encoding:
Text File  |  1995-07-28  |  34.9 KB  |  980 lines

  1. ;;; rnews.el --- USENET news reader for gnu emacs
  2. ;; Keywords: news
  3.  
  4. ;; Copyright (C) 1985, 1986, 1987 Free Software Foundation, Inc.
  5.  
  6. ;; This file is part of XEmacs.
  7.  
  8. ;; XEmacs is free software; you can redistribute it and/or modify it
  9. ;; under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation; either version 2, or (at your option)
  11. ;; any later version.
  12.  
  13. ;; XEmacs is distributed in the hope that it will be useful, but
  14. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. ;; General Public License for more details.
  17.  
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  20. ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22. ;; Created Sun Mar 10,1985 at 21:35:01 ads and sundar@hernes.ai.mit.edu
  23. ;; Should do the point pdl stuff sometime
  24. ;; finito except pdl.... Sat Mar 16,1985 at 06:43:44
  25. ;; lets keep the summary stuff out until we get it working ..
  26. ;;    sundar@hermes.ai.mit.edu Wed Apr 10,1985 at 16:32:06
  27. ;; hack slash maim. mly@prep.ai.mit.edu Thu 18 Apr, 1985 06:11:14
  28. ;; modified to correct reentrance bug, to not bother with groups that
  29. ;;   received no new traffic since last read completely, to find out
  30. ;;   what traffic a group has available much more quickly when
  31. ;;   possible, to do some completing reads for group names - should
  32. ;;   be much faster...
  33. ;;    KING@KESTREL.arpa, Thu Mar 13 09:03:28 1986
  34. ;; made news-{next,previous}-group skip groups with no new messages; and
  35. ;; added checking for unsubscribed groups to news-add-news-group
  36. ;;    tower@prep.ai.mit.edu Jul 18 1986
  37. ;; bound rmail-output to C-o; and changed header-field commands binding to
  38. ;; agree with the new C-c C-f usage in sendmail
  39. ;;     tower@prep Sep  3 1986
  40. ;; added news-rotate-buffer-body
  41. ;;    tower@prep Oct 17 1986
  42. ;; made messages more user friendly, cleanuped news-inews
  43. ;; move posting and mail code to new file rnewpost.el
  44. ;;    tower@prep Oct 29 1986
  45. ;; added caesar-region, rename news-caesar-buffer-body, hacked accordingly
  46. ;;    tower@prep Nov 21 1986
  47. ;; added (provide 'rnews)    tower@prep 22 Apr 87
  48. (provide 'rnews)
  49. (require 'mail-utils)
  50.  
  51. (autoload 'rmail-output "rmailout"
  52.   "Append this message to Unix mail file named FILE-NAME."
  53.   t)
  54.  
  55. (autoload 'news-reply "rnewspost"
  56.   "Compose and post a reply to the current article on USENET.
  57. While composing the reply, use \\[mail-yank-original] to yank the original
  58. message into it."
  59.   t)
  60.  
  61. (autoload 'news-mail-other-window "rnewspost"
  62.   "Send mail in another window.
  63. While composing the message, use \\[mail-yank-original] to yank the
  64. original message into it."
  65.   t)
  66.  
  67. (autoload 'news-post-news "rnewspost"
  68.   "Begin editing a new USENET news article to be posted."
  69.   t)
  70.  
  71. (autoload 'news-mail-reply "rnewspost"
  72.   "Mail a reply to the author of the current article.
  73. While composing the reply, use \\[mail-yank-original] to yank the original
  74. message into it."
  75.   t)
  76.  
  77. (defvar news-group-hook-alist nil
  78.   "Alist of (GROUP-REGEXP . HOOK) pairs.
  79. Just before displaying a message, each HOOK is called
  80. if its GROUP-REGEXP matches the current newsgroup name.")
  81.  
  82. (defvar rmail-last-file (expand-file-name "~/mbox.news"))
  83.  
  84. ;Now in paths.el.
  85. ;(defvar news-path "/usr/spool/news/"
  86. ;  "The root directory below which all news files are stored.")
  87.  
  88. (defvar news-startup-file "$HOME/.newsrc" "Contains ~/.newsrc")
  89. (defvar news-certification-file "$HOME/.news-dates" "Contains ~/.news-dates")
  90.  
  91. ;; random headers that we decide to ignore.
  92. (defvar news-ignored-headers
  93.   "^Path:\\|^Posting-Version:\\|^Article-I.D.:\\|^Expires:\\|^Date-Received:\\|^References:\\|^Control:\\|^Xref:\\|^Lines:\\|^Posted:\\|^Relay-Version:\\|^Message-ID:\\|^Nf-ID:\\|^Nf-From:\\|^Approved:\\|^Sender:"
  94.   "All random fields within the header of a message.")
  95.  
  96. (defvar news-mode-map nil)
  97. (defvar news-read-first-time-p t)
  98. ;; Contains the (dotified) news groups of which you are a member. 
  99. (defvar news-user-group-list nil)
  100.  
  101. (defvar news-current-news-group nil)
  102. (defvar news-current-group-begin nil)
  103. (defvar news-current-group-end  nil)
  104. (defvar news-current-certifications nil
  105.        "An assoc list of a group name and the time at which it is
  106. known that the group had no new traffic")
  107. (defvar news-current-certifiable nil
  108.     "The time when the directory we are now working on was written")
  109.  
  110. (defvar news-message-filter nil
  111.   "User specifiable filter function that will be called during
  112. formatting of the news file")
  113.  
  114. ;(defvar news-mode-group-string "Starting-Up"
  115. ;  "Mode line group name info is held in this variable")
  116. (defvar news-list-of-files nil
  117.   "Global variable in which we store the list of files
  118. associated with the current newsgroup")
  119. (defvar news-list-of-files-possibly-bogus nil
  120.   "variable indicating we only are guessing at which files are available.
  121. Not currently used.")
  122.  
  123. ;; association list in which we store lists of the form
  124. ;; (pointified-group-name (first last old-last))
  125. (defvar news-group-article-assoc nil)
  126.   
  127. (defvar news-current-message-number 0 "Displayed Article Number")
  128. (defvar news-total-current-group 0 "Total no of messages in group")
  129.  
  130. (defvar news-unsubscribe-groups ())
  131. (defvar news-point-pdl () "List of visited news messages.")
  132. (defvar news-no-jumps-p t)
  133. (defvar news-buffer () "Buffer into which news files are read.")
  134.  
  135. (defmacro news-push (item ref)
  136.   (list 'setq ref (list 'cons item ref)))
  137.  
  138. (defmacro news-cadr (x) (list 'car (list 'cdr x)))
  139. (defmacro news-cdar (x) (list 'cdr (list 'car x)))
  140. (defmacro news-caddr (x) (list 'car (list 'cdr (list 'cdr x))))
  141. (defmacro news-cadar (x) (list 'car (list 'cdr (list 'car x))))
  142. (defmacro news-caadr (x) (list 'car (list 'car (list 'cdr x))))
  143. (defmacro news-cdadr (x) (list 'cdr (list 'car (list 'cdr x))))
  144.  
  145. (defmacro news-wins (pfx index)
  146.   (` (file-exists-p (concat (, pfx) "/" (int-to-string (, index))))))
  147.  
  148. (defvar news-max-plausible-gap 2
  149.     "* In an rnews directory, the maximum possible gap size.
  150. A gap is a sequence of missing messages between two messages that exist.
  151. An empty file does not contribute to a gap -- it ends one.")
  152.  
  153. (defun news-find-first-and-last (prefix base)
  154.   (and (news-wins prefix base)
  155.        (cons (news-find-first-or-last prefix base -1)
  156.          (news-find-first-or-last prefix base 1))))
  157.  
  158. (defmacro news-/ (a1 a2)
  159. ;; a form of / that guarantees that (/ -1 2) = 0
  160.   (if (zerop (/ -1 2))
  161.       (` (/ (, a1) (, a2)))
  162.     (` (if (< (, a1) 0)
  163.        (- (/ (- (, a1)) (, a2)))
  164.      (/ (, a1) (, a2))))))
  165.  
  166. (defun news-find-first-or-last (pfx base dirn)
  167.   ;; first use powers of two to find a plausible ceiling
  168.   (let ((original-dir dirn))
  169.     (while (news-wins pfx (+ base dirn))
  170.       (setq dirn (* dirn 2)))
  171.     (setq dirn (news-/ dirn 2))
  172.     ;; Then use a binary search to find the high water mark
  173.     (let ((offset (news-/ dirn 2)))
  174.       (while (/= offset 0)
  175.     (if (news-wins pfx (+ base dirn offset))
  176.         (setq dirn (+ dirn offset)))
  177.     (setq offset (news-/ offset 2))))
  178.     ;; If this high-water mark is bogus, recurse.
  179.     (let ((offset (* news-max-plausible-gap original-dir)))
  180.       (while (and (/= offset 0) (not (news-wins pfx (+ base dirn offset))))
  181.     (setq offset (- offset original-dir)))
  182.       (if (= offset 0)
  183.       (+ base dirn)
  184.     (news-find-first-or-last pfx (+ base dirn offset) original-dir)))))
  185.  
  186. (defun rnews ()
  187. "Read USENET news for groups for which you are a member and add or
  188. delete groups.
  189. You can reply to articles posted and send articles to any group.
  190.  
  191. Type \\[describe-mode] once reading news to get a list of rnews commands."
  192.   (interactive)
  193.   (let ((last-buffer (buffer-name)))
  194.     (make-local-variable 'rmail-last-file)
  195.     (switch-to-buffer (setq news-buffer (get-buffer-create "*news*")))
  196.     (news-mode)
  197.     (setq news-buffer-save last-buffer)
  198.     (setq buffer-read-only nil)
  199.     (erase-buffer)
  200.     (setq buffer-read-only t)
  201.     (set-buffer-modified-p t)
  202.     (sit-for 0)
  203.     (message "Getting new USENET news...")
  204.     (news-set-mode-line)
  205.     (news-get-certifications)
  206.     (news-get-new-news)))
  207.  
  208. (defun news-group-certification (group)
  209.   (cdr-safe (assoc group news-current-certifications)))
  210.  
  211.  
  212. (defun news-set-current-certifiable ()
  213.   ;; Record the date that corresponds to the directory you are about to check
  214.   (let ((file (concat news-path
  215.               (string-subst-char ?/ ?. news-current-news-group))))
  216.     (setq news-current-certifiable
  217.       (nth 5 (file-attributes
  218.           (or (file-symlink-p file) file))))))
  219.  
  220. (defun news-get-certifications ()
  221.   ;; Read the certified-read file from last session
  222.   (save-excursion
  223.     (save-window-excursion
  224.       (setq news-current-certifications
  225.         (car-safe
  226.          (condition-case var
  227.          (let*
  228.              ((file (substitute-in-file-name news-certification-file))
  229.               (buf (find-file-noselect file)))
  230.            (and (file-exists-p file)
  231.             (progn
  232.               (switch-to-buffer buf 'norecord)
  233.               (unwind-protect
  234.                   (read-from-string (buffer-string))
  235.                 (kill-buffer buf)))))
  236.            (error nil)))))))
  237.  
  238. (defun news-write-certifications ()
  239.   ;; Write a certification file.
  240.   ;; This is an assoc list of group names with doubletons that represent
  241.   ;; mod times of the directory when group is read completely.
  242.   (save-excursion
  243.     (save-window-excursion
  244.       (with-output-to-temp-buffer
  245.       "*CeRtIfIcAtIoNs*"
  246.       (print news-current-certifications))
  247.       (let ((buf (get-buffer "*CeRtIfIcAtIoNs*")))
  248.     (switch-to-buffer buf)
  249.     (write-file (substitute-in-file-name news-certification-file))
  250.     (kill-buffer buf)))))
  251.  
  252. (defun news-set-current-group-certification ()
  253.   (let ((cgc (assoc news-current-news-group news-current-certifications)))
  254.     (if cgc (setcdr cgc news-current-certifiable)
  255.       (news-push (cons news-current-news-group news-current-certifiable)
  256.          news-current-certifications))))
  257.  
  258. (defun news-set-minor-modes ()
  259.   "Creates a minor mode list that has group name, total articles,
  260. and attribute for current article."
  261.   (setq news-minor-modes (list (cons 'foo
  262.                      (concat news-current-message-number
  263.                          "/"
  264.                          news-total-current-group
  265.                          (news-get-attribute-string)))))
  266.   ;; Detect Emacs versions 18.16 and up, which display
  267.   ;; directly from news-minor-modes by using a list for mode-name.
  268.   (or (boundp 'minor-mode-alist)
  269.       (setq minor-modes news-minor-modes)))
  270.  
  271. (defun news-set-message-counters ()
  272.   "Scan through current news-groups filelist to figure out how many messages
  273. are there. Set counters for use with minor mode display."
  274.     (if (null news-list-of-files)
  275.     (setq news-current-message-number 0)))
  276.  
  277. (if news-mode-map
  278.     nil
  279.   (setq news-mode-map (make-keymap))
  280.   (suppress-keymap news-mode-map)
  281.   (define-key news-mode-map "." 'beginning-of-buffer)
  282.   (define-key news-mode-map " " 'scroll-up)
  283.   (define-key news-mode-map "\177" 'scroll-down)
  284.   (define-key news-mode-map "n" 'news-next-message)
  285.   (define-key news-mode-map "c" 'news-make-link-to-message)
  286.   (define-key news-mode-map "p" 'news-previous-message)
  287.   (define-key news-mode-map "j" 'news-goto-message)
  288.   (define-key news-mode-map "q" 'news-exit)
  289.   (define-key news-mode-map "e" 'news-exit)
  290.   (define-key news-mode-map "\ej" 'news-goto-news-group)
  291.   (define-key news-mode-map "\en" 'news-next-group)
  292.   (define-key news-mode-map "\ep" 'news-previous-group)
  293.   (define-key news-mode-map "l" 'news-list-news-groups)
  294.   (define-key news-mode-map "?" 'describe-mode)
  295.   (define-key news-mode-map "g" 'news-get-new-news)
  296.   (define-key news-mode-map "f" 'news-reply)
  297.   (define-key news-mode-map "m" 'news-mail-other-window)
  298.   (define-key news-mode-map "a" 'news-post-news)
  299.   (define-key news-mode-map "r" 'news-mail-reply)
  300.   (define-key news-mode-map "o" 'news-save-item-in-file)
  301.   (define-key news-mode-map "\C-o" 'rmail-output)
  302.   (define-key news-mode-map "t" 'news-show-all-headers)
  303.   (define-key news-mode-map "x" 'news-force-update)
  304.   (define-key news-mode-map "A" 'news-add-news-group)
  305.   (define-key news-mode-map "u" 'news-unsubscribe-current-group)
  306.   (define-key news-mode-map "U" 'news-unsubscribe-group)
  307.   (define-key news-mode-map "\C-c\C-r" 'news-caesar-buffer-body))
  308.  
  309. (defun news-mode ()
  310.   "News Mode is used by M-x rnews for reading USENET Newsgroups articles.
  311. New readers can find additional help in newsgroup: news.announce.newusers .
  312. All normal editing commands are turned off.
  313. Instead, these commands are available:
  314.  
  315. .    move point to front of this news article (same as Meta-<).
  316. Space    scroll to next screen of this news article.
  317. Delete  scroll down previous page of this news article.
  318. n    move to next news article, possibly next group.
  319. p    move to previous news article, possibly previous group.
  320. j    jump to news article specified by numeric position.
  321. M-j     jump to news group.
  322. M-n     goto next news group.
  323. M-p     goto previous news group.
  324. l       list all the news groups with current status.
  325. ?       print this help message.
  326. C-c C-r caesar rotate all letters by 13 places in the article's body (rot13).
  327. g       get new USENET news.
  328. f       post a reply article to USENET.
  329. a       post an original news article.
  330. A       add a newsgroup. 
  331. o    save the current article in the named file (append if file exists).
  332. C-o    output this message to a Unix-format mail file (append it).
  333. c       \"copy\" (actually link) current or prefix-arg msg to file.
  334.     warning: target directory and message file must be on same device
  335.         (UNIX magic)
  336. t       show all the headers this news article originally had.
  337. q    quit reading news after updating .newsrc file.
  338. e    exit updating .newsrc file.
  339. m    mail a news article.  Same as C-x 4 m.
  340. x       update last message seen to be the current message.
  341. r    mail a reply to this news article.  Like m but initializes some fields.
  342. u       unsubscribe from current newsgroup.
  343. U       unsubscribe from specified newsgroup."
  344.   (interactive)
  345.   (kill-all-local-variables)
  346.   (make-local-variable 'news-read-first-time-p)
  347.   (setq news-read-first-time-p t)
  348.   (make-local-variable 'news-current-news-group)
  349. ;  (setq news-current-news-group "??")
  350.   (make-local-variable 'news-current-group-begin)
  351.   (setq news-current-group-begin 0)
  352.   (make-local-variable 'news-current-message-number)
  353.   (setq news-current-message-number 0)
  354.   (make-local-variable 'news-total-current-group)
  355.   (make-local-variable 'news-buffer-save)
  356.   (make-local-variable 'version-control)
  357.   (setq version-control 'never)
  358.   (make-local-variable 'news-point-pdl)
  359. ;  This breaks it.  I don't have time to figure out why. -- RMS
  360. ;  (make-local-variable 'news-group-article-assoc)
  361.   (setq major-mode 'news-mode)
  362.   (setq modeline-process '(news-minor-modes))
  363.   (setq mode-name "NEWS")
  364.   (news-set-mode-line)
  365.   (set-syntax-table text-mode-syntax-table)
  366.   (use-local-map news-mode-map)
  367.   (setq local-abbrev-table text-mode-abbrev-table)
  368.   (run-hooks 'news-mode-hook))
  369.  
  370. (defun string-subst-char (new old string)
  371.   (let (index)
  372.     (setq old (regexp-quote (char-to-string old))
  373.       string (substring string 0))
  374.     (while (setq index (string-match old string))
  375.       (aset string index new)))
  376.   string)
  377.  
  378. ;; update read message number
  379. (defmacro news-update-message-read (ngroup nno)
  380.   (list 'setcar
  381.     (list 'news-cdadr
  382.           (list 'assoc ngroup 'news-group-article-assoc))
  383.     nno))
  384.  
  385. (defun news-parse-range (number-string)
  386.   "Parse string representing range of numbers of he form <a>-<b>
  387. to a list (a . b)"
  388.   (let ((n (string-match "-" number-string)))
  389.     (if n
  390.     (cons (string-to-int (substring number-string 0 n))
  391.           (string-to-int (substring number-string (1+ n))))
  392.       (setq n (string-to-int number-string))
  393.       (cons n n))))
  394.  
  395. ;(defun is-in (elt lis)
  396. ;  (catch 'foo
  397. ;    (while lis
  398. ;      (if (equal (car lis) elt)
  399. ;      (throw 'foo t)
  400. ;    (setq lis (cdr lis))))))
  401.  
  402. (defun news-get-new-news ()
  403.   "Get new USENET news, if there is any for the current user."
  404.   (interactive)
  405.   (if (not (null news-user-group-list))
  406.       (news-update-newsrc-file))
  407.   (setq news-group-article-assoc ())
  408.   (setq news-user-group-list ())
  409.   (message "Looking up %s file..." news-startup-file)
  410.   (let ((file (substitute-in-file-name news-startup-file))
  411.     (temp-user-groups ()))
  412.     (save-excursion
  413.       (let ((newsrcbuf (find-file-noselect file))
  414.         start end endofline tem)
  415.     (set-buffer newsrcbuf)
  416.     (goto-char 0)
  417.     (while (search-forward ": " nil t)
  418.       (setq end (point))
  419.       (beginning-of-line)
  420.       (setq start (point))
  421.       (end-of-line)
  422.       (setq endofline (point))
  423.       (setq tem (buffer-substring start (- end 2)))
  424.       (let ((range (news-parse-range
  425.             (buffer-substring end endofline))))
  426.         (if (assoc tem news-group-article-assoc)
  427.         (message "You are subscribed twice to %s; I ignore second"
  428.              tem)          
  429.           (setq temp-user-groups (cons tem temp-user-groups)
  430.             news-group-article-assoc
  431.             (cons (list tem (list (car range)
  432.                       (cdr range)
  433.                       (cdr range)))
  434.               news-group-article-assoc)))))
  435.     (kill-buffer newsrcbuf)))      
  436.     (setq temp-user-groups (nreverse temp-user-groups))
  437.     (message "Prefrobnicating...")
  438.     (switch-to-buffer news-buffer)
  439.     (setq news-user-group-list temp-user-groups)
  440.     (while (and temp-user-groups
  441.         (not (news-read-files-into-buffer
  442.               (car temp-user-groups) nil)))
  443.       (setq temp-user-groups (cdr temp-user-groups)))
  444.     (if (null temp-user-groups)
  445.     (message "No news is good news.")
  446.       (message ""))))
  447.  
  448. (defun news-list-news-groups ()
  449.   "Display all the news groups to which you belong."
  450.   (interactive)
  451.   (with-output-to-temp-buffer "*Newsgroups*"
  452.     (save-excursion
  453.       (set-buffer standard-output)
  454.       (insert
  455.     "News Group        Msg No.       News Group        Msg No.\n")
  456.       (insert
  457.     "-------------------------       -------------------------\n")
  458.       (let ((temp news-user-group-list)
  459.         (flag nil))
  460.     (while temp
  461.       (let ((item (assoc (car temp) news-group-article-assoc)))
  462.         (insert (car item))
  463.         (indent-to (if flag 52 20))
  464.         (insert (int-to-string (news-cadr (news-cadr item))))
  465.         (if flag
  466.         (insert "\n")
  467.           (indent-to 33))
  468.         (setq temp (cdr temp) flag (not flag))))))))
  469.  
  470. ;; Mode line hack
  471. (defun news-set-mode-line ()
  472.   "Set mode line string to something useful."
  473.   (setq mode-line-process
  474.     (concat " "
  475.         (if (integerp news-current-message-number)
  476.             (int-to-string news-current-message-number)
  477.          "??")
  478.         "/"
  479.         (if (integerp news-current-group-end)
  480.             (int-to-string news-current-group-end)
  481.           news-current-group-end)))
  482.   (setq mode-line-buffer-identification
  483.     (concat "NEWS: "
  484.         news-current-news-group
  485.         ;; Enough spaces to pad group name to 17 positions.
  486.         (substring "                 "
  487.                0 (max 0 (- 17 (length news-current-news-group))))))
  488.   (set-buffer-modified-p t)
  489.   (sit-for 0))
  490.  
  491. (defun news-goto-news-group (gp)
  492.   "Takes a string and goes to that news group."
  493.   (interactive (list (completing-read "NewsGroup: "
  494.                       news-group-article-assoc)))
  495.   (message "Jumping to news group %s..." gp)
  496.   (news-select-news-group gp)
  497.   (message "Jumping to news group %s... done." gp))
  498.  
  499. (defun news-select-news-group (gp)
  500.   (let ((grp (assoc gp news-group-article-assoc)))
  501.     (if (null grp)
  502.      (error "Group %s not subscribed to" gp)
  503.       (progn
  504.     (news-update-message-read news-current-news-group
  505.                   (news-cdar news-point-pdl))
  506.     (news-read-files-into-buffer  (car grp) nil)
  507.     (news-set-mode-line)))))
  508.  
  509. (defun news-goto-message (arg)
  510.   "Goes to the article ARG in current newsgroup."
  511.   (interactive "p")
  512.   (if (null current-prefix-arg)
  513.       (setq arg (read-no-blanks-input "Go to article: " "")))
  514.   (news-select-message arg))
  515.  
  516. (defun news-select-message (arg)
  517.   (if (stringp arg) (setq arg (string-to-int arg)))
  518.   (let ((file (concat news-path
  519.               (string-subst-char ?/ ?. news-current-news-group)
  520.               "/" arg)))
  521.     (if (= arg 
  522.        (or (news-cadr (memq (news-cdar news-point-pdl) news-list-of-files))
  523.            0))
  524.     (setcdr (car news-point-pdl) arg))
  525.     (setq news-current-message-number arg)
  526.     (if (file-exists-p file)
  527.       (let ((buffer-read-only nil))
  528.         (news-read-in-file file)
  529.         (news-set-mode-line))
  530.       (news-set-mode-line)
  531.       (error "Article %d nonexistent" arg))))
  532.  
  533. (defun news-force-update ()
  534.   "updates the position of last article read in the current news group"
  535.   (interactive)
  536.   (setcdr (car news-point-pdl) news-current-message-number)
  537.   (message "Updated to %d" news-current-message-number))
  538.  
  539. (defun news-next-message (arg)
  540.   "Move ARG messages forward within one newsgroup.
  541. Negative ARG moves backward.
  542. If ARG is 1 or -1, moves to next or previous newsgroup if at end."
  543.   (interactive "p")
  544.   (let ((no (+ arg news-current-message-number)))
  545.     (if (or (< no news-current-group-begin) 
  546.         (> no news-current-group-end))
  547.     (cond ((= arg 1)
  548.            (news-set-current-group-certification)
  549.            (news-next-group))
  550.           ((= arg -1)
  551.            (news-previous-group))
  552.           (t (error "Article out of range")))
  553.       (let ((plist (news-get-motion-lists
  554.              news-current-message-number
  555.              news-list-of-files)))
  556.     (if (< arg 0)
  557.         (news-select-message (nth (1- (- arg)) (car (cdr plist))))
  558.       (news-select-message (nth (1- arg) (car plist))))))))
  559.  
  560. (defun news-previous-message (arg)
  561.   "Move ARG messages backward in current newsgroup.
  562. With no arg or arg of 1, move one message
  563. and move to previous newsgroup if at beginning.
  564. A negative ARG means move forward."
  565.   (interactive "p")
  566.   (news-next-message (- arg)))
  567.  
  568. (defun news-move-to-group (arg)
  569.   "Given arg move forward or backward to a new newsgroup."
  570.   (let ((cg news-current-news-group))
  571.     (let ((plist (news-get-motion-lists cg news-user-group-list))
  572.       ngrp)
  573.       (if (< arg 0)
  574.       (or (setq ngrp (nth (1- (- arg)) (news-cadr plist)))
  575.           (error "No previous news groups"))
  576.     (or (setq ngrp (nth arg (car plist)))
  577.         (error "No more news groups")))
  578.       (news-select-news-group ngrp))))
  579.  
  580. (defun news-next-group ()
  581.   "Moves to the next user group."
  582.   (interactive)
  583. ;  (message "Moving to next group...")
  584.   (news-move-to-group 0)
  585.   (while (null news-list-of-files)
  586.     (news-move-to-group 0)))
  587. ;  (message "Moving to next group... done.")
  588.  
  589. (defun news-previous-group ()
  590.   "Moves to the previous user group."
  591.   (interactive)
  592. ;  (message "Moving to previous group...")
  593.   (news-move-to-group -1)
  594.   (while (null news-list-of-files)
  595.     (news-move-to-group -1)))
  596. ;  (message "Moving to previous group... done.")
  597.  
  598. (defun news-get-motion-lists (arg listy)
  599.   "Given a msgnumber/group this will return a list of two lists;
  600. one for moving forward and one for moving backward."
  601.   (let ((temp listy)
  602.     (result ()))
  603.     (catch 'out
  604.       (while temp
  605.     (if (equal (car temp) arg)
  606.         (throw 'out (cons (cdr temp) (list result)))
  607.       (setq result (nconc (list (car temp)) result))
  608.       (setq temp (cdr temp)))))))
  609.  
  610. ;; miscellaneous io routines
  611. (defun news-read-in-file (filename)
  612.   (erase-buffer)
  613.   (let ((start (point)))
  614.   (insert-file-contents filename)
  615.   (news-convert-format)
  616.   ;; Run each hook that applies to the current newsgroup.
  617.   (let ((hooks news-group-hook-alist))
  618.     (while hooks
  619.       (goto-char start)
  620.       (if (string-match (car (car hooks)) news-group-name)
  621.       (funcall (cdr (car hooks))))
  622.       (setq hooks (cdr hooks))))
  623.   (goto-char start)
  624.   (forward-line 1)
  625.   (if (eobp)
  626.       (message "(Empty file?)")
  627.     (goto-char start))))
  628.  
  629. (defun news-convert-format ()
  630.   (save-excursion
  631.     (save-restriction
  632.       (let* ((start (point))
  633.          (end (condition-case ()
  634.               (progn (search-forward "\n\n") (point))
  635.             (error nil)))
  636.          has-from has-date)
  637.        (cond (end
  638.           (narrow-to-region start end)
  639.           (goto-char start)
  640.           (setq has-from (search-forward "\nFrom:" nil t))
  641.           (cond ((and (not has-from) has-date)
  642.              (goto-char start)
  643.              (search-forward "\nDate:")
  644.              (beginning-of-line)
  645.              (kill-line) (kill-line)))
  646.           (news-delete-headers start)
  647.           (goto-char start)))))))
  648.  
  649. (defun news-show-all-headers ()
  650.   "Redisplay current news item with all original headers"
  651.   (interactive)
  652.   (let (news-ignored-headers
  653.     (buffer-read-only ()))
  654.     (erase-buffer)
  655.     (news-set-mode-line)
  656.     (news-read-in-file
  657.      (concat news-path
  658.          (string-subst-char ?/ ?. news-current-news-group)
  659.          "/" (int-to-string news-current-message-number)))))
  660.  
  661. (defun news-delete-headers (pos)
  662.   (goto-char pos)
  663.   (and (stringp news-ignored-headers)
  664.        (while (re-search-forward news-ignored-headers nil t)
  665.      (beginning-of-line)
  666.      (delete-region (point)
  667.             (progn (re-search-forward "\n[^ \t]")
  668.                    (forward-char -1)
  669.                    (point))))))
  670.  
  671. (defun news-exit ()
  672.   "Quit news reading session and update the .newsrc file."
  673.   (interactive)
  674.   (if (y-or-n-p "Do you really wanna quit reading news ? ")
  675.       (progn (message "Updating %s..." news-startup-file)
  676.          (news-update-newsrc-file)
  677.          (news-write-certifications)
  678.          (message "Updating %s... done" news-startup-file)
  679.          (message "Now do some real work")
  680.          (and (fboundp 'bury-buffer) (bury-buffer (current-buffer)))
  681.          (switch-to-buffer news-buffer-save)
  682.          (setq news-user-group-list ()))
  683.     (message "")))
  684.  
  685. (defun news-update-newsrc-file ()
  686.   "Updates the .newsrc file in the users home dir."
  687.   (let ((newsrcbuf (find-file-noselect
  688.              (substitute-in-file-name news-startup-file)))
  689.     (tem news-user-group-list)
  690.     group)
  691.     (save-excursion
  692.       (if (not (null news-current-news-group))
  693.       (news-update-message-read news-current-news-group
  694.                     (news-cdar news-point-pdl)))
  695.       (set-buffer newsrcbuf)
  696.       (while tem
  697.     (setq group (assoc (car tem) news-group-article-assoc))
  698.     (if (= (news-cadr (news-cadr group)) (news-caddr (news-cadr group)))
  699.         nil
  700.       (goto-char 0)
  701.       (if (search-forward (concat (car group) ": ") nil t)
  702.           (kill-line nil)
  703.         (insert (car group) ": \n") (backward-char 1))
  704.       (insert (int-to-string (car (news-cadr group))) "-"
  705.           (int-to-string (news-cadr (news-cadr group)))))
  706.     (setq tem (cdr tem)))
  707.      (while news-unsubscribe-groups
  708.        (setq group (assoc (car news-unsubscribe-groups)
  709.               news-group-article-assoc))
  710.        (goto-char 0)
  711.        (if (search-forward (concat (car group) ": ") nil t)
  712.        (progn
  713.           (backward-char 2)
  714.           (kill-line nil)
  715.           (insert "! " (int-to-string (car (news-cadr group)))
  716.               "-" (int-to-string (news-cadr (news-cadr group))))))
  717.        (setq news-unsubscribe-groups (cdr news-unsubscribe-groups)))
  718.      (save-buffer)
  719.      (kill-buffer (current-buffer)))))
  720.  
  721.  
  722. (defun news-unsubscribe-group (group)
  723.   "Removes you from newgroup GROUP."
  724.   (interactive (list (completing-read  "Unsubscribe from group: "
  725.                       news-group-article-assoc)))
  726.   (news-unsubscribe-internal group))
  727.  
  728. (defun news-unsubscribe-current-group ()
  729.   "Removes you from the newsgroup you are now reading."
  730.   (interactive)
  731.   (if (y-or-n-p "Do you really want to unsubscribe from this group ? ")
  732.       (news-unsubscribe-internal news-current-news-group)))
  733.  
  734. (defun news-unsubscribe-internal (group)
  735.   (let ((tem (assoc group news-group-article-assoc)))
  736.     (if tem
  737.     (progn
  738.       (setq news-unsubscribe-groups (cons group news-unsubscribe-groups))
  739.       (news-update-message-read group (news-cdar news-point-pdl))
  740.       (if (equal group news-current-news-group)
  741.           (news-next-group))
  742.       (message ""))
  743.       (error "Not subscribed to group: %s" group))))
  744.  
  745. (defun news-save-item-in-file (file)
  746.   "Save the current article that is being read by appending to a file."
  747.   (interactive "FSave item in file: ")
  748.   (append-to-file (point-min) (point-max) file))
  749.  
  750. (defun news-get-pruned-list-of-files (gp-list end-file-no)
  751.   "Given a news group it finds all files in the news group.
  752. The arg must be in slashified format.
  753. Using ls was found to be too slow in a previous version."
  754.   (let
  755.       ((answer
  756.     (and
  757.      (not (and end-file-no
  758.            (equal (news-set-current-certifiable)
  759.              (news-group-certification gp-list))
  760.            (setq news-list-of-files nil
  761.              news-list-of-files-possibly-bogus t)))
  762.      (let* ((file-directory (concat news-path
  763.                     (string-subst-char ?/ ?. gp-list)))
  764.         tem
  765.         (last-winner
  766.          (and end-file-no
  767.               (news-wins file-directory end-file-no)
  768.               (news-find-first-or-last file-directory end-file-no 1))))
  769.        (setq news-list-of-files-possibly-bogus t news-list-of-files nil)
  770.        (if last-winner
  771.            (progn
  772.          (setq news-list-of-files-possibly-bogus t
  773.                news-current-group-end last-winner)
  774.          (while (> last-winner end-file-no)
  775.            (news-push last-winner news-list-of-files)
  776.            (setq last-winner (1- last-winner)))
  777.          news-list-of-files)
  778.          (if (or (not (file-directory-p file-directory))
  779.              (not (file-readable-p file-directory)))
  780.          nil
  781.            (setq news-list-of-files
  782.              (condition-case error
  783.              (directory-files file-directory)
  784.                (file-error
  785.             (if (string= (nth 2 error) "permission denied")
  786.                 (message "Newsgroup %s is read-protected"
  787.                      gp-list)
  788.               (signal 'file-error (cdr error)))
  789.             nil)))
  790.            (setq tem news-list-of-files)
  791.            (while tem
  792.          (if (or (not (string-match "^[0-9]*$" (car tem)))
  793.              ;; dont get confused by directories that look like numbers
  794.              (file-directory-p
  795.               (concat file-directory "/" (car tem)))
  796.              (<= (string-to-int (car tem)) end-file-no))
  797.              (setq news-list-of-files
  798.                (delq (car tem) news-list-of-files)))
  799.          (setq tem (cdr tem)))
  800.            (if (null news-list-of-files)
  801.            (progn (setq news-current-group-end 0)
  802.               nil)
  803.          (setq news-list-of-files
  804.                (mapcar 'string-to-int news-list-of-files))
  805.          (setq news-list-of-files (sort news-list-of-files '<))
  806.          (setq news-current-group-end
  807.                (elt news-list-of-files
  808.                 (1- (length news-list-of-files))))
  809.          news-list-of-files)))))))
  810.     (or answer (progn (news-set-current-group-certification) nil))))
  811.  
  812. (defun news-read-files-into-buffer (group reversep)
  813.   (let* ((files-start-end (news-cadr (assoc group news-group-article-assoc)))
  814.      (start-file-no (car files-start-end))
  815.      (end-file-no (news-cadr files-start-end))
  816.      (buffer-read-only nil))
  817.     (setq news-current-news-group group)
  818.     (setq news-current-message-number nil)
  819.     (setq news-current-group-end nil)
  820.     (news-set-mode-line)
  821.     (news-get-pruned-list-of-files group end-file-no)
  822.     (news-set-mode-line)
  823.     ;; @@ should be a lot smarter than this if we have to move
  824.     ;; @@ around correctly.
  825.     (setq news-point-pdl (list (cons (car files-start-end)
  826.                      (news-cadr files-start-end))))
  827.     (if (null news-list-of-files)
  828.     (progn (erase-buffer)
  829.            (setq news-current-group-end end-file-no)
  830.            (setq news-current-group-begin end-file-no)
  831.            (setq news-current-message-number end-file-no)
  832.            (news-set-mode-line)
  833. ;           (message "No new articles in " group " group.")
  834.            nil)
  835.       (setq news-current-group-begin (car news-list-of-files))
  836.       (if reversep
  837.       (setq news-current-message-number news-current-group-end)
  838.     (if (> (car news-list-of-files) end-file-no)
  839.         (setcdr (car news-point-pdl) (car news-list-of-files)))
  840.     (setq news-current-message-number news-current-group-begin))
  841.       (news-set-message-counters)
  842.       (news-set-mode-line)
  843.       (news-read-in-file (concat news-path
  844.                  (string-subst-char ?/ ?. group)
  845.                  "/"
  846.                  (int-to-string
  847.                    news-current-message-number)))
  848.       (news-set-message-counters)
  849.       (news-set-mode-line)
  850.       t)))
  851.  
  852. (defun news-add-news-group (gp)
  853.   "Resubscribe to or add a USENET news group named GROUP (a string)."
  854. ; @@ (completing-read ...)
  855. ; @@ could be based on news library file ../active (slightly facist)
  856. ; @@ or (expensive to compute) all directories under the news spool directory
  857.   (interactive "sAdd news group: ")
  858.   (let ((file-dir (concat news-path (string-subst-char ?/ ?. gp))))
  859.     (save-excursion
  860.       (if (null (assoc gp news-group-article-assoc))
  861.       (let ((newsrcbuf (find-file-noselect
  862.                 (substitute-in-file-name news-startup-file))))
  863.         (if (file-directory-p file-dir)
  864.         (progn
  865.           (switch-to-buffer newsrcbuf)
  866.           (goto-char 0)
  867.           (if (search-forward (concat gp "! ") nil t)
  868.               (progn
  869.             (message "Re-subscribing to group %s." gp)
  870.             ;;@@ news-unsubscribe-groups isn't being used
  871.             ;;(setq news-unsubscribe-groups
  872.             ;;    (delq gp news-unsubscribe-groups))
  873.             (backward-char 2)
  874.             (delete-char 1)
  875.             (insert ":"))
  876.             (progn
  877.               (message
  878.                "Added %s to your list of newsgroups." gp)
  879.               (end-of-buffer)
  880.               (insert gp ": 1-1\n")))
  881.           (search-backward gp nil t)
  882.           (let (start end endofline tem)
  883.             (search-forward ": " nil t)
  884.             (setq end (point))
  885.             (beginning-of-line)
  886.             (setq start (point))
  887.             (end-of-line)
  888.             (setq endofline (point))
  889.             (setq tem (buffer-substring start (- end 2)))
  890.             (let ((range (news-parse-range
  891.                   (buffer-substring end endofline))))
  892.               (setq news-group-article-assoc
  893.                 (cons (list tem (list (car range)
  894.                           (cdr range)
  895.                           (cdr range)))
  896.                   news-group-article-assoc))))
  897.           (save-buffer)
  898.           (kill-buffer (current-buffer)))
  899.           (message "Newsgroup %s doesn't exist." gp)))
  900.     (message "Already subscribed to group %s." gp)))))
  901.  
  902. (defun news-make-link-to-message (number newname)
  903.     "Forges a link to an rnews message numbered number (current if no arg)
  904. Good for hanging on to a message that might or might not be
  905. automatically deleted."
  906.   (interactive "P
  907. FName to link to message: ")
  908.   (add-name-to-file
  909.    (concat news-path
  910.        (string-subst-char ?/ ?. news-current-news-group)
  911.        "/" (if number
  912.            (prefix-numeric-value number)
  913.          news-current-message-number))
  914.    newname))
  915.  
  916. ;;; caesar-region written by phr@prep.ai.mit.edu  Nov 86
  917. ;;; modified by tower@prep Nov 86
  918. (defun caesar-region (&optional n)
  919.   "Caesar rotation of region by N, default 13, for decrypting netnews."
  920.   (interactive (if current-prefix-arg    ; Was there a prefix arg?
  921.            (list (prefix-numeric-value current-prefix-arg))
  922.          (list nil)))
  923.   (cond ((not (numberp n)) (setq n 13))
  924.     ((< n 0) (setq n (- 26 (% (- n) 26))))
  925.     (t (setq n (% n 26))))        ;canonicalize N
  926.   (if (not (zerop n))        ; no action needed for a rot of 0
  927.       (progn
  928.     (if (or (not (boundp 'caesar-translate-table))
  929.         (/= (aref caesar-translate-table ?a) (+ ?a n)))
  930.         (let ((i 0) (lower "abcdefghijklmnopqrstuvwxyz") upper)
  931.           (message "Building caesar-translate-table...")
  932.           (setq caesar-translate-table (make-vector 256 0))
  933.           (while (< i 256)
  934.         (aset caesar-translate-table i i)
  935.         (setq i (1+ i)))
  936.           (setq lower (concat lower lower) upper (upcase lower) i 0)
  937.           (while (< i 26)
  938.         (aset caesar-translate-table (+ ?a i) (aref lower (+ i n)))
  939.         (aset caesar-translate-table (+ ?A i) (aref upper (+ i n)))
  940.         (setq i (1+ i)))
  941.           (message "Building caesar-translate-table... done")))
  942.     (let ((from (region-beginning))
  943.           (to (region-end))
  944.           (i 0) str len)
  945.       (setq str (buffer-substring from to))
  946.       (setq len (length str))
  947.       (while (< i len)
  948.         (aset str i (aref caesar-translate-table (aref str i)))
  949.         (setq i (1+ i)))
  950.       (goto-char from)
  951.       (kill-region from to)
  952.       (insert str)))))
  953.  
  954. ;;; news-caesar-buffer-body written by paul@media-lab.mit.edu  Wed Oct 1, 1986
  955. ;;; hacked further by tower@prep.ai.mit.edu
  956. (defun news-caesar-buffer-body (&optional rotnum)
  957.   "Caesar rotates all letters in the current buffer by 13 places.
  958. Used to encode/decode possibly offensive messages (commonly in net.jokes).
  959. With prefix arg, specifies the number of places to rotate each letter forward.
  960. Mail and USENET news headers are not rotated."
  961.   (interactive (if current-prefix-arg    ; Was there a prefix arg?
  962.            (list (prefix-numeric-value current-prefix-arg))
  963.          (list nil)))
  964.   (save-excursion
  965.     (let ((buffer-status buffer-read-only))
  966.       (setq buffer-read-only nil)
  967.       ;; setup the region
  968.       (set-mark (if (progn (goto-char (point-min))
  969.                 (search-forward
  970.                  (concat "\n"
  971.                      (if (equal major-mode 'news-mode)
  972.                      ""
  973.                        mail-header-separator)
  974.                      "\n") nil t))
  975.              (point)
  976.            (point-min)))
  977.       (goto-char (point-max))
  978.       (caesar-region rotnum)
  979.       (setq buffer-read-only buffer-status))))
  980.