1

For certain articles, one does not need certain sections to be counted in the final word count. How to signal this in an Orgmode buffer? Perhaps there is a tag or similar feature to do this on the heading which I ignore.

2 Answers2

2

One option is to use the org-wc package. Then, you can exclude sections from counting by customizing org-wc-ignore-commented-trees and commenting out the sections using org-toggle-comment.

Of course, it is not so handy that such commented sections are also excluded from things like export. But, probably the behavior can quite easily be changed/improved via some small modifications in the org-wc source code (would make a nice contribution I think).

dalanicolai
  • 6,108
  • 7
  • 23
1

Nothing built-in, but you can easily write a function to do so, using org-map-entries. Let's say that headings that are tagged :wcignore: are to be ignored. Then what you need to do is visit each heading and if it is not ignored, count its words and remember the count. At the end, add all the counts together:


(defun my/count-words-in-heading ()
  (let* ((e (org-element-at-point))
         (begin (org-element-property :begin e))
         (end (org-element-property :end e)))
    (count-words begin end)))

(defun my/count-words-in-not-ignored-headings ()
  (apply #'+ (org-map-entries #'my/count-words-in-heading
                              "-wcignore"'file)))

Then evaluate it with M-: (my/count-words-in-not-ignored-headings).

By default, tag inheritance is enabled, so if you tag a heading, then all its subheadings will inherit the tag (and in this case, they will be ignored as well). This is probably what you want in most cases, but if you want finer control, you can turn off tag inheritance temporarily for the duration of the subsequent evaluation:

M-: (let ((org-use-tag-inheritance nil) (my/count-words-in-not-ignored-heading))

That will count untagged subheadings even if the top level heading is tagged.

EDIT: In response to the comment, you can make the function into a command and bind it to a key:

(defun my/count-words-in-not-ignored-headings ()
    (interactive)
    ... <same as before> ...

(define-key org-mode-map (kbd "C-c z") #'my/count-words-in-not-ignored-headings)

Adjust key to availability and taste.

NickD
  • 27,023
  • 3
  • 23
  • 42
  • Is it possible to have this as a interactive function not needing to evaluate it with M-:? In any case, I get "eval-expression: Symbol’s value as variable is void: my/count-words-in-not-ignored-headings" – Emmanuel Goldstein Mar 25 '23 at 06:27
  • Edited the answer to add the (bog-standard) way to make it into a command and bind it to a key. – NickD Mar 25 '23 at 14:36
  • 1
    That's because you are evaluating it as a *variable*: `M-: count-words-in-not-ignored-headings`. What you want to do is *call the function*: `M-: (my/count-words-in-not-ignored-headings)`. Note the parens. – NickD Mar 25 '23 at 14:42