2019-03-12 08:54:19 +02:00
|
|
|
use crate::error::Error;
|
|
|
|
|
use crate::executor::RunType;
|
|
|
|
|
use crate::terminal::print_separator;
|
|
|
|
|
use crate::utils::{require, require_option, PathExt};
|
|
|
|
|
use directories::BaseDirs;
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
use std::env;
|
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
|
|
const EMACS_UPGRADE: &str = include_str!("emacs.el");
|
|
|
|
|
|
|
|
|
|
pub struct Emacs {
|
|
|
|
|
directory: Option<PathBuf>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Emacs {
|
|
|
|
|
fn directory_path(base_dirs: &BaseDirs) -> Option<PathBuf> {
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
|
return base_dirs.home_dir().join(".emacs.d").if_exists();
|
|
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
return env::var("HOME")
|
|
|
|
|
.ok()
|
|
|
|
|
.and_then(|home| PathBuf::from(home).join(".emacs.d").if_exists())
|
|
|
|
|
.or_else(|| base_dirs.data_dir().join(".emacs.d").if_exists());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn new(base_dirs: &BaseDirs) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
directory: Emacs::directory_path(base_dirs),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn directory(&self) -> Option<&PathBuf> {
|
|
|
|
|
self.directory.as_ref()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn upgrade(&self, run_type: RunType) -> Result<(), Error> {
|
|
|
|
|
let emacs = require("emacs")?;
|
|
|
|
|
let init_file = require_option(self.directory.as_ref())?.join("init.el").require()?;
|
|
|
|
|
|
|
|
|
|
print_separator("Emacs");
|
|
|
|
|
|
2019-11-03 07:45:19 -05:00
|
|
|
// Convert the whitespace in the emacs lisp code to NONBREAKING SPACE.
|
|
|
|
|
let escaped: String = EMACS_UPGRADE
|
|
|
|
|
.chars()
|
|
|
|
|
.map(|c| if c.is_whitespace() { '\u{00a0}' } else { c })
|
|
|
|
|
.collect();
|
|
|
|
|
|
2019-03-12 08:54:19 +02:00
|
|
|
run_type
|
|
|
|
|
.execute(&emacs)
|
2019-05-15 10:59:05 +03:00
|
|
|
.args(&["--batch", "-l"])
|
|
|
|
|
.arg(init_file)
|
2019-11-03 07:45:19 -05:00
|
|
|
.arg("--eval")
|
|
|
|
|
.arg(escaped)
|
2019-03-12 08:54:19 +02:00
|
|
|
.check_run()
|
|
|
|
|
}
|
|
|
|
|
}
|