Emacs: prevent cursor movement when scrolling off

2019-02-26 16:21发布

I am reviewing some code in emacs that has a series of for loops that span several pages. The indentations done by the author are poor so it is not easy to tell where loops begin and end. I am using the highlight parenthesis mode to find where loops. However, it is often the case that the loop covers a few pages of code. Thus whenever I scroll to see more code the colors of the parenthesis change and I can't find where the loop ends. I've read other's posts about using mark and multi-window scrolling but this doesn't seem to work in my situation. Also locking the cursor position on the screen isn't helpful since it still moves with the screen. Any suggestions or advice?

3条回答
淡お忘
2楼-- · 2019-02-26 16:49

Here is a modification of https://github.com/nschum/highlight-parentheses.el/blob/master/highlight-parentheses.el, which will permit you to scroll up or down without deleting the overlays. You can add additional this-command statements to suit your needs.

(defun hl-paren-highlight ()
  "Highlight the parentheses around point."
  (unless
      (or
        (eq this-command 'mwheel-scroll)
        (eq this-command 'scroll-up)
        (eq this-command 'scroll-down))
    (unless (= (point) hl-paren-last-point)
      (setq hl-paren-last-point (point))
      (let ((overlays hl-paren-overlays)
            pos1 pos2
            (pos (point)))
        (save-excursion
          (condition-case err
              (while (and (setq pos1 (cadr (syntax-ppss pos1)))
                          (cdr overlays))
                (move-overlay (pop overlays) pos1 (1+ pos1))
                (when (setq pos2 (scan-sexps pos1 1))
                  (move-overlay (pop overlays) (1- pos2) pos2)
                  ))
            (error nil))
          (goto-char pos))
        (dolist (ov overlays)
          (move-overlay ov 1 1))))))
查看更多
叛逆
3楼-- · 2019-02-26 16:51

I think you're looking for the wrong solution.

Place your cursor on loops starting parenthesis or curly bracket and press C-M-n This runs the command forward-list which will jump your cursor to the matching end paren or curly brace.

Open the buffer, move to the starting paren, use C-x 3 to split the window in half, move to the new window, and use forward-list now you should have the starting paren in view on the left and the ending paren in view on the right.

Alternatively, use hideshow to collapse blocks of code inside the loop to shrink the size and potentially fit it on one screen.

Also look into follow-mode this will allow you to view one buffer in multiple windows continuously effectively doubling or tripling the number of continuous lines of code you can see. This will work with show-paren-mode.

Also if the indentation is really so terrible, run mark-whole-buffer and indent-region to fix it up.

查看更多
唯我独甜
4楼-- · 2019-02-26 16:52

You can use forward-list (C-M-n) and backward-list (C-M-p) to navigate forward and backward over a parenthetical group. (Also try forward-sexp (C-M-f) and backward-sexp (C-M-b)).

Check out the EmacsWiki on Navigating Parentheses, the Emacs manual node on matching paretheses, and this SO thread on matching brackets.

查看更多
登录 后发表回答