From: Paul Sexton <psexton@xnet.co.nz>
To: emacs-orgmode@gnu.org
Subject: Context-sensitive word count in org mode (elisp)
Date: Wed, 16 Feb 2011 03:51:23 +0000 (UTC) [thread overview]
Message-ID: <loom.20110216T044140-839@post.gmane.org> (raw)
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")))))
next reply other threads:[~2011-02-16 3:51 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-02-16 3:51 Paul Sexton [this message]
2011-02-16 9:12 ` Context-sensitive word count in org mode (elisp) Christian Moe
2011-02-16 9:47 ` Dan Davison
2011-02-16 9:45 ` Christian Moe
2011-02-16 20:34 ` Paul Sexton
2011-02-17 10:02 ` Christian Moe
2011-02-17 18:57 ` Eric Schulte
2011-02-16 10:14 ` Bastien
2011-02-16 18:15 ` Samuel Wales
2011-02-16 13:03 ` Joost Kremers
2011-02-16 23:28 ` Paul Sexton
2011-02-17 16:50 ` Samuel Wales
2011-02-17 18:55 ` Paul Sexton
2011-03-27 19:40 ` [Orgmode] " Samuel Wales
2011-02-18 14:34 ` Bastien
2011-02-20 21:49 ` Paul Sexton
2011-02-21 23:30 ` Samuel Wales
[not found] ` <4D601314.8000701@xnet.co.nz>
2011-02-22 11:28 ` Bastien
-- strict thread matches above, loose matches on Subject: below --
2011-02-16 16:22 Benjamin Beckwith
2011-02-16 23:31 ` Paul Sexton
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.orgmode.org/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=loom.20110216T044140-839@post.gmane.org \
--to=psexton@xnet.co.nz \
--cc=emacs-orgmode@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).