To: "Christian J. Robinson" <infynity@cyberhighway.net>
Cc: Vim Developers List <vim-dev@vim.org>
Subject: patch 5.4.38 (was: optwin.vim obliterates mappings)
In-Reply-To: <Pine.LNX.4.10.9908191940080.1423-100000@draken.localnet>
Fcc: outbox
From: Bram Moolenaar <Bram@moolenaar.net>
------------

Christian J. Robinson wrote:

> I've noticed that optwin.vim has commands to set and remove mappings, but
> a user could have previously definde mappings for these keys, so they're
> lost.
> 
> I know that mappings local to a buffer are a todo item, but until then
> optwin.vim could check to see if mappings for these keys already exist
> and set them in some variable to restore them later.

Indeed.  I'll use your suggestion and make a patch out of it.

I also noticed that sometimes a string option would be handled like a boolean
option.  For example, when changing the line:
	:set go=mrt
to
	:set go&
and hitting <CR> to execute it, would result in
	:set go		nogo

This was because any option line without a "=" was handled like a boolean
option.  I added a check for the value being "0" or "1", that should catch
almost all situations.

> It might be a good idea to do a mapcheck() for conflicting mappings too,
> but I'm not sure what should be done if conflicting mappings are found.

No, I also don't know how to handle that.  Hopefully nobody uses two-character
mappings that start with <CR> or <Space>.


Patch 5.4.38
Problem:    In the option-window, ":set go&" resulted in 'go' being handled
	    like a boolean option.
	    Mappings for <Space> and <CR> were overruled by the option-window.
Solution:   When the value of an option isn't 0 or 1, don't handle it like a
	    boolean option.
	    Save and restore mappings for <Space> and <CR> when entering and
	    leaving the option-window.
Files:	    runtime/optwin.vim


*** ../vim-5.4.37/runtime/optwin.vim	Tue Aug 10 16:09:44 1999
--- runtime/optwin.vim	Fri Aug 20 11:17:22 1999
***************
*** 1,7 ****
  " These commands create the option window.
  "
  " Maintainer:	Bram Moolenaar <Bram@vim.org>
! " Last change:	1999 Jul 29
  
  " Make sure the '<' flag is not included in 'cpoptions', otherwise <CR> would
  " not be recognized.  See ":help 'cpoptions'".
--- 1,7 ----
  " These commands create the option window.
  "
  " Maintainer:	Bram Moolenaar <Bram@vim.org>
! " Last change:	1999 Aug 20
  
  " Make sure the '<' flag is not included in 'cpoptions', otherwise <CR> would
  " not be recognized.  See ":help 'cpoptions'".
***************
*** 98,104 ****
    if a:local
      exe "norm! " . a:thiswin . "\<C-W>w"
    endif
!   if match(a:line, "=") >= 0
      call setline(a:lnum, "set " . name . "=" . val)
    else
      if val
--- 98,104 ----
    if a:local
      exe "norm! " . a:thiswin . "\<C-W>w"
    endif
!   if match(a:line, "=") >= 0 || (val != "0" && val != "1")
      call setline(a:lnum, "set " . name . "=" . val)
    else
      if val
***************
*** 848,853 ****
--- 848,859 ----
  fun! OW_enter()
    let cpo_save = &cpo
    let &cpo = ""
+   " save existing mappings
+   let g:OW_mappings = ""
+   call OW_mapsave("<CR>", "n")
+   call OW_mapsave("<CR>", "i")
+   call OW_mapsave("<Space>", "n")
+   call OW_mapsave("<Space>", "i")
    noremap <CR> :call OW_CR()<CR><C-\><C-N>:echo<CR>
    inoremap <CR> <Esc>:call OW_CR()<CR>:echo<CR>
    noremap <Space> :call OW_Space()<CR>:echo<CR>
***************
*** 855,860 ****
--- 861,873 ----
    let &cpo = cpo_save
  endfun
  
+ fun! OW_mapsave(map, mode)
+   let m = maparg(a:map, a:mode)
+   if m != ""
+     let g:OW_mappings = g:OW_mappings . ":".a:mode."map ".a:map." ".m."|"
+   endif
+ endfun
+ 
  fun! OW_leave()
    let cpo_save = &cpo
    let &cpo = ""
***************
*** 864,869 ****
--- 877,886 ----
      unmap <Space>
      iunmap <Space>
    endif
+   if exists("g:OW_mappings")
+     exe g:OW_mappings
+     unlet g:OW_mappings
+   endif
    let &cpo = cpo_save
  endfun
  
***************
*** 879,887 ****
    delfun OW_Header
    au! optwin
    bdel! option-window
-   call OW_leave()
    delfun OW_enter
    delfun OW_leave
  endfun
  
  " Execute the enter autocommands now, to enable the mappings
--- 896,904 ----
    delfun OW_Header
    au! optwin
    bdel! option-window
    delfun OW_enter
    delfun OW_leave
+   delfun OW_mapsave
  endfun
  
  " Execute the enter autocommands now, to enable the mappings
*** ../vim-5.4.37/src/version.h	Fri Aug 20 11:21:14 1999
--- src/version.h	Fri Aug 20 11:19:10 1999
***************
*** 19,26 ****
  #define VIM_VERSION_MINOR_STR		"4"
  #define VIM_VERSION_BUILD		 57
  #define VIM_VERSION_BUILD_STR		"57"
! #define VIM_VERSION_PATCHLEVEL		 37
! #define VIM_VERSION_PATCHLEVEL_STR	"37"
  
  /*
   * VIM_VERSION_NODOT is used for the runtime directory name.
--- 19,26 ----
  #define VIM_VERSION_MINOR_STR		"4"
  #define VIM_VERSION_BUILD		 57
  #define VIM_VERSION_BUILD_STR		"57"
! #define VIM_VERSION_PATCHLEVEL		 38
! #define VIM_VERSION_PATCHLEVEL_STR	"38"
  
  /*
   * VIM_VERSION_NODOT is used for the runtime directory name.
***************
*** 30,35 ****
   */
  #define VIM_VERSION_NODOT	"vim54"
  #define VIM_VERSION_SHORT	"5.4"
! #define VIM_VERSION_MEDIUM	"5.4.37"
! #define VIM_VERSION_LONG	"VIM - Vi IMproved 5.4.37 (1999 Aug 19)"
! #define VIM_VERSION_LONG_DATE	"VIM - Vi IMproved 5.4.37 (1999 Aug 19, compiled "
--- 30,35 ----
   */
  #define VIM_VERSION_NODOT	"vim54"
  #define VIM_VERSION_SHORT	"5.4"
! #define VIM_VERSION_MEDIUM	"5.4.38"
! #define VIM_VERSION_LONG	"VIM - Vi IMproved 5.4.38 (1999 Aug 20)"
! #define VIM_VERSION_LONG_DATE	"VIM - Vi IMproved 5.4.38 (1999 Aug 20, compiled "

--
hundred-and-one symptoms of being an internet addict:
163. You go outside for the fresh air (at -30 degrees) but open the
     window first to hear new mail arrive.

--/-/---- Bram Moolenaar ---- Bram@moolenaar.net ---- Bram@vim.org ---\-\--
  \ \    www.vim.org/iccf      www.moolenaar.net       www.vim.org    / /
