unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
* [PATCH v4 1/2] VIM: Add better attachment support
@ 2014-10-24 16:38 Ian Main
  2014-10-24 16:38 ` [PATCH v4 2/2] VIM: Add URI handling Ian Main
  2014-10-25 10:41 ` [PATCH v4 1/2] VIM: Add better attachment support Tomi Ollila
  0 siblings, 2 replies; 4+ messages in thread
From: Ian Main @ 2014-10-24 16:38 UTC (permalink / raw)
  To: notmuch

Change how the notmuch vim client supports attachments:

- For each message part a 'Part <number>: <filename>' is added to the
  header.
- You can then use 'e' to extract the attachment under the cursor or
  use it elsewhere to extract all attachments (the prior behavior)
- You can use 'v' to 'view' the attachment/part using xdg-open by
  default.
- If the message is 'text/html' we include a 'Part:' for the body of
  the message so you can easily view it in a web browser if you so
  choose.

    Ian
---

- Fixed commit message
- Fixed documentation

 vim/notmuch.txt |  8 +++++-
 vim/notmuch.vim | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 89 insertions(+), 3 deletions(-)

diff --git a/vim/notmuch.txt b/vim/notmuch.txt
index 4374102..d5e1ad2 100644
--- a/vim/notmuch.txt
+++ b/vim/notmuch.txt
@@ -72,6 +72,9 @@ q	Quit view
 A	Archive (-inbox -unread)
 I	Mark as read (-unread)
 t	Tag (prompted)
+e       Extract attachment on the current 'Part' line or all
+	attachments if the cursor is elsewhere.
+<enter> View attachment on the current 'Part' line.
 s	Search
 p	Save patches
 r	Reply
@@ -148,6 +151,9 @@ You can also configure your externail mail reader and sendemail program:
 >
 	let g:notmuch_reader = 'mutt -f %s'
 	let g:notmuch_sendmail = 'sendmail'
-<
+
+You can also configure what probram is used to view attachments:
+
+	let g:notmuch_view_attachment = 'xdg-open'
 
 vim:tw=78:ts=8:noet:ft=help:
diff --git a/vim/notmuch.vim b/vim/notmuch.vim
index cad9517..1466e50 100644
--- a/vim/notmuch.vim
+++ b/vim/notmuch.vim
@@ -35,6 +35,7 @@ let g:notmuch_show_maps = {
 	\ 't':		'show_tag("")',
 	\ 'o':		'show_open_msg()',
 	\ 'e':		'show_extract_msg()',
+	\ '<Enter>':	'show_view_attachment()',
 	\ 's':		'show_save_msg()',
 	\ 'p':		'show_save_patches()',
 	\ 'r':		'show_reply()',
@@ -58,6 +59,8 @@ let s:notmuch_date_format_default = '%d.%m.%y'
 let s:notmuch_datetime_format_default = '%d.%m.%y %H:%M:%S'
 let s:notmuch_reader_default = 'mutt -f %s'
 let s:notmuch_sendmail_default = 'sendmail'
+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
 
@@ -152,13 +155,72 @@ function! s:show_info()
 	ruby vim_puts get_message.inspect
 endfunction
 
+function! s:show_view_attachment()
+	let line = getline(".")
+ruby << EOF
+	m = get_message
+	line = VIM::evaluate('line')
+
+	match = line.match(/^Part (\d*):/)
+	if match and match.length == 2
+		# Set up the tmpdir
+		tmpdir = VIM::evaluate('g:notmuch_attachment_tmpdir')
+		tmpdir = File.expand_path(tmpdir)
+		Dir.mkdir(tmpdir) unless Dir.exists?(tmpdir)
+
+		p = m.mail.parts[match[1].to_i - 1]
+		if p == nil
+			# Not a multipart message, use the message itself.
+			p = m.mail
+		end
+		if p.filename and p.filename.length > 0
+			filename = p.filename
+		else
+			suffix = ''
+			if p.mime_type == 'text/html'
+				suffix = '.html'
+			end
+			filename = "part-#{match[1]}#{suffix}"
+		end
+
+		# Sanitize just in case..
+		filename.gsub!(/[^0-9A-Za-z.\-]/, '_')
+
+		fullpath = File.expand_path("#{tmpdir}/#{filename}")
+		vim_puts "Viewing attachment #{fullpath}"
+		File.open(fullpath, 'w') do |f|
+			f.write p.body.decoded
+			cmd = VIM::evaluate('g:notmuch_view_attachment')
+			system(cmd, fullpath)
+		end
+	else
+		vim_puts "No attachment on this line."
+	end
+EOF
+endfunction
+
 function! s:show_extract_msg()
+	let line = getline(".")
 ruby << EOF
 	m = get_message
-	m.mail.attachments.each do |a|
+	line = VIM::evaluate('line')
+
+	# If the user is on a line that has an 'Part'
+	# line, we just extract the one attachment.
+	match = line.match(/^Part (\d*):/)
+	if match and match.length == 2
+		a = m.mail.parts[match[1].to_i - 1]
 		File.open(a.filename, 'w') do |f|
 			f.write a.body.decoded
-			print "Extracted '#{a.filename}'"
+			vim_puts "Extracted #{a.filename}"
+		end
+	else
+		# Extract them all..
+		m.mail.attachments.each do |a|
+			File.open(a.filename, 'w') do |f|
+				f.write a.body.decoded
+				vim_puts "Extracted #{a.filename}"
+			end
 		end
 	end
 EOF
@@ -331,6 +393,16 @@ ruby << EOF
 			b << "To: %s" % msg['to']
 			b << "Cc: %s" % msg['cc']
 			b << "Date: %s" % msg['date']
+			cnt = 0
+			m.parts.each do |p|
+				cnt += 1
+				b << "Part %d: %s (%s)" % [cnt, p.mime_type, p.filename]
+			end
+			# Add a special case for text/html messages.  Here we show the
+			# only 'part' so that we can view it in a web browser if we want.
+			if m.parts.length == 0 and part.mime_type == 'text/html'
+				b << "Part 1: text/html"
+			end
 			nm_m.body_start = b.count
 			b << "--- %s ---" % part.mime_type
 			part.convert.each_line do |l|
@@ -425,6 +497,14 @@ function! s:set_defaults()
 		endif
 	endif
 
+	if !exists('g:notmuch_attachment_tmpdir')
+		let g:notmuch_attachment_tmpdir = s:notmuch_attachment_tmpdir_default
+	endif
+
+	if !exists('g:notmuch_view_attachment')
+		let g:notmuch_view_attachment = s:notmuch_view_attachment_default
+	endif
+
 	if !exists('g:notmuch_folders_count_threads')
 		if exists('g:notmuch_rb_count_threads')
 			let g:notmuch_count_threads = g:notmuch_rb_count_threads
-- 
1.9.3

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

* [PATCH v4 2/2] VIM: Add URI handling
  2014-10-24 16:38 [PATCH v4 1/2] VIM: Add better attachment support Ian Main
@ 2014-10-24 16:38 ` Ian Main
  2014-10-25 10:41 ` [PATCH v4 1/2] VIM: Add better attachment support Tomi Ollila
  1 sibling, 0 replies; 4+ messages in thread
From: Ian Main @ 2014-10-24 16:38 UTC (permalink / raw)
  To: notmuch

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

---
Fixed commit message.

 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 d5e1ad2..f51b20f 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 'Part' line or all
 	attachments if the cursor is elsewhere.
-<enter> View attachment on the current 'Part' 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}.")
+			cmd = VIM::evaluate('g:notmuch_open_uri')
+			system(cmd, uri.to_s)
+		end
+	else
+		vim_puts('URI not found.')
+	end
+EOF
+endfunction
+
 function! s:show_open_msg()
 ruby << EOF
 	m = get_message
@@ -481,6 +537,10 @@ function! s:set_defaults()
 		endif
 	endif
 
+	if !exists('g:notmuch_open_uri')
+		let g:notmuch_open_uri = s:notmuch_open_uri_default
+	endif
+
 	if !exists('g:notmuch_reader')
 		if exists('g:notmuch_rb_reader')
 			let g:notmuch_reader = g:notmuch_rb_reader
@@ -693,11 +753,11 @@ ruby << EOF
 		open_compose_helper(lines, cur)
 	end
 
-	def open_compose()
+	def open_compose(to_email)
 		lines = []
 
 		lines << "From: #{$email}"
-		lines << "To: "
+		lines << "To: #{to_email}"
 		cur = lines.count
 
 		lines << "Cc: "
-- 
1.9.3

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

* Re: [PATCH v4 1/2] VIM: Add better attachment support
  2014-10-24 16:38 [PATCH v4 1/2] VIM: Add better attachment support Ian Main
  2014-10-24 16:38 ` [PATCH v4 2/2] VIM: Add URI handling Ian Main
@ 2014-10-25 10:41 ` Tomi Ollila
  2014-10-27 18:35   ` Ian Main
  1 sibling, 1 reply; 4+ messages in thread
From: Tomi Ollila @ 2014-10-25 10:41 UTC (permalink / raw)
  To: Ian Main, notmuch

On Fri, Oct 24 2014, Ian Main <imain@stemwinder.org> wrote:

> Change how the notmuch vim client supports attachments:
>
> - For each message part a 'Part <number>: <filename>' is added to the
>   header.
> - You can then use 'e' to extract the attachment under the cursor or
>   use it elsewhere to extract all attachments (the prior behavior)
> - You can use 'v' to 'view' the attachment/part using xdg-open by
>   default.
> - If the message is 'text/html' we include a 'Part:' for the body of
>   the message so you can easily view it in a web browser if you so
>   choose.
>
>     Ian
> ---
>
> - Fixed commit message
> - Fixed documentation
>
>  vim/notmuch.txt |  8 +++++-
>  vim/notmuch.vim | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--
>  2 files changed, 89 insertions(+), 3 deletions(-)
>
> diff --git a/vim/notmuch.txt b/vim/notmuch.txt
> index 4374102..d5e1ad2 100644
> --- a/vim/notmuch.txt
> +++ b/vim/notmuch.txt
> @@ -72,6 +72,9 @@ q	Quit view
>  A	Archive (-inbox -unread)
>  I	Mark as read (-unread)
>  t	Tag (prompted)
> +e       Extract attachment on the current 'Part' line or all

This and the following patch use spaces instead of tab in the line above
(and following patch few lines below) -- the indentation looked weird and
that got me to look more.

I was going to look more of this but run out of time. I'll look the
this through after someone else who uses vim has tested these patched
and reported their experiences.

Tomi


> +	attachments if the cursor is elsewhere.
> +<enter> View attachment on the current 'Part' line.
>  s	Search
>  p	Save patches
>  r	Reply
> @@ -148,6 +151,9 @@ You can also configure your externail mail reader and sendemail program:
>  >
>  	let g:notmuch_reader = 'mutt -f %s'
>  	let g:notmuch_sendmail = 'sendmail'
> -<
> +
> +You can also configure what probram is used to view attachments:
> +
> +	let g:notmuch_view_attachment = 'xdg-open'
>  
>  vim:tw=78:ts=8:noet:ft=help:
> diff --git a/vim/notmuch.vim b/vim/notmuch.vim
> index cad9517..1466e50 100644
> --- a/vim/notmuch.vim
> +++ b/vim/notmuch.vim
> @@ -35,6 +35,7 @@ let g:notmuch_show_maps = {
>  	\ 't':		'show_tag("")',
>  	\ 'o':		'show_open_msg()',
>  	\ 'e':		'show_extract_msg()',
> +	\ '<Enter>':	'show_view_attachment()',
>  	\ 's':		'show_save_msg()',
>  	\ 'p':		'show_save_patches()',
>  	\ 'r':		'show_reply()',
> @@ -58,6 +59,8 @@ let s:notmuch_date_format_default = '%d.%m.%y'
>  let s:notmuch_datetime_format_default = '%d.%m.%y %H:%M:%S'
>  let s:notmuch_reader_default = 'mutt -f %s'
>  let s:notmuch_sendmail_default = 'sendmail'
> +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
>  
> @@ -152,13 +155,72 @@ function! s:show_info()
>  	ruby vim_puts get_message.inspect
>  endfunction
>  
> +function! s:show_view_attachment()
> +	let line = getline(".")
> +ruby << EOF
> +	m = get_message
> +	line = VIM::evaluate('line')
> +
> +	match = line.match(/^Part (\d*):/)
> +	if match and match.length == 2
> +		# Set up the tmpdir
> +		tmpdir = VIM::evaluate('g:notmuch_attachment_tmpdir')
> +		tmpdir = File.expand_path(tmpdir)
> +		Dir.mkdir(tmpdir) unless Dir.exists?(tmpdir)
> +
> +		p = m.mail.parts[match[1].to_i - 1]
> +		if p == nil
> +			# Not a multipart message, use the message itself.
> +			p = m.mail
> +		end
> +		if p.filename and p.filename.length > 0
> +			filename = p.filename
> +		else
> +			suffix = ''
> +			if p.mime_type == 'text/html'
> +				suffix = '.html'
> +			end
> +			filename = "part-#{match[1]}#{suffix}"
> +		end
> +
> +		# Sanitize just in case..
> +		filename.gsub!(/[^0-9A-Za-z.\-]/, '_')
> +
> +		fullpath = File.expand_path("#{tmpdir}/#{filename}")
> +		vim_puts "Viewing attachment #{fullpath}"
> +		File.open(fullpath, 'w') do |f|
> +			f.write p.body.decoded
> +			cmd = VIM::evaluate('g:notmuch_view_attachment')
> +			system(cmd, fullpath)
> +		end
> +	else
> +		vim_puts "No attachment on this line."
> +	end
> +EOF
> +endfunction
> +
>  function! s:show_extract_msg()
> +	let line = getline(".")
>  ruby << EOF
>  	m = get_message
> -	m.mail.attachments.each do |a|
> +	line = VIM::evaluate('line')
> +
> +	# If the user is on a line that has an 'Part'
> +	# line, we just extract the one attachment.
> +	match = line.match(/^Part (\d*):/)
> +	if match and match.length == 2
> +		a = m.mail.parts[match[1].to_i - 1]
>  		File.open(a.filename, 'w') do |f|
>  			f.write a.body.decoded
> -			print "Extracted '#{a.filename}'"
> +			vim_puts "Extracted #{a.filename}"
> +		end
> +	else
> +		# Extract them all..
> +		m.mail.attachments.each do |a|
> +			File.open(a.filename, 'w') do |f|
> +				f.write a.body.decoded
> +				vim_puts "Extracted #{a.filename}"
> +			end
>  		end
>  	end
>  EOF
> @@ -331,6 +393,16 @@ ruby << EOF
>  			b << "To: %s" % msg['to']
>  			b << "Cc: %s" % msg['cc']
>  			b << "Date: %s" % msg['date']
> +			cnt = 0
> +			m.parts.each do |p|
> +				cnt += 1
> +				b << "Part %d: %s (%s)" % [cnt, p.mime_type, p.filename]
> +			end
> +			# Add a special case for text/html messages.  Here we show the
> +			# only 'part' so that we can view it in a web browser if we want.
> +			if m.parts.length == 0 and part.mime_type == 'text/html'
> +				b << "Part 1: text/html"
> +			end
>  			nm_m.body_start = b.count
>  			b << "--- %s ---" % part.mime_type
>  			part.convert.each_line do |l|
> @@ -425,6 +497,14 @@ function! s:set_defaults()
>  		endif
>  	endif
>  
> +	if !exists('g:notmuch_attachment_tmpdir')
> +		let g:notmuch_attachment_tmpdir = s:notmuch_attachment_tmpdir_default
> +	endif
> +
> +	if !exists('g:notmuch_view_attachment')
> +		let g:notmuch_view_attachment = s:notmuch_view_attachment_default
> +	endif
> +
>  	if !exists('g:notmuch_folders_count_threads')
>  		if exists('g:notmuch_rb_count_threads')
>  			let g:notmuch_count_threads = g:notmuch_rb_count_threads
> -- 
> 1.9.3
>
> _______________________________________________
> notmuch mailing list
> notmuch@notmuchmail.org
> http://notmuchmail.org/mailman/listinfo/notmuch

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

* Re: [PATCH v4 1/2] VIM: Add better attachment support
  2014-10-25 10:41 ` [PATCH v4 1/2] VIM: Add better attachment support Tomi Ollila
@ 2014-10-27 18:35   ` Ian Main
  0 siblings, 0 replies; 4+ messages in thread
From: Ian Main @ 2014-10-27 18:35 UTC (permalink / raw)
  To: Tomi Ollila, notmuch

Tomi Ollila wrote:
> On Fri, Oct 24 2014, Ian Main <imain@stemwinder.org> wrote:
> 
> > Change how the notmuch vim client supports attachments:
> >
> > - For each message part a 'Part <number>: <filename>' is added to the
> >   header.
> > - You can then use 'e' to extract the attachment under the cursor or
> >   use it elsewhere to extract all attachments (the prior behavior)
> > - You can use 'v' to 'view' the attachment/part using xdg-open by
> >   default.
> > - If the message is 'text/html' we include a 'Part:' for the body of
> >   the message so you can easily view it in a web browser if you so
> >   choose.
> >
> >     Ian
> > ---
> >
> > - Fixed commit message
> > - Fixed documentation
> >
> >  vim/notmuch.txt |  8 +++++-
> >  vim/notmuch.vim | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--
> >  2 files changed, 89 insertions(+), 3 deletions(-)
> >
> > diff --git a/vim/notmuch.txt b/vim/notmuch.txt
> > index 4374102..d5e1ad2 100644
> > --- a/vim/notmuch.txt
> > +++ b/vim/notmuch.txt
> > @@ -72,6 +72,9 @@ q	Quit view
> >  A	Archive (-inbox -unread)
> >  I	Mark as read (-unread)
> >  t	Tag (prompted)
> > +e       Extract attachment on the current 'Part' line or all
> 
> This and the following patch use spaces instead of tab in the line above
> (and following patch few lines below) -- the indentation looked weird and
> that got me to look more.
> 
> I was going to look more of this but run out of time. I'll look the
> this through after someone else who uses vim has tested these patched
> and reported their experiences.
> 
> Tomi

Ah, good catch.  Thanks Tomi.  I generally run with expandtab which makes
vim use spaces instead of tabs.  I see in the .vim src file it's set to turn
that off but not in the docs.  I'll check it myself too.

    Ian


> > +	attachments if the cursor is elsewhere.
> > +<enter> View attachment on the current 'Part' line.
> >  s	Search
> >  p	Save patches
> >  r	Reply
> > @@ -148,6 +151,9 @@ You can also configure your externail mail reader and sendemail program:
> >  >
> >  	let g:notmuch_reader = 'mutt -f %s'
> >  	let g:notmuch_sendmail = 'sendmail'
> > -<
> > +
> > +You can also configure what probram is used to view attachments:
> > +
> > +	let g:notmuch_view_attachment = 'xdg-open'
> >  
> >  vim:tw=78:ts=8:noet:ft=help:
> > diff --git a/vim/notmuch.vim b/vim/notmuch.vim
> > index cad9517..1466e50 100644
> > --- a/vim/notmuch.vim
> > +++ b/vim/notmuch.vim
> > @@ -35,6 +35,7 @@ let g:notmuch_show_maps = {
> >  	\ 't':		'show_tag("")',
> >  	\ 'o':		'show_open_msg()',
> >  	\ 'e':		'show_extract_msg()',
> > +	\ '<Enter>':	'show_view_attachment()',
> >  	\ 's':		'show_save_msg()',
> >  	\ 'p':		'show_save_patches()',
> >  	\ 'r':		'show_reply()',
> > @@ -58,6 +59,8 @@ let s:notmuch_date_format_default = '%d.%m.%y'
> >  let s:notmuch_datetime_format_default = '%d.%m.%y %H:%M:%S'
> >  let s:notmuch_reader_default = 'mutt -f %s'
> >  let s:notmuch_sendmail_default = 'sendmail'
> > +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
> >  
> > @@ -152,13 +155,72 @@ function! s:show_info()
> >  	ruby vim_puts get_message.inspect
> >  endfunction
> >  
> > +function! s:show_view_attachment()
> > +	let line = getline(".")
> > +ruby << EOF
> > +	m = get_message
> > +	line = VIM::evaluate('line')
> > +
> > +	match = line.match(/^Part (\d*):/)
> > +	if match and match.length == 2
> > +		# Set up the tmpdir
> > +		tmpdir = VIM::evaluate('g:notmuch_attachment_tmpdir')
> > +		tmpdir = File.expand_path(tmpdir)
> > +		Dir.mkdir(tmpdir) unless Dir.exists?(tmpdir)
> > +
> > +		p = m.mail.parts[match[1].to_i - 1]
> > +		if p == nil
> > +			# Not a multipart message, use the message itself.
> > +			p = m.mail
> > +		end
> > +		if p.filename and p.filename.length > 0
> > +			filename = p.filename
> > +		else
> > +			suffix = ''
> > +			if p.mime_type == 'text/html'
> > +				suffix = '.html'
> > +			end
> > +			filename = "part-#{match[1]}#{suffix}"
> > +		end
> > +
> > +		# Sanitize just in case..
> > +		filename.gsub!(/[^0-9A-Za-z.\-]/, '_')
> > +
> > +		fullpath = File.expand_path("#{tmpdir}/#{filename}")
> > +		vim_puts "Viewing attachment #{fullpath}"
> > +		File.open(fullpath, 'w') do |f|
> > +			f.write p.body.decoded
> > +			cmd = VIM::evaluate('g:notmuch_view_attachment')
> > +			system(cmd, fullpath)
> > +		end
> > +	else
> > +		vim_puts "No attachment on this line."
> > +	end
> > +EOF
> > +endfunction
> > +
> >  function! s:show_extract_msg()
> > +	let line = getline(".")
> >  ruby << EOF
> >  	m = get_message
> > -	m.mail.attachments.each do |a|
> > +	line = VIM::evaluate('line')
> > +
> > +	# If the user is on a line that has an 'Part'
> > +	# line, we just extract the one attachment.
> > +	match = line.match(/^Part (\d*):/)
> > +	if match and match.length == 2
> > +		a = m.mail.parts[match[1].to_i - 1]
> >  		File.open(a.filename, 'w') do |f|
> >  			f.write a.body.decoded
> > -			print "Extracted '#{a.filename}'"
> > +			vim_puts "Extracted #{a.filename}"
> > +		end
> > +	else
> > +		# Extract them all..
> > +		m.mail.attachments.each do |a|
> > +			File.open(a.filename, 'w') do |f|
> > +				f.write a.body.decoded
> > +				vim_puts "Extracted #{a.filename}"
> > +			end
> >  		end
> >  	end
> >  EOF
> > @@ -331,6 +393,16 @@ ruby << EOF
> >  			b << "To: %s" % msg['to']
> >  			b << "Cc: %s" % msg['cc']
> >  			b << "Date: %s" % msg['date']
> > +			cnt = 0
> > +			m.parts.each do |p|
> > +				cnt += 1
> > +				b << "Part %d: %s (%s)" % [cnt, p.mime_type, p.filename]
> > +			end
> > +			# Add a special case for text/html messages.  Here we show the
> > +			# only 'part' so that we can view it in a web browser if we want.
> > +			if m.parts.length == 0 and part.mime_type == 'text/html'
> > +				b << "Part 1: text/html"
> > +			end
> >  			nm_m.body_start = b.count
> >  			b << "--- %s ---" % part.mime_type
> >  			part.convert.each_line do |l|
> > @@ -425,6 +497,14 @@ function! s:set_defaults()
> >  		endif
> >  	endif
> >  
> > +	if !exists('g:notmuch_attachment_tmpdir')
> > +		let g:notmuch_attachment_tmpdir = s:notmuch_attachment_tmpdir_default
> > +	endif
> > +
> > +	if !exists('g:notmuch_view_attachment')
> > +		let g:notmuch_view_attachment = s:notmuch_view_attachment_default
> > +	endif
> > +
> >  	if !exists('g:notmuch_folders_count_threads')
> >  		if exists('g:notmuch_rb_count_threads')
> >  			let g:notmuch_count_threads = g:notmuch_rb_count_threads
> > -- 
> > 1.9.3
> >
> > _______________________________________________
> > notmuch mailing list
> > notmuch@notmuchmail.org
> > http://notmuchmail.org/mailman/listinfo/notmuch

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

end of thread, other threads:[~2014-10-27 18:35 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-10-24 16:38 [PATCH v4 1/2] VIM: Add better attachment support Ian Main
2014-10-24 16:38 ` [PATCH v4 2/2] VIM: Add URI handling Ian Main
2014-10-25 10:41 ` [PATCH v4 1/2] VIM: Add better attachment support Tomi Ollila
2014-10-27 18:35   ` Ian Main

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).