1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
|
;;; matlab-cgen.el --- In buffer code generation features (templates, etc) -*- lexical-binding: t -*-
;; Copyright (C) 2024 Free Software Foundation, Inc.
;; Author: Eric Ludlam <zappo@gnu.org>
;;
;; 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 3 of the
;; License, 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 this program. If not, see http://www.gnu.org/licenses/.
;;; Commentary:
;;
;; This library supports tempo templates, and other misc functions designed
;; to create code in an Emacs buffer.
(require 'matlab)
(require 'tempo)
;;; Code:
(defvar matlab-tempo-tags nil
"List of templates used in MATLAB mode.")
;; This trick allows this file to be autoloaded ONLY when the user uses the insert prefix.
;;
;;;###autoload (autoload 'matlab-insert-map-fcn "matlab-cgen" "Keymap for C-c C-c in matlab-mode" t 'keymap)
(defvar matlab-insert-map
(let ((km (make-sparse-keymap)))
(define-key km "c" 'matlab-insert-next-case)
(define-key km "e" 'matlab-insert-end-block)
(define-key km "i" 'tempo-template-matlab-if)
(define-key km "I" 'tempo-template-matlab-if-else)
(define-key km "f" 'tempo-template-matlab-for)
(define-key km "s" 'tempo-template-matlab-switch)
(define-key km "t" 'tempo-template-matlab-try)
(define-key km "w" 'tempo-template-matlab-while)
(define-key km "F" 'tempo-template-matlab-function)
(define-key km "'" 'matlab-stringify-region)
;; Not really inserts, but auto coding stuff
(define-key km "\C-s" 'matlab-ispell-strings-and-comments)
km)
"Keymap used for inserting simple texts based on context.")
(defvar matlab-insert-map-fcn nil
"Keymap for `matlab-insert-map' (C-c C-c) in `matlab-mode'.")
(fset 'matlab-insert-map-fcn (setq matlab-insert-map-fcn matlab-insert-map))
(add-hook 'matlab-mode-hook #'matlab-cgen-hook-fcn t)
(defun matlab-cgen-hook-fcn ()
"Hook run in `matlab-mode-hook' needed for cgen support."
;; Tempo tags
(make-local-variable 'tempo-local-tags)
(setq tempo-local-tags (append matlab-tempo-tags tempo-local-tags))
)
;; We might load late, so loop over all matlab buffers, and run the hook.
(dolist (B (buffer-list))
(with-current-buffer B
(when (eq major-mode 'matlab-mode)
(matlab-cgen-hook-fcn))))
;;; Templates and smart code gen features
;;
(defun matlab-insert-end-block (&optional reindent)
"Insert and END block based on the current syntax.
Optional argument REINDENT indicates if the specified block
should be re-indented."
(interactive "P")
(when (not (matlab-line-empty-p (matlab-compute-line-context 1)))
(end-of-line) (insert "\n"))
(let ((valid t) (begin nil))
(save-excursion
(condition-case nil
(progn
(matlab-backward-sexp t)
(setq begin (point)
valid (buffer-substring-no-properties
(point) (save-excursion
(re-search-forward "[\n,;.]" nil t)
(point)))))
(error (setq valid nil))))
(if (not valid)
(error "No block to end")
(insert "end")
(if (stringp valid) (insert " % " valid))
(matlab-indent-line)
(if reindent (indent-region begin (point) nil)))))
(tempo-define-template
"matlab-for"
'("for " p "=" p "," > n>
r> &
"end" > %)
"for"
"Insert a MATLAB for statement"
'matlab-tempo-tags
)
(tempo-define-template
"matlab-while"
'("while (" p ")," > n>
r> &
"end" > %)
"while"
"Insert a MATLAB while statement"
'matlab-tempo-tags
)
(tempo-define-template
"matlab-if"
'("if " p > n
r>
"end" > n)
"if"
"Insert a MATLAB if statement"
'matlab-tempo-tags
)
(tempo-define-template
"matlab-if-else"
'("if " p > n
r>
"else" > n
"end" > n)
"if"
"Insert a MATLAB if statement"
'matlab-tempo-tags
)
(tempo-define-template
"matlab-try"
'("try " > n
r>
"catch" > n
p > n
"end" > n)
"try"
"Insert a MATLAB try catch statement"
'matlab-tempo-tags
)
(tempo-define-template
"matlab-switch"
'("switch " p > n
"otherwise" > n
r>
"end" > n)
"switch"
"Insert a MATLAB switch statement with region in the otherwise clause."
'matlab-tempo-tags)
(defun matlab-insert-next-case ()
"Insert a case statement inside this switch statement."
(interactive)
;; First, make sure we are where we think we are.
(let ((valid t))
(save-excursion
(condition-case nil
(progn
(matlab-backward-sexp t)
(setq valid (looking-at "switch")))
(error (setq valid nil))))
(if (not valid)
(error "Not in a switch statement")))
(when (not (matlab-line-empty-p (matlab-compute-line-context 1)))
(end-of-line) (insert "\n"))
(indent-to 0)
(insert "case ")
(matlab-indent-line))
(tempo-define-template
"matlab-function"
'("function "
(P "output argument(s): " output t)
;; Insert brackets only if there is more than one output argument
(if (string-match "," (tempo-lookup-named 'output))
'(l "[" (s output) "]")
'(l (s output)))
;; Insert equal sign only if there is output argument(s)
(if (= 0 (length (tempo-lookup-named 'output))) nil
" = ")
;; The name of a function, as defined in the first line, should
;; be the same as the name of the file without .m extension
(if (= 1 (count-lines 1 (point)))
(tempo-save-named
'fname
(file-name-nondirectory (file-name-sans-extension
(buffer-file-name))))
'(l (P "function name: " fname t)))
(tempo-lookup-named 'fname)
"(" (P "input argument(s): ") ")" n
"% " (upcase (tempo-lookup-named 'fname)) " - " (P "H1 line: ") n
"% " p n
(if matlab-functions-have-end
'(l "end" n)))
"function"
"Insert a MATLAB function statement"
'matlab-tempo-tags
)
(defun matlab-stringify-region (begin end)
"Put MATLAB single quotes (\\=') around region and quote all quotes within it.
Stringification allows you to type in normal MATLAB code, mark it, and
then turn it into a MATLAB string that will output exactly what's in
the region. BEGIN and END mark the region to be stringified."
(interactive "r")
(save-excursion
(goto-char begin)
(if (re-search-forward "\n" end t)
(error
"You may only stringify regions that encompass less than one line"))
(let ((m (make-marker)))
(move-marker m end)
(goto-char begin)
(insert "'")
(while (re-search-forward "'" m t)
(insert "'"))
(goto-char m)
(insert "'"))))
(provide 'matlab-cgen)
;;; matlab-cgen.el ends here
;; LocalWords: Ludlam zappo autoloaded Keymap keymap fset setq defun dolist reindent progn sexp
;; LocalWords: stringp fname nondirectory upcase allstring
|