Commit | Line | Data |
---|---|---|
c5de26b4 | 1 | ;;; prog-mode.el --- Generic major mode for programming -*- lexical-binding: t -*- |
55577e7c | 2 | |
c5de26b4 | 3 | ;; Copyright (C) 2013 Free Software Foundation, Inc. |
55577e7c SM |
4 | |
5 | ;; Maintainer: FSF | |
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 | ||
57 | (defvar prog-prettify-symbols-alist nil) | |
58 | ||
59 | (defcustom prog-prettify-symbols nil | |
60 | "Whether symbols should be prettified. | |
9bfff84b TZ |
61 | When set to an alist in the form `((STRING . CHARACTER)...)' it |
62 | will augment the mode's native prettify alist." | |
55577e7c SM |
63 | :type '(choice |
64 | (const :tag "No thanks" nil) | |
65 | (const :tag "Mode defaults" t) | |
66 | (alist :tag "Mode defaults augmented with your own list" | |
67 | :key-type string :value-type character)) | |
68 | :version "24.4") | |
69 | ||
70 | (defun prog--prettify-font-lock-compose-symbol (alist) | |
71 | "Compose a sequence of ascii chars into a symbol. | |
72 | Regexp match data 0 points to the chars." | |
73 | ;; Check that the chars should really be composed into a symbol. | |
74 | (let* ((start (match-beginning 0)) | |
75 | (end (match-end 0)) | |
76 | (syntaxes (if (eq (char-syntax (char-after start)) ?w) | |
77 | '(?w) '(?. ?\\)))) | |
78 | (if (or (memq (char-syntax (or (char-before start) ?\ )) syntaxes) | |
79 | (memq (char-syntax (or (char-after end) ?\ )) syntaxes) | |
80 | (nth 8 (syntax-ppss))) | |
81 | ;; No composition for you. Let's actually remove any composition | |
82 | ;; we may have added earlier and which is now incorrect. | |
83 | (remove-text-properties start end '(composition)) | |
84 | ;; That's a symbol alright, so add the composition. | |
85 | (compose-region start end (cdr (assoc (match-string 0) alist))))) | |
86 | ;; Return nil because we're not adding any face property. | |
87 | nil) | |
88 | ||
89 | (defun prog-prettify-font-lock-symbols-keywords () | |
90 | (when prog-prettify-symbols | |
91 | (let ((alist (append prog-prettify-symbols-alist | |
92 | (if (listp prog-prettify-symbols) | |
93 | prog-prettify-symbols | |
94 | nil)))) | |
95 | `((,(regexp-opt (mapcar 'car alist) t) | |
96 | (0 (prog--prettify-font-lock-compose-symbol ',alist))))))) | |
97 | ||
98 | (defun prog-prettify-install (alist) | |
9bfff84b TZ |
99 | "Install prog-mode support to prettify symbols according to ALIST. |
100 | ||
101 | ALIST is in the format `((STRING . CHARACTER)...)' like | |
102 | `prog-prettify-symbols'. | |
103 | ||
104 | Internally, `font-lock-add-keywords' is called." | |
55577e7c SM |
105 | (setq-local prog-prettify-symbols-alist alist) |
106 | (let ((keywords (prog-prettify-font-lock-symbols-keywords))) | |
107 | (if keywords (font-lock-add-keywords nil keywords)))) | |
108 | ||
109 | ;;;###autoload | |
110 | (define-derived-mode prog-mode fundamental-mode "Prog" | |
111 | "Major mode for editing programming language source code." | |
112 | (set (make-local-variable 'require-final-newline) mode-require-final-newline) | |
113 | (set (make-local-variable 'parse-sexp-ignore-comments) t) | |
114 | ;; Any programming language is always written left to right. | |
115 | (setq bidi-paragraph-direction 'left-to-right)) | |
116 | ||
117 | (provide 'prog-mode) | |
c5de26b4 | 118 | |
55577e7c | 119 | ;;; prog-mode.el ends here |