1

Say that I have a compiler/foo.vim compiler file. Next, I want to define a command that changes compiler, runs it and changes the compiler back to the original.

For example:

command! MyCommand { var previous_compiler = ???? # save info about the current compiler. How?! compiler foo silent make execute "compiler " .. previous_compiler copen } 

2 Answers 2

2

I can self-answer my own question. It is enough to use b:current_compiler variable.

command! MyCommand { var previous_compiler = "" if exists("b:current_compiler") previous_compiler = b:current_compiler endif compiler erlang silent make if strlen(previous_compiler) > 0 execute "compiler " .. previous_compiler endif copen } 
1
  • 2
    strlen > 0: !empty(previous_compiler). Also execute 'compiler' previous_compiler should suffice, though that may have been changed for vim9script. Commented Jan 3, 2024 at 21:13
-1

Here is a very simple compiler script taken from Vim's own runtime ($VIMRUNTIME/compiler/eslint.vim):

" Vim compiler file " Compiler: ESLint for JavaScript " Maintainer: Romain Lafourcade <XXX@YYY> " Last Change: 2020 August 20 if exists("current_compiler") finish endif let current_compiler = "eslint" if exists(":CompilerSet") != 2 command -nargs=* CompilerSet setlocal <args> endif CompilerSet makeprg=npx\ eslint\ --format\ compact CompilerSet errorformat=%f:\ line\ %l\\,\ col\ %c\\,\ %m,%-G%.%# 

Take all the time you need to read that snippet carefully, line by line.

Now, how would you save the current compiler?.

1
  • By reading at the script above I would var previous_compiler = current_compiler but that won't work since current_compiler is limited to the eslint.vim scope (it is enough to :echo current_compiler from any other buffer to get E121). However, using b:current_compiler seems to work, as I answered below (we posted an answer at the same time). Commented Jan 3, 2024 at 18:40

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.