diff options
author | 2010-09-29 19:44:40 -0400 | |
---|---|---|
committer | 2010-09-29 19:44:40 -0400 | |
commit | bd53e7866f48c64b6f6eea22529a9f98da5eaad6 (patch) | |
tree | 1331f955916bd658148536f37c14cbf281fdcb70 /modules | |
parent | 9e40d2652d45de9ed6e5e9c1d94c76c3f94e9ed4 (diff) | |
download | textadept-bd53e7866f48c64b6f6eea22529a9f98da5eaad6.tar.gz textadept-bd53e7866f48c64b6f6eea22529a9f98da5eaad6.zip |
Added a textadept.snapopen module.
Diffstat (limited to 'modules')
-rw-r--r-- | modules/textadept/init.lua | 1 | ||||
-rw-r--r-- | modules/textadept/menu.lua | 19 | ||||
-rw-r--r-- | modules/textadept/snapopen.lua | 112 |
3 files changed, 132 insertions, 0 deletions
diff --git a/modules/textadept/init.lua b/modules/textadept/init.lua index 01acde58..6bcfbf5d 100644 --- a/modules/textadept/init.lua +++ b/modules/textadept/init.lua @@ -12,6 +12,7 @@ require 'textadept.find' require 'textadept.mime_types' require 'textadept.run' require 'textadept.session' +require 'textadept.snapopen' require 'textadept.snippets' -- These need to be loaded last. diff --git a/modules/textadept/menu.lua b/modules/textadept/menu.lua index b29c781a..fb3dbc9e 100644 --- a/modules/textadept/menu.lua +++ b/modules/textadept/menu.lua @@ -92,6 +92,9 @@ local ID = { CLEAR_BOOKMARKS = 417, GOTO_NEXT_BOOKMARK = 418, GOTO_PREV_BOOKMARK = 419, + SNAPOPEN_USERHOME = 420, + SNAPOPEN_HOME = 421, + SNAPOPEN_CURRENTDIR = 422, -- Buffer NEXT_BUFFER = 501, PREV_BUFFER = 502, @@ -226,6 +229,11 @@ local menubar = { { l.MENU_TOOLS_BM_NEXT, ID.GOTO_NEXT_BOOKMARK }, { l.MENU_TOOLS_BM_PREV, ID.GOTO_PREV_BOOKMARK }, }, + { title = l.MENU_TOOLS_SNAPOPEN_TITLE, + { l.MENU_TOOLS_SNAPOPEN_USERHOME, ID.SNAPOPEN_USERHOME }, + { l.MENU_TOOLS_SNAPOPEN_HOME, ID.SNAPOPEN_HOME }, + { l.MENU_TOOLS_SNAPOPEN_CURRENTDIR, ID.SNAPOPEN_CURRENTDIR }, + }, }, gtkmenu { title = l.MENU_BUF_TITLE, @@ -288,6 +296,7 @@ local b, v = 'buffer', 'view' local m_snippets = _m.textadept.snippets local m_editing = _m.textadept.editing local m_bookmarks = _m.textadept.bookmarks +local m_snapopen = _m.textadept.snapopen local m_run = _m.textadept.run local function set_encoding(encoding) @@ -435,6 +444,16 @@ local actions = { [ID.CLEAR_BOOKMARKS] = { m_bookmarks.clear }, [ID.GOTO_NEXT_BOOKMARK] = { m_bookmarks.goto_next }, [ID.GOTO_PREV_BOOKMARK] = { m_bookmarks.goto_prev }, + -- Tools -> Snapopen + [ID.SNAPOPEN_USERHOME] = { m_snapopen.open, _USERHOME }, + [ID.SNAPOPEN_HOME] = { m_snapopen.open, _HOME }, + [ID.SNAPOPEN_CURRENTDIR] = { + function() + if buffer.filename then + m_snapopen.open(buffer.filename:match('^.+[/\\]')) + end + end + }, -- Buffer [ID.NEXT_BUFFER] = { 'goto_buffer', v, 1, false }, [ID.PREV_BUFFER] = { 'goto_buffer', v, -1, false }, diff --git a/modules/textadept/snapopen.lua b/modules/textadept/snapopen.lua new file mode 100644 index 00000000..c2c867a4 --- /dev/null +++ b/modules/textadept/snapopen.lua @@ -0,0 +1,112 @@ +-- Copyright 2007-2010 Mitchell mitchell<att>caladbolg.net. See LICENSE.
+
+local locale = _G.locale
+
+---
+-- Snapopen for the textadept module.
+module('_m.textadept.snapopen', package.seeall)
+
+-- Markdown
+-- ## Settings
+--
+-- * `PATHS`: Table of default paths to search.
+-- * `DEPTH`: Maximum directory depth to search (defaults to 4).
+--
+-- ## Examples
+--
+-- local snapopen = _m.textadept.snapopen
+--
+-- -- Show all files in PATHS.
+-- snapopen()
+--
+-- -- Show all files in the current file's directory.
+-- snapopen(buffer.filename:match('^.+[/\\]'), nil, true)
+--
+-- -- Show all Lua files in PATHS.
+-- snapopen(nil, '!%.lua$')
+--
+-- -- Ignore the .hg folder in the local Mercurial repository.
+-- local project_dir = '/path/to/project'
+-- snapopen(project_dir, { folders = { '%.hg' } }, true)
+
+-- settings
+PATHS = {}
+DEPTH = 4
+-- end settings
+
+local lfs = require 'lfs'
+
+-- Determines whether or not the given file matches the given filter.
+-- @param file The filename.
+-- @param filter The filter table.
+-- @return boolean true or false.
+local function exclude(file, filter)
+ if not filter then return false end
+ local string_match, string_sub = string.match, string.sub
+ for i = 1, #filter do
+ local patt = filter[i]
+ if string_sub(patt, 1, 1) ~= '!' then
+ if string_match(file, patt) then return true end
+ else
+ if not string_match(file, string_sub(patt, 2)) then return true end
+ end
+ end
+ return false
+end
+
+-- Adds a directory's contents to a list of files.
+-- @param dir The directory to open.
+-- @param list The list of files to add dir's contents to.
+-- @param depth The current depth of nested folders.
+-- @param filter The filter table.
+local function add_directory(dir, list, depth, filter)
+ local string_match, string_gsub = string.match, string.gsub
+ for file in lfs.dir(dir) do
+ if not string_match(file, '^%.%.?$') then
+ file = dir..(not WIN32 and '/' or '\\')..file
+ if lfs.attributes(file).mode == 'directory' then
+ if not exclude(file, filter.folders) and depth < DEPTH then
+ add_directory(file, list, depth + 1, filter)
+ end
+ elseif not exclude(file, filter) then
+ list[#list + 1] = string_gsub(file, '^%.[/\\]', '')
+ end
+ end
+ end
+end
+
+---
+-- Quickly open a file in set of directories.
+-- @param paths A string directory path or table of directory paths to search.
+-- @param filter A filter for files and folders to exclude. The filter may be
+-- a string or table. Each filter is a Lua pattern. Any files matching a
+-- filter are excluded. Prefix a pattern with '!' to exclude any files that
+-- do not match the filter. Directories can be excluded by adding filters to
+-- a table assigned to a 'folders' key in the filter table.
+-- @param exclusive Flag indicating whether or not to exclude PATHS in the
+-- search. Defaults to false.
+-- @usage _m.textadept.snapopen.open()
+-- @usage _m.textadept.snapopen.open(buffer.filename:match('^.+/'), nil, true)
+-- @usage _m.textadept.snapopen.open(nil, '!%.lua$')
+-- @usage _m.textadept.snapopen.open(nil, { folders = { '.hg' } })
+function open(paths, filter, exclusive)
+ if not paths then paths = {} end
+ if type(paths) == 'string' then paths = { paths } end
+ if not filter then filter = {} end
+ if type(filter) == 'string' then filter = { filter } end
+ if not exclusive then
+ for _, path in ipairs(PATHS) do paths[#paths + 1] = path end
+ end
+ local list = {}
+ for _, path in ipairs(paths) do add_directory(path, list, 1, filter) end
+ local out =
+ gui.dialog('filteredlist',
+ '--title', locale.IO_OPEN_TITLE,
+ '--button1', 'gtk-ok',
+ '--button2', 'gtk-cancel',
+ '--no-newline',
+ '--columns', 'File',
+ '--items', unpack(list))
+ local response, index = out:match('^(%d+)[\r\n]+(%d+)')
+ if response == '1' then io.open_file(list[tonumber(index) + 1]) end
+end
|