Commit c4f268a1 authored by Stefan Monnier's avatar Stefan Monnier
Browse files

* lisp/progmodes/opascal.el: Rename from delphi.el. Use lexical-binding.

(opascal-newline-always-indents): Remove custom.
(opascal-tab, opascal-newline): Remove commands.
(opascal-new-comment-line): Insert "\n" instead of calling newline.
(opascal-mode-map): Keep default bindings for RET and TAB and DEL.
(opascal-save-match-data): Remove, use save-match-data instead.
(opascal-save-state): Use with-silent-modifications.
parent 9ca243c6
......@@ -71,6 +71,12 @@ bound to <f11> and M-<f10>, respectively.
* Changes in Specialized Modes and Packages in Emacs 24.4
** Delphi mode is now called OPascal mode.
*** All delphi-* variables and functions have been renamed to opascal-*.
*** `delphi-newline-always-indents' is not supported any more
use `electric-indent-mode' instead.
*** `delphi-tab' is gone, replaced by `indent-for-tab-command'.
** jit-lock-debug-mode lets you use the debuggers on code run via jit-lock.
** completing-read-multiple's separator can now be a regexp.
......
2013-01-31 Stefan Monnier <monnier@iro.umontreal.ca>
 
* progmodes/opascal.el: Rename from delphi.el. Use lexical-binding.
(opascal-newline-always-indents): Remove custom.
(opascal-tab, opascal-newline): Remove commands.
(opascal-new-comment-line): Insert "\n" instead of calling newline.
(opascal-mode-map): Keep default bindings for RET and TAB and DEL.
(opascal-save-match-data): Remove, use save-match-data instead.
(opascal-save-state): Use with-silent-modifications.
* progmodes/pascal.el (pascal-mode-syntax-table): Accept //..\n comments
(bug#13585).
 
......
;;; delphi.el --- major mode for editing Delphi source (Object Pascal) in Emacs
;;; opascal.el --- major mode for editing Object Pascal source in Emacs -*- lexical-binding: t -*-
;; Copyright (C) 1998-1999, 2001-2013 Free Software Foundation, Inc.
......@@ -24,74 +24,55 @@
;;; Commentary:
;; To enter Delphi mode when you find a Delphi source file, one must override
;; the auto-mode-alist to associate Delphi with .pas (and .dpr and .dpk)
;; files. Emacs, by default, will otherwise enter Pascal mode. E.g.
;; To enter OPascal mode when you find an Object Pascal source file, one must
;; override the auto-mode-alist to associate OPascal with .pas (and .dpr and
;; .dpk) files. Emacs, by default, will otherwise enter Pascal mode. E.g.
;;
;; (autoload 'delphi-mode "delphi")
;; (setq auto-mode-alist
;; (cons '("\\.\\(pas\\|dpr\\|dpk\\)$" . delphi-mode) auto-mode-alist))
;; (autoload 'opascal-mode "opascal")
;; (add-to-list 'auto-mode-alist
;; '("\\.\\(pas\\|dpr\\|dpk\\)\\'" . opascal-mode))
;; To get keyword, comment, and string literal coloring, be sure that font-lock
;; is running. One can manually do M-x font-lock-mode in a Delphi buffer, or
;; one can put in .emacs:
;;
;; (add-hook 'delphi-mode-hook 'turn-on-font-lock)
;; If font-lock is not loaded by default, you might have to do:
;;
;; (autoload 'font-lock-mode "font-lock")
;; (autoload 'turn-on-font-lock "font-lock")
;; (setq font-lock-support-mode 'lazy-lock-mode)
;;
;; Lazy lock is very necessary for faster screen updates.
;; For good performance, be sure to byte-compile delphi.el, e.g.
;;
;; M-x byte-compile-file <give the path to delphi.el when prompted>
;; This will generate delphi.elc, which will be loaded instead of delphi.el
;; when delphi-mode is autoloaded.
;; When you have entered Delphi mode, you may get more info by pressing
;; When you have entered OPascal mode, you may get more info by pressing
;; C-h m.
;; This Delphi mode implementation is fairly tolerant of syntax errors, relying
;; as much as possible on the indentation of the previous statement. This also
;; makes it faster and simpler, since there is less searching for properly
;; constructed beginnings.
;; This OPascal mode implementation is fairly tolerant of syntax errors,
;; relying as much as possible on the indentation of the previous statement.
;; This also makes it faster and simpler, since there is less searching for
;; properly constructed beginnings.
;;; Code:
(provide 'delphi)
(defgroup delphi nil
"Major mode for editing Delphi source in Emacs."
:version "21.1"
(defgroup opascal nil
"Major mode for editing OPascal source in Emacs."
:version "24.4"
:group 'languages)
(defconst delphi-debug nil
(defconst opascal-debug nil
"True if in debug mode.")
(defcustom delphi-search-path "."
(define-obsolete-variable-alias
'delphi-search-path 'opascal-search-path "24.4")
(defcustom opascal-search-path "."
"Directories to search when finding external units.
It is a list of directory strings. If only a single directory,
it can be a single string instead of a list. If a directory
ends in \"...\" then that directory is recursively searched."
:type 'string
:group 'delphi)
:type 'string)
(defcustom delphi-indent-level 3
"Indentation of Delphi statements with respect to containing block.
(define-obsolete-variable-alias
'delphi-indent-level 'opascal-indent-level "24.4")
(defcustom opascal-indent-level 3
"Indentation of OPascal statements with respect to containing block.
E.g.
begin
// This is an indent of 3.
end;"
:type 'integer
:group 'delphi)
:type 'integer)
(defcustom delphi-compound-block-indent 0
(define-obsolete-variable-alias
'delphi-compound-block-indent 'opascal-compound-block-indent "24.4")
(defcustom opascal-compound-block-indent 0
"Extra indentation for blocks in compound statements. E.g.
// block indent = 0 vs // block indent = 2
......@@ -101,10 +82,11 @@ end else begin end
end; else
begin
end;"
:type 'integer
:group 'delphi)
:type 'integer)
(defcustom delphi-case-label-indent delphi-indent-level
(define-obsolete-variable-alias
'delphi-case-label-indent 'opascal-case-label-indent "24.4")
(defcustom opascal-case-label-indent opascal-indent-level
"Extra indentation for case statement labels. E.g.
// case indent = 0 vs // case indent = 3
......@@ -114,58 +96,52 @@ v2: process_v2; v2: process_v2;
else else
process_else; process_else;
end; end;"
:type 'integer
:group 'delphi)
:type 'integer)
(defcustom delphi-verbose t ; nil
"If true then Delphi token processing progress is reported to the user."
:type 'boolean
:group 'delphi)
(define-obsolete-variable-alias 'delphi-verbose 'opascal-verbose "24.4")
(defcustom opascal-verbose t ; nil
"If true then OPascal token processing progress is reported to the user."
:type 'boolean)
(defcustom delphi-tab-always-indents t
"Non-nil means TAB in Delphi mode should always reindent the current line,
(define-obsolete-variable-alias
'delphi-tab-always-indents 'opascal-tab-always-indents "24.4")
(defcustom opascal-tab-always-indents tab-always-indent
"Non-nil means TAB in OPascal mode should always reindent the current line,
regardless of where in the line point is when the TAB command is used."
:type 'boolean
:group 'delphi)
(defcustom delphi-newline-always-indents t
"Non-nil means NEWLINE in Delphi mode should always reindent the current
line, insert a blank line and move to the default indent column of the blank
line. If nil, then no indentation occurs, and NEWLINE does the usual
behavior. This is useful when one needs to do customized indentation that
differs from the default."
:type 'boolean
:group 'delphi)
(defcustom delphi-comment-face 'font-lock-comment-face
"Face used to color Delphi comments."
:type 'face
:group 'delphi)
(defcustom delphi-string-face 'font-lock-string-face
"Face used to color Delphi strings."
:type 'face
:group 'delphi)
(defcustom delphi-keyword-face 'font-lock-keyword-face
"Face used to color Delphi keywords."
:type 'face
:group 'delphi)
(defcustom delphi-other-face nil
:type 'boolean)
(define-obsolete-variable-alias
'delphi-comment-face 'opascal-comment-face "24.4")
(defcustom opascal-comment-face 'font-lock-comment-face
"Face used to color OPascal comments."
:type 'face)
(define-obsolete-variable-alias
'delphi-string-face 'opascal-string-face "24.4")
(defcustom opascal-string-face 'font-lock-string-face
"Face used to color OPascal strings."
:type 'face)
(define-obsolete-variable-alias
'delphi-keyword-face 'opascal-keyword-face "24.4")
(defcustom opascal-keyword-face 'font-lock-keyword-face
"Face used to color OPascal keywords."
:type 'face)
(define-obsolete-variable-alias 'delphi-other-face 'opascal-other-face "24.4")
(defcustom opascal-other-face nil
"Face used to color everything else."
:type '(choice (const :tag "None" nil) face)
:group 'delphi)
:type '(choice (const :tag "None" nil) face))
(defconst delphi-directives
(defconst opascal-directives
'(absolute abstract assembler automated cdecl default dispid dynamic
export external far forward index inline message name near nodefault
overload override pascal private protected public published read readonly
register reintroduce resident resourcestring safecall stdcall stored
virtual write writeonly)
"Delphi4 directives.")
"OPascal4 directives.")
(defconst delphi-keywords
(defconst opascal-keywords
(append
'(;; Keywords.
and array as asm at begin case class const constructor contains
......@@ -180,261 +156,241 @@ differs from the default."
break exit)
;; We want directives to look like keywords.
delphi-directives)
"Delphi4 keywords.")
opascal-directives)
"OPascal4 keywords.")
(defconst delphi-previous-terminators `(semicolon comma)
(defconst opascal-previous-terminators `(semicolon comma)
"Expression/statement terminators that denote a previous expression.")
(defconst delphi-comments
(defconst opascal-comments
'(comment-single-line comment-multi-line-1 comment-multi-line-2)
"Tokens that represent comments.")
(defconst delphi-strings
(defconst opascal-strings
'(string double-quoted-string)
"Tokens that represent string literals.")
(defconst delphi-whitespace `(space newline ,@delphi-comments)
(defconst opascal-whitespace `(space newline ,@opascal-comments)
"Tokens that are considered whitespace.")
(defconst delphi-routine-statements
(defconst opascal-routine-statements
'(procedure function constructor destructor property)
"Marks the start of a routine, or routine-ish looking expression.")
(defconst delphi-body-expr-statements '(if while for on)
(defconst opascal-body-expr-statements '(if while for on)
"Statements that have either a single statement or a block as a body and also
are followed by an expression.")
(defconst delphi-expr-statements `(case ,@delphi-body-expr-statements)
(defconst opascal-expr-statements `(case ,@opascal-body-expr-statements)
"Expression statements contain expressions after their keyword.")
(defconst delphi-body-statements `(else ,@delphi-body-expr-statements)
(defconst opascal-body-statements `(else ,@opascal-body-expr-statements)
"Statements that have either a single statement or a block as a body.")
(defconst delphi-expr-delimiters '(then do of)
(defconst opascal-expr-delimiters '(then do of)
"Expression delimiter tokens.")
(defconst delphi-binary-ops
(defconst opascal-binary-ops
'(plus minus equals not-equals times divides div mod and or xor)
"Delphi binary operations.")
"OPascal binary operations.")
(defconst delphi-visibilities '(public private protected published automated)
(defconst opascal-visibilities '(public private protected published automated)
"Class visibilities.")
(defconst delphi-block-statements
(defconst opascal-block-statements
'(begin try case repeat initialization finalization asm)
"Statements that contain multiple substatements.")
(defconst delphi-mid-block-statements
`(except finally ,@delphi-visibilities)
(defconst opascal-mid-block-statements
`(except finally ,@opascal-visibilities)
"Statements that mark mid sections of the enclosing block.")
(defconst delphi-end-block-statements `(end until)
(defconst opascal-end-block-statements `(end until)
"Statements that end block sections.")
(defconst delphi-match-block-statements
`(,@delphi-end-block-statements ,@delphi-mid-block-statements)
(defconst opascal-match-block-statements
`(,@opascal-end-block-statements ,@opascal-mid-block-statements)
"Statements that match the indentation of the parent block.")
(defconst delphi-decl-sections '(type const var label resourcestring)
(defconst opascal-decl-sections '(type const var label resourcestring)
"Denotes the start of a declaration section.")
(defconst delphi-interface-types '(dispinterface interface)
(defconst opascal-interface-types '(dispinterface interface)
"Interface types.")
(defconst delphi-class-types '(class object)
(defconst opascal-class-types '(class object)
"Class types.")
(defconst delphi-composite-types
`(,@delphi-class-types ,@delphi-interface-types record)
(defconst opascal-composite-types
`(,@opascal-class-types ,@opascal-interface-types record)
"Types that contain declarations within them.")
(defconst delphi-unit-sections
(defconst opascal-unit-sections
'(interface implementation program library package)
"Unit sections within which the indent is 0.")
(defconst delphi-use-clauses `(uses requires exports contains)
(defconst opascal-use-clauses `(uses requires exports contains)
"Statements that refer to foreign symbols.")
(defconst delphi-unit-statements
`(,@delphi-use-clauses ,@delphi-unit-sections initialization finalization)
(defconst opascal-unit-statements
`(,@opascal-use-clauses ,@opascal-unit-sections initialization finalization)
"Statements indented at level 0.")
(defconst delphi-decl-delimiters
`(,@delphi-decl-sections ,@delphi-unit-statements
,@delphi-routine-statements)
(defconst opascal-decl-delimiters
`(,@opascal-decl-sections ,@opascal-unit-statements
,@opascal-routine-statements)
"Statements that a declaration statement should align with.")
(defconst delphi-decl-matchers
`(begin ,@delphi-decl-sections)
(defconst opascal-decl-matchers
`(begin ,@opascal-decl-sections)
"Statements that should match to declaration statement indentation.")
(defconst delphi-enclosing-statements
`(,@delphi-block-statements ,@delphi-mid-block-statements
,@delphi-decl-sections ,@delphi-use-clauses ,@delphi-routine-statements)
(defconst opascal-enclosing-statements
`(,@opascal-block-statements ,@opascal-mid-block-statements
,@opascal-decl-sections ,@opascal-use-clauses ,@opascal-routine-statements)
"Delimits an enclosing statement.")
(defconst delphi-previous-statements
`(,@delphi-unit-statements ,@delphi-routine-statements)
(defconst opascal-previous-statements
`(,@opascal-unit-statements ,@opascal-routine-statements)
"Delimits a previous statement.")
(defconst delphi-previous-enclosing-statements
`(,@delphi-block-statements ,@delphi-mid-block-statements
,@delphi-decl-sections)
(defconst opascal-previous-enclosing-statements
`(,@opascal-block-statements ,@opascal-mid-block-statements
,@opascal-decl-sections)
"Delimits a previous enclosing statement.")
(defconst delphi-begin-enclosing-tokens
`(,@delphi-block-statements ,@delphi-mid-block-statements)
(defconst opascal-begin-enclosing-tokens
`(,@opascal-block-statements ,@opascal-mid-block-statements)
"Tokens that a begin token indents from.")
(defconst delphi-begin-previous-tokens
`(,@delphi-decl-sections ,@delphi-routine-statements)
(defconst opascal-begin-previous-tokens
`(,@opascal-decl-sections ,@opascal-routine-statements)
"Tokens that a begin token aligns with, but only if not part of a nested
routine.")
(defconst delphi-space-chars "\000-\011\013- ") ; all except \n
(defconst delphi-non-space-chars (concat "^" delphi-space-chars))
(defconst delphi-spaces-re (concat "[" delphi-space-chars "]*"))
(defconst delphi-leading-spaces-re (concat "^" delphi-spaces-re))
(defconst delphi-word-chars "a-zA-Z0-9_")
(defmacro delphi-save-match-data (&rest forms)
;; Executes the forms such that the current match data is preserved, so as
;; not to disturb any existing search results.
`(let ((data (match-data)))
(unwind-protect
(progn ,@forms)
(set-match-data data))))
(defmacro delphi-save-excursion (&rest forms)
(defconst opascal-space-chars "\000-\011\013- ") ; all except \n
(defconst opascal-non-space-chars (concat "^" opascal-space-chars))
(defconst opascal-spaces-re (concat "[" opascal-space-chars "]*"))
(defconst opascal-leading-spaces-re (concat "^" opascal-spaces-re))
(defconst opascal-word-chars "a-zA-Z0-9_")
(defmacro opascal-save-excursion (&rest forms)
;; Executes the forms such that any movements have no effect, including
;; searches.
`(save-excursion
(delphi-save-match-data
(save-match-data
(let ((inhibit-point-motion-hooks t)
(deactivate-mark nil))
(progn ,@forms)))))
(defmacro delphi-save-state (&rest forms)
(defmacro opascal-save-state (&rest forms)
;; Executes the forms such that any buffer modifications do not have any side
;; effects beyond the buffer's actual content changes.
`(let ((delphi-ignore-changes t)
(old-supersession-threat
(symbol-function 'ask-user-about-supersession-threat))
(buffer-read-only nil)
(inhibit-read-only t)
(buffer-undo-list t)
(before-change-functions nil)
(after-change-functions nil)
(modified (buffer-modified-p)))
;; Disable any queries about editing obsolete files.
(fset 'ask-user-about-supersession-threat (lambda (_fn)))
(unwind-protect
(progn ,@forms)
(set-buffer-modified-p modified)
(fset 'ask-user-about-supersession-threat old-supersession-threat))))
(defsubst delphi-is (element in-set)
`(let ((opascal--ignore-changes t))
(with-silent-modifications
,@forms)))
(defsubst opascal-is (element in-set)
;; If the element is in the set, the element cdr is returned, otherwise nil.
(memq element in-set))
(defun delphi-string-of (start end)
(defun opascal-string-of (start end)
;; Returns the buffer string from start to end.
(buffer-substring-no-properties start end))
(defun delphi-looking-at-string (p s)
(defun opascal-looking-at-string (p s)
;; True if point p marks the start of string s. s is not a regular
;; expression.
(let ((limit (+ p (length s))))
(and (<= limit (point-max))
(string= s (delphi-string-of p limit)))))
(string= s (opascal-string-of p limit)))))
(defun delphi-token-of (kind start end)
(defun opascal-token-of (kind start end)
;; Constructs a token from a kind symbol and its start/end points.
`[,kind ,start ,end])
(defsubst delphi-token-kind (token)
(defsubst opascal-token-kind (token)
;; Returns the kind symbol of the token.
(if token (aref token 0) nil))
(defun delphi-set-token-kind (token to-kind)
(defun opascal-set-token-kind (token to-kind)
;; Sets the kind symbol of the token.
(if token (aset token 0 to-kind)))
(defsubst delphi-token-start (token)
(defsubst opascal-token-start (token)
;; Returns the start point of the token.
(if token (aref token 1) (point-min)))
(defsubst delphi-token-end (token)
(defsubst opascal-token-end (token)
;; Returns the end point of the token.
(if token (aref token 2) (point-min)))
(defun delphi-set-token-start (token start)
(defun opascal-set-token-start (token start)
;; Sets the start point of the token.
(if token (aset token 1 start)))
(defun delphi-set-token-end (token end)
(defun opascal-set-token-end (token end)
;; Sets the end point of the token.
(if token (aset token 2 end)))
(defun delphi-token-string (token)
(defun opascal-token-string (token)
;; Returns the string image of the token.
(if token
(delphi-string-of (delphi-token-start token) (delphi-token-end token))
(opascal-string-of (opascal-token-start token) (opascal-token-end token))
""))
(defun delphi-in-token (p token)
(defun opascal-in-token (p token)
;; Returns true if the point p is within the token's start/end points.
(and (<= (delphi-token-start token) p) (< p (delphi-token-end token))))
(and (<= (opascal-token-start token) p) (< p (opascal-token-end token))))
(defun delphi-column-of (p)
(defun opascal-column-of (p)
;; Returns the column of the point p.
(save-excursion (goto-char p) (current-column)))
(defun delphi-face-of (token-kind)
(defun opascal-face-of (token-kind)
;; Returns the face property appropriate for the token kind.
(cond ((delphi-is token-kind delphi-comments) delphi-comment-face)
((delphi-is token-kind delphi-strings) delphi-string-face)
((delphi-is token-kind delphi-keywords) delphi-keyword-face)
(delphi-other-face)))
(cond ((opascal-is token-kind opascal-comments) opascal-comment-face)
((opascal-is token-kind opascal-strings) opascal-string-face)
((opascal-is token-kind opascal-keywords) opascal-keyword-face)
(opascal-other-face)))
(defvar delphi-progress-last-reported-point nil
(defvar opascal-progress-last-reported-point nil
"The last point at which progress was reported.")
(defconst delphi-parsing-progress-step 16384
(defconst opascal-parsing-progress-step 16384
"Number of chars to process before the next parsing progress report.")
(defconst delphi-scanning-progress-step 2048
(defconst opascal-scanning-progress-step 2048
"Number of chars to process before the next scanning progress report.")
(defconst delphi-fontifying-progress-step delphi-scanning-progress-step
(defconst opascal-fontifying-progress-step opascal-scanning-progress-step
"Number of chars to process before the next fontification progress report.")
(defun delphi-progress-start ()
(defun opascal-progress-start ()
;; Initializes progress reporting.
(setq delphi-progress-last-reported-point nil))
(setq opascal-progress-last-reported-point nil))
(defun delphi-progress-done (&rest msgs)
(defun opascal-progress-done (&rest msgs)
;; Finalizes progress reporting.
(setq delphi-progress-last-reported-point nil)
(when delphi-verbose
(setq opascal-progress-last-reported-point nil)
(when opascal-verbose
(if (null msgs)
(message "")
(apply #'message msgs))))
(defun delphi-step-progress (p desc step-size)
(defun opascal-step-progress (p desc step-size)
;; If enough distance has elapsed since the last reported point, then report
;; the current progress to the user.
(cond ((null delphi-progress-last-reported-point)
(cond ((null opascal-progress-last-reported-point)
;; This is the first progress step.
(setq delphi-progress-last-reported-point p))
(setq opascal-progress-last-reported-point p))
((and delphi-verbose
(>= (abs (- p delphi-progress-last-reported-point)) step-size))
((and opascal-verbose
(>= (abs (- p opascal-progress-last-reported-point)) step-size))
;; Report the percentage complete.
(setq delphi-progress-last-reported-point p)
(setq opascal-progress-last-reported-point p)
(message "%s %s ... %d%%"
desc (buffer-name) (/ (* 100 p) (point-max))))))
(defun delphi-next-line-start (&optional from-point)
(defun opascal-next-line-start (&optional from-point)
;; Returns the first point of the next line.
(let ((curr-point (point))
(next nil))
......@@ -444,24 +400,24 @@ routine.")
(goto-char curr-point)
next))
(defvar delphi-ignore-changes t
"Internal flag to control if the Delphi mode responds to buffer changes.
Defaults to t in case the `delphi-after-change' function is called on a
non-Delphi buffer. Set to nil in a Delphi buffer. To override, just do:
(let ((delphi-ignore-changes t)) ...)")
(defvar opascal--ignore-changes t
"Internal flag to control if the OPascal mode responds to buffer changes.
Defaults to t in case the `opascal-after-change' function is called on a
non-OPascal buffer. Set to nil in OPascal buffers. To override, just do:
(let ((opascal--ignore-changes t)) ...)")
(defun delphi-set-text-properties (from to properties)
(defun opascal-set-text-properties (from to properties)
;; Like `set-text-properties', except we do not consider this to be a buffer