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

如何检查Emacs中是否存在当前缓冲区?

  •  35
  • oneself  · 技术社区  · 16 年前

    (if (buffer-exists "my-buffer-name")
        ; do something
     )
    

    谢谢

    4 回复  |  直到 13 年前
        1
  •  66
  •   Mirzhan Irkegulov    9 年前

    documentation :

    (get-buffer name)
    
    Return the buffer named name (a string).
    If there is no live buffer named name, return nil.
    name may also be a buffer; if so, the value is that buffer.
    
    (get-buffer-create name)
    
    Return the buffer named name, or create such a buffer and return it.
    A new buffer is created if there is no live buffer named name.
    If name starts with a space, the new buffer does not keep undo information.
    If name is a buffer instead of a string, then it is the value returned.
    The value is never nil.
    
        2
  •  8
  •   Ken Wayne VanderLinde    8 年前

    这就是我所做的:

    (when (get-buffer "*scratch*")
      (kill-buffer "*scratch*"))
    

    这将检查缓冲区是否有划痕。如果有这样的事,杀了它。 如果没有,什么也不做。

        3
  •  6
  •   zarkone    9 年前

    不确定这个谓词出现的版本,但现在Emacs已经出现了 buffer-live-p :

    buffer-live-p is a built-in function in `buffer.c'.
    
    (buffer-live-p OBJECT)
    
    Return non-nil if OBJECT is a buffer which has not been killed.
    Value is nil if OBJECT is not a buffer or if it has been killed.
    
        4
  •  5
  •   Ken Wayne VanderLinde    8 年前

    如果您想如上所述定义您的假设函数,可以这样做:

    (defun buffer-exists (bufname)   (not (eq nil (get-buffer bufname))))
    

    *scratch*

    (defun buffer-exists (bufname) (not (eq nil (get-buffer bufname))))
    (if (buffer-exists "*scratch*")  (kill-buffer "*scratch*"))