I have a list that looks like this:

  (start (assoc) (line . 430) (col . 28))
  (end (assoc) (line . 430) (col . 37)))
  (start (assoc) (line . 433) (col . 14))
  (end (assoc) (line . 433) (col . 23))))

I'd like to highlight all the regions specified by the start/end pairs. I should be able to do this if I had a function that highlighted a region in an emacs buffer, but I see no such function. I checked the hl libray but all the commands seemed to design for interactive use. Is there anything that can help me?

1 Answer 1


See library Highlight (highlight.el).

You can highlight the text you want in various ways. One way is to use a regexp, with function (command) hlt-highlight-regexp-region. Or if you locate the region to highlight in another way you can use hlt-highlight-region.

Or you can use function hlt-highlight-regions, which highlights a set of buffer zones, defined by their limit positions. For example, this highlights buffer positions 1 to 3, 6 to 8, and 12 to 16 using face highlight:

(hlt-highlight-regions '((1 3) (6 8) (12 16)) 'highlight)

(If you also use library Zones (zones.el) then you can use hlt-highlight-regions interactively, as well as make use of other buffer-zone features.)

In your case, you would apparently just need to convert your information about line and column numbers to buffer positions, then pass the pairs of buffer positions to hlt-highlight-regions.

It's a bit unfortunate that your input data is in the form of line and column numbers instead of buffer positions or x-y pixel coordinates. Here is code for how to convert line+column data to buffer positions.

  • How would I remove the regions created using your function? I know there's a similar hlt-unhighlight-regions command, but if the buffer was modified and the points are no longer the same?
    – rgrinberg
    Commented Jul 2, 2017 at 18:14
  • 1. If you want to unhighlight text between two buffer limits then you need to provide those limits to the unhighlighting function. If you want to use the same buffer limits that you used previously to highlight, save those limits and reuse them. 2. Unhighlighting commands, like highlighting commands, take a FACE argument. So you can unhighlight only some particular face across some extent (e.g., the whole buffer), without unhighlighting other faces. 3. This is not the place to document the library - please read the doc.
    – Drew
    Commented Jul 2, 2017 at 22:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.