-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
emacs_sage_shell_view.py
72 lines (53 loc) · 2.54 KB
/
emacs_sage_shell_view.py
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
r"""
Emacs sage-shell-mode Backend for the Sage Rich Output System
This module defines the Emacs backend for :mod:`sage.repl.rich_output`
based on the IPython shell version.
"""
# Copyright (C) 2016 - 2018 Sho Takemori <[email protected]>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from emacs_sage_shell import ip
from sage.repl.rich_output.output_basic import OutputLatex, OutputPlainText
from sage.repl.rich_output.output_browser import OutputHtml
from sage.repl.rich_output.output_catalog import OutputImagePng
from sage.repl.rich_output.preferences import DisplayPreferences
from sage.repl.rich_output.backend_ipython import BackendIPythonCommandline
class BackendEmacs(BackendIPythonCommandline):
def __init__(self, text=True, plot=True):
super().__init__()
if text:
self.__text = "latex"
else:
self.__text = None
self.__plot = plot
def default_preferences(self):
return DisplayPreferences(text=self.__text)
def supported_output(self):
return [OutputLatex, OutputPlainText, OutputHtml, OutputImagePng]
def _repr_(self):
return "Emacs babel"
def displayhook(self, plain_text, rich_output):
if self.__plot and isinstance(rich_output, OutputImagePng):
msg = rich_output.png.filename(ext='png')
msg = "BEGIN_PNG:%s:END_PNG" % msg
return ({'text/plain': msg}, {})
if isinstance(rich_output, OutputHtml):
text = "BEGIN_TEXT:" + str(plain_text.text.get(), 'utf-8')
text += ":END_TEXTBEGIN_LATEX:"
text += str(rich_output.latex.get(), 'utf-8') + ":END_LATEX"
return ({'text/plain': text}, {})
return super().displayhook(plain_text, rich_output)
def set_backend(text=True, plot=True):
if text or plot:
backend = BackendEmacs(text=text, plot=plot)
else:
backend = BackendIPythonCommandline()
backend.get_display_manager().switch_backend(backend, shell=ip)