about summary refs log tree commit diff stats
path: root/dwm.c
diff options
context:
space:
mode:
Diffstat (limited to 'dwm.c')
-rw-r--r--dwm.c70
1 files changed, 14 insertions, 56 deletions
diff --git a/dwm.c b/dwm.c
index 3650ccd..25bc926 100644
--- a/dwm.c
+++ b/dwm.c
@@ -119,7 +119,6 @@ void cleanup(void);
 void configure(Client *c);
 void configurenotify(XEvent *e);
 void configurerequest(XEvent *e);
-unsigned int counttiled(void);
 void destroynotify(XEvent *e);
 void detach(Client *c);
 void detachstack(Client *c);
@@ -168,12 +167,8 @@ void spawn(const char *arg);
 void tag(const char *arg);
 unsigned int textnw(const char *text, unsigned int len);
 unsigned int textw(const char *text);
-void tileh(void);
-void tilehstack(unsigned int n);
-Client *tilemaster(unsigned int n);
 void tileresize(Client *c, int x, int y, int w, int h);
-void tilev(void);
-void tilevstack(unsigned int n);
+void tile(void);
 void togglefloating(const char *arg);
 void togglelayout(const char *arg);
 void toggletag(const char *arg);
@@ -468,15 +463,6 @@ configurerequest(XEvent *e) {
 	XSync(dpy, False);
 }
 
-unsigned int
-counttiled(void) {
-	unsigned int n;
-	Client *c;
-
-	for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next), n++);
-	return n;
-}
-
 void
 destroynotify(XEvent *e) {
 	Client *c;
@@ -1508,44 +1494,6 @@ textw(const char *text) {
 }
 
 void
-tileh(void) {
-	int x, w;
-	unsigned int i, n = counttiled();
-	Client *c;
-
-	if(n == 0)
-		return;
-	c = tilemaster(n);
-	if(--n == 0)
-		return;
-
-	x = tx;
-	w = tw / n;
-	if(w < bh)
-		w = tw;
-
-	for(i = 0, c = nexttiled(c->next); c; c = nexttiled(c->next), i++) {
-		if(i + 1 == n) /* remainder */
-			tileresize(c, x, ty, (tx + tw) - x - 2 * c->bw, th - 2 * c->bw);
-		else
-			tileresize(c, x, ty, w - 2 * c->bw, th - 2 * c->bw);
-		if(w != tw)
-			x = c->x + c->w + 2 * c->bw;
-	}
-}
-
-Client *
-tilemaster(unsigned int n) {
-	Client *c = nexttiled(clients);
-
-	if(n == 1)
-		tileresize(c, wx, wy, ww - 2 * c->bw, wh - 2 * c->bw);
-	else
-		tileresize(c, mx, my, mw - 2 * c->bw, mh - 2 * c->bw);
-	return c;
-}
-
-void
 tileresize(Client *c, int x, int y, int w, int h) {
 	resize(c, x, y, w, h, RESIZEHINTS);
 	if((RESIZEHINTS) && ((c->h < bh) || (c->h > h) || (c->w < bh) || (c->w > w)))
@@ -1554,17 +1502,27 @@ tileresize(Client *c, int x, int y, int w, int h) {
 }
 
 void
-tilev(void) {
+tile(void) {
 	int y, h;
-	unsigned int i, n = counttiled();
+	unsigned int i, n;
 	Client *c;
 
+	for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next), n++);
 	if(n == 0)
 		return;
-	c = tilemaster(n);
+
+	/* master */
+	c = nexttiled(clients);
+
+	if(n == 1)
+		tileresize(c, wx, wy, ww - 2 * c->bw, wh - 2 * c->bw);
+	else
+		tileresize(c, mx, my, mw - 2 * c->bw, mh - 2 * c->bw);
+
 	if(--n == 0)
 		return;
 
+	/* tile stack */
 	y = ty;
 	h = th / n;
 	if(h < bh)
id=3ba69edab5f0c787424dac9649e43a7743da13ca'>3ba69ed ^
1f52939 ^



3ba69ed ^

1f52939 ^


36c6030 ^
217e885 ^
1f52939 ^
217e885 ^


















1f52939 ^
217e885 ^

217e885 ^
3ba69ed ^












217e885 ^
1f52939 ^
36c6030 ^



217e885 ^







3ba69ed ^
217e885 ^

1f52939 ^
1f52939 ^

3ba69ed ^
1f52939 ^


3ba69ed ^







1f52939 ^
3ba69ed ^









1f52939 ^
3ba69ed ^


217e885 ^

3ba69ed ^




























217e885 ^
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


           
               

                
            


                   
                 






                                             







                           
                                   


                                  
                                                                     


                  
                                                                 
                                                        



                          
                      



                                     

                                            


                 
                
                           
                                                      


















                                                                                  
                              

                                   
 












                                                                                                         
                                                     
                             



                                                 







                                                                         
                                    

         
                   

                                                                        
                                  


                                                                                                  







                                                                           
 









                                                                           
                  


                                                                   

                 




























                                                                                            
 
package msg

import (
	"bytes"
	"errors"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"path"
	"strings"

	"github.com/emersion/go-message"
	"github.com/emersion/go-message/mail"

	"git.sr.ht/~sircmpwn/aerc/models"
	"git.sr.ht/~sircmpwn/aerc/widgets"
	"git.sr.ht/~sircmpwn/getopt"
)

type forward struct{}

func init() {
	register(forward{})
}

func (forward) Aliases() []string {
	return []string{"forward"}
}

func (forward) Complete(aerc *widgets.Aerc, args []string) []string {
	return nil
}

func (forward) Execute(aerc *widgets.Aerc, args []string) error {
	opts, optind, err := getopt.Getopts(args, "AT:")
	if err != nil {
		return err
	}
	attach := false
	template := ""
	for _, opt := range opts {
		switch opt.Option {
		case 'A':
			attach = true
		case 'T':
			template = opt.Value
		}
	}

	to := ""
	if len(args) != 1 {
		to = strings.Join(args[optind:], ", ")
	}

	widget := aerc.SelectedTab().(widgets.ProvidesMessage)
	acct := widget.SelectedAccount()
	if acct == nil {
		return errors.New("No account selected")
	}
	store := widget.Store()
	if store == nil {
		return errors.New("Cannot perform action. Messages still loading")
	}
	msg, err := widget.SelectedMessage()
	if err != nil {
		return err
	}
	acct.Logger().Println("Forwarding email " + msg.Envelope.MessageId)

	subject := "Fwd: " + msg.Envelope.Subject
	defaults := map[string]string{
		"To":      to,
		"Subject": subject,
	}

	addTab := func() (*widgets.Composer, error) {
		if template != "" {
			defaults["OriginalFrom"] = models.FormatAddresses(msg.Envelope.From)
			defaults["OriginalDate"] = msg.Envelope.Date.Format("Mon Jan 2, 2006 at 3:04 PM")
		}

		composer, err := widgets.NewComposer(aerc, aerc.Config(), acct.AccountConfig(),
			acct.Worker(), template, defaults)
		if err != nil {
			aerc.PushError("Error: " + err.Error())
			return nil, err
		}

		tab := aerc.NewTab(composer, subject)
		if to == "" {
			composer.FocusRecipient()
		} else {
			composer.FocusTerminal()
		}
		composer.OnHeaderChange("Subject", func(subject string) {
			if subject == "" {
				tab.Name = "New email"
			} else {
				tab.Name = subject
			}
			tab.Content.Invalidate()
		})
		return composer, nil
	}

	if attach {
		tmpDir, err := ioutil.TempDir("", "aerc-tmp-attachment")
		if err != nil {
			return err
		}
		tmpFileName := path.Join(tmpDir,
			strings.ReplaceAll(fmt.Sprintf("%s.eml", msg.Envelope.Subject), "/", "-"))
		store.FetchFull([]uint32{msg.Uid}, func(reader io.Reader) {
			tmpFile, err := os.Create(tmpFileName)
			if err != nil {
				println(err)
				// TODO: Do something with the error
				addTab()
				return
			}

			defer tmpFile.Close()
			io.Copy(tmpFile, reader)
			composer, err := addTab()
			if err != nil {
				return
			}
			composer.AddAttachment(tmpFileName)
			composer.OnClose(func(composer *widgets.Composer) {
				os.RemoveAll(tmpDir)
			})
		})
	} else {
		if template == "" {
			template = aerc.Config().Templates.Forwards
		}

		// TODO: something more intelligent than fetching the 1st part
		// TODO: add attachments!
		store.FetchBodyPart(msg.Uid, []int{1}, func(reader io.Reader) {
			header := message.Header{}
			header.SetText(
				"Content-Transfer-Encoding", msg.BodyStructure.Encoding)
			header.SetContentType(
				msg.BodyStructure.MIMEType, msg.BodyStructure.Params)
			header.SetText("Content-Description", msg.BodyStructure.Description)
			entity, err := message.New(header, reader)
			if err != nil {
				// TODO: Do something with the error
				addTab()
				return
			}
			mreader := mail.NewReader(entity)
			part, err := mreader.NextPart()
			if err != nil {
				// TODO: Do something with the error
				addTab()
				return
			}
			buf := new(bytes.Buffer)
			buf.ReadFrom(part.Body)
			defaults["Original"] = buf.String()
			addTab()
		})
	}
	return nil
}