f55dc42bf892cf5904918b26cc30431677f68587
[emacs.git] / .emacs.d / elisp / local / mic-paren.el
1 ;;; mic-paren.el --- advanced highlighting of matching parentheses
2
3 ;;; Copyright (C) 2008, 2012, 2013 Thien-Thi Nguyen
4 ;;; Copyright (C) 1997 Mikael Sjödin (mic@docs.uu.se)
5
6 ;; Version: 3.11
7 ;; Released: 2013-09-20
8 ;; Author: Mikael Sjödin (mic@docs.uu.se)
9 ;; Klaus Berndl <berndl@sdm.de>
10 ;; Jonathan Kotta <jpkotta@gmail.com>
11 ;; Maintainer: ttn
12 ;; Keywords: languages, faces, parenthesis, matching
13 ;;
14 ;; This program contains additional code by:
15 ;; - Vinicius Jose Latorre <vinicius@cpqd.br>
16 ;; - Steven L Baur <steve@xemacs.org>
17 ;; - Klaus Berndl <berndl@sdm.de>
18 ;; - ttn
19 ;;
20 ;; mic-paren.el is free software
21 ;;
22 ;; This file is *NOT* (yet?) part of GNU Emacs.
23 ;;
24 ;; This program is free software; you can redistribute it and/or modify
25 ;; it under the terms of the GNU General Public License as published by
26 ;; the Free Software Foundation; either version 3, or (at your option)
27 ;; any later version.
28 ;;
29 ;; This program is distributed in the hope that it will be useful,
30 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
31 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 ;; GNU General Public License for more details.
33 ;;
34 ;; You should have received a copy of the GNU General Public License
35 ;; along with GNU Emacs; see the file COPYING. If not, write to the
36 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
37 ;; Boston, MA 02111-1307, USA.
38
39 ;;; Commentary:
40
41 ;; ----------------------------------------------------------------------
42 ;; Short Description:
43 ;;
44 ;; Load this file, activate it and Emacs will display highlighting on
45 ;; whatever parenthesis (and paired delimiter if you like this) matches
46 ;; the one before or after point. This is an extension to the paren.el
47 ;; file distributed with Emacs. The default behaviour is similar to
48 ;; paren.el but more sophisticated. Normally you can try all default
49 ;; settings to enjoy mic-paren.
50 ;;
51 ;; Some examples to try in your ~/.emacs:
52 ;;
53 ;; (add-hook 'LaTeX-mode-hook
54 ;; (function (lambda ()
55 ;; (paren-toggle-matching-quoted-paren 1)
56 ;; (paren-toggle-matching-paired-delimiter 1))))
57 ;;
58 ;; (add-hook 'c-mode-common-hook
59 ;; (function (lambda ()
60 ;; (paren-toggle-open-paren-context 1))))
61 ;;
62 ;; If you use CUA mode, these might be useful, too:
63 ;;
64 ;; (put 'paren-forward-sexp 'CUA 'move)
65 ;; (put 'paren-backward-sexp 'CUA 'move)
66 ;;
67 ;; ----------------------------------------------------------------------
68 ;; Installation:
69 ;;
70 ;; o Place this file in a directory in your `load-path' and byte-compile
71 ;; it. If there are warnings, please report them to ttn.
72 ;; o Put the following in your .emacs file:
73 ;; (require 'mic-paren) ; loading
74 ;; (paren-activate) ; activating
75 ;; ;;; set here any of the customizable variables of mic-paren:
76 ;; ;;; ...
77 ;; o Restart your Emacs; mic-paren is now installed and activated!
78 ;; o To list the possible customizations type `C-h f paren-activate' or
79 ;; go to the customization group `mic-paren-matching'.
80 ;;
81 ;; ----------------------------------------------------------------------
82 ;; Long Description:
83 ;;
84 ;; mic-paren.el is an extension and replacement to the packages paren.el
85 ;; and stig-paren.el for Emacs. When mic-paren is active Emacs normal
86 ;; parenthesis matching is deactivated. Instead parenthesis matching will
87 ;; be performed as soon as the cursor is positioned at a parenthesis. The
88 ;; matching parenthesis (or the entire structured expression between the
89 ;; parentheses) is highlighted until the cursor is moved away from the
90 ;; parenthesis. Features include:
91 ;; o Both forward and backward parenthesis matching (simultaneously if
92 ;; cursor is between two expressions).
93 ;; o Indication of mismatched parentheses.
94 ;; o Recognition of "escaped" (also often called "quoted") parentheses.
95 ;; o Recognition of SML-style "sexp-ish" comment syntax.
96 ;; NB: This support is preliminary; there are still problems
97 ;; when the parens in the comment span multiple lines, etc.
98 ;; o Option to match "escaped" parens too, especially in (La)TeX-mode
99 ;; (e.g., matches expressions like "\(foo bar\)" properly).
100 ;; o Offers two functions as replacement for `forward-sexp' and
101 ;; `backward-sexp' which handle properly quoted parens (s.a.). These
102 ;; new functions can automatically be bounded to the original binding
103 ;; of the standard `forward-sexp' and `backward-sexp' functions.
104 ;; o Option to activate matching of paired delimiter (i.e., characters with
105 ;; syntax '$'). This is useful for writing in LaTeX-mode for example.
106 ;; o Option to select in which situations (always, never, if match, if
107 ;; mismatch) the entire expression should be highlighted or only the
108 ;; matching parenthesis.
109 ;; o Message describing the match when the matching parenthesis is off-screen
110 ;; (vertical and/or horizontal). Message contains either the linenumber or
111 ;; the number of lines between the two matching parens. Option to select in
112 ;; which cases this message should be displayed.
113 ;; o Optional delayed highlighting (useful on slow systems),
114 ;; o Functions to activate/deactivate mic-paren.el are provided.
115 ;; o Numerous options to control the behaviour and appearance of
116 ;; mic-paren.el.
117 ;;
118 ;; mic-paren.el was originally developed and tested under Emacs 19.28 -
119 ;; 20.3. Since then, support for Emacs 19 and 20 has bit-rotted (not
120 ;; dropped completely, but not tested against changes, either), and
121 ;; will probably be removed without warning in a future version. This
122 ;; version was developed and tested under Emacs 23.0.60 (wip). XEmacs
123 ;; compatibility has been provided by Steven L Baur <steve@xemacs.org>.
124 ;; Jan Dubois (jaduboi@ibm.net) provided help to get mic-paren to work in
125 ;; OS/2.
126 ;;
127 ;; This file (and other wonderful stuff) can be obtained from
128 ;; the Emacs Wiki: <http://www.emacswiki.org/>
129 ;;
130 ;; ----------------------------------------------------------------------
131 ;; Available customizable options:
132 ;; - `paren-priority'
133 ;; - `paren-overlay-priority'
134 ;; - `paren-sexp-mode'
135 ;; - `paren-highlight-at-point'
136 ;; - `paren-highlight-offscreen'
137 ;; - `paren-display-message'
138 ;; - `paren-message-linefeed-display'
139 ;; - `paren-message-no-match'
140 ;; - `paren-message-show-linenumber'
141 ;; - `paren-message-truncate-lines'
142 ;; - `paren-max-message-length'
143 ;; - `paren-ding-unmatched'
144 ;; - `paren-delay'
145 ;; - `paren-dont-touch-blink'
146 ;; - `paren-match-face'
147 ;; - `paren-mismatch-face'
148 ;; - `paren-no-match-paren'
149 ;; - `paren-bind-modified-sexp-functions'
150 ;; Available customizable faces:
151 ;; - `paren-face-match'
152 ;; - `paren-face-mismatch'
153 ;; - `paren-face-no-match'
154 ;; Available commands:
155 ;; - `paren-activate'
156 ;; - `paren-deactivate'
157 ;; - `paren-toggle-matching-paired-delimiter'
158 ;; - `paren-toggle-matching-quoted-paren'
159 ;; - `paren-toggle-open-paren-context'
160 ;; - `paren-forward-sexp'
161 ;; - `paren-backward-sexp'
162 ;; ----------------------------------------------------------------------
163 ;;
164 ;; IMPORTANT NOTES (important for people who have customized mic-paren
165 ;; from within elisp):
166 ;; - In version >= 3.3 the prefix "mic-" has been removed from the
167 ;; command-names `mic-paren-forward-sexp' and `mic-paren-backward-sexp'.
168 ;; Now all user-functions and -options begin with the prefix "paren-"
169 ;; because this package should be a replacement of the other
170 ;; paren-packages like paren.el and stig-paren.el!
171 ;; - In version >= 3.2 the prefix "mic-" has been removed from the
172 ;; command-names `mic-paren-toggle-matching-quoted-paren' and
173 ;; `mic-paren-toggle-matching-paired-delimiter'.
174 ;; - In versions >= 3.1 mic-paren is NOT auto-activated after loading.
175 ;; - In versions >= 3.0 the variable `paren-face' has been renamed to
176 ;; `paren-match-face'.
177 ;;
178 ;; ----------------------------------------------------------------------
179 ;; Versions:
180 ;; v3.11 + Added support for recognizing SML-style comments as a sexp.
181 ;; Thanks to Leo Liu, Stefan Monnier.
182 ;;
183 ;; v3.10 + Added message-length clamping (var `paren-max-message-length').
184 ;; Thanks to Jonathan Kotta.
185 ;;
186 ;; v3.9 + Fixed XEmacs bug in `define-mic-paren-nolog-message'.
187 ;; Thanks to Sivaram Neelakantan.
188 ;;
189 ;; v3.8 + Maintainership (crassly) grabbed by ttn.
190 ;; + License now GPLv3+.
191 ;; + Byte-compiler warnings eliminated; if you see one, tell me!
192 ;; + Dropped funcs: mic-char-bytes, mic-char-before.
193 ;; + Docstrings, messages, comments revamped.
194 ;;
195 ;; v3.7 + Removed the message "You should be in LaTeX-mode!".
196 ;; + Fixed a bug in `paren-toggle-matching-quoted-paren'.
197 ;; + Fixed some misspellings in the comments and docs.
198 ;;
199 ;; v3.6 + Fixed a very small bug in `mic-paren-horizontal-pos-visible-p'.
200 ;; + The informational messages like "Matches ... [+28]" which are
201 ;; displayed if the matching paren is offscreen, do not longer
202 ;; wasting the log.
203 ;;
204 ;; v3.5 + No mic-paren-messages are displayed if we are in isearch-mode.
205 ;; + Matching quoted parens is switched on if entering a minibuffer.
206 ;; This is useful for easier inserting regexps, e.g., with
207 ;; `query-replace-regexp'. Now \(...\) will be highlighted
208 ;; in the minibuffer.
209 ;; + New option `paren-message-show-linenumber': You can determine
210 ;; the computation of the offscreen-message-linenumber. Either the
211 ;; number of lines between the two matching parens or the absolute
212 ;; linenumber. (Thank you for the idea and a first implementation
213 ;; to Eli Barzilay <eli@barzilay.org>.)
214 ;; + New option `paren-message-truncate-lines': If mic-paren messages
215 ;; should be truncated or not (has only an effect in GNU Emacs 21).
216 ;; (Thank you for the idea and a first implementation to Eli
217 ;; Barzilay <eli@barzilay.org>.)
218 ;;
219 ;; v3.4 + Corrected some bugs in the backward-compatibility for older
220 ;; Emacsen. Thanks to Tetsuo Tsukamoto <czkmt@remus.dti.ne.jp>.
221 ;;
222 ;; v3.3 + Now the priority of the paren-overlays can be customized
223 ;; (option `paren-overlay-priority'). For a description of the
224 ;; priority of an overlay see in the emacs-lisp-manual the node
225 ;; "Overlays". This option is mainly useful for experienced
226 ;; users which use many packages using overlays to perform their
227 ;; tasks.
228 ;; + Now you can determine what line-context will be displayed if
229 ;; the matching open paren is offscreen. In functional
230 ;; programming languages like lisp it is useful to display the
231 ;; following line in the echo-area if the opening matching paren
232 ;; has no preceding text in the same line.
233 ;; But in procedural languages like C++ or Java it is convenient
234 ;; to display the first previous non empty line in this case
235 ;; instead of the following line. Look at the new variable
236 ;; `paren-open-paren-context-backward' and the related toggling
237 ;; function `paren-toggle-open-paren-context' for a detailed
238 ;; description of this new feature.
239 ;; + In addition to the previous described new feature you can
240 ;; specify how a linefeed in the message (e.g., if the matching
241 ;; paren is offscreen) is displayed. This is mainly because the
242 ;; standard echo-area display of a linefeed (^J) is bad to read.
243 ;; Look at the option `paren-message-linefeed-display'.
244 ;; + Solved a little bug in the compatibility-code for Emacsen
245 ;; not supporting current customize-feature.
246 ;; + Removed the prefix "mic-" from the commands
247 ;; `mic-paren-forward-sexp' and `mic-paren-backward-sexp'.
248 ;; For an explanation look at comments for version v3.2.
249 ;;
250 ;; v3.2 + The prefix "mic-" has been removed from the commands
251 ;; `mic-paren-toggle-matching-quoted-paren' and
252 ;; `mic-paren-toggle-matching-paired-delimiter'. This is for
253 ;; consistency. Now all user-variables, -faces and -commands
254 ;; begin with the prefix "paren-" and all internal functions
255 ;; and variables begin with the prefix "mic-paren-".
256 ;; + Now you can exactly specify in which situations the whole
257 ;; sexp should be highlighted (option `paren-sexp-mode'):
258 ;; Always, never, if match or if mismatch. Tested with Gnus
259 ;; Emacs >= 20.3.1 and XEmacs >= 21.1.
260 ;;
261 ;; v3.1 + From this version on mic-paren is not autoloaded. To
262 ;; activate it you must call `paren-activate' (either in your
263 ;; .emacs or manually with M-x). Therefore the variable
264 ;; `paren-dont-activate-on-load' ise obsolet and has been
265 ;; removed.
266 ;; + Now mic-paren works also in older Emacsen without the
267 ;; custom-feature. If the actual custom-library is provided
268 ;; mic-paren use them and is full customizable otherwise normal
269 ;; defvars are used for the options.
270 ;; + Fix of a bug displaying a message if the matching paren is
271 ;; horizontal out of view.
272 ;; + All new features are now tested with XEmacs >= 21.1.6.
273 ;;
274 ;; v3.0 + Checking if matching paren is horizontally offscreen (in
275 ;; case of horizontal scrolling). In that case the message is
276 ;; displayed in the echo-area (anlogue to vertical offscreen).
277 ;; In case of horizontal offscreen closing parenthesis the
278 ;; displayed message is probably wider than the frame/window.
279 ;; So you can only read the whole message if you are using a
280 ;; package like mscroll.el (scrolling long messages) in GNU
281 ;; Emacs.
282 ;; + Now full customizable, means all user-options and -faces now
283 ;; can be set with the custom-feature of Emacs. On the other
284 ;; hand, this means this version of mic-paren only works with an
285 ;; Emacs which provides the custom-package!
286 ;; + In case of the matching paren is offscreen now the displayed
287 ;; message contains the linenumber of the matching paren too.
288 ;; This version is only tested with Gnu Emacs >= 20.4 and not with
289 ;; any XEmacs!
290 ;; Implemented by Klaus Berndl <berndl@sdm.de>.
291 ;;
292 ;; v2.3 No additional feature but replacing `char-bytes' and
293 ;; `char-before' with `mic-char-bytes' and `mic-char-before' to
294 ;; prevent a clash in the global-namespace. Now the new
295 ;; features of v2.1 and v2.2 are also tested with XEmacs!
296 ;;
297 ;; v2.2 Adding the new feature for matching paired delimiter. Not
298 ;; tested with XEmacs. Implemented by Klaus Berndl <berndl@sdm.de>
299 ;;
300 ;; v2.1 Adding the new feature for matching escaped parens too. Not
301 ;; tested with XEmacs. Implemented by Klaus Berndl <berndl@sdm.de>.
302 ;;
303 ;; v2.0 Support for MULE and Emacs 20 multibyte characters added.
304 ;; Inspired by the suggestion and code of Saito Takaaki
305 ;; <takaaki@is.s.u-tokyo.ac.jp>.
306 ;;
307 ;; v1.9 Avoids multiple messages/dings when point has not moved. Thus,
308 ;; mic-paren no longer overwrites messages in minibuffer. Inspired by
309 ;; the suggestion and code of Eli Barzilay <eli@barzilay.org>.
310 ;;
311 ;; v1.3.1 Some spelling corrected (from Vinicius Jose Latorre
312 ;; <vinicius@cpqd.br> and Steven L Baur <steve@xemacs.org>).
313 ;;
314 ;; v1.3 Added code from Vinicius Jose Latorre <vinicius@cpqd.br> to
315 ;; highlight unmatched parentheses (useful in minibuffer).
316 ;;
317 ;; v1.2.1 Fixed stuff to work with OS/2 emx-emacs:
318 ;; - checks if `x-display-colour-p' is bound before calling it;
319 ;; - changed how X/Lucid Emacs is detected.
320 ;; Added automatic load of the timer-feature (plus variable to
321 ;; disable the loading).
322
323 ;;; Code:
324
325 (defvar mic-paren-version "3.11"
326 "Version of mic-paren.")
327
328 (eval-when-compile (require 'cl))
329
330 ;;; ======================================================================
331 ;; Compatibility stuff
332 ;; BLOB to make custom stuff work even without customize
333 (eval-and-compile
334 (condition-case ()
335 (require 'custom)
336 (error nil))
337 (unless (fboundp 'defgroup)
338 (defmacro defgroup (&rest rest) nil))
339 (unless (fboundp 'defcustom)
340 (defmacro defcustom (sym val str &rest rest)
341 `(defvar ,sym ,val ,str)))
342 (unless (fboundp 'defface)
343 (defmacro defface (sym val str &rest rest)
344 `(defvar ,sym (make-face ',sym) ,str))))
345
346 ;;; ======================================================================
347 ;;; here begin the user options
348
349 (defgroup mic-paren-matching nil
350 "Enable advanced (un)matching of parens and expressions."
351 :prefix "paren-"
352 :group 'paren-matching)
353
354 (defcustom paren-priority 'both
355 "*Control behavior in a \"abutted close-open\" situation.
356 This occurs when point is between a closing and an opening
357 parenthesis, such as: (A B)(C D)
358 ^
359 point
360 close -- highlight the parenthesis matching the close-parenthesis
361 before the point (highlight opening paren before A).
362 open -- highlight the parenthesis matching the open-parenthesis after
363 the point (highlight closing paren after D).
364 both -- highlight both parenthesis matching the parenthesis beside
365 the point (highlight opening before A and closing after D)."
366 :type '(choice (const :tag "Match close" close)
367 (const :tag "Match open" open)
368 (const :tag "Match both" both))
369 :group 'mic-paren-matching)
370
371 (defcustom paren-overlay-priority 999
372 "*Non-negative integer to specify paren overlay priority.
373 For details, see info node `(elisp) Overlays'.
374 Normally you don't want to change the default value!"
375 :set (function
376 (lambda (symbol value)
377 (set symbol (if (< value 0) (* -1 value) value))))
378 :initialize 'custom-initialize-default
379 :type 'integer
380 :group 'mic-paren-matching)
381
382 (defcustom paren-sexp-mode nil
383 "*Control in which situations the whole sexp should be highlighted.
384 This means the whole s-expression between the matching parentheses is
385 highlighted instead of only the matching/mismatching parenthesis.
386
387 t -- Always highlight the whole s-expression.
388 nil -- Never highlight the whole s-expression.
389 match -- Highlight the whole s-expression only if the parens match.
390 mismatch -- Highlight the whole s-expression only if the parens don't match."
391 :type '(choice (const :tag "Never sexp-mode" nil)
392 (const :tag "Always sexp-mode" t)
393 (const :tag "If match" match)
394 (const :tag "If mismatch" mismatch))
395 :group 'mic-paren-matching)
396
397 (defcustom paren-highlight-at-point t
398 "*Non-nil highlights both parens when point is after the close-paren.
399 If nil, only the open parenthesis is highlighted."
400 :type '(choice (const :tag "Highlight both" t)
401 (const :tag "Highlight open" nil))
402 :group 'mic-paren-matching)
403
404 (defcustom paren-highlight-offscreen nil
405 "*Non-nil enables highlighting text not visible in the current buffer.
406
407 This is useful if you regularly display the current buffer in
408 multiple windows or frames (e.g., if you use Follow mode, by
409 andersl@csd.uu.se). Note: this option may slow down your Emacs.
410
411 This variable is ignored (treated as non-nil) if you set
412 `paren-sexp-mode' to non-nil."
413 :type 'boolean
414 :group 'mic-paren-matching)
415
416 (defcustom paren-display-message 'only
417 "*Display message if matching parenthesis is off-screen.
418 Possible settings are:
419 always -- message is always displayed regardless if offscreen or not
420 only -- message is only displayed when matching is offscreen
421 never -- never a message is displayed."
422 :type '(choice (const :tag "Display always" always)
423 (const :tag "Display if offscreen" only)
424 (const :tag "No Message display" never))
425 :group 'mic-paren-matching)
426
427 (defcustom paren-message-linefeed-display " RET "
428 "*String for displaying a linefeed in the matching paren context message.
429 There are three predefined values:
430 - Displays linefeeds with \" RET \" in the message.
431 - Displays linefeeds with a SPACE in the message.
432 - Displays linefeeds in the standard-form, means with \"^J\".
433 But you can also specify any user-defined string for it.
434
435 For further explanations about message displaying look at
436 `paren-display-message'."
437 :type '(choice (const :tag "Display with \"RET\"" :value " RET ")
438 (const :tag "Display with a SPACE" :value " ")
439 (const :tag "Standard" :value "^J")
440 (string :tag "User defined"))
441 :group 'mic-paren-matching)
442
443 (defcustom paren-message-show-linenumber 'sexp
444 "*Determine the computation of the offscreen-message-linenumber.
445
446 If the matching paren is offscreen, then maybe a message with the
447 context of the matching paren and it's linenumber is displayed
448 \(depends on the setting in `paren-display-message'). Here the
449 computation of the linenumber can be determined:
450
451 sexp -- Display the number of lines between the matching parens. Then the
452 number of lines is displayed as negative number if the matching paren
453 is somewhere above. Otherwise the number has a positive sign.
454
455 absolute -- Display the absolute linenumber of the machting paren computed
456 from the beginning of the buffer."
457 :type '(choice (const :tag "Count accros sexp" sexp)
458 (const :tag "Absolute number" absolute))
459 :group 'mic-paren-matching)
460
461 (defcustom paren-message-no-match t
462 "*Display message if no matching parenthesis is found."
463 :type '(choice (const :tag "Display message" t)
464 (const :tag "No message" nil))
465 :group 'mic-paren-matching)
466
467 (defcustom paren-message-truncate-lines t
468 "*Non nil means truncate lines for all messages mic-paren can display.
469 This option has only an effect with GNU Emacs 21.x!"
470 :type 'boolean
471 :group 'mic-paren-matching)
472
473 (defcustom paren-max-message-length 0
474 "*If positive, the max length `mic-paren-nolog-message' should output.
475 The length is reduced by removing the middle section of the message.
476 A value of zero means do not modify the message."
477 :type 'integer
478 :group 'mic-paren-matching)
479
480 (defcustom paren-ding-unmatched nil
481 "*Non-nil means make noise in unmatched situations.
482 An unmatched situation occurs if the cursor is at an unmatched
483 parenthesis or no matching parenthesis is found.
484 Even if nil, typing an unmatched parenthesis produces a ding."
485 :type 'boolean
486 :group 'mic-paren-matching)
487
488 (defcustom paren-delay nil
489 "*This variable controls when highlighting is done.
490 The variable has different meaning in different versions of Emacs.
491
492 In Emacs 19.29 and below:
493 This variable is ignored.
494
495 In Emacs 19.30:
496 A value of nil will make highlighting happen immediately \(this may slow
497 down your Emacs if running on a slow system). Any non-nil value will
498 delay highlighting for the time specified by post-command-idle-delay.
499
500 In Emacs 19.31 and above:
501 A value of nil will make highlighting happen immediately \(this may slow
502 down your Emacs if running on a slow system). If not nil, the value
503 should be a number \(possible a floating point number if your Emacs
504 support floating point numbers). The number is the delay in seconds
505 before mic-paren performs highlighting.
506
507 If you change this variable when mic-paren is active you have to
508 re-activate \(with M-x paren-activate) mic-paren for the change to take
509 effect."
510 :type '(choice (number :tag "Delay time")
511 (const :tag "No delay" nil))
512 :group 'mic-paren-matching)
513
514 (defcustom paren-dont-touch-blink nil
515 "*Non-nil means not to change the value of `blink-matching-paren'.
516 This takes effect when mic-paren is activated or deactivated. If nil
517 mic-paren turns of blinking when activated and turns on blinking when
518 deactivated."
519 :type 'boolean
520 :group 'mic-paren-matching)
521
522 (defcustom paren-dont-load-timer (not (string-match "XEmacs\\|Lucid"
523 emacs-version))
524 "*Non-nil inhibits loading `timer'.
525
526 \(I have no idea why Emacs user ever want to set this to non-nil but I hate
527 packages which loads/activates stuff I don't want to use so I provide this
528 way to prevent the loading if someone doesn't want timers to be loaded.)"
529 :type 'boolean
530 :group 'mic-paren-matching)
531
532 (defcustom paren-bind-modified-sexp-functions t
533 "*Automatic binding of the new sexp-functions to the old bindings.
534 If non nil mic-paren checks at load-time the keybindings for the functions
535 `forward-sexp' and `backward-sexp' and binds the modified sexp-functions
536 `paren-forward-sexp' and `paren-backward-sexp' to exactly these
537 bindings if and only if matching quoted/escaped parens is turned on by
538 `paren-toggle-matching-quoted-paren'. These new bindings are done only
539 in a buffer-local keymap, therefore if you activate the quoted matching
540 only in some modes from within a hook only in these buffers the new
541 bindings are active and in all other not.
542
543 If you deactivate the quoted matching feature by
544 `paren-toggle-matching-quoted-paren' then `forward-sexp' and
545 `backward-sexp' will be bound again to their original key-bindings!"
546 :type 'boolean
547 :group 'mic-paren-matching)
548
549 ;;; ------------------------------
550 ;;; Faces
551 ;;; ------------------------------
552
553 (defface paren-face-match
554 '((((class color)) (:background "turquoise"))
555 (t (:background "gray")))
556 "Mic-paren mode face used for a matching paren."
557 :group 'faces
558 :group 'mic-paren-matching)
559
560 (defface paren-face-mismatch
561 '((((class color)) (:foreground "white" :background "purple"))
562 (t (:reverse-video t)))
563 "Mic-paren mode face used for a mismatching paren."
564 :group 'faces
565 :group 'mic-paren-matching)
566
567 (defface paren-face-no-match
568 '((((class color)) (:foreground "black" :background "yellow"))
569 (t (:reverse-video t)))
570 "Mic-paren mode face used for an unmatched paren."
571 :group 'faces
572 :group 'mic-paren-matching)
573
574 (defcustom paren-match-face 'paren-face-match
575 "*Face to use for showing the matching parenthesis."
576 :type 'face
577 :group 'mic-paren-matching)
578
579 (defcustom paren-mismatch-face 'paren-face-mismatch
580 "*Face to use when highlighting a mismatched parenthesis."
581 :type 'face
582 :group 'mic-paren-matching)
583
584 (defcustom paren-no-match-face 'paren-face-no-match
585 "*Face to use when highlighting an unmatched parenthesis."
586 :type 'face
587 :group 'mic-paren-matching)
588
589 ;;; End of User Options
590 ;;; ======================================================================
591
592 ;;; Below there are only variables and options which either should be not
593 ;;; set directly but with toggle-functions or pure internal variables.
594
595 (defvar paren-match-quoted-paren nil
596 "*Non-nil enables matching properly quoted (or escaped) parens.
597 E.g., \"\\\(x-3y + z = 7\\\)\"\) in a TeX file. GNU Emacs can not match
598 quoted parens, so we must temporally deactivate the quoting until emacs
599 has done its sexp-parsing. Therefore emacs itself does not \(can not!\)
600 take into consideration if either both matched parens are quoted or none.
601 But nevertheless we do this! Only symmetric balanced parens are matched;
602 either both matching parens must be quoted or none, otherwise they we will
603 be highlighted as mismatched.
604
605 This package offers also two slightly modified versions of sexp traversal
606 functions: `paren-forward-sexp' and `paren-backward-sexp'. These versions
607 can also jump to escaped/quoted parens.
608
609 If this variable is not nil and `paren-bind-modified-sexp-functions' is
610 set to non nil, then `paren-toggle-matching-quoted-paren' will also toggle
611 the original binding of `forward-sexp' and `backward-sexp' between the
612 original functions and the modified equivalents.
613
614 Do NOT set this variable directly but use
615 `paren-toggle-matching-quoted-paren' to activate/deactivate/toggle this
616 feature! The best method is to do this in a mode hook, e.g.:
617 \(add-hook \'LaTeX-mode-hook
618 \(function \(lambda \(\)
619 \(paren-toggle-matching-quoted-paren 1\)\)\)\)")
620
621 (make-variable-buffer-local 'paren-match-quoted-paren)
622
623 (defvar paren-match-paired-delimiter nil
624 "*Non-nil enables matching of characters with syntax \"$\".
625 E.g., in LaTeX \"$...$\" is equivalent to \"\\(...\\)\".
626 Unlike to parens quoted/escaped paired delimiter will never match.
627
628 Do NOT set this variable directly but use
629 `paren-toggle-matching-paired-delimiter' to activate/deactivate/toggle
630 this feature! The best method is to do this in a mode hook, e.g.:
631 \(add-hook \'LaTeX-mode-hook
632 \(function \(lambda \(\)
633 \(paren-toggle-matching-paired-delimiter 1\)\)\)\)")
634
635 (make-variable-buffer-local 'paren-match-paired-delimiter)
636
637 (defvar paren-open-paren-context-backward nil
638 "*Controls which context of the matching open paren will be displayed.
639 This takes effect if the matching open paren is offscreen or
640 `paren-display-message' is `always' (see `paren-display-message')
641 and the matching open paren has no previous text in the same line.
642 Possible values:
643 nil -- Contents of the **next** not empty and not whitespace-line will be
644 displayed. This value is useful for example in functional programming
645 languages like (emacs)lisp.
646 not-nil -- Contents of the first **previous** not empty and not only
647 whitespace-line will be displayed. This value is useful for example in
648 procedural programming languages like C, C++, Java etc.
649
650 Lets take a look at a short example:
651 In languages like C++ we often have situations like
652 if \(i > VALUE\)
653 \{
654 // some code
655 \}
656 With a value non nil the displayed opening-brace context would be
657 \"if \(i > VALUE\)^J\{\" but with nil it would be \"\{^J // some code\"
658 which would be in C++ lesser useful as the non nil version.
659 \(The ^J stands for a newline in the buffer\).
660
661 Do NOT set this variable directly but use `paren-toggle-open-paren-context'
662 to change the value of this option! The best method is to do this in a
663 mode hook, e.g.:
664 \(add-hook \'c-common-mode-hook
665 \(function \(lambda \(\)
666 \(paren-toggle-open-paren-context 1\)\)\)\)")
667
668 (make-variable-buffer-local 'paren-open-paren-context-backward)
669
670 (defconst mic-paren-original-keybinding-of-sexp-functions
671 (list (car (where-is-internal 'forward-sexp))
672 (car (where-is-internal 'backward-sexp))))
673
674 ;;; Compatibility.
675 ;;; Try to make mic-paren work in different Emacs flavours.
676
677 ;; XEmacs compatibility (mainly by Steven L Baur <steve@xemacs.org>).
678 (eval-and-compile
679 (if (string-match "\\(Lucid\\|XEmacs\\)" emacs-version)
680 (progn
681 (fset 'mic-make-overlay 'make-extent)
682 (fset 'mic-delete-overlay 'delete-extent)
683 (fset 'mic-overlay-put 'set-extent-property)
684 (fset 'mic-cancel-timer 'delete-itimer)
685 (fset 'mic-run-with-idle-timer 'start-itimer))
686 (fset 'mic-make-overlay 'make-overlay)
687 (fset 'mic-delete-overlay 'delete-overlay)
688 (fset 'mic-overlay-put 'overlay-put)
689 (fset 'mic-cancel-timer 'cancel-timer)
690 (fset 'mic-run-with-idle-timer 'run-with-idle-timer)))
691
692 (defun paren-clamp-string-maybe (str)
693 "Remove the middle of STR if it exceeds `paren-max-message-length'.
694 However, if STR is `nil' or `paren-max-message-length' is zero,
695 simply return STR."
696 (if (or (not str) (zerop paren-max-message-length))
697 str
698 (let ((len (string-width str)))
699 (if (<= len paren-max-message-length)
700 str
701 (let* ((sep "[...]")
702 (cut (ash (- paren-max-message-length
703 (string-width sep))
704 -1)))
705 (concat (substring str 0 cut)
706 sep
707 (substring str (- len cut))))))))
708
709 (eval-when-compile
710 (defmacro define-mic-paren-nolog-message (yes no)
711 `(defun mic-paren-nolog-message (&rest args)
712 "Work like `message' but without logging.
713 See variable `paren-max-message-length'."
714 (let ((msg (paren-clamp-string-maybe
715 (cond ((or (null args)
716 (null (car args)))
717 nil)
718 ((null (cdr args))
719 (car args))
720 (t
721 (apply 'format args))))))
722 (if msg ,yes ,no)
723 msg))))
724
725 (eval-and-compile
726 (if (and (fboundp 'display-message)
727 (fboundp 'clear-message))
728 ;; Some GNU Emacs versions need the `eval' so as to avoid saying:
729 ;; > the following functions are not known to be defined:
730 ;; > display-message, clear-message
731 (eval '(define-mic-paren-nolog-message
732 (display-message 'no-log msg)
733 (clear-message 'no-log)))
734 (define-mic-paren-nolog-message
735 (let (message-log-max) (message "%s" msg))
736 (message nil))))
737
738 ;;; ======================================================================
739 ;;; Pure Internal variables
740
741 (defvar mic-paren-overlays (vector (mic-make-overlay 1 1)
742 (mic-make-overlay 1 1)
743 (mic-make-overlay 1 1))
744 "Vector of of the form [BACKW POINT FOREW].
745
746 BACKW: Overlay for the open-paren which matches the close-paren
747 before point. When in sexp-mode this is the overlay for
748 the expression before point.
749
750 POINT: Overlay for the close-paren before point.
751 This is not used when is sexp-mode.
752
753 FOREW: Overlay for the close-paren which matches the open-paren
754 after point. When in sexp-mode this is the overlay for
755 the expression after point.")
756
757 (defvar mic-paren-idle-timer nil
758 "Idle-timer.
759 Used only in Emacs 19.31 and above (and if paren-delay is nil).")
760
761 (defvar mic-paren-previous-location [nil nil nil]
762 "Where point was the last time mic-paren performed some action.
763 This is is a vector of the form [POINT BUFFER WINDOW].")
764
765 ;;; ======================================================================
766 ;;; User Functions
767
768 ;;;###autoload
769 (defun paren-activate ()
770 "Activate mic-paren parenthesis highlighting.
771 Note: This also deactivates the paren.el
772 and stig-paren.el packages if they are active!
773
774 The following options are available via the customize-feature:
775 `paren-priority'
776 `paren-overlay-priority'
777 `paren-sexp-mode'
778 `paren-highlight-at-point'
779 `paren-highlight-offscreen'
780 `paren-display-message'
781 `paren-message-linefeed-display'
782 `paren-message-no-match'
783 `paren-message-show-linenumber'
784 `paren-message-truncate-lines'
785 `paren-ding-unmatched'
786 `paren-delay'
787 `paren-dont-touch-blink'
788 `paren-match-face'
789 `paren-mismatch-face'
790 `paren-no-match-face'
791 `paren-bind-modified-sexp-functions'
792
793 The following options are settable via toggling functions \(look at the
794 documentation of these options for the names of these functions\):
795 `paren-match-quoted-paren'
796 `paren-match-paired-delimiter'
797 `paren-open-paren-context-backward'"
798 (interactive)
799 ;; Deactivate mic-paren.el (to remove redundant hooks).
800 (paren-deactivate)
801 ;; Deactivate paren.el if loaded.
802 (when (boundp 'post-command-idle-hook)
803 (remove-hook 'post-command-idle-hook 'show-paren-command-hook))
804 (remove-hook 'post-command-hook 'show-paren-command-hook)
805 (and (boundp 'show-paren-overlay)
806 show-paren-overlay
807 (mic-delete-overlay show-paren-overlay))
808 (and (boundp 'show-paren-overlay-1)
809 show-paren-overlay-1
810 (mic-delete-overlay show-paren-overlay-1))
811 ;; Deactivate stig-paren.el if loaded.
812 (when (boundp 'post-command-idle-hook)
813 (remove-hook 'post-command-idle-hook 'stig-paren-command-hook))
814 (remove-hook 'post-command-hook 'stig-paren-command-hook)
815 (remove-hook 'post-command-hook 'stig-paren-safe-command-hook)
816 (remove-hook 'pre-command-hook 'stig-paren-delete-overlay)
817 ;; Deactivate Emacs standard parenthesis blinking.
818 (or paren-dont-touch-blink
819 (setq blink-matching-paren nil))
820
821 (cond(
822 ;; If timers are available use them
823 ;; (Emacs 19.31 and above).
824 (featurep 'timer)
825 (if (numberp paren-delay)
826 (setq mic-paren-idle-timer
827 (mic-run-with-idle-timer paren-delay t
828 'mic-paren-command-idle-hook))
829 (add-hook 'post-command-hook 'mic-paren-command-hook)))
830 ;; If the idle hook exists assume it is functioning and use it
831 ;; (Emacs 19.30).
832 ((and (boundp 'post-command-idle-hook)
833 (boundp 'post-command-idle-delay))
834 (if paren-delay
835 (add-hook 'post-command-idle-hook 'mic-paren-command-idle-hook)
836 (add-hook 'post-command-hook 'mic-paren-command-hook)))
837 ;; Check if we (at least) have `post-comand-hook', and use it
838 ;; (Emacs 19.29 and below).
839 ((boundp 'post-command-hook)
840 (add-hook 'post-command-hook 'mic-paren-command-hook))
841 ;; Not possible to install mic-paren hooks
842 (t (error "Cannot activate mic-paren in this Emacs version")))
843 ;; We want matching quoted parens in the minibuffer to ease inserting
844 ;; paren-expressions in a regexp.
845 (add-hook 'minibuffer-setup-hook
846 'mic-paren-minibuffer-setup-hook)
847 (add-hook 'minibuffer-exit-hook
848 'mic-paren-minibuffer-exit-hook))
849
850 ;;;###autoload
851 (defun paren-deactivate ()
852 "Deactivate mic-paren parenthesis highlighting."
853 (interactive)
854 ;; Deactivate (don't bother to check where/if mic-paren is acivte, just
855 ;; delete all possible hooks and timers).
856 (when (boundp 'post-command-idle-hook)
857 (remove-hook 'post-command-idle-hook 'mic-paren-command-idle-hook))
858 (when mic-paren-idle-timer
859 (mic-cancel-timer mic-paren-idle-timer))
860 (remove-hook 'post-command-hook 'mic-paren-command-hook)
861 (remove-hook 'minibuffer-setup-hook
862 'mic-paren-minibuffer-setup-hook)
863 (remove-hook 'minibuffer-exit-hook
864 'mic-paren-minibuffer-exit-hook)
865 ;; Remove any old highlight.
866 (mic-delete-overlay (aref mic-paren-overlays 0))
867 (mic-delete-overlay (aref mic-paren-overlays 1))
868 (mic-delete-overlay (aref mic-paren-overlays 2))
869
870 ;; Reactivate Emacs standard parenthesis blinking.
871 (or paren-dont-touch-blink
872 (setq blink-matching-paren t)))
873
874 ;;;###autoload
875 (defun paren-toggle-matching-paired-delimiter (arg &optional no-message)
876 "Toggle matching paired delimiter.
877 Force on with positive ARG. Use this in mode hooks to activate or
878 deactivate paired delimiter matching. If optional second argument
879 NO-MESSAGE is non-nil then don't display a message about the
880 current activation state of the paired-delimiter-matching feature."
881 (interactive "P")
882 (setq paren-match-paired-delimiter (if (numberp arg)
883 (> arg 0)
884 (not paren-match-paired-delimiter)))
885 (unless no-message
886 (message "Matching paired delimiter is %s"
887 (if paren-match-paired-delimiter
888 "ON"
889 "OFF"))))
890
891 ;;;###autoload
892 (defun paren-toggle-matching-quoted-paren (arg &optional no-message)
893 "Toggle matching quoted parens.
894 Force on with positive ARG. Use this in mode hooks to activate or
895 deactivate quoted paren matching. If optional second argument
896 NO-MESSAGE is non-nil then don't display a message about the
897 current activation state of the quoted-paren-matching feature."
898 (interactive "P")
899 (setq paren-match-quoted-paren (if (numberp arg)
900 (> arg 0)
901 (not paren-match-quoted-paren)))
902 ;; If matching quoted parens is active now bind the original binding
903 ;; of forward-sexp and backward-sexp to the modified versions
904 ;; `paren-forward-sexp' and `paren-backward-sexp'. If not, bind
905 ;; it back to the original `forward-sexp' and `backward-sexp'.
906 (let ((f (car mic-paren-original-keybinding-of-sexp-functions))
907 (b (cadr mic-paren-original-keybinding-of-sexp-functions))
908 (funs (if paren-match-quoted-paren
909 '(paren-forward-sexp . paren-backward-sexp)
910 '(forward-sexp . backward-sexp))))
911 (when (and paren-bind-modified-sexp-functions b f)
912 (local-set-key f (car funs))
913 (local-set-key b (cdr funs))))
914 (unless no-message
915 (message "Matching quoted parens is %s"
916 (if paren-match-quoted-paren
917 "ON"
918 "OFF"))))
919
920 ;;;###autoload
921 (defun paren-toggle-open-paren-context (arg)
922 "Toggle whether or not to determine context of the matching open-paren.
923 Force backward context with positive ARG. Use this in mode-hooks.
924 See `paren-open-paren-context-backward'."
925 (interactive "P")
926 (setq paren-open-paren-context-backward
927 (if (numberp arg)
928 (> arg 0)
929 (not paren-open-paren-context-backward))))
930
931 ;;;###autoload
932 (defun paren-forward-sexp (&optional arg)
933 "Act like `forward-sexp' but also handle quoted parens.
934 See `paren-match-quoted-paren'."
935 (interactive "p")
936 (or arg (setq arg 1))
937 (let* ((uncharquote-diff (if (< arg 0) 2 1))
938 (match-check-diff (if (< arg 0) 1 2))
939 (charquote (mic-paren-uncharquote (- (point) uncharquote-diff)))
940 match-pos mismatch)
941 ;; We must encapsulate this in condition-case so we regain control
942 ;; after error and we can undo our unquoting if any done before!
943 (condition-case ()
944 (setq match-pos (scan-sexps (point) arg))
945 (error nil))
946 (mic-paren-recharquote charquote)
947 (if (not match-pos)
948 (buffer-end arg)
949 (setq mismatch (mic-paren-fcq-mismatch (- match-pos match-check-diff)
950 charquote))
951 (if mismatch
952 (forward-sexp arg)
953 (goto-char match-pos)))))
954
955 ;;;###autoload
956 (defun paren-backward-sexp (&optional arg)
957 "Act like `backward-sexp' but also match quoted parens.
958 See `paren-match-quoted-paren'."
959 (interactive "p")
960 (or arg (setq arg 1))
961 (paren-forward-sexp (- arg)))
962
963 ;;; ======================================================================
964 ;;; Internal functions
965
966 (defun mic-paren-command-idle-hook ()
967 (condition-case paren-error
968 (mic-paren-highlight)
969 (error
970 (unless (window-minibuffer-p (selected-window))
971 (message "mic-paren caught error (please report): %s"
972 paren-error)))))
973
974 (defun mic-paren-command-hook ()
975 (or executing-kbd-macro
976 ;; NB: This might cause trouble since the function is unreliable.
977 (input-pending-p)
978 (mic-paren-command-idle-hook)))
979
980 (defun mic-paren-minibuffer-setup-hook ()
981 (paren-toggle-matching-quoted-paren 1 t))
982
983 (defun mic-paren-minibuffer-exit-hook ()
984 (paren-toggle-matching-quoted-paren -1 t))
985
986 (defun mic-paren-fcq-mismatch (pos charquote)
987 (not (zerop (logxor (if (mic-paren-is-following-char-quoted pos) 1 0)
988 (if charquote 1 0)))))
989
990 (defun mic-paren-highlight ()
991 "Do everything: highlighting, dinging, messages, cleaning-up.
992 This is the main function of mic-paren."
993 ;; Remove any old highlighting.
994 (mic-delete-overlay (aref mic-paren-overlays 0))
995 (mic-delete-overlay (aref mic-paren-overlays 1))
996 (mic-delete-overlay (aref mic-paren-overlays 2))
997
998 (let ((loc mic-paren-previous-location)
999 charquote two opos matched-paren mismatch face visible)
1000
1001 (flet ((highlight-p
1002 (pos prio which)
1003 (let ((fcq (mic-paren-is-following-char-quoted pos))
1004 (right-prio (eq prio paren-priority))
1005 (get-c-0 (if which 'preceding-char 'following-char))
1006 (get-c-1 (if which 'following-char 'preceding-char)))
1007 (or (and (eq (char-syntax (funcall get-c-0))
1008 (if which ?\) ?\())
1009 (not (and (eq (char-syntax (funcall get-c-1))
1010 (if which ?\( ?\)))
1011 right-prio))
1012 (or paren-match-quoted-paren
1013 (not fcq)))
1014 (and paren-match-paired-delimiter
1015 (eq (char-syntax (funcall get-c-0)) ?\$)
1016 (not (and (eq (char-syntax (funcall get-c-1)) ?\$)
1017 right-prio))
1018 (not fcq)))))
1019
1020 (comment-style
1021 ()
1022 (or (get major-mode 'mic-paren-comment-style)
1023 (put major-mode 'mic-paren-comment-style
1024 ;; Tested (lightly) w/ SML, Modula-2, Pascal.
1025 (flet ((sub (str pos) (condition-case ()
1026 (aref str (if (> 0 pos)
1027 (+ (length str)
1028 pos)
1029 pos))
1030 (error 0))))
1031 (if (string= "()" (string (sub comment-start 0)
1032 (sub comment-end -1)))
1033 'sexp
1034 'normal)))))
1035
1036 (sexp-ish-comment-edge
1037 (p mult)
1038 (and (eq 'sexp (comment-style))
1039 (if (> 0 mult)
1040 (prog1 (nth 8 (syntax-ppss (1- p)))
1041 (forward-char 1))
1042 ;; hmmm
1043 (save-match-data
1044 (looking-at (regexp-quote comment-start))))))
1045
1046 (find-other-paren
1047 (forwardp)
1048 (let ((mult (if forwardp 1 -1)))
1049 ;; Find the position of the other paren.
1050 (save-excursion
1051 (save-restriction
1052 (when blink-matching-paren-distance
1053 (let ((lim (+ (point) (* blink-matching-paren-distance
1054 mult))))
1055 (narrow-to-region (if forwardp
1056 (point-min)
1057 (max lim (point-min)))
1058 (if forwardp
1059 (min lim (point-max))
1060 (point-max)))))
1061 (condition-case ()
1062 (setq opos (let ((p (point)))
1063 (if (not (sexp-ish-comment-edge p mult))
1064 (scan-sexps p mult)
1065 (forward-comment mult)
1066 (point))))
1067 (error nil))))
1068 ;; We must call matching-paren because `scan-sexps' doesn't
1069 ;; care about the kind of paren (e.g., matches '( and '}).
1070 ;; However, `matching-paren' only returns the character
1071 ;; displaying the matching paren in buffer's syntax-table
1072 ;; (regardless of the buffer's current contents!). Below we
1073 ;; compare the results of `scan-sexps' and `matching-paren'
1074 ;; and if different we display a mismatch.
1075 (let ((c (funcall (if forwardp
1076 'following-char
1077 'preceding-char))))
1078 (setq matched-paren (matching-paren c))
1079 ;; matching-paren can only handle chars with syntax ) or (.
1080 (when (eq (char-syntax c) ?\$)
1081 (setq matched-paren c)))
1082 ;; If we have changed the syntax of the escape or quote-char
1083 ;; we must undo this and we can do this first now.
1084 (mic-paren-recharquote charquote)
1085 opos))
1086
1087 (nov
1088 (place b e face)
1089 (let ((ov (mic-make-overlay b e)))
1090 (mic-overlay-put ov 'face face)
1091 (mic-overlay-put ov 'priority paren-overlay-priority)
1092 (aset mic-paren-overlays
1093 (cdr (assq place '((backw . 0)
1094 (point . 1)
1095 (forew . 2))))
1096 ov)))
1097
1098 (new-location-p
1099 ()
1100 (not (and (eq (point) (aref loc 0))
1101 (eq (current-buffer) (aref loc 1))
1102 (eq (selected-window) (aref loc 2)))))
1103
1104 (ding-maybe
1105 (ok)
1106 (and ok paren-ding-unmatched
1107 (new-location-p)
1108 (ding)))
1109
1110 (sorry
1111 (place b e)
1112 ;; Highlight unmatched paren.
1113 (nov place b e paren-no-match-face)
1114 ;; Print no-match message.
1115 (and paren-message-no-match
1116 (not (window-minibuffer-p (selected-window)))
1117 (not isearch-mode)
1118 (new-location-p)
1119 (mic-paren-nolog-message "No %sing parenthesis found"
1120 (if (eq 'backw place)
1121 "open"
1122 "clos")))
1123 (ding-maybe paren-message-no-match))
1124
1125 (set-mismatch/face/visible
1126 (c-at ofs)
1127 (setq mismatch (or (not matched-paren)
1128 (/= matched-paren (funcall c-at opos))
1129 (mic-paren-fcq-mismatch (+ opos ofs) charquote))
1130 face (if mismatch
1131 paren-mismatch-face
1132 paren-match-face)
1133 visible (when (pos-visible-in-window-p opos)
1134 (save-excursion
1135 (goto-char opos)
1136 (let ((hrel (- (current-column)
1137 (window-hscroll))))
1138 (and (< -1 hrel)
1139 (> (window-width) hrel)))))))
1140
1141 (sexp-mode-p
1142 ()
1143 (case paren-sexp-mode
1144 (match (not mismatch))
1145 (mismatch mismatch)
1146 ((nil t) paren-sexp-mode)))
1147
1148 (finish
1149 (get-message)
1150 ;; Print messages if match is offscreen.
1151 (and (not (eq paren-display-message 'never))
1152 (or (not visible) (eq paren-display-message 'always))
1153 (not (window-minibuffer-p (selected-window)))
1154 (not isearch-mode)
1155 (new-location-p)
1156 (let ((message-truncate-lines paren-message-truncate-lines))
1157 (mic-paren-nolog-message
1158 "%s %s"
1159 (if mismatch "MISMATCH:" "Matches")
1160 (funcall get-message opos))))
1161 ;; Ding if mismatch.
1162 (ding-maybe mismatch)))
1163
1164 ;; Handle backward highlighting.
1165 (when (highlight-p (- (point) 2) 'open t)
1166 (setq charquote (mic-paren-uncharquote (- (point) 2)))
1167 (if (not (find-other-paren nil))
1168 (sorry 'backw (point) (1- (point)))
1169 (set-mismatch/face/visible 'char-after -1)
1170 (when (or visible paren-highlight-offscreen paren-sexp-mode)
1171 (let ((sexp-mismatch (sexp-mode-p)))
1172 (nov 'backw opos (if sexp-mismatch
1173 (point)
1174 (1+ opos))
1175 face)
1176 (when (and (not sexp-mismatch)
1177 paren-highlight-at-point)
1178 (nov 'point (1- (point)) (point) face))))
1179 (finish 'mic-paren-get-matching-open-text)))
1180
1181 ;; Handle forward highlighting.
1182 (when (highlight-p (1- (point)) 'close nil)
1183 (setq charquote (mic-paren-uncharquote (1- (point))))
1184 (if (not (find-other-paren t))
1185 (sorry 'forew (point) (1+ (point)))
1186 (set-mismatch/face/visible 'char-before -2)
1187 (when (or visible paren-highlight-offscreen paren-sexp-mode)
1188 (nov 'forew (if (sexp-mode-p)
1189 (point)
1190 (1- opos))
1191 opos face))
1192 (finish 'mic-paren-get-matching-close-text))))
1193
1194 ;; Store the point's position.
1195 (unless (window-minibuffer-p (selected-window))
1196 (aset loc 0 (point))
1197 (aset loc 1 (current-buffer))
1198 (aset loc 2 (selected-window)))))
1199
1200 (defun mic-paren-get-matching-open-text (open)
1201 "Return a string with the context around OPEN-paren.
1202 If there's stuff on this line preceding the paren, then
1203 display text from beginning of line to paren.
1204
1205 If, however, the paren is at the beginning of a line (means only
1206 whitespace before the paren), then skip whitespace forward and
1207 display text from paren to end of the next line containing
1208 non-space text. But if `paren-open-paren-context-backward' is
1209 non-nil, then skip whitespaces backward and display text from
1210 beginning of previous line to paren."
1211 (let* ((loc (if (eq paren-message-show-linenumber 'sexp)
1212 (point) (point-min)))
1213 (str (save-excursion
1214 (goto-char open)
1215 (if (save-excursion
1216 (skip-chars-backward " \t")
1217 (not (bolp)))
1218 (progn
1219 (beginning-of-line)
1220 (format "%s... [%s%d-]"
1221 (buffer-substring (point) (1+ open))
1222 (if (eq paren-message-show-linenumber 'sexp)
1223 "-" "")
1224 (count-lines loc open)))
1225 (let (paren-context-string)
1226 (if (not paren-open-paren-context-backward)
1227 (progn
1228 (forward-char 1)
1229 (skip-chars-forward "\n \t")
1230 (end-of-line)
1231 (setq paren-context-string
1232 (buffer-substring open (point))))
1233 (skip-chars-backward "\n \t")
1234 (beginning-of-line)
1235 (setq paren-context-string
1236 (buffer-substring (point) (1+ open))))
1237 (format "%s [%s%d]"
1238 paren-context-string
1239 (if (eq paren-message-show-linenumber 'sexp)
1240 "-" "")
1241 (count-lines loc open)))))))
1242 (while (string-match "[\n]" str)
1243 (setq str (replace-match paren-message-linefeed-display nil t str)))
1244 str))
1245
1246 (defun mic-paren-get-matching-close-text (close)
1247 "Return a string with the context around CLOSE-paren.
1248 The whole line up until the close-paren with \"...\"
1249 appended if there is more text after the close-paren."
1250 (let* ((loc (if (eq paren-message-show-linenumber 'sexp)
1251 (point) (point-min)))
1252 (str (save-excursion
1253 (goto-char close)
1254 (forward-char -1)
1255 (skip-chars-backward "\n \t")
1256 (beginning-of-line)
1257 (format "%s%s [%s%d]"
1258 (buffer-substring (point) close)
1259 (progn
1260 (goto-char close)
1261 (if (looking-at "[ \t]*$")
1262 ""
1263 "..."))
1264 (if (eq paren-message-show-linenumber 'sexp)
1265 "+" "")
1266 (count-lines loc close)))))
1267 (while (string-match "[\n]" str)
1268 (setq str (replace-match paren-message-linefeed-display nil t str)))
1269 str))
1270
1271 (defun mic-paren-is-following-char-quoted (pos)
1272 "Return t if character at POS escapes or quotes the following char."
1273 (let ((n 0))
1274 (while (and (<= (point-min) pos)
1275 (memq (char-syntax (char-after pos)) '(?\\ ?/)))
1276 (setq n (1+ n)
1277 pos (1- pos)))
1278 (not (zerop (% n 2)))))
1279
1280 (defun mic-paren-uncharquote (pos)
1281 "Modify syntax of character at POS, maybe.
1282 If the syntax of character C at POS is escape or quote and if the
1283 character is not escaped or quoted itself then modify its syntax to
1284 punctuation and return the list (C SYNTAX-STRING-OF-C); otherwise nil."
1285 (when (and (<= (point-min) pos)
1286 paren-match-quoted-paren
1287 (mic-paren-is-following-char-quoted pos))
1288 (let ((c (char-after pos)))
1289 (modify-syntax-entry c ".")
1290 (list c (char-to-string (char-syntax c))))))
1291
1292 (defun mic-paren-recharquote (charquote)
1293 "Modify syntax entry according to CHARQUOTE.
1294 If CHARQUOTE is nil, do nothing. Otherwise, it
1295 should be a list of the form (CHAR SYNTAX-STRING)."
1296 (when charquote
1297 (apply 'modify-syntax-entry charquote)))
1298
1299 ;;; ======================================================================
1300 ;;; Initialization when loading
1301
1302 ;;; Try to load the timer feature if it's not already loaded.
1303 (or paren-dont-load-timer
1304 (featurep 'timer)
1305 (condition-case ()
1306 (require 'timer)
1307 (error nil)))
1308
1309 (provide 'mic-paren)
1310 (provide 'paren)
1311
1312 ;;; Local variables:
1313 ;;; coding: utf-8
1314 ;;; End:
1315 ;;; mic-paren.el ends here