Including the Lissner's Snippet collection

This includes the starting auto-insert code for bash scripts, fish
scripts and Emacs lisp. Off to a great start here.
This commit is contained in:
Howard Abrams 2021-11-12 13:05:31 -08:00
parent ee2eab464d
commit 9f6eca95a1
5 changed files with 71 additions and 4 deletions

View file

@ -82,6 +82,15 @@ Using [[https://github.com/joaotavora/yasnippet][yasnippet]] to convert template
(add-to-list 'yas-snippet-dirs (expand-file-name "snippets" user-emacs-directory))
(yas-global-mode +1))
#+END_SRC
Check out [[http://joaotavora.github.io/yasnippet/][the documentation]] for writing them.
Seems the best [[https://github.com/hlissner/doom-snippets][collection of snippets]] is what Henrik Lissner has made for Doom (otherwise, we should use [[http://github.com/AndreaCrotti/yasnippet-snippets][yasnippet-snippets]] package):
#+BEGIN_SRC emacs-lisp
(use-package doom-snippets
:after yasnippet
:straight (doom-snippets :type git :host github :repo "hlissner/doom-snippets" :files ("*.el" "*")))
#+END_SRC
*Note:* Including his snippets also includes some [[https://github.com/hlissner/doom-snippets#snippets-api][helper functions]] and other features.
** Auto Insert Templates
The [[https://www.emacswiki.org/emacs/AutoInsertMode][auto-insert]] feature is a wee bit complicated. All I want is to associate a filename regular expression with a YASnippet template. I'm stealing some ideas from Henrik Lissner's [[https://github.com/hlissner/doom-emacs/blob/develop/modules/editor/file-templates/autoload.el][set-file-template!]] macro, but maybe simpler?
#+BEGIN_SRC emacs-lisp

View file

@ -163,7 +163,10 @@ And some special keybindings to encrypt/decrypt files:
** Emacs Lisp
Why yes, I do find I code a lot in Emacs...
#+BEGIN_SRC emacs-lisp
(ha-auto-insert-file (rx ".el" eol) "emacs-lisp-mode.el")
#+END_SRC
However, most of my Emacs Lisp code is in literate org files.
*** Clever Parenthesis
@ -360,15 +363,21 @@ While filename extensions work fine most of the time, I don't like to pre-pend =
:straight nil
:mode (rx (or (seq ".sh" eol)
"/bin/"))
:config
(ha-auto-insert-file (rx (or (seq ".sh" eol)
"/bin/")) "sh-mode.sh")
:hook
(after-save . executable-make-buffer-file-executable-if-script-p))
#+END_SRC
*Note:* we make the script /executable/ by default. See [[https://emacsredux.com/blog/2021/09/29/make-script-files-executable-automatically/][this essay]] for details, but it appears that the executable bit is only turned on if the script has a shebang at the top of the file.
** Fish Shell
#+BEGIN_SRC emacs-lisp
(use-package fish-mode
:mode (rx ".fish" eol)
:hook (fish-mode . (lambda () (add-hook 'before-save-hook 'fish_indent-before-save))))
(use-package fish-mode
:mode (rx ".fish" eol)
:config
(ha-auto-insert-file (rx ".fish")) "fish-mode.sh")
:hook
(fish-mode . (lambda () (add-hook 'before-save-hook 'fish_indent-before-save))))
#+END_SRC
* Technical Artifacts :noexport:
Provide a name in order to =require= this code.

View file

@ -0,0 +1,33 @@
;;; `(file-name-nondirectory (buffer-file-name))` --- $1 -*- lexical-binding: t; -*-
;;
;; Copyright (C) `(format-time-string "%Y")` Howard X. Abrams
;;
;; Author: Howard X. Abrams <http://gitlab.com/howardabrams>
;; Maintainer: Howard X. Abrams <howard.abrams@gmail.com>
;; Created: `(format-time-string "%e %B %Y")`
;;
;; This file is not part of GNU Emacs. Obviously. But you knew that.
;;
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; $2
;;; Code:
$0
(provide '`(file-name-nondirectory (file-name-sans-extension (buffer-file-name)))`)
;;; `(file-name-nondirectory (buffer-file-name))` ends here

8
templates/fish-mode.fish Executable file
View file

@ -0,0 +1,8 @@
#!/usr/bin/env fish
# ----------------------------------------------------------------------
# `(upcase (file-name-nondirectory (file-name-sans-extension (buffer-file-name))))`: $1
# ----------------------------------------------------------------------
function `(downcase (file-name-nondirectory (file-name-sans-extension (buffer-file-name))))` --description "$1"
$0
end

8
templates/sh-mode.sh Executable file
View file

@ -0,0 +1,8 @@
#!/usr/bin/env `(if (equal (file-name-extension buffer-file-name) "fish") "fish" "bash")`
# ----------------------------------------------------------------------
# `(upcase (file-name-nondirectory (file-name-sans-extension (buffer-file-name))))`: $0
# ----------------------------------------------------------------------
set -euo pipefail
$0