summaryrefslogtreecommitdiffhomepage
path: root/lib/skels/jao-skel.el
blob: 3a6672c7733f2b5abba13d8ef70462038f4b68a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
;; jao-skel.el: common definitions and functions  -*- lexical-binding: t; -*-

;; Copyright (C) 2004-2012, 2022 Jose Antonio Ortega Ruiz

;; Author: Jose A Ortega Ruiz <jao@gnu.org>
;; Keywords: tools

;; This file 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, or (at your option)
;; any later version.

;; This file 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 GNU Emacs; see the file COPYING.  If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.

;;; Commentary:

;; Aux functions used in other skeletons

;;; Code:

(require 'skeleton)
(require 'autoinsert)

(defvar jao-skel-company-name nil
  "Company name used in copyright notice")

(defvar jao-skel-copyright-file ".copyright"
  "Basename of the raw (uncommented) copyright file")

(defun jao-skel--prefix (pref) (or pref (concat comment-start " ")))
(defun jao-skel--suffix (suff) (or suff (concat " " comment-end)))

(defun jao-skel-copyright-line (prefix &optional suffix omit-cpy)
  "Create a brief copyright notice with given PREFIX and SUFFIX"
  (concat (jao-skel--prefix prefix)
          (if omit-cpy "" "Copyright ")
          "(c) " (format-time-string "%Y") " "
          (or jao-skel-company-name (user-full-name))
          (jao-skel--suffix suffix) "\n"))

(defun jao-skel-date-line (prefix &optional suffix)
  "Create a start date line"
  (concat (jao-skel--prefix prefix)
          "Start date: " (format-time-string "%a %b %d, %Y %H:%M")
          (jao-skel--suffix suffix) "\n"))

(defun jao-skel-author-line (prefix &optional suffix)
  "Create an author date line"
  (concat (jao-skel--prefix prefix)
          "Author: " (user-full-name) " <" user-mail-address "> "
          (jao-skel--suffix suffix) "\n"))

(defun jao-skel-copyright-lines (&optional prefix suffix)
  (concat (jao-skel-copyright-line prefix suffix) "\n"
          (jao-skel-author-line prefix suffix)
          (jao-skel-date-line prefix suffix)))

(defun jao-skel-basename ()
  "Get buffer file name without dir nor extension"
  (file-name-sans-extension (file-name-nondirectory (buffer-file-name))))

(defun jao-skel-extension ()
  "Find the extension of the currently visited file"
  (let ((elems (split-string (file-name-nondirectory (buffer-file-name))
                             "\\.")))
    (nth (- (length elems) 1) elems)))

(defun jao-skel--insert-commented-file (file-name)
  (let* ((start (point))
         (end (+ start (cadr (insert-file-contents file-name)))))
    (goto-char end)
    (comment-region start (point))))

(defun jao-skel-insert-license ()
  (let ((dir (locate-dominating-file (buffer-file-name) jao-skel-copyright-file)))
    (when dir
      (let ((file (expand-file-name jao-skel-copyright-file dir)))
        (when (file-exists-p file)
          (jao-skel--insert-commented-file file))))))

(defun jao-skel-install (regexp skel)
  (let ((ex (assoc regexp auto-insert-alist)))
    (if ex (setf (cdr ex) skel)
      (add-to-list 'auto-insert-alist (cons regexp skel)))))

(defsubst jao-skel-install* (lst)
  (mapc (lambda (x) (apply #'jao-skel-install x))  lst))

(provide 'jao-skel)