fringe.el 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. ;;; fringe.el --- fringe setup and control -*- coding: utf-8 -*-
  2. ;; Copyright (C) 2002-2012 Free Software Foundation, Inc.
  3. ;; Author: Simon Josefsson <simon@josefsson.org>
  4. ;; Maintainer: FSF
  5. ;; Keywords: frames
  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 contains code to initialize the built-in fringe bitmaps
  20. ;; as well as helpful functions for customizing the appearance of the
  21. ;; fringe.
  22. ;; The code is influenced by scroll-bar.el and avoid.el. The author
  23. ;; gratefully acknowledge comments and suggestions made by Miles
  24. ;; Bader, Eli Zaretskii, Richard Stallman, Pavel Janík and others which
  25. ;; improved this package.
  26. ;;; Code:
  27. (defgroup fringe nil
  28. "Window fringes."
  29. :version "22.1"
  30. :group 'frames)
  31. ;; Define the built-in fringe bitmaps and setup default mappings
  32. (when (boundp 'fringe-bitmaps)
  33. (let ((bitmaps '(question-mark
  34. left-arrow right-arrow up-arrow down-arrow
  35. left-curly-arrow right-curly-arrow
  36. left-triangle right-triangle
  37. top-left-angle top-right-angle
  38. bottom-left-angle bottom-right-angle
  39. left-bracket right-bracket
  40. filled-rectangle hollow-rectangle
  41. filled-square hollow-square
  42. vertical-bar horizontal-bar
  43. empty-line))
  44. (bn 1))
  45. (while bitmaps
  46. (push (car bitmaps) fringe-bitmaps)
  47. (put (car bitmaps) 'fringe bn)
  48. (setq bitmaps (cdr bitmaps)
  49. bn (1+ bn))))
  50. (setq-default fringe-indicator-alist
  51. '((truncation . (left-arrow right-arrow))
  52. (continuation . (left-curly-arrow right-curly-arrow))
  53. (overlay-arrow . right-triangle)
  54. (up . up-arrow)
  55. (down . down-arrow)
  56. (top . (top-left-angle top-right-angle))
  57. (bottom . (bottom-left-angle bottom-right-angle
  58. top-right-angle top-left-angle))
  59. (top-bottom . (left-bracket right-bracket
  60. top-right-angle top-left-angle))
  61. (empty-line . empty-line)
  62. (unknown . question-mark)))
  63. (setq-default fringe-cursor-alist
  64. '((box . filled-rectangle)
  65. (hollow . hollow-rectangle)
  66. (bar . vertical-bar)
  67. (hbar . horizontal-bar)
  68. (hollow-small . hollow-square))))
  69. (defmacro fringe-bitmap-p (symbol)
  70. "Return non-nil if SYMBOL is a fringe bitmap."
  71. `(get ,symbol 'fringe))
  72. ;; Control presence of fringes
  73. (defvar fringe-mode)
  74. (defvar fringe-mode-explicit nil
  75. "Non-nil means `set-fringe-mode' should really do something.
  76. This is nil while loading `fringe.el', and t afterward.")
  77. (defun set-fringe-mode-1 (_ignore value)
  78. "Call `set-fringe-mode' with VALUE.
  79. See `fringe-mode' for valid values and their effect.
  80. This is usually invoked when setting `fringe-mode' via customize."
  81. (set-fringe-mode value))
  82. (defun set-fringe-mode (value)
  83. "Set `fringe-mode' to VALUE and put the new value into effect.
  84. See `fringe-mode' for possible values and their effect."
  85. (setq fringe-mode value)
  86. (when fringe-mode-explicit
  87. (modify-all-frames-parameters
  88. (list (cons 'left-fringe (if (consp fringe-mode)
  89. (car fringe-mode)
  90. fringe-mode))
  91. (cons 'right-fringe (if (consp fringe-mode)
  92. (cdr fringe-mode)
  93. fringe-mode))))))
  94. ;; For initialization of fringe-mode, take account of changes
  95. ;; made explicitly to default-frame-alist.
  96. (defun fringe-mode-initialize (symbol value)
  97. (let* ((left-pair (assq 'left-fringe default-frame-alist))
  98. (right-pair (assq 'right-fringe default-frame-alist))
  99. (left (cdr left-pair))
  100. (right (cdr right-pair)))
  101. (if (or left-pair right-pair)
  102. ;; If there's something in default-frame-alist for fringes,
  103. ;; don't change it, but reflect that into the value of fringe-mode.
  104. (progn
  105. (setq fringe-mode (cons left right))
  106. (if (equal fringe-mode '(nil . nil))
  107. (setq fringe-mode nil))
  108. (if (equal fringe-mode '(0 . 0))
  109. (setq fringe-mode 0)))
  110. ;; Otherwise impose the user-specified value of fringe-mode.
  111. (custom-initialize-reset symbol value))))
  112. (defconst fringe-styles
  113. '(("default" . nil)
  114. ("no-fringes" . 0)
  115. ("right-only" . (0 . nil))
  116. ("left-only" . (nil . 0))
  117. ("half-width" . (4 . 4))
  118. ("minimal" . (1 . 1))))
  119. (defcustom fringe-mode nil
  120. "Specify appearance of fringes on all frames.
  121. This variable can be nil (the default) meaning the fringes should have
  122. the default width (8 pixels), it can be an integer value specifying
  123. the width of both left and right fringe (where 0 means no fringe), or
  124. a cons cell where car indicates width of left fringe and cdr indicates
  125. width of right fringe (where again 0 can be used to indicate no
  126. fringe).
  127. Note that the actual width may be rounded up to ensure that the sum of
  128. the width of the left and right fringes is a multiple of the frame's
  129. character width. However, a fringe width of 0 is never rounded.
  130. To set this variable in a Lisp program, use `set-fringe-mode' to make
  131. it take real effect.
  132. Setting the variable with a customization buffer also takes effect.
  133. If you only want to modify the appearance of the fringe in one frame,
  134. you can use the interactive function `set-fringe-style'."
  135. :type `(choice
  136. ,@ (mapcar (lambda (style)
  137. (let ((name
  138. (replace-regexp-in-string "-" " " (car style))))
  139. `(const :tag
  140. ,(concat (capitalize (substring name 0 1))
  141. (substring name 1))
  142. ,(cdr style))))
  143. fringe-styles)
  144. (integer :tag "Specific width")
  145. (cons :tag "Different left/right sizes"
  146. (integer :tag "Left width")
  147. (integer :tag "Right width")))
  148. :group 'fringe
  149. :require 'fringe
  150. :initialize 'fringe-mode-initialize
  151. :set 'set-fringe-mode-1)
  152. ;; We just set fringe-mode, but that was the default.
  153. ;; If it is set again, that is for real.
  154. (setq fringe-mode-explicit t)
  155. (defun fringe-query-style (&optional all-frames)
  156. "Query user for fringe style.
  157. Returns values suitable for left-fringe and right-fringe frame parameters.
  158. If ALL-FRAMES, the negation of the fringe values in
  159. `default-frame-alist' is used when user enters the empty string.
  160. Otherwise the negation of the fringe value in the currently selected
  161. frame parameter is used."
  162. (let* ((mode (completing-read
  163. (concat
  164. "Select fringe mode for "
  165. (if all-frames "all frames" "selected frame")
  166. ": ")
  167. fringe-styles nil t))
  168. (style (assoc (downcase mode) fringe-styles)))
  169. (if style (cdr style)
  170. (if (eq 0 (cdr (assq 'left-fringe
  171. (if all-frames
  172. default-frame-alist
  173. (frame-parameters (selected-frame))))))
  174. nil
  175. 0))))
  176. (defun fringe-mode (&optional mode)
  177. "Set the default appearance of fringes on all frames.
  178. When called interactively, query the user for MODE. Valid values
  179. for MODE include `none', `default', `left-only', `right-only',
  180. `minimal' and `half'.
  181. When used in a Lisp program, MODE can be a cons cell where the
  182. integer in car specifies the left fringe width and the integer in
  183. cdr specifies the right fringe width. MODE can also be a single
  184. integer that specifies both the left and the right fringe width.
  185. If a fringe width specification is nil, that means to use the
  186. default width (8 pixels). This command may round up the left and
  187. right width specifications to ensure that their sum is a multiple
  188. of the character width of a frame. It never rounds up a fringe
  189. width of 0.
  190. Fringe widths set by `set-window-fringes' override the default
  191. fringe widths set by this command. This command applies to all
  192. frames that exist and frames to be created in the future. If you
  193. want to set the default appearance of fringes on the selected
  194. frame only, see the command `set-fringe-style'."
  195. (interactive (list (fringe-query-style 'all-frames)))
  196. (set-fringe-mode mode))
  197. (defun set-fringe-style (&optional mode)
  198. "Set the default appearance of fringes on the selected frame.
  199. When called interactively, query the user for MODE. Valid values
  200. for MODE include `none', `default', `left-only', `right-only',
  201. `minimal' and `half'.
  202. When used in a Lisp program, MODE can be a cons cell where the
  203. integer in car specifies the left fringe width and the integer in
  204. cdr specifies the right fringe width. MODE can also be a single
  205. integer that specifies both the left and the right fringe width.
  206. If a fringe width specification is nil, that means to use the
  207. default width (8 pixels). This command may round up the left and
  208. right width specifications to ensure that their sum is a multiple
  209. of the character width of a frame. It never rounds up a fringe
  210. width of 0.
  211. Fringe widths set by `set-window-fringes' override the default
  212. fringe widths set by this command. If you want to set the
  213. default appearance of fringes on all frames, see the command
  214. `fringe-mode'."
  215. (interactive (list (fringe-query-style)))
  216. (modify-frame-parameters
  217. (selected-frame)
  218. (list (cons 'left-fringe (if (consp mode) (car mode) mode))
  219. (cons 'right-fringe (if (consp mode) (cdr mode) mode)))))
  220. (defsubst fringe-columns (side &optional real)
  221. "Return the width, measured in columns, of the fringe area on SIDE.
  222. If optional argument REAL is non-nil, return a real floating point
  223. number instead of a rounded integer value.
  224. SIDE must be the symbol `left' or `right'."
  225. (funcall (if real '/ 'ceiling)
  226. (or (funcall (if (eq side 'left) 'car 'cadr)
  227. (window-fringes))
  228. 0)
  229. (float (frame-char-width))))
  230. (provide 'fringe)
  231. ;;; fringe.el ends here