emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Get counting of items
@ 2014-04-01  8:20 Martin Gross
  2014-04-01  9:00 ` Thorsten Jolitz
  0 siblings, 1 reply; 4+ messages in thread
From: Martin Gross @ 2014-04-01  8:20 UTC (permalink / raw)
  To: emacs-orgmode@gnu.org

Dear helpers,

I would like to get a counting of the first level items in a buffer
(or even better in a region).  Since I‘m not a programmer I tried
this, which doesn‘t work:

(defun org-items-counting ()
  "Print a message with the counting of the first level outline items
in the current buffer"
  (interactive)
  (save-excursion
     (goto-char (point-min))
     (message "Counting of first level outline items: %d"
(count-matches "\n* "))))

Probably there is a very much better approaching to this problem.
Could please somebody help me?

Thanks in advance,

Martin

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

* Re: Get counting of items
  2014-04-01  8:20 Martin Gross
@ 2014-04-01  9:00 ` Thorsten Jolitz
  2014-04-01 22:55   ` Richard Lawrence
  0 siblings, 1 reply; 4+ messages in thread
From: Thorsten Jolitz @ 2014-04-01  9:00 UTC (permalink / raw)
  To: emacs-orgmode

Martin Gross <m-gross@gmx.net> writes:

> Dear helpers,
>
> I would like to get a counting of the first level items in a buffer
> (or even better in a region).  Since I‘m not a programmer I tried
> this, which doesn‘t work:
>
> (defun org-items-counting ()
>   "Print a message with the counting of the first level outline items
> in the current buffer"
>   (interactive)
>   (save-excursion
>      (goto-char (point-min))
>      (message "Counting of first level outline items: %d"
> (count-matches "\n* "))))
>
> Probably there is a very much better approaching to this problem.
> Could please somebody help me?

This should work, although its a bit funny (you can wrap it in an
interactive command like above):

#+begin_src emacs-lisp
  (with-current-buffer "my.org"
    (eval (append (list '+)
                  (org-map-entries
                   (lambda () (if (eq (org-outline-level) 1) 1 0))))))
#+end_src

or rather something like this:

#+begin_src emacs-lisp
  (with-current-buffer "my.org"
    (let ((count 0))
      (goto-char (point-min))
      (while (re-search-forward "^\\* " nil 'noerror)
        (setq count (1+ count)))
      count))
#+end_src

-- 
cheers,
Thorsten

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

* Re: Get counting of items
  2014-04-01  9:00 ` Thorsten Jolitz
@ 2014-04-01 22:55   ` Richard Lawrence
  0 siblings, 0 replies; 4+ messages in thread
From: Richard Lawrence @ 2014-04-01 22:55 UTC (permalink / raw)
  To: emacs-orgmode

Thorsten Jolitz <tjolitz@gmail.com> writes:

> #+begin_src emacs-lisp
>   (with-current-buffer "my.org"
>     (eval (append (list '+)
>                   (org-map-entries
>                    (lambda () (if (eq (org-outline-level) 1) 1 0))))))
> #+end_src

Or, slightly more simply:

#+begin_src emacs-lisp
(with-current-buffer "my.org"
    (apply '+ (org-map-entries
                   (lambda () (if (eq (org-outline-level) 1) 1 0)))))
#+end_src

which you could wrap into a function like:

#+begin_src emacs-lisp
(defun count-toplevel-headlines ()
  "Count the top level headlines in the current buffer"
  (interactive)
  (message
    (format "Number of first level headlines: %s" 
      (save-excursion
        (apply '+ (org-map-entries
           (lambda () (if (eq (org-outline-level) 1) 1 0))))))))
#+end_src

Best,
Richard


(If possible, please encrypt your reply to me using my PGP key:
Key ID: CF6FA646
Fingerprint: 9969 43E1 CF6F A646.
See http://www.ocf.berkeley.edu/~rwl/encryption.html for more information.)

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

* Re: Get counting of items
@ 2014-04-03  8:39 Martin Gross
  0 siblings, 0 replies; 4+ messages in thread
From: Martin Gross @ 2014-04-03  8:39 UTC (permalink / raw)
  To: emacs-orgmode@gnu.org

Dear Thorsten

> Here is a generalised form:
>
> #+begin_src emacs-lisp
>   (defun count-org-items (&optional level operator match scope skip)
>     "Print a counting of outline items."
>     (interactive)
>     (let ((headline-level (or level 1)) ; 1-8
>           (op (or operator '=))) ; '>= '<= '> '<
>     (save-excursion
>       (message "Counting of level%s%d outline items (match=%s, scope=%s, skip=%s): %d"
>                op headline-level match scope skip
>                (eval (append (list '+)
>                              (org-map-entries
>                               `(lambda () (if (,op (org-outline-level) ,headline-level) 1 0))
>                               match scope skip)))))))
> #+end_src
>
> usage:
>
> ,----------------------------------------
> | (count-org-items 2 '<= "WAITING" 'file)
> `----------------------------------------
>
> result:
>
> ,------------------------------------------------------------------------------
> | "Counting of level<=2 outline items (match=WAITING, scope=file, skip=nil): 3"
> `------------------------------------------------------------------------------
>
> see C-h v org-map-entries for more info, its very powerfull. Use it
> with M-: (count-org-items ...) or write a more sophisticated
> (interactive) spec.
>

Very impressive. Thank you very much!

Martin

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

end of thread, other threads:[~2014-04-03 13:14 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-04-03  8:39 Get counting of items Martin Gross
  -- strict thread matches above, loose matches on Subject: below --
2014-04-01  8:20 Martin Gross
2014-04-01  9:00 ` Thorsten Jolitz
2014-04-01 22:55   ` Richard Lawrence

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