Recently Written · git

coleslaw-snippets

Boilerplate for Coleslaw markdown.

git clone https://github.com/equwal/coleslaw-snippets

Log | Files | Refs


coleslaw-snippets.el (4186 bytes)

1 ;;; coleslaw-snippets.el --- Yasnippets for Coleslaw markup files. -*- lexical-binding: t; -*-
2 ;; Copyright (C) 2019 Spenser Truex
3 ;; Author: Spenser Truex <web@spensertruex.com>
4 ;; Created: 2019-06-22
5 ;; Version: 0.0.1
6 ;; Package-Requires: ((emacs "24.4") (yasnippet "0.13.0"))
7 ;; Keywords: coleslaw wp convenience docs
8 ;; URL: https://spensertruex.com/coleslaw-snippets
9 ;; Homepage: https://spensertruex.com/coleslaw-snippets
10 ;; This file is not part of GNU Emacs.
11 ;; This file is free software.
12 ;; License:
13 ;; Licensed with the GNU GPL v3 see:
14 ;; <https://www.gnu.org/licenses/>.
15 
16 ;;; Commentary:
17 ;; Snippets for all Coleslaw markdown files
18 ;;
19 ;; -   Also see coleslaw-mode for dispatching the mode based on the
20 ;; `format' header.
21 ;; -   To install for coleslaw-mode, add
22 ;;
23 ;; &nbsp;
24 ;; (add-hook 'coleslaw-mode-hook 'coleslaw-snippets)
25 ;;
26 ;; to your init file, and bind coleslaw-insert-header to a key like
27 ;;
28 ;; (global-set-key (kbd "C-c H") 'coleslaw-insert-header)
29 ;;
30 ;; You may also choose to use the autoinsert package to automatically
31 ;; insert the snippets into coleslaw markdown files. Then you would add:
32 ;;
33 ;; (dolist (type '("\\.page\\'" "\\.post\\'"))
34 ;;   (add-to-list 'auto-mode-alist (cons type 'coleslaw-mode)))
35 ;;
36 ;; to your init.
37 ;;
38 ;; You may choose to advise the coleslaw-insert-header function with
39 ;; coleslaw-dispatch if you have coleslaw-mode installed.
40 ;;
41 ;; More docs at https://spensertruex.com/coleslaw-snippets
42 
43 ;;; Code:
44 
45 (require 'cl-lib)
46 
47 (defvar coleslaw-snippets-separator ";;;;;"
48   "The string used between the coleslaw headers as in the example:
49 ;;;;;
50 title: Example
51 format: cl-who
52 date: 2019-06-15
53 ;;;;;
54 Where the separator is \";;;;;\".")
55 
56 (defvar coleslaw-snippets-formats (list "md" "cl-who" "rst" "html" "org")
57   "The format header values that coleslaw will allow to be auto-inserted.")
58 
59 (defun coleslaw-snippets--valid-format (str)
60   "Determine if the STR is permissible for a format: header in Coleslaw."
61   (when (stringp str)
62     (cl-some (lambda (x) (string-equal x str)) coleslaw-snippets-formats)))
63 
64 
65 (defun coleslaw-snippets--bufftype (type)
66   "Determine if the file type of the current buffer is TYPE."
67   (string-equal type (cl-subseq buffer-file-name (- (length buffer-file-name) 5))))
68 
69 (defun coleslaw-snippets--insist-format (first-prompt &optional second-prompt)
70   "Insist that the format inserted be a valid coleslaw format.
71 Add formats to the `coleslaw-snippets-formats' list for new features or
72 nonstandard formats.  FIRST-PROMPT and SECOND-PROMPT are used to
73 prompt the user at the minibuffer.  The FIRST-PROMPT is for the
74 first, and SECOND-PROMPT is used in subsequent requests.  If
75 FIRST-PROMPT is NIL, the second-prompt is only used."
76   (let ((res (read-from-minibuffer (if first-prompt
77                                        first-prompt
78                                      second-prompt))))
79     (if (coleslaw-snippets--valid-format res)
80         res
81       (coleslaw-snippets--insist-format nil second-prompt))))
82 
83 ;;;###autoload
84 (defun coleslaw-snippets-insert-header  ()
85   "Insert the skeleton for as specified by default for a coleslaw file type."
86   (interactive)
87   (skeleton-insert '(nil str
88                          "\ntitle: "
89                          (skeleton-read "title: ")
90                          "\nformat: "
91                          (coleslaw-snippets--insist-format "format: "
92                                                   "Bad format, try another format: ")
93                          (if (coleslaw-snippets--bufftype ".page")
94                              (concat "\nurl: " (skeleton-read "url: "))
95                            "")
96                          (if (coleslaw-snippets--bufftype ".post")
97                              (if (y-or-n-p "Insert excerpt? ")
98                                  (concat "\nexcerpt: "
99                                          (skeleton-read "excerpt: "))
100                                "")
101                            "")
102                          "\ndate: "
103                          (format-time-string "%Y-%m-%d" (current-time))
104 			 "\n" str)
105 		               0 (regexp-quote coleslaw-snippets-separator)))
106 
107 (provide 'coleslaw-snippets)
108 ;;; coleslaw-snippets.el ends here