Emacs — calculating new window-start/end without redisplay

前端 未结 3 592
不思量自难忘°
不思量自难忘° 2021-01-06 02:55

Is it possible to calculate a new window-start/end without a redisplay occurring? If so, then an example would be greatly appreciated. If not, th

相关标签:
3条回答
  • 2021-01-06 03:31

    Offhand, I'd say that the error is raised because you pass a BOUND arg to a search function. For example:

    (re-search-backward "\n" window-start t)
    
    (re-search-backward "\s\\|\t" pbol t)
    

    Check your values of window-start and pbol. Remember that when you search backward the bound must not be greater than the current position (point).

    0 讨论(0)
  • 2021-01-06 03:33

    I think you want to use jit-lock-register instead of post-command-hook. This way, the redisplay code will call you back once it has decided of a window-start and you'll be able to add the overlays you want before the buffer's content is displayed.

    0 讨论(0)
  • 2021-01-06 03:36

    See also Emacs bug tracker feature request #22404 (which has not yet been implemented, but the mailing archive contains a rough draft rudimentary patch that creates a new hook for this specific issue): https://debbugs.gnu.org/cgi/bugreport.cgi?bug=22404

    • Minor-mode for testing window-start and window-end BEFORE visual redisplay.
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ;; test-mode
    ;; A minor-mode for testing `window-start` / `window-end` BEFORE visual redisplay.
    
    (defvar test-this-command nil
    "This local variable is set within the `post-command-hook`; and,
    is also used by the `window-scroll-functions` hook.")
    (make-variable-buffer-local 'test-this-command)
    
    (defun test-post-command-hook-fn ()
    "A function attached to the `post-command-hook`."
      (setq test-this-command this-command)
      (test-demo-fn))
    
    (defun test-window-scroll-functions-fn (win _start)
    "A function attached to the `window-scroll-functions` hook."
      (test-demo-fn))
    
    (defun test-demo-fn ()
    "This is a test-mode demonstration function."
      (when
          (and
            test-mode
            test-this-command
            (window-live-p (get-buffer-window (current-buffer)))
            (not (minibufferp))
            (pos-visible-in-window-p (point)
              (get-buffer-window (current-buffer) (selected-frame)) t))
        (let* (
            (selected-window (selected-window))
            (window-start (window-start selected-window))
            (window-end (window-end selected-window t)) )
          (message "window-start: %s | window-end: %s" window-start window-end)
          (setq test-this-command nil) )))
    
    (define-minor-mode test-mode
    "A minor-mode for testing `window-start` / `window-end` BEFORE visual redisplay."
      :init-value nil
      :lighter " TEST"
      :keymap nil
      :global nil
      :group nil
      (cond
        (test-mode
          (set (make-local-variable 'scroll-conservatively) 101)
          (add-hook 'post-command-hook 'test-post-command-hook-fn nil t)
          (add-hook 'window-scroll-functions 'test-window-scroll-functions-fn nil t)
          (when (called-interactively-p 'any)
            (message "Turned ON `test-mode`.")))
        (t
          (kill-local-variable 'scroll-conservatively)
          (kill-local-variable 'test-this-command)
          (remove-hook 'post-command-hook 'test-post-command-hook-fn t)
          (remove-hook 'window-scroll-functions 'test-window-scroll-functions-fn t)
          (when (called-interactively-p 'any)
            (message "Turned OFF `test-mode`.") ))))
    
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    
    0 讨论(0)
提交回复
热议问题