Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if Vim is running in restricted mode?

Tags:

vim

... 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
like image 570
UncleZeiv Avatar asked May 22 '13 11:05

UncleZeiv


People also ask

What is Vim restricted mode?

If a leading r is prepended to any of the names, vim enters "restricted" mode, where certain actions are disabled. vim has a large number of command-line options. The most useful are described here: -c command. Execute command upon startup.

What is RVIM?

DESCRIPTION. Vim is a text editor that is upwards compatible to Vi. It can be used to edit all kinds of plain text. It is especially useful for editing programs.


2 Answers

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:')
like image 143
Ingo Karkat Avatar answered Oct 05 '22 22:10

Ingo Karkat


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

like image 23
Kent Avatar answered Oct 06 '22 00:10

Kent