-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLispIDEView.h
178 lines (164 loc) · 4.19 KB
/
LispIDEView.h
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
// LispIDEView.h : interface of the CLispIDEView class
//
////////////////////////////////////////////////////////////////////////////
#pragma once
#include <iostream>
#include <fstream>
using namespace std;
class CLispIDEView :
public CWindowImpl<CLispIDEView, CScintillaLispCtrl>
{
public:
DECLARE_WND_SUPERCLASS(NULL, "Scintilla")
CString m_FilePath;
CFont m_Font;
BEGIN_MSG_MAP_EX(CLispIDEView)
MSG_WM_ERASEBKGND(OnEraseBackGround)
MSG_WM_CHAR(OnChar)
MESSAGE_HANDLER(WM_CONTEXTMENU, OnContextMenu)
NOTIFY_CODE_HANDLER_EX(SCN_UPDATEUI, OnUpdateUI)
END_MSG_MAP()
BOOL PreTranslateMessage(MSG* pMsg) {
pMsg;
return FALSE;
}
LRESULT OnContextMenu(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled) {
GetTopLevelParent().SendMessage(uMsg, wParam, lParam);
return 1;
}
void Init() {
CScintillaLispCtrl::Init();
ClearCmdKey(SCK_RETURN + (SCMOD_NORM << 16));
ClearCmdKey(SCK_RETURN + (SCMOD_SHIFT << 16));
}
void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if (nChar == VK_RETURN) {
if (::GetKeyState(VK_SHIFT) < 0 || nFlags & 0x0100) {
SetMsgHandled(TRUE);
CString pipetext = GetPipeString();
if (!pipetext.IsEmpty()) {
GetTopLevelParent().SendMessage(WM_USER + 2, (WPARAM)&pipetext, 0);
}
}
else {
SetMsgHandled(FALSE);
Call(SCI_NEWLINE, 0, 0);
}
}
else {
SetMsgHandled(FALSE);
}
}
CString GetPipeString() {
CString text = GetSelText();
if (text.IsEmpty()) {
Sci_Position lStart = GetCurrentPos() - 1;
int chBr = GetCharAt(lStart);
if (chBr == ')') {
Sci_Position lEnd = BraceMatch(lStart, 0);
// if there is a matching brace highlight it
if (lEnd >= 0) {
Sci_TextRange tr;
tr.chrg.cpMin = lEnd;
tr.chrg.cpMax = lStart + 1;
int len = tr.chrg.cpMax - tr.chrg.cpMin + 1;
char *buf = new char[len];
tr.lpstrText = buf;
GetTextRange(&tr);
text = tr.lpstrText;
delete[] buf;
}
}
}
return text;
}
LRESULT OnEraseBackGround(HDC hdc) {
return 1;
}
/////////////////////////////////////
// @mfunc Try to load a new file
// @rvalue BOOL | FALSE on error - TRUE on success
//
BOOL LoadFile(LPCTSTR szPath) //@parm filename of to load
{
// if pathname is empty do nothing
if (szPath == NULL || *szPath == '\0')
return TRUE;
// try to get extension and figure out what lexer to use
CString strFile(szPath);
BOOL bReturn = TRUE;
// ty to open file in sharing mode
std::ifstream file;
streamoff len = 0L;
streamsize nTotal;
TCHAR *szBuffer = NULL;
file.open(szPath, ios::in | ios::binary);
// ok success - try to get length of file
if (file.is_open())
{
file.seekg(0, ios::end);
len = file.tellg();
if (len > 0)
{
// alloc new buffer of sie = filesize+1 for termination NULL
szBuffer = new TCHAR[(UINT)len + 1];
if (szBuffer != NULL)
{
file.seekg(0, ios::beg);
file.read(szBuffer, len);
nTotal = file.gcount();
if (nTotal > 0 && nTotal <= len)
szBuffer[nTotal] = '\0';
// read error
if (file.bad())
{
file.close();
bReturn = FALSE;
}
}
else
{
file.close();
bReturn = FALSE;
}
}
file.close();
// set text to control
SetText(szBuffer);
// Respect the users EOL settings
ConvertEOLs(GetEOLMode());
// tell scintilla that we have an unmodified document
SetSavePoint();
GotoPos(0);
EmptyUndoBuffer();
m_FilePath = szPath;
}
// file open error - return
else {
bReturn = FALSE;
}
// clean up
if (szBuffer != NULL) {
delete[] szBuffer;
}
return bReturn;
}
BOOL SaveFile(LPCTSTR szPath) //@parm filename to save to
{
std::ofstream file;
file.open(szPath, ios_base::out | ios_base::binary);
if (file.fail())
{
MessageBox("Save failed", "LispIDE", MB_OK | MB_ICONERROR);
return FALSE;
}
Sci_Position buflen = GetLength() + 1; //last NULL
CString text = this->GetText((int)buflen);
file.write(text, buflen - 1);
file.close();
SetSavePoint();
m_FilePath = szPath;
return TRUE;
}
};