📄 matlab-eei.el
字号:
;;; matlab-eei.el --- major mode for MATLAB dot-m files;;; $Revision: 1.2.4.2 $;; Author: Paul Kinnucan <paulk@mathworks.com>;; Maintainer: Paul Kinnucan <paulk@mathworks.com>;; Created: May 2, 2000;; Version: 1.0;; Keywords: MATLAB;;;; LCD Archive Entry:;; matlab|Paul Kinnucan|paulk@mathworks.com|;; Major mode for editing and debugging MATLAB dot-m files|;; May-00|1.0|~/modes/matlab-eei.el|;;;; Copyright 2000-2004 The MathWorks, Inc.;;;; 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, 675 Mass Ave, Cambridge, MA 02139, USA.;;;;; Commentary:;;;;; Finding Updates:;;;; The latest stable version of matlab.el can be found here:;;;; ftp://ftp.mathworks.com/pub/contrib/emacs_add_ons/matlab-eei.el;;;;; Installation:;;(require 'cl)(require 'wid-edit)(require 'matlab-vdm)(defconst matlab-eei-xemacsp (string-match "XEmacs" (emacs-version)) "Non-nil if we are running in the XEmacs environment.")(defconst matlab-eei-new-m-file-name "untitled" "Name of a new m-file.")(defvar matlab-eei-new-m-file-count 0 "Number of new m-files created thus far in this session.")(defun matlab-eei-eval-output (lisp-form) "Evaluates Lisp forms output by eei." ;; (message lisp-form) (condition-case error-desc (eval (read lisp-form)) (error (message "Error: evaluating output from the MATLAB EEI caused a Lisp error.") (message "EEI output: %s." lisp-form) (message "Lisp error: %s" error-desc))))(defun matlab-eei-extract-lisp-form (eei-output)"Extract first complete Lisp form from eei output.Returns (FORM . REMAINDER) where FORM is the Lisp formor the null string and REMAINDER is the remainder of theeei output following the Lisp form." (let ((lisp-form "") (remainder "") (level 0) in-string-p in-escape-p (curr-pos 1) (output-length (length eei-output)) command-end lisp-form-end) (setq lisp-form-end (catch 'found-lisp-form ;; skip over any inital white space. (string-match "^[\n\t ]*(" eei-output) (setq curr-pos (match-end 0)) (while (< curr-pos output-length) (cond ;; Current character = left slash (escape) ((equal (aref eei-output curr-pos) ?\\) (if in-string-p (setq in-escape-p (not in-escape-p)))) ;; Current character = quotation mark ((equal (aref eei-output curr-pos) ?\") (if in-string-p (if in-escape-p (setq in-escape-p nil) (setq in-string-p nil)) (setq in-string-p t))) ;; Current character = right paren ((and (not in-string-p) (equal (aref eei-output curr-pos) ?\))) (if (= level 0) (throw 'found-lisp-form curr-pos) (setq level (1- level)) (if (< level 0) (error "Error parsing eei output.")))) ;; Current character = left paren ((and (not in-string-p) (equal (aref eei-output curr-pos) ?\() (setq level (1+ level)))) (t (if in-escape-p (setq in-escape-p nil)))) (setq curr-pos (1+ curr-pos))) -1)) (if (> lisp-form-end 1) (progn (setq lisp-form (substring eei-output 0 (1+ lisp-form-end))) (when (< lisp-form-end (1- output-length)) (setq remainder (substring eei-output (1+ lisp-form-end) output-length)) (if (string-match "(" remainder) (setq remainder (substring remainder (string-match "(" remainder))) (setq remainder "")))) (setq remainder eei-output)) (cons lisp-form remainder)))(defvar matlab-eei-standard-comint-filter nil "Standard process filter for comint buffers.")(defvar matlab-eei-output nil "Contains output from the eei.")(defun matlab-eei-asynch-output-listener (process output) "Listens at the eei socket for asynchronous eei output." (let* ((combined-output (concat matlab-eei-output output)) (parsed-output (if (string-match "^[\n\t ]*(" combined-output) (matlab-eei-extract-lisp-form combined-output))) (lisp-form (car parsed-output)) (remainder (cdr parsed-output)) forms) ;; (message "asynch form: %s" lisp-form) ;; (message "asynch remainder: %s" remainder) ;; Insert output in MATLAB EEI buffer. (funcall matlab-eei-standard-comint-filter process output) ;; Extract forms from eei output. (while (not (string= lisp-form "")) ;; (message " evaluating %s" lisp-form) (setq forms (append forms (list lisp-form))) (setq parsed-output (matlab-eei-extract-lisp-form remainder)) (setq lisp-form (car parsed-output)) (setq remainder (cdr parsed-output))) (setq matlab-eei-output remainder) (if forms (mapc (lambda (form) (matlab-eei-eval-output form)) forms))))(defvar matlab-eei-process nil "Process object for MATLAB EEI process.")(defun matlab-eei-process-sentinel (process status) "Sentinel function invoked by Emacs when MATLAB exits.This function invokes the standard Emacs exit function`save-buffers-kill-emacs'." (if (string-match "exited" status) (save-buffers-kill-emacs)));;;###autoload(defun matlab-eei-connect () "Connects Emacs to MATLAB's external editor interfaceon socket 5600." (let* ((matlab-editor-port 5600) (matlab-eei-buffer-name "MATLAB EEI") (matlab-eei-buffer (make-comint "MATLAB EEI" (cons (system-name) matlab-editor-port)))) (setq matlab-eei-process (get-process matlab-eei-buffer-name)) (process-kill-without-query matlab-eei-process) (set-process-sentinel matlab-eei-process 'matlab-eei-process-sentinel) (setq matlab-eei-standard-comint-filter 'comint-output-filter) (set-process-filter matlab-eei-process 'matlab-eei-asynch-output-listener) (add-hook 'matlab-mode-hook 'matlab-eei-setup-matlab-mode) (setq matlab-eei-new-m-file-count 0) (matlab-eei-send-mcmd "get_stop_conditions") (matlab-eei-send-mcmd "update_debug_mode_status")))(defun matlab-eei-setup-matlab-mode () "Sets up matlab-mode in a newly opened m-file." (matlab-eei-minor-mode) (make-local-hook 'after-save-hook) (add-hook 'after-save-hook 'matlab-eei-reset-breakpoints nil t) (make-local-hook 'after-revert-hook) (add-hook 'after-revert-hook 'matlab-eei-reset-breakpoints nil t) (matlab-eei-send-mcmd (format "init_breakpoints %s" (buffer-file-name))) (matlab-eei-send-mcmd "update_stack") (turn-on-matlab-vdm-mode))(defun matlab-eei-send-mcmd (command &optional synch) "Sends an MEI M Command to the MATLAB side of the MEI interfaceand waits for a response." ;; (message "mcmd: command = %s synch = %s" command ;; (if synch "yes" "no")) (if (and matlab-eei-process (eq (process-status matlab-eei-process) 'open)) (progn (setq matlab-eei-last-event nil) (process-send-string matlab-eei-process (concat command "\n")) (if synch (accept-process-output matlab-eei-process 10 0))))); (defun matlab-eei-eval-m-expr (expr); "Send EXPR to MATLAB for evaluation. EXPR can be any valid; MATLAB expression, including multiline expressions. This; command prepends and appends a line containing a single quotation; mark to EXPR to indicate its beginning and end, respectively.; For this reason, EXPR may not contain a line containing a single; quotation mark."; (if (string-match "\n\"\n" expr); (error (concat "Cannot evaluate an expression containing "; "a quotation mark by itself on a line.")); (process-send-string matlab-eei-process ; (format "eval\n\"\n%s\n\"\n" expr))))(defun matlab-eei-eval-m-expr (expr) "Send EXPR to MATLAB for evaluation. EXPR can be any validMATLAB expression, including multiline expressions." (process-send-string matlab-eei-process (format "eval %d\n%s" (length expr) expr))) (defvar matlab-eei-last-event nil"Debug event that followed the last command.")(defun matlab-eei-wait-for-event () "Waits for an event to occur. Returns the event or \"timeout\" if noevent occurred within 10 seconds." (if matlab-eei-last-event matlab-eei-last-event (accept-process-output matlab-eei-process 10 0) (or matlab-eei-last-event "timeout")))(defun matlab-eei-mcmd-quit () "Tells MATLAB to exit from eei mode." (matlab-eei-send-mcmd "quit"))(defun matlab-eei-ready () "E-message sent by MATLAB when it is ready to accept a command." (setq matlab-eei-last-event nil))(defcustom matlab-eei-reuse-frame t "If non-nil causes Emacs to reuse the current frame toopen a new file." :group 'matlab :type 'boolean) (defun matlab-eei-find-file (filename &optional wildcards) "Open file FILENAME." (let ((file (if (string-match "\\.p$" filename) (concat (file-name-sans-extension filename) ".m") filename))) (if (file-exists-p file) (let ((buffer (find-file-noselect file t nil))) (if (string= (buffer-name (current-buffer)) matlab-eei-stack-navigator-name) (other-window 1)) (if (listp buffer) (progn (setq buffer (nreverse buffer)) (if matlab-eei-reuse-frame (progn (switch-to-buffer-other-frame (car buffer)) (setq buffer (cdr buffer)))) (mapcar 'switch-to-buffer buffer)) (if matlab-eei-reuse-frame (switch-to-buffer buffer) (switch-to-buffer-other-frame buffer)))) (message "Error: Cannot find %s" file))))(defun matlab-eei-new-document (content) "Creates a new M-file buffer containing CONTENT.This function implements the newDocument method ofMATLAB's ExternalEditorInterface." (setq matlab-eei-new-m-file-count (1+ matlab-eei-new-m-file-count)) (switch-to-buffer (create-file-buffer (if (= matlab-eei-new-m-file-count 1) (concat matlab-eei-new-m-file-name ".m") (concat matlab-eei-new-m-file-name (number-to-string matlab-eei-new-m-file-count) ".m"))) nil) (if (not (string= content "null")) (insert content)) (matlab-mode))(defun matlab-eei-open-document (docpath) "Opens the document specified by DOCPATH.This function implements the OpenDocument methodof the MATLAB ExternalEditorInterface." (matlab-eei-find-file docpath))(defun matlab-eei-open-document-to-line (docpath line to-front highlight) "Opens the document specified by DOCPATH to theline specified by LINE. If TO-FRONT is non-nil, this functionraises the Emacs frame to the front of the user's desktop. If HIGHLIGHT is non-nil, this function highlights the line specified byLINE-NO. This function implements the openDocumentToLine method ofMATLAB's ExternalEditorInterface." ;; (message "open-document-to-line: doc: %s, line: %s" docpath line) (matlab-eei-find-file docpath) ;; If the user issues a step command and the current line is ;; the last line in a function, MATLAB issues an open-document-to-line ;; command with the line number equal to the negative of the last line ;; in the M-file and then issues a stop event with the same negative ;; line number. In this case, we ignore the line positioning. ;; (if (> line -1) (progn (goto-line line) (if matlab-eei-matlab-in-debug-mode-p (matlab-eei-move-debug-cursor docpath line)) (if to-front (raise-frame)) ;; The following never occurs for some reason. ;; (if highlight ;; (matlab-eei-breakpoint-highlight-create line)) ))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;; Set and Clear Breakpoints ;;;; ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;(defcustom matlab-eei-breakpoint-marker-colors (cons "black" "orange red")"*Specifies the foreground and background colors of the debugger'sbreakpoint marker." :group 'matlab :type '(cons :tag "Colors" (string :tag "Foreground") (string :tag "Background")) :set '(lambda (sym val) (make-face 'matlab-eei-breakpoint-marker) (set-face-foreground 'matlab-eei-breakpoint-marker (car val)) (set-face-background 'matlab-eei-breakpoint-marker (cdr val)) (set-default sym val)))(defcustom matlab-eei-provisional-breakpoint-marker-colors (cons "red" "orange")"*Specifies the foreground and background colors of the debugger'sprovisional breakpoint marker. A provisional breakpoint is a breakpoint that MATLABhas not yet confirmed as existing." :group 'matlab :type '(cons :tag "Colors" (string :tag "Foreground") (string :tag "Background")) :set '(lambda (sym val) (make-face 'matlab-eei-provisional-breakpoint-marker)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -