Skip to content

Tickets #4872, #4873 and #4874: fix 'insert control code' function#5054

Open
peripherium wants to merge 1 commit intoMidnightCommander:masterfrom
peripherium:fix/control-sequence-from-keyboard
Open

Tickets #4872, #4873 and #4874: fix 'insert control code' function#5054
peripherium wants to merge 1 commit intoMidnightCommander:masterfrom
peripherium:fix/control-sequence-from-keyboard

Conversation

@peripherium
Copy link

@peripherium peripherium commented Mar 8, 2026

The previous implementation used ascii_alpha_to_cntrl(), which assumed
alphabetic 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

The function is used both in the Insert literal dialog in mcedit
and in the mc command line.

Checklist

  • I have referenced the issue(s) resolved by this PR (if any)
  • I have signed-off my contribution with git commit --amend -s
  • Lint and unit tests pass locally with my changes (make indent && make check)
  • I have added tests that prove my fix is effective or that my feature works
  • I have added the necessary documentation (if appropriate)

@github-actions github-actions bot added needs triage Needs triage by maintainers prio: medium Has the potential to affect progress labels Mar 8, 2026
@github-actions github-actions bot added this to the Future Releases milestone Mar 8, 2026
@zyv
Copy link
Member

zyv commented Mar 8, 2026

Since you are fixing keycode_to_cntrl, could you please add a test for it? Thank you!

@zyv zyv requested a review from egmontkob March 8, 2026 14:22
@zyv
Copy link
Member

zyv commented Mar 8, 2026

@egmontkob adding you, since this PR fixes 3 issues that you have reported...

@peripherium peripherium force-pushed the fix/control-sequence-from-keyboard branch from 94db99e to fc84480 Compare March 9, 2026 10:35
@peripherium
Copy link
Author

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.

Copy link
Contributor

@mc-worker mc-worker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zyv zyv added area: mcedit mcedit, the built-in text editor and removed needs triage Needs triage by maintainers labels Mar 9, 2026
@zyv zyv modified the milestones: Future Releases, 4.9.0 Mar 9, 2026
@peripherium peripherium force-pushed the fix/control-sequence-from-keyboard branch from fc84480 to 74024a5 Compare March 10, 2026 06:31
Copy link
Contributor

@egmontkob egmontkob left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition doesn't match the comment above.

return ch - '`';

return ch;
return -1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing spaces here, please run make indent.

lib/util.c Outdated
if (ch >= '@' && ch <= '_')
return ch - '@';

if (ch >= '`' && ch <= '~')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor

@egmontkob egmontkob left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested now, and it indeed fixes those three bugs, thanks!

@peripherium peripherium force-pushed the fix/control-sequence-from-keyboard branch from 74024a5 to d97eeee Compare March 11, 2026 08:46
@peripherium
Copy link
Author

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 ch == '?' returns 0x7f. Should I remove the conditions with the lowercase characters from the code?

@egmontkob
Copy link
Contributor

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: ` { | } ~. Here it's absolutely reasonable that the user will instead press @ [ \ ] ^ (or _ which didn't have a counterpart).

@zyv zyv changed the title Ticket #4872, #4873 and #4874: Fix 'insert control code' function. Tickets #4872, #4873 and #4874: fix 'insert control code' function Mar 11, 2026
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>
@peripherium peripherium force-pushed the fix/control-sequence-from-keyboard branch from d97eeee to 0a7f477 Compare March 12, 2026 08:23
@peripherium
Copy link
Author

Originally I allowed characters from ´ to ~ because I assumed someone looking at the ASCII table might wonder why those printable characters were excluded.

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 a-z.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: mcedit mcedit, the built-in text editor prio: medium Has the potential to affect progress

4 participants