如何在专用缓冲​​区中显示结果?

时间:2015-11-10 19:14:46

标签: elisp

如果我当前的缓冲区是foo,并且我运行C-x C-b(又名list-buffers),我的Emacs窗口会分成两部分,其中foo占据两个结果之一窗口和占用另一个的专用*Buffer List*缓冲区。在此操作结束时,foo仍然是活动缓冲区,并且point在执行C-x C-b之前保持不变。

我想编写一个交互式函数,以类似的方式显示多页输出:必要时拆分窗口,调出一个专用缓冲区,保留活动缓冲区,就像运行函数一样。

我设法做的最好的是具有以下结构的函数:

(defun my-func ()
  (interactive)
  (let ((this-buffer (current-buffer))
        (other-buffer (get-buffer-create "*my-func's Dedicated Buffer*")))
    (switch-to-buffer-other-window other-buffer)
    (erase-buffer)
    (insert (compute-pages-and-pages-of-stuff))
    (beginning-of-buffer)
    (switch-to-buffer-other-window this-buffer)))

这或多或少都有效,但我认为Emacs已经有内置语法来简化这些代码。

IOW,我认为有一个特殊的形式类似于下面代码段中的(虚构的)WITH-BUFFER-OTHER-WINDOW

(defun my-func ()
    (interactive)
    (let ((other-buffer (get-buffer-create "*my-func's Dedicated Buffer*")))
        (WITH-BUFFER-OTHER-WINDOW other-buffer
           (erase-buffer)
           (insert (compute-pages-and-pages-of-stuff))
           (beginning-of-buffer))))

或者甚至

(defun my-func ()
    (interactive)
    (let ((other-buffer (get-buffer-create "*my-func's Dedicated Buffer*")))
        (WITH-BUFFER-OTHER-WINDOW other-buffer
            (compute-pages-and-pages-of-stuff))))

我尝试了save-excursionsave-current-buffer这样的表单,但它们没有重现所需的行为。

是否有内置的方法来执行上述操作?

1 个答案:

答案 0 :(得分:1)

这是利用内置插件display-bufferwith-current-buffer获得的最短内容:

(defun my-func ()
  (interactive)
  (let ((buffer (get-buffer-create "*my-func's Dedicated Buffer*")))
    (with-current-buffer buffer
      (erase-buffer)
      (insert (compute-pages-and-pages-of-stuff))
      (goto-char (point-min)))
    (display-buffer buffer)))

如果您使用此模式以至于开始讨厌您,请随意定义辅助宏:

(defmacro my-setup-and-display-buffer (name &rest body)
  (declare (indent 1))
  (let ((buffer (make-symbol "buffer")))
    `(let ((,buffer (get-buffer-create ,name)))
       (with-current-buffer ,buffer
         ,@body)
       (display-buffer ,buffer))))

(my-setup-and-display-buffer "*my-func's Dedicated Buffer*"
  (erase-buffer)
  (insert (compute-pages-and-pages-of-stuff))
  (goto-char (point-min)))