2016-01-30 21:16:10 -05:00
|
|
|
;;; core-popup.el --- taming stray windows
|
2015-11-19 05:50:04 -05:00
|
|
|
|
2015-12-21 05:44:44 -05:00
|
|
|
(use-package shackle
|
2015-11-19 05:50:04 -05:00
|
|
|
:config
|
2015-12-21 05:44:44 -05:00
|
|
|
(shackle-mode 1)
|
|
|
|
(setq shackle-rules
|
|
|
|
'(;; Plugins
|
2015-12-29 23:13:45 -05:00
|
|
|
("\\` ?\\*[hH]elm.*?\\*\\'" :regexp t :align below :size 20 :select t)
|
2016-02-23 13:08:07 -05:00
|
|
|
(" ?\\*Flycheck.+\\*" :regexp t :align below :size 15 :noselect t)
|
2015-12-23 02:42:45 -05:00
|
|
|
(" *NeoTree*" :align left :select t)
|
|
|
|
("*evil-registers*" :align below :size 0.3)
|
|
|
|
("*quickrun*" :align below :size 15 :noselect t)
|
|
|
|
("*eval*" :align below :size 15)
|
2016-01-02 15:06:16 -05:00
|
|
|
("*esup*" :align below :size 30 :noselect t)
|
2016-03-03 01:38:37 -05:00
|
|
|
("*ert*" :align below :size 20 :noselect t)
|
2015-12-09 02:03:13 -05:00
|
|
|
|
|
|
|
;; vcs
|
2015-12-23 02:42:45 -05:00
|
|
|
("^\\*git-gutter.+\\*$" :regexp t :align below :size 0.4 :noselect t)
|
|
|
|
("*vc-diff*" :align below :size 0.4 :noselect t)
|
|
|
|
("*vc-change-log*" :align below :select t)
|
2015-12-21 05:44:44 -05:00
|
|
|
(vc-annotate-mode :same t)
|
2015-12-09 02:03:13 -05:00
|
|
|
|
2015-12-23 02:42:45 -05:00
|
|
|
("*Apropos*" :align below :size 0.3)
|
|
|
|
("*minor-modes*" :align below :size 0.5 :noselect t)
|
2015-11-30 16:50:04 -05:00
|
|
|
|
|
|
|
;; Org
|
2015-12-23 02:42:45 -05:00
|
|
|
("^\\*Org Src .+\\*$" :regexp t :align below :size 0.4 :select t)
|
|
|
|
("^\\*Org-Babel.*\\*$" :regexp t :align below :size 0.4)
|
|
|
|
(org-agenda-mode :align below :size 0.4)
|
2016-02-06 22:23:11 -05:00
|
|
|
("*Org Agenda*" :align below :size 0.4)
|
2015-12-23 02:42:45 -05:00
|
|
|
("*Agenda Commands*" :align below :size 0.5)
|
2015-12-29 23:13:45 -05:00
|
|
|
(" *Org todo*" :align below :size 5 :noselect t)
|
|
|
|
("*Org Links*" :align below :size 5)
|
2015-12-21 05:44:44 -05:00
|
|
|
|
|
|
|
;; Emacs
|
2015-12-23 02:42:45 -05:00
|
|
|
("^\\*.+-Profiler-Report .+\\*$" :regexp t :align below :size 0.3)
|
|
|
|
("*Backtrace*" :align below :size 0.25 :noselect t)
|
|
|
|
("*scratch*" :align below :size 0.3 :select t)
|
2016-03-03 01:38:45 -05:00
|
|
|
("*Help*" :align below :size 30 :select t)
|
2015-12-29 23:13:45 -05:00
|
|
|
("*Messages*" :align below :size 20 :select t)
|
2016-02-17 07:31:05 -05:00
|
|
|
("*Completions*" :align below :size 20 :noselect t)
|
2015-12-23 02:42:45 -05:00
|
|
|
(debugger-mode :align below :size 0.25 :noselect t)
|
2015-12-21 05:44:44 -05:00
|
|
|
(compilation-mode :noselect t)
|
2015-12-09 02:09:47 -05:00
|
|
|
|
|
|
|
;; REPLs
|
2015-12-21 05:44:44 -05:00
|
|
|
((:custom (lambda (b &rest _)
|
2015-12-23 21:57:12 -05:00
|
|
|
(when (featurep 'repl-toggle)
|
2015-12-23 02:42:45 -05:00
|
|
|
(when (string-prefix-p "*" (buffer-name (get-buffer b)))
|
|
|
|
(with-current-buffer b repl-p)))))
|
2016-02-19 12:35:05 -05:00
|
|
|
:popup t :align below :size 16)))
|
2015-11-19 05:50:04 -05:00
|
|
|
|
2016-03-03 01:38:37 -05:00
|
|
|
(after! ert
|
2016-03-04 22:39:48 -05:00
|
|
|
(add-hook! 'ert-results-mode-hook 'narf|hide-mode-line)
|
2016-03-03 01:38:37 -05:00
|
|
|
(map! (:map ert-results-mode-map
|
|
|
|
[escape] 'quit-window
|
|
|
|
"<escape>" 'quit-window)))
|
|
|
|
|
2016-03-01 02:09:52 -05:00
|
|
|
(after! help-mode
|
|
|
|
;; So that help buffer links do not open in the help popup, we need to redefine these
|
|
|
|
;; three button types to use `switch-to-buffer-other-window' rather than
|
|
|
|
;; `pop-to-buffer'. Instead of lambas these help-functions should be function symbols,
|
|
|
|
;; so that we could advise those instead of clumsify redefine these button types.
|
|
|
|
(define-button-type 'help-function-def
|
|
|
|
:supertype 'help-xref
|
|
|
|
'help-function (lambda (fun file)
|
|
|
|
(require 'find-func)
|
|
|
|
(when (eq file 'C-source)
|
|
|
|
(setq file (help-C-file-name (indirect-function fun) 'fun)))
|
|
|
|
(let ((location
|
|
|
|
(find-function-search-for-symbol fun nil file)))
|
|
|
|
(switch-to-buffer-other-window (car location))
|
|
|
|
(if (cdr location)
|
|
|
|
(goto-char (cdr location))
|
|
|
|
(message "Unable to find location in file"))))
|
|
|
|
'help-echo (purecopy "mouse-2, RET: find function's definition"))
|
|
|
|
|
|
|
|
(define-button-type 'help-variable-def
|
|
|
|
:supertype 'help-xref
|
|
|
|
'help-function (lambda (var &optional file)
|
|
|
|
(when (eq file 'C-source)
|
|
|
|
(setq file (help-C-file-name var 'var)))
|
|
|
|
(let ((location (find-variable-noselect var file)))
|
|
|
|
(switch-to-buffer-other-window (car location))
|
|
|
|
(if (cdr location)
|
|
|
|
(goto-char (cdr location))
|
|
|
|
(message "Unable to find location in file"))))
|
|
|
|
'help-echo (purecopy "mouse-2, RET: find variable's definition"))
|
|
|
|
|
|
|
|
(define-button-type 'help-face-def
|
|
|
|
:supertype 'help-xref
|
|
|
|
'help-function (lambda (fun file)
|
|
|
|
(require 'find-func)
|
|
|
|
(let ((location
|
|
|
|
(find-function-search-for-symbol fun 'defface file)))
|
|
|
|
(switch-to-buffer-other-window (car location))
|
|
|
|
(if (cdr location)
|
|
|
|
(goto-char (cdr location))
|
|
|
|
(message "Unable to find location in file"))))
|
|
|
|
'help-echo (purecopy "mouse-2, RET: find face's definition")))
|
|
|
|
|
2015-12-21 05:44:44 -05:00
|
|
|
(after! helm
|
|
|
|
;; This is a good alternative to either popwin or shackle, specifically for helm. If
|
|
|
|
;; either fail me (for the last time), this is where I'll turn.
|
|
|
|
;;(add-to-list 'display-buffer-alist
|
|
|
|
;; `(,(rx bos "*helm" (* not-newline) "*" eos)
|
|
|
|
;; (display-buffer-in-side-window)
|
|
|
|
;; (inhibit-same-window . t)
|
|
|
|
;; (window-height . 0.4)))
|
2015-11-19 05:50:04 -05:00
|
|
|
|
2015-12-21 05:44:44 -05:00
|
|
|
;; Helm tries to clean up after itself, but shackle has already done this. This fixes
|
|
|
|
;; that. To reproduce, add a helm rule in `shackle-rules', open two splits
|
|
|
|
;; side-by-side, move to the buffer on the right and invoke helm. It will close all
|
2016-03-01 02:05:04 -05:00
|
|
|
;; but the left-most buffer.
|
2015-12-21 05:44:44 -05:00
|
|
|
(setq-default helm-split-window-in-side-p t))
|
2015-11-19 05:50:04 -05:00
|
|
|
|
2016-01-29 06:58:38 -05:00
|
|
|
(after! helm-swoop
|
|
|
|
(setq helm-swoop-split-window-function (lambda ($buf) (narf/popup-buffer $buf))))
|
|
|
|
|
2015-12-21 05:44:44 -05:00
|
|
|
(after! helm-ag
|
|
|
|
;; Helm-ag needs a little coaxing for it to cooperate with shackle. Mostly to prevent
|
|
|
|
;; it from switching between windows and buffers.
|
|
|
|
(defadvice helm-ag--edit-abort (around helm-ag-edit-abort-popup-compat activate)
|
|
|
|
(cl-letf (((symbol-function 'select-window) 'ignore)) ad-do-it)
|
|
|
|
(narf/popup-close nil t t))
|
|
|
|
(defadvice helm-ag--edit-commit (around helm-ag-edit-commit-popup-compat activate)
|
|
|
|
(cl-letf (((symbol-function 'select-window) 'ignore)) ad-do-it)
|
|
|
|
(narf/popup-close nil t t))
|
|
|
|
(defadvice helm-ag--edit (around helm-ag-edit-popup-compat activate)
|
|
|
|
(cl-letf (((symbol-function 'other-window) 'ignore)
|
|
|
|
((symbol-function 'switch-to-buffer) 'narf/popup-buffer))
|
|
|
|
ad-do-it)))
|
2015-11-19 05:50:04 -05:00
|
|
|
|
|
|
|
(after! quickrun
|
2015-12-21 05:44:44 -05:00
|
|
|
;; This allows us to run code several times in a row without having to close the popup
|
|
|
|
;; window and move back to the code buffer.
|
2015-12-29 23:13:45 -05:00
|
|
|
(defun narf*quickrun-close-popup (&optional _ _ _ _)
|
2015-12-21 05:44:44 -05:00
|
|
|
(let* ((buffer (get-buffer quickrun/buffer-name))
|
|
|
|
(window (and buffer (get-buffer-window buffer))))
|
|
|
|
(when buffer
|
|
|
|
(shut-up! (quickrun/kill-running-process))
|
|
|
|
(narf/popup-close window nil t))))
|
2015-12-29 23:13:45 -05:00
|
|
|
(advice-add 'quickrun :before 'narf*quickrun-close-popup)
|
|
|
|
(advice-add 'quickrun-region :before 'narf*quickrun-close-popup)
|
2015-11-19 05:50:04 -05:00
|
|
|
|
2016-03-04 22:37:51 -05:00
|
|
|
;; Turns on `nlinum-mode', and ensures window is scrolled to EOF
|
2015-12-21 05:44:44 -05:00
|
|
|
(defun narf|quickrun-after-run ()
|
|
|
|
(let ((window (get-buffer-window quickrun/buffer-name)))
|
|
|
|
(with-selected-window window
|
|
|
|
(narf|nlinum-enable)
|
2016-03-04 22:39:48 -05:00
|
|
|
(narf|hide-mode-line)
|
2015-12-21 05:44:44 -05:00
|
|
|
(let* ((lines (count-lines (point-min) (point-max)))
|
|
|
|
(act-lines (max 5 (min 30 lines))))
|
|
|
|
(set-window-start window (evil-line-position (+ 2 (- lines act-lines))))
|
2016-03-04 22:37:51 -05:00
|
|
|
(evil-resize-window act-lines)))))
|
2015-12-21 05:44:44 -05:00
|
|
|
(add-hook 'quickrun-after-run-hook 'narf|quickrun-after-run)
|
|
|
|
|
|
|
|
;; I let `narf|quickrun-after-run' handle scrolling, so quickrun shouldn't have to!
|
|
|
|
(advice-add 'quickrun/recenter :override 'ignore))
|
|
|
|
|
|
|
|
|
|
|
|
(after! repl-toggle
|
|
|
|
(map! :map repl-toggle-mode-map
|
|
|
|
"ESC ESC" 'narf/popup-close))
|
2015-11-19 05:50:04 -05:00
|
|
|
|
2015-11-20 21:47:45 -05:00
|
|
|
(add-hook! org-load
|
2015-12-21 05:44:44 -05:00
|
|
|
;; This ensures org-src-edit yields control of its buffer to shackle.
|
|
|
|
(defun org-src-switch-to-buffer (buffer context)
|
|
|
|
(pop-to-buffer buffer))
|
|
|
|
|
|
|
|
;; And these for org-todo, org-link and agenda
|
|
|
|
(defun org-pop-to-buffer-same-window (&optional buffer-or-name norecord label)
|
|
|
|
"Pop to buffer specified by BUFFER-OR-NAME in the selected window."
|
|
|
|
(display-buffer buffer-or-name))
|
2015-11-20 21:47:45 -05:00
|
|
|
|
|
|
|
(defun org-switch-to-buffer-other-window (&rest args)
|
|
|
|
(mapc (lambda (b)
|
2015-11-23 15:21:24 -05:00
|
|
|
(let ((buf (if (stringp b) (get-buffer-create b) b)))
|
2015-12-21 05:44:44 -05:00
|
|
|
(pop-to-buffer buf t t)))
|
2015-11-20 21:47:45 -05:00
|
|
|
args)))
|
|
|
|
|
2016-01-07 01:14:16 -05:00
|
|
|
(after! flycheck
|
|
|
|
(map! :map flycheck-error-list-mode-map
|
|
|
|
:n "q" 'narf/popup-close
|
|
|
|
:n [escape] 'narf/popup-close))
|
|
|
|
|
2015-11-19 05:50:04 -05:00
|
|
|
|
2015-12-21 05:44:44 -05:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
2015-11-19 05:50:04 -05:00
|
|
|
|
2015-12-21 05:44:44 -05:00
|
|
|
(defvar narf-popup-windows '()
|
|
|
|
"A list of windows that have been opened via shackle. Do not touch this!")
|
2015-11-19 05:50:04 -05:00
|
|
|
|
2015-12-21 05:44:44 -05:00
|
|
|
(defun narf*popup-add (&rest _)
|
|
|
|
(add-to-list 'narf-popup-windows (get-buffer-window shackle-last-buffer)))
|
|
|
|
(advice-add 'shackle-display-buffer :after 'narf*popup-add)
|
|
|
|
|
|
|
|
(defun narf--popup-remove (window)
|
|
|
|
(setq narf-popup-windows (delete window narf-popup-windows)))
|
|
|
|
|
|
|
|
(defun narf/popup-p (&optional window)
|
2016-03-01 02:05:04 -05:00
|
|
|
"Whether WINDOW is a shackle popup window or not."
|
2015-12-21 05:44:44 -05:00
|
|
|
(and narf-popup-windows
|
|
|
|
(-any? (lambda (w)
|
|
|
|
(if (window-live-p w) t (narf--popup-remove w) nil))
|
|
|
|
narf-popup-windows)
|
|
|
|
(if window
|
|
|
|
(-any? (lambda (w) (eq window w)) narf-popup-windows)
|
|
|
|
t)))
|
|
|
|
|
|
|
|
(defun narf/popup-buffer (buffer &optional plist)
|
2016-03-01 02:05:04 -05:00
|
|
|
"Display BUFFER in a shackle popup."
|
2015-12-21 05:44:44 -05:00
|
|
|
(let ((buffer-name (if (stringp buffer) buffer (buffer-name buffer))))
|
|
|
|
(shackle-display-buffer (get-buffer-create buffer-name)
|
|
|
|
nil (or plist (shackle-match buffer-name)))))
|
|
|
|
|
|
|
|
(defun narf/popup-close (&optional window dont-kill dont-close-all)
|
2016-03-01 02:05:04 -05:00
|
|
|
"Find and close the currently active popup (if available)."
|
2015-12-21 05:44:44 -05:00
|
|
|
(interactive)
|
|
|
|
(when (not window)
|
|
|
|
(if (narf/popup-p (selected-window))
|
|
|
|
(setq window (selected-window))
|
|
|
|
(unless dont-close-all
|
2016-03-04 22:40:35 -05:00
|
|
|
(narf/popup-close-all dont-kill))))
|
2015-12-21 05:44:44 -05:00
|
|
|
(when (and window (window-live-p window))
|
|
|
|
;; REPL buffer
|
|
|
|
(cond ((and (derived-mode-p 'comint-mode)
|
|
|
|
(featurep 'repl-toggle)
|
|
|
|
repl-toggle-mode)
|
2016-02-20 15:32:19 -05:00
|
|
|
(setq rtog/--last-buffer nil))
|
2015-12-21 05:44:44 -05:00
|
|
|
((eq major-mode 'messages-buffer-mode)
|
|
|
|
(bury-buffer)
|
|
|
|
(setq dont-kill t)))
|
|
|
|
(narf--popup-remove window)
|
|
|
|
(unless dont-kill
|
|
|
|
(kill-buffer (window-buffer window)))
|
|
|
|
(delete-window window)))
|
|
|
|
|
2016-03-04 22:40:35 -05:00
|
|
|
(defun narf/popup-close-all (&optional dont-kill-buffers)
|
2016-03-01 02:05:04 -05:00
|
|
|
"Closes all popup windows (and kills the buffers if DONT-KILL-BUFFERS is non-nil)"
|
2015-12-21 05:44:44 -05:00
|
|
|
(interactive)
|
|
|
|
(mapc (lambda (w) (narf/popup-close w dont-kill-buffers))
|
|
|
|
narf-popup-windows)
|
|
|
|
(setq narf-popup-windows nil))
|
|
|
|
|
2016-03-01 02:05:04 -05:00
|
|
|
;;
|
2015-11-19 05:50:04 -05:00
|
|
|
(defun narf/popup-toggle ()
|
2016-03-01 02:05:04 -05:00
|
|
|
"Toggles the popup window, reopening the last popup (if available)."
|
2015-11-19 05:50:04 -05:00
|
|
|
(interactive)
|
2015-12-21 05:44:44 -05:00
|
|
|
(if (narf/popup-p)
|
|
|
|
(narf/popup-close t)
|
|
|
|
(narf/popup-last-buffer)))
|
2015-11-19 05:50:04 -05:00
|
|
|
|
2015-12-21 05:44:44 -05:00
|
|
|
(defun narf/popup-last-buffer ()
|
2016-03-01 02:05:04 -05:00
|
|
|
"Pop up the last popup buffer."
|
2015-11-19 05:50:04 -05:00
|
|
|
(interactive)
|
2015-12-21 05:44:44 -05:00
|
|
|
(if shackle-last-buffer
|
|
|
|
(narf/popup-buffer shackle-last-buffer)
|
|
|
|
(narf/popup-messages)))
|
2015-11-19 05:50:04 -05:00
|
|
|
|
2015-12-21 05:44:44 -05:00
|
|
|
(defun narf/popup-messages ()
|
2016-03-01 02:05:04 -05:00
|
|
|
"Pop up the *Messages* buffer."
|
2015-11-19 05:50:04 -05:00
|
|
|
(interactive)
|
2015-12-23 02:42:45 -05:00
|
|
|
(narf/popup-buffer "*Messages*")
|
2015-12-29 23:13:57 -05:00
|
|
|
(with-current-buffer "*Messages*"
|
2016-03-04 22:39:48 -05:00
|
|
|
(narf|hide-mode-line)
|
2015-12-29 23:13:57 -05:00
|
|
|
(goto-char (point-max)))))
|
2015-12-21 05:44:44 -05:00
|
|
|
|
2015-11-19 05:50:04 -05:00
|
|
|
(provide 'core-popup)
|
|
|
|
;;; core-popup.el ends here
|