From mboxrd@z Thu Jan 1 00:00:00 1970 From: Christian Moe Subject: Re: Context-sensitive word count in org mode (elisp) Date: Wed, 16 Feb 2011 10:45:12 +0100 Message-ID: <4D5B9CA8.5070100@christianmoe.com> References: Reply-To: mail@christianmoe.com Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Return-path: Received: from [140.186.70.92] (port=49735 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Ppdtl-0003wW-Io for emacs-orgmode@gnu.org; Wed, 16 Feb 2011 04:42:14 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Ppdtk-00044W-FB for emacs-orgmode@gnu.org; Wed, 16 Feb 2011 04:42:13 -0500 Received: from mars.hitrost.net ([91.185.211.18]:23181) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Ppdtk-00044J-2H for emacs-orgmode@gnu.org; Wed, 16 Feb 2011 04:42:12 -0500 In-Reply-To: List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: Paul Sexton Cc: emacs-orgmode@gnu.org Forgot to add the code. #+begin_src emacs-lisp ;; Adapted from code posted by Paul Sexton <2011-02-16 Wed 4:51am> ;; - Everything now contained in one function ;; - Will count correct number of words inside Latex macro (defun org-word-count (beg end) (interactive "r") (unless mark-active (setf beg (point-min) end (point-max))) (let ((wc 0) (latex-macro-regexp "\\\\[A-Za-z]+\\(\\[[^]]*\\]\\| \\){\\([^}]*\\)}")) ; CHANGED (save-excursion (goto-char beg) (while (< (point) end) (re-search-forward "\\w+\\W*") (cond ((or (org-in-commented-line) (org-at-table-p)) ; CHANGED nil) ((looking-at org-any-link-re) (goto-char (match-end 0))) ((save-excursion (backward-char) (looking-at latex-macro-regexp)) (goto-char (match-beginning 2)) ; CHANGED (setf wc (+ 2 wc))) (t (incf wc))))) (message (format "%d words in %s." wc (if mark-active "region" "buffer"))))) #+end_src Yours, Christian On 2/16/11 4:51 AM, Paul Sexton wrote: > 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 >