score-mode.el 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. ;;; score-mode.el --- mode for editing Gnus score files
  2. ;; Copyright (C) 1996, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
  4. ;; Keywords: news, mail
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;; Code:
  18. (eval-when-compile (require 'cl))
  19. (require 'mm-util) ; for mm-universal-coding-system
  20. (require 'gnus-util) ; for gnus-pp, gnus-run-mode-hooks
  21. (defvar gnus-score-edit-done-hook nil
  22. "*Hook run at the end of closing the score buffer.")
  23. (defvar gnus-score-mode-hook nil
  24. "*Hook run in score mode buffers.")
  25. (defvar gnus-score-menu-hook nil
  26. "*Hook run after creating the score mode menu.")
  27. (defvar gnus-score-edit-exit-function nil
  28. "Function run on exit from the score buffer.")
  29. (defvar gnus-score-mode-map nil)
  30. (unless gnus-score-mode-map
  31. (setq gnus-score-mode-map (make-sparse-keymap))
  32. (set-keymap-parent gnus-score-mode-map emacs-lisp-mode-map)
  33. (define-key gnus-score-mode-map "\C-c\C-c" 'gnus-score-edit-exit)
  34. (define-key gnus-score-mode-map "\C-c\C-d" 'gnus-score-edit-insert-date)
  35. (define-key gnus-score-mode-map "\C-c\C-p" 'gnus-score-pretty-print))
  36. (defvar score-mode-syntax-table
  37. (let ((table (copy-syntax-table lisp-mode-syntax-table)))
  38. (modify-syntax-entry ?| "w" table)
  39. table)
  40. "Syntax table used in score-mode buffers.")
  41. ;; We need this to cope with non-ASCII scoring.
  42. (defvar score-mode-coding-system mm-universal-coding-system)
  43. ;;;###autoload
  44. (defun gnus-score-mode ()
  45. "Mode for editing Gnus score files.
  46. This mode is an extended emacs-lisp mode.
  47. \\{gnus-score-mode-map}"
  48. (interactive)
  49. (kill-all-local-variables)
  50. (use-local-map gnus-score-mode-map)
  51. (gnus-score-make-menu-bar)
  52. (set-syntax-table score-mode-syntax-table)
  53. (setq major-mode 'gnus-score-mode)
  54. (setq mode-name "Score")
  55. (lisp-mode-variables nil)
  56. (make-local-variable 'gnus-score-edit-exit-function)
  57. (gnus-run-mode-hooks 'emacs-lisp-mode-hook 'gnus-score-mode-hook))
  58. (defun gnus-score-make-menu-bar ()
  59. (unless (boundp 'gnus-score-menu)
  60. (easy-menu-define
  61. gnus-score-menu gnus-score-mode-map ""
  62. '("Score"
  63. ["Exit" gnus-score-edit-exit t]
  64. ["Insert date" gnus-score-edit-insert-date t]
  65. ["Format" gnus-score-pretty-print t]))
  66. (run-hooks 'gnus-score-menu-hook)))
  67. (defun gnus-score-edit-insert-date ()
  68. "Insert date in numerical format."
  69. (interactive)
  70. (princ (time-to-days (current-time)) (current-buffer)))
  71. (defun gnus-score-pretty-print ()
  72. "Format the current score file."
  73. (interactive)
  74. (goto-char (point-min))
  75. (let ((form (read (current-buffer))))
  76. (erase-buffer)
  77. (let ((emacs-lisp-mode-syntax-table score-mode-syntax-table))
  78. (gnus-pp form)))
  79. (goto-char (point-min)))
  80. (defun gnus-score-edit-exit ()
  81. "Stop editing the score file."
  82. (interactive)
  83. (unless (file-exists-p (file-name-directory (buffer-file-name)))
  84. (make-directory (file-name-directory (buffer-file-name)) t))
  85. (let ((coding-system-for-write score-mode-coding-system))
  86. (save-buffer))
  87. (bury-buffer (current-buffer))
  88. (let ((buf (current-buffer)))
  89. (when gnus-score-edit-exit-function
  90. (funcall gnus-score-edit-exit-function))
  91. (when (eq buf (current-buffer))
  92. (switch-to-buffer (other-buffer (current-buffer))))))
  93. (provide 'score-mode)
  94. ;;; score-mode.el ends here