Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I've set up my mappings as follows:

"Set up mappings for LaTeX files 
let b:myEngine=0
let g:myEngineList=["pdflatex","texexec"] "pdflatex or context
function! ToggleEngine()
  if b:myEngine == "pdflatex"
        let b:myEngine = "context"
        execute "imap <F5> :! pdflatex %;<CR>"
        execute "map ;new \begin{document}<CR><CR>\end{document}"
  else
        let b:myEngine = "pdflatex"
        execute "imap <F5> :! context %;<CR>"
        execute "map ;new \starttext<CR><CR>\stoptext"
  fi
  echo "compilation engine:" g:myEngineList[b:myEngine]
endfunction

However this isn't behaving at all how I expect; it refuses to remap the keys. Initially no keys are mapped, and once they're mapped, they stay mapped. How do I fix this?

I call ToggleEngine() as follows: nmap <silent> <F7> :call ToggleSpell()<CR>

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

when do you call ToggleEngine()?

Suggestion:

"Set up mappings for LaTeX files 
let g:myEngineList=["pdflatex","texexec"] "pdflatex or context
function! ToggleEngine()
  if b:myEngine == "pdflatex"
     let b:myEngine = "context"
  else
     let b:myEngine = "pdflatex"
  fi
  execute 'inoremap <F5> :!' . b:myEngine . ' %<cr>'
  echo "spell checking language: " . b:myEngine
endfunction
share|improve this answer
    
That is a good solution, except a part of the question I failed to add is that that isn't the only mapping, there are other mappings which don't depend on the name that fail to change. I'm trying out inoremap instead of imap. We'll see. –  EricR Nov 22 '10 at 16:35
    
Turns out it was the difference between imap and inoremap. Thank you! –  EricR Nov 22 '10 at 16:42
add comment

Based on lh-vim-lib, here is another solution that defines a menu and a command (adding a mapping becomes as simple as calling the command from the mapping).

In your case, it would look like:

function! s:getSNR()
  if !exists("s:SNR")
    let s:SNR=matchstr(expand("<sfile>"), "<SNR>\\d\\+_\\zegetSNR$")
  endif
  return s:SNR 
endfunction


function! s:UsePdfLaTeX()
  set makeprg=pdflatex\ $*
  inoremap <buffer> <localleader>new \begin{document}<CR><CR>\end{document}
endfunction

function! s:UseContext()
  set makeprg=context\ $*
  inoremap <buffer> <localleader>new \startext<CR><CR>\stoptext
endfunction

let s:Data = {
      \ "variable": "myengine",
      \ "values": [ 'pdflatex', 'context' ],
      \ "actions": [ function(s:getSNR()."UsePdfLaTeX"), function(s:getSNR()."UseContext") ],
      \ "menu": { "priority": '42.42', "name": '&LaTeX.&Engine'}
      \}
call lh#menu#def_toggle_item(s:Data)
nnoremap <silent> <F7> :Toggle LaTeXEngine<cr>

Note: It can be extended to support as many alternatives as you wish.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.