Download
(defun ska-skel-matlab-function (fname)
"Magic matlab function inserting.
First the user is asked for a function name.
When the function-name doesn't match the current filename the user is
asked for the creation of a new file. If a matlab entry exists in
`auto-insert-alist' this might then take effect. If no new file is
created we offers to erase the current buffer.
The next step is the definition of output arguments. If an argument
is empty the definition of output argument is considered done. The
output list should be nicely formatted:
- nothing between the 'function' keyword and the function-name when
no output argument
- the single name of a single argument
- several output arguments put in to [...].
The definition of input arguments works the same. If any are defined
they are put into parens.
Finally the documentation is prepared:
- A Matlab H1-line
- Usage-line copied from the function definitino
- a list of input and output arguments
- copyright and author notice using auto-insert-copyright"
(interactive "sFunction name: ")
(if (or
(not (buffer-file-name))
(not (equal fname
(replace-in-string
(file-name-nondirectory (buffer-file-name))
".m" ""))))
(if (y-or-n-p "create new file? ")
(find-file (concat fname ".m"))
(when (not (equal (point-min) (point-max)))
(if (y-or-n-p "erase current file contents? ")
(erase-buffer)))))
(insert "function [")
(let ((gotopos)
(outarg)
(hasoutargp nil)
(outargs)
(inarg)
(hasinargp nil)
(inargs))
(while (> (length (setq outarg
(read-string
"output argument (ENTER to finish) ")))
0)
(insert (concat outarg ", "))
(setq outargs (cons outarg outargs))
(setq hasoutargp t))
(if (not hasoutargp)
(backward-delete-char 1)
(backward-delete-char 2)
(insert "] = ")
(when (= (length outargs) 1)
(save-excursion
(beginning-of-line)
(while (re-search-forward "[][]" (point-at-eol) t)
(replace-match "")))))
(insert fname " (")
(while (> (length (setq inarg
(read-string
"input argument (ENTER to finish) ")))
0)
(insert (concat inarg ", "))
(setq inargs (cons inarg inargs))
(setq hasinargp t))
(if (not hasinargp)
(backward-delete-char 1)
(backward-delete-char 2)
(insert ")"))
(insert (concat "\n% " (upcase fname) " "))
(setq gotopos (point))
(insert "<H1 line>\n")
(insert "%\n% Usage: ")
(insert (save-excursion
(search-backward-regexp "function ")
(search-forward-regexp "function ")
(buffer-substring (point) (point-at-eol))))
(insert "\n%\n")
(when hasoutargp
(insert "% Returns\n% -------\n")
(mapc #'(lambda (oa)
(insert (concat "% " oa ": \n")))
(reverse outargs)))
(when hasinargp
(insert "%\n% Expects\n% -------\n")
(mapc #'(lambda (ia)
(insert (concat "% " ia ": \n")))
(reverse inargs)))
(insert (concat
"%\n%\n% Copyright (C) "
(substring (current-time-string) -4) " by "
auto-insert-copyright "\n"))
(insert (concat "% Author: " (user-full-name) "\n%\n"))
(goto-char gotopos)
(mark-end-of-line nil)
))
(provide 'ska-skel-matlab)