aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--core/.buffer.luadoc37
-rw-r--r--core/file_io.lua98
-rw-r--r--core/keys.lua2
-rw-r--r--core/ui.lua1
-rw-r--r--doc/14_Appendix.md20
-rw-r--r--modules/textadept/init.lua8
-rw-r--r--modules/textadept/keys.lua12
-rw-r--r--modules/textadept/menu.lua12
-rw-r--r--modules/textadept/run.lua2
-rw-r--r--modules/textadept/session.lua2
10 files changed, 81 insertions, 113 deletions
diff --git a/core/.buffer.luadoc b/core/.buffer.luadoc
index 488422a9..82a363ae 100644
--- a/core/.buffer.luadoc
+++ b/core/.buffer.luadoc
@@ -2721,43 +2721,6 @@ function new() end
function text_range(buffer, start_pos, end_pos) end
---
--- Reloads the file in the buffer.
--- @param buffer The global buffer.
-function reload(buffer) end
-
----
--- Sets the encoding for the buffer to *encoding*, converting its contents from
--- the old encoding to the new one.
--- @param buffer The global buffer.
--- @param encoding The string encoding to set. Valid encodings are ones that GNU
--- iconv accepts.
--- @usage buffer.set_encoding(buffer, 'ASCII')
-function set_encoding(buffer, encoding) end
-
----
--- Saves the buffer to the file.
--- Emits `FILE_BEFORE_SAVE` and `FILE_AFTER_SAVE` events.
--- @param buffer The global buffer.
--- @see _G.events
-function save(buffer) end
-
----
--- Saves the buffer to the *utf8_filename* or user-specified filename.
--- Emits a `FILE_SAVED_AS` event.
--- @param buffer The global buffer.
--- @param utf8_filename The new filepath to save the buffer to. Must be UTF-8
--- encoded.
--- @see _G.events
-function save_as(buffer, utf8_filename) end
-
----
--- Closes the buffer, prompting the user to continue if there are unsaved
--- changes, and returns `true` if the buffer was closed.
--- @param buffer The global buffer.
--- @return `true` if the buffer was closed; `nil` otherwise.
-function close(buffer) end
-
----
-- Returns the name of the lexer used by the buffer, or the name of the lexer
-- under the caret in a multiple-language lexer if *current* is `true`.
-- @param buffer The global buffer.
diff --git a/core/file_io.lua b/core/file_io.lua
index 967332f8..0a2d1963 100644
--- a/core/file_io.lua
+++ b/core/file_io.lua
@@ -36,28 +36,22 @@
-- * _`filename`_: The UTF-8-encoded filename.
-- @field _G.events.FILE_BEFORE_SAVE (string)
-- Emitted right before saving a file to disk.
--- Emitted by [`buffer:save()`][].
+-- Emitted by [`io.save_file()`](#save_file).
-- Arguments:
--
-- * _`filename`_: The UTF-8-encoded filename.
---
--- [`buffer:save()`]: buffer.html#save
-- @field _G.events.FILE_AFTER_SAVE (string)
-- Emitted right after saving a file to disk.
--- Emitted by [`buffer:save()`][].
+-- Emitted by [`io.save_file()`](#save_file).
-- Arguments:
--
-- * _`filename`_: The UTF-8-encoded filename.
---
--- [`buffer:save()`]: buffer.html#save
-- @field _G.events.FILE_SAVED_AS (string)
-- Emitted after saving a file under a different filename.
--- Emitted by [`buffer:save_as()`][].
+-- Emitted by [`io.save_file_as()`](#save_file_as).
-- Arguments:
--
-- * _`filename`_: The UTF-8-encoded filename.
---
--- [`buffer:save_as()`]: buffer.html#save_as
-- @field SNAPOPEN_MAX (number)
-- The maximum number of files to list in the snapopen dialog.
-- The default value is `1000`.
@@ -193,10 +187,10 @@ function io.open_file(utf8_filenames)
end
end
--- LuaDoc is in core/.buffer.luadoc.
-local function reload(buffer)
- if not buffer then buffer = _G.buffer end
- buffer:check_global()
+---
+-- Reloads the current buffer's file contents, discarding any changes.
+-- @name reload_file
+function io.reload_file()
if not buffer.filename then return end
local pos, first_visible_line = buffer.current_pos, buffer.first_visible_line
local filename = buffer.filename:iconv(_CHARSET, 'UTF-8')
@@ -215,9 +209,13 @@ local function reload(buffer)
buffer.mod_time = lfs.attributes(filename, 'modification')
end
--- LuaDoc is in core/.buffer.luadoc.
-local function set_encoding(buffer, encoding)
- buffer:check_global()
+---
+-- Converts the current buffer's contents to string encoding *encoding*.
+-- @param encoding The string encoding to set. Valid encodings are ones that GNU
+-- iconv accepts.
+-- @usage io.set_buffer_encoding('ASCII')
+-- @name set_buffer_encoding
+function io.set_buffer_encoding(encoding)
if not buffer.encoding then
error(_L['Cannot change binary file encoding'])
end
@@ -232,12 +230,15 @@ local function set_encoding(buffer, encoding)
buffer:goto_pos(pos)
buffer.encoding, buffer.encoding_bom = encoding, io.boms[encoding]
end
+-- Sets the default buffer encoding.
+events_connect(events.BUFFER_NEW, function() buffer.encoding = 'UTF-8' end)
--- LuaDoc is in core/.buffer.luadoc.
-local function save(buffer)
- if not buffer then buffer = _G.buffer end
- buffer:check_global()
- if not buffer.filename then buffer:save_as() return end
+---
+-- Saves the current buffer to its file.
+-- Emits `FILE_BEFORE_SAVE` and `FILE_AFTER_SAVE` events.
+-- @name save_file
+function io.save_file()
+ if not buffer.filename then io.save_file_as() return end
events.emit(events.FILE_BEFORE_SAVE, buffer.filename)
local text = buffer:get_text()
if buffer.encoding then
@@ -254,10 +255,13 @@ local function save(buffer)
events.emit(events.FILE_AFTER_SAVE, buffer.filename)
end
--- LuaDoc is in core/.buffer.luadoc.
-local function save_as(buffer, utf8_filename)
- if not buffer and not utf8_filename then buffer = _G.buffer end
- buffer:check_global()
+---
+-- Saves the current buffer to file *utf8_filename* or user-specified filename.
+-- Emits a `FILE_SAVED_AS` event.
+-- @param utf8_filename Optional new filepath to save the buffer to. Must be
+-- UTF-8 encoded. If `nil`, the user is prompted for one.
+-- @name save_file_as
+function io.save_file_as(utf8_filename)
if not utf8_filename then
utf8_filename = ui.dialog('filesave',
'--title', _L['Save'],
@@ -269,27 +273,29 @@ local function save_as(buffer, utf8_filename)
end
if utf8_filename == '' then return end
buffer.filename = utf8_filename
- buffer:save()
+ io.save_file()
events.emit(events.FILE_SAVED_AS, utf8_filename)
end
---
-- Saves all unsaved buffers to their respective files.
--- @see buffer.save
--- @name save_all
-function io.save_all()
+-- @see io.save_file
+-- @name save_all_files
+function io.save_all_files()
local current_buffer = _BUFFERS[buffer]
for i, buffer in ipairs(_BUFFERS) do
view:goto_buffer(i)
- if buffer.filename and buffer.dirty then buffer:save() end
+ if buffer.filename and buffer.dirty then io.save_file() end
end
view:goto_buffer(current_buffer)
end
--- LuaDoc is in core/.buffer.luadoc.
-local function close(buffer)
- if not buffer then buffer = _G.buffer end
- buffer:check_global()
+---
+-- Closes the current buffer, prompting the user to continue if there are
+-- unsaved changes, and returns `true` if the buffer was closed.
+-- @return `true` if the buffer was closed; `nil` otherwise.
+-- @name close_buffer
+function io.close_buffer()
local filename = buffer.filename or buffer._type or _L['Untitled']
if buffer.dirty and ui.dialog('msgbox',
'--title', _L['Close without saving?'],
@@ -310,14 +316,14 @@ end
-- and returning `true` if the user did not cancel.
-- No buffers are saved automatically. They must be saved manually.
-- @return `true` if user did not cancel.
--- @see buffer.close
--- @name close_all
-function io.close_all()
+-- @see io.close_buffer
+-- @name close_all_buffers
+function io.close_all_buffers()
while #_BUFFERS > 1 do
view:goto_buffer(#_BUFFERS)
- if not buffer:close() then return false end
+ if not io.close_buffer() then return false end
end
- return buffer:close() -- the last one
+ return io.close_buffer() -- the last one
end
-- Prompts the user to reload the current file if it has been modified outside
@@ -341,27 +347,19 @@ local function update_modified_file()
'--button2', _L['_No'],
'--no-cancel',
'--no-newline') == '1' then
- buffer:reload()
+ io.reload_file()
end
end
end
events_connect(events.BUFFER_AFTER_SWITCH, update_modified_file)
events_connect(events.VIEW_AFTER_SWITCH, update_modified_file)
--- Set additional buffer functions.
-events_connect(events.BUFFER_NEW, function()
- buffer.reload = reload
- buffer.save, buffer.save_as = save, save_as
- buffer.close = close
- buffer.encoding, buffer.set_encoding = 'UTF-8', set_encoding
-end)
-
--- Close initial "Untitled" buffer.
+-- Closes the initial "Untitled" buffer.
events_connect(events.FILE_OPENED, function(utf8_filename)
local buf = _BUFFERS[1]
if #_BUFFERS == 2 and not (buf.filename or buf._type or buf.dirty) then
view:goto_buffer(1)
- buffer:close()
+ io.close_buffer()
end
end)
diff --git a/core/keys.lua b/core/keys.lua
index ac782df1..bcfc8f7c 100644
--- a/core/keys.lua
+++ b/core/keys.lua
@@ -59,7 +59,7 @@ local M = {}
-- functionally equivalent; you can use either. Examples are:
--
-- keys['cn'] = buffer.new
--- keys['cs'] = buffer.save
+-- keys['cz'] = buffer.undo
-- keys['a('] = {textadept.editing.enclose, '(', ')'}
-- keys['cu'] = function() io.snapopen(_USERHOME) end
--
diff --git a/core/ui.lua b/core/ui.lua
index c58be351..6e3d88ec 100644
--- a/core/ui.lua
+++ b/core/ui.lua
@@ -249,7 +249,6 @@ events_connect(events.BUFFER_NEW, function()
end)
-- Sets the title of the Textadept window to the buffer's filename.
--- @param buffer The global buffer.
local function set_title()
local filename = buffer.filename or buffer._type or _L['Untitled']
local basename = buffer.filename and filename:match('[^/\\]+$') or filename
diff --git a/doc/14_Appendix.md b/doc/14_Appendix.md
index 8696ae58..5687411e 100644
--- a/doc/14_Appendix.md
+++ b/doc/14_Appendix.md
@@ -181,8 +181,13 @@ MARK\_ERROR\_BACK |Removed |N/A<sup>c</sup>
open |Changed |\_G.[io.snapopen()][]<sup>e</sup>
**buffer** | |
get\_style\_name(buffer, n) |Renamed |[style\_name][]\[n\]
+reload |Renamed |[io.reload\_file][]
+save |Renamed |[io.save\_file][]
+save\_as |Renamed |[io.save\_file\_as][]
+close |Renamed |[io.close\_buffer][]
+set\_encoding |Renamed |[io.set\_buffer\_encoding][]
**events** | |
-N/A |New |[INITIALIZED][]<sup>f</sup>
+N/A |New |[INITIALIZED][]
handlers |Removed |N/A
**gui** |Renamed |[ui][]
docstatusbar\_text |Renamed |[bufstatusbar\_text][]
@@ -191,6 +196,8 @@ find.goto\_file\_in\_list() |Renamed |find.[goto\_file\_found()][]
select\_theme |Removed |N/A
**io** | |
try\_encodings |Renamed |[encodings][]
+save\_all |Renamed |[save\_all\_files][]
+close\_all |Renamed |[close\_all\_buffers][]
<sup>a</sup>`arg` is `nil` when resetting.
@@ -202,10 +209,6 @@ try\_encodings |Renamed |[encodings][]
<sup>e</sup>Changed arguments too.
-<sup>f</sup>Custom menus and key bindings should take advantage of this since
-not all buffer functions are available at the `require()` stage. See
-*modules/textadept/init.lua* for an example.
-
[buffer.new()]: api/buffer.html#new
[textadept]: api/textadept.html
[filter\_through()]: api/textadept.editing.html#filter_through
@@ -218,12 +221,19 @@ not all buffer functions are available at the `require()` stage. See
[MARK\_ERROR]: api/textadept.run.html#MARK_ERROR
[io.snapopen()]: api/io.html#snapopen
[style\_name]: api/buffer.html#style_name
+[io.reload\_file]: api/io.html#reload_file
+[io.save\_file]: api/io.html#save_file
+[io.save\_file\_as]: api/io.html#save_file_as
+[io.close\_buffer]: api/io.html#close_buffer
+[io.set\_buffer\_encoding]: api/io.html#set_buffer_encoding
[INITIALIZED]: api/events.html#INITIALIZED
[ui]: api/ui.html
[bufstatusbar\_text]: api/ui.html#bufstatusbar_text
[maximized]: api/ui.html#maximized
[goto\_file\_found()]: api/ui.find.html#goto_file_found
[encodings]: api/io.html#encodings
+[save\_all\_files]: api/io.html#save_all_files
+[close\_all\_buffers]: api/io.html#close_all_buffers
[`buffer.marker_back`]: api/buffer.html#marker_back
[`events.VIEW_NEW`]: api/events.html#VIEW_NEW
[`buffer.indic_fore`]: api/buffer.html#indic_fore
diff --git a/modules/textadept/init.lua b/modules/textadept/init.lua
index b3049a49..9dd072b7 100644
--- a/modules/textadept/init.lua
+++ b/modules/textadept/init.lua
@@ -19,10 +19,8 @@ M.run = require('textadept.run')
M.session = require('textadept.session')
M.snippets = require('textadept.snippets')
-events.connect(events.INITIALIZED, function()
- -- These need to be loaded last.
- M.keys = require('textadept.keys')
- M.menu = require('textadept.menu')
-end)
+-- These need to be loaded last.
+M.keys = require('textadept.keys')
+M.menu = require('textadept.menu')
return M
diff --git a/modules/textadept/keys.lua b/modules/textadept/keys.lua
index 3acb2a28..f7cb599b 100644
--- a/modules/textadept/keys.lua
+++ b/modules/textadept/keys.lua
@@ -258,7 +258,7 @@ M.utils = {
events.emit(events.UPDATE_UI) -- for updating statusbar
end,
set_encoding = function(encoding)
- buffer:set_encoding(encoding)
+ io.set_buffer_encoding(encoding)
events.emit(events.UPDATE_UI) -- for updating statusbar
end,
set_eol_mode = function(mode)
@@ -363,11 +363,11 @@ for _, f in ipairs(menu_buffer_functions) do buffer[f] = buffer[f] end
keys[not OSX and (not CURSES and 'cn' or 'cmn') or 'mn'] = buffer.new
keys[not OSX and 'co' or 'mo'] = io.open_file
keys[not OSX and not CURSES and 'cao' or 'cmo'] = io.open_recent_file
-keys[not OSX and (not CURSES and 'cO' or 'mo') or 'mO'] = buffer.reload
-keys[not OSX and 'cs' or 'ms'] = buffer.save
-keys[not OSX and (not CURSES and 'cS' or 'cms') or 'mS'] = buffer.save_as
-keys[not OSX and 'cw' or 'mw'] = buffer.close
-keys[not OSX and (not CURSES and 'cW' or 'cmw') or 'mW'] = io.close_all
+keys[not OSX and (not CURSES and 'cO' or 'mo') or 'mO'] = io.reload_file
+keys[not OSX and 'cs' or 'ms'] = io.save_file
+keys[not OSX and (not CURSES and 'cS' or 'cms') or 'mS'] = io.save_file_as
+keys[not OSX and 'cw' or 'mw'] = io.close_buffer
+keys[not OSX and (not CURSES and 'cW' or 'cmw') or 'mW'] = io.close_all_buffers
-- TODO: textadept.sessions.load
-- TODO: textadept.sessions.save
keys[not OSX and 'cq' or 'mq'] = quit
diff --git a/modules/textadept/menu.lua b/modules/textadept/menu.lua
index b67f8916..af8e7550 100644
--- a/modules/textadept/menu.lua
+++ b/modules/textadept/menu.lua
@@ -21,12 +21,12 @@ local menubar = {
{_L['_New'], buffer.new},
{_L['_Open'], io.open_file},
{_L['Open _Recent...'], io.open_recent_file},
- {_L['Re_load'], buffer.reload},
- {_L['_Save'], buffer.save},
- {_L['Save _As'], buffer.save_as},
+ {_L['Re_load'], io.reload_file},
+ {_L['_Save'], io.save_file},
+ {_L['Save _As'], io.save_file_as},
SEPARATOR,
- {_L['_Close'], buffer.close},
- {_L['Close All'], io.close_all},
+ {_L['_Close'], io.close_buffer},
+ {_L['Close All'], io.close_all_buffers},
SEPARATOR,
{_L['Loa_d Session...'], textadept.session.load},
{_L['Sav_e Session...'], textadept.session.save},
@@ -366,7 +366,7 @@ if not CURSES then
events.connect(events.LEXER_LOADED, set_language_contextmenu)
events.connect(events.BUFFER_AFTER_SWITCH, set_language_contextmenu)
events.connect(events.VIEW_AFTER_SWITCH, set_language_contextmenu)
- events.connect(events.BUFFER_NEW, set_lang_contextmenu)
+ events.connect(events.BUFFER_NEW, set_language_contextmenu)
end
return M
diff --git a/modules/textadept/run.lua b/modules/textadept/run.lua
index 917cb7a5..67850924 100644
--- a/modules/textadept/run.lua
+++ b/modules/textadept/run.lua
@@ -50,7 +50,7 @@ local preferred_view
local function command(cmd_table, compiling)
if not buffer.filename then return end
buffer:annotation_clear_all()
- buffer:save()
+ io.save_file()
local command = cmd_table[buffer.filename:match('[^.]+$')]
if not command then return end
if type(command) == 'function' then command = command() end
diff --git a/modules/textadept/session.lua b/modules/textadept/session.lua
index 8ae408fd..b44a777c 100644
--- a/modules/textadept/session.lua
+++ b/modules/textadept/session.lua
@@ -50,7 +50,7 @@ function M.load(filename)
if filename == '' then return end
local not_found = {}
local f = io.open(filename, 'rb')
- if not f then io.close_all() return false end
+ if not f then io.close_all_buffers() return false end
local current_view, splits = 1, {[0] = {}}
local lfs_attributes = lfs.attributes
for line in f:lines() do