[gnu.emacs.gnus] Reordering the .newsrc

tale@pawl.rpi.edu (David C Lawrence) (03/27/89)

Well, here is my contribution to the reordering of the .newsrc.
Basically what I did was take David Detlefs idea and make my own code
for it, adding two more features.  Both of these features can be
overridden. 
  
Here is what happens: the function checks to see whether any
unsubscribed groups exist before the point where the rest of the
groups are all unsubscribed.  It moves all such groups to the
beginning of the "unsubscribed section".  The groups get sorted
alphabetically as long as optional first argument nosort is non-nil.
Finally all of the ranges which have more than two article numbers get
condensed to a two article range of the first number and the last, as
long as the optional second argument nocompress is non-nil.

The second action is probably much more curious than the first, so I
will explain why it I do it.  I dislike the huge size of .newsrc.el
and notice that one thing which contributes heavily to it's size is
the ranges of articles in groups I don't even read.  Back when I used
rn, I would go through my .newsrc periodically and shorten all such
ranges as I have mentioned. group! 1-3,5,7-8,10 became group! 1-10.
There certainly is a loss of information there as it seems to indicate
that I have seen or killed articles which I have not.  Frankly, I
don't care.  If a group is unsubscribed, what is happening in it means
nothing to me.  If I want to go back and look at an article which I
have now engulfed in my range, I still can.  The advantage of it to me
is this: my .newsrc.el has shrunk from 24k to 13k and my .newsrc also
experienced minimal but still existent shrinkage.

Sorry for the dissertation.  Here's the code:

-------------------- Cut -------------------- Cut --------------------
;;; Based on an idea by David.Detlefs@DLD.AVALON.CS.CMU.EDU
;;; Suitable for calling as a gnus-Save-newsrc-hook.
(defun gnus-reorder-newsrc-file (&optional nosort nocompress)
  (let (gnus-unsub-assoc reordered)
    (save-excursion
      (set-buffer (get-file-buffer gnus-current-startup-file))
      (goto-char (point-max))
      ;; protect against a totally unsubscribed .newsrc
      (if (not (re-search-backward "^.*:" nil t)) nil
        (re-search-forward "^.*! " nil t)
        (setq gnus-unsub-assoc
              (memq (assoc
                     (buffer-substring (- (point) 2)
                                       (progn (beginning-of-line) (point)))
                     gnus-newsrc-assoc) gnus-newsrc-assoc))
        (while (re-search-backward "^.*! " nil t)
          (let ((position (point)) unsub-line)
            (setq gnus-unsub-assoc
                  (cons (assoc
                         (buffer-substring
                          (point) (progn (skip-chars-forward "^!") (point)))
                         gnus-newsrc-assoc) gnus-unsub-assoc)
                  unsub-line (buffer-substring position
                                               (progn (beginning-of-line)
                                                 (next-line 1) (point)))
                  reordered t)
            (delete-region position (point))
            (goto-char (point-max)) (insert unsub-line) (goto-char position)))
        (if (or nosort (not reordered)) nil
          (sort-lines nil (progn (re-search-forward "! " nil t)
                                 (beginning-of-line) (point)) (point-max))
          (setq gnus-unsub-assoc (sort gnus-unsub-assoc 'gnus-assoc-lessp)))
        (if nocompress nil
          (goto-char (point-min))
          (re-search-forward "! " nil t)
          (while (re-search-forward "," nil t)
            (let ((compress-group
                   (assoc (buffer-substring
                           (progn (beginning-of-line) (point))
                           (progn (skip-chars-forward "^!") (point)))
                          gnus-newsrc-assoc)) first-unread)
              (delete-region (progn (skip-chars-forward "^-,") (point))
                             (progn (end-of-line) (skip-chars-backward "^-,")
                                    (point)))
              (insert "-")
              (setcdr (nth 2 compress-group)
                      (cdr (nth (1- (length compress-group)) compress-group)))
              (setcdr (memq (nth 2 compress-group) compress-group) nil))))
        (setq gnus-newsrc-assoc
              (append (gnus-set-difference gnus-newsrc-assoc gnus-unsub-assoc)
                      gnus-unsub-assoc))))))

(defun gnus-assoc-lessp (list1 list2)
  "Returns t if the car of LIST1 (a string) is less than the car of
LIST2 by doing comparison with string-lessp."
  (string-lessp (car list1) (car list2)))
--
      tale@rpitsmts.bitnet, tale%mts@itsgw.rpi.edu, tale@pawl.rpi.edu