【问题标题】:How to execute emacs grep-find link in the same window?如何在同一窗口中执行 emacs grep-find 链接?
【发布时间】:2024-04-24 22:40:01
【问题描述】:

当我使用grep-find 时,它会打开另一个窗口(框架中的区域),其中包含我可以选择的结果列表。当我选择一个时,它会在与grep-find 不同的窗口中打开目标文件。

如何让目标文件在与grep 结果相同的窗口中打开(用我实际查找的内容替换grep 结果窗口)。

如何防止 grep-find 打开单独的窗口(让它在当前窗口中打开)。我的目标是寻找东西,我找到它,我去找它,所有这些都在同一个窗口内。我想将此添加到我的.emacs 文件中。

【问题讨论】:

    标签: emacs dot-emacs


    【解决方案1】:

    看起来没有任何方法可以配置 compile 包来满足您的要求。并且没有简单的方法可以使用advice 来调整行为。我认为你必须求助于编辑实际跳转到错误的函数,你可以在 .emacs 中添加以下内容(在 Emacs 23.1 中测试):

    (eval-after-load "compile"
    '(defun compilation-goto-locus (msg mk end-mk)
      "Jump to an error corresponding to MSG at MK.
    All arguments are markers.  If END-MK is non-nil, mark is set there
    and overlay is highlighted between MK and END-MK."
      ;; Show compilation buffer in other window, scrolled to this error.
      (let* ((from-compilation-buffer (eq (window-buffer (selected-window))
                      (marker-buffer msg)))
         ;; Use an existing window if it is in a visible frame.
         (pre-existing (get-buffer-window (marker-buffer msg) 0))
         (w (if (and from-compilation-buffer pre-existing)
            ;; Calling display-buffer here may end up (partly) hiding
            ;; the error location if the two buffers are in two
            ;; different frames.  So don't do it if it's not necessary.
            pre-existing
          (let ((display-buffer-reuse-frames t)
            (pop-up-windows t))
            ;; Pop up a window.
            (display-buffer (marker-buffer msg)))))
         (highlight-regexp (with-current-buffer (marker-buffer msg)
                 ;; also do this while we change buffer
                 (compilation-set-window w msg)
                 compilation-highlight-regexp)))
    ;; Ideally, the window-size should be passed to `display-buffer' (via
    ;; something like special-display-buffer) so it's only used when
    ;; creating a new window.
    (unless pre-existing (compilation-set-window-height w))
    
    (switch-to-buffer (marker-buffer mk))
    
        ;; was
    ;; (if from-compilation-buffer
    ;;     ;; If the compilation buffer window was selected,
    ;;     ;; keep the compilation buffer in this window;
    ;;     ;; display the source in another window.
    ;;     (let ((pop-up-windows t))
    ;;       (pop-to-buffer (marker-buffer mk) 'other-window))
    ;;   (if (window-dedicated-p (selected-window))
    ;;       (pop-to-buffer (marker-buffer mk))
    ;;     (switch-to-buffer (marker-buffer mk))))
    ;; If narrowing gets in the way of going to the right place, widen.
    (unless (eq (goto-char mk) (point))
      (widen)
      (goto-char mk))
    (if end-mk
        (push-mark end-mk t)
      (if mark-active (setq mark-active)))
    ;; If hideshow got in the way of
    ;; seeing the right place, open permanently.
    (dolist (ov (overlays-at (point)))
      (when (eq 'hs (overlay-get ov 'invisible))
        (delete-overlay ov)
        (goto-char mk)))
    
    (when highlight-regexp
      (if (timerp next-error-highlight-timer)
          (cancel-timer next-error-highlight-timer))
      (unless compilation-highlight-overlay
        (setq compilation-highlight-overlay
          (make-overlay (point-min) (point-min)))
        (overlay-put compilation-highlight-overlay 'face 'next-error))
      (with-current-buffer (marker-buffer mk)
        (save-excursion
          (if end-mk (goto-char end-mk) (end-of-line))
          (let ((end (point)))
        (if mk (goto-char mk) (beginning-of-line))
        (if (and (stringp highlight-regexp)
             (re-search-forward highlight-regexp end t))
            (progn
              (goto-char (match-beginning 0))
              (move-overlay compilation-highlight-overlay
                    (match-beginning 0) (match-end 0)
                    (current-buffer)))
          (move-overlay compilation-highlight-overlay
                (point) end (current-buffer)))
        (if (or (eq next-error-highlight t)
            (numberp next-error-highlight))
            ;; We want highlighting: delete overlay on next input.
            (add-hook 'pre-command-hook
                  'compilation-goto-locus-delete-o)
          ;; We don't want highlighting: delete overlay now.
          (delete-overlay compilation-highlight-overlay))
        ;; We want highlighting for a limited time:
        ;; set up a timer to delete it.
        (when (numberp next-error-highlight)
          (setq next-error-highlight-timer
            (run-at-time next-error-highlight nil
                     'compilation-goto-locus-delete-o)))))))
    (when (and (eq next-error-highlight 'fringe-arrow))
      ;; We want a fringe arrow (instead of highlighting).
      (setq next-error-overlay-arrow-position
        (copy-marker (line-beginning-position)))))))
    

    eval-afer-load 部分只是确保您在 Emacs 定义它之后重新定义它,以便您的更改生效。

    【讨论】:

    • 非常感谢特雷!你拯救了我的一天。
    • Trey,是否可以调整您的功能,让 Shift 鼠标单击在同一个缓冲区中打开,而不是保持相同的鼠标单击行为在单独的窗口中打开?
    • @Ammari 不简单。 hack 方法是为 Shift-mouse 设置一个新的绑定,在本地设置一个变量(let ((use-same-window t)) ...),然后发出命令跳转到错误,并在上面的函数中选择你想要的行为。
    【解决方案2】:

    您可以添加绑定(例如 Alt-m)并执行以下操作

    (define-key grep-mode-map "\M-m" (lambda() 
                                       (interactive) 
                                       (compile-goto-error)
                                       (delete-other-windows)
                                       (kill-buffer "*grep*")))
    

    我没有找到用自定义函数替换标准“Enter”/鼠标单击绑定的方法

    【讨论】:

    • 标准的输入/鼠标单击绑定是缓冲区子字符串的文本属性,您可以编辑该属性,或更改编译添加该属性的方式
    【解决方案3】:

    还有另一种方法:

    (defun eab/compile-goto-error ()
      (interactive)
      (let ((cwc (current-window-configuration)))
        (funcall
         `(lambda ()
            (defun eab/compile-goto-error-internal ()
              (let ((cb (current-buffer))
                    (p (point)))
                (set-window-configuration ,cwc)
                (switch-to-buffer cb)
                (goto-char p ))))))
      (compile-goto-error)
      (run-with-timer 0.01 nil 'eab/compile-goto-error-internal))
    

    【讨论】:

      【解决方案4】:

      我有同样的问题,并在 emacs.stackexchange https://emacs.stackexchange.com/a/33908/20000找到了这个答案

      (defun my-compile-goto-error-same-window ()
        (interactive)
        (let ((display-buffer-overriding-action
               '((display-buffer-reuse-window
                  display-buffer-same-window)
                 (inhibit-same-window . nil))))
          (call-interactively #'compile-goto-error)))
      
      (defun my-compilation-mode-hook ()
        (local-set-key (kbd "o") #'my-compile-goto-error-same-window))
      
      (add-hook 'compilation-mode-hook #'my-compilation-mode-hook)
      

      *grep* 缓冲区中按o 将打开同一帧中的位置和文件。

      我发现这是一个优雅的解决方案,无需删除帧或过多的 lisp 代码,只需挂接到 compilation-mode-hook

      【讨论】:

        最近更新 更多