Emacs - 调试一个字符太宽的叠加层

时间:2014-05-01 20:10:53

标签: emacs elisp

我正在寻找一些帮助,请调试第二个例子 - 它应该只有一(1)个字符宽,但最终只有两(2)个字符宽。


工作示例:以下代码创建了一个只有一(1)个字符宽的叠加层,即正确

  
(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 示例:以下代码创建了一个宽度为两(2)个字符的叠加层,不正确

  
(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"))) ))

编辑 - 已修复示例:根据@Lindydancer在下面的答案中提供的有用指导,以下是现在有效的修订示例 - 也许可能是简化,但至少我现在理解这个概念:

  
(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"))) )))

1 个答案:

答案 0 :(得分:1)

我认为问题在于您将叠加层添加到所有线条。如果该行短于my-column,则叠加层会溢出下面的行,给人的印象是某些叠加层是两个字符宽,而实际上它是两个单字符叠加层,彼此相邻。< / p>

尝试限制代码,将叠加层仅放在长度至少为my-overlay的行上。