emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Nicolas Goaziou <n.goaziou@gmail.com>
To: Jambunathan K <kjambunathan@gmail.com>
Cc: emacs-orgmode@gnu.org, Carsten Dominik <carsten.dominik@gmail.com>
Subject: Re: Outline cycling does not preserve point's position
Date: Wed, 11 Sep 2013 22:01:31 +0200	[thread overview]
Message-ID: <87fvtb6sbo.fsf@gmail.com> (raw)
In-Reply-To: <87zjrjcp4e.fsf@gmail.com> (Jambunathan K.'s message of "Wed, 11 Sep 2013 21:44:09 +0530")

Jambunathan K <kjambunathan@gmail.com> writes:

> I am happy with whatever is the latest version.  You may want to commit
> it.

I copy here[fn:1] the current version, for the record, along with its backward
counterpart. Some points are still to be discussed:

  1. What to do on node properties?
  2. What to do on source blocks?


[fn:1] Here is the code:

(defun org-forward-linear-element ()
  "Move forward to beginning of next element, ignoring depth.
The function implements some special moves for convenience:
  - On an affiliated keyword, jump to the beginning of the
    relative element.
  - On an item or a footnote definition, move to the second
    element inside, if any.
  - On a table, jump after it.
  - On a verse block, stop after each blank line."
  (interactive)
  (when (eobp) (user-error "Cannot move further down"))
  (let* ((element (org-element-at-point))
         (type (org-element-type element))
         (post-affiliated (org-element-property :post-affiliated element))
         (contents-begin (org-element-property :contents-begin element))
         (contents-end (org-element-property :contents-end element))
         (end (let ((end (org-element-property :end element)) (parent element))
                (while (and (setq parent (org-element-property :parent parent))
                            (= (org-element-property :contents-end parent) end))
                  (setq end (org-element-property :end parent)))
                end)))
    (cond ((not element)
           (skip-chars-forward " \r\t\n")
           (or (eobp) (beginning-of-line)))
          ;; On affiliated keywords, move to element's beginning.
          ((and post-affiliated (< (point) post-affiliated))
           (goto-char post-affiliated))
          ;; At a table row, move to the end of the table.
          ((eq type 'table-row)
           (goto-char (org-element-property
                       :end (org-element-property :parent element))))
          ((eq type 'table) (goto-char end))
          ((not contents-begin) (goto-char end))
          ;; If current element contents are invisible, skip the
          ;; element altogether.
          ((outline-invisible-p (line-end-position))
           (case type
             (headline
              (org-with-limited-levels (outline-next-visible-heading 1)))
             ;; At a plain list, make sure we move to the next item
             ;; instead of skipping the whole list.
             (plain-list (forward-char)
                         (org-forward-linear-element))
             (otherwise (goto-char end))))
          ((>= (point) contents-end) (goto-char end))
          ((>= (point) contents-begin)
           ;; Handle special cases.  In all other situations, point is
           ;; where it should be.
           (case type
             (paragraph (goto-char end))
             ;; At a plain list, try to move to second element in
             ;; first item, if possible.
             (plain-list (end-of-line)
                         (org-forward-linear-element))
             ;; Consider blank lines as separators in verse blocks to
             ;; ease editing.
             (verse-block
              (beginning-of-line)
              (if (not (re-search-forward "^[ \t]*$" contents-end t))
                  (goto-char end)
                (skip-chars-forward " \r\t\n")
                (if (= (point) contents-end) (goto-char contents)
                  (beginning-of-line))))))
          ;; When contents start on the middle of a line (e.g. in
          ;; items and footnote definitions), try to reach first
          ;; element starting after current line.
          ((> (line-end-position) contents-begin)
           (end-of-line)
           (org-forward-linear-element))
          (t (goto-char contents-begin)))))

(defun org-backward-linear-element ()
  "Move backward to start of previous element, ignoring depth."
  (interactive)
  (when (bobp) (user-error "Cannot move further up"))
  (let* ((element (org-element-at-point))
         (type (org-element-type element))
         (contents-begin (org-element-property :contents-begin element))
         (contents-end (org-element-property :contents-end element))
         (post-affiliated (org-element-property :post-affiliated element))
         (begin (org-element-property :begin element)))
    (cond ((not element) (goto-char (point-min)))
          ((= (point) begin)
           (backward-char)
           (org-backward-linear-element))
          ((and post-affiliated (<= (point) post-affiliated)) (goto-char begin))
          ((eq type 'table-row)
           (goto-char (org-element-property
                       :contents-begin (org-element-property :parent element))))
          ((eq type 'table) (goto-char begin))
          ((not contents-begin)
           (goto-char (or post-affiliated begin)))
          ((eq type 'paragraph)
           (goto-char contents-begin)
           ;; When at first paragraph in an item or a footnote
           ;; definition, move directly to beginning of line.
           (let ((parent-contents
                  (org-element-property
                   :contents-begin (org-element-property :parent element))))
             (when (and parent-contents (= parent-contents contents-begin))
               (beginning-of-line))))
          ((eq type 'verse-block)
           (if (= (point) contents-begin)
               (goto-char (or post-affiliated begin))
             ;; Inside a verse block, see blank lines as paragraph
             ;; separators.
             (let ((origin (point)))
               (skip-chars-backward " \r\t\n" contents-begin)
               (when (re-search-backward "^[ \t]*$" contents-begin 'move)
                 (skip-chars-forward " \r\t\n" origin)
                 (if (= (point) origin) (goto-char contents-begin)
                   (beginning-of-line))))))
          ;; At the end of a greater element, move to the beginning of
          ;; the last element within.
          ((>= (point) contents-end)
           (goto-char (1- contents-end))
           (org-backward-linear-element))
          (t (goto-char (or post-affiliated begin))))
    ;; Ensure we never leave point invisible.
    (when (outline-invisible-p (point)) (beginning-of-visual-line))))


Regards,

-- 
Nicolas Goaziou

  reply	other threads:[~2013-09-11 20:01 UTC|newest]

Thread overview: 74+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-09-07 12:11 Outline cycling does not preserve point's position Sebastien Vauban
2013-09-07 14:17 ` Carsten Dominik
     [not found]   ` <BED1FBAA-8BB5-45D6-8328-11C0BB2DF015-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2013-09-07 19:28     ` Sebastien Vauban
2013-09-08  6:16       ` Carsten Dominik
2013-09-09  7:57         ` Sebastien Vauban
2013-09-08 11:03       ` Eric Schulte
2013-09-09  8:39         ` Sebastien Vauban
2013-09-08 16:23       ` Carsten Dominik
2013-09-09  8:11         ` Sebastien Vauban
2013-09-09  8:13           ` Carsten Dominik
2013-09-09  8:23             ` Sebastien Vauban
2013-09-09  8:27               ` Carsten Dominik
2013-09-09  8:33                 ` Sebastien Vauban
2013-09-09  8:38                   ` Bastien
2013-09-09  9:05                     ` Carsten Dominik
2013-09-09 11:32                       ` Nicolas Goaziou
2013-09-09 11:49                         ` Bastien
2013-09-09 15:27                           ` Nicolas Goaziou
2013-09-09 14:23                         ` Carsten Dominik
2013-09-09 15:16                           ` Jambunathan K
2013-09-09 15:41                           ` Nicolas Goaziou
2013-09-09 17:42                             ` Sebastien Vauban
2013-09-10  3:47                             ` Carsten Dominik
2013-09-10  6:03                               ` Carsten Dominik
2013-09-10  6:48                                 ` Eric Abrahamsen
2013-09-10  7:32                                 ` Suvayu Ali
2013-09-10  7:53                                   ` Suvayu Ali
2013-09-10  7:58                                     ` Carsten Dominik
2013-09-10  8:16                                       ` Carsten Dominik
2013-09-10  8:50                                         ` Suvayu Ali
2013-09-10  9:02                                           ` Carsten Dominik
2013-09-10  9:50                                             ` Suvayu Ali
2013-09-10 16:33                                               ` Nicolas Goaziou
2013-09-10 18:35                                                 ` Jambunathan K
2013-09-10 18:39                                                   ` Nicolas Goaziou
2013-09-10 19:22                                                     ` Jambunathan K
2013-09-10 19:40                                                       ` Jambunathan K
2013-09-10 19:52                                                       ` Nicolas Goaziou
2013-09-10 18:58                                                 ` Suvayu Ali
2013-09-10 19:07                                                   ` Suvayu Ali
2013-09-10 19:48                                                   ` Nicolas Goaziou
2013-09-10 20:13                                                     ` Suvayu Ali
2013-09-10 20:29                                                       ` Nicolas Goaziou
2013-09-10 21:08                                                     ` Carsten Dominik
2013-09-11 12:24                                                       ` Suvayu Ali
2013-09-12  6:54                                                         ` Jambunathan K
2013-09-12  9:11                                                           ` Nicolas Goaziou
2013-09-12  9:28                                                             ` Jambunathan K
2013-09-12  9:47                                                               ` Suvayu Ali
2013-09-11  2:49                                                     ` Jambunathan K
2013-09-11 11:09                                                       ` Nicolas Goaziou
2013-09-11 11:34                                                         ` Jambunathan K
2013-09-11 15:19                                                           ` Nicolas Goaziou
2013-09-11 15:40                                                             ` Jambunathan K
2013-09-11 15:31                                                         ` Jambunathan K
2013-09-11 15:40                                                           ` Nicolas Goaziou
2013-09-11 16:14                                                             ` Jambunathan K
2013-09-11 20:01                                                               ` Nicolas Goaziou [this message]
2013-09-11 22:11                                                                 ` Suvayu Ali
2013-09-12  6:43                                                                 ` Jambunathan K
2013-09-12  9:07                                                                   ` Nicolas Goaziou
2013-09-12 10:12                                                                     ` Jambunathan K
2013-09-12  7:17                                                                 ` Sebastien Vauban
2013-09-13 10:57                                                                 ` Carsten Dominik
2013-09-13 22:29                                                                   ` Nicolas Goaziou
2013-09-14  5:33                                                                     ` Carsten Dominik
2013-09-14 17:16                                                                     ` Suvayu Ali
2013-09-15  4:39                                                                       ` Carsten Dominik
2013-09-10 20:16                                         ` Samuel Wales
2013-09-10  5:06                             ` Jambunathan K
2013-09-11  3:57                             ` Jambunathan K
2013-09-11 15:47                               ` Nicolas Goaziou
2013-09-09  8:38                   ` Carsten Dominik
2013-09-09 11:30                     ` Nicolas Goaziou

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=87fvtb6sbo.fsf@gmail.com \
    --to=n.goaziou@gmail.com \
    --cc=carsten.dominik@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=kjambunathan@gmail.com \
    /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).