如何使用cons单元格定义和以后删除与dolist的重叠 [英] How to use a cons cell to define and later remove overlays with `dolist`

查看:127
本文介绍了如何使用cons单元格定义和以后删除与dolist的重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一些指导,以减少执行自定义重叠删除功能所需的时间。由于多数变量都具有值,所以导致延迟高达 0.1 秒,但是并不是每个变量都必须使用。

I am looking for some guidance, please, to reduce the time needed to perform my custom overlay removal function. The delay of up to 0.1 seconds is caused because a plethora of variables all have values, however, not every variable is necessarily used.

我的目标是在使用第一个变量时附加一个可以设置为非 nil 的第二个变量,但我不确定如何设置以及如何将其纳入覆盖删除功能中。

My goal is to attach a second variable that can be set to non-nil whenever the first variable is used, but I am unsure how to set this up and how to incorporate that into the overlay removal function.

或许这样看起来会很有用:

Perhaps something that looks like this would be useful:


  • if (variable-one。t),然后(remove-overlays(point-min) )'显示字符)

  • if (variable-one . t), then (remove-overlays (point-min) (point-max) 'display character)

在以下示例中, Mx子字符-mode 会在光标访问任何这些字符时将字符1,2或3叠加:

In the following example, M-x sub-char-mode will place an overlay over the characters 1, 2 or 3 whenever the cursor is visiting any of those characters:


  • 1 将成为 | 1

2 将成为 | 2

3 将成为 | 3

(defvar variable-one (concat
  (propertize (char-to-string ?\u007C)
    'face 'font-lock-warning-face
    'cursor t)
  (propertize "1" 'face 'highlight 'cursor t) ))

(defvar variable-one-p (cons variable-one nil))

(defvar variable-two (concat
  (propertize (char-to-string ?\u007C)
    'face 'font-lock-warning-face
    'cursor t)
  (propertize "2" 'face 'highlight 'cursor t) ))

(defvar variable-two-p (cons variable-two nil))

(defvar variable-three (concat
  (propertize (char-to-string ?\u007C)
    'face 'font-lock-warning-face
    'cursor t)
  (propertize "3" 'face 'highlight 'cursor t) ))

(defvar variable-three-p (cons variable-three nil))

(defun substitute-character ()
  (cond
    ((eq (char-after (point)) 49)
      (setq variable-one-p (cons variable-one t))
      (overlay-put (make-overlay (point) (1+ (point))) 'display variable-one))
    ((eq (char-after (point)) 50)
      (setq variable-two-p (cons variable-two t))
      (overlay-put (make-overlay (point) (1+ (point))) 'display variable-two))
    ((eq (char-after (point)) 51)
      (setq variable-three-p (cons variable-three t))
      (overlay-put (make-overlay (point) (1+ (point))) 'display variable-three))))

(defun remove-sub-char ()
  (dolist (character `(
      ,variable-one
      ,variable-two
      ,variable-three))
    (remove-overlays (point-min) (point-max) 'display character))
 (dolist (my-variable `(
      ,variable-one-p
      ,variable-two-p
      ,variable-three-p))
    (setq my-variable nil)) )

(defun sub-char-post-command-hook ()
  (remove-sub-char)
  (substitute-character))

(define-minor-mode sub-char-mode
"A minor-mode for testing overlay-removal with cons cells."
  :init-value nil
  :lighter " OV-REMOVE"
  :keymap nil
  :global nil
  :group 'lawlist
  (cond
    (sub-char-mode
      (add-hook 'post-command-hook 'sub-char-post-command-hook nil t)
      (message "Turned ON `sub-char-mode`."))
    (t
      (remove-hook 'post-command-hook 'sub-char-post-command-hook t)
      (remove-sub-char)
      (message "Turned OFF `sub-char-mode`."))))






在此粘贴此图片感到遗憾 - 随时删除它。但是我无法将其粘贴到评论中,以回复您的评论来询问外观。这是 vline-style = compose col-highlight-vline-face-flag = nil

推荐答案

初稿>(2014年8月24日):第一个草案答案将变量定义为 cons 单元格 - car 一个预定的重叠字符串,而 cdr nil 。当光标访问字符 1 2 3 ,这些字符顶部放置一个重叠式广告,并且适用的 cons 单元格的 cdr 设置为 t 通过使用 setcdr 。覆盖删除功能包含变量名称列表和相应的 cons 单元格 - 当 cdr cons 单元格不是 nil (即 t ),覆盖层被删除,适用的 cons 单元格的 cdr 设置回 nil 使用 setcdr 。这种类型的设置的优点是,覆盖删除功能将快速查看,然后跳过 cons cell cdr nil - 从而在处理大量具有预定叠加字符串的变量时节省了大量的时间。

First Draft (August 24, 2014):  The first draft answer defines the variables as a cons cell -- the car is a predetermined overlay string, and the cdr is nil. When the cursor visits the characters 1, 2 or 3, an overlay is placed on top of those characters and the cdr of the applicable cons cell is set to t by using setcdr. The overlay removal function contains a list of variable names and the corresponding cons cells -- when the cdr of the cons cell is non-nil (i.e., t), the overlay is removed, and the cdr of the applicable cons cell is set back to nil using setcdr. The advantage of this type of setup is that the overlay removal function will quickly look at and then skip over variables whose cons cell cdr is nil -- thus saving a substantial amount of time when dealing with large quantities of variables with predetermined overlay strings.

编辑(2014年8月26日):修改后的代码,允许在不同缓冲区中使用相同的变量名称并设置缓冲区本地值。相关主题是:如何使用`setcdr`与缓冲区局部变量 合并变量名称变为`dolist`循环并更改其值

EDIT (August 26, 2014):  Modified code to permit using the same variable names in different buffers and set buffer-local values. Related threads are: How to use `setcdr` with buffer-local variables and Incorporate variable name into `dolist` cycle and change its value .

(defvar variable-one
  (cons
    (concat
      (propertize (char-to-string ?\u007C)
        'face 'font-lock-warning-face
        'cursor t)
      (propertize "1" 'face 'highlight 'cursor t) )
    nil))

(make-variable-buffer-local 'variable-one)

(defvar variable-two
  (cons
    (concat
     (propertize (char-to-string ?\u007C)
       'face 'font-lock-warning-face
       'cursor t)
     (propertize "2" 'face 'highlight 'cursor t) )
    nil))

(make-variable-buffer-local 'variable-two)

(defvar variable-three
  (cons
    (concat
     (propertize (char-to-string ?\u007C)
       'face 'font-lock-warning-face
       'cursor t)
     (propertize "3" 'face 'highlight 'cursor t) )
  nil))

(make-variable-buffer-local 'variable-three)

(defun sub-char ()
  (cond
    ((eq (char-after (point)) 49)
      (let ((newlist (copy-list variable-one)))
        (setcdr newlist t)
        (setq-local variable-one newlist)
        (overlay-put (make-overlay (point) (1+ (point))) 'display (car variable-one))))
    ((eq (char-after (point)) 50)
      (let ((newlist (copy-list variable-two)))
        (setcdr newlist t)
        (setq-local variable-two newlist)
        (overlay-put (make-overlay (point) (1+ (point))) 'display (car variable-two))))
    ((eq (char-after (point)) 51)
      (let ((newlist (copy-list variable-three)))
        (setcdr newlist t)
        (setq-local variable-three newlist)
        (overlay-put (make-overlay (point) (1+ (point))) 'display (car variable-three))))))

(defun remove-sub-char ()
  (dolist (character `(
      (variable-one ,variable-one)
      (variable-two ,variable-two)
      (variable-three ,variable-three)))
    (when (cdr (car (cdr character)))
      (let* (
          (var (car character))
          (newlist (copy-list (car (cdr character)))) )
        (remove-overlays (point-min) (point-max) 'display (car (car (cdr character))))
        (setcdr newlist nil)
        (set (car character) newlist)
        (message "var1: %s | var2: %s | var3: %s" variable-one variable-two variable-three) ))))

(defun sub-char-post-command-hook ()
  (remove-sub-char)
  (sub-char))

(define-minor-mode sub-char-mode
"A minor-mode for testing overlay-removal with cons cells."
  :init-value nil
  :lighter " OV-REMOVE"
  :keymap nil
  :global nil
  :group 'lawlist
  (cond
    (sub-char-mode
      (add-hook 'post-command-hook 'sub-char-post-command-hook nil t)
      (message "Turned ON `sub-char-mode`."))
    (t
      (remove-hook 'post-command-hook 'sub-char-post-command-hook t)
      (remove-sub-char)
      (message "Turned OFF `sub-char-mode`."))))

这篇关于如何使用cons单元格定义和以后删除与dolist的重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆