Function: tex-count-words

tex-count-words is an interactive and byte-compiled function defined in tex-mode.el.gz.

Signature

(tex-count-words BEGIN END)

Documentation

Count the number of words in the buffer.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/textmodes/tex-mode.el.gz
(defun tex-count-words (begin end)
  "Count the number of words in the buffer."
  (interactive
   (if (and transient-mark-mode mark-active)
       (list (region-beginning) (region-end))
     (list (point-min) (point-max))))
  ;; TODO: skip comments and math and maybe some environments.
  (save-excursion
    (goto-char begin)
    (let ((count 0))
      (while (and (< (point) end) (re-search-forward "\\<" end t))
	(if (not (eq (char-syntax (preceding-char)) ?/))
	    (progn
	      ;; Don't count single-char words.
              (unless (looking-at ".\\>") (incf count))
	      (forward-char 1))
	  (let ((cmd
		 (buffer-substring-no-properties
		  (point) (progn (when (zerop (skip-chars-forward "a-zA-Z@"))
				   (forward-char 1))
				 (point)))))
	    (when (member cmd tex-discount-args-cmds)
	      (skip-chars-forward "*")
	      (forward-comment (point-max))
	      (when (looking-at "\\[")
		(forward-sexp 1)
		(forward-comment (point-max)))
	      (if (not (looking-at "{"))
		  (forward-char 1)
		(forward-sexp 1))))))
      (message "%s words" count))))