summaryrefslogtreecommitdiffhomepage
path: root/lib/net/randomsig.el
blob: 71f31e16270eeea9fdc6d68cad193b82c19b5f97 (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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
;;; randomsig.el --- insert a randomly selected signature

;; Copyright (C) 2001, 2002, 2013, 2020, 2021 Hans-Jürgen Ficker

;; Emacs Lisp Archive Entry
;; Author: Hans-Juergen Ficker <hj@backmes.de>
;; Version: 0.7.0
;; X-CVS-Version: $Id: randomsig.el,v 1.1.1.1 2003/09/17 22:49:45 jao Exp $
;; Keywords: mail random signature

;; This file is not currently part of GNU Emacs.

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

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

;;; Commentary:

;; This is yet another implementation to insert a randomly choosen
;; signature into a mail.

;; It is only tested with gnus.

;; To make it work, put the following lines into your ~/.gnus:

;; (require 'randomsig)
;; (define-key message-mode-map (kbd "C-c s") 'randomsig-replace-sig)
;; (define-key message-mode-map (kbd "C-c S") 'randomsig-select-sig)
;; (require 'gnus-sum) ; probably required for `gnus-summary-save-map'
;; (define-key gnus-summary-save-map "-" 'gnus/randomsig-summary-read-sig)
;; (setq randomsig-dir "/some/directory")
;; (setq randomsig-files '("some" "files"))
;; ;; or (setq randomsig-files (randomsig-search-sigfiles))
;; ;; or (setq randomsig-files 'randomsig-search-sigfiles)
;; (setq message-signature 'randomsig-signature)

;; This will also define the shortcut `C-c s' in message-mode to
;; change the signature, `C-c S' in message-mode to interactively
;; select the signature to replace the current signature, and `O -' in
;; gnus-summary-mode to read the signature from the selected mail.

;; `randomsig-files' must be a list of existing files, an existing
;; file, or a function returning a list of existing files. If these
;; don't have absolute paths, they are located in `randomsig-dir'.

;; File format: Each file must contain at least one signature.
;; Signatures are separated with `randomsig-delimiter-pattern'. If
;; there is only one signature in the file, the delimiter can be
;; omitted, so real .signature-files can be used.

;; `randomsig-delimiter' is used when inserting new signatures with
;; `randomsig-message-read-sig' into the signature file. So
;; `randomsig-delimiter' should match `randomsig-delimiter-pattern'.

;; `randomsig-static-string' is put in front of every random signature
;; if non-`nil'.

;; The *-read-sig functions read the signature of a message, or use
;; the marked text, and write it to a signature-file, for which the
;; name is asked. If the file does not exist, it will be generated.
;; When called with any prefix, the signatures will be offered to edit
;; before saving.

;; if `randomsig-replace-sig' is called with any prefix, it will ask
;; for a file to get the signature from.

;; `randomsig-select-sig' will offer a list of signatures to select
;; from in an extra buffer. n will jump to the next signature, p to
;; the previous, RET will insert the selected signature, q will exit
;; the selection buffer without replacing the current signature, R
;; will reload the signature-files, and e will open a buffer for
;; editing the signature at the point. When called with any prefix, it
;; will ask for a file to get the signatures from

;; `randomsig-search-sigfiles' will search for regular files in
;; `randomsig-dir', which do not match `randomsig-search-unwanted'. A
;; subdirectory of `randomsig-dir' can be given as optional argument.

;; Completion will only work for files in `randomsig-files', though
;; others files can be used, too.

;;; Changelog:

;; 2001/04/12   0.1
;; * Initial release

;; 2001/04/19   0.2
;; * inserted `randomsig-delimiter' to add the capability to change
;;   the delimiter between the signatures (thanks to Andreas Büsching
;;   <crunchy@tzi.de>)

;; 2001/04/25   0.3
;; * new function `randomsig-search-sigfiles', to search all regular files
;;   in directory `randomsig-dir'
;; * normal signatures only worked, when using only one signature. Fixed.

;; 2001/04/25   0.3.1
;; * Fixed a bug in `randomsig-search-sigfiles'

;; 2001/04/26   0.3.2
;; * replaced `point-at-eol' with `line-end-position' (Don't know where
;;   `point-at-eol' is defined)
;; * require cl
;; * require message in some functions

;; 2001/07/09   0.3.3
;; * don't (setq message-signature 'randomsig-signature) by default,
;;   the user can do this in his .gnus
;; * remove unnecessary optional arguments to `find-file-noselect' to
;;   make it work with XEmacs
;; (Thanks to Micha Wiedenmann <Micha.Wiedenmann@gmx.net> for both
;; suggestions)
;; * documentation updates

;; 2001/07/12   0.3.4
;; * more fixes for XEmacs
;; * more documentation Updates

;; 2001/07/20   0.4.0
;; * new command `randomsig-select-sig' to interactively select a signature
;; * new mode `randomsig-select-mode' (for `randomsig-select-sig')
;; * `randomsig-files' can also be function returning a list of
;;   Signature files
;; * `randomsig-replace-sig' does not remove old signature when interrupted

;; 2001/07/22   0.4.1
;; * (require 'message) only when needed

;; 2001/08/13   0.5.0
;; * doesn't require message anymore, so it should work without gnus

;; 2001/08/20   0.5.1
;; * add (random t) to initialize random seed (thanks to Evgeny
;;   Roubinchtein <evgenyr@cs.washington.edu> for pointing this out
;; * insert a newline if it is missing at the end of a signature file

;; 2001/09/17   0.5.2
;; * new variable `randomsig-static-string' (thanks to Raymond Scholz
;;   <rscholz@zonix.de>)

;; 2001/10/01   0.5.3
;; * Documentation updates

;; 2002/01/20   0.5.99
;; * It is now possible to edit signatures before saving, or to edit
;;   single signatures from the selection buffer.
;; * Mark many variables as user option
;; * randomsig-files-to-list works recursive

;; 2002/03/04   0.6.0
;; * `randomsig-replace-signature-in-signature-files' should be safer now
;; * `randomsig-files-to-list' did endless recursion when called
;;   with nil. Fixed.
;; * Some error-handling for non-existing `randomsig-dir'.

;; 2002/09/21   0.7.0
;; * most variables customizable
;; * `randomsig-static-string' works for `randomsig-select-sig', too
;;   (thanks to Mark Trettin <mtr-dev0@gmx.de> for pointing this out)
;; * documentation updates

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


(defconst randomsig-version "0.7.0")


(defvar randomsig-dir "~/.signatures"
  "*Directory for signature-files. See also `randomsig-files'")


(defgroup randomsig nil
  "insert a randomly choosen signature into a mail."
  :group 'mail
  :group 'news)

(defcustom randomsig-files '("default")
  "*Files with random signatures.
This variable may be a list of strings, a string, or a function returning a
list of strings.
The files are searched in `randomsig-dir', if they don't have absolute paths.
The signatures have to be separated by lines matching
`randomsig-delimiter-pattern' at the beginning."
  :type '(choice
	  (repeat
	   :tag "List of filenames"
	   (string :tag "filename"))
	  (function
	   :tag "function returning the signature files"
	   :value randomsig-search-sigfiles))
  :group 'randomsig)

(defcustom randomsig-delimiter "-- "
  "*delimiter used when adding new signatures in signature file.
You have to change `randomsig-delimiter-pattern', too, if you change this."
  :type '(string)
  :group 'randomsig)


(defcustom randomsig-delimiter-pattern
  (concat "^" (regexp-quote randomsig-delimiter) "$")
  "*Regular expression that matches the delimiters between signatures.
`randomsig-delimiter' must match `randomsig-delimiter-pattern'."
  :type '(regexp)
  :group 'randomsig)


(defcustom randomsig-search-unwanted "\\(/\\|^\\)\\(CVS\\|RCS\\|.*~\\)$"
  "*Regular expression matching unwanted files when scanning with
`randomsig-search-sigfiles'"
  :type '(regexp)
  :group 'randomsig)


(defcustom randomsig-static-string nil
  "*Static string to be inserted above every random signature.
You probably want to have a newline at the end of it."
  :type '(choice
	  (const :tag "none" nil)
	  (string))
  :group 'randomsig)


(defvar randomsig-buffer-name "*Signatures*"
  "Name for the (temporary) buffer for the signatures")

(defvar randomsig-edit-buffer-name "*Edit Signature*"
  "Name for the (temporary) buffer for editing the signatures")

(defvar randomsig-select-original-buffer nil)
(defvar randomsig-select-original-position nil)

(defvar randomsig-history nil)

(defvar randomsig-buffer-file-pos-list nil)

(defvar randomsig-select-edit-bufferpos nil)

(defvar randomsig-loaded-files nil)

;; definitions for XEmacs:
(unless (fboundp 'line-end-position)
  (defalias 'line-end-position 'point-at-eol))

(defun randomsig-mark-active-p () mark-active)

(require 'cl-lib)

(random t) ; Initialize random seed

;;; Helper Functions

(defun randomsig-files-to-list (files)
  ;; return a list of strings
  (cond ((listp files) files)
	((and (symbolp files)
	      (fboundp files))
         (randomsig-files-to-list (funcall files)))
	((and (symbolp files)
	      (boundp files))
         (randomsig-files-to-list (symbol-value files)))
	((stringp files) (list files))
	(t nil)))

(defun randomsig-prompt (&optional prompt)
  ;; Prompt for a signature file.
  (let ((files (randomsig-files-to-list randomsig-files)))
    (completing-read (if prompt prompt "signature: ")
                     (mapcar 'list files) nil t nil randomsig-history (car files))))

(defun randomsig-read-signatures-to-buffer (buffer-name &optional files)
  ;; read the signatures into the signature buffer
  ;; save possibly local variables `randomsig-files' and `randomsig-dir'
  (let ((sigfiles randomsig-files) (sigdir randomsig-dir))
    (if (get-buffer buffer-name)
	(progn
	  (set-buffer buffer-name)
	  (setq buffer-read-only nil)
	  (delete-region (point-min) (point-max)))
      (progn
	(get-buffer-create buffer-name)
	(set-buffer buffer-name)))
    (set (make-local-variable 'randomsig-files) sigfiles)
    (set (make-local-variable 'randomsig-dir) sigdir))

  (setq randomsig-buffer-file-pos-list nil)

  (unless files
    (setq files randomsig-files))

  (setq randomsig-loaded-files files)

  ;; get a list with file names of signature files
  (let ((sigfiles (randomsig-files-to-list files)))
    ;; Insert all files into the newly created buffer
    (mapcar
     (lambda (fname)

       (let ((pos (point-max)))
	 ;;(add-to-list 'randomsig-buffer-file-pos-list (cons fname pos) t)
					; this does not work with XEmacs
	 (goto-char pos)
	 (insert-file-contents (expand-file-name fname randomsig-dir))
	 ;; No delimiter at the beginning? Insert one.
	 (unless (string-match randomsig-delimiter-pattern
			       (buffer-substring (goto-char pos)
						 (line-end-position)))
	   (goto-char pos)
	   (insert randomsig-delimiter)
	   (insert "\n")
	   ;; Correct position...
	   (setq pos (+ pos (length randomsig-delimiter) 1)))

	 (setq randomsig-buffer-file-pos-list
	       (append randomsig-buffer-file-pos-list
		       (list (cons fname pos))))
	 (goto-char (point-max))
 	 (unless (and (char-before)
 		      (char-equal (char-before) ?\n)) ; Newline?
 	   (insert "\n"))))
     sigfiles)
    (set-buffer-modified-p nil)
    (setq buffer-read-only t)
    (current-buffer)))



(defun randomsig-insert-signature (sig)
  ;; Insert SIG as signature in current buffer
  (save-excursion
    (goto-char (point-max))
    (insert "\n-- \n" sig)))



(defun randomsig-goto-signature ()
;; This function is stolen fom message-goto signature.
;; Go to beginnig of the signature, and return t.
;; If there is no signature in current buffer, go to end of buffer,
;; and return nil.
  (goto-char (point-min))
  (if (re-search-forward "^-- $" nil t)
      (progn
	(forward-line 1)
	t)
    (progn
      (goto-char (point-max))
      nil)))



(defun randomsig-replace-signature (sig)
  ;; Replace the current signature with SIG
  (save-excursion
    (when (randomsig-goto-signature)
      (forward-line -1)
      (backward-char)
      (delete-region (point) (point-max)))

    (randomsig-insert-signature sig)))


(defun randomsig-signature (&optional files)
  "Return a randomly choosen signature.
If FILES is non-nil, a signature out of FILES will be choosen.
Else a signature out of `randomsig-files' will be choosen."
  (save-excursion

    (randomsig-read-signatures-to-buffer randomsig-buffer-name files)

    (goto-char (point-min))
    (let '(count 0) 'selected

	 ;; Count number of signatures
	 (while (search-forward-regexp randomsig-delimiter-pattern nil t)
	   (setq count (1+ count)))

	 ;; Select random signature out out these
	 (setq selected (1+ (random count)))
	 (goto-char (point-min))
	 (if (search-forward-regexp randomsig-delimiter-pattern nil t selected)
	     (forward-char))

	 ;; Cut signature and return it
	 (let '(here (point)) 'signature-string

	      (if (not (search-forward-regexp randomsig-delimiter-pattern
					      nil t))
		  (goto-char (point-max))
		(beginning-of-line))
	      (setq signature-string
		    (concat randomsig-static-string
			    (buffer-substring here (point))))
	      (kill-buffer randomsig-buffer-name)
	      signature-string))))


(defun randomsig-replace-sig (arg)
  "Replace the actual signature with a new one.
When called with prefix, read the filename of the signature-file
that should be used"
  (interactive "P")
  (save-excursion

    (randomsig-replace-signature
     (randomsig-signature
      (if arg
	  (randomsig-prompt "read from signature-lib: ")
	randomsig-files)))))



(defun randomsig-message-read-sig (arg)
  "Get the signature of current message and copy it to a file.
If mark is active, get the marked region instead.
When called with prefix, let the user edit the signature before saving"
  (interactive "P")
  (save-excursion
    (let '(signature-string
	   (if (randomsig-mark-active-p)

	       (buffer-substring (point) (mark))

	     (progn
	       (if (randomsig-goto-signature)
		   (let `(here (point))
		     (goto-char (point-max))
		     (while (char-equal (char-before) 10)
		       (backward-char))
		     (buffer-substring here (point)))
		 nil))))
      (when signature-string
	(if arg
	    (progn
	      ;; make sure this is nil...
	      (setq randomsig-select-edit-bufferpos nil)
	      (randomsig-edit signature-string))
	  (randomsig-write-signature signature-string))))))


(defun randomsig-write-signature (signature-string)
  (set-buffer (find-file-noselect
	       (expand-file-name
		(randomsig-prompt "Write to signature-lib: ")
		randomsig-dir)))

  (goto-char (point-max))
  (insert (concat randomsig-delimiter "\n"))
  (insert signature-string)
  (insert "\n")
  (save-buffer))


(defun gnus/randomsig-summary-read-sig (arg)
  "Get the signature of current message and copy it to a file"
  (interactive "P")
  (progn ;save-excursion
    ;; FIXME: Doesn't return to summary buffer (save-excursion should do this)
    (gnus-summary-select-article-buffer)
    (randomsig-message-read-sig arg)))


(defun randomsig-search-sigfiles (&optional file)
  "Scan `randomsig-dir' and its subdirectories for regular files.
If FILE is given, only FILE and its subdirectory will be searched."
  (unless (file-exists-p randomsig-dir)
    (error "\"%s\" does not exist" randomsig-dir))
  (unless (file-directory-p randomsig-dir)
    (error "\"%s\" is not a directory" randomsig-dir))
  (unless file
    (setq file ""))

  (if (or (string-match "\\(/\\|^\\)\\(\\.\\|\\.\\.\\)$" file)
	  (string-match randomsig-search-unwanted file))
      ;; unwanted...
      nil

    (let '(path (expand-file-name file randomsig-dir))
      (if (file-directory-p path)
	  (mapcan (lambda (f)
		    (randomsig-search-sigfiles (if (string= file "")
						   f
						 (concat file "/" f))))
		  (directory-files path))
	(if (file-regular-p path)
	    (list file)
	  nil)))))


;;; Commands/Function for randomsig-edit-mode

(defun randomsig-edit (signature)
  (if (get-buffer randomsig-edit-buffer-name)
      (kill-buffer randomsig-edit-buffer-name))
  (switch-to-buffer (get-buffer-create randomsig-edit-buffer-name))
  (insert signature)
  (goto-char (point-min))
  (set-buffer-modified-p t)
  (setq buffer-read-only nil)
  (randomsig-edit-mode))



(defun randomsig-replace-signature-in-signature-files (signature)
  (if (not randomsig-select-edit-bufferpos)
      (error "Not in select buffer previously"))
  (set-buffer randomsig-buffer-name)
  (let* ((fname (randomsig-buffer-which-file))
	 (sig_end
	  ;; point in selection buffer, where signature ends
	  (progn
	    (if (search-forward-regexp randomsig-delimiter-pattern nil t)
		(search-backward-regexp randomsig-delimiter-pattern nil nil))
	    (point)))
	 (sig_start
	  ;; point in selection buffer, where signature starts
	  (progn
	    (if (search-backward-regexp randomsig-delimiter-pattern nil t)
		(progn
		  (search-forward-regexp randomsig-delimiter-pattern nil nil)
		  (forward-char)))
	    (point)))
	 (f_start
	  ;; point in selection buffer, where signature file starts
	  (- (cdr (assoc fname randomsig-buffer-file-pos-list))
	     (point-min)))
	 ;; point in file, where Signature starts/ends
	 (f_sig_start (- sig_start f_start))
	 (f_sig_end (- sig_end f_start))
	 ;; old signature
	 (old_sig (randomsig-signature-at-point)))
    (set-buffer (find-file-noselect (expand-file-name fname randomsig-dir)))

    (if (not (string= old_sig (buffer-substring f_sig_start f_sig_end)))
	(error "Signature file has changed"))
    (delete-region f_sig_start f_sig_end)
    (goto-char f_sig_start)
    (insert signature)
    (save-buffer))
  (randomsig-select-reload))


(defun randomsig-edit-done ()
  (interactive)
  (let ((signature-string (buffer-string))
	(edit-buffer (current-buffer)))
    (if randomsig-select-edit-bufferpos
	(randomsig-replace-signature-in-signature-files signature-string)
      (randomsig-write-signature signature-string))
    (kill-buffer edit-buffer)))


(define-derived-mode randomsig-edit-mode text-mode
  "Randomsig Edit"
  "A major mode for editing signatures.
You most likely do not want to call `randomsig-edit-mode' directly.

\\{randomsig-edit-mode-map}"
  (define-key randomsig-edit-mode-map
    (kbd "C-c C-c") 'randomsig-edit-done))


;;; Commands for randomsig-select-mode

(defun randomsig-select-next ()
  "Goto next signature."
  (interactive)
  (if (search-forward-regexp randomsig-delimiter-pattern nil t)
      (forward-char)))


(defun randomsig-select-prev ()
  "Goto next signature."
  (interactive)
  (if (search-backward-regexp randomsig-delimiter-pattern nil t 2)
      (forward-line)))


(defun randomsig-signature-at-point()
  ;; Return the signature at current cursor position
  (save-excursion
    (if (search-backward-regexp randomsig-delimiter-pattern nil t)
	(forward-line))
    (let ((beginning (point)))
      (if (search-backward-regexp randomsig-delimiter-pattern nil t)
	  (forward-line))
      (if (not (search-forward-regexp randomsig-delimiter-pattern
				      nil t))
	  (goto-char (point-max))
	(beginning-of-line))
      (buffer-substring beginning (point)))))


(defun randomsig-select-replace ()
  "Replace the signature in `randomsig-select-original-buffer'
with the signature at the current position, and quit selection."
  (interactive)
  (let ((sig (randomsig-signature-at-point)))
    (kill-buffer randomsig-buffer-name)
    (switch-to-buffer randomsig-select-original-buffer)
    (randomsig-replace-signature (concat randomsig-static-string sig))
    (goto-char randomsig-select-original-position)))


(defun randomsig-select-quit ()
  "Quit the signature-buffer without selection of a signature."
  (interactive)
  (kill-buffer randomsig-buffer-name))


(defun randomsig-select-abort ()
  "Abort the selection from signature-buffer."
  (interactive)
  (ding)
  (kill-buffer randomsig-buffer-name))


(defun randomsig-select-reload ()
  "Reload the current randomsig-buffer"
  (interactive)
  (set-buffer randomsig-buffer-name)
  (let ((pos (point)))
    (randomsig-read-signatures-to-buffer randomsig-buffer-name
					 randomsig-loaded-files)
    (goto-char pos)))


(defun randomsig-select-edit ()
  "Edit the signature at point"
  (interactive)
  (setq randomsig-select-edit-bufferpos (point))
  (randomsig-edit (randomsig-signature-at-point)))


(defun randomsig-buffer-which-file ()
  (let ((p 0)
	(fname "")
	(l randomsig-buffer-file-pos-list))
    (while (progn
	     (setq fname (car (car l)))
	     (setq l (cdr l))
	     (setq p (cdr (car l)))
	     (and l (<= p (point)))))
    fname))


(define-derived-mode randomsig-select-mode fundamental-mode
  "Randomsig Select"
  "A major mode for selecting signatures.
You most likely do not want to call `randomsig-select-mode' directly; use
`randomsig-select-sig' instead.

\\{randomsig-select-mode-map}"

  (define-key randomsig-select-mode-map (kbd "n") 'randomsig-select-next)
  (define-key randomsig-select-mode-map (kbd "p") 'randomsig-select-prev)
  (define-key randomsig-select-mode-map (kbd "?") 'describe-mode)
  (define-key randomsig-select-mode-map (kbd "h") 'describe-mode)
  (define-key randomsig-select-mode-map (kbd "RET") 'randomsig-select-replace)
  (define-key randomsig-select-mode-map (kbd "R") 'randomsig-select-reload)
  (define-key randomsig-select-mode-map (kbd "e") 'randomsig-select-edit)
  (define-key randomsig-select-mode-map (kbd "q") 'randomsig-select-quit)
  (define-key randomsig-select-mode-map (kbd "C-g") 'randomsig-select-abort)

  ;; Experimental: show the file
  ;; FIXME: this does only work for Gnu Emacs 21
  (and (not (boundp 'xemacs-codename))
       (>= emacs-major-version 21)
       (setq mode-line-buffer-identification
	     '(:eval (format "%-12s"
			     (concat "["
				     (randomsig-buffer-which-file)
				     "]"))))))

(defun randomsig-select-sig (arg)
  "Select a new signature from a list.
If called with prefix argument, read the filename of the signature-file
that should be used."
  (interactive "P")

  (setq randomsig-select-original-buffer (current-buffer))
  (setq randomsig-select-original-position (point))


  (switch-to-buffer
   (randomsig-read-signatures-to-buffer
    randomsig-buffer-name
    (if arg
	(randomsig-prompt "read from signature-lib: ")
      randomsig-files)))
  (goto-char 0)
  (forward-line)
  (randomsig-select-mode))



(provide 'randomsig)


;;; randomsig.el ends here