forked from purcell/emacs.d
/
wc.el
executable file
·94 lines (78 loc) · 2.8 KB
/
wc.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
;;; wc.el --- count words
;; Version: 2
;; Copyright (C) 2007 Evans Winner
;; Time-stamp: <2012-10-06 14:31:52 evansw>
;; Author: thorne <ego111@gmail.com>
;; Created: 2007.9.1
;; Keywords: wp
;; Favorite day: Monday
;; This file is not part of GNU Emacs.
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License,
;; version 2, as published by the Free Software Foundation.
;; This program is distributed in the hope that it will be
;; useful, but WITHOUT ANY WARRANTY; without warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public
;; License along with this program; if not, write to the Free
;; Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
;; MA 02111-1307 USA
;;; 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
(wc-count-matches "\\sw+"
(if (> end start)
start end)
(if (> end start)
end start)))))
;; Taken from somewhere for emacs 21 compat. Where did I find this?
;; Something called puppet-mode, apparently. No idea what it's for.
(defun wc-count-matches (re start end)
"The same as Emacs 22 count-matches, for portability to other
versions of Emacs."
(save-excursion
(let ((n 0))
(goto-char start)
(while (re-search-forward re end t) (setq n (1+ n)))
n)))
;;;###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