开发者

Recenter a named buffer that is not necessarily the current buffer in Emacs Lisp

I'd like to recenter a buffer,开发者_运维问答 called *Lense*, where I've inserted some text. I wished to make it the current buffer by (set-buffer "*Lense*"), then (recenter 0)). By the following code segments:

(save-excursion (set-buffer "*Lense*")
                (recenter 0))

However, it seems that the above code would only recenter the buffer which is the current buffer, and (set-buffer "*Lense*") has no effect to make the current buffer to be *Lense*.

Please help me to figure out the right way to recenter the named buffer *Lense*.


If the buffer you want to recenter is visible, then you want to recenter its window.

(with-selected-window (get-buffer-window "*Lense*")
  (recenter 0))

This will blow up if the buffer is not being displayed, so you may want to condition-case or unwind-protect.

If you want to handle the case where the buffer is not visible, you need to move the point. The window that will eventually display the buffer will center near the point, so you will have to move the point somewhere that will make that happen. Seems like your insert operation will DTRT, so you don't need to worry in that case.


I have not found (recenter) function, but there is (recenter-top-bottom). You can try using it instead of (recenter). Standard C-l hotkey calls exactly this function (recenter-top-bottom), also it has optional argument, line of window, which can be passed by C-u arg C-l.

PS: I can be wrong because I'm not familiar with emacs-lisp, I'm just using emacs with minimal customizations.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜