Feature or package to warn me before deleting many words

Hi,
I am working with Atom on my laptop and while typing I sometimes accidentally mark a bunch of text by touching the touchpad with my palms and then replace the marked text with whatever I am currently typing. Normally I realize it and undo the changes and then continue but I don’t want to be scared all the time that I am deleting a significant amount of text without realizing it especially when a lot of work has gone into it.
I have enabled “disable touchpad while typing” (Ubuntu) but that doesn’t prevent this from happening from time to time.
My QUESTION therefore is: Is there some way to get a prompt / warning whenever I am about to delete a certain number (ideally adjustable) of words?
Thanks in advance.

This is a terrible implementation, but it was fun to come up with. I don’t know if a cleaner method is possible. It will remove the cursor if you click the notification button, which is annoying, but maybe solvable.
– It now dismisses automatically when you keep typing or trigger a new one.

The editor has a “will add text” event, but it only passes the text to be added, not anything that will be deleted. It also requires a synchronous return value, so any kind of notification won’t work.

If you actually use this and ever get an error relating to text manipulation, remember to try it without this first and see if the error still occurs (also remember to do it in safe mode if it does). This code is not friendly; it’s replacing a core component with an imitation, and it will probably break things that rely on the insertion happening.

function tooManyWords(selection) {
  return selection.getText().split(" ").length > 2; // Terrible; don't do it this way
}

atom.workspace.observeTextEditors(editor => {
  let notif;
  editor.observeSelections(selection => {
    const oldInsert = selection.insertText.bind(selection);
    selection.insertText = (text, options) => {
      if (notif) {
        notif.dismiss();
        notif = undefined;
      }
      if (text !== "" && tooManyWords(selection)) {
        notif = atom.notifications.addWarning("Really delete?", {
          dismissable: true,
          buttons: [
            {
              text: "yes",
              onDidClick: () => {
                notif.dismiss();
                notif = undefined;
                oldInsert(text, options);
              }
            }
          ]
        });
        return selection.getBufferRange();
      } else {
        return oldInsert(text, options);
      }
    }
  });
});

Thanks, for taking the time.
I’m a bit of a noob. Unfortunately you can’t assume a high degree of knowledge on my side related to Atom. Here we go:

  1. Where / how do I enter this code exactly?
  2. What procedure should I perform only in safe mode: running your code or deleting it again if an error occurs?
  3. You wrote: “and it will probably break things that rely on the insertion happening.” Can you expand a bit what you mean here? Are you saying that my whole editing with Atom will be impaired when using your code?
  4. Is there any danger to trying out your code? I could just delete it again or insert what has originally been there and everything should work as it does now, right?
  1. In the folder ~/.atom, there should be a file called init.coffee or init.js. If it’s init.coffee and you’ve not added anything to it, you can delete it and make init.js instead. The code will then work when you paste it into init.js and reload Atom.

  2. Safe mode launches Atom with community packages and the init script disabled. My point here was that if you encounter a bug, it’s probably caused by this code. To confirm, you should try reproducing the bug with this code disabled (delete / comment it out and reload Atom, or launch Atom in safe mode).

  3. I don’t know what it will affect. I tested it briefly, and nothing threw an error, but it’s possible you will have a package that does not play well with it. E.g., a package that performs text replacements.

  4. Unlikely. Any effects of this code will be reverted when you remove it and reload Atom. It is not permanently changing anything. There is a slight risk that a package expects the operation to happen, even when it didn’t, and that package may do unexpected things because of it.

1 Like

I tried it but it but unfortunately it had no effect: I can delete (many) words without being asked whether I really want to do so. What could be the reason that it worked with you but didn’t in my case?