(defun avrforth-term-simple-send (proc string) "Default function for sending to PROC input STRING. This just sends STRING plus a newline. To override this, set the hook `term-input-sender'." (term-send-string proc string) (term-send-string proc "\r")) (add-hook 'term-mode-hook (function (lambda () ;(term-line-mode) (use-local-map term-mode-map) (setq term-prompt-regexp "^>+ *") (setq term-input-sender (function avrforth-term-simple-send))))) (defun avrforth-quit-terminal () (interactive) (setq proc (get-buffer-process avrforth-terminal-buffer) avrforth-terminal-buffer nil) (delete-process proc)) (defun avrforth-fix-forth-mode () (fundamental-mode) ;; to be sure ) (define-key avrforth-mode-map "\M-\r" 'avrforth-send-paragraph) (define-key avrforth-mode-map "\M-\r" 'avrforth-send-paragraph) (define-key avrforth-mode-map "\C-c\C-p" 'avrforth-send-paragraph) (define-key avrforth-mode-map "\C-c\C-r" 'avrforth-send-region) (define-key avrforth-mode-map "\C-c\C-b" 'avrforth-send-buffer) (define-key avrforth-mode-map "\C-c\C-t" 'avrforth-run-terminal) (defvar avrforth-terminal-buffer nil) (defvar avrforth-terminal-port "/dev/ttyUSB0") (defvar avrforth-terminal-speed 38400) (defvar avrforth-term-input-chunk-size 1) (defvar avrforth-term-input-delay 10) (defun avrforth-run-terminal (port speed) (interactive (if current-prefix-arg (list (serial-read-name) (serial-read-speed)) (list avrforth-terminal-port avrforth-terminal-speed))) (if avrforth-terminal-buffer (pop-to-buffer avrforth-terminal-buffer) (setq avrforth-terminal-buffer (serial-term port speed)))) (defun avrforth-send-region (start end) "Send the current region to the Forth terminal." (interactive "r") (let ((oldbuf (current-buffer))) (with-temp-buffer (insert-buffer-substring oldbuf start end) (format-encode-buffer 'avrforth) (let ((ebuffer (current-buffer)) (estart (point-min)) (eend (point-max))) (set-buffer avrforth-terminal-buffer) (insert-buffer-substring ebuffer estart eend) (insert "\n"))))) (defun term-slow-send-string (str &optional proc) "Send to PROC the contents of STR as input. This is equivalent to `process-send-string', except that long input strings are broken up into chunks of size `term-input-chunk-size'. Processes are given a chance to output between chunks. This can help prevent processes from hanging when you send them long inputs on some OS's." (when (not proc) (setq proc (get-buffer-process (current-buffer)))) (let* ((len (length str)) (i (min len avrforth-term-input-chunk-size))) (process-send-string proc (substring str 0 i)) (while (< i len) (let ((next-i (+ i avrforth-term-input-chunk-size))) (accept-process-output proc); 0 avrforth-term-input-delay) (process-send-string proc (substring str i (min len next-i))) (setq i next-i))))) (defun avrforth-send-region (start end) "Send the current region to the Forth terminal." (interactive "r") (let ((oldbuf (current-buffer))) (with-temp-buffer (insert-buffer-substring oldbuf start end) (format-encode-buffer 'avrforth) (let ((string (concat (buffer-substring (point-min) (point-max)) "\n"))) (set-buffer avrforth-terminal-buffer) (term-slow-send-string (replace-regexp-in-string "\n" "\r" string)))))) (defun avrforth-end-of-paragraph () (if (looking-at "[\t\n ]+") (skip-chars-backward "\t\n ")) (if (not (re-search-forward "\n[ \t]*\n" nil t)) (goto-char (point-max)))) (defun avrforth-send-paragraph () "Send the current or the previous paragraph to the Forth process" (interactive) (let (end) (save-excursion (avrforth-end-of-paragraph) (skip-chars-backward "\t\n ") (setq end (point)) (if (re-search-backward "\n[ \t]*\n" nil t) (setq start (point)) (goto-char (point-min))) (skip-chars-forward "\t\n ") (avrforth-send-region (point) end)))) (defun avrforth-send-buffer () "Send the current buffer to the Forth process." (interactive) (if (eq (current-buffer) avrforth-terminal-buffer) (error "Not allowed to send this buffer's contents to Forth")) (avrforth-send-region (point-min) (point-max)))