Menu

[f284f8]: / joe / state.c  Maximize  Restore  History

Download this file

162 lines (139 with data), 3.2 kB

  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
/*
* JOE state file
* Copyright
* (C) 1992 Joseph H. Allen
*
* This file is part of JOE (Joe's Own Editor)
*/
#include "types.h"
/* Set to enable use of ~/.joe_state file */
int joe_state;
/* Save a history buffer */
void save_hist(FILE *f,B *b)
{
char *buf = vsmk(128);
if (b) {
P *p = pdup(b->bof, "save_hist");
if (b->eof->line>10)
pline(p,b->eof->line-10);
while (!piseof(p)) {
buf = brzs(buf, p);
buf = vsadd(buf, '\n');
pnextl(p);
fprintf(f,"\t");
emit_string(f,sv(buf));
fprintf(f,"\n");
}
prm(p);
}
fprintf(f,"done\n");
}
/* Load a history buffer */
void load_hist(FILE *f,B **bp)
{
B *b;
char *buf = 0;
char *bf = 0;
P *q;
b = *bp;
if (!b)
*bp = b = bmk(NULL);
q = pdup(b->eof, "load_hist");
while(vsgets(&buf,f) && zcmp(buf, "done")) {
const char *p = buf;
ptrdiff_t len;
parse_ws(&p,'#');
len = parse_string(&p,&bf);
if (len>0) {
if (bf[len - 1] != '\n')
bf[len - 1] = '\n';
binsm(q,bf,len);
pset(q,b->eof);
}
}
prm(q);
}
/* Save state */
#define STATE_ID "# JOE state file v1.0\n"
void save_state()
{
const char *home = getenv("HOME");
char *path = NULL;
mode_t old_mask;
FILE *f;
if (!joe_state)
return;
if (!home)
return;
path = vsfmt(NULL,0,"%s/.joe_state",path);
old_mask = umask(0066);
f = fopen(path, "w");
umask(old_mask);
if(!f)
return;
/* Write ID */
fprintf(f, "%s\n", STATE_ID);
/* Write state information */
fprintf(f,"search\n"); save_srch(f);
fprintf(f,"macros\n"); save_macros(f);
fprintf(f,"files\n"); save_hist(f,filehist);
fprintf(f,"find\n"); save_hist(f,findhist);
fprintf(f,"replace\n"); save_hist(f,replhist);
fprintf(f,"run\n"); save_hist(f,runhist);
fprintf(f,"build\n"); save_hist(f,buildhist);
fprintf(f,"grep\n"); save_hist(f,grephist);
fprintf(f,"cmd\n"); save_hist(f,cmdhist);
fprintf(f,"math\n"); save_hist(f,mathhist);
fprintf(f,"yank\n"); save_yank(f);
fprintf(f,"file_pos\n"); save_file_pos(f);
fclose(f);
}
/* Load state */
void load_state()
{
char *path = (char *)getenv("HOME");
char *buf = vsmk(128);
FILE *f;
if (!joe_state)
return;
if (!path)
return;
path = vsfmt(NULL, 0, "%s/.joe_state", path);
f = fopen(path, "r");
if(!f)
return;
/* Only read state information if the version is correct */
if (vsgets(&buf, f) && !zcmp(buf,STATE_ID)) {
/* Read state information */
while(vsgets(&buf,f)) {
if(!zcmp(buf,"search"))
load_srch(f);
else if(!zcmp(buf, "macros"))
load_macros(f);
else if(!zcmp(buf, "files"))
load_hist(f,&filehist);
else if(!zcmp(buf, "find"))
load_hist(f,&findhist);
else if(!zcmp(buf, "replace"))
load_hist(f,&replhist);
else if(!zcmp(buf, "run"))
load_hist(f,&runhist);
else if(!zcmp(buf, "build"))
load_hist(f,&buildhist);
else if(!zcmp(buf, "grep"))
load_hist(f,&grephist);
else if(!zcmp(buf, "cmd"))
load_hist(f,&cmdhist);
else if(!zcmp(buf, "math"))
load_hist(f,&mathhist);
else if(!zcmp(buf, "yank"))
load_yank(f);
else if(!zcmp(buf, "file_pos"))
load_file_pos(f);
else { /* Unknown... skip until next done */
while(vsgets(&buf,f) && zcmp(buf,"done"));
}
}
}
fclose(f);
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.