From ff160687915804b109fe626a5c7fbe949f227106 Mon Sep 17 00:00:00 2001 From: Simeon Simeonov Date: Wed, 10 Jan 2024 21:49:17 +0100 Subject: Update markdown-mode and yasnippet. Add some new Python snippets --- .../snippets/python-mode.statnett/.yas-setup.el | 31 +++++++++++++--------- 1 file changed, 19 insertions(+), 12 deletions(-) (limited to '.emacs.d/snippets/python-mode.statnett/.yas-setup.el') 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 @@ (require 'yasnippet) (defvar yas-text) +(defvar python-split-arg-arg-regex +"\\([[:alnum:]*]+\\)\\(:[[:blank:]]*[[:alpha:]]*\\)?\\([[:blank:]]*=[[:blank:]]*[[:alnum:]]*\\)?" +"Regular expression matching an argument of a python function. +First group should give the argument name.") + +(defvar python-split-arg-separator +"[[:space:]]*,[[:space:]]*" +"Regular expression matching the separator in a list of argument.") + (defun python-split-args (arg-string) - "Split a python argument string into ((name, default)..) tuples" + "Split a python argument string ARG-STRING into a tuple of argument names." (mapcar (lambda (x) - (split-string x "[[:blank:]]*=[[:blank:]]*" t)) - (split-string arg-string "[[:blank:]]*,[[:blank:]]*" t))) + (when (string-match python-split-arg-arg-regex x) + (match-string-no-properties 1 x))) + (split-string arg-string python-split-arg-separator t))) (defun python-args-to-docstring () - "return docstring format for the python arguments in yas-text" + "Return docstring format for the python arguments in yas-text." (let* ((indent (concat "\n" (make-string (current-column) 32))) (args (python-split-args yas-text)) (max-len (if args (apply 'max (mapcar (lambda (x) (length (nth 0 x))) args)) 0)) (formatted-args (mapconcat - (lambda (x) - (concat (nth 0 x) (make-string (- max-len (length (nth 0 x))) ? ) " -- " - (if (nth 1 x) (concat "\(default " (nth 1 x) "\)")))) - args - indent))) + (lambda (x) + (concat (nth 0 x) (make-string (- max-len (length (nth 0 x))) ? ) " -- " + (if (nth 1 x) (concat "\(default " (nth 1 x) "\)")))) + args + indent))) (unless (string= formatted-args "") (mapconcat 'identity (list "Keyword Arguments:" formatted-args) indent)))) @@ -33,6 +43,3 @@ (list "\nParameters\n----------" formatted-params "\nReturns\n-------" formatted-ret) "\n")))) - - -(add-hook 'python-mode-hook #'yasnippet-snippets--fixed-indent) -- cgit v1.3