2012-09-04 02:01:22 +02:00
|
|
|
|
;;; ein-org-src.el --- Notebook mode using org-src.el
|
|
|
|
|
|
|
|
|
|
;; Copyright (C) 2012 Takafumi Arakaki
|
|
|
|
|
|
|
|
|
|
;; Author: Takafumi Arakaki <aka.tkf at gmail.com>
|
|
|
|
|
|
|
|
|
|
;; This file is NOT part of GNU Emacs.
|
|
|
|
|
|
|
|
|
|
;; ein-org-src.el 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.
|
|
|
|
|
|
|
|
|
|
;; ein-org-src.el 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 ein-org-src.el.
|
|
|
|
|
;; If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
|
|
;;
|
|
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
2012-09-05 16:58:04 +02:00
|
|
|
|
(eval-when-compile (require 'cl))
|
2012-09-04 17:09:48 +02:00
|
|
|
|
(require 'org-src nil t)
|
2012-09-04 17:53:25 +02:00
|
|
|
|
(eval-when-compile (defvar markdown-mode-map))
|
2012-09-04 02:01:22 +02:00
|
|
|
|
|
2012-09-04 17:04:44 +02:00
|
|
|
|
(require 'ein-worksheet)
|
2012-09-04 02:01:22 +02:00
|
|
|
|
|
2012-09-30 20:12:29 +02:00
|
|
|
|
(defun ein:ml-fontify (limit)
|
2012-09-04 02:08:21 +02:00
|
|
|
|
"Fontify next input area comes after the current point then
|
|
|
|
|
return `t' or `nil' if not found.
|
|
|
|
|
See info node `(elisp) Search-based Fontification'."
|
2012-09-04 02:01:22 +02:00
|
|
|
|
(ein:log-ignore-errors
|
2012-09-30 20:12:29 +02:00
|
|
|
|
(ein:ml-fontify-1 limit)))
|
2012-09-04 02:01:22 +02:00
|
|
|
|
|
2012-09-30 20:12:29 +02:00
|
|
|
|
(defun ein:ml-current-or-next-input-cell (ewoc-node)
|
2012-09-04 14:58:12 +02:00
|
|
|
|
"Almost identical to `ein:worksheet-next-input-cell' but return
|
|
|
|
|
the current cell if EWOC-NODE is the input area node."
|
2012-09-04 14:40:54 +02:00
|
|
|
|
(let* ((ewoc-data (ewoc-data ewoc-node))
|
|
|
|
|
(cell (ein:$node-data ewoc-data))
|
|
|
|
|
(path (ein:$node-path ewoc-data))
|
|
|
|
|
(element (nth 1 path)))
|
|
|
|
|
(if (memql element '(prompt input))
|
|
|
|
|
cell
|
|
|
|
|
(ein:cell-next cell))))
|
|
|
|
|
|
2012-09-30 20:12:29 +02:00
|
|
|
|
(defun ein:ml-fontify-1 (limit)
|
|
|
|
|
"Actual implementation of `ein:ml-fontify'.
|
2012-09-04 02:08:21 +02:00
|
|
|
|
This function may raise an error."
|
2012-09-04 02:18:22 +02:00
|
|
|
|
(ein:and-let* ((pos (point))
|
|
|
|
|
(node (ein:worksheet-get-nearest-cell-ewoc-node pos limit))
|
2012-09-30 20:12:29 +02:00
|
|
|
|
(cell (ein:ml-current-or-next-input-cell node))
|
2012-09-04 14:40:54 +02:00
|
|
|
|
(start (ein:cell-input-pos-min cell))
|
2012-09-04 14:53:35 +02:00
|
|
|
|
(end (ein:cell-input-pos-max cell))
|
|
|
|
|
((<= end limit))
|
2012-09-04 13:49:04 +02:00
|
|
|
|
((< start end))
|
2012-09-04 02:01:22 +02:00
|
|
|
|
(lang (ein:cell-language cell)))
|
2012-09-04 13:49:04 +02:00
|
|
|
|
(let ((inhibit-read-only t))
|
2012-09-04 13:58:43 +02:00
|
|
|
|
(org-src-font-lock-fontify-block lang start end)
|
2012-09-04 14:46:14 +02:00
|
|
|
|
;; Emacs fontification mechanism requires the function to move
|
2012-09-04 15:06:59 +02:00
|
|
|
|
;; the point. Do *not* use `(goto-char end)'. As END is in the
|
|
|
|
|
;; input area, fontification falls into an infinite loop.
|
|
|
|
|
(ewoc-goto-node (oref cell :ewoc) (ein:cell-element-get cell :footer)))
|
2012-09-04 02:23:38 +02:00
|
|
|
|
t))
|
2012-09-04 02:01:22 +02:00
|
|
|
|
|
2012-09-30 20:12:29 +02:00
|
|
|
|
(defun ein:ml-back-to-prev-node ()
|
2012-09-04 14:16:47 +02:00
|
|
|
|
(ein:aand (ein:worksheet-get-ewoc) (ewoc-goto-prev it 1)))
|
|
|
|
|
|
2012-09-30 20:12:29 +02:00
|
|
|
|
(defvar ein:ml-font-lock-keywords
|
|
|
|
|
'((ein:ml-fontify))
|
2012-09-04 02:01:22 +02:00
|
|
|
|
"Default `font-lock-keywords' for `ein:notebook-org-src-mode'.")
|
|
|
|
|
|
2012-09-30 20:12:29 +02:00
|
|
|
|
(defun ein:ml-set-font-lock-defaults ()
|
2012-09-04 02:01:22 +02:00
|
|
|
|
(set (make-local-variable 'font-lock-defaults)
|
2012-09-30 20:12:29 +02:00
|
|
|
|
'(ein:ml-font-lock-keywords
|
2012-09-04 14:58:12 +02:00
|
|
|
|
;; The following are adapted from org-mode but I am not sure
|
|
|
|
|
;; if I need them:
|
2012-09-04 14:16:47 +02:00
|
|
|
|
t nil nil
|
2012-09-30 20:12:29 +02:00
|
|
|
|
ein:ml-back-to-prev-node)))
|
2012-09-04 02:01:22 +02:00
|
|
|
|
|
2012-09-04 02:15:22 +02:00
|
|
|
|
(define-derived-mode ein:notebook-org-src-mode fundamental-mode "ein:os"
|
|
|
|
|
"Notebook mode with org-mode powered fontification."
|
2012-09-04 17:45:34 +02:00
|
|
|
|
(make-local-variable 'indent-line-function)
|
|
|
|
|
(make-local-variable 'indent-region-function)
|
2012-09-30 20:12:29 +02:00
|
|
|
|
(ein:ml-keymap-setup-python)
|
|
|
|
|
(ein:ml-set-font-lock-defaults))
|
2012-09-04 02:15:22 +02:00
|
|
|
|
|
2012-09-04 18:35:36 +02:00
|
|
|
|
(eval-after-load "auto-complete"
|
|
|
|
|
'(add-to-list 'ac-modes 'ein:notebook-org-src-mode))
|
|
|
|
|
|
2012-09-04 17:53:25 +02:00
|
|
|
|
|
|
|
|
|
;;; Keymap setup functions
|
|
|
|
|
|
2012-09-30 20:12:29 +02:00
|
|
|
|
(defun ein:ml-keymap-setup-python ()
|
2012-09-04 17:45:34 +02:00
|
|
|
|
(when (boundp 'python-mode-map)
|
|
|
|
|
(set-keymap-parent ein:notebook-org-src-mode-map python-mode-map))
|
|
|
|
|
(cond
|
|
|
|
|
((featurep 'python)
|
|
|
|
|
(setq indent-line-function #'python-indent-line-function)
|
|
|
|
|
(setq indent-region-function #'python-indent-region))
|
|
|
|
|
((featurep 'python-mode)
|
|
|
|
|
;; FIXME: write keymap setup for python-mode.el
|
|
|
|
|
)))
|
|
|
|
|
|
2012-09-30 20:12:29 +02:00
|
|
|
|
(defun ein:ml-keymap-setup-markdown ()
|
2012-09-04 17:53:25 +02:00
|
|
|
|
"Use `markdown-mode-map'. NOTE: This function is not used now."
|
|
|
|
|
(when (featurep 'markdown-mode)
|
|
|
|
|
(set-keymap-parent ein:notebook-org-src-mode-map markdown-mode-map)))
|
|
|
|
|
|
2012-09-30 20:12:29 +02:00
|
|
|
|
;; FIXME: dynamically call ein:ml-keymap-setup-LANG using
|
2012-09-04 17:53:25 +02:00
|
|
|
|
;; `post-command-hook'.
|
2012-09-30 20:12:29 +02:00
|
|
|
|
;; FIMXE: add more ein:ml-keymap-setup-LANG to switch kaymap.
|
2012-09-04 17:45:34 +02:00
|
|
|
|
|
2012-09-05 16:58:04 +02:00
|
|
|
|
|
|
|
|
|
;;; yasnippet
|
|
|
|
|
|
2012-09-30 20:12:29 +02:00
|
|
|
|
(defvar ein:ml-yasnippet-parents '(python-mode markdown-mode)
|
2012-09-05 16:58:04 +02:00
|
|
|
|
"Parent modes for `ein:notebook-org-src-mode' to register in yasnippet.")
|
|
|
|
|
|
2012-09-30 20:12:29 +02:00
|
|
|
|
(defun ein:ml-setup-yasnippet ()
|
2012-09-05 16:58:04 +02:00
|
|
|
|
(loop for define-parents in '(yas/define-parents
|
|
|
|
|
yas--define-parents)
|
|
|
|
|
when (fboundp define-parents)
|
|
|
|
|
do (ignore-errors
|
|
|
|
|
;; `let' is for workaround the bug in yasnippet
|
|
|
|
|
(let ((mode-sym 'ein:notebook-org-src-mode))
|
|
|
|
|
(funcall define-parents
|
|
|
|
|
mode-sym
|
2012-09-30 20:12:29 +02:00
|
|
|
|
ein:ml-yasnippet-parents)))))
|
2012-09-05 16:58:04 +02:00
|
|
|
|
|
2012-09-30 20:12:29 +02:00
|
|
|
|
(eval-after-load "yasnippet" '(ein:ml-setup-yasnippet))
|
2012-09-05 16:58:04 +02:00
|
|
|
|
|
2012-09-04 02:01:22 +02:00
|
|
|
|
(provide 'ein-org-src)
|
|
|
|
|
|
|
|
|
|
;;; ein-org-src.el ends here
|