Viper mode in all modes - emacs

I'm moving from vi to emacs and, using viper and vimpulse, it gets pretty annoying when I C-w C-w to a window and cannot get back to the original one with the same command because the other buffer, which could be a help buffer, is not in the Viper mode. How can I fix this?
I tried to add other modes to the viper configuration and make them start with the viper mode on by default using hooks:
(add-to-list viper-vi-state-mode-list 'help-mode)
(add-hook 'help-mode (lambda () (setq viper-mode t)))
But none of these commands did work. In fact, the first one yielded the following error:
Warning (initialization): An error occurred while loading `/home/konrad/.emacs.d/init.el':
Wrong type argument: symbolp, (fundamental-mode ...
Besides the C-w C-w, I also miss not being able to navigate using hjkl. Isn't there a way to reuse the keybindings set by viper, instead of rebinding them again for every mode?

The error is because you neglected to quote the variable viper-vi-state-mode-list like so:
(add-to-list 'viper-vi-state-mode-list 'help-mode)
See this question as to why you need to quote 'viper-vi-state-mode-list.
But, that didn't solve the problem for me, this is how I got C-w C-w to work the way you want:
(define-key help-mode-map (kbd "C-w C-w") 'vimpulse-cycle-windows)

The syntax for adding the hook to help-mode would look like this:
(add-hook 'help-mode-hook (lambda () (viper-mode t)))
Note the hook variable is named with "-hook", and setq doesn't work here because viper-mode is a command, not a variable.
You might be able to get all modes to activate viper with:
(add-hook 'fundamental-mode-hook (lambda () (viper-mode t)))
since all modes inherit from fundamental-mode.

Related

Can't find a place to bind paredit's convolute-sexp

Strangely, while the various paredit cheatsheets show M-? bound to paredit-convolute-sexp, the paredit.el file doesn't have any binding for that function, though it does define the function. I edited paredit.el to add it here:
....
("M-q" paredit-reindent-defun)
("M-\?" paredit-convolute-sexp)
But after restarting emacs, it is still not bound to M-?. I tried using just "M-?" in the binding with no backslash, but made no difference.
I also tried to do this:
(defun my-clojure-hook ()
(auto-complete-mode 1)
(define-key clojure-mode-map
(kbd "M-\?" 'paredit-convolute-sexp)))
(add-hook 'clojure-mode-hook 'my-clojure-hook)
Also tried with and without the backslash.
That also made no difference, even though it does turn on auto complete mode fine.
Can anyone advise where I should put this binding?
You're missing a close parenthesis on your call to kbd. Your hook should read as follows (incidentally, I don't think you need to escape the question mark):
(defun my-clojure-hook ()
(auto-complete-mode 1)
(define-key clojure-mode-map
(kbd "M-?") 'paredit-convolute-sexp))
And actually, the call to define-key only needs to be made once (say, in your .emacs file) rather than every time you open a new clojure buffer, so you don't need to put it in the hook per se.

Keep getting mode name when switching buffers in Emacs

I have strange thing in my Emacs and I can't locate it, everytime I switch a buffer I get message with major mode name even when I call the function I get minibuffer-inactive-mode
The only global function (for all modes) in my .emacs file (I think) is this:
(add-hook 'after-change-major-mode-hook (lambda ()
(if (not (memql (intern (major-mode))
'(fundamental-mode
erc-mode
text-mode
sql-mode)))
(local-set-key (kbd "RET")
'new-line-and-indent-fix))))
How to find the place that add this annoying thing? What different hook can be executed on each mode?
There is no major-mode function in vanilla Emacs. Whatever that function is in your config, it's probably responsible for displaying the message you're seeing.
You want to fix your code (as per Stefan's comment), but you probably also want to look into that non-standard function:
M-x find-function RET major-mode RET

Binding similar commands from different modes to the same key

I use emacs in multiple modes (ESS, Auctex, Slime, elisp, etc...) all using evil-mode key-bindings. Each of the interaction modes have similar functions for evaluating regions, lines or buffers that I have bound to shortcuts using spacebar as a prefix.
;; bind slime's eval and elisp eval to the key sequence "<SPC>e"
(evil-define-key 'normal lisp-mode-map (kbd "<SPC>e") 'slime-eval-last-expression)
(evil-define-key 'normal lisp-interaction-mode-map (kbd "<SPC>e") 'eval-last-sexp)
I would like to set a default key for a "type" of function, so that I don't need to have an entry like the above for every interaction mode I use and for every command. This would hopefully give a more readable .emacs init file and make it easier to change my key-bindings in the future.
I'm fairly sure that I could do this myself using a series of hooks, but I wonder if there is any existing or built-in support for this?
Thanks
tensorproduct
I don't know anything about Evil, so I'll give the normal Emacs solution:
(global-set-key [?\s ?e] #'my-eval-last-sexp)
(defvar my-eval-last-sexp-command #'undefined)
(defun my-eval-last-sexp ()
(interactive)
(call-interactively my-eval-last-sexp-command))
(add-hook 'emacs-lisp-mode-hook
(lambda () (set (make-local-variable 'my-eval-last-sexp-command) #'eval-last-sexp))
(add-hook 'lisp-mode-hook
(lambda () (set (make-local-variable 'my-eval-last-sexp-command) #'slime-eval-last-expression))
...
As you can see, there's only one mention of the key you want (in this case [?\s ?e]). But you don't save much on the amount of code you have to write. You might improve it by making my-eval-last-sexp a bit more complex (e.g. it could try to guess the command name from the major mode name), or by replacing the hook function with a global alist.
Hopefully, in some future Emacs, all such source-code modes that interact with some interpreter/compiler will share more of their code so that your problem will simply disappear.

Set custom keybinding for specific Emacs mode

Though I know how to set a global key-binding in Emacs, I find it hard to even Google out the code for a local (minor-mode specific) key-binding. For instance, I have this code in my .emacs:
;; PDFLaTeX from AucTeX
(global-set-key (kbd "C-c M-p")
(lambda ()
(interactive)
(shell-command (concat "pdflatex " buffer-file-name))))
I don't want to set it globally. Is there a function like local-set-key?
I use the following:
(add-hook 'LaTeX-mode-hook
(lambda () (local-set-key (kbd "C-0") #'run-latexmk)))
to have a bind defined for LaTeX mode alone.
To bind a key in a mode, you need to wait for the mode to be loaded before defining the key. One could require the mode, or use eval-after-load
(eval-after-load 'latex
'(define-key LaTeX-mode-map [(tab)] 'outline-cycle))
Don't forget either '—eval-after-load is not a macro, so it needs them.
You need to identify the key map for that mode (for example, LaTeX-mode-map) and use the function define-key. As an example, along with activating outline-minor-mode within LaTeX mode, I have:
(define-key LaTeX-mode-map [(tab)] 'outline-cycle))
In this case the major mode (LaTeX) holds the key binding, but there is also an outline-minor-mode-map.
None of the other answers satisfied my needs. So this may help other people. I wanted Tab to jump to the beginning of the line if I'm in Evil's normal mode (basically this means everywhere in Emacs), but I instead wanted it to cycle between org item states if I am in an org-mode document.
One option was to mess around with separate bindings and constant binding-rebinding whenever I switched buffers (because evil allows only one binding per key in its normal state).
But a more efficient option was to make Tab run my own code which runs the required function based on which major mode the current buffer uses. So if I am in a org buffer, this code runs org-cycle, and otherwise it runs evil-first-non-blank (go to the first non-whitespace character on the line).
The technique I used here can also be used by calling your custom function via global-set-key instead, for people who use regular non-evil Emacs.
For those who don't know Emacs lisp, the first line after the "if" statement is the true-action, and the line after that is the false-action. So if major-mode equals org-mode, we run org-cycle, otherwise we run evil-first-non-blank in all other modes:
(defun my/tab-jump-or-org-cycle ()
"jumps to beginning of line in all modes except org mode, where it cycles"
(interactive)
(if (equal major-mode 'org-mode)
(org-cycle)
(evil-first-non-blank))
)
(define-key evil-normal-state-map (kbd "<tab>") 'my/tab-jump-or-org-cycle)

Auto-complete mode doesn't turn on automatically in ObjC buffers

I load auto-complete mode like this:
(let ((ac-path "path/to/auto-complete"))
(add-to-list 'load-path ac-path)
(require 'auto-complete-config)
(add-to-list 'ac-dictionary-directories (concat ac-path "ac-dict"))
(ac-config-default))
It works fine with C major mode, but doesn't turn on automatically when I open ObjC files. I can still turn it on manually and it will work fine along with the ObjC major mode.
Here's a snippet from the docs regarding ObjC major mode:
The hook `c-mode-common-hook' is run with no args
at mode initialization, then `objc-mode-hook'.
If I understand correctly, auto-complete adds a hook to the c-mode-common-hook, but objc-mode-hook somehow overrides it. Is there a way to fix this?
Thanks.
While looking through the source code of auto-complete.el, I've stumbled upon this definition
(defcustom ac-modes
'(emacs-lisp-mode
lisp-interaction-mode
c-mode cc-mode c++-mode
java-mode clojure-mode scala-mode
scheme-mode
ocaml-mode tuareg-mode
perl-mode cperl-mode python-mode ruby-mode
ecmascript-mode javascript-mode js-mode js2-mode php-mode css-mode
makefile-mode sh-mode fortran-mode f90-mode ada-mode
xml-mode sgml-mode)
"Major modes `auto-complete-mode' can run on."
:type '(repeat symbol)
:group 'auto-complete)
It turns out that auto-complete doesn't have a true global mode. It is enabled only with those major modes that are included in the ac-modes variable.
So, adding the following line to the .emacs file has solved the issue for me.
; add this line after the auto-complete mode has been loaded
(add-to-list 'ac-modes 'objc-mode)
Use the following:
(defun my-objc-mode-hook ()
(auto-complete-mode 1))
(add-hook 'objc-mode-hook 'my-objc-mode-hook)
Note 1: The function auto-complete-mode is a toggle function, when called with no arguments.
Note 2: It's possible to add an anonymous function using lambda, but this have several drawbacks. The most important ones are: modifying the function and reevaluating the expression will add the modified function in addition to the earlier version and C-h v xxx will print the full unformatted lambda function, which typically is hopeless to read and understand.
(add-hook 'objc-mode-hook 'auto-complete-mode)
That should do it if you're using auto-complete-mode. You can add more complex things to mode hooks by doing:
(add-hook 'objc-mode-hook '(lambda ()
(something-with arguments)))
Note that both arguments to add-hook are quoted, this is necessary and if you add unquoted functions they will probably not work.