Question

I'm looking for some assistance, please, debugging the second example -- it should be only one (1) character wide, but ends up being two (2) characters wide.


WORKING EXAMPLE:  The following code creates an overlay of just one (1) character wide, which is correct:

(let ((my-column (current-column)))
  (overlay-put
    (make-overlay
      (save-excursion (beginning-of-line) (+ (point) my-column))
      (+ 1 (save-excursion (beginning-of-line) (+ (point) my-column)) ) )
    'face '(background-color . "pink")))

BROKEN EXAMPLE:  The following code creates an overlay that is two (2) characters wide, which is incorrect:

(let ((my-column (current-column)))
  (save-excursion
    (goto-char (point-min))
    (while (re-search-forward "\n" nil t)
      (overlay-put
        (make-overlay
          (save-excursion (beginning-of-line) (+ (point) my-column))
          (+ 1 (save-excursion (beginning-of-line) (+ (point) my-column)) ) )
        'face '(background-color . "pink"))) ))

EDIT -- FIXED EXAMPLE:  Based on the helpful guidance of @Lindydancer in the answer below, here is the revised example that now works -- perhaps it could be simplified, but at least I now understand the concept:

(let* (
    (my-column (current-column))
    my-line-beginning
    my-line-ending
    my-line-length)
  (save-excursion
    (goto-char (point-min))
    (while (re-search-forward "\n" nil t)
      (setq my-line-beginning (point))
      (end-of-line)
      (setq my-line-ending (point))
      (setq my-line-length (- my-line-ending my-line-beginning))
      (when (< my-column my-line-length)
        (overlay-put
          (make-overlay
            (save-excursion (beginning-of-line) (+ (point) my-column))
            (+ 1 (save-excursion (beginning-of-line) (+ (point) my-column)) ) )
          'face '(background-color . "pink"))) )))
Was it helpful?

Solution

I think the problem is that you add the overlay to all lines. If the line is shorter than my-column, the overlay will spill over lines below, giving the impression that some overlays are two character wide, when in reality it's two one-character overlays placed next to eachother.

Try to limit the code to put the overlay only on lines that are at least my-overlay long.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top