Skip to content

Instantly share code, notes, and snippets.

@twlz0ne
Last active July 1, 2022 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twlz0ne/0b66fefd460236b7d51567e99028afff to your computer and use it in GitHub Desktop.
Save twlz0ne/0b66fefd460236b7d51567e99028afff to your computer and use it in GitHub Desktop.
Add string support for thing-at-point #Emacs #emacs-lisp
;;; string-at-point.el --- Add string support for ‘thing-at-point’ -*- lexical-binding: t; -*-
;; Copyright (C) 2020 Gong Qijian <gongqijian@gmail.com>
;; SPDX-License-Identifier: GPL-3.0-or-later
;;; Commentary:
;;; Code:
(require 'pcase)
(put 'string 'bounds-of-thing-at-point 'string-at-point-bounds-of-string-at-point)
(defun string-at-point-bounds-of-string-at-point ()
"Return the bounds of the string at point."
(or (pcase (syntax-ppss)
(`(,_ ,_ ,_ ,at-string-p ,_ ,_ ,_ ,_ ,string-start . ,_)
(when (and at-string-p string-start)
(cons string-start
(save-excursion
(goto-char string-start)
(forward-sexp)
(point))))))
;; Point at the beginning of string.
(if (eq (char-after (point)) ?\")
(cons (point)
(save-excursion
(forward-sexp 1)
(point))))))
(defun string-at-point ()
"Return the string at point, or nil if none is found."
(form-at-point 'string))
(provide 'string-at-point)
;;; string-at-point.el ends here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment