FileType conditional

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

FileType conditional

Justin Randall-2
With FileType detection already enabled.  I want to run a command depending on if the
FileType is c or cpp.  For example:

If the FileType is c or cpp, then
     let g:C_LoadMenus = "yes"
else
     let g:C_LoadMenus = "no"
endif

Does anybody know a good way to implement this logic in my .vimrc?



       
               
______________________________________________________
Click here to donate to the Hurricane Katrina relief effort.
http://store.yahoo.com/redcross-donate3/
Reply | Threaded
Open this post in threaded view
|

Re: FileType conditional

A.J.Mechelynck
----- Original Message -----
From: "Justin Randall" <[hidden email]>
To: <[hidden email]>
Sent: Friday, September 09, 2005 3:43 PM
Subject: FileType conditional


> With FileType detection already enabled.  I want to run a command
> depending on if the
> FileType is c or cpp.  For example:
>
> If the FileType is c or cpp, then
>     let g:C_LoadMenus = "yes"
> else
>     let g:C_LoadMenus = "no"
> endif
>
> Does anybody know a good way to implement this logic in my .vimrc?

    if &ft == "c"
        " 'filetype' is c
    elseif &ft == "cpp"
        " it is cpp
    else
        " it is something else
    endif

see ":help expression-syntax" and scroll down to where there is |expr9| in
the left margin.

Best regards,
Tony.


Reply | Threaded
Open this post in threaded view
|

Re: FileType conditional

A.J.Mechelynck
----- Original Message -----
From: "Tony Mechelynck" <[hidden email]>
To: "Justin Randall" <[hidden email]>; <[hidden email]>
Sent: Friday, September 09, 2005 3:51 PM
Subject: Re: FileType conditional


> ----- Original Message -----
> From: "Justin Randall" <[hidden email]>
> To: <[hidden email]>
> Sent: Friday, September 09, 2005 3:43 PM
> Subject: FileType conditional
>
>
>> With FileType detection already enabled.  I want to run a command
>> depending on if the
>> FileType is c or cpp.  For example:
>>
>> If the FileType is c or cpp, then
>>     let g:C_LoadMenus = "yes"
>> else
>>     let g:C_LoadMenus = "no"
>> endif
>>
>> Does anybody know a good way to implement this logic in my .vimrc?
>
>    if &ft == "c"
>        " 'filetype' is c
>    elseif &ft == "cpp"
>        " it is cpp
>    else
>        " it is something else
>    endif
>
> see ":help expression-syntax" and scroll down to where there is |expr9| in
> the left margin.
>
> Best regards,
> Tony.

P.S.

    let g:C_LoadMenus = ((&ft == "c") || (&ft == "cpp"))?"yes":"no"

using the ternary <condition>?<value if true>:<value if false> operator


If you can make use of 1 and 0 rather than "yes" and "no" you can even do

    let g:C_LoadMenus = (&ft == "c") || (&ft == "cpp")

and test (elsewhere) on "if g:C_LoadMenus" rather than on "if g:C_LoadMenus
== 'yes'"

Best regards,
Tony.


Reply | Threaded
Open this post in threaded view
|

Re: FileType conditional

Tim Chase-2
In reply to this post by Justin Randall-2
> With FileType detection already enabled. I want to run a
> command depending on if the  FileType is c or cpp.
> For example:
> If the FileType is c or cpp, then
>      let g:C_LoadMenus = "yes"
> else
>      let g:C_LoadMenus = "no"
> endif

Well, starting with the info in

        :help new-filetype

you should be able to do something like assuming g:C_LoadMenus is
no unless evidenced otherwise, as set by something like

augroup filetypedetect
   au! BufRead,BufNewFile *.c,*.cpp let g:C_LoadMenus = "yes"
augroup END

in your ~/.vim/filetype.vim file.

Note that this is doing some peculiar things...setting a global
variable, even though it's being decided on a single file type.
Your original example, if you opened a C/C++ file would set the
C_LoadMenus, but then opening any other file type in another
window/buffer (say, a PHP file) would unset this, even though
your C file is still open.  By assuming it's not set until you
set it, you bypass this irregularity.

Additionally, this doesn't actually perform any sort of action
(other than setting a global variable).  This might impact
mappings or scripts later run.  HOWEVER...if scripts in your
vimrc are making decisions based on this value, the filetype
isn't set until after the vimrc is loaded.

Just a few thoughts on the matter,

-tim





Reply | Threaded
Open this post in threaded view
|

Re: FileType conditional

Tim Chase-2
In reply to this post by Justin Randall-2
> P.S. The variable g:C_LoadMenus is set to be on "yes" by
> default and it seems that once you set it, then that's it.

What sort of situation would trigger it to be "no"?  If any other
filetype is loaded?  Even if you currently have a C/C++ file loaded?

My thoughts would be to default LoadMenus to "no", and then only
set them when they're loaded.

Perhaps if you detail what you're trying to do, and in what
situations you'd like to do it (when you want it to trigger such
an evaluation), other solutions might become apparent.

-tim





Reply | Threaded
Open this post in threaded view
|

Re: FileType conditional

Justin Randall-2
Yes I'm trying to load the menu only when I have a C/C++ file loaded in any buffer.  It
defaults to "yes", but I want it defaulted to "no" unless there is a C/C++ file loaded.
I will try to set the default to be off, and let you know if that works.  By the way,
this is for the c.vim C/C++ IDE plugin located here
http://www.vim.org/scripts/script.php?script_id=213
I have tried emailing the author Fritz Mehner for suggestions, but have not heard back
yet.


--- Tim Chase <[hidden email]> wrote:

> > P.S. The variable g:C_LoadMenus is set to be on "yes" by
> > default and it seems that once you set it, then that's it.
>
> What sort of situation would trigger it to be "no"?  If any other
> filetype is loaded?  Even if you currently have a C/C++ file loaded?
>
> My thoughts would be to default LoadMenus to "no", and then only
> set them when they're loaded.
>
> Perhaps if you detail what you're trying to do, and in what
> situations you'd like to do it (when you want it to trigger such
> an evaluation), other solutions might become apparent.
>
> -tim
>
>
>
>
>
>



       
               
______________________________________________________
Click here to donate to the Hurricane Katrina relief effort.
http://store.yahoo.com/redcross-donate3/
Reply | Threaded
Open this post in threaded view
|

Re: FileType conditional

A.J.Mechelynck
----- Original Message -----
From: "Justin Randall" <[hidden email]>
To: <[hidden email]>
Sent: Friday, September 09, 2005 6:52 PM
Subject: Re: FileType conditional


> Yes I'm trying to load the menu only when I have a C/C++ file loaded in
> any buffer.  It
> defaults to "yes", but I want it defaulted to "no" unless there is a C/C++
> file loaded. ()
> I will try to set the default to be off, and let you know if that works.
> By the way,
> this is for the c.vim C/C++ IDE plugin located here
> http://www.vim.org/scripts/script.php?script_id=213
> I have tried emailing the author Fritz Mehner for suggestions, but have
> not heard back
> yet.

Define a function which loads your menu:

    function! s:LoadCMenu()
        " do your stuff
        ...
        ...
    endfunction

then invoke it when a file is seen to be c or cpp:

    autocmd FileType c,cpp call s:LoadCMenu()

For a filetype-plugin there's an alternate possibility:

---- ~/.vim/after/ftplugin/c.vim ---- (for Unix)
---- ~/vimfiles/after/ftplugin/c.vim ---- (for Windows)
    source /foo/bar/baz/filename.vim



IIRC, cpp causes both cpp and c plugins to be loaded.


Best regards,
Tony.


Reply | Threaded
Open this post in threaded view
|

Re: FileType conditional

Justin Randall-2
Thanks Tony that did the trick!

--- Tony Mechelynck <[hidden email]> wrote:

> ----- Original Message -----
> From: "Justin Randall" <[hidden email]>
> To: <[hidden email]>
> Sent: Friday, September 09, 2005 6:52 PM
> Subject: Re: FileType conditional
>
>
> > Yes I'm trying to load the menu only when I have a C/C++ file loaded in
> > any buffer.  It
> > defaults to "yes", but I want it defaulted to "no" unless there is a C/C++
> > file loaded. ()
> > I will try to set the default to be off, and let you know if that works.
> > By the way,
> > this is for the c.vim C/C++ IDE plugin located here
> > http://www.vim.org/scripts/script.php?script_id=213
> > I have tried emailing the author Fritz Mehner for suggestions, but have
> > not heard back
> > yet.
>
> Define a function which loads your menu:
>
>     function! s:LoadCMenu()
>         " do your stuff
>         ...
>         ...
>     endfunction
>
> then invoke it when a file is seen to be c or cpp:
>
>     autocmd FileType c,cpp call s:LoadCMenu()
>
> For a filetype-plugin there's an alternate possibility:
>
> ---- ~/.vim/after/ftplugin/c.vim ---- (for Unix)
> ---- ~/vimfiles/after/ftplugin/c.vim ---- (for Windows)
>     source /foo/bar/baz/filename.vim
>
>
>
> IIRC, cpp causes both cpp and c plugins to be loaded.
>
>
> Best regards,
> Tony.
>
>
>



       
               
______________________________________________________
Click here to donate to the Hurricane Katrina relief effort.
http://store.yahoo.com/redcross-donate3/