[PATCH v2] VIM: Add URI handling
On Fri, Oct 24 2014, Ian Main wrote: > Add URI handling to the vim client. You can now press 'enter' by default and > the client will parse the current line and find any 'Part's or URIs available > for opening. If there are more than one it opens the one under the cursor or > else it opens the only one available. It also supports mailto: URI's and will > compose a new message when activated. The lines are way too long in this commit message. 72-chars except in cases where there is no word breaks in the text. id:1414101891-10714-1-git-send-email-imain at stemwinder.org has also one 75-character line (and talks about 'This patch ...' which could be changed too (but is tolerated if not)). Tomi > > By default xdg-open is used for everything but mailto: which generally > does the right thing afaict. > > Note that this is now dependant on the attachment patch in order to make > the nice 'enter' behavior work for both. > > Ian > --- > vim/notmuch.txt | 3 ++- > vim/notmuch.vim | 76 > +++-- > 2 files changed, 70 insertions(+), 9 deletions(-) > > diff --git a/vim/notmuch.txt b/vim/notmuch.txt > index 838a904..5d84fde 100644 > --- a/vim/notmuch.txt > +++ b/vim/notmuch.txt > @@ -74,7 +74,8 @@ I Mark as read (-unread) > tTag (prompted) > e Extract attachment on the current 'Attachment' line or all > attachments if the cursor is elsewhere. > -v View attachment on the current 'Attachment' line. > + View email part on the current 'Part' line, or open URI under cursor > +or on line. > sSearch > pSave patches > rReply > diff --git a/vim/notmuch.vim b/vim/notmuch.vim > index 1466e50..2f76f55 100644 > --- a/vim/notmuch.vim > +++ b/vim/notmuch.vim > @@ -12,7 +12,7 @@ let g:notmuch_folders_maps = { > \ '':'folders_show_search()', > \ 's': 'folders_search_prompt()', > \ '=': 'folders_refresh()', > - \ 'c': 'compose()', > + \ 'c': 'compose("")', > \ } > > let g:notmuch_search_maps = { > @@ -25,7 +25,7 @@ let g:notmuch_search_maps = { > \ 's': 'search_search_prompt()', > \ '=': 'search_refresh()', > \ '?': 'search_info()', > - \ 'c': 'compose()', > + \ 'c': 'compose("")', > \ } > > let g:notmuch_show_maps = { > @@ -35,13 +35,13 @@ let g:notmuch_show_maps = { > \ 't': 'show_tag("")', > \ 'o': 'show_open_msg()', > \ 'e': 'show_extract_msg()', > - \ '':'show_view_attachment()', > + \ '':'show_view_magic()', > \ 's': 'show_save_msg()', > \ 'p': 'show_save_patches()', > \ 'r': 'show_reply()', > \ '?': 'show_info()', > \ '': 'show_next_msg()', > - \ 'c': 'compose()', > + \ 'c': 'compose("")', > \ } > > let g:notmuch_compose_maps = { > @@ -63,6 +63,7 @@ let s:notmuch_view_attachment_default = 'xdg-open' > let s:notmuch_attachment_tmpdir_default = '~/.notmuch/tmp' > let s:notmuch_folders_count_threads_default = 0 > let s:notmuch_compose_start_insert_default = 1 > +let s:notmuch_open_uri_default = 'xdg-open' > > function! s:new_file_buffer(type, fname) > exec printf('edit %s', a:fname) > @@ -141,8 +142,8 @@ function! s:show_reply() > end > endfunction > > -function! s:compose() > - ruby open_compose > +function! s:compose(to_email) > + ruby open_compose(VIM::evaluate('a:to_email')) > let b:compose_done = 0 > call s:set_map(g:notmuch_compose_maps) > autocmd BufDelete call s:on_compose_delete() > @@ -155,6 +156,22 @@ function! s:show_info() > ruby vim_puts get_message.inspect > endfunction > > +function! s:show_view_magic() > + let line = getline(".") > + > +ruby << EOF > + line = VIM::evaluate('line') > + > + # Easiest to check for 'Part' types first.. > + match = line.match(/^Part (\d*):/) > + if match and match.length == 2 > + VIM::command('call s:show_view_attachment()') > + else > + VIM::command('call s:show_open_uri()') > + end > +EOF > +endfunction > + > function! s:show_view_attachment() > let line = getline(".") > ruby << EOF > @@ -226,6 +243,45 @@ ruby << EOF > EOF > endfunction > > +function! s:show_open_uri() > + let line = getline(".") > + let pos = getpos(".") > + let col = pos[2] > +ruby << EOF > + m = get_message > + line = VIM::evaluate('line') > + col = VIM::evaluate('col') - 1 > + uris = URI.extract(line) > + wanted_uri = nil > + if uris.length == 1 > + wanted_uri = uris[0] > + else > + uris.each do |uri| > + # Check to see the URI is at the present cursor location > + idx = line.index(uri) > + if col >= idx and col <= idx + uri.length > +
Re: [PATCH v2] VIM: Add URI handling
On Fri, Oct 24 2014, Ian Main im...@stemwinder.org wrote: Add URI handling to the vim client. You can now press 'enter' by default and the client will parse the current line and find any 'Part's or URIs available for opening. If there are more than one it opens the one under the cursor or else it opens the only one available. It also supports mailto: URI's and will compose a new message when activated. The lines are way too long in this commit message. 72-chars except in cases where there is no word breaks in the text. id:1414101891-10714-1-git-send-email-im...@stemwinder.org has also one 75-character line (and talks about 'This patch ...' which could be changed too (but is tolerated if not)). Tomi By default xdg-open is used for everything but mailto: which generally does the right thing afaict. Note that this is now dependant on the attachment patch in order to make the nice 'enter' behavior work for both. Ian --- vim/notmuch.txt | 3 ++- vim/notmuch.vim | 76 +++-- 2 files changed, 70 insertions(+), 9 deletions(-) diff --git a/vim/notmuch.txt b/vim/notmuch.txt index 838a904..5d84fde 100644 --- a/vim/notmuch.txt +++ b/vim/notmuch.txt @@ -74,7 +74,8 @@ I Mark as read (-unread) tTag (prompted) e Extract attachment on the current 'Attachment' line or all attachments if the cursor is elsewhere. -v View attachment on the current 'Attachment' line. +enter View email part on the current 'Part' line, or open URI under cursor +or on line. sSearch pSave patches rReply diff --git a/vim/notmuch.vim b/vim/notmuch.vim index 1466e50..2f76f55 100644 --- a/vim/notmuch.vim +++ b/vim/notmuch.vim @@ -12,7 +12,7 @@ let g:notmuch_folders_maps = { \ 'Enter':'folders_show_search()', \ 's': 'folders_search_prompt()', \ '=': 'folders_refresh()', - \ 'c': 'compose()', + \ 'c': 'compose()', \ } let g:notmuch_search_maps = { @@ -25,7 +25,7 @@ let g:notmuch_search_maps = { \ 's': 'search_search_prompt()', \ '=': 'search_refresh()', \ '?': 'search_info()', - \ 'c': 'compose()', + \ 'c': 'compose()', \ } let g:notmuch_show_maps = { @@ -35,13 +35,13 @@ let g:notmuch_show_maps = { \ 't': 'show_tag()', \ 'o': 'show_open_msg()', \ 'e': 'show_extract_msg()', - \ 'Enter':'show_view_attachment()', + \ 'Enter':'show_view_magic()', \ 's': 'show_save_msg()', \ 'p': 'show_save_patches()', \ 'r': 'show_reply()', \ '?': 'show_info()', \ 'Tab': 'show_next_msg()', - \ 'c': 'compose()', + \ 'c': 'compose()', \ } let g:notmuch_compose_maps = { @@ -63,6 +63,7 @@ let s:notmuch_view_attachment_default = 'xdg-open' let s:notmuch_attachment_tmpdir_default = '~/.notmuch/tmp' let s:notmuch_folders_count_threads_default = 0 let s:notmuch_compose_start_insert_default = 1 +let s:notmuch_open_uri_default = 'xdg-open' function! s:new_file_buffer(type, fname) exec printf('edit %s', a:fname) @@ -141,8 +142,8 @@ function! s:show_reply() end endfunction -function! s:compose() - ruby open_compose +function! s:compose(to_email) + ruby open_compose(VIM::evaluate('a:to_email')) let b:compose_done = 0 call s:set_map(g:notmuch_compose_maps) autocmd BufDelete buffer call s:on_compose_delete() @@ -155,6 +156,22 @@ function! s:show_info() ruby vim_puts get_message.inspect endfunction +function! s:show_view_magic() + let line = getline(.) + +ruby EOF + line = VIM::evaluate('line') + + # Easiest to check for 'Part' types first.. + match = line.match(/^Part (\d*):/) + if match and match.length == 2 + VIM::command('call s:show_view_attachment()') + else + VIM::command('call s:show_open_uri()') + end +EOF +endfunction + function! s:show_view_attachment() let line = getline(.) ruby EOF @@ -226,6 +243,45 @@ ruby EOF EOF endfunction +function! s:show_open_uri() + let line = getline(.) + let pos = getpos(.) + let col = pos[2] +ruby EOF + m = get_message + line = VIM::evaluate('line') + col = VIM::evaluate('col') - 1 + uris = URI.extract(line) + wanted_uri = nil + if uris.length == 1 + wanted_uri = uris[0] + else + uris.each do |uri| + # Check to see the URI is at the present cursor location + idx = line.index(uri) + if col = idx and col = idx + uri.length + wanted_uri = uri + break + end +
[PATCH v2] VIM: Add URI handling
Add URI handling to the vim client. You can now press 'enter' by default and the client will parse the current line and find any 'Part's or URIs available for opening. If there are more than one it opens the one under the cursor or else it opens the only one available. It also supports mailto: URI's and will compose a new message when activated. By default xdg-open is used for everything but mailto: which generally does the right thing afaict. Note that this is now dependant on the attachment patch in order to make the nice 'enter' behavior work for both. Ian --- vim/notmuch.txt | 3 ++- vim/notmuch.vim | 76 +++-- 2 files changed, 70 insertions(+), 9 deletions(-) diff --git a/vim/notmuch.txt b/vim/notmuch.txt index 838a904..5d84fde 100644 --- a/vim/notmuch.txt +++ b/vim/notmuch.txt @@ -74,7 +74,8 @@ I Mark as read (-unread) t Tag (prompted) e Extract attachment on the current 'Attachment' line or all attachments if the cursor is elsewhere. -v View attachment on the current 'Attachment' line. + View email part on the current 'Part' line, or open URI under cursor +or on line. s Search p Save patches r Reply diff --git a/vim/notmuch.vim b/vim/notmuch.vim index 1466e50..2f76f55 100644 --- a/vim/notmuch.vim +++ b/vim/notmuch.vim @@ -12,7 +12,7 @@ let g:notmuch_folders_maps = { \ '':'folders_show_search()', \ 's': 'folders_search_prompt()', \ '=': 'folders_refresh()', - \ 'c': 'compose()', + \ 'c': 'compose("")', \ } let g:notmuch_search_maps = { @@ -25,7 +25,7 @@ let g:notmuch_search_maps = { \ 's': 'search_search_prompt()', \ '=': 'search_refresh()', \ '?': 'search_info()', - \ 'c': 'compose()', + \ 'c': 'compose("")', \ } let g:notmuch_show_maps = { @@ -35,13 +35,13 @@ let g:notmuch_show_maps = { \ 't': 'show_tag("")', \ 'o': 'show_open_msg()', \ 'e': 'show_extract_msg()', - \ '':'show_view_attachment()', + \ '':'show_view_magic()', \ 's': 'show_save_msg()', \ 'p': 'show_save_patches()', \ 'r': 'show_reply()', \ '?': 'show_info()', \ '': 'show_next_msg()', - \ 'c': 'compose()', + \ 'c': 'compose("")', \ } let g:notmuch_compose_maps = { @@ -63,6 +63,7 @@ let s:notmuch_view_attachment_default = 'xdg-open' let s:notmuch_attachment_tmpdir_default = '~/.notmuch/tmp' let s:notmuch_folders_count_threads_default = 0 let s:notmuch_compose_start_insert_default = 1 +let s:notmuch_open_uri_default = 'xdg-open' function! s:new_file_buffer(type, fname) exec printf('edit %s', a:fname) @@ -141,8 +142,8 @@ function! s:show_reply() end endfunction -function! s:compose() - ruby open_compose +function! s:compose(to_email) + ruby open_compose(VIM::evaluate('a:to_email')) let b:compose_done = 0 call s:set_map(g:notmuch_compose_maps) autocmd BufDelete call s:on_compose_delete() @@ -155,6 +156,22 @@ function! s:show_info() ruby vim_puts get_message.inspect endfunction +function! s:show_view_magic() + let line = getline(".") + +ruby << EOF + line = VIM::evaluate('line') + + # Easiest to check for 'Part' types first.. + match = line.match(/^Part (\d*):/) + if match and match.length == 2 + VIM::command('call s:show_view_attachment()') + else + VIM::command('call s:show_open_uri()') + end +EOF +endfunction + function! s:show_view_attachment() let line = getline(".") ruby << EOF @@ -226,6 +243,45 @@ ruby << EOF EOF endfunction +function! s:show_open_uri() + let line = getline(".") + let pos = getpos(".") + let col = pos[2] +ruby << EOF + m = get_message + line = VIM::evaluate('line') + col = VIM::evaluate('col') - 1 + uris = URI.extract(line) + wanted_uri = nil + if uris.length == 1 + wanted_uri = uris[0] + else + uris.each do |uri| + # Check to see the URI is at the present cursor location + idx = line.index(uri) + if col >= idx and col <= idx + uri.length + wanted_uri = uri + break + end + end + end + + if wanted_uri + uri = URI.parse(wanted_uri) + if uri.class == URI::MailTo + vim_puts("Composing new email to #{uri.to}.") + VIM::command("call s:compose('#{uri.to}')") + else + vim_puts("Opening #{uri.to_s}.") + cmd =
[PATCH v2] VIM: Add URI handling
Add URI handling to the vim client. You can now press 'enter' by default and the client will parse the current line and find any 'Part's or URIs available for opening. If there are more than one it opens the one under the cursor or else it opens the only one available. It also supports mailto: URI's and will compose a new message when activated. By default xdg-open is used for everything but mailto: which generally does the right thing afaict. Note that this is now dependant on the attachment patch in order to make the nice 'enter' behavior work for both. Ian --- vim/notmuch.txt | 3 ++- vim/notmuch.vim | 76 +++-- 2 files changed, 70 insertions(+), 9 deletions(-) diff --git a/vim/notmuch.txt b/vim/notmuch.txt index 838a904..5d84fde 100644 --- a/vim/notmuch.txt +++ b/vim/notmuch.txt @@ -74,7 +74,8 @@ I Mark as read (-unread) t Tag (prompted) e Extract attachment on the current 'Attachment' line or all attachments if the cursor is elsewhere. -v View attachment on the current 'Attachment' line. +enter View email part on the current 'Part' line, or open URI under cursor +or on line. s Search p Save patches r Reply diff --git a/vim/notmuch.vim b/vim/notmuch.vim index 1466e50..2f76f55 100644 --- a/vim/notmuch.vim +++ b/vim/notmuch.vim @@ -12,7 +12,7 @@ let g:notmuch_folders_maps = { \ 'Enter':'folders_show_search()', \ 's': 'folders_search_prompt()', \ '=': 'folders_refresh()', - \ 'c': 'compose()', + \ 'c': 'compose()', \ } let g:notmuch_search_maps = { @@ -25,7 +25,7 @@ let g:notmuch_search_maps = { \ 's': 'search_search_prompt()', \ '=': 'search_refresh()', \ '?': 'search_info()', - \ 'c': 'compose()', + \ 'c': 'compose()', \ } let g:notmuch_show_maps = { @@ -35,13 +35,13 @@ let g:notmuch_show_maps = { \ 't': 'show_tag()', \ 'o': 'show_open_msg()', \ 'e': 'show_extract_msg()', - \ 'Enter':'show_view_attachment()', + \ 'Enter':'show_view_magic()', \ 's': 'show_save_msg()', \ 'p': 'show_save_patches()', \ 'r': 'show_reply()', \ '?': 'show_info()', \ 'Tab': 'show_next_msg()', - \ 'c': 'compose()', + \ 'c': 'compose()', \ } let g:notmuch_compose_maps = { @@ -63,6 +63,7 @@ let s:notmuch_view_attachment_default = 'xdg-open' let s:notmuch_attachment_tmpdir_default = '~/.notmuch/tmp' let s:notmuch_folders_count_threads_default = 0 let s:notmuch_compose_start_insert_default = 1 +let s:notmuch_open_uri_default = 'xdg-open' function! s:new_file_buffer(type, fname) exec printf('edit %s', a:fname) @@ -141,8 +142,8 @@ function! s:show_reply() end endfunction -function! s:compose() - ruby open_compose +function! s:compose(to_email) + ruby open_compose(VIM::evaluate('a:to_email')) let b:compose_done = 0 call s:set_map(g:notmuch_compose_maps) autocmd BufDelete buffer call s:on_compose_delete() @@ -155,6 +156,22 @@ function! s:show_info() ruby vim_puts get_message.inspect endfunction +function! s:show_view_magic() + let line = getline(.) + +ruby EOF + line = VIM::evaluate('line') + + # Easiest to check for 'Part' types first.. + match = line.match(/^Part (\d*):/) + if match and match.length == 2 + VIM::command('call s:show_view_attachment()') + else + VIM::command('call s:show_open_uri()') + end +EOF +endfunction + function! s:show_view_attachment() let line = getline(.) ruby EOF @@ -226,6 +243,45 @@ ruby EOF EOF endfunction +function! s:show_open_uri() + let line = getline(.) + let pos = getpos(.) + let col = pos[2] +ruby EOF + m = get_message + line = VIM::evaluate('line') + col = VIM::evaluate('col') - 1 + uris = URI.extract(line) + wanted_uri = nil + if uris.length == 1 + wanted_uri = uris[0] + else + uris.each do |uri| + # Check to see the URI is at the present cursor location + idx = line.index(uri) + if col = idx and col = idx + uri.length + wanted_uri = uri + break + end + end + end + + if wanted_uri + uri = URI.parse(wanted_uri) + if uri.class == URI::MailTo + vim_puts(Composing new email to #{uri.to}.) + VIM::command(call s:compose('#{uri.to}')) + else + vim_puts(Opening #{uri.to_s}.) +