How do I programmaticaly force my Emacs X Window to get current user input focus?
I want to use this in the following Bash script
# Debug in Emacs using GDB
function emacs-gdb()
{
if [ -z $1 ]; then
echo -e "Usage: $FUNCNAME EXE EXE_ARGS...";
else
if (($# >= 2)); then
local ARGS="--args ${#:1:100}";
else
local ARGS="$1";
fi
emacsclient --eval "(gdb \"gdb -i=mi $ARGS\")"
fi
}
to make the Emacs window automatically get window focus.
You can do this by adding the call to raise-frame in your eval code.
For example:
emacsclient --eval "(progn (raise-frame) (gdb \"gdb -i=mi $ARGS\"))"
I've had this copied from somewhere related to org-protocol.
It should help you as well:
(defadvice raise-frame (after make-it-work (&optional frame) activate)
"Work around some bug? in raise-frame/Emacs/GTK/Metacity/something.
Katsumi Yamaoka posted this in
http://article.gmane.org/gmane.emacs.devel:39702"
(call-process
"wmctrl" nil nil nil "-s" "1")
(call-process
"wmctrl" nil nil nil "-i" "-R"
(frame-parameter (or frame (selected-frame)) 'outer-window-id)))
This is in addition to #tungd's advice of calling raise-frame of course.
Related
How do I execute a shell command (eg, git gui) from a specific directory? I'd prefer a cross-platform approach that doesn't depend shell operators like && or ; because I need this to run on Windows and unix. (For example, calling cd /path/to/dir && git gui won't work on Windows because && is not valid.)
I tried:
(async-shell-command (concat "cd \""
(replace-regexp-in-string
"/" "\\\\" (file-name-directory (buffer-file-name))) "\" ; git gui"))
That fails because for whatever reason the shell thinks ; git gui is part of the path, and it reports: The system cannot find the path specified.
So, I'd rather not deal with shell quirks and I'm hoping there's an elisp function that sets the directory for shell-command or async-shell-command. I tried:
(shell-process-pushd (file-name-directory (buffer-file-name)))
(shell-command "git gui")
(shell-process-popd nil)
That had no effect: git gui always opens in my home directory. (Also, shell-process-pushd/popd are not documented.)
This also doesn't work:
(start-process "gitgui" nil "git" "gui")
Neither does this:
(let '(bufdir (file-name-directory (buffer-file-name)))
(with-temp-buffer
(print bufdir)
(cd bufdir)
(shell-command "git gui")))
Are you sure you're using the trailing slash to indicate directory name in emacs-lisp?
(let ((default-directory "~/.emacs.d"))
(shell-command-to-string "echo $PWD"))
;;; ⇒ "/Users/me"
(let ((default-directory "~/.emacs.d/"))
(shell-command-to-string "echo $PWD"))
;;; ⇒ "/Users/me/.emacs.d"
(shell-command) does use the the buffer's default-directory, which means there is no reason to cd to the buffer's directory.
The problem in my case was that I was mistakenly running git gui on a buffer that didn't have an associated .git/ directory.
Try this:
(let ((default-directory "~/.emacs.d/")) (shell-command "ls"))
For example, I have this setup in my .emacs
(defun gtags-create-or-update ()
"Create or update the gnu global tag file."
(interactive)
(if (y-or-n-p-with-timeout
(format "Run gtags to create/update tag file for code at %s (default no)? "
default-directory)
5 nil) ; default - no run
(unless (= 0 (call-process "global" nil nil nil " -p")) ; tagfile doesn't exist?
(let ((olddir default-directory)
(topdir (read-directory-name
"gtags: top of source tree: " default-directory)))
(cd topdir)
(shell-command "gtags -v")
;; (shell-command "gtags && echo 'created tagfile'")
(cd olddir)) ; restore
;; tagfile already exists; update it
(shell-command "global -uv"))))
;; (shell-command "global -u && echo 'updated tagfile'")))
(add-hook 'c-mode-common-hook
(lambda ()
(require 'gtags)
(gtags-mode t)
(gtags-create-or-update)))
When I run gtags-create-or-update explicitly, emacs prompt in the minibuffer to ask me whether to create/update tag files. However, when I open a c/cpp file, it always pops up a GUI window ask me yes or no, which is super annoying. I am wondering why this is happening.
What #phils says in the comment. To avoid dialog boxes in GUI Emacs, you can set the use-dialog-box to nil Put the line (setq use-dialog-box nil) in your initialization file.
How can I tell emacs not to pop up the *Shell Command Output* buffer when calling a shell command like this?
(shell-command MY_COMMAND)
Currently emacs splits the current window into two, showing the (mostly irrelevant) output buffer. To me it would be completely sufficient if I could look it up later if I feel like it.
Maybe using shell-command was the root of the problem. I think I found a solution with call-process which works, although there may be a more elegant way:
(call-process-shell-command
"cat ~/.emacs.d/init.el"
nil "*Shell Command Output*" t
)
shell-command takes an optional argument OUTPUT-BUFFER where you can specify the buffer to output to. If it is t (actually not a buffer-name and not nil) it will be output in the current buffer. So we wrap this into a with-temp-buffer and will never have to bother with it:
(with-temp-buffer
(shell-command "cat ~/.emacs.d/init.el" t))
In my experience, if the shell command itself produces no output, then the emacs *Shell Command Output* buffer won't pop open.
Therefore, to avoid the output buffer, silence the output of the command.
One easy way is:
add " > /dev/null 2>&1" to the end of any shell command.
(Caveat: I'm unsure if /dev/null exists on 100% of platforms where one can run emacs, but on every Linux distro it should be fine.)
If the call to elisp function shell-command is in an elisp script, then you could change this:
(shell-command cmd)
to this:
(shell-command (concat cmd " > /dev/null 2>&1"))
If you occasionally do want to monitor the output, then you could create one wrapper function that suppresses the output via /dev/null, and one wrapper function with no suppression, and toggle between them as you wish.
The above advice was tested on: GNU Emacs 24.5.1 (x86_64-pc-linux-gnu, GTK+ Version 3.18.9) of 2017-09-20 on lcy01-07, modified by Debian
This utility function might help. It returns the actual value of the shell command
(defun shell-command-as-string (cmd)
(with-temp-buffer
(shell-command-on-region (point-min) (point-max)
cmd t)
(buffer-string)))
What's even better, is to use
(shell-command (concat cmd " 1>&2") t t)
This way, the output is saved in the error buffer, should you want to look at it. But it does not pop up automatically.
I am doing Rails development and find that I need to spawn a shell, rename the buffer (e.g. webrick), then kick off the command (rails s) and then do the whole thing over again if I want a rails console or rails dbconsole, rspec, spork, etc. every time I start up emacs.
I am hoping for something like this:
(defun spawn-shell ()
"Invoke shell test"
(with-temp-buffer
(shell (current-buffer))
(process-send-string nil "echo 'test1'")
(process-send-string nil "echo 'test2'")))
I don't want the shell to go away when it exits because the output in the shell buffer is important and some times I need to kill it and restart it but I don't want to lose that history.
Essentially, I want to take the manual process and make it invokable.
Any help is much appreciated
Tom
Perhaps this version of spawn-shell will do what you want:
(defun spawn-shell (name)
"Invoke shell test"
(interactive "MName of shell buffer to create: ")
(pop-to-buffer (get-buffer-create (generate-new-buffer-name name)))
(shell (current-buffer))
(process-send-string nil "echo 'test1'\n")
(process-send-string nil "echo 'test2'\n"))
It prompts for a name to use when you run it interactively (M-x spawn-shell). It creates a new buffer based on the input name using generate-new-buffer-name, and you were missing the newlines on the end of the strings you were sending to the process.
If your only problem is that the shell buffer disappears after the commands have been executed, why not use get-buffer-create instead of with-temp-buffer?
If I am trying to run a shell-command in an Emacs Lisp function in which I call rsync (or scp) multiple times, which shell-command variant should I use? I am currently using shell-command, which locks up Emacs until the process is done, and the output that should be visible with the --verbose to rsync is not printed; I can use shell-command with an & at the end of the command string to make it asynchronous, which does print the progress — but while it doesn't "lock up" Emacs entirely, the minibuffer repeatedly asks if I want to kill the process which is crippling in the meantime; and start-process-shell-command, which appears to halt the function only after the first file/directory is transferred; neglecting the rest when there are multiple rsync calls made through my function. None of these seem ideal, any hints?
I have had the most success using start-process myself.
(start-process "process-name"
(get-buffer-create "*rsync-buffer*")
"/path/to/rsync"
arg1
...
argn)
This will send all the output to a single buffer.
One solution might be to run the command in an actual shell buffer. Then you get to choose which one of those to run:
M-x shell
M-x eshell
M-x term
If you like that idea, you can code it up like this:
(defun my-rsync-routine ()
"run some rsync processes"
(with-temp-buffer
(shell (current-buffer))
(process-send-string nil "rsync ...")
(process-send-string nil "rsync ...")
(process-send-string nil "rsync ...")))
Read more on 'process-send-string for its usage. You might also want to have some error checking on the output from the processes.