I am trying to reduce the word count in a document I am writing. The existing word count functionality for emacs is surprisingly lacking. I wanted a word count function for org mode which excluded tables and comments, and ended up writing one myself.
If this function is called with a region highlighted, it counts the words in the region. Otherwise it counts words in the whole buffer. It ignores commented lines and tables. LaTeX-style macros such as \foo{bar,baz} are counted as 1 word, as a compromise (more often than not they should count as 0, but they do sometimes expand to 1 or more words in the final document). Limitations: - Does not ignore BEGIN_SRC/END_SRC or inline src_* blocks (babel). Should be easy enough to add however. - There is probably a better way of identifying latex macros than my 'latex-macro-regexp' below. - Ignores all org links. I couldn't figure out how to extract "description" text from links, but I didn't look very hard. Improvements welcome. Paul ------------------------------------------------------------------------ (defun in-comment-p () "Return non-nil if point is in a comment." (if (or (null comment-start-skip) (eq (preceding-char) ?\r)) nil (save-excursion (let ((pos (point))) (re-search-backward "^\\|\r" nil t) (or (looking-at comment-start-skip) (re-search-forward comment-start-skip pos t)))))) (defun in-org-table-p () "Return non-nil if point is in an org-mode table." (if (or (not (boundp 'org-table-any-line-regexp)) (null org-table-any-line-regexp) (eq (preceding-char) ?\r)) nil (save-excursion (let ((pos (point))) (re-search-backward "^\\|\r" nil t) (looking-at org-table-any-line-regexp))))) (defvar latex-macro-regexp "\\\\[A-Za-z]+\\(\\[[^]]*\\]\\|\\){\\([^}]*\\)}") (defun org-word-count (beg end) (interactive "r") (unless mark-active (setf beg (point-min) end (point-max))) (let ((wc 0)) (save-excursion (goto-char beg) (while (< (point) end) (re-search-forward "\\w+\\W*") (cond ((or (in-comment-p) (in-org-table-p)) nil) ((looking-at org-any-link-re) (goto-char (match-end 0))) ((save-excursion (backward-char) (looking-at latex-macro-regexp)) (goto-char (match-end 0)) (setf wc (+ 2 wc))) (t (incf wc))))) (message (format "%d words in %s." wc (if mark-active "region" "buffer"))))) _______________________________________________ Emacs-orgmode mailing list Please use `Reply All' to send replies to the list. Emacs-orgmode@gnu.org http://lists.gnu.org/mailman/listinfo/emacs-orgmode