3

When I open a PHP file in Vim I want Vim to provide SQL assistance as well as all the PHP assistance.
From :h sql-completion-filetypes:

This can be enabled easily with the following steps (assuming a Perl file):

1.  :e test.pl  
2.  :set filetype=sql  
3.  :set ft=perl

I would like to automate this: So far I have tried

Appending the following commands to ~/.vim/after/ftplugins/php.vim

:set filetype=sql  
:set ft=php

This throws errors like

Error detected while processing FileType Auto commands for "*":       
E218: autocommand nesting too deep

Which I'm assuming is happening because when whenever the set ft=php command is run - it then re-triggers the ~/.vim/after/ftplugins/php.vim script which then re-triggers the set ft=php statement.
Is it possible to automate running these commands :set filetype=sql, :set ft=php when opening a php file?

  • setf sql.php to set multiple filetypes on a file. To automate this: autocmd FileType php setf sql.php – 65-e9-7e-17 Dec 24 '15 at 1:37
  • thanks, its a good idea, but unfortunately it doesn't work – the_velour_fog Dec 24 '15 at 1:53
3

On my Vim (7.4.944), the SQL filetype plugin performs the following check:

if !exists("*SQL_SetType")
    " NOTE: You cannot use function! since this file can be
    " sourced from within this function.  That will result in
    " an error reported by Vim.

That seems to be a good way to load sql.vim once. So, in my ~/.vim/ftplugin/php.vim, I added:

if !exists("*SQL_SetType")
    set ft=sql
    set ft=php
endif

I don't use SQL in PHP, but AFAICT, the SQL settings were loaded.

  • thanks muru, it works! so basically this code snippet says: if SQL_SetType hasn't been set that means sql.vim hasn't run - so run the set ft=sql set ft=php commands - the set ft=php will cause your snippet to re-run - but your if condition will protect against the recursion (kind of like putting up shields to protect against disruptor fire - I guess...) – the_velour_fog Dec 24 '15 at 5:18
  • @the_velour_fog yep. If you're familiar with C, it's like an include guard. Note also that I used ~/.vim/ftplugin, and not after/ftplugin, since I want this to run as early as possible, so that a minimum of PHP-related code is called twice. – muru Dec 24 '15 at 5:39
  • Thanks for the explanation, I was wondering why you did that. Yes I am familiar with the programming pattern of include guards, but annoyingly I don't know vimscript, so I couldn't write something like your code snippet! – the_velour_fog Dec 24 '15 at 5:45
  • @the_velour_fog if it helps, I didn't write it. I just lifted it from sql.vim. :P – muru Dec 24 '15 at 5:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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