[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: find-file-hook, recenter, scroll-conservatively and save-place

From: Juri Linkov
Subject: Re: find-file-hook, recenter, scroll-conservatively and save-place
Date: Thu, 31 Jan 2019 22:57:18 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (x86_64-pc-linux-gnu)

>> My question: is there a better hook that we could use for save-place
>> restoration purposes?
>> I also looked into xref, and there the behavior is much better: the
>> (recenter) is included in the default configuration of
>> xref-after-jump-hook and it works in all the scenarios that I have
>> tried.
> Juri, should we handle this somehow?

I don't like the default recentering too.  I had to fix it with
a lot of customization, e.g.

(add-hook 'xref-after-jump-hook 'reposition-window)
(add-hook 'xref-after-return-hook 'reposition-window)
(add-hook 'find-function-after-hook 'reposition-window)

;; Let `C-M-a' (beginning-of-defun) not scroll the window
;; when after jumping point stays within current window bounds
(advice-add 'beginning-of-defun :around
            (lambda (orig-fun &rest args)
              (let ((w-s (window-start))
                    (w-e (window-end)))
                (apply orig-fun args)
                (when (and
                       ;; Only when used interactively
                       (eq this-command 'beginning-of-defun)
                       ;; And only when jumping outside of window
                       ;; to the center of the window
                       (or (< (point) w-s) (> (point) w-e)))
                  (recenter 11))))
            '((name . recenter)))

;; Save and restore window start positions on returning to previous search
(setq isearch-push-state-function
      (lambda ()
        ;; Recenter new search hits outside of window boundaries
        (when (and isearch-success (not (pos-visible-in-window-p)))
          ;; reposition-window takes too much time in large buffers
          (if (or (eq major-mode 'fundamental-mode)
                  (> (buffer-size) 1000000))
              (recenter 11)
            (condition-case nil
                ;; Prevent errors from reposition-window
              (error nil))))
        `(lambda (cmd)
           (when isearch-success
             (set-window-start nil ,(window-start))))))

> That is, provide 'window-point' and 'window-start' action alist entries
> with the former allowing the 'switch-to-buffer-preserve-window-point'
> logic (among others) and the latter optionally allowing to recenter.

Like switch-to-buffer-preserve-window-point used in dired-find-file?

> I don't know how 'save-place' could pass these on to the
> 'pop-to-buffer' call in 'find-file' though.  Can you think
> of any other use cases where these would be helpful?

Interesting question.  Maybe introduce two new buffer-local variables
'window-point' and 'window-start' that a hook could set and then
display-buffer could read and call functions window-point and window-start?
These buffer-local values should be used only once and should be reverted
to nil after the first use.

BTW, a related question: should save-place save window-start as well?
It should be easy to implement after this problem is solved.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]