aboutsummaryrefslogtreecommitdiffhomepage
path: root/core
diff options
context:
space:
mode:
authorGravatar mitchell <70453897+667e-11@users.noreply.github.com>2014-03-27 13:45:29 -0400
committerGravatar mitchell <70453897+667e-11@users.noreply.github.com>2014-03-27 13:45:29 -0400
commit7785897657cd9c3a5e1483ebacad73939f375a7e (patch)
treeb168e4ec9c72248be21841807177870a07f32659 /core
parentf65b2b2a66f05b20010256ca1d81cc3252ea1471 (diff)
Added basic project support for snapopen and build scripts.
Also fixed some curses errors introduced by the last commit.
Diffstat (limited to 'core')
-rw-r--r--core/file_io.lua64
-rw-r--r--core/init.lua15
-rw-r--r--core/ui.lua6
3 files changed, 77 insertions, 8 deletions
diff --git a/core/file_io.lua b/core/file_io.lua
index a313cc66..6e82f66d 100644
--- a/core/file_io.lua
+++ b/core/file_io.lua
@@ -358,9 +358,46 @@ function io.open_recent_file()
if button == 1 and i then io.open_file(io.recent_files[i]) end
end
+-- List of version control directories.
+local vcs = {'.bzr', '.git', '.hg', '.svn', 'CVS'}
+
+---
+-- Returns the root directory of the project that contains filesystem path
+-- *path*.
+-- @param path Optional filesystem path to a project or a file contained within
+-- a project. The default value is the buffer's filename or the current
+-- working directory.
+-- @return string root
+-- @name get_project_root
+function io.get_project_root(path)
+ local root
+ local lfs_attributes = lfs.attributes
+ local dir = path or (buffer.filename or lfs.currentdir()):match('^(.+)[/\\]')
+ while dir do
+ for i = 1, #vcs do
+ if lfs_attributes(dir..'/'..vcs[i], 'mode') == 'directory' then
+ if vcs[i] ~= '.svn' and vcs[i] ~= 'CVS' then return dir end
+ root = dir
+ break
+ end
+ end
+ dir = dir:match('^(.+)[/\\]')
+ end
+ return root
+end
+
+---
+-- Map of file paths to filters used by `io.snapopen()`.
+-- @class table
+-- @name snapopen_filters
+-- @see snapopen
+io.snapopen_filters = {}
+
---
-- Prompts the user to select files to be opened from *paths*, a string
-- directory path or list of directory paths, using a filtered list dialog.
+-- If *paths* is `nil`, uses the current project's root directory, which is
+-- obtained from `io.get_project_root()`.
-- Files shown in the dialog do not match any pattern in either string or table
-- *filter* or, unless *exclude_FILTER* is `true`, in `lfs.FILTER`. A filter
-- table contains Lua patterns that match filenames to exclude, an optional
@@ -368,15 +405,22 @@ end
-- and an optional `extensions` sub-table that contains raw file extensions to
-- exclude. Any patterns starting with '!' exclude files and directories that do
-- not match the pattern that follows. The number of files in the list is capped
--- at `SNAPOPEN_MAX`.
+-- at `SNAPOPEN_MAX`. If *filter* is `nil` and *paths* is ultimately a string,
+-- the filter from the `io.snapopen_filters` table is used. In that case, unless
+-- explicitly specified, *exclude_FILTER* becomes `true`.
-- *opts* is an optional table of additional options for
-- `ui.dialogs.filteredlist()`.
--- @param paths String directory path or table of directory paths to search.
--- @param filter Optional filter for files and directories to exclude.
+-- @param paths Optional string directory path or table of directory paths to
+-- search. The default value is the current project's root directory, if
+-- available.
+-- @param filter Optional filter for files and directories to exclude. The
+-- default value comes from `io.snapopen_filters` if *paths* is a string.
-- @param exclude_FILTER Optional flag indicating whether or not to exclude the
-- default filter `lfs.FILTER` in the search. If `false`, adds `lfs.FILTER` to
-- *filter*.
--- The default value is `false` to include the default filter.
+-- Normally, the default value is `false` to include the default filter.
+-- However, in the instances where *filter* comes from `io.snapopen_filters`,
+-- the default value is `true`.
-- @param opts Optional table of additional options for
-- `ui.dialogs.filteredlist()`.
-- @usage io.snapopen(buffer.filename:match('^.+/')) -- list all files in the
@@ -385,12 +429,22 @@ end
-- directory
-- @usage io.snapopen('/project', {folders = {'build'}}) -- list all source
-- files in a project directory
+-- @see io.snapopen_filters
-- @see lfs.FILTER
-- @see SNAPOPEN_MAX
-- @see ui.dialogs.filteredlist
-- @name snapopen
function io.snapopen(paths, filter, exclude_FILTER, opts)
- if type(paths) == 'string' then paths = {paths} end
+ if not paths then paths = io.get_project_root() end
+ if type(paths) == 'string' then
+ if not filter then
+ filter = io.snapopen_filters[paths]
+ if filter and type(exclude_FILTER) == "nil" then
+ exclude_FILTER = filter ~= lfs.FILTER
+ end
+ end
+ paths = {paths}
+ end
local utf8_list = {}
for i = 1, #paths do
lfs.dir_foreach(paths[i], function(file)
diff --git a/core/init.lua b/core/init.lua
index df4a5e38..2d540018 100644
--- a/core/init.lua
+++ b/core/init.lua
@@ -16,6 +16,17 @@ keys = require('keys')
_M = {} -- language modules table
-- LuaJIT compatibility.
if jit then module, package.searchers, bit32 = nil, package.loaders, bit end
+-- curses compatibility.
+if CURSES then
+ function spawn(argv, working_dir, stdout_cb, stderr_cb, exit_cb)
+ local current_dir = lfs.currentdir()
+ lfs.chdir(working_dir:iconv(_CHARSET, 'UTF-8'))
+ local p = io.popen(argv:iconv(_CHARSET, 'UTF-8')..' 2>&1')
+ stdout_cb(p:read('*all'))
+ exit_cb(select(3, p:close()))
+ lfs.chdir(current_dir)
+ end
+end
--[[ This comment is for LuaDoc.
---
@@ -128,6 +139,8 @@ local timeout
---
-- Spawns an interactive child process *argv* in a separate thread with the help
-- of GLib.
+-- The terminal version spawns processes in the same thread and does not use
+-- GLib.
-- @param argv A UTF-8-encoded command line string containing the program's name
-- followed by arguments to pass to it. `PATH` is searched for program names.
-- @param working_dir The child's UTF-8 current working directory (cwd) or `nil`
@@ -136,6 +149,8 @@ local timeout
-- of standard output read from the child. Stdout is read asynchronously in
-- 1KB or 0.5KB blocks (depending on the platform), or however much data is
-- available at the time. All text is encoded in `_CHARSET`.
+-- The terminal version sends all output, whether it be stdout or stderr to
+-- this callback.
-- @param stderr_cb A Lua function that accepts a string parameter for a block
-- of standard error read from the child. Stderr is read asynchronously in 1KB
-- or 0.5kB blocks (depending on the platform), or however much data is
diff --git a/core/ui.lua b/core/ui.lua
index 6ebd28b1..3aa30855 100644
--- a/core/ui.lua
+++ b/core/ui.lua
@@ -28,7 +28,7 @@ local ui = ui
-- The default value is `true`.
-- @field SILENT_PRINT (bool)
-- Whether or not to print messages to buffers silently.
--- The default value is `false`, and focuses buffers when messages are printed
+-- The default value is `false`, and focuses buffers when messages are printed
-- to them.
module('ui')]]
@@ -94,8 +94,8 @@ ui.dialogs = setmetatable({}, {__index = function(t, k)
return function(options)
if not options.button1 then options.button1 = _L['_OK'] end
local select = options.select
- if type(select) == 'number' then
- options.select = select - 1
+ if type(select) == 'number' then
+ options.select = select - 1
elseif type(select) == 'table' then
for i = 1, #select do select[i] = select[i] - 1 end
end