prog-mode.el 4.39 KB
Newer Older
Glenn Morris's avatar
Glenn Morris committed
1
;;; prog-mode.el --- Generic major mode for programming  -*- lexical-binding: t -*-
2

Glenn Morris's avatar
Glenn Morris committed
3
;; Copyright (C) 2013 Free Software Foundation, Inc.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

;; Maintainer: FSF
;; Keywords: internal
;; Package: emacs

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;; This major mode is mostly intended as a parent of other programming
;; modes.  All major modes for programming languages should derive from this
;; mode so that users can put generic customization on prog-mode-hook.

;;; Code:

(eval-when-compile (require 'cl-lib))

(defgroup prog-mode nil
  "Generic programming mode, from which others derive."
  :group 'languages)

(defvar prog-mode-map
  (let ((map (make-sparse-keymap)))
    (define-key map [?\C-\M-q] 'prog-indent-sexp)
    map)
  "Keymap used for programming modes.")

(defun prog-indent-sexp (&optional defun)
  "Indent the expression after point.
When interactively called with prefix, indent the enclosing defun
instead."
  (interactive "P")
  (save-excursion
    (when defun
      (end-of-line)
      (beginning-of-defun))
    (let ((start (point))
	  (end (progn (forward-sexp 1) (point))))
      (indent-region start end nil))))

(defvar prog-prettify-symbols-alist nil)

(defcustom prog-prettify-symbols nil
  "Whether symbols should be prettified.
61 62
When set to an alist in the form `((STRING . CHARACTER)...)' it
will augment the mode's native prettify alist."
63 64 65 66 67 68 69 70 71 72 73 74 75 76
  :type '(choice
          (const :tag "No thanks" nil)
          (const :tag "Mode defaults" t)
          (alist :tag "Mode defaults augmented with your own list"
                 :key-type string :value-type character))
  :version "24.4")

(defun prog--prettify-font-lock-compose-symbol (alist)
  "Compose a sequence of ascii chars into a symbol.
Regexp match data 0 points to the chars."
  ;; Check that the chars should really be composed into a symbol.
  (let* ((start (match-beginning 0))
	 (end (match-end 0))
	 (syntaxes (if (eq (char-syntax (char-after start)) ?w)
77 78
		       '(?w) '(?. ?\\)))
	 match)
79 80
    (if (or (memq (char-syntax (or (char-before start) ?\ )) syntaxes)
	    (memq (char-syntax (or (char-after end) ?\ )) syntaxes)
81 82
            ;; syntax-ppss could modify the match data (bug#14595)
            (progn (setq match (match-string 0)) (nth 8 (syntax-ppss))))
83 84 85 86
	;; No composition for you.  Let's actually remove any composition
	;; we may have added earlier and which is now incorrect.
	(remove-text-properties start end '(composition))
      ;; That's a symbol alright, so add the composition.
87
      (compose-region start end (cdr (assoc match alist)))))
88 89 90 91 92 93 94 95 96 97 98 99 100
  ;; Return nil because we're not adding any face property.
  nil)

(defun prog-prettify-font-lock-symbols-keywords ()
  (when prog-prettify-symbols
    (let ((alist (append prog-prettify-symbols-alist
                         (if (listp prog-prettify-symbols)
                             prog-prettify-symbols
                           nil))))
      `((,(regexp-opt (mapcar 'car alist) t)
         (0 (prog--prettify-font-lock-compose-symbol ',alist)))))))

(defun prog-prettify-install (alist)
101 102 103 104 105 106
"Install prog-mode support to prettify symbols according to ALIST.

ALIST is in the format `((STRING . CHARACTER)...)' like
`prog-prettify-symbols'.

Internally, `font-lock-add-keywords' is called."
107 108 109 110 111 112 113 114 115 116 117 118 119
  (setq-local prog-prettify-symbols-alist alist)
  (let ((keywords (prog-prettify-font-lock-symbols-keywords)))
    (if keywords (font-lock-add-keywords nil keywords))))

;;;###autoload
(define-derived-mode prog-mode fundamental-mode "Prog"
  "Major mode for editing programming language source code."
  (set (make-local-variable 'require-final-newline) mode-require-final-newline)
  (set (make-local-variable 'parse-sexp-ignore-comments) t)
  ;; Any programming language is always written left to right.
  (setq bidi-paragraph-direction 'left-to-right))

(provide 'prog-mode)
Glenn Morris's avatar
Glenn Morris committed
120

121
;;; prog-mode.el ends here