Vim Tips Wiki
Register
(Improve script and express frustration)
(→‎Comments: but why not prevent the message in the first place rather than use echo ""?)
Line 60: Line 60:
 
This is a pretty cool tip though! I like the idea of multiple things mapped to the same key.
 
This is a pretty cool tip though! I like the idea of multiple things mapped to the same key.
 
:I think the idea of <tt>echo ""</tt> is to clear the message line, for example, if you previously got a "Pattern not found" message.
 
:I think the idea of <tt>echo ""</tt> is to clear the message line, for example, if you previously got a "Pattern not found" message.
  +
::I think you're right about the purpose, but I personally think it is better to avoid the message in the first place with :silent! or the 'e' option for searches. But I guess I'm just nitpicking.
   
 
----
 
----

Revision as of 14:08, 9 May 2008

Proposed tip Please edit this page to improve it, or add your comments below (do not use the discussion page).

Please use new tips to discuss whether this page should be a permanent tip, or whether it should be merged to an existing tip.
created April 22, 2008 · complexity basic · author Niels AdB · version 7.0

The following function can be used to maintain a list of handlers, to make handling a keypress in different contexts more dynamic.

function! RunHandlers(handler_list, default_action)
  for handler in a:handler_list
    execute "let handled = " handler."()"
    if handled
      return
    endif
  endfor
  execute a:default_action
endfunction

This function calls a list of handlers until one returns true, indicating the event was handled. If no handler returns true, a default action is executed.

Take for example, the following CloseScratch function.

function! CloseScratch()
  if &buftype == "nofile" && &bufhidden == "hide" && !&swapfile
    bdelete
    return 1
  endif
  return 0
endfunction

Using RunHandlers I can now dynamically overload <Esc> to call this function (in normal mode), like so:

" Default Escape and clear message line.
function! DefaultEsc()
  normal! "<Esc>"
  echo ""
endfunction

let g:esc_handler = []
call add(g:esc_handler, "CloseScratch")
nnoremap <Esc> :call RunHandlers(g:esc_handler, "call DefaultEsc()")<CR>

Adding more handlers should be easy enough. In this case, CloseScratch is tried first, and if that fails DefaultEsc is called.

Comments

Try using :silent or <silent> rather than an empty echo to make the bottom line clear.

This is a pretty cool tip though! I like the idea of multiple things mapped to the same key.

I think the idea of echo "" is to clear the message line, for example, if you previously got a "Pattern not found" message.
I think you're right about the purpose, but I personally think it is better to avoid the message in the first place with :silent! or the 'e' option for searches. But I guess I'm just nitpicking.

Will someone please explain what DefaultEsc actually does!? I can understand the echo, and I know that normal! "<Esc>" will execute the normal command "press the Escape key" (without invoking any mapping for that key). But what does the Escape key do in normal mode (apart from beep/flash if you had them turned on)??

I have improved the script, and I can see that it is potentially useful, but I think the tip is pretty disappointing without a realistic example of chaining a couple of handlers. --JohnBeckett 11:26, 9 May 2008 (UTC)