Without hacking the source or creating my own isearch-repeat... function, is it possible to disable the default sticky highlight for isearch-repeat-forward and isearch-repeat-backward?
I'm using the latest developer build of Emacs Trunk --with-ns.
Here are some ideas (a work in progress), but they do not resolve the issue -- isearch-mode-end-hook does not seem to be linked to isearch-repeat-forward and isearch-repeat-backward. It would be nice if any keyboard input (escape and/or arrow keys) could momentarily be linked to lawlist-lazy-highlight-cleanup when exiting isearch-repeat.
(add-hook 'isearch-mode-end-hook 'my-isearch-end)
(defun my-isearch-end ()
"Custom behaviors for `isearch-mode-end-hook'."
(when isearch-mode-end-hook-quit
(lawlist-lazy-highlight-cleanup t)))
(defun lawlist-lazy-highlight-cleanup (&optional force)
"Stop lazy highlighting and remove extra highlighting from current buffer.
FORCE non-nil means do it whether or not `lazy-highlight-cleanup'
is nil. This function is called when exiting an incremental search if
`lazy-highlight-cleanup' is non-nil."
(interactive '(t))
(if (or force lazy-highlight-cleanup)
(while isearch-lazy-highlight-overlays
(delete-overlay (car isearch-lazy-highlight-overlays))
(setq isearch-lazy-highlight-overlays
(cdr isearch-lazy-highlight-overlays))))
(when isearch-lazy-highlight-timer
(cancel-timer isearch-lazy-highlight-timer)
(setq isearch-lazy-highlight-timer nil))
(lawlist-isearch-dehighlight))
(defun lawlist-isearch-dehighlight ()
(interactive)
(when isearch-overlay
(delete-overlay isearch-overlay)))
(defun lawlist-isearch-repeat-forward ()
"Repeat incremental search forwards."
(interactive)
(isearch-repeat 'forward)
;; need to add something that says sit-for any keyboard input before cleanup occurs.
;; (read-event) ;; read-char-exclusive | read-char | read-event
(sit-for 60)
(lawlist-lazy-highlight-cleanup))
(defun lawlist-isearch-repeat-backward ()
"Repeat incremental search backwards."
(interactive)
(isearch-repeat 'backward)
;; need to add something that says sit-for any keyboard input before cleanup occurs.
;; (read-event) ;; read-char-exclusive | read-char | read-event
(sit-for 60)
(lawlist-lazy-highlight-cleanup))
While this does not anwswer your question, it may do the thing you ultimately want to solve:
(define-key isearch-mode-map [(control up)] 'isearch-ring-retreat)
(define-key isearch-mode-map [(control down)] 'isearch-ring-advance)
After you type C-S, you may use C-up and C-down to move through search history. So C-S C-up will do what you want: repeat the last search without leaving "sticky" overlays.
See also search-ring-update, to further custom this if you like.
Using the source code of isearch.el as a guide, the following OSX additional key assignments eliminate the problem with sticky highlight as outlined in the question above.
(define-key global-map [?\s-f] 'isearch-forward)
(define-key esc-map [?\s-f] 'isearch-forward-regexp)
(define-key minibuffer-local-isearch-map [?\s-f] 'isearch-forward-exit-minibuffer)
(define-key isearch-mode-map [?\s-f] 'isearch-repeat-forward)
(define-key global-map [?\s-F] 'isearch-backward)
(define-key esc-map [?\s-F] 'isearch-backward-regexp)
(define-key minibuffer-local-isearch-map [?\s-F] 'isearch-reverse-exit-minibuffer)
(define-key isearch-mode-map [?\s-F] 'isearch-repeat-backward)
Related
What is the proper way to override a keybinding for a major mode so it only affects the buffer-local keymap? I thought I could use local-set-key or make-local-variable, but my attempts there affect the keymaps globally (shown below).
Is it necessary to copy the map, as done below, or create a minor mode with a different keymap? It would be nice to be able to make it a local variable if possible.
For example, say I want to have original bound to C-c C-c in the global emacs-lisp-mode-map but after calling jump-to-other-buffer I want C-c C-c to be bound to local-version only in that buffer.
(defun original ()
(interactive)
(message "original"))
(defun local-version ()
(interactive)
(message "local binding"))
;; open new buffer in emacs-lisp mode and set a local key
(defun jump-to-other-buffer ()
(interactive)
(with-current-buffer (get-buffer-create "*test1*")
(emacs-lisp-mode)
;; These change bindings in all elisp buffers
;; (make-local-variable 'emacs-lisp-mode-map)
;; (define-key emacs-lisp-mode-map (kbd "C-c C-c") 'local-version)
;; (local-set-key (kbd "C-c C-c") 'local-version)
(let ((overriding-local-map (copy-keymap emacs-lisp-mode-map)))
(define-key overriding-local-map (kbd "C-c C-c") 'local-version)
(use-local-map overriding-local-map))
(pop-to-buffer (current-buffer))))
;; default binding in elisp buffers
(define-key emacs-lisp-mode-map (kbd "C-c C-c") 'original)
I like VIM idea of text objects, so I installed EVIL (a Emacs plugin to emulate VIM features). But I'd like «insert» mode to leave Emacs keybindings unchanged (except perhaps Escape which is to switch into «normal» mode). Any way to achieve this?
By the way: ATM «insert» mode have a mixed set of hotkeys, which isn't very comfortable either way. E.g. the «M-b» works as in Emacs, but the «C-o» works as in VIM.
In the #emacs IRC channel I was told that someone already solved a similar problem. Here's the modified version I use:
(require 'evil)
;; remove all keybindings from insert-state keymap
(setcdr evil-insert-state-map nil)
;; but [escape] should switch back to normal state
(define-key evil-insert-state-map [escape] 'evil-normal-state)
(define-key evil-normal-state-map (kbd "C-u") 'evil-scroll-up)
(define-key evil-normal-state-map (kbd "[ m") 'beginning-of-defun)
(define-key evil-normal-state-map (kbd "] m") 'end-of-defun)
(define-key evil-normal-state-map (kbd "k") 'evil-previous-visual-line)
(define-key evil-normal-state-map (kbd "j") 'evil-next-visual-line)
(evil-mode t)
(setq evil-jumps-cross-buffers nil) ;; for C-o and C-i to not cross buffers
(provide 'emvil)
The (provide 'emvil) is to allow require 'ing it in the configuration. I also found it useful to jump-to-definition in the next split screen unless the definition is in the buffer I'm currently in. Here's the code:
(defun evil-goto-definition-next-split ()
"If there's a free split, goto definition in this split,
otherwise use current one (except when a definition in the
current split)"
(interactive)
(let ((origin-spl (selected-window))
(origin-buf (current-buffer)))
(evil-goto-definition)
(when (and (eq origin-spl (selected-window)) ;; otherwise it's done
(not (eq origin-buf (current-buffer)))) ;; otherwise either definition not found, or
;; it's in the same buffer
(let ((defin-buf (current-buffer))
(defin-point (point)))
(switch-to-buffer origin-buf)
(other-window 1)
(switch-to-buffer defin-buf)
(goto-char defin-point)
))
))
(define-key evil-normal-state-map (kbd "g d") 'evil-goto-definition-next-split)
When running certain commands, the mini-buffer asks input strings, (e.g. the C-M-s).
Sometimes I need to enter complex strings into minibuffer. Therefore, I need to use movement commands such as C-f, C-b, C-a, etc. However, these does not work when I am entering string inside command C-M-s.
So, what is the general command / key-bindings for me to focus point in mini-buffer for extended movement support?
Edit:
I just discovered that M-e will work for searching commands. But I am not sure whether this command is the general command to "switch from buffer to minibuffer for dedicated editing"
Yes, M-e is what you are looking for, if you want to edit the search string. It lets you perform general editing on it. Just hit C-s or C-M-s again when you are ready to search for the edited string.
However, M-e is only for editing the search string. If you instead want to interrupt isearch to do some editing somewhere, then just end isearch to do that, and then resume isearch when done editing, by using C-s or C-M-s again.
I have been happy using these and I bind them to the function button and arrow keys on my Mac keyboard. I frequently block and copy text and move in and out of the mini-buffer. The following example frees up some of the keymap assignments in the minibuffer-local-map and minibuffer-local-completion-map (i.e., by setting them to nil) so that I can use my own custom keyboard shortcuts to enter and exit the mini-buffer.
From inside the mini-buffer, you can use C-h k and then the type the keyboard shortcut to see what function is bound.
When I switch in and out of the mini-buffer window, I use a custom function that changes the mode-line color, and mini-buffer prompt color, and default color inside the mini-buffer, but that is beyond the scope of your question. [I just add the name of my mini-buffer color change function at the tail end of the following four functions -- i.e., after the if / then statements.]
(defun lawlist-windmove-right ()
(interactive)
(if (window-in-direction 'right)
(select-window (window-in-direction 'right))
(other-window 1)))
(defun lawlist-windmove-left ()
(interactive)
(if (window-in-direction 'left)
(select-window (window-in-direction 'left))
(other-window -1)))
(defun lawlist-windmove-up ()
(interactive)
(if (window-in-direction 'above)
(select-window (window-in-direction 'above))
(other-window 1)))
(defun lawlist-windmove-down ()
(interactive)
(if (window-in-direction 'below)
(select-window (window-in-direction 'below))
(other-window -1)))
(define-key minibuffer-local-map [prior] nil)
(define-key minibuffer-local-map [next] nil)
(define-key minibuffer-local-map [home] nil)
(define-key minibuffer-local-map [end] nil)
(define-key minibuffer-local-completion-map [prior] nil)
(define-key minibuffer-local-completion-map [next] nil)
(define-key minibuffer-local-completion-map [home] nil)
(define-key minibuffer-local-completion-map [end] nil)
(global-set-key (kbd "<end>") 'lawlist-windmove-right)
(global-set-key (kbd "<home>") 'lawlist-windmove-left)
(global-set-key (kbd "<prior>") 'lawlist-windmove-up)
(global-set-key (kbd "<next>") 'lawlist-windmove-down)
I have set up a key binding for a function that modifies the behavior of Emacs auto-complete:
;; ISSUE: when I type the whole candidate string and then press [SPC],
;; Emacs will insert two spaces.
(define-key ac-menu-map (kbd "SPC")
(defun ac-complete-with-space ()
"Select the candidate and append a space. Save your time for typing space."
(interactive)
(ac-complete)
;; FIXME: this auto-inserts two spaces.
(insert " ")
))
... and I want to disable this key binding in ac-menu-map in org-mode only.
I have tried the following:
;; Avoid always selecting unwanted first candidate with auto-complete
;; when writing in org-mode.
(add-hook 'org-mode-hook
(lambda ()
;; (define-key ac-menu-map (kbd "SPC") nil)
;; (define-key ac-menu-map (kbd "SPC") 'self-insert-command)
;; (setq-local ac-menu-map (delq (kbd "SPC") ac-menu-map))
))
Unfortunately, this does not unset the key binding locally (i.e., only in org-mode). Instead, it removes the key binding from the ac-menu-map everywhere.
A different approach to solve your problem would be to check in ac-complete-with-space the mode. If org-mode, then call self-insert-command, else follow your current logic.
(defun ac-complete-with-space ()
"Select the candidate and append a space. save your time for typing space."
(interactive)
(ac-complete)
(insert " ")
)
;; NOTE: ac-completing-map is the parent map of ac-menu-map.
(define-key ac-completing-map (kbd "SPC") 'ac-complete-with-space)
(add-hook 'org-mode-hook
(lambda ()
(define-key ac-menu-map (kbd "SPC") 'self-insert-command)))
I would like to ask regular ESS/R users what key bindings do they use frequently and tips on using ESS/R.
I have set several shortcuts in my .emacs file. The most useful are:
C-tab to switch between the R command line and the file (similar to josh answer, but much faster):
(global-set-key [C-tab] 'other-window)
Control and up/down arrow keys to search history with matching what you've already typed:
(define-key comint-mode-map [C-up] 'comint-previous-matching-input-from-input)
(define-key comint-mode-map [C-down] 'comint-next-matching-input-from-input)
Comment-uncomment a selected region with C-d or C-maj-d
(defun uncomment-region (beg end)
"Like `comment-region' invoked with a C-u prefix arg."
(interactive "r")
(comment-region beg end -1))
(define-key ess-mode-map (kbd "C-d") 'comment-region)
(define-key ess-mode-map (kbd "C-S-d") 'uncomment-region)
Also I've also enabled CUA mode (from options menu) and reconfigured quite a lot of shortcuts to require only two keystrokes (instead of four in standard mode):
;; Delete selection when pressing [delete] key
(delete-selection-mode t)
;; ESS Mode (.R file)
(define-key ess-mode-map "\C-l" 'ess-eval-line-and-step)
(define-key ess-mode-map "\C-p" 'ess-eval-function-or-paragraph-and-step)
(define-key ess-mode-map "\C-r" 'ess-eval-region)
;; iESS Mode (R console)
(define-key inferior-ess-mode-map "\C-u" 'comint-kill-input)
(define-key inferior-ess-mode-map "\C-w" 'backward-kill-word)
(define-key inferior-ess-mode-map "\C-a" 'comint-bol)
(define-key inferior-ess-mode-map [home] 'comint-bol)
;; Comint Mode (R console as well)
(define-key comint-mode-map "\C-e" 'comint-show-maximum-output)
(define-key comint-mode-map "\C-r" 'comint-show-output)
(define-key comint-mode-map "\C-o" 'comint-kill-output)
;; Search with C-f / C-F (control-maj-F for backware search)
(global-set-key "\C-f" 'isearch-forward)
(global-set-key (kbd "C-S-f") 'isearch-backward)
(define-key isearch-mode-map "\C-f" 'isearch-repeat-forward)
(define-key isearch-mode-map (kbd "C-S-f") 'isearch-repeat-backward)
;; Save with C-s / C-S
(global-set-key (kbd "C-s") 'save-buffer)
(global-set-key (kbd "C-S-s") 'write-file)
;; need to redefine them for isearch mode (don't know why)
(define-key isearch-mode-map (kbd "C-s") 'save-buffer)
(define-key isearch-mode-map (kbd "C-S-s") 'write-file)
;; Pause = dedicate window.
(defun toggle-current-window-dedication ()
(interactive)
(let* ((window (selected-window))
(dedicated (window-dedicated-p window)))
(set-window-dedicated-p window (not dedicated))
(message "Window %sdedicated to %s"
(if dedicated "no longer " "")
(buffer-name))))
(global-set-key [pause] 'toggle-current-window-dedication)
;; delete = delete
(global-set-key [delete] 'delete-char)
;; C-b = list buffers
(global-set-key (kbd "C-b") 'bs-show)
You will find many more useful shortcuts in ESS documentation.
C-c C-z ess-switch-to-end-of-ESS
is nice to jump from your source file that you are editing foo.R to the R console
I found this link to be extremely helpful. It provides elisp code to make Shift+Enter do many common tasks in a context dependent fashion.
http://kieranhealy.org/blog/archives/2009/10/12/make-shift-enter-do-a-lot-in-ess/
Great stuff, have been using it for ages. Unfortunately as of 15-11-2013 the uncomment key binding may not work due to EMACS changes (I think, at least it was working before I loaded the latest version). This is because the default uncomment function has 3 arguments but the one defined above has 2. The best way to fix this is to simply delete the uncomment function from the code and retain the keybinding, so it uses the default uncomment function. Or in other words just use this:
(define-key ess-mode-map (kbd "C-d") 'comment-region)
(define-key ess-mode-map (kbd "C-S-d") 'uncomment-region)
M-n and M-p in the ESS R console for next/previous command.