Skip to content

Instantly share code, notes, and snippets.

@PhilHudson
Created August 30, 2019 21:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PhilHudson/9edc4e5673420f7772a289df0079245d to your computer and use it in GitHub Desktop.
Save PhilHudson/9edc4e5673420f7772a289df0079245d to your computer and use it in GitHub Desktop.
shadow-file
;; Adapted from
;; http://muublog.blogspot.com/2018/01/for-various-reasons-i-needed-to-sync.html
(eval-and-compile
(require 'dash)) ; for `-lambda'
(defcustom ph/shadow-file-mappings nil
"Each key is the regular expression matching the file(s)
that should be copied, and the value is the target directory."
:group 'files
:tag "Shadow file mappings"
:type '(alist
:key-type (regexp :tag "Source file pattern")
:value-type (directory :tag "Target directory")))
(defcustom ph/shadow-file-overwrite-flag t
"Ask the user before overwriting the destination file.
When set to a number, the user will be asked. When `nil',
the file will not be copied. When
`t', the file will be copied without asking."
:group 'files
:tag "Overwrite shadow files"
:type '(choice
(const :tag "Never" nil)
(const :tag "Always" t)
(const :tag "Ask" 1)))
(defun ph/shadow-file-sync ()
"Sync the current file if it matches one of the regexps.
This function will match each regexp in
`ph/shadow-file-mappings' against the current file name. If
there is a match, the current file will be copied to the
configured target directory.
If the file already exist target directory, the option
`ph/shadow-file-overwrite-flag' will control if the file
should be written automatically or if the user should be
presented with a question.
In theory, the same file can be copied to multiple target
directories, by configuring multiple regexps that match the same
file."
(mapc
(-lambda ((source-file-pattern . target-directory))
(when (string-match-p source-file-pattern (buffer-file-name))
(copy-file
(buffer-file-name)
target-directory
ph/shadow-file-overwrite-flag)
(message
"Copied file %s to %s" (buffer-file-name) target-directory)))
ph/shadow-file-mappings))
(add-hook 'after-save-hook 'ph/shadow-file-sync)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment