Source

sage-mode / emacs / sage-view.el

  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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
;;; sage-view.el --- Typeset SAGE output on the fly
;; $Id: sage-view.el,v 1.2 2009/02/25 18:32:51 meulien Exp $

;; Copyright (C) 2008  Matthias Meulien, Nick Alexander

;; Authors: Matthias Meulien <matthias.meulien@xlim.fr>, Nick Alexander
;; <ncalexander@gmail.com>
;; Keywords: sage math image

;; 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/>.

;;; Commentary:

;; `sage-view' typesets output in an `inferior-sage-mode' buffer and displays
;; plots inline in an `inferior-sage-mode'.  Inline displays are context
;; sensitive; by default, right-clicking brings up a context-menu.

;; Use `sage-view' to enable the minor mode, and then
;; `sage-view-enable-inline-output', `sage-view-disable-inline-output' and
;; `sage-view-enable-inline-plots', `sage-view-disable-inline-plots' enable
;; and disable the relevant features.  You might add some of those functions
;; to `sage-view-hook' to configure `sage-view' to your liking.

;; You can customize `sage-view' using the Emacs customize interface by M-x
;; customize-group RET sage-view RET.  In particular you can customize
;; magnification and margins

;; This mode was inspired by doc-view.el by Tassilo Horn, preview.el
;; by David Kastrup, and imath.el by Yasuaki Honda.

;; The LaTex style used by preview.el is mandatory to use
;; sage-view.el. It is shipped with AUCTeX.

;;; Todo:
;; - Add a auto-reveal stuff to overlays
;; - Check that display is image capable
;; - Disabling sage-view mode should remove overlays
;; - Set color, center image, enlarge overlay to window full size
;; - Add zoom features to overlays
;; - Add horizontal scrolling

;; Bugs:
;; - Numpy output can be a text array... should not be inserted into
;;   $$ signs (hum... example?)

;;; Code:
(require 'sage)
(require 'sage-mode)
(require 'mouse) ;; for popup-menu

(defcustom sage-view-latex-head
  "\\documentclass{article}
\\usepackage[active, tightpage, pdftex, displaymath]{preview}
\\usepackage{amstext}
\\begin{document}
\\begin{preview}
\\begin{math}\n"
  "String to be inserted at the top of a LaTeX document."
  :type 'string
  :group 'sage-view)

(defcustom sage-view-latex-tail
  "\n\\end{math}
\\end{preview}
\\end{document}\n"
  "String to be inserted at the end of a LaTeX document."
  :type 'string
  :group 'sage-view)

(defvar sage-view-start-string "<html><\\(?:script type=\"math/tex\"\\)>"
  "HTML tags that identify the begining of a math formula in Sage
  output.")

(defvar sage-view-final-string "</\\(?:span\\|script\\)></html>"
  "HTML tags that identify the end of a math formula in Sage
  output.")

(defvar sage-view-dir-name nil)
(defvar sage-view-inline-plots-enabled nil)
(defvar sage-view-inline-output-enabled nil)

(defun sage-view-latex->pdf (ov)
  "Start conversion of the LATEX document associated to OV to PDF.

See also `sage-view-process-overlay'."
  (let* ((latex (concat (overlay-get ov 'file-sans-extension) ".tex"))
	 (options (append
		   (list (concat "--output-directory=" sage-view-dir-name)
			 (concat "-interaction=" "nonstopmode")
			 (concat "-output-format=" "pdf")
			 latex)))
	 (proc (apply 'start-process
		      (append (list "latex->pdf" nil "latex") options))))
    (process-put proc 'overlay ov)
    (set-process-sentinel proc 'sage-view-latex->pdf-sentinel)))

(defun sage-view-pdf->png (ov)
  "Start conversion of the PDF file associated to OV to PNG.

See also `sage-view-process-overlay'."
  (let* ((base (overlay-get ov 'file-sans-extension))
	 (png (concat base ".png"))
	 (pdf (concat base ".pdf"))
	 (level (int-to-string sage-view-anti-aliasing-level))
	 (scale (or (overlay-get ov 'scale) sage-view-scale))
	 (options (append
		   sage-view-gs-options
		   (list (concat "-dTextAlphaBits=" level)
			 (concat "-dGraphicsAlphaBits=" level)
			 (concat "-sOutputFile=" png)
			 (concat "-r" (sage-view-compute-resolution scale))
			 pdf)))
	 (proc (apply 'start-process
		      (append
		       (list "pdf->png" "*sage-view*" sage-view-gs-command)
		       options))))
    (process-put proc 'overlay ov)
    (set-process-sentinel proc 'sage-view-pdf->png-sentinel)))

(defun sage-view-latex->pdf-sentinel (proc event)
  "If PROC (supposed to be a conversion process from LATEX to
PDF) was successful, convert the PDF to PNG.

See also `sage-view-process-overlay'."
  (let* ((ov (process-get proc 'overlay))
	 (base (overlay-get ov 'file-sans-extension)))
    (if (string-match "finished" event)
	(sage-view-pdf->png ov)
      (overlay-put ov 'display
		   (concat "Conversion failed (see " base ".log" ")")))))

(defun sage-view-pdf->png-sentinel (proc event)
  "If PROC (supposed to be a conversion process from PDF to PNG)
was successful, update the overlay associated to PROC.

See also `sage-view-process-overlay'."
  (let* ((ov (process-get proc 'overlay))
	 (base (overlay-get ov 'file-sans-extension))
	 (png (concat base ".png"))
	 (image (when (and (string-match "finished" event)
			   png (file-readable-p png))
		  (append (list 'image :type 'png :file png :margin sage-view-margin)))))
    (if (not image)
	(overlay-put ov 'display
		     (concat "Conversion failed (see " base ".log" ")"))
      (overlay-put ov 'display image))
    (sit-for 0)))

(defun sage-view-compute-resolution (scale)
  (let ((w (* scale (/ (* 25.4 (display-pixel-width))
	      (display-mm-width))))
	(h (* scale (/ (* 25.4 (display-pixel-height))
	      (display-mm-height)))))
    (concat (int-to-string w) "x" (int-to-string h))))

(defun sage-view-process-overlay (ov)
  "Associate a LATEX document to OV and start conversion process
from LATEX to PDF.

The conversion process is done by `sage-view-latex->pdf'. When it
ends, `sage-view-latex->pdf-sentinel' is called: If the
conversion is successful, a conversion process from PDF to PNG
starts. When it ends, `sage-view-pdf->png-sentinel' is called: If
the last conversion is successful, OV displays the resulting
image."
  (let* ((base (expand-file-name
		(make-temp-name "sage-view_") sage-view-dir-name))
	 (file (concat base ".tex")))
    (with-temp-file file
      (insert sage-view-latex-head)
      (insert (overlay-get ov 'math))
      (insert sage-view-latex-tail))
    (overlay-put ov 'file-sans-extension base)
    (sage-view-latex->pdf ov)))

(defun sage-view-output-filter-process-inline-output (string)
  "Substitute overlays to inline output.

Each region delimited by `sage-view-start-string' and
`sage-view-final-string' is replaced by an overlay.

This function expects the buffer to be narrowed to the current
output. And should be wrapped in a `save-excursion' and
`save-restriction' call.

See also `sage-view-output-filter'."
  (goto-char (point-min))
  (while (re-search-forward sage-view-start-string (point-max) t)
    (let* ((header-beg (match-beginning 0))
           (header-end (match-end 0))

           (footer-beg (when (re-search-forward sage-view-final-string (point-max) t)
                         (match-beginning 0)))
           (footer-end (match-end 0))
           (text (buffer-substring-no-properties header-end footer-beg))
           (ov (make-overlay header-beg
                             footer-end
                             nil nil nil))
           (map (make-sparse-keymap)))
      (overlay-put ov 'help-echo "mouse-3: Open contextual menu")
      (overlay-put ov 'math text)
      (overlay-put ov 'display (format "Typesetting %s..." text))
      (define-key map [mouse-3]
        `(lambda (event) (interactive "e")
           (sage-view-context-menu ,ov event)))
      (overlay-put ov 'keymap map)
      (sage-view-process-overlay ov))))

(autoload 'dired-rename-file "dired-aux")
(defun sage-view-output-filter-process-inline-plots (string)
  "Generate and place one overlay image for one inline plot,
found by looking for a particular png file in directory
`sage-view-dir-name'.

This function expects the buffer to be narrowed to just the
current output; see `sage-view-output-filter' for how to do
that."
  ;; we need to foil emacs' image cache, which doesn't reload files with the same name
  (let* ((pngname (format "%s/sage-view.png" sage-view-dir-name))
	 (base (expand-file-name (make-temp-name "sage-view-plot_") sage-view-dir-name))
	 (pngname2 (concat base ".png")))
    ;; (message "Looking for plot at %s..." pngname)
    (when (and pngname
	       (file-exists-p pngname)
	       (file-readable-p pngname))
      ;; (message "Looking for plot at %s... not found." pngname)
      ;; the found branch
      ;; (message "Looking for plot at %s... found!" pngname)
      (dired-rename-file pngname pngname2 t)
      ;; (message "Renamed %s to %s" pngname pngname2)

      (goto-char (point-max))
      (let ((im (create-image pngname2 'png))
	    (sage-view-inline-plot-overlay
	     (make-overlay (- (point) 1) (- (point) 0) nil nil nil))) ;; adding a margin screws this up
	(overlay-put sage-view-inline-plot-overlay 'display im)
	(overlay-put sage-view-inline-plot-overlay 'before-string "\n\n") ;; help alignment as much as possible
	(overlay-put sage-view-inline-plot-overlay 'after-string "\n\n")  ;; but emacs doesn't handle this right IMHO
	;; (dired-delete-file pngname2 'always) ;; causes problems with emacs image loading
      ))))

(defun sage-view-output-filter (string)
  "Generate and place overlay images for inline output and inline plots.

Function to be inserted in `comint-output-filter-functions'."
  (save-excursion
    (save-restriction
      (narrow-to-region comint-last-input-end
			(process-mark (get-buffer-process (current-buffer))))
      (when sage-view-inline-plots-enabled
	(sage-view-output-filter-process-inline-plots string))
      (when (and sage-view-inline-output-enabled
		 (string-match sage-view-final-string string))
	(sage-view-output-filter-process-inline-output string)))))

;;;###autoload
(defun sage-view-enable-inline-output ()
  "Enable inline output pretty-printing, i.e. typeset output from sage in the `inferior-sage-mode' buffer.
WARNING: this communicates with the sage process.  Only use this
when `sage-view' mode is enabled and sage is running."
  (interactive)
  (setq sage-view-inline-output-enabled t)
  (python-send-receive-multiline "pretty_print_default(True);"))

;;;###autoload
(defun sage-view-disable-inline-output ()
  "Disable inline output pretty-printing, i.e. do not typeset output from sage in the `inferior-sage-mode' buffer.
WARNING: this communicates with the sage process.  Only use this
when `sage-view' mode is enabled and sage is running."
  (interactive)
  (setq sage-view-inline-output-enabled nil)
  (python-send-receive-multiline "pretty_print_default(False);"))

;;;###autoload
(defun sage-view-enable-inline-plots ()
  "Enable inline plotting, i.e. display plots in the `inferior-sage-mode' buffer and do not spawn an external viewer.
WARNING: this communicates with the sage process.  Only use this
when `sage-view' mode is enabled and sage is running."
  (interactive)
  (setq sage-view-inline-plots-enabled t)
  (python-send-receive-multiline "sage.plot.plot.DOCTEST_MODE = True;")
  ;; sage 4.7
  (python-send-receive-multiline
   (format "sage.plot.plot.DOCTEST_MODE_FILE = '%s/sage-view.png';"
	   sage-view-dir-name))
  ;; sage 5.0
  (python-send-receive-multiline
   (format "if hasattr(sage.plot,'graphics'): sage.plot.graphics.DOCTEST_MODE_FILE = '%s/sage-view.png';\n"
	   sage-view-dir-name)))

;;;###autoload
(defun sage-view-disable-inline-plots ()
  "Disable inline plotting, i.e. do not display plots in the `inferior-sage-mode' buffer and instead spawn an external viewer.
WARNING: this communicates with the sage process.  Only use this
when `sage-view' mode is enabled and sage is running."
  (interactive)
  (setq sage-view-inline-plots-enabled nil)
  (python-send-receive-multiline "sage.plot.plot.DOCTEST_MODE = False;")
  ;; sage 4.7
  (python-send-receive-multiline "sage.plot.plot.DOCTEST_MODE_FILE = None;")
  ;; sage 5.0
  (python-send-receive-multiline "if hasattr(sage.plot,'graphics'): sage.plot.graphics.DOCTEST_MODE_FILE = None;\n"))

(defun sage-view-create-temp ()
  "Create a temporary directory and set `sage-view-dir-name'
to its name.

Nothing is done if `sage-view-dir-name' equals the name of a
writable directory."
  (unless (and sage-view-dir-name
	       (file-directory-p sage-view-dir-name)
	       (file-writable-p sage-view-dir-name))
    ;; I'm not sure SAGE_TESTDIR is the right thing, but it's
    ;; .sage/tmp which seems like what I should use.
    (let ((tmp-dir (shell-command-to-string (concat sage-command " -sh -c 'echo -n $SAGE_TESTDIR' 2>/dev/null"))))
      (when (= 0 (length tmp-dir))
	(setq tmp-dir "~/.sage/tmp"))
      (setq sage-view-dir-name
	    (make-temp-name (expand-file-name "emacs" tmp-dir))))
    (condition-case err
	(make-directory sage-view-dir-name)
      (error (message "Creation of `%s' failed: %s"
		      sage-view-dir-name (error-message-string err))))))

(defun sage-view-delete-temp ()
  "Delete the directory named after `sage-view-dir-name'."
  (when sage-view-dir-name
    (condition-case err
	(progn
	  (mapc #'delete-file
		(directory-files sage-view-dir-name t "^sage-view" t))
	  (delete-directory sage-view-dir-name))
      (error (message "Deletion of `%s' failed: %s"
		      sage-view-dir-name (error-message-string err))))))

(defun sage-view-overlay-activep (ov)
  "Check whether there is a valid image associated with OV."
  (eq (car (overlay-get ov 'display)) 'image))

(defun sage-view-copy-text (ov)
  "Copy LATEX source of OV into the kill buffer."
  (let ((text (overlay-get ov 'math)))
    (if text
	(kill-new text)
      (message "No LaTeX code available"))))

(defun sage-view-save-image (ov)
  "Copy image file associated to OV.

Make sure that there is a valid image associated with OV with
`sage-view-overlay-activep'."
  (let* ((spec (cdr (overlay-get ov 'display)))
	 (file (plist-get spec :file))
	 (name (when (and file (file-readable-p file))
		 (expand-file-name
		  (read-file-name "Write image to file: "
				 default-directory
				 "sage-view.png")))))
    (if name
	(copy-file file name))))

(defun sage-view-regenerate (ov)
  ""
  (overlay-put ov 'scale sage-view-scale)
  (sage-view-process-overlay ov))

(defun sage-view-zoom-in (ov)
  ""
  (let ((scale (or (overlay-get ov 'scale) sage-view-scale)))
    (overlay-put ov 'scale (+ scale sage-view-scale-factor))
    (message "Overlay's scale set to %s" scale)
    ;(overlay-put ov 'display (overlay-get ov 'math))
    (sage-view-process-overlay ov)))

(defun sage-view-zoom-out (ov)
  ""
  (let ((scale (or (overlay-get ov 'scale) sage-view-scale)))
    (overlay-put ov 'scale (- scale sage-view-scale-factor))
    (message "Overlay's scale set to %s" scale)
    ;(overlay-put ov 'display (overlay-get ov 'math))
    (sage-view-process-overlay ov)))

(defun sage-view-context-menu (ov ev)
  "Pop up a menu for OV at position EV."
  (popup-menu
   `("Sage Mode"
     ["Regenerate" (lambda () (interactive) (sage-view-regenerate ,ov))]
     ["Copy Text" (lambda () (interactive) (sage-view-copy-text ,ov))]
     ["Save As..." (lambda () (interactive) (sage-view-save-image ,ov))
      `(sage-view-overlay-activep ,ov)]
     ["Zoom in" (lambda () (interactive) (sage-view-zoom-in ,ov))
      `(sage-view-overlay-activep ,ov)]
     ["Zoom out" (lambda () (interactive) (sage-view-zoom-out ,ov))
      `(sage-view-overlay-activep ,ov)])
   ev))


;; TODO: require a graphics console before turning it on, or make it possile to turn it on
;;;###autoload
(define-minor-mode sage-view
  "Toggle automatic typesetting of Sage output.

Typesetting of math formulas is done by LATEX subprocesses and
PDF to PNG conversions." nil
  :group 'sage-view
  :lighter " Sage-View"
  (if sage-view
      (progn
	(make-local-variable 'sage-view-dir-name)
	(sage-view-create-temp)
	(sage-view-enable-inline-output)
	(sage-view-enable-inline-plots)
	(make-local-variable 'comint-preoutput-filter-functions)
	(make-local-variable 'comint-output-filter-function)
	(add-hook 'comint-output-filter-functions 'sage-view-output-filter)
	(add-hook 'kill-buffer-hook 'sage-view-delete-temp))
    (progn
      (sage-view-disable-inline-output)
      (sage-view-disable-inline-plots)
      (remove-hook 'comint-output-filter-functions 'sage-view-output-filter)
      (remove-hook 'comint-preoutput-filter-functions 'sage-view-preoutput-filter))))

(provide 'sage-view)
;;; sage-view.el ends here
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.