9fans/plan9port

acme backspace/ctrl-h delete the selection and one more char before selection if selection exists

Opened this issue · 8 comments

Not sure whether it is by design.

If a row had text selected, backspace/ctrl-h will delete the selected text and one more prefix char.

Step to reproduce:

1, input '12345678'
2, select '5678'
3, press backspace or ctrl-h, it will delete '5678' and also '4'

I checked the codes in acme/text.c:

 820         if(t->q1 > t->q0){
 821                 if(t->ncache != 0)
 822                         error("text.type");
 823                 cut(t, t, nil, TRUE, TRUE, nil, 0);
 824                 t->eq0 = ~0;
 825         }
 826         textshow(t, t->q0, t->q0, 1);

It will CUT but NOT DELETE existing selection before ^F/ESC/^H/^U/^W/CR pressed, then introduce some weird results,
for example, after selection cut, one more prefix char will be erased by ^H/Backspace.

If I understand it right, according to some tutorials/manuals, the correct result should be?

  • ^F: if selection exists, just ignore the selection instead of cutting it, and put the text cursor to q1? or the end of the word?

  • ESC: select the most recently typed text. if selection already exists, DELETE it but NOT CUT it. So pressing ESC first time will select the last typed text, pressing it again will DELETE the selection. it can be undo by Undo and can not Paste?

  • ^H/Backspace: delete one backward char or current selection, if selection exists, should delete(but not cut) the selection only, and do not delete one more backward char before the selection.

  • ^W/^U: delete one backward word/to the start of line. if selection exists, selection should be ignored and put cursor to q1(or q0, but hard to determine the direction of selection), as most editors does?

  • CR: if selection exists, DELETE it and insert a line break.

I never looked into the reason for this behavior, but the way I see it, generally if you have a selection and you input a character, say X, the selection gets replaced by X. If X is a backspace (a control character, yet still a character) that results in the behavior you observe. Took me a few minutes to get used to but seems more reasonable than what other programs commonly do.

(I'm actually annoyed in this text box for instance, to select a word and have to hit backspace twice to delete the preceding whitespace too.)

macOS text handling removes the character preceding a selection on backspace, but only if it's a space character. Maybe that would be a good approach for Acme to take?

jxy commented

what's wrong with pressing ESC for this?

what's wrong with pressing ESC for this?

@jxy

If no selection existing, ESC will delete the last typed text directly. It can be undo with "Undo".

If selection existing, ESC will cut the selected text. It can be undo with "Undo" and paste with "Paste".

It seems to be convenient sometimes, but I not sure whether the behaviour match the definition of ESC key or not.

The definition and expected behavior of ESC seems to be "delete the last typed text"?

yn- commented

@cjacker If I am understanding your description correctly, that behaviour is supposed to be correct in the plan9 culture. As far as I understand, the rationale is as follows.

As you would expect, the idea of input upon selection is to substitute the selected text with the input. In many editors, this substitution is handled with an exception---the backspace. When a backspace is the input character, the selected text is replaced by an empty string, instead of the backspace character itself. In plan9, there is no such exception. The selected text is replaced by a backspace, resulted in removal of one previous character. The plan9 behaviour might seem unusual (maybe also inconvenient), but it is supposed to be more consistent.

I haven't actually asked 9fans about that behaviour. Hopefully they will spot it and correct it in case my explanation were wrong.

sam exhibits the same behavior.

I put this question to the 9fans mailing list. The behavior is by design. In the Plan 9 world, when you select something and press delete, it replaces the text with what was typed. So your selection is replaced by "backspace" which deletes the remaining character. This explains it further:

https://9fans.topicbox.com/groups/9fans/Tf778565df277dd77-M396229d3fd3befcd0bcc95df

@cjacker, please close this issue as acme is working according to design.