emacs-orgmode
[Top][All Lists]
Advanced

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

Re: [O] Word-counting in a subtree


From: Nicolas Goaziou
Subject: Re: [O] Word-counting in a subtree
Date: Tue, 22 Aug 2017 11:24:03 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.2 (gnu/linux)

Hello,

Jacopo De Simoi <address@hidden> writes:

> this has the drawback that it also counts words in the header line; except 
> this, could work as a temporary solution, but I'd still like to cook up some 
> tag integrated with org

What about something like this (untested):

  (defun my-count-words (&optional ignore-headings beg end)
    (save-restriction
      (narrow-to-region (or beg (point-min)) (or end (point-max)))
      (let ((count 0)
            (count-words-in-string
             (lambda (s) (length (split-string s "[^[:word:]]" t)))))
        (org-element-map
            (org-element-parse-buffer)
            '(clock code entity example-block fixed-width footnote-reference
                    inline-src-block latex-fragment latex-environment
                    link macro plain-text src-block timestamp verbatim)
          (lambda (datum)
            (pcase (org-element-type datum)
              (`plain-text
               (unless (and ignore-headings
                            (memq (org-element-type
                                   (org-element-property :parent datum))
                                  '(headline inlinetask)))
                 (cl-incf count (funcall count-words-in-string datum))))
              ;; Count contents in words.
              ((or `code `example-block `fixed-width `latex-environment
                   `latex-fragment `src-block `verbatim)
               (cl-incf count (funcall count-words-in-string
                                       (org-element-property :value datum))))
              ;; Object counting as a single word.
              (`(or `entity `inline-src-block `macro `timestamp)
               (cl-incf count))
              (`link
               ;; Links with contents are handled recursively by
               ;; `org-element-map'.
               (unless (org-element-contents datum) (cl-incf count)))
              (`footnote-reference
               (pcase (org-footnote-get-definition
                       (org-element-property :label datum))
                 (`(,_ ,_ ,_ ,definition)
                  (cl-incf count (funcall count-words-in-string 
definition)))))))
          ;; Do not blindly count footnote definitions and inline
          ;; references contents. We want to limit ourselves to
          ;; definitions actually referenced in the part of the document
          ;; we're parsing.
          nil nil '(footnote-definition footnote-reference) t)
        ;; Return words count.
        count)))

Note that some parts are really arbitrary (e.g., how to count a src
block...).

Regards,

-- 
Nicolas Goaziou



reply via email to

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