2015-05-12 20:42:01 +00:00
|
|
|
;;; focus.el --- Dim the font color of text in surrounding paragraphs -*- lexical-binding: t; -*-
|
|
|
|
|
|
|
|
;; Copyright (C) 2015 Lars Tveito
|
|
|
|
|
|
|
|
;; Author: Lars Tveito <larstvei@ifi.uio.no>
|
|
|
|
;; URL: http://github.com/larstvei/Focus
|
|
|
|
;; Created: 11th May 2015
|
|
|
|
;; Version: 0.0.1
|
|
|
|
;; Package-Requires: ((emacs "24") (cl-lib "1.0"))
|
|
|
|
|
|
|
|
;; 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
|
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
|
|
|
|
|
|
|
;; This program is distributed in the hope that it will be useful,
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
2015-05-12 21:21:33 +00:00
|
|
|
;; Focus provides `focus-mode` that dims the text of surrounding paragraphs,
|
|
|
|
;; similar to [iA Writer's](https://ia.net/writer) Focus Mode.
|
2015-05-12 20:42:01 +00:00
|
|
|
;;
|
2015-05-12 21:21:33 +00:00
|
|
|
;; Enable the mode with `M-x focus-mode'.
|
2015-05-12 20:42:01 +00:00
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
2015-05-11 23:54:10 +00:00
|
|
|
(require 'cl-lib)
|
|
|
|
|
2015-05-12 20:23:36 +00:00
|
|
|
(defvar-local focus-pre-overlay nil
|
|
|
|
"The overlay that dims the text prior to the current-point.")
|
|
|
|
|
|
|
|
(defvar-local focus-post-overlay nil
|
|
|
|
"The overlay that dims the text past the current-point.")
|
2015-05-11 22:48:09 +00:00
|
|
|
|
2015-05-11 22:57:31 +00:00
|
|
|
(defun focus-search-backward (regex)
|
2015-05-12 20:23:36 +00:00
|
|
|
"A wrapper for re-search-backward, where the point does not move,
|
|
|
|
and if the search fails, it returns NIL."
|
2015-05-11 22:57:31 +00:00
|
|
|
(save-excursion (re-search-backward regex nil t)))
|
|
|
|
|
|
|
|
(defun focus-search-forward (regex)
|
2015-05-12 20:23:36 +00:00
|
|
|
"A wrapper for re-search-backward, where the point does not move,
|
|
|
|
and if the search fails, it returns NIL."
|
2015-05-11 22:57:31 +00:00
|
|
|
(save-excursion (re-search-forward regex nil t)))
|
|
|
|
|
2015-05-11 23:54:10 +00:00
|
|
|
(defun focus-average-colors (color &rest colors)
|
2015-05-12 20:23:36 +00:00
|
|
|
"This function takes one or more colors and returns the average
|
|
|
|
of RGB values of the given colors."
|
2015-05-11 23:54:10 +00:00
|
|
|
(let* ((colors (cons color colors))
|
|
|
|
(colors (mapcar 'color-name-to-rgb colors))
|
|
|
|
(len (length colors))
|
|
|
|
(sums (apply 'cl-mapcar '+ colors))
|
|
|
|
(avg (mapcar (lambda (v) (/ v len)) sums)))
|
|
|
|
(apply 'color-rgb-to-hex avg)))
|
2015-05-11 22:48:09 +00:00
|
|
|
|
|
|
|
(defun focus-move-focus ()
|
2015-05-12 20:23:36 +00:00
|
|
|
"If `focus-mode' is enabled, this command fires after each
|
|
|
|
command, and moves the dimming overlays."
|
2015-05-11 22:57:31 +00:00
|
|
|
(let* ((pre (or (focus-search-backward "^\n") (point-min)))
|
|
|
|
(post (or (focus-search-forward "^\n") (point-max))))
|
|
|
|
(move-overlay focus-pre-overlay (point-min) pre)
|
|
|
|
(move-overlay focus-post-overlay post (point-max))))
|
2015-05-11 22:48:09 +00:00
|
|
|
|
2015-05-11 23:12:32 +00:00
|
|
|
(defun focus-init ()
|
2015-05-12 20:23:36 +00:00
|
|
|
"This function is run when focus-mode is enabled. It sets the
|
|
|
|
`focus-pre-overlay' and `focus-post-overlay' to overlays; these
|
|
|
|
are invisible until `focus-move-focus' is run. It adds
|
|
|
|
focus-move-focus to `post-command-hook'."
|
2015-05-11 23:12:32 +00:00
|
|
|
(setq focus-pre-overlay (make-overlay (point-min) (point-min))
|
|
|
|
focus-post-overlay (make-overlay (point-max) (point-max)))
|
|
|
|
(let ((color (focus-average-colors
|
|
|
|
(face-attribute 'default :foreground)
|
|
|
|
(face-attribute 'default :background))))
|
|
|
|
(mapc (lambda (o) (overlay-put o 'face (cons 'foreground-color color)))
|
|
|
|
(list focus-pre-overlay focus-post-overlay)))
|
|
|
|
(add-hook 'post-command-hook 'focus-move-focus nil t))
|
|
|
|
|
|
|
|
(defun focus-terminate ()
|
2015-05-12 20:23:36 +00:00
|
|
|
"When `focus-mode' is disabled the overlays pointed to by
|
|
|
|
`focus-pre-overlay' and `focus-post-overlay' are deleted, and
|
|
|
|
`focus-move-focus' is removed from `post-command-hook'."
|
2015-05-11 23:12:32 +00:00
|
|
|
(progn (mapc 'delete-overlay (list focus-pre-overlay focus-post-overlay))
|
|
|
|
(remove-hook 'post-command-hook 'focus-move-focus t)))
|
|
|
|
|
2015-05-11 22:48:09 +00:00
|
|
|
;;;###autoload
|
|
|
|
(define-minor-mode focus-mode
|
2015-05-12 20:43:26 +00:00
|
|
|
"Dim the font color of text in surrounding paragraphs."
|
2015-05-11 22:48:09 +00:00
|
|
|
:init-value nil
|
2015-05-11 23:12:32 +00:00
|
|
|
(if focus-mode (focus-init) (focus-terminate)))
|
2015-05-12 20:42:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
(provide 'focus)
|
|
|
|
;;; focus.el ends here
|