summaryrefslogtreecommitdiff
path: root/usr.bin/mg/mail.c
blob: f14ca04de137fdfa9413b59d0bdbf745ac77b6ea (plain)
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
/* $OpenBSD: mail.c,v 1.7 2005/10/13 05:47:45 kjell Exp $ */
/*
 * This file is in the public domain.
 *
 * Author: Vincent Labrecque, April 2003
 */
#include <ctype.h>

#include "def.h"
#include "kbd.h"
#include "funmap.h"

void		 mail_init(void);
static int	 fake_self_insert(int, int);
static int	 mail(int, int);

int limit = 72;

/* mappings for all "printable" characters ('-' -> '~') */
static PF mail_fake[] = {
	fake_self_insert, fake_self_insert, fake_self_insert, fake_self_insert,
	fake_self_insert, fake_self_insert, fake_self_insert, fake_self_insert,
	fake_self_insert, fake_self_insert, fake_self_insert, fake_self_insert,
	fake_self_insert, fake_self_insert, fake_self_insert, fake_self_insert,
	fake_self_insert, fake_self_insert, fake_self_insert, fake_self_insert,
	fake_self_insert, fake_self_insert, fake_self_insert, fake_self_insert,
	fake_self_insert, fake_self_insert, fake_self_insert, fake_self_insert,
	fake_self_insert, fake_self_insert, fake_self_insert, fake_self_insert,
	fake_self_insert, fake_self_insert, fake_self_insert, fake_self_insert,
	fake_self_insert, fake_self_insert, fake_self_insert, fake_self_insert,
	fake_self_insert, fake_self_insert, fake_self_insert, fake_self_insert,
	fake_self_insert, fake_self_insert, fake_self_insert, fake_self_insert,
	fake_self_insert, fake_self_insert, fake_self_insert, fake_self_insert,
	fake_self_insert, fake_self_insert, fake_self_insert, fake_self_insert,
	fake_self_insert, fake_self_insert, fake_self_insert, fake_self_insert,
	fake_self_insert, fake_self_insert, fake_self_insert, fake_self_insert,
	fake_self_insert, fake_self_insert, fake_self_insert, fake_self_insert,
	fake_self_insert, fake_self_insert, fake_self_insert, fake_self_insert,
	fake_self_insert, fake_self_insert, fake_self_insert, fake_self_insert,
	fake_self_insert, fake_self_insert, fake_self_insert, fake_self_insert,
	fake_self_insert, fake_self_insert, fake_self_insert, fake_self_insert,
	fake_self_insert, fake_self_insert, fake_self_insert, fake_self_insert,
	fake_self_insert, fake_self_insert, fake_self_insert, fake_self_insert,
	fake_self_insert, fake_self_insert, fake_self_insert
};

static struct KEYMAPE (1 + IMAPEXT) mailmap = {
	1,
	1 + IMAPEXT,
	rescan,
	{
		{ ' ', '~', mail_fake, NULL }
	}
};

int
mail_set_limit(int f, int n)
{
	char buf[32], *rep;

	if ((f & FFARG) != 0) {
		limit = n;
	} else {
		if ((rep = eread("Margin: ", buf, sizeof(buf),
		    EFNEW | EFCR)) == NULL)
			return (ABORT);
		else if (rep[0] == '\0')
			return (FALSE);
		limit = atoi(rep);
	}
	return (TRUE);
}

void
mail_init(void)
{
	funmap_add(mail, "mail-mode");
	funmap_add(mail_set_limit, "mail-set-margin");
	maps_add((KEYMAP *)&mailmap, "mail-mode");
}

static int
mail(int f, int n)
{
	curbp->b_modes[0] = name_mode("fundamental");
	curbp->b_modes[1] = name_mode("mail-mode");
	if (curbp->b_modes[1] == NULL) {
		panic("can't happen");
		mail_init();
		curbp->b_modes[1] = name_mode("mail-mode");
	}
	curbp->b_nmodes = 1;
	curwp->w_flag |= WFMODE;
	return (TRUE);
}

static int
fake_self_insert(int f, int n)
{
	int len = llength(curwp->w_dotp), col;

	if (len + 1 > limit) {
		/*
		 * Find the column at which we should cut, taking
		 * word boundaries into account.
		 */
		for (col = limit; col > 0; col--)
			if (isspace(curwp->w_dotp->l_text[col])) {
				col++;	/* XXX - skip the space */
				break;
			}

		if (curbp->b_doto == len) {
			/*
			 * User is appending to the line; simple case.
			 */
			if (col) {
				curwp->w_doto = col;
				lnewline();
				gotoeol(0, 1);
			}
			curwp->w_wrapline = NULL;
		} else if ((len - col) > 0) {
			/*
			 * User is shifting words by inserting in the middle.
			 */
			const char *trail;
			int save_doto = curwp->w_doto;
			LINE *save_dotp = curwp->w_dotp;
			int tlen = len - col;

			trail = curwp->w_dotp->l_text + col;

			/*
			 * Create a new line or reuse the last wrapping line
			 * unless we could fill it.
			 */
			if (curwp->w_wrapline != lforw(curwp->w_dotp) ||
			    llength(curwp->w_wrapline) + tlen >= limit) {
				curwp->w_doto = col;
				lnewline();
				curwp->w_wrapline = curwp->w_dotp;
				curwp->w_dotp = save_dotp;
			} else {
				curwp->w_dotp = curwp->w_wrapline;
				curwp->w_doto = 0;

				/* insert trail */
				linsert_str(trail, tlen);

				/* delete trail */
				curwp->w_dotp = save_dotp;
				curwp->w_doto = col;
				ldelete(tlen, KNONE);	/* don't store in kill ring */
			}

			/*
			 * Readjust dot to point at where the user expects
			 * it to be after an insertion.
			 */
			curwp->w_doto = save_doto;
			if (curwp->w_doto >= col) {
				curwp->w_dotp = curwp->w_wrapline;
				curwp->w_doto -= col;
			}
		}
	}
	selfinsert(f, n);
	return (TRUE);
}