[Vim-latex-cvs] vimfiles/plugin imaps.vim,1.9.2.4,1.9.2.5
Brought to you by:
srinathava,
tmaas
From: <sri...@us...> - 2002-12-19 11:44:58
|
Update of /cvsroot/vim-latex/vimfiles/plugin In directory sc8-pr-cvs1:/tmp/cvs-serv7443 Modified Files: Tag: b-newimaps imaps.vim Log Message: Comments on the last version: " Break text up into "initial <+template+> final"; any piece may be empty. let initial = substitute(text, pattern, '\1', '') let template = substitute(text, pattern, '\2', '') let final = substitute(text, pattern, '\3', '') This assumes that there is only a single <+template+> in the text. Afterwards: let final = substitute(final, startpat, escape(phs, '\'), 'g') let final = substitute(final, endpat, escape(phe, '\'), 'g') This will fail when the user uses something like: call Imap_PutTextWithMovement('bar?desc?', '?', '?') with g:Imap_Place..Start = '<+' != g:Imap_Place...End i.e when phs and phe are the same, but the [bg]:Imap_Place* settings arent. Also, I dont think let marker = phs . phs . phe . phe is a "rare" enough string. As in the previous example, this could simply be ????. We do _not_ want to accidentally delete stuff from the user's files. let marker = '<!--- @#% Start Here @#% ----!>' This sounds safer. Ofcourse, we could always make a search and be sure, but I am willing to bet that this string will never happen on purpose in some file. Changes this time: 1. Cleaned up the IMAP_PutTextWithMovement() function. This function now calls the IMAP_Jumpfunc() function instead of trying to do the same thing. 2. IMAP_Jumpfunc(): After suggestions from LH, implemented the option of . not deleting empty placeholders. . going up and down. (<C-J> and <C-K>) . option of not deleting the placeholder while moving. TODO: . command to insert placeholder at current position . command to erase all placeholders. I am going to follow up this commit with another big one renaming all the old Tex_IMAP()s to IMAP()s... After that, it is time to merge this branch into the main trunk and get working on a next release version and also solving the encoding problem once and for all... Index: imaps.vim =================================================================== RCS file: /cvsroot/vim-latex/vimfiles/plugin/imaps.vim,v retrieving revision 1.9.2.4 retrieving revision 1.9.2.5 diff -C2 -d -r1.9.2.4 -r1.9.2.5 *** imaps.vim 18 Dec 2002 16:36:19 -0000 1.9.2.4 --- imaps.vim 19 Dec 2002 11:44:55 -0000 1.9.2.5 *************** *** 8,12 **** " while preserving filetype indentation. " ! " Last Change: Wed Dec 18 11:00 AM 2002 EST " " Documentation: {{{ --- 8,12 ---- " while preserving filetype indentation. " ! " Last Change: Thu Dec 19 03:00 AM 2002 PST " " Documentation: {{{ *************** *** 49,53 **** " form: " ! " call Tex_IMAP (lhs, rhs, ft) " " Some characters in the RHS have special meaning which help in cursor --- 49,53 ---- " form: " ! " call IMAP (lhs, rhs, ft) " " Some characters in the RHS have special meaning which help in cursor *************** *** 56,60 **** " Example One: " ! " call Tex_IMAP ("bit`", "\\begin{itemize}\<cr>\\item <++>\<cr>\\end{itemize}<++>", "tex") " " This effectively sets up the map for "bit`" whenever you edit a latex file. --- 56,60 ---- " Example One: " ! " call IMAP ("bit`", "\\begin{itemize}\<cr>\\item <++>\<cr>\\end{itemize}<++>", "tex") " " This effectively sets up the map for "bit`" whenever you edit a latex file. *************** *** 83,87 **** " Example Two: " You can use the <C-r> command to insert dynamic elements such as dates. ! " call Tex_IMAP ('date`', "\<c-r>=strftime('%b %d %Y')\<cr>", '') " " sets up the map for date` to insert the current date. --- 83,87 ---- " Example Two: " You can use the <C-r> command to insert dynamic elements such as dates. ! " call IMAP ('date`', "\<c-r>=strftime('%b %d %Y')\<cr>", '') " " sets up the map for date` to insert the current date. *************** *** 91,94 **** --- 91,96 ---- " '----%<----' above and below the visually selected range of lines. The " length of the string is chosen to be equal to the longest line in the range. + " Recommended Usage: + " '<,'>Snip "--------------------------------------%<-------------------------------------- " }}} *************** *** 110,114 **** " IMAP: Adds a "fake" insert mode mapping. {{{ " For example, doing ! " Tex_IMAP('abc', 'def' ft) " will mean that if the letters abc are pressed in insert mode, then " they will be replaced by def. If ft != '', then the "mapping" will be --- 112,116 ---- " IMAP: Adds a "fake" insert mode mapping. {{{ " For example, doing ! " IMAP('abc', 'def' ft) " will mean that if the letters abc are pressed in insert mode, then " they will be replaced by def. If ft != '', then the "mapping" will be *************** *** 223,309 **** function! IMAP_PutTextWithMovement(str, ...) ! " Find the place holders for the input. if a:0 < 2 let phs = '<+' let phe = '+>' else ! let phs = a:1 ! let phe = a:2 endif - let startpat = escape(phs, '\') - let endpat = escape(phe, '\') - " A very rare string: - let marker = phs . phs . phe . phe - " TODO: Maybe I need to play some games with iconv() to define marker and - " markerpat in case marker !~ marker ... let text = a:str - " If the user does not want to use place-holders, then remove them. - if exists('g:Imap_UsePlaceHolders') && !g:Imap_UsePlaceHolders - " a heavy-handed way to just use the first placeholder and remove the - " rest. Replace the first template with phe ... - let text = substitute(text, '\V'.startpat.'\.\{-}'.endpat, endpat, '') - " ... delete all the others ... - let text = substitute(text, '\V'.startpat.'\.\{-}'.endpat, '', 'g') - " ... and replace the first phe with phs.phe . - let text = substitute(text, '\V'.endpat, startpat.endpat, '') - endif - let pattern = '\V\(\.\{-}\)' .startpat. '\(\.\{-}\)' .endpat. '\(\.\*\)' " If there are no place holders, just return the text. ! if text !~ pattern return text endif - " Break text up into "initial <+template+> final"; any piece may be empty. - let initial = substitute(text, pattern, '\1', '') - let template = substitute(text, pattern, '\2', '') - let final = substitute(text, pattern, '\3', '') ! " Now, get the place holders for the output. ! let phs = s:PlaceHolderStart() " default <+ ! let phe = s:PlaceHolderEnd() " default +> ! let template = phs . template . phe ! let final = substitute(final, startpat, escape(phs, '\'), 'g') ! let final = substitute(final, endpat, escape(phe, '\'), 'g') ! " Now, start building the return value. ! let text = initial . marker . template . marker . final ! " Return to Normal mode: this works even if 'insertmode' is set: ! let text = text . "\<C-\>\<C-N>" ! " Move to the first marker and delete it. Cursor ends on beginning of ! " template. ! let text = text . "2?" . marker . "\<CR>" . "v/" . marker . "/e\<CR>x" ! " Enter Visual mode and move to just before the next marker. ! let text = text . "v/" . marker . "/s-1\<CR>\<C-\>\<C-N>" ! " Move to the second marker and delete it. ! let text = text . "/" . marker . "\<CR>" . "d/" . marker . "/e\<CR>" ! " Note: We use a search strategy instead of computing the length of the ! " template because strlen() returns different things depending on the ! " encoding. ! " Clean up the search history. Restore Visual selection. ! let text = text . ":" . s:RemoveLastHistoryItem . "\<CR>gv" ! " Finally, append commands to Select <+template+> or replace <++> . ! if template == phs . phe " template looks like <++> so Change it: ! let text = text . "c" ! else " Enter Select mode. ! let text = text . "\<C-G>" endif ! return text ! endfunction ! " Tex_PutTextWithMovement: old version of IMAP_PutTextWithMovement ! " Description: ! " This function is supplied to maintain backward compatibility. ! " This function is only for use in latex-suite. ! function! Tex_PutTextWithMovement(text) ! ! let phs = s:PlaceHolderStart() ! let phe = s:PlaceHolderEnd() ! let newText = substitute(a:text, '<+', phs, 'g') ! let newText = substitute(newText, '+>', phe, 'g') ! return IMAP_PutTextWithMovement(newText) ! endfunction " }}} --- 225,298 ---- function! IMAP_PutTextWithMovement(str, ...) ! " The placeholders used in the particular input string. These can be ! " different from what the user wants to use. if a:0 < 2 let phs = '<+' let phe = '+>' else ! let phs = escape(a:1, '\') ! let phe = escape(a:2, '\') endif let text = a:str " If there are no place holders, just return the text. ! if text !~ '\V'.phs.'\{-}'.phe return text endif ! " The user's placeholder settings. ! let phsUser = s:PlaceHolderStart() ! let pheUser = s:PlaceHolderEnd() ! " A very rare string: Do not use any special characters here. This is used ! " for moving to the beginning of the inserted text ! let marker = '<!--- @#% Start Here @#% ----!>' ! let markerLength = strlen(marker) ! ! " If the user does not want to use place-holders, then remove all but the ! " first placeholder ! if exists('g:Imap_UsePlaceHolders') && !g:Imap_UsePlaceHolders ! " a heavy-handed way to just use the first placeholder and remove the ! " rest. Replace the first placeholder with phe ... ! let text = substitute(text, '\V'.phs.'\.\{-}'.phe, phe, '') ! " ... delete all the others ... ! let text = substitute(text, '\V'.phs.'\.\{-}'.phe, '', 'g') ! " ... and replace the first phe with phsUser.pheUser . ! let text = substitute(text, '\V'.phe, phsUser.pheUser, '') endif ! " now replace all occurences of the placeholders here with the users choice ! " of placeholder settings. ! " NOTE: There can be more than 1 placeholders here. Therefore use a global ! " search and replace. ! let text = substitute(text, '\V'.phs.'\(\.\{-}\)'.phe, phsUser.'\1'.pheUser, 'g') ! " Now append the marker (the rare string) to the beginning of the text so ! " we know where the expansion started from ! let text = marker.text ! " This search will move us to the very beginning of the text to be ! " inserted. ! " The steps are: ! " 1. enter escape mode (using <C-\><C-n> so it works in insertmode as ! " well) ! " 2. Search backward for marker text. ! " 3. delete from the beginning to the end of marker into the blackhole ! " register. ! let movement = "\<C-\>\<C-N>" ! \ . "?\\V".marker."\<CR>" ! \ . '"_d/\V'.marker."/e\<CR>" ! " Now enter insert mode and call IMAP_Jumpfunc() to take us to the next ! " placeholder and get us either into visual or insert mode. Since we do ! " at least one search in this function, remove it from the search history ! " first. ! " NOTE: Even if we performed more than one search, vim will only put one ! " of them in the user's search list. ! let movement = movement.':'.s:RemoveLastHistoryItem."\<CR>" ! \ . "i\<C-r>=IMAP_Jumpfunc('', 1)\<CR>" ! ! return text.movement ! endfunction " }}} *************** *** 311,340 **** " Author: Luc Hermitte " ! function! IMAP_Jumpfunc() ! let phs = escape(s:PlaceHolderStart(), '\') ! let phe = escape(s:PlaceHolderEnd(), '\') ! if !search(phs.'.\{-}' . phe, 'W') "no more marks ! return "" else ! if strpart(getline('.'), col('.') + strlen(phs) - 1) ! \ =~ '^' . phe - return substitute(phs . phe, '.', "\<Del>", 'g') - else - if col('.') > 1 - return "\<Esc>lv/\\V" . phe . "/e\<CR>\<C-g>" - else - return "\<C-\>\<C-n>v/\\V" . phe . "/e\<CR>\<C-g>" - endif - endif endif endfunction " map only if there is no mapping already. allows for user customization. if !hasmapto('IMAP_Jumpfunc') ! inoremap <C-J> <c-r>=IMAP_Jumpfunc()<CR> nmap <C-J> i<C-J> end " }}} --- 300,383 ---- " Author: Luc Hermitte " ! function! IMAP_Jumpfunc(direction, inclusive) ! let g:Imap_DeleteEmptyPlaceHolders = 0 ! " The user's placeholder settings. ! let phsUser = s:PlaceHolderStart() ! let pheUser = s:PlaceHolderEnd() ! ! let searchOpts = a:direction ! " If the user has nowrapscan set, then do not make the search wrap around ! " the end (or beginning) of the file. ! if ! &wrapscan ! let searchOpts = direction.'W' ! endif ! ! let searchString = '' ! " If this is not an inclusive search or if it is inclusive, but the ! " current cursor position does not contain a placeholder character, then ! " search for the placeholder characters. ! if !a:inclusive || strpart(getline('.'), col('.')-1) !~ '\V^'.phsUser ! let searchString = '\V'.phsUser.'\.\{-}'.pheUser ! endif ! ! " If we didn't find any placeholders return quietly. ! if searchString != '' && !search(searchString, searchOpts) ! return '' ! endif ! ! " At this point, we are at the beginning of a placeholder. ! " Remember the position here. ! let position = line('.') . "normal! ".virtcol('.').'|' ! " Open any closed folds and make this part of the text visible. ! silent! foldopen! ! ! " Calculate if we have an empty placeholder or if it contains some ! " description. ! let template = ! \ matchstr(strpart(getline('.'), col('.')-1), ! \ '\V\^'.phsUser.'\zs\.\{-}\ze'.pheUser) ! let placeHolderEmpty = !strlen(template) ! ! " This movement command selects the placeholder text. In the forward mode, ! " we select left-right, otherwise right-left. ! if a:direction =~ 'b' ! " If we are going in the backward direction, make the selection from ! " right to left so that a backward search for phsUser doesnt get us ! " back to the same placeholder. ! let movement = "\<C-\>\<C-N>:".position."\<CR>" ! \ . "/\\V".pheUser."/e\<CR>" ! \ . "v?\\V".phsUser."?b\<CR>" else ! let movement = "\<C-\>\<C-N>:".position."\<CR>v/\\V".pheUser."/e\<CR>" ! endif ! ! if placeHolderEmpty && g:Imap_DeleteEmptyPlaceHolders ! " delete the empty placeholder into the blackhole. ! return movement."\"_c\<C-o>:".s:RemoveLastHistoryItem."\<CR>" ! ! else ! return movement."\<C-\>\<C-N>:".s:RemoveLastHistoryItem."\<CR>gv\<C-g>" endif + endfunction + " }}} + " Maps for IMAP_Jumpfunc {{{ " map only if there is no mapping already. allows for user customization. if !hasmapto('IMAP_Jumpfunc') ! inoremap <C-J> <c-r>=IMAP_Jumpfunc('', 0)<CR> ! inoremap <C-K> <c-r>=IMAP_Jumpfunc('b', 0)<CR> nmap <C-J> i<C-J> + nmap <C-K> i<C-K> + if exists('g:Imap_StickyPlaceHolders') && g:Imap_StickyPlaceHolders + vmap <C-J> <C-\><C-N>i<C-J> + vmap <C-K> <C-\><C-N>i<C-K> + else + vmap <C-J> <Del>i<C-J> + vmap <C-K> <Del>i<C-K> + endif end " }}} |