Release T-gnus 6.13.0.
[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
30   (require 'cl)
31   (require 'static))
32
33 (defvar gnus-score-mode-hook nil
34   "*Hook run in score mode buffers.")
35
36 (defvar gnus-score-menu-hook nil
37   "*Hook run after creating the score mode menu.")
38
39 (defvar gnus-score-edit-exit-function nil
40   "Function run on exit from the score buffer.")
41
42 (defvar gnus-score-mode-map nil)
43 (unless gnus-score-mode-map
44   (setq gnus-score-mode-map (make-sparse-keymap))
45   (set-keymap-parent gnus-score-mode-map emacs-lisp-mode-map)
46   (define-key gnus-score-mode-map "\C-c\C-c" 'gnus-score-edit-exit)
47   (define-key gnus-score-mode-map "\C-c\C-d" 'gnus-score-edit-insert-date)
48   (define-key gnus-score-mode-map "\C-c\C-p" 'gnus-score-pretty-print))
49
50 (defvar score-mode-syntax-table
51   (let ((table (copy-syntax-table lisp-mode-syntax-table)))
52     (modify-syntax-entry ?| "w" table)
53     table)
54   "Syntax table used in score-mode buffers.")
55
56 (defvar score-mode-coding-system (static-if (boundp 'MULE)
57                                      '*ctext*
58                                    'ctext))
59
60 ;;;###autoload
61 (defun gnus-score-mode ()
62   "Mode for editing Gnus score files.
63 This mode is an extended emacs-lisp mode.
64
65 \\{gnus-score-mode-map}"
66   (interactive)
67   (kill-all-local-variables)
68   (use-local-map gnus-score-mode-map)
69   (gnus-score-make-menu-bar)
70   (set-syntax-table score-mode-syntax-table)
71   (setq major-mode 'gnus-score-mode)
72   (setq mode-name "Score")
73   (lisp-mode-variables nil)
74   (make-local-variable 'gnus-score-edit-exit-function)
75   (run-hooks 'emacs-lisp-mode-hook 'gnus-score-mode-hook))
76
77 (defun gnus-score-make-menu-bar ()
78   (unless (boundp 'gnus-score-menu)
79     (easy-menu-define
80      gnus-score-menu gnus-score-mode-map ""
81      '("Score"
82        ["Exit" gnus-score-edit-exit t]
83        ["Insert date" gnus-score-edit-insert-date t]
84        ["Format" gnus-score-pretty-print t]))
85     (run-hooks 'gnus-score-menu-hook)))
86
87 (defun gnus-score-edit-insert-date ()
88   "Insert date in numerical format."
89   (interactive)
90   (princ (time-to-days (current-time)) (current-buffer)))
91
92 (defun gnus-score-pretty-print ()
93   "Format the current score file."
94   (interactive)
95   (goto-char (point-min))
96   (let ((form (read (current-buffer))))
97     (erase-buffer)
98     (let ((emacs-lisp-mode-syntax-table score-mode-syntax-table))
99       (pp form (current-buffer))))
100   (goto-char (point-min)))
101
102 (defun gnus-score-edit-exit ()
103   "Stop editing the score file."
104   (interactive)
105   (unless (file-exists-p (file-name-directory (buffer-file-name)))
106     (make-directory (file-name-directory (buffer-file-name)) t))
107   (save-buffer-as-coding-system score-mode-coding-system)
108   (bury-buffer (current-buffer))
109   (let ((buf (current-buffer)))
110     (when gnus-score-edit-exit-function
111       (funcall gnus-score-edit-exit-function))
112     (when (eq buf (current-buffer))
113       (switch-to-buffer (other-buffer (current-buffer))))))
114
115 (provide 'score-mode)
116
117 ;;; score-mode.el ends here