2018-10-23 21:46:32 -05:00
|
|
|
;;; jupyter-julia.el --- Jupyter support for Julia -*- lexical-binding: t -*-
|
|
|
|
|
|
|
|
;; Copyright (C) 2018 Nathaniel Nicandro
|
|
|
|
|
|
|
|
;; Author: Nathaniel Nicandro <nathanielnicandro@gmail.com>
|
|
|
|
;; Created: 23 Oct 2018
|
2019-02-14 23:05:00 -06:00
|
|
|
;; Version: 0.7.1
|
2018-10-23 21:46:32 -05:00
|
|
|
|
|
|
|
;; 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 2, 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 GNU Emacs; see the file COPYING. If not, write to the
|
|
|
|
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
;; Boston, MA 02111-1307, USA.
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
2018-11-18 22:52:02 -06:00
|
|
|
;; Support methods for integration with Julia.
|
2018-10-23 21:46:32 -05:00
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
(require 'jupyter-repl)
|
2018-11-18 23:51:16 -06:00
|
|
|
|
|
|
|
(declare-function julia-latexsub-or-indent "ext:julia-mode" (arg))
|
2018-10-23 21:46:32 -05:00
|
|
|
|
2018-11-18 22:52:02 -06:00
|
|
|
(cl-defmethod jupyter-indent-line (&context (major-mode julia-mode))
|
|
|
|
"Call `julia-latexsub-or-indent'."
|
|
|
|
(call-interactively #'julia-latexsub-or-indent))
|
|
|
|
|
|
|
|
(cl-defmethod jupyter-load-file-code (file &context (jupyter-lang julia))
|
|
|
|
(format "include(\"%s\");" file))
|
|
|
|
|
|
|
|
;;; Completion
|
|
|
|
|
2018-10-23 21:46:32 -05:00
|
|
|
(cl-defmethod jupyter-completion-prefix (&context (jupyter-lang julia))
|
2019-01-18 21:38:46 -06:00
|
|
|
(cond
|
|
|
|
;; Completing argument lists
|
|
|
|
((and (char-before)
|
|
|
|
(eq (char-syntax (char-before)) ?\()
|
|
|
|
(or (not (char-after))
|
|
|
|
(looking-at-p "\\_>")
|
|
|
|
(not (memq (char-syntax (char-after)) '(?w ?_)))))
|
|
|
|
(buffer-substring-no-properties
|
|
|
|
(jupyter-completion-symbol-beginning (1- (point)))
|
|
|
|
(point)))
|
|
|
|
(t
|
2019-02-14 23:09:00 -06:00
|
|
|
(let ((prefix (cl-call-next-method "\\\\\\|\\.\\|::?" 2)))
|
2019-01-18 21:38:46 -06:00
|
|
|
(prog1 prefix
|
2019-02-14 23:09:00 -06:00
|
|
|
(when (consp prefix)
|
|
|
|
(let ((beg (- (point) (length (car prefix)))))
|
|
|
|
(cond
|
|
|
|
;; Include the \ in the prefix so it gets replaced if a canidate is
|
|
|
|
;; selected.
|
|
|
|
((eq (char-before beg) ?\\)
|
|
|
|
(setcar prefix (concat "\\" (car prefix))))
|
|
|
|
;; Also include : to complete symbols when used as dictionary keys
|
|
|
|
((and (eq (char-before beg) ?:)
|
|
|
|
(not (eq (char-before (1- beg)) ?:)))
|
|
|
|
(setcar prefix (concat ":" (car prefix))))))))))))
|
2018-10-23 21:46:32 -05:00
|
|
|
|
|
|
|
(cl-defmethod jupyter-completion-post-completion (candidate
|
|
|
|
&context (jupyter-lang julia))
|
|
|
|
"Insert the unicode representation of a LaTeX completion."
|
|
|
|
(if (eq (aref candidate 0) ?\\)
|
|
|
|
(when (get-text-property 0 'annot candidate)
|
|
|
|
(search-backward candidate)
|
|
|
|
(delete-region (point) (match-end 0))
|
|
|
|
;; Alternatively use `julia-latexsub-or-indent', but I have found
|
|
|
|
;; problems with that.
|
|
|
|
(insert (string-trim (get-text-property 0 'annot candidate))))
|
|
|
|
(cl-call-next-method)))
|
|
|
|
|
2018-11-18 22:52:02 -06:00
|
|
|
;;; `markdown-mode'
|
|
|
|
|
2018-10-23 21:46:32 -05:00
|
|
|
(cl-defmethod jupyter-markdown-follow-link (link-text url _ref-label _title-text _bang
|
|
|
|
&context (jupyter-lang julia))
|
|
|
|
"Send a help query to the Julia REPL for LINK-TEXT if URL is \"@ref\".
|
|
|
|
If URL is \"@ref <section>\" then open a browser to the Julia
|
|
|
|
manual for <section>. Otherwise follow the link normally."
|
|
|
|
(if (string-prefix-p "@ref" url)
|
|
|
|
(if (string= url "@ref")
|
|
|
|
;; Links have the form `fun`
|
|
|
|
(let ((fun (substring link-text 1 -1)))
|
|
|
|
(if (not (eq major-mode 'jupyter-repl-mode))
|
|
|
|
(jupyter-inspect fun (1- (length fun)))
|
|
|
|
(goto-char (point-max))
|
|
|
|
(jupyter-repl-replace-cell-code (concat "?" fun))
|
|
|
|
(jupyter-repl-ret)))
|
|
|
|
(let* ((ref (split-string url))
|
|
|
|
(section (cadr ref)))
|
|
|
|
(browse-url
|
|
|
|
(format "https://docs.julialang.org/en/latest/manual/%s/" section))))
|
|
|
|
(cl-call-next-method)))
|
|
|
|
|
2018-11-18 22:52:02 -06:00
|
|
|
;;; `jupyter-repl-after-change'
|
2018-10-24 02:16:24 -05:00
|
|
|
|
2018-10-23 21:46:32 -05:00
|
|
|
(defvar ansi-color-names-vector)
|
|
|
|
|
2019-02-22 20:52:00 -06:00
|
|
|
(defun jupyter-julia-add-prompt (prompt color)
|
|
|
|
"Display PROMPT at the beginning of the cell using COLOR as the foreground.
|
2018-11-02 12:27:57 -05:00
|
|
|
Make the character after `point' invisible."
|
|
|
|
(add-text-properties (point) (1+ (point)) '(invisible t rear-nonsticky t))
|
2019-02-22 20:52:00 -06:00
|
|
|
(let ((ov (make-overlay (point) (1+ (point)) nil t))
|
|
|
|
(md (propertize prompt
|
|
|
|
'fontified t
|
|
|
|
'font-lock-face `((:foreground ,color)))))
|
|
|
|
(overlay-put ov 'after-string (propertize " " 'display md))
|
|
|
|
(overlay-put ov 'evaporate t)))
|
2018-11-02 12:27:57 -05:00
|
|
|
|
2018-10-23 21:46:32 -05:00
|
|
|
(cl-defmethod jupyter-repl-after-change ((_type (eql insert)) beg _end
|
|
|
|
&context (jupyter-lang julia))
|
2018-11-18 22:52:02 -06:00
|
|
|
"Change the REPL prompt when a REPL mode is entered."
|
2018-10-23 21:46:32 -05:00
|
|
|
(when (= beg (jupyter-repl-cell-code-beginning-position))
|
2018-11-02 12:27:57 -05:00
|
|
|
(save-excursion
|
|
|
|
(goto-char beg)
|
2019-03-02 14:30:17 -06:00
|
|
|
(when (and (bound-and-true-p blink-paren-function)
|
|
|
|
(eq (char-syntax (char-after)) ?\)))
|
|
|
|
;; Spoof `last-command-event' so that a "No matching paren" message
|
|
|
|
;; doesn't happen.
|
|
|
|
(setq last-command-event ?\[))
|
2018-11-02 12:27:57 -05:00
|
|
|
(cl-case (char-after)
|
|
|
|
(?\]
|
2019-02-14 16:12:00 -06:00
|
|
|
;; Longer timeout to account for initial Pkg import and compilation.
|
|
|
|
(let* ((jupyter-default-timeout jupyter-long-timeout)
|
|
|
|
(pkg-prompt
|
|
|
|
;; FIXME: This modifies the ans variable in IJulia. Maybe
|
|
|
|
;; evaluate this in the user-expressions of an execute-request?
|
|
|
|
(jupyter-eval "import Pkg; Pkg.REPLMode.promptf()")))
|
2018-11-02 12:27:57 -05:00
|
|
|
(when pkg-prompt
|
2019-02-22 20:52:00 -06:00
|
|
|
(jupyter-julia-add-prompt
|
2018-11-02 12:27:57 -05:00
|
|
|
(substring pkg-prompt 1 (1- (length pkg-prompt)))
|
|
|
|
(aref ansi-color-names-vector 5))))) ; magenta
|
|
|
|
(?\;
|
2019-02-22 20:52:00 -06:00
|
|
|
(jupyter-julia-add-prompt
|
2018-11-02 12:27:57 -05:00
|
|
|
"shell> " (aref ansi-color-names-vector 1))) ; red
|
|
|
|
(?\?
|
2019-02-22 20:52:00 -06:00
|
|
|
(jupyter-julia-add-prompt
|
2018-11-02 12:27:57 -05:00
|
|
|
"help?> " (aref ansi-color-names-vector 3)))))) ; yellow
|
2018-10-23 21:46:32 -05:00
|
|
|
(cl-call-next-method))
|
|
|
|
|
|
|
|
(cl-defmethod jupyter-repl-after-change ((_type (eql delete)) beg _len
|
|
|
|
&context (jupyter-lang julia))
|
2018-11-02 12:27:22 -05:00
|
|
|
"Reset the prompt if needed."
|
2018-10-23 21:46:32 -05:00
|
|
|
(when (= beg (jupyter-repl-cell-code-beginning-position))
|
2018-11-02 12:27:22 -05:00
|
|
|
(jupyter-repl-cell-reset-prompt)))
|
2018-10-23 21:46:32 -05:00
|
|
|
|
2019-03-02 14:29:22 -06:00
|
|
|
;;; REPL font lock
|
|
|
|
|
|
|
|
(defun jupyter-julia--propertize-repl-mode-char (beg end)
|
|
|
|
(jupyter-repl-cell-cond
|
|
|
|
beg end
|
|
|
|
;; Handle Julia package prompt so `syntax-ppss' works properly.
|
|
|
|
(when (and (eq (char-syntax (char-after (point-min))) ?\))
|
|
|
|
(= (point-min)
|
|
|
|
(save-restriction
|
|
|
|
(widen)
|
|
|
|
;; Looks at the position before the narrowed cell-code
|
|
|
|
;; which is why the widen is needed here.
|
|
|
|
(jupyter-repl-cell-code-beginning-position))))
|
|
|
|
(put-text-property
|
|
|
|
(point-min) (1+ (point-min)) 'syntax-table '(1 . ?.)))))
|
|
|
|
|
|
|
|
(cl-defmethod jupyter-repl-after-init (&context (jupyter-lang julia))
|
|
|
|
(add-function
|
|
|
|
:after (local 'syntax-propertize-function)
|
|
|
|
#'jupyter-julia--propertize-repl-mode-char))
|
|
|
|
|
2018-11-18 22:49:52 -06:00
|
|
|
;;; `jupyter-org'
|
|
|
|
|
|
|
|
(cl-defmethod jupyter-org-error-location (&context (jupyter-lang julia))
|
|
|
|
(when (and (re-search-forward "^Stacktrace:" nil t)
|
|
|
|
(re-search-forward
|
|
|
|
"top-level scope at In\\[[0-9]+\\]:\\([0-9]+\\)" nil t))
|
|
|
|
(string-to-number (match-string 1))))
|
|
|
|
|
2019-01-13 02:20:38 -06:00
|
|
|
(cl-defmethod org-babel-jupyter-transform-code (code changelist &context (jupyter-lang julia))
|
|
|
|
(when (plist-get changelist :dir)
|
2019-03-12 14:24:02 -05:00
|
|
|
(setq code
|
|
|
|
(format "\
|
2019-03-12 15:13:36 -05:00
|
|
|
let __JUPY_saved_dir = pwd()
|
|
|
|
cd(\"%s\")
|
|
|
|
try
|
|
|
|
%s
|
|
|
|
finally
|
|
|
|
cd(__JUPY_saved_dir)
|
|
|
|
end
|
2019-03-12 14:24:02 -05:00
|
|
|
end"
|
|
|
|
(plist-get changelist :dir) code)))
|
2019-01-13 02:20:38 -06:00
|
|
|
code)
|
|
|
|
|
2018-10-23 21:46:32 -05:00
|
|
|
(provide 'jupyter-julia)
|
|
|
|
|
|
|
|
;;; jupyter-julia.el ends here
|