From ae1cbd80a71ca7a63b271c2a53e74d2d56deeb6a Mon Sep 17 00:00:00 2001 From: Bastian Rinsche Date: Mon, 5 Mar 2018 02:28:46 +0100 Subject: [PATCH] Allow dynamic loading of themes and syntaxes Signed-off-by: Bastian Rinsche --- README.md | 8 ++++++++ qlhighlight/src/lib.rs | 28 +++++++++++++++++++++++++--- qlhighlight/src/quicklook.rs | 1 + qlhighlight/src/util.rs | 32 ++++++++++++++++++++++++++------ 4 files changed, 60 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 7a24554..0985cec 100644 --- a/README.md +++ b/README.md @@ -31,4 +31,12 @@ Setting a theme (default is an [included xcode-like theme](qlhighlight/res/Xcode Available themes are the default theme and [these](https://docs.rs/syntect/2.0.0/syntect/highlighting/struct.ThemeSet.html#method.load_defaults). +Adding additional themes. Set to absolute path of directory containing `.tmTheme` files: + + defaults write de.bastianrinsche.QLSyntaxHighlight themeDirectory '/path/to/themes' + +Adding addiontal syntaxes. Set to absolute path of directory containing `.sublime-syntax` files: + + defaults write de.bastianrinsche.QLSyntaxHighlight syntaxDirectory '/path/to/syntaxes' + ![screenshot](img/screenshot.png) diff --git a/qlhighlight/src/lib.rs b/qlhighlight/src/lib.rs index 7f6e783..c28a0c4 100644 --- a/qlhighlight/src/lib.rs +++ b/qlhighlight/src/lib.rs @@ -6,6 +6,7 @@ mod quicklook; use std::fmt::Write; use std::io::Cursor; +use std::path::Path; use core_foundation::base::{OSStatus, TCFType}; use core_foundation::data::CFData; @@ -36,9 +37,30 @@ pub extern "C" fn GeneratePreviewForURL( let theme_bytes = include_bytes!("../res/XCodelike.tmTheme"); let xcode_theme = ThemeSet::load_from_reader(&mut Cursor::new(&theme_bytes[..])).unwrap(); - let ts = ThemeSet::load_defaults(); - let syntax_set = SyntaxSet::load_defaults_nonewlines(); - let theme = ts.themes + let mut theme_set = ThemeSet::load_defaults(); + let mut syntax_set = SyntaxSet::load_defaults_nonewlines(); + + if let Some(theme_dir) = conf.theme_dir { + let directory = theme_dir.to_string(); + let theme_dir = Path::new(&directory); + if let Ok(mut custom_themes) = ThemeSet::load_from_folder(&theme_dir) { + theme_set.themes.append(&mut custom_themes.themes); + } + }; + + if let Some(syntax_dir) = conf.syntax_dir { + let directory = syntax_dir.to_string(); + let syntax_dir = Path::new(&directory); + if let Ok(mut custom_syntaxes) = SyntaxSet::load_from_folder(&syntax_dir) { + for set in custom_syntaxes.syntaxes() { + syntax_set.add_syntax(set.clone()); + } + syntax_set.link_syntaxes() + } + }; + + let theme = theme_set + .themes .get(&conf.theme_name.to_string()) .unwrap_or(&xcode_theme); diff --git a/qlhighlight/src/quicklook.rs b/qlhighlight/src/quicklook.rs index f57571a..0aa0f65 100644 --- a/qlhighlight/src/quicklook.rs +++ b/qlhighlight/src/quicklook.rs @@ -3,6 +3,7 @@ use core_foundation::data::CFDataRef; use core_foundation::string::CFStringRef; use core_foundation::dictionary::CFDictionaryRef; +#[allow(dead_code)] extern "C" { #[link_name = "kUTTypeHTML"] pub static kUTTypeHTML: CFStringRef; diff --git a/qlhighlight/src/util.rs b/qlhighlight/src/util.rs index 1ee9ed7..c47db97 100644 --- a/qlhighlight/src/util.rs +++ b/qlhighlight/src/util.rs @@ -17,18 +17,28 @@ extern "C" { pub static kCFPreferencesAnyHost: CFStringRef; } +const FONT_SIZE: &'static str = "fontSize"; +const FONT_FAMILY: &'static str = "fontFamily"; +const THEME: &'static str = "theme"; +const THEME_DIR: &'static str = "themeDirectory"; +const SYNTAX_DIR: &'static str = "syntaxDirectory"; + #[derive(Debug)] pub struct Config { pub font_size: CFString, pub font_family: CFString, pub theme_name: CFString, + pub theme_dir: Option, + pub syntax_dir: Option, } pub fn get_settings() -> Config { let keys = CFArray::from_CFTypes(&[ - CFString::new("fontSize"), - CFString::new("fontFamily"), - CFString::new("theme"), + CFString::new(FONT_SIZE), + CFString::new(FONT_FAMILY), + CFString::new(THEME), + CFString::new(THEME_DIR), + CFString::new(SYNTAX_DIR), ]); let prefs = unsafe { @@ -43,23 +53,33 @@ pub fn get_settings() -> Config { let prefs: CFDictionary = unsafe { CFDictionary::wrap_under_get_rule(prefs) }; let font_size = prefs - .find2(&CFString::new("fontSize")) + .find2(&CFString::new(FONT_SIZE)) .and_then(|ptr| unsafe { CFType::wrap_under_create_rule(ptr).downcast::() }) .unwrap_or(CFString::new("11")); let font_family = prefs - .find2(&CFString::new("fontFamily")) + .find2(&CFString::new(FONT_FAMILY)) .and_then(|ptr| unsafe { CFType::wrap_under_create_rule(ptr).downcast::() }) .unwrap_or(CFString::new("Menlo, monospace")); let theme_name = prefs - .find2(&CFString::new("theme")) + .find2(&CFString::new(THEME)) .and_then(|ptr| unsafe { CFType::wrap_under_create_rule(ptr).downcast::() }) .unwrap_or(CFString::new("InspiredGithub")); + let theme_dir = prefs + .find2(&CFString::new(THEME_DIR)) + .and_then(|ptr| unsafe { CFType::wrap_under_create_rule(ptr).downcast::() }); + + let syntax_dir = prefs + .find2(&CFString::new(SYNTAX_DIR)) + .and_then(|ptr| unsafe { CFType::wrap_under_create_rule(ptr).downcast::() }); + Config { font_size, font_family, theme_name, + theme_dir, + syntax_dir, } }