Martin Gross <m-gr...@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


Reply via email to