【问题标题】:Make Emacs Compilation window show source with error in other frame使 Emacs 编译窗口在其他框架中显示错误的源代码
【发布时间】:2013-11-08 00:37:28
【问题描述】:

我有两个框架;第二帧总是用来显示 *compilation* 缓冲区。我想让 (compilation-goto-error) 导航到有错误的行到另一个框架。我已经实现了一个功能来做到这一点:

(defun my-compile-goto-error-other-frame ()
  (interactive)
  ;;; just for simplification; in the read life there should be 
  ;;; a search of a frame by name
  (other-frame 1)
  ;;; I rely on the behavior when 'compile-goto-error' uses an another 
  ;;; window in a frame to show a source code
  (switch-to-buffer "*compilation*")
  (compile-goto-error)
  (recenter-top-bottom)
  (delete-other-windows))

我认为这个实现很丑(但在大多数情况下它有效)。

  • 如何摆脱创建新可见缓冲区并杀死未使用的缓冲区的顺序?有时会有闪烁。可能有办法在不可见的情况下执行这些步骤并只显示结果?

  • 如何正确实现 (my-compile-display-error-other-frame)

与以下类似?

(defun my-compile-display-error-other-frame ()
  (my-compile-goto-error-other-frame)
  (other-frame 1))

谢谢。

更新

@lawlist,抱歉不清楚。此代码中没有基础功能。标准的 'compile-goto-error' 和我希望实现我自己的,它将在另一个框架中打开代码。我相信我的实现并不好;我想它可能看起来像:

(defun ... () 
  ;;; Make Emacs think that we are still in the frame where 
  ;;; "*compilation*" buffer active is
  (with-context-of-current-frame 
     ;;; But make another frame called "main-window" active
     (with-another-frame "main-window"
        ;;; Since *compile-mode* "thinks" that it still the same frame (because of context)
        ;;; it creates a new window here but actually the window with source
        ;;; code buffer is shown in frame "main-window"
        (progn (compile-goto-error)
               (recenter-top-bottom)))))

在我的实现中,我不喜欢我必须切换到另一个帧,然后选择一个带有编译日志的缓冲区(在慢速/远程终端上很明显),然后执行“compile-goto-error”,然后之前删除打开“编译”缓冲区。我认为这些步骤是多余的。

【问题讨论】:

  • How to get rid of the sequence of creating new visible buffers and kill unused ones? 我在您的代码中没有看到创建然后删除的缓冲区——请详细说明。
  • 如果没有更多信息,我最好的猜测是负责创建缓冲区 *compilation* 的底层函数是您想要修改的,以便它显示在新框架中,而不是在新框架中显示当前帧中的窗口 -- 然后删除当前帧中的窗口;然后你切换到另一个框架;然后切换到编译缓冲区。我们需要知道负责的函数的名称,例如,makeinfo-recenter-compilation-buffer?无论是使用display-buffer 还是pop-to-buffer 或其他东西都会影响你如何处理这种情况。
  • 我明白了——函数 compilation-start 中的变量 outbuf 。 . .很快就会更新。
  • @lawist,我已经更新了这个问题。目前还没有负责的功能。
  • 是的,有一些底层函数——关键的是compilation-start,它控制显示*compilation*缓冲区——这是需要调整的,如回答。我已经发布了一个适用于 OSX 上当前版本的 Emacs Trunk 的答案。如果您使用的是 Windows,则可能需要一个额外的步骤——请告诉我,我们可以添加一个 switch-to-frame(我认为 Emacs for Windows 的最后一个稳定版本有一个错误)。请务必将正在编译的缓冲区的帧名称设置为答案开头描述的可识别名称。

标签: emacs compilation elisp


【解决方案1】:

此答案要求包含要编译的缓冲区的帧必须命名为以下之一 - 例如,使用 set-buffer-nameMAINSYSTEMORGMISCELLANEOUS。然后,一旦您设置了要编译的缓冲区的帧名称,运行M-x compile。如果您想了解更多关于这个过程的信息,请访问我的另一个线程:How to intercept a file before it opens and decide which frame

compilation-start被修改的相关部分是与变量outbuf相关的部分。

(defvar regexp-frame-names "^\\(?:MAIN\\|SYSTEM\\|ORG\\|MISCELLANEOUS\\|COMPILATION\\)$"
    "Regexp matching frames with specific names.")

(defvar zweibaranov-buffer-regexp nil
  "Regexp of file / buffer names displayed in frame `COMPILATION`.")
(setq zweibaranov-buffer-regexp '("\\*compilation\\*"))

(defun zweibaranov-display-buffer-pop-up-frame (buffer alist)
  (cond
    ((regexp-match-p zweibaranov-buffer-regexp (buffer-name buffer))
      (if (get-frame "COMPILATION")
          (switch-to-frame "COMPILATION")
        ;; If unnamed frame exists, then take control of it.
        (catch 'break (dolist (frame (frame-list))
          (if (not (string-match regexp-frame-names (frame-parameter frame 'name)))
            (throw 'break (progn
              (switch-to-frame (frame-parameter frame 'name))
              (set-frame-name "COMPILATION"))))))
        ;; If dolist found no unnamed frame, then create / name it.
        (if (not (get-frame "COMILATION"))
          (progn
            (make-frame)
            (set-frame-name "COMPILATION"))) )
      (set-window-buffer (selected-window) (buffer-name buffer))
      (set-buffer (buffer-name buffer)) )
    (t nil) ))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; REGEXP FUNCTION ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun regexp-match-p (regexps string)
"Before the lisp function, define the variable like this:\n
(defvar example-regexp nil
  \"Regexps matching `buffer-name buffer` for frame name `SYSTEM`.\")
    (setq example-regexp '(\"\\(\\*foo\\*\\|\\*bar\\*\\)\"))
\nWithin the lisp function, use something like this:\n
(regexp-match-p example-regexp (buffer-name buffer))
\nOr, this:\n
(regexp-match-p example-regexp buffer-filename)"
  ;; (setq case-fold-search nil) ;; take case into consideration
  (catch 'matched
    (dolist (regexp regexps)
      (if (string-match regexp string)
        (throw 'matched t)))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; FRAME UTILITIES ;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; http://www.emacswiki.org/emacs/frame-fns.el
(defun get-frame-name (&optional frame)
  "Return the string that names FRAME (a frame).  Default is selected frame."
  (unless frame (setq frame (selected-frame)))
  (if (framep frame)
      (cdr (assq 'name (frame-parameters frame)))
    (error "Function `get-frame-name': Argument not a frame: `%s'" frame)))

;; http://www.emacswiki.org/emacs/frame-fns.el
(defun get-frame (frame)
  "Return a frame, if any, named FRAME (a frame or a string).
  If none, return nil.
  If FRAME is a frame, it is returned."
  (cond ((framep frame) frame)
        ((stringp frame)
         (catch 'get-a-frame-found
           (dolist (fr (frame-list))
             (when (string= frame (get-frame-name fr))
               (throw 'get-a-frame-found fr)))
           nil))
        (t
         (error
          "Function `get-frame-name': Arg neither a string nor a frame: `%s'"
          frame))))

;; https://stackoverflow.com/questions/17823448/if-frame-named-xyz-exists-then-switch-to-that-frame
(defun switch-to-frame (frame-name)
  (let ((frames (frame-list)))
    (catch 'break
      (while frames
        (let ((frame (car frames)))
          (if (equal (frame-parameter frame 'name) frame-name)
              (throw 'break (select-frame-set-input-focus frame))
            (setq frames (cdr frames))))))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(require 'compile)

(defalias 'compilation-start 'lawlist-compilation-start)

(defun lawlist-compilation-start (command &optional mode name-function highlight-regexp)
  "Run compilation command COMMAND (low level interface).
If COMMAND starts with a cd command, that becomes the `default-directory'.
The rest of the arguments are optional; for them, nil means use the default.

MODE is the major mode to set in the compilation buffer.  Mode
may also be t meaning use `compilation-shell-minor-mode' under `comint-mode'.

If NAME-FUNCTION is non-nil, call it with one argument (the mode name)
to determine the buffer name.  Otherwise, the default is to
reuses the current buffer if it has the proper major mode,
else use or create a buffer with name based on the major mode.

If HIGHLIGHT-REGEXP is non-nil, `next-error' will temporarily highlight
the matching section of the visited source line; the default is to use the
global value of `compilation-highlight-regexp'.

Returns the compilation buffer created."
  (or mode (setq mode 'compilation-mode))
  (let* ((name-of-mode
      (if (eq mode t)
          "compilation"
        (replace-regexp-in-string "-mode\\'" "" (symbol-name mode))))
     (thisdir default-directory)
     (thisenv compilation-environment)
     outwin outbuf)
    (with-current-buffer
    (setq outbuf
          (display-buffer (get-buffer-create
               (compilation-buffer-name name-of-mode mode name-function)) '(zweibaranov-display-buffer-pop-up-frame)))
      (let ((comp-proc (get-buffer-process (current-buffer))))
      (if comp-proc
          (if (or (not (eq (process-status comp-proc) 'run))
                  (eq (process-query-on-exit-flag comp-proc) nil)
                  (yes-or-no-p
                   (format "A %s process is running; kill it? "
                           name-of-mode)))
              (condition-case ()
                  (progn
                    (interrupt-process comp-proc)
                    (sit-for 1)
                    (delete-process comp-proc))
                (error nil))
            (error "Cannot have two processes in `%s' at once"
                   (buffer-name)))))
      ;; first transfer directory from where M-x compile was called
      (setq default-directory thisdir)
      ;; Make compilation buffer read-only.  The filter can still write it.
      ;; Clear out the compilation buffer.
      (let ((inhibit-read-only t)
        (default-directory thisdir))
    ;; Then evaluate a cd command if any, but don't perform it yet, else
    ;; start-command would do it again through the shell: (cd "..") AND
    ;; sh -c "cd ..; make"
    (cd (cond
             ((not (string-match "\\`\\s *cd\\(?:\\s +\\(\\S +?\\|'[^']*'\\|\"\\(?:[^\"`$\\]\\|\\\\.\\)*\"\\)\\)?\\s *[;&\n]"
                                 command))
              default-directory)
             ((not (match-end 1)) "~")
             ((eq (aref command (match-beginning 1)) ?\')
              (substring command (1+ (match-beginning 1))
                         (1- (match-end 1))))
             ((eq (aref command (match-beginning 1)) ?\")
              (replace-regexp-in-string
               "\\\\\\(.\\)" "\\1"
               (substring command (1+ (match-beginning 1))
                          (1- (match-end 1)))))
             ;; Try globbing as well (bug#15417).
             (t (let* ((substituted-dir
                        (substitute-env-vars (match-string 1 command)))
                       ;; FIXME: This also tries to expand `*' that were
                       ;; introduced by the envvar expansion!
                       (expanded-dir
                        (file-expand-wildcards substituted-dir)))
                  (if (= (length expanded-dir) 1)
                      (car expanded-dir)
                    substituted-dir)))))
    (erase-buffer)
    ;; Select the desired mode.
    (if (not (eq mode t))
            (progn
              (buffer-disable-undo)
              (funcall mode))
      (setq buffer-read-only nil)
      (with-no-warnings (comint-mode))
      (compilation-shell-minor-mode))
        ;; Remember the original dir, so we can use it when we recompile.
        ;; default-directory' can't be used reliably for that because it may be
        ;; affected by the special handling of "cd ...;".
        ;; NB: must be done after (funcall mode) as that resets local variables
        (set (make-local-variable 'compilation-directory) thisdir)
    (set (make-local-variable 'compilation-environment) thisenv)
    (if highlight-regexp
        (set (make-local-variable 'compilation-highlight-regexp)
         highlight-regexp))
        (if (or compilation-auto-jump-to-first-error
        (eq compilation-scroll-output 'first-error))
            (set (make-local-variable 'compilation-auto-jump-to-next) t))
    ;; Output a mode setter, for saving and later reloading this buffer.
    (insert "-*- mode: " name-of-mode
        "; default-directory: "
                (prin1-to-string (abbreviate-file-name default-directory))
        " -*-\n"
        (format "%s started at %s\n\n"
            mode-name
            (substring (current-time-string) 0 19))
        ;; The command could be split into several lines, see
        ;; `rgrep' for example.  We want to display it as one
        ;; line.
        (apply 'concat (split-string command (regexp-quote "\\\n") t))
        "\n")
    (setq thisdir default-directory))
      (set-buffer-modified-p nil))
    ;; Pop up the compilation buffer.
    ;; http://lists.gnu.org/archive/html/emacs-devel/2007-11/msg01638.html
    (setq outwin (display-buffer outbuf))
    (with-current-buffer outbuf
      (let ((process-environment
         (append
          compilation-environment
          (if (if (boundp 'system-uses-terminfo);`If' for compiler warning.
              system-uses-terminfo)
          (list "TERM=dumb" "TERMCAP="
            (format "COLUMNS=%d" (window-width)))
        (list "TERM=emacs"
              (format "TERMCAP=emacs:co#%d:tc=unknown:"
                  (window-width))))
          ;; Set the EMACS variable, but
          ;; don't override users' setting of $EMACS.
          (unless (getenv "EMACS")
        (list "EMACS=t"))
          (list "INSIDE_EMACS=t")
          (copy-sequence process-environment))))
    (set (make-local-variable 'compilation-arguments)
         (list command mode name-function highlight-regexp))
    (set (make-local-variable 'revert-buffer-function)
         'compilation-revert-buffer)
    (set-window-start outwin (point-min))

    ;; Position point as the user will see it.
    (let ((desired-visible-point
           ;; Put it at the end if `compilation-scroll-output' is set.
           (if compilation-scroll-output
           (point-max)
         ;; Normally put it at the top.
         (point-min))))
      (if (eq outwin (selected-window))
          (goto-char desired-visible-point)
        (set-window-point outwin desired-visible-point)))

    ;; The setup function is called before compilation-set-window-height
    ;; so it can set the compilation-window-height buffer locally.
    (if compilation-process-setup-function
        (funcall compilation-process-setup-function))
    (compilation-set-window-height outwin)
    ;; Start the compilation.
    (if (fboundp 'start-process)
        (let ((proc
           (if (eq mode t)
               ;; comint uses `start-file-process'.
               (get-buffer-process
            (with-no-warnings
              (comint-exec
               outbuf (downcase mode-name)
               (if (file-remote-p default-directory)
                   "/bin/sh"
                 shell-file-name)
               nil `("-c" ,command))))
             (start-file-process-shell-command (downcase mode-name)
                               outbuf command))))
              ;; Make the buffer's mode line show process state.
              (setq mode-line-process
                    '(:propertize ":%s" face compilation-mode-line-run))

              ;; Set the process as killable without query by default.
              ;; This allows us to start a new compilation without
              ;; getting prompted.
              (when compilation-always-kill
                (set-process-query-on-exit-flag proc nil))

              (set-process-sentinel proc 'compilation-sentinel)
              (unless (eq mode t)
                ;; Keep the comint filter, since it's needed for proper
        ;; handling of the prompts.
        (set-process-filter proc 'compilation-filter))
          ;; Use (point-max) here so that output comes in
          ;; after the initial text,
          ;; regardless of where the user sees point.
          (set-marker (process-mark proc) (point-max) outbuf)
          (when compilation-disable-input
        (condition-case nil
            (process-send-eof proc)
          ;; The process may have exited already.
          (error nil)))
          (run-hook-with-args 'compilation-start-hook proc)
              (setq compilation-in-progress
            (cons proc compilation-in-progress)))
      ;; No asynchronous processes available.
      (message "Executing `%s'..." command)
      ;; Fake mode line display as if `start-process' were run.
      (setq mode-line-process
        '(:propertize ":run" face compilation-mode-line-run))
      (force-mode-line-update)
      (sit-for 0)           ; Force redisplay
      (save-excursion
        ;; Insert the output at the end, after the initial text,
        ;; regardless of where the user sees point.
        (goto-char (point-max))
        (let* ((inhibit-read-only t) ; call-process needs to modify outbuf
           (compilation-filter-start (point))
           (status (call-process shell-file-name nil outbuf nil "-c"
                     command)))
          (run-hooks 'compilation-filter-hook)
          (cond ((numberp status)
             (compilation-handle-exit
              'exit status
              (if (zerop status)
              "finished\n"
            (format "exited abnormally with code %d\n" status))))
            ((stringp status)
             (compilation-handle-exit 'signal status
                          (concat status "\n")))
            (t
             (compilation-handle-exit 'bizarre status status)))))
      (set-buffer-modified-p nil)
      (message "Executing `%s'...done" command)))
      ;; Now finally cd to where the shell started make/grep/...
      (setq default-directory thisdir)
      ;; The following form selected outwin ever since revision 1.183,
      ;; so possibly messing up point in some other window (bug#1073).
      ;; Moved into the scope of with-current-buffer, though still with
      ;; complete disregard for the case when compilation-scroll-output
      ;; equals 'first-error (martin 2008-10-04).
      (when compilation-scroll-output
    (goto-char (point-max))))

    ;; Make it so the next C-x ` will use this buffer.
    (setq next-error-last-buffer outbuf)))

【讨论】:

  • 这些功能很完善,会很有用。谢谢!
  • 我的一个编辑有一个 frame-bufs 部分——我刚刚用更新的答案删除了它。我在启动 Emacs 时设置了初始框架的框架名称,并且我每天使用 5 个框架 - 如果您有兴趣,请参阅答案开头的链接。
  • 这对于拥有多台显示器的人来说是完美的,对吧?你有没有考虑过用它做一个包?
  • @teaforthecat -- 可以创建一个设置,将此功能与display-monitor-attributes-list 之类的东西联系起来,以针对特定监视器上的帧。上面答案中的链接包含一个更强大的解决方案,用于创建和定位具有与特定正则表达式匹配的缓冲区的不同帧。我不时修改和改进那个相关的线程,但没有动力去创建一个包——我猜是因为每个人都有自己的品味,而且 Emacs 通常是由每个用户根据他/她的个人喜好定制的。跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多