微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

emacs - 滚动到缓冲区顶部?

如何解决emacs - 滚动到缓冲区顶部?

Emacs(和所有其他文本编辑器)认在缓冲区底行下方显示空白区域。我希望 emacs 也能够像这样在缓冲区中的顶行上方滚动/显示空白区域上方,以便可以在屏幕中央查看小文件的顶行。

解决方法

我已经开始了一个小的次要模式来完成这个。然而,随着第一个版本的发展,它可能有问题,不能处理所有边缘情况(例如缩小),而且效率不是特别高。

因此,我很乐意接受改进:如果您可以改进此代码或对其进行扩展,请随时直接编辑此答案。

(defvar vertical-center-num-buffers 0
  "The number of buffers in which `vertical-center-mode' is activated.")

(define-minor-mode vertical-center-mode
  "This minor mode displays the contents of a buffer vertically
centered with respect to the window height. This,of course,only
makes sense for buffers whose content is shorter than the window
height."
  nil
  " vc"
  nil

  ;; is the mode being turned on or off?
  (if vertical-center-mode
  ;; on
  (progn
    ;; keep track of the number of lines in the buffer
    (setq-local vertical-center-num-lines (count-lines (point-min) (point-max)))
    ;; use an overlay to display empty lines at the beginning of the buffer
    (setq-local vertical-center-overlay (make-overlay (point-min) (point-max)))
    ;; initial call to the function that centers the buffer contents
    (vertical-center--lines-changed 0)

    ;; react to changes to the buffer or the window
    (add-hook 'kill-buffer-hook 'vertical-center--kill-buffer)
    (add-hook 'window-size-change-functions 'vertical-center--window-size-changed)
    (when (= vertical-center-num-buffers 0)
      (add-hook 'before-change-functions 'vertical-center--before-change)
      (add-hook 'after-change-functions 'vertical-center--after-change))

    ;; this is just to play nice and remove the above hook
    ;; functions when they're no longer needed. Let's keep our
    ;; fingers crossed that we'll always stay in sync.
    (setq vertical-center-num-buffers (1+ vertical-center-num-buffers)))

;; off
;; delete/unset data structures when the mode is turned off
(delete-overlay vertical-center-overlay)
(makunbound 'vertical-center-num-lines)
(makunbound 'vertical-center-overlay)
(setq vertical-center-num-buffers (1- vertical-center-num-buffers))

;; remove hook functions when they're no longer needed
(when (= vertical-center-num-buffers 0)
  (remove-hook 'kill-buffer-hook 'vertical-center--kill-buffer)
  (remove-hook 'window-size-change-functions 'vertical-center--window-size-changed)
  (remove-hook 'before-change-functions 'vertical-center--before-change)
  (remove-hook 'after-change-functions 'vertical-center--after-change))))

;; handle killing of buffers
(defun vertical-center--kill-buffer ()
  (when vertical-center-mode
(setq vertical-center-num-buffers (1- vertical-center-num-buffers))))    

;; react to changes in the window height
(defun vertical-center--window-size-changed (arg)
  (vertical-center--lines-changed 0))

;; handle deletions of buffer text
(defun vertical-center--before-change (beginning end)
  (when (boundp 'vertical-center-num-lines)
(let ((num-lines 0))
  (while (< beginning end)
    (when (= (char-after beginning) ?\n)
      (setq num-lines (1- num-lines)))
    (setq beginning (1+ beginning)))
  (when (< num-lines 0)
    (vertical-center--lines-changed num-lines)))))

;; handle insertions into the buffer
(defun vertical-center--after-change (beginning end previous-length)
  (when (boundp 'vertical-center-num-lines)
(let ((num-lines 0))
  (while (< beginning end)
    (when (= (char-after beginning) ?\n)
      (setq num-lines (1+ num-lines)))
    (setq beginning (1+ beginning)))
  (when (> num-lines 0)
    (vertical-center--lines-changed num-lines)))))

;; update the display when either the buffer content or the window
;; height has changed
(defun vertical-center--lines-changed (num-lines)
  (setq vertical-center-num-lines (+ vertical-center-num-lines num-lines))
  (let ((top-margin (/ (- (window-height) vertical-center-num-lines) 2)))
    ;; set the top margin
    (overlay-put vertical-center-overlay 'before-string 
             (when (> top-margin 0)
               (make-string top-margin ?\n)))))

将上述代码保存在您选择的目录中名为 "vertical-center.el" 的文件中,然后将以下行添加到您的 .emacs 文件中:

(setq load-path (append load-path "<directory>"))
(autoload 'vertical-center-mode "vertical-center")

此处,<directory> 应该是您保存 "vertical-center.el" 文件的目录的路径。

重新启动 Emacs 后,您现在可以通过键入 M-x vertical-center-mode 来激活或停用该模式。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。