From mboxrd@z Thu Jan 1 00:00:00 1970 From: Paul Sexton Subject: Re: Context-sensitive word count in org mode (elisp) Date: Wed, 16 Feb 2011 20:34:15 +0000 (UTC) Message-ID: References: <4D5B9CA8.5070100@christianmoe.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Return-path: Received: from [140.186.70.92] (port=44411 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Ppo55-0001ow-Js for emacs-orgmode@gnu.org; Wed, 16 Feb 2011 15:34:36 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Ppo54-0001HI-DI for emacs-orgmode@gnu.org; Wed, 16 Feb 2011 15:34:35 -0500 Received: from lo.gmane.org ([80.91.229.12]:38595) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Ppo53-0001H1-VV for emacs-orgmode@gnu.org; Wed, 16 Feb 2011 15:34:34 -0500 Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1Ppo4x-0004lf-0l for emacs-orgmode@gnu.org; Wed, 16 Feb 2011 21:34:32 +0100 Received: from rp.young.med.auckland.ac.nz ([130.216.140.20]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Wed, 16 Feb 2011 21:34:27 +0100 Received: from psexton by rp.young.med.auckland.ac.nz with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Wed, 16 Feb 2011 21:34:27 +0100 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: emacs-orgmode@gnu.org Christian Moe christianmoe.com> writes: > > 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 > Thanks, I wasn't aware of those pre-existing functions. I don't agree with changing '(match-end 0)' to '(match-beginning 2)' however. For most latex macros, I don't want to count the words inside the macro's arguments. For example, I don't want the next of footnotes to be included in the word count. However others differ, and there will always be cases where one DOES want to count the macro arguments - so maybe org-word-count should do this optionally. Paul