Skip to content

Commit

Permalink
Add outline-indent-backward-same-level and outline-indent-forward-sam…
Browse files Browse the repository at this point in the history
…e-level
  • Loading branch information
jamescherti committed Nov 25, 2024
1 parent 148e09f commit f8efff5
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 38 deletions.
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ The **outline-indent** Emacs package provides a minor mode that enables code fol
The **outline-indent** package is a fast and reliable alternative to the **origami.el** and **yafolding.el** packages. (*origami.el* and *yafolding.el* are no longer maintained, slow, and known to have bugs that impact their reliability and performance.)

In addition to code folding, *outline-indent* allows:
- moving indented blocks up and down with `(outline-move-subtree-up)` and `(outline-move-subtree-down)`,
- moving indented blocks up and down with `(outline-indent-move-subtree-up)` and `(outline-indent-move-subtree-down)`,
- indenting/unindenting to adjust indentation levels with `(outline-indent-shift-right)` and `(outline-indent-shift-left)`,
- inserting a new line with the same indentation level as the current line with `(outline-indent-insert-heading)`,
- Move backward/forward to the indentation level of the current line with `(outline-backward-same-level)` and `(outline-forward-same-level)`.
- Move backward/forward to the indentation level of the current line with `(outline-indent-backward-same-level)` and `(outline-indent-forward-same-level)`.
- Customizing the ellipsis to replace the default "..." with something more visually appealing, such as "▼".
- and other features.

Expand All @@ -37,6 +37,7 @@ The *outline-indent.el* Emacs package offers a similar functionality to Vim's `s
- [Adjusting the shift width and default offset](#adjusting-the-shift-width-and-default-offset)
- [Usage](#usage)
- [Functions specific to outline-indent-minor-mode](#functions-specific-to-outline-indent-minor-mode)
- [outline-indent-backward-same-level and outline-indent-forward-same-level](#outline-indent-backward-same-level-and-outline-indent-forward-same-level)
- [outline-indent-shift-left and outline-indent-shift-right](#outline-indent-shift-left-and-outline-indent-shift-right)
- [outline-indent-move-subtree-up and outline-indent-move-subtree-down](#outline-indent-move-subtree-up-and-outline-indent-move-subtree-down)
- [outline-indent-insert-heading](#outline-indent-insert-heading)
Expand Down Expand Up @@ -126,6 +127,22 @@ Explanation:

### Functions specific to outline-indent-minor-mode

#### outline-indent-backward-same-level and outline-indent-forward-same-level

*(By default, `outline-indent-advise-outline-functions` is set to t, which means that you can also use the built-in outline functions `(outline-backward-same-level)` and `(outline-forward-same-level)` as an alternative to `(outline-indent-backward-same-level)` and `(outline-indent-forward-same-level)`)*

To move to the next block with the same indentation level:

``` emacs-lisp
(outline-indent-forward-same-level)
```

To move to the previous block with the same indentation level:

``` emacs-lisp
(outline-indent-backward-same-level)
```

#### outline-indent-shift-left and outline-indent-shift-right

*(By default, `outline-indent-advise-outline-functions` is set to t, which means that you can also use the built-in outline functions `(outline-promote)` and `(outline-demote)` as an alternative to `(outline-indent-shift-left)` and `(outline-indent-shift-right)`)*
Expand Down
98 changes: 62 additions & 36 deletions outline-indent.el
Original file line number Diff line number Diff line change
Expand Up @@ -166,30 +166,31 @@ Functions that will be advised include:
It is recommended to keep this set to t for improved behavior."
:type 'boolean
:set (lambda (symbol value)
(set-default symbol value)
(if value
;; Advise the built-in `outline-mode' and `outline-minor-mode'
;; functions to improve compatibility with
;; `outline-indent-minor-mode'. The built-in `outline-minor-mode'
;; functions will work exactly as before and will only exhibit
;; different behavior when `outline-indent-minor-mode' is active.
(progn
(advice-add 'outline-promote :around #'outline-indent--advice-promote)
(advice-add 'outline-demote :around #'outline-indent--advice-demote)
(advice-add 'outline-insert-heading :around #'outline-indent--advice-insert-heading)
(advice-add 'outline-forward-same-level :around #'outline-indent--advice-forward-same-level)
(advice-add 'outline-backward-same-level :around #'outline-indent--advice-backward-same-level)
(advice-add 'outline-move-subtree-up :around #'outline-indent--advice-move-subtree-up)
(advice-add 'outline-move-subtree-down :around #'outline-indent--advice-move-subtree-down))
(progn
(advice-remove 'outline-promote #'outline-indent--advice-promote)
(advice-remove 'outline-demote #'outline-indent--advice-demote)
(advice-remove 'outline-insert-heading #'outline-indent--advice-insert-heading)
(advice-remove 'outline-forward-same-level #'outline-indent--advice-forward-same-level)
(advice-remove 'outline-backward-same-level #'outline-indent--advice-backward-same-level)
(advice-remove 'outline-move-subtree-up #'outline-indent--advice-move-subtree-up)
(advice-remove 'outline-move-subtree-down #'outline-indent--advice-move-subtree-down))))
:set
(lambda (symbol value)
(set-default symbol value)
(if value
;; Advise the built-in `outline-mode' and `outline-minor-mode'
;; functions to improve compatibility with
;; `outline-indent-minor-mode'. The built-in `outline-minor-mode'
;; functions will work exactly as before and will only exhibit
;; different behavior when `outline-indent-minor-mode' is active.
(progn
(advice-add 'outline-promote :around #'outline-indent--advice-promote)
(advice-add 'outline-demote :around #'outline-indent--advice-demote)
(advice-add 'outline-insert-heading :around #'outline-indent--advice-insert-heading)
(advice-add 'outline-forward-same-level :around #'outline-indent--advice-forward-same-level)
(advice-add 'outline-backward-same-level :around #'outline-indent--advice-backward-same-level)
(advice-add 'outline-move-subtree-up :around #'outline-indent--advice-move-subtree-up)
(advice-add 'outline-move-subtree-down :around #'outline-indent--advice-move-subtree-down))
;; Disable
(advice-remove 'outline-promote #'outline-indent--advice-promote)
(advice-remove 'outline-demote #'outline-indent--advice-demote)
(advice-remove 'outline-insert-heading #'outline-indent--advice-insert-heading)
(advice-remove 'outline-forward-same-level #'outline-indent--advice-forward-same-level)
(advice-remove 'outline-backward-same-level #'outline-indent--advice-backward-same-level)
(advice-remove 'outline-move-subtree-up #'outline-indent--advice-move-subtree-up)
(advice-remove 'outline-move-subtree-down #'outline-indent--advice-move-subtree-down)))
:group 'outline-indent)

(defvar outline-indent-minor-mode-map
Expand Down Expand Up @@ -263,8 +264,8 @@ addressing the issue where the cursor might be reset after the operation."
(setq arg 1))
(outline-indent-move-subtree-down (- arg)))

(defun outline-indent-shift-right (&optional which arg)
"Demote the subtree, increasing its indentation level.
(defun outline-indent-shift-right (&optional _which arg)
"Increasing the indentation level.
The global variable `outline-indent-shift-width' or
`outline-indent-default-offset' is used to determine the number of spaces to
Expand All @@ -273,10 +274,10 @@ indent the subtree.
WHICH is ignored (backward compatibility with `outline-demote').
If ARG is positive, indent the outline. If ARG is negative, unindent the
outline. Defaults to 1 if ARG is nil."
(interactive)
(unless which
;; Ignore: Warning: Unused lexical argument `which'
(setq which t))
(interactive
(list (if (and transient-mark-mode mark-active) 'region
(outline-back-to-heading)
(if current-prefix-arg nil 'subtree))))
(unless arg
(setq arg 1))
(let ((shift-right (>= arg 0))
Expand Down Expand Up @@ -304,16 +305,17 @@ outline. Defaults to 1 if ARG is nil."
(move-to-column (+ column shift-width))
(move-to-column (max (- column shift-width) 0)))))

(defun outline-indent-shift-left (&optional which)
"Promote the subtree, decreasing its indentation level.
(defun outline-indent-shift-left (&optional _which)
"Decreasing the indentation level. Equivalent to `outline-promote'.
The global variable `outline-indent-shift-width' or
`outline-indent-default-offset' is used to determine the number of spaces to
unindent the subtree.
WHICH is ignored (backward compatibility with `outline-promote')."
(interactive)
(unless which
;; Ignore: Warning: Unused lexical argument `which'
(setq which t))
(interactive (list (if (and transient-mark-mode mark-active) 'region
(outline-back-to-heading)
(if current-prefix-arg nil 'subtree))))
(outline-indent-shift-right nil -1))

(defalias 'outline-indent-demote #'outline-indent-shift-right
Expand Down Expand Up @@ -464,6 +466,30 @@ ORIG-FUN is the original function being advised, and ARGS are its arguments."
;; Apply the original function without modification
(apply orig-fun args)))

(defun outline-indent-backward-same-level (&optional arg)
"Move backward to the ARG'th subheading at same indentation level as this one.
Stop at the first and last indented blocks of a superior indentation."
(interactive "p")
(unless arg
(setq arg 1))
(if (advice-member-p 'outline-indent--advice-backward-same-level
'outline-backward-same-level)
(outline-backward-same-level arg)
(outline-indent--advice-backward-same-level 'outline-backward-same-level
arg)))

(defun outline-indent-forward-same-level (&optional arg)
"Move forward to the ARG'th subheading at same indentation level as this one.
Stop at the first and last indented blocks of a superior indentation."
(interactive "p")
(unless arg
(setq arg 1))
(if (advice-member-p 'outline-indent--advice-forward-same-level
'outline-forward-same-level)
(outline-forward-same-level arg)
(outline-indent--advice-forward-same-level 'outline-forward-same-level
arg)))

;;;###autoload
(define-minor-mode outline-indent-minor-mode
"Toggle `outline-indent-minor-mode'.
Expand Down

0 comments on commit f8efff5

Please sign in to comment.