failed attempts to intercept copy command when the cursor is in the empty text field...
[LeanCalc.git] / Sources / Calculation.m
1 //
2 //  Calculation.m
3 //  LeanCalc
4 //
5 //  Created by Marc Liyanage on 04.02.05.
6 //  Copyright 2005 __MyCompanyName__. All rights reserved.
7 //
8
9 #import "Calculation.h"
10
11
12 @implementation Calculation
13
14 - (id)init {
15
16         self = [super init];
17         if (!self) return nil;
18         
19         STRING *falseString = makestring("false");
20         VALUE falseValue;
21         falseValue.v_type = V_STR;
22         falseValue.v_subtype = V_NOSUBTYPE;
23         falseValue.v_str = falseString;
24         setconfig(CONFIG_TILDE, &falseValue);
25         setconfig(CONFIG_TAB, &falseValue);
26         
27         [Calculation setKeys:[NSArray arrayWithObject:@"expression"] triggerChangeNotificationsForDependentKey:@"result"];
28         
29         return self;
30 }
31
32
33
34 - (void)dealloc {
35         
36         [expression release];
37         [next release];
38         [super dealloc];
39 }
40
41
42
43 - (NSString *)shortcut {
44
45         int historyIndex = [self historyIndex];
46         if (historyIndex < 1) return nil;
47
48         unichar ch = 0x2318;
49         NSString *str = [NSString stringWithCharacters:&ch length:1];
50         return [NSString stringWithFormat:@"%@ %d", str, historyIndex];
51 }
52
53
54
55 - (int)historyIndex {
56         if (!next) return 0;
57         int nextIndex = [next historyIndex];
58         if (nextIndex > 8 || nextIndex == -1) return -1;
59         return nextIndex + 1;
60 }
61
62
63 extern jmp_buf calc_jmp_buf;
64 extern jmp_buf jmpbuf;
65 extern int calc_jmp;
66 extern char *calc_error;
67
68 - (NSString *)result {
69
70         if (![expression length]) return nil;
71
72         int error;
73         if ((error = setjmp(calc_jmp_buf)) != 0) {
74                 reinitialize();
75                 calc_jmp = 1;
76                 return nil;
77         }
78         calc_jmp = 1;
79         
80         if ((error = setjmp(jmpbuf)) != 0) {
81                 reinitialize();
82                 return nil;
83         }       
84         reinitialize();
85
86         openstring([expression UTF8String], [expression length]);
87         math_divertio();
88         getcommands(1);
89         char *result = math_getdivertedio();
90         NSString *resultString = [[NSString stringWithCString:result] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
91
92         if (result) free(result);
93
94         return resultString;
95         
96 }
97
98
99 - (BOOL)expressionIsEmpty {
100         return ![expression length];
101 }
102
103
104
105 - (void)encodeWithCoder:(NSCoder *)coder {
106         [coder encodeObject:expression forKey:@"expression"];
107         [coder encodeObject:next forKey:@"next"];
108 }
109
110
111 - (id)initWithCoder:(NSCoder *)decoder {
112
113         self = [super init];
114         if (!self) return nil;
115         
116         [self setValue:[decoder decodeObjectForKey:@"expression"] forKey:@"expression"];
117         [self setValue:[decoder decodeObjectForKey:@"next"] forKey:@"next"];
118         return self;
119 }
120
121
122
123
124
125 @end