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

Feature/compact view #159

Merged
merged 6 commits into from
Feb 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 10 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ Here we rename a method named `MoveCursor()` to `Cursor()` in multiple files, us
- [Use Your Own Map](#use-your-own-map)
- [Edit Mode](#edit-mode)
- [Limitation](#limitation)
- [Quickfix Mode](#quickfix-mode)
- [Arguments](#arguments)
- [Example](#example)
- [Tips](#tips)
Expand All @@ -38,7 +37,7 @@ Here we rename a method named `MoveCursor()` to `Cursor()` in multiple files, us

- Preview mode for fast exploring.

- View results in a quickfix window if you feel more familiar with quickfix window.
- Two types of views. For both users who like a sublime-like, rich context result window, or who feel more comfortable with good old quickfix window. (similar to ack.vim)

- Various options for customized search, view and edit.

Expand Down Expand Up @@ -70,8 +69,7 @@ Here we rename a method named `MoveCursor()` to `Cursor()` in multiple files, us

6. `:CtrlSFOpen` can reopen CtrlSF window when you have closed CtrlSF window. It is free because it won't invoke a same but new search. A handy command `:CtrlSFToggle` is also available.

7. Alternatively run `:CtrlSFQuickfix [pattern]`, it will only open a quickfix
window to show search result.
7. If you prefer a quickfix-like result window, just try to press `M` in CtrlSF window.

## Key Maps

Expand All @@ -84,6 +82,7 @@ In CtrlSF window:
- `P` - Like `Enter` but open file in a preview window and switch focus to it.
- `O` - Like `Enter` but always leave CtrlSF window opening.
- `T` - Like `t` but focus CtrlSF window instead of new opened tab.
- `M` - Switch result window between **normal** view and **compact** view.
- `q` - Quit CtrlSF window.
- `<C-J>` - Move cursor to next match.
- `<C-K>` - Move cursor to previous match.
Expand Down Expand Up @@ -122,12 +121,6 @@ CtrlSF provides many maps you can use for quick accessing all features, here I l

Input `:CtrlSF foo ` in command line where `foo` is the last search pattern of vim.

There are also some maps for quickfix mode, for example:

- `<Plug>CtrlsfQuickfixPrompt`

like `<Plug>CtrlSFPrompt` but instead invoking `:CtrlSFQuickfix`.

For a full list of maps, please refer to the document.

I strongly recommend you should do some maps for a nicer user experience, because typing 8 characters for every single search is really boring and painful experience. Another reason is that **one of the most useful feature 'Search Visual Selected Word' can be accessed by map only.**
Expand All @@ -143,9 +136,6 @@ nmap <C-F>p <Plug>CtrlSFPwordPath
nnoremap <C-F>o :CtrlSFOpen<CR>
nnoremap <C-F>t :CtrlSFToggle<CR>
inoremap <C-F>t <Esc>:CtrlSFToggle<CR>
nmap <C-F>l <Plug>CtrlSFQuickfixPrompt
vmap <C-F>l <Plug>CtrlSFQuickfixVwordPath
vmap <C-F>L <Plug>CtrlSFQuickfixVwordExec
```

## Edit Mode
Expand All @@ -164,24 +154,6 @@ vmap <C-F>L <Plug>CtrlSFQuickfixVwordExec

- If a file's content varies from last search, CtrlSF will refuse to write your changes to that file (for safety concern). As a rule of thumb, invoke a new search before editing, or just run `:CtrlSFUpdate`.

## Quickfix Mode

The primary motivation of creating CtrlSF is being tired of small, context free quickfix window, so there is no Quickfix Mode in CtrlSF at first. But some users requested for Quickfix Mode as they need it in some cases, I accepted, Quickfix Mode was then finally added into CtrlSF (#94).

You can access Quickfix Mode by commands and maps.

Command:

- `:CtrlSFQuickfix [pattern]`

Maps:

- <Plug>CtrlSFQuickfixPrompt
- <Plug>CtrlSFQuickfixVwordPath
- <Plug>CtrlSFQuickfixVwordExec

But, **as I've no idea about reinventing another ack.vim, only minimum features are implemented in Quickfix Mode. Actually no customization beyond normal quickfix window is applied currently, I choose to leave it to users.**

## Arguments

CtrlSF has a lot of arguments you can use in search. Most arguments are similar to Ack/Ag's but not perfectly same. Here are some most frequently used arguments:
Expand Down Expand Up @@ -245,12 +217,19 @@ Read `:h ctrlsf-arguments` for a full list of arguments.
```vim
let g:ctrlsf_context = '-B 5 -A 3'
```

- `g:ctrlsf_default_root` defines how CtrlSF find search root when no explicit path is given. Two possible values are `cwd` and `project`. `cwd` means current working directory and `project` means project root. CtrlSF locates project root by searching VCS root (.git, .hg, .svn, etc.)

```vim
let g:ctrlsf_default_root = 'project'
```

- `g:ctrlsf_default_view_mode` defines default view mode which CtrlSF will use. Possible values are `normal` and `compact`. The default value is `normal`.

```vim
let g:ctrlsf_default_view_mode = 'compact'
```

- `g:ctrlsf_extra_backend_args` is a dictionary that defines extra arguments that will be passed *literally* to backend, especially useful when you have your favorite backend and need some backend-specific features. For example, using `ptignore` file for [pt][8] should be like

```vim
Expand Down
123 changes: 83 additions & 40 deletions autoload/ctrlsf.vim
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
"""""""""""""""""""""""""""""""""

" remember what user is searching
let s:current_mode = ''
let s:current_query = ''
let s:only_quickfix = 0

" s:ExecSearch()
"
" Basic process: query, parse, render and display.
"
func! s:ExecSearch(args, only_quickfix) abort
func! s:ExecSearch(args) abort
try
call ctrlsf#opt#ParseOptions(a:args)
catch /ParseOptionsException/
Expand All @@ -42,21 +42,8 @@ func! s:ExecSearch(args, only_quickfix) abort
" Parsing
call ctrlsf#db#ParseAckprgResult(output)

" Only populate and open the quickfix window
if a:only_quickfix
call setqflist(ctrlsf#db#MatchListQF())
botright copen
return
endif

call ctrlsf#win#OpenMainWindow()
call ctrlsf#win#Draw()
call ctrlsf#buf#ClearUndoHistory()
call ctrlsf#hl#HighlightMatch()

" scroll up to top line
1normal z<CR>
call ctrlsf#NextMatch(1)
" Open and draw contents
call s:OpenAndDraw()

" populate quickfix and location list
if g:ctrlsf_populate_qflist
Expand All @@ -67,7 +54,7 @@ endf

" Search()
"
func! ctrlsf#Search(args, only_quickfix) abort
func! ctrlsf#Search(args, ...) abort
let args = a:args

" If no pattern is given, use word under the cursor
Expand All @@ -76,9 +63,13 @@ func! ctrlsf#Search(args, only_quickfix) abort
endif

let s:current_query = args
let s:only_quickfix = a:only_quickfix

call s:ExecSearch(s:current_query, s:only_quickfix)
" if view mode is not specified, use 'g:ctrlsf_default_view_mode'
let s:current_mode = empty(a:000) ?
\ g:ctrlsf_default_view_mode :
\ a:1

call s:ExecSearch(s:current_query)
endf

" Update()
Expand All @@ -87,7 +78,7 @@ func! ctrlsf#Update() abort
if empty(s:current_query)
return -1
endif
call s:ExecSearch(s:current_query, s:only_quickfix)
call s:ExecSearch(s:current_query)
endf

" Open()
Expand All @@ -106,9 +97,35 @@ func! ctrlsf#Redraw() abort
call ctrlsf#win#MoveCursor(wlnum, lnum, col)
endf

" SwitchViewMode()
"
func! ctrlsf#SwitchViewMode() abort
let next = ctrlsf#CurrentMode() ==# 'normal' ? 'compact' : 'normal'

" set current view mode
let s:current_mode = next

call ctrlsf#Quit()
call s:OpenAndDraw()
endf

" Quickfix()
"
" This is DEPRECATED method which is used only for backward-compatible
"
func! ctrlsf#Quickfix(args) abort
call ctrlsf#log#Notice("CtrlSFQuickfix is DEPRECATED! Invoking CtrlSF's compact view instead.")
sleep 1
call ctrlsf#Search(a:args, 'compact')
endf

" Save()
"
func! ctrlsf#Save()
if ctrlsf#CurrentMode() !=# 'normal'
ctrlsf#log#Notice("Edit mode is disabled in compact view.")
endif

if !&l:modified
return
endif
Expand Down Expand Up @@ -157,10 +174,28 @@ func! ctrlsf#Toggle() abort
endif
endf

" ClearSelectedLine()
"
func! ctrlsf#ClearSelectedLine() abort
call ctrlsf#hl#ClearSelectedLine()
endf

" ToggleMap()
"
func! ctrlsf#ToggleMap() abort
call ctrlsf#buf#ToggleMap()

if b:ctrlsf_map_enabled
echo "Maps enabled."
else
echo "Maps disabled."
endif
endf

" JumpTo()
"
func! ctrlsf#JumpTo(mode) abort
let [file, line, match] = ctrlsf#view#Reflect(line('.'))
let [file, line, match] = ctrlsf#view#Locate(line('.'))

if empty(file) || empty(line)
return
Expand Down Expand Up @@ -202,6 +237,10 @@ endf
" Move cursor to the next match after current cursor position.
"
func! ctrlsf#NextMatch(forward) abort
if ctrlsf#CurrentMode() !=# 'normal'
return
endif

let [_, cur_vlnum, cur_vcol, _] = getpos('.')
let [vlnum, vcol] = ctrlsf#view#FindNextMatch(a:forward, &wrapscan)

Expand All @@ -220,6 +259,14 @@ func! ctrlsf#NextMatch(forward) abort
endif
endf

" CurrentMode()
"
func! ctrlsf#CurrentMode()
let vmode = empty(s:current_mode) ? 'normal' : s:current_mode
call ctrlsf#log#Debug("Current Mode: %s", vmode)
return vmode
endf

" OpenFileInWindow()
"
" OpenFileInWindow() has 2 modes:
Expand Down Expand Up @@ -319,27 +366,23 @@ func! s:PreviewFile(file, lnum, col, follow) abort
endif
endf

" s:Quit()
" s:OpenAndDraw()
"
func! s:Quit() abort
call ctrlsf#preview#ClosePreviewWindow()
call ctrlsf#win#CloseMainWindow()
endf
func! s:OpenAndDraw() abort
call ctrlsf#win#OpenMainWindow()
call ctrlsf#win#Draw()
call ctrlsf#buf#ClearUndoHistory()
call ctrlsf#hl#ReloadSyntax()
call ctrlsf#hl#HighlightMatch()

" ClearSelectedLine()
"
func! ctrlsf#ClearSelectedLine() abort
call ctrlsf#hl#ClearSelectedLine()
" scroll up to top line
1normal z<CR>
call ctrlsf#NextMatch(1)
endf

" ToggleMap()
" s:Quit()
"
func! ctrlsf#ToggleMap() abort
call ctrlsf#buf#ToggleMap()

if b:ctrlsf_map_enabled
echo "Maps enabled."
else
echo "Maps disabled."
endif
func! s:Quit() abort
call ctrlsf#preview#ClosePreviewWindow()
call ctrlsf#win#CloseMainWindow()
endf
4 changes: 4 additions & 0 deletions autoload/ctrlsf/buf.vim
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,14 @@ endf
"
func! ctrlsf#buf#ClearUndoHistory() abort
let modified_bak = getbufvar('%', '&modified')
let modifiable_bak = getbufvar('%', '&modifiable')
setl modifiable
let ul_bak = &undolevels
set undolevels=-1
exe "normal a \<BS>\<Esc>"
let &undolevels = ul_bak
unlet ul_bak
call setbufvar('%', '&modifiable', modifiable_bak)
call setbufvar('%', '&modified', modified_bak)
endf

Expand Down Expand Up @@ -96,6 +99,7 @@ func! ctrlsf#buf#ToggleMap(...) abort
\ "quit" : "ctrlsf#Quit()",
\ "next" : "ctrlsf#NextMatch(1)",
\ "prev" : "ctrlsf#NextMatch(0)",
\ "chgmode" : "ctrlsf#SwitchViewMode()",
\ "loclist" : "ctrlsf#OpenLocList()",
\ "prevw" : "ctrlsf#JumpTo('preview')",
\ }
Expand Down
8 changes: 7 additions & 1 deletion autoload/ctrlsf/hl.vim
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func! ctrlsf#hl#HighlightMatch(...) abort
return -1
endif

let pattern = ctrlsf#opt#GetOpt("_vimhlregex")
let pattern = ctrlsf#opt#GetOpt("_vimhlregex")[ctrlsf#CurrentMode()]
call ctrlsf#log#Debug("HighlightRegex: %s", pattern)

silent! call matchdelete(w:ctrlsf_match_hlid)
Expand All @@ -37,3 +37,9 @@ endf
func! ctrlsf#hl#ClearSelectedLine() abort
silent! call matchdelete(w:ctrlsf_line_hlid)
endf

" ReloadSyntax()
"
func! ctrlsf#hl#ReloadSyntax() abort
runtime syntax/ctrlsf.vim
endf
5 changes: 4 additions & 1 deletion autoload/ctrlsf/opt.vim
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,10 @@ func! ctrlsf#opt#ParseOptions(options_str) abort
let s:options["_vimregex"] = ctrlsf#pat#Regex()

" vimhlregex
let s:options["_vimhlregex"] = ctrlsf#pat#HighlightRegex()
let s:options["_vimhlregex"] = {
\ 'normal': ctrlsf#pat#HighlightRegex('normal'),
\ 'compact': ctrlsf#pat#HighlightRegex('compact')
\ }

call ctrlsf#log#Debug("Options: %s", string(s:options))
endf
17 changes: 13 additions & 4 deletions autoload/ctrlsf/pat.vim
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ endf

" HighlightRegex()
"
func! ctrlsf#pat#HighlightRegex() abort
func! ctrlsf#pat#HighlightRegex(vmode) abort
let base = ctrlsf#pat#Regex()

let magic = strpart(base, 0, 2)
Expand All @@ -85,10 +85,19 @@ func! ctrlsf#pat#HighlightRegex() abort

" sign (to prevent matching out of file body)
let sign = ''
if magic ==# '\v'
let sign = '(^\d+:.*)@<='

if a:vmode ==# 'normal'
if magic ==# '\v'
let sign = '(^\d+:.*)@<='
else
let sign = '\(\^\d\+:\.\*\)\@<='
endif
else
let sign = '\(\^\d\+:\.\*\)\@<='
if magic ==# '\v'
let sign = '(\|\d+ col \d+\|.*)@<='
else
let sign = '\(|\d\+ col \d\+|\.\*\)\@<='
endif
endif

return printf('%s%s%s%s', magic, case, sign, pattern)
Expand Down
Loading