;;; sync-file.el --- a viewing mode for books and novels. -*- lexical-binding: t; -*- ;; Copyright (C) 2019 Robert Rose ;; Author: Robert Rose ;; Keywords: lisp sync file ;; Version: 0.0.1 ;; Package-Requires ((emacs "24")) ;; 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 of the License, 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. If not, see . ;;; Commentary: ;; Allows you to syncronize files by newest file date. Directories arent currently handled, ;; as I have no need for that functionaliity at this point. Parent directories will always ;; be created if they dont exist. Some conditions that may occur haven't been covered, such ;; as write permissions and attributes. If one of the files is a regular file and the other ;; is a directory, strange behaviours may occur. Another thing to note is that file names ;; can make use of TRAMP, which makes remote sync and root file access possible. ;; e.g., ;; (let ((files ;; '(("~/foo/file1.txt" . "/ssh:use@host:~/foo/file1.txt") ;; ("~/foo/file2.txt" . "/ssh:use@host:~/foo/file2.txt") ;; ("~/foo/file3.txt" . "/ssh:use@host:~/foo/file3.txt")) )) ;; (sync-files files)) ;;; Code: (defun copy-file-make-parents (name newname) "Copy file and make parent directories if neccessary." (let* ((parent (file-name-directory (file-name-directory newname)))) (unless (file-directory-p parent) (make-directory parent t)) (copy-file name newname t))) (defun sync-file (f1 f2) "Syncronize newest to oldest file. Parent directories will be created." (cond ((and (file-exists-p f1) (file-exists-p f2)) (cond ((file-newer-than-file-p f1 f2) (copy-file f1 f2 t)) ((file-newer-than-file-p f2 f1) (copy-file f2 f1 t)) )) ((and (file-exists-p f1) (not (file-exists-p f2))) (copy-file-make-parents f1 f2) ) ((and (file-exists-p f2) (not (file-exists-p f1))) (copy-file-make-parents f2 f1)) )) (defun sync-files (files) "Syncronize newest to oldest files. Parent directories will be created. `files' should be in the form (cons f1 f2)." (dolist (file files) (sync-file (car file) (cdr file)))) (provide 'sync-file) ;;; sync-file.el ends here