unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
* [PATCH 0/2] vim plugin: implement viewing/saving attachments
@ 2011-09-17  2:27 Ryan Harper
  2011-09-17  2:27 ` [PATCH 1/2] vim plugin: Implement show_view_all_mime_parts Ryan Harper
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Ryan Harper @ 2011-09-17  2:27 UTC (permalink / raw)
  To: notmuch

I've just started using notmuch and the vim plugin.  I'm almost ready to
ditch my mutt config, but I really needed attachment viewing/saving.

I've got something functional, but I've never hacked on vimscript before
so looking for feedback on how to clean up the patches.


Ryan
---
Give a man a beer, he'll drink for the day. Teach a man to brew, he'll be drunk the rest of his life.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 1/2] vim plugin: Implement show_view_all_mime_parts
  2011-09-17  2:27 [PATCH 0/2] vim plugin: implement viewing/saving attachments Ryan Harper
@ 2011-09-17  2:27 ` Ryan Harper
  2011-09-17  2:27 ` [PATCH 2/2] vim plugin: Implement attachment saving Ryan Harper
  2011-09-19  0:18 ` [PATCH 0/2] vim plugin: implement viewing/saving attachments Blake Sweeney
  2 siblings, 0 replies; 6+ messages in thread
From: Ryan Harper @ 2011-09-17  2:27 UTC (permalink / raw)
  To: notmuch

Implement mime attachment viewing in the vim plugin via
notmuch show --format=text parsing for attachment segments.
Extract the part ID, filename, and content-type.

Storing this in a dictionary for use later when implementing
attachment saving.

Signed-off-by: Ryan Harper <rharper@shake.ath.cx>
---
 vim/plugin/notmuch.vim |   50 +++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 49 insertions(+), 1 deletions(-)

diff --git a/vim/plugin/notmuch.vim b/vim/plugin/notmuch.vim
index 21985c7..cbc5b51 100644
--- a/vim/plugin/notmuch.vim
+++ b/vim/plugin/notmuch.vim
@@ -556,7 +556,55 @@ function! s:NM_show_reply()
 endfunction
 
 function! s:NM_show_view_all_mime_parts()
-        echo 'not implemented'
+        let info = b:nm_raw_info
+        let words = b:nm_search_words
+        let cmd = ['show', '--format=text']
+        let mid = <SID>NM_show_message_id()
+        call add(cmd, <SID>NM_show_message_id())
+        call add(cmd, 'AND')
+        call extend(cmd, <SID>NM_get_search_words())
+        let data = <SID>NM_run(cmd)
+        let lines = split(data, "\n")
+        let attachments = []
+        let bufflines = []
+
+        for line in lines
+                if match(line, g:notmuch_show_attachment_begin_regexp) != -1
+                        let m = matchlist(line, 'ID: \(\d\+\), Filename: \(.*\), Content-type: \(\S\+\)')
+                        if len(m)
+                                " create a attachment dict (id, filename, type, str)
+                                " id - message id
+                                " filename - filename from MIME attachment
+                                " type - Content-type 
+                                " str - displayable string for the buffer
+                                let att = {}
+                                let att['id'] = m[1]
+                                let att['filename']=m[2]
+                                let att['type']=m[3]
+                                let att['mid']=mid
+                                let id = att['id']
+                                let fn = att['filename']
+                                let ty = att['type']
+                                let att['str']=printf('%d %-60s[%s]', id, fn, ty)
+                                call add(attachments, att)
+                                call add(bufflines, att['str'])
+                        endif
+                endif
+
+        endfor
+        if len(attachments) == 0
+                echo 'No attachments'
+        endif
+
+        let prev_bufnr = bufnr('%')
+        setlocal bufhidden=hide
+        call <SID>NM_newBuffer('', 'show', bufflines)
+        setlocal bufhidden=delete
+        let b:attachments = attachments
+        let b:nm_prev_bufnr = prev_bufnr
+        let b:nm_raw_info = info
+        let b:nm_search_words = words
+        call <SID>NM_set_map('n', g:notmuch_show_maps)
 endfunction
 
 function! s:NM_show_view_raw_message()
-- 
1.7.6

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 2/2] vim plugin: Implement attachment saving
  2011-09-17  2:27 [PATCH 0/2] vim plugin: implement viewing/saving attachments Ryan Harper
  2011-09-17  2:27 ` [PATCH 1/2] vim plugin: Implement show_view_all_mime_parts Ryan Harper
@ 2011-09-17  2:27 ` Ryan Harper
  2012-03-03  3:12   ` Charlie Allom
  2011-09-19  0:18 ` [PATCH 0/2] vim plugin: implement viewing/saving attachments Blake Sweeney
  2 siblings, 1 reply; 6+ messages in thread
From: Ryan Harper @ 2011-09-17  2:27 UTC (permalink / raw)
  To: notmuch

Using the dictionary of attachments in a message we can implement
saving the attachment.  Select the current attachment under the cursor
request a new name, but default to the extracted name.

Currently saves to the current working directory.

Signed-off-by: Ryan Harper <rharper@shake.ath.cx>
---
 vim/plugin/notmuch.vim |   50 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 50 insertions(+), 0 deletions(-)

diff --git a/vim/plugin/notmuch.vim b/vim/plugin/notmuch.vim
index cbc5b51..efa6fca 100644
--- a/vim/plugin/notmuch.vim
+++ b/vim/plugin/notmuch.vim
@@ -158,6 +158,7 @@ let g:notmuch_show_maps = {
         \ 'd':          ':call <SID>NM_show_delete_message()<CR>',
         \ 'N':          ':call <SID>NM_show_mark_read_then_next_open_message()<CR>',
         \ 'v':          ':call <SID>NM_show_view_all_mime_parts()<CR>',
+        \ 'S':          ':call <SID>NM_show_save_mime_parts()<CR>',
         \ '+':          ':call <SID>NM_show_add_tag()<CR>',
         \ '-':          ':call <SID>NM_show_remove_tag()<CR>',
         \ '<Space>':    ':call <SID>NM_show_advance_marking_read_and_archiving()<CR>',
@@ -607,6 +608,55 @@ function! s:NM_show_view_all_mime_parts()
         call <SID>NM_set_map('n', g:notmuch_show_maps)
 endfunction
 
+function! s:NM_show_save_mime_part()
+        let attachments = b:attachments
+        let info = b:nm_raw_info
+        let words = b:nm_search_words
+        let lnum = line('.')
+        let curline = getline(lnum)
+        let curatt = {}
+
+        " need to extract the id from the current line to
+        " use to find which attachment from the list we're
+        " attempting to save
+        let m = matchlist(curline, '\(\d\+\)')
+        if len(m)
+                let id = m[1]
+                for a in attachments
+                        if a['id'] == id
+                                let curatt = a
+                                break
+                        endif
+                endfor
+        endif
+
+
+        let fn = curatt['filename']
+        let prompt = printf('Save: %s', fn)
+        let outfile = input(prompt)
+        if strlen(outfile)
+                echo outfile
+        else
+                let outfile = fn
+        endif
+
+        "build save command:
+        " notmuch show --part=<part num> <msg_id> AND <search terms> >$filename < /dev/null
+        let cmd = ["show"]
+        call add(cmd, printf("--part=%s", curatt['id']))
+        call add(cmd, curatt['mid'])
+        call add(cmd, 'AND')
+        call extend(cmd, <SID>NM_get_search_words())
+        call add(cmd, printf('>%s', outfile))
+
+        "invoke notmuch via shell
+        let nmcmd = g:notmuch_cmd . ' ' . join(cmd) . '< /dev/null'
+        let out = system(nmcmd)
+        let err = v:shell_error
+        " TODO: error handling
+
+endfunction
+
 function! s:NM_show_view_raw_message()
         echo 'not implemented'
 endfunction
-- 
1.7.6

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH 0/2] vim plugin: implement viewing/saving attachments
  2011-09-17  2:27 [PATCH 0/2] vim plugin: implement viewing/saving attachments Ryan Harper
  2011-09-17  2:27 ` [PATCH 1/2] vim plugin: Implement show_view_all_mime_parts Ryan Harper
  2011-09-17  2:27 ` [PATCH 2/2] vim plugin: Implement attachment saving Ryan Harper
@ 2011-09-19  0:18 ` Blake Sweeney
  2011-09-19 18:15   ` Ryan Harper
  2 siblings, 1 reply; 6+ messages in thread
From: Blake Sweeney @ 2011-09-19  0:18 UTC (permalink / raw)
  To: Ryan Harper, notmuch

On Fri, 16 Sep 2011 21:27:48 -0500, Ryan Harper <rharper@shake.ath.cx> wrote:
> I've just started using notmuch and the vim plugin.  I'm almost ready to ditch
> my mutt config, but I really needed attachment viewing/saving.
> 
> I've got something functional, but I've never hacked on vimscript before so
> looking for feedback on how to clean up the patches.
> 
> 
> Ryan --- Give a man a beer, he'll drink for the day. Teach a man to brew,
> he'll be drunk the rest of his life.
> 

In the second patch you have a typo, the function is called
NM_show_save_mime_part in the config but should be called
NM_show_save_mime_parts. Other than that, I tried it and like it. 

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 0/2] vim plugin: implement viewing/saving attachments
  2011-09-19  0:18 ` [PATCH 0/2] vim plugin: implement viewing/saving attachments Blake Sweeney
@ 2011-09-19 18:15   ` Ryan Harper
  0 siblings, 0 replies; 6+ messages in thread
From: Ryan Harper @ 2011-09-19 18:15 UTC (permalink / raw)
  To: Blake Sweeney, notmuch

On Sun, 18 Sep 2011 20:18:56 -0400, Blake Sweeney <blakes.85@gmail.com> wrote:
> On Fri, 16 Sep 2011 21:27:48 -0500, Ryan Harper <rharper@shake.ath.cx> wrote:
> > I've just started using notmuch and the vim plugin.  I'm almost ready to ditch
> > my mutt config, but I really needed attachment viewing/saving.
> > 
> > I've got something functional, but I've never hacked on vimscript before so
> > looking for feedback on how to clean up the patches.
> > 
> > 
> > Ryan --- Give a man a beer, he'll drink for the day. Teach a man to brew,
> > he'll be drunk the rest of his life.
> > 
> 
> In the second patch you have a typo, the function is called
> NM_show_save_mime_part in the config but should be called
> NM_show_save_mime_parts. Other than that, I tried it and like it. 

Blake,

thanks for testing and catching that.  I did the patches out of repository and
was transplanting back to the git repo.  I'll fixup and resubmit.


Ryan
-- 
Give a man a beer, he'll drink for the day. Teach a man to brew, he'll be drunk the rest of his life.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 2/2] vim plugin: Implement attachment saving
  2011-09-17  2:27 ` [PATCH 2/2] vim plugin: Implement attachment saving Ryan Harper
@ 2012-03-03  3:12   ` Charlie Allom
  0 siblings, 0 replies; 6+ messages in thread
From: Charlie Allom @ 2012-03-03  3:12 UTC (permalink / raw)
  To: notmuch

Ryan Harper <rharper@...> writes:

> Using the dictionary of attachments in a message we can implement
> saving the attachment.  Select the current attachment under the cursor
> request a new name, but default to the extracted name.
>

Hi Ryan,

can you see why I might be getting these errors on 'S'?

here is my patch https://p.6core.net/p/0jzvp42a5rn8bhny

Error detected while processing function <SNR>14_NM_show_save_mime_parts:
line    1:
E121: Undefined variable: b:attachments
E15: Invalid expression: b:attachments
line   14:
E121: Undefined variable: attachments
E15: Invalid expression: attachments
line   23:
E716: Key not present in Dictionary: filename
E15: Invalid expression: curatt['filename']
line   24:
E121: Undefined variable: fn
E116: Invalid arguments for function printf('Save: %s', fn)
E15: Invalid expression: printf('Save: %s', fn)
line   25:
E121: Undefined variable: prompt
E116: Invalid arguments for function input(prompt)
E15: Invalid expression: input(prompt)
line   26:
E121: Undefined variable: outfile
E116: Invalid arguments for function strlen(outfile)
E15: Invalid expression: strlen(outfile)
line   35:
E716: Key not present in Dictionary: id
E116: Invalid arguments for function printf("--part=%s", curatt['id']))
E116: Invalid arguments for function add
line   36:
E716: Key not present in Dictionary: mid
E116: Invalid arguments for function add
line   39:
E121: Undefined variable: outfile
E116: Invalid arguments for function printf('>%s', outfile))
E116: Invalid arguments for function add
Press ENTER or type command to continue

Regards,
  C.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2012-03-03  3:15 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-09-17  2:27 [PATCH 0/2] vim plugin: implement viewing/saving attachments Ryan Harper
2011-09-17  2:27 ` [PATCH 1/2] vim plugin: Implement show_view_all_mime_parts Ryan Harper
2011-09-17  2:27 ` [PATCH 2/2] vim plugin: Implement attachment saving Ryan Harper
2012-03-03  3:12   ` Charlie Allom
2011-09-19  0:18 ` [PATCH 0/2] vim plugin: implement viewing/saving attachments Blake Sweeney
2011-09-19 18:15   ` Ryan Harper

Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).