Don't require easymenu. Require mm-util.
[gnus] / 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 (eval-when-compile (require 'cl))
29 (require 'mm-util)                      ; for mm-auto-save-coding-system
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 ;; We need this to cope with non-ASCII scoring.
55 (defvar score-mode-coding-system mm-auto-save-coding-system)
56
57 ;;;###autoload
58 (defun gnus-score-mode ()
59   "Mode for editing Gnus score files.
60 This mode is an extended emacs-lisp mode.
61
62 \\{gnus-score-mode-map}"
63   (interactive)
64   (kill-all-local-variables)
65   (use-local-map gnus-score-mode-map)
66   (gnus-score-make-menu-bar)
67   (set-syntax-table score-mode-syntax-table)
68   (setq major-mode 'gnus-score-mode)
69   (setq mode-name "Score")
70   (lisp-mode-variables nil)
71   (make-local-variable 'gnus-score-edit-exit-function)
72   (run-hooks 'emacs-lisp-mode-hook 'gnus-score-mode-hook))
73
74 (defun gnus-score-make-menu-bar ()
75   (unless (boundp 'gnus-score-menu)
76     (easy-menu-define
77      gnus-score-menu gnus-score-mode-map ""
78      '("Score"
79        ["Exit" gnus-score-edit-exit t]
80        ["Insert date" gnus-score-edit-insert-date t]
81        ["Format" gnus-score-pretty-print t]))
82     (run-hooks 'gnus-score-menu-hook)))
83
84 (defun gnus-score-edit-insert-date ()
85   "Insert date in numerical format."
86   (interactive)
87   (princ (time-to-days (current-time)) (current-buffer)))
88
89 (defun gnus-score-pretty-print ()
90   "Format the current score file."
91   (interactive)
92   (goto-char (point-min))
93   (let ((form (read (current-buffer))))
94     (erase-buffer)
95     (let ((emacs-lisp-mode-syntax-table score-mode-syntax-table))
96       (pp form (current-buffer))))
97   (goto-char (point-min)))
98
99 (defun gnus-score-edit-exit ()
100   "Stop editing the score file."
101   (interactive)
102   (unless (file-exists-p (file-name-directory (buffer-file-name)))
103     (make-directory (file-name-directory (buffer-file-name)) t))
104   (let ((coding-system-for-write score-mode-coding-system))
105     (save-buffer))
106   (bury-buffer (current-buffer))
107   (let ((buf (current-buffer)))
108     (when gnus-score-edit-exit-function
109       (funcall gnus-score-edit-exit-function))
110     (when (eq buf (current-buffer))
111       (switch-to-buffer (other-buffer (current-buffer))))))
112
113 (provide 'score-mode)
114
115 ;;; score-mode.el ends here