The thing I want to do is to highlight regions in a file covered by jest JavaScript tests. For testing I'm in an .emacs buffer, and I'm trying to highlight a single line in another buffer that I have opened in a different window.

I was looking at this question, which uses hlt-highlight-region from library Highlight, but I'm not able to make this work.

I have code like this that should change the single line's background to green:

  (let* ((dir "/home/kuba/projects/jcubic/terminal/repo")
         (buffer (get-file-buffer (concat dir "/js/jquery.terminal-src.js")))
         (start '((line . 3156) (column . 0)))
         (end '((line . 3156) (column . 18))))
    (with-current-buffer buffer
      (let* ((start-line-pos (line-beginning-position (cdr (assoc 'line start))))
             (start-pos (+ start-line-pos (cdr (assoc 'column start))))
             (end-line-pos (line-beginning-position (cdr (assoc 'line end))))
             (end-pos (+ end-line-pos (cdr (assoc 'column end)))))
        (hlt-highlight-region start-pos end-pos '((t (:background "green")))))))

But the code is returning nil and nothing happens. What's wrong with this code?

  • Your problem is not (hlt-highlight-region N1 N2 '((t (:background "green"))), at least. If you just do that, with two buffer positions for N1 and N2 then you'll see that it works. Try inserting (message "START: %S, END: %S" start-pos end-pos) just before the call to hlt-highlight-region, to see what values those vars have. Or use M-x debug-on-entry followed by the name of your function, and walk through the debugger with d or c to see what happens.
    – Drew
    Jun 17, 2018 at 0:34
  • @Drew I've investigated and it seems that line-begninning-position is not accepting line number as argument maybe it's offset or something.
    – jcubic
    Jun 17, 2018 at 7:00
  • line-beginning-position accepts a number of lines to move. It returns the buffer position of the beginning of the line that it arrives at after that move. (The move is temporary/simulated - point is left where it was initially.) If you want the position of the beginning of the current line then don't pass an argument.
    – Drew
    Jun 17, 2018 at 14:42


Your Answer

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

Browse other questions tagged or ask your own question.