Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

run_editor: open $VISUAL or $EDITOR at current slide location #12

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 21 additions & 1 deletion bin/tkn
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 -------------------------------------------------------
Expand Down Expand Up @@ -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
Expand Down