Emacs: xah-get-thing.el

By Xah Lee. Date: . Last updated: .

This is the home page of the package xah-get-thing.el.

The package gives you the following functions:

  1. xah-get-thing-at-cursor (was named unit-at-cursor)
  2. xah-get-thing-or-selection (was named get-selection-or-unit)

They are useful for writing commands that act on text selection if there's one, or a current {word, symbol, line, block, …} under cursor.

Download

github: https://github.com/xahlee/xah-get-thing-or-selection

On MELPA http://melpa.org/#/xah-get-thing 〔➤see Emacs: How to Install Packages Using ELPA, MELPA

xah-get-thing-or-selection

(xah-get-thing-or-selection 'word) returns a vector of in this form: [string boundary-begin-position boundary-end-position]

If the region is active, the string is the text of active region. Else, it is the current word, or symbol, or line, block, filepath, url, etc, depending on the argument.

;; example of using xah-get-thing-or-selection
(defun ff ()
  "print current word or text selection."
  (interactive)
  (let* ((bds (xah-get-thing-or-selection 'word))
         (text (elt bds 0))
         (begin (elt bds 1)) ; thing begin position
         (end (elt bds 2))   ; thing end position
         )
    (message "thing is: %s" text)))

Eval the above. 〔➤see How to Evaluate Emacs Lisp Code〕 Then, try to call the command when cursor is on a word. Then, try again when there's a text selection.

xah-get-thing-at-cursor

xah-get-thing-at-cursor is just like xah-get-thing-or-selection, except it doesn't care about region.

One thing useful is the 'block unit, which is defined as text surrounded by blank lines.

For detail, see the inline doc by calling describe-function on them.

How is this differ from emacs's builtin thing-at-point?

xah-get-thing-at-cursor or xah-get-thing-or-selection are not dependent of syntax table, so you get same thing regardless what's current major mode.

back to Xah Emacs Packages

Like it? Buy Xah Emacs Tutorial. Thanks.

or, buy something from Best Keyboard for Emacs