aboutsummaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authormitchell <70453897+667e-11@users.noreply.github.com>2014-06-12 21:18:13 -0400
committermitchell <70453897+667e-11@users.noreply.github.com>2014-06-12 21:18:13 -0400
commita9f6d85ead8c83ce210e5ffb05f84361e029f419 (patch)
treed7fcd5c957e3170f396eb515b94927174ad955d1 /core
parentd3e1bd0272a6ac75d927e0c7d5fcfde91d90bcc4 (diff)
downloadtextadept-a9f6d85ead8c83ce210e5ffb05f84361e029f419.tar.gz
textadept-a9f6d85ead8c83ce210e5ffb05f84361e029f419.zip
Lua code cleanup.
Diffstat (limited to 'core')
-rw-r--r--core/file_io.lua37
-rw-r--r--core/init.lua2
-rw-r--r--core/keys.lua45
-rw-r--r--core/ui.lua1
4 files changed, 32 insertions, 53 deletions
diff --git a/core/file_io.lua b/core/file_io.lua
index a79f907c..7ddfee00 100644
--- a/core/file_io.lua
+++ b/core/file_io.lua
@@ -98,6 +98,8 @@ io.boms = {
-- @name encodings
io.encodings = {'UTF-8', 'ASCII', 'ISO-8859-1', 'MacRoman'}
+local c = _SCINTILLA.constants
+local EOLs = {['\r\n'] = c.EOL_CRLF, ['\r'] = c.EOL_CR, ['\n'] = c.EOL_LF}
---
-- Opens *filenames*, a string filename or list of filenames, or the
-- user-selected filenames.
@@ -122,7 +124,7 @@ function io.open_file(filenames)
local text = ''
local f, err = io.open(filename, 'rb')
if f then
- text = f:read('*all')
+ text = f:read('*a')
f:close()
if not text then return end -- filename exists, but cannot read it
elseif lfs.attributes(filename) then
@@ -147,12 +149,8 @@ function io.open_file(filenames)
end
buffer.code_page = buffer.encoding and buffer.CP_UTF8 or 0
-- Detect EOL mode.
- local s, e = text:find('\r\n?')
- if s and e then
- buffer.eol_mode = (s == e and buffer.EOL_CR or buffer.EOL_CRLF)
- else
- buffer.eol_mode = buffer.EOL_LF
- end
+ buffer.eol_mode = EOLs[text:match('\r\n?')] or buffer.EOL_LF
+ -- Insert buffer text and set properties.
buffer:add_text(text, #text)
buffer:goto_pos(0)
buffer:empty_undo_buffer()
@@ -176,9 +174,8 @@ end
function io.reload_file()
if not buffer.filename then return end
local pos, first_visible_line = buffer.current_pos, buffer.first_visible_line
- local f, err = io.open(buffer.filename, 'rb')
- assert(f, err)
- local text = f:read('*all')
+ local f = assert(io.open(buffer.filename, 'rb'))
+ local text = f:read('*a')
f:close()
local encoding, encoding_bom = buffer.encoding, buffer.encoding_bom
if encoding_bom then text = text:sub(#encoding_bom + 1, -1) end
@@ -221,8 +218,7 @@ function io.save_file()
if buffer.encoding then
text = (buffer.encoding_bom or '')..text:iconv(buffer.encoding, 'UTF-8')
end
- local f, err = io.open(buffer.filename, 'wb')
- assert(f, err)
+ local f = assert(io.open(buffer.filename, 'wb'))
f:write(text)
f:close()
buffer:set_save_point()
@@ -269,15 +265,14 @@ end
-- @name close_buffer
function io.close_buffer()
local filename = buffer.filename or buffer._type or _L['Untitled']
- if buffer.modify and ui.dialogs.msgbox{
- title = _L['Close without saving?'],
- text = _L['There are unsaved changes in'],
- informative_text = filename:iconv('UTF-8', _CHARSET),
- icon = 'gtk-dialog-question', button1 = _L['_Cancel'],
- button2 = _L['Close _without saving']
- } ~= 2 then
- return nil -- returning false can cause unwanted key command propagation
- end
+ local confirm = not buffer.modify or ui.dialogs.msgbox{
+ title = _L['Close without saving?'],
+ text = _L['There are unsaved changes in'],
+ informative_text = filename:iconv('UTF-8', _CHARSET),
+ icon = 'gtk-dialog-question', button1 = _L['_Cancel'],
+ button2 = _L['Close _without saving']
+ } == 2
+ if not confirm then return nil end -- nil return won't propagate a key command
buffer:delete()
return true
end
diff --git a/core/init.lua b/core/init.lua
index e8ca6545..23d64b40 100644
--- a/core/init.lua
+++ b/core/init.lua
@@ -32,7 +32,7 @@ if CURSES or OSX then
local current_dir = lfs.currentdir()
lfs.chdir(working_dir)
local p = io.popen(argv..' 2>&1')
- stdout_cb(p:read('*all'))
+ stdout_cb(p:read('*a'))
exit_cb(select(3, p:close()))
lfs.chdir(current_dir)
return p
diff --git a/core/keys.lua b/core/keys.lua
index 1c8a6997..7d658a57 100644
--- a/core/keys.lua
+++ b/core/keys.lua
@@ -134,38 +134,23 @@ M.KEYSYMS = {
-- From curses.h.
[263] = '\b', [343] = '\n',
-- From Scintilla.h.
- [300] = 'down', [301] = 'up', [302] = 'left', [303] = 'right',
- [304] = 'home', [305] = 'end',
- [306] = 'pgup', [307] = 'pgdn',
- [308] = 'del',
- [309] = 'ins',
+ [300] = 'down', [301] = 'up', [302] = 'left', [303] = 'right', [304] = 'home',
+ [305] = 'end', [306] = 'pgup', [307] = 'pgdn', [308] = 'del', [309] = 'ins',
-- From <gdk/gdkkeysyms.h>.
[0xFE20] = '\t', -- backtab; will be 'shift'ed
- [0xFF08] = '\b',
- [0xFF09] = '\t',
- [0xFF0D] = '\n',
- [0xFF1B] = 'esc',
- [0xFFFF] = 'del',
- [0xFF50] = 'home',
- [0xFF51] = 'left', [0xFF52] = 'up',
- [0xFF53] = 'right', [0xFF54] = 'down',
- [0xFF55] = 'pgup', [0xFF56] = 'pgdn',
- [0xFF57] = 'end',
- [0xFF63] = 'ins',
- [0xFF95] = 'kphome',
- [0xFF96] = 'kpleft', [0xFF97] = 'kpup',
- [0xFF98] = 'kpright', [0xFF99] = 'kpdown',
- [0xFF9A] = 'kppgup', [0xFF9B] = 'kppgdn',
- [0xFF9C] = 'kpend',
- [0xFFAA] = 'kpmul', [0xFFAB] = 'kpadd',
- [0xFFAD] = 'kpsub', [0xFFAF] = 'kpdiv',
- [0xFFAE] = 'kpdec',
- [0xFFB0] = 'kp0', [0xFFB1] = 'kp1', [0xFFB2] = 'kp2', [0xFFB3] = 'kp3',
- [0xFFB4] = 'kp4', [0xFFB5] = 'kp5', [0xFFB6] = 'kp6', [0xFFB7] = 'kp7',
- [0xFFB8] = 'kp8', [0xFFB9] = 'kp9',
- [0xFFBE] = 'f1', [0xFFBF] = 'f2', [0xFFC0] = 'f3', [0xFFC1] = 'f4',
- [0xFFC2] = 'f5', [0xFFC3] = 'f6', [0xFFC4] = 'f7', [0xFFC5] = 'f8',
- [0xFFC6] = 'f9', [0xFFC7] = 'f10', [0xFFC8] = 'f11', [0xFFC9] = 'f12',
+ [0xFF08] = '\b', [0xFF09] = '\t', [0xFF0D] = '\n', [0xFF1B] = 'esc',
+ [0xFFFF] = 'del', [0xFF50] = 'home', [0xFF51] = 'left', [0xFF52] = 'up',
+ [0xFF53] = 'right', [0xFF54] = 'down', [0xFF55] = 'pgup', [0xFF56] = 'pgdn',
+ [0xFF57] = 'end', [0xFF63] = 'ins', [0xFF95] = 'kphome', [0xFF9C] = 'kpend',
+ [0xFF96] = 'kpleft', [0xFF97] = 'kpup', [0xFF98] = 'kpright',
+ [0xFF99] = 'kpdown', [0xFF9A] = 'kppgup', [0xFF9B] = 'kppgdn',
+ [0xFFAA] = 'kpmul', [0xFFAB] = 'kpadd', [0xFFAD] = 'kpsub',
+ [0xFFAF] = 'kpdiv', [0xFFAE] = 'kpdec', [0xFFB0] = 'kp0', [0xFFB1] = 'kp1',
+ [0xFFB2] = 'kp2', [0xFFB3] = 'kp3', [0xFFB4] = 'kp4', [0xFFB5] = 'kp5',
+ [0xFFB6] = 'kp6', [0xFFB7] = 'kp7', [0xFFB8] = 'kp8', [0xFFB9] = 'kp9',
+ [0xFFBE] = 'f1', [0xFFBF] = 'f2', [0xFFC0] = 'f3', [0xFFC1] = 'f4',
+ [0xFFC2] = 'f5', [0xFFC3] = 'f6', [0xFFC4] = 'f7', [0xFFC5] = 'f8',
+ [0xFFC6] = 'f9', [0xFFC7] = 'f10', [0xFFC8] = 'f11', [0xFFC9] = 'f12',
}
-- The current key sequence.
diff --git a/core/ui.lua b/core/ui.lua
index 7e5f56ed..e78e6c29 100644
--- a/core/ui.lua
+++ b/core/ui.lua
@@ -211,7 +211,6 @@ end
-- @usage ui.set_theme('light', {font = 'Monospace', fontsize = 12})
-- @name set_theme
function ui.set_theme(name, props)
- if not name then return end
name = name:find('[/\\]') and name or
package.searchpath(name, _USERHOME..'/themes/?.lua;'..
_HOME..'/themes/?.lua')