emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Context-sensitive word count in org mode (elisp)
@ 2011-02-16  3:51 Paul Sexton
  2011-02-16  9:12 ` Christian Moe
                   ` (4 more replies)
  0 siblings, 5 replies; 20+ messages in thread
From: Paul Sexton @ 2011-02-16  3:51 UTC (permalink / raw)
  To: emacs-orgmode

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")))))

^ permalink raw reply	[flat|nested] 20+ messages in thread
* Re: Context-sensitive word count in org mode (elisp)
@ 2011-02-16 16:22 Benjamin Beckwith
  2011-02-16 23:31 ` Paul Sexton
  0 siblings, 1 reply; 20+ messages in thread
From: Benjamin Beckwith @ 2011-02-16 16:22 UTC (permalink / raw)
  To: Paul Sexton, emacs-orgmode

Hi Paul,

I have a small emacs project that creates a word-count mode and function
for use.  It currently does not ignore sections like your proposal, but I think
that would be interesting functionality.

Mine does keep a running tally of adds/delete in the mode line.  It also
lets you set a goal and provides a visual indication when that goal is
met -- I believe that this feature may be of interest to you.

You can find it at:
https://github.com/bnbeckwith/wc-mode

Regards,
Ben

^ permalink raw reply	[flat|nested] 20+ messages in thread

end of thread, other threads:[~2011-03-27 19:40 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-02-16  3:51 Context-sensitive word count in org mode (elisp) Paul Sexton
2011-02-16  9:12 ` 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

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).