Coder Social home page Coder Social logo

valloric / matchtagalways Goto Github PK

View Code? Open in Web Editor NEW
539.0 10.0 24.0 494 KB

A Vim plugin that always highlights the enclosing html/xml tags

Home Page: http://valloric.github.com/MatchTagAlways/

License: GNU General Public License v3.0

Python 71.84% HTML 9.14% Vim Script 19.02%
vim html syntax-highlighting xml

matchtagalways's Introduction

Always highlight enclosing tags

Build Status

The MatchTagAlways.vim (MTA) plug-in for the Vim text editor always highlights the XML/HTML tags that enclose your cursor location. It's probably easiest to describe with a screenshot:

MatchTagAlways screen shot

It even works with HTML templating languages like Jinja or HTML5 use-cases like unclosed tags. It's pretty smart and should do the right thing in most cases. If it doesn't, report the problem on the issue tracker!

Installation

Use Vundle to install the plugin. You are using Vundle to manage your Vim plugins, right? Pathogen works fine too (but I recommend Vundle).

Note that the plugin requires that your copy of Vim is compiled with Python support. You can check for this with :echo has('python') in Vim. If the output is 1, then you have Python support.

After installation you should be done. The plugin should be plug & play. It will automatically turn itself on for HTML, XML and a few other HTML-like filetypes. You can also explicitly turn it on for other filetypes too (more details in the Options section).

Options

All options have reasonable defaults so if the plug-in works after installation you don't need to change any options. These options can be configured in your [vimrc script] vimrc by including a line like this:

let g:mta_use_matchparen_group = 1

Note that after changing an option in your [vimrc script] vimrc you have to restart Vim for the changes to take effect.

The g:mta_filetypes option

This option holds all the filetypes for which this plugin will try to find and highlight enclosing tags. It's a Vim dictionary with keys being Vim filetypes. The values set for those keys don't matter and are not checked, the only thing that matters is that a key is present in the dictionary (VimL has no sets).

You can find out what the current file's filetype is in Vim with :set ft?. Don't forget that question mark at the end!

Default: { 'html' : 1, 'xhtml' : 1, 'xml' : 1, 'jinja' : 1 }

let g:mta_filetypes = {
    \ 'html' : 1,
    \ 'xhtml' : 1,
    \ 'xml' : 1,
    \ 'jinja' : 1,
    \}

The g:mta_use_matchparen_group option

When set to 1, forces the use of the MatchParen syntax group. This is the same group that Vim uses to highlight parens, braces etc. This option is useful for people who want to use the same highlight color for both constructs.

When set to 0, MTA will use a custom MatchTag syntax group with a default highlight color. See the g:mta_set_default_matchtag_color option for instructions on how to change that color.

By default, this option is set to 1 because this makes it very unlikely that your colorscheme will conflict with the default colors used for the MatchTag group. It's the safe choice. Feel free to toggle this option to 0 (the author uses it like this).

Default: 1

let g:mta_use_matchparen_group = 1

The g:mta_set_default_matchtag_color option

This option only makes sense when g:mta_use_matchparen_group is set to 0. When it is, g:mta_set_default_matchtag_color option can be used to prevent MTA from overwriting any color you have set for the MatchTag group.

So, if you want to use a custom color for tag highlighting, have both g:mta_use_matchparen_group and g:mta_set_default_matchtag_color set to 0 and then set a custom color for MatchTag in your vimrc. For example, the following command would set a light green text background and a black foreground color ('foreground' is the text color):

highlight MatchTag ctermfg=black ctermbg=lightgreen guifg=black guibg=lightgreen

See :help highlight for more details on text highlighting commands.

Default: 1

let g:mta_set_default_matchtag_color = 1

Comands

You can use the plugin to also go to the closing tag if this tag is in the visible screen.

MtaJumpToOtherTag

Jumps to the enclosing tag if the tag is visible. If you are on top of an opening tag, it will jump to the closing tag. If you are on the closing tag, it will jump to the opening tag. If you are inside a tag, then it will jump to the closing tag.

Setting a mapping for this command:

nnoremap <leader>% :MtaJumpToOtherTag<cr>

FAQ

I've noticed that sometimes no tags are highlighted. Why?

The plugin only scans the lines that are visible in your window. If an opening tag is visible but the closing tag is not, no tag will be highlighted. This is for performance reasons (ie. what happens if the user opens a 10k HTML file?).

It's also possible that the plugin's parser is just out of ideas on how to extract the enclosing tags out of your text. This should be very rare though.

Contact

If you have questions, bug reports, suggestions, etc. please use the issue tracker. The latest version is available at http://valloric.github.com/MatchTagAlways/.

The author's homepage is http://val.markovic.io.

License

This software is licensed under the GPL v3 license. © 2012 Strahinja Val Markovic <[email protected]>.

matchtagalways's People

Contributors

corpix avatar cosminadrianpopescu avatar dawsbot avatar mcfiredrill avatar pmav99 avatar prurigro avatar raffy-li avatar sharat87 avatar valloric avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

matchtagalways's Issues

cursor jumps sporadically

while typing inside tags cursor appears to jump sporadically and sometimes lag behind typing. this plugin is causing the issue.

issue demo

iterm2+neovim.

Error under python3.6

Hi, after upgrading from Python 3.5 to 3.6 on opening of my Python file I see

Error detected while processing /Users/admin/.vim/bundle/MatchTagAlways/autoload/MatchTagAlways.vim:
line   22:
E887: Sorry, this command is disabled, the Python's site module could not be loaded.

macOS

As I found problem is here:

if has('python')
    " Python 2
    py import sys  # this is line 22
    py import vim
    exe 'python sys.path = sys.path + ["' . s:script_folder_path . '/../python"]'
    py import mta_vim
else
    " Python 3
    py3 import sys
    py3 import vim
    exe 'python3 sys.path = sys.path + ["' . s:script_folder_path . '/../python"]'
    py3 import mta_vim
endif

But why Python 2 if I have Python 3?

mta hangs vim when opening remote files on windows

with mta, vim hangs on opening remote files. after playing with the script, I found that in the autoload script, python module search path is first appended with mta/python folder.
exe 'python sys.path = sys.path + ["' . s:script_folder_path . '/../python"]'

in my case, however, sys.path contains some invalid path originally constructed from the remote path (not sure what did this). and the next 'python import' command becomes very slow. my guess is that python has to go through all the paths in front before looking at the one just added to locate mta_vim and somehow the remote looking invalid path blocks everything.

anyway, I worked around this problem by inserting mta/python in the front to make sure mta_vim is found before python touches the invalid path in sys.path.

Possibly add PHP by default?

Although I agree that this feature may not be what everyone wants, for some people it might be nice. Not like I do a lot of PHP coding (luckily.. why should one ever..) but for now I'll add it in my own vimrc by setting g:mta_filetypes.

Not working under certain conditions?

Ok so 2 conditions

Create a html document, paste this in:

<!DOCTYPE html>
<html>
    <head>
    <meta charset="utf-8">
    <style>


        .style .grouping:first-child {
            border-top: 0;
            padding-top: 0;
        }

        .style .grouping:last-child {
            padding-bottom: 0;
        }

        .style a {
            display: block;
            font-weight: bold;
            font-family: "Helvetica Neue", "Helvetica", Helvetica, Arial, sans-serif;
            text-rendering: optimizeLegibility;
            line-height: 1.1;
            margin-bottom: 14px;
        }


        @media all and (max-width: 720px) {

        html { overflow-y: scroll; }

        body{
            padding: 0;
        }

        .style {
            background: white;
            padding: 5px;
            /* bottom: 0; */
            bottom: 0px;
            box-shadow: -1px 0px 2px rgba(0,0,0,.5);
            border-top-left-radius: 7px;
            border-top-right-radius: 7px;
        }

        style {
        padding-left: 0px; 
        }

          .style {
            position: static;
            width: auto;
            border-width: 0 1px 1px 1px;
            margin-top: -10px;
            overflow: inherit;
          }

          .style {
            margin-top: 30px;
            text-align: center;
            position: static;
          }

          .default .pictures img {
            width: 100%;
          }

          .widget .content {
            left: 20px;
            top: 300px;
            position: static;
          }


        }

    </style>
    </head>
    <body></body>
</html>

So the html / style / head / body tags are not highlighting for me here.

Also, people use js template languages in their html. So something valid like this:

<div>
    <script id="blah-template" type="text/x-handlebars-template">
        <option value="">blah</option>
        {{#if objects.0.domain }}
            {{#each objects}}
                <option value="{{domain.id}}">{{domain.name}}</option>
            {{/each}}
        {{else}}
            {{#each objects}}
                <option value="{{id}}">{{name}}</option>
            {{/each}}
        {{/if}}
    </script>
</div>

Will cause surrounding elements to all of a sudden not highlight.

python/mta_core.py:84: SyntaxWarning: invalid escape sequence '\S'

Python 3.12 complains:

Error detected while processing BufReadPost Autocommands for "*":
Error executing lua callback: /usr/share/nvim/runtime/filetype.lua:24: Error executing lua: /usr/share/nvim/runtime/filetype.lua:25: BufReadPost Autocommands for "*"..FileType Autocommands
for "*"..script /home/marcin/.config/nvim/plugged/MatchTagAlways/autoload/MatchTagAlways.vim[31]..function provider#python3#Call[18]..script /home/marcin/.config/nvim/plugged/MatchTagAlways
/autoload/MatchTagAlways.vim, line 31: Vim(return):/home/marcin/.config/nvim/plugged/MatchTagAlways/autoload/../python/mta_core.py:84: SyntaxWarning: invalid escape sequence '\S'
stack traceback:
        [C]: in function 'nvim_cmd'
        /usr/share/nvim/runtime/filetype.lua:25: in function </usr/share/nvim/runtime/filetype.lua:24>
        [C]: in function 'nvim_buf_call'
        /usr/share/nvim/runtime/filetype.lua:24: in function </usr/share/nvim/runtime/filetype.lua:10>
stack traceback:
        [C]: in function 'nvim_buf_call'
        /usr/share/nvim/runtime/filetype.lua:24: in function </usr/share/nvim/runtime/filetype.lua:10>

Probably string needs to be marked as regexp.

Filetype detection is not working for gsp (groovy server pages)

Hi,
I was trying to use MTA for gsp (groovy server pages) which is similar to jsp. I have used following option but it is not working for gsp. Html and xml are working fine. Can you look into it?
let g:mta_filetypes = {
\ 'html' : 1,
\ 'xhtml' : 1,
\ 'xml' : 1,
\ 'gsp' : 1,
\ 'jinja' : 1,
}

Custom highlight color doesn't work

I tried to set custom highlight color as stated in readme but is simply doesn't work (the default works ok). I use your plugin with Vundle.

My .vimrc :

Plugin 'Valloric/MatchTagAlways'
let g:mta_use_matchparen_group=0
let g:mta_set_default_matchtag_color=0
highlight MatchTag ctermfg=black ctermbg=lightgreen guifg=grey guibg=lightgreen

Effect:

screenshot from 2014-06-15 22 59 25

Default:

screenshot from 2014-06-15 23 00 07

import mta_core

In my env, MatchTagAlways throw error ImportError: No module named mta_core.
I use neobundle to manage my plugins.

Getting a deprecation warning using python 3.7

"lib/object_generators/object_generators.rb" 1L, 1C
Error detected while processing /root/.vim/bundle/MatchTagAlways/autoload/MatchTagAlways.vim:
line   28:
/must>not&exist/foo:1: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses

:echo has('python') => 0
:echo has('python3') => 1

I don't see anyone else talking about this so I reckon I might be doing something wrong?

I compiled vim from source with python3 (not python2) and I pointed it at my python 3.7 directory.

Anything I can do?

tags not highlighted when off screen

If, for example, you are inside a div tag and you can see the closing </div> but you cannot see the opening <div>, then, of course, the tags will not be highlighted. Let's say that now, you press <c-e> to bring the opening <div> into view; the tags are not highlighted until curpos is updated.

Not big deal. Would it be that hard to just highlight tags no matter what? Or does this have something to do with vim's builtin matching/highlighting?

I'm surprised that this is the only plugin for this, honestly. Still nice stuff and thanks.

Matching tags backwards

I noticed that only the closing tags are matched. Is there a way to match the opening tags too? So, when the cursor is on a closing tag, it would highlight the opened tag that it is closing.

XML highlight group?

The MatchTag highlight group works for HTML, but doesn't seem to apply to XML. What highlight group do I need to set the colour of for XML tag matching?

.vimrc

let g:mta_use_matchparen_group = 0
let g:mta_set_default_matchtag_color = 1

In colour scheme:

hi MatchTag ctermfg=11 ctermbg=16 cterm=underline

^ Doesn't apply to XML - it gives some bright blue background while backgruond 16 is dark brown.

No tags are highlighted.

I'm using a vundle install. On opening a Jinja template (mostly html) none of the tags are highlighted. I'd be happy to provide you with any details you require.

I'm running Ubuntu 13.4 and vim 7.3.547. Vim version info lists '-lpython2.7' so I expect the Python support to be fine. I mostly do Python work, and my Python plugins work fine.

Creating a plain html file from scratch seems to work as expected. Some Jinja files work, but others doesn't. Complexity doesn't seem to correlate to what works and what doesn't.

Taking a file that does not work and reducing it to only a few tags does not solve the problem, so I'm wondering if the script stops running or gets into an unforseen state.
Reducing one of those files and then reopening the file makes it work again.

It also does not seem to play well with deeper nesting. It works fine on a in a tr tag but doesn't extend into the table tag.

The plugin is a good idea, and could be useful, but not effective enough at this point.

Error with quotation marks

I'm using the latest version of the plug-in with Vim 8.1.648 and I get this error whenever I move the cursor to a quotation mark in the file:

Error detected while processing function <SNR>32_Highlight_Matching_Pair[20]..<SNR>32_SearchForMatchingTag:                                            
line   19:                                                                                                                                             
E475: Invalid argument: 0  

Adding php to g:mta_filetypes does not work as expected

Thanks for creating this excellent plugin, and for your other bundles!

I'm trying to get MatchTagAlways to work with HTML embedded within PHP files using the following line in my vimrc:

let g:mta_filetypes = { 'html' : 1, 'xhtml' : 1, 'xml' : 1, 'jinja' : 1, 'ant': 1, 'php': 1 }

When I open a PHP file (:set ft? results in filetype=php), no tag match highlights occur.

Am I doing it wrong?

File type detection doesn't work for combined files

If I set a combined HTML/CSS/Javascript file to "javascript.html.css" the file type detection fails.

If I remove the filetype detection, and replace the "*" in the autocmd with autocmd filetype html, then it works fine.

The method use to search the dictionary can't handle the combined file types. Use 'split(filetype,'.')' and compare each element, or just stick with four or five autocommands..

Tags not hightlighted

I've installed the plugin with pathogen and :scriptnames shows it running but tags are not highlighted like the animated gif on the site. Opening/closing parens are highlighted but not tags surrounding complete blocks. If I get rid of matchparen.vim then no highlighting happens at all. It leads me to believe MTA isn't doing anything.

I'm pretty new to vim so I apologize for being clueless how to troubleshoot further.

vim 7.4 (compiled with python) installed in Linux Mint Petra from the repo.
I've done the same installation on another ubuntu box and got the same results.

output from :scriptnames
1: /usr/share/vim/vimrc
2: /usr/share/vim/vim73/debian.vim
3: /usr/share/vim/vim73/syntax/syntax.vim
4: /usr/share/vim/vim73/syntax/synload.vim
5: /usr/share/vim/vim73/syntax/syncolor.vim
6: /usr/share/vim/vim73/filetype.vim
7: /usr/share/vim/vim73/menu.vim
8: /usr/share/vim/vim73/autoload/paste.vim
9: ~/.vimrc
10: ~/.vim/autoload/pathogen.vim
11: /usr/share/vim/vim73/ftoff.vim
12: /usr/share/vim/vim73/syntax/nosyntax.vim
13: /usr/share/vim/vim73/ftplugin.vim
14: /usr/share/vim/vim73/indent.vim
15: ~/.vim/bundle/MatchTagAlways/plugin/MatchTagAlways.vim
16: /usr/share/vim/vim73/plugin/getscriptPlugin.vim
17: /usr/share/vim/vim73/plugin/gzip.vim
18: /usr/share/vim/vim73/plugin/matchparen.vim
19: /usr/share/vim/vim73/plugin/netrwPlugin.vim
20: /usr/share/vim/vim73/plugin/rrhelper.vim
21: /usr/share/vim/vim73/plugin/spellfile.vim
22: /usr/share/vim/vim73/plugin/tarPlugin.vim
23: /usr/share/vim/vim73/plugin/tohtml.vim
24: /usr/share/vim/vim73/plugin/vimballPlugin.vim
25: /usr/share/vim/vim73/plugin/zipPlugin.vim
26: /usr/share/vim/gvimrc

Highlighting on handlebars doesn't appear to work

Below is the code for the detection of handlebars files in init.vim (I am using nvim)
On HTML files the highlighting seems to work, and set tf? in nvim returns hmtl.handlebars

let g:mta_filetypes = {
\ 'html' : 1,
\ 'xhtml' : 1,
\ 'xml' : 1,
\ 'jinja' : 1,
\ 'html.handlebars' : 1,
}

Error detected while processing /usr/share/vim/vimfiles/plugin/MatchTagAlways.vim & ~/.vimrc

Hi Valloric,

I am currently using MatchTagAlways with Vim on Fedora 20 and it seems that the backlashes "" that are used to break the lines in MatchTagAlways.vim and ~/.vimrc are not recognized by Vim 7.4 on Fedora anymore. On Windows everything works perfectly. Here is the error console for MatchTagsAlways.vim:

[phongvcao@localhost ~]$ vim
Error detected while processing /usr/share/vim/vimfiles/plugin/MatchTagAlways.vim:
line 25:
E10: \ should be followed by /, ? or &
line 26:
E10: \ should be followed by /, ? or &
line 30:
E15: Invalid expression:
E15: Invalid expression:
line 31:
E10: \ should be followed by /, ? or &
line 32:
E10: \ should be followed by /, ? or &
line 33:
E10: \ should be followed by /, ? or &
line 34:
E10: \ should be followed by /, ? or &
line 35:
E10: \ should be followed by /, ? or &
line 36:
E10: \ should be followed by /, ? or &
line 37:
E10: \ should be followed by /, ? or &
line 39:
E15: Invalid expression:
E15: Invalid expression:
line 40:
E10: \ should be followed by /, ? or &
line 41:
E15: Invalid expression:
E15: Invalid expression:
line 42:
E10: \ should be followed by /, ? or &
Press ENTER or type command to continue

To fix this, I had to eliminate all the "" character from ~/.vimrc and MatchTagAlways.vim. For example I had to use this:
let g:mta_filetypes = { 'html' : 1, 'xhtml' : 1, 'xml' : 1, 'jinja' : 1, 'php' : 1, }

instead of:
let g:mta_filetypes = {
\ 'html' : 1,
\ 'xhtml' : 1,
\ 'xml' : 1,
\ 'jinja' : 1,
}

So just a small note to you that you should eliminate all the "" in MatchTagAlways' source code and tutorial so that new users won't get this kind of error message.

Thank you Valloric for writing such a great plugin. I found out MatchTagAlways from StackOverflow.

Have a great day buddy!

Best regards,

Phong Cao,

Does not work in .html.tmpl files

I've a file with extension .html.tmpl that contains html elements. Even if this plugin works in .html files, does not work in tmpl ones.

let g:mta_filetypes = {
    \ 'html' : 1,
    \ 'tmpl' : 1,
    \}

Not working with handlebars template

:set ft?
filetype=hbs syntax=html
let g:mta_filetypes =
      \ get( g:, 'mta_filetypes', {
      \ 'html' : 1,
      \ 'xhtml' : 1,
      \ 'xml' : 1,
      \ 'jinja' : 1,
      \ 'eruby' : 1,
      \ 'htmldjango' : 1,
      \ 'django' : 1,
      \ 'hbs' : 1,
      \} )

index.hbs

<div>
 <span>{{name}}</span>
</div>

Changing the extension to html works perfectly fine, but it's not working with hbs extension.

MatchTagAlways Python error freezes screen when files are left open for a few minutes

I recently updated MatchTagAlways to the latest version in master. After doing this I went on to editing some files in Neovim and left them open for a while. When I came back to any one of those files and tried to move the cursor, the window was frozen and displayed the following error:

Error detected while processing function <SNR>78_HighlightEnclosingTagsIfPossible[12].
.<SNR>78_HighlightEnclosingTags[1]..<SNR>78_GetEnclosingTagLocations[6]..provider#python#Call:                                                                              
line   18: Invalid channel "1"

At this point, no matter what key I pressed on the keyboard, the window would remain frozen showing this error. As soon as I disabled MatchTagAlways in my init.vim, the problem disappeared.

Some info about my environment:

  • OS: Fedora 24.1
  • Neovim 0.1.7
  • MatchTagAlways (master)

My MatchTagAlways config is fairly simple:

let g:mta_use_matchparen_group = 0
let g:mta_filetypes = {
    \ 'html' : 1,
    \ 'xhtml' : 1,
    \ 'xml' : 1,
    \ 'phtml' : 1,
    \ 'php': 1,
    \}

If you need any more info from me, just let me know. Thanks.

(Neovim) Error detected while processing function <SNR>137_HighlightEnclosingTagsIfPossible

I'm using Neovim v0.7.0 on Ubuntu. I've started experiencing this error when I open .html files:

Error detected while processing function <SNR>137_HighlightEnclosingTagsIfPossible[12]..<SNR>137_HighlightEnclosingTags:
line    1:
E714: List required
Press ENTER or type command to continue

line   5:
E121: Undefined variable: opening_tag_line
Press ENTER or type command to continue

I don't experience the same error if I open the file in Vim, as opposed to nvim.

Slowdown inside matchings

When i'm inside some reachable matching tag (e.g.: the ones who the opening and closing tags are on the screen), there's a noticeable slowdown on the cursor speed. Probably because of constant pattern checking while moving inside of those.

Looking superficially, one possible solution would be triggering the matching only when entering or leaving a tag level, but i don't know how complex it can be.

I come back with more ideas after looking better the code.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.