Bump version to 24.4.91
[emacs.git] / lisp / progmodes / prog-mode.el
blobe364c8335aad5fe3b89cada96c1658ccb4c003ad
1 ;;; prog-mode.el --- Generic major mode for programming -*- lexical-binding: t -*-
3 ;; Copyright (C) 2013-2015 Free Software Foundation, Inc.
5 ;; Maintainer: emacs-devel@gnu.org
6 ;; Keywords: internal
7 ;; Package: emacs
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; This major mode is mostly intended as a parent of other programming
27 ;; modes. All major modes for programming languages should derive from this
28 ;; mode so that users can put generic customization on prog-mode-hook.
30 ;;; Code:
32 (eval-when-compile (require 'cl-lib))
34 (defgroup prog-mode nil
35 "Generic programming mode, from which others derive."
36 :group 'languages)
38 (defvar prog-mode-map
39 (let ((map (make-sparse-keymap)))
40 (define-key map [?\C-\M-q] 'prog-indent-sexp)
41 map)
42 "Keymap used for programming modes.")
44 (defun prog-indent-sexp (&optional defun)
45 "Indent the expression after point.
46 When interactively called with prefix, indent the enclosing defun
47 instead."
48 (interactive "P")
49 (save-excursion
50 (when defun
51 (end-of-line)
52 (beginning-of-defun))
53 (let ((start (point))
54 (end (progn (forward-sexp 1) (point))))
55 (indent-region start end nil))))
57 (defvar-local prettify-symbols-alist nil
58 "Alist of symbol prettifications.
59 Each element looks like (SYMBOL . CHARACTER), where the symbol
60 matching SYMBOL (a string, not a regexp) will be shown as
61 CHARACTER instead.")
63 (defun prettify-symbols--compose-symbol (alist)
64 "Compose a sequence of characters into a symbol.
65 Regexp match data 0 points to the chars."
66 ;; Check that the chars should really be composed into a symbol.
67 (let* ((start (match-beginning 0))
68 (end (match-end 0))
69 (syntaxes-beg (if (memq (char-syntax (char-after start)) '(?w ?_))
70 '(?w ?_) '(?. ?\\)))
71 (syntaxes-end (if (memq (char-syntax (char-before end)) '(?w ?_))
72 '(?w ?_) '(?. ?\\)))
73 match)
74 (if (or (memq (char-syntax (or (char-before start) ?\s)) syntaxes-beg)
75 (memq (char-syntax (or (char-after end) ?\s)) syntaxes-end)
76 ;; syntax-ppss could modify the match data (bug#14595)
77 (progn (setq match (match-string 0)) (nth 8 (syntax-ppss))))
78 ;; No composition for you. Let's actually remove any composition
79 ;; we may have added earlier and which is now incorrect.
80 (remove-text-properties start end '(composition))
81 ;; That's a symbol alright, so add the composition.
82 (compose-region start end (cdr (assoc match alist)))))
83 ;; Return nil because we're not adding any face property.
84 nil)
86 (defun prettify-symbols--make-keywords ()
87 (if prettify-symbols-alist
88 `((,(regexp-opt (mapcar 'car prettify-symbols-alist) t)
89 (0 (prettify-symbols--compose-symbol ',prettify-symbols-alist))))
90 nil))
92 (defvar-local prettify-symbols--keywords nil)
94 ;;;###autoload
95 (define-minor-mode prettify-symbols-mode
96 "Toggle Prettify Symbols mode.
97 With a prefix argument ARG, enable Prettify Symbols mode if ARG is
98 positive, and disable it otherwise. If called from Lisp, enable
99 the mode if ARG is omitted or nil.
101 When Prettify Symbols mode and font-locking are enabled, symbols are
102 prettified (displayed as composed characters) according to the rules
103 in `prettify-symbols-alist' (which see), which are locally defined
104 by major modes supporting prettifying. To add further customizations
105 for a given major mode, you can modify `prettify-symbols-alist' thus:
107 (add-hook 'emacs-lisp-mode-hook
108 (lambda ()
109 (push '(\"<=\" . ?≤) prettify-symbols-alist)))
111 You can enable this mode locally in desired buffers, or use
112 `global-prettify-symbols-mode' to enable it for all modes that
113 support it."
114 :init-value nil
115 (if prettify-symbols-mode
116 ;; Turn on
117 (when (setq prettify-symbols--keywords (prettify-symbols--make-keywords))
118 (font-lock-add-keywords nil prettify-symbols--keywords)
119 (setq-local font-lock-extra-managed-props
120 (cons 'composition font-lock-extra-managed-props))
121 (font-lock-fontify-buffer))
122 ;; Turn off
123 (when prettify-symbols--keywords
124 (font-lock-remove-keywords nil prettify-symbols--keywords)
125 (setq prettify-symbols--keywords nil))
126 (when (memq 'composition font-lock-extra-managed-props)
127 (setq font-lock-extra-managed-props (delq 'composition
128 font-lock-extra-managed-props))
129 (with-silent-modifications
130 (remove-text-properties (point-min) (point-max) '(composition nil))))))
132 (defun turn-on-prettify-symbols-mode ()
133 (when (and (not prettify-symbols-mode)
134 (local-variable-p 'prettify-symbols-alist))
135 (prettify-symbols-mode 1)))
137 ;;;###autoload
138 (define-globalized-minor-mode global-prettify-symbols-mode
139 prettify-symbols-mode turn-on-prettify-symbols-mode)
141 ;;;###autoload
142 (define-derived-mode prog-mode fundamental-mode "Prog"
143 "Major mode for editing programming language source code."
144 (setq-local require-final-newline mode-require-final-newline)
145 (setq-local parse-sexp-ignore-comments t)
146 ;; Any programming language is always written left to right.
147 (setq bidi-paragraph-direction 'left-to-right))
149 (provide 'prog-mode)
151 ;;; prog-mode.el ends here