;;; emacs-wiki-count-words.el --- Counts number of words in Wiki buffer by translating it to html and washing it. ;; Author: Anirudh Sasikumar ;; Version: 0.1 ;; URL: http://www.aloofhosting.com/anisk/code/emacs-wiki-count-words.el ;; This file is free software; you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by ;; the Free Software Foundation; either version 2, or (at your option) ;; any later version. ;; This file is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied 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 GNU Emacs; see the file COPYING. If not, write to ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330, ;; Boston, MA 02111-1307, USA. ;;; Commentary: ;; This file is for use within emacs-wiki mode only. ;; When emacs-wiki-count-words is called while in a emacswiki buffer, ;; the number of words after converting the buffer to html and parsing ;; it is returned. ;; I find it convenient to bind this function to C-x w analogous to C-x l ;; which calls the built in function (count-lines-page) ;; Example: ;; While in an EmacsWiki buffer, press C-x w to get the number of words in ;; the wiki buffer ;;; Code: (defun count-words (&optional silent) "Counts the number of words in the buffer. If silent is t, then does not print message." (interactive) (save-excursion (goto-char 0) (let ((inhibit-field-text-motion t) (count 0)) (while (forward-word 1) (setq count (1+ count))) (if (not silent) (message (format "There are %d words in current buffer." count))) count))) ;custom funcs for html2text (defun html2text-clean-sup (p1 p2 p3 p4) (html2text-delete-tags p1 p2 p3 p4) (goto-char p1) (insert "^")) (defun html2text-clean-hr-mod (p1 p2) (html2text-delete-single-tag p1 p2)) (defun emacs-wiki-html-to-text-html2text () "Converts html in current buffer to text using html2text" (interactive) (let ((html2text-format-single-element-list '(("hr" . html2text-clean-hr-mod))) (html2text-replace-list '((" " . " ") (" " . " ") (">" . ">") ("<" . "<") (""" . "\"") ("&" . "&"))) (html2text-remove-tag-list '("h3" "html" "body" "p" "a" "center" "b" "blockquote" "u" "i" "img" "dir" "head" "div" "br" "font" "title" "meta" "span" "code" "em")) (html2text-format-tag-list '(("sup" . html2text-clean-sup) ("ul" . html2text-clean-ul) ("ol" . html2text-clean-ol) ("dl" . html2text-clean-dl))) tempreg) (html2text))) (defun emacs-wiki-count-words () "Counts total words in entry filename" (interactive) (let ((kill-whole-line t) (inhibit-read-only t) (text (buffer-substring (point-min) (point-max))) (wordcnt 0)) (with-current-buffer (get-buffer-create "*Wiki Word Counter*") (insert text) ;converts to html (let ((emacs-wiki-publishing-header "") (emacs-wiki-publishing-footer "")) (emacs-wiki-maybe) (emacs-wiki-replace-markup)) ;parses html (emacs-wiki-html-to-text-html2text) (setq wordcnt (count-words t))) (kill-buffer "*Wiki Word Counter*") (message (format "There are %d words in current Wiki buffer" wordcnt)) wordcnt )) (define-key emacs-wiki-mode-map [(control ?x) (?w)] 'emacs-wiki-count-words)