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. It's the default value of the variable with all enhancements.
Each function in the list represents some modification. Each function in the list represents some modification.
Some of them might be order dependant so don't shuffle the list! 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 ** 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 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))) (spacemacs//centered-buffer-mode-disable-branch)))
(defun spacemacs//centered-buffer-mode-enable-branch (interact) (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. ;; Mode will be applied to the indirect buffer.
(setq spacemacs-centered-buffer-mode nil) (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 (unless spacemacs--centered-buffer-mode-origin-buffer
(let* ((window (selected-window)) (let* ((window (selected-window))
(origin-buffer (window-buffer 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!")))))))) (message "Not enough space to center the buffer!"))))))))
(defun spacemacs//centered-buffer-mode-prev-next-useful-buffer-advice () (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) (when (bound-and-true-p spacemacs-centered-buffer-mode)
(spacemacs-centered-buffer-mode -1))) (spacemacs-centered-buffer-mode -1)))
(defun spacemacs//centered-buffer-mode-disable-branch () (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 (when spacemacs--centered-buffer-mode-origin-buffer
(let* ((window (selected-window)) (let* ((window (selected-window))
(origin-buffer spacemacs--centered-buffer-mode-origin-buffer) (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 () (defun spacemacs//centered-buffer-prune-indirect-buffer-list ()
"Remove indirect buffer from the `spacemacs--centered-buffer-mode-indirect-buffers' "Remove indirect buffer from the `spacemacs--centered-buffer-mode-indirect-buffers'
if it not displayed. Disables `centered-buffer-mode' hooks if the buffer hasn't at least one live window. Disables `centered-buffer-mode' hooks
if `spacemacs--centered-buffer-mode-indirect-buffers' has no elements left(nil)." 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) (dolist (buffer spacemacs--centered-buffer-mode-indirect-buffers)
(unless (and (buffer-live-p buffer) (unless (and (buffer-live-p buffer)
(window-live-p (get-buffer-window buffer t))) (window-live-p (get-buffer-window buffer t)))

View File

@ -46,8 +46,7 @@ keeping their content visible.
(spacemacs-centered-buffer-mode -1) (spacemacs-centered-buffer-mode -1)
(setq space-doc-mode old-flag)) (setq space-doc-mode old-flag))
(if (derived-mode-p 'org-mode) (if (derived-mode-p 'org-mode)
(dolist (modificator (append '(spacemacs//space-doc-set-cache (dolist (modificator (append (list 'spacemacs//space-doc-set-cache)
spacemacs//space-doc-runs-deferred)
spacemacs-space-doc-modificators)) spacemacs-space-doc-modificators))
(funcall modificator space-doc-mode)) (funcall modificator space-doc-mode))
(message (format "space-doc-mode error:%s isn't an org-mode buffer" (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 If the argument has non-nil value - enable the modifications introduced
by the function. Otherwise - disable.") 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 () (defun spacemacs//space-doc-centered-buffer-mode-hook ()
"Applies `spacemacs//space-doc-emphasis-overlays' to the "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 (when space-doc-mode
(spacemacs//space-doc-set-cache t) (spacemacs//space-doc-set-cache t)
(when (or (member 'spacemacs//space-doc-emphasis-overlays (when (member 'spacemacs//space-doc-emphasis-overlays
spacemacs-space-doc-modificators) spacemacs-space-doc-modificators)
(member 'spacemacs//space-doc-emphasis-overlays
spacemacs-space-doc-modificators-deferred))
(spacemacs//space-doc-emphasis-overlays t)))) (spacemacs//space-doc-emphasis-overlays t))))
(when (member 'spacemacs//space-doc-center-buffer-mode (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. "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'." This functions is aimed to be used with `spacemacs-space-doc-modificators'."
(when (and flag (not spacemacs-centered-buffer-mode)) (when (and flag (not spacemacs-centered-buffer-mode))
(run-with-idle-timer 0 ;; `spacemacs-centered-buffer-mode' is heavy so it's better to make if deferred.
nil ;; Also it has to be run when the `window-body-width' is properly calculated.
(lambda () (run-with-idle-timer 0 nil 'spacemacs-centered-buffer-mode)))
(spacemacs-centered-buffer-mode +1)
(spacemacs//space-doc-set-cache t)
(spacemacs//space-doc-emphasis-overlays t)))))
(defun spacemacs//space-doc-org-indent-mode (&optional flag) (defun spacemacs//space-doc-org-indent-mode (&optional flag)
"Enable `org-indent-mode' if flag is non nil, disable it otherwise. "Enable `org-indent-mode' if flag is non nil, disable it otherwise.
@ -407,16 +396,6 @@ Open all other links with `browse-url'."
'subtree) 'subtree)
(browse-url (concat "https://" path))))) (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 (defun spacemacs//space-doc-find-regions-by-text-property
(property value &optional start end) (property value &optional start end)
"Return a list of pairs (region-beginning region-end) in "Return a list of pairs (region-beginning region-end) in