Skip to content

Commit

Permalink
Extensions: make foo.bar allow editing menu's contents.
Browse files Browse the repository at this point in the history
  • Loading branch information
trogdoro committed Nov 19, 2013
1 parent 8805da0 commit 8a21c20
Show file tree
Hide file tree
Showing 11 changed files with 344 additions and 172 deletions.
37 changes: 30 additions & 7 deletions lib/xiki/core/expander.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ def self.menu
# will we add stuff to the hash based on definitions or menu
# dirs.
#
# Expander.parse("a").should == {:name=>"a"}
# This stuff this method extracts should probably be limited
# to elements that determine which expander to use, or are of
# interest to multiple expanders.
#
# Expander.parse("a").should == {:name=>"a", :path=>"a"}
# Expander.parse("/tmp").should == {:file_path=>"/tmp"}
# Expander.parse(:foo=>"bar").should == {:foo=>"bar"}
def self.parse *args
Expand Down Expand Up @@ -125,11 +129,18 @@ def self.parse *args

# If menu-like, extract menu and items and return...

if thing =~ /^[\w _-]+(\/.*|$)/
if thing =~ /^[\w _.-]+(\/|$)/

path_items = Path.split thing # Split into items

options[:name] = Menu.format_name path_items.shift # First arg is name, so pull it off

# Split off extension if any...

extension = options[:name].slice! /\..*/
options[:extension] = extension if extension


# Store original path, since it could be a pattern, and set items extracted from path...

if ! options[:items]
Expand Down Expand Up @@ -188,7 +199,6 @@ def self.parse *args
# Expander.expand "ip" # => "192.0.0.1"
# @expander/docs/expanding/
def self.expand *args

options = nil

# If 1st arg is just a hash with sources, we're being called again so don't re-parse and re-find expands
Expand Down Expand Up @@ -237,15 +247,19 @@ def self.expand *args
txt = options[:output]

if options[:client] == "web" && txt !~ /^\s*<html[ >]/i
txt = Xiki::Html.to_html txt, options
if options[:extension]
txt = "extension but web"
else
txt = Xiki::Html.to_html txt, options
end
end

txt
end


# Adds :expander=>TheClass to options that says it .expands? this path (represented by the options).
# Expander.expanders(:name=>"foo").should == "guess"
# Expander.expanders(:name=>"foo")[:expanders].should == [Xiki::Menu]
# Expander.expanders(:file_path=>"/tmp/").should == {:file_path=>"/tmp/", :expander=>FileTree}
# Expander.expanders("a").should == "guess"
def self.expanders *args
Expand Down Expand Up @@ -410,10 +424,19 @@ def self.delegate_to_keys args, options, block
Launcher.open "#{file}\n #{menu}", options
}
elsif menu =~ /^\.@/ # If menu is .@foo..., nest under current file

# For key shortcuts, if .@..., use current file, or view name

menu.sub! /^\./, ''
lambda{
->{
bm = Keys.input :optional=>1, :prompt=>"Enter bookmark, or pause for current file." # Terminated by pause
file = bm ? Keys.bookmark_as_path(:bm=>bm, :include_file=>1) : View.file

# Nest under... tree path if we're in one, else view path, else buffer name...

ancestor = FileTree.grab_file_on_line
ancestor ||= View.file || View.name

file = bm ? Keys.bookmark_as_path(:bm=>bm, :include_file=>1) : ancestor
Launcher.open "#{file}\n #{menu}", options
}
else
Expand Down
153 changes: 42 additions & 111 deletions lib/xiki/core/launcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,8 @@ def self.launch_by_proc list=nil

def self.init_default_launchers

# TODO > Deprecated - only keep this around until shell stuff has been ported over sufficiently!"]

# %\n | multiline\n | commands
Launcher.add /^\%\// do # For % with nested quoted lines
path = Tree.construct_path :list=>1
Expand All @@ -553,7 +555,7 @@ def self.init_default_launchers
orig.go
end

self.add(/^\*$/) do |line| # *... buffer
self.add(/^\*$/) do |line| # *
Line.sub! /.+/, "all"

Launcher.launch
Expand Down Expand Up @@ -733,6 +735,8 @@ def self.do_last_launch options={}
Tree.to_parent(:u) :
Tree.to_parent
end

Tree.to_parent if Line =~ /^ +- backtrace:$/ # If we went to "- backtrace:", go up again
Tree.kill_under
end

Expand Down Expand Up @@ -1110,107 +1114,6 @@ def self.wrapper path

end

def self.wrapper_rb dir, file, path
output = Console.run "ruby #{Xiki.dir}/etc/wrappers/wrapper.rb #{file} \"#{path}\"", :sync=>1, :dir=>dir

# Sensible thing for now is to just do literal output
# output = Tree.children output, path if path !~ /^\./

# How to know when to do children?!
# Because it called .menu, and menu had no args
# Make it set env var?

# output = Tree.children output, path

Tree << output
end

def self.wrapper_js dir, file, path
output = Console.run "node #{Xiki.dir}etc/wrappers/wrapper.js \"#{dir}#{file}\" \"#{path}\"", :sync=>1, :dir=>dir
output = Tree.children output, path
Tree << output
end

def self.wrapper_coffee dir, file, path
txt = CoffeeScript.to_js("#{dir}#{file}")
tmp_file = "/tmp/tmp.js"
File.open(tmp_file, "w") { |f| f << txt }

output = Console.run "node #{Xiki.dir}etc/wrappers/wrapper.js \"#{tmp_file}\" \"#{path}\"", :sync=>1, :dir=>dir
output = Tree.children output, path
Tree << output
end

def self.wrapper_notes dir, file, path
if match = path.match(/^(\| .+)(\| .*)/)
heading, content = match[1..2]
# [nil, nil])[1..2]
else
heading, content = [path, nil]
end

heading = nil if heading.blank?

# heading, content = (path.match(/^(\| .+)(\| .*)/) || [nil, nil])[1..2]

dir = "#{dir}/" if dir !~ /\/$/
output = Notes.drill "#{dir}#{file}", heading, content
Tree << output
end

def self.wrapper_menu dir, file, path
heading, content = (path.match(/^(\| .+)(\| .*)?/) || [nil, nil])[1..2]

# output = Menu.drill "#{dir}/#{file}", heading, content

# output = Tree.children File.read(file), Tree.rootless(path)
output = Tree.children File.read("#{Bookmarks[dir]}/#{file}"), path

Tree << output
end

def self.wrapper_py dir, file, path
output = Console.run "python #{Xiki.dir}etc/wrappers/wrapper.py \"#{dir}#{file}\" \"#{path}\"", :sync=>1, :dir=>dir
output = Tree.children output, path if path !~ /^\./
Tree << output
end

def self.wrapper_haml dir, file, path

engine = Haml::Engine.new(File.read "#{dir}#{file}")

foos = ["foo1", "foo2", "foo3"]
o = Object.new
o.instance_eval do
@foo = "Foooo"
@foos = foos
end

txt = engine.render(o, "foo"=>"Fooooooo", "foos"=>foos)

Tree << Tree.quote(txt)
end

def self.wrapper_rakefile dir, file, path

# If just file passed, show all tasks

if path.blank?
txt = Console.sync "rake -T", :dir=>dir

txt = txt.scan(/^rake (.+?) *#/).flatten

Tree << txt.map{|o| "- #{o}/\n"}.join
return
end

# Task name passed, so run it

path.sub! /\/$/, ''
Console.run "rake #{path}", :dir=>dir
nil
end

def self.reload_menu_dirs
dir = "#{Xiki.dir}lib/xiki/tools"
Files.in_dir(dir).each do |f|
Expand Down Expand Up @@ -1382,7 +1285,7 @@ def self.launch insert_options={}
# - pattern.rb
# | if nested = value[options[key]]
if view = View.name; options[:target_view] = view; end
if file = View.file; options[:extension] = File.extname(file); end
if file = View.file; options[:target_extension] = File.extname(file); end

# # Stopping doing this - anyone still relying on it?!
# # - Fix conf!
Expand All @@ -1409,11 +1312,12 @@ def self.launch insert_options={}
# Automatically repress slash if were on ^... or |... line
insert_options[:no_slash] = 1 if options[:args] && options[:args].last =~ /(^[>|:]|\n)/

return if self.process_directives txt, insert_options
return if self.process_directives txt, options, insert_options

txt = txt.to_s if txt
return if txt.blank?

$el.deactivate_mark # So aquamacs doesn't highlight something after double-click
Tree.<< txt, insert_options

nil
Expand All @@ -1422,6 +1326,8 @@ def self.launch insert_options={}

def self.expand_again_if_beg txt, options

return txt if ! txt.is_a? String

return txt if txt !~ /\A@beg\/(.+)\/\z/ # Only try to do something if menu returned @beg/.../

beg = $1
Expand Down Expand Up @@ -1465,7 +1371,7 @@ def self.expand_again_if_beg txt, options
# Called by .launch to do appriate thing if result starts
# with @open file/, @flash/, or something else that instructs the editor to
# take an action.
def self.process_directives txt, options
def self.process_directives txt, options, insert_options

# If "<<< foo", launch foo in place of parent...

Expand All @@ -1491,15 +1397,15 @@ def self.process_directives txt, options
elsif arg == "quoted/"
Ol["todo: implement!"]
else
left, right = Tree.sibling_bounds :cross_blank_lines=>1
left, right = Line.left, Line.right+1
end

old_txt = View.txt left, right
indent = old_txt[/^ */]
View.delete left, right
txt = "#{txt}\n".gsub /^/, indent
txt = "#{txt}".gsub(/^/, indent)

Tree.output_and_search txt, options.merge(:not_under=>1)
Tree.output_and_search txt, insert_options.merge(:not_under=>1)

return true

Expand All @@ -1519,7 +1425,9 @@ def self.process_directives txt, options
end
end

View.open txt
open_options = options[:prefix] == 0 ? {:same_view=>1} : {} # 0+ means use same view

View.open txt, open_options

if line_number
View.line = line_number
Expand Down Expand Up @@ -1575,6 +1483,27 @@ def self.insert_menu

prefix = Keys.prefix

# /foo/
# @bar/
if prefix == 2
Line << "#{Keys.bookmark_as_path}"
Line << "\n @"
menu = Keys.input :timed=>1
menu = "" if menu == " "
Line << "#{menu}"
Launcher.go
return
end

# /foo/
# @
if prefix == 8
Line << "#{Keys.bookmark_as_path}"
Line << "\n @"
Launcher.go
return
end

if prefix == :u # Insert @last to see recent menu names and drill in.
Line << "$#{Keys.input :timed=>1}//"
Launcher.go
Expand All @@ -1595,7 +1524,9 @@ def self.insert_menu

# Todo: if dash+, do auto-complete even if exact match - how to implement?

input = Keys.input(:timed=>true, :prompt=>"Start typing a menu that might exist (or type 'all'): ")
prompt = "Start typing a menu ('a' for all): "
prompt.sub! ")", ", space for suggestions)" if ! blank
input = Keys.input(:timed=>true, :prompt=>prompt)

# If space, they want to do just raw "@", which will suggest something based on parent
input = "" if input == " "
Expand Down Expand Up @@ -1661,4 +1592,4 @@ def require_menu file, options={}
Xiki::Launcher.add stem if ! Xiki::Launcher.menus[1][stem]
end

Xiki::Launcher.init_default_launchers
#Xiki::Launcher.init_default_launchers
Loading

0 comments on commit 8a21c20

Please sign in to comment.