aboutsummaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/._G.luadoc1
-rw-r--r--core/.gui.luadoc15
-rw-r--r--core/gui.lua89
-rw-r--r--core/init.lua13
-rw-r--r--core/locale.conf2
-rw-r--r--core/locales/locale.ru.conf2
6 files changed, 96 insertions, 26 deletions
diff --git a/core/._G.luadoc b/core/._G.luadoc
index 94de2a73..09749436 100644
--- a/core/._G.luadoc
+++ b/core/._G.luadoc
@@ -12,7 +12,6 @@ module('_G')
-- * `_LEXERPATH` [string]: Paths to lexers, formatted like
-- [`package.path`][package_path].
-- * `_RELEASE` [string]: The Textadept release version.
--- * `_THEME` [string]: The [theme](../manual/8_Themes.lua) file to use.
-- * `_USERHOME` [string]: Path to the user's `~/.textadept/`.
-- * `_CHARSET` [string]: The character set encoding of the filesystem. This is
-- used in [File I/O](../modules/io.html).
diff --git a/core/.gui.luadoc b/core/.gui.luadoc
index deb39d58..94edc031 100644
--- a/core/.gui.luadoc
+++ b/core/.gui.luadoc
@@ -107,3 +107,18 @@ function dialog(kind, ...) end
-- @usage gui.filteredlist('Title', { 'Foo', 'Bar' }, { 'a', 'b', 'c', 'd' },
-- false, '--output-column', '2')
function filteredlist(title, columns, items, int_return, ...) end
+
+---
+-- Sets the editor theme from the given name.
+-- Themes in `_USERHOME/themes/` are checked first, followed by `_HOME/themes/`.
+-- If the name contains slashes ('/' on Linux and Mac OSX and '\' on Win32), it
+-- is assumed to be an absolute path so `_USERHOME` and `_HOME` are not checked.
+-- Throws an error if the theme is not found. Any errors in the theme are
+-- printed to `io.stderr`.
+-- @param name The name or absolute path of a theme. If nil, sets the default
+-- theme.
+function set_theme(name) end
+
+---
+-- Prompts the user to select an editor theme from a filtered list.
+function select_theme() end
diff --git a/core/gui.lua b/core/gui.lua
index 0beb1a11..3ebc63e5 100644
--- a/core/gui.lua
+++ b/core/gui.lua
@@ -3,7 +3,8 @@
local L = locale.localize
local gui = gui
--- LuaDoc is in core/.gui.luadoc.
+-- Helper function for printing messages to buffers.
+-- @see gui._print
local function _print(buffer_type, ...)
if buffer._type ~= buffer_type then
for i, view in ipairs(_VIEWS) do
@@ -78,6 +79,74 @@ function gui.goto_file(filename, split, preferred_view)
io.open_file(filename)
end
+local THEME
+-- LuaDoc is in core/.gui.luadoc.
+function gui.set_theme(name)
+ if not name then
+ -- Read theme from ~/.textadept/theme, defaulting to 'light'.
+ local f = io.open(_USERHOME..'/theme', 'rb')
+ if f then
+ name = f:read('*line'):match('[^\r\n]+')
+ f:close()
+ end
+ if not name or name == '' then name = 'light' end
+ end
+
+ -- Get the path of the theme.
+ local theme
+ if not name:find('[/\\]') then
+ if lfs.attributes(_USERHOME..'/themes/'..name) then
+ theme = _USERHOME..'/themes/'..name
+ elseif lfs.attributes(_HOME..'/themes/'..name) then
+ theme = _HOME..'/themes/'..name
+ end
+ elseif lfs.attributes(name) then
+ theme = name
+ end
+ if not theme then error(('"%s" %s'):format(name, L("theme not found."))) end
+
+ if buffer and view then
+ local current_buffer, current_view = _BUFFERS[buffer], _VIEWS[view]
+ for i in ipairs(_BUFFERS) do
+ view:goto_buffer(i)
+ buffer.property['lexer.lpeg.color.theme'] = theme..'/lexer.lua'
+ local lexer = buffer:get_lexer()
+ buffer:set_lexer('null') -- lexer needs to be changed to reset styles
+ buffer:set_lexer(lexer)
+ local ok, err = pcall(dofile, theme..'/buffer.lua')
+ if not ok then io.stderr:write(err) end
+ end
+ view:goto_buffer(current_buffer)
+ for i in ipairs(_VIEWS) do
+ gui.goto_view(i)
+ local lexer = buffer:get_lexer()
+ buffer:set_lexer('null') -- lexer needs to be changed to reset styles
+ buffer:set_lexer(lexer)
+ local ok, err = pcall(dofile, theme..'/view.lua')
+ if not ok then io.stderr:write(err) end
+ end
+ gui.goto_view(current_view)
+ end
+ THEME = theme
+end
+
+-- LuaDoc is in core/.gui.luadoc.
+function gui.select_theme()
+ local themes, themes_found = {}, {}
+ for theme in lfs.dir(_HOME..'/themes') do
+ if not theme:find('^%.%.?$') then themes_found[theme] = true end
+ end
+ if lfs.attributes(_USERHOME..'/themes') then
+ for theme in lfs.dir(_USERHOME..'/themes') do
+ if not theme:find('^%.%.?$') then themes_found[theme] = true end
+ end
+ end
+ for theme in pairs(themes_found) do themes[#themes + 1] = theme end
+ table.sort(themes)
+ local theme = gui.filteredlist(L('Select Theme'), L('Name'), themes)
+ if theme then gui.set_theme(theme) end
+end
+
local connect = events.connect
-- Sets default properties for a Scintilla window.
@@ -96,11 +165,9 @@ connect(events.VIEW_NEW, function()
for _, key in ipairs(ctrl_shift_keys) do
buffer:clear_cmd_key(string.byte(key), c.SCMOD_CTRL + c.SCMOD_SHIFT)
end
-
- if _THEME and #_THEME > 0 then
- local ok, err = pcall(dofile, _THEME..'/view.lua')
- if not ok then io.stderr:write(err) end
- end
+ -- Load theme.
+ local ok, err = pcall(dofile, THEME..'/view.lua')
+ if not ok then io.stderr:write(err) end
end)
connect(events.VIEW_NEW, function() events.emit(events.UPDATE_UI) end)
@@ -119,16 +186,12 @@ local function set_properties()
buffer.property['textadept.home'] = _HOME
buffer.property['lexer.lpeg.home'] = _LEXERPATH
buffer.property['lexer.lpeg.script'] = _HOME..'/lexers/lexer.lua'
- if _THEME and #_THEME > 0 then
- buffer.property['lexer.lpeg.color.theme'] = _THEME..'/lexer.lua'
- end
+ buffer.property['lexer.lpeg.color.theme'] = THEME..'/lexer.lua'
-- Buffer.
buffer.code_page = _SCINTILLA.constants.SC_CP_UTF8
-- Load theme.
- if _THEME and #_THEME > 0 then
- local ok, err = pcall(dofile, _THEME..'/buffer.lua')
- if not ok then io.stderr:write(err) end
- end
+ local ok, err = pcall(dofile, THEME..'/buffer.lua')
+ if not ok then io.stderr:write(err) end
end
-- Sets default properties for a Scintilla document.
diff --git a/core/init.lua b/core/init.lua
index 97628b33..9dac66fb 100644
--- a/core/init.lua
+++ b/core/init.lua
@@ -15,18 +15,7 @@ require 'keys'
_LEXERPATH = _USERHOME..'/lexers/?.lua;'.._HOME..'/lexers'
-_THEME = 'light'
-local f = io.open(_USERHOME..'/theme', 'rb')
-if f then
- local theme = f:read('*line'):match('[^\r\n]+')
- f:close()
- if theme and #theme > 0 then _THEME = theme end
-end
-if not _THEME:find('[/\\]') then
- local theme = _THEME
- _THEME = _USERHOME..'/themes/'..theme
- if not lfs.attributes(_THEME) then _THEME = _HOME..'/themes/'..theme end
-end
+gui.set_theme()
-- LuaDoc is in core/._G.luadoc.
function user_dofile(filename)
diff --git a/core/locale.conf b/core/locale.conf
index c272c343..cf9a3e86 100644
--- a/core/locale.conf
+++ b/core/locale.conf
@@ -37,6 +37,7 @@ Name = Name
%File = File
Untitled = Untitled
Switch Buffers = Switch Buffers
+theme not found. = theme not found.
CRLF = CRLF
CR = CR
LF = LF
@@ -220,6 +221,7 @@ Toggle Virtual Space = Toggle _Virtual Space
Zoom In = Zoom _In
Zoom Out = Zoom _Out
Reset Zoom = _Reset Zoom
+Select Theme... = Select _Theme...
Help = _Help
Show Manual = Show _Manual
Show LuaDoc = Show _LuaDoc
diff --git a/core/locales/locale.ru.conf b/core/locales/locale.ru.conf
index 8ec93ec1..0b349239 100644
--- a/core/locales/locale.ru.conf
+++ b/core/locales/locale.ru.conf
@@ -37,6 +37,7 @@ Name = Название
%File = Файл
Untitled = Безымянный
Switch Buffers = Переключение между буферами
+theme not found. =
CRLF = CRLF
CR = CR
LF = LF
@@ -220,6 +221,7 @@ Toggle Virtual Space = Переключить режим _виртульных
Zoom In = _Приблизить
Zoom Out = _Отдалить
Reset Zoom = _Сбросить масштаб
+Select Theme... =
Help = _Справка
Show Manual = Показать _руководство
Show LuaDoc = Показать документацию по _lua