Python `tempo' templates start to become usable

This commit is contained in:
Gerard Vermeulen 2022-09-25 11:42:58 +02:00
parent bc38c4bbe1
commit 262c185628
1 changed files with 40 additions and 25 deletions

View File

@ -4336,19 +4336,25 @@ adaptation to =lexical-binding=:
1. [[https://www.emacswiki.org/emacs/TempoMode][Tempo (Emacs Wiki)]]
2. [[https://www.lysator.liu.se/~davidk/elisp/][David Kågedals elisp]]
3. [[https://github.com/yilkalargaw/emacs-native-snippets][Emacs's Native Snippets]], [[https://www.reddit.com/r/emacs/comments/wdbk34/emacss_native_templating_and_snippet_fuctionality/][Emacs's native templating and snippet functionality]]
Listing [[lst:configure-tempo-abbrev-mode]] provide the tools to add =tempo-tags= to
abbreviation tables. Listing [[lst:configure-tempo-latex-completing-read]] shows how
to combine =tempo=, =latex= and =completing-read= for the builtin =tempo=
package that is part of Emacs-28.1 and later.
Listing [[lst:configure-tempo-ui]] provide a function to define local keybindings
and the tools to add =tempo-tags= to abbreviation tables. Listing
[[lst:configure-tempo-latex-completing-read]] shows how to combine =tempo=, =latex=
and =completing-read= for the builtin =tempo= package that is part of Emacs-28.1
and later. Listing [[lst:setup-python-tempo]] configures =tempo= for =python-mode=.
#+caption[Configure =tempo with =abbrev-mode=]:
#+caption: Configure =tempo= with =abbrev-mode=.
#+name: lst:configure-tempo-abbrev-mode
#+caption[Configure =tempo= user interface]:
#+caption: Configure =tempo= user-interface.
#+name: lst:configure-tempo-ui
#+begin_src emacs-lisp
(with-eval-after-load 'tempo
(custom-set-variables
'(tempo-interactive t))
(defun setup-local-tempo-key-bindings ()
"Initialize local `tempo' key bindings."
(local-set-key (kbd "M-n") #'tempo-forward-mark)
(local-set-key (kbd "M-p") #'tempo-backward-mark))
(defun abbrev-tempo-expand-if-complete ()
"Hook function for `define-abbrev' with `no-self-insert' property `t'."
(tempo-expand-if-complete))
@ -4356,19 +4362,12 @@ package that is part of Emacs-28.1 and later.
(put 'abbrev-tempo-expand-if-complete 'no-self-insert t)
(defun add-tempo-tags-to-abbrev-table (tempo-tags abbrev-table)
"Add the tags in TEMPO-TAGS to ABBREV-TABLE."
"Add the tags in TEMPO-TAGS to ABBREV-TABLE.
Allows `tempo' expansion by typing <SPC> after a complete `tempo' tag."
(dolist (tag tempo-tags)
(unless (abbrev-symbol (car tag) abbrev-table)
(define-abbrev abbrev-table
(car tag) 1 'abbrev-tempo-expand-if-complete))))
(defun setup-local-tempo-key-bindings ()
"Initialize local `tempo' key bindings."
(define-prefix-command 'tempo-map)
(local-set-key (kbd "C-c t") 'tempo-map)
(define-key tempo-map (kbd "c") #'tempo-complete-tag)
(define-key tempo-map (kbd "f") #'tempo-forward-mark)
(define-key tempo-map (kbd "b") #'tempo-backward-mark)))
(car tag) 1 'abbrev-tempo-expand-if-complete)))))
#+end_src
#+caption[Configure =tempo= for =latex= with =completing-read=]:
@ -4415,19 +4414,35 @@ package that is part of Emacs-28.1 and later.
(require 'tempo))
(defun setup-tempo-python ()
(setup-local-tempo-key-bindings)
(defvar python-tempo-tags nil)
(tempo-define-template
"python-class"
"python-base-class"
'("class " p ":" n >)
"cls"
"Insert a class"
'python-tempo-tags)
"clsb" "Define a base class" 'python-tempo-tags)
(tempo-define-template
"python-derived-class"
'("class " p "(" p "):" n >)
"clsd" "Define a derived class" 'python-tempo-tags)
(tempo-define-template
"python-class-method"
'("@classmethod" > n > "def " p "(cls, " p "):" n >)
"defc" "Define a class method" 'python-tempo-tags)
(tempo-define-template
"python-function"
'("def " p "(" p "):" n >)
"deff" "Define a function" 'python-tempo-tags)
(tempo-define-template
"python-normal-method"
'("def " p "(self, " p "):" n >)
"defm" "Define a normal method" 'python-tempo-tags)
(tempo-define-template
"python-static-method"
'("@staticmethod" > n > "def " p "(" p "):" n >)
"defs" "Define a static method" 'python-tempo-tags)
(tempo-use-tag-list 'python-tempo-tags)
(add-tempo-tags-to-abbrev-table python-tempo-tags python-mode-abbrev-table))
(add-tempo-tags-to-abbrev-table python-tempo-tags python-mode-abbrev-table)
(setup-local-tempo-key-bindings))
(add-hook 'python-mode-hook 'setup-tempo-python))
#+end_src