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

Export indent to g:Context_indent_function #45

Closed
wants to merge 2 commits into from

Conversation

maxigit
Copy link
Contributor

@maxigit maxigit commented Feb 5, 2020

g:Context_indent_function being a funcref has to start with an uppercase
(unless there is a way to get away with it)

I'm not expecting you to accept the pull request, it's just a proof of concept, it works in theory
but defining custom function seems a bit more tricky than expected.

Example for markdown and orgmode (and indentation)

function MyIndent(line)
  let line = getline(a:line)
  let headings = match(line, '^*\+\zs\s')+1
  if headings <= 0
    let headings = match(line, '^#\+\zs\s')+1
    if headings <= 0
      let headings = 5
    endif
  endif
  return indent(a:line)+headings
endfunction

let g:Context_indent_function = funcref("MyIndent")
let g:context_skip_regex = '^\s*$'

For some reason it hangs at the begining. I think context doesn't like to to start with a indent level of 5 on an empty file ....

#44

g:Context_indent_function being a funcref has to start with an uppercase
(unless there is a way to get away with it)
@wellle
Copy link
Owner

wellle commented Feb 5, 2020

Very nice, thanks for the proof of concept and example function! I'll consider adding this 👍

For some reason it hangs at the begining. I think context doesn't like to to start with a indent level of 5 on an empty file ....

Yes, indent() returns -1 for invalid lines, for example try echo indent(0). This is used as exit condition in context#popup#get_context() for example. So you could probably fix that by making your custom function have that property too. So for example like this maybe (untested):

function MyIndent(line)
    let indent = indent(a:line)
    if indent < 0
        return [indent, indent]
    endif

    let line = getline(a:line)
    let headings = match(line, '^*\+\zs\s')+1
    if headings <= 0
        let headings = match(line, '^#\+\zs\s')+1
        if headings <= 0
            let headings = 5
        endif
    endif

    return [indent+headings, indent]
endfunction

let g:Context_indent_function = funcref("MyIndent")
let g:context_skip_regex = '^\s*$'

Edit: Updated according to changes introduced in #85.

@maxigit
Copy link
Contributor Author

maxigit commented Feb 6, 2020

Your modification works perfectly. Thanks.

@wellle
Copy link
Owner

wellle commented Feb 6, 2020

Hey, thanks again! I tried using this branch for a bit and I found one fundamental issue I don't have a good idea yet how we can solve it in a clean way. Have a look at this screenshot:

Screenshot 2020-02-06 20 18 23

Because of the +5 in the example indent function from above the border line (▬▬▬▬▬▬ <context.vim>) is indented five characters too much. Do you see a good way to fix that?

@maxigit
Copy link
Contributor Author

maxigit commented Feb 6, 2020

I guess the indentation of the border line is a feature (which I didn't realize was there until now).
An option would be obviously to add a global variable to enable/disable it (or have an offset ?)
However, I'm not really happy with the +5 either, it would probably best to be able to specify a special level of indentation (-1, 999, a string ) which mean this line is never part of a context.

@maxigit
Copy link
Contributor Author

maxigit commented Feb 6, 2020 via email

'border_line' is used to indent the border line
@maxigit
Copy link
Contributor Author

maxigit commented Feb 8, 2020

I've pushed a new version ...

@wellle
Copy link
Owner

wellle commented Feb 9, 2020

Thank you! To be honest I also have some concerns* about the new implementation. Let me think about it some more.

*Now we always evaluate the border line indent thing, even though we almost never use. That seems a bit wasteful IMO.

@maxigit
Copy link
Contributor Author

maxigit commented Feb 10, 2020 via email

@wellle
Copy link
Owner

wellle commented Feb 17, 2020

@maxigit: I picked your first commit and then added some more to add a separate setting for the border indent function. I opened a new PR: #54

Would you have a look at that and let me know if that works for you and makes sense? Thank you!

@maxigit
Copy link
Contributor Author

maxigit commented Feb 18, 2020

Seems to work. However, to make it work I add to do let g:context_skip_regex = '^\s*$'.
Which is then line pased to border_line, is the last line of the popup or the first line after the popup ?

@wellle
Copy link
Owner

wellle commented Feb 18, 2020

@maxigit: Thanks for checking! I mentioned that in the README and updated the linked comment 👍

Do you think it's clear enough now?

@maxigit
Copy link
Contributor Author

maxigit commented Feb 19, 2020 via email

@wellle
Copy link
Owner

wellle commented Feb 19, 2020

Thank you for the feedback, will merge it later 👍

The reason you need to update the skip regex is that the default one does skip lines starting with #. I did this because in many file formats those lines are comments and I wanted to exclude comment lines from the context. So in order to include markdown header lines (which start with #) we need to not skip those lines, hence a different skip regex is required.

@maxigit
Copy link
Contributor Author

maxigit commented Feb 19, 2020 via email

@wellle
Copy link
Owner

wellle commented Feb 19, 2020

Added in #54.

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

Successfully merging this pull request may close these issues.

None yet

2 participants