# w3m-like keybindings for Chawan.
# Copy-paste this into your config file, or just include it (place it in
# ~/.chawan/w3m.toml or ~/.config/chawan/w3m.toml, then at the beginning of
# ~/.chawan/config.toml, include = "w3m.toml".)

# these are disabled by default in w3m
[status]
show-cursor-position = false
show-hover-link = false

[cmd.w3m]
pageDown = '(n = 1) => pager.scrollDown(pager.height * n)'
pageUp = '(n = 1) => pager.scrollUp(pager.height * n)'
mark = '''
() => {
	/* id is always the current position; this way we can clear by
	   setting a mark twice at the same position. */
	const id = pager.buffer.cursorx + " " + pager.buffer.cursory;
	if (!pager.getMarkPos(id))
		pager.setMark(id);
	else
		pager.clearMark(id);
}
'''
gotoLine = 'n => pager.gotoLine(n)'
centerColumn = '() => pager.centerColumn()'
centerLine = '() => pager.centerLine()'
prevMark = '''
() => {
	const next = pager.findPrevMark();
	if (next)
		pager.gotoMark(next);
	else
		pager.alert("No mark exists before here");
}
'''
nextMark = '''
() => {
	const next = pager.findNextMark();
	if (next)
		pager.gotoMark(next);
	else
		pager.alert("No mark exists before here");
}
'''
editFile = '''
() => {
	if (pager.url.protocol == "file:")
		pager.extern(pager.getEditorCommand(pager.url.pathname))
	else
		pager.alert("Can't edit other than local file");
}
'''
askQuit = '''
() => pager.ask("Do you want to exit Chawan?").then(x => x ? quit() : void(0))
'''
cursorUp = '(n = 1) => pager.setCursorYCenter(pager.cursory - n)'
cursorDown = '(n = 1) => pager.setCursorYCenter(pager.cursory + n)'
cursorLeft = '(n = 1) => pager.setCursorXCenter(pager.cursorx - n)'
cursorRight = '(n = 1) => pager.setCursorXCenter(pager.cursorx + n)'

[page]
# Page/cursor movement
' ' = 'w3m.pageDown'
C-v = 'w3m.pageDown'
b = 'w3m.pageUp'
M-v = 'w3m.pageUp'
PageDown = 'w3m.pageDown'
PageUp = 'w3m.pageUp'
C-f = 'w3m.cursorRight'
C-b = 'w3m.cursorLeft'
C-n = 'w3m.cursorDown'
C-p = 'w3m.cursorUp'
Right = 'w3m.cursorRight'
Left = 'w3m.cursorLeft'
Down = 'w3m.cursorDown'
Up = 'w3m.cursorUp'
J = 'scrollDown'
K = 'scrollUp'
'^' = 'cursorLineBegin'
C-a = 'cursorLineBegin'
'$' = 'cursorLineEnd'
C-e = 'cursorLineEnd'
w = 'cursorNextWord'
W = 'cursorWordBegin'
'<' = 'pageLeft'
'>' = 'pageRight'
'.' = 'scrollLeft'
',' = 'scrollRight'
g = 'gotoLineOrStart'
'M-<' = 'cursorFirstLine'
G = 'gotoLineOrEnd'
'M->' = 'cursorLastLine'
M-g = 'w3m.gotoLine'
Z = 'w3m.centerColumn'
z = 'w3m.centerLine'
TAB = 'cursorNextLink'
C-u = 'cursorPrevLink'
S-TAB = 'cursorPrevLink'
M-TAB = 'cursorPrevLink'
'[' = 'cursorNthLink'
']' = 'cursorRevNthLink'
# Hyperlink selection
LF = 'click'
RET = 'click'
c = 'peek'
u = 'peekCursor'
a = 'saveLink'
M-LF = 'saveLink'
M-RET = 'saveLink'
I = 'viewImage'
M-I = 'saveImage'
# File and URL-related actions
U = 'load'
V = 'load' #TODO file only
#TODO exec shell
# Buffer operations
B = 'discardBufferPrev'
v = 'toggleSource'
E = 'editFile'
C-l = 'redraw'
R = 'reload'
M-s = 'saveSource'
S = 'saveScreen'
M-e = 'editScreen'
#TODO buffer selection mode
'C-@' = 'w3m.mark'
M-p = 'w3m.prevMark'
M-n = 'w3m.nextMark'
# Search
'/' = 'searchForward'
C-s = 'searchForward'
'?' = 'searchBackward'
C-r = 'searchBackward'
n = 'searchNext'
N = 'searchPrev'
C-w = 'toggleWrap'
# Misc
#TODO shell out, help file, options, cookies
C-c = 'cancel'
q = 'w3m.askQuit'
Q = 'quit'
C-d = ''
'\' = ''
D = ''

[line]
C-o = 'line.openEditor'
'C-x C-e' = ''
