* prog-mode.el: Comment fixes.
[emacs.git] / lisp / progmodes / prog-mode.el
blobe2700414636130f12b80a54c749931935927cde1
1 ;;; prog-mode.el --- Generic major mode for programming -*- lexical-binding: t -*-
3 ;; Copyright (C) 2013 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
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 prog-prettify-symbols-alist nil)
59 (defcustom prog-prettify-symbols nil
60 "Whether symbols should be prettified.
61 When set to an alist in the form `((STRING . CHARACTER)...)' it
62 will augment the mode's native prettify alist."
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")
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)
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)))))))
98 (defun prog-prettify-install (alist)
99 "Install prog-mode support to prettify symbols according to ALIST.
101 ALIST is in the format `((STRING . CHARACTER)...)' like
102 `prog-prettify-symbols'.
104 Internally, `font-lock-add-keywords' is called."
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))))
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))
117 (provide 'prog-mode)
119 ;;; prog-mode.el ends here