2016-09-29 37 views
0

我已經在emacs中定製了gdb窗口。在調試過程中,新的源代碼在不同的窗口中打開。我只想在一個窗口中看到源代碼。我GDB定製是:[emacs] [gdb自定義]如何在一個窗口中顯示源緩衝區?

;  _____________________________________________________________________________________ 
; |           |           | 
; |    BREAKPOINTS     |           | 
; |__________________________________________|           | 
; |           |           | 
; |     STACK     |           | 
; |__________________________________________|           | 
; |           |           | 
; |           |           | 
; |     LOCALS     |           | 
; |           |    SOURCE CODE    | 
; |__________________________________________|           | 
; |           |           | 
; |           |           | 
; |           |           | 
; |           |           | 
; |           |           | 
; |           |           | 
; |     GDB      |           | 
; |           |__________________________________________| 
; |           |           | 
; |           |           | 
; |           |     I/O     | 
; |           |           | 
; |__________________________________________|__________________________________________| 

(require 'gud) 

; invoke 
(global-set-key [f8] 'gdb) 

; GDB layout 
(defadvice gdb-setup-windows (after activate) 
    (gdb-setup-my-windows) 
) 

(defun gdb-setup-my-windows() 
    (set-window-dedicated-p (selected-window) nil) 
    (switch-to-buffer gud-comint-buffer) 
    (delete-other-windows) 
    (let 
    ((win0 (selected-window))    ; breakpoints 
    (win1 (split-window-horizontally 
     (floor (* 0.5 (window-width))))) ; source + i/o 
    (win2 (split-window-vertically 
     (floor (* 0.5 (window-body-height))))) ; gdb 
    (win3 (split-window-vertically 
     (floor (* 0.5 (window-body-height))))) ; locals 
    (win4 (split-window-vertically 
     (floor (* 0.6 (window-body-height))))) ; stack 
    ) 
    (select-window win1) 
    ; configurating right window 
    (let 
    ((winSrc (selected-window)) ; source 
    (winIO (split-window-vertically (floor (* 0.9 (window-body-height))))) ; I/O 
    ) 
     (set-window-buffer winIO (gdb-get-buffer-create 'gdb-inferior-io)) 
     (set-window-buffer 
    winSrc 
    (if gud-last-last-frame 
    (gud-find-file (car gud-last-last-frame)) 
     (if gdb-main-file 
     (gud-find-file gdb-main-file) 
    (list-buffers-noselect)))) 
     (setq gdb-source-window winSrc) 
     (set-window-dedicated-p winIO t) 
    ) 

    (set-window-buffer win0 (gdb-get-buffer-create 'gdb-breakpoints-buffer)) 
    (set-window-buffer win3 (gdb-get-buffer-create 'gdb-locals-buffer)) 
    (set-window-buffer win4 (gdb-get-buffer-create 'gdb-stack-buffer)) 
    (select-window win2) 
) 
) 

; GDB variables 
(setq gdb-many-windows t) 
(setq gdb-show-main t) 
(setq gdb-show-changed-values t) 
(setq gdb-use-colon-colon-notation t) 
(setq gdb-use-separate-io-buffer nil) 
(setq gdb-delete-out-of-scope t) 
(setq gdb-speedbar-auto-raise t) 

主屏幕是: gdb screen after start

但是,當我開始調試,然後下一個源文件在另一個窗口打開。見例如波紋管: New source code in gdb window

應用再現的例子是:

main.cpp中

#include "classB.h" 

int main() 
{ 
    B *b = 0; 
    b = new B(); 
    return 0; 
} 

classA.h

#ifndef CLASS_A_H 
#define CLASS_A_H 

class A 
{ 
public: 
    A(); 
}; 

#endif 

classA.cpp

#include "classA.h" 
#include <iostream> 

A::A() 
{ 
    std::cout << "Constructor A" << std::endl; 
} 

classB.h

#ifndef CLASS_B_H 
#define CLASS_B_H 

#include "classA.h" 

class B : public A 
{ 
public: 
    B(); 
}; 

#endif 

classB.cpp

#include "classB.h" 
#include <iostream> 

B::B() : A() 
{ 
    std::cout << "Constructor B" << std::endl; 
} 

生成文件

SOURCES=main.cpp classA.cpp classB.cpp 
TARGET=test 
CXX_FLAGS=-g 

.PHONY: all 

all: $(TARGET) 

$(TARGET): $(SOURCES) 
    $(CXX) $(CXX_FLAGS) $^ -o [email protected] 

.PHONY: clean 

clean: 
    rm -vf $(TARGET) 

步驟重現:

  1. 運行的emacs

  2. 的Mx的gdb

  3. GDB -i = MI測試

  4. 在GDB命令窗口運行:啓動

  5. 運行:下一步

  6. 運行:步

我的環境是:Ubuntu14.04,GDB - 7.7.1,emacs的 - 25.1.1。

我試着用set-window-dedicated-p。但這不是我的問題的解決方案。 我是新的emacs,請幫助我,我的配置有什麼問題?

+0

對不起話題,但你用什麼主題'linum'和緩衝區分隔符? –

+0

我建議從標題中刪除[標籤],因爲SO已經將標籤作爲問題的一部分。 –

+0

@ChakravarthyRaghunandan,我正在使用idea-darkula-theme。 –

回答

0

display-buffer-alist可讓您控制在哪個窗口中顯示新緩衝區。在開始通過註冊函數指針裏面

(add-to-list 'display-buffer-alist 
     (cons 'cdb-source-code-buffer-p 
      (cons 'display-source-code-buffer nil))) 

然後實現選擇窗口的功能,例如

(defun cdb-source-code-buffer-p (bufName action) 
    "Return whether BUFNAME is a source code buffer." 
    (let ((buf (get-buffer bufName))) 
    (and buf 
    (with-current-buffer buf 
     (derived-mode-p buf 'c++-mode 'c-mode 'csharp-mode 'nxml-mode))))) 

(defun display-source-code-buffer (sourceBuf alist) 
    "Find a window with source code and set sourceBuf inside it." 
    (let* ((curbuf (current-buffer)) 
    (wincurbuf (get-buffer-window curbuf)) 
    (win (if (and wincurbuf 
       (derived-mode-p sourceBuf 'c++-mode 'c-mode 'nxml-mode) 
       (derived-mode-p (current-buffer) 'c++-mode 'c-mode 'nxml-mode)) 
      wincurbuf 
     (get-window-with-predicate 
     (lambda (window) 
      (let ((bufName (buffer-name (window-buffer window)))) 
      (or (cdb-source-code-buffer-p bufName nil) 
      (assoc bufName display-buffer-alist) 
      ))))))) ;; derived-mode-p doesn't work inside this, don't know why... 
    (set-window-buffer win sourceBuf) 
    win)) 

cdb-source-code-buffer-p選擇源代碼緩衝器,爲此display-source-code-buffer被調用時,它返回窗口你要。

+0

對不起,我是Emacs的新手,我不明白我應該如何在gdb配置文件中使用它。你能解釋一下嗎? 我將函數添加到我的配置文件,並試圖調用display-source-code-buffer函數,但它不起作用。 –

+0

@EgorCh。請閱讀「display-buffer-alist」的文檔,輸入「M-x describe-variable」,然後輸入「display-buffer-alist」。在那裏你會發現你不會自己調用display-source-code-buffer,當它需要顯示一個源代碼緩衝區時,Emacs會爲你調用它。有關Emacs lisp的一般介紹,請閱讀[this](https://www.gnu.org/software/emacs/manual/elisp.html) –

相關問題