Use things as defined in thingatpt as bounds

One can customize what sort of bounds should be used for a mode, where
derived modes are used to generalize. By default text-mode and prog-mode
are defined.
This commit is contained in:
Lars Tveito 2015-05-17 00:58:20 +02:00
parent eeb12d8db8
commit 35f3cf5d52

View File

@ -31,6 +31,7 @@
;;; Code: ;;; Code:
(require 'cl-lib) (require 'cl-lib)
(require 'thingatpt)
(defcustom focus-dimness 0 (defcustom focus-dimness 0
"When `focus-mode' is enabled, the dimness of the sections that "When `focus-mode' is enabled, the dimness of the sections that
@ -43,21 +44,41 @@ and foreground color."
:type '(integer) :type '(integer)
:group 'focus) :group 'focus)
(defcustom focus-mode-to-thing '((prog-mode . defun) (text-mode . sentence))
"An associated list between mode and thing (defined in
thingatpt), which determines the narrowness of the focused
section.
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
many derivatives should be placed by the end of the list.
Things that are defined include `symbol', `list', `sexp',
`defun', `filename', `url', `email', `word', `sentence',
`whitespace', `line', and `page'."
:type '(repeat symbol)
:group 'focus)
(defvar-local focus-pre-overlay nil (defvar-local 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-local focus-post-overlay nil
"The overlay that dims the text past the current-point.") "The overlay that dims the text past the current-point.")
(defun focus-search-backward (regex) (defun focus-any (f lst)
"A wrapper for re-search-backward, where the point does not move, "This function takes a function and a list, and returns the
and if the search fails, it returns NIL." first NON-NIL value from applying F to an element in LST."
(save-excursion (re-search-backward regex nil t))) (when lst
(let ((v (funcall f (car lst))))
(if v v (focus-any f (cdr lst))))))
(defun focus-search-forward (regex) (defun focus-bounds ()
"A wrapper for re-search-backward, where the point does not move, "Returns the bounds of the first thing in `focus-things-order' that
and if the search fails, it returns NIL." is NON-NIL."
(save-excursion (re-search-forward regex nil t))) (let* ((modes (mapcar 'car focus-things-order))
(mode (focus-any 'derived-mode-p modes))
(thing (if mode (cdr (assoc mode focus-things-order)) 'sentence)))
(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 "This function takes one or more colors and returns the average
@ -83,10 +104,10 @@ be generated, and returns this color."
(defun focus-move-focus () (defun focus-move-focus ()
"If `focus-mode' is enabled, this command fires after each "If `focus-mode' is enabled, this command fires after each
command, and moves the dimming overlays." command, and moves the dimming overlays."
(let* ((pre (or (focus-search-backward "^\n") (point-min))) (let* ((bounds (focus-bounds)))
(post (or (focus-search-forward "^\n") (point-max)))) (when bounds
(move-overlay focus-pre-overlay (point-min) pre) (move-overlay focus-pre-overlay (point-min) (car bounds))
(move-overlay focus-post-overlay post (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 focus-mode is enabled. It sets the