Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autoformat indicator #18

Closed
ixjlyons opened this issue Sep 28, 2014 · 7 comments
Closed

Autoformat indicator #18

ixjlyons opened this issue Sep 28, 2014 · 7 comments

Comments

@ixjlyons
Copy link

I have a small feature proposal. I haven't forked this project, but I wouldn't mind doing so and creating a pull request if that sort of thing is welcomed.

My current workflow using vim-pencil with LaTeX involves frequently toggling autoformat on and off (for instance, to switch from writing a paragraph to writing an equation, where I want to manually specify all formatting), as I haven't quite figured out g:pencil#autoformat_blacklist. I thought it might be nice to provide a convenience function, similar to PencilMode(), which returns a string (preferably user-overridable) indicating whether or not autoformat is enabled. To this end, I put the following in my .vimrc and it seems to work for my purposes:

let g:pencil#autoformat_indicators = {'auto': 'auto', 'noauto': 'noauto'}
fun! PencilAutoformat()
    if exists('b:last_autoformat')
        if b:last_autoformat
            return get(g:pencil#autoformat_indicators, 'auto', 'auto')
        else
            return get(g:pencil#autoformat_indicators, 'noauto', 'noauto')
        en
    else
        return ''
    en
endf

If this were built into plugin/pencil.vim (again, like PencilMode()), people could then add something like this to their .vimrc:

let g:pencil#autoformat_indicators = {'auto': 'a+', 'noauto': 'a-',}
let statusline=%<%f\ %{PencilMode()}\ %{PencilAutoformat()}
@reedes
Copy link
Collaborator

reedes commented Sep 28, 2014

I should document the blacklist a bit better, but it's pretty straightforward. It's just a list of syntax highlighting groups, that when detected at the cursor position, will disable autoformat:

Try the following in your .vimrc:

let g:pencil#autoformat_blacklist = [
        \ 'markdownCode',
        \ 'markdownUrl',
        \ 'markdownIdDeclaration',
        \ 'markdownLinkDelimiter',
        \ 'markdownHighlight[A-Za-z0-9]+',
        \ 'mkdCode',
        \ 'mkdIndentCode',
        \ 'markdownFencedCodeBlock',
        \ 'markdownInlineCode',
        \ 'mmdTable[A-Za-z0-9]*',
        \ 'txtCode',
        \ 'texMath',
        \ ]

...where texMath should part of the highlight group name when the cursor is inside a formula.

If that works, I'll add it to the default group list.

Another way to identify the highlight group via an F10 key mapping (or another mapping of your choice):

map <F10> :echo "hi<"
\ . synIDattr(synID(line("."),col("."),1),"name") . '> trans<'
\ . synIDattr(synID(line("."),col("."),0),"name") . "> lo<"
\ . synIDattr(synIDtrans(synID(line("."),col("."),1)),"name") . ">"<CR>

ETA: the status line indicator will be tricky, because the autoformat is dependent on cursor position. So better to rely on highlight groups.

@ixjlyons
Copy link
Author

The texMath addition does work for the math environments (like \begin{equation}...\end{equation} and \[...\]). However, I pretty much want total control over formatting any time I'm working between \begin{...} and \end{...}, and there aren't really highlight groups for these cases. In fact, I think the only time I actually want autoformatting on is in texSectionZone, texSubsectionZone, and texSubSubSectionZone.

Would g:pencil#autoformat_whitelist be something worth looking into? That is, if I have autoformat off by default, I could set it to activate only in specific environments.

Also, since I have autoformat off by default and only toggle it manually, the dependence on cursor position shouldn't be an issue, right?

@reedes
Copy link
Collaborator

reedes commented Sep 29, 2014

The blacklist is still enforced even when toggling autoformat on, so if you enter insert mode when cursor position on a blacklisted highlight group, there will be no set fo+=a. Thus, the indicator would be misleading. I'm not certain if it's possible to reliably update the status line in that case to reflect that autoformat isn't active due to the blacklisting.

I'll have to think about the whitelist idea. It could be applied after the blacklist as an override to prevent autoformat from being disabled. (That assumes there's a distinct highlight group to whitelist.)

I'll try working with a tex filetype to get a better handle on how it might work. Could you point me to representative samples?

@ixjlyons
Copy link
Author

Here is one example which demonstrates some of the basic environments [link].

Essentially the lorem ipsum paragraphs are where I'd want autoformatting, and everything else I can format myself. There are occasions, such as in the caption environment of a figure, where it would be convenient to have autoformat enabled, but is rare enough that just hitting gq a few times is easy and efficient enough.

@reedes
Copy link
Collaborator

reedes commented Oct 1, 2014

That sample document is very helpful.

I found a better mapping to show the highlight stack: link

Could you try the following addition to your blacklist and see how it behaves?

let g:pencil#autoformat_blacklist = [
        \ 'markdownCode',
        \ 'markdownUrl',
        \ 'markdownIdDeclaration',
        \ 'markdownLinkDelimiter',
        \ 'markdownHighlight[A-Za-z0-9]+',
        \ 'mkdCode',
        \ 'mkdIndentCode',
        \ 'markdownFencedCodeBlock',
        \ 'markdownInlineCode',
        \ 'mmdTable[A-Za-z0-9]*',
        \ 'txtCode',
        \ 'texAbstract',
        \ 'texBeginEndName',
        \ 'texDelimiter',
        \ 'texDocType',
        \ 'texInputFile',
        \ 'texMath',
        \ 'texRefZone',
        \ 'texSection$',
        \ 'texStatement',
        \ 'texTitle',
        \ ]

There's still a problem with entering insert mode from the leading characters in the begin{figure} block, but I could fix that with a code change that scans the line more comprehensively for blacklisted highlight groups.

@ixjlyons
Copy link
Author

ixjlyons commented Oct 1, 2014

I've been thinking that what you describe at the end is the problem. That is, when I start typing on the line after entering \begin{figure}<CR>, whatever I start typing pops up the line above. I guess I'm thinking autoformatting is causing problems as I'm writing rather than when I make changes to a line and don't want autoformatting to mess up the formatting I've already deliberately specified.

Also, texAbstract is a place where I would want autoformatting enabled.

Honestly, you shouldn't feel obligated to support writing LaTeX documents fluently. I think it would require pretty substantial effort to do so, as there are many packages which provide their own environments, plus an author can define his or her own environments at will. The whitelist idea may also not work, as I've found that inside a table environment, for example, the highlight group is texSectionZone (so it's basically the only highlight group where I want autoformatting, but not always). It might be more appropriate to integrate the idea of vim-pencil into a full-on LaTeX plugin, like Vim-LaTeX or LaTeX-Box, as they probably provide much more powerful ways of obtaining information about the environment than highlight groups. As far as I know, neither of them provides the functionality.

For quite a while, I've done pretty much everything in vim except write LaTeX documents, mainly because I was never satisfied with line wrapping options I tried. I discovered this plugin, and it's basically what I've always wanted -- when I insert text into an already-wrapped line, it maintains the textwidth of the line. After adding the little bit of script from my original post, I'm actually quite happy leaving autoformat off by default and toggling it on only when I'm working on a paragraph.

@reedes
Copy link
Collaborator

reedes commented Oct 1, 2014

With the latest commit, if pencil can't find a highlight group when entering Insert mode, it'll scan first towards the beginning of the line from the cursor position (as it has done). And now if it still no highlight group, it'll scan towards the end of the line.

The default blacklist is also updated.

I'm holding off on the whitelist idea for now, but will consider it for the future.

For the autoformat indicator, feel free to fork and see if you can get it working to your satisfaction.

Agreed that the LaTeX environment can get complex. But if pencil can support the common case for the shipped syntax file, it should cater to many (most?) users.

As far as disabling autoformat when entering \begin{figure}<CR>, that's a good question. I've had a similar problem defining a new triple-backquote block in markdown, requiring me to hit escape and then Insert again. I'm not sure yet how to detect and disable.

@reedes reedes closed this as completed in 290b087 Sep 7, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants