1 |
|
|
/* $OpenBSD: modes.c,v 1.21 2017/05/30 07:05:22 florian Exp $ */ |
2 |
|
|
|
3 |
|
|
/* This file is in the public domain. */ |
4 |
|
|
|
5 |
|
|
/* |
6 |
|
|
* Commands to toggle modes. Without an argument, these functions will |
7 |
|
|
* toggle the given mode. A negative or zero argument will turn the mode |
8 |
|
|
* off. A positive argument will turn the mode on. |
9 |
|
|
*/ |
10 |
|
|
|
11 |
|
|
#include <sys/queue.h> |
12 |
|
|
#include <signal.h> |
13 |
|
|
#include <stdio.h> |
14 |
|
|
#include <string.h> |
15 |
|
|
|
16 |
|
|
#include "def.h" |
17 |
|
|
#include "kbd.h" |
18 |
|
|
|
19 |
|
|
int changemode(int, int, char *); |
20 |
|
|
|
21 |
|
|
int defb_nmodes = 0; |
22 |
|
|
struct maps_s *defb_modes[PBMODES] = { &fundamental_mode }; |
23 |
|
|
int defb_flag = 0; |
24 |
|
|
|
25 |
|
|
int |
26 |
|
|
changemode(int f, int n, char *newmode) |
27 |
|
|
{ |
28 |
|
|
int i; |
29 |
|
|
struct maps_s *m; |
30 |
|
|
|
31 |
|
|
if ((m = name_mode(newmode)) == NULL) { |
32 |
|
|
dobeep(); |
33 |
|
|
ewprintf("Can't find mode %s", newmode); |
34 |
|
|
return (FALSE); |
35 |
|
|
} |
36 |
|
|
if (!(f & FFARG)) { |
37 |
|
|
for (i = 0; i <= curbp->b_nmodes; i++) |
38 |
|
|
if (curbp->b_modes[i] == m) { |
39 |
|
|
/* mode already set */ |
40 |
|
|
n = 0; |
41 |
|
|
break; |
42 |
|
|
} |
43 |
|
|
} |
44 |
|
|
if (n > 0) { |
45 |
|
|
for (i = 0; i <= curbp->b_nmodes; i++) |
46 |
|
|
if (curbp->b_modes[i] == m) |
47 |
|
|
/* mode already set */ |
48 |
|
|
return (TRUE); |
49 |
|
|
if (curbp->b_nmodes >= PBMODES - 1) { |
50 |
|
|
dobeep(); |
51 |
|
|
ewprintf("Too many modes"); |
52 |
|
|
return (FALSE); |
53 |
|
|
} |
54 |
|
|
curbp->b_modes[++(curbp->b_nmodes)] = m; |
55 |
|
|
} else { |
56 |
|
|
/* fundamental is b_modes[0] and can't be unset */ |
57 |
|
|
for (i = 1; i <= curbp->b_nmodes && m != curbp->b_modes[i]; |
58 |
|
|
i++); |
59 |
|
|
if (i > curbp->b_nmodes) |
60 |
|
|
return (TRUE); /* mode wasn't set */ |
61 |
|
|
for (; i < curbp->b_nmodes; i++) |
62 |
|
|
curbp->b_modes[i] = curbp->b_modes[i + 1]; |
63 |
|
|
curbp->b_nmodes--; |
64 |
|
|
} |
65 |
|
|
upmodes(curbp); |
66 |
|
|
return (TRUE); |
67 |
|
|
} |
68 |
|
|
|
69 |
|
|
int |
70 |
|
|
indentmode(int f, int n) |
71 |
|
|
{ |
72 |
|
|
return (changemode(f, n, "indent")); |
73 |
|
|
} |
74 |
|
|
|
75 |
|
|
int |
76 |
|
|
fillmode(int f, int n) |
77 |
|
|
{ |
78 |
|
|
return (changemode(f, n, "fill")); |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
#ifdef NOTAB |
82 |
|
|
int |
83 |
|
|
notabmode(int f, int n) |
84 |
|
|
{ |
85 |
|
|
if (changemode(f, n, "notab") == FALSE) |
86 |
|
|
return (FALSE); |
87 |
|
|
if (f & FFARG) { |
88 |
|
|
if (n <= 0) |
89 |
|
|
curbp->b_flag &= ~BFNOTAB; |
90 |
|
|
else |
91 |
|
|
curbp->b_flag |= BFNOTAB; |
92 |
|
|
} else |
93 |
|
|
curbp->b_flag ^= BFNOTAB; |
94 |
|
|
return (TRUE); |
95 |
|
|
} |
96 |
|
|
#endif /* NOTAB */ |
97 |
|
|
|
98 |
|
|
int |
99 |
|
|
overwrite_mode(int f, int n) |
100 |
|
|
{ |
101 |
|
|
if (changemode(f, n, "overwrite") == FALSE) |
102 |
|
|
return (FALSE); |
103 |
|
|
if (f & FFARG) { |
104 |
|
|
if (n <= 0) |
105 |
|
|
curbp->b_flag &= ~BFOVERWRITE; |
106 |
|
|
else |
107 |
|
|
curbp->b_flag |= BFOVERWRITE; |
108 |
|
|
} else |
109 |
|
|
curbp->b_flag ^= BFOVERWRITE; |
110 |
|
|
return (TRUE); |
111 |
|
|
} |
112 |
|
|
|
113 |
|
|
int |
114 |
|
|
set_default_mode(int f, int n) |
115 |
|
|
{ |
116 |
|
|
int i; |
117 |
|
|
struct maps_s *m; |
118 |
|
|
char modebuf[32], *bufp; |
119 |
|
|
|
120 |
|
|
if ((bufp = eread("Set Default Mode: ", modebuf, sizeof(modebuf), |
121 |
|
|
EFNEW)) == NULL) |
122 |
|
|
return (ABORT); |
123 |
|
|
else if (bufp[0] == '\0') |
124 |
|
|
return (FALSE); |
125 |
|
|
if ((m = name_mode(modebuf)) == NULL) { |
126 |
|
|
dobeep(); |
127 |
|
|
ewprintf("can't find mode %s", modebuf); |
128 |
|
|
return (FALSE); |
129 |
|
|
} |
130 |
|
|
if (!(f & FFARG)) { |
131 |
|
|
for (i = 0; i <= defb_nmodes; i++) |
132 |
|
|
if (defb_modes[i] == m) { |
133 |
|
|
/* mode already set */ |
134 |
|
|
n = 0; |
135 |
|
|
break; |
136 |
|
|
} |
137 |
|
|
} |
138 |
|
|
if (n > 0) { |
139 |
|
|
for (i = 0; i <= defb_nmodes; i++) |
140 |
|
|
if (defb_modes[i] == m) |
141 |
|
|
/* mode already set */ |
142 |
|
|
return (TRUE); |
143 |
|
|
if (defb_nmodes >= PBMODES - 1) { |
144 |
|
|
dobeep(); |
145 |
|
|
ewprintf("Too many modes"); |
146 |
|
|
return (FALSE); |
147 |
|
|
} |
148 |
|
|
defb_modes[++defb_nmodes] = m; |
149 |
|
|
} else { |
150 |
|
|
/* fundamental is defb_modes[0] and can't be unset */ |
151 |
|
|
for (i = 1; i <= defb_nmodes && m != defb_modes[i]; i++); |
152 |
|
|
if (i > defb_nmodes) |
153 |
|
|
/* mode was not set */ |
154 |
|
|
return (TRUE); |
155 |
|
|
for (; i < defb_nmodes; i++) |
156 |
|
|
defb_modes[i] = defb_modes[i + 1]; |
157 |
|
|
defb_nmodes--; |
158 |
|
|
} |
159 |
|
|
if (strcmp(modebuf, "overwrite") == 0) { |
160 |
|
|
if (n <= 0) |
161 |
|
|
defb_flag &= ~BFOVERWRITE; |
162 |
|
|
else |
163 |
|
|
defb_flag |= BFOVERWRITE; |
164 |
|
|
} |
165 |
|
|
#ifdef NOTAB |
166 |
|
|
if (strcmp(modebuf, "notab") == 0) { |
167 |
|
|
if (n <= 0) |
168 |
|
|
defb_flag &= ~BFNOTAB; |
169 |
|
|
else |
170 |
|
|
defb_flag |= BFNOTAB; |
171 |
|
|
} |
172 |
|
|
#endif /* NOTAB */ |
173 |
|
|
return (TRUE); |
174 |
|
|
} |