1 /*******************************************************************************
2  * Copyright (c) 2013-2021, Andrés Martinelli <andmarti@gmail.com>             *
3  * All rights reserved.                                                        *
4  *                                                                             *
5  * This file is a part of SC-IM                                                *
6  *                                                                             *
7  * SC-IM is a spreadsheet program that is based on SC. The original authors    *
8  * of SC are James Gosling and Mark Weiser, and mods were later added by       *
9  * Chuck Martin.                                                               *
10  *                                                                             *
11  * Redistribution and use in source and binary forms, with or without          *
12  * modification, are permitted provided that the following conditions are met: *
13  * 1. Redistributions of source code must retain the above copyright           *
14  *    notice, this list of conditions and the following disclaimer.            *
15  * 2. Redistributions in binary form must reproduce the above copyright        *
16  *    notice, this list of conditions and the following disclaimer in the      *
17  *    documentation and/or other materials provided with the distribution.     *
18  * 3. All advertising materials mentioning features or use of this software    *
19  *    must display the following acknowledgement:                              *
20  *    This product includes software developed by Andrés Martinelli            *
21  *    <andmarti@gmail.com>.                                                    *
22  * 4. Neither the name of the Andrés Martinelli nor the                        *
23  *   names of other contributors may be used to endorse or promote products    *
24  *   derived from this software without specific prior written permission.     *
25  *                                                                             *
26  * THIS SOFTWARE IS PROVIDED BY ANDRES MARTINELLI ''AS IS'' AND ANY            *
27  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED   *
28  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE      *
29  * DISCLAIMED. IN NO EVENT SHALL ANDRES MARTINELLI BE LIABLE FOR ANY           *
30  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES  *
31  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;*
32  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND *
33  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT  *
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE       *
35  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.           *
36  *******************************************************************************/
37 
38 /**
39  * \file digraphs.c
40  * \author V. Guruprasad <prasad@inspiredresearch.com>
41  * \date 2019-10-02
42  * \brief A simple implementation of digraphs like in Vim
43  */
44 
45 #include <wchar.h>
46 #include <wctype.h>
47 
48 wint_t digraphs[][2] = {
49     { L'a', L'α' }, { L'b', L'β' }, { L'c', L'ξ' }, { L'd', L'δ' },
50     { L'e', L'ε' }, { L'f', L'φ' }, { L'g', L'γ' }, { L'h', L'θ' },
51     { L'i', L'ι' }, { L'j', L'ϊ' }, { L'k', L'κ' }, { L'l', L'λ' },
52     { L'm', L'μ' }, { L'n', L'ν' }, { L'o', L'ο' }, { L'p', L'π' },
53     { L'q', L'ψ' }, { L'r', L'ρ' }, { L's', L'σ' }, { L't', L'τ' },
54     { L'u', L'υ' }, { L'v', L'ϋ' }, { L'w', L'ω' }, { L'x', L'χ' },
55     { L'y', L'η' }, { L'z', L'ζ' },
56 
57     { L'A', L'Α' }, { L'B', L'Β' }, { L'C', L'Ξ' }, { L'D', L'Δ' },
58     { L'E', L'Ε' }, { L'F', L'Φ' }, { L'G', L'Γ' }, { L'H', L'Θ' },
59     { L'I', L'Ι' }, { L'J', L'Ϊ' }, { L'K', L'Κ' }, { L'L', L'Λ' },
60     { L'M', L'Μ' }, { L'N', L'Ν' }, { L'O', L'Ο' }, { L'P', L'Π' },
61     { L'Q', L'Ψ' }, { L'R', L'Ρ' }, { L'S', L'Σ' }, { L'T', L'Τ' },
62     { L'U', L'Υ' }, { L'V', L'Ϋ' }, { L'W', L'Ω' }, { L'X', L'Χ' },
63     { L'Y', L'Η' }, { L'Z', L'Ζ' },
64 };
65 
66 wint_t trigraphs[][3] = {
67     { L'*', L's', L'ς' },
68     { L'R', L'T', L'√' },
69     { L'F', L'A', L'∀' },
70     { L'T', L'E', L'∃' },
71     { L'N', L'B', L'∇' },
72     { L'(', L'-', L'∈' },
73     { L'-', L')', L'∋' },
74     { L'd', L'P', L'∂' },
75     { L'I', L'n', L'∫' },
76     { L'I', L'o', L'∮' },
77     { L'D', L'I', L'∬' },
78     { L'*', L'P', L'∏' },
79     { L'+', L'Z', L'∑' },
80     { L'+', L'-', L'±' },
81     { L'-', L'+', L'∓' },
82     { L'0', L'(', L'∝' },
83     { L'0', L'0', L'∞' },
84     { L'-', L'L', L'∟' },
85     { L'-', L'V', L'∠' },
86     { L'P', L'P', L'∥' },
87     { L'A', L'N', L'∧' },
88     { L'O', L'R', L'∨' },
89     { L')', L'U', L'∪' },
90     { L'(', L'U', L'∩' },
91     { L')', L'C', L'⊃' },
92     { L'(', L'C', L'⊂' },
93     { L')', L'_', L'⊇' },
94     { L'(', L'_', L'⊆' },
95     { L'.', L':', L'∴' },
96     { L':', L'.', L'∵' },
97     { L'?', L'-', L'≃' },
98     { L'!', L'=', L'≠' },
99     { L'=', L'3', L'≡' },
100     { L'=', L'<', L'≤' },
101     { L'>', L'=', L'≥' },
102     { L'<', L'*', L'≪' },
103     { L'>', L'*', L'≫' },
104     { L'!', L'<', L'≮' },
105     { L'!', L'>', L'≯' },
106 
107     { L'\0', L'\0', L'\0' },
108 };
109 
get_digraph(wint_t x,wint_t y)110 wint_t get_digraph(wint_t x, wint_t y) {
111     if (y == L'*') {
112         if (x >= L'a' && x <= L'z')
113             return digraphs[x - L'a'][1];
114         if (x >= L'A' && x <= L'Z')
115             return digraphs[x - L'A' + 26][1];
116     }
117     wint_t (*tp)[3] = trigraphs;
118     for (; *tp[0] != L'\0'; tp++) {
119         if (tp[0][0] == x && tp[0][1] == y)
120             return tp[0][2];
121     }
122     return x;
123 }
124 
125