;; matlab-mode.el - major mode for MATLAB dot-m files ;; ;; This major mode for GNU emacs provides support for editing MATLAB dot-m ;; files. It automatically indents for block structures, line continuations ;; (e.g., ...), and comments. The usual paren matching support is included. ;; Filling and auto-fill works for comment lines. For convenient use add ;; something like the following to your .emacs start-up file: ;; (autoload 'matlab-mode \"matlab-mode\" \"Enter Matlab mode.\" t) ;; (setq auto-mode-alist (cons '(\"\\.m$\" . matlab-mode) auto-mode-alist)) ;; (defun my-matlab-mode-hook () ;; (setq fill-column 76) ;; (turn-on-auto-fill)) ;; (setq matlab-mode-hook 'my-matlab-mode-hook) ;; Put this file where load-path can find it and ... enjoy. ;; For Lucid Emacs, add ``(font-lock-mode 1)'' to your matlab-mode-hook. ;; ;; Warning - ;; MATLAB has a syntax which is it quite difficult to parse properly in ;; elisp. Therefore, there are certain limitations in this version of ;; matlab-mode. For example, don't expect matlab-mode to be able to ;; know a quoted keyword (e.g. 'for') is text and not another language ;; element. Fixing this is *not* easy. If you want to try, go for it! ;; (Actually, I know how to do this, but I'm too lazy now and it would ;; make the code less efficient. - mrw) ;; ;; Copyright (C) 1991-1993 Matthew R. Wette. ;; Everyone is granted permission to copy, modify and redistribute this ;; file provided: ;; 1. All copies contain this copyright notice. ;; 2. All modified copies carry a prominant notice stating who made the last ;; modification and the date of such modification. ;; 3. No direct charge is made for this software or works derived from it. ;; This software may be distributed with licensed software as long as ;; no itemized charge is made for this specific code. ;; ;; Version 1.06.0, Dated 23Nov93 ;; ;; 23Nov93 by Matt Wette ;; added Lucid emacs, GNU emacs font-lock and lhilit support; repaired ;; mtlb-block-{beg,end}-kw (Thanks to Dave Mellinger ) ;; removed string delim entry from matlab-mode-syntax-table (MATLAB lang ;; sucks here -- why not use \" for strings?). Released vers 1.06.0 ;; ;; 10Aug93 by Matt Wette ;; added matlab-indent-end-before-return; indent may be fixed now ;; still not working for emacs 19 ;; ;; 02Aug93 by Matt Wette ;; fixed error in mtlb-calc-indent; bumped version to 1.05.1; added ;; mtlb-prev-line; bumped version to 1.05.3; added mtlb-calc-blok-indent ;; ;; 01Aug93 by Matt Wette ;; Fixed bug which treated form as block-begin keyword. Reworked ;; mtlb-calc-indent -- seems to work better w/ redundant cont lines now. ;; Bumbed version to 1.05. ;; ;; 13Jun93 by Matt Wette ;; Changed `linea' to `lattr', `linet' to `ltype', fixed ;; Bumped version number from 1.03bb to 1.04. ;; ;; 02May91 by Matt Wette, mwette@csi.jpl.nasa.gov ;; Added matlab-auto-fill for auto-fill-hook so that this mode doesn't ;; try to fill matlab code, just comments. ;; ;; 22Apr91 by Matt Wette, mwette@csi.jpl.nasa.gov ;; Changed `mtlb-ltype-cont' to `mtlb-lattr-cont', `mtlb-ltype-comment-on- ;; line' to `mtlb-lattr-comment' and `mtlb-ltype-unbal-mexp' to `mtlb- ;; lattr-unbal-mext' to emphasize that these are line attributes and not ;; line types. Modified `matlab-line-type' to reflect the change ini ;; logic. ;; ;; 18Apr91 by Matt Wette, mwette@csi.jpl.nasa.gov ;; Modified matlab-comment-return so that when hit on a line with a ;; comment at the end it will go to the comment column. To get the ;; comment indented with the code, just hit TAB. ;; ;; 17Apr91 by Matt Wette, mwette@csi.jpl.nasa.gov ;; Received critique from gray@scr.slb.com. Changed ml- to mtlb- due to ;; possible conflict with mlsupport.el routines. Added matlab-comment ;; -line-s and -on-line-s. Fixed bug in matlab-comment (set-fill-prefix). ;; matlab-comment-return now works if called on a non-comment line. ;; ;; 04Mar91 by Matt Wette, mwette@csi.jpl.nasa.gov ;; Added const matlab-indent-before-return. Released Version 1.02. ;; ;; 02Feb91 by Matt Wette, mwette@csi.jpl.nasa.gov ;; Changed names of ml-*-line to ml-ltype-*. Cleaned up a lot. Added ;; ml-format-comment-line, fixed ml-format-region. Changed added `-s' ;; on end of matlab-comment-region string. Justify needs to be cleaned ;; up. ;; ;; Fri Feb 1 09:03:09 1991; gray@scr.slb.com ;; Add function matlab-comment-region, which inserts the string ;; contained in the variable matlab-comment-region at the start ;; of every line in the region. With an argument the region is ;; uncommented. [Straight copy from fortran.el] ;; ;; 25Jan91 by Matt Wette, mwette@csi.jpl.nasa.gov ;; Got indentation of matrix expression to work, I think. Also, ;; added tabs to comment start regular-expression. ;; ;; 14Jan91 by Matt Wette, mwette@csi.jpl.nasa.gov ;; Added functions (ml-unbal-matexp ml-matexp-indent) for matrix ;; expressions. ;; ;; 07Jan91 by Matt Wette, mwette@csi.jpl.nasa.gov ;; Many changes. Seems to work reasonably well. Still would like ;; to add some support for filling in comments and handle continued ;; matrix expressions. Released as Version 1.0. ;; ;; 04Jan91 by Matt Wette, mwette@csi.jpl.nasa.gov ;; Created. Used eiffel.el as a guide. ;; ;; Constants which the user can change (defconst matlab-indent-level 2 "*The indentation in matlab-mode.") (defconst matlab-comment-column 40 "*The goal comment column in matlab-mode buffers.") (defconst matlab-comment-line-s "% " "*String to start comment on line by itself.") (defconst matlab-comment-on-line-s "% " "*String to start comment on line with code.") (defconst matlab-comment-region-s "%$$$ " "*String inserted by \\[matlab-comment-region] at start of each line in \ region.") (defconst matlab-indent-before-return nil "*If non-nil, do matlab-indent-line before return (as well as after \ return).") (defconst matlab-indent-end-before-return nil "*If non-nil, do matlab-indent-line before return on end-line.") (defconst matlab-show-vers t "*If non-nil, shows the version number on load.") ;; Syntax Table (defvar matlab-mode-syntax-table nil "Syntax table used in matlab-mode buffers.") (if matlab-mode-syntax-table () (setq matlab-mode-syntax-table (make-syntax-table)) (modify-syntax-entry ?\\ "." matlab-mode-syntax-table) (modify-syntax-entry ?/ "." matlab-mode-syntax-table) (modify-syntax-entry ?* "." matlab-mode-syntax-table) (modify-syntax-entry ?+ "." matlab-mode-syntax-table) (modify-syntax-entry ?- "." matlab-mode-syntax-table) (modify-syntax-entry ?= "." matlab-mode-syntax-table) (modify-syntax-entry ?< "." matlab-mode-syntax-table) (modify-syntax-entry ?> "." matlab-mode-syntax-table) (modify-syntax-entry ?& "." matlab-mode-syntax-table) (modify-syntax-entry ?| "." matlab-mode-syntax-table) (modify-syntax-entry ?\' "." matlab-mode-syntax-table) (modify-syntax-entry ?\' "." matlab-mode-syntax-table) (modify-syntax-entry ?\" "." matlab-mode-syntax-table) (modify-syntax-entry ?% "<" matlab-mode-syntax-table) (modify-syntax-entry ?\n ">" matlab-mode-syntax-table) (set-syntax-table matlab-mode-syntax-table)) ;; Abbrev Table (defvar matlab-mode-abbrev-table nil "Abbrev table used in matlab-mode buffers.") (define-abbrev-table 'matlab-mode-abbrev-table ()) ;; Mode Map (defvar matlab-mode-map () "Keymap used in matlab-mode.") (if matlab-mode-map () (setq matlab-mode-map (make-sparse-keymap)) (define-key matlab-mode-map "\r" 'matlab-return) (define-key matlab-mode-map "\^j" 'matlab-linefeed) (define-key matlab-mode-map "\C-c\r" 'matlab-comment-return) (define-key matlab-mode-map "\t" 'matlab-indent-line) (define-key matlab-mode-map "\M-;" 'matlab-comment) (define-key matlab-mode-map "\C-c;" 'matlab-comment-region) (define-key matlab-mode-map "\C-cq" 'matlab-fill-region) (define-key matlab-mode-map "\C-cf" 'matlab-fill-comment-line) (define-key matlab-mode-map "\C-cj" 'matlab-justify-line) (define-key matlab-mode-map "\C-ct" 'matlab-show-line-attr) (define-key matlab-mode-map "\C-cb" 'matlab-show-blok-indent) (define-key matlab-mode-map "\C-cm" 'matlab-show-mexp-indent) (define-key matlab-mode-map "\M-\r" 'newline)) (defvar matlab-define-menus nil "should matlab-mode define global and local pop-up menus?") ;; matlab-mode (defun matlab-mode () "Major mode for editing MATLAB dot-m files. Version 1.06.0 dated 23Nov93. Will run matlab-mode-hook if it is non-nil. Filling works (comments justify). Special Key Bindings: \\{matlab-mode-map} Variables: matlab-indent-level Level to indent blocks. matlab-comment-column Goal column for on-line comments. fill-column Column used in auto-fill. matlab-comment-line-s String to start comment line. matlab-comment-region-s String to put comment lines in region. matlab-indent-before-return If t, matlab-indent-line before RET. matlab-indent-end-before-return If t, indent-line before RET on end. matlab-show-vers If t, show version on start-up. Commands: matlab-mode Enter MATLAB major mode. matlab-return RET with post indenting. matlab-linefeed RET with pre and post indent. matlab-comment-return RET for next-line comment. matlab-indent-line Indent line for structure. matlab-comment Add comment to current line. matlab-comment-indent Compute indent for comment. matlab-comment-region Comment (with arg, uncomment) region. matlab-fill-region Fill region (usually comments). matlab-justify-line Delete space on end and justify. To add automatic support put something like the following in your .emacs file: \(autoload 'matlab-mode \"matlab-mode\" \"Enter Matlab mode.\" t\) \(setq auto-mode-alist \(cons '\(\"\\\\.m$\" . matlab-mode\) \ auto-mode-alist\)\) \(defun my-matlab-mode-hook \(\) \(setq fill-column 76\) \(turn-on-auto-fill\)\) \(setq matlab-mode-hook 'my-matlab-mode-hook\)" (interactive) (kill-all-local-variables) (use-local-map matlab-mode-map) (setq major-mode 'matlab-mode) (setq mode-name "Matlab") (setq local-abbrev-table matlab-mode-abbrev-table) (set-syntax-table matlab-mode-syntax-table) (make-local-variable 'paragraph-start) (setq paragraph-start (concat "^$\\|" page-delimiter)) (make-local-variable 'paragraph-separate) (setq paragraph-separate paragraph-start) (make-local-variable 'paragraph-ignore-fill-prefix) (setq paragraph-ignore-fill-prefix t) (make-local-variable 'indent-line-function) (setq indent-line-function 'matlab-indent-line) (make-local-variable 'comment-start-skip) (setq comment-start-skip "%[ \t]*") (make-local-variable 'comment-column) (setq comment-column 'matlab-comment-column) (make-local-variable 'auto-fill-function) (setq auto-fill-function 'matlab-auto-fill) (make-local-variable 'comment-indent-function) (setq comment-indent-function 'matlab-comment-indent) (make-local-variable 'fill-column) (setq fill-column default-fill-column) (make-local-variable 'fill-prefix) (setq fill-prefix matlab-comment-line-s) ;;(setq font-lock-keywords matlab-font-lock-keywords) ;; Lucid emacs ;;(setq (make-local-variable 'font-lock-keywords) matlab-font-lock-keywords) (run-hooks 'matlab-mode-hook) (if matlab-show-vers (message "matlab-mode, Version 1.06.0 -- Modified by pcmac on 04/18/95"))) (defconst mtlb-cline-start-skip "[ \t]*%[ \t]*" "*The regular expression for skipping comment start.") ;; (defun matlab-auto-fill () "Do filling." (interactive) (cond ((mtlb-ltype-comment) auto-fill-function) ((mtlb-lattr-comment) auto-fill-function) (t ()))) (defun matlab-return () "Handle carriage return in matlab-mode." (interactive) (if matlab-indent-before-return (matlab-indent-line) (if matlab-indent-end-before-return (if (mtlb-ltype-block-end) (matlab-indent-line)))) (newline) (matlab-indent-line)) (defun matlab-linefeed () "Handle linefeed in matlab-mode. Has effect of matlab-return with (not matlab-indent-before-return)." (interactive) (if (not matlab-indent-before-return) (matlab-indent-line)) (newline) (matlab-indent-line)) (defun matlab-comment-return () "Handle carriage return for matlab comment line." (interactive) (cond ((mtlb-ltype-comment) (mtlb-set-comment-fill-prefix) (newline) (insert fill-prefix) (matlab-indent-line)) ((mtlb-lattr-comment) (newline) (indent-to matlab-comment-column) (insert matlab-comment-on-line-s)) (t (newline) (matlab-comment) (matlab-indent-line)))) (defun matlab-indent-line () "Indent a line in matlab-mode." (interactive) (save-excursion (beginning-of-line) (delete-horizontal-space) (indent-to (mtlb-calc-indent)) ;; -- If line contains a comment, format it. -- (if (or (mtlb-ltype-comment) (mtlb-lattr-comment)) (matlab-comment))) (skip-chars-forward " \t%")) (defun matlab-comment () "Add a comment to the current line. If one already exists, format it." (interactive) (cond ((mtlb-ltype-empty) (matlab-indent-line) (insert matlab-comment-line-s)) ((mtlb-ltype-comment) (save-excursion (if (and (= 0 (forward-line -1)) (mtlb-ltype-comment)) (progn (mtlb-set-comment-fill-prefix) (forward-line 1) (beginning-of-line) (delete-horizontal-space) (delete-char 1) (delete-horizontal-space) (insert fill-prefix))))) ((mtlb-lattr-comment) (beginning-of-line) (search-forward "%") (forward-char -1) (delete-horizontal-space) (insert " ") (if (< (current-column) matlab-comment-column) (indent-to matlab-comment-column)) (skip-chars-forward "% ")) (t (end-of-line) (re-search-backward "[^ \t\n^]" 0 t) (forward-char) (delete-horizontal-space) (if (< (current-column) matlab-comment-column) (indent-to matlab-comment-column) (insert " ")) (insert matlab-comment-on-line-s)))) (defun matlab-comment-indent () "Indent a comment line in matlab-mode." (mtlb-calc-indent)) (defun mtlb-calc-indent () "Return the appropriate indentation for this line as an int." (let ((indent 0)) (save-excursion (if (not (mtlb-prev-line)) 0 (setq indent (current-indentation)) (setq indent (+ indent (cond ((mtlb-ltype-comment) (mtlb-set-comment-fill-prefix) 0) ((mtlb-ltype-block-beg) matlab-indent-level) ((mtlb-ltype-block-end) 0) (t (mtlb-calc-blok-indent))))) (if (mtlb-lattr-unbal-mexp) (setq indent (+ indent (mtlb-calc-mexp-indent)))) (if (mtlb-lattr-cont) (setq indent (+ indent (* 2 matlab-indent-level))))) (if (mtlb-prev-line) (if (mtlb-lattr-cont) (setq indent (- indent (* 2 matlab-indent-level)))))) (if (mtlb-ltype-block-end) (setq indent (- indent matlab-indent-level))) (if (< indent 0) (setq indent 0)) indent)) (defun mtlb-prev-line () "Find the previous line. Return 0 if not found." (interactive) (if (/= 0 (forward-line -1)) () (if (mtlb-ltype-empty) (mtlb-prev-line) t))) (defun matlab-comment-region (beg-region end-region arg) "Comments every line in the region. Puts matlab-comment-region-s at the beginning of every line in the region. BEG-REGION and END-REGION are args which specify the region boundaries. With non-nil ARG, uncomments the region." (interactive "*r\nP") (let ((end-region-mark (make-marker)) (save-point (point-marker))) (set-marker end-region-mark end-region) (goto-char beg-region) (beginning-of-line) (if (not arg) ;comment the region (progn (insert matlab-comment-region-s) (while (and (= (forward-line 1) 0) (< (point) end-region-mark)) (insert matlab-comment-region-s))) (let ((com (regexp-quote matlab-comment-region-s))) ;uncomment the region (if (looking-at com) (delete-region (point) (match-end 0))) (while (and (= (forward-line 1) 0) (< (point) end-region-mark)) (if (looking-at com) (delete-region (point) (match-end 0)))))) (goto-char save-point) (set-marker end-region-mark nil) (set-marker save-point nil))) (defun matlab-fill-region (beg-region end-region &optional justify-flag) "Fill the region. Non-nil arg means justify commment lines as well." (interactive "*r\nP") (let ((end-reg-mk (make-marker))) (set-marker end-reg-mk end-region) (goto-char beg-region) (beginning-of-line) (while (< (save-excursion (forward-line 1) (point)) end-reg-mk) (if (save-excursion (= (forward-line 1) 0)) (progn (cond ((mtlb-ltype-comment) (while (matlab-fill-comment-line)) (if justify-flag (justify-comment-line)))) (forward-line 1)))))) (defun matlab-fill-comment-line () "Fill the current comment line." (interactive) (let ((prev-indent-col 0)) (beginning-of-line) (re-search-forward mtlb-cline-start-skip) (setq prev-indent-col (current-column)) (mtlb-set-comment-fill-prefix) (if (/= (forward-line 1) 0) () (beginning-of-line) (re-search-forward mtlb-cline-start-skip) (if (/= prev-indent-col (current-column)) (progn (forward-line -1) ()) (mtlb-join-comment-lines) (if (mtlb-wrap-line) (save-excursion (forward-line 1) (beginning-of-line) (insert fill-prefix) t)))))) (defun mtlb-join-comment-lines () "Join current comment line to previous, deleting space and comment mark." (interactive) (beginning-of-line) (forward-char -1) (delete-char 1) ; delete newline (delete-horizontal-space) (delete-char 1) ; delete "%" (delete-horizontal-space) (insert " ")) (defun mtlb-wrap-line () "Wrap line so last token on line does not exceed fill-column." (interactive) (save-excursion (end-of-line) (delete-horizontal-space) (if (< (current-column) fill-column) () (while (> (current-column) fill-column) (forward-char -1)) (while (not (looking-at "[ \t]")) (forward-char -1)) (delete-horizontal-space) (insert "\n") t))) (defun mtlb-set-comment-fill-prefix () "Set the fill-prefix for the current comment line." (setq fill-prefix (save-excursion (beginning-of-line) (buffer-substring (point) (progn (re-search-forward mtlb-cline-start-skip) (point))))) (if (equal fill-prefix "") (setq fill-prefix nil))) (defun matlab-justify-line () "Delete space on end of line and justify." (interactive) (save-excursion (end-of-line) (delete-horizontal-space) (justify-current-line))) ;;; ;;; line attributes ... (defun mtlb-lattr-comment () "Returns t if current line contains a comment." (save-excursion (beginning-of-line) (looking-at ".*%"))) (defun mtlb-lattr-cont () "Returns t if current line ends in .. and optional comment." (save-excursion (beginning-of-line) (re-search-forward "[^; \t.][ \t]*\\.\\.\\.+[ \t]*\\(%.*\\)?$" (save-excursion (end-of-line) (point)) t))) (defun mtlb-lattr-unbal-mexp () (/= (mtlb-calc-mexp-indent) 0)) ;;; ;;; line types ... (defun mtlb-ltype-empty () "Returns t if current line is empty." (save-excursion (beginning-of-line) (looking-at "^[ \t]*$"))) (defun mtlb-ltype-comment () "Returns t if current line is a MATLAB comment line." (save-excursion (beginning-of-line) (looking-at "[ \t]*%"))) (defun mtlb-ltype-block-beg () "Returns t if line contains beginning of MATLAB block." (save-excursion (beginning-of-line) (and (looking-at (concat "[^%\n]*" mtlb-block-beg-kw)) (not (mtlb-ltype-blk-beg-end))))) (defun mtlb-ltype-block-end () "Returns t if line contains end of MATLAB block." (save-excursion (beginning-of-line) (and (looking-at (concat "\\([^%\n]*[ \t]\\)?" mtlb-block-end-kw)) (not (mtlb-ltype-blk-beg-end))))) (defun mtlb-ltype-blk-beg-end () "Returns t if line contains matching block begin-end in matlab-mode." (save-excursion (beginning-of-line) (looking-at (concat "[^%\n]*" mtlb-block-beg-kw "[^%\n]+" mtlb-block-end-kw)))) ;;; ;;; utility functions ... (defconst mtlb-block-beg-kw "\\b\\(for\\|while\\|if\\|else\\|elseif\\)\\b" "Regular expression for keywords which begin blocks in matlab-mode.") (defconst mtlb-block-end-kw "\\b\\(end\\|else\\|elseif\\)\\b" "Regular expression for keywords which end blocks.") (defun mtlb-calc-blok-indent () (let ((indent 0)) (save-excursion (beginning-of-line) (while (< (point) (save-excursion (end-of-line) (point))) (cond ((looking-at mtlb-block-beg-kw) (setq indent (+ indent matlab-indent-level))) ((looking-at mtlb-block-end-kw) (setq indent (- indent matlab-indent-level)))) (forward-char))) indent)) (defun mtlb-calc-mexp-indent () (let ((indent 0)) (save-excursion (beginning-of-line) (while (< (point) (save-excursion (end-of-line) (point))) (cond ((looking-at "\\[") (setq indent (+ indent matlab-indent-level))) ((looking-at "\\]") (setq indent (- indent matlab-indent-level)))) (forward-char))) (* 2 indent))) ;;; ;;; -- debugging -- (defun matlab-show-line-attr () "Display type and attributes of current line. Used in debugging." (interactive) (let ((msg "matlab-show-line-attr:")) (cond ((mtlb-ltype-empty) (setq msg (concat msg " empty"))) ((mtlb-ltype-comment) (setq msg (concat msg " comment"))) ((mtlb-ltype-block-beg) (setq msg (concat msg " block-begin"))) ((mtlb-ltype-block-end) (setq msg (concat msg " block-end"))) (t (setq msg (concat msg " other")))) (if (mtlb-lattr-cont) (setq msg (concat msg " w/cont"))) (if (mtlb-lattr-comment) (setq msg (concat msg " w/comm"))) (if (mtlb-lattr-unbal-mexp) (setq msg (concat msg " w/unbal-mexp"))) (message msg))) (defun matlab-show-blok-indent () "Display indentation for matrix expression. Used in debugging." (interactive) (let ((msg "matlab-show-blok-indent: ")) (setq msg (concat msg (mtlb-calc-blok-indent))) (message msg))) (defun matlab-show-mexp-indent () "Display indentation for matrix expression. Used in debugging." (interactive) (let ((msg "matlab-show-mexp-indent: ")) (setq msg (concat msg (mtlb-calc-mexp-indent))) (message msg))) ;;; --- version 19 stuff ... (defvar matlab-font-lock-keywords (list '("\\b\\(break\\|else\\|elseif\\|end\\|for\\|if\\|return\\|while\\)\\b" . font-lock-keyword-face) '("\\bfunction[^=]+=[^)]+)" . font-lock-function-name-face) ) "Expressions to hightlight in Matlab mode.") (if (featurep 'hilit19) (hilit-set-mode-patterns 'matlab-mode '(("%.*$" nil comment) ;;("'.*'" nil string) ("\\bfunction[^=]+=[^)]+)" nil defun) ;; key words ("\\<\\(load\\|save\\|clear\\)\\>" nil include) ("\\<\\(break\\|else\\|elseif\\|end\\|for\\|if\\|return\\)\\>" nil keyword) ))) ;;; -- stuff which belongs elsewhere -- (defun justify-comment-line () "Add spaces to comment line point is in, so it ends at fill-column." (interactive) (save-excursion (save-restriction (let (ncols beg) (beginning-of-line) (forward-char (length fill-prefix)) (skip-chars-forward " \t") (setq beg (point)) (end-of-line) (narrow-to-region beg (point)) (goto-char beg) (while (re-search-forward " *" nil t) (delete-region (+ (match-beginning 0) (if (save-excursion (skip-chars-backward " ])\"'") (memq (preceding-char) '(?. ?? ?!))) 2 1)) (match-end 0))) (goto-char beg) (while (re-search-forward "[.?!][])""']*\n" nil t) (forward-char -1) (insert " ")) (goto-char (point-max)) (setq ncols (- fill-column (current-column))) (if (search-backward " " nil t) (while (> ncols 0) (let ((nmove (+ 3 (% (random) 3)))) (while (> nmove 0) (or (search-backward " " nil t) (progn (goto-char (point-max)) (search-backward " "))) (skip-chars-backward " ") (setq nmove (1- nmove)))) (insert " ") (skip-chars-backward " ") (setq ncols (1- ncols)))))))) ;;{{{ menu definitions for FSF19 & Lucid (and matlab-define-menus (cond ((string-match "Lucid" emacs-version) ;; Lemacs stuff (defvar matlab-mode-menu '("Matlab" ["Return" matlab-return t] ["Linefeed" matlab-linefeed t] ["Comment return" matlab-comment-return t] ["Indent line" matlab-indent-line t] ["Comment" matlab-comment t] ["Comment region" matlab-comment-region t] ["Fill region" matlab-fill-region t] ["Fill comment line" matlab-fill-comment-line t] ["Justify line" matlab-justify-line t] ["Show line attr" matlab-show-line-attr t] ["Show blok indent" matlab-show-blok-indent t] ["Show mexp indent" matlab-show-mexp-indent t] ["Newline" newline t]) "Menu for matlab mode") (defvar matlab-global-menu '("Matlab" ["matlab-mode" matlab-mode t]) "Menubar menu for Matlab") (defun matlab-menu () (interactive) (popup-menu matlab-mode-menu)) (define-key matlab-mode-map 'button3 'matlab-menu) (add-menu nil (car matlab-global-menu) (cdr matlab-mode-menu))) ((string-match "^19" emacs-version) (defvar matlab-mode-menu (make-sparse-keymap "Matlab Menu") "Menu for matlab mode") (define-key matlab-mode-menu [newline] '("Newline" . newline)) (define-key matlab-mode-menu [show-mexp-indent] '("Show mexp indent" . matlab-show-mexp-indent)) (define-key matlab-mode-menu [show-blok-indent] '("Show blok indent" . matlab-show-blok-indent)) (define-key matlab-mode-menu [show-line-attr] '("Show line attr" . matlab-show-line-attr)) (define-key matlab-mode-menu [justify-line] '("Justify line" . matlab-justify-line)) (define-key matlab-mode-menu [fill-comment-line] '("Fill comment line" . matlab-fill-comment-line)) (define-key matlab-mode-menu [fill-region] '("Fill region" . matlab-fill-region)) (define-key matlab-mode-menu [comment-region] '("Comment region" . matlab-comment-region)) (define-key matlab-mode-menu [comment] '("Comment" . matlab-comment)) (define-key matlab-mode-menu [indent-line] '("Indent line" . matlab-indent-line)) (define-key matlab-mode-menu [comment-return] '("Comment return" . matlab-comment-return)) (define-key matlab-mode-menu [linefeed] '("Linefeed" . matlab-linefeed)) (define-key matlab-mode-menu [return] '("Matlab return" . matlab-return)) (define-key matlab-mode-map [menu-bar matlab] (cons "Matlab" matlab-mode-menu)) (define-key matlab-mode-map [S-down-mouse-2] matlab-mode-menu) (defvar matlab-global-menu (make-sparse-keymap "Matlab") "Globally enabled menu for matlab commands.") (define-key matlab-global-menu [mat-mod] '("Matlab mode" . matlab-mode)) (define-key global-map [menu-bar matlab] (cons "Matlab" matlab-global-menu)) ))) ;;}}} (provide 'matlab-mode) ;; --- last line of matlab-mode.el ---