R/backup_dot_emacs.R

#' backup `.emacs` to .emacs.d and restore an old one
#' @description backup some import files to `.emacs.d`
#' @author lgm
#' @param restoreFile if restoreFile="",just back up; if a file like "2017-06-27.emacs", rename the current .emacs to temp20171118.emacs,and restore the old one.
#' @return message
#' @export
#' @examples
#' backup_dot_emacs() # just back up
#' backup_dot_emacs("2017-06-16.emacs") #restore an old file.

backup_dot_emacs <- function(restoreFile="") {
	file <- c(".emacs")
	orig_path <- file.path("~", file)
	tstamp <- format(Sys.Date(), "%Y-%m-%d")
	file_bak <- paste0(tstamp, file)
	new_path <- file.path("~/.emacs.d", file_bak)

	tmp <- paste0("temp",tstamp,file)
	tmpemacs <- file.path("~/.emacs.d",tmp)

	if (restoreFile == "") {
		if (!file.exists(new_path)) {
			system(paste("cp", orig_path, new_path))
			return("The .emacs is backuped.")
		} else {
			return("already backuped.")
		}
	} else {
		system(paste("cp", orig_path, tmpemacs))
		system(paste("rm", orig_path))
		system(paste("cp",paste0("~/.emacs.d/",restoreFile),orig_path))
		return(paste("replace the .emacs with",restoreFile))
	}

}
Gabegit/gmtools documentation built on May 6, 2019, 5:32 p.m.