emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: "François Pinard" <pinard@iro.umontreal.ca>
To: emacs-orgmode@gnu.org
Subject: Re: viewing number of nested headlines
Date: Tue, 05 Feb 2013 07:43:22 -0500	[thread overview]
Message-ID: <86d2wfkk2d.fsf@iro.umontreal.ca> (raw)
In-Reply-To: <87y5f3vycq.fsf@bzg.ath.cx> (Bastien's message of "Tue, 05 Feb 2013 11:39:17 +0100")

Bastien <bzg@altern.org> writes:

> 42 147 <aeuster@gmail.com> writes:

>> Is there a way to see how many nested headlines there are within a
>> higher-level headline (not necessarily top-level)?

> This is a nice feature of VimOrganizer but no, there is no way to do
> this with Org for now.

Hi!

I'm not sure the following code responds to your wish, maybe it would
help enough?  Surely, it is often useful to me.  You might have to
adapt the first few lines to your local installation or taste, however.



(defvar fp-org-distribution "~/emacs/_/org-mode")
(add-to-list 'load-path (concat fp-org-distribution "/lisp"))
(add-to-list 'load-path (concat fp-org-distribution "/contrib/lisp" ) t)

(require 'org)

(defface fp-warn-face
  '((((class color) (background light))
     (:foreground "chocolate2" :weight bold))
    (((class color) (background dark))
     (:foreground "chocolate1" :weight bold)))
  "Face for own highlights.")

(define-key org-mode-map "\C-cow" 'fp-org-weight-display)





(defun fp-org-weight-display ()
  "Show header weights in the entire buffer.

Use \\[fp-org-weight-remove-overlays] to remove the header weights."
  (interactive)
  (fp-org-weight-remove-overlays)
  (save-excursion
    (goto-char (point-min))
    (outline-next-visible-heading 1)
    (while (not (eobp))
      (save-excursion
        (fp-org-weight-put-overlay (fp-org-weights-at-point)
                                   (funcall outline-level)))
      (outline-next-visible-heading 1))
    ;; Arrange to remove the overlays upon next change.
    (when org-remove-highlights-with-change
      (org-add-hook 'before-change-functions 'fp-org-weight-remove-overlays
                    nil 'local))))

(defvar fp-org-weight-overlays nil)
(make-variable-buffer-local 'fp-org-weight-overlays)

(defun fp-org-weight-put-overlay (weights &optional level)
  "Put an overlays on the current line, displaying WEIGHTS.
If LEVEL is given, prefix weights with a corresponding number of stars.
This creates a new overlay and stores it in `fp-org-weight-overlays', so that it
will be easy to remove."
  (let* ((h (car weights))
         (p (cdr weights))
         (c 50)
         (l (if level (org-get-valid-level level 0) 0))
	 (off 0))
    (org-move-to-column c)
    (unless (eolp) (skip-chars-backward "^ \t"))
    (skip-chars-backward " \t")
    (let* ((ov (make-overlay (1- (point)) (point-at-eol)))
           (d (+ off (max 0 (- c (current-column) 2))))
           (tx (concat (buffer-substring (1- (point)) (point))
                       " "
                       (make-string d ? )
                       " "
                       (org-add-props (format "%s %3s %6s "
                                              (make-string l ?*)
                                              (if (zerop p) "" p)
                                              (if (zerop h) "" (format "(%s h)" h)))
                           (list 'face 'fp-warn-face))
                       "")))
      (if (not (featurep 'xemacs))
          (overlay-put ov 'display tx)
        (overlay-put ov 'invisible t)
        (overlay-put ov 'end-glyph (make-glyph tx)))
      (push ov fp-org-weight-overlays))))

(defun fp-org-weight-remove-overlays (&optional beg end noremove)
  "Remove the occur highlights from the buffer.
BEG and END are ignored.  If NOREMOVE is nil, remove this function
from the `before-change-functions' in the current buffer."
  (interactive)
  (unless org-inhibit-highlight-removal
    (mapc 'delete-overlay fp-org-weight-overlays)
    (setq fp-org-weight-overlays nil)
    (unless noremove
      (remove-hook 'before-change-functions
		   'fp-org-weight-remove-overlays 'local))))

;; Compliment of Nicolas Goaziou <n.goaziou@gmail.com>, 2012-02-26
(defun fp-org-weights-at-point ()
  "Return cons of number of subtrees and paragraphs in the subtree at point.
Paragraphs (also encompasses equivalent structures)."
  (org-with-wide-buffer
   (org-narrow-to-subtree)
   (let ((tree (org-element-parse-buffer 'element)) (num-hl 0) (num-el 0))
     (org-element-map tree 'headline (lambda (hl) (incf num-hl)))
     (org-element-map
      tree '(paragraph table verse-block quote-block src-block example-block)
      (lambda (el) (incf num-el)))
     (cons (1- num-hl) num-el))))

  reply	other threads:[~2013-02-05 12:43 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-02-04 23:59 viewing number of nested headlines 42 147
2013-02-05 10:39 ` Bastien
2013-02-05 12:43   ` François Pinard [this message]
2013-02-05 18:34     ` Samuel Wales
2013-02-05 13:25   ` Sebastien Vauban
2013-02-05 14:08     ` François Pinard
2013-02-05 20:17   ` 42 147
2013-02-05 21:03     ` Bastien
2013-02-05 22:07       ` 42 147
2013-02-06 20:50         ` Achim Gratz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=86d2wfkk2d.fsf@iro.umontreal.ca \
    --to=pinard@iro.umontreal.ca \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).