Advertisement
Guest User

wc.el

a guest
Dec 2nd, 2012
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 1.58 KB | None | 0 0
  1. ;;; Commentary:
  2.  
  3. ;; I can't freaking believe this is still not part of Emacs.
  4. ;; Maybe it could be done better--this is pretty basic, really.
  5. ;; I mean, sure you can't trust the word count for things like
  6. ;; sending rocket ships to the moon, but frankly, is it that
  7. ;; important?  Anyway, do M-x wc RET and it should pretty much
  8. ;; work.  It uses the current syntax table's definition of a
  9. ;; word.
  10.  
  11. ;;; Code.
  12.  
  13. ;; This is the logic, and can be used in a program.  The other
  14. ;; functions use this interactively in various ways.
  15. (defun wc-non-interactive (start end)
  16.   "Count the number of words in the current region."
  17.   (save-excursion
  18.     (save-restriction
  19.       (narrow-to-region start end)
  20.       (goto-char (point-min))
  21.       (count-matches "\\sw+"))))
  22.  
  23. ;;;###autoload
  24. (defun wc-buffer ()
  25.   "Display the number of words in the current buffer."
  26.   (interactive)
  27.   (message (concat "The current buffer contains "
  28.            (number-to-string
  29.             (wc-non-interactive (point-min) (point-max)))
  30.            " words.")))
  31.  
  32. ;;;###autoload
  33. (defun wc-region (start end)
  34.   "Display number of words in the region."
  35.   (interactive "r")
  36.   (message (concat "The current region contains "
  37.            (number-to-string
  38.             (wc-non-interactive start end))
  39.            " words.")))
  40.  
  41. ;;;###autoload
  42. (defun wc-dwim ()
  43.   "Display a word count.
  44. If there is a region defined, display the count for the region.
  45. If not, display a word count for the whole buffer."
  46.   (interactive)
  47.   (if mark-active
  48.       (wc-region (point) (mark))
  49.     (wc-buffer)))
  50.  
  51. (defalias 'wc 'wc-dwim)
  52.  
  53. (provide 'wc)
  54.  
  55. ;;; wc.el ends here
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement