about summary refs log tree commit diff stats
path: root/mu.vim
Commit message (Collapse)AuthorAgeFilesLines
* 2838Kartik K. Agaram2016-04-161-1/+1
|
* 2735 - define recipes using 'def'Kartik K. Agaram2016-03-081-2/+2
| | | | | | | | | | | | I'm dropping all mention of 'recipe' terminology from the Readme. That way I hope to avoid further bike-shedding discussions while I very slowly decide on the right terminology with my students. I could be smarter in my error messages and use 'recipe' when code uses it and 'function' otherwise. But what about other words like ingredient? It would all add complexity that I'm not yet sure is worthwhile. But I do want separate experiences for veteran programmers reading about Mu on github and for people learning programming using Mu.
* 2562Kartik K. Agaram2016-01-171-1/+0
| | | | | | | | | | | | We want to use the type 'recipe' for recipe *variables*, because it seems nicer to say `recipe number -> number` rather than recipe-ordinal, etc. To support this we'll allow recipe names to be mentioned without any type. This might make a couple of places in this commit more brittle. I'm dropping error messages, causing them to not happen in some situations. Maybe I should just bite the bullet and require an explicit :recipe-literal. We'll see.
* 2367Kartik K. Agaram2015-11-051-1/+1
|
* 2306 - recipe headersKartik K. Agaram2015-10-281-0/+1
| | | | | | | | | | Once a student has gotten used to recipes and ingredients using the staged 'next-ingredient' approach there's no reason to avoid conventional function headers. As an added bonus we can now: a) check that all 'reply' instructions in a recipe are consistent b) deduce what to reply without needing to say so everytime c) start thinking about type parameters for recipes (generic functions!)
* 2295 - drop first-class recipes and continuationsKartik K. Agaram2015-10-281-1/+0
| | | | Making life too complex at this time.
* 2294Kartik K. Agaram2015-10-281-0/+1
| | | | Bah, sick of CALL and continuations.
* 2266 - drop experiment with genericsKartik K. Agaram2015-10-071-1/+1
|
* 2252Kartik K. Agaram2015-10-051-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | I can't easily use generic containers without needing some syntax for generic recipes: push:number a:number, l:list:number which would be implemented as: T <- next-type a:T <- next-ingredient etc. Another concern: how to represent map<string, list<number>>? map::address:array:character::list:number where the '::' is just silently turned into ':'. Agh, all this is so baroque. All this while I've been trying to avoid getting into language design. All I want is some lightweight way to avoid security holes and memory corruption. But now it seems like I need facets to control compile-time activities and so on.
* 2174Kartik K. Agaram2015-09-061-1/+2
|
* 2153Kartik K. Agaram2015-09-051-1/+1
|
* 2139Kartik K. Agaram2015-09-041-0/+1
|
* 2080Kartik K. Agaram2015-08-261-0/+1
|
* 2059Kartik K. Agaram2015-08-221-1/+1
|
* 1960Kartik K. Agaram2015-08-091-2/+2
|
* 1903Kartik K. Agaram2015-07-301-0/+5
|
* 1868 - start using naked literals everywhereKartik K. Agaram2015-07-281-0/+2
| | | | First step to reducing typing burden. Next step: inferring types.
* 1780 - now we always reclaim local scopesKartik K. Agaram2015-07-131-1/+1
| | | | | | But still no difference in either memory footprint or in running time. This will teach me -- for the umpteenth time -- to optimize before measuring.
* 1774Kartik K. Agaram2015-07-131-1/+1
|
* 1771Kartik K. Agaram2015-07-131-1/+2
|
* 1646Kartik K. Agaram2015-06-251-1/+1
|
* 1555 - mu.vim: distinguish control-flow from constantsKartik K. Agaram2015-06-121-5/+7
| | | | We also do this in regular C++ now.
* 1506Kartik K. Agaram2015-05-281-1/+1
|
* 1432 - contrast scenarios and recipesKartik K. Agaram2015-05-231-2/+3
|
* 1410Kartik K. Agaram2015-05-201-1/+2
|
* 1378Kartik K. Agaram2015-05-151-0/+1
|
* 1333Kartik K. Agaram2015-05-101-3/+11
|
* 1331Kartik K. Agaram2015-05-101-2/+4
|
* 1327 - better error handling in chessboardKartik K. Agaram2015-05-101-1/+1
| | | | | Also a bugfix in break to label, because I noticed the screen wasn't being cleaned up on quit.
* vim highlighting for labelsKartik K. Agaram2015-04-181-1/+2
|
* 1093 - little more vim support for the old arc versionKartik K. Agaram2015-04-171-0/+2
|
* 1048Kartik K. Agaram2015-04-111-5/+5
|
* 1025 - switch syntax highlighting to support c++ versionKartik K. Agaram2015-04-061-6/+5
|
* 561 - vim hackKartik K. Agaram2015-01-131-4/+7
|
* 548 - hacky vim filetype for muKartik K. Agaram2015-01-131-0/+37
mmitter Drew DeVault <sir@cmpwn.com> 2019-03-31 14:42:18 -0400 Implement header-regex-match filters' href='/akspecs/aerc/commit/widgets/msgviewer.go?id=8e5ed2a161bf654888a8b48af2d9fdf6fbc0c7e0'>8e5ed2a ^
36419d8 ^
8e5ed2a ^



bbdf9df ^








f9262e4 ^
27b2517 ^





f9262e4 ^
bbdf9df ^






27b2517 ^


0abafa6 ^

27b2517 ^
fa04a1e ^
0abafa6 ^



27b2517 ^
f9262e4 ^







fa04a1e ^

0abafa6 ^
bbdf9df ^
6c36e04 ^


711d228 ^

6c36e04 ^
bbdf9df ^





f9262e4 ^

bbdf9df ^
f9262e4 ^

bbdf9df ^

0abafa6 ^
711d228 ^

f9262e4 ^

711d228 ^




f9262e4 ^

711d228 ^


0abafa6 ^




fa04a1e ^
f9262e4 ^




fa04a1e ^













f9262e4 ^



fa04a1e ^


f9262e4 ^


fa04a1e ^


5685a17 ^
fa04a1e ^




27b2517 ^



4bdc0f3 ^




337dd18 ^
4bdc0f3 ^

337dd18 ^
4bdc0f3 ^

337dd18 ^
4bdc0f3 ^
27b2517 ^

fa04a1e ^


5685a17 ^
fa04a1e ^
337dd18 ^

5685a17 ^
337dd18 ^










5685a17 ^
337dd18 ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303



               
             

                 
                 
 
                                         
                                     
                                        
                                                  
                                             
                                  
                                 

                                       



                                               


                           
                                  
                     







                                  

 















                                                                                    
 
                                                                       
                                                
 
                                                
                                                                           




                                    
                                                                       










                                                   
                                                                  



                                    
                                                                


                            

                                                    
                                      
                                                             






                                                
 




                                      

                                      


                                                  
                                 



                                                

                                                                            


                                                             
                                                                            
                         












                                                                           
                                                                            



                                  








                                              
                                    





                                                                           
                                






                                 


                                                                

                                      
          
 



                                    
                     







                                

 
                                        
                                                        


                                                           

                                                                                   
                                                                                       





                                                                     

                                                       
                                 

                                                  

                           
                           

                                                                     

                                               




                                                        

                                               


                                                   




                                       
                                                




                                                                                  













                                                               



                                           


                                            


                                    


                        
                        




                                             



                                                             




                                                    
                             

                                                                        
                

                                                      
         
                                                              

                                          


                                    
                           
 

                           
                        










                                                                             
                             
 
package widgets

import (
	"bytes"
	"fmt"
	"io"
	"os/exec"
	"strings"

	"github.com/danwakefield/fnmatch"
	"github.com/emersion/go-imap"
	"github.com/emersion/go-message"
	_ "github.com/emersion/go-message/charset"
	"github.com/emersion/go-message/mail"
	"github.com/gdamore/tcell"
	"github.com/google/shlex"
	"github.com/mattn/go-runewidth"

	"git.sr.ht/~sircmpwn/aerc/config"
	"git.sr.ht/~sircmpwn/aerc/lib"
	"git.sr.ht/~sircmpwn/aerc/lib/ui"
	"git.sr.ht/~sircmpwn/aerc/worker/types"
)

type MessageViewer struct {
	conf    *config.AercConfig
	err     error
	filter  *exec.Cmd
	msg     *types.MessageInfo
	pager   *exec.Cmd
	source  io.Reader
	pagerin io.WriteCloser
	sink    io.WriteCloser
	grid    *ui.Grid
	term    *Terminal
}

func formatAddresses(addrs []*imap.Address) string {
	val := bytes.Buffer{}
	for i, addr := range addrs {
		if addr.PersonalName != "" {
			val.WriteString(fmt.Sprintf("%s <%s@%s>",
				addr.PersonalName, addr.MailboxName, addr.HostName))
		} else {
			val.WriteString(fmt.Sprintf("%s@%s",
				addr.MailboxName, addr.HostName))
		}
		if i != len(addrs)-1 {
			val.WriteString(", ")
		}
	}
	return val.String()
}

func NewMessageViewer(conf *config.AercConfig, store *lib.MessageStore,
	msg *types.MessageInfo) *MessageViewer {

	grid := ui.NewGrid().Rows([]ui.GridSpec{
		{ui.SIZE_EXACT, 3}, // TODO: Based on number of header rows
		{ui.SIZE_WEIGHT, 1},
	}).Columns([]ui.GridSpec{
		{ui.SIZE_WEIGHT, 1},
	})

	// TODO: let user specify additional headers to show by default
	headers := ui.NewGrid().Rows([]ui.GridSpec{
		{ui.SIZE_EXACT, 1},
		{ui.SIZE_EXACT, 1},
		{ui.SIZE_EXACT, 1},
	}).Columns([]ui.GridSpec{
		{ui.SIZE_WEIGHT, 1},
		{ui.SIZE_WEIGHT, 1},
	})
	headers.AddChild(
		&HeaderView{
			Name:  "From",
			Value: formatAddresses(msg.Envelope.From),
		}).At(0, 0)
	headers.AddChild(
		&HeaderView{
			Name:  "To",
			Value: formatAddresses(msg.Envelope.To),
		}).At(0, 1)
	headers.AddChild(
		&HeaderView{
			Name:  "Subject",
			Value: msg.Envelope.Subject,
		}).At(1, 0).Span(1, 2)
	headers.AddChild(ui.NewFill(' ')).At(2, 0).Span(1, 2)

	body := ui.NewGrid().Rows([]ui.GridSpec{
		{ui.SIZE_WEIGHT, 1},
	}).Columns([]ui.GridSpec{
		{ui.SIZE_WEIGHT, 1},
		{ui.SIZE_EXACT, 20},
	})

	var (
		filter  *exec.Cmd
		pager   *exec.Cmd
		pipe    io.WriteCloser
		pagerin io.WriteCloser
		term    *Terminal
		viewer  *MessageViewer
	)
	cmd, err := shlex.Split(conf.Viewer.Pager)
	if err != nil {
		goto handle_error
	}
	pager = exec.Command(cmd[0], cmd[1:]...)

	for _, f := range conf.Filters {
		mime := strings.ToLower(msg.BodyStructure.MIMEType) +
			"/" + strings.ToLower(msg.BodyStructure.MIMESubType)
		switch f.FilterType {
		case config.FILTER_MIMETYPE:
			if fnmatch.Match(f.Filter, mime, 0) {
				filter = exec.Command("sh", "-c", f.Command)
			}
		case config.FILTER_HEADER:
			var header string
			switch f.Header {
			case "subject":
				header = msg.Envelope.Subject
			case "from":
				header = formatAddresses(msg.Envelope.From)
			case "to":
				header = formatAddresses(msg.Envelope.To)
			case "cc":
				header = formatAddresses(msg.Envelope.Cc)
			}
			if f.Regex.Match([]byte(header)) {
				filter = exec.Command("sh", "-c", f.Command)
			}
		}
		if filter != nil {
			break
		}
	}
	if filter != nil {
		pipe, _ = filter.StdinPipe()
		pagerin, _ = pager.StdinPipe()
	} else {
		pipe, _ = pager.StdinPipe()
	}

	term, _ = NewTerminal(pager)
	// TODO: configure multipart view. I left a spot for it in the grid
	body.AddChild(term).At(0, 0).Span(1, 2)

	grid.AddChild(headers).At(0, 0)
	grid.AddChild(body).At(1, 0)

	viewer = &MessageViewer{
		filter:  filter,
		grid:    grid,
		msg:     msg,
		pager:   pager,
		pagerin: pagerin,
		sink:    pipe,
		term:    term,
	}

	store.FetchBodyPart(msg.Uid, 0, func(reader io.Reader) {
		viewer.source = reader
		viewer.attemptCopy()
	})

	term.OnStart = func() {
		viewer.attemptCopy()
	}

	return viewer

handle_error:
	viewer = &MessageViewer{
		err:  err,
		grid: grid,
		msg:  msg,
	}
	return viewer
}

func (mv *MessageViewer) attemptCopy() {
	if mv.source != nil && mv.pager.Process != nil {
		header := message.Header{}
		header.SetText("Content-Transfer-Encoding",
			mv.msg.BodyStructure.Encoding)
		header.SetContentType(
			mv.msg.BodyStructure.MIMEType, mv.msg.BodyStructure.Params)
		header.SetText("Content-Description", mv.msg.BodyStructure.Description)
		if mv.filter != nil {
			stdout, _ := mv.filter.StdoutPipe()
			mv.filter.Start()
			go func() {
				_, err := io.Copy(mv.pagerin, stdout)
				if err != nil {
					mv.err = err
					mv.Invalidate()
				}
				mv.pagerin.Close()
				stdout.Close()
			}()
		}
		go func() {
			entity, err := message.New(header, mv.source)
			if err != nil {
				mv.err = err
				mv.Invalidate()
				return
			}
			reader := mail.NewReader(entity)
			part, err := reader.NextPart()
			if err != nil {
				mv.err = err
				mv.Invalidate()
				return
			}
			io.Copy(mv.sink, part.Body)
			mv.sink.Close()
		}()
	}
}

func (mv *MessageViewer) Draw(ctx *ui.Context) {
	if mv.err != nil {
		ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ', tcell.StyleDefault)
		ctx.Printf(0, 0, tcell.StyleDefault, "%s", mv.err.Error())
		return
	}
	mv.grid.Draw(ctx)
}

func (mv *MessageViewer) Invalidate() {
	mv.grid.Invalidate()
}

func (mv *MessageViewer) OnInvalidate(fn func(d ui.Drawable)) {
	mv.grid.OnInvalidate(func(_ ui.Drawable) {
		fn(mv)
	})
}

func (mv *MessageViewer) Event(event tcell.Event) bool {
	if mv.term != nil {
		return mv.term.Event(event)
	}
	return false
}

func (mv *MessageViewer) Focus(focus bool) {
	if mv.term != nil {
		mv.term.Focus(focus)
	}
}

type HeaderView struct {
	ui.Invalidatable
	Name  string
	Value string
}

func (hv *HeaderView) Draw(ctx *ui.Context) {
	name := hv.Name
	size := runewidth.StringWidth(name)
	lim := ctx.Width() - size - 1
	value := runewidth.Truncate(" "+hv.Value, lim, "…")
	var (
		hstyle tcell.Style
		vstyle tcell.Style
	)
	// TODO: Make this more robust and less dumb
	if hv.Name == "PGP" {
		vstyle = tcell.StyleDefault.Foreground(tcell.ColorGreen)
		hstyle = tcell.StyleDefault.Bold(true)
	} else {
		vstyle = tcell.StyleDefault
		hstyle = tcell.StyleDefault.Bold(true)
	}
	ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ', vstyle)
	ctx.Printf(0, 0, hstyle, name)
	ctx.Printf(size, 0, vstyle, value)
}

func (hv *HeaderView) Invalidate() {
	hv.DoInvalidate(hv)
}

type MultipartView struct {
	ui.Invalidatable
}

func (mpv *MultipartView) Draw(ctx *ui.Context) {
	ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ', tcell.StyleDefault)
	ctx.Fill(0, 0, ctx.Width(), 1, ' ', tcell.StyleDefault.Reverse(true))
	ctx.Printf(0, 0, tcell.StyleDefault.Reverse(true), "text/plain")
	ctx.Printf(0, 1, tcell.StyleDefault, "text/html")
	ctx.Printf(0, 2, tcell.StyleDefault, "application/pgp-si…")
}

func (mpv *MultipartView) Invalidate() {
	mpv.DoInvalidate(mpv)
}