dynamic-setting.el 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. ;;; dynamic-setting.el --- Support dynamic changes
  2. ;; Copyright (C) 2009-2012 Free Software Foundation, Inc.
  3. ;; Author: Jan Djärv <jan.h.d@swipnet.se>
  4. ;; Maintainer: FSF
  5. ;; Keywords: font, system-font, tool-bar-style
  6. ;; Package: emacs
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; This file provides the lisp part of the GConf and XSetting code in
  20. ;; xsetting.c. But it is nothing that prevents it from being used by
  21. ;; other configuration schemes.
  22. ;;; Code:
  23. ;;; Customizable variables
  24. (declare-function font-get-system-font "xsettings.c" ())
  25. (defvar font-use-system-font)
  26. (defun font-setting-change-default-font (display-or-frame set-font)
  27. "Change font and/or font settings for frames on display DISPLAY-OR-FRAME.
  28. If DISPLAY-OR-FRAME is a frame, the display is the one for that frame.
  29. If SET-FONT is non-nil, change the font for frames. Otherwise re-apply the
  30. current form for the frame (i.e. hinting or somesuch changed)."
  31. (let ((new-font (and (fboundp 'font-get-system-font)
  32. (font-get-system-font)))
  33. (frame-list (frames-on-display-list display-or-frame)))
  34. (when (and new-font (display-graphic-p display-or-frame))
  35. (clear-font-cache)
  36. (if set-font
  37. ;; Set the font on all current and future frames, as though
  38. ;; the `default' face had been "set for this session":
  39. (set-frame-font new-font nil frame-list)
  40. ;; Just redraw the existing fonts on all frames:
  41. (dolist (f frame-list)
  42. (let ((frame-font
  43. (or (font-get (face-attribute 'default :font f 'default)
  44. :user-spec)
  45. (frame-parameter f 'font-parameter))))
  46. (when frame-font
  47. (set-frame-parameter f 'font-parameter frame-font)
  48. (set-face-attribute 'default f
  49. :width 'normal
  50. :weight 'normal
  51. :slant 'normal
  52. :font frame-font))))))))
  53. (defun dynamic-setting-handle-config-changed-event (event)
  54. "Handle config-changed-event on the display in EVENT.
  55. Changes can be
  56. The monospace font. If `font-use-system-font' is nil, the font
  57. is not changed.
  58. The normal font.
  59. Xft parameters, like DPI and hinting.
  60. The Gtk+ theme name.
  61. The tool bar style."
  62. (interactive "e")
  63. (let ((type (nth 1 event))
  64. (display-name (nth 2 event)))
  65. (cond ((and (eq type 'monospace-font-name) font-use-system-font)
  66. (font-setting-change-default-font display-name t))
  67. ((eq type 'font-render)
  68. (font-setting-change-default-font display-name nil))
  69. ;; This is a bit heavy, ideally we would just clear faces
  70. ;; on the affected display, and perhaps only the relevant
  71. ;; faces. Oh well.
  72. ((eq type 'theme-name) (clear-face-cache))
  73. ((eq type 'tool-bar-style) (force-mode-line-update t)))))
  74. (define-key special-event-map [config-changed-event]
  75. 'dynamic-setting-handle-config-changed-event)