Refactor: simplify logic and edit docstrings

- Collapse some nested cond forms
- Simplify condition for global cycling
- Remove redundant calls to (outline-back-to-heading) and
  (looking-at outline-regexp)
- Remove unused vars
- modify some docstrings and comments
master
yuhan0 7 years ago
parent bf330503ae
commit 0c4c085bdf
  1. 183
      outshine.el

@ -1918,89 +1918,90 @@ Essentially a much simplified version of `next-line'."
(beginning-of-line 2))) (beginning-of-line 2)))
(defun outshine-cycle (&optional arg) (defun outshine-cycle (&optional arg)
"Visibility cycling for outline(-minor)-mode. "Visibility cycling for `outshine-mode'.
- When point is at the beginning of the buffer, or when called with a The behavior of this command is determined by the first matching
C-u prefix argument, rotate the entire buffer through 3 states: condition among the following:
1. OVERVIEW: Show only top-level headlines.
2. CONTENTS: Show all headlines of all levels, but no body text. 1. When point is at the beginning of the buffer, or when called
3. SHOW ALL: Show everything. with a `\\[universal-argument]' universal argument, rotate the entire buffer
through 3 states:
- When point is at the beginning of a headline, rotate the subtree started
by this line through 3 different states: - OVERVIEW: Show only top-level headlines.
1. FOLDED: Only the main headline is shown. - CONTENTS: Show all headlines of all levels, but no body text.
2. CHILDREN: The main headline and the direct children are shown. From - SHOW ALL: Show everything.
this state, you can move to one of the children and
zoom in further. 2. When point is at the beginning of a headline, rotate the
3. SUBTREE: Show the entire subtree, including body text. subtree starting at this line through 3 different states:
- When point is not at the beginning of a headline, execute - FOLDED: Only the main headline is shown.
`indent-relative', like TAB normally does." - CHILDREN: The main headline and its direct children are shown.
From this state, you can move to one of the children
and zoom in further.
- SUBTREE: Show the entire subtree, including body text.
3. Otherwise, execute `indent-relative', like TAB normally does.
The behavior of this function is modified by the following
variables: `outshine-cycle-emulate-tab` and
`outshine-org-style-global-cycling-at-bob-p`, which see."
(interactive "P") (interactive "P")
(setq deactivate-mark t) (setq deactivate-mark t)
(cond (cond
((equal arg '(4)) ;; Beginning of buffer or called with C-u: Global cycling
;; Run `outshine-cycle-buffer' as if at the top of the buffer. ((or (equal arg '(4))
(save-excursion (and (bobp)
(goto-char (point-min)) (or
(outshine-cycle-buffer))) ;; outline-magic style behaviour - always global cycle at bob
(t (not outshine-org-style-global-cycling-at-bob-p)
(cond ;; org-mode style behaviour - only cycle if not on a heading
;; Beginning of buffer: Global cycling (not (outline-on-heading-p)))))
((or (outshine-cycle-buffer))
;; outline-magic style behaviour
(and ;; At a heading: rotate between three different views
(bobp) ((save-excursion (beginning-of-line 1) (looking-at outline-regexp))
(not outshine-org-style-global-cycling-at-bob-p)) (outline-back-to-heading)
;; org-mode style behaviour (let ((goal-column 0) eoh eol eos)
(and ;; First, some boundaries
(bobp) (save-excursion
(not (outline-on-heading-p)) (save-excursion (outshine-next-line) (setq eol (point)))
outshine-org-style-global-cycling-at-bob-p)) (outline-end-of-heading) (setq eoh (point))
(outshine-cycle-buffer)) (outline-end-of-subtree) (setq eos (point)))
;; Find out what to do next and set `this-command'
((save-excursion (beginning-of-line 1) (looking-at outline-regexp)) (cond
;; At a heading: rotate between three different views ((= eos eoh)
(outline-back-to-heading) ;; Nothing is hidden behind this heading
(let ((goal-column 0) beg eoh eol eos) (outshine--cycle-message "EMPTY ENTRY"))
;; First, some boundaries ((>= eol eos)
(save-excursion ;; Entire subtree is hidden in one line: open it
(outline-back-to-heading) (setq beg (point)) (outline-show-entry)
(save-excursion (outshine-next-line) (setq eol (point))) (outline-show-children)
(outline-end-of-heading) (setq eoh (point)) (outshine--cycle-message "CHILDREN")
(outline-end-of-subtree) (setq eos (point))) (setq
;; Find out what to do next and set `this-command' this-command 'outshine-cycle-children))
(cond ((eq last-command 'outshine-cycle-children)
((= eos eoh) ;; We just showed the children, now show everything.
;; Nothing is hidden behind this heading (outline-show-subtree)
(outshine--cycle-message "EMPTY ENTRY")) (outshine--cycle-message "SUBTREE"))
((>= eol eos) (t
;; Entire subtree is hidden in one line: open it ;; Default action: hide the subtree.
(outline-show-entry) (outline-hide-subtree)
(outline-show-children) (outshine--cycle-message "FOLDED")))))
(outshine--cycle-message "CHILDREN")
(setq ;; Not at a headline: TAB emulation
this-command 'outshine-cycle-children)) ((outshine-cycle-emulate-tab)
((eq last-command 'outshine-cycle-children) (indent-relative))
;; We just showed the children, now show everything.
(outline-show-subtree) (t (outline-back-to-heading))))
(outshine--cycle-message "SUBTREE"))
(t
;; Default action: hide the subtree.
(outline-hide-subtree)
(outshine--cycle-message "FOLDED")))))
;; TAB emulation
((outshine-cycle-emulate-tab)
(indent-relative))
(t
;; Not at a headline: Do indent-relative
(outline-back-to-heading))))))
(defun outshine-cycle-buffer (&optional arg) (defun outshine-cycle-buffer (&optional arg)
"Cycle the visibility state of buffer. "Rotate the visibility state of the buffer through 3 states:
With a numeric prefix, show all headlines up to that level." - OVERVIEW: Show only top-level headlines.
- CONTENTS: Show all headlines of all levels, but no body text.
- SHOW ALL: Show everything.
With a numeric prefix ARG, show all headlines up to that level."
(interactive "P") (interactive "P")
(save-excursion (save-excursion
(cond (cond
@ -2011,21 +2012,17 @@ With a numeric prefix, show all headlines up to that level."
;; We just created the overview - now do table of contents ;; We just created the overview - now do table of contents
;; This can be slow in very large buffers, so indicate action ;; This can be slow in very large buffers, so indicate action
(outshine--cycle-message "CONTENTS...") (outshine--cycle-message "CONTENTS...")
(save-excursion ;; Visit all headings and show their offspring
;; Visit all headings and show their offspring (goto-char (point-max))
(goto-char (point-max)) (while (not (bobp))
(catch 'exit (condition-case nil
(while (and (progn (condition-case nil (progn
(outline-previous-visible-heading 1) (outline-previous-visible-heading 1)
(error (goto-char (point-min)))) (outline-show-branches))
t) (error (goto-char (point-min)))))
(looking-at outline-regexp)) (outshine--cycle-message "CONTENTS...done")
(outline-show-branches) (setq this-command 'outshine-cycle-toc
(if (bobp) (throw 'exit nil)))) outshine-current-buffer-visibility-state 'contents))
(outshine--cycle-message "CONTENTS...done"))
(setq
this-command 'outshine-cycle-toc
outshine-current-buffer-visibility-state 'contents))
((eq last-command 'outshine-cycle-toc) ((eq last-command 'outshine-cycle-toc)
;; We just showed the table of contents - now show everything ;; We just showed the table of contents - now show everything
(outline-show-all) (outline-show-all)

Loading…
Cancel
Save