summaryrefslogtreecommitdiff
path: root/elisp/erbot/contrib/mkback.el
blob: 4c93c2bd3fcbbc2eeea883d0b45c7dd132616c32 (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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
;;; mkback.el---advanced assistance to manual archiving/backup of files. 
;; Time-stamp: <2004-11-29 17:03:37 deego>
;; Copyright (C) 2002 D. Goel
;; Copyright (C) 2002 Free Software Foundation, Inc.
;; Emacs Lisp Archive entry
;; Filename: mkback.el
;; Package: mkback
;; Author: Deepak Goel <deego@gnufans.org>
;; Keywords: backup project
;; Version: 1.5dev
;; For latest version: 

(defvar mkback-home-page  
  "http://www.gnufans.net/~deego/emacspub/lisp-mine/fastron/")

;; Namespace: mkback-, 
 
;; This file is NOT (yet) part of GNU Emacs.
 
;; This 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 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.
 

;; uncoment this bash script, tweak if needed and save it to, say,
;; ~/bin/mkback. From then on, commands like mkback * will work
;; (interactively) from bash.

;;; #!/bin/bash

;;; emacs -nw -l ~/.emacs --eval="(require 'mkback)" \
;;; --eval="(require 'mkback)" \
;;; --eval="(mkback-from-batch $*)"

;; (with thanks to Damian Elmes), if you prefer aliases: (untested)
;; alias mkback="emacs -batch -nw --eval=\"(progn (require 'mkback) (mkback-from-batch $*)\""


(eval-when-compile (require 'cl))

;; Quick start:
(defvar mkback-quick-start
  "See M-x mkback-introduction.

Drop mkback.el somewhere in your load-path and add to your .emacs.
\(require 'mkback\)
\(mkback-install-for-eshell\)


For advanced users who use autoload mkback, simply add this to .emacs
instead of the above:
\(defvar mkback-after-load-hooks\)
\(add-hook 'mkback-after-load-hooks 'mkback-install-for-eshell\)


You should now have access to M-x mkback in emacs *and* in the
command-line mkback in eshell.

Note that the mkback-install-for-eshell step is optional.  Mkback will
work in eshell even without this step, but this step makes it do good
things for eshell---see commentary. 

For bash access to mkback, see the bash script above.  Try the various
defcustoms to customize."  )

;;;###autoload
(defun mkback-quick-start ()
  "Provides electric help regarding `mkback-quick-start'."
  (interactive)
  (with-electric-help
   '(lambda () (insert mkback-quick-start) nil) "*doc*"))

;;; Introduction:
;; Stuff that gets posted to gnu.emacs.sources
;; as introduction
(defvar mkback-introduction
  "mkback searches for a backup/ folder in the
file's directory, or its parent directory, or the grandparent
directory , and so on.  It then backs up the requested file with
a mirroring of the relative directory structure, and the
current date/time information. The file in question need not be a text
file. 

The primary functions from emacs are M-x mkback and M-x mkback-buffer. 
Add (mkback-install-for-eshell), and you have an eshell-optimized
command called mkback. Finally, you can use mkback from bash by
calling emacs in batch-mode, See the included batch-script at the top
of this file.

Only tested on GNU/Linux.  Designed in a platform-independent
way--should even work on VMS.  Tested with Emacs21.2 only.Type M-x
mkback-quick-start and M-x mkback-commentary for more details.
"  )





;;;###autoload
(defun mkback-introduction ()
  "Provides electric help regarding `mkback-introduction'."
  (interactive)
  (with-electric-help
   '(lambda () (insert mkback-introduction) nil) "*doc*"))

;;; Commentary:
(defvar mkback-commentary
  "Please M-x mkback-quick-start and M-x mkback-introduction first. 

Optionally, add (mkback-install-for-eshell) to .emacs.  That makes
mkback do nice things--- 

* in eshell, typing mkb TAB some-file-name ENTER works.
* in eshell, typing mkb TAB ENTER works..

Note that this tab-completion may not work the very first time if you
follow the autoload-route to mkback-install-for-eshell. 

See the various defcustoms, hooks for customization.


I periodically made backups of the files I used, annotating the
backups with today's dates.  Here are some design decisions:

* The folder is called backup but customizable. 

* Sometimes, one does not want a folder to be cluttered by a huge
  backup/.  Consider this: project/folder1/ project/folder2/ and
  project/folder3/.  You often need to tar up your project/ to deliver
  it to folks.  Now, you don't want folders like
  project/folder1/backup/ existing. 

  In such a case, you would rather mkback a file like
  project/folder1/file.lisp into
  project/backup/folder1/file-date.lisp. 
  
  Thus, mkback looks in current folder and in ancestors for
  backup/'s.


* I did not want to name foo.lisp as foo.lisp-date, because that
changes extension, thus emacs etc. had a hard time recognizing the
backup-file's type. if i ever wanted to browse the backuped file.  So
I preferred foo-date.lisp

* Most of the time, I would not make more than once backup in a day,
but if I did, I could call the new one foo-date-a.lisp etc. 

* If the date is listed in yyyy-mm-dd format, then an alphabetical
directory listing is \(mostly\) also a time-ordered directory listing.
Pretty convenient.  I have started naming all my dates in this
format. One can customize the date-format.
 
* I am almost always in eshell when I do an archiving.  So, there we go..


If you are working on a patch or new feature, it is recommended that
you download the latest mkback from mkback-home-page first, and work
on that.


"
)

;;;###autoload
(defun mkback-commentary ()
  "Provides electric help regarding `mkback-commentary'."
  (interactive)
  (with-electric-help
   '(lambda () (insert mkback-commentary) nil) "*doc*"))

;;; History:

;;; Bugs:






;;; New features:
(defvar mkback-new-features
  "	

 New since 1.4
 ============================================
 
 * By default, the file-modification time of the backup-ed file is
   now same as that of the original file.

 * By default, The backuped file's name now stores both the
   file-modification-time as well as the time at which the backup is
   made. 



"
)


;;;###autoload
(defun mkback-new-features ()
  "Provides electric help regarding `mkback-new-features'."
  (interactive)
  (with-electric-help
   '(lambda () (insert mkback-new-features) nil) "*doc*"))

(defvar mkback-version "1.5dev")

;;==========================================
;;; Code:


(defcustom mkback-chase-links-method 'dir
  "How to chase symlinks
This can take 4 values:
'dir, 'file 'all and 'none. 
'file: only chase file links, 
'all: chase all links, 
'none: don't chase links, 
The author likes the 'dir option. ")

(defvar mkback-before-load-hooks nil)
(defvar mkback-after-load-hooks nil)
(run-hooks 'mkback-before-load-hooks)


(defcustom mkback-create-new-backup-dir-p nil "")
(defcustom mkback-dir "backup" "") 

(defcustom mkback-time-format 
  "-%Y%m%d-%H%M-%S"
  "The string to use for time-format..  More generally, any expression
that evals to a valid string..
The current format is chosen to be windoze compatible. 
The earlier format was:
 -%Y-%m-%d:%H%M:%S"
)


(defcustom mkback-time-format-modtime
  "-%Y%m%d-%H%M-%S--"
  "The string to use for time-format..  More generally, any expression
that evals to a valid string..
We get the last-modified-time of the file and use it here. ")

 
(defcustom mkback-loudness 100
  "suggested: Anywhere from 0 to 100"
)
(defcustom mkback-interactivity 100
  "Suggested: Anywhere from -100  to 100.. 
if this number is too low, mkback will ask you for less and less
confirmations. 
0 is the recommended value once you are familiar with mkback. "
)

(defvar mkback-err nil "internal")
(defcustom mkback-default-get-backup-dir 'mkback-get-backup-dir
  "")
(defcustom mkback-default-get-backup-path-name 'mkback-get-backup-path-name
  "")
(defcustom mkback-default-get-backup-file-name 'mkback-get-backup-file-name
  "")



(defmacro mkback-withit (expr &rest rest)
  "Caution: var-capture by its very nature.."
  `(let ((it ,expr))
     ,@rest))

(defcustom mkback-max-depth 4
  "Is an integer.. this is the max number of ancestors to ascend to look
for an archive directory.  

A value of nil here means: no max number..  Nil is not currently
recommended as can potentially cause infinite looping if no backup/
exists in the entire ancestory.")


(defcustom mkback-keep-time-p t
  "When true, gives the destination file the same last-modified-time
  as that of the original.")


;;;###autoload
(defun mkback-get-backup-dir (dir &optional suffix depth)
  "An example of arguments is:
\(mkback-get-backup-dir /home/aa/bb dd\). 
Then, this function looks for a backup directory in /home/aa/bb.  If
it exists, then this function returns:  /home/aa/bb/backup/dd. 
Else this function calls
\(mkback-get-backup-dir \"/home/aa\" \"dd/bb\" \"ff\" \).  

See what i mean? If no backup/ exists here, then a backup/ exists in a
parent directory.. but then, you want to create aa/ first in that
directory when creating backup, don't you?  So, this function returns
that...

Returns nil if can't find any. 
"
  (unless depth (setq depth 0))
  (if (and mkback-max-depth (> depth mkback-max-depth))
      nil
    (progn
      ;;(unless dir (setq dir default-directory))
      (unless suffix (setq suffix ""))
      (mkback-message 25 "Considering dir= %S and suffix=%S" dir suffix)
      (let* ((dir-unslashed (expand-file-name "" dir))
	     (dir-backup (expand-file-name mkback-dir dir))
	     (dir-backup-suf (expand-file-name suffix dir-backup)))
	(if 
	    (and (file-exists-p dir-backup)
		 (file-directory-p dir-backup))
	    dir-backup-suf
	  (mkback-get-backup-dir
	   ;; parent dir
	   (file-name-directory  dir-unslashed)
	   ;; increase suffix
	   (mkback-withit (file-name-nondirectory dir-unslashed)
		   (if (equal suffix "")  
		       it 
		     ;; commenting this out.. should NOT use /
		     ;;(concat it "/" suffix)
		     (concat (file-name-as-directory it) suffix)
		     )) 
	   (+ depth 1)))))))
    


(defun mkback-chase-links (file)
  (case mkback-chase-links-method
    ('dir
     (let ((dir (or 
		 (file-name-directory file)
		 ;; else take the current directory... this comes in
		 ;; handy when calling mkback-from-batch. 
		 default-directory )))
       ;; since we supply default-directory, 
       ;; this if is now mute...  but let's keep it. 
       (if dir
	   (expand-file-name (file-name-nondirectory file)
			     (file-truename dir))
	 file)))
    ('file (file-chase-links file))
    ('all (file-truename file))
    (t file)))

;;;###autoload
(defun mkback-get-backup-path-name (file &optional backup-dir
				      )
  "Looks around for a suitable backup/ directory nearby and returns a
suitable backup pathname. 

this is one heck of a powerful function..

SHOULD REALLY USE the function file-name-directory!
"
  (setq file (mkback-chase-links file))
  
  (let 
      ((initdir
	(file-name-directory (expand-file-name file))))
    (unless backup-dir
      (setq backup-dir (funcall mkback-default-get-backup-dir 
			initdir)))
    (if (not backup-dir)
	(if mkback-create-new-backup-dir-p
	    (setq backup-dir 
		  (expand-file-name mkback-dir 
				    initdir))
	  (error "No archi(v)e directory exists here or nearby. "))))

  (funcall mkback-default-get-backup-file-name file backup-dir))



(defun mkback-get-backup-file-name (file dir)
  "File is the original file, dir is the destination directory.
This function will thus rename file with date appended, and then
append the same to the destination directory. "
  (let* 
      ((baseinit (file-name-sans-extension
	      (file-name-nondirectory file)))
       (extinit (file-name-extension file))
       (base
	(if (string= baseinit "")
	    (concat "." extinit) baseinit))
       (ext2
	(if (string= baseinit "")
	    nil extinit))
       (raw-name-file
	(concat
	 base
	 (format-time-string 
	  (eval mkback-time-format-modtime)
	  (nth 5 (file-attributes file)))
	 (format-time-string (eval mkback-time-format))
	 ))
       (raw-name 
	(expand-file-name
	 raw-name-file
	 dir))
       (ext
	(mkback-withit ext2
		       (if (null it) ""
			 (concat "." it )))))
    (while
	(file-exists-p (concat raw-name ext))
      (setq raw-name (concat raw-name "a")))
    (concat raw-name ext)))


(defun mkback-message (points &rest args)
  (when (> (+ points mkback-loudness) 50)
    (apply #'message args)))



;;; 2002-05-03 T09:41:03-0400 (Friday)    D. Goel
(defun mkback-no-errors (file)
  (mkback-ignore-errors (mkback file)))


(defvar mkback-after-backup-hook nil
  "Each of the functions in this hook shall take two arguments: the
full name of the original file and the ful name of the backuped
file. ")

(defcustom mkback-gzip-p nil
  "Whether to gzip the mkbacked files. More generally, any post-backup
action to perform on the backuped file. ")

(defcustom mkback-gzip-expression
  '(when
       (> (nth 7 (file-attributes it)) 250)
     (shell-command (format "gzip %s" it)))
  "Use it for the filename here. The current expression works only on
gnulinux type systems.")

;;;###autoload
(defun mkback (&optional file)
  "Backup file/files. 
With no argument, will prompt for file.  If file is a list of files instead of
one file, will loop over them.  

When file is a single file, Returns nil if backup fails, returns
non-nil otherwise. 

When file is a list of files, returns the list of such results. 
"
  (interactive "F")
  (unless file 
    (setq file (read-file-name "File: ")))
  (unless file (error "No filaname supplied to mkback: nil"))
  (if (listp file)
      (mapcar #'mkback-no-errors file)
    (progn
      (unless (file-exists-p file) 
	(error "File does not exist: %S" file))
      (when (file-directory-p file)
	(error "Currently, can archive only files, not directies: %S" file))
      (mkback-withit
       (funcall mkback-default-get-backup-path-name file)
       (let* ((dir (file-name-directory it))
	      (dir-existsp (file-exists-p dir))
	      (failed nil))
	 (unless dir-existsp
	   (if (mkback-y-or-n-p 50
		(format "Create directory %S" dir))
	       (make-directory dir t)
	     (mkback-message 99 "Not creating directory!")))
	 (setq dir-existsp (and (file-exists-p dir) (file-directory-p dir)))
	 (setq failed (not dir-existsp))
	 (unless failed
	   (if
	       (mkback-y-or-n-p 0
		(format "Copy %S to %S" (file-name-nondirectory file)
			it))
	       (progn
		 (copy-file file it nil mkback-keep-time-p))
	     (setq failed t)))
	 (run-hook-with-args 'mkback-after-backup-hook file it)
	 (setq failed (not (file-exists-p it)))
	 (when mkback-gzip-p
	   (ignore-errors
	     (eval 
	      mkback-gzip-expression)))
	 (if failed
	     (mkback-message 99 "File not created: %s " it)
	   (mkback-message 99 "Backup (now) exists:\n %s" it))
	 (not failed))))))


  
	
;;;###autoload
(defun mkback-install-for-eshell ()
  (interactive)
  (defalias 'eshell/mkback 'mkback))


(defmacro mkback-ignore-errors (&rest body)
  "\(Programmer: This function should track my ignore-errors-my. \)

Like ignore-errors, but tells the error..
Improved for me by Kalle on 7/3/01:
 * used backquote: something i was too lazy to convert my macro to..
 * removed the progn: condition-case automatically has one..
 * made sure that the return is nil.. just as it is in ignore-errors. "
  `(condition-case mkback-err (progn ,@body)
     (error
      (ding t)
      (ding t)
      (ding t)
      (message "IGNORED ERROR: %s" (error-message-string mkback-err))
      (sit-for 1)
      nil)))




;;; 2002-05-03 T11:05:43-0400 (Friday)    D. Goel
(defun mkback-y-or-n-p (add &rest args)
  (if (> (+ add mkback-interactivity) 50)
      (apply 'y-or-n-p args)
    t))


;;; 2002-05-03 T11:07:10-0400 (Friday)    D. Goel
;;;###autoload
(defun mkback-buffer ()
  (interactive)
  (mkback-withit 
   (buffer-file-name)
   (if it (mkback it)
     (mkback-message 0 "Buffer has no associated file: %S"
		    (buffer-name)))))

;;; 2002-05-03 T11:10:32-0400 (Friday)    D. Goel
;;;###autoload
(defun mkback-buffer-doit ()
  (interactive)
  (let ((mkback-interactivity -100))
    (mkback-buffer)))


(defmacro mkback-from-batch (&rest files)
  "The files get passed to emacs as symbols.. we need to simply format
them.."
  `(mkback
    (mkback-symbols-to-strings (quote ,files))))

(defun mkback-symbols-to-strings (files)
  (cond
   ((null files) nil)
   ((listp files) (mapcar 'mkback-symbols-to-strings files))
   (t (format "%s" files))))





(defmacro mkback-from-batch-doit (&rest files)
  "The files get passed to emacs as symbols.. we need to simply format
them.."
  `(let ((mkback-interactivity -100))
     (mkback
      (mkback-symbols-to-strings (quote ,files)))))


(defun mkback-symbols-to-strings (files)
  (cond
   ((null files) nil)
   ((listp files) (mapcar 'mkback-symbols-to-strings files))
   (t (format "%s" files))))


;; these 2 provided for historical compatibility for the next few versions..
;; and THEY WILL BE REMOVED SOON.. 
(defalias 'mkback-this-buffer 'mkback-buffer)
(defalias 'mkback-this-buffer-doit 'mkback-buffer-doit)

(provide 'mkback)
(run-hooks 'mkback-after-load-hooks)



;;; mkback.el ends here