2

Okey, I agree - the title is useless. The thing is, I have no idea how to put this into a one liner...

What I'm trying to do is to map the space key so that it serves as a :nohl mapping, but at the same time, when it is on a folded line, to serve as a za in normal mode (open/close fold).

Is this even possible?

What I'm having trouble is distinguishing between the two - is there a way to "detect" a folded line below the cursor, or to detect an incremental search "currently in progress" (as in, there is something highlighted)?

Or am I tackling this in a completely wrong way? All advices welcomed!

Rook
  • 60,248
  • 49
  • 165
  • 242

3 Answers3

2

It may be ugly, but it worked for me:

noremap <Space> :nohl<CR> za

The drawback is an error occuring when pressing space on unfolded lines.

And R
  • 487
  • 1
  • 14
  • 17
2

Reusing ideas from Andy Rk:

function! FoldSetWithNoHL()
    set nohls
    if (foldclosed(line('.')) >= 0)
        exe "normal za"
    endif
endfunction

map <space> :silent! call FoldSetWithNoHL()<cr>
Community
  • 1
  • 1
Zsolt Botykai
  • 50,406
  • 14
  • 85
  • 110
  • 1
    This has the unfortunate side effect of turning off search highlighting completely instead of just clearing the current search highlighting. It also opens folds but does not close them. – codelahoma Feb 14 '12 at 12:46
1

Incorporating fixes to the issues I listed in my earlier comment:

function! ToggleFoldWithNoHL()
  if foldlevel('.')
    normal! za
  endif
endfunction

nnoremap <silent> <space> :nohlsearch<cr>:call ToggleFoldWithNoHL()<cr>

There doesn't seem to be a way to detect that there's an active search highlight, so if you have a search active inside a fold, this will clear the search but also close the fold. In that case, another space should put you right back where you want to be.

codelahoma
  • 895
  • 8
  • 14
  • Note that you don't need `execute` there. `normal za` by itself (without quotes) works, although you might want to use `normal! za` in case either `z` or `za` are remapped to something else. – Kurt Hutchinson Feb 14 '12 at 16:44
  • Thanks. I meant to include the `!`, but am still learning the wheres and whens of `execute` and such. Editing to incorporate. – codelahoma Feb 15 '12 at 02:59