From 237aae32a944f31ff2d1cf712a26136c18794c68 Mon Sep 17 00:00:00 2001 From: Emmanuel Charpentier Date: Sun, 9 Apr 2023 20:34:09 +0200 Subject: [PATCH 1/2] Re-enable typeset output in Emacs' *Sage* buffer. This patch adapts sage-shell-mode to recent pmodification of Sages' rich output generation, and filters spurious math delimiters. --- emacs_sage_shell_view.py | 8 ++++++-- sage-shell-view.el | 5 ++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/emacs_sage_shell_view.py b/emacs_sage_shell_view.py index 81203f6..719643e 100644 --- a/emacs_sage_shell_view.py +++ b/emacs_sage_shell_view.py @@ -23,7 +23,8 @@ # along with this program. If not, see . from emacs_sage_shell import ip -from sage.repl.rich_output.output_basic import OutputLatex +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 @@ -43,6 +44,9 @@ def __init__(self, text=True, plot=True): def default_preferences(self): return DisplayPreferences(text=self.__text) + def supported_output(self): + return [OutputLatex , OutputPlainText , OutputHtml , OutputImagePng] + def _repr_(self): return "Emacs babel" @@ -52,7 +56,7 @@ def displayhook(self, plain_text, rich_output): msg = "BEGIN_PNG:%s:END_PNG" % msg return ({u'text/plain': msg}, {}) - elif isinstance(rich_output, OutputLatex): + elif isinstance(rich_output, OutputHtml): text = "BEGIN_TEXT:" + str(plain_text.text.get(), 'utf-8') + ":END_TEXTBEGIN_LATEX:" + \ str(rich_output.latex.get(), 'utf-8') + ":END_LATEX" return ({'text/plain': text}, {}) diff --git a/sage-shell-view.el b/sage-shell-view.el index 8a46e8c..e2cfd3d 100644 --- a/sage-shell-view.el +++ b/sage-shell-view.el @@ -207,7 +207,10 @@ computes the resolution automatically." sage-shell-view-latex-documentclass sage-shell-view-latex-preamble sage-shell-view-latex-math-environment - math-expr + ;; math-expr + (replace-regexp-in-string + "^\$+" "" + (replace-regexp-in-string "\$+$" "" math-expr)) sage-shell-view-latex-math-environment)) (defun sage-shell-view-dir-name () From f394754ebca227d1959f991069ab86a613350b6d Mon Sep 17 00:00:00 2001 From: Emmanuel Charpentier Date: Sat, 10 Jun 2023 10:27:15 +0200 Subject: [PATCH 2/2] Reintegrate Alessandro Presta's fix to typesetting, somehow dropped. [ Cosmetic ] Reformatted docstring >80 characters wide. --- sage-shell-blocks.el | 4 ++-- sage-shell-mode.el | 37 ++++++++++++++++++++++++------------- sage-shell-view.el | 27 ++++++++++++++++++--------- 3 files changed, 44 insertions(+), 24 deletions(-) diff --git a/sage-shell-blocks.el b/sage-shell-blocks.el index 9c1da1f..ffc9460 100644 --- a/sage-shell-blocks.el +++ b/sage-shell-blocks.el @@ -38,8 +38,8 @@ Note that '^' to match at the beginning of the line should not be added to `sage-shell-blocks:delimiter'. -Strange behaviour might arise if `sage-shell-blocks:delimiter' matches multiple lines -at a time." +Strange behaviour might arise if `sage-shell-blocks:delimiter' matches +multiple lines at a time." :type 'string :group 'sage-shell) diff --git a/sage-shell-mode.el b/sage-shell-mode.el index 47df154..fe60efc 100644 --- a/sage-shell-mode.el +++ b/sage-shell-mode.el @@ -87,7 +87,8 @@ (defcustom sage-shell:input-history-cache-file nil - "If non nil, then `comint-input-ring' is saved to this file when the Sage process exits." + "If non nil, then `comint-input-ring' is saved to this file +when the Sage process exits." :group 'sage-shell :type '(choice (file :tag "file") (const :tag "Off" nil))) @@ -186,32 +187,37 @@ If the value is equal to '(\"\"), then it does not ignore anything." :type '(repeat string)) (defcustom sage-shell-edit:temp-file-header "# -*- coding: utf-8 -*-\n" - "`sage-shell-edit:send-region', `sage-shell-edit:send-buffer' and related commands use a temporary file. + "`sage-shell-edit:send-region', `sage-shell-edit:send-buffer' and related +commands use a temporary file. This string will be inserted to the temporary file before evaluating code." :type 'string :group 'sage-shell) (defcustom sage-shell:use-prompt-toolkit nil - "Non `nil' means the Sage process uses the new prompt of IPython 5 and 6. Must be disabled for Ipython >=7, which is incompatible" + "Non `nil' means the Sage process uses the new prompt of IPython 5 and 6. +Must be disabled for Ipython >=7, which is incompatible" :type 'boolean :group 'sage-shell) ;; (make-variable-buffer-local 'sage-shell:use-prompt-toolkit) (defcustom sage-shell:use-simple-prompt t - "Non `nil' means the Sage process must be started with the `simple-prompt' option and use `readline' to interact with IPython >=7." + "Non `nil' means the Sage process must be started with the `simple-prompt' +option and use `readline' to interact with IPython >=7." :type 'boolean :group 'sage-shell) (defcustom sage-shell:check-ipython-version-on-startup t - "Non `nil' means check if `sage-shell:use-prompt-toolkit' and `sage-shell:simple-prompt' are correctly set when starting the Sage process. + "Non `nil' means check if `sage-shell:use-prompt-toolkit' and +`sage-shell:simple-prompt' are correctly set when starting the Sage process. The checking is done asyncally." :type 'boolean :group 'sage-shell) (defcustom sage-shell:set-ipython-version-on-startup t - "Non `nil' means set `sage-shell:use-prompt-toolkit' and `sage-shell:simple-prompt' according to the available IPython version. + "Non `nil' means set `sage-shell:use-prompt-toolkit' and +`sage-shell:simple-prompt' according to the available IPython version. This (synchronous) setting can be replaced by setting variables in the init file.") (defcustom sage-shell-sagetex:pre-latex-command @@ -1146,7 +1152,8 @@ When sync is nil this return a lambda function to get the result." " To disable this checking, set `sage-shell:check-ipython-version-on-startup' to `nil'."))) (defcustom sage-shell:shell-command "/bin/sh" - "Path of a command that behaves like a shell. It should accept -c option. This variable is used only when `sage-shell:use-prompt-toolkit' is non-`nil'." + "Path of a command that behaves like a shell. It should accept -c option. +This variable is used only when `sage-shell:use-prompt-toolkit' is non-`nil'." :type 'string :group 'sage-shell) @@ -1286,8 +1293,8 @@ if [ $1 = .. ]; then shift; fi; exec \"$@\"" (switch-function 'switch-to-buffer) buffer-name) "Running Sage function internal. -SIWTCH-FUNCTION is 'no-switch, or a function with one -argument. If buffer-name is non-nil, it will be the buffer name of the process buffer." +SWITCH-FUNCTION is 'no-switch, or a function with one argument. +If buffer-name is non-nil, it will be the buffer name of the process buffer." (let ((buf (get-buffer-create (if (stringp buffer-name) buffer-name (sage-shell:shell-buffer-name new)))) @@ -1347,7 +1354,8 @@ function asks which process is to be restarted." (process-send-eof proc)))) (defun sage-shell:set-ipython-version (&optional startup-msg) - "Set `sage-shell:use-prompt-toolkit{ and `sage-shell:use-simple-prompt' according to the IPython version used by Sage." + "Set `sage-shell:use-prompt-toolkit{ and `sage-shell:use-simple-prompt' +according to the IPython version used by Sage." (message (concat "Setting up sage-shell according to your Sage's IPython version... " @@ -1375,7 +1383,8 @@ function asks which process is to be restarted." '(sage-shell:use-simple-prompt t)))))) (defun sage-shell:check-ipython-version (&optional startup-msg) - "Check IPython version and check if sage-shell:use-prompt-toolkit is correctly set." + "Check IPython version and check if sage-shell:use-prompt-toolkit is +correctly set." (interactive) (message "Checking IPython version...") (deferred:$ @@ -4105,7 +4114,8 @@ This function set the command list by using `sage-shell-cpl:set-cmd-lst'" (lambda (_proc _event) (message "Scanning Magma types ... Done! (%d seconds)\n Saving cache to -'%s' for future instant use\n. Delete the above file to force re-creation of the cache." +'%s' for future instant use\n. Delete the above file to force re-creation of +the cache." (- (cadr (current-time)) time) (sage-shell-interfaces:get "magma" 'cache-file)))))))))) @@ -4742,7 +4752,8 @@ inserted in the process buffer before executing the command." (defun sage-shell:send-doctest (arg) (interactive "P") - "If looking at a sage: prompt, send the current doctest lines to the Sage process. + "If looking at a sage: prompt, send the current doctest lines to the Sage +process. With prefix argument, send all doctests (at sage: prompts) until the end of the docstring." (sage-shell-edit:set-sage-proc-buf-internal :select-p t) diff --git a/sage-shell-view.el b/sage-shell-view.el index e2cfd3d..90f5901 100644 --- a/sage-shell-view.el +++ b/sage-shell-view.el @@ -83,7 +83,8 @@ Otherwise, if non-nil will start both. Each of these can be enabled or disabled later by calling `sage-shell-view-enable-inline-plots', `sage-shell-view-disable-inline-plots', `sage-shell-view-enable-inline-output', `sage-shell-view-disable-inline-output', -`sage-shell-view-toggle-inline-plots' or `sage-shell-view-toggle-inline-output'." +`sage-shell-view-toggle-inline-plots' or +`sage-shell-view-toggle-inline-output'." :type '(choice (const :tag "Inline Plots" plots) (const :tag "Typeset Output" output) (const :tag "Both" t)) @@ -535,7 +536,8 @@ Function to be inserted in `comint-output-filter-functions'." (sage-shell-view-output-filter-process-inline-output string)))))) (defun sage-shell-view-update-modeline () - "Update modeline to include information about whether sage-shell-view is enabled." + "Update modeline to include information about whether sage-shell-view +is enabled." (when (eq major-mode 'sage-shell-mode) (let ((fmt (format "/%s%s" (if sage-shell-view-inline-plots-enabled "p" "") @@ -551,14 +553,17 @@ Function to be inserted in `comint-output-filter-functions'." ;;;###autoload (defun sage-shell-view-enable-inline-output () - "Enable inline output pretty-printing, i.e. typeset output from sage in the `sage-shell-mode' buffer. -WARNING: this communicates with the sage process. Only use this when sage is running." + "Enable inline output pretty-printing, i.e. typeset output from sage in the +`sage-shell-mode' buffer. +WARNING: this communicates with the sage process. Only use this when sage is +running." (interactive) (sage-shell-view--set-inline-state 'text t)) (defun sage-shell-view-disable-inline-output () - "Disable inline output pretty-printing, i.e. do not typeset output from sage in the `sage-shell-mode' buffer. + "Disable inline output pretty-printing, i.e. do not typeset output from sage +in the `sage-shell-mode' buffer. WARNING: this communicates with the sage process. Only use this when sage is running." (interactive) (sage-shell-view--set-inline-state @@ -566,15 +571,19 @@ WARNING: this communicates with the sage process. Only use this when sage is ru ;;;###autoload (defun sage-shell-view-enable-inline-plots () - "Enable inline plotting, i.e. display plots in the `sage-shell-mode' buffer and do not spawn an external viewer. -WARNING: this communicates with the sage process. Only use this when sage is running." + "Enable inline plotting, i.e. display plots in the `sage-shell-mode' buffer +and do not spawn an external viewer. +WARNING: this communicates with the sage process. +Only use this when sage is running." (interactive) (sage-shell-view--set-inline-state 'plot t)) (defun sage-shell-view-disable-inline-plots () - "Disable inline plotting, i.e. do not display plots in the `sage-shell-mode' buffer and instead spawn an external viewer. -WARNING: this communicates with the sage process. Only use this when sage is running." + "Disable inline plotting, i.e. do not display plots in the +`sage-shell-mode' buffer and instead spawn an external viewer. +WARNING: this communicates with the sage process. +Only use this when sage is running." (interactive) (sage-shell-view--set-inline-state 'plot nil))