-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib-calc.c
188 lines (176 loc) · 4.67 KB
/
lib-calc.c
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
/*
* Copyright Neil Brown ©2020-2021 <neil@brown.name>
* May be distributed under terms of GPLv2 - see file:COPYING
*
* Perform numeric calculations.
*
* 'str' should hold an expression which is computed.
* It may can contain variables in which can comm2 should produce
* the contents as a string
* The result is passed to comm2 in decimal and hex
*/
#include <gmp.h>
#include "core.h"
int do_calc(const char *expr, mpq_t result,
bool (*getvar)(const char *name, int len, mpq_t val, void *data),
void *data);
static bool getvar(const char *name, int len, mpq_t val, void *data)
{
struct cmd_info *ci = data;
char *vnm;
char *vval;
if (!ci)
return False;
vnm = strnsave(ci->focus, name, len);
vval = comm_call_ret(strsave, ci->comm2, "get", ci->focus, 0, NULL, vnm);
if (!vval)
return False;
if (do_calc(vval, val, NULL, NULL) != 0)
return False;
return True;
}
DEF_CMD(calc)
{
int ret;
mpq_t result;
const char *formats = "xf";
if (!ci->str || !ci->comm2)
return Enoarg;
if (ci->str2)
formats = ci->str2;
mpq_init(result);
ret = do_calc(ci->str, result, getvar, (void*)ci);
if (ret == 0) {
/* success */
if (mpz_cmp_si(mpq_denref(result), 1) == 0) {
char *buf = mpz_get_str(NULL, 10,
mpq_numref(result));
comm_call(ci->comm2, "result", ci->focus, 0, NULL, buf);
free(buf);
if (strchr(formats, 'x')) {
buf = mpz_get_str(NULL, 16, mpq_numref(result));
comm_call(ci->comm2, "hex-result", ci->focus, 0,
NULL, strconcat(ci->focus, "0x",buf));
free(buf);
}
if (strchr(formats, 'X')) {
buf = mpz_get_str(NULL, -16, mpq_numref(result));
comm_call(ci->comm2, "hex-result", ci->focus, 0,
NULL, strconcat(ci->focus, "0X",buf));
free(buf);
}
if (strchr(formats, 'o')) {
buf = mpz_get_str(NULL, 8, mpq_numref(result));
comm_call(ci->comm2, "oct-result", ci->focus, 0,
NULL, strconcat(ci->focus, "0o",buf));
free(buf);
}
} else {
char *buf = NULL;
mpf_t fl;
buf = mpq_get_str(NULL, 10, result);
comm_call(ci->comm2, "frac-result", ci->focus, 0, NULL,
buf);
free(buf);
if (strchr(formats, 'f')) {
mpf_init2(fl, 20);
mpf_set_q(fl, result);
gmp_asprintf(&buf, "%.10Fg", fl);
mpf_clear(fl);
comm_call(ci->comm2, "float-result", ci->focus,
0, NULL, buf);
free(buf);
}
}
} else {
comm_call(ci->comm2, "err", ci->focus, ret-1);
}
mpq_clear(result);
return ret == 0 ? 1 : Efail;
}
DEF_CMD(calc_replace)
{
int ret;
mpq_t result;
const char *expr = ci->str;
struct mark *m2 = ci->mark2;
bool hex = False, oct = False;
if (ci->num != NO_NUMERIC) {
/* e.g. from Alt-3 alt-#. Pop up a calc window */
struct pane *doc = call_ret(pane, "docs:byname", ci->focus,
0, NULL, "*Calc*");
struct pane *p;
if (!doc)
doc = call_ret(pane, "doc:from-text", ci->focus,
0, NULL, "*Calc*", 0, NULL, "? ");
if (!doc) {
call("Message", ci->focus, 0, NULL,
"Cannot create *Calc* - sorry");
return Efail;
}
attr_set_str(&doc->attrs, "view-default", "view-calc");
p = call_ret(pane, "PopupTile", ci->focus, 0, NULL, "MD3tsa");
if (!p) {
call("Message", ci->focus, 0, NULL,
"Cannot popup *Calc* - sorry");
return Efail;
}
p = home_call_ret(pane, doc, "doc:attach-view", p, 1);
if (p)
call("doc:file", p, 1);
return 1;
}
if (!expr) {
if (!ci->mark)
return Enoarg;
/* Try to find a WORD */
call("doc:WORD", ci->focus, -1, ci->mark);
m2 = mark_dup(ci->mark);
call("doc:WORD", ci->focus, 1, m2);
expr = call_ret(strsave, "doc:get-str", ci->focus, 0, ci->mark, NULL,
0, m2);
if (!expr || !*expr)
return Enoarg;
}
mpq_init(result);
if (expr[0] == '#') {
hex = True;
expr += 1;
} else if (expr[0] == '@') {
oct = True;
expr += 1;
}
ret = do_calc(expr, result, NULL, NULL);
if (ret == 0) {
/* success */
char buf[100];
mpf_t fl;
if (mpz_cmp_si(mpq_denref(result), 1) == 0) {
gmp_snprintf(buf, sizeof(buf),
hex ? "%#Zx" : oct ? "0o%Zo" : "%Zd",
mpq_numref(result));
} else {
mpf_init(fl);
mpf_set_q(fl, result);
gmp_snprintf(buf, sizeof(buf), "%.10Fg", fl);
mpf_clear(fl);
}
if (!ci->mark || !m2 ||
call("doc:replace", ci->focus, 0, m2, buf, 0, ci->mark) <= 0)
call("Message", ci->focus, 0, NULL,
strconcat(ci->focus, expr, " -> ", buf));
} else {
call("Message", ci->focus, 0, NULL,
strconcat(ci->focus, expr, " -> error at ",
expr + ret - 1));
}
mpq_clear(result);
return ret == 0 ? 1 : Efail;
}
void edlib_init(struct pane *ed safe)
{
call_comm("global-set-command", ed, &calc,
0, NULL, "CalcExpr");
call_comm("global-set-command", ed, &calc_replace,
0, NULL, "interactive-cmd-calc-replace");
}