Skip to content

Commit

Permalink
fix issue #213
Browse files Browse the repository at this point in the history
add support for input history
  • Loading branch information
Yggdroot committed Oct 8, 2018
1 parent c8b74c3 commit a64ea18
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 7 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,9 @@ Once LeaderF is launched:
| `<Tab>` | switch to normal mode
| `<C-V>`<br>`<S-Insert>` | paste from clipboard
| `<C-U>` | clear the prompt
| `<C-J>`<br>`<Down>` | move the cursor downward in the result window
| `<C-K>`<br>`<Up>` | move the cursor upward in the result window
| `<C-J>` | move the cursor downward in the result window
| `<C-K>` | move the cursor upward in the result window
| `<Up>`/`<Down>` | recall last/next input pattern from history
| `<2-LeftMouse>`<br>`<CR>` | open the file under cursor or selected(when multiple files are selected)
| `<C-X>` | open in horizontal split window
| `<C-]>` | open in vertical split window
Expand Down
91 changes: 89 additions & 2 deletions autoload/leaderf/python/leaderf/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ def _toEnd(self):
def setPattern(self, pattern):
if pattern:
self.clear()
for ch in pattern:
self._insert(ch)
self._cmdline = list(pattern)
self._cursor_pos = len(self._cmdline)
self._buildPattern()

def _buildPrompt(self):
Expand Down Expand Up @@ -299,6 +299,92 @@ def setNameOnlyFeature(self, state):
def setRefineFeature(self, state):
self._supports_refine = state

def writeHistory(self, category):
if self._is_and_mode:
pattern = self._and_delimiter.join(self._pattern)
elif self._refine:
pattern = self._delimiter.join(self._pattern)
else:
pattern = self._pattern

if not pattern:
return

history_dir = os.path.join(lfEval("g:Lf_CacheDirectory"), '.LfCache', 'history', category)
if self._is_fuzzy:
history_file = os.path.join(history_dir, 'fuzzy.txt')
else:
history_file = os.path.join(history_dir, 'regex.txt')

if not os.path.exists(history_dir):
os.makedirs(history_dir)

if not os.path.exists(history_file):
with lfOpen(history_file, 'w', errors='ignore'):
pass

with lfOpen(history_file, 'r+', errors='ignore') as f:
lines = f.readlines()

pattern += '\n'
if pattern in lines:
lines.remove(pattern)

if len(lines) >= int(lfEval("get(g:, 'Lf_HistoryNumber', '100')")):
del lines[0]

lines.append(pattern)

f.seek(0)
f.truncate(0)
f.writelines(lines)

def previousHistory(self, category):
history_dir = os.path.join(lfEval("g:Lf_CacheDirectory"), '.LfCache', 'history', category)
if self._is_fuzzy:
history_file = os.path.join(history_dir, 'fuzzy.txt')
else:
history_file = os.path.join(history_dir, 'regex.txt')

if not os.path.exists(history_file):
return False

with lfOpen(history_file, 'r', errors='ignore') as f:
lines = f.readlines()
if self._history_index == 0:
self._pattern_backup = self._pattern

if -self._history_index < len(lines):
self._history_index -= 1
self.setPattern(lines[self._history_index].rstrip())
else:
return False

return True

def nextHistory(self, category):
history_dir = os.path.join(lfEval("g:Lf_CacheDirectory"), '.LfCache', 'history', category)
if self._is_fuzzy:
history_file = os.path.join(history_dir, 'fuzzy.txt')
else:
history_file = os.path.join(history_dir, 'regex.txt')

if not os.path.exists(history_file):
return False

with lfOpen(history_file, 'r', errors='ignore') as f:
lines = f.readlines()
if self._history_index < 0:
self._history_index += 1
if self._history_index == 0:
self.setPattern(self._pattern_backup)
else:
self.setPattern(lines[self._history_index].rstrip())
else:
return False

return True

@property
def isPrefix(self): #assume that there are no \%23l, \%23c, \%23v, \%...
pos = self._cursor_pos
Expand Down Expand Up @@ -339,6 +425,7 @@ def isFuzzy(self):
@cursorController
def input(self, callback):
try:
self._history_index = 0
self._blinkon = True
while 1:
self._buildPrompt()
Expand Down
24 changes: 22 additions & 2 deletions autoload/leaderf/python/leaderf/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,9 @@ def accept(self, mode=''):
if self._getInstance().window.cursor[0] <= self._help_length:
lfCmd("norm! j")
return

self._cli.writeHistory(self._getExplorer().getStlCategory())

if len(self._selections) > 0:
files = []
for i in sorted(self._selections.keys()):
Expand Down Expand Up @@ -1307,12 +1310,28 @@ def input(self):
self._index = 0 # search from beginning
if self._cli.pattern:
self._search(cur_content)
elif equal(cmd, '<Up>') or equal(cmd, '<C-K>'):
elif equal(cmd, '<C-K>'):
self._toUp()
self._previewResult(False)
elif equal(cmd, '<Down>') or equal(cmd, '<C-J>'):
elif equal(cmd, '<C-J>'):
self._toDown()
self._previewResult(False)
elif equal(cmd, '<Up>'):
if self._cli.previousHistory(self._getExplorer().getStlCategory()):
if self._getInstance().isReverseOrder():
lfCmd("normal! G")
else:
lfCmd("normal! gg")
self._index = 0 # search from beginning
self._search(cur_content)
elif equal(cmd, '<Down>'):
if self._cli.nextHistory(self._getExplorer().getStlCategory()):
if self._getInstance().isReverseOrder():
lfCmd("normal! G")
else:
lfCmd("normal! gg")
self._index = 0 # search from beginning
self._search(cur_content)
elif equal(cmd, '<LeftMouse>'):
if self._leftClick():
break
Expand All @@ -1334,6 +1353,7 @@ def input(self):
if self.accept('t') is None:
break
elif equal(cmd, '<Quit>'):
self._cli.writeHistory(self._getExplorer().getStlCategory())
self.quit()
break
elif equal(cmd, '<Tab>'): # switch to Normal mode
Expand Down
7 changes: 6 additions & 1 deletion doc/leaderf.txt
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,10 @@ g:Lf_ReverseOrder *g:Lf_ReverseOrder*
g:Lf_AndDelimiter *g:Lf_AndDelimiter*
The And operator character, default value is ' '(space).

g:Lf_HistoryNumber *g:Lf_HistoryNumber*
Specify the number of records in history.
Default value is 100.

==============================================================================
USAGE *leaderf-usage*

Expand Down Expand Up @@ -583,7 +587,8 @@ Once LeaderF is launched: *prompt* *leaderf-prompt*
<Tab> : switch to normal mode.
<C-V>, <S-Insert> : paste from clipboard.
<C-U> : clear the prompt.
<C-J>, <Down>, <C-K>, <Up> : navigate the result list.
<C-J>, <C-K> : navigate the result list.
<Up>, <Down> : recall last/next input pattern from history.
<2-LeftMouse> or <CR> : open the file under cursor or selected(when
multiple files are selected).
<C-X> : open in horizontal split window.
Expand Down

0 comments on commit a64ea18

Please sign in to comment.