代码之家  ›  专栏  ›  技术社区  ›  lawlist

如何使用cons单元格定义并稍后使用“dolist”删除覆盖`

  •  1
  • lawlist  · 技术社区  · 12 年前

    我正在寻找一些指导,请,以减少执行自定义覆盖删除功能所需的时间。延迟达 0.1 秒是因为过多的变量都有值,然而,并不是每个变量都必须使用。

    我的目标是附加一个可以设置为非- nil 无论何时使用第一个变量,但我不确定如何设置该变量,以及如何将其合并到覆盖移除函数中。

    也许这样的东西会有用:

    • 如果 (variable-one . t) 然后 (remove-overlays (point-min) (point-max) 'display character)

    在下面的示例中, M-x sub-char-mode 每当光标访问任何字符时,将在字符1、2或3上放置覆盖:

    • 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 = 无 :

    enter image description here

    2 回复  |  直到 12 年前
        1
  •  0
  •   Community Mohan Dere    9 年前

    初稿 (2014年8月24日):第一份答案草案将变量定义为 cons 单元格-- car 是预定的覆盖字符串,并且 cdr 是 nil 。当光标访问字符时 1 , 2 或 3 ,在这些字符和 cdr公司 适用的 欺骗 单元格设置为 t 通过使用 setcdr 。覆盖移除函数包含变量名列表和对应的 欺骗 单元格--当 cdr公司 的 欺骗 单元格为非- 无 (即。, 吨 ),覆盖将被删除 cdr公司 适用的 欺骗 单元格设置回 无 使用 设置cdr 。这种类型设置的优点是覆盖删除功能将快速查看并跳过 欺骗 单间牢房 cdr公司 是 无 --因此在处理具有预定覆盖字符串的大量变量时节省了大量时间。

    编辑 (2014年8月26日):修改代码,允许在不同的缓冲区中使用相同的变量名,并设置缓冲区本地值。相关线程包括: How to use `setcdr` with buffer-local variables 和 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`."))))
    
        2
  •  0
  •   Stefan    12 年前

    我建议你先把你的 variable-FOO-p vars:不仅名称错误(“-p”后缀用于 谓词 它们必然是函数,而不是变量),但它们是不需要的。而不是告诉 remove-overlays 删除具有特定 display 属性,只需添加自己的属性(例如。 (overlay-put <youroverlay> 'vline t) )这样你就可以做一个 (remove-overlays (point-min) (point-max) 'vline t) 当然,另一种至少同样有效的方法是保留一个缓冲区局部变量(更好的是,窗口局部变量),该变量保存当前放置的所有覆盖的列表。这样,您可以使用单个 (mapc #'delete-overlay vline--overlays) ,这更有效。可以通过以下方式提高效率 移动 / 再利用,再利用 而不是删除它们,然后创建新的覆盖。