Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;;; Commentary:
- ;; I can't freaking believe this is still not part of Emacs.
- ;; Maybe it could be done better--this is pretty basic, really.
- ;; I mean, sure you can't trust the word count for things like
- ;; sending rocket ships to the moon, but frankly, is it that
- ;; important? Anyway, do M-x wc RET and it should pretty much
- ;; work. It uses the current syntax table's definition of a
- ;; word.
- ;;; Code.
- ;; This is the logic, and can be used in a program. The other
- ;; functions use this interactively in various ways.
- (defun wc-non-interactive (start end)
- "Count the number of words in the current region."
- (save-excursion
- (save-restriction
- (narrow-to-region start end)
- (goto-char (point-min))
- (count-matches "\\sw+"))))
- ;;;###autoload
- (defun wc-buffer ()
- "Display the number of words in the current buffer."
- (interactive)
- (message (concat "The current buffer contains "
- (number-to-string
- (wc-non-interactive (point-min) (point-max)))
- " words.")))
- ;;;###autoload
- (defun wc-region (start end)
- "Display number of words in the region."
- (interactive "r")
- (message (concat "The current region contains "
- (number-to-string
- (wc-non-interactive start end))
- " words.")))
- ;;;###autoload
- (defun wc-dwim ()
- "Display a word count.
- If there is a region defined, display the count for the region.
- If not, display a word count for the whole buffer."
- (interactive)
- (if mark-active
- (wc-region (point) (mark))
- (wc-buffer)))
- (defalias 'wc 'wc-dwim)
- (provide 'wc)
- ;;; wc.el ends here
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement