remove-text-properties似乎不影响`display`文本属性

时间:2018-09-30 05:15:38

标签: emacs font-lock

我很困惑为什么使用remove-text-properties删除display文本属性不会更改缓冲区中的显示。相反,似乎我必须使用set-text-propertiesnil完全删除所有文本属性。例如,为什么remove-text-properties在这里不能代替set-text-properties

(defvar my-regex "#\\([[:alnum:]]+\\) \\([0-9]+\\)")
(defvar-local my--fontified-p nil)

(defun my-remove-display ()
  "Remove the display, eg. '#blah<2020>' -> '#blah 2020."
  (save-excursion
    (goto-char (point-min))
    (while (re-search-forward my-regex nil 'move)
      ;; why can't I use remove-text-properties here to get rid of 'display?
      (set-text-properties (match-beginning 0) (match-end 0) nil))))

(defun my-toggle-display ()
  "Toggle font-locking and display of '#blah 2020'."
  (interactive)
  (if (setq my--fontified-p (not my--fontified-p))
      (progn
        (font-lock-add-keywords
         nil
         `((,my-regex
            (0 (prog1 nil
                 (put-text-property
                  (1+ (match-beginning 0)) (match-end 0)
                  'display
                  (format "%s<%s>"
                          (match-string-no-properties 1)
                          (match-string-no-properties 2)))))
            (0 'font-lock-constant-face t))))
        (font-lock-flush)
        (font-lock-ensure))
    (my-remove-display)
    (font-lock-refresh-defaults)))

;;; Example that gets fontified
;; #blah 2020

1 个答案:

答案 0 :(得分:3)

它对我有用:

(defun my-remove-display ()
  "Remove the display, eg. '#blah<2020>' -> '#blah 2020."
  (save-excursion
    (goto-char (point-min))
    (while (re-search-forward my-regex nil 'move)
      (remove-text-properties (match-beginning 0) (match-end 0) '(display)))))

您没有显示您尝试过的remove-text-properties代码。这是您尝试过的吗?您是否通过了'display而不是'(display)