1 ;;; score-mode.el --- mode for editing Gnus score files
3 ;; Copyright (C) 1996, 2001-2017 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news, mail
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
27 (eval-when-compile (require 'cl
))
28 (require 'mm-util
) ; for mm-universal-coding-system
29 (require 'gnus-util
) ; for gnus-pp, gnus-run-mode-hooks
31 (defcustom gnus-score-edit-done-hook nil
32 "Hook run at the end of closing the score buffer."
36 (defcustom gnus-score-mode-hook nil
37 "Hook run in score mode buffers."
41 (defcustom gnus-score-menu-hook nil
42 "Hook run after creating the score mode menu."
46 (defvar gnus-score-edit-exit-function nil
47 "Function run on exit from the score buffer.")
49 (defvar gnus-score-mode-map
50 (let ((map (make-sparse-keymap)))
51 (set-keymap-parent map emacs-lisp-mode-map
)
52 (define-key map
"\C-c\C-c" 'gnus-score-edit-exit
)
53 (define-key map
"\C-c\C-d" 'gnus-score-edit-insert-date
)
54 (define-key map
"\C-c\C-p" 'gnus-score-pretty-print
)
57 (defvar score-mode-syntax-table
58 (let ((table (copy-syntax-table lisp-mode-syntax-table
)))
59 (modify-syntax-entry ?|
"w" table
)
61 "Syntax table used in score-mode buffers.")
63 ;; We need this to cope with non-ASCII scoring.
64 (defvar score-mode-coding-system mm-universal-coding-system
)
67 (define-derived-mode gnus-score-mode emacs-lisp-mode
"Score"
68 "Mode for editing Gnus score files.
69 This mode is an extended emacs-lisp mode.
71 \\{gnus-score-mode-map}"
72 (gnus-score-make-menu-bar)
73 (make-local-variable 'gnus-score-edit-exit-function
))
75 (defun gnus-score-make-menu-bar ()
76 (unless (boundp 'gnus-score-menu
)
78 gnus-score-menu gnus-score-mode-map
""
80 ["Exit" gnus-score-edit-exit t
]
81 ["Insert date" gnus-score-edit-insert-date t
]
82 ["Format" gnus-score-pretty-print t
]))
83 (run-hooks 'gnus-score-menu-hook
)))
85 (defun gnus-score-edit-insert-date ()
86 "Insert date in numerical format."
88 (princ (time-to-days (current-time)) (current-buffer)))
90 (defun gnus-score-pretty-print ()
91 "Format the current score file."
93 (goto-char (point-min))
94 (let ((form (read (current-buffer))))
96 (let ((emacs-lisp-mode-syntax-table score-mode-syntax-table
))
98 (goto-char (point-min)))
100 (defun gnus-score-edit-exit ()
101 "Stop editing the score file."
103 (unless (file-exists-p (file-name-directory (buffer-file-name)))
104 (make-directory (file-name-directory (buffer-file-name)) t
))
105 (let ((coding-system-for-write score-mode-coding-system
))
107 (bury-buffer (current-buffer))
108 (let ((buf (current-buffer)))
109 (when gnus-score-edit-exit-function
110 (funcall gnus-score-edit-exit-function
))
111 (when (eq buf
(current-buffer))
112 (switch-to-buffer (other-buffer (current-buffer))))))
114 (provide 'score-mode
)
116 ;;; score-mode.el ends here