Commit | Line | Data |
---|---|---|
c5de26b4 | 1 | ;;; prog-mode.el --- Generic major mode for programming -*- lexical-binding: t -*- |
55577e7c | 2 | |
ba318903 | 3 | ;; Copyright (C) 2013-2014 Free Software Foundation, Inc. |
55577e7c | 4 | |
34dc21db | 5 | ;; Maintainer: emacs-devel@gnu.org |
55577e7c SM |
6 | ;; Keywords: internal |
7 | ;; Package: emacs | |
8 | ||
9 | ;; This file is part of GNU Emacs. | |
10 | ||
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. | |
15 | ||
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. | |
20 | ||
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/>. | |
23 | ||
24 | ;;; Commentary: | |
25 | ||
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. | |
29 | ||
30 | ;;; Code: | |
31 | ||
32 | (eval-when-compile (require 'cl-lib)) | |
33 | ||
34 | (defgroup prog-mode nil | |
35 | "Generic programming mode, from which others derive." | |
36 | :group 'languages) | |
37 | ||
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.") | |
43 | ||
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)))) | |
56 | ||
14dd22d2 JB |
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.") | |
62 | ||
63 | (defun prettify-symbols--compose-symbol (alist) | |
64 | "Compose a sequence of characters into a symbol. | |
55577e7c SM |
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 (if (eq (char-syntax (char-after start)) ?w) | |
f0ffb9b7 | 70 | '(?w ?_) '(?. ?\\))) |
f3d674df | 71 | match) |
f612933b JB |
72 | (if (or (memq (char-syntax (or (char-before start) ?\s)) syntaxes) |
73 | (memq (char-syntax (or (char-after end) ?\s)) syntaxes) | |
f3d674df JB |
74 | ;; syntax-ppss could modify the match data (bug#14595) |
75 | (progn (setq match (match-string 0)) (nth 8 (syntax-ppss)))) | |
55577e7c SM |
76 | ;; No composition for you. Let's actually remove any composition |
77 | ;; we may have added earlier and which is now incorrect. | |
78 | (remove-text-properties start end '(composition)) | |
79 | ;; That's a symbol alright, so add the composition. | |
f3d674df | 80 | (compose-region start end (cdr (assoc match alist))))) |
55577e7c SM |
81 | ;; Return nil because we're not adding any face property. |
82 | nil) | |
83 | ||
14dd22d2 JB |
84 | (defun prettify-symbols--make-keywords () |
85 | (if prettify-symbols-alist | |
86 | `((,(regexp-opt (mapcar 'car prettify-symbols-alist) t) | |
87 | (0 (prettify-symbols--compose-symbol ',prettify-symbols-alist)))) | |
88 | nil)) | |
89 | ||
90 | (defvar-local prettify-symbols--keywords nil) | |
91 | ||
01a43901 | 92 | ;;;###autoload |
14dd22d2 JB |
93 | (define-minor-mode prettify-symbols-mode |
94 | "Toggle Prettify Symbols mode. | |
95 | With a prefix argument ARG, enable Prettify Symbols mode if ARG is | |
96 | positive, and disable it otherwise. If called from Lisp, enable | |
97 | the mode if ARG is omitted or nil. | |
98 | ||
99 | When Prettify Symbols mode and font-locking are enabled, symbols are | |
100 | prettified (displayed as composed characters) according to the rules | |
101 | in `prettify-symbols-alist' (which see), which are locally defined | |
102 | by major modes supporting prettifying. To add further customizations | |
103 | for a given major mode, you can modify `prettify-symbols-alist' thus: | |
104 | ||
105 | (add-hook 'emacs-lisp-mode-hook | |
106 | (lambda () | |
107 | (push '(\"<=\" . ?≤) prettify-symbols-alist))) | |
108 | ||
109 | You can enable this mode locally in desired buffers, or use | |
110 | `global-prettify-symbols-mode' to enable it for all modes that | |
111 | support it." | |
112 | :init-value nil | |
113 | (if prettify-symbols-mode | |
114 | ;; Turn on | |
115 | (when (setq prettify-symbols--keywords (prettify-symbols--make-keywords)) | |
116 | (font-lock-add-keywords nil prettify-symbols--keywords) | |
117 | (setq-local font-lock-extra-managed-props | |
118 | (cons 'composition font-lock-extra-managed-props)) | |
119 | (font-lock-fontify-buffer)) | |
120 | ;; Turn off | |
121 | (when prettify-symbols--keywords | |
122 | (font-lock-remove-keywords nil prettify-symbols--keywords) | |
123 | (setq prettify-symbols--keywords nil)) | |
124 | (when (memq 'composition font-lock-extra-managed-props) | |
125 | (setq font-lock-extra-managed-props (delq 'composition | |
126 | font-lock-extra-managed-props)) | |
127 | (with-silent-modifications | |
128 | (remove-text-properties (point-min) (point-max) '(composition nil)))))) | |
129 | ||
130 | (defun turn-on-prettify-symbols-mode () | |
131 | (when (and (not prettify-symbols-mode) | |
132 | (local-variable-p 'prettify-symbols-alist)) | |
133 | (prettify-symbols-mode 1))) | |
134 | ||
01a43901 | 135 | ;;;###autoload |
14dd22d2 JB |
136 | (define-globalized-minor-mode global-prettify-symbols-mode |
137 | prettify-symbols-mode turn-on-prettify-symbols-mode) | |
55577e7c | 138 | |
01a43901 | 139 | ;;;###autoload |
55577e7c SM |
140 | (define-derived-mode prog-mode fundamental-mode "Prog" |
141 | "Major mode for editing programming language source code." | |
f612933b JB |
142 | (setq-local require-final-newline mode-require-final-newline) |
143 | (setq-local parse-sexp-ignore-comments t) | |
55577e7c SM |
144 | ;; Any programming language is always written left to right. |
145 | (setq bidi-paragraph-direction 'left-to-right)) | |
146 | ||
147 | (provide 'prog-mode) | |
c5de26b4 | 148 | |
55577e7c | 149 | ;;; prog-mode.el ends here |