fix horizontal scroll

Issue #5 new
Yuriy Pitometsu repo owner created an issue

Do it in the window under cursor, not in the currently active window.

Comments (2)

  1. Yuriy Pitometsu reporter

    Take (window-buffer curwin) and edge cases from mwheel-scroll to hscroll-{left,right}.

    ;; workaround for smooth horizontal scrolling
    ;; TODO: C-u prefix
    (defun hscroll-left ()
      "Scroll left one step."
      (interactive)
      (let ((step
             (if (zerop hscroll-step)
                 nil
               hscroll-step)))
        (scroll-left step t)))
    
    (defun hscroll-right ()
      "Scroll right one step."
      (interactive)
      (let ((step
             (if (zerop hscroll-step)
                 nil
               hscroll-step)))
        (scroll-right step t)))
    
    (defun mwheel-scroll (event)
      "Scroll up or down according to the EVENT.
    This should be bound only to mouse buttons 4 and 5 on non-Windows
    systems."
      (interactive (list last-input-event))
      (let* ((curwin (if mouse-wheel-follow-mouse
                         (prog1
                             (selected-window)
                           (select-window (mwheel-event-window event)))))
         (buffer (window-buffer curwin))
         (opoint (with-current-buffer buffer
               (when (eq (car-safe transient-mark-mode) 'only)
                 (point))))
             (mods
          (delq 'click (delq 'double (delq 'triple (event-modifiers event)))))
             (amt (assoc mods mouse-wheel-scroll-amount)))
        ;; Extract the actual amount or find the element that has no modifiers.
        (if amt (setq amt (cdr amt))
          (let ((list-elt mouse-wheel-scroll-amount))
        (while (consp (setq amt (pop list-elt))))))
        (if (floatp amt) (setq amt (1+ (truncate (* amt (window-height))))))
        (when (and mouse-wheel-progressive-speed (numberp amt))
          ;; When the double-mouse-N comes in, a mouse-N has been executed already,
          ;; So by adding things up we get a squaring up (1, 3, 6, 10, 15, ...).
          (setq amt (* amt (event-click-count event))))
        (unwind-protect
        (let ((button (mwheel-event-button event)))
          (cond ((eq button mouse-wheel-down-event)
                     (condition-case nil (funcall mwheel-scroll-down-function amt)
                       ;; Make sure we do indeed scroll to the beginning of
                       ;; the buffer.
                       (beginning-of-buffer
                        (unwind-protect
                            (funcall mwheel-scroll-down-function)
                          ;; If the first scroll succeeded, then some scrolling
                          ;; is possible: keep scrolling til the beginning but
                          ;; do not signal an error.  For some reason, we have
                          ;; to do it even if the first scroll signaled an
                          ;; error, because otherwise the window is recentered
                          ;; for a reason that escapes me.  This problem seems
                          ;; to only affect scroll-down.  --Stef
                          (set-window-start (selected-window) (point-min))))))
            ((eq button mouse-wheel-up-event)
                     (condition-case nil (funcall mwheel-scroll-up-function amt)
                       ;; Make sure we do indeed scroll to the end of the buffer.
                       (end-of-buffer (while t (funcall mwheel-scroll-up-function)))))
            (t (error "Bad binding in mwheel-scroll"))))
          (if curwin (select-window curwin)))
        ;; If there is a temporarily active region, deactivate it if
        ;; scrolling moves point.
        (when opoint
          (with-current-buffer buffer
        (when (/= opoint (point))
          ;; Call `deactivate-mark' at the original position, so that
          ;; the original region is saved to the X selection.
          (let ((newpoint (point)))
            (goto-char opoint)
            (deactivate-mark)
            (goto-char newpoint))))))
      (when (and mouse-wheel-click-event mouse-wheel-inhibit-click-time)
        (if mwheel-inhibit-click-event-timer
        (cancel-timer mwheel-inhibit-click-event-timer)
          (add-hook 'pre-command-hook 'mwheel-filter-click-events))
        (setq mwheel-inhibit-click-event-timer
          (run-with-timer mouse-wheel-inhibit-click-time nil
                  'mwheel-inhibit-click-timeout))))
    
  2. Yuriy Pitometsu reporter

    UPD: C-u and NOT switch to scrolled window.

    (defun horizontal-scroll-left ()
      "Scroll left one step."
      (let ((step
             (if (zerop hscroll-step)
                 nil
               hscroll-step)))
        (scroll-left step t)))
    
    (defun hscroll-left ()
      "Scroll left one step."
      (interactive)
      (horizontal-scroll-left))
    
    
    (defun horizontal-mscroll-left (event)
      "Scroll left one step according to the EVENT."
      (interactive (list last-input-event))
      (with-current-buffer (window-buffer
                            (if mouse-wheel-follow-mouse
                                (prog1
                                    (selected-window)
                                  (select-window (mwheel-event-window event)))))
        (horizontal-scroll-left)))
    
    (defun horizontal-scroll-right ()
      "Scroll right one step."
      (let ((step
             (if (zerop hscroll-step)
                 nil
               hscroll-step)))
        (scroll-right step t)))
    
    (defun hscroll-right ()
      "Scroll right one step."
      (interactive)
      (horizontal-scroll-right))
    
    (defun horizontal-mscroll-right (event)
      "Scroll right one step according to the EVENT."
      (interactive (list last-input-event))
      (with-current-buffer (window-buffer
                            (if mouse-wheel-follow-mouse
                                (prog1
                                    (selected-window)
                                  (select-window (mwheel-event-window event)))))
        (horizontal-scroll-right)))
    
  3. Log in to comment