Elisp: Get Text at Cursor (thing-at-point)
thing-at-point
thing-at-point-
(thing-at-point THING &optional NO-PROPERTIES)Return a string that's the current THING under cursor. THING should be any of
'filename'url'word'symbol'number'whitespace'line'sentence'page'list'sexp'defun'email'uuid
(defun my-thing () "print current word." (interactive) (message "%s" (thing-at-point 'word)))
Get Boundary Positions of a Thing
Sometimes you also need to know a thing's boundary (the begin and end positions) , because you may need to delete it.
bounds-of-thing-at-point-
Return a Cons Pair that's the boundary positions of the text unit under cursor.
(defun my-get-boundary-and-thing () "example of using `bounds-of-thing-at-point'" (interactive) (let (xbounds xpos1 xpos2 xthing) (setq xbounds (bounds-of-thing-at-point 'symbol) xpos1 (car xbounds) xpos2 (cdr xbounds) xthing (buffer-substring-no-properties xpos1 xpos2)) (message "thing begin at [%s], end at [%s], thing is [%s]" xpos1 xpos2 xthing)))
thing-at-point and Syntax Table
The exact meaning of βthingβ, depends on current buffer's Syntax Table.