UncleZeiv
UncleZeiv

Reputation: 18488

How to detect if Vim is running in restricted mode?

... or in any mode for that matter.

I just want to prevent some extensions from loading when that is the case, something like:

if ! currentmode('restricted')
     Bundle('some-extension')
endif

Upvotes: 5

Views: 1600

Answers (2)

Kent
Kent

Reputation: 195269

I am not sure if this is a good idea:

restricted-mode disabled external commands (also some related functions). If we call external command or some certain functions in a rvim, we get Error E145.

So maybe you could just call some dummy external command via system(), then catch the Exception E145. to distinguish if it is in restricted mode. e.g.

try
    call system("echo x") "or even call system("")
catch /E145/
"your codes
endtry

hope it helps

Upvotes: 2

Ingo Karkat
Ingo Karkat

Reputation: 172768

You're right; a special variable like v:vimmode would be helpful, but I don't think such a thing currently exists. Why not raise this on the vim_dev mailing list?!

In the meantime, you have to detect the mode through the result of invoking something that is forbidden in restricted mode. My best idea that is the least intrusive on success is invoking writefile() with an empty file name:

silent! call writefile([], '')
" In restricted mode, this fails with E145: Shell commands not allowed in rvim
" In non-restricted mode, this fails with E482: Can't create file <empty>
let isRestricted = (v:errmsg =~# '^E145:')

Upvotes: 5

Related Questions