The answer to “I want to detect (…) event” is usually to use the appropriate hook. The manual has a list of standard hooks. There's no hook that triggers on a line change, so the next thing is to look for a hook that triggers on any motion. There's no hook specifically for that either, to you're down to a hook that runs every command.
Keep track of the current line number in a variable, and run the desired code whenever that value changes. Untested proof-of-concept:
(defvar showme-last-position nil)
(make-variable-buffer-local 'showme-last-line)
(defun showme-is-on-different-line (marker)
"Returns true if MARKER and point are on different lines."
(save-excursion
(if (< showme-last-position (point))
(progn
(beginning-of-line)
(< showme-last-position (point)))
(end-of-line)
(> showme-last-position (point)))))
(defun showme-if-needed ()
;; Create marker if needed
(if (null showme-last-position)
(setq showme-last-position (make-marker)))
;; Is marker on a different line from point, or uninitialized?
(when (or (null (marker-position showme-last-position))
(showme-is-on-different-line showme-last-position))
;; Update marker and run showme
(set-marker-position showme-last-position (point))
(showme)))
(Note that my way of detecting a line change is faster than recomputing the line number — line-number-at-pos
can be slow in a large file.)
I haven't reviewed your code, there may be a better trigger than a change in the line number. You may want to preprocess the buffer and add overlays. Font Lock mode has facilities for that, and can do the job lazily based on what parts of the buffer get shown so that opening a large file doesn't take forever.
One thing I do notice in your code is that the way you return to the original buffer and window is unreliable: other-window
might not do the right thing depending on the window configuration. Whenever you want to move and then return to the original place, use save-excursion
.