Tickets #4872, #4873 and #4874: fix 'insert control code' function#5054
Tickets #4872, #4873 and #4874: fix 'insert control code' function#5054peripherium wants to merge 1 commit intoMidnightCommander:masterfrom
Conversation
|
Since you are fixing |
|
@egmontkob adding you, since this PR fixes 3 issues that you have reported... |
94db99e to
fc84480
Compare
|
I initially didn't add a test for this function because the previous implementation didn't have one, so I assumed that adding a test was not required. In the meantime, I have taken a closer look at the test suite and added the requested test here. If this test is not sufficient or needs adjustments, please let me know. |
fc84480 to
74024a5
Compare
egmontkob
left a comment
There was a problem hiding this comment.
The commit message is duplicated.
lib/util.c
Outdated
| if ((ch >= ASCII_A && ch <= ASCII_Z) || (ch >= ASCII_a && ch <= ASCII_z)) | ||
| ch &= 0x1f; | ||
| // Only KEY_M_CTRL and lowest 7 bits of ch may be set | ||
| if ((ch & KEY_M_CTRL) != 0 && (ch & ~(KEY_M_CTRL | 0x7f)) != 0) |
There was a problem hiding this comment.
The condition doesn't match the comment above.
| return ch - '`'; | ||
|
|
||
| return ch; | ||
| return -1; |
There was a problem hiding this comment.
Shouldn't you also handle the common practice of Ctrl+? as 0x7F ?
lib/util.h
Outdated
| @@ -264,7 +264,7 @@ void save_file_position (const vfs_path_t *filename_vpath, long line, long colum | |||
|
|
|||
| /* if ch is in [A-Za-z], returns the corresponding control character, | |||
There was a problem hiding this comment.
You should update this comment with the additional characters you handle now; also the modified "else" return value.
lib/util.c
Outdated
| // Only KEY_M_CTRL and lowest 7 bits of ch may be set | ||
| if ((ch & KEY_M_CTRL) != 0 && (ch & ~(KEY_M_CTRL | 0x7f)) != 0) | ||
| return -1; | ||
|
|
There was a problem hiding this comment.
Trailing spaces here, please run make indent.
lib/util.c
Outdated
| if (ch >= '@' && ch <= '_') | ||
| return ch - '@'; | ||
|
|
||
| if (ch >= '`' && ch <= '~') |
There was a problem hiding this comment.
Do we need to extend beyond the lowercase ASCII chars?
Ctrl+backtick, Ctrl+{ etc. are absolutely unconventional ways to denote the control chars, I don't think we should handle them.
egmontkob
left a comment
There was a problem hiding this comment.
Tested now, and it indeed fixes those three bugs, thanks!
74024a5 to
d97eeee
Compare
|
I included the lowercase characters because I thought it would be user-friendly: even if shift wasn't pressed during input, the corresponding control code can be entered via the dialog. However, this is a bit inconsistent with the last inserted condition, according to which |
|
Lowercase letters are absolutely fine, in fact, expected; the user probably won't bother pressing Shift. What's unnecessary IMHO is support the characters "surrounding" the lowercase letters in the ASCII table: |
Validate input key codes, ignore special keys returned by get_key_code(). Fix handling of KEY_M_CTRL combinations and extend mapping for '@', '[', '\', ']', '^', '_'. Fixes: MidnightCommander#4872 Fixes: MidnightCommander#4873 Fixes: MidnightCommander#4874 Signed-off-by: Manuel Einfalt <einfalt1@proton.me> lib/util.c (keycode_to_cntrl): Rename from ascii_alpha_to_cntrl. Validate input key codes, ignore special keys returned by get_key_code(). Fix handling of KEY_M_CTRL combinations and extend mapping for '@', '[', '\', ']', '^', '_'. Fixes: MidnightCommander#4872 Fixes: MidnightCommander#4873 Fixes: MidnightCommander#4874 Signed-off-by: Manuel Einfalt <einfalt1@proton.me>
d97eeee to
0a7f477
Compare
|
Originally I allowed characters from However, if this is not intuitive and could instead confuse future readers, then it doesn't belong in the code. So I removed it and restricted the range to |
The previous implementation used
ascii_alpha_to_cntrl(), which assumedalphabetic input and did not properly validate key codes returned by the
terminal libraries. The function has been renamed to
keycode_to_cntrl()and extended to correctly validate and convert key codes.
Changes
correctly handle
KEY_M_CTRLcombinations (e.g.Ctrl+J).Fixes mcedit: Display corruption (missing header) after inserting literal
^J#4872.ignore special keys returned by
get_key_code()(e.g. function or cursor keys).Fixes mcedit: Insert literal, followed by special key acts weirdly #4873.
extend control mappings for
@ [ \ ] ^ _.Fixes mcedit: Insert literal should accept plain non-letters #4874.
return
-1for invalid input and ignore it at the call sitesThe function is used both in the Insert literal dialog in
mceditand in the
mccommand line.Checklist
git commit --amend -smake indent && make check)