-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheader.el
265 lines (208 loc) · 7.92 KB
/
header.el
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
;*******************************************************************************;
; ;
; 42_header.el for 42 Emacs header ;
; Created on : Tue Jun 18 10:46:22 2013 ;
; Made by : David "Thor" GIRON <[email protected]> ;
; ;
;*******************************************************************************;
(require 'string)
(require 'list)
(require 'comments)
;******************************************************************************;
; ;
; ::: :::::::: ;
; filename_____________________________.ext :+: :+: :+: ;
; +:+ +:+ +:+ ;
; By: login____ <[email protected]> +#+ +:+ +#+ ;
; +#+#+#+#+#+ +#+ ;
; Created: yyyy/mm/dd 15:27:11 by login____ #+# #+# ;
; Updated: 2018/06/17 04:23:01 by suvitiel ### ########.fr ;
; ;
;******************************************************************************;
(global-set-key (kbd "C-c C-h") 'header-insert)
(setq write-file-hooks (cons 'header-update write-file-hooks))
(set 'user-login "tmaze")
(set 'user-mail (let ((mail (getenv "MAIL")))
(if (string= mail nil)
mail)
)
)
(set 'left-std-margin 5)
(set 'right-std-margin 5)
(set 'info-std-width 41)
(set 'ft-1 " ::: ::::::::")
(set 'ft-2 " :+: :+: :+:")
(set 'ft-3 " +:+ +:+ +:+ ")
(set 'ft-4 " +#+ +:+ +#+ ")
(set 'ft-5 "+#+#+#+#+#+ +#+ ")
(set 'ft-6 " #+# #+# ")
(set 'ft-7 " ### ########.fr ")
(set 'ft-std-width 25)
;*******************************************************************************;
(defun header-chop-str (str n)
(if (> (length str) n)
(let* ((max (- n 3))
(new (substring str 0 max)))
(concat new "..."))
str)
)
(defun header-make-left-margin ()
"Creates the header comments start token and left margin"
(let ((fill (string-fill (- left-std-margin (comments-start-token-length)))))
(concat (comments-start-token) fill))
)
(defun header-make-right-margin ()
"Creates the header right margin and comments end token"
(let ((fill (string-fill (- right-std-margin (comments-end-token-length)))))
(concat fill (comments-end-token)))
)
(defun header-make-central-gap (left-chunk right-chunk)
"Creates the gap between the left infos block and the right logo"
(string-fill (- line-std-width
(string-length left-chunk)
(string-length right-chunk)))
)
(defun header-make-file-name ()
"Creates the 'file.ext' entry of the header."
(let* ((filename (header-chop-str (file-name-nondirectory (buffer-file-name))
info-std-width))
(fill (string-fill (- info-std-width (string-length filename)))))
(concat filename fill))
)
(defun header-make-by ()
"Creates the 'By: login <mail>' entry of the header."
(let* ((mail-span (- info-std-width (+ (length user-login) 7)))
(by (concat "By: " user-login " <" (header-chop-str user-mail mail-span) ">"))
(fill (string-fill (- info-std-width (string-length by)))))
(concat by fill))
)
(defun header-make-creation-date ()
"Creates the 'Created: yyyy/mm/dd hh:mm:ss' entry of the header.'"
(concat "Created: " (format-time-string "%Y/%m/%d %T") " by " user-login)
)
(defun header-make-update-date ()
"Creates the 'Updated: yyyy/mm/dd hh:mm:ss' entry of the header.'"
(concat "Updated: " (format-time-string "%Y/%m/%d %T") " by " user-login)
)
;*******************************************************************************;
(defun header-insert-line-01 ()
"Line 1 of the header"
(comments-insert-bar)
)
(defun header-insert-line-02 ()
"Line 2 of the header"
(comments-insert-empty-line)
)
(defun header-insert-line-03 ()
"Line 3 of the header"
(let* ((left-margin (header-make-left-margin))
(right-margin (header-make-right-margin))
(central-gap (header-make-central-gap
left-margin
(concat ft-1 right-margin))))
(insert (concat left-margin central-gap ft-1 right-margin))
)
)
(defun header-insert-line-04 ()
"Line 4 of the header"
(let* ((left-margin (header-make-left-margin))
(right-margin (header-make-right-margin))
(filename (header-make-file-name))
(central-gap (header-make-central-gap (concat left-margin filename)
(concat ft-2 right-margin))))
(insert (concat left-margin filename central-gap ft-2 right-margin))
)
)
(defun header-insert-line-05 ()
"Line 5 of the header"
(let* ((left-margin (header-make-left-margin))
(right-margin (header-make-right-margin))
(central-gap (header-make-central-gap left-margin
(concat ft-3 right-margin))))
(insert (concat left-margin central-gap ft-3 right-margin))
)
)
(defun header-insert-line-06 ()
"Line 6 of the header"
(let* ((left-margin (header-make-left-margin))
(right-margin (header-make-right-margin))
(by (header-make-by))
(central-gap (header-make-central-gap (concat left-margin by)
(concat ft-4 right-margin))))
(insert (concat left-margin by central-gap ft-4 right-margin))
)
)
(defun header-insert-line-07 ()
"Line 7 of the header"
(let* ((left-margin (header-make-left-margin))
(right-margin (header-make-right-margin))
(central-gap (header-make-central-gap left-margin
(concat ft-5 right-margin))))
(insert (concat left-margin central-gap ft-5 right-margin))
)
)
(defun header-insert-line-08 ()
"Line 8 of the header"
(let* ((left-margin (header-make-left-margin))
(right-margin (header-make-right-margin))
(created (header-make-creation-date))
(central-gap (header-make-central-gap (concat left-margin created)
(concat ft-6 right-margin))))
(insert (concat left-margin created central-gap ft-6 right-margin))
)
)
(defun header-insert-line-09 ()
"Line 9 of the header"
(let* ((left-margin (header-make-left-margin))
(right-margin (header-make-right-margin))
(updated (header-make-update-date))
(central-gap (header-make-central-gap (concat left-margin updated)
(concat ft-7 right-margin))))
(insert (concat left-margin updated central-gap ft-7 right-margin))
)
)
(defun header-insert-line-10 ()
"Line 10 of the header"
(comments-insert-empty-line)
)
(defun header-insert-line-11 ()
"Line 11 of the header"
(comments-insert-bar)
)
;*******************************************************************************;
(defun header-insert ()
"Creates a header for the current source file."
(interactive)
(save-excursion
(goto-char (point-min))
(header-insert-line-01)
(header-insert-line-02)
(header-insert-line-03) (newline)
(header-insert-line-04) (newline)
(header-insert-line-05) (newline)
(header-insert-line-06) (newline)
(header-insert-line-07) (newline)
(header-insert-line-08) (newline)
(header-insert-line-09) (newline)
(header-insert-line-10)
(header-insert-line-11)
)
)
(defun header-update ()
"Updates the header for the current source file."
(interactive)
(save-excursion
(if (buffer-modified-p)
(progn
(goto-char (point-min))
(if (search-forward "Updated" nil t)
(progn
(delete-region
(progn (beginning-of-line) (point))
(progn (end-of-line) (point)))
(header-insert-line-09)
(message "Header up to date."))))))
nil)
;******************************************************************************;
(provide 'header)