;;; emacs-wiki-count-words.el --- Counts number of words in Wiki buffer by translating it to html and washing it.
;; Author: Anirudh Sasikumar <anirudh@anisk.8bit.co.uk>
;; 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
;; 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."
    (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)))

;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"
  (let ((html2text-format-single-element-list 
	 '(("hr" . html2text-clean-hr-mod)))
	 '(("&nbsp;" . " ") ("&#32;" . " ") ("&gt;" . ">") 
	   ("&lt;" . "<") ("&quot;" . "\"") ("&amp;" . "&")))
	 '("h3" "html" "body" "p" "a" "center" "b" "blockquote" 
	   "u" "i" "img" "dir" "head" "div" "br" "font" "title" 
	   "meta" "span" "code" "em"))
	 '(("sup" . html2text-clean-sup)
	   ("ul"  . html2text-clean-ul)
	   ("ol"  . html2text-clean-ol)
	   ("dl"  . html2text-clean-dl))) tempreg)

(defun emacs-wiki-count-words ()
  "Counts total words in entry filename"
  (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 ""))
      ;parses html
      (setq wordcnt (count-words t)))
  (kill-buffer "*Wiki Word Counter*")
  (message (format "There are %d words in current Wiki buffer" wordcnt))

(define-key emacs-wiki-mode-map
  [(control ?x) (?w)]

Copyright © 2004-2011 Anirudh Sasikumar. All rights reserved.
Last Updated: November 29, 2011 4:41 PM