2

I edit a lot of Docbook files. <screen/> blocks are normally unindented in Docbook source code. How do I tell Vim to shift the <screen> tag to the very left when entering such tag? After closing that block with </screen>, it would be nice to continue indenting as it was before the starting <screen>. I use:

filetype plugin indent on
set smartindent
1

1 Answer 1

0

The filetype indentation rules (filetype plugin indent on) should take precedence over whatever set smartindent defines.

Is your filetype getting recognized? What does set filetype? return?

I see in $VIMRUNTIME/filetype.vim that .sgm and .sgml files are either recognized as sgml, sgmllnx, or docbk filetypes. It depends on the text on the first line.

" SGML
au BufNewFile,BufRead *.sgm,*.sgml
    \ if getline(1).getline(2).getline(3).getline(4).getline(5) =~? 'linuxdoc' |
    \   setf sgmllnx |
    \ elseif getline(1) =~ '<!DOCTYPE.*DocBook' || getline(2) =~ '<!DOCTYPE.*DocBook' |
    \   let b:docbk_type = "sgml" |
    \   let b:docbk_ver = 4 |
    \   setf docbk |
    \ else |
    \   setf sgml |
    \ endif

There is a $VIMRUNTIME/indent/docbk.vim file (which follows xml rules) but there's not a $VIMRUNTIME/indent/sgml.vim or $VIMRUNTIME/indent/sgmllnx.vim file.

If the filetype is getting set by Vim to sgml or sgmllnx, then the smartindent indentation rules will apply. If the filetype is getting set by Vim to docbk, then the rules in $VIMRUNTIME/indent/docbk.vim should apply. The docbk.vim indentation rules follow xml rules, so it's very smart about indenting by tags.

5
  • Thanks for the answer Chris! The files I edit are correctly recognized as type docbk. It means that $VIMRUNTIME/indent/xml.vim is used. Now, how to modify it to not indent the <screen/> tag? Nov 19, 2021 at 14:55
  • I looked into the distribution xnl.vim file and the function XmlIndentGet seems to be responsible for returning the size if the indent for the new line. If i get it correctly, Vim is calculating indent for a new line only (i.e. after ENTER is hit, for exampl). I wonder if it can change previous line as well given I hit ENTER after the <scree>' tag. Maybe modifying the indent file is not a solution at all, but just creating a function that checks whether a type <screen>` and the shifts that line :left?!? Nov 19, 2021 at 15:14
  • 1
    @TomášBažant What happens if you let b:xml_indent_open = '.\{-}<[/]\@!\(screen\)\@!' and let b:xml_indent_close = '.\{-}</\(screen\)\@!' in a docbk buffer before loading $VIMRUNTIME/indent/docbk.vim?
    – D. Ben Knoble
    Nov 19, 2021 at 17:01
  • 1
    e.g., I think you can put those two lines in ~/.vim/indent/docbk.vim? :help ftplugin-overrule (1) suggests it will be loaded before the one in $VIMRUNTIME, so that should do it.
    – D. Ben Knoble
    Nov 19, 2021 at 17:03
  • Thanks for the advice. Unfortunately, <screen> behaves as a normal XML tag, indented based on the previous tag's indent. Nov 20, 2021 at 17:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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