Skip to content

Commit 1028996

Browse files
committed
SDL: alt-num key to restore previous edit file
1 parent ca1ec5e commit 1028996

File tree

8 files changed

+133
-21
lines changed

8 files changed

+133
-21
lines changed

ChangeLog

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
2015-12-05
2+
Implemented loading recent files via ALT+1 .. ALT+9
3+
Fix potential editor crash with empty support widget
4+
Fix potential crash with debug target
5+
Fix restore path to only function when no other arguments supplied
6+
17
2015-11-01
28
Fix debugger launch in linux build
39
Fix editor display issue with keyword completion

configure.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ dnl This program is distributed under the terms of the GPL v2.0
77
dnl Download the GNU Public License (GPL) from www.gnu.org
88
dnl
99

10-
AC_INIT([smallbasic], [0.12.1])
10+
AC_INIT([smallbasic], [0.12.2])
1111
AC_CONFIG_SRCDIR([configure.ac])
1212

1313
AC_CANONICAL_TARGET

debian/changelog

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
smallbasic (0.12.2) unstable; urgency=low
2+
* Various - see web site
3+
4+
-- Chris Warren-Smith <cwarrensmith@gmail.com> Sat, 5 Dec 2015 09:45:25 +1000
5+
16
smallbasic (0.12.1) unstable; urgency=low
27
* Various - see web site
38

ide/android/assets/main.bas

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ sub do_about()
5555
print "(_ ._ _ _.|||_) /\ (_ |/ "
5656
print "__)| | |(_||||_)/--\__)|\_"
5757
print
58-
print "Version 0.12.1"
58+
print "Version 0.12.2"
5959
print
6060
print "Copyright (c) 2002-2015 Chris Warren-Smith"
6161
print "Copyright (c) 1999-2006 Nic Christopoulos" + chr(10)

src/platform/sdl/editor.cpp

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
#include "common/sbapp.h"
1212
#include "ui/textedit.h"
1313
#include "platform/sdl/runtime.h"
14+
#include "platform/sdl/settings.h"
15+
16+
using namespace strlib;
1417

1518
void onlineHelp(Runtime *runtime, TextEditInput *widget) {
1619
char path[100];
@@ -29,26 +32,27 @@ void onlineHelp(Runtime *runtime, TextEditInput *widget) {
2932
runtime->browseFile(path);
3033
}
3134

32-
void System::editSource(strlib::String &loadPath) {
33-
logEntered();
34-
35-
strlib::String fileName;
35+
void setupStatus(String &dirtyFile, String &cleanFile, String &loadPath) {
36+
const char *help = " Ctrl+h (C-h)=Help";
37+
String fileName;
3638
int i = loadPath.lastIndexOf('/', 0);
3739
if (i != -1) {
3840
fileName = loadPath.substring(i + 1);
3941
} else {
4042
fileName = loadPath;
4143
}
42-
43-
const char *help = " Ctrl+h (C-h)=Help";
44-
strlib::String dirtyFile;
44+
dirtyFile.empty();
4545
dirtyFile.append(" * ");
4646
dirtyFile.append(fileName);
4747
dirtyFile.append(help);
48-
strlib::String cleanFile;
48+
cleanFile.empty();
4949
cleanFile.append(" - ");
5050
cleanFile.append(fileName);
5151
cleanFile.append(help);
52+
}
53+
54+
void System::editSource(String &loadPath) {
55+
logEntered();
5256

5357
int w = _output->getWidth();
5458
int h = _output->getHeight();
@@ -58,8 +62,11 @@ void System::editSource(strlib::String &loadPath) {
5862
TextEditInput *editWidget = new TextEditInput(_programSrc, charWidth, charHeight, 0, 0, w, h);
5963
TextEditHelpWidget *helpWidget = new TextEditHelpWidget(editWidget, charWidth, charHeight);
6064
TextEditInput *widget = editWidget;
61-
_modifiedTime = getModifiedTime();
65+
String dirtyFile;
66+
String cleanFile;
6267

68+
setupStatus(dirtyFile, cleanFile, loadPath);
69+
_modifiedTime = getModifiedTime();
6370
editWidget->updateUI(NULL, NULL);
6471
editWidget->setLineNumbers();
6572
editWidget->setFocus(true);
@@ -85,6 +92,7 @@ void System::editSource(strlib::String &loadPath) {
8592
_state = kEditState;
8693

8794
showCursor(kIBeam);
95+
setRecentFile(loadPath.c_str());
8896

8997
while (_state == kEditState) {
9098
MAEvent event = getNextEvent();
@@ -216,6 +224,26 @@ void System::editSource(strlib::String &loadPath) {
216224
_output->selectScreen(SOURCE_SCREEN);
217225
_state = kEditState;
218226
break;
227+
case SB_KEY_ALT('1'):
228+
case SB_KEY_ALT('2'):
229+
case SB_KEY_ALT('3'):
230+
case SB_KEY_ALT('4'):
231+
case SB_KEY_ALT('5'):
232+
case SB_KEY_ALT('6'):
233+
case SB_KEY_ALT('7'):
234+
case SB_KEY_ALT('8'):
235+
case SB_KEY_ALT('9'):
236+
if (editWidget->isDirty()) {
237+
saveFile(editWidget, loadPath);
238+
}
239+
if (getRecentFile(loadPath, event.key - SB_KEY_ALT('1'))) {
240+
loadSource(loadPath.c_str());
241+
editWidget->reload(_programSrc);
242+
dirty = !editWidget->isDirty();
243+
setupStatus(dirtyFile, cleanFile, loadPath);
244+
_modifiedTime = getModifiedTime();
245+
}
246+
break;
219247
default:
220248
redraw = widget->edit(event.key, sw, charWidth);
221249
break;

src/platform/sdl/settings.cpp

Lines changed: 80 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
#include <ctype.h>
1414
#include <sys/stat.h>
1515

16-
#include "platform/sdl/settings.h"
1716
#include "ui/utils.h"
1817
#include "ui/textedit.h"
1918
#include "common/smbas.h"
19+
#include "platform/sdl/settings.h"
2020

2121
static const char *ENV_VARS[] = {
2222
"APPDATA", "HOME", "TMP", "TEMP", "TMPDIR"
@@ -29,13 +29,17 @@ static const char *ENV_VARS[] = {
2929
#define DEFAULT_WIDTH 640
3030
#define DEFAULT_HEIGHT 480
3131
#define DEFAULT_SCALE 100
32+
#define NUM_RECENT_ITEMS 9
3233

3334
#if defined(__MINGW32__)
3435
#define makedir(f) mkdir(f)
3536
#else
3637
#define makedir(f) mkdir(f, 0700)
3738
#endif
3839

40+
strlib::String recentPath[NUM_RECENT_ITEMS];
41+
int recentPosition[NUM_RECENT_ITEMS];
42+
3943
FILE *openConfig(const char *flags, bool debug) {
4044
FILE *result = NULL;
4145
char path[FILENAME_MAX];
@@ -94,19 +98,39 @@ int nextHex(FILE *fp, int def) {
9498
}
9599

96100
//
97-
// sets the next string in the file as the current working directory
101+
// returns the length of the next string
98102
//
99-
void restorePath(FILE *fp) {
103+
int nextString(FILE *fp) {
100104
int pos = ftell(fp);
101105
int len = 0;
102-
for (int c = fgetc(fp); c != EOF && c != ',' && c != '\n'; c = fgetc(fp)) {
106+
for (int c = fgetc(fp); c != EOF; c = fgetc(fp)) {
107+
if (c == '\n') {
108+
if (len > 0) {
109+
// string terminator
110+
break;
111+
} else {
112+
// skip previous terminator
113+
pos++;
114+
continue;
115+
}
116+
}
103117
len++;
104118
}
105119
fseek(fp, pos, SEEK_SET);
120+
return len;
121+
}
122+
123+
//
124+
// sets the next string in the file as the current working directory
125+
//
126+
void restorePath(FILE *fp, bool restoreDir) {
127+
int len = nextString(fp);
106128
if (len > 0) {
107129
String path;
108130
path.append(fp, len);
109-
chdir(path.c_str());
131+
if (restoreDir) {
132+
chdir(path.c_str());
133+
}
110134
}
111135
}
112136

@@ -127,8 +151,17 @@ void restoreSettings(SDL_Rect &rect, int &fontScale, bool debug, bool restoreDir
127151
for (int i = 0; i < THEME_COLOURS; i++) {
128152
g_user_theme[i] = nextHex(fp, g_user_theme[i]);
129153
}
130-
if (restoreDir) {
131-
restorePath(fp);
154+
restorePath(fp, restoreDir);
155+
156+
// restore recent paths
157+
for (int i = 0; i < NUM_RECENT_ITEMS; i++) {
158+
int len = nextString(fp);
159+
if (len > 1) {
160+
recentPath[i].empty();
161+
recentPath[i].append(fp, len);
162+
} else {
163+
break;
164+
}
132165
}
133166
fclose(fp);
134167
} else {
@@ -158,6 +191,7 @@ void saveSettings(SDL_Window *window, int fontScale, bool debug) {
158191
for (int i = 0; i < THEME_COLOURS; i++) {
159192
fprintf(fp, (i + 1 < THEME_COLOURS ? "%06x," : "%06x"), g_user_theme[i]);
160193
}
194+
fprintf(fp, "\n");
161195

162196
// save the current working directory
163197
char path[FILENAME_MAX + 1];
@@ -168,11 +202,47 @@ void saveSettings(SDL_Window *window, int fontScale, bool debug) {
168202
path[i] = '/';
169203
}
170204
}
171-
fprintf(fp, "\n%s\n", path);
172-
} else {
173-
fprintf(fp, "\n%s\n", path);
174205
}
206+
fprintf(fp, "%s\n", path);
207+
208+
// save the recent editor paths
209+
for (int i = 0; i < NUM_RECENT_ITEMS; i++) {
210+
if (recentPath[i].length() > 0) {
211+
fprintf(fp, "%s\n", recentPath[i].c_str());
212+
}
213+
}
214+
175215
fclose(fp);
176216
}
177217
}
178218

219+
bool getRecentFile(strlib::String &path, unsigned position) {
220+
bool result = false;
221+
if (position < NUM_RECENT_ITEMS && recentPath[position].length() > 0 &&
222+
!recentPath[position].equals(path)) {
223+
path.empty();
224+
path.append(recentPath[position]);
225+
result = true;
226+
}
227+
return result;
228+
}
229+
230+
void setRecentFile(const char *filename) {
231+
bool found = false;
232+
for (int i = 0; i < NUM_RECENT_ITEMS && !found; i++) {
233+
if (recentPath[i].equals(filename, false)) {
234+
found = true;
235+
}
236+
}
237+
if (!found) {
238+
// shift items downwards
239+
for (int i = NUM_RECENT_ITEMS - 1; i > 0; i--) {
240+
recentPath[i].empty();
241+
recentPath[i].append(recentPath[i - 1]);
242+
}
243+
244+
// create new item in first position
245+
recentPath[0].empty();
246+
recentPath[0].append(filename);
247+
}
248+
}

src/platform/sdl/settings.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
void restoreSettings(SDL_Rect &rect, int &fontScale, bool debug, bool restoreDir);
1515
void saveSettings(SDL_Window *window, int fontScale, bool debug);
16+
void setRecentFile(const char *path);
17+
bool getRecentFile(strlib::String &path, unsigned position);
1618

1719
#endif
1820

src/ui/textedit.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ const char *helpText =
103103
"A-g goto line\n"
104104
"A-n trim line-endings\n"
105105
"A-t select theme\n"
106+
"A-<n> recent file\n"
106107
"SHIFT-<arrow> select\n"
107108
"TAB indent line\n"
108109
"F1,A-h keyword help\n"

0 commit comments

Comments
 (0)