diff options
| author | Simeon Simeonov | 2024-01-10 21:49:17 +0100 |
|---|---|---|
| committer | Simeon Simeonov | 2024-01-10 21:49:17 +0100 |
| commit | ff160687915804b109fe626a5c7fbe949f227106 (patch) | |
| tree | 9838ea050edf4288234a16206aecef7c9825c62f /.emacs.d/snippets/python-mode.statnett/.yas-setup.el | |
| parent | 7d08994f744685d6237ae3b4defde3eb2441fa42 (diff) | |
Update markdown-mode and yasnippet. Add some new Python snippets
Diffstat (limited to '.emacs.d/snippets/python-mode.statnett/.yas-setup.el')
| -rw-r--r-- | .emacs.d/snippets/python-mode.statnett/.yas-setup.el | 31 |
1 files changed, 19 insertions, 12 deletions
diff --git a/.emacs.d/snippets/python-mode.statnett/.yas-setup.el b/.emacs.d/snippets/python-mode.statnett/.yas-setup.el index 76af532..8b29215 100644 --- a/.emacs.d/snippets/python-mode.statnett/.yas-setup.el +++ b/.emacs.d/snippets/python-mode.statnett/.yas-setup.el | |||
| @@ -1,23 +1,33 @@ | |||
| 1 | (require 'yasnippet) | 1 | (require 'yasnippet) |
| 2 | (defvar yas-text) | 2 | (defvar yas-text) |
| 3 | 3 | ||
| 4 | (defvar python-split-arg-arg-regex | ||
| 5 | "\\([[:alnum:]*]+\\)\\(:[[:blank:]]*[[:alpha:]]*\\)?\\([[:blank:]]*=[[:blank:]]*[[:alnum:]]*\\)?" | ||
| 6 | "Regular expression matching an argument of a python function. | ||
| 7 | First group should give the argument name.") | ||
| 8 | |||
| 9 | (defvar python-split-arg-separator | ||
| 10 | "[[:space:]]*,[[:space:]]*" | ||
| 11 | "Regular expression matching the separator in a list of argument.") | ||
| 12 | |||
| 4 | (defun python-split-args (arg-string) | 13 | (defun python-split-args (arg-string) |
| 5 | "Split a python argument string into ((name, default)..) tuples" | 14 | "Split a python argument string ARG-STRING into a tuple of argument names." |
| 6 | (mapcar (lambda (x) | 15 | (mapcar (lambda (x) |
| 7 | (split-string x "[[:blank:]]*=[[:blank:]]*" t)) | 16 | (when (string-match python-split-arg-arg-regex x) |
| 8 | (split-string arg-string "[[:blank:]]*,[[:blank:]]*" t))) | 17 | (match-string-no-properties 1 x))) |
| 18 | (split-string arg-string python-split-arg-separator t))) | ||
| 9 | 19 | ||
| 10 | (defun python-args-to-docstring () | 20 | (defun python-args-to-docstring () |
| 11 | "return docstring format for the python arguments in yas-text" | 21 | "Return docstring format for the python arguments in yas-text." |
| 12 | (let* ((indent (concat "\n" (make-string (current-column) 32))) | 22 | (let* ((indent (concat "\n" (make-string (current-column) 32))) |
| 13 | (args (python-split-args yas-text)) | 23 | (args (python-split-args yas-text)) |
| 14 | (max-len (if args (apply 'max (mapcar (lambda (x) (length (nth 0 x))) args)) 0)) | 24 | (max-len (if args (apply 'max (mapcar (lambda (x) (length (nth 0 x))) args)) 0)) |
| 15 | (formatted-args (mapconcat | 25 | (formatted-args (mapconcat |
| 16 | (lambda (x) | 26 | (lambda (x) |
| 17 | (concat (nth 0 x) (make-string (- max-len (length (nth 0 x))) ? ) " -- " | 27 | (concat (nth 0 x) (make-string (- max-len (length (nth 0 x))) ? ) " -- " |
| 18 | (if (nth 1 x) (concat "\(default " (nth 1 x) "\)")))) | 28 | (if (nth 1 x) (concat "\(default " (nth 1 x) "\)")))) |
| 19 | args | 29 | args |
| 20 | indent))) | 30 | indent))) |
| 21 | (unless (string= formatted-args "") | 31 | (unless (string= formatted-args "") |
| 22 | (mapconcat 'identity (list "Keyword Arguments:" formatted-args) indent)))) | 32 | (mapconcat 'identity (list "Keyword Arguments:" formatted-args) indent)))) |
| 23 | 33 | ||
| @@ -33,6 +43,3 @@ | |||
| 33 | (list "\nParameters\n----------" formatted-params | 43 | (list "\nParameters\n----------" formatted-params |
| 34 | "\nReturns\n-------" formatted-ret) | 44 | "\nReturns\n-------" formatted-ret) |
| 35 | "\n")))) | 45 | "\n")))) |
| 36 | |||
| 37 | |||
| 38 | (add-hook 'python-mode-hook #'yasnippet-snippets--fixed-indent) | ||
