【问题标题】:Emacs: prevent cursor movement when scrolling off of the screenEmacs:滚动离开屏幕时防止光标移动
【发布时间】:2018-02-02 05:57:09
【问题描述】:

我正在查看 emacs 中的一些代码,这些代码具有一系列跨越多个页面的 for 循环。作者所做的缩进很差,因此很难判断循环的开始和结束位置。我正在使用高亮括号模式来查找循环位置。但是,循环通常会覆盖几页代码。因此,每当我滚动查看更多代码时,括号的颜色就会发生变化,并且我找不到循环结束的位置。我已经阅读了其他关于使用标记和多窗口滚动的帖子,但这似乎不适用于我的情况。锁定屏幕上的光标位置也无济于事,因为它仍然随屏幕移动。有什么建议或意见吗?

【问题讨论】:

    标签: emacs cursor scroll


    【解决方案1】:

    您可以使用forward-list (C-M-n) 和backward-list (C-M-p) 在括号内向前和向后导航。 (也可以试试forward-sexp (C-M-f) 和backward-sexp (C-M-b))。

    查看EmacsWiki on Navigating Parentheses、Emacs manual node on matching paretheses 和this SO thread on matching brackets。

    【讨论】:

      【解决方案2】:

      这是对https://github.com/nschum/highlight-parentheses.el/blob/master/highlight-parentheses.el 的修改,它允许您向上或向下滚动而不删除覆盖。您可以添加额外的this-command 语句以满足您的需要。

      (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】:

        我认为您正在寻找错误的解决方案。

        将光标放在以圆括号或花括号开头的循环上,然后按 C-M-n 这将运行命令 forward-list,它将使您的光标跳转到匹配的结束括号或花括号。

        打开缓冲区,移动到起始括号,使用 C-x 3 将窗口分成两半,移动到新窗口,然后使用forward-list 现在您应该可以看到起始括号在左侧,在右侧视图中的结尾括号。

        或者,使用 hideshow 折叠循环内的代码块以缩小尺寸并可能适合一个屏幕。

        还可以查看follow-mode,这将允许您在多个窗口中连续查看一个缓冲区,有效地使您可以看到的连续代码行数增加一倍或三倍。这将适用于show-paren-mode。

        另外,如果缩进真的很糟糕,运行mark-whole-buffer 和indent-region 来修复它。

        【讨论】:

          猜你喜欢
          • 2023-03-30
          • 1970-01-01
          • 2013-05-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-12-14
          相关资源
          最近更新 更多