emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* New org-count-words function
@ 2018-06-12 19:58 Adam Porter
  2018-06-15 16:13 ` Grant Rettke
  0 siblings, 1 reply; 2+ messages in thread
From: Adam Porter @ 2018-06-12 19:58 UTC (permalink / raw)
  To: emacs-orgmode

Hi friends,

Here's a new function that counts lines, words, and characters in the
region or the subtree at point.  It prints a message like `count-words`
does, like:

    Subtree "Heading" has 7 line, 5 words, and 28 characters.

Note that it does *not* count words in heading lines, planning lines,
and drawers, so it gives a more useful count for the "prose" in the
subtree.

Here's the code:

#+BEGIN_SRC elisp
(defun ap/org-count-words ()
  "If region is active, count words in it; otherwise count words in current subtree."
  (interactive)
  (if (use-region-p)
      (funcall-interactively #'count-words-region (region-beginning) (region-end))
    (org-with-wide-buffer
     (cl-loop for (lines words characters)
              in (org-map-entries
                  (lambda ()
                    (ap/org-forward-to-entry-content 'unsafe)
                    (let ((end (org-entry-end-position)))
                      (list (count-lines (point) end)
                            (count-words (point) end)
                            (- end (point)))))
                  nil 'tree)
              sum lines into total-lines
              sum words into total-words
              sum characters into total-characters
              finally do (message "Subtree \"%s\" has %s lines, %s words, and %s characters."
                                  (org-get-heading t t)
                                  total-lines total-words
                                  total-characters)))))
#+END_SRC

It requires this supporting function:

#+BEGIN_SRC elisp
(defun ap/org-forward-to-entry-content (&optional unsafe)
  "Skip headline, planning line, and all drawers in current entry.
    If UNSAFE is non-nil, assume point is on headline."
  (unless unsafe
    ;; To improve performance in loops (e.g. with `org-map-entries')
    (org-back-to-heading))
  (cl-loop for element = (org-element-at-point)
           for pos = (pcase element
                       (`(headline . ,_)
                        (org-element-property :contents-begin element))
                       (`(,(or 'planning 'property-drawer 'drawer) . ,_)
                        (org-element-property :end element)))
           while pos
           do (goto-char pos)))
#+END_SRC

I think it's an improvement over the functions I've seen that do this.
Hope someone finds it useful.

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

* Re: New org-count-words function
  2018-06-12 19:58 New org-count-words function Adam Porter
@ 2018-06-15 16:13 ` Grant Rettke
  0 siblings, 0 replies; 2+ messages in thread
From: Grant Rettke @ 2018-06-15 16:13 UTC (permalink / raw)
  To: Adam Porter; +Cc: Org-mode

On Tue, Jun 12, 2018 at 2:58 PM, Adam Porter <adam@alphapapa.net> wrote:
> I think it's an improvement over the functions I've seen that do this.
> Hope someone finds it useful.

Thank you that is very nice.

Got me thinking that it would be fun to write some code that would
report file statistics by element.

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

end of thread, other threads:[~2018-06-15 16:13 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-06-12 19:58 New org-count-words function Adam Porter
2018-06-15 16:13 ` Grant Rettke

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