Skip to content

Commit dbc9995

Browse files
committed
support extended ASCII characters
1 parent d9c1531 commit dbc9995

6 files changed

Lines changed: 43 additions & 43 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## 1.0.0beta2 (April 6, 2017)
4+
5+
* Replace all instances of `isprint()` with `!iscntrl()`, so that extended
6+
ASCII characters can be inserted and displayed in the editor.
7+
* Include font files in offline version of tutorial.
8+
39
## 1.0.0beta1 (April 6, 2017)
410

511
* Add changelog.

doc/02.enteringRawMode.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,13 @@ the character it represents if it is a printable character.
138138

139139
{{keypresses}}
140140

141-
`isprint()` comes from `<ctype.h>`, and `printf()` comes from `<stdio.h>`.
141+
`iscntrl()` comes from `<ctype.h>`, and `printf()` comes from `<stdio.h>`.
142142

143-
`isprint()` tests whether a character is printable. Letters, digits, symbols,
144-
punctuation, and spaces are all printable. Control characters, newlines, tabs,
145-
and the null byte are all considered nonprintable.
143+
`iscntrl()` tests whether a character is a control character. Control
144+
characters are nonprintable characters that we don't want to print to the
145+
screen. ASCII codes 0&ndash;31 are all control characters, and 127 is also a
146+
control character. ASCII codes 32&ndash;126 are all printable. (Check out the
147+
[ASCII table](http://asciitable.com) to see all of the characters.)
146148

147149
`printf()` can print multiple representations of a byte. `%d` tells it to
148150
format the byte as a decimal number (its ASCII code), and `%c` tells it to
@@ -311,8 +313,7 @@ As far as I can tell:
311313
* `INPCK` enables parity checking, which doesn't seem to apply to modern
312314
terminal emulators.
313315
* `ISTRIP` causes the 8th bit of each input byte to be stripped, meaning it
314-
will set it to `0`. I don't know of any keys that send bytes with the 8th
315-
bit set anyways.
316+
will set it to `0`. This is probably already turned off.
316317
* `CS8` is not a flag, it is a bit mask with multiple bits, which we set using
317318
the bitwise-OR (`|`) operator unlike all the flags we are turning off. It
318319
sets the character size (CS) to 8 bits per byte. On my system, it's already

doc/07.syntaxHighlighting.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -488,12 +488,10 @@ counterparts, we'll render them using inverted colors (black on white).
488488

489489
{{nonprintables}}
490490

491-
We use `isprint()` to check if the current character is printable. If not, we
492-
translate it into a printable character by adding its value to `'@'` (in ASCII,
493-
the capital letters of the alphabet come after the `@` character), or using the
494-
`'?'` character if it's not in the alphabetic range. Notice that we have to
495-
make sure it's greater than or equal to `0`, because `char` values can be
496-
negative (which they often will be when opening a binary file).
491+
We use `iscntrl()` to check if the current character is a control character. If
492+
so, we translate it into a printable character by adding its value to `'@'` (in
493+
ASCII, the capital letters of the alphabet come after the `@` character), or
494+
using the `'?'` character if it's not in the alphabetic range.
497495

498496
We then use the `<esc>[7m` escape sequence to switch to inverted colors before
499497
printing the translated symbol. We use `<esc>[m` to turn off inverted colors

doc/08.appendices.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,6 @@ program called [leg](https://github.com/yjerem/leg). I will try to document the
158158
process of editing the `steps.diff` file using `leg` in that repo's `README.md`
159159
file, soon.
160160

161-
Changes to the code will cause problems for everyone who is currently part of
162-
the way through the tutorial, so these changes should probably be kept in a
163-
separate branch until a new, `1.0.0` version of the tutorial is released or
164-
something. Changes to the code should be bugfixes only, not new features.
165-
166161
To use `leg` to generate the final static HTML files:
167162

168163
1. You need to have Ruby installed.

leg.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
:name: kilo
3-
:version: "1.0.0beta1"
3+
:version: "1.0.0beta2"
44
:title: Build Your Own Text Editor
55
:rouge_theme: github
66
:bold_weight: 500

steps.diff

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,10 @@ diff --git a/kilo.c b/kilo.c
125125
char c;
126126
- while (read(STDIN_FILENO, &c, 1) == 1 && c != 'q');
127127
+ while (read(STDIN_FILENO, &c, 1) == 1 && c != 'q') {
128-
+ if (isprint(c)) {
129-
+ printf("%d ('%c')\n", c, c);
130-
+ } else {
128+
+ if (iscntrl(c)) {
131129
+ printf("%d\n", c);
130+
+ } else {
131+
+ printf("%d ('%c')\n", c, c);
132132
+ }
133133
+ }
134134
+
@@ -210,12 +210,12 @@ diff --git a/kilo.c b/kilo.c
210210
@@ -28,9 +28,9 @@ int main() {
211211
char c;
212212
while (read(STDIN_FILENO, &c, 1) == 1 && c != 'q') {
213-
if (isprint(c)) {
214-
- printf("%d ('%c')\n", c, c);
215-
+ printf("%d ('%c')\r\n", c, c);
216-
} else {
213+
if (iscntrl(c)) {
217214
- printf("%d\n", c);
218215
+ printf("%d\r\n", c);
216+
} else {
217+
- printf("%d ('%c')\n", c, c);
218+
+ printf("%d ('%c')\r\n", c, c);
219219
}
220220
}
221221

@@ -258,10 +258,10 @@ diff --git a/kilo.c b/kilo.c
258258
+ while (1) {
259259
+ char c = '\0';
260260
+ read(STDIN_FILENO, &c, 1);
261-
if (isprint(c)) {
262-
printf("%d ('%c')\r\n", c, c);
263-
} else {
261+
if (iscntrl(c)) {
264262
printf("%d\r\n", c);
263+
} else {
264+
printf("%d ('%c')\r\n", c, c);
265265
}
266266
+ if (c == 'q') break;
267267
}
@@ -325,8 +325,8 @@ diff --git a/kilo.c b/kilo.c
325325
char c = '\0';
326326
- read(STDIN_FILENO, &c, 1);
327327
+ if (read(STDIN_FILENO, &c, 1) == -1 && errno != EAGAIN) die("read");
328-
if (isprint(c)) {
329-
printf("%d ('%c')\r\n", c, c);
328+
if (iscntrl(c)) {
329+
printf("%d\r\n", c);
330330
} else {
331331

332332
~~~ step: sections c1
@@ -381,10 +381,10 @@ diff --git a/kilo.c b/kilo.c
381381
while (1) {
382382
char c = '\0';
383383
if (read(STDIN_FILENO, &c, 1) == -1 && errno != EAGAIN) die("read");
384-
- if (isprint(c)) {
385-
- printf("%d ('%c')\r\n", c, c);
386-
- } else {
384+
- if (iscntrl(c)) {
387385
- printf("%d\r\n", c);
386+
- } else {
387+
- printf("%d ('%c')\r\n", c, c);
388388
- }
389389
- if (c == 'q') break;
390390
+ if (c == CTRL_KEY('q')) break;
@@ -665,10 +665,10 @@ diff --git a/kilo.c b/kilo.c
665665
+ printf("\r\n");
666666
+ char c;
667667
+ while (read(STDIN_FILENO, &c, 1) == 1) {
668-
+ if (isprint(c)) {
669-
+ printf("%d ('%c')\r\n", c, c);
670-
+ } else {
668+
+ if (iscntrl(c)) {
671669
+ printf("%d\r\n", c);
670+
+ } else {
671+
+ printf("%d ('%c')\r\n", c, c);
672672
+ }
673673
+ }
674674
+
@@ -705,10 +705,10 @@ diff --git a/kilo.c b/kilo.c
705705
- printf("\r\n");
706706
- char c;
707707
- while (read(STDIN_FILENO, &c, 1) == 1) {
708-
- if (isprint(c)) {
709-
- printf("%d ('%c')\r\n", c, c);
710-
- } else {
708+
- if (iscntrl(c)) {
711709
- printf("%d\r\n", c);
710+
- } else {
711+
- printf("%d ('%c')\r\n", c, c);
712712
- }
713713
+ while (i < sizeof(buf) - 1) {
714714
+ if (read(STDIN_FILENO, &buf[i], 1) != 1) break;
@@ -3106,7 +3106,7 @@ diff --git a/kilo.c b/kilo.c
31063106
+ editorSetStatusMessage("");
31073107
+ return buf;
31083108
+ }
3109-
+ } else if (isprint(c)) {
3109+
+ } else if (!iscntrl(c)) {
31103110
+ if (buflen == bufsize - 1) {
31113111
+ bufsize *= 2;
31123112
+ buf = realloc(buf, bufsize);
@@ -3330,7 +3330,7 @@ diff --git a/kilo.c b/kilo.c
33303330
+ if (callback) callback(buf, c);
33313331
return buf;
33323332
}
3333-
} else if (isprint(c)) {
3333+
} else if (!iscntrl(c)) {
33343334
@@ -594,6 +596,8 @@ char *editorPrompt(char *prompt) {
33353335
buf[buflen++] = c;
33363336
buf[buflen] = '\0';
@@ -4301,8 +4301,8 @@ diff --git a/kilo.c b/kilo.c
43014301
int j;
43024302
for (j = 0; j < len; j++) {
43034303
- if (hl[j] == HL_NORMAL) {
4304-
+ if (!isprint(c[j])) {
4305-
+ char sym = (c[j] >= 0 && c[j] <= 26) ? '@' + c[j] : '?';
4304+
+ if (iscntrl(c[j])) {
4305+
+ char sym = (c[j] <= 26) ? '@' + c[j] : '?';
43064306
+ abAppend(ab, "\x1b[7m", 4);
43074307
+ abAppend(ab, &sym, 1);
43084308
+ abAppend(ab, "\x1b[m", 3);

0 commit comments

Comments
 (0)