2013-06-06 17:32:13 -04:00
|
|
|
;;; prog-mode.el --- Generic major mode for programming -*- lexical-binding: t -*-
|
2013-06-05 14:10:27 -04:00
|
|
|
|
2015-01-01 14:26:41 -08:00
|
|
|
;; Copyright (C) 2013-2015 Free Software Foundation, Inc.
|
2013-06-05 14:10:27 -04:00
|
|
|
|
2014-02-09 17:34:22 -08:00
|
|
|
;; Maintainer: emacs-devel@gnu.org
|
2013-06-05 14:10:27 -04:00
|
|
|
;; 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)
|
|
|
|
|
2014-08-08 15:51:47 +02:00
|
|
|
(defcustom prog-mode-hook nil
|
2014-09-21 18:07:26 -04:00
|
|
|
"Normal hook run when entering programming modes."
|
2014-08-08 15:51:47 +02:00
|
|
|
:type 'hook
|
|
|
|
:options '(flyspell-prog-mode abbrev-mode flymake-mode linum-mode
|
|
|
|
prettify-symbols-mode)
|
|
|
|
:group 'prog-mode)
|
|
|
|
|
2013-06-05 14:10:27 -04:00
|
|
|
(defvar prog-mode-map
|
|
|
|
(let ((map (make-sparse-keymap)))
|
|
|
|
(define-key map [?\C-\M-q] 'prog-indent-sexp)
|
|
|
|
map)
|
|
|
|
"Keymap used for programming modes.")
|
|
|
|
|
2015-06-14 14:47:16 +00:00
|
|
|
(defvar prog-indentation-context nil
|
|
|
|
"Non-nil while indenting embedded code chunks.
|
|
|
|
There are languages where part of the code is actually written in
|
|
|
|
a sub language, e.g., a Yacc/Bison or ANTLR grammar also consists
|
|
|
|
of plain C code. This variable enables the major mode of the
|
|
|
|
main language to use the indentation engine of the sub mode for
|
|
|
|
lines in code chunks written in the sub language.
|
|
|
|
|
|
|
|
When a major mode of such a main language decides to delegate the
|
|
|
|
indentation of a line/region to the indentation engine of the sub
|
|
|
|
mode, it is supposed to bind this variable to non-nil around the call.
|
|
|
|
|
|
|
|
The non-nil value looks as follows
|
|
|
|
\(FIRST-COLUMN (START . END) PREVIOUS-CHUNKS)
|
|
|
|
|
|
|
|
FIRST-COLUMN is the column the indentation engine of the sub mode
|
|
|
|
should usually choose for top-level language constructs inside
|
|
|
|
the code chunk (instead of 0).
|
|
|
|
|
|
|
|
START to END is the region of the code chunk. See function
|
|
|
|
`prog-widen' for additional info.
|
|
|
|
|
|
|
|
PREVIOUS-CHUNKS, if non-nil, provides the indentation engine of
|
|
|
|
the sub mode with the virtual context of the code chunk. Valid
|
|
|
|
values are:
|
|
|
|
|
|
|
|
- A string containing code which the indentation engine can
|
|
|
|
consider as standing in front of the code chunk. To cache the
|
|
|
|
string's calculated syntactic information for repeated calls
|
|
|
|
with the same string, it is valid and expected for the inner
|
|
|
|
mode to add text-properties to the string.
|
|
|
|
|
|
|
|
A typical use case is for grammars with code chunks which are
|
|
|
|
to be indented like function bodies - the string would contain
|
|
|
|
a corresponding function header.
|
|
|
|
|
|
|
|
- A function called with the start position of the current
|
|
|
|
chunk. It will return either the region of the previous chunk
|
|
|
|
as \(PREV-START . PREV-END) or nil if there is no further
|
|
|
|
previous chunk.
|
|
|
|
|
|
|
|
A typical use case are literate programming sources - the
|
|
|
|
function would successively return the code chunks of the
|
|
|
|
previous macro definitions for the same name.")
|
|
|
|
|
2013-06-05 14:10:27 -04:00
|
|
|
(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))))
|
|
|
|
|
2015-06-14 14:47:16 +00:00
|
|
|
(defun prog-first-column ()
|
|
|
|
"Return the indentation column normally used for top-level constructs."
|
|
|
|
(or (car prog-indentation-context) 0))
|
|
|
|
|
|
|
|
(defun prog-widen ()
|
|
|
|
"Remove restrictions (narrowing) from current code chunk or buffer.
|
|
|
|
This function can be used instead of `widen' in any function used
|
|
|
|
by the indentation engine to make it respect the value
|
|
|
|
`prog-indentation-context'.
|
|
|
|
|
2015-08-20 17:33:48 -07:00
|
|
|
This function (like `widen') is useful inside a
|
2015-06-14 14:47:16 +00:00
|
|
|
`save-restriction' to make the indentation correctly work when
|
|
|
|
narrowing is in effect."
|
|
|
|
(let ((chunk (cadr prog-indentation-context)))
|
|
|
|
(if chunk
|
|
|
|
;; no widen necessary here, as narrow-to-region changes (not
|
|
|
|
;; just narrows) existing restrictions
|
|
|
|
(narrow-to-region (car chunk) (or (cdr chunk) (point-max)))
|
|
|
|
(widen))))
|
|
|
|
|
|
|
|
|
2013-06-19 00:13:25 +02:00
|
|
|
(defvar-local prettify-symbols-alist nil
|
|
|
|
"Alist of symbol prettifications.
|
|
|
|
Each element looks like (SYMBOL . CHARACTER), where the symbol
|
|
|
|
matching SYMBOL (a string, not a regexp) will be shown as
|
|
|
|
CHARACTER instead.")
|
|
|
|
|
|
|
|
(defun prettify-symbols--compose-symbol (alist)
|
|
|
|
"Compose a sequence of characters into a symbol.
|
2013-06-05 14:10:27 -04:00
|
|
|
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))
|
2014-11-30 23:07:19 -05:00
|
|
|
(syntaxes-beg (if (memq (char-syntax (char-after start)) '(?w ?_))
|
|
|
|
'(?w ?_) '(?. ?\\)))
|
|
|
|
(syntaxes-end (if (memq (char-syntax (char-before end)) '(?w ?_))
|
2014-04-21 16:31:22 -04:00
|
|
|
'(?w ?_) '(?. ?\\)))
|
2013-06-16 03:26:42 +02:00
|
|
|
match)
|
2014-11-30 23:07:19 -05:00
|
|
|
(if (or (memq (char-syntax (or (char-before start) ?\s)) syntaxes-beg)
|
|
|
|
(memq (char-syntax (or (char-after end) ?\s)) syntaxes-end)
|
2013-06-16 03:26:42 +02:00
|
|
|
;; syntax-ppss could modify the match data (bug#14595)
|
|
|
|
(progn (setq match (match-string 0)) (nth 8 (syntax-ppss))))
|
2013-06-05 14:10:27 -04:00
|
|
|
;; 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.
|
2013-06-16 03:26:42 +02:00
|
|
|
(compose-region start end (cdr (assoc match alist)))))
|
2013-06-05 14:10:27 -04:00
|
|
|
;; Return nil because we're not adding any face property.
|
|
|
|
nil)
|
|
|
|
|
2013-06-19 00:13:25 +02:00
|
|
|
(defun prettify-symbols--make-keywords ()
|
|
|
|
(if prettify-symbols-alist
|
|
|
|
`((,(regexp-opt (mapcar 'car prettify-symbols-alist) t)
|
|
|
|
(0 (prettify-symbols--compose-symbol ',prettify-symbols-alist))))
|
|
|
|
nil))
|
|
|
|
|
|
|
|
(defvar-local prettify-symbols--keywords nil)
|
|
|
|
|
2013-06-20 12:28:23 +02:00
|
|
|
;;;###autoload
|
2013-06-19 00:13:25 +02:00
|
|
|
(define-minor-mode prettify-symbols-mode
|
|
|
|
"Toggle Prettify Symbols mode.
|
|
|
|
With a prefix argument ARG, enable Prettify Symbols mode if ARG is
|
|
|
|
positive, and disable it otherwise. If called from Lisp, enable
|
|
|
|
the mode if ARG is omitted or nil.
|
|
|
|
|
|
|
|
When Prettify Symbols mode and font-locking are enabled, symbols are
|
|
|
|
prettified (displayed as composed characters) according to the rules
|
|
|
|
in `prettify-symbols-alist' (which see), which are locally defined
|
|
|
|
by major modes supporting prettifying. To add further customizations
|
|
|
|
for a given major mode, you can modify `prettify-symbols-alist' thus:
|
|
|
|
|
|
|
|
(add-hook 'emacs-lisp-mode-hook
|
|
|
|
(lambda ()
|
|
|
|
(push '(\"<=\" . ?≤) prettify-symbols-alist)))
|
|
|
|
|
|
|
|
You can enable this mode locally in desired buffers, or use
|
|
|
|
`global-prettify-symbols-mode' to enable it for all modes that
|
|
|
|
support it."
|
|
|
|
:init-value nil
|
|
|
|
(if prettify-symbols-mode
|
|
|
|
;; Turn on
|
|
|
|
(when (setq prettify-symbols--keywords (prettify-symbols--make-keywords))
|
|
|
|
(font-lock-add-keywords nil prettify-symbols--keywords)
|
|
|
|
(setq-local font-lock-extra-managed-props
|
|
|
|
(cons 'composition font-lock-extra-managed-props))
|
* lisp/font-lock.el (font-lock-flush, font-lock-ensure): New functions.
(font-lock-fontify-buffer): Mark interactive-only.
(font-lock-multiline, font-lock-fontified, font-lock-set-defaults):
Make buffer-local.
(font-lock-specified-p): Remove redundant boundp check.
(font-lock-flush-function, font-lock-ensure-function): New vars.
(font-lock-turn-on-thing-lock): Set them.
(font-lock-default-fontify-buffer): Obey font-lock-dont-widen.
(font-lock-after-change-function): Make `old-len' optional.
(font-lock-set-defaults): Remove redundant `set' of font-lock-defaults.
Call font-lock-flush, just in case.
* lisp/progmodes/verilog-mode.el (verilog-preprocess): Disable workaround in
recent Emacsen.
* lisp/progmodes/vera-mode.el (vera-fontify-buffer): Declare obsolete.
(vera-mode-map, vera-mode-menu): Remove bindings to it.
* lisp/progmodes/idlw-help.el (idlwave-help-fontify): Use font-lock-ensure
and with-syntax-table.
* lisp/textmodes/conf-mode.el (conf-quote-normal):
* lisp/progmodes/sh-script.el (sh-set-shell):
* lisp/progmodes/prog-mode.el (prettify-symbols-mode):
* lisp/progmodes/f90.el (f90-font-lock-n):
* lisp/progmodes/cwarn.el (cwarn-mode):
* lisp/nxml/nxml-mode.el (nxml-toggle-char-ref-extra-display):
* lisp/progmodes/compile.el (compilation-setup, compilation--unsetup):
* lisp/hi-lock.el (hi-lock-mode, hi-lock-unface-buffer)
(hi-lock-set-pattern, hi-lock-set-file-patterns): Use font-lock-flush.
* lisp/mail/rmail.el (rmail-variables): Set font-lock-dont-widen instead of
font-lock-fontify-buffer-function and
font-lock-unfontify-buffer-function.
(rmail-unfontify-buffer-function, rmail-fontify-message):
Use with-silent-modifications.
* lisp/htmlfontify.el (hfy-force-fontification): Use jit-lock-fontify-now
and font-lock-ensure.
* lisp/bs.el (bs-show-in-buffer): Use font-lock-ensure.
* lisp/gnus/mm-view.el (mm-display-inline-fontify): Use font-lock-ensure.
* lisp/gnus/gnus-cite.el (gnus-message-citation-mode): Use font-lock-flush.
* lisp/org/org-compat.el (org-font-lock-ensure): New function.
* lisp/org/ox-odt.el (org-odt-do-format-code):
* lisp/org/ox-html.el (org-html-fontify-code):
* lisp/org/org.el (org-fontify-like-in-org-mode):
* lisp/org/org-src.el (org-src-font-lock-fontify-block):
* lisp/org/org-clock.el (org-clock-get-clocktable): Use it.
* lisp/org/ox-org.el (org-org-publish-to-org): Use it. Avoid using find-file
from Elisp.
* test/automated/ruby-mode-tests.el (ruby-assert-face): Use font-lock-ensure.
(ruby-interpolation-keeps-non-quote-syntax): Use syntax-propertize.
2014-05-28 23:45:29 -04:00
|
|
|
(font-lock-flush))
|
2013-06-19 00:13:25 +02:00
|
|
|
;; Turn off
|
|
|
|
(when prettify-symbols--keywords
|
|
|
|
(font-lock-remove-keywords nil prettify-symbols--keywords)
|
|
|
|
(setq prettify-symbols--keywords nil))
|
|
|
|
(when (memq 'composition font-lock-extra-managed-props)
|
|
|
|
(setq font-lock-extra-managed-props (delq 'composition
|
|
|
|
font-lock-extra-managed-props))
|
|
|
|
(with-silent-modifications
|
|
|
|
(remove-text-properties (point-min) (point-max) '(composition nil))))))
|
|
|
|
|
|
|
|
(defun turn-on-prettify-symbols-mode ()
|
|
|
|
(when (and (not prettify-symbols-mode)
|
|
|
|
(local-variable-p 'prettify-symbols-alist))
|
|
|
|
(prettify-symbols-mode 1)))
|
|
|
|
|
2013-06-20 12:28:23 +02:00
|
|
|
;;;###autoload
|
2013-06-19 00:13:25 +02:00
|
|
|
(define-globalized-minor-mode global-prettify-symbols-mode
|
|
|
|
prettify-symbols-mode turn-on-prettify-symbols-mode)
|
2013-06-05 14:10:27 -04:00
|
|
|
|
2013-06-20 12:28:23 +02:00
|
|
|
;;;###autoload
|
2013-06-05 14:10:27 -04:00
|
|
|
(define-derived-mode prog-mode fundamental-mode "Prog"
|
|
|
|
"Major mode for editing programming language source code."
|
2013-06-17 03:09:13 +02:00
|
|
|
(setq-local require-final-newline mode-require-final-newline)
|
|
|
|
(setq-local parse-sexp-ignore-comments t)
|
2013-06-05 14:10:27 -04:00
|
|
|
;; Any programming language is always written left to right.
|
|
|
|
(setq bidi-paragraph-direction 'left-to-right))
|
|
|
|
|
|
|
|
(provide 'prog-mode)
|
2013-06-06 17:32:13 -04:00
|
|
|
|
2013-06-05 14:10:27 -04:00
|
|
|
;;; prog-mode.el ends here
|