jcs
/amend
/amendments
/37
settings: Finally add a settings screen to set tab width and author
jcs made amendment 37 over 3 years ago
--- amend.π.r Wed Dec 15 17:05:38 2021
+++ amend.π.r Thu Dec 16 20:51:30 2021
@@ -5,11 +5,12 @@ data 'MENU' (128) {
};
data 'MENU' (129) {
- $"0081 0000 0000 0000 0000 FFFF FFF7 0446" /* .Å.............F */
- $"696C 650B 4E65 7720 5265 706F 2E2E 2E00" /* ile.New Repo.... */
+ $"0081 0000 0000 0000 0000 FFFF FFD7 0446" /* .Å...........◊.F */
+ $"696C 650B 4E65 7720 5265 706F 2E2E 2E00" /* ile.New Repo.... */
$"4E00 000C 4F70 656E 2052 6570 6F2E 2E2E" /* N...Open Repo... */
- $"004F 0000 012D 0000 0000 0451 7569 7400" /* .O...-.....Quit. */
- $"5100 0000" /* Q... */
+ $"004F 0000 012D 0000 0000 0B53 6574 7469" /* .O...-.....Setti */
+ $"6E67 732E 2E2E 0000 0000 012D 0000 0000" /* ngs........-.... */
+ $"0451 7569 7400 5100 0000" /* .Quit.Q... */
};
data 'MENU' (130) {
@@ -64,6 +65,18 @@ data 'DITL' (130) {
$"0802 5E30" /* ..^0 */
};
+data 'DITL' (131, "SETTINGS") {
+ $"0005 0000 0000 0050 00BE 0064 00F8 0404" /* .......P.æ.d.... */
+ $"5361 7665 0000 0000 0050 0014 0064 004E" /* Save.....P...d.N */
+ $"0406 4361 6E63 656C 0000 0000 0014 008C" /* ..Cancel.......å */
+ $"0024 00F5 1007 756E 6B6E 6F77 6E61 0000" /* .$....unknowna.. */
+ $"0000 0032 008C 0042 00AB 1001 346F 0000" /* ...2.å.B.´..4o.. */
+ $"0000 0014 0014 0025 0084 880E 436F 6D6D" /* .......%.Ñà.Comm */
+ $"6974 2061 7574 686F 723A 0000 0000 0032" /* it author:.....2 */
+ $"0014 0042 005F 880A 5461 6220 7769 6474" /* ...B._à.Tab widt */
+ $"683A" /* h: */
+};
+
data 'WIND' (128) {
$"002B 0060 012B 01A0 0003 0100 0000 0000" /* .+.`.+.†........ */
$"0000 0A41 626F 7574 2043 6172 6C56 280A" /* ...About CarlV(. */
@@ -75,25 +88,14 @@ data 'vers' (1) {
$"6A6F 7368 7561 2073 7465 696E" /* joshua stein */
};
-data 'STR#' (128, "FILE_EXTENSIONS") {
- $"0002 022E 6302 2E68" /* ....c..h */
-};
-
-data 'STR ' (130, "TABWIDTH") {
+data 'STR ' (129, "TABWIDTH") {
$"0134" /* .4 */
};
-data 'STR ' (128, "LAST_FROM") {
- $"294D 6163 696E 746F 7368 3A62 6163 6B75" /* )Macintosh:backu */
- $"7073 3A63 6172 6C20 3230 3231 3039 3234" /* ps:carl 20210924 */
- $"3A62 726F 7773 6572 2E63" /* :browser.c */
+data 'STR ' (128, "AUTHOR") {
+ $"0775 6E6B 6E6F 776E" /* .unknown */
};
-data 'STR ' (129, "LAST_TO") {
- $"184D 6163 696E 746F 7368 3A63 6172 6C3A" /* .Macintosh:carl: */
- $"6272 6F77 7365 722E 63" /* browser.c */
-};
-
data 'LDEF' (128, "commit list", purgeable) {
$"2F3A 0004 4E75 0000 0000" /* /:..Nu.... */
};
@@ -120,6 +122,16 @@ data 'TMPL' (129, "AFIL") {
data 'DLOG' (128, "WAIT_DLOG_ID") {
$"0066 0059 00A0 01A4 0001 0100 0100 0000" /* .f.Y.†.§........ */
$"0000 0080 001A 280A" /* ...Ä..(. */
+};
+
+data 'DLOG' (129) {
+ $"004C 0092 0114 0182 0001 0100 0100 0000" /* .L.í...Ç........ */
+ $"0000 0081 00" /* ...Å. */
+};
+
+data 'DLOG' (130, "SETTINGS_DLOG_ID") {
+ $"005C 0068 00D2 0178 0001 0100 0000 0000" /* .\.h.“.x........ */
+ $"0000 0083 00BA 280A" /* ...É.∫(. */
};
data 'BNDL' (128) {
--- amend.h Wed Nov 17 17:43:23 2021
+++ amend.h Thu Dec 16 17:18:28 2021
@@ -24,7 +24,8 @@
#define FILE_MENU_ID 129
#define FILE_MENU_NEW_ID 1
#define FILE_MENU_OPEN_ID 2
-#define FILE_MENU_QUIT_ID 4
+#define FILE_MENU_SETTINGS_ID 4
+#define FILE_MENU_QUIT_ID 6
#define EDIT_MENU_ID 130
#define EDIT_MENU_CUT_ID 1
@@ -42,8 +43,15 @@
#define COMMIT_LDEF_ID 128
-#define TABWIDTH_ID 130
+#define SETTINGS_DLOG_ID 130
+#define SETTINGS_SAVE_ID 1
+#define SETTINGS_CANCEL_ID 2
+#define SETTINGS_AUTHOR_ID 3
+#define SETTINGS_TABWIDTH_ID 4
+#define STR_AUTHOR_ID 128
+#define STR_TABWIDTH_ID 129
+
struct tmpl {
Handle h;
ResType type;
@@ -52,5 +60,7 @@ struct tmpl {
};
extern struct tmpl tmpls[10];
extern short ntmpls;
+
+extern short app_res_frefid;
extern MenuHandle file_menu, edit_menu, repo_menu, commit_menu;
--- browser.c Wed Dec 15 16:15:08 2021
+++ browser.c Thu Dec 16 17:19:32 2021
@@ -24,6 +24,7 @@
#include "diff.h"
#include "patch.h"
#include "repo.h"
+#include "settings.h"
#include "tetab.h"
#include "util.h"
@@ -159,6 +160,7 @@ browser_init(struct repo *repo)
te_bounds = bounds;
InsetRect(&te_bounds, 2, 2);
browser->diff_te = TEStylNew(&te_bounds, &bounds);
+ TETabWidth = settings.tabwidth;
TEAutoView(true, browser->diff_te);
TETabEnable(browser->diff_te);
--- committer.c Wed Dec 15 16:16:11 2021
+++ committer.c Thu Dec 16 17:20:00 2021
@@ -23,6 +23,7 @@
#include "committer.h"
#include "diff.h"
#include "repo.h"
+#include "settings.h"
#include "tetab.h"
#include "util.h"
@@ -489,7 +490,7 @@ committer_commit(struct committer *committer)
repo_commit(committer->browser->repo, committer->diffed_files,
committer->ndiffed_files, committer->diff_adds, committer->diff_subs,
- (*(committer->log_te))->hText, loglen,
+ settings.author, (*(committer->log_te))->hText, loglen,
(*(committer->diff_te))->hText, committer->diff_te_len);
HUnlock(committer->diff_te);
--- main.c Wed Dec 15 16:57:40 2021
+++ main.c Thu Dec 16 17:18:57 2021
@@ -21,10 +21,12 @@
#include "browser.h"
#include "committer.h"
#include "repo.h"
+#include "settings.h"
#include "util.h"
MenuHandle file_menu, edit_menu, repo_menu, commit_menu;
Handle commit_list_ldef_h;
+short app_res_frefid;
short quitting = 0;
struct browser *cur_browser = NULL;
@@ -57,8 +59,12 @@ main(void)
InitDialogs(0);
InitCursor();
MaxApplZone();
+
+ app_res_frefid = CurResFile();
err_init();
+
+ settings_load();
mbar = GetNewMBar(MBAR_ID);
SetMenuBar(mbar);
@@ -90,7 +96,7 @@ main(void)
err(1, "Can't find commit list LDEF %d", COMMIT_LDEF_ID);
HLock(commit_list_ldef_h);
((tCodeStub *)*commit_list_ldef_h)->addr = &commit_list_ldef;
-
+
/* see if we were started by double-clicking a .repo file */
CountAppFiles(&finder_action, &finder_count);
@@ -264,6 +270,9 @@ handle_menu(long menu_id)
cur_browser = browser_init(repo);
break;
}
+ case FILE_MENU_SETTINGS_ID:
+ settings_edit();
+ break;
case FILE_MENU_QUIT_ID:
if (cur_browser) {
browser_close(cur_browser);
--- repo.c Wed Dec 15 16:12:29 2021
+++ repo.c Wed Dec 15 21:27:04 2021
@@ -859,7 +859,8 @@ repo_export_patch(struct repo *repo, struct repo_commi
void
repo_commit(struct repo *repo, short *files, short nfiles, short adds,
- short subs, Handle log, short loglen, Handle diff, unsigned long difflen)
+ short subs, char *author, Handle log, short loglen, Handle diff,
+ unsigned long difflen)
{
Str255 tfilename;
struct repo_file *file;
@@ -867,10 +868,9 @@ repo_commit(struct repo *repo, short *files, short nfi
FInfo finfo;
short commit_len, pos = 0, len;
time_t date;
- char author[] = "jcs"; /* XXX */
long fsize;
short i, commit_id, id, error, frefnum;
-
+
/* date (long) */
commit_len = sizeof(long);
@@ -897,7 +897,7 @@ repo_commit(struct repo *repo, short *files, short nfi
(*commith)[pos++] = (date >> 16) & 0xff;
(*commith)[pos++] = (date >> 8) & 0xff;
(*commith)[pos++] = date & 0xff;
-
+
(*commith)[pos++] = strlen(author);
for (i = 0; i < strlen(author); i++)
(*commith)[pos++] = author[i];
--- repo.h Wed Dec 15 16:06:05 2021
+++ repo.h Wed Dec 15 21:27:28 2021
@@ -95,6 +95,7 @@ short repo_checkout_file(struct repo *repo, struct rep
void repo_export_patch(struct repo *repo, struct repo_commit *commit,
short vrefnum, Str255 filename);
void repo_commit(struct repo *repo, short *files, short nfiles, short adds,
- short subs, Handle log, short loglen, Handle diff, unsigned long difflen);
+ short subs, char *author, Handle log, short loglen, Handle diff,
+ unsigned long difflen);
#endif
--- settings.c Thu Dec 16 20:48:42 2021
+++ settings.c Thu Dec 16 20:48:42 2021
@@ -0,0 +1,157 @@
+/*
+ * Copyright (c) 2021 joshua stein <jcs@jcs.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <stdio.h>
+#include <string.h>
+
+#include "amend.h"
+#include "settings.h"
+#include "util.h"
+
+struct settings settings = { 0 };
+
+void
+settings_load(void)
+{
+ short cur_res;
+ char *author;
+
+ cur_res = CurResFile();
+ UseResFile(app_res_frefid);
+
+ author = xGetStringAsChar(STR_AUTHOR_ID);
+ memcpy(settings.author, author, sizeof(settings.author));
+ settings.author[sizeof(settings.author)] = '\0';
+ free(author);
+ if (settings.author[0] == '\0')
+ sprintf(settings.author, "unknown");
+
+ settings.tabwidth = (short)xGetStringAsLong(STR_TABWIDTH_ID);
+ if (settings.tabwidth < 1 || settings.tabwidth > 20) {
+ warn("Bogus tabwidth resource %d", settings.tabwidth);
+ settings.tabwidth = 4;
+ }
+
+ UseResFile(cur_res);
+}
+
+void
+settings_save(void)
+{
+ Handle res;
+ size_t l;
+ short cur_res;
+ char tmp[8];
+
+ cur_res = CurResFile();
+ UseResFile(app_res_frefid);
+
+ res = xGetResource('STR ', STR_AUTHOR_ID);
+ HLock(res);
+ l = strlen(settings.author);
+ SetHandleSize(res, l + 1);
+ memcpy(*res, settings.author, l + 1);
+ CtoPstr(*res);
+ HUnlock(res);
+ ChangedResource(res);
+ WriteResource(res);
+ ReleaseResource(res);
+
+ res = xGetResource('STR ', STR_TABWIDTH_ID);
+ HLock(res);
+ sprintf(tmp, "%d", settings.tabwidth);
+ l = strlen(tmp);
+ SetHandleSize(res, l + 1);
+ memcpy(*res, tmp, l + 1);
+ CtoPstr(*res);
+ HUnlock(res);
+ ChangedResource(res);
+ WriteResource(res);
+ ReleaseResource(res);
+
+ UseResFile(cur_res);
+}
+
+void
+settings_edit(void)
+{
+ Str255 txt;
+ Handle ihandle;
+ size_t len;
+ short done = 0, itype, hit, tabwidth, cur_res;
+ DialogPtr dlg;
+ Rect irect;
+
+ dlg = GetNewDialog(SETTINGS_DLOG_ID, nil, (WindowPtr)-1);
+ if (dlg == NULL)
+ err(1, "Can't find settings DLOG %d", SETTINGS_DLOG_ID);
+
+ GetDItem(dlg, SETTINGS_AUTHOR_ID, &itype, &ihandle, &irect);
+ memcpy(txt, settings.author, strlen(settings.author) + 1);
+ CtoPstr(txt);
+ SetIText(ihandle, txt);
+
+ GetDItem(dlg, SETTINGS_TABWIDTH_ID, &itype, &ihandle, &irect);
+ sprintf((char *)&txt, "%d", settings.tabwidth);
+ CtoPstr(txt);
+ SetIText(ihandle, txt);
+
+ ShowWindow(dlg);
+
+ while (!done) {
+ ModalDialog(nil, &hit);
+ switch (hit) {
+ case SETTINGS_SAVE_ID:
+ done = 1;
+ break;
+ case SETTINGS_CANCEL_ID:
+ goto done;
+ case SETTINGS_AUTHOR_ID:
+ break;
+ case SETTINGS_TABWIDTH_ID:
+ break;
+ }
+ }
+
+ cur_res = CurResFile();
+ UseResFile(app_res_frefid);
+
+ GetDItem(dlg, SETTINGS_AUTHOR_ID, &itype, &ihandle, &irect);
+ GetIText(ihandle, txt);
+ PtoCstr(txt);
+ if (strlen((char *)txt) >= sizeof(settings.author)) {
+ warn("Author setting is too long (%ld max)",
+ sizeof(settings.author) - 1);
+ goto done;
+ }
+ memcpy(settings.author, txt, strlen((char *)txt) + 1);
+
+ GetDItem(dlg, SETTINGS_TABWIDTH_ID, &itype, &ihandle, &irect);
+ GetIText(ihandle, txt);
+ PtoCstr(txt);
+ tabwidth = atoi((char *)txt);
+ HUnlock(ihandle);
+ if (tabwidth != settings.tabwidth) {
+ warn("Restart Amend for new tabwidth setting to take effect");
+ settings.tabwidth = tabwidth;
+ }
+
+ settings_save();
+
+done:
+ DisposeDialog(dlg);
+ UseResFile(cur_res);
+}
--- settings.h Thu Dec 16 17:50:49 2021
+++ settings.h Thu Dec 16 17:50:49 2021
@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) 2021 joshua stein <jcs@jcs.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifndef __SETTINGS_H__
+#define __SETTINGS_H__
+
+struct settings {
+ char author[32];
+ short tabwidth;
+};
+
+extern struct settings settings;
+
+void settings_load(void);
+void settings_save(void);
+void settings_edit(void);
+
+#endif
--- util.c Mon Nov 22 09:48:09 2021
+++ util.c Thu Dec 16 17:04:39 2021
@@ -295,6 +295,36 @@ xGetString(short id)
return h;
}
+char *
+xGetStringAsChar(short id)
+{
+ StringHandle h = xGetString(id);
+ char *out;
+ size_t l;
+
+ HLock(h);
+
+ l = (*h)[0];
+ out = xmalloc(l + 1);
+ memcpy((void *)out, (void *)(*h + 1), l);
+ out[l] = '\0';
+
+ DisposHandle(h);
+ return out;
+}
+
+long
+xGetStringAsLong(short id)
+{
+ char *c;
+ long r;
+
+ c = xGetStringAsChar(id);
+ r = atol(c);
+ free(c);
+ return r;
+}
+
/*
* Filesystem utilities
*/