Emacs — remove an overlay `after-string` with a variable value

梦想的初衷 提交于 2019-12-11 10:35:36

问题


What is the proper way, please, to remove after-string overlays with variable values?

When using C-u C-x =, it only shows up as after-string without stating what the value is.

For example, once I lay an overlay using (overlay-put (make-overlay (point) (point)) 'after-string my-concatenated-string), I would like to be able to delete it without programming Emacs to remember every single my-concatenated-string that was previously used in the buffer -- there might be a few different ones on every line?

Is it sufficient to use?: (remove-overlays (window-start) (window-end)) 'after-string)

Or, is it better to use?: (remove-overlays (window-start) (window-end)) 'after-string t)

Or, is there another method to get them all?


EDIT (March 17, 2014):  My confusion is apparently coming from a misunderstanding between an object and a property.

In general, an overlay property is created as follows:

(overlay-put (make-overlay (point) (point)) 'my-property 'property-number-one )

In general, an overlay object is created as follows:

(overlay-put (make-overlay (point) (+ (point) 1))
  'face '(:background "gray50" :foreground "black"))

Here is a unique situation where an 'after-string smells-like an object. My assumption is: if it smells-like an object, then perhaps a value needs to be included when attempting to remove it so that I'm not left with a disconnected 'after-string:

(save-excursion
  (end-of-line)
  (let ((eol-floating-column (+ (current-column) 10)))
    (overlay-put (make-overlay (point) (point))
                 'after-string
                 (concat
                  (propertize (char-to-string ?\uE001)
                              'display
                              `((space :align-to ,eol-floating-column)
                                (space :width 0)))
                  (propertize (char-to-string ?\u00B6)
                              'face '(:background "gray50" :foreground "black")
                              'cursor t) ))))

回答1:


The way the code is written, if you omit the last parameter, it only removes an overlay if the value is `nil' (which it doesn't appear to be in your case).

As you don't know the value of the property, I don't think you can use the function. However, you can simply write something like (assuming the value of the after-string is never nil):

    (dolist (o (overlays-in (window-start) (window-end)))
      (when (overlay-get o 'after-string)
        (delete-overlay o))

Also note that if you do this from a post-command hook, window-end might not reflect the true value. To be safe you can do (window-end nil t), however this could be a bit slower.




回答2:


When you put the overlay, add another property (like (overlay-put ol 'lawlist t), for example), after which you can remove those overlays with (remove-overlays BEG END 'lawlist t).




回答3:


(defun lawlist-remove-overlays (beg end name val)
  "Remove the overlays."
  ;; DEBUGGING
  ;; (unless (and beg end name val)
  ;;   (message "ERROR -- beg:  %s | end:  %s | name:  %s | val:  %s" beg end name val))
  (let* (
      (point-max (point-max))
      (point-min (point-min))
      (narrowed-p (not (equal (- point-max point-min) (buffer-size))))
      (beg (if beg beg point-min))
      (end
        (cond
          ((and
              (not narrowed-p)
              end)
            end)
          ((and
              (not narrowed-p)
              (null end))
            point-max)
          ((and
              narrowed-p
              end
              (< end point-max))
            end)
          ((and
              narrowed-p
              end
              (= end point-max))
            (1+ end))
          ((and
              narrowed-p
              (null end))
            (1+ point-max)) )))
    (when (and beg end name val)
      (overlay-recenter end)
      (dolist (o (overlays-in beg end))
        (when (eq (overlay-get o name) val)
          (delete-overlay o))))))

  (dolist (description `(
    ,fci-pre-limit-string
    ,fci-pre-limit-active-region-string
    ,fci-at-limit-string
    ,fci-post-limit-string
    ,fci-wrapped-limit-string
    ,fci-cursor-at-eol-string
    ,fci-tab-text-left
    ,fci-tab-text-right
    ,fci-tab-sandwiched))
  (lawlist-remove-overlays nil nil 'after-string description))

See also this related thread which deals with targeting overlays with values containing text properties:

https://emacs.stackexchange.com/a/9847/2287



来源:https://stackoverflow.com/questions/23707953/emacs-remove-an-overlay-after-string-with-a-variable-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!