Day 18: Checking unsaved files (File::Unsaved)

About the series: perlancar's 2014 Advent Calendar: Introduction to a selection of 24 modules which I published in 2014. Table of contents.

No, I'm not tempted to do a pun, despite the aptness of the situation :-)

File::Unsaved is yet another one of my little modules. Its one and only task is to check whether a file is "unsaved", that is, opened in an editor and has unsaved modifications. Currently it has support for several editors: Emacs and joe (on Unix; I haven't checked how it/they work(s) on Windows with regard to signalling unsaved files), and vim.

Quoting the POD, here's how it works:

  • For emacs and joe: check whether .#<name> (symlink) exists. Emacs targets the symlink to <user>@<host>.<PID>:<timestamp> while joe to <user>@<host>.<PID>. Caveat: Unix only.

  • For vim: check whether .<name>.swp exists, newer than file, and its 0x03ef-th byte has the value of U (which vim uses to mark the file as unsaved). Caveat: vim can be instructed to put swap file somewhere else or not create swap file at all, so in those cases unsaved data will not be detected.

Why I need this: I sometimes log stuffs to text files using a CLI tool, e.g. whenever I download some files, I usually "ls -laR" them and append the output to a log file, along with date and host. This lets me know when and where I download files. This can be used to avoid downloading the same files twice, help me locate the files, or do some simple data transfer accounting.

For convenience, nowadays the format that I often use is Org, which lets me edit and outline the log files (e.g. by date) in Emacs. However, sometimes I forget to save the file while appending more data from the CLI, clobbering things. This module lets me check unsaved data and prevent me from clobbering the log files.

Leave a comment

About perlancar

user-pic #perl #indonesia