From mboxrd@z Thu Jan 1 00:00:00 1970 From: stardiviner Subject: Re: [RFC] Org Num library Date: Mon, 04 Feb 2019 14:35:49 +0800 Message-ID: <87h8dkvz5m.fsf@gmail.com> References: <87r2fftxq7.fsf@nicolasgoaziou.fr> <84k1ikludt.fsf@gmail.com> <87pns9b44k.fsf@nicolasgoaziou.fr> Reply-To: numbchild@gmail.com Mime-Version: 1.0 Content-Type: text/plain Return-path: Received: from eggs.gnu.org ([209.51.188.92]:60210) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gqXvH-00016N-R6 for emacs-orgmode@gnu.org; Mon, 04 Feb 2019 01:40:05 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gqXv3-00015Z-5n for emacs-orgmode@gnu.org; Mon, 04 Feb 2019 01:39:55 -0500 Received: from [115.236.255.229] (port=24897 helo=dark.localdomain) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gqXux-0000vT-4h for emacs-orgmode@gnu.org; Mon, 04 Feb 2019 01:39:46 -0500 In-reply-to: <87pns9b44k.fsf@nicolasgoaziou.fr> List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: emacs-orgmode@gnu.org Cc: Marco Wahl Nicolas Goaziou writes: > Hello, > > Marco Wahl writes: > >> Just one idea came to my mind. What about adding an option to restart >> the numbering at 1 for the first heading in the case when narrowing is >> in effect? > > AFAICT, detecting narrowing changes requires using > `post-command-hook'. Add more things into `post-command-hook' is not a good idea. Org will really getting slow! I saw many Org Mode newbie complain about Org Mode slow on big Org file. Myself has this experience often. I have to disable some features so speedup Org rendering. Well, I think this is just my personal opinion. I respect author's decision. > Therefore an update to numbering, when such change happens, might get in > the way, e.g., if it slows down input. > > Anyway, here is a POC, if anyone wants to test it. > > Regards, > > -- > Nicolas Goaziou > From 1659484513fd4badb1fca867810a87756d1d46b5 Mon Sep 17 00:00:00 2001 > From: Nicolas Goaziou > Date: Sun, 3 Feb 2019 10:33:47 +0100 > Subject: [PATCH] org-num: Add an option to start numbering according to > narrowing > > * lisp/org-num.el (org-num-visible-only): > (org-num--start): New variables. > (org-num--current-numbering): Change signature. > (org-num--number-region): > (org-num--update): Apply signature change. > (org-num--check-narrowing): New function. > (org-num-mode): Add, or remove, new function in `post-command-hook'. > --- > lisp/org-num.el | 80 ++++++++++++++++++++++++++++++++----------------- > 1 file changed, 53 insertions(+), 27 deletions(-) > > diff --git a/lisp/org-num.el b/lisp/org-num.el > index f062526a0..de0a4ad48 100644 > --- a/lisp/org-num.el > +++ b/lisp/org-num.el > @@ -135,6 +135,12 @@ control tag inheritance." > :type 'boolean > :safe #'booleanp) > > +(defcustom org-num-visible-only nil > + "Non-nil restricts numbering to the visible part of the buffer." > + :group 'org-appearance > + :type 'boolean > + :safe #'booleanp) > + > > ;;; Internal Variables > > @@ -154,6 +160,9 @@ inheritance of no-numbering attributes.") > A numbering is a list of integers, in reverse order. So numbering > for headline \"1.2.3\" is (3 2 1).") > > +(defvar-local org-num--start 1 > + "Buffer position where headlines start to be numbered.") > + > (defvar-local org-num--missing-overlay nil > "Buffer position signaling a headline without an overlay.") > > @@ -255,16 +264,22 @@ otherwise." > (org-entry-get (point) "UNNUMBERED") > t)))) > > -(defun org-num--current-numbering (level skip) > +(defun org-num--current-numbering (pos level skip) > "Return numbering for current headline. > -LEVEL is headline's level, and SKIP its skip value. Return nil > -if headline should be skipped." > + > +POS is headline's beginning position, LEVEL its level, and SKIP > +its skip value. > + > +Numbering starts from `org-num--start' position. Return nil if > +headline should not be numbered." > (cond > ;; Skipped by inheritance. > ((and org-num--skip-level (> level org-num--skip-level)) nil) > ;; Skipped by a non-nil skip value; set `org-num--skip-level' > ;; to skip the whole sub-tree later on. > (skip (setq org-num--skip-level level) nil) > + ;; Skipped because it is before visible part of the buffer. > + ((and org-num-visible-only (< pos org-num--start)) nil) > (t > (setq org-num--skip-level nil) > ;; Compute next numbering, and update `org-num--numbering'. > @@ -296,10 +311,12 @@ first." > (let ((regexp (org-num--headline-regexp)) > (new nil)) > (while (re-search-forward regexp end t) > - (let* ((level (org-reduced-level > - (- (match-end 0) (match-beginning 0) 1))) > - (skip (org-num--skip-value)) > - (numbering (org-num--current-numbering level skip))) > + (let* ((pos (line-beginning-position)) > + (level (org-reduced-level > + (- (match-end 0) (match-beginning 0) 1))) > + (skip (org-num--skip-value)) > + (numbering > + (org-num--current-numbering pos level skip))) > ;; Apply numbering to current headline. Store overlay for > ;; the return value. > (push (org-num--make-overlay numbering level skip) > @@ -324,26 +341,26 @@ missing overlays to that list." > ;; last known overlay, make sure to parse the buffer between > ;; these two overlays. > ((org-num--valid-overlay-p overlay) > - (let ((next (overlay-start overlay)) > - (last (and new-overlays (overlay-start (car new-overlays))))) > - (cond > - ((null org-num--missing-overlay)) > - ((> org-num--missing-overlay next)) > - ((or (null last) (> org-num--missing-overlay last)) > - (setq org-num--missing-overlay nil) > - (setq new-overlays (nconc (org-num--number-region last next) > - new-overlays))) > - ;; If it is already after the last known overlay, reset it: > - ;; some previous invalid overlay already triggered the > - ;; necessary parsing. > - (t > - (setq org-num--missing-overlay nil)))) > - ;; Update OVERLAY's numbering. > - (let* ((level (overlay-get overlay 'level)) > - (skip (overlay-get overlay 'skip)) > - (numbering (org-num--current-numbering level skip))) > - (org-num--refresh-display overlay numbering) > - (push overlay new-overlays))) > + (let ((next (overlay-start overlay))) > + (let ((last (and new-overlays (overlay-start (car new-overlays))))) > + (cond > + ((null org-num--missing-overlay)) > + ((> org-num--missing-overlay next)) > + ((or (null last) (> org-num--missing-overlay last)) > + (setq org-num--missing-overlay nil) > + (setq new-overlays (nconc (org-num--number-region last next) > + new-overlays))) > + ;; If it is already after the last known overlay, reset it: > + ;; some previous invalid overlay already triggered the > + ;; necessary parsing. > + (t > + (setq org-num--missing-overlay nil)))) > + ;; Update OVERLAY's numbering. > + (let* ((level (overlay-get overlay 'level)) > + (skip (overlay-get overlay 'skip)) > + (numbering (org-num--current-numbering next level skip))) > + (org-num--refresh-display overlay numbering) > + (push overlay new-overlays)))) > ;; Invalid overlay. It indicates that the buffer needs to be > ;; parsed again between the two surrounding valid overlays or > ;; buffer boundaries. > @@ -430,6 +447,13 @@ See this variable for the meaning of BEG and END." > (when (or org-num--missing-overlay org-num--invalid-flag) > (org-num--update)))) > > +(defun org-num--check-narrowing () > + "Check buffer narrowing; update numbering if necessary. > +This function is meant to be used in `post-command-hook'." > + (when (and org-num-visible-only (/= org-num--start (point-min))) > + (setq org-num--start (point-min)) > + (org-num--update))) > + > > ;;; Public Functions > > @@ -450,10 +474,12 @@ NUMBERING is a list of numbers." > (setq org-num--numbering nil) > (setq org-num--overlays (nreverse (org-num--number-region nil nil))) > (add-hook 'after-change-functions #'org-num--verify nil t) > + (add-hook 'post-command-hook #'org-num--check-narrowing nil t) > (add-hook 'change-major-mode-hook #'org-num--clear nil t)) > (t > (org-num--clear) > (remove-hook 'after-change-functions #'org-num--verify t) > + (remove-hook 'post-command-hook #'org-num--check-narrowing t) > (remove-hook 'change-major-mode-hook #'org-num--clear t)))) -- [ stardiviner ] I try to make every word tell the meaning what I want to express. Blog: https://stardiviner.github.io/ IRC(freenode): stardiviner, Matrix: stardiviner GPG: F09F650D7D674819892591401B5DF1C95AE89AC3