From fe9a0809434460785bbddca318276ab9a7431bf3 Mon Sep 17 00:00:00 2001 From: Laas Toom Date: Wed, 4 Feb 2015 17:51:06 +0200 Subject: [PATCH] run_editor: open $VISUAL or $EDITOR at current slide location --- README.md | 2 ++ bin/tkn | 22 +++++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index dfc8302..d18b408 100644 --- a/README.md +++ b/README.md @@ -157,6 +157,8 @@ There is one hard-coded visual effect: Once the exact characters of a given slid * Toggle status: "s". +* Edit deck with $VISUAL or $EDITOR or vim. If editor supports, jump to current slide location: "v" + * Quit: "q". Searching accepts a regular expression. A nice trick to enable a modifier in a particular search is to use the diff --git a/bin/tkn b/bin/tkn index d5e1c71..788900b 100755 --- a/bin/tkn +++ b/bin/tkn @@ -36,10 +36,16 @@ def section(title) end def slide(content, format, *args) + # Find out line number where this slide is defined + # account for code, block, center etc that insert one line + loc = caller.first =~ /^#{__FILE__}/ ? caller[1] : caller[0] + line = /[^:]+:(\d+):in/.match(loc).captures.first + $slides << { content: content.strip_heredoc, format: format, - args: args + args: args, + line: line } end @@ -242,6 +248,16 @@ def toggle_status(status, n, total) !status end +def run_editor(deck,slide) + editor = ENV['VISUAL'] + editor = ENV['EDITOR'] if !editor || editor.empty? + editor = %x(which vim).chomp if !editor || editor.empty? + # if no editor found, can't do much + return if !editor || editor.empty? + # Joe, nano and vi(m) support opening file at given line number + linenum = editor =~ /joe|nano|vi/ ? "+#{slide[:line]}" : "" + system("#{editor} #{linenum} #{deck}") +end # # --- Main Loop ------------------------------------------------------- @@ -313,6 +329,10 @@ loop do n = search_for(n) when 's' status = toggle_status(status, n, $slides.size) + when 'v' + clear_slide(slide) + run_editor(deck,slide) + clear_slide(slide) when 'q' clear_slide(slide) exit