simplify space-doc-mode

refactoring
This commit is contained in:
JAremko 2016-07-15 11:52:54 +03:00 committed by Eivind Fonn
parent 7e1eae0b9a
commit 9ba525cdde
3 changed files with 19 additions and 33 deletions

View File

@ -532,7 +532,6 @@ You can modify the list of visual enhancements applied by the =space-doc-mode=:
It's the default value of the variable with all enhancements.
Each function in the list represents some modification.
Some of them might be order dependant so don't shuffle the list!
You can make a modification deferred by moving it to =spacemacs-space-doc-modificators-deferred= list.
** Remap paste key to be able to paste copied text multiple times
In vim and evil, pasting over a text would cause it to be copied, hence making it impossible to paste

View File

@ -62,9 +62,11 @@ that differed modifications won't cause an overflow."
(spacemacs//centered-buffer-mode-disable-branch)))
(defun spacemacs//centered-buffer-mode-enable-branch (interact)
"Assume to be called interactively when INTERACT has non nil value."
"Used it `spacemacs-centered-buffer-mode'.
Assume to be called interactively when INTERACT has non nil value."
;; Mode will be applied to the indirect buffer.
(setq spacemacs-centered-buffer-mode nil)
;; Don't run if the mode is enabled(we are in the indirect buffer).
(unless spacemacs--centered-buffer-mode-origin-buffer
(let* ((window (selected-window))
(origin-buffer (window-buffer window))
@ -131,10 +133,15 @@ that differed modifications won't cause an overflow."
(message "Not enough space to center the buffer!"))))))))
(defun spacemacs//centered-buffer-mode-prev-next-useful-buffer-advice ()
"Disables `spacemacs-centered-buffer-mode' when `spacemacs/previous-useful-buffer'
or `spacemacs/next-useful-buffer' is called. It's better than flagging the original
buffer as 'unuseful'."
(when (bound-and-true-p spacemacs-centered-buffer-mode)
(spacemacs-centered-buffer-mode -1)))
(defun spacemacs//centered-buffer-mode-disable-branch ()
"Used in `spacemacs-centered-buffer-mode'."
;; Don't run if the mode is disabled.
(when spacemacs--centered-buffer-mode-origin-buffer
(let* ((window (selected-window))
(origin-buffer spacemacs--centered-buffer-mode-origin-buffer)
@ -192,8 +199,9 @@ Uses text-pixel-size if provided, otherwise calculates it with `window-pixel-wid
(defun spacemacs//centered-buffer-prune-indirect-buffer-list ()
"Remove indirect buffer from the `spacemacs--centered-buffer-mode-indirect-buffers'
if it not displayed. Disables `centered-buffer-mode' hooks
if `spacemacs--centered-buffer-mode-indirect-buffers' has no elements left(nil)."
if the buffer hasn't at least one live window. Disables `centered-buffer-mode' hooks
and advices if `spacemacs--centered-buffer-mode-indirect-buffers' has no elements left to
minimize the performance hit when the mode isn't used."
(dolist (buffer spacemacs--centered-buffer-mode-indirect-buffers)
(unless (and (buffer-live-p buffer)
(window-live-p (get-buffer-window buffer t)))

View File

@ -46,8 +46,7 @@ keeping their content visible.
(spacemacs-centered-buffer-mode -1)
(setq space-doc-mode old-flag))
(if (derived-mode-p 'org-mode)
(dolist (modificator (append '(spacemacs//space-doc-set-cache
spacemacs//space-doc-runs-deferred)
(dolist (modificator (append (list 'spacemacs//space-doc-set-cache)
spacemacs-space-doc-modificators))
(funcall modificator space-doc-mode))
(message (format "space-doc-mode error:%s isn't an org-mode buffer"
@ -72,21 +71,14 @@ The functions work with a current buffer and accept ENABLE(flag) argument.
If the argument has non-nil value - enable the modifications introduced
by the function. Otherwise - disable.")
;; NOTE: Dont forget to update Spacemacs FAQ if you modify this list!
(defvar spacemacs-space-doc-modificators-deferred
'()
"Same as `spacemacs-space-doc-modificators' but the modificators will
be run deferred.")
(defun spacemacs//space-doc-centered-buffer-mode-hook ()
"Applies `spacemacs//space-doc-emphasis-overlays' to the
indirect buffer (because overlays aren't copied for the original one)."
indirect buffer (because overlays aren't copied from the original one).
And calculates `spacemacs//space-doc-set-cache' (it's buffer local)."
(when space-doc-mode
(spacemacs//space-doc-set-cache t)
(when (or (member 'spacemacs//space-doc-emphasis-overlays
spacemacs-space-doc-modificators)
(member 'spacemacs//space-doc-emphasis-overlays
spacemacs-space-doc-modificators-deferred))
(when (member 'spacemacs//space-doc-emphasis-overlays
spacemacs-space-doc-modificators)
(spacemacs//space-doc-emphasis-overlays t))))
(when (member 'spacemacs//space-doc-center-buffer-mode
@ -98,12 +90,9 @@ indirect buffer (because overlays aren't copied for the original one)."
"Enable `spacemacs-centered-buffer-mode' if flag is non nil, disable it otherwise.
This functions is aimed to be used with `spacemacs-space-doc-modificators'."
(when (and flag (not spacemacs-centered-buffer-mode))
(run-with-idle-timer 0
nil
(lambda ()
(spacemacs-centered-buffer-mode +1)
(spacemacs//space-doc-set-cache t)
(spacemacs//space-doc-emphasis-overlays t)))))
;; `spacemacs-centered-buffer-mode' is heavy so it's better to make if deferred.
;; Also it has to be run when the `window-body-width' is properly calculated.
(run-with-idle-timer 0 nil 'spacemacs-centered-buffer-mode)))
(defun spacemacs//space-doc-org-indent-mode (&optional flag)
"Enable `org-indent-mode' if flag is non nil, disable it otherwise.
@ -407,16 +396,6 @@ Open all other links with `browse-url'."
'subtree)
(browse-url (concat "https://" path)))))
(defun spacemacs//space-doc-runs-deferred (&optional flag)
"Run each modificator function from the
`spacemacs-space-doc-modificators-deferred' list
in the next command loop. This way heavy modificator functions
won't affect document opening time. FLAG is passed through."
(run-with-idle-timer 0 nil (lambda (flag)
(dolist (modf spacemacs-space-doc-modificators-deferred)
(funcall modf flag)))
flag))
(defun spacemacs//space-doc-find-regions-by-text-property
(property value &optional start end)
"Return a list of pairs (region-beginning region-end) in