;; 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)
