Generalize thing-at-point & region functions

This commit is contained in:
Henrik Lissner 2020-01-03 02:39:47 -05:00
parent d426349f21
commit 27bf3c85b5
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
7 changed files with 80 additions and 58 deletions

View file

@ -621,10 +621,7 @@ config blocks in your private config."
(defun doom--help-search-prompt (prompt)
(let ((query
(if (use-region-p)
(buffer-substring-no-properties (region-beginning) (region-end))
(or (thing-at-point 'symbol t) ""))))
(let ((query (doom-thing-at-point-or-region)))
(if (featurep 'counsel)
query
(read-string prompt query 'git-grep query))))

View file

@ -57,9 +57,63 @@ POS defaults to the current position."
;;;###autoload
(defun doom-point-in-string-or-comment-p (&optional pos)
"Return non-nil if POS is in a string or comment."
(declare (side-effect-free t))
(or (doom-point-in-string-p pos)
(doom-point-in-comment-p pos)))
;;;###autoload
(defun doom-region-active-p ()
"Return non-nil if selection is active.
Detects evil visual mode as well."
(declare (side-effect-free t))
(or (use-region-p)
(and (bound-and-true-p evil-local-mode)
(evil-visual-state-p))))
;;;###autoload
(defun doom-region-beginning ()
"Return beginning position of selection.
Uses `evil-visual-beginning' if available."
(declare (side-effect-free t))
(if (bound-and-true-p evil-local-mode)
evil-visual-beginning
(region-beginning)))
;;;###autoload
(defun doom-region-end ()
"Return end position of selection.
Uses `evil-visual-end' if available."
(declare (side-effect-free t))
(if (bound-and-true-p evil-local-mode)
evil-visual-end
(region-end)))
;;;###autoload
(defun doom-thing-at-point-or-region (&optional thing prompt)
"Grab the current selection, THING at point, or xref identifier at point.
Returns THING if it is a string. Otherwise, if nothing is found at point and
PROMPT is non-nil, prompt for a string (if PROMPT is a string it'll be used as
the prompting string). Returns nil if all else fails.
NOTE: Don't use THING for grabbing symbol-at-point. The xref fallback is smarter
in some cases."
(declare (side-effect-free t))
(cond ((stringp thing)
thing)
((doom-region-active-p)
(buffer-substring-no-properties
(doom-region-beginning)
(doom-region-end)))
(thing
(thing-at-point thing t))
((require 'xref nil t)
;; A little smarter than using `symbol-at-point', though in most cases,
;; xref ends up using `symbol-at-point' anyway.
(xref-backend-identifier-at-point (xref-find-backend)))
(prompt
(read-string (if (stringp prompt) prompt "")))))
;;
;;; Commands