home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / functions / gnus-hide-quote.el < prev    next >
Encoding:
Text File  |  1991-02-11  |  5.5 KB  |  149 lines

  1. ;From: jwz@lucid.com (Jamie Zawinski)
  2. ;Subject: new gnus-hide-quote.el
  3. ;Date: 29 Jan 91 23:18:00 GMT
  4. ;Organization: Lucid, Inc., Menlo Park, CA
  5. ;
  6. ;Tim Lambert posted a version of this back in december; I've improved it to
  7. ;automagically cope with citation styles other than quoted-lines-begin-with-">".
  8. ;I've been using this for a couple of days, and haven't run across any messages
  9. ;that it doesn't Do the Right Thing for, even SuperCited ones (but even if it
  10. ;did guess wrong, you can still give it a prefix argument and it will prompt 
  11. ;you for the citation prefix.)
  12. ;
  13. ;Let me know if you stumble across any messages for which it picks an incorrect
  14. ;citation string.
  15. ;                    -- Jamie <jwz@lucid.com>
  16.  
  17. ;;; -*- Mode:Emacs-Lisp -*-
  18.  
  19. ;;; Don't you hate it when an article has pages of quoted text that you've
  20. ;;; already read?  Load this file and you can just type 'h' to get rid of
  21. ;;; it and 'H' to bring it back.
  22. ;;;
  23. ;;; If someone uses something other than ">" to mark the quoted text, it
  24. ;;; notices that; it can even cope with the kind of stuff that SuperCite
  25. ;;; inserts, and simple indentation (though it tries that as a last resort).
  26. ;;; If it can't figure out what the attribution string is, it prompts for it.
  27. ;;; 'C-uh' will make it prompt anyway, with it's guess as a default.  If a
  28. ;;; SuperCited article has multiple sections like
  29. ;;;
  30. ;;;       FOO> some text, some text
  31. ;;;       FOO> blah blah blah
  32. ;;;       oh yeah?
  33. ;;;       BAR> some text, some text
  34. ;;;       BAR> blah blah blah
  35. ;;;
  36. ;;; you can generally make both sections go away just by typing 'h' twice.
  37. ;;; Also, if two blocks of text to be elided are seperated only by blank
  38. ;;; lines, the blank lines are hidden as well.
  39. ;;;
  40. ;;;  14-dec-90    Tim Lambert <lambert@spectrum.cs.unsw.oz.au>
  41. ;;;        Created.
  42. ;;;  27-jan-91    Jamie Zawinski <jwz@lucid.com>
  43. ;;;        Made it automatic.
  44.  
  45. (require 'gnus)
  46.  
  47. (defun gnus-hide-quote-internal (prefix)
  48.   (let ((search-pattern (concat "\n+" prefix))
  49.     (looking-at-pattern (concat "^" prefix)))
  50.     (save-excursion
  51.       (save-restriction
  52.     (widen)
  53.     (goto-char (point-min))
  54.     (and (search-forward "\n\n" nil t)
  55.          (forward-char -1))
  56.     (while (re-search-forward search-pattern nil t)
  57.       ;; go forward one line, so that exactly one line of each
  58.       ;; elided block is visible, to give a little bit of context.
  59.       (forward-line 1)
  60.       (while (looking-at looking-at-pattern)
  61.         (delete-char -1)
  62.         (insert "\^M")
  63.         (forward-line 1)))))))
  64.  
  65. (defconst gnus-possible-quote-prefixes
  66.     '("^[^ \t\n\(A-Z#%;]"    ;; first, search for ">", "}", etc.
  67.       "^[ \t]+[^ \t\n\(A-Z#%;]"    ;; then that with leading whitespace.
  68.                 ;; these don't use #%; because of shar files
  69.                       ;; and postscript and lisp code...
  70.       "^[ \t]*[A-Z]+[]}>[{<-]"  ;; then, SuperCite: "FOO> ", "  Yow>", etc.
  71.       "^[ \t]+"            ;; then, simple indentation.
  72.       )
  73.   "Regexps to search for to identify quoted-text attributions.
  74. These regexps should match the initial subsequence of the line that is the
  75. attribution prefix.  They are ordered; regexps which are less ambiguous and 
  76. less likely to produce mismatches should come first.  The entire buffer will 
  77. be searched for two or more consecutive lines which match the first element 
  78. of this list, then the second, and so on.  The initial subsequence of the 
  79. two lines which first match is returned.")
  80.  
  81. (defun gnus-identify-quote-prefix ()
  82.   "Figure out what the current message uses for attribution.  See the
  83. documentation for gnus-possible-quote-prefixes."
  84.   (save-excursion
  85.     (goto-char (point-min))
  86.     (search-forward "\n\n" nil t)
  87.     (let ((match nil)
  88.       (start (point))
  89.       (rest gnus-possible-quote-prefixes))
  90.       (while (and rest (not match))
  91.     (goto-char start)
  92.     (let ((regexp (car rest)))
  93.       (while (not (or match (eobp)))
  94.         (if (re-search-forward regexp nil 0)
  95.         (progn
  96.           (beginning-of-line)
  97.           (let ((prefix (buffer-substring (point) (match-end 0))))
  98.             (forward-line 1)
  99.             (if (looking-at (regexp-quote prefix))
  100.             (setq match prefix)
  101.             (forward-line -1)))))
  102.         (forward-line 1)))
  103.     (setq rest (cdr rest)))
  104.       match)))
  105.  
  106. (defun gnus-Subject-hide-quote (&optional prefix-string)
  107.   "Hide quotations in current article."
  108.   (interactive (list
  109.          (let* ((default (gnus-eval-in-buffer-window gnus-Article-buffer
  110.                    (gnus-identify-quote-prefix)))
  111.             (string (if (or current-prefix-arg (not default))
  112.                     (read-from-minibuffer
  113.                       (concat
  114.                     "String that starts quotation lines"
  115.                     (if default
  116.                         (concat " \(default \"" default "\"\)"))
  117.                     ": "))
  118.                     default)))
  119.            (if (string= "" string)
  120.                (or default (error "You tell me, buckaroo."))
  121.                string))))
  122.   (if (string= prefix-string "") (error "empty string"))
  123.   (message "Hiding text beginning with \"%s\"..." prefix-string)
  124.   (gnus-eval-in-buffer-window gnus-Article-buffer
  125.     (save-excursion
  126.       (goto-char (point-min))
  127.       (let ((buffer-read-only nil)
  128.         (quote-regexp (concat "\n*" (regexp-quote prefix-string))))
  129.     (gnus-hide-quote-internal quote-regexp)
  130.     (set-buffer-modified-p nil))
  131.       (setq selective-display t)))
  132.   (message "Hiding text beginning with \"%s\"... done." prefix-string))
  133.       
  134. (defun gnus-Subject-show-quote ()
  135.   "Show quotations in curent article."
  136.   (interactive)
  137.   (gnus-eval-in-buffer-window gnus-Article-buffer
  138.     (save-excursion
  139.       (goto-char (point-min))
  140.       (let ((buffer-read-only nil))
  141.     (while (search-forward "\C-m" nil t)
  142.       (delete-char -1)
  143.       (insert "\n")))
  144.       (set-buffer-modified-p nil))))
  145.  
  146. (define-key gnus-Subject-mode-map "h" 'gnus-Subject-hide-quote)
  147. (define-key gnus-Subject-mode-map "H" 'gnus-Subject-show-quote)
  148.  
  149.