From 9f6eca95a16d5bc5495d867095e461074bea54ac Mon Sep 17 00:00:00 2001 From: Howard Abrams Date: Fri, 12 Nov 2021 13:05:31 -0800 Subject: [PATCH] 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. --- ha-config.org | 9 +++++++++ ha-programming.org | 17 +++++++++++++---- templates/emacs-lisp-mode.el | 33 +++++++++++++++++++++++++++++++++ templates/fish-mode.fish | 8 ++++++++ templates/sh-mode.sh | 8 ++++++++ 5 files changed, 71 insertions(+), 4 deletions(-) create mode 100644 templates/emacs-lisp-mode.el create mode 100755 templates/fish-mode.fish create mode 100755 templates/sh-mode.sh diff --git a/ha-config.org b/ha-config.org index a962102..edc342a 100644 --- a/ha-config.org +++ b/ha-config.org @@ -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 diff --git a/ha-programming.org b/ha-programming.org index d72a20e..0b25204 100644 --- a/ha-programming.org +++ b/ha-programming.org @@ -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. diff --git a/templates/emacs-lisp-mode.el b/templates/emacs-lisp-mode.el new file mode 100644 index 0000000..e7b0e1d --- /dev/null +++ b/templates/emacs-lisp-mode.el @@ -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 +;; Maintainer: Howard X. Abrams +;; 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 . + +;;; Commentary: +;; +;; $2 + +;;; Code: + +$0 + +(provide '`(file-name-nondirectory (file-name-sans-extension (buffer-file-name)))`) +;;; `(file-name-nondirectory (buffer-file-name))` ends here diff --git a/templates/fish-mode.fish b/templates/fish-mode.fish new file mode 100755 index 0000000..4cd9098 --- /dev/null +++ b/templates/fish-mode.fish @@ -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 diff --git a/templates/sh-mode.sh b/templates/sh-mode.sh new file mode 100755 index 0000000..92f5949 --- /dev/null +++ b/templates/sh-mode.sh @@ -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