Emacs 26.3: jerky scroll - emacs

Emac's scrolling is quite jerky and unpredictable. Weirdly, as you scroll, the top line nearly never is a line with no characters--but is very very rarely. My best guess is that emacs's vertical scrollbar is perhaps not considering the number of lines in the file but the number of characters, or something? Then once it finds the character it wants to have on the top line it backtracks to find the beginning of that character?
Any way to simply have this give every line an equal share of the scrollbar scroll region?

I added the following to my ~/.emacs file.
The first three adjust appearance while using the scroll bar. Just scroll-conservatively may be enough in most cases but I see edge cases where the other two seem to help. I don't have a full explanation and would like to know more if anyone has an idea.
The next two affect the mouse's scroll wheel. Specifically, the mouse-wheel-scroll-amount setting means move 2 lines per wheel click normally, 10 lines if shift is being held down. or entire page (what nil means in this use) (the number of lines on the screen) if control is being held down. mouse-wheel-progressive-speed prevents scroll from getting faster and faster as you do more of it.
(setq scroll-step 1)
(setq scroll-conservatively 10000)
(setq auto-window-vscroll nil)
(setq mouse-wheel-scroll-amount '(2 ((shift) . 10) ((control) . nil)))
(setq mouse-wheel-progressive-speed nil)

Related

Moving point to the end of the last line without reorienting the buffer

Is it possible to move point to the end of a buffer without reorienting the text around that line? This seems to be the default behaviour of goto-char. My goal is to correct a minor annoyance which places the cursor at the second last line when I press "L". I wrote a custom function to do this properly but now when I move the point to the last line the screen scrolls down half a page and it becomes the center of the buffer.
(defun cf-last-line (count) (interactive "p")
(let ((max (truncate (window-screen-lines))))
(move-to-window-line max)
(line-move (* -1 (1- count)) t t)
(beginning-of-line)))
Edit: It turns out my problem is related to the fact that the GUI shows partial lines (which may appear to be fully exposed but upon closer inspection lie just below the status bus). I suppose my question then becomes whether or not it is possible to have the point lie on such a partial line (though I suspect this is unlikely) without moving it to the center and if not whether it is possible to instead prevent the X11 frame from showing partial lines at the bottom of the window.
Solution as described by lawlist:
(setq scroll-conservatively 101)
(setq make-cursor-line-fully-visible nil)

split window vertically instead of horizontally in sql-mode

When I connect to the database, it opens a new window by split horizontally, but that will cause the buffer too narrow to display the sql and hard to read. So how to disable split the windows horizontally? I want to split the windows always vertically.
Assuming sql-mode uses emacs to dertermine the best way to spilt (and not forces a certain way) and you now have the buffers next to each other and want them atop of each other you can do this by
(setq split-height-threshold 0)
(setq split-width-threshold nil)
If the directions are wrong due to a horizontal vertical misunderstanding you can switch nil and 0

Centre Emacs buffer within window

I wrap all my code at 80 columns, and there are times where the Emacs window is wider than 80 columns and there is a lot of unused whitespace on the right side.
I would like to position the Emacs buffer, so all the text is displayed in the middle of the window.
This is different to centre aligning text (more akin to the whitespace on either side of the text when viewing pdfs).
I think this can be achieved by dynamically adjusting the fringe mode widths, depending on the current window size, but I'm not sure where to start. Any ideas?
As demonstrated here this is indeed possible:
(set-fringe-mode
(/ (- (frame-pixel-width)
(* 80 (frame-char-width)))
2))
However, as I am testing this I seem to have more luck with using margins, at least when also resizing my frame:
(defun my-resize-margins ()
(let ((margin-size (/ (- (frame-width) 80) 2)))
(set-window-margins nil margin-size margin-size)))
(add-hook 'window-configuration-change-hook #'my-resize-margins)
(my-resize-margins)
Here is a function which should do what you want, using margins instead of fringes (since I tend to display buffer boundaries in the fringe and I find it becomes ugly if the fringe is too large).
(defun my/center (width)
(interactive "nBuffer width: ")
(let* ((adj (- (window-text-width)
width))
(total-margin (+ adj
left-margin-width
right-margin-width)))
(setq left-margin-width (/ total-margin 2))
(setq right-margin-width (- total-margin left-margin-width)))
(set-window-buffer (selected-window) (current-buffer)))
You ask to display the buffer in the center of the window, which just moves some of the extra whitespace to the left of the buffer, from the right.
How about a solution that eliminates that extra whitespace instead? If that is acceptable, here are two approaches.
If the buffer is alone in its frame, then you can fit the frame to the buffer, using library fit-frame.el. I bind command fit-frame to C-x C-_. This saves space not only within Emacs but for your desktop. (Library zoom-frm.el lets you also shrink/enlarge a frame incrementally, so you can save space by shrinking a frame when you don't need to see its content in detail.)
If not (so the buffer is shown in a frame where there are multiple windows), and if the buffer's window has another window to the left or right of it, then you can do one of the following:
2a. If the buffer's window has another window to the left or right of it, then you can use command fit-window-to-buffer. But you will also need to set option fit-window-to-buffer-horizontally to non-nil.
2b. Use C-{ (shrink-window-horizontally), followed by C-x z z z..., to incrementally shrink the window width (removing the extra whitespace).
2c. Load library face-remap+.el. Whenever you use text-scaling (e.g. C-x C- or C-x =), the window size grows or shrinks along with the text size, so you don't get extra whitespace added at the right when you shrink the text. This is controlled by user option text-scale-resize-window.
Center window mode
https://github.com/anler/centered-window-mode
Global minor mode that centers the text of the window.
If another window is visible the text goes back to normal if its width is less than "cwm-centered-window-width."
Modern answer is https://github.com/rnkn/olivetti or https://github.com/joostkremers/writeroom-mode, both worked immediately for me where other things did not

Changing Horizontal Scrolling in Emacs

Before using Emacs, I used Nano, and I would like to change the horizontal scrolling in Emacs to be slightly more intuitive. Just for clarification, by horizontal scrolling, I mean the forward-char and backward-char movement out of the window boundaries, and not the Ctrl+x > commands.
The main thing I would like to figure out is how to have it only scroll one letter at a time (I guess like horizontal "smooth-scrolling"), or maybe even one full pane width at a time.
I've tried changing some variables containing "hscroll" (like hscroll-margin), but none of them seem to get me closer to the behavior I am looking for.
What variables should I be looking at to get horizontal scrolling working how I want?
I think that you can get what you want with:
(setq hscroll-margin 0)
(setq hscroll-step 1)
If not, can you describe how you would like the behaviour to be.

How to recenter an Emacs buffer around an overlay

I want to recenter an Emacs buffer so as to show as much as possible of an overlay and of its context around it. Specifically, I want this behavior:
If the overlay fits in the visible window, I want to show as much as line before it as lines after it;
Otherwise, I want the beginning of the overlay to be shown.
This behavior is somewhat similar to what I see when using different regions highlighted in ediff.
Is there a not-so-complicated way to achieve this? I tried to look into the ediff code (specifically ediff-util.el) but things seems very complicated to me.
I'm not quite sure what usage you're looking for, but this code should do what you want.
It can be called with an overlay, or if called interactively, will choose one of the overlays at the current position and do the action on it.
(defun make-overlay-visible (overlay)
"given an overlay, center it on the window
(or make beginning visible if it cannot fit in the window)"
(interactive (list (car (overlays-at (point)))))
(when overlay
(goto-char (overlay-start overlay))
(recenter 0)
(when (and (pos-visible-in-window-p (overlay-start overlay))
(pos-visible-in-window-p (overlay-end overlay)))
(goto-char (/ (+ (overlay-start overlay) (overlay-end overlay)) 2))
(recenter))))