dotfiles/emacs-lisp/tempel.org
main fa71c99b65
Remove org-fragtog
Signed-off-by: main <magic_rb@redalder.org>
2022-11-23 13:35:02 +01:00

3.4 KiB

Tempel

#

Tempel is a tiny template package for Emacs, which uses the syntax of the Emacs Tempo library. Tempo is an ancient temple of the church of Emacs. It is 27 years old, but still in good shape since it successfully resisted change over the decades. However it may look a bit dusty here and there. Therefore we present to you, Tempel, a modernized implementation of Tempo, in the form of three commands.

Set the template file to the result of tangling Tempel - Templates.

  (setq tempel-path (expand-file-name "~/roam/emacs-lisp/templates.lisp"))

Hook tempel-capf on both prog-mode and text-mode.

  (defun tempel-setup-capf ()
    (add-hook 'completion-at-point-functions #'tempel-complete -1 'local))

  (add-hook 'prog-mode-hook 'tempel-setup-capf)
  (add-hook 'text-mode-hook 'tempel-setup-capf)

Define keymaps, the defaults are unnecessarily hard to trigger.

  (general-define-key
    :keymaps '(insert normal)
    "C-n" 'nil
    "C-p" 'nil)
  (general-define-key
    :keymaps 'tempel-map
    "M-{" nil
    "M-}" nil
    "C-n" 'tempel-next
    "C-p" 'tempel-previous)

lsp-mode completely wipes completion-at-point-functions, so we need re-add cape after it removes everything.

  (use-package lsp-mode
    :defer t
    :after tempel
    :config
    (add-hook 'lsp-mode-hook 'tempel-setup-capf))

Fix LSP not getting notified about changes, can be fixed by notifying it at the of template expansion.

  (advice-add
   'tempel--disable
   :before
   (lambda (&rest r)
     (when lsp-mode
       (let* ((region-start (tempel--beginning))
              (region-end (tempel--end)))
         (lsp-on-change region-start region-end (- region-end region-start))))))

To setup a post template return point, use (p (ignore (setq tempel-retpoint (point)))) in a template.

  (defvar tempel-retpoint nil)

  (defun tempel-retpoint-here ()
    (setq tempel-retpoint (point))
    "")

  (advice-add
   'tempel--disable
   :before
   (lambda (&rest r)
     (when tempel-retpoint
       (goto-char tempel-retpoint)
       (setq tempel-retpoint nil))))

To allow for => as template keys, 'symbol won't work, but 'evil-word will.

  (defun tempel--prefix-bounds ()
  "Return prefix bounds."
  (if tempel-trigger-prefix
      (let ((end (point))
            (beg (save-excursion
                   (search-backward tempel-trigger-prefix
                                    (line-beginning-position) 'noerror))))
        (when (and beg (save-excursion
                         (not (re-search-backward "\\s-" beg 'noerror))))
          (cons (+ beg (length tempel-trigger-prefix)) end)))
    (bounds-of-thing-at-point 'symbol)))