home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / gnu / emacs / bug / 1622 < prev    next >
Encoding:
Text File  |  1993-01-08  |  1.9 KB  |  53 lines

  1. Newsgroups: gnu.emacs.bug
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!traffic.den.mmc.com!kevin
  3. From: kevin@traffic.den.mmc.com (Kevin Rodgers)
  4. Subject: count-matches
  5. Message-ID: <9301081827.AA02773@traffic>
  6. Sender: gnulists@ai.mit.edu
  7. Reply-To: kevin@traffic.den.mmc.com
  8. Organization: GNUs Not Usenet
  9. Distribution: gnu
  10. Date: Fri, 8 Jan 1993 18:27:32 GMT
  11. Approved: bug-gnu-emacs@prep.ai.mit.edu
  12. Lines: 39
  13.  
  14. Re: Emacs 18.58
  15.  
  16. 1. count-matches (aka how-many), which is defined in replace.el, isn't
  17.    documented in the elisp manual (1.03).
  18.  
  19. 2. count-matches should preserve the match data by saving and restoring
  20.    it before returning.
  21.  
  22. 3. It would be more useful if count-matches returned an integer rather
  23.    than printing a message; how-many (which should probably be renamed
  24.    how-many-matches) could be used to print the message containing the
  25.    count returned by count-matches.
  26.  
  27. 4. Just as string-match is provided as an analog to re-search-forward,
  28.    count-string-matches and how-many-string-matches should be provided
  29.    as well (and documented in the elisp manual):
  30.  
  31. (defun count-string-matches (regexp string &optional start)
  32.   "Return the number of matches for REGEXP in STRING.  If optional
  33. argument START is non-nil, count matches from that index in STRING."
  34.   (let ((match-data (match-data))
  35.     (count 0))
  36.     (unwind-protect
  37.     (while (string-match regexp string start)
  38.       (setq count (1+ count)
  39.         start (match-end 0)))
  40.       (store-match-data match-data))
  41.     count))
  42.  
  43. (defun how-many-string-matches (regexp string &optional start)
  44.   "Print the number of matches for REGEXP in STRING.  If optional
  45. argument START is non-nil, count matches from that index in STRING."
  46.   (message "%d occurrences" (count-string-matches regexp string start)))
  47. --
  48. Kevin Rodgers                kevin@traffic.den.mmc.com
  49. Martin Marietta MS A16401        (303) 790-3971
  50. 116 Inverness Dr. East
  51. Englewood CO 80112 USA            GO BUFFS!
  52.  
  53.