Major changes to plugin structure and options, doc updates, and ftplugin added with sane defaults for viewing net responses

This commit is contained in:
sloum 2023-07-05 15:44:04 -07:00
parent d2ae97728e
commit 3d2d1e99c3
4 changed files with 153 additions and 66 deletions

View File

@ -3,6 +3,7 @@ LANGNAME := netclient
DOC := /doc
PLUG := /plugin
FT := /ftplugin
ifeq ($(OS), Windows_NT)
ROOT := $(HOME)/vimfiles
@ -17,11 +18,14 @@ endif
install: .${PLUG}/${LANGNAME}.vim .${DOC}/${LANGNAME}.txt
install -d ${ROOT}${DOC}
install -d ${ROOT}${PLUG}
install -d ${ROOT}${FT}
install -m 0644 .${DOC}/${LANGNAME}.txt ${ROOT}${DOC}
install -m 0644 .${PLUG}/${LANGNAME}.vim ${ROOT}${PLUG}
install -m 0644 .${FT}/${LANGNAME}.vim ${ROOT}${FT}
vim -c 'execute ":helptags ~/.vim/doc"|q'
.PHONY: remove
remove:
rm -rf ${ROOT}${DOC}/${LANGNAME}.txt
rm -rf ${ROOT}${PLUG}/${LANGNAME}.vim
rm -rf ${ROOT}${FT}/${LANGNAME}.vim

View File

@ -6,40 +6,54 @@ handles the browsing itself. The backend is customizable.
It supports:
- History (Forward/Back)
- Reloading of the current page
- Opening addresses
- Goto (for links, by number—when formatted in a known way, or by URL in text)
- Opening addresses (in the current buffer, or a new buffer)
- Follow links, by number—when formatted in a known way—or by URL in text
- Swapable client backends
- Configurable command leader
- Configurable behavior for multiple buffers
Because netclient only uses information visible on the page, you can go
back using Vim's standard undo feature (u). Having gone back by pressing
u, you can go forward with redo (<C-r>). Each render places the current
URL on the first line of the buffer. To refresh, you can navigate to
that URL and follow it—a new request to the URL will be made.
MAPPINGS *netclient-mappings*
<g:netclient_leader>o
Will query for a URL and then attempt to navigate to that URL.
Will open in the current buffer, unless said buffer is not a
netclient buffer, in which case it will be treated as O, not o.
[number?]<g:netclient_leader>g
<g:netclient_leader>O
Will query for a URL and then attempt to navigate to that URL.
Will open in a new buffer, making a split if configured to do so.
[number?]<g:netclient_leader>f
The `[number?]` above is optional. If you type a number before
the browser leader and `g` the browser will attempt to navigate
to the link with the given number. If no number is given, the
browser will assume that your cursor is over a URL and will try
to navigate to that URL.
Will follow in the current buffer, unless said buffer is not a
netclient buffer, in which case it will be treated as F, not f.
[number?]<g:netclient_leader>F
The `[number?]` above is optional. If you type a number before
the browser leader and `g` the browser will attempt to navigate
to the link with the given number. If no number is given, the
browser will assume that your cursor is over a URL and will try
to navigate to that URL.
Will follow in a new buffer, making a split if configured to do so.
[number]<g:netclient_leader>u
Unlike with the `g` mapping, the number here is required. The
client will report the URL associated with the given link
number, but will not navigate to it.
<g:netclient_leader>b
Go BACK in history by one URL
<g:netclient_leader>f
Go FORWARD in history by one URL
<g:netclient_leader>r
Reload the current page. A new request will be sent.
CONFIGURATION *netclient-config*
@ -60,6 +74,11 @@ netclient can be configured in a number of different ways.
a similar format for most available backends (or build
your own backend).
The lynx setup is the default, as it is the most common
package available supporting the format required by
netclient. The custom netclient client backend is
called `ncg` and is packaged separately.
2. Customize the leader key
The default leader it " " (two spaces). You can change it
@ -70,3 +89,15 @@ netclient can be configured in a number of different ways.
Once it is set, all commands will be prefixed with that
leader.
3. Customize new buffer behavior
The default is to open new buffers in the same window. This,
however, can be updated to use a new split by setting the
following:
let g:netclient_buffer_style = 1
If instead of a split, you would like a tab, you can:
let g:netclient_buffer_style = 1

8
ftplugin/netclient.vim Normal file
View File

@ -0,0 +1,8 @@
if exists("g:loaded_netclient_ft")
finish
endif
let g:loaded_netclient_ft = 1
set wrap
set linebreak
set nolist

View File

@ -4,25 +4,32 @@
" Maintainer: Sloum <sloum@rawtext.club>
" License: Vim license
if exists("g:loaded_netclient")
if exists("g:loaded_netclient_global")
finish
endif
let g:loaded_netclient = 1
let g:loaded_netclient_global = 1
let save_cpo = &cpo
set cpo&vim
setlocal tabstop=3
setlocal softtabstop=3
setlocal shiftwidth=3
setlocal nocindent
setlocal expandtab
let s:history = []
let s:index = -1
if !exists("g:netclient_command")
let g:netclient_command = "lynx -dump -dont_wrap_pre %s"
" XXX
" A client backend written in go that supports gopher,
" gemini, and http/https called `ncg` is specifically
" written for this plugin and is available from:
" https://git.rawtext.club/sloum/ncg
" If you get it, comment the above lynx line and
" uncomment the below line to use it.
"
" let g:netclient_command = "ncg %s"
endif
if !exists("g:netclient_buffer_style")
" 0 - New buffer in same window
" 1 - New buffer in new split
" 2 - New buffer in new tab
let g:netclient_buffer_style = 0
endif
" Sets the local leader to ' ' (space space)
@ -37,25 +44,7 @@ function NetclientClearBuffer()
call deletebufline(bufname(), 1, line("$"))
endfunction
function NetclientBack()
if s:index < 1
echo "Already at the beginning of history"
return
endif
let s:index = s:index - 1
call NetclientGetU(s:history[s:index])
endfunction
function NetclientForward()
if s:index + 1 >= len(s:history)
echo "Already at the end of history"
return
endif
let s:index = s:index + 1
call NetclientGetU(s:history[s:index])
endfunction
function NetclientFollowURL(num)
function NetclientFindURL(num)
let l:url = ""
if a:num
let l:exp = "^\\s\\+". a:num . "\\.\\s*\\(\\S\\+\\)"
@ -68,51 +57,106 @@ function NetclientFollowURL(num)
else
let l:url = expand("<cWORD>")
endif
return l:url
endfunction
function NetclientFollowURLnew(num)
let l:url = NetclientFindURL(a:num)
let l:isURL = match(l:url, '\w\+:\/\/\S\+')
if l:isURL >= 0
call NetclientAddToHistory(l:url)
if g:netclient_buffer_style
vnew
else
enew
endif
set filetype=netclient
call NetclientGetU(l:url)
else
echo "Invalid URL"
endif
endfunction
function NetclientRefresh()
if s:index < 0
echo "There is nothing to refresh"
function NetclientFollowURL(num)
let l:valid_buf = NetclientIsNCbuffer()
if !l:valid_buf
call NetclientFollowURLnew(a:num)
return
endif
call NetclientGetU(s:history[s:index])
endfunction
let l:url = NetclientFindURL(a:num)
let l:isURL = match(l:url, '\w\+:\/\/\S\+')
function NetclientAddToHistory(url)
if s:index < 0
let s:history = add(s:history, a:url)
if l:isURL >= 0
call NetclientGetU(l:url)
else
let s:history = add(s:history[0:s:index], a:url)
echo "Invalid URL"
endif
let s:index = s:index + 1
endfunction
function NetclientIsNCbuffer()
let l:f = &filetype
if l:f == "netclient"
return 1
elseif l:f == ""
set filetype=netclient
return 1
else
return 0
endif
endfunction
function NetclientGoNew()
if g:netclient_buffer_style == 1
vnew
elseif g:netclient_buffer_style == 2
tabnew
else
enew
endif
set filetype=netclient
call NetclientGo()
endfunction
function NetclientGo()
let l:valid = NetclientIsNCbuffer()
if !l:valid
call NetclientGoNew()
return
endif
let l:u = trim(input("GO: "))
if l:u == ""
return
endif
call NetclientAddToHistory(l:u)
call NetclientGetU(l:u)
endfunction
function NetclientGetU(u)
let l:txt = system(printf(g:netclient_command, a:u))
let l:resp = substitute(l:txt, "[\\x0]", "\\n", "g")
" TODO parse for links and add the links to the links array
" TODO add a history array and a history pointer var
call NetclientClearBuffer()
call append(1, split(l:resp, "\n"))
if v:shell_error == 0
call NetclientClearBuffer()
call append(0,[a:u, ""] + split(l:resp, "\n"))
call cursor(1,1)
elseif v:shell_error == 2
" gemini line request
let l:query = trim(input(l:resp))
let l:qloc = match(u, "?")
if l:qloc < 0
call NetclientGetU(u . "?" . l:query)
else
call NetclientGetU(u[: l:qloc] . l:query)
endif
elseif v:shell_error == 3
let l:query = trim(input(l:resp))
let l:qloc = match(u, "\t")
if l:qloc < 0
call NetclientGetU(u . "\t" . l:query)
else
call NetclientGetU(u[: l:qloc] . l:query)
endif
else
echo l:resp
endif
endfunction
function NetclientCheckURL(num)
@ -126,11 +170,11 @@ function NetclientCheckURL(num)
echo l:url
endfunction
nnoremap <buffer> <localleader>o :call NetclientGo()<cr>
nnoremap <buffer> <localleader>g :<C-U>call NetclientFollowURL(v:count)<cr>
nnoremap <buffer> <localleader>b :call NetclientBack()<cr>
nnoremap <buffer> <localleader>f :call NetclientForward()<cr>
nnoremap <buffer> <localleader>r :call NetclientRefresh()<cr>
nnoremap <buffer> <localleader>u :<C-U>call NetclientCheckURL(v:count)<cr>
nnoremap <localleader>O :call NetclientGoNew()<cr>
nnoremap <localleader>o :call NetclientGo()<cr>
nnoremap <localleader>f :<C-U>call NetclientFollowURL(v:count)<cr>
nnoremap <localleader>F :<C-U>call NetclientFollowURLnew(v:count)<cr>
nnoremap <localleader>u :<C-U>call NetclientCheckURL(v:count)<cr>
let &cpo = save_cpo