2017-06-28 01:05:50 +00:00
|
|
|
;;; centered-buffer-mode.el --- Centering minor mode -*- lexical-binding: t -*-
|
|
|
|
;;
|
|
|
|
;; Copyright (C) 2012-2017 Sylvain Benner & Contributors
|
2016-07-01 18:20:28 +00:00
|
|
|
;;
|
2016-07-05 21:23:36 +00:00
|
|
|
;; Author: Eugene "JAremko" Yaremenko <w3techplayground@gmail.com>
|
|
|
|
;; Keywords: centering buffer minor-mode
|
2016-07-01 18:20:28 +00:00
|
|
|
;; Created: 1 July 2016
|
2017-06-28 01:05:50 +00:00
|
|
|
;; Version: 1.02
|
2016-07-01 18:20:28 +00:00
|
|
|
;; Package-Requires: ((emacs "24.4"))
|
|
|
|
;; URL: https://github.com/syl20bnr/spacemacs
|
|
|
|
|
|
|
|
;; This file is not part of GNU Emacs.
|
|
|
|
|
|
|
|
;; This program is free software; you can redistribute it and/or modify
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
|
|
|
|
|
|
|
;; This program is distributed in the hope that it will be useful,
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
(require 'face-remap)
|
|
|
|
|
2017-06-28 01:05:50 +00:00
|
|
|
(defgroup spacemacs-centered-buffer-mode nil
|
|
|
|
"Minor mode to center buffer in its window."
|
2016-07-15 14:15:14 +00:00
|
|
|
:group 'convenience)
|
|
|
|
|
2016-07-05 21:23:36 +00:00
|
|
|
(defcustom spacemacs-centered-buffer-mode-min-fringe-width 50
|
2017-06-28 01:05:50 +00:00
|
|
|
"Minimal fringe width. `centered-buffer-mode' will be disabled
|
|
|
|
if one of windows displaying it becomes too narrow."
|
|
|
|
:type 'integer
|
|
|
|
:group 'spacemacs-centered-buffer-mode)
|
|
|
|
|
|
|
|
(defcustom spacemacs-centered-buffer-mode-min-content-width 400
|
|
|
|
"Minimal width of the centered buffer content."
|
|
|
|
:type 'integer
|
|
|
|
:group 'spacemacs-centered-buffer-mode)
|
|
|
|
|
|
|
|
(defcustom spacemacs-centered-buffer-mode-max-content-width 800
|
|
|
|
"Maximal width of the centered buffer content before it stops starching."
|
2016-07-05 21:23:36 +00:00
|
|
|
:type 'integer
|
2016-07-15 14:15:14 +00:00
|
|
|
:group 'spacemacs-centered-buffer-mode)
|
2016-07-05 21:23:36 +00:00
|
|
|
|
|
|
|
(defcustom spacemacs-centered-buffer-mode-safety-gap-width 20
|
|
|
|
"Add extra width to the modified buffer to make sure
|
|
|
|
that differed modifications won't cause an overflow."
|
|
|
|
:type 'integer
|
2016-07-15 14:15:14 +00:00
|
|
|
:group 'spacemacs-centered-buffer-mode)
|
2016-07-05 21:23:36 +00:00
|
|
|
|
2017-06-28 01:05:50 +00:00
|
|
|
(defcustom spacemacs-centered-buffer-mode-fringe-color
|
|
|
|
(face-background 'default)
|
2016-07-05 21:23:36 +00:00
|
|
|
"Color of the fringes."
|
2016-07-05 23:36:15 +00:00
|
|
|
:type 'color
|
2016-07-15 14:15:14 +00:00
|
|
|
:group 'spacemacs-centered-buffer-mode)
|
2016-07-05 21:23:36 +00:00
|
|
|
|
2017-06-28 01:05:50 +00:00
|
|
|
(defcustom spacemacs-centered-buffer-mode-deferred-update nil
|
|
|
|
"Defer buffer re-centering if t.
|
|
|
|
If set to t it may improve input latency."
|
|
|
|
:type 'boolean
|
|
|
|
:group 'spacemacs-centered-buffer-mode)
|
|
|
|
|
|
|
|
(defcustom spacemacs-centered-buffer-mode-only-grow nil
|
|
|
|
"If t ignore reduction of the centered buffer width.
|
|
|
|
NOTE: Useful with some types of line wrappings but also
|
|
|
|
can be mush slower with huge buffers."
|
|
|
|
:type 'boolean
|
|
|
|
:group 'spacemacs-centered-buffer-mode)
|
|
|
|
|
|
|
|
(defcustom spacemacs-centered-buffer-mode-visual-line-mode t
|
|
|
|
"If t also enable `visual-line-mode' when `centered-buffer-mode' is enabled."
|
|
|
|
:type 'boolean
|
|
|
|
:group 'spacemacs-centered-buffer-mode)
|
|
|
|
|
|
|
|
(defvar-local spacemacs--centered-buffer-origin-buffer nil)
|
|
|
|
(defvar-local spacemacs--centered-buffer-indirect-buffer nil)
|
|
|
|
(defvar-local spacemacs--centered-buffer-text-pixel-size nil)
|
|
|
|
(defvar spacemacs--centered-buffer-adviced-fontify-fs (list))
|
|
|
|
(defvar spacemacs-centered-buffer-mode-default-fringe-color (face-background
|
|
|
|
'fringe))
|
|
|
|
(defvar spacemacs--centered-buffer-indirect-buffers (list))
|
|
|
|
(defvar-local spacemacs--centered-buffer-on-buffer-updated-timer nil)
|
|
|
|
(defvar-local spacemacs--centered-buffer-update-all-timer nil)
|
|
|
|
(defvar-local spacemacs--centered-buffer-fontify-region-min-pos nil)
|
|
|
|
(defvar-local spacemacs--centered-buffer-fontify-region-max-pos nil)
|
2016-07-01 18:20:28 +00:00
|
|
|
|
|
|
|
(define-minor-mode spacemacs-centered-buffer-mode
|
2016-07-05 21:23:36 +00:00
|
|
|
"Minor mode to center buffer in its window."
|
2016-07-01 18:20:28 +00:00
|
|
|
:init-value nil
|
2016-07-15 14:15:14 +00:00
|
|
|
:group 'spacemacs-centered-buffer-mode
|
2017-06-28 01:05:50 +00:00
|
|
|
(if (display-graphic-p)
|
|
|
|
(if spacemacs-centered-buffer-mode
|
|
|
|
(if (not (window-dedicated-p))
|
|
|
|
(spacemacs//centered-buffer-enable-branch
|
|
|
|
(called-interactively-p 'any))
|
|
|
|
(setq spacemacs-centered-buffer-mode nil)
|
|
|
|
(when (called-interactively-p 'any)
|
|
|
|
(message "Can't center in dedicated window!")))
|
|
|
|
(spacemacs//centered-buffer-disable-branch))
|
|
|
|
(message "centered buffer mode doesn't work in terminal :(")))
|
|
|
|
|
|
|
|
(defun spacemacs//centered-buffer-enable-branch (interact)
|
2016-07-15 08:52:54 +00:00
|
|
|
"Used it `spacemacs-centered-buffer-mode'.
|
|
|
|
Assume to be called interactively when INTERACT has non nil value."
|
2016-07-07 17:55:19 +00:00
|
|
|
;; Mode will be applied to the indirect buffer.
|
|
|
|
(setq spacemacs-centered-buffer-mode nil)
|
2016-07-15 08:52:54 +00:00
|
|
|
;; Don't run if the mode is enabled(we are in the indirect buffer).
|
2017-06-28 01:05:50 +00:00
|
|
|
(unless spacemacs--centered-buffer-origin-buffer
|
|
|
|
(when spacemacs-centered-buffer-mode-visual-line-mode
|
|
|
|
(visual-line-mode +1))
|
2016-07-07 17:55:19 +00:00
|
|
|
(let* ((window (selected-window))
|
|
|
|
(origin-buffer (window-buffer window))
|
|
|
|
(indirect-buffer
|
|
|
|
(if (buffer-live-p
|
2017-06-28 01:05:50 +00:00
|
|
|
spacemacs--centered-buffer-indirect-buffer)
|
|
|
|
spacemacs--centered-buffer-indirect-buffer
|
|
|
|
(setq spacemacs--centered-buffer-indirect-buffer
|
2016-07-07 17:55:19 +00:00
|
|
|
(make-indirect-buffer origin-buffer
|
|
|
|
(format "%s(centered)"
|
|
|
|
origin-buffer)
|
|
|
|
t)))))
|
2017-06-28 01:05:50 +00:00
|
|
|
(spacemacs//centered-buffer-buffer-fringr-color-toggle origin-buffer
|
|
|
|
t)
|
|
|
|
(setq spacemacs--centered-buffer-indirect-buffer
|
2016-07-15 14:15:14 +00:00
|
|
|
indirect-buffer
|
|
|
|
spacemacs-centered-buffer-mode-default-fringe-color
|
|
|
|
(face-background 'fringe))
|
2016-07-07 17:55:19 +00:00
|
|
|
(switch-to-buffer indirect-buffer nil t)
|
|
|
|
(with-mode-disabled
|
|
|
|
page-break-lines-mode
|
|
|
|
(let* ((fringe-w (spacemacs//centered-buffer-calc-fringe
|
|
|
|
window)))
|
|
|
|
(if (> fringe-w spacemacs-centered-buffer-mode-min-fringe-width)
|
|
|
|
(progn
|
|
|
|
;; Fix visual glitch.
|
|
|
|
(spacemacs/toggle-line-numbers)
|
|
|
|
(spacemacs/toggle-line-numbers)
|
|
|
|
;;
|
2017-06-28 01:05:50 +00:00
|
|
|
(setq spacemacs--centered-buffer-indirect-buffers
|
2016-07-07 17:55:19 +00:00
|
|
|
(append (list indirect-buffer)
|
2017-06-28 01:05:50 +00:00
|
|
|
spacemacs--centered-buffer-indirect-buffers)
|
|
|
|
spacemacs--centered-buffer-origin-buffer
|
|
|
|
origin-buffer
|
2016-07-07 17:55:19 +00:00
|
|
|
spacemacs-centered-buffer-mode t
|
|
|
|
indicate-empty-lines nil
|
|
|
|
fringes-outside-margins t
|
|
|
|
left-fringe-width fringe-w
|
|
|
|
right-fringe-width fringe-w
|
|
|
|
;; looks better with some margin.
|
|
|
|
left-margin-width (if (or (not left-margin-width)
|
|
|
|
(= left-margin-width 0))
|
|
|
|
1
|
|
|
|
left-margin-width)
|
2017-06-28 01:05:50 +00:00
|
|
|
spacemacs--centered-buffer-text-pixel-size
|
|
|
|
(spacemacs//centered-buffer-window-text-pixel-size
|
|
|
|
window))
|
|
|
|
(face-remap-add-relative
|
|
|
|
'fringe
|
|
|
|
:background
|
|
|
|
spacemacs-centered-buffer-mode-fringe-color)
|
2016-07-07 17:55:19 +00:00
|
|
|
(set-window-buffer window indirect-buffer)
|
2017-06-28 01:05:50 +00:00
|
|
|
(spacemacs//centered-buffer-toggle-hooks t))
|
|
|
|
(setq spacemacs--centered-buffer-origin-buffer nil)
|
2016-07-07 17:55:19 +00:00
|
|
|
(set-buffer origin-buffer)
|
2017-06-28 01:05:50 +00:00
|
|
|
(spacemacs//centered-buffer-buffer-fringr-color-toggle
|
|
|
|
origin-buffer
|
|
|
|
nil)
|
2016-07-07 17:55:19 +00:00
|
|
|
(kill-buffer indirect-buffer)
|
2017-06-28 01:05:50 +00:00
|
|
|
(setq spacemacs--centered-buffer-indirect-buffer nil)
|
2016-07-07 17:55:19 +00:00
|
|
|
(when interact
|
|
|
|
(message "Not enough space to center the buffer!"))))))))
|
|
|
|
|
2017-06-28 01:05:50 +00:00
|
|
|
(defun spacemacs//centered-buffer-on-buffer-update-internal (buffer)
|
|
|
|
"Calls `spacemacs//centered-buffer-update-fringes' on the buffer
|
|
|
|
using `spacemacs--centered-buffer-fontify-region-min-pos' and
|
|
|
|
`spacemacs--centered-buffer-fontify-region-max-pos' as BEGIN END."
|
|
|
|
(when (buffer-live-p buffer)
|
|
|
|
(with-current-buffer buffer
|
|
|
|
(when spacemacs--centered-buffer-origin-buffer
|
|
|
|
(if spacemacs-centered-buffer-mode-only-grow
|
|
|
|
(spacemacs//centered-buffer-update-fringes
|
|
|
|
spacemacs--centered-buffer-fontify-region-min-pos
|
|
|
|
spacemacs--centered-buffer-fontify-region-max-pos
|
|
|
|
buffer)
|
|
|
|
(spacemacs//centered-buffer-update-fringes
|
|
|
|
(point-min)
|
|
|
|
(point-max)
|
|
|
|
buffer)))
|
|
|
|
(setq spacemacs--centered-buffer-fontify-region-min-pos nil
|
|
|
|
spacemacs--centered-buffer-fontify-region-max-pos nil
|
|
|
|
spacemacs--centered-buffer-on-buffer-updated-timer nil))))
|
|
|
|
(byte-compile 'spacemacs//centered-buffer-on-buffer-update-internal)
|
|
|
|
|
|
|
|
(defun spacemacs//centered-buffer-on-buffer-updated (begin end _)
|
|
|
|
"Schedules call to `spacemacs//centered-buffer-on-buffer-update-internal'
|
|
|
|
with `run-with-idle-timer'. BEGIN and END are pushed to
|
|
|
|
`spacemacs--centered-buffer-fontify-region-min-pos' and
|
|
|
|
`spacemacs--centered-buffer-fontify-region-max-pos'"
|
|
|
|
(when spacemacs--centered-buffer-origin-buffer
|
|
|
|
(setq
|
|
|
|
spacemacs--centered-buffer-fontify-region-min-pos
|
|
|
|
(min
|
|
|
|
(or spacemacs--centered-buffer-fontify-region-min-pos
|
|
|
|
most-positive-fixnum)
|
|
|
|
begin)
|
|
|
|
spacemacs--centered-buffer-fontify-region-max-pos
|
|
|
|
(max
|
|
|
|
(or spacemacs--centered-buffer-fontify-region-max-pos
|
|
|
|
most-negative-fixnum)
|
|
|
|
end))
|
|
|
|
(unless spacemacs--centered-buffer-on-buffer-updated-timer
|
|
|
|
(setq
|
|
|
|
spacemacs--centered-buffer-on-buffer-updated-timer
|
|
|
|
(run-with-idle-timer
|
|
|
|
(if spacemacs-centered-buffer-mode-deferred-update 0.5 0)
|
|
|
|
nil
|
|
|
|
'spacemacs//centered-buffer-on-buffer-update-internal
|
|
|
|
(current-buffer))))))
|
|
|
|
(byte-compile 'spacemacs//centered-buffer-on-buffer-updated)
|
|
|
|
|
|
|
|
(defun spacemacs//centered-buffer-toggle-hooks (on?)
|
|
|
|
"Add `centered-buffer-mode' hooks and advices if ON? non-nil
|
|
|
|
otherwise remove them."
|
|
|
|
(if on?
|
|
|
|
(progn
|
|
|
|
(unless (= spacemacs-centered-buffer-mode-min-content-width
|
|
|
|
spacemacs-centered-buffer-mode-max-content-width)
|
|
|
|
(let ((fontify-func (or font-lock-fontify-region-function
|
|
|
|
'font-lock-default-fontify-region)))
|
|
|
|
(unless (memq fontify-func
|
|
|
|
spacemacs--centered-buffer-adviced-fontify-fs)
|
|
|
|
(push fontify-func spacemacs--centered-buffer-adviced-fontify-fs)
|
|
|
|
(advice-add fontify-func
|
|
|
|
:after
|
|
|
|
#'spacemacs//centered-buffer-on-buffer-updated)))
|
|
|
|
(add-hook 'after-change-functions
|
|
|
|
#'spacemacs//centered-buffer-on-buffer-updated
|
|
|
|
nil
|
|
|
|
t))
|
|
|
|
(add-hook 'kill-buffer-hook
|
|
|
|
#'spacemacs//centered-buffer-prune-indirect-buffer-list)
|
|
|
|
(advice-add 'toggle-frame-fullscreen
|
|
|
|
:after
|
|
|
|
#'spacemacs//centered-buffer-window-configuration-change-hk)
|
|
|
|
(advice-add 'toggle-frame-maximized
|
|
|
|
:after
|
|
|
|
#'spacemacs//centered-buffer-window-configuration-change-hk)
|
|
|
|
(add-hook 'window-configuration-change-hook
|
|
|
|
#'spacemacs//centered-buffer-window-configuration-change-hk))
|
|
|
|
(dolist (func spacemacs--centered-buffer-adviced-fontify-fs)
|
|
|
|
(when (fboundp func)
|
|
|
|
(advice-remove func
|
|
|
|
#'spacemacs//centered-buffer-on-buffer-updated)))
|
|
|
|
(setq spacemacs--centered-buffer-adviced-fontify-fs nil)
|
|
|
|
(remove-hook 'after-change-functions
|
|
|
|
#'spacemacs//centered-buffer-on-buffer-updated)
|
|
|
|
(advice-remove 'toggle-frame-maximized
|
|
|
|
#'spacemacs//centered-buffer-window-configuration-change-hk)
|
|
|
|
(advice-remove 'toggle-frame-fullscreen
|
|
|
|
#'spacemacs//centered-buffer-window-configuration-change-hk)
|
|
|
|
(remove-hook 'window-configuration-change-hook
|
|
|
|
#'spacemacs//centered-buffer-window-configuration-change-hk)))
|
2016-07-07 17:55:19 +00:00
|
|
|
|
2017-06-28 01:05:50 +00:00
|
|
|
(defun spacemacs//centered-buffer-disable-branch ()
|
2016-07-15 08:52:54 +00:00
|
|
|
"Used in `spacemacs-centered-buffer-mode'."
|
2016-08-16 16:18:56 +00:00
|
|
|
;; Don't run if the mode is disabled(we are not in the indirect buffer).
|
2017-06-28 01:05:50 +00:00
|
|
|
(when spacemacs--centered-buffer-origin-buffer
|
2016-07-01 18:20:28 +00:00
|
|
|
(let* ((window (selected-window))
|
2017-06-28 01:05:50 +00:00
|
|
|
(origin-buffer spacemacs--centered-buffer-origin-buffer)
|
2016-07-01 18:20:28 +00:00
|
|
|
(indirect-buffer (window-buffer window)))
|
2017-06-28 01:05:50 +00:00
|
|
|
(setq spacemacs--centered-buffer-origin-buffer nil)
|
2016-07-07 17:55:19 +00:00
|
|
|
(switch-to-buffer origin-buffer nil t)
|
2017-06-28 01:05:50 +00:00
|
|
|
(spacemacs//centered-buffer-buffer-fringr-color-toggle origin-buffer
|
|
|
|
nil)
|
|
|
|
(setq spacemacs--centered-buffer-indirect-buffer nil)
|
2016-07-05 21:23:36 +00:00
|
|
|
(when (buffer-live-p indirect-buffer)
|
|
|
|
(dolist (window (get-buffer-window-list indirect-buffer 2))
|
|
|
|
(set-window-buffer window origin-buffer))
|
|
|
|
(kill-buffer indirect-buffer)))))
|
2016-07-01 18:20:28 +00:00
|
|
|
|
2017-06-28 01:05:50 +00:00
|
|
|
(defun spacemacs//centered-buffer-buffer-fringr-color-toggle (buffer flag)
|
2016-07-07 17:55:19 +00:00
|
|
|
"Change fringe color of the BUFFER if FLAG has non-nil value.
|
|
|
|
Revert changes Otherwise."
|
|
|
|
(with-current-buffer buffer
|
|
|
|
(if flag
|
|
|
|
(face-remap-add-relative 'fringe :background
|
|
|
|
spacemacs-centered-buffer-mode-fringe-color)
|
|
|
|
(face-remap-reset-base 'fringe)
|
2017-06-28 01:05:50 +00:00
|
|
|
(face-remap-add-relative
|
|
|
|
'fringe
|
|
|
|
:background
|
|
|
|
spacemacs-centered-buffer-mode-default-fringe-color))))
|
|
|
|
|
|
|
|
(defsubst spacemacs//centered-buffer-window-text-pixel-size (window
|
|
|
|
&optional from to)
|
|
|
|
"Calculate WINDOW text pixel width with `window-text-pixel-size' in
|
|
|
|
FROM TO region
|
|
|
|
and clamp it in between `spacemacs-centered-buffer-mode-min-content-width'
|
|
|
|
and `spacemacs-centered-buffer-mode-max-content-width'.
|
|
|
|
If `spacemacs-centered-buffer-mode-min-content-width' and
|
|
|
|
`spacemacs-centered-buffer-mode-max-content-width' are equal return
|
|
|
|
their value instead."
|
|
|
|
(if (= spacemacs-centered-buffer-mode-min-content-width
|
|
|
|
spacemacs-centered-buffer-mode-max-content-width)
|
|
|
|
spacemacs-centered-buffer-mode-max-content-width
|
|
|
|
(max
|
|
|
|
spacemacs-centered-buffer-mode-min-content-width
|
|
|
|
(car (window-text-pixel-size
|
|
|
|
window
|
|
|
|
from
|
|
|
|
to
|
|
|
|
spacemacs-centered-buffer-mode-max-content-width)))))
|
|
|
|
(byte-compile 'spacemacs//centered-buffer-window-text-pixel-size)
|
2016-07-07 17:55:19 +00:00
|
|
|
|
2017-06-28 01:05:50 +00:00
|
|
|
(defsubst spacemacs//centered-buffer-calc-fringe
|
|
|
|
(&optional window text-pixel-size)
|
2016-07-01 18:20:28 +00:00
|
|
|
"Calculate fringe width for `spacemacs-centered-buffer-mode'.
|
2017-06-28 01:05:50 +00:00
|
|
|
The function Uses TEXT-PIXEL-SIZE if provided,
|
|
|
|
otherwise calculates it with `window-pixel-width'."
|
2016-07-01 18:20:28 +00:00
|
|
|
(-(/ (- (window-pixel-width window)
|
|
|
|
(or text-pixel-size
|
2017-06-28 01:05:50 +00:00
|
|
|
(spacemacs//centered-buffer-window-text-pixel-size
|
|
|
|
window)))
|
2016-07-01 18:20:28 +00:00
|
|
|
2)
|
2016-07-05 21:23:36 +00:00
|
|
|
(if (bound-and-true-p org-indent-mode) 40
|
|
|
|
spacemacs-centered-buffer-mode-safety-gap-width)))
|
2017-06-28 01:05:50 +00:00
|
|
|
(byte-compile 'spacemacs//centered-buffer-calc-fringe)
|
2016-07-01 18:20:28 +00:00
|
|
|
|
2017-06-28 01:05:50 +00:00
|
|
|
(defsubst spacemacs//centered-buffer-buffer-update-window-fringes (window)
|
2016-08-30 09:05:05 +00:00
|
|
|
"Update fringe width of WINDOW if it displays `centered-buffer-mode' buffer."
|
2017-06-28 01:05:50 +00:00
|
|
|
(when (and (buffer-local-value 'spacemacs--centered-buffer-origin-buffer
|
2016-08-30 09:05:05 +00:00
|
|
|
(window-buffer window))
|
|
|
|
;; Might be needed because
|
|
|
|
;; (spacemacs-centered-buffer-mode -1) kills buffers.
|
|
|
|
(buffer-live-p (window-buffer window)))
|
|
|
|
(let ((fringe-w (spacemacs//centered-buffer-calc-fringe
|
2017-06-28 01:05:50 +00:00
|
|
|
window
|
|
|
|
(buffer-local-value
|
|
|
|
'spacemacs--centered-buffer-text-pixel-size
|
|
|
|
(window-buffer window)))))
|
2016-08-30 09:05:05 +00:00
|
|
|
(if (> fringe-w spacemacs-centered-buffer-mode-min-fringe-width)
|
|
|
|
(set-window-fringes window fringe-w fringe-w t)
|
|
|
|
(spacemacs-centered-buffer-mode -1)
|
2017-06-28 01:05:50 +00:00
|
|
|
(when spacemacs--centered-buffer-indirect-buffers
|
2016-08-30 09:05:05 +00:00
|
|
|
(spacemacs//centered-buffer-prune-indirect-buffer-list))))))
|
|
|
|
|
2017-06-28 01:05:50 +00:00
|
|
|
(defun spacemacs//centered-buffer-update-all ()
|
|
|
|
"Update fringe width of all `centered-buffer-mode' windows."
|
|
|
|
(setq spacemacs--centered-buffer-update-all-timer nil)
|
2016-07-05 21:23:36 +00:00
|
|
|
(dolist (frame (frame-list))
|
|
|
|
(when (frame-live-p frame)
|
|
|
|
(dolist (window (window-list frame 2))
|
2016-08-30 09:05:05 +00:00
|
|
|
(spacemacs//centered-buffer-buffer-update-window-fringes window)))))
|
2017-06-28 01:05:50 +00:00
|
|
|
(byte-compile 'spacemacs//centered-buffer-update-all)
|
|
|
|
|
|
|
|
(defun spacemacs//centered-buffer-window-configuration-change-hk ()
|
|
|
|
"Schedules call to `spacemacs//centered-buffer-update-all'."
|
|
|
|
(when (and spacemacs--centered-buffer-indirect-buffers
|
|
|
|
(not spacemacs--centered-buffer-update-all-timer))
|
|
|
|
(setq
|
|
|
|
spacemacs--centered-buffer-update-all-timer
|
|
|
|
(run-with-idle-timer
|
|
|
|
(if spacemacs-centered-buffer-mode-deferred-update 0.5 0)
|
|
|
|
nil
|
|
|
|
'spacemacs//centered-buffer-update-all))))
|
|
|
|
(byte-compile 'spacemacs//centered-buffer-window-configuration-change-hk)
|
2016-08-30 09:05:05 +00:00
|
|
|
|
2017-06-28 01:05:50 +00:00
|
|
|
(defun spacemacs//centered-buffer-update-fringes (begin end buffer)
|
|
|
|
"Check BEGIN END region pixel width and update fringes width
|
|
|
|
if BUFFER's content doesn't fit."
|
|
|
|
(when (buffer-live-p buffer)
|
|
|
|
(with-current-buffer buffer
|
|
|
|
(dolist (window (get-buffer-window-list buffer 2 t))
|
|
|
|
(save-excursion
|
|
|
|
(let* ((min-pos (progn (goto-char begin)
|
|
|
|
(point-at-bol)))
|
|
|
|
(max-pos (progn (goto-char end)
|
|
|
|
(point-at-eol)))
|
|
|
|
(updated-segment-max-width
|
|
|
|
(spacemacs//centered-buffer-window-text-pixel-size
|
|
|
|
window
|
|
|
|
min-pos
|
|
|
|
max-pos)))
|
|
|
|
(when (or (not spacemacs-centered-buffer-mode-only-grow)
|
|
|
|
(> updated-segment-max-width
|
|
|
|
spacemacs--centered-buffer-text-pixel-size))
|
|
|
|
(setq spacemacs--centered-buffer-text-pixel-size
|
|
|
|
updated-segment-max-width))
|
|
|
|
(spacemacs//centered-buffer-buffer-update-window-fringes
|
|
|
|
window)))))))
|
|
|
|
(byte-compile 'spacemacs//centered-buffer-update-fringes)
|
|
|
|
|
|
|
|
(defsubst spacemacs//centered-buffer-prune-indirect-buffer-list ()
|
|
|
|
"Remove dead buffers from `spacemacs--centered-buffer-indirect-buffers'
|
|
|
|
and disables `centered-buffer-mode' hooks and advices if
|
|
|
|
`spacemacs--centered-buffer-indirect-buffers' in empty (nil)."
|
|
|
|
(dolist (buffer spacemacs--centered-buffer-indirect-buffers)
|
|
|
|
(unless (or (buffer-live-p buffer)
|
|
|
|
(not spacemacs--centered-buffer-indirect-buffers))
|
2016-07-05 21:23:36 +00:00
|
|
|
(let ((origin-buffer (buffer-local-value
|
2017-06-28 01:05:50 +00:00
|
|
|
'spacemacs--centered-buffer-origin-buffer
|
2016-07-05 21:23:36 +00:00
|
|
|
buffer)))
|
2017-06-28 01:05:50 +00:00
|
|
|
(setq spacemacs--centered-buffer-indirect-buffers
|
|
|
|
(delete buffer
|
|
|
|
spacemacs--centered-buffer-indirect-buffers))
|
|
|
|
(when (buffer-live-p origin-buffer)
|
|
|
|
(spacemacs//centered-buffer-buffer-fringr-color-toggle
|
|
|
|
origin-buffer nil)
|
|
|
|
(with-current-buffer origin-buffer
|
|
|
|
(setq spacemacs--centered-buffer-indirect-buffers nil))))))
|
2016-07-15 14:15:14 +00:00
|
|
|
;; Remove hooks and advices when they are not needed anymore.
|
2017-06-28 01:05:50 +00:00
|
|
|
(unless spacemacs--centered-buffer-indirect-buffers
|
|
|
|
(spacemacs//centered-buffer-toggle-hooks nil)))
|
2016-07-05 21:23:36 +00:00
|
|
|
|
|
|
|
(defmacro with-mode-disabled (mode &rest body)
|
|
|
|
"Evaluate BODY with MODE disabled."
|
|
|
|
(declare (indent 1) (debug t))
|
|
|
|
`(if (not (bound-and-true-p ,mode))
|
|
|
|
(progn ,@body)
|
|
|
|
(,mode -1)
|
|
|
|
,@body
|
|
|
|
(,mode +1)))
|
2016-07-01 18:20:28 +00:00
|
|
|
|
|
|
|
(provide 'centered-buffer-mode)
|
|
|
|
|
|
|
|
;;; centered-buffer-mode.el ends here
|