Skip to content

Commit

Permalink
support extended ASCII characters
Browse files Browse the repository at this point in the history
  • Loading branch information
paigeruten committed Apr 7, 2017
1 parent d9c1531 commit dbc9995
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 43 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 1.0.0beta2 (April 6, 2017)

* Replace all instances of `isprint()` with `!iscntrl()`, so that extended
ASCII characters can be inserted and displayed in the editor.
* Include font files in offline version of tutorial.

## 1.0.0beta1 (April 6, 2017)

* Add changelog.
Expand Down
13 changes: 7 additions & 6 deletions doc/02.enteringRawMode.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,13 @@ the character it represents if it is a printable character.

{{keypresses}}

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

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

`printf()` can print multiple representations of a byte. `%d` tells it to
format the byte as a decimal number (its ASCII code), and `%c` tells it to
Expand Down Expand Up @@ -311,8 +313,7 @@ As far as I can tell:
* `INPCK` enables parity checking, which doesn't seem to apply to modern
terminal emulators.
* `ISTRIP` causes the 8th bit of each input byte to be stripped, meaning it
will set it to `0`. I don't know of any keys that send bytes with the 8th
bit set anyways.
will set it to `0`. This is probably already turned off.
* `CS8` is not a flag, it is a bit mask with multiple bits, which we set using
the bitwise-OR (`|`) operator unlike all the flags we are turning off. It
sets the character size (CS) to 8 bits per byte. On my system, it's already
Expand Down
10 changes: 4 additions & 6 deletions doc/07.syntaxHighlighting.md
Original file line number Diff line number Diff line change
Expand Up @@ -488,12 +488,10 @@ counterparts, we'll render them using inverted colors (black on white).

{{nonprintables}}

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

We then use the `<esc>[7m` escape sequence to switch to inverted colors before
printing the translated symbol. We use `<esc>[m` to turn off inverted colors
Expand Down
5 changes: 0 additions & 5 deletions doc/08.appendices.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,6 @@ program called [leg](https://github.com/yjerem/leg). I will try to document the
process of editing the `steps.diff` file using `leg` in that repo's `README.md`
file, soon.

Changes to the code will cause problems for everyone who is currently part of
the way through the tutorial, so these changes should probably be kept in a
separate branch until a new, `1.0.0` version of the tutorial is released or
something. Changes to the code should be bugfixes only, not new features.

To use `leg` to generate the final static HTML files:

1. You need to have Ruby installed.
Expand Down
2 changes: 1 addition & 1 deletion leg.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
:name: kilo
:version: "1.0.0beta1"
:version: "1.0.0beta2"
:title: Build Your Own Text Editor
:rouge_theme: github
:bold_weight: 500
Expand Down
50 changes: 25 additions & 25 deletions steps.diff
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ diff --git a/kilo.c b/kilo.c
char c;
- while (read(STDIN_FILENO, &c, 1) == 1 && c != 'q');
+ while (read(STDIN_FILENO, &c, 1) == 1 && c != 'q') {
+ if (isprint(c)) {
+ printf("%d ('%c')\n", c, c);
+ } else {
+ if (iscntrl(c)) {
+ printf("%d\n", c);
+ } else {
+ printf("%d ('%c')\n", c, c);
+ }
+ }
+
Expand Down Expand Up @@ -210,12 +210,12 @@ diff --git a/kilo.c b/kilo.c
@@ -28,9 +28,9 @@ int main() {
char c;
while (read(STDIN_FILENO, &c, 1) == 1 && c != 'q') {
if (isprint(c)) {
- printf("%d ('%c')\n", c, c);
+ printf("%d ('%c')\r\n", c, c);
} else {
if (iscntrl(c)) {
- printf("%d\n", c);
+ printf("%d\r\n", c);
} else {
- printf("%d ('%c')\n", c, c);
+ printf("%d ('%c')\r\n", c, c);
}
}

Expand Down Expand Up @@ -258,10 +258,10 @@ diff --git a/kilo.c b/kilo.c
+ while (1) {
+ char c = '\0';
+ read(STDIN_FILENO, &c, 1);
if (isprint(c)) {
printf("%d ('%c')\r\n", c, c);
} else {
if (iscntrl(c)) {
printf("%d\r\n", c);
} else {
printf("%d ('%c')\r\n", c, c);
}
+ if (c == 'q') break;
}
Expand Down Expand Up @@ -325,8 +325,8 @@ diff --git a/kilo.c b/kilo.c
char c = '\0';
- read(STDIN_FILENO, &c, 1);
+ if (read(STDIN_FILENO, &c, 1) == -1 && errno != EAGAIN) die("read");
if (isprint(c)) {
printf("%d ('%c')\r\n", c, c);
if (iscntrl(c)) {
printf("%d\r\n", c);
} else {

~~~ step: sections c1
Expand Down Expand Up @@ -381,10 +381,10 @@ diff --git a/kilo.c b/kilo.c
while (1) {
char c = '\0';
if (read(STDIN_FILENO, &c, 1) == -1 && errno != EAGAIN) die("read");
- if (isprint(c)) {
- printf("%d ('%c')\r\n", c, c);
- } else {
- if (iscntrl(c)) {
- printf("%d\r\n", c);
- } else {
- printf("%d ('%c')\r\n", c, c);
- }
- if (c == 'q') break;
+ if (c == CTRL_KEY('q')) break;
Expand Down Expand Up @@ -665,10 +665,10 @@ diff --git a/kilo.c b/kilo.c
+ printf("\r\n");
+ char c;
+ while (read(STDIN_FILENO, &c, 1) == 1) {
+ if (isprint(c)) {
+ printf("%d ('%c')\r\n", c, c);
+ } else {
+ if (iscntrl(c)) {
+ printf("%d\r\n", c);
+ } else {
+ printf("%d ('%c')\r\n", c, c);
+ }
+ }
+
Expand Down Expand Up @@ -705,10 +705,10 @@ diff --git a/kilo.c b/kilo.c
- printf("\r\n");
- char c;
- while (read(STDIN_FILENO, &c, 1) == 1) {
- if (isprint(c)) {
- printf("%d ('%c')\r\n", c, c);
- } else {
- if (iscntrl(c)) {
- printf("%d\r\n", c);
- } else {
- printf("%d ('%c')\r\n", c, c);
- }
+ while (i < sizeof(buf) - 1) {
+ if (read(STDIN_FILENO, &buf[i], 1) != 1) break;
Expand Down Expand Up @@ -3106,7 +3106,7 @@ diff --git a/kilo.c b/kilo.c
+ editorSetStatusMessage("");
+ return buf;
+ }
+ } else if (isprint(c)) {
+ } else if (!iscntrl(c)) {
+ if (buflen == bufsize - 1) {
+ bufsize *= 2;
+ buf = realloc(buf, bufsize);
Expand Down Expand Up @@ -3330,7 +3330,7 @@ diff --git a/kilo.c b/kilo.c
+ if (callback) callback(buf, c);
return buf;
}
} else if (isprint(c)) {
} else if (!iscntrl(c)) {
@@ -594,6 +596,8 @@ char *editorPrompt(char *prompt) {
buf[buflen++] = c;
buf[buflen] = '\0';
Expand Down Expand Up @@ -4301,8 +4301,8 @@ diff --git a/kilo.c b/kilo.c
int j;
for (j = 0; j < len; j++) {
- if (hl[j] == HL_NORMAL) {
+ if (!isprint(c[j])) {
+ char sym = (c[j] >= 0 && c[j] <= 26) ? '@' + c[j] : '?';
+ if (iscntrl(c[j])) {
+ char sym = (c[j] <= 26) ? '@' + c[j] : '?';
+ abAppend(ab, "\x1b[7m", 4);
+ abAppend(ab, &sym, 1);
+ abAppend(ab, "\x1b[m", 3);
Expand Down

0 comments on commit dbc9995

Please sign in to comment.