
Command creation:
asprintf(&awk, "echo \"%s\" | %s -v line=\"%d\" -v column=\"%d\" -v point=\"%ld\" -f %s",
        region, AWK_BIN, curbp->b_line, curwp->w_col, curbp->b_point, seleted_script);


Problems:
  1. Returning to point
    - there needs to be a calculation that is figured automatically
      _in ait_ that determines how to move the point. If, for example,
      you removed text before the point, the point is subtracted by
      the number of printable chars[fn:1]. If text is added before the
      point, then the number of printable chars is added to the point.
    - probably the easiest solution for this would be to just do a
      simple diff between 0 -> curbp->b_point, adding up the
      deletions or additions:

      int adjust_point(const char *old, const char *new, int point) {
        int old_len = strlen(old);
        int new_len = strlen(new);

        // Common prefix
        int pre = 0;
        while (pre < old_len && pre < new_len && old[pre] == new[pre])
            pre++;

        // Common suffix (don't overlap with prefix)
        int suf = 0;
        while (suf < old_len - pre && suf < new_len - pre &&
               old[old_len - 1 - suf] == new[new_len - 1 - suf])
            suf++;

        int edit_start = pre;
        int edit_old_end = old_len - suf;
        int edit_new_end = new_len - suf;

        if (point <= edit_start)
            return point;                         // Before edit: unchanged

        if (point >= edit_old_end)
            return point + (new_len - old_len);   // After edit: shift by delta

        // Inside edit: clamp to end of new edit region
        return edit_new_end;
    }

Example:
Let's do a checkbox checking function like Emacs' org-mode's C-c C-c.
To do this, let's assume we have V2 of the awk scripting feature
completed. Somehow you'd setup the keybinding of C-c C-c (either
globally or per syntax mode) to run your checkbox.awk script. The
script would maybe look like this:
=== checkbox.awk ===
NR == line {
  if ($0 ~ "[x]") {
    sub(/\[x\]/, "[ ]");
  } else {
    sub(/\[ \]/, "[x]");
  }
  print
  next
}

{
  print
}
=== END ===
This would iterate over the entire file, make the single change to
update the checkbox to checked or not. Note that this script assumes
that you're on the same line as the checkbox hence the `NR == line`.
As a reminder, `line` is always defined by ait in these scripts. What
if we wanted to be _anywhere_ in a multi-line checkbox item you'd
have to do some awk fanciness to backtrace to the checkbox. While
this is possible, it might be a little much:
=== new_checkbox.awk ===
{ lines[NR] = $0 }
END {
  found_line = 0
  for (i = NR; i >= 1; i--) {
    if(NR == line || found_line) {
      found_line = 1
      if(lines[i] ~ "[x]") {
        sub(/\[x\]/, "[ ]", line[i]);
        found_line = 0;
        break;
      } else if(lines[i] ~ "[ ]") {
        sub(/\[ \]/, "[x]", line[i]);
        found_line = 0;
        break;
      }
    }
  }
  for(i = 1; i <= NR; i++)
    print lines[i]
}
=== END ===
I don't know if that actually works but basically it slurps the file
into an array, loops backwards until you find the current line, then
searches for the next checkbox and updates it in the array, and
finally prints the entire file back out with the change. The isn't
computationally annoying but it gets the job done and it's super
portable.


Footnotes:
  1. This might be really tricky with combined UTF-8.
