I never really use the REPL and I find annoying that it pops up every time I connect to a swank instance. How can I prevent SLIME to switch to the repl buffer?
I tried to find where it does that in slime's code, but it's kind of huge when you're not sure what to look for.
Thanks
Here's how I've done it in lispy:
(defun lispy--eval-lisp (str)
"Eval STR as Common Lisp code."
(require 'slime-repl)
(unless (slime-current-connection)
(let ((wnd (current-window-configuration)))
(slime)
(while (not (and (slime-current-connection)
(get-buffer-window (slime-output-buffer))))
(sit-for 0.2))
(set-window-configuration wnd)))
(let (deactivate-mark)
(cadr (slime-eval `(swank:eval-and-grab-output ,str)))))
Here was my solution, thanks to #abo-abo's answer.
(defun my-slime-connect () (interactive)
(let ((wnd (current-window-configuration)))
(call-interactively 'slime-connect)
(sit-for 0.2) ;; Not sure if necessary, haven't tested without it.
(set-window-configuration wnd)))
Related
And I dont want this behavior. Actually, not everytime is focus switched. For example, buffers like *Help* or *Message* does not have this behavior, this unwanted feature is active only when closing regular file buffers.
How to disable this feature completelly ?
My solution for this problem is simply write my own handler for buffer kill. Hope it will help someone in future.
(global-set-key (kbd "C-w")
(lambda ()
(interactive)
(if (string= (substring (buffer-name) 0 2) " *")
(progn
(message "You can not kill protected BUFFER"))
(progn
(if (string= (substring (buffer-name) 0 1) "*")
(progn
(call-interactively 'kill-buffer))
(progn
(call-interactively 'kill-buffer)
(run-at-time "0.5 sec" nil
(lambda ()
(call-interactively 'windmove-right)))))))))
You need to have installed ace-window package for windmove-right, that feature with protected buffers is not relevant to this answer so you can freely remove it.
I'd like to make a simple change to Emacs so that the next-buffer and previous-buffer commands (which I have bound to C-x <RIGHT> and C-x <LEFT> will skip over the *Messages* buffer.
I'm using Emacs 24 and the Emacs Starter Kit.
I've read the following related questions and answers, but they are not what I want:
Buffer cycling in Emacs: avoiding scratch and Messages buffer
Emacs disable *Messages* buffer
Emacs Lisp Buffer out of focus function?
Here are some of the reasons why they don't work:
I'd like to keep it as simple as possible. Fewer configuration changes are better.
I don't want to kill or prevent *Messages* altogether.
(add-to-list 'ido-ignore-buffers "^\*Messages\*" helps with my C-x b (ido-switch-buffer) but does not change how next-buffer and previous-buffer behave.
This way you can avoid the infinite loop:
(defun next-code-buffer ()
(interactive)
(let (( bread-crumb (buffer-name) ))
(next-buffer)
(while
(and
(string-match-p "^\*" (buffer-name))
(not ( equal bread-crumb (buffer-name) )) )
(next-buffer))))
(global-set-key [remap next-buffer] 'next-code-buffer)
This code loops over non-starred buffers ("^\*"). For your case (only avoid *Messages*) it would be:
(defun next-code-buffer ()
(interactive)
(let (( bread-crumb (buffer-name) ))
(next-buffer)
(while
(and
(equal "*Messages*" (buffer-name))
(not ( equal bread-crumb (buffer-name) )) )
(next-buffer))))
(global-set-key [remap next-buffer] 'next-code-buffer)
You can write previous-code-buffer just replacing every next-buffer with previous-buffer.
The simplest I can think of is defining an advice for both functions. Here it is for next-buffer. Similarly would be for previous-buffer. You can also define a configuration variable to enable/disable the behavior (or activating/deactivating the advice):
(defadvice next-buffer (after avoid-messages-buffer-in-next-buffer)
"Advice around `next-buffer' to avoid going into the *Messages* buffer."
(when (string= "*Messages*" (buffer-name))
(next-buffer)))
;; activate the advice
(ad-activate 'next-buffer)
Maybe you can compare buffers in some other way instead of its string name, but that will work. The code for previous buffer is almost the same. I don't know either if there is a way of calling the original function without triggering the advice once inside the advice itself, but again, the code will work even if the name of the buffer is tested afterwards (will fail if you just have one buffer, and it is the messages buffer; some code can check if there is just one buffer and don't call next-buffer again).
If you want to use a standalone function that does the same thing:
(defun my-next-buffer ()
"next-buffer, only skip *Messages*"
(interactive)
(next-buffer)
(when (string= "*Messages*" (buffer-name))
(next-buffer)))
(global-set-key [remap next-buffer] 'my-next-buffer)
(global-set-key [remap previous-buffer] 'my-next-buffer)
This is what I'm using, based on Diego's answer:
(setq skippable-buffers '("*Messages*" "*scratch*" "*Help*"))
(defun my-next-buffer ()
"next-buffer that skips certain buffers"
(interactive)
(next-buffer)
(while (member (buffer-name) skippable-buffers)
(next-buffer)))
(defun my-previous-buffer ()
"previous-buffer that skips certain buffers"
(interactive)
(previous-buffer)
(while (member (buffer-name) skippable-buffers)
(previous-buffer)))
(global-set-key [remap next-buffer] 'my-next-buffer)
(global-set-key [remap previous-buffer] 'my-previous-buffer)
It is not great yet, because it will hang if there are no buffers other than the skippable-buffers I list. I use C-g to break out of the loop when it happens as a hackaround.
As RubenCaro's answer points out, the other answers can enter infinite loops. I thought David James' approach of a skippable buffers list was a bit nicer, though, so here's a variant of that.
(setq my-skippable-buffers '("*Messages*" "*scratch*" "*Help*"))
(defun my-change-buffer (change-buffer)
"Call CHANGE-BUFFER until current buffer is not in `my-skippable-buffers'."
(let ((initial (current-buffer)))
(funcall change-buffer)
(let ((first-change (current-buffer)))
(catch 'loop
(while (member (buffer-name) my-skippable-buffers)
(funcall change-buffer)
(when (eq (current-buffer) first-change)
(switch-to-buffer initial)
(throw 'loop t)))))))
(defun my-next-buffer ()
"`next-buffer' that skips `my-skippable-buffers'."
(interactive)
(my-change-buffer 'next-buffer))
(defun my-previous-buffer ()
"`previous-buffer' that skips `my-skippable-buffers'."
(interactive)
(my-change-buffer 'previous-buffer))
(global-set-key [remap next-buffer] 'my-next-buffer)
(global-set-key [remap previous-buffer] 'my-previous-buffer)
I found a snippet to close all dired buffers, which I want to use in sunrise commander:
(defun er/kill-all-dired-buffers()
"Kill all dired buffers."
(interactive)
(save-excursion
(let((count 0))
(dolist(buffer (buffer-list))
(set-buffer buffer)
(when (equal major-mode 'sr-mode)
(or (equal major-mode 'dired-mode))
(setq count (1+ count))
(kill-buffer buffer)))
(message "Killed %i dired buffer(s)." count ))))
(setq sr-quit-hook 'er/kill-all-dired-buffers)
Issue being, I can't make it work both for sr-mode and dired-mode together.
How do I check "if major mode is sr-mode OR dired-mode"?
EDIT:
Just a syntax error.
Should be
(when (or (equal major-mode 'dired-mode) (equal major-mode 'sr-mode))
Have to admit it's not too intuitive.
The canonical way would be (when (derived-mode-p 'sr-mode 'dired-mode) ...).
I tried some things and found this to work on my emacs-ielm - perhaps it might help also:
(if (member major-mode '(fsharp-mode c-mode java-mode inferior-emacs-lisp-mode))
(message "yeah right"))
Maybe the correct check function is:
(derived-mode-p &rest MODES)
See 'subr.el'.
I'd like to make a simple change to Emacs so that the next-buffer and previous-buffer commands (which I have bound to C-x <RIGHT> and C-x <LEFT> will skip over the *Messages* buffer.
I'm using Emacs 24 and the Emacs Starter Kit.
I've read the following related questions and answers, but they are not what I want:
Buffer cycling in Emacs: avoiding scratch and Messages buffer
Emacs disable *Messages* buffer
Emacs Lisp Buffer out of focus function?
Here are some of the reasons why they don't work:
I'd like to keep it as simple as possible. Fewer configuration changes are better.
I don't want to kill or prevent *Messages* altogether.
(add-to-list 'ido-ignore-buffers "^\*Messages\*" helps with my C-x b (ido-switch-buffer) but does not change how next-buffer and previous-buffer behave.
This way you can avoid the infinite loop:
(defun next-code-buffer ()
(interactive)
(let (( bread-crumb (buffer-name) ))
(next-buffer)
(while
(and
(string-match-p "^\*" (buffer-name))
(not ( equal bread-crumb (buffer-name) )) )
(next-buffer))))
(global-set-key [remap next-buffer] 'next-code-buffer)
This code loops over non-starred buffers ("^\*"). For your case (only avoid *Messages*) it would be:
(defun next-code-buffer ()
(interactive)
(let (( bread-crumb (buffer-name) ))
(next-buffer)
(while
(and
(equal "*Messages*" (buffer-name))
(not ( equal bread-crumb (buffer-name) )) )
(next-buffer))))
(global-set-key [remap next-buffer] 'next-code-buffer)
You can write previous-code-buffer just replacing every next-buffer with previous-buffer.
The simplest I can think of is defining an advice for both functions. Here it is for next-buffer. Similarly would be for previous-buffer. You can also define a configuration variable to enable/disable the behavior (or activating/deactivating the advice):
(defadvice next-buffer (after avoid-messages-buffer-in-next-buffer)
"Advice around `next-buffer' to avoid going into the *Messages* buffer."
(when (string= "*Messages*" (buffer-name))
(next-buffer)))
;; activate the advice
(ad-activate 'next-buffer)
Maybe you can compare buffers in some other way instead of its string name, but that will work. The code for previous buffer is almost the same. I don't know either if there is a way of calling the original function without triggering the advice once inside the advice itself, but again, the code will work even if the name of the buffer is tested afterwards (will fail if you just have one buffer, and it is the messages buffer; some code can check if there is just one buffer and don't call next-buffer again).
If you want to use a standalone function that does the same thing:
(defun my-next-buffer ()
"next-buffer, only skip *Messages*"
(interactive)
(next-buffer)
(when (string= "*Messages*" (buffer-name))
(next-buffer)))
(global-set-key [remap next-buffer] 'my-next-buffer)
(global-set-key [remap previous-buffer] 'my-next-buffer)
This is what I'm using, based on Diego's answer:
(setq skippable-buffers '("*Messages*" "*scratch*" "*Help*"))
(defun my-next-buffer ()
"next-buffer that skips certain buffers"
(interactive)
(next-buffer)
(while (member (buffer-name) skippable-buffers)
(next-buffer)))
(defun my-previous-buffer ()
"previous-buffer that skips certain buffers"
(interactive)
(previous-buffer)
(while (member (buffer-name) skippable-buffers)
(previous-buffer)))
(global-set-key [remap next-buffer] 'my-next-buffer)
(global-set-key [remap previous-buffer] 'my-previous-buffer)
It is not great yet, because it will hang if there are no buffers other than the skippable-buffers I list. I use C-g to break out of the loop when it happens as a hackaround.
As RubenCaro's answer points out, the other answers can enter infinite loops. I thought David James' approach of a skippable buffers list was a bit nicer, though, so here's a variant of that.
(setq my-skippable-buffers '("*Messages*" "*scratch*" "*Help*"))
(defun my-change-buffer (change-buffer)
"Call CHANGE-BUFFER until current buffer is not in `my-skippable-buffers'."
(let ((initial (current-buffer)))
(funcall change-buffer)
(let ((first-change (current-buffer)))
(catch 'loop
(while (member (buffer-name) my-skippable-buffers)
(funcall change-buffer)
(when (eq (current-buffer) first-change)
(switch-to-buffer initial)
(throw 'loop t)))))))
(defun my-next-buffer ()
"`next-buffer' that skips `my-skippable-buffers'."
(interactive)
(my-change-buffer 'next-buffer))
(defun my-previous-buffer ()
"`previous-buffer' that skips `my-skippable-buffers'."
(interactive)
(my-change-buffer 'previous-buffer))
(global-set-key [remap next-buffer] 'my-next-buffer)
(global-set-key [remap previous-buffer] 'my-previous-buffer)
C-x s uses diff to show changes. How can I use ediff instead?
I can see a couple of approaches to doing this. The first is to replace the save-some-buffers-action-alist variable with modified code, which is more straightforward. The second is to advise save-some-buffers and redefine the functions called by those actions, but that's a bit trickier.
I tried it both ways, and I think this is the best option:
;; Use ediff instead of diff in `save-some-buffers'
(eval-after-load "files"
'(progn
(setcdr (assq ?d save-some-buffers-action-alist)
`(,(lambda (buf)
(if (null (buffer-file-name buf))
(message "Not applicable: no file")
(add-hook 'ediff-after-quit-hook-internal
'my-save-some-buffers-with-ediff-quit t)
(save-excursion
(set-buffer buf)
(let ((enable-recursive-minibuffers t))
(ediff-current-file)
(recursive-edit))))
;; Return nil to ask about BUF again.
nil)
,(purecopy "view changes in this buffer")))
(defun my-save-some-buffers-with-ediff-quit ()
"Remove ourselves from the ediff quit hook, and
return to the save-some-buffers minibuffer prompt."
(remove-hook 'ediff-after-quit-hook-internal
'my-save-some-buffers-with-ediff-quit)
(exit-recursive-edit))))
My attempt at using advice is flawed (it breaks the C-r behaviour which also calls view-buffer, which caused me to reconsider using advice for this purpose), but FWIW:
(defadvice save-some-buffers (around my-save-some-buffers-with-ediff)
"Use ediff instead of diff."
(require 'cl)
(flet ((view-buffer (&rest) nil)
(diff-buffer-with-file
(buf)
(add-hook 'ediff-after-quit-hook-internal
'my-save-some-buffers-with-ediff-quit t)
(save-excursion
(set-buffer buf)
(ediff-current-file))))
(let ((enable-recursive-minibuffers t))
ad-do-it)))
(ad-activate 'save-some-buffers)
(defun my-save-some-buffers-with-ediff-quit ()
"Remove ourselves from the ediff quit hook, and
return to the save-some-buffers minibuffer prompt."
(remove-hook 'ediff-after-quit-hook-internal
'my-save-some-buffers-with-ediff-quit)
(exit-recursive-edit))
The variable diff-command is customizable, says the documentation. However, remember that it points to an external program, and not an elisp function. ediff is an elisp function that is in ediff.el. You might have to edit diff.el to (require 'ediff) and then tweak here and there in diff.el to see that you break nothing else.