readd randomsig
[emacs.git] / .emacs.d / elisp / local / randomsig.el
1 ;;; randomsig.el --- insert a randomly selected signature
2
3 ;; Copyright (C) 2001, 2002 Hans-Jürgen Ficker
4
5 ;; Emacs Lisp Archive Entry
6 ;; Author: Hans-Juergen Ficker <hj@backmes.de>
7 ;; Version: 0.7.0
8 ;; X-CVS-Version: $Id: randomsig.el,v 1.35 2003/07/17 14:54:15 hjficker Exp $
9 ;; Keywords: mail random signature
10
11 ;; This file is not currently part of GNU Emacs.
12
13 ;; This program is free software; you can redistribute it and/or
14 ;; modify it under the terms of the GNU General Public License as
15 ;; published by the Free Software Foundation; either version 2, or (at
16 ;; your option) any later version.
17
18 ;; This program is distributed in the hope that it will be useful, but
19 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 ;; General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with this program ; see the file COPYING. If not, write to
25 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
27
28 ;;; Commentary:
29
30 ;; This is yet another implementation to insert a randomly choosen
31 ;; signature into a mail.
32
33 ;; It is only tested with gnus.
34
35 ;; To make it work, put the following lines into your ~/.gnus:
36
37 ;; (require 'randomsig)
38 ;; (define-key message-mode-map (kbd "C-c s") 'randomsig-replace-sig)
39 ;; (define-key message-mode-map (kbd "C-c S") 'randomsig-select-sig)
40 ;; (require 'gnus-sum) ; probably required for `gnus-summary-save-map'
41 ;; (define-key gnus-summary-save-map "-" 'gnus/randomsig-summary-read-sig)
42 ;; (setq randomsig-dir "/some/directory")
43 ;; (setq randomsig-files '("some" "files"))
44 ;; ;; or (setq randomsig-files (randomsig-search-sigfiles))
45 ;; ;; or (setq randomsig-files 'randomsig-search-sigfiles)
46 ;; (setq message-signature 'randomsig-signature)
47
48 ;; This will also define the shortcut `C-c s' in message-mode to
49 ;; change the signature, `C-c S' in message-mode to interactively
50 ;; select the signature to replace the current signature, and `O -' in
51 ;; gnus-summary-mode to read the signature from the selected mail.
52
53 ;; `randomsig-files' must be a list of existing files, an existing
54 ;; file, or a function returning a list of existing files. If these
55 ;; don't have absolute paths, they are located in `randomsig-dir'.
56
57 ;; File format: Each file must contain at least one signature.
58 ;; Signatures are separated with `randomsig-delimiter-pattern'. If
59 ;; there is only one signature in the file, the delimiter can be
60 ;; omitted, so real .signature-files can be used.
61
62 ;; `randomsig-delimiter' is used when inserting new signatures with
63 ;; `randomsig-message-read-sig' into the signature file. So
64 ;; `randomsig-delimiter' should match `randomsig-delimiter-pattern'.
65
66 ;; `randomsig-static-string' is put in front of every random signature
67 ;; if non-`nil'.
68
69 ;; The *-read-sig functions read the signature of a message, or use
70 ;; the marked text, and write it to a signature-file, for which the
71 ;; name is asked. If the file does not exist, it will be generated.
72 ;; When called with any prefix, the signatures will be offered to edit
73 ;; before saving.
74
75 ;; if `randomsig-replace-sig' is called with any prefix, it will ask
76 ;; for a file to get the signature from.
77
78 ;; `randomsig-select-sig' will offer a list of signatures to select
79 ;; from in an extra buffer. n will jump to the next signature, p to
80 ;; the previous, RET will insert the selected signature, q will exit
81 ;; the selection buffer without replacing the current signature, R
82 ;; will reload the signature-files, and e will open a buffer for
83 ;; editing the signature at the point. When called with any prefix, it
84 ;; will ask for a file to get the signatures from
85
86 ;; `randomsig-search-sigfiles' will search for regular files in
87 ;; `randomsig-dir', which do not match `randomsig-search-unwanted'. A
88 ;; subdirectory of `randomsig-dir' can be given as optional argument.
89
90 ;; Completion will only work for files in `randomsig-files', though
91 ;; others files can be used, too.
92
93 ;;; Changelog:
94
95 ;; 2001/04/12 0.1
96 ;; * Initial release
97
98 ;; 2001/04/19 0.2
99 ;; * inserted `randomsig-delimiter' to add the capability to change
100 ;; the delimiter between the signatures (thanks to Andreas Büsching
101 ;; <crunchy@tzi.de>)
102
103 ;; 2001/04/25 0.3
104 ;; * new function `randomsig-search-sigfiles', to search all regular files
105 ;; in directory `randomsig-dir'
106 ;; * normal signatures only worked, when using only one signature. Fixed.
107
108 ;; 2001/04/25 0.3.1
109 ;; * Fixed a bug in `randomsig-search-sigfiles'
110
111 ;; 2001/04/26 0.3.2
112 ;; * replaced `point-at-eol' with `line-end-position' (Don't know where
113 ;; `point-at-eol' is defined)
114 ;; * require cl
115 ;; * require message in some functions
116
117 ;; 2001/07/09 0.3.3
118 ;; * don't (setq message-signature 'randomsig-signature) by default,
119 ;; the user can do this in his .gnus
120 ;; * remove unnecessary optional arguments to `find-file-noselect' to
121 ;; make it work with XEmacs
122 ;; (Thanks to Micha Wiedenmann <Micha.Wiedenmann@gmx.net> for both
123 ;; suggestions)
124 ;; * documentation updates
125
126 ;; 2001/07/12 0.3.4
127 ;; * more fixes for XEmacs
128 ;; * more documentation Updates
129
130 ;; 2001/07/20 0.4.0
131 ;; * new command `randomsig-select-sig' to interactively select a signature
132 ;; * new mode `randomsig-select-mode' (for `randomsig-select-sig')
133 ;; * `randomsig-files' can also be function returning a list of
134 ;; Signature files
135 ;; * `randomsig-replace-sig' does not remove old signature when interrupted
136
137 ;; 2001/07/22 0.4.1
138 ;; * (require 'message) only when needed
139
140 ;; 2001/08/13 0.5.0
141 ;; * doesn't require message anymore, so it should work without gnus
142
143 ;; 2001/08/20 0.5.1
144 ;; * add (random t) to initialize random seed (thanks to Evgeny
145 ;; Roubinchtein <evgenyr@cs.washington.edu> for pointing this out
146 ;; * insert a newline if it is missing at the end of a signature file
147
148 ;; 2001/09/17 0.5.2
149 ;; * new variable `randomsig-static-string' (thanks to Raymond Scholz
150 ;; <rscholz@zonix.de>)
151
152 ;; 2001/10/01 0.5.3
153 ;; * Documentation updates
154
155 ;; 2002/01/20 0.5.99
156 ;; * It is now possible to edit signatures before saving, or to edit
157 ;; single signatures from the selection buffer.
158 ;; * Mark many variables as user option
159 ;; * randomsig-files-to-list works recursive
160
161 ;; 2002/03/04 0.6.0
162 ;; * `randomsig-replace-signature-in-signature-files' should be safer now
163 ;; * `randomsig-files-to-list' did endless recursion when called
164 ;; with nil. Fixed.
165 ;; * Some error-handling for non-existing `randomsig-dir'.
166
167 ;; 2002/09/21 0.7.0
168 ;; * most variables customizable
169 ;; * `randomsig-static-string' works for `randomsig-select-sig', too
170 ;; (thanks to Mark Trettin <mtr-dev0@gmx.de> for pointing this out)
171 ;; * documentation updates
172
173 ;; 2003/07/17 0.7.1
174 ;; * handle errors when reading files
175 ;; * `lambda' functions are allowed, too
176 ;; * add customizations for hooks `randomsig-select-mode-hook' and
177 ;; `randomsig-edit-mode-hook'
178
179
180 (eval-when-compile
181 (require 'cl))
182
183
184 (defconst randomsig-version "0.7.1")
185
186
187 (defvar randomsig-dir "~/.signatures"
188 "*Directory for signature-files. See also `randomsig-files'")
189
190
191 (defgroup randomsig nil
192 "insert a randomly choosen signature into a mail."
193 :group 'mail
194 :group 'news)
195
196 (defcustom randomsig-files '("default")
197 "*Files with random signatures.
198 This variable may be a list of strings, a string, or a function returning a
199 list of strings.
200 The files are searched in `randomsig-dir', if they don't have absolute paths.
201 The signatures have to be separated by lines matching
202 `randomsig-delimiter-pattern' at the beginning."
203 :type '(choice
204 (repeat
205 :tag "List of filenames"
206 (string :tag "filename"))
207 (function
208 :tag "function returning the signature files"
209 :value randomsig-search-sigfiles))
210 :group 'randomsig)
211
212 (defcustom randomsig-delimiter "-- "
213 "*delimiter used when adding new signatures in signature file.
214 You have to change `randomsig-delimiter-pattern', too, if you change this."
215 :type '(string)
216 :group 'randomsig)
217
218
219 (defcustom randomsig-delimiter-pattern
220 (concat "^" (regexp-quote randomsig-delimiter) "$")
221 "*Regular expression that matches the delimiters between signatures.
222 `randomsig-delimiter' must match `randomsig-delimiter-pattern'."
223 :type '(regexp)
224 :group 'randomsig)
225
226
227 (defcustom randomsig-search-unwanted "\\(/\\|^\\)\\(CVS\\|RCS\\|.*~\\)$"
228 "*Regular expression matching unwanted files when scanning with
229 `randomsig-search-sigfiles'"
230 :type '(regexp)
231 :group 'randomsig)
232
233
234 (defcustom randomsig-static-string nil
235 "*Static string to be inserted above every random signature.
236 You probably want to have a newline at the end of it."
237 :type '(choice
238 (const :tag "none" nil)
239 (string))
240 :group 'randomsig)
241
242 (defcustom randomsig-select-mode-hook nil
243 "*Hook run when entering randomsig select mode."
244 :type 'hook
245 :group 'randomsig)
246
247 (defcustom randomsig-edit-mode-hook nil
248 "*Hook run when entering randomsig select mode."
249 :type 'hook
250 :group 'randomsig)
251
252 (defvar randomsig-buffer-name "*Signatures*"
253 "Name for the (temporary) buffer for the signatures")
254
255 (defvar randomsig-edit-buffer-name "*Edit Signature*"
256 "Name for the (temporary) buffer for editing the signatures")
257
258 (defvar randomsig-select-original-buffer nil)
259 (defvar randomsig-select-original-position nil)
260
261 (defvar randomsig-history nil)
262
263 (defvar randomsig-buffer-file-pos-list nil)
264
265 (defvar randomsig-select-edit-bufferpos nil)
266
267 (defvar randomsig-loaded-files nil)
268
269 ;; definitions for XEmacs:
270 (unless (fboundp 'line-end-position)
271 (defalias 'line-end-position 'point-at-eol))
272
273 (defun randomsig-mark-active-p ()
274 (if (fboundp 'region-active-p)
275
276 (region-active-p) ; XEmacs
277
278 mark-active)) ; Gnu Emacs
279
280
281 (require 'cl)
282
283 (random t) ; Initialize random seed
284
285 ;;; Helper Functions
286
287 (defun randomsig-files-to-list (files)
288 ;; return a list of strings
289 (cond ((and (listp files)
290 (eq (car files) 'lambda)) (randomsig-files-to-list (funcall files)))
291 ((listp files) files)
292 ((and (symbolp files)
293 (fboundp files)) (randomsig-files-to-list (funcall files)))
294 ((and (symbolp files)
295 (boundp files)) (randomsig-files-to-list (symbol-value files)))
296 ((stringp files) (list files))
297 (t nil)))
298
299
300 (defun randomsig-prompt (&optional prompt)
301 ;; Prompt for a signature file.
302 (completing-read (if prompt
303 prompt
304 "signature: ")
305 (mapcar
306 (lambda (x) (list x))
307 (randomsig-files-to-list randomsig-files))
308 nil
309 nil
310 nil
311 randomsig-history))
312
313
314
315 (defun randomsig-read-signatures-to-buffer (buffer-name &optional files)
316 ;; read the signatures into the signature buffer
317 ;; save possibly local variables `randomsig-files' and `randomsig-dir'
318 (let ((sigfiles randomsig-files) (sigdir randomsig-dir))
319 (if (get-buffer buffer-name)
320 (progn
321 (set-buffer buffer-name)
322 (setq buffer-read-only nil)
323 (delete-region (point-min) (point-max)))
324 (progn
325 (get-buffer-create buffer-name)
326 (set-buffer buffer-name)))
327 (set (make-local-variable 'randomsig-files) sigfiles)
328 (set (make-local-variable 'randomsig-dir) sigdir))
329
330 (setq randomsig-buffer-file-pos-list nil)
331
332 (unless files
333 (setq files randomsig-files))
334
335 (setq randomsig-loaded-files files)
336
337 ;; get a list with file names of signature files
338 (let ((sigfiles (randomsig-files-to-list files)))
339 ;; Insert all files into the newly created buffer
340 (mapcar
341 (lambda (fname)
342
343 (let ((pos (point-max)))
344 ;;(add-to-list 'randomsig-buffer-file-pos-list (cons fname pos) t)
345 ; this does not work with XEmacs
346 (goto-char pos)
347 (condition-case err
348 (insert-file-contents (expand-file-name fname randomsig-dir))
349 (file-error
350 (message "%s" (error-message-string err))
351 (ding)
352 (sit-for 1.0)))
353
354 ;; No delimiter at the beginning? Insert one.
355 (unless (string-match randomsig-delimiter-pattern
356 (buffer-substring (goto-char pos)
357 (line-end-position)))
358 (goto-char pos)
359 (insert randomsig-delimiter)
360 (insert "\n")
361 ;; Correct position...
362 (setq pos (+ pos (length randomsig-delimiter) 1)))
363
364 (setq randomsig-buffer-file-pos-list
365 (append randomsig-buffer-file-pos-list
366 (list (cons fname pos))))
367 (goto-char (point-max))
368 (unless (and (char-before)
369 (char-equal (char-before) ?\n)) ; Newline?
370 (insert "\n"))))
371 sigfiles)
372 (set-buffer-modified-p nil)
373 (setq buffer-read-only t)
374 (current-buffer)))
375
376
377
378 (defun randomsig-insert-signature (sig)
379 ;; Insert SIG as signature in current buffer
380 (save-excursion
381 (goto-char (point-max))
382 (insert "\n-- \n" sig)))
383
384
385
386 (defun randomsig-goto-signature ()
387 ;; This function is stolen fom message-goto signature.
388 ;; Go to beginnig of the signature, and return t.
389 ;; If there is no signature in current buffer, go to end of buffer,
390 ;; and return nil.
391 (goto-char (point-min))
392 (if (re-search-forward "^-- $" nil t)
393 (progn
394 (forward-line 1)
395 t)
396 (progn
397 (goto-char (point-max))
398 nil)))
399
400
401
402 (defun randomsig-replace-signature (sig)
403 ;; Replace the current signature with SIG
404 (save-excursion
405 (when (randomsig-goto-signature)
406 (forward-line -1)
407 (backward-char)
408 (delete-region (point) (point-max)))
409
410 (randomsig-insert-signature sig)))
411
412
413 (defun randomsig-signature (&optional files)
414 "Return a randomly choosen signature.
415 If FILES is non-nil, a signature out of FILES will be choosen.
416 Else a signature out of `randomsig-files' will be choosen."
417 (save-excursion
418
419 (randomsig-read-signatures-to-buffer randomsig-buffer-name files)
420
421 (goto-char (point-min))
422 (let '(count 0) 'selected
423
424 ;; Count number of signatures
425 (while (search-forward-regexp randomsig-delimiter-pattern nil t)
426 (setq count (1+ count)))
427
428 ;; Select random signature out out these
429 (setq selected (1+ (random count)))
430 (goto-char (point-min))
431 (if (search-forward-regexp randomsig-delimiter-pattern nil t selected)
432 (forward-char))
433
434 ;; Cut signature and return it
435 (let '(here (point)) 'signature-string
436
437 (if (not (search-forward-regexp randomsig-delimiter-pattern
438 nil t))
439 (goto-char (point-max))
440 (beginning-of-line))
441 (setq signature-string
442 (concat randomsig-static-string
443 (buffer-substring here (point))))
444 (kill-buffer randomsig-buffer-name)
445 signature-string))))
446
447
448 (defun randomsig-replace-sig (arg)
449 "Replace the actual signature with a new one.
450 When called with prefix, read the filename of the signature-file
451 that should be used"
452 (interactive "P")
453 (save-excursion
454
455 (randomsig-replace-signature
456 (randomsig-signature
457 (if arg
458 (randomsig-prompt "read from signature-lib: ")
459 randomsig-files)))))
460
461
462
463 (defun randomsig-message-read-sig (arg)
464 "Get the signature of current message and copy it to a file.
465 If mark is active, get the marked region instead.
466 When called with prefix, let the user edit the signature before saving"
467 (interactive "P")
468 (save-excursion
469 (let '(signature-string
470 (if (randomsig-mark-active-p)
471
472 (buffer-substring (point) (mark))
473
474 (progn
475 (if (randomsig-goto-signature)
476 (let `(here (point))
477 (goto-char (point-max))
478 (while (char-equal (char-before) 10)
479 (backward-char))
480 (buffer-substring here (point)))
481 nil))))
482 (when signature-string
483 (if arg
484 (progn
485 ;; make sure this is nil...
486 (setq randomsig-select-edit-bufferpos nil)
487 (randomsig-edit signature-string))
488 (randomsig-write-signature signature-string))))))
489
490
491 (defun randomsig-write-signature (signature-string)
492 (set-buffer (find-file-noselect
493 (expand-file-name
494 (randomsig-prompt "Write to signature-lib: ")
495 randomsig-dir)))
496
497 (goto-char (point-max))
498 (insert (concat randomsig-delimiter "\n"))
499 (insert signature-string)
500 (insert "\n")
501 (save-buffer))
502
503
504 (defun gnus/randomsig-summary-read-sig (arg)
505 "Get the signature of current message and copy it to a file"
506 (interactive "P")
507 (progn ;save-excursion
508 ;; FIXME: Doesn't return to summary buffer (save-excursion should do this)
509 (gnus-summary-select-article-buffer)
510 (randomsig-message-read-sig arg)))
511
512
513 (defun randomsig-search-sigfiles (&optional file)
514 "Scan `randomsig-dir' and its subdirectories for regular files.
515 If FILE is given, only FILE and its subdirectory will be searched."
516 (unless (file-exists-p randomsig-dir)
517 (error "\"%s\" does not exist" randomsig-dir))
518 (unless (file-directory-p randomsig-dir)
519 (error "\"%s\" is not a directory" randomsig-dir))
520 (unless file
521 (setq file ""))
522
523 (if (or (string-match "\\(/\\|^\\)\\(\\.\\|\\.\\.\\)$" file)
524 (string-match randomsig-search-unwanted file))
525 ;; unwanted...
526 nil
527
528 (let '(path (expand-file-name file randomsig-dir))
529 (if (file-directory-p path)
530 (mapcan (lambda (f)
531 (randomsig-search-sigfiles (if (string= file "")
532 f
533 (concat file "/" f))))
534 (directory-files path))
535 (if (file-regular-p path)
536 (list file)
537 nil)))))
538
539
540 ;;; Commands/Function for randomsig-edit-mode
541
542 (defun randomsig-edit (signature)
543 (if (get-buffer randomsig-edit-buffer-name)
544 (kill-buffer randomsig-edit-buffer-name))
545 (switch-to-buffer (get-buffer-create randomsig-edit-buffer-name))
546 (insert signature)
547 (goto-char (point-min))
548 (set-buffer-modified-p t)
549 (setq buffer-read-only nil)
550 (randomsig-edit-mode))
551
552
553
554 (defun randomsig-replace-signature-in-signature-files (signature)
555 (if (not randomsig-select-edit-bufferpos)
556 (error "Not in select buffer previously"))
557 (set-buffer randomsig-buffer-name)
558 (let* ((fname (randomsig-buffer-which-file))
559 (sig_end
560 ;; point in selection buffer, where signature ends
561 (progn
562 (if (search-forward-regexp randomsig-delimiter-pattern nil t)
563 (search-backward-regexp randomsig-delimiter-pattern nil nil))
564 (point)))
565 (sig_start
566 ;; point in selection buffer, where signature starts
567 (progn
568 (if (search-backward-regexp randomsig-delimiter-pattern nil t)
569 (progn
570 (search-forward-regexp randomsig-delimiter-pattern nil nil)
571 (forward-char)))
572 (point)))
573 (f_start
574 ;; point in selection buffer, where signature file starts
575 (- (cdr (assoc fname randomsig-buffer-file-pos-list))
576 (point-min)))
577 ;; point in file, where Signature starts/ends
578 (f_sig_start (- sig_start f_start))
579 (f_sig_end (- sig_end f_start))
580 ;; old signature
581 (old_sig (randomsig-signature-at-point)))
582 (set-buffer (find-file-noselect (expand-file-name fname randomsig-dir)))
583
584 (if (not (string= old_sig (buffer-substring f_sig_start f_sig_end)))
585 (error "Signature file has changed"))
586 (delete-region f_sig_start f_sig_end)
587 (goto-char f_sig_start)
588 (insert signature)
589 (save-buffer))
590 (randomsig-select-reload))
591
592
593 (defun randomsig-edit-done ()
594 (interactive)
595 (let ((signature-string (buffer-string))
596 (edit-buffer (current-buffer)))
597 (if randomsig-select-edit-bufferpos
598 (randomsig-replace-signature-in-signature-files signature-string)
599 (randomsig-write-signature signature-string))
600 (kill-buffer edit-buffer)))
601
602
603 (define-derived-mode randomsig-edit-mode text-mode
604 "Randomsig Edit"
605 "A major mode for editing signatures.
606 You most likely do not want to call `randomsig-edit-mode' directly.
607
608 \\{randomsig-edit-mode-map}"
609 (define-key randomsig-edit-mode-map
610 (kbd "C-c C-c") 'randomsig-edit-done))
611
612
613 ;;; Commands for randomsig-select-mode
614
615 (defun randomsig-select-next ()
616 "Goto next signature."
617 (interactive)
618 (if (search-forward-regexp randomsig-delimiter-pattern nil t)
619 (forward-char)))
620
621
622 (defun randomsig-select-prev ()
623 "Goto next signature."
624 (interactive)
625 (if (search-backward-regexp randomsig-delimiter-pattern nil t 2)
626 (forward-line)))
627
628
629 (defun randomsig-signature-at-point()
630 ;; Return the signature at current cursor position
631 (save-excursion
632 (if (search-backward-regexp randomsig-delimiter-pattern nil t)
633 (forward-line))
634 (let ((beginning (point)))
635 (if (search-backward-regexp randomsig-delimiter-pattern nil t)
636 (forward-line))
637 (if (not (search-forward-regexp randomsig-delimiter-pattern
638 nil t))
639 (goto-char (point-max))
640 (beginning-of-line))
641 (buffer-substring beginning (point)))))
642
643
644 (defun randomsig-select-replace ()
645 "Replace the signature in `randomsig-select-original-buffer'
646 with the signature at the current position, and quit selection."
647 (interactive)
648 (let ((sig (randomsig-signature-at-point)))
649 (kill-buffer randomsig-buffer-name)
650 (switch-to-buffer randomsig-select-original-buffer)
651 (randomsig-replace-signature (concat randomsig-static-string sig))
652 (goto-char randomsig-select-original-position)))
653
654
655 (defun randomsig-select-quit ()
656 "Quit the signature-buffer without selection of a signature."
657 (interactive)
658 (kill-buffer randomsig-buffer-name))
659
660
661 (defun randomsig-select-abort ()
662 "Abort the selection from signature-buffer."
663 (interactive)
664 (ding)
665 (kill-buffer randomsig-buffer-name))
666
667
668 (defun randomsig-select-reload ()
669 "Reload the current randomsig-buffer"
670 (interactive)
671 (set-buffer randomsig-buffer-name)
672 (let ((pos (point)))
673 (randomsig-read-signatures-to-buffer randomsig-buffer-name
674 randomsig-loaded-files)
675 (goto-char pos)))
676
677
678 (defun randomsig-select-edit ()
679 "Edit the signature at point"
680 (interactive)
681 (setq randomsig-select-edit-bufferpos (point))
682 (randomsig-edit (randomsig-signature-at-point)))
683
684
685 (defun randomsig-buffer-which-file ()
686 (let ((p 0)
687 (fname "")
688 (l randomsig-buffer-file-pos-list))
689 (while (progn
690 (setq fname (car (car l)))
691 (setq l (cdr l))
692 (setq p (cdr (car l)))
693 (and l (<= p (point)))))
694 fname))
695
696
697 (define-derived-mode randomsig-select-mode fundamental-mode
698 "Randomsig Select"
699 "A major mode for selecting signatures.
700 You most likely do not want to call `randomsig-select-mode' directly; use
701 `randomsig-select-sig' instead.
702
703 \\{randomsig-select-mode-map}"
704
705 (define-key randomsig-select-mode-map (kbd "n") 'randomsig-select-next)
706 (define-key randomsig-select-mode-map (kbd "p") 'randomsig-select-prev)
707 (define-key randomsig-select-mode-map (kbd "?") 'describe-mode)
708 (define-key randomsig-select-mode-map (kbd "h") 'describe-mode)
709 (define-key randomsig-select-mode-map (kbd "RET") 'randomsig-select-replace)
710 (define-key randomsig-select-mode-map (kbd "R") 'randomsig-select-reload)
711 (define-key randomsig-select-mode-map (kbd "e") 'randomsig-select-edit)
712 (define-key randomsig-select-mode-map (kbd "q") 'randomsig-select-quit)
713 (define-key randomsig-select-mode-map (kbd "C-g") 'randomsig-select-abort)
714
715 ;; Experimental: show the file
716 ;; FIXME: this does only work for Gnu Emacs 21
717 (and (not (boundp 'xemacs-codename))
718 (>= emacs-major-version 21)
719 (setq mode-line-buffer-identification
720 '(:eval (format "%-12s"
721 (concat "["
722 (randomsig-buffer-which-file)
723 "]"))))))
724
725 (defun randomsig-select-sig (arg)
726 "Select a new signature from a list.
727 If called with prefix argument, read the filename of the signature-file
728 that should be used."
729 (interactive "P")
730
731 (setq randomsig-select-original-buffer (current-buffer))
732 (setq randomsig-select-original-position (point))
733
734
735 (switch-to-buffer
736 (randomsig-read-signatures-to-buffer
737 randomsig-buffer-name
738 (if arg
739 (randomsig-prompt "read from signature-lib: ")
740 randomsig-files)))
741 (goto-char 0)
742 (forward-line)
743 (randomsig-select-mode))
744
745
746
747 (provide 'randomsig)
748
749
750 ;;; randomsig.el ends here