emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Orgmode] Context-sensitive word count in org mode (elisp)


From: Christian Moe
Subject: Re: [Orgmode] Context-sensitive word count in org mode (elisp)
Date: Wed, 16 Feb 2011 10:45:12 +0100
User-agent: Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10.5; en-US; rv:1.9.2.13) Gecko/20101207 Thunderbird/3.1.7

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.
address@hidden
http://lists.gnu.org/mailman/listinfo/emacs-orgmode





reply via email to

[Prev in Thread] Current Thread [Next in Thread]