Applied purcell's recommendations

This commit is contained in:
Lars Tveito 2015-05-17 12:36:20 +02:00
parent b4fb132314
commit c89d08f510

View File

@ -6,7 +6,7 @@
;; URL: http://github.com/larstvei/Focus ;; URL: http://github.com/larstvei/Focus
;; Created: 11th May 2015 ;; Created: 11th May 2015
;; Version: 0.0.1 ;; Version: 0.0.1
;; Package-Requires: ((emacs "24") (cl-lib "1.0")) ;; Package-Requires: ((emacs "24") (cl-lib "0.5"))
;; This program is free software; you can redistribute it and/or modify ;; 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 ;; it under the terms of the GNU General Public License as published by
@ -34,20 +34,22 @@
(require 'thingatpt) (require 'thingatpt)
(defcustom focus-dimness 0 (defcustom focus-dimness 0
"When `focus-mode' is enabled, the dimness of the sections that "Amount of dimness in out of focus sections is determined by this integer.
are out of focus is determined by this integer. A positive value
increases the dimness of the sections, whilst a negative value
decreases the dimness.
The default is 0 which means a 50/50 mixture of the background A positive value increases the dimness of the sections. A
and foreground color."
negative value decreases the dimness.
The default is 0 which means a 50/50 mixture of the background and foreground
color."
:type '(integer) :type '(integer)
:group 'focus) :group 'focus)
(defcustom focus-mode-to-thing '((prog-mode . defun) (text-mode . sentence)) (defcustom focus-mode-to-thing '((prog-mode . defun) (text-mode . sentence))
"An associated list between mode and thing (defined in "An associated list between mode and thing.
thingatpt), which determines the narrowness of the focused
section. A thing is defined in thingatpt.el; the thing determines the
narrowness of the focused section.
Note that the order of the list matters. The first mode that the Note that the order of the list matters. The first mode that the
current mode is derived from is used, so more modes that have current mode is derived from is used, so more modes that have
@ -59,12 +61,17 @@ Things that are defined include `symbol', `list', `sexp',
:type '(repeat symbol) :type '(repeat symbol)
:group 'focus) :group 'focus)
(defvar-local focus-pre-overlay nil (defvar focus-pre-overlay nil
"The overlay that dims the text prior to the current-point.") "The overlay that dims the text prior to the current-point.")
(defvar-local focus-post-overlay nil (defvar focus-post-overlay nil
"The overlay that dims the text past the current-point.") "The overlay that dims the text past the current-point.")
;; Use make-local-variable for backwards compatibility.
(dolist (var '(focus-pre-overlay
focus-post-overlay))
(make-local-variable var))
;; Changing major-mode should not affect Focus mode. ;; Changing major-mode should not affect Focus mode.
(dolist (var '(focus-pre-overlay (dolist (var '(focus-pre-overlay
focus-post-overlay focus-post-overlay
@ -72,23 +79,20 @@ Things that are defined include `symbol', `list', `sexp',
(put var 'permanent-local t)) (put var 'permanent-local t))
(defun focus-any (f lst) (defun focus-any (f lst)
"This function takes a function and a list, and returns the "Apply F to each element of LST and return first NON-NIL."
first NON-NIL value from applying F to an element in LST."
(when lst (when lst
(let ((v (funcall f (car lst)))) (let ((v (funcall f (car lst))))
(if v v (focus-any f (cdr lst)))))) (if v v (focus-any f (cdr lst))))))
(defun focus-bounds () (defun focus-bounds ()
"Returns the bounds of the first thing in `focus-mode-to-thing' that "Return the current bounds, based on `focus-mode-to-thing'."
is NON-NIL."
(let* ((modes (mapcar 'car focus-mode-to-thing)) (let* ((modes (mapcar 'car focus-mode-to-thing))
(mode (focus-any 'derived-mode-p modes)) (mode (focus-any 'derived-mode-p modes))
(thing (if mode (cdr (assoc mode focus-mode-to-thing)) 'sentence))) (thing (if mode (cdr (assoc mode focus-mode-to-thing)) 'sentence)))
(bounds-of-thing-at-point thing))) (bounds-of-thing-at-point thing)))
(defun focus-average-colors (color &rest colors) (defun focus-average-colors (color &rest colors)
"This function takes one or more colors and returns the average "Takes one or more COLORS and returns the average of the RGB-values."
of RGB values of the given colors."
(let* ((colors (cons color colors)) (let* ((colors (cons color colors))
(colors (mapcar 'color-name-to-rgb colors)) (colors (mapcar 'color-name-to-rgb colors))
(len (length colors)) (len (length colors))
@ -97,8 +101,7 @@ of RGB values of the given colors."
(apply 'color-rgb-to-hex avg))) (apply 'color-rgb-to-hex avg)))
(defun focus-make-dim-color () (defun focus-make-dim-color ()
"Uses `focus-dimness' to determine how dim a color that should "Return a dimmed color relative to the current theme."
be generated, and returns this color."
(let ((background (face-attribute 'default :background)) (let ((background (face-attribute 'default :background))
(foreground (face-attribute 'default :foreground)) (foreground (face-attribute 'default :foreground))
(backgrounds (if (> focus-dimness 0) focus-dimness 1)) (backgrounds (if (> focus-dimness 0) focus-dimness 1))
@ -108,18 +111,21 @@ be generated, and returns this color."
(make-list foregrounds foreground))))) (make-list foregrounds foreground)))))
(defun focus-move-focus () (defun focus-move-focus ()
"If `focus-mode' is enabled, this command fires after each "Move `focus-pre-overlay' and `focus-post-overlay'.
command, and moves the dimming overlays."
If function `focus-mode' is enabled, this command fires after
each command."
(let* ((bounds (focus-bounds))) (let* ((bounds (focus-bounds)))
(when bounds (when bounds
(move-overlay focus-pre-overlay (point-min) (car bounds)) (move-overlay focus-pre-overlay (point-min) (car bounds))
(move-overlay focus-post-overlay (cdr bounds) (point-max))))) (move-overlay focus-post-overlay (cdr bounds) (point-max)))))
(defun focus-init () (defun focus-init ()
"This function is run when focus-mode is enabled. It sets the "This function is run when command `focus-mode' is enabled.
`focus-pre-overlay' and `focus-post-overlay' to overlays; these
are invisible until `focus-move-focus' is run. It adds It sets the `focus-pre-overlay' and `focus-post-overlay' to
focus-move-focus to `post-command-hook'." overlays; these are invisible until `focus-move-focus' is run. It
adds `focus-move-focus' to `post-command-hook'."
(setq focus-pre-overlay (make-overlay (point-min) (point-min)) (setq focus-pre-overlay (make-overlay (point-min) (point-min))
focus-post-overlay (make-overlay (point-max) (point-max))) focus-post-overlay (make-overlay (point-max) (point-max)))
(let ((color (focus-make-dim-color))) (let ((color (focus-make-dim-color)))
@ -128,9 +134,10 @@ focus-move-focus to `post-command-hook'."
(add-hook 'post-command-hook 'focus-move-focus nil t)) (add-hook 'post-command-hook 'focus-move-focus nil t))
(defun focus-terminate () (defun focus-terminate ()
"When `focus-mode' is disabled the overlays pointed to by "This function is run when command `focus-mode' is disabled.
`focus-pre-overlay' and `focus-post-overlay' are deleted, and
`focus-move-focus' is removed from `post-command-hook'." The overlays pointed to by `focus-pre-overlay' and `focus-post-overlay' are
deleted, and `focus-move-focus' is removed from `post-command-hook'."
(progn (mapc 'delete-overlay (list focus-pre-overlay focus-post-overlay)) (progn (mapc 'delete-overlay (list focus-pre-overlay focus-post-overlay))
(remove-hook 'post-command-hook 'focus-move-focus t))) (remove-hook 'post-command-hook 'focus-move-focus t)))