【问题标题】:Restore vertical buffer position in Emacs在 Emacs 中恢复垂直缓冲区位置
【发布时间】:2013-11-27 16:34:28
【问题描述】:

我有以下代码,我使用这些代码更新了我正在使用的一些遗留代码的编码样式。它现在可以正常工作,但是令人讨厌的是,在区域上执行命令后,缓冲区会滚动,因此该区域的顶部位于窗口的顶部。我该如何解决这个问题?

(defun mkm/cleanup ()
  (interactive)
  (let (start end doit)
    (if (region-active-p)
        (setq start (region-beginning) end (region-end) doit t)
      (progn (setq start (point-min) end (point-max))
             (setq doit (y-or-n-p "Really cleanup whole buffer? "))))
    (if doit
        (save-excursion
          (save-restriction
            (narrow-to-region start end)
            (replace-regexp "\([[:alpha:]]\)[ ]+" "\1 " nil (point-min) (point-max))
            (replace-regexp " *&" " &" nil (point-min) (point-max))
            (replace-regexp "[ ]*\\*" " *" nil (point-min) (point-max))
            (replace-regexp "( +" "(" nil (point-min) (point-max))
            (replace-regexp " +)" ")" nil (point-min) (point-max))
            (replace-regexp "{ +" "{" nil (point-min) (point-max))
            (replace-regexp "} +" "}" nil (point-min) (point-max))
            (replace-regexp " +]" "]" nil (point-min) (point-max))
            (replace-regexp "\s*,\s*" ", " nil (point-min) (point-max))
            (replace-regexp "\s*->\s*" "->" nil (point-min) (point-max))
            (replace-regexp "\\[ +" "\\[" nil (point-min) (point-max))
            (replace-regexp " += +" " = " nil (point-min) (point-max))
            (replace-regexp "\([[:alpha:]]\)[ ]+>" "\1>" nil (point-min) (point-max))
            (replace-regexp "<\s+" "<" nil (point-min) (point-max))
            (replace-regexp "<<\([^\s]\)" "<< \1" nil (point-min) (point-max))))
      (message "Cleanup aborted."))))

【问题讨论】:

    标签: regex emacs elisp


    【解决方案1】:

    这是代码,相关函数是save-window-excursion

    (defun mkm/cleanup ()
      (interactive)
      (let (start end doit)
        (if (region-active-p)
            (setq start (region-beginning)
                  end (region-end)
                  doit t)
          (setq start (point-min)
                end (point-max)
                doit (y-or-n-p "Really cleanup whole buffer? ")))
        (if doit
            (save-window-excursion
              (save-excursion
                (save-restriction
                  (narrow-to-region start end)
                  (mapc
                   (lambda(x)
                     (replace-regexp
                      (car x) (cdr x)
                      nil (point-min) (point-max)))
                   '(("\([[:alpha:]]\)[ ]+" . "\1 ")
                     (" *&" . " &")("[ ]*\\*" . " *")
                     ("( +" . "(")(" +)" . ")")("{ +" . "{")
                     ("} +" . "}")(" +]" . "]")("\s*,\s*" . ", ")
                     ("\s*->\s*" . "->")("\\[ +" . "\\[")
                     (" += +" . " = ")
                     ("\([[:alpha:]]\)[ ]+>" . "\1>")
                     ("<\s+" . "<")("<<\([^\s]\)" . "<< \1"))))))
          (message "Cleanup aborted.")))) 
    

    【讨论】:

    • 太好了,成功了!我认为您的意思是 save-window-excursion 是相关功能。感谢您提出重构的建议,我认为这样会更好:)
    【解决方案2】:

    不要使用save-excursionsave-excursion 说,“做这件事,然后回到这里。”听起来您希望它改为“做这些事情,并保持在最终的位置。”

    【讨论】:

      【解决方案3】:

      您可能只需要:

      (let ((windowstart (window-start)))
        ;; ...
        (set-window-start (selected-window) windowstart))
      

      【讨论】:

        猜你喜欢
        • 2012-08-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-24
        • 1970-01-01
        • 2013-12-08
        • 1970-01-01
        相关资源
        最近更新 更多