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 --- .emacs.d/snippets/python-mode/.yas-setup.el | 31 ++++++++++++++++++----------- .emacs.d/snippets/python-mode/__call__ | 7 +++++++ .emacs.d/snippets/python-mode/__repr__ | 7 +++++++ .emacs.d/snippets/python-mode/__str__ | 7 +++++++ .emacs.d/snippets/python-mode/script | 1 - .emacs.d/snippets/python-mode/with-open | 6 ++++++ 6 files changed, 46 insertions(+), 13 deletions(-) create mode 100644 .emacs.d/snippets/python-mode/__call__ create mode 100644 .emacs.d/snippets/python-mode/__repr__ create mode 100644 .emacs.d/snippets/python-mode/__str__ create mode 100644 .emacs.d/snippets/python-mode/with-open (limited to '.emacs.d/snippets/python-mode') diff --git a/.emacs.d/snippets/python-mode/.yas-setup.el b/.emacs.d/snippets/python-mode/.yas-setup.el index 76af532..8b29215 100644 --- a/.emacs.d/snippets/python-mode/.yas-setup.el +++ b/.emacs.d/snippets/python-mode/.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) diff --git a/.emacs.d/snippets/python-mode/__call__ b/.emacs.d/snippets/python-mode/__call__ new file mode 100644 index 0000000..e807be5 --- /dev/null +++ b/.emacs.d/snippets/python-mode/__call__ @@ -0,0 +1,7 @@ +# -*- mode: snippet -*- +# name: __call__ +# key: _call +# group: Special methods +# -- +def __call__(self, ${1:*args}): + return $0 diff --git a/.emacs.d/snippets/python-mode/__repr__ b/.emacs.d/snippets/python-mode/__repr__ new file mode 100644 index 0000000..85cb80f --- /dev/null +++ b/.emacs.d/snippets/python-mode/__repr__ @@ -0,0 +1,7 @@ +# -*- mode: snippet -*- +# name: __repr__ +# key: _repr +# group: Special methods +# -- +def __repr__(self): + return $0 diff --git a/.emacs.d/snippets/python-mode/__str__ b/.emacs.d/snippets/python-mode/__str__ new file mode 100644 index 0000000..f623df7 --- /dev/null +++ b/.emacs.d/snippets/python-mode/__str__ @@ -0,0 +1,7 @@ +# -*- mode: snippet -*- +# name: __str__ +# key: _str +# group: Special methods +# -- +def __str__(self): + return $0 diff --git a/.emacs.d/snippets/python-mode/script b/.emacs.d/snippets/python-mode/script index 2e8cc35..9637f4c 100644 --- a/.emacs.d/snippets/python-mode/script +++ b/.emacs.d/snippets/python-mode/script @@ -3,7 +3,6 @@ # key: script # -- #!/usr/bin/env python -# -*- coding: utf-8 -*- def main(inargs=None): diff --git a/.emacs.d/snippets/python-mode/with-open b/.emacs.d/snippets/python-mode/with-open new file mode 100644 index 0000000..77c7210 --- /dev/null +++ b/.emacs.d/snippets/python-mode/with-open @@ -0,0 +1,6 @@ +# -*- mode: snippet -*- +# name: with-open +# key: wo +# -- +with open(${1:"filename"}${2:, encoding="${3:utf-8}"}${4:, mode="${5:w}"}) as ${6:myfile}: + $0 \ No newline at end of file -- cgit v1.3