Re: [O] v3, with support for narrowing
At Sat, 14 May 2011 15:48:10 -0700, Samuel Wales wrote: > What I meant was, if desired you can make the call to the word count > function be (funcall variable) to allow substitution of that function. > > (Actually I'm finding that using w3m to count exact words is pretty fast.) Hi Samuel, OK, I see now. That's clearly a good idea. Next time I fiddle with this code (don't know when that might be), I'll try to remember that. Otherwise anyone else please feel free to do what is necessary. cheers, Simon
Re: [O] v3, with support for narrowing
Hi Simon, On 2011-04-28, Simon Guest wrote: > I'm not that interested in higher fidelity counting. I'm especially > interested in speed. What I meant was, if desired you can make the call to the word count function be (funcall variable) to allow substitution of that function. (Actually I'm finding that using w3m to count exact words is pretty fast.) Samuel -- The Kafka Pandemic: http://thekafkapandemic.blogspot.com/2010/12/welcome-to-kafka-pandemic-two-forces_9182.html I support the Whittemore-Peterson Institute (WPI) === I want to see the original (pre-hold) Lo et al. 2010 NIH/FDA/Harvard MRV paper.
Re: [O] v3, with support for narrowing
At Fri, 29 Apr 2011 10:56:51 -0400, Nick Dokos wrote: > Indeed: it would require a bit of refactoring of Simon's code to provide the > function(s) to apply to each entry, and changes to the top level functions to > use the mapping API instead of looping explicitly. That sounds like a good idea. I may at some stage want to exclude counting certain trees, and then I may have another look at this if someone else hasn't already done it. But for now, time pressure dictates I stop hacking on my word count function. cheers, Simon
Re: [O] v3, with support for narrowing
Eric Abrahamsen wrote: > > The mapping API allows you to walk through the entries, filter the ones > > you want and apply a function on each remaining entry. So cribbing heavily > > from Simon's code, the following should count all the entries with tag > > "foo": > > Nice! Seems like the mapping API could be used as the basis for this > whole functionality. > Indeed: it would require a bit of refactoring of Simon's code to provide the function(s) to apply to each entry, and changes to the top level functions to use the mapping API instead of looping explicitly. Nick
Re: [O] v3, with support for narrowing
On Fri, Apr 29 2011, Nick Dokos wrote: > Eric Abrahamsen wrote: > >> On Fri, Apr 29 2011, Simon Guest wrote: >> >> > At Thu, 28 Apr 2011 15:47:07 -0700, >> > Samuel Wales wrote: >> >> Is it possible to make it so that you can show the overlays for just a >> >> subtree or region instead of the entire buffer? >> > >> > Hi Samuel, >> > >> > Good idea! So I just fixed it to handle narrowing properly, so narrow >> > to your region or subtree of interest first, and then count as usual. >> > >> > Attached v3 which does this. I'm not normally this responsive, you >> > just caught me at a good time. ;-) >> >> Is this still a good time? Because what I'd really like is >> inclusion/exclusion tags, so that I can mark some subtrees to count, and >> others to ignore. I tried doing this last night, based on other parts of >> the export code, and my elisp failed. Perhaps just exclusion tags? >> Here's hoping you still have a bit of time/interest for improvements! >> >> (I'm translating a novel, not writing one, and want to keep the original >> text out of the count. Plus I've got a bunch of research/notes sections >> that should be excluded.) >> >> If you're out of time, I'll have another whack at it, and maybe bring my >> broken efforts to the list. >> > > The mapping API allows you to walk through the entries, filter the ones > you want and apply a function on each remaining entry. So cribbing heavily > from Simon's code, the following should count all the entries with tag "foo": Nice! Seems like the mapping API could be used as the basis for this whole functionality. Not that I'm quite volunteering yet, I'm still working out how to make all this work… Thanks, Eric
Re: [O] v3, with support for narrowing
Eric Abrahamsen wrote: > On Fri, Apr 29 2011, Simon Guest wrote: > > > At Thu, 28 Apr 2011 15:47:07 -0700, > > Samuel Wales wrote: > >> Is it possible to make it so that you can show the overlays for just a > >> subtree or region instead of the entire buffer? > > > > Hi Samuel, > > > > Good idea! So I just fixed it to handle narrowing properly, so narrow > > to your region or subtree of interest first, and then count as usual. > > > > Attached v3 which does this. I'm not normally this responsive, you > > just caught me at a good time. ;-) > > Is this still a good time? Because what I'd really like is > inclusion/exclusion tags, so that I can mark some subtrees to count, and > others to ignore. I tried doing this last night, based on other parts of > the export code, and my elisp failed. Perhaps just exclusion tags? > Here's hoping you still have a bit of time/interest for improvements! > > (I'm translating a novel, not writing one, and want to keep the original > text out of the count. Plus I've got a bunch of research/notes sections > that should be excluded.) > > If you're out of time, I'll have another whack at it, and maybe bring my > broken efforts to the list. > The mapping API allows you to walk through the entries, filter the ones you want and apply a function on each remaining entry. So cribbing heavily from Simon's code, the following should count all the entries with tag "foo": --8<---cut here---start->8--- (defun show-count () (let ((p (point)) wc) (when (setq wc (get-text-property p :org-wc)) (org-wc-put-overlay wc (funcall outline-level))) (when org-remove-highlights-with-change (org-add-hook 'before-change-functions 'org-wc-remove-overlays nil 'local (defun count-foo () (interactive) (let (bmp (buffer-modified-p)) (org-wc-count-subtrees) (org-map-entries 'show-count "+foo" 'file) (set-buffer-modified-p bmp))) --8<---cut here---end--->8--- Nick
Re: [O] v3, with support for narrowing
On Fri, Apr 29 2011, Simon Guest wrote: > At Thu, 28 Apr 2011 15:47:07 -0700, > Samuel Wales wrote: >> Is it possible to make it so that you can show the overlays for just a >> subtree or region instead of the entire buffer? > > Hi Samuel, > > Good idea! So I just fixed it to handle narrowing properly, so narrow > to your region or subtree of interest first, and then count as usual. > > Attached v3 which does this. I'm not normally this responsive, you > just caught me at a good time. ;-) Is this still a good time? Because what I'd really like is inclusion/exclusion tags, so that I can mark some subtrees to count, and others to ignore. I tried doing this last night, based on other parts of the export code, and my elisp failed. Perhaps just exclusion tags? Here's hoping you still have a bit of time/interest for improvements! (I'm translating a novel, not writing one, and want to keep the original text out of the count. Plus I've got a bunch of research/notes sections that should be excluded.) If you're out of time, I'll have another whack at it, and maybe bring my broken efforts to the list. Thanks, Eric
[O] v3, with support for narrowing
At Thu, 28 Apr 2011 15:47:07 -0700, Samuel Wales wrote: > Is it possible to make it so that you can show the overlays for just a > subtree or region instead of the entire buffer? Hi Samuel, Good idea! So I just fixed it to handle narrowing properly, so narrow to your region or subtree of interest first, and then count as usual. Attached v3 which does this. I'm not normally this responsive, you just caught me at a good time. ;-) > Also, I have a plugin-compatible backend that will get you the /exact/ > word count (uses w3m). If you're interested you can have it be an > alternate backend. I'm not that interested in higher fidelity counting. I'm especially interested in speed. cheers, Simon ;; org-wc.el ;; ;; Count words in org mode trees. ;; Shows word count per heading line, summed over sub-headings. ;; Aims to be fast, so doesn't check carefully what it's counting. ;-) ;; ;; Simon Guest, 23/4/11 ;; ;; Implementation based on: ;; - Paul Sexton's word count posted on org-mode mailing list 21/2/11. ;; - clock overlays ;; ;; v2 ;; 29/4/11 ;; Don't modify buffer, and fixed handling of empty sections. ;; ;; v3 ;; 29/4/11 ;; Handle narrowing correctly, so partial word count works on narrowed regions. (defun org-in-heading-line () "Is point in a line starting with `*'?" (equal (char-after (point-at-bol)) ?*)) (defun org-word-count (beg end) "Report the number of words in the Org mode buffer or selected region." (interactive "r") (unless mark-active (setf beg (point-min) end (point-max))) (let ((wc (org-word-count-aux beg end))) (message (format "%d words in %s." wc (if mark-active "region" "buffer") (defun org-word-count-aux (beg end) "Report the number of words in the selected region. Ignores: heading lines, blocks, comments, drawers. LaTeX macros are counted as 1 word." (let ((wc 0) (block-begin-re "^#\\\+BEGIN") (block-end-re "^#\\+END") (latex-macro-regexp "[A-Za-z]+\\(\\[[^]]*\\]\\|\\){\\([^}]*\\)}") (drawers-re (concat "^[ \t]*:\\(" (mapconcat 'regexp-quote org-drawers "\\|") "\\):[ \t]*$")) (drawers-end-re "^[ \t]*:END:")) (save-excursion (goto-char beg) (while (< (point) end) (cond ;; Ignore heading lines. ((org-in-heading-line) (forward-line)) ;; Ignore blocks. ((looking-at block-begin-re) (re-search-forward block-end-re)) ;; Ignore comments. ((org-in-commented-line) (forward-line)) ;; Ignore drawers. ((looking-at drawers-re) (re-search-forward drawers-end-re nil t)) ;; Count latex macros as 1 word, ignoring their arguments. ((save-excursion (backward-char) (looking-at latex-macro-regexp)) (goto-char (match-end 0)) (setf wc (+ 2 wc))) (t (progn (and (re-search-forward "\\w+\\W*" end 'skip) (incf wc))) wc)) (defun org-wc-count-subtrees () "Count words in each subtree, putting result as the property :org-wc on that heading." (interactive) (remove-text-properties (point-min) (point-max) '(:org-wc t)) (save-excursion (goto-char (point-max)) (while (outline-previous-heading) (save-restriction (org-narrow-to-subtree) (let ((wc (org-word-count-aux (point-min) (point-max (put-text-property (point) (point-at-eol) :org-wc wc) (goto-char (point-min))) (defun org-wc-display (beg end total-only) "Show subtree word counts in the entire buffer. With prefix argument, only show the total wordcount for the buffer or region in the echo area. Use \\[org-wc-remove-overlays] to remove the subtree times. Ignores: heading lines, blocks, comments, drawers. LaTeX macros are counted as 1 word." (interactive "r\nP") (org-wc-remove-overlays) (unless total-only (let ((bmp (buffer-modified-p)) wc p) (org-wc-count-subtrees) (save-excursion (goto-char (point-min)) (while (or (and (equal (setq p (point)) (point-min)) (get-text-property p :org-wc)) (setq p (next-single-property-change (point) :org-wc))) (goto-char p) (when (setq wc (get-text-property p :org-wc)) (org-wc-put-overlay wc (funcall outline-level ;; Arrange to remove the overlays upon next change. (when org-remove-highlights-with-change (org-add-hook 'before-change-functions 'org-wc-remove-overlays nil 'local))) (set-buffer-modified-p bmp))) (if mark-active (org-word-count beg end) (org-word-count (point-min) (point-max (defvar org-wc-overlays nil) (make-variable-buffer-loc