forked from nim-lang/Nim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnimrepl.nim
172 lines (142 loc) · 5.09 KB
/
nimrepl.nim
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
#
#
# Nimrod REPL
# (c) Copyright 2012 Dominik Picheta
#
# See the file "copying.txt", included in this
# distribution, for details about the copyright.
#
import glib2, gtk2, gdk2, os, osproc, dialogs, strutils
when defined(tinyc):
const runCmd = "run"
else:
const runCmd = "c -r"
var nimExe = findExe("nimrod")
if nimExe.len == 0: nimExe = "../bin" / addFileExt("nimrod", os.exeExt)
proc execCode(code: string): string =
var f: TFile
if open(f, "temp.nim", fmWrite):
f.write(code)
f.close()
result = osproc.execProcess(
"$# $# --verbosity:0 --hint[Conf]:off temp.nim" % [nimExe, runCmd],
{poStdErrToStdOut})
else:
result = "cannot open file 'temp.nim'"
var shiftPressed = False
var w: gtk2.PWindow
var InputTextBuffer: PTextBuffer
var OutputTextBuffer: PTextBuffer
proc destroy(widget: PWidget, data: pgpointer){.cdecl.} =
main_quit()
proc FileOpenClicked(menuitem: PMenuItem, userdata: pgpointer) {.cdecl.} =
var path = ChooseFileToOpen(w)
if path != "":
var file = readFile(path)
if file != nil:
set_text(InputTextBuffer, file, len(file).gint)
else:
error(w, "Unable to read from file")
proc FileSaveClicked(menuitem: PMenuItem, userdata: pgpointer) {.cdecl.} =
var path = ChooseFileToSave(w)
if path == "": return
var startIter: TTextIter
var endIter: TTextIter
get_start_iter(InputTextBuffer, addr(startIter))
get_end_iter(InputTextBuffer, addr(endIter))
var InputText = get_text(InputTextBuffer, addr(startIter),
addr(endIter), False)
var f: TFile
if open(f, path, fmWrite):
f.write(InputText)
f.close()
else:
error(w, "Unable to write to file")
proc inputKeyPressed(widget: PWidget, event: PEventKey,
userdata: pgpointer): bool {.cdecl.} =
if ($keyval_name(event.keyval)).tolower() == "shift_l":
# SHIFT is pressed
shiftPressed = True
proc setError(msg: string) =
outputTextBuffer.setText(msg, msg.len.gint)
proc inputKeyReleased(widget: PWidget, event: PEventKey,
userdata: pgpointer): bool {.cdecl.} =
#echo(keyval_name(event.keyval))
if ($keyval_name(event.keyval)).tolower() == "shift_l":
# SHIFT is released
shiftPressed = False
if ($keyval_name(event.keyval)).tolower() == "return":
#echo($keyval_name(event.keyval), "Shift_L")
# Enter pressed
if shiftPressed == False:
var startIter: TTextIter
var endIter: TTextIter
get_start_iter(InputTextBuffer, addr(startIter))
get_end_iter(InputTextBuffer, addr(endIter))
var InputText = get_text(InputTextBuffer, addr(startIter),
addr(endIter), False)
try:
var r = execCode($InputText)
set_text(OutputTextBuffer, r, len(r).gint)
except EIO:
setError("Error: Could not open file temp.nim")
proc initControls() =
w = window_new(gtk2.WINDOW_TOPLEVEL)
set_default_size(w, 500, 600)
set_title(w, "Nimrod REPL")
discard signal_connect(w, "destroy", SIGNAL_FUNC(nimrepl.destroy), nil)
# MainBox (vbox)
var MainBox = vbox_new(False, 0)
add(w, MainBox)
# TopMenu (MenuBar)
var TopMenu = menu_bar_new()
show(TopMenu)
var FileMenu = menu_new()
var OpenMenuItem = menu_item_new("Open")
append(FileMenu, OpenMenuItem)
show(OpenMenuItem)
discard signal_connect(OpenMenuItem, "activate",
SIGNAL_FUNC(FileOpenClicked), nil)
var SaveMenuItem = menu_item_new("Save...")
append(FileMenu, SaveMenuItem)
show(SaveMenuItem)
discard signal_connect(SaveMenuItem, "activate",
SIGNAL_FUNC(FileSaveClicked), nil)
var FileMenuItem = menu_item_new("File")
set_submenu(FileMenuItem, FileMenu)
show(FileMenuItem)
append(TopMenu, FileMenuItem)
pack_start(MainBox, TopMenu, False, False, 0)
# VPaned - Seperates the InputTextView and the OutputTextView
var paned = vpaned_new()
set_position(paned, 450)
pack_start(MainBox, paned, True, True, 0)
show(paned)
# Init the TextBuffers
InputTextBuffer = text_buffer_new(nil)
OutputTextBuffer = text_buffer_new(nil)
# InputTextView (TextView)
var InputScrolledWindow = scrolled_window_new(nil, nil)
set_policy(InputScrolledWindow, POLICY_AUTOMATIC, POLICY_AUTOMATIC)
var InputTextView = text_view_new(InputTextBuffer)
add_with_viewport(InputScrolledWindow, InputTextView)
add1(paned, InputScrolledWindow)
show(InputScrolledWindow)
show(InputTextView)
discard signal_connect(InputTextView, "key-release-event",
SIGNAL_FUNC(inputKeyReleased), nil)
discard signal_connect(InputTextView, "key-press-event",
SIGNAL_FUNC(inputKeyPressed), nil)
# OutputTextView (TextView)
var OutputScrolledWindow = scrolled_window_new(nil, nil)
set_policy(OutputScrolledWindow, POLICY_AUTOMATIC, POLICY_AUTOMATIC)
var OutputTextView = text_view_new(OutputTextBuffer)
add_with_viewport(OutputScrolledWindow, OutputTextView)
add2(paned, OutputScrolledWindow)
show(OutputScrolledWindow)
show(OutputTextView)
show(w)
show(MainBox)
nimrod_init()
initControls()
main()