chrisbra / NrrwRgn

A Narrow Region Plugin for vim (like Emacs Narrow Region)
http://www.vim.org/scripts/script.php?script_id=3075
674 stars 25 forks source link

Feature Request: Support modeline like functionality #51

Closed fourjay closed 9 years ago

fourjay commented 9 years ago

Having the ability to indicate filetype in the narrow region itself would be quite helpful. My usage pattern: 1) Isolate an embedded bit of code in a different language (SQL for example) 2) Run NR on the section 3) Manually set filetype

I was curious if I could add a modeline (which did not work of course). But I'd definitely use this facility if was offered. Among other things, the modeline becomes a form of documentation.

I see g:nrrw_custom_options, but this is to broad. Not every file I work with has only one embedded language, and maintaining a directory exrc file is an impediment.

Thanks for considering this :-)

fourjay commented 9 years ago

Here's a reasonable workaround, map a NRX to a repetition of the NR mapping with an additional pipe char plus the set ft statement. This comes close enough to my request to scratch the itch (still seems like some version of it would be useful)

fourjay commented 9 years ago

I've come up with a fairly simple approach that seems like it might be a good candidate for inclusion in the main release, altering (or in my case creating a new function) that takes a (prompted) filetype argument. FWIW, I think this plugin/approach is the strongest way to support multi-language files. Among other things, it allows the full power of ftplugins, not just syntax highlighting. With this approach we get snippets, custom FT plugins, autocomplete etc...

Thanks

     function! s:NR_ft_complete(...)                                                                                                                                               
     " this could clearly be smarter :-) 
     return          "sql\n" .                                                                                                                                                 
                  \   "html\n" .                                                                                                                                                
                  \   "css\n" .                                                                                                                                                 
                  \   "awk\n" .                                                                                                                                                 
                  \   "ldif\n" .                                                                                                                                                
                  \   "mail\n" .                                                                                                                                                
                  \   "csv\n" .                                                                                                                                                 
                  \   "perl\n" .                                                                                                                                                
                  \   "sh\n" .                                                                                                                                                  
                  \   "javascript\n"                                                                                                                                            

  endfunction

     command! -nargs=* -bang -range -complete=custom,<SID>NR_ft_complete␣                                                                                                          
              \ NN                                                                                                                                                              
              \ :<line1>,<line2> call nrrwrgn#NrrwRgn('',<q-bang> )                                                                                                             
              \ | set filetype=<args>                                                                                                                                           
chrisbra commented 9 years ago

Hi Josef!

On Mi, 30 Sep 2015, Josef Fortier wrote:

I've come up with a fairly simple approach that seems like it might be a good candidate for inclusion in the main release, altering (or in my case creating a new function) that takes a (prompted) filetype argument. FWIW, I think this plugin/approach is the strongest way to support multi-language files. Among other things, it allows the full power of ftplugins, not just syntax highlighting, snippets etc.

Thanks

    command! -nargs=* -bang -range -complete=custom,<SID>NR_ft_complete␣                                                                                                          
              \ NN                                                                                                                                                              
              \ :<line1>,<line2> call nrrwrgn#NrrwRgn('',<q-bang> )                                                                                                             
              \ | set filetype=<args>                                                                                                                                           

  function NrrwRegionGuessFiletype()                                                                                                                                            
      let firstlines = getline(1) . getline(2)                                                                                                                                  
      let explicit_syntax =␣                                                                                                                                                    
                  \matchstr(␣                                                                                                                                                   
                  \  firstlines,␣                                                                                                                                               
                  \  '\v(SQL|HTML|AWK|CSS|MAIL|JAVASCRIPT)'                                                                                                                     
                  \ )                                                                                                                                                           
      if explicit_syntax != ''                                                                                                                                                  
          execute "set filetype=" . tolower(explicit_syntax)                                                                                                                    
      elseif firstlines =~ '\v(UPDATE|INSERT|SELECT)'                                                                                                                           
          set filetype=sql                                                                                                                                                      
      elseif firstlines =~ 'BEGIN {'                                                                                                                                            
          set filetype=awk                                                                                                                                                      
      elseif firstlines =~ '\v\<.*\>'                                                                                                                                           
          set filetype=html                                                                                                                                                     
      endif                                                                                                                                                                     
  endfunction

And you want this included? or just documented as an example on how to use it?

Liebe Grüße

Christian

Nein, das ist keine SchreIBMaschine!

fourjay commented 9 years ago

Either one would make me happy :-) FWIW I deliberately made the mapping default to the old behavior so that existing behavior would not change, but instead would be augmented (with user friendly, at least to me, tab completion).

chrisbra commented 9 years ago

I have included this for now in the documentation. If this turns out to be really useful, I'll put it into the plugin later.

Thanks for contributing.

bpj commented 4 years ago

Sorry for shaking a closed issue, but I think this properly goes here rather than in a separate/new issue.

In my experience just setting 'filetype' is sometimes not enough, since other options set in the "parent" buffer which are not affected by setting 'filetype' may affect the behavior of the "child" buffer. In particular I often have problems with 'indentexpr'. Obviously I can — and do — just empty 'indentexpr' but that is often not the desired thing to do: it may be that the "child" filetype normally sets its own 'indentexpr'. It would be great if there were some way to run all the autocommands etc which are normally run when a new buffer of the "child" filetype is loaded.

chrisbra commented 4 years ago

Hm, that is nearly impossible. Setting the filetype should trigger all the related and necessary autocommands (like indenting etc). But there can be of course autocommands, that run on filenames or similar, There is no way to capture those and apply them to the narrowed buffer I believe. Not sure how to provide a better option here.