Vim Tips Wiki
No edit summary
 
(Change <tt> to <code>, perhaps also minor tweak.)
(13 intermediate revisions by 10 users not shown)
Line 1: Line 1:
  +
{{TipImported
{{review}}
 
{{Tip
 
 
|id=1213
 
|id=1213
  +
|previous=1206
|title=better indent support for php with html
 
  +
|next=1215
|created=April 22, 2006 10:19
+
|created=2006
 
|complexity=advanced
 
|complexity=advanced
|author=simple patch for php/html
+
|author=[[User:Heptite|Heptite]]
 
|version=5.7
 
|version=5.7
 
|rating=37/15
 
|rating=37/15
  +
|category1=HTML
|text=
 
  +
|category2=Indenting
1) Append the indent/html.vim to indent/php.vim
 
  +
|category3=PHP
 
(from if exists('g:html_indent_tags'), and everything below)
 
 
 
 
2) Create a function in php.vim
 
 
 
 
function! GetPhpHtmlIndent(lnum)
 
 
let html_ind = HtmlIndentGet(a:lnum)
 
 
let php_ind = GetPhpIndent()
 
 
" priority one for php indent script
 
 
if php_ind &gt; -1
 
 
return php_ind
 
 
endif
 
 
if html_ind &gt; -1
 
 
return html_ind
 
 
endif
 
 
return -1
 
 
 
 
endfunction
 
 
 
 
 
 
3) set setlocal indentexpr=GetPhpHtmlIndent(v:lnum) in php.vim
 
 
 
 
4) Search for ' [-- special handling for &lt;pre&gt;: no indenting --]' in php.vim and append the following:
 
 
 
 
" [-- special handling for &lt;pre&gt;: no indenting --]
 
 
if getline(a:num) =~ "^&lt;?" &amp;&amp; (0&lt; searchpair('&lt;?', '', '?&gt;', 'nWb')
 
 
\ || 0 &lt; searchpair('&lt;?', '', '?&gt;', 'nW'))
 
 
" we're in a line with &lt;/pre&gt; or inside &lt;pre&gt; ... &lt;/pre&gt;
 
 
return -1
 
 
endif
 
 
 
 
 
 
5) some more improvement: add: ',&gt;' to the indentkeys in php.vim
 
 
 
 
Probably some bugs, but MUCH better than before! Please improve.
 
 
 
 
 
 
 
 
 
 
}}
 
}}
  +
This script allows you to indent HTML sections in PHP files. Create file <code>~/.vim/indent/php.vim</code> (<code>$HOME/vimfiles/indent/php.vim</code> on Windows systems) containing the following:
   
  +
<pre>
== Comments ==
 
  +
" Better indent support for PHP by making it possible to indent HTML sections
made by Jens Berlips : jens_[at]_berlips_[nospam]_com
 
  +
" as well.
 
  +
if exists("b:did_indent")
'''Anonymous'''
 
  +
finish
, April 22, 2006 10:28
 
 
endif
----
 
get php.vim here:
 
   
  +
" This script pulls in the default indent/php.vim with the :runtime command
http://www.berlips.com/vim/php.vim
 
  +
" which could re-run this script recursively unless we catch that:
  +
if exists('s:doing_indent_inits')
  +
finish
 
endif
  +
let s:doing_indent_inits = 1
  +
runtime! indent/html.vim
  +
unlet b:did_indent
  +
runtime! indent/php.vim
  +
unlet s:doing_indent_inits
   
 
function! GetPhpHtmlIndent(lnum)
and you dont have to make any changes =)
 
  +
if exists('*HtmlIndent')
  +
let html_ind = HtmlIndent()
  +
else
 
let html_ind = HtmlIndentGet(a:lnum)
  +
endif
 
let php_ind = GetPhpIndent()
 
" priority one for php indent script
 
if php_ind > -1
 
return php_ind
  +
endif
 
if html_ind > -1
 
if getline(a:num) =~ "^<?" && (0< searchpair('<?', '', '?>', 'nWb')
 
\ || 0 < searchpair('<?', '', '?>', 'nW'))
 
return -1
  +
endif
 
return html_ind
  +
endif
 
return -1
 
endfunction
   
 
setlocal indentexpr=GetPhpHtmlIndent(v:lnum)
'''Anonymous'''
 
  +
setlocal indentkeys+=<>>
, May 5, 2006 2:10
 
  +
</pre>
----
 
This tip rocks. I used the file supplied at http://www.berlips.com/vim/php.vim and after running dos2unux on it, it worked like a charm. Now if only support could be added for inline SQL statements, I'll never have to &lt;tab&gt; again!
 
   
 
==Comments==
kirchner[at]nospam[dot]alum[dot]mit[dot]edu
 
  +
I personally think it's more straightforward to just :set ft=html while you're editing HTML blocks.
, June 20, 2006 6:41
 
----
 
<!-- parsed by vimtips.py in 0.494719 seconds-->
 

Revision as of 06:13, 13 July 2012

Tip 1213 Printable Monobook Previous Next

created 2006 · complexity advanced · author Heptite · version 5.7


This script allows you to indent HTML sections in PHP files. Create file ~/.vim/indent/php.vim ($HOME/vimfiles/indent/php.vim on Windows systems) containing the following:

" Better indent support for PHP by making it possible to indent HTML sections
" as well.
if exists("b:did_indent")
  finish
endif

" This script pulls in the default indent/php.vim with the :runtime command
" which could re-run this script recursively unless we catch that:
if exists('s:doing_indent_inits')
  finish
endif
let s:doing_indent_inits = 1
runtime! indent/html.vim
unlet b:did_indent
runtime! indent/php.vim
unlet s:doing_indent_inits

function! GetPhpHtmlIndent(lnum)
  if exists('*HtmlIndent')
    let html_ind = HtmlIndent()
  else
    let html_ind = HtmlIndentGet(a:lnum)
  endif
  let php_ind = GetPhpIndent()
  " priority one for php indent script
  if php_ind > -1
    return php_ind
  endif
  if html_ind > -1
    if getline(a:num) =~ "^<?" && (0< searchpair('<?', '', '?>', 'nWb')
          \ || 0 < searchpair('<?', '', '?>', 'nW'))
      return -1
    endif
    return html_ind
  endif
  return -1
endfunction

setlocal indentexpr=GetPhpHtmlIndent(v:lnum)
setlocal indentkeys+=<>>

Comments

I personally think it's more straightforward to just :set ft=html while you're editing HTML blocks.