summary refs log tree commit diff stats
path: root/ranger/defaults/apps.py
blob: 0bb1c5cb3f3c994e0b71b48a3a45a6b8a99db7ad (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# Copyright (c) 2009, 2010 hut <hut@lavabit.com>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

from re import compile, VERBOSE
from ranger.applications import *

INTERPRETED_LANGUAGES = compile(r'''
	^(text|application)\/x-(
		haskell|perl|python|ruby|sh
	)$''', VERBOSE)

class CustomApplications(Applications):
	def app_default(self, c):
		"""How to determine the default application?"""
		f = c.file

		if f.extension is not None:
			if f.extension in ('pdf'):
				return self.app_apvlv(c)
			if f.extension in ('swc', 'smc'):
				return self.app_zsnes(c)

		if INTERPRETED_LANGUAGES.match(f.mimetype):
			return self.app_edit_or_run(c)

		if f.container:
			return self.app_aunpack(c)

		if f.video or f.audio:
			if f.video:
				c.flags += 'd'
			return self.app_mplayer(c)
		
		if f.image:
			return self.app_feh(c)

		if f.document:
			return self.app_editor(c)

	# ----------------------------------------- application definitions
	def app_pager(self, c):
		return tup('less', *c)

	def app_vim(self, c):
		return tup('vim', *c)

	app_editor = app_vim

	def app_edit_or_run(self, c):
		if c.mode is 1:
			return self.app_self(c)
		return self.app_editor(c)

	def app_mplayer(self, c):
		if c.mode is 1:
			return tup('mplayer', *c)

		elif c.mode is 2:
			args = "mplayer -fs -sid 0 -vfm ffmpeg -lavdopts" \
					"lowres=1:fast:skiploopfilter=all:threads=8".split()
			args.extend(c)
			return tup(*args)

		elif c.mode is 3:
			return tup('mplayer', '-mixer', 'software', *c)

		else:
			return tup('mplayer', '-fs', *c)

	def app_feh(self, c):
		arg = {1: '--bg-scale', 2: '--bg-tile', 3: '--bg-center'}

		c.flags += 'd'

		if c.mode in arg:
			return tup('feh', arg[c.mode], c.file.path)
		if c.mode is 4:
			return tup('gimp', *c)
		return tup('feh', *c)

	def app_aunpack(self, c):
		if c.mode is 0:
			c.flags += 'p'
			return tup('aunpack', '-l', c.file.path)
		return tup('aunpack', c.file.path)
	
	def app_apvlv(self, c):
		c.flags += 'd'
		return tup('apvlv', *c)

	def app_make(self, c):
		if c.mode is 0:
			return tup("make")
		if c.mode is 1:
			return tup("make", "install")
		if c.mode is 2:
			return tup("make", "clear")
	
	def app_firefox(self, c):
		return tup("firefox")

	def app_javac(self, c):
		return tup("javac", *c)
	
	def app_java(self, c):
		def strip_extensions(file):
			if '.' in file.basename:
				return file.path[:file.path.index('.')]
			return file.path
		files_without_extensions = map(strip_extensions, c.files)
		return tup("java", files_without_extensions)
	
	def app_zsnes(self, c):
		return tup("zsnes", c.file)
	
	def app_evince(self, c):
		return tup("evince", *c)
	
	def app_wine(self, c):
		return tup("wine", c.file)

	def app_totem(self, c):
		if c.mode is 0:
			return tup("totem", "--fullscreen", *c)
		if c.mode is 1:
			return tup("totem", *c)
="p">] run [ event-loop screen, console, env, resources ] # result turns red screen-should-contain-in-color 1/red, [ . . . . . . . . . . .3 . . . ] ] # this requires tracking a couple more things container sandbox [ response-starting-row-on-screen:num expected-response:text ] # include expected response when saving or restoring a sandbox before <end-save-sandbox> [ { expected-response:text <- get *sandbox, expected-response:offset break-unless expected-response filename <- append filename, [.out] resources <- dump resources, filename, expected-response } ] before <end-restore-sandbox> [ { filename <- append filename, [.out] contents <- slurp resources, filename break-unless contents *curr <- put *curr, expected-response:offset, contents } ] # clicks on sandbox responses save it as 'expected' after <global-touch> [ # check if it's inside the output of any sandbox { sandbox-left-margin:num <- get *current-sandbox, left:offset click-column:num <- get t, column:offset on-sandbox-side?:bool <- greater-or-equal click-column, sandbox-left-margin break-unless on-sandbox-side? first-sandbox:&:sandbox <- get *env, sandbox:offset break-unless first-sandbox first-sandbox-begins:num <- get *first-sandbox, starting-row-on-screen:offset click-row:num <- get t, row:offset below-sandbox-editor?:bool <- greater-or-equal click-row, first-sandbox-begins break-unless below-sandbox-editor? # identify the sandbox whose output is being clicked on sandbox:&:sandbox, sandbox-index:num <- find-click-in-sandbox-output env, click-row break-unless sandbox # update it sandbox <- toggle-expected-response sandbox # minimal update to disk save-sandbox resources, sandbox, sandbox-index # minimal update to screen sandbox-right-margin:num <- get *current-sandbox, right:offset row:num <- render-sandbox-response screen, sandbox, sandbox-left-margin, sandbox-right-margin { height:num <- screen-height screen at-bottom?:bool <- greater-or-equal row, height break-if at-bottom? draw-horizontal screen, row, sandbox-left-margin, sandbox-right-margin } screen <- update-cursor screen, current-sandbox, env loop +next-event } ] def find-click-in-sandbox-output env:&:environment, click-row:num -> sandbox:&:sandbox, sandbox-index:num [ local-scope load-inputs # assert click-row >= sandbox.starting-row-on-screen sandbox:&:sandbox <- get *env, sandbox:offset start:num <- get *sandbox, starting-row-on-screen:offset clicked-on-sandboxes?:bool <- greater-or-equal click-row, start assert clicked-on-sandboxes?, [extract-sandbox called on click to sandbox editor] # while click-row < sandbox.next-sandbox.starting-row-on-screen sandbox-index <- copy 0 { next-sandbox:&:sandbox <- get *sandbox, next-sandbox:offset break-unless next-sandbox next-start:num <- get *next-sandbox, starting-row-on-screen:offset found?:bool <- lesser-than click-row, next-start break-if found? sandbox <- copy next-sandbox sandbox-index <- add sandbox-index, 1 loop } # return sandbox if click is in its output region response-starting-row:num <- get *sandbox, response-starting-row-on-screen:offset return-unless response-starting-row, null/no-click-in-sandbox-output, 0/sandbox-index click-in-response?:bool <- greater-or-equal click-row, response-starting-row return-unless click-in-response?, null/no-click-in-sandbox-output, 0/sandbox-index return sandbox, sandbox-index ] def toggle-expected-response sandbox:&:sandbox -> sandbox:&:sandbox [ local-scope load-inputs expected-response:text <- get *sandbox, expected-response:offset { # if expected-response is set, reset break-unless expected-response *sandbox <- put *sandbox, expected-response:offset, null } { # if not, set expected response to the current response break-if expected-response response:text <- get *sandbox, response:offset *sandbox <- put *sandbox, expected-response:offset, response } ] # when rendering a sandbox, color it in red/green if expected response exists after <render-sandbox-response> [ { break-unless sandbox-response *sandbox <- put *sandbox, response-starting-row-on-screen:offset, row row <- render-sandbox-response screen, sandbox, left, right jump +render-sandbox-end } ] def render-sandbox-response screen:&:screen, sandbox:&:sandbox, left:num, right:num -> row:num, screen:&:screen [ local-scope load-inputs sandbox-response:text <- get *sandbox, response:offset expected-response:text <- get *sandbox, expected-response:offset row:num <- get *sandbox response-starting-row-on-screen:offset { break-if expected-response row <- render-text screen, sandbox-response, left, right, 245/grey, row return } response-is-expected?:bool <- equal expected-response, sandbox-response { break-if response-is-expected? row <- render-text screen, sandbox-response, left, right, 1/red, row } { break-unless response-is-expected?:bool row <- render-text screen, sandbox-response, left, right, 2/green, row } ] before <end-render-sandbox-reset-hidden> [ *sandbox <- put *sandbox, response-starting-row-on-screen:offset, 0 ]