views:

77

answers:

3

I am writing a terminal-based application, but I want the user to be able to edit certain text data in a separate editor. For example, if the user chooses to edit the list of current usernames, the list should open as a text file in the user's favorite editor (vim, gedit, etc.). This will probably be an environment variable such as $MYAPPEDITOR. This is similar to the way commit messages work in svn.

Is the best way to do this to create a temporary file in /tmp, and read it in when the editor process is terminated? Or is there a better way to approach this problem?

+1  A: 

The way svn and mercurial do it is by making a file in /tmp.

BTW, you don't need a MYAPPEDITOR, on nix there's EDITOR already present.

Prody
+2  A: 

There's already a $EDITOR variable, which is extremely standard and I have seen it working on a wide variety of unixes. Also, vi is always an option on any flavor of unix.

Debian has a sensible-editor command that invokes $EDITOR if it can, or falls back to some standard ones otherwise. Freedesktop.org has an xdg-open command that will detect which desktop environment is running and open the file with the associated application. As far as I know, sensible-editor doesn't exist on other distributions, and of course xdg-open will fail in a text-only environment, but it couldn't hurt to try as many options as possible, if you think it's important that a desktop user can see their happy shiny gedit or kate instead of scary old vi or nano. ;)

The way crontab and sudoedit work is also by making a file in /tmp. git puts it under .git, and svn actually puts it in the current directory (not /tmp).

jleedev
There is also $VISUAL - which should be preferred over $EDITOR (which might be set to 'ed' instead of 'vim'). Raymond's 'The Art of Unix Programming' (http://www.catb.org/~esr/writings/taoup) discusses this; POSIX mentions them (http://www.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html - also http://www.opengroup.org/onlinepubs/9699919799/utilities/mailx.html, http://www.opengroup.org/onlinepubs/9699919799/utilities/more.html, http://www.opengroup.org/onlinepubs/9699919799/utilities/crontab.html). These are not wholly consistent, in my view (or, at least, the defaults aren't).
Jonathan Leffler
A: 

Since you mention svn in your post, why not just follow the same methodology? svn opens a file with a particular name with whatever $EDITOR (or $SVN_EDITOR) contains - this might actually require some work on your part; determining the parameters to each supported editor. In either case, you have the name of the file that was saved (or the error code of the application if something failed) and you can just use that.

ezpz