Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.
How to determine if in the current buffer some text is selected?
You can use region-active-p.
Just to add something to the short answer, region-active-p won't
work in -batch, so you need to enable this (which is actually
default in plain Emacs):
Required, but never shown