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