Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
;;; SAVE-HOOKS --- Folder Actions on Save
;;
;; Author: Howard Abrams <howard.abrams@gmail.com>
;; Copyright © 2015, Howard Abrams, all rights reserved.
;; Created: 18 March 2015
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Commentary:
;;
;; A hook that analyzes the directory where a file is being saved and
;; if there exists a file called .on-save, it executes it with the
;; name of the file that was saved.
;;
;; Note: The .on-save script must have the executable bit set, as
;; well as a shebang line, since the hook simply calls it.
;;
;; What good is this? Well, think of a script that kicks off the unit
;; tests whenever a file is saved. Or how about a script with an
;; rsync command to copy the file to a remote location.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Change log:
;;
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 3, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Code:
(defun ha/folder-action-save-hook ()
"A file save hook that will look for a script in the same directory, called .on-save. It will then execute that script asynchronously."
(let* ((filename (buffer-file-name))
(dir (file-name-directory filename))
(script (concat dir ".on-save"))
(cmd (concat script " " filename)))
(write-file filename nil)
(when (file-exists-p script)
(async-shell-command cmd))))
(add-hook 'local-write-file-hooks 'ha/folder-action-save-hook)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; save-hooks.el ends here
@camdez

This comment has been minimized.

Copy link

commented Mar 19, 2015

Hey Howard, thanks for sharing! I've dabbled with some hacks like this myself. The one thing to watch out for, though, is that this could be a big security risk. If you cloned down a repo with a malicious .on-save script and edited a file, you could be in trouble.

If you wanted to enhance it, you might consider checksumming (md5 or otherwise) the script and comparing it against a list of approved scripts which you could persist to the users custom.el file. If the file is not known to be safe you could prompt the user whether or not to run it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.