Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug where topspace-mode doesn't work locally #6

Merged
merged 1 commit into from
Mar 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<h1 align="center"> TopSpace </h1>
<p align="center">Scroll down & recenter top lines in Emacs / get upper margins/padding.</p>

<!-- padding cursor -->
<!-- cursor -->

<p align="center">
<a href="http://melpa.org/#/topspace"><img src="http://melpa.org/packages/topspace-badge.svg" height="20"/></a>
Expand All @@ -17,12 +17,9 @@ as you scroll down or recenter top text.

TopSpace is:


* **Easier on the eyes**: Recenter or scroll down top text to a more comfortable eye level for reading, especially when in full-screen or on a large monitor.
* **Easy to use**: No new keybindings are required, keep using all your previous scrolling & recentering commands, except now you can also scroll above the top lines. It also integrates seamlessly with [centered-cursor-mode][1] to keep the cursor centered all the way to the top line.

Fill out the [satisfaction survey](https://www.supersurvey.com/QRMU65MKE) to help the author know what you would like improved or added.

# Installation

TopSpace is available on [MELPA](http://melpa.org).
Expand All @@ -47,24 +44,23 @@ To enable `topspace-mode` globally on startup, add the following to your Emacs c

# Customization
```elisp
(defcustom topspace-autocenter-buffers
t
(defcustom topspace-autocenter-buffers t
"Center small buffers with top space when first opened or window sizes change.
This is done by automatically calling `topspace-recenter-buffer'
and the positioning can be customized with `topspace-center-position'.
Top space will not be added if the number of text lines in the buffer is larger
than or close to the selected window's height.
Customize `topspace-center-position' to adjust the centering position.

If non-nil, then always autocenter. If nil, never autocenter.
If set to a predicate function (function that returns a boolean value),
then do auto-centering only when that function returns a non-nil value."
:group 'topspace
:type '(choice (const :tag "always" t)
(const :tag "never" nil)
(function :tag "predicate function")))

(defcustom topspace-center-position
0.4
(defcustom topspace-center-position 0.4
"Target position when centering buffers as a ratio of frame height.
A value from 0 to 1 where lower values center buffers higher up in the screen.
Used in `topspace-recenter-buffer' when called or when opening/resizing buffers
Expand All @@ -78,8 +74,10 @@ This is useful in particular when `global-topspace-mode' is enabled but you want
`topspace-mode' to be inactive in certain buffers or in any specific
circumstance. When inactive, `topspace-mode' will still technically be on,
but will be effectively off and have no effect on the buffer.
Note that if `topspace-active' returns non-nil but `topspace-mode' is off,
`topspace-mode' will still be disabled.

If t, then always be active. If nil, never be active.
If non-nil, then always be active. If nil, never be active.
If set to a predicate function (function that returns a boolean value),
then be active only when that function returns a non-nil value."
:type '(choice (const :tag "always" t)
Expand Down Expand Up @@ -128,5 +126,6 @@ commands so that custom topspace functions are called before or after
each time any of these other commands are called (interactively or
otherwise).

Fill out the [satisfaction survey](https://www.supersurvey.com/QRMU65MKE) to help the author know what you would like improved or added.

[1]: https://github.com/andre-r/centered-cursor-mode.el
110 changes: 61 additions & 49 deletions topspace.el
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ space should be reduced in size or not")
"Displaying top space before the first window config change can cause errors.
This flag signals to wait until then to display top space.")

(defvar topspace--advice-added nil "Keep track if advice-add done already.")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Customization

Expand All @@ -105,24 +107,23 @@ This flag signals to wait until then to display top space.")
:link '(url-link "https://github.com/trevorpogue/topspace")
:link '(emacs-commentary-link :tag "Commentary" "topspace"))

(defcustom topspace-autocenter-buffers
t
(defcustom topspace-autocenter-buffers t
"Center small buffers with top space when first opened or window sizes change.
This is done by automatically calling `topspace-recenter-buffer'
and the positioning can be customized with `topspace-center-position'.
Top space will not be added if the number of text lines in the buffer is larger
than or close to the selected window's height.
Customize `topspace-center-position' to adjust the centering position.

If non-nil, then always autocenter. If nil, never autocenter.
If set to a predicate function (function that returns a boolean value),
then do auto-centering only when that function returns a non-nil value."
:group 'topspace
:type '(choice (const :tag "always" t)
(const :tag "never" nil)
(function :tag "predicate function")))

(defcustom topspace-center-position
0.4
(defcustom topspace-center-position 0.4
"Target position when centering buffers as a ratio of frame height.
A value from 0 to 1 where lower values center buffers higher up in the screen.
Used in `topspace-recenter-buffer' when called or when opening/resizing buffers
Expand All @@ -136,8 +137,10 @@ This is useful in particular when `global-topspace-mode' is enabled but you want
`topspace-mode' to be inactive in certain buffers or in any specific
circumstance. When inactive, `topspace-mode' will still technically be on,
but will be effectively off and have no effect on the buffer.
Note that if `topspace-active' returns non-nil but `topspace-mode' is off,
`topspace-mode' will still be disabled.

If t, then always be active. If nil, never be active.
If non-nil, then always be active. If nil, never be active.
If set to a predicate function (function that returns a boolean value),
then be active only when that function returns a non-nil value."
:type '(choice (const :tag "always" t)
Expand Down Expand Up @@ -176,20 +179,24 @@ TOTAL-LINES is used in the same way as in `scroll-down'."
(defun topspace--filter-args-scroll-down (&optional total-lines)
"Run before `scroll-down' for scrolling above the top line.
TOTAL-LINES is used in the same way as in `scroll-down'."
(setq total-lines (car total-lines))
(setq total-lines (or total-lines (- (topspace--window-height)
next-screen-context-lines)))
(setq topspace--total-lines-scrolling total-lines)
(list (topspace--scroll total-lines)))
(cond
((not (topspace--enabled)) total-lines)
((setq total-lines (car total-lines))
(setq total-lines (or total-lines (- (topspace--window-height)
next-screen-context-lines)))
(setq topspace--total-lines-scrolling total-lines)
(list (topspace--scroll total-lines)))))

(defun topspace--filter-args-scroll-up (&optional total-lines)
"Run before `scroll-up' for scrolling above the top line.
TOTAL-LINES is used in the same way as in `scroll-up'."
(setq total-lines (car total-lines))
(setq total-lines (* (or total-lines (- (topspace--window-height)
next-screen-context-lines)) -1))
(setq topspace--total-lines-scrolling total-lines)
(list (* (topspace--scroll total-lines) -1)))
(cond
((not (topspace--enabled)) total-lines)
((setq total-lines (car total-lines))
(setq total-lines (* (or total-lines (- (topspace--window-height)
next-screen-context-lines)) -1))
(setq topspace--total-lines-scrolling total-lines)
(list (* (topspace--scroll total-lines) -1)))))

(defun topspace--after-scroll (&optional total-lines)
"Run after `scroll-up'/`scroll-down' for scrolling above the top line.
Expand All @@ -199,29 +206,33 @@ and no top space was present before scrolling but it should be after scrolling.
The reason this is needed is because `topspace--put' only draws the overlay when
`window-start` equals 1, which can only be true after the scroll command is run
in the described case above."
(setq total-lines topspace--total-lines-scrolling)
(when (and (> topspace--window-start-before-scroll 1) (= (window-start) 1))
(let ((lines-already-scrolled (count-screen-lines
1 topspace--window-start-before-scroll)))
(setq total-lines (abs total-lines))
(set-window-start (selected-window) 1)
(topspace--put (- total-lines lines-already-scrolled)))))
(cond
((not (topspace--enabled)))
((setq total-lines topspace--total-lines-scrolling)
(when (and (> topspace--window-start-before-scroll 1) (= (window-start) 1))
(let ((lines-already-scrolled (count-screen-lines
1 topspace--window-start-before-scroll)))
(setq total-lines (abs total-lines))
(set-window-start (selected-window) 1)
(topspace--put (- total-lines lines-already-scrolled)))))))

(defun topspace--after-recenter (&optional line-offset redisplay)
"Recenter near the top of buffers by adding top space appropriately.
LINE-OFFSET and REDISPLAY are used in the same way as in `recenter'."
;; redisplay is unused but needed since this function
;; must take the same arguments as `recenter'
redisplay ; remove flycheck warning for unused argument (see above)
(when (= (window-start) 1)
(unless line-offset
(setq line-offset (round (/ (topspace--window-height) 2))))
(when (< line-offset 0)
;; subtracting 3 below made `recenter-top-bottom' act correctly
;; when it moves point to bottom and top space is added to get there
(setq line-offset (- (- (topspace--window-height) line-offset) 3)))
(topspace--put (- line-offset (topspace--count-screen-lines (window-start)
(point))))))
(cond
((not (topspace--enabled)))
((when (= (window-start) 1)
(unless line-offset
(setq line-offset (round (/ (topspace--window-height) 2))))
(when (< line-offset 0)
;; subtracting 3 below made `recenter-top-bottom' act correctly
;; when it moves point to bottom and top space is added to get there
(setq line-offset (- (- (topspace--window-height) line-offset) 3)))
(topspace--put (- line-offset (topspace--count-screen-lines (window-start)
(point))))))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Top space line height calculation
Expand Down Expand Up @@ -324,8 +335,9 @@ return unexpected value when END is in column 0. This fixes that issue."

(defun topspace--put (&optional height)
"Put/draw top space as an overlay with the target line height HEIGHT."
(let ((old-height (topspace--height)))
(unless (topspace--eval-choice-p topspace-active) (setq height 0))
(let ((old-height))
(unless (topspace--enabled) (setq height 0) (setq old-height 0))
(unless old-height (setq old-height (topspace--height)))
(when height (setq height (topspace--set-height height)))
(when (not height) (setq height old-height))
(when (and (> height 0) (> height old-height))
Expand Down Expand Up @@ -446,26 +458,22 @@ Topspace will not be enabled for:

(defun topspace--enable ()
"Enable `topspace-mode' and do mode setup."
(cond ((topspace--enable-p)
(topspace--add-hooks)
(advice-add #'scroll-up :filter-args #'topspace--filter-args-scroll-up)
(advice-add #'scroll-down :filter-args
#'topspace--filter-args-scroll-down)
(advice-add #'scroll-up :after #'topspace--after-scroll)
(advice-add #'scroll-down :after #'topspace--after-scroll)
(advice-add #'recenter :after #'topspace--after-recenter)
(dolist (window (get-buffer-window-list))
(with-selected-window window (topspace--put))))))
(when (topspace--enable-p)
(topspace--add-hooks)
(unless topspace--advice-added
(setq topspace--advice-added t)
(advice-add #'scroll-up :filter-args #'topspace--filter-args-scroll-up)
(advice-add #'scroll-down :filter-args
#'topspace--filter-args-scroll-down)
(advice-add #'scroll-up :after #'topspace--after-scroll)
(advice-add #'scroll-down :after #'topspace--after-scroll)
(advice-add #'recenter :after #'topspace--after-recenter))
(dolist (window (get-buffer-window-list))
(with-selected-window window (topspace--put)))))

(defun topspace--disable ()
"Disable `topspace-mode' and do mode cleanup."
(message "disable")
(remove-overlays 1 1 'topspace--remove-from-buffer-tag t)
(advice-remove #'scroll-up #'topspace--filter-args-scroll-up)
(advice-remove #'scroll-down #'topspace--filter-args-scroll-down)
(advice-remove #'scroll-up #'topspace--after-scroll)
(advice-remove #'scroll-down #'topspace--after-scroll)
(advice-remove #'recenter #'topspace--after-recenter)
(topspace--remove-hooks))

;;;###autoload
Expand Down Expand Up @@ -528,6 +536,10 @@ Otherwise behave as if called interactively."
topspace-mode
:group 'topspace)

(defun topspace--enabled ()
"Return t only if both `topspace-mode' and `topspace-active' are non-nil."
(and (topspace--eval-choice-p topspace-active) topspace-mode))

(provide 'topspace)

;;; topspace.el ends here