Splitting window in Emacs Lisp function

ε祈祈猫儿з 提交于 2019-12-08 05:26:35

问题


I'd like to be able to run a shell command on the current file that I'm editing and have the output be shown in the Shell Command Output window. I've been able to define the function which is shown below.

(defun cpp-check ()
  "Run cpp-check on current file the buffer is visiting."
  (shell-command
   (concat "/home/sburke/downloads/cppcheck-1.31/cppcheck "
       (buffer-file-name))))

The only problem is that the output window isn't brought to the foreground in any way. What I'd like to happen is for the window to be split and the output window shown there. Also, am I on the right track here defining the function to be put in my .emacs file or is there a better way?

Any help would be appreciated. Thanks.


回答1:


Take a look at the documentation for 'shell-command, this worked well for me:

(defun cpp-check ()
  "Run cpp-check on current file the buffer is visiting."
  (shell-command
   (concat "/home/sburke/downloads/cppcheck-1.31/cppcheck "
       (buffer-file-name))
   "cpp-check"))

It creates a new buffer named "cpp-check" and puts the results there. The current frame is split in to, and the "cpp-check" buffer is put in the other window.




回答2:


See the function `pop-to-buffer'. I think.

You should be able to give it a buffer name to pop to -- just give in the Shell Command Output buffer.




回答3:


This is what I came up with. Thanks for the responses. I defined a function that will go ahead and run cpp-check. I wanted it bound to a key in c-mode so I add it as a hook. I ran into the difference between normal functions and ones that can be bound to keymaps so I had to make the function interactive. This article helped explain that. So now when the shortcut is pressed the results come up in another window, but the cursor remains in the original buffer, which is what I want. The only problem is that the output is shown in the minibuffer as well which isn't quite what I want. Any thoughts on fixing that little detail?

(defun cpp-check ()
  (interactive)
  "Run cpp-check on current file the buffer is visiting."
  (shell-command
   (concat "/home/sburke/downloads/cppcheck-1.31/cppcheck "
       (buffer-file-name)))
  (display-buffer "*Shell Command Output*"))

(add-hook 'c-mode-common-hook
           (lambda ()
         (define-key c-mode-base-map
               "\C-x\p" 'cpp-check)))



回答4:


splitting the window is (split-window-vertically) It has an optional arg of the size of the (top if positive, bottom if negative) part of the window.

Then, and you need to do is bring the shell results buffer to the front with switch-to-buffer or switch-to-buffer-other-window.

Remember that when you spit the window (frame) in emacs, you end up with two "windows" because of a naming confusing back in the day that it's too late to deal with now...



来源:https://stackoverflow.com/questions/821853/splitting-window-in-emacs-lisp-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!