summaryrefslogtreecommitdiffhomepage
path: root/attic/skels/common-skel.el
blob: 977e7ea186d549fedfaea3cca6234959016d13d7 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
;; common definitions and functions

;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2012 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-company-name nil
  "Company name used in copyright notice")

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

(defvar jao-skels-default-scm nil
  "Default SCM system")

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

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

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

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

(defun jao-cvs-line (prefix &optional suffix)
  "Create a CVS ID line"
  (concat (jao-prefix prefix) "$" "Id$" (jao-suffix suffix) "\n"))

(defun jao-svn-line (prefix &optional suffix)
  "Create a SVN ID line"
  (concat (jao-prefix prefix) "X-SVN: $" "Id$" (jao-suffix suffix) "\n"))

(defun jao-arch-line (&optional prefix suffix)
  "Create an arch-tag line"
  (let ((uuid (shell-command-to-string "uuidgen")))
    (concat (jao-prefix prefix) "arch-tag: " uuid (jao-suffix suffix) "\n")))

(defun jao-insert-arch-line ()
  (interactive)
  (insert (jao-arch-line)))

(defun jao-scm-line (prefix &optional suffix)
  "Create an scm line"
  (let* ((formats '(("arch" . jao-arch-line)
                    ("svn" . jao-svn-line)
                    ("cvs" . jao-cvs-line)
                    ("none" . (lambda (p f) ""))))
         (names (mapcar 'car formats))
         (prompt (concat "SCM (" (mapconcat 'identity names ", ") "): "))
         (sel (or jao-skels-default-scm
                  (completing-read prompt formats nil 1)))
         (fun (cdr (assoc sel formats))))
    (when fun (concat (funcall fun prefix suffix)))))

(defun jao-c&co-line (&optional prefix suffix)
  (concat (jao-scm-line prefix suffix) "\n"
          (jao-co-line prefix suffix)))

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

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

(defun jao-basedir ()
  "Base directory"
  (file-name-nondirectory
   (substring (file-name-directory (buffer-file-name)) 0 -1)))

(defun jao-dir-level (l)
  (let ((elems (split-string
                (file-name-sans-extension (buffer-file-name)) "/")))
    (mapconcat 'identity (nthcdr (- (length elems) (+ 1 l)) elems) "/")))

(defun jao-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-other-file-name (ext1 ext2)
  "Find the complimentary file name of header/source file"
  (let ((extension (jao-extension))
        (basename (jao-basename)))
    (if (string= extension ext1) (concat basename "." ext2)
      (concat basename "." ext1))))

(defun jao-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-insert-copyright-file ()
  (let ((dir (locate-dominating-file (buffer-file-name) jao-copyright-file)))
    (when dir
      (let ((file (expand-file-name jao-copyright-file dir)))
        (when (file-exists-p file)
          (jao-insert-commented-file file))))))

(defun jao-provide-skel (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-provide (lst)
  (mapc (lambda (x) (apply #'jao-provide-skel x))  lst))

(provide 'common-skel)