Personal emacs config
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
2.7 KiB

  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. (require 'view)
  22. ;; FIXME: Make a class with `:get-notebook-name' slot like `ein:worksheet'
  23. (declare-function ess-help-underline "ess-help")
  24. (defun ein:pager-new (name events)
  25. ;; currently pager = name.
  26. (ein:pager-bind-events name events)
  27. name)
  28. (defun ein:pager-bind-events (pager events)
  29. "Bind events related to PAGER to the event handler EVENTS."
  30. (ein:events-on events
  31. 'open_with_text.Pager
  32. #'ein:pager--open-with-text
  33. pager))
  34. (defun ein:pager--open-with-text (pager data)
  35. (let ((text (plist-get data :text)))
  36. (unless (equal (ein:trim text) "")
  37. (ein:pager-clear pager)
  38. (ein:pager-expand pager)
  39. (ein:pager-append-text pager text))))
  40. (defun ein:pager-clear (pager)
  41. (ein:with-read-only-buffer (get-buffer-create pager)
  42. (erase-buffer)))
  43. (defun ein:pager-expand (pager)
  44. (pop-to-buffer (get-buffer-create pager))
  45. (goto-char (point-min)))
  46. (defun ein:pager-append-text (pager text)
  47. (ein:with-read-only-buffer (get-buffer-create pager)
  48. (insert (ansi-color-apply text))
  49. (if (featurep 'ess-help)
  50. (ess-help-underline))
  51. (unless (eql 'ein:pager-mode major-mode)
  52. (ein:pager-mode))))
  53. ;; FIXME: this should be automatically called when opening pager.
  54. (defun ein:pager-goto-docstring-bset-loc ()
  55. "Goto the best location of the documentation."
  56. (interactive)
  57. (goto-char (point-min))
  58. (search-forward-regexp "^Docstring:")
  59. (beginning-of-line 0)
  60. (recenter 0))
  61. (defvar ein:pager-mode-map
  62. (let ((map (make-sparse-keymap)))
  63. (define-key map "\C-c\C-b" 'ein:pager-goto-docstring-bset-loc)
  64. map)
  65. "Keymap for ein:pager-mode.")
  66. (define-derived-mode ein:pager-mode view-mode "ein:pager"
  67. "IPython notebook pager mode.
  68. Commands:
  69. \\{ein:pager-mode-map}"
  70. (setq-local view-no-disable-on-exit t)
  71. (font-lock-mode))
  72. (provide 'ein-pager)
  73. ;;; ein-pager.el ends here