emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/raku-mode 44faa06bba 197/253: Use comint.


From: ELPA Syncer
Subject: [nongnu] elpa/raku-mode 44faa06bba 197/253: Use comint.
Date: Sat, 29 Jan 2022 08:28:57 -0500 (EST)

branch: elpa/raku-mode
commit 44faa06bba8582166b13f53c86a6925414aa6a44
Author: Matias Linares <matiaslina@gmail.com>
Commit: Matias Linares <matiaslina@gmail.com>

    Use comint.
---
 perl6-mode.el |   8 +++-
 perl6-repl.el | 141 ++++++++++++++++++++++------------------------------------
 2 files changed, 60 insertions(+), 89 deletions(-)

diff --git a/perl6-mode.el b/perl6-mode.el
index e21e8cb353..72e899a292 100644
--- a/perl6-mode.el
+++ b/perl6-mode.el
@@ -50,7 +50,7 @@
     (define-key map (kbd "C-c C-r") 'perl6-send-region-to-repl)
     (define-key map (kbd "C-c C-h") 'perl6-send-buffer-to-repl)
     map)
-  "Keymap for `perl6-mode'")
+  "Keymap for `perl6-mode'.")
 
 (easy-menu-define perl6-mode-menu perl6-mode-map
   "Menu for `perl6-mode'"
@@ -77,6 +77,12 @@
   (setq-local comment-start-skip "#+ *")
   (setq-local comment-use-syntax t)
   (setq-local comment-end "")
+   ;; REPL
+  (setq comint-prompt-regexp perl6-prompt-regexp)
+  (setq comint-prompt-read-only t)
+  ;; this makes it so commands like M-{ and M-} work.
+  (set (make-local-variable 'paragraph-separate) "\\'")
+  (set (make-local-variable 'paragraph-start) perl6-prompt-regexp)
   ;; Indentation (see SMIE in the Emacs manual)
   ;; TODO add rules for HEREDOC indentation
   (smie-setup perl6-smie-grammar #'perl6-smie-rules
diff --git a/perl6-repl.el b/perl6-repl.el
index 549c457073..0dab25cdf6 100644
--- a/perl6-repl.el
+++ b/perl6-repl.el
@@ -1,108 +1,73 @@
-(require 'term)
+;;; perl6-repl -- Repl for support Raku
 
-(defun perl6-repl-send-line (astring)
-  ;;send home if we have a regular perl6 prompt this line
-  ;;otherwise, simulate "C-b" until we get to the beginning
-  (if (equal (buffer-substring (- (line-beginning-position) 2) 
(line-beginning-position)) "> ") (term-send-home)
-    (progn (while (> (- (point) (line-beginning-position)) 2) 
(term-send-string "\^b"))))
-  (term-send-raw-string astring) ;send the argument as perl6 input
-  (term-send-raw-string "\^M") ;sends a return to execute astring
-  ;;(term-send-raw-string "\^y") ;sends a C-y, which yanks the killed line
-  )
+;;; Commentary:
+;; Basic repl support for Raku
 
-(setq perl6-repl--buffer-name "Raku REPL")
-
-(defun perl6-repl--buffer-name-earmuf ()
-  (concat "*" perl6-repl--buffer-name "*"))
+;;; Code:
+(require 'comint)
 
 (defcustom perl6-exec-path "raku"
   "Raku executable path."
   :type 'string
   :group 'perl6)
 
-(defun perl6-exec-path-exists-p ()
-  (or (file-executable-p perl6-exec-path)
-      (executable-find perl6-exec-path)))
-
-(defun perl6-repl ()
-  "Runs Perl6 in a `term' buffer in another window."
-  (interactive)
-  (let ((termbuf (apply 'make-term perl6-repl--buffer-name perl6-exec-path 
nil)))
-    (set-buffer termbuf)
-    (term-mode)
-    (term-set-escape-char 24) ;this sets the escape char to C-x instead of C-c
-    (term-char-mode)
-    (switch-to-buffer-other-window termbuf)))
-
+(defcustom perl6-exec-arguments ""
+  "Raku command line arguments."
+  :type 'string
+  :group 'perl6)
 
-(defun perl6-repl-ready-p ()
-  (or (< (point) 2)
-      (not (equal (buffer-substring (- (point)2)
-                                    (point))
-                  "> "))))
+(defvar perl6-prompt-regexp "^> "
+  "Prompt for `run-perl6'.")
 
-(defun perl6-create-new-repl ()
-  (progn (perl6-repl)
-         (while (perl6-repl-ready-p)
-           (sit-for 0.1))))
+(defvar perl6-buffer-name "Raku REPL"
+  "Buffer name for `run-perl6.")
 
-(defun perl6-send-line-to-repl (&optional line)
+(defun run-perl6 ()
+  "Run an inferior instance of `raku' inside Emacs."
   (interactive)
+  (let* ((perl6-program perl6-exec-path)
+         (check-proc (comint-check-proc perl6-buffer-name))
+         (buffer (apply 'make-comint-in-buffer
+                        perl6-buffer-name
+                        check-proc
+                        perl6-exec-path
+                        '()
+                        (split-string perl6-exec-arguments))))
+    (display-buffer buffer)))
+
+(defun perl6-comint-get-process ()
+  "Raku process name."
+  (get-process perl6-buffer-name))
 
-  (if (perl6-exec-path-exists-p)
-      (let ((jbuf (get-buffer (perl6-repl--buffer-name-earmuf)))
-            (cbuf (current-buffer))
-            (cwin (selected-window))
-            (pos (point))
-            (linecontents
-             (progn (when line ;if a line is passed to the function, go there
-                      (goto-char (point-min))
-                      (forward-line (- line 1)))
-                    (buffer-substring (line-beginning-position) 
(line-end-position))))
-            ) ;save pos of start of next line
-        (if jbuf (switch-to-buffer jbuf)
-          ;;if there is not a perl6 REPl open, open it and wait for prompt
+(defun perl6-send-string-to-repl (str)
+  "Send STR to the repl."
+  (comint-send-string (perl6-comint-get-process)
+                      (concat str "\n")))
 
-          (perl6-create-new-repl))
-        (perl6-repl-send-line linecontents)
-        (select-window cwin)
-        (switch-to-buffer cbuf)
-        (goto-char pos))
-    (message "Cannot execute %s" perl6-exec-path)))
+(defun perl6-send-line-to-repl ()
+  "Send a line to the repl."
+  (interactive)
+  (run-perl6)
+  (let ((str (buffer-substring-no-properties (line-beginning-position) 
(line-end-position))))
+    (perl6-send-string-to-repl str)))
 
-(defun perl6-send-region-to-repl (begin end)
-  (interactive (if (use-region-p)
-                   (list (region-beginning)
-                         (region-end))
-                 (list nil nil)))
-  (if (and begin end (perl6-exec-path-exists-p))
-      (let ((jbuf (get-buffer (perl6-repl--buffer-name-earmuf)))
-            (cbuf (current-buffer))
-            (cwin (selected-window))
-            (pos (mark))
-            (contents (buffer-substring-no-properties begin end)))
-        (if jbuf (switch-to-buffer jbuf)
-          ;;if there is not a perl6 REPl open, open it and wait for prompt
-          (perl6-create-new-repl))
-        (mapc 'perl6-repl-send-line (split-string contents "\n+"))
-        (select-window cwin)
-        (switch-to-buffer cbuf))
-    (message "Cannot execute %s" perl6-exec-path)))
+(defun perl6-send-region-to-repl ()
+  "Send a region to the repl."
+  (interactive)
+  (run-perl6)
+  (if (region-active-p)
+      (let ((buf (buffer-substring-no-properties (region-beginning)
+                                                 (region-end))))
+        (perl6-send-string-to-repl buf))
+    (message "No region selected")))
 
 (defun perl6-send-buffer-to-repl ()
+  "Send a buffer to the repl."
   (interactive)
-  (if (perl6-exec-path-exists-p)
-      (let ((jbuf (get-buffer (perl6-repl--buffer-name-earmuf)))
-            (cbuf (current-buffer))
-            (cwin (selected-window))
-            (contents (buffer-string)))
-        (if jbuf (switch-to-buffer jbuf)
-          (perl6-create-new-repl))
-        ;; Send te line to the repl
-        (set-text-properties 0 (length contents) nil contents)
-        (mapc 'perl6-repl-send-line (split-string contents "\n+"))
-        (select-window cwin)
-        (switch-to-buffer cbuf))
-    (message "Cannot execute %s" perl6-exec-path)))
+  (run-perl6)
+  (let ((buf (buffer-substring-no-properties (point-min)
+                                             (point-max))))
+    (perl6-send-string-to-repl buf)))
 
 (provide 'perl6-repl)
+;;; perl6-repl.el ends here



reply via email to

[Prev in Thread] Current Thread [Next in Thread]