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