Last active
August 29, 2015 14:01
-
-
Save ajchemist/065e69d8e9a60003b243 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;;; usage -*- lexical-binding: t; -*- | |
;;; download this gist file, `winnum.el' and place it on one of your `load-path'. | |
;;; now, M-x byte-compile-file winnum.el RET and then M-x load-library winnum RET. | |
;;; f8 or super-o keybinding do all the fun! | |
;;; winnum id is on a mode-line. | |
;;; | |
;;; | |
;;; -UUU:**-1-F1 *scratch* All (1,0) (Fundamental yas) --------------- | |
;;; ^ | |
;;; +---this! | |
;;; winnum fix starts here | |
(eval-when-compile | |
(require 'cl-lib) | |
(require 'windmove)) | |
(defface winnum | |
'((((type tty) (class color)) :foreground "#d54e53" :weight bold) | |
(((type tty) (class mono)) :inverse-video t :weight bold) | |
(((type x w32 ns)) :foreground "#d54e53" :weight bold) | |
(t :weight bold)) | |
"The face used for the window number in the mode-line." | |
:group 'faces) | |
(global-set-key (kbd "s-o") 'winnum-select) | |
(global-set-key [f8] 'winnum-select) | |
(defun winnum-list () | |
"Returns a list of the windows, in fixed order and the | |
minibuffer (even if not active) last." | |
(let* ((walk-windows-start | |
(car (cl-set-difference | |
(window-list (selected-frame) t) | |
(window-list (selected-frame) 0)))) | |
(walk-windows-current walk-windows-start) | |
list) | |
(while (progn | |
(setq walk-windows-current | |
(next-window walk-windows-current t)) | |
(setq list (cons walk-windows-current list)) | |
(not (eq walk-windows-current walk-windows-start)))) | |
(reverse list))) | |
;; prefix n \C-xo | |
;; ASCII(7) | |
(defun winnum-select () | |
"Selects the nth window." | |
(interactive) | |
(let ((<-> '(left right up down)) _ num) | |
(catch 'arrow% | |
(while (or (or (when (memq _ <->) | |
(throw 'arrow% | |
(windmove-do-window-select _))) | |
(not (characterp _))) | |
(<= _ 48) ; 0 not included | |
(and (> _ 57) (< _ 97)) | |
(> _ 122)) | |
(setq _ (read-event | |
#("Switch to Winnum: " 0 18 | |
(face minibuffer-prompt))))) | |
(cond ((> _ 96) (setq num (- _ 87))) | |
(t (setq num (- _ 48)))) | |
(let ((window (nth (1- num) (winnum-list)))) | |
(if (and window (or (not (window-minibuffer-p window)) | |
(minibuffer-window-active-p window))) | |
(select-window window) | |
(error "No such window.")))))) | |
(defun winnum-string () | |
"Returns the string containing the number of the current window" | |
(let* ((_ (length (memq ;Returns the the number of the current window. | |
(selected-window) (nreverse (winnum-list))))) | |
(nstr (cond ((< _ 16) (format "%x" _)) | |
(t (format "%c" (+ _ 87)))))) | |
(propertize nstr 'face 'winnum))) | |
(let* ((copy mode-line-format) | |
(q (memq 'mode-line-remote copy))) | |
(unless (listp (cadr q)) | |
(setcdr q (cons '(:eval (winnum-string)) (cdr q))))) | |
;;; winnum fix ends here | |
;;; flash effect starts here | |
(let ((wsflash:face-bg "#e50000") | |
(wsflash:face-fg "#eaeaea") | |
(wsflash:flash-time 0.4) | |
wsflash:timer wsflash:engaged wsflash:saved-mode-line-faces) | |
(defmacro wsflash-start-timer (timer secs func) | |
"Cancel TIMER, if valid, then run for SECS seconds before executing FUNC. | |
TIMER is a symbol; SECS is a number or time; and FUNC is a function, | |
usually a quoted symbol. SECS and FUNC are interpreted as in the corresponding | |
arguments to `run-with-idle-timer'." | |
`(progn | |
(when (and ,timer (timerp ,timer)) | |
(cancel-timer ,timer)) | |
(setq ,timer (run-with-idle-timer ,secs nil ,func)))) | |
(defmacro wsflash-clear-timer (timer) | |
"Cancel TIMER, if valid, then null the symbol's value." | |
`(progn | |
(when ,timer | |
(when (timerp ,timer) | |
(cancel-timer ,timer)) | |
(setq ,timer nil)))) | |
(defun wsflash-start () | |
(unless wsflash:engaged | |
(wsflash-start-timer wsflash:timer | |
wsflash:flash-time | |
#'wsflash-out) | |
(setq wsflash:engaged t) | |
;; face transition | |
(setq wsflash:saved-mode-line-faces | |
(cons (face-attribute 'mode-line :background) | |
(face-attribute 'mode-line :foreground))) | |
(set-face-background 'mode-line wsflash:face-bg) | |
(set-face-foreground 'mode-line wsflash:face-fg))) | |
(defun wsflash-out () | |
(when wsflash:engaged | |
(wsflash-clear-timer wsflash:timer) | |
(setq wsflash:engaged nil) | |
;; face transition | |
(when wsflash:saved-mode-line-faces | |
(set-face-background 'mode-line (car wsflash:saved-mode-line-faces)) | |
(set-face-foreground 'mode-line (cdr wsflash:saved-mode-line-faces)))))) | |
;; (defadvice select-window (after wsflash-select-window activate) | |
;; (wsflash-start)) | |
(defadvice other-window (after wsflash-select-window activate) | |
(wsflash-start)) | |
(defadvice winnum-select (after wsflash-select-window activate) | |
(wsflash-start)) | |
(defadvice windmove-do-window-select (after wsflash-select-window activate) | |
(wsflash-start)) | |
;;; flash effect starts here |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment