Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

# -*- encoding: utf-8 -*- 

r""" 

Emacs sage-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) 2015 Ivan Andrus <darthandrus@gmail.com> 

# 

# Distributed under the terms of the GNU General Public License (GPL) 

# as published by the Free Software Foundation; either version 2 of 

# the License, or (at your option) any later version. 

# http://www.gnu.org/licenses/ 

#***************************************************************************** 

 

import sys 

from sage.repl.rich_output.backend_ipython import BackendIPythonCommandline 

from sage.repl.rich_output.output_catalog import * 

 

 

class BackendEmacs(BackendIPythonCommandline): 

""" 

Emacs Backend 

 

This backend is used by Emacs' sage-mode to have typeset output 

and inline images. 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output.backend_emacs import BackendEmacs 

sage: BackendEmacs() 

Emacs sage-mode 

""" 

 

def _repr_(self): 

r""" 

Return string representation of the backend 

 

OUTPUT: 

 

String. 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output.backend_emacs import BackendEmacs 

sage: backend = BackendEmacs() 

sage: backend._repr_() 

'Emacs sage-mode' 

""" 

return 'Emacs sage-mode' 

 

def default_preferences(self): 

""" 

Return the backend's display preferences 

 

Override this method to change the default preferences when 

using your backend. 

 

OUTPUT: 

 

Instance of 

:class:`~sage.repl.rich_output.preferences.DisplayPreferences`. 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output.backend_emacs import BackendEmacs 

sage: backend = BackendEmacs() 

sage: backend.default_preferences() 

Display preferences: 

* graphics is not specified 

* supplemental_plot is not specified 

* text is not specified 

""" 

from sage.repl.rich_output.preferences import DisplayPreferences 

return DisplayPreferences() 

 

def displayhook(self, plain_text, rich_output): 

""" 

Backend implementation of the displayhook 

 

INPUT: 

 

- ``plain_text`` -- instance of 

:class:`~sage.repl.rich_output.output_basic.OutputPlainText`. The 

plain text version of the output. 

 

- ``rich_output`` -- instance of an output container class 

(subclass of 

:class:`~sage.repl.rich_output.output_basic.OutputBase`). Guaranteed 

to be one of the output containers returned from 

:meth:`supported_output`, possibly the same as 

``plain_text``. 

 

OUTPUT: 

 

Because this is based on the IPython commandline display hook, 

it returns the IPython display data, a pair of 

dictionaries. The first dictionary contains mime types as keys 

and the respective output as value. The second dictionary is 

metadata. 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output.output_basic import OutputPlainText, OutputLatex 

sage: plain_text = OutputPlainText.example() 

sage: from sage.repl.rich_output.backend_emacs import BackendEmacs 

sage: backend = BackendEmacs() 

sage: backend.displayhook(plain_text, plain_text) 

({u'text/plain': 'Example plain text output'}, {}) 

sage: latex_text = OutputLatex.example() 

sage: backend.displayhook(plain_text, latex_text) 

({u'text/plain': 'BEGIN_TEXT:Example plain text output:END_TEXT\nBEGIN_LATEX:\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\int \\sin\\left(x\\right)\\,{d x}:END_LATEX'}, 

{}) 

""" 

 

if isinstance(rich_output, OutputPlainText): 

return ({u'text/plain': rich_output.text.get_str()}, {}) 

elif isinstance(rich_output, OutputAsciiArt): 

return ({u'text/plain': rich_output.ascii_art.get_str()}, {}) 

elif isinstance(rich_output, OutputLatex): 

text = "BEGIN_TEXT:" + plain_text.text.get_str() + ":END_TEXT\nBEGIN_LATEX:" + \ 

rich_output.latex.get_str() + ":END_LATEX" 

return ({u'text/plain': text}, {}) 

 

# TODO: perhaps handle these by returning the data inline, 

# e.g. base64 encoded, so that sage-mode can show inline 

# images for remotely running shells. 

elif isinstance(rich_output, OutputImagePng): 

msg = self.launch_viewer( 

rich_output.png.filename(ext='png'), plain_text.text.get()) 

return ({u'text/plain': msg}, {}) 

elif isinstance(rich_output, OutputImageGif): 

msg = self.launch_viewer( 

rich_output.gif.filename(ext='gif'), plain_text.text.get()) 

return ({u'text/plain': msg}, {}) 

elif isinstance(rich_output, OutputImagePdf): 

msg = self.launch_viewer( 

rich_output.pdf.filename(ext='pdf'), plain_text.text.get()) 

return ({u'text/plain': msg}, {}) 

elif isinstance(rich_output, OutputImageDvi): 

msg = self.launch_viewer( 

rich_output.dvi.filename(ext='dvi'), plain_text.text.get()) 

return ({u'text/plain': msg}, {}) 

elif isinstance(rich_output, OutputSceneJmol): 

msg = self.launch_jmol(rich_output, plain_text.text.get()) 

return ({u'text/plain': msg}, {}) 

elif isinstance(rich_output, OutputSceneWavefront): 

msg = self.launch_sage3d(rich_output, plain_text.text.get()) 

return ({u'text/plain': msg}, {}) 

else: 

raise TypeError('rich_output type not supported')