;;; pic-a-sig ;;; Copyright 2003 by Adrian Hosey ;;; For updates check http://web.warhound.org/ (add-hook 'write-file-hooks 'select-sig) (defun select-sig (&optional sigbuf) "Find magic sig string and replace it with a random line" (save-excursion (save-restriction (save-match-data (widen) (goto-char (point-min)) (if (re-search-forward "^$$Sig$$$" nil t) ; Grrr... this replace-match should work but doesn't, so ; we have to use the let clause. Why doesn't it work? ;(replace-match (pic-a-sig sigbuf) t t nil))))) (let ((start (- (point) 7))) (delete-region start (point)) (goto-char start) (if sigbuf (insert (pic-a-sig sigbuf)) (insert (pic-a-sig (make-sigbuf)))) )))))) (defun select-sig-at-point (&optional sigbuf) "Insert a random line at the point" (interactive) (if sigbuf (insert (pic-a-sig sigbuf)) (insert (pic-a-sig (make-sigbuf)))) ) (defun pic-a-sig (sigbuf) "Pick a sig line from the supplied buffer." (progn (switch-to-buffer sigbuf) (let ((max-line-count (count-lines 1 (point-max)))) (random t) (goto-line (random (+ max-line-count 1))) (let ((sigline (thing-at-point 'line))) (while (string-match "\C-m" sigline) (aset sigline (match-beginning 0) ?\n)) (kill-buffer nil) (substring sigline 0 (- (length sigline) 1)) )))) (defun make-sigbuf () "Fill a new buffer with the contents of the .pic-a-sig file" (let ((sigbuf (generate-new-buffer (generate-new-buffer-name "sigtmp")))) (save-excursion (switch-to-buffer sigbuf) (insert-file-contents-literally "~/.pic-a-sig")) ; If I've got save-excursion up there why do I need switch-to-buffer here? (switch-to-buffer (other-buffer)) sigbuf)) (defun sig-cheat () "Insert a sigline matching the given pattern" (interactive) (let ((sigbuf (make-sigbuf)) (pattern (read-string "Pattern? "))) (save-excursion (save-restriction (save-match-data (switch-to-buffer sigbuf) (widen) (goto-char (point-min)) (let ((start (point))) (while (re-search-forward pattern nil t) (beginning-of-line) (delete-region start (point)) (end-of-line) (setq start (+ 1 (point)))) (goto-char (point-max)) (delete-region start (point)))))) (select-sig sigbuf)))