Emacs script to add function headers in python mode

by grdvnl

I have an usual python-mode setup with python-mode.el and all packages that help set up a good IDE environment for Python in Emacs. But, I did not find any scripts/functions that would help me generate a doc string for methods. The script I have shared below should do the trick. This is the initial script and I hope to add to this script as I use it. The script tries to follows the markup syntax that is supported by reStructuredText

Please feel free comment on the git repo if you find any issues or have any improvements


;; add the following lines to .emacs file.
;; When a function header needs to be generated, place the cursor
;; on the same line as function definition and call the function
;; generate-header. Alternatively, a key-binding can be added
;; using (global-set-key) or adding this function to the python-mode-hook

(defun get-function-definition(sentence)
(if (string-match "def.*(.*):" sentence)
(match-string 0 sentence))
)

(defun get-parameters(sentence)
(setq y (get-function-definition sentence))
(if y
(if (string-match "(.*)" y)
(match-string 0 y)))
)

(require 'thingatpt)
(defun generate-header()
(interactive)
(setq p (get-parameters (thing-at-point 'sentence)))
(forward-line 1)
(insert "\t\"\"\"\n\n\n")
(setq params (split-string p "[?\,?\(?\)?\ ]"))
(while params
(if (/= (length (chomp (car params))) 0)
(progn
(insert "\t:param ")
(insert (chomp (car params)))
(insert ": \n")))
(setq params (cdr params)))
(insert "\n\t\"\"\"\n\n")
)

Git

About these ads