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

File pager #7

Open
wants to merge 4 commits into
base: master
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
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ piped data, and sends it to emacs.

In your `.bashrc` or `.zshrc` file, put something like this


if [ $INSIDE_EMACS ]; then
export PAGER="emacs-pipe"
export PAGER="emacs-pager"
elif [ -x "`which less`" ]; then
export PAGER="`which less`"
export LESS="-isR"
Expand Down
50 changes: 37 additions & 13 deletions emacs-pager
Original file line number Diff line number Diff line change
@@ -1,17 +1,41 @@
#!/usr/bin/env ruby
#!/usr/bin/env python

require 'digest/md5'
require 'fileutils'
import tempfile
import subprocess
import sys

input = ARGF.read
file = "/tmp/#{Digest::MD5.hexdigest input}.emacs-pager"
def stdin_pager():
'''Reads everything from standard input, saves it to a temporary file, and
fires up emacsclient on that temporary file, taking care to clean it up
when emacsclient exits for any reason.'''
try:
with tempfile.NamedTemporaryFile(suffix='.emacs-pager', mode='w+b') as f:
bytes = sys.stdin.read()
f.write(bytes)
f.flush()
return subprocess.call('emacsclient %s' % f.name, shell=True)
except KeyboardInterrupt, e:
print >> sys.stderr, 'Interrupted'
sys.exit(74) # EX_IOERR from sysexits.h
return 1 # Normally, this should *not* be reached.

File.open(file, 'w') do |f|
f.write(input)
end
def file_pager(file_name):
'''Fires up an emacsclient with the file_name open in 'view-mode'.'''
try:
return subprocess.call(
'emacsclient -t --eval "(view-file \\"%s\\")"' % (file_name),
shell=True)
except KeyboardInterrupt, e:
print >> sys.stderr, 'Interrupted'
sys.exit(74) # EX_IOERR from sysexits.h
return 1 # Normally, this should *not* be reached.

puts 'reading into emacs...'

`emacsclient #{file}`

FileUtils.rm(file)
if __name__ == '__main__':
args = sys.argv[1:]
status = 0
if len(args) > 0:
for fn in args:
status = status or file_pager(fn)
else:
status = status or stdin_pager()
sys.exit(status)