Raimondi / delimitMate

Vim plugin, provides insert mode auto-completion for quotes, parens, brackets, etc.
http://www.vim.org/scripts/script.php?script_id=2754
1.98k stars 117 forks source link

latex #235

Closed coachshea closed 8 years ago

coachshea commented 8 years ago

First, thank you for the great plugin. I have loved using delimitMate. I am currently trying to use it in conjunction with latex. I know there have been latex questions asked before, but I don't believe this specific question has been asked. I was wondering if there was a way to set up delimitMate so that when I typed a single quote within a latex file ('), I would get the following: `|' and similarly when I type a double quote ("), I would get``|''. Any help is greatly appreciated. Thank you again.

Raimondi commented 8 years ago

Sure, there is a way just not with delimitMate. Put something like the following lines in your vimrc:

autocmd FileType latex inoremap <buffer> ' \|'<Left>
autocmd FileType latex inoremap <buffer> " \|"<Left>

If your vim is 7.4 and has the patch 849 then replace <Left> with <C-G>U<Left> in both mappings to prevent breaking undo with the mapping.

coachshea commented 8 years ago

Thanks for your quick reply. That's too bad though. I have something like that now, but of course it doesn't contain the logic that delimitMate possesses (i.e. not expanding I'm and the like). I thought maybe something could be done pair `' similar to (), [], etc. but my efforts to expand b:delimitmate_matchpairs had no effect. Thanks again for you time.

Atcold commented 8 years ago

I'm not sure I understood what has been asked here, maybe I have the same question. When editing LaTeX, would be nice having

` -> `_'

where _ is the cursor, and, analogously

`` -> ``_''

Am I asking the same thing? Is this possible? Right now, the default behaviour is

` -> `_`
Raimondi commented 8 years ago

This should do what you describe

let delimitMate_quotes = '"'
let delimitMate_matchpairs = "<:>,(:),[:],{:},`:'"
Atcold commented 8 years ago

@Raimondi: this is absolutely awesome! Would you know how to combine let with autocmd BufNewFile,BufRead *.{tex,bib}?

Raimondi commented 8 years ago

If I understood correctly:

autocmd BufNewFile,BufRead *.{tex,bib} let foo = 'bar'
Atcold commented 8 years ago

Wow. I just found it in the manual! You're blazing fast! Maybe it could be helpful adding a line of suggestion about how to handle this for LaTeX? Moreover, from the user manual

let delimitMate_matchpairs = "(:),[:],{:},<:>"
au FileType vim,html let b:delimitMate_matchpairs = "(:),[:],{:},<:>"

shouldn't be both lets preceded by an au? And I'm not sure what's the role of

let delimitMate_quotes = '"'

Oh, it's for removing ' from the list, correct?

Raimondi commented 8 years ago

I don't use latex so I had no idea, it might be good to put it in the docs. You can make the settings apply globally, in that case you don't need to use an autocmd and you need to use a global variable, as in the first case. In the case that oyu want to change the settings for some filetypes you need to use an autocmd and use a buffer local variable, as in the second line.

The default for let delimitMate_quotes = '"' takes away both the single quote and the backtick.

Atcold commented 8 years ago

Hmm... almost there (I hope you don't mind keeping the chat going).

Why is there the need to take away the single quote and backtick from delimitMate_quotes? I noticed a b: before the name of the variable. Is that a buffer local variable? And lastly, what would be the snippet for changing the default behaviour only for specific FileType? I mean, shouldn't be both lets preceded by an autocmd?

Raimondi commented 8 years ago

You need to remove both from delimitMate_quotes because any character there will be remapped to add functionality.

The b: prefix is for buffer local variables, there are several scopes besides buffer local, see :help internal-variables.

In delimitMate local options override global options, so if you put the following in your vimrc:

let g:delimitMate_expand_cr = 0
au FileType c let b:delimitMate_expand_cr = 1

and you open a file with python or bash filetype then car return will not be expanded because the feature was turned off globally. But if you open a file with C filetype then the buffer local variable is set due to the autocmd and it takes precedence over the global variable, and the car return will be expanded.

Atcold commented 8 years ago

Therefore, since I want to remove additional functionality for the quotes only when editing LaTeX documents, I will have to specify b:delimitMate_quotes with the au casing for the FileType. Correct?

P.S. I've noticed I've been always using (since I originally borrowed someone else vimrc) au BufNewFile,BufRead *.{extA,extB,...}, which is identical to your shorter au FileType fileTypeA,fileTypeB,..., correct?

Thank you so much for teaching!

Raimondi commented 8 years ago

That's correct, you need to use b:delimitMate_quotes and b:delimitMate_matchpairs.

Two files with the same extension could have different filetypes, some languages share the same extension. In general, if the code you need to run is related to a language then use FileType.

Atcold commented 8 years ago

How can mere mortals offer you a 🍺? I think I'll send you a PR with this setting to your documentation for LaTeX users, once I'm confident with what it entitles.

Atcold commented 8 years ago

OK, I'm still experimenting. In LaTeX another quote is $. So I've tried

au FileType tex,bib let b:delimitMate_quotes = '$'

Now, I type $| and I get $|$. But inside the $|$ if I open a [ it won't be closed. I've thought to put $ in the matchpairs, but opening and closing symbols have to be different.

Is there a way to get smart_matchpairs within the smart_quotes? If I add an extra space ($| $), then yes, typing [ will have it closed.

Raimondi commented 8 years ago

Try adding this:

au FileType tex,bib let b:delimitMate_smart_matchpairs = '^\%(\w\|\!\|[£]\|[^[:space:][:punct:]]\)'
Atcold commented 8 years ago

Absolutely marvellous, @Raimondi! As soon as I'm done experimenting I'll add a new chapter to your documentation!

Question. How do I know what's the default setting for a given variable? :let delimitMate_quotes does say E121: Undefined variable: delimitMate_quotes. I was expecting the default entry to be shown.

Raimondi commented 8 years ago

You have to RTFM, see :help 'delimitMate_quotes' in this case.

Atcold commented 8 years ago

Yes, I did read it all! That's why I was expecting to be able to ping the result, directly from Vim, and check whether it matches what is written on the manual. But it looks like the variable is undefined and I get an E121. The only two variable I can see directly from Vim are delimitMate_expand_cr and delimitMate_version, nothing else.

Edit: delimitMate_expand_cr is visible because I did set it in my ~/.vimrc. My bad...

Raimondi commented 8 years ago

Oh! I get it, my bad. delimitMate uses a script local variable to store the default values (I'm not even sure why I did it that way). So, in order to check the default values you need to look in the code, I think it's in the function init() in plugin/delimitMate.vim

Atcold commented 8 years ago

Hello @Raimondi, so far all good. Would you know how to change-inside-$ as one does with ci" for change-inside-"? Since $ is now a quotation field delimiter, it would be nice being able to treat it as a text object too. I hope it's not too off topic, though.