options.el 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. ;;; options.el --- edit Options command for Emacs
  2. ;; Copyright (C) 1985, 2001-2012 Free Software Foundation, Inc.
  3. ;; Maintainer: FSF
  4. ;; Obsolete-since: 22.1
  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. ;; This code provides functions to list and edit the values of all global
  18. ;; option variables known to loaded Emacs Lisp code. There are two entry
  19. ;; points, `list-options' and `edit' options'. The latter enters a major
  20. ;; mode specifically for editing option values. Do `M-x describe-mode' in
  21. ;; that context for more details.
  22. ;; The customization buffer feature is intended to make this obsolete.
  23. ;;; Code:
  24. ;;;###autoload
  25. (defun list-options ()
  26. "Display a list of Emacs user options, with values and documentation.
  27. It is now better to use Customize instead."
  28. (interactive)
  29. (with-output-to-temp-buffer "*List Options*"
  30. (let (vars)
  31. (princ "This facility is obsolete; we recommend using M-x customize instead.")
  32. (mapatoms (function (lambda (sym)
  33. (if (user-variable-p sym)
  34. (setq vars (cons sym vars))))))
  35. (setq vars (sort vars 'string-lessp))
  36. (while vars
  37. (let ((sym (car vars)))
  38. (when (boundp sym)
  39. (princ ";; ")
  40. (prin1 sym)
  41. (princ ":\n\t")
  42. (prin1 (symbol-value sym))
  43. (terpri)
  44. (princ (substitute-command-keys
  45. (documentation-property sym 'variable-documentation)))
  46. (princ "\n;;\n"))
  47. (setq vars (cdr vars))))
  48. (with-current-buffer "*List Options*"
  49. (Edit-options-mode)
  50. (setq buffer-read-only t)))))
  51. ;;;###autoload
  52. (defun edit-options ()
  53. "Edit a list of Emacs user option values.
  54. Selects a buffer containing such a list,
  55. in which there are commands to set the option values.
  56. Type \\[describe-mode] in that buffer for a list of commands.
  57. The Custom feature is intended to make this obsolete."
  58. (interactive)
  59. (list-options)
  60. (pop-to-buffer "*List Options*"))
  61. (defvar Edit-options-mode-map
  62. (let ((map (make-keymap)))
  63. (define-key map "s" 'Edit-options-set)
  64. (define-key map "x" 'Edit-options-toggle)
  65. (define-key map "1" 'Edit-options-t)
  66. (define-key map "0" 'Edit-options-nil)
  67. (define-key map "p" 'backward-paragraph)
  68. (define-key map " " 'forward-paragraph)
  69. (define-key map "n" 'forward-paragraph)
  70. map)
  71. "")
  72. ;; Edit Options mode is suitable only for specially formatted data.
  73. (put 'Edit-options-mode 'mode-class 'special)
  74. (defun Edit-options-mode ()
  75. "\\<Edit-options-mode-map>\
  76. Major mode for editing Emacs user option settings.
  77. Special commands are:
  78. \\[Edit-options-set] -- set variable point points at. New value read using minibuffer.
  79. \\[Edit-options-toggle] -- toggle variable, t -> nil, nil -> t.
  80. \\[Edit-options-t] -- set variable to t.
  81. \\[Edit-options-nil] -- set variable to nil.
  82. Changed values made by these commands take effect immediately.
  83. Each variable description is a paragraph.
  84. For convenience, the characters \\[backward-paragraph] and \\[forward-paragraph] move back and forward by paragraphs."
  85. (kill-all-local-variables)
  86. (set-syntax-table emacs-lisp-mode-syntax-table)
  87. (use-local-map Edit-options-mode-map)
  88. (make-local-variable 'paragraph-separate)
  89. (setq paragraph-separate "[^\^@-\^?]")
  90. (make-local-variable 'paragraph-start)
  91. (setq paragraph-start "\t")
  92. (setq truncate-lines t)
  93. (setq major-mode 'Edit-options-mode)
  94. (setq mode-name "Options")
  95. (run-mode-hooks 'Edit-options-mode-hook))
  96. (defun Edit-options-set () (interactive)
  97. (Edit-options-modify
  98. (lambda (var) (eval-minibuffer (concat "New " (symbol-name var) ": ")))))
  99. (defun Edit-options-toggle () (interactive)
  100. (Edit-options-modify (lambda (var) (not (symbol-value var)))))
  101. (defun Edit-options-t () (interactive)
  102. (Edit-options-modify (lambda (var) t)))
  103. (defun Edit-options-nil () (interactive)
  104. (Edit-options-modify (lambda (var) nil)))
  105. (defun Edit-options-modify (modfun)
  106. (save-excursion
  107. (let ((buffer-read-only nil) var pos)
  108. (re-search-backward "^;; \\|\\`")
  109. (forward-char 3)
  110. (setq pos (point))
  111. (save-restriction
  112. (narrow-to-region pos (progn (end-of-line) (1- (point))))
  113. (goto-char pos)
  114. (setq var (read (current-buffer))))
  115. (goto-char pos)
  116. (forward-line 1)
  117. (forward-char 1)
  118. (save-excursion
  119. (set var (funcall modfun var)))
  120. (kill-sexp 1)
  121. (prin1 (symbol-value var) (current-buffer)))))
  122. (provide 'options)
  123. ;;; options.el ends here