Importing pgnus-0.79
[elisp/gnus.git-] / lisp / score-mode.el
1 ;;; score-mode.el --- mode for editing Gnus score files
2 ;; Copyright (C) 1996 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Keywords: news, mail
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (require 'easymenu)
29 (eval-when-compile (require 'cl))
30
31 (defvar gnus-score-mode-hook nil
32   "*Hook run in score mode buffers.")
33
34 (defvar gnus-score-menu-hook nil
35   "*Hook run after creating the score mode menu.")
36
37 (defvar gnus-score-edit-exit-function nil
38   "Function run on exit from the score buffer.")
39
40 (defvar gnus-score-mode-map nil)
41 (unless gnus-score-mode-map
42   (setq gnus-score-mode-map (make-sparse-keymap))
43   (set-keymap-parent gnus-score-mode-map emacs-lisp-mode-map)
44   (define-key gnus-score-mode-map "\C-c\C-c" 'gnus-score-edit-exit)
45   (define-key gnus-score-mode-map "\C-c\C-d" 'gnus-score-edit-insert-date)
46   (define-key gnus-score-mode-map "\C-c\C-p" 'gnus-score-pretty-print))
47
48 (defvar score-mode-syntax-table
49   (let ((table (copy-syntax-table lisp-mode-syntax-table)))
50     (modify-syntax-entry ?| "w" table)
51     table)
52   "Syntax table used in score-mode buffers.")
53
54 (defvar score-mode-coding-system 'binary)
55
56 ;;;###autoload
57 (defun gnus-score-mode ()
58   "Mode for editing Gnus score files.
59 This mode is an extended emacs-lisp mode.
60
61 \\{gnus-score-mode-map}"
62   (interactive)
63   (kill-all-local-variables)
64   (use-local-map gnus-score-mode-map)
65   (gnus-score-make-menu-bar)
66   (set-syntax-table score-mode-syntax-table)
67   (setq major-mode 'gnus-score-mode)
68   (setq mode-name "Score")
69   (lisp-mode-variables nil)
70   (make-local-variable 'gnus-score-edit-exit-function)
71   (run-hooks 'emacs-lisp-mode-hook 'gnus-score-mode-hook))
72
73 (defun gnus-score-make-menu-bar ()
74   (unless (boundp 'gnus-score-menu)
75     (easy-menu-define
76      gnus-score-menu gnus-score-mode-map ""
77      '("Score"
78        ["Exit" gnus-score-edit-exit t]
79        ["Insert date" gnus-score-edit-insert-date t]
80        ["Format" gnus-score-pretty-print t]))
81     (run-hooks 'gnus-score-menu-hook)))
82
83 (defun gnus-score-edit-insert-date ()
84   "Insert date in numerical format."
85   (interactive)
86   (princ (time-to-days (current-time)) (current-buffer)))
87
88 (defun gnus-score-pretty-print ()
89   "Format the current score file."
90   (interactive)
91   (goto-char (point-min))
92   (let ((form (read (current-buffer))))
93     (erase-buffer)
94     (let ((emacs-lisp-mode-syntax-table score-mode-syntax-table))
95       (pp form (current-buffer))))
96   (goto-char (point-min)))
97
98 (defun gnus-score-edit-exit ()
99   "Stop editing the score file."
100   (interactive)
101   (unless (file-exists-p (file-name-directory (buffer-file-name)))
102     (make-directory (file-name-directory (buffer-file-name)) t))
103   (let ((coding-system-for-write score-mode-coding-system))
104     (save-buffer))
105   (bury-buffer (current-buffer))
106   (let ((buf (current-buffer)))
107     (when gnus-score-edit-exit-function
108       (funcall gnus-score-edit-exit-function))
109     (when (eq buf (current-buffer))
110       (switch-to-buffer (other-buffer (current-buffer))))))
111
112 (provide 'score-mode)
113
114 ;;; score-mode.el ends here