ein-pager.el 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. ;;; ein-pager.el --- Pager module
  2. ;; Copyright (C) 2012- Takafumi Arakaki
  3. ;; Author: Takafumi Arakaki <aka.tkf at gmail.com>
  4. ;; This file is NOT part of GNU Emacs.
  5. ;; ein-pager.el is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; ein-pager.el is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with ein-pager.el. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;;
  17. ;;; Code:
  18. (require 'ansi-color)
  19. (require 'ein-core)
  20. (require 'ein-events)
  21. ;; FIXME: Make a class with `:get-notebook-name' slot like `ein:worksheet'
  22. (defun ein:pager-new (name events)
  23. ;; currently pager = name.
  24. (ein:pager-bind-events name events)
  25. name)
  26. (defun ein:pager-bind-events (pager events)
  27. "Bind events related to PAGER to the event handler EVENTS."
  28. (ein:events-on events
  29. 'open_with_text.Pager
  30. #'ein:pager--open-with-text
  31. pager))
  32. (defun ein:pager--open-with-text (pager data)
  33. (let ((text (plist-get data :text)))
  34. (unless (equal (ein:trim text) "")
  35. (ein:pager-clear pager)
  36. (ein:pager-expand pager)
  37. (ein:pager-append-text pager text))))
  38. (defun ein:pager-clear (pager)
  39. (ein:with-read-only-buffer (get-buffer-create pager)
  40. (erase-buffer)))
  41. (defun ein:pager-expand (pager)
  42. (pop-to-buffer (get-buffer-create pager))
  43. (goto-char (point-min)))
  44. (defun ein:pager-append-text (pager text)
  45. (ein:with-read-only-buffer (get-buffer-create pager)
  46. (insert (ansi-color-apply text))
  47. (ein:pager-mode)))
  48. ;; FIXME: this should be automatically called when opening pager.
  49. (defun ein:pager-goto-docstring-bset-loc ()
  50. "Goto the best location of the documentation."
  51. (interactive)
  52. (goto-char (point-min))
  53. (search-forward-regexp "^Docstring:")
  54. (beginning-of-line 0)
  55. (recenter 0))
  56. (define-derived-mode ein:pager-mode fundamental-mode "ein:pager"
  57. "IPython notebook pager mode."
  58. (view-mode)
  59. (font-lock-mode))
  60. (setq ein:pager-mode-map (make-sparse-keymap))
  61. (let ((map ein:pager-mode-map))
  62. (define-key map "\C-c\C-b" 'ein:pager-goto-docstring-bset-loc)
  63. (define-key map "q" 'bury-buffer)
  64. map)
  65. (provide 'ein-pager)
  66. ;;; ein-pager.el ends here