Skip to content

Commit be0357c

Browse files
committed
Enable ? ] ; magics in inferior-julia and cleanup
This commit cleans up the code for inferior-julia and adds highlighting and linking for error messages. It also allows ? ] and ; REPL magic prefixes to work as expected.
1 parent 455cf88 commit be0357c

File tree

1 file changed

+80
-26
lines changed

1 file changed

+80
-26
lines changed

julia-mode.el

Lines changed: 80 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -863,49 +863,105 @@ following commands are defined:
863863
;; Code for `inferior-julia-mode'
864864
(require 'comint)
865865

866-
(defcustom julia-program "julia"
866+
(defcustom inferior-julia-program "julia"
867867
"Path to the program used by `inferior-julia'."
868868
:type 'string
869869
:group 'julia)
870870

871-
(defcustom julia-arguments '("-i" "--color=yes")
872-
"Commandline arguments to pass to `julia-program'."
871+
(defcustom inferior-julia-buffer "*Inferior Julia*"
872+
"Name of buffer for running an inferior Julia process."
873+
:type 'string
874+
:group 'julia)
875+
876+
(defcustom inferior-julia-arguments '("-i" "--color=yes")
877+
"Commandline arguments to pass to `inferior-julia-program'."
873878
:type '(repeat (string :tag "argument"))
874879
:group 'julia)
875880

876-
(defvar julia-prompt-regexp "^\\w*> "
881+
(defcustom inferior-julia-prompt-read-only comint-prompt-read-only
882+
"If non-nil, the Julia prompt is read only."
883+
:type 'boolean)
884+
885+
(defvar inferior-julia-prompt-regexp "^\\w*> "
877886
"Regexp for matching `inferior-julia' prompt.")
878887

888+
(defvar inferior-julia-error-regexp-alist
889+
(list (list (rx line-start " " ?\[ (1+ num) "] " (1+ nonl) " at "
890+
(group (1+ (not (any ?\n ?:))))
891+
?:
892+
(group (1+ num))) 1 2))
893+
"Value for `compilation-error-regexp-alist' in inferior Julia.")
894+
879895
(defvar inferior-julia-mode-map
880896
(nconc (make-sparse-keymap) comint-mode-map)
881897
"Basic mode map for `inferior-julia-mode'.")
882898

899+
(defvar inferior-julia-mode-syntax-table
900+
(make-syntax-table julia-mode-syntax-table)
901+
"Syntax table for use in `inferior-julia-mode' buffers.")
902+
883903
;;;###autoload
884-
(defun inferior-julia ()
904+
(defun inferior-julia (&optional arg)
885905
"Run an inferior instance of julia inside Emacs."
886-
(interactive)
887-
(let ((julia-program julia-program))
888-
(when (not (comint-check-proc "*Julia*"))
889-
(apply #'make-comint-in-buffer "Julia" "*Julia*"
890-
julia-program nil julia-arguments))
891-
(pop-to-buffer-same-window "*Julia*")
892-
(inferior-julia-mode)))
893-
894-
(defun inferior-julia--initialize ()
895-
"Helper function to initialize `inferior-julia'."
896-
(setq comint-use-prompt-regexp t))
897-
898-
(define-derived-mode inferior-julia-mode comint-mode "Julia"
899-
"Major mode for `inferior-julia'.
900-
906+
(interactive "P")
907+
(let ((buffer (get-buffer-create inferior-julia-buffer)))
908+
(unless arg
909+
(pop-to-buffer buffer))
910+
(with-current-buffer buffer
911+
(let ((proc (apply #'make-comint
912+
(substring inferior-julia-buffer 1 -1)
913+
inferior-julia-program nil
914+
inferior-julia-arguments)))
915+
;; Pkg and REPL modules must be accessible for ] and ? magics
916+
(comint-send-string
917+
proc "baremodule _InferiorJulia import Pkg, REPL end;"))
918+
(inferior-julia-mode))
919+
buffer))
920+
921+
(defun inferior-julia--send (proc string)
922+
"Send STRING to inferior Julia PROC.
923+
Checks if first character in STRING is special. \"?\" invokes Julia
924+
help, \"]\" uses the Pkg repl, and \";\" sends shell commands."
925+
(let* ((c (aref string 0))
926+
(wrapper (cond
927+
((char-equal c ??)
928+
(cons "eval(_InferiorJulia.REPL.helpmode(\"" "\"))"))
929+
((char-equal c ?\])
930+
(cons "_InferiorJulia.Pkg.pkg\"" "\""))
931+
((char-equal c ?\;)
932+
(cons "Base.repl_cmd(`" "`, stdout)"))
933+
(t
934+
(cons "" ""))))
935+
(wrapped-string (if (eq (length (car wrapper)) 0)
936+
string
937+
(substring string 1))))
938+
(comint-simple-send
939+
proc
940+
(concat (car wrapper) wrapped-string (cdr wrapper)))))
941+
942+
(define-derived-mode inferior-julia-mode comint-mode "Inferior Julia"
943+
"Major mode for interacting with an inferior Julia process.
944+
945+
Key bindings:
901946
\\<inferior-julia-mode-map>"
902-
nil "Julia"
947+
:group 'julia
903948
:abbrev-table julia-mode-abbrev-table
904-
(setq-local comint-prompt-regexp julia-prompt-regexp)
905-
(setq-local comint-prompt-read-only t)
949+
(setq comint-prompt-regexp inferior-julia-prompt-regexp)
950+
(setq-local comint-prompt-read-only inferior-julia-prompt-read-only)
951+
952+
(setq-local compilation-error-regexp-alist inferior-julia-error-regexp-alist)
953+
(compilation-shell-minor-mode 1)
954+
(compilation-forget-errors)
955+
906956
(setq-local font-lock-defaults '(julia-font-lock-keywords t))
907-
(setq-local paragraph-start julia-prompt-regexp)
957+
(setq-local paragraph-start inferior-julia-prompt-regexp)
908958
(setq-local indent-line-function #'julia-indent-line)
959+
(setq-local comment-use-syntax t)
960+
(setq-local comment-start "# ")
961+
(setq-local comment-start-skip "#+\\s-*")
962+
963+
(setq-local comint-input-sender #'inferior-julia--send)
964+
909965
(when julia-force-tab-complete
910966
(setq-local tab-always-indent 'complete)
911967
(abbrev-mode 1))
@@ -914,8 +970,6 @@ following commands are defined:
914970
(add-hook 'completion-at-point-functions
915971
#'julia-mode-latexsub-completion-at-point-around nil t))
916972

917-
(add-hook 'inferior-julia-mode-hook #'inferior-julia--initialize)
918-
919973
;;;###autoload
920974
(defalias 'run-julia #'inferior-julia
921975
"Run an inferior instance of julia inside Emacs.")

0 commit comments

Comments
 (0)