]> git.lyx.org Git - lyx.git/blob - src/mathed/math_utils.C
fix pullArg when pressing <Delete> at the end of an cell
[lyx.git] / src / mathed / math_utils.C
1 /* 
2  *  File:        math_utils.C
3  *  Purpose:     X independent general mathed routines
4  *  Author:      Alejandro Aguilar Sierra <asierra@servidor.unam.mx> 
5  *  Created:     August 1996
6  *  
7  *  Copyright: 1996, 1997 Alejandro Aguilar Sierra
8  *
9  *  License: GNU GPL version 2 or later
10  */
11
12 #include <algorithm>
13
14 #include "math_defs.h"
15 #include "symbol_def.h"
16
17 using std::sort;
18 using std::lower_bound;
19
20 namespace {
21
22 // This table includes all binary operators and relations
23 struct binary_op_pair {
24         short id;
25         short isrel;
26 };
27
28
29 binary_op_pair binary_op_table[] = {
30       { LM_leq, LMB_RELATION }, { LM_geq, LMB_RELATION }, 
31       { LM_equiv, LMB_RELATION }, { LM_models, LMB_RELATION }, 
32       { LM_prec, LMB_RELATION }, { LM_succ, LMB_RELATION }, 
33       { LM_sim, LMB_RELATION }, { LM_perp, LMB_RELATION }, 
34       { LM_preceq, LMB_RELATION }, { LM_succeq, LMB_RELATION }, 
35       { LM_simeq, LMB_RELATION }, { LM_mid, LMB_RELATION }, 
36       { LM_ll, LMB_RELATION }, { LM_gg, LMB_RELATION }, 
37       { LM_asymp, LMB_RELATION }, { LM_parallel, LMB_RELATION }, 
38       { LM_subset, LMB_RELATION }, { LM_supset, LMB_RELATION }, 
39       { LM_approx, LMB_RELATION }, { LM_smile, LMB_RELATION }, 
40       { LM_subseteq, LMB_RELATION }, { LM_supseteq, LMB_RELATION }, 
41       { LM_cong, LMB_RELATION }, { LM_frown, LMB_RELATION }, 
42       { LM_sqsubseteq, LMB_RELATION }, { LM_sqsupseteq, LMB_RELATION }, 
43       { LM_doteq, LMB_RELATION }, { LM_neq, LMB_RELATION }, 
44       { LM_in, LMB_RELATION }, { LM_ni, LMB_RELATION }, 
45       { LM_propto, LMB_RELATION }, { LM_notin, LMB_RELATION }, 
46       { LM_vdash, LMB_RELATION }, { LM_dashv, LMB_RELATION }, 
47       { LM_bowtie, LMB_RELATION },
48       { LM_pm, LMB_OPERATOR }, { LM_cap, LMB_OPERATOR }, 
49       { LM_diamond, LMB_OPERATOR }, { LM_oplus, LMB_OPERATOR },
50       { LM_mp, LMB_OPERATOR }, { LM_cup, LMB_OPERATOR }, 
51       { LM_bigtriangleup, LMB_OPERATOR }, { LM_ominus, LMB_OPERATOR },
52       { LM_times, LMB_OPERATOR }, { LM_uplus, LMB_OPERATOR }, 
53       { LM_bigtriangledown, LMB_OPERATOR }, { LM_otimes, LMB_OPERATOR },
54       { LM_div, LMB_OPERATOR }, { LM_sqcap, LMB_OPERATOR }, 
55       { LM_triangleright, LMB_OPERATOR }, { LM_oslash, LMB_OPERATOR },
56       { LM_cdot, LMB_OPERATOR }, { LM_sqcup, LMB_OPERATOR }, 
57       { LM_triangleleft, LMB_OPERATOR }, { LM_odot, LMB_OPERATOR },
58       { LM_star, LMB_OPERATOR }, { LM_vee, LMB_OPERATOR }, 
59       { LM_amalg, LMB_OPERATOR }, { LM_bigcirc, LMB_OPERATOR },
60       { LM_setminus, LMB_OPERATOR }, { LM_wedge, LMB_OPERATOR }, 
61       { LM_dagger, LMB_OPERATOR }, { LM_circ, LMB_OPERATOR },
62       { LM_bullet, LMB_OPERATOR }, { LM_wr, LMB_OPERATOR }, 
63       { LM_ddagger, LMB_OPERATOR }
64 };
65
66
67 struct comparator {
68         // used by sort and lower_bound
69         inline
70         int operator()(binary_op_pair const & a, binary_op_pair const & b) const
71         {
72                 return a.id < b.id;
73         }
74 };
75
76 } // namespace anon
77
78
79 int MathLookupBOP(short id)
80 {
81         static int const bopCount =
82                 sizeof(binary_op_table) / sizeof(binary_op_pair);
83         static bool issorted = false;
84         
85         if (!issorted) {
86                 sort(binary_op_table, binary_op_table + bopCount, comparator());
87                 issorted = true;
88         }
89
90         binary_op_pair search_elem = { id, 0 };
91         
92         binary_op_pair * res = lower_bound(binary_op_table,
93                                            binary_op_table + bopCount,
94                                            search_elem, comparator());
95         if (res != binary_op_table + bopCount && res->id == id)
96                 return res->isrel;
97         else
98                 return LMB_NONE;
99 }