Initial revision
[bpt/emacs.git] / lisp / gnus / score-mode.el
CommitLineData
eec82323
LMI
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@ifi.uio.no>
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(require 'timezone)
30(eval-when-compile (require 'cl))
31
32(defvar gnus-score-mode-hook nil
33 "*Hook run in score mode buffers.")
34
35(defvar gnus-score-menu-hook nil
36 "*Hook run after creating the score mode menu.")
37
38(defvar gnus-score-edit-exit-function nil
39 "Function run on exit from the score buffer.")
40
41(defvar gnus-score-mode-map nil)
42(unless gnus-score-mode-map
43 (setq gnus-score-mode-map (copy-keymap 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;;;###autoload
49(defun gnus-score-mode ()
50 "Mode for editing Gnus score files.
51This mode is an extended emacs-lisp mode.
52
53\\{gnus-score-mode-map}"
54 (interactive)
55 (kill-all-local-variables)
56 (use-local-map gnus-score-mode-map)
57 (gnus-score-make-menu-bar)
58 (set-syntax-table emacs-lisp-mode-syntax-table)
59 (setq major-mode 'gnus-score-mode)
60 (setq mode-name "Score")
61 (lisp-mode-variables nil)
62 (make-local-variable 'gnus-score-edit-exit-function)
63 (run-hooks 'emacs-lisp-mode-hook 'gnus-score-mode-hook))
64
65(defun gnus-score-make-menu-bar ()
66 (unless (boundp 'gnus-score-menu)
67 (easy-menu-define
68 gnus-score-menu gnus-score-mode-map ""
69 '("Score"
70 ["Exit" gnus-score-edit-exit t]
71 ["Insert date" gnus-score-edit-insert-date t]
72 ["Format" gnus-score-pretty-print t]))
73 (run-hooks 'gnus-score-menu-hook)))
74
75(defun gnus-score-edit-insert-date ()
76 "Insert date in numerical format."
77 (interactive)
78 (princ (gnus-score-day-number (current-time)) (current-buffer)))
79
80(defun gnus-score-pretty-print ()
81 "Format the current score file."
82 (interactive)
83 (goto-char (point-min))
84 (let ((form (read (current-buffer))))
85 (erase-buffer)
86 (pp form (current-buffer)))
87 (goto-char (point-min)))
88
89(defun gnus-score-edit-exit ()
90 "Stop editing the score file."
91 (interactive)
92 (unless (file-exists-p (file-name-directory (buffer-file-name)))
93 (make-directory (file-name-directory (buffer-file-name)) t))
94 (save-buffer)
95 (bury-buffer (current-buffer))
96 (let ((buf (current-buffer)))
97 (when gnus-score-edit-exit-function
98 (funcall gnus-score-edit-exit-function))
99 (when (eq buf (current-buffer))
100 (switch-to-buffer (other-buffer (current-buffer))))))
101
102(defun gnus-score-day-number (time)
103 (let ((dat (decode-time time)))
104 (timezone-absolute-from-gregorian
105 (nth 4 dat) (nth 3 dat) (nth 5 dat))))
106
107(provide 'score-mode)
108
109;;; score-mode.el ends here