fringe.el 10.1 KB
Newer Older
Kenichi Handa's avatar
Kenichi Handa committed
1
;;; fringe.el --- fringe setup and control  -*- coding: utf-8 -*-
Simon Josefsson's avatar
Simon Josefsson committed
2

Glenn Morris's avatar
Glenn Morris committed
3
;; Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008,
Glenn Morris's avatar
Glenn Morris committed
4
;;   2009, 2010 Free Software Foundation, Inc.
Simon Josefsson's avatar
Simon Josefsson committed
5 6 7 8

;; Author: Simon Josefsson <simon@josefsson.org>
;; Maintainer: FSF
;; Keywords: frames
9
;; Package: emacs
Simon Josefsson's avatar
Simon Josefsson committed
10 11 12

;; This file is part of GNU Emacs.

13
;; GNU Emacs is free software: you can redistribute it and/or modify
Simon Josefsson's avatar
Simon Josefsson committed
14
;; it under the terms of the GNU General Public License as published by
15 16
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
Simon Josefsson's avatar
Simon Josefsson committed
17 18 19 20 21 22 23

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
24
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
Simon Josefsson's avatar
Simon Josefsson committed
25 26 27

;;; Commentary:

Kim F. Storm's avatar
Kim F. Storm committed
28 29 30
;; This file contains code to initialize the built-in fringe bitmaps
;; as well as helpful functions for customizing the appearance of the
;; fringe.
Simon Josefsson's avatar
Simon Josefsson committed
31 32 33 34 35 36 37 38

;; The code is influenced by scroll-bar.el and avoid.el.  The author
;; gratefully acknowledge comments and suggestions made by Miles
;; Bader, Eli Zaretski, Richard Stallman, Pavel Janík and others which
;; improved this package.

;;; Code:

Kim F. Storm's avatar
Kim F. Storm committed
39 40
(defgroup fringe nil
  "Window fringes."
41
  :version "22.1"
Kim F. Storm's avatar
Kim F. Storm committed
42 43
  :group 'frames)

Kim F. Storm's avatar
Kim F. Storm committed
44 45
;; Define the built-in fringe bitmaps and setup default mappings

Kim F. Storm's avatar
Kim F. Storm committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
(when (boundp 'fringe-bitmaps)
  (let ((bitmaps '(question-mark
		   left-arrow right-arrow up-arrow down-arrow
		   left-curly-arrow right-curly-arrow
		   left-triangle right-triangle
		   top-left-angle top-right-angle
		   bottom-left-angle bottom-right-angle
		   left-bracket right-bracket
		   filled-rectangle hollow-rectangle
		   filled-square hollow-square
		   vertical-bar horizontal-bar
		   empty-line))
	(bn 1))
    (while bitmaps
      (push (car bitmaps) fringe-bitmaps)
      (put (car bitmaps) 'fringe bn)
      (setq bitmaps (cdr bitmaps)
	    bn (1+ bn))))

  (setq-default fringe-indicator-alist
		'((truncation . (left-arrow right-arrow))
		  (continuation . (left-curly-arrow right-curly-arrow))
		  (overlay-arrow . right-triangle)
		  (up . up-arrow)
		  (down . down-arrow)
		  (top . (top-left-angle top-right-angle))
		  (bottom . (bottom-left-angle bottom-right-angle
			     top-right-angle top-left-angle))
		  (top-bottom . (left-bracket right-bracket
				 top-right-angle top-left-angle))
		  (empty-line . empty-line)
		  (unknown . question-mark)))

  (setq-default fringe-cursor-alist
		'((box . filled-rectangle)
		  (hollow . hollow-rectangle)
		  (bar . vertical-bar)
		  (hbar . horizontal-bar)
		  (hollow-small . hollow-square))))
85

Kim F. Storm's avatar
Kim F. Storm committed
86 87 88 89 90 91

(defmacro fringe-bitmap-p (symbol)
  "Return non-nil if SYMBOL is a fringe bitmap."
  `(get ,symbol 'fringe))


92 93
;; Control presence of fringes

Simon Josefsson's avatar
Simon Josefsson committed
94 95
(defvar fringe-mode)

96 97 98 99
(defvar fringe-mode-explicit nil
  "Non-nil means `set-fringe-mode' should really do something.
This is nil while loading `fringe.el', and t afterward.")

Simon Josefsson's avatar
Simon Josefsson committed
100 101 102 103 104 105 106 107 108 109 110
(defun set-fringe-mode-1 (ignore value)
  "Call `set-fringe-mode' with VALUE.
See `fringe-mode' for valid values and their effect.
This is usually invoked when setting `fringe-mode' via customize."
  (set-fringe-mode value))

(defun set-fringe-mode (value)
  "Set `fringe-mode' to VALUE and put the new value into effect.
See `fringe-mode' for possible values and their effect."
  (setq fringe-mode value)

111 112 113 114 115 116 117 118
  (when fringe-mode-explicit
    (modify-all-frames-parameters
     (list (cons 'left-fringe (if (consp fringe-mode)
				  (car fringe-mode)
				fringe-mode))
	   (cons 'right-fringe (if (consp fringe-mode)
				   (cdr fringe-mode)
				 fringe-mode))))))
Simon Josefsson's avatar
Simon Josefsson committed
119

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
;; For initialization of fringe-mode, take account of changes
;; made explicitly to default-frame-alist.
(defun fringe-mode-initialize (symbol value)
  (let* ((left-pair (assq 'left-fringe default-frame-alist))
	 (right-pair (assq 'right-fringe default-frame-alist))
	 (left (cdr left-pair))
	 (right (cdr right-pair)))
    (if (or left-pair right-pair)
	;; If there's something in default-frame-alist for fringes,
	;; don't change it, but reflect that into the value of fringe-mode.
	(progn
	  (setq fringe-mode (cons left right))
	  (if (equal fringe-mode '(nil . nil))
	      (setq fringe-mode nil))
	  (if (equal fringe-mode '(0 . 0))
	      (setq fringe-mode 0)))
      ;; Otherwise impose the user-specified value of fringe-mode.
      (custom-initialize-reset symbol value))))

139 140 141 142 143 144 145 146
(defconst fringe-styles
  '(("default" . nil)
    ("no-fringes" . 0)
    ("right-only" . (0 . nil))
    ("left-only" . (nil . 0))
    ("half-width" . (4 . 4))
    ("minimal" . (1 . 1))))

Simon Josefsson's avatar
Simon Josefsson committed
147
(defcustom fringe-mode nil
Lute Kamstra's avatar
Lute Kamstra committed
148
  "Specify appearance of fringes on all frames.
Simon Josefsson's avatar
Simon Josefsson committed
149 150 151 152 153 154
This variable can be nil (the default) meaning the fringes should have
the default width (8 pixels), it can be an integer value specifying
the width of both left and right fringe (where 0 means no fringe), or
a cons cell where car indicates width of left fringe and cdr indicates
width of right fringe (where again 0 can be used to indicate no
fringe).
155 156 157
Note that the actual width may be rounded up to ensure that the sum of
the width of the left and right fringes is a multiple of the frame's
character width.  However, a fringe width of 0 is never rounded.
Simon Josefsson's avatar
Simon Josefsson committed
158 159 160 161
To set this variable in a Lisp program, use `set-fringe-mode' to make
it take real effect.
Setting the variable with a customization buffer also takes effect.
If you only want to modify the appearance of the fringe in one frame,
162
you can use the interactive function `set-fringe-style'."
163 164 165 166 167 168 169 170 171 172 173 174 175
  :type `(choice
          ,@ (mapcar (lambda (style)
                      (let ((name
                             (replace-regexp-in-string "-" " " (car style))))
                        `(const :tag
                                ,(concat (capitalize (substring name 0 1))
                                         (substring name 1))
                                ,(cdr style))))
                    fringe-styles)
          (integer :tag "Specific width")
          (cons :tag "Different left/right sizes"
                (integer :tag "Left width")
                (integer :tag "Right width")))
Kim F. Storm's avatar
Kim F. Storm committed
176
  :group 'fringe
Simon Josefsson's avatar
Simon Josefsson committed
177
  :require 'fringe
178
  :initialize 'fringe-mode-initialize
Simon Josefsson's avatar
Simon Josefsson committed
179 180
  :set 'set-fringe-mode-1)

181 182 183 184
;; We just set fringe-mode, but that was the default.
;; If it is set again, that is for real.
(setq fringe-mode-explicit t)

Simon Josefsson's avatar
Simon Josefsson committed
185 186 187 188 189 190 191
(defun fringe-query-style (&optional all-frames)
  "Query user for fringe style.
Returns values suitable for left-fringe and right-fringe frame parameters.
If ALL-FRAMES, the negation of the fringe values in
`default-frame-alist' is used when user enters the empty string.
Otherwise the negation of the fringe value in the currently selected
frame parameter is used."
192 193 194 195 196 197 198 199 200 201 202 203 204 205
  (let* ((mode (completing-read
                (concat
                 "Select fringe mode for "
                 (if all-frames "all frames" "selected frame")
                 " (type ? for list): ")
                fringe-styles nil t))
         (style (assoc (downcase mode) fringe-styles)))
    (if style (cdr style)
      (if (eq 0 (cdr (assq 'left-fringe
                           (if all-frames
                               default-frame-alist
                             (frame-parameters (selected-frame))))))
          nil
        0))))
Simon Josefsson's avatar
Simon Josefsson committed
206 207

(defun fringe-mode (&optional mode)
208
  "Set the default appearance of fringes on all frames.
Lute Kamstra's avatar
Lute Kamstra committed
209

210 211 212
When called interactively, query the user for MODE.  Valid values
for MODE include `none', `default', `left-only', `right-only',
`minimal' and `half'.
Lute Kamstra's avatar
Lute Kamstra committed
213 214 215 216 217

When used in a Lisp program, MODE can be a cons cell where the
integer in car specifies the left fringe width and the integer in
cdr specifies the right fringe width.  MODE can also be a single
integer that specifies both the left and the right fringe width.
218 219 220 221 222
If a fringe width specification is nil, that means to use the
default width (8 pixels).  This command may round up the left and
right width specifications to ensure that their sum is a multiple
of the character width of a frame.  It never rounds up a fringe
width of 0.
Lute Kamstra's avatar
Lute Kamstra committed
223 224 225 226 227 228

Fringe widths set by `set-window-fringes' override the default
fringe widths set by this command.  This command applies to all
frames that exist and frames to be created in the future.  If you
want to set the default appearance of fringes on the selected
frame only, see the command `set-fringe-style'."
Simon Josefsson's avatar
Simon Josefsson committed
229 230 231 232
  (interactive (list (fringe-query-style 'all-frames)))
  (set-fringe-mode mode))

(defun set-fringe-style (&optional mode)
233
  "Set the default appearance of fringes on the selected frame.
Lute Kamstra's avatar
Lute Kamstra committed
234

235 236 237
When called interactively, query the user for MODE.  Valid values
for MODE include `none', `default', `left-only', `right-only',
`minimal' and `half'.
Lute Kamstra's avatar
Lute Kamstra committed
238 239 240 241 242

When used in a Lisp program, MODE can be a cons cell where the
integer in car specifies the left fringe width and the integer in
cdr specifies the right fringe width.  MODE can also be a single
integer that specifies both the left and the right fringe width.
243 244 245 246 247
If a fringe width specification is nil, that means to use the
default width (8 pixels).  This command may round up the left and
right width specifications to ensure that their sum is a multiple
of the character width of a frame.  It never rounds up a fringe
width of 0.
Lute Kamstra's avatar
Lute Kamstra committed
248 249 250 251 252

Fringe widths set by `set-window-fringes' override the default
fringe widths set by this command.  If you want to set the
default appearance of fringes on all frames, see the command
`fringe-mode'."
Simon Josefsson's avatar
Simon Josefsson committed
253 254 255 256 257 258
  (interactive (list (fringe-query-style)))
  (modify-frame-parameters
   (selected-frame)
   (list (cons 'left-fringe (if (consp mode) (car mode) mode))
	 (cons 'right-fringe (if (consp mode) (cdr mode) mode)))))

259 260 261 262 263 264 265 266 267 268
(defsubst fringe-columns (side &optional real)
  "Return the width, measured in columns, of the fringe area on SIDE.
If optional argument REAL is non-nil, return a real floating point
number instead of a rounded integer value.
SIDE must be the symbol `left' or `right'."
  (funcall (if real '/ 'ceiling)
	   (or (funcall (if (eq side 'left) 'car 'cadr)
			(window-fringes))
	       0)
           (float (frame-char-width))))
Kim F. Storm's avatar
Kim F. Storm committed
269

Simon Josefsson's avatar
Simon Josefsson committed
270 271
(provide 'fringe)

272
;; arch-tag: 6611ef60-0869-47ed-8b93-587ee7d3ff5d
Simon Josefsson's avatar
Simon Josefsson committed
273
;;; fringe.el ends here