Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@howardabrams
Created March 19, 2015 18:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save howardabrams/0aff8f90e426c4b976d4 to your computer and use it in GitHub Desktop.
Save howardabrams/0aff8f90e426c4b976d4 to your computer and use it in GitHub Desktop.
;;; 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:
;;
;; v1. Runs a script in the current directory of the file
;; v2. Runs a script in an ancestor of the file's directory
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; 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 parent-directory (dir)
"Return the parent of DIR. If dir is at the top of the tree, return nil."
(unless (equal "/" dir)
(file-name-directory (directory-file-name dir))))
(defun find-file-in-heirarchy (current-dir fname)
"Search CURRENT-DIR for a file named FNAME, and walk up through the directory hierarchy."
(let ((file (concat current-dir fname))
(parent (parent-directory (expand-file-name current-dir))))
(if (file-exists-p file)
file
(when parent
(find-file-in-heirarchy parent fname)))))
(defun ha/find-on-save-script (filename)
"Locate a .on-save script in directory containing FILENAME (or ancestor)."
(let ((dir (file-name-directory filename)))
(concat (find-file-in-heirarchy dir ".on-save") " " filename)))
(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))
(script (ha/find-on-save-script filename)))
(write-file filename nil)
;; Need to re-add the hook to the local file:
(add-hook 'local-write-file-hooks 'ha/folder-action-save-hook)
(when script
(async-shell-command script))))
(add-hook 'local-write-file-hooks 'ha/folder-action-save-hook)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; save-hooks.el ends here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment