Idiom for shelling out to $EDITOR/$PAGER?

Ben Finney ben+python at benfinney.id.au
Fri Dec 23 01:12:20 EST 2011


Cameron Simpson <cs at zip.com.au> writes:

> On 22Dec2011 22:16, Tim Chase <python.list at tim.thechases.com> wrote:
> | -proper & efficient detection of file-change, to know whether the
> | user actually did anything
>
> Wait for the editor to exit?
> In that scenario I go for:
>   - wait for edit to exit
>   - if exit status 0 and file non-empty, trust it
>     (subject to parse issues afterwards of course)

That doesn't address the concern Tim raised: did the user actually do
anything, did the file change?

The exit status of text editors are not bound to distinguish “buffer was
modified”, and certainly don't do so in any standard way.

My advice:

* Compute a before-edit hash of the text (MD5 or SHA-1 would be fine).

* Invoke the editor on that text.

* Wait for (or detect) the exit of the editor process.

* Compute an after-edit hash of the text resulting from the editor.

* Compare the hashes to see whether the text changed.

-- 
 \       “Come on Milhouse, there's no such thing as a soul! It's just |
  `\      something they made up to scare kids, like the Boogie Man or |
_o__)                          Michael Jackson.” —Bart, _The Simpsons_ |
Ben Finney



More information about the Python-list mailing list