MH & nmh: Email for Users & Programmers

May, 2006

Writing Your Own Draft Message Editor(s)

You may want to do more than an editor like vi or emacs can do to a draft message. For instance, you might want to start prompter on the new draft, then run a second editor to fill in the body. Or you might want to do something automatically each time a draft message is edited, like updating a mail message log. Or you could customize the way that a particular MH program, such as dist, handles a draft message. These are all good reasons to write a special editing shell script.

There are five things to know before you write your editor script:

  1. Each of the four mail composition commands will make a draft message for you. If no other editor (including prompter) has touched it, the draft will contain: For more information, see the Section Making the Draft from the Template File.
  2. The full pathname of the draft message will be in the $1 parameter.
  3. You can use the environment variables listed in the Table Environment Variables that MH Sets.
  4. Your editor should save the edited draft in the same file ($1) where it read the draft from.
  5. If your editor exits with a zero status, whatnow will prompt the user What now?. But, as the Section Aborted Draft Messages explains, if your editor exits with a nonzero status, whatnow will abort without sending the message, and:
distprompter, an editor shell script, acts like prompter to read the message header of a message you're distributing with dist. When it's done reading the header fields, it saves the draft and exits without giving you a chance to type the body (you can't type a body for dist).

Even if you don't run distprompter, it's a good example of what you can do with an MH draft editor. To run distprompter or another editor that you write, put an entry like this in your MH profile:

    dist: -editor distprompter
    
If you write a more general purpose shell script editor for all the MH message composing programs, you can use an MH profile entry like this instead:
    Editor: myeditor
    
One more note about editors: even though the mh-profile(5) and whatnow(1) manual pages have a lot of details, they don't explain an editor's environment or the effect of its exit status very thoroughly. I wrote this little test editor script that helped me learn. Maybe it'll help you:
    #! /bin/sh
    echo The environment of $0:
    printenv
    echo "Command line had: '$*'"
    echo -n "Enter exit status for $0: "
    read stat
    exit $stat
    
Run it by typing, for example:
    % comp -editor testedit