emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Word-counting in a subtree
@ 2017-08-21 17:21 Jacopo De Simoi
  2017-08-21 18:09 ` Eric S Fraga
  0 siblings, 1 reply; 5+ messages in thread
From: Jacopo De Simoi @ 2017-08-21 17:21 UTC (permalink / raw)
  To: Org Mode List

tl;dr 

Is there a way to create a token on a subtree header that would count words in 
the subtree and [optionally] mark a maximum allowed number of words?

Long question

I am preparing an application and filling out many forms; each section of the 
form (that I rewrote as a subtree-header) has a maximum number of allowed 
words. 
It would be nice to have some kind of token to add to the subtree header which 
would count the number of words in the corresponding paragraph in a semi-
automatic way.  
As a bonus, one could also set a maximum number of words, and the token would 
switch to alert face when the maximum is reached. 

Is there a reasonable way to accomplish this? 

Thanks
 __J

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

* Re: Word-counting in a subtree
  2017-08-21 17:21 Word-counting in a subtree Jacopo De Simoi
@ 2017-08-21 18:09 ` Eric S Fraga
  2017-08-21 21:34   ` Jacopo De Simoi
  0 siblings, 1 reply; 5+ messages in thread
From: Eric S Fraga @ 2017-08-21 18:09 UTC (permalink / raw)
  To: Jacopo De Simoi; +Cc: Org Mode List

[-- Attachment #1: Type: text/plain, Size: 573 bytes --]

On Monday, 21 Aug 2017 at 13:21, Jacopo De Simoi wrote:
> tl;dr 
>
> Is there a way to create a token on a subtree header that would count words in 
> the subtree and [optionally] mark a maximum allowed number of words?

I cannot help you directly but what I do is: narrow the region to the
sub-tree while writing and then simply execute M-x count-words RET every
now and again.

There is a wc-mode which displays the number of words/chars in the
modeline but my modeline is already too busy for this...

-- 
: Eric S Fraga via Emacs 26.0.50, Org release_9.0.9-787-g197c58

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 194 bytes --]

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

* Re: Word-counting in a subtree
  2017-08-21 18:09 ` Eric S Fraga
@ 2017-08-21 21:34   ` Jacopo De Simoi
  2017-08-22  8:31     ` Simon Guest
  2017-08-22  9:24     ` Nicolas Goaziou
  0 siblings, 2 replies; 5+ messages in thread
From: Jacopo De Simoi @ 2017-08-21 21:34 UTC (permalink / raw)
  To: Org Mode List

On Monday, August 21, 2017 2:09:25 PM EDT Eric S Fraga wrote:
> On Monday, 21 Aug 2017 at 13:21, Jacopo De Simoi wrote:
> > tl;dr
> > 
> > Is there a way to create a token on a subtree header that would count
> > words in the subtree and [optionally] mark a maximum allowed number of
> > words?
> I cannot help you directly but what I do is: narrow the region to the
> sub-tree while writing and then simply execute M-x count-words RET every
> now and again.

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

> 
> There is a wc-mode which displays the number of words/chars in the
> modeline but my modeline is already too busy for this...

Indeed modelines are crowded these days…

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

* Re: Word-counting in a subtree
  2017-08-21 21:34   ` Jacopo De Simoi
@ 2017-08-22  8:31     ` Simon Guest
  2017-08-22  9:24     ` Nicolas Goaziou
  1 sibling, 0 replies; 5+ messages in thread
From: Simon Guest @ 2017-08-22  8:31 UTC (permalink / raw)
  To: Jacopo De Simoi; +Cc: Org Mode List

[-- Attachment #1: Type: text/plain, Size: 1226 bytes --]

[copy to list]

A few years ago I wrote org-wc.el, which is on MELPA.  It uses overlays to
display a word count against each heading.

Just install the package, and run org-wc-display.

https://github.com/tesujimath/org-wc

Hope that helps.

cheers,
Simon

On 22 August 2017 at 09:34, Jacopo De Simoi <wilderkde@gmail.com> wrote:

> On Monday, August 21, 2017 2:09:25 PM EDT Eric S Fraga wrote:
> > On Monday, 21 Aug 2017 at 13:21, Jacopo De Simoi wrote:
> > > tl;dr
> > >
> > > Is there a way to create a token on a subtree header that would count
> > > words in the subtree and [optionally] mark a maximum allowed number of
> > > words?
> > I cannot help you directly but what I do is: narrow the region to the
> > sub-tree while writing and then simply execute M-x count-words RET every
> > now and again.
>
> 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
>
> >
> > There is a wc-mode which displays the number of words/chars in the
> > modeline but my modeline is already too busy for this...
>
> Indeed modelines are crowded these days…
>
>
>

[-- Attachment #2: Type: text/html, Size: 2026 bytes --]

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

* Re: Word-counting in a subtree
  2017-08-21 21:34   ` Jacopo De Simoi
  2017-08-22  8:31     ` Simon Guest
@ 2017-08-22  9:24     ` Nicolas Goaziou
  1 sibling, 0 replies; 5+ messages in thread
From: Nicolas Goaziou @ 2017-08-22  9:24 UTC (permalink / raw)
  To: Jacopo De Simoi; +Cc: Org Mode List

Hello,

Jacopo De Simoi <wilderkde@gmail.com> 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

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

end of thread, other threads:[~2017-08-22  9:24 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-08-21 17:21 Word-counting in a subtree Jacopo De Simoi
2017-08-21 18:09 ` Eric S Fraga
2017-08-21 21:34   ` Jacopo De Simoi
2017-08-22  8:31     ` Simon Guest
2017-08-22  9:24     ` Nicolas Goaziou

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