[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] master b60416c 01/34: First commit
From: |
Artur Malabarba |
Subject: |
[elpa] master b60416c 01/34: First commit |
Date: |
Fri, 16 Oct 2015 09:56:48 +0000 |
branch: master
commit b60416c0ffb6bbf32f90a264ffe37620e6329d1c
Author: Artur Malabarba <address@hidden>
Commit: Artur Malabarba <address@hidden>
First commit
---
Readme.org | 20 ++++++
spotlight.el | 201 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 221 insertions(+), 0 deletions(-)
diff --git a/Readme.org b/Readme.org
new file mode 100644
index 0000000..6587e12
--- /dev/null
+++ b/Readme.org
@@ -0,0 +1,20 @@
+#+TITLE: Spotlight --- Never lose your cursor again
+
+This is a global minor-mode. Turn it on everywhere with:
+#+BEGIN_SRC emacs-lisp
+(spotlight-mode 1)
+#+END_SRC
+
+Whenever point moves a long distance (as defined by
+~spotlight-minimum-distance~) a light will shine on top of your cursor
+to make sure you see where it is.
+
+- To customize the appearance of the spotlight, configure
+ ~spotlight-size~ and ~spotlight-brightness~.
+
+- To customize how long it lasts, configure ~spotlight-blink-duration~
+ and ~spotlight-blink-delay~.
+
+- To customize when it is used at all, configure
+ ~spotlight-minimum-distance~ and
+ ~spotlight-blink-when-buffer-changes~.
diff --git a/spotlight.el b/spotlight.el
new file mode 100644
index 0000000..5bc13a5
--- /dev/null
+++ b/spotlight.el
@@ -0,0 +1,201 @@
+;;; spotlight.el --- Highlight the cursor whenever it moves long distances
-*- lexical-binding: t; -*-
+
+;; Copyright (C) 2015 Artur Malabarba
+
+;; Author: Artur Malabarba <address@hidden>
+;; Keywords: convenience
+;; Version: 0.1
+;; Package-Requires: ((cl-lib "0.5"))
+
+;; 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:
+
+;; This is a global minor-mode. Turn it on everywhere with
+;; (spotlight-mode 1)
+
+;;; Code:
+
+(require 'cl-lib)
+
+(defgroup spotlight nil
+ "Customization group for spotlight."
+ :group 'emacs
+ :prefix "spotlight-")
+
+(defvar spotlight--timer nil)
+
+(defcustom spotlight-minimum-distance 15
+ "Minimum movement distance in lines to blink the spotlight."
+ :type 'integer)
+
+(defcustom spotlight-push-mark nil
+ "Should the mark be pushed before long movements?"
+ :type 'boolean)
+
+(defcustom spotlight-blink-when-buffer-changes t
+ "Should the spotlight blink when changing buffer?"
+ :type 'boolean)
+
+(defcustom spotlight-blink-duration 0.3
+ "Time, in seconds, that the blink should last."
+ :type 'number)
+
+(defcustom spotlight-blink-delay 0.3
+ "Time, in seconds, before starting to fade the spotlight."
+ :type 'number)
+
+(defcustom spotlight-size 15
+ "Size of the spotlight in characters."
+ :type 'number)
+
+(defcustom spotlight-brightness 0.5
+ "Brightness as a float between 0 and 1."
+ :type 'number)
+
+
+;;; Overlays
+(defvar spotlight--ovs nil)
+
+(defun spotlight--colored-overlay (color)
+ "Put an overlay at point with background COLOR."
+ (let ((ov (make-overlay (point) (1+ (point)))))
+ (overlay-put ov 'face (list :background color))
+ (overlay-put ov 'spotlight t)
+ (push ov spotlight--ovs)))
+
+(defun spotlight--ov-at-point ()
+ (car (cl-member-if (lambda (o) (overlay-get o 'spotlight))
+ (overlays-at (point)))))
+
+(defun spotlight--vanish ()
+ "Turn off the spotlight."
+ (when (timerp spotlight--timer)
+ (cancel-timer spotlight--timer))
+ (mapc #'delete-overlay spotlight--ovs)
+ (setq spotlight--ovs nil))
+
+
+;;; Colors
+(defun spotlight--int-range (a b)
+ "Return a list of integers between A inclusive and B exclusive.
+Only returns `spotlight-size' elements."
+ (let ((d (/ (- b a) spotlight-size))
+ (out (list a)))
+ (dotimes (_ (1- spotlight-size))
+ (push (+ (car out) d) out))
+ (nreverse out)))
+
+(defun spotlight--color-range ()
+ "Return a list of background colors for the spotlight."
+ (let ((bg (color-values (face-attribute 'default :background))))
+ (apply #'cl-mapcar (lambda (r g b) (format "#%04x%04x%04x" r g b))
+ (if (< (color-distance "black" bg)
+ (color-distance "white" bg))
+ (mapcar (lambda (n) (butlast (spotlight--int-range (*
spotlight-brightness 65535) n))) bg)
+ (mapcar (lambda (n) (cdr (spotlight--int-range (* (- 1
spotlight-brightness) 65535) n))) bg)))))
+
+
+;;; Blinking
+(defun spotlight--shine ()
+ "Shine a spotlight at point."
+ (let ((colors (spotlight--color-range)))
+ (save-excursion
+ (while colors
+ (if (looking-at "$")
+ (progn
+ ;; (spotlight--after-string)
+ (setq colors nil))
+ (spotlight--colored-overlay (pop colors))
+ (forward-char 1))))))
+
+(defun spotlight--dec ()
+ "Decrease the spotlight brightness by one."
+ (let ((o (spotlight--ov-at-point)))
+ (if (not o)
+ (spotlight--vanish)
+ (delete-overlay o)
+ (save-excursion
+ (while (progn (forward-char 1)
+ (setq o (spotlight--ov-at-point)))
+ (move-overlay o (1- (point)) (point)))))))
+
+(defun spotlight-blink ()
+ "Blink the spotlight at the position of the cursor."
+ (interactive)
+ (spotlight--vanish)
+ (spotlight--shine)
+ (setq spotlight--timer
+ (run-at-time spotlight-blink-delay
+ (/ spotlight-blink-duration 1.0 (length
spotlight--colors))
+ #'spotlight--dec)))
+
+
+;;; Movement detection
+(defvar spotlight--previous-place nil)
+(defvar spotlight--previous-mark-head nil)
+
+(defun spotlight--maybe-push-mark ()
+ "Push mark if it seems to be safe."
+ (when (and spotlight-push-mark
+ (not mark-active))
+ (let ((head (car mark-ring)))
+ (when (and (eq spotlight--previous-mark-head head)
+ (not (equal head spotlight--previous-place)))
+ (push-mark spotlight--previous-place)))))
+
+(defun spotlight--post-command ()
+ "Blink if point moved very far."
+ (cond
+ ((not (markerp spotlight--previous-place))
+ (spotlight--vanish))
+ ;; Blink because we changed buffer.
+ ((not (equal (marker-buffer spotlight--previous-place)
+ (current-buffer)))
+ (when spotlight-blink-when-buffer-changes
+ (unless (window-minibuffer-p)
+ (spotlight-blink))))
+ ;; Blink for distance movement
+ ((and (> (abs (- (point) spotlight--previous-place))
+ spotlight-minimum-distance)
+ (> (count-screen-lines (min (point) spotlight--previous-place)
+ (max (point) spotlight--previous-place))
+ spotlight-minimum-distance))
+ (spotlight--maybe-push-mark)
+ (spotlight-blink))
+ ;; Even if we don't blink, vanish any previous spotlight.
+ (t (spotlight--vanish)))
+ (unless (window-minibuffer-p)
+ (setq spotlight--previous-mark-head (car mark-ring))
+ (setq spotlight--previous-place (point-marker))))
+
+
+;;; Minor-mode
+(defcustom spotlight-lighter (cond
+ ((char-displayable-p ?💡) "💡")
+ ((char-displayable-p ?Λ) "Λ")
+ (t "*"))
+ "Lighter string used on the mode-line."
+ :type 'string)
+
+;;;###autoload
+(define-minor-mode spotlight-mode
+ nil nil spotlight-lighter nil
+ :global t
+ (if spotlight-mode
+ (add-hook 'post-command-hook #'spotlight--post-command)
+ (remove-hook 'post-command-hook #'spotlight--post-command)))
+
+(provide 'spotlight)
+;;; spotlight.el ends here
- [elpa] master 36be965 09/34: Make beacon color customizable, (continued)
- [elpa] master 36be965 09/34: Make beacon color customizable, Artur Malabarba, 2015/10/16
- [elpa] master 43cbfd4 15/34: Merge pull request #3 from tsdh/master, Artur Malabarba, 2015/10/16
- [elpa] master 8abbedc 05/34: Default to blinking on window scroll instead of point movement, Artur Malabarba, 2015/10/16
- [elpa] master fbcf9fc 16/34: Drop cl-lib dep, Artur Malabarba, 2015/10/16
- [elpa] master 19096a6 10/34: Initial (failed) impl for beacon at end of line, Artur Malabarba, 2015/10/16
- [elpa] master e533e04 11/34: Fix an error, Artur Malabarba, 2015/10/16
- [elpa] master 03d12b4 03/34: Rename file, Artur Malabarba, 2015/10/16
- [elpa] master 8941740 07/34: Fix docs, Artur Malabarba, 2015/10/16
- [elpa] master c82095a 06/34: Add url, Artur Malabarba, 2015/10/16
- [elpa] master 9152a09 08/34: Separate mark-pushing from blinking, Artur Malabarba, 2015/10/16
- [elpa] master b60416c 01/34: First commit,
Artur Malabarba <=
- [elpa] master ca3ec24 14/34: Fix temporary line breaks due to too long after-strings, Artur Malabarba, 2015/10/16
- [elpa] master c507480 13/34: Fix after-string overlays, Artur Malabarba, 2015/10/16
- [elpa] master 02112fa 12/34: Fix lighter, Artur Malabarba, 2015/10/16
- [elpa] master f1bb231 20/34: Fix scrolling outside command loop, Artur Malabarba, 2015/10/16
- [elpa] master 0b7041e 22/34: More documentation, Artur Malabarba, 2015/10/16
- [elpa] master 5622af4 25/34: [#2] Add more options for preventing a blink, Artur Malabarba, 2015/10/16
- [elpa] master 6f647f6 21/34: Only blink selected window, Artur Malabarba, 2015/10/16
- [elpa] master 0036355 23/34: Copyright, Artur Malabarba, 2015/10/16
- [elpa] master bccd3de 33/34: Document contributors, Artur Malabarba, 2015/10/16
- [elpa] master d1f0728 26/34: Extend doc, Artur Malabarba, 2015/10/16