Add 2012 to FSF copyright years for Emacs files
[bpt/emacs.git] / lisp / progmodes / pascal.el
index acd49e7..db15e3c 100644 (file)
@@ -1,8 +1,6 @@
-;;; pascal.el --- major mode for editing pascal source in Emacs
+;;; pascal.el --- major mode for editing pascal source in Emacs -*- lexical-binding: t -*-
 
-;; Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
-;;               2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
-;;               Free Software Foundation, Inc.
+;; Copyright (C) 1993-2012  Free Software Foundation, Inc.
 
 ;; Author: Espen Skoglund <esk@gnu.org>
 ;; Keywords: languages
@@ -42,7 +40,6 @@
 ;;       pascal-tab-always-indent  t
 ;;       pascal-auto-endcomments   t
 ;;       pascal-auto-lineup        '(all)
-;;       pascal-toggle-completions nil
 ;;       pascal-type-keywords      '("array" "file" "packed" "char"
 ;;                                  "integer" "real" "string" "record")
 ;;       pascal-start-keywords     '("begin" "end" "function" "procedure"
     (define-key map ":"        'electric-pascal-colon)
     (define-key map "="        'electric-pascal-equal)
     (define-key map "#"        'electric-pascal-hash)
-    (define-key map "\r"       'electric-pascal-terminate-line)
-    (define-key map "\t"       'electric-pascal-tab)
-    (define-key map "\M-\t"    'pascal-complete-word)
-    (define-key map "\M-?"     'pascal-show-completions)
+    ;; These are user preferences, so not to set by default.
+    ;;(define-key map "\r"       'electric-pascal-terminate-line)
+    ;;(define-key map "\t"       'electric-pascal-tab)
+    (define-key map "\M-\t"    'completion-at-point)
+    (define-key map "\M-?"     'completion-help-at-point)
     (define-key map "\177"     'backward-delete-char-untabify)
     (define-key map "\M-\C-h"  'pascal-mark-defun)
     (define-key map "\C-c\C-b" 'pascal-insert-block)
@@ -229,17 +227,17 @@ will do all lineups."
   :type '(set :extra-offset 8
              (const :tag "Everything" all)
              (const :tag "Parameter lists" paramlist)
-             (const :tag "Decalrations" declaration)
+             (const :tag "Declarations" declaration)
              (const :tag "Case statements" case))
   :group 'pascal)
 
-(defcustom pascal-toggle-completions nil
-  "*Non-nil means \\<pascal-mode-map>\\[pascal-complete-word] should try all possible completions one by one.
-Repeated use of \\[pascal-complete-word] will show you all of them.
+(defvar pascal-toggle-completions nil
+  "*Non-nil meant \\<pascal-mode-map>\\[pascal-complete-word] would try all possible completions one by one.
+Repeated use of \\[pascal-complete-word] would show you all of them.
 Normally, when there is more than one possible completion,
-it displays a list of all possible completions."
-  :type 'boolean
-  :group 'pascal)
+it displays a list of all possible completions.")
+(make-obsolete-variable 'pascal-toggle-completions
+                        'completion-cycle-threshold "24.1")
 
 (defcustom pascal-type-keywords
   '("array" "file" "packed" "char" "integer" "real" "string" "record")
@@ -274,22 +272,12 @@ are handled in another way, and should not be added to this list."
 ;;;  Macros
 ;;;
 
-(defsubst pascal-get-beg-of-line (&optional arg)
-  (save-excursion
-    (beginning-of-line arg)
-    (point)))
-
-(defsubst pascal-get-end-of-line (&optional arg)
-  (save-excursion
-    (end-of-line arg)
-    (point)))
-
 (defun pascal-declaration-end ()
   (let ((nest 1))
     (while (and (> nest 0)
                (re-search-forward
                 "[:=]\\|\\(\\<record\\>\\)\\|\\(\\<end\\>\\)"
-                (save-excursion (end-of-line 2) (point)) t))
+                (point-at-eol 2) t))
       (cond ((match-beginning 1) (setq nest (1+ nest)))
            ((match-beginning 2) (setq nest (1- nest)))
            ((looking-at "[^(\n]+)") (setq nest 0))))))
@@ -298,7 +286,7 @@ are handled in another way, and should not be added to this list."
 (defun pascal-declaration-beg ()
   (let ((nest 1))
     (while (and (> nest 0)
-               (re-search-backward "[:=]\\|\\<\\(type\\|var\\|label\\|const\\)\\>\\|\\(\\<record\\>\\)\\|\\(\\<end\\>\\)" (pascal-get-beg-of-line 0) t))
+               (re-search-backward "[:=]\\|\\<\\(type\\|var\\|label\\|const\\)\\>\\|\\(\\<record\\>\\)\\|\\(\\<end\\>\\)" (point-at-bol 0) t))
       (cond ((match-beginning 1) (setq nest 0))
            ((match-beginning 2) (setq nest (1- nest)))
            ((match-beginning 3) (setq nest (1+ nest)))))
@@ -306,8 +294,7 @@ are handled in another way, and should not be added to this list."
 
 
 (defsubst pascal-within-string ()
-  (save-excursion
-    (nth 3 (parse-partial-sexp (pascal-get-beg-of-line) (point)))))
+  (nth 3 (parse-partial-sexp (point-at-bol) (point))))
 
 
 ;;;###autoload
@@ -315,9 +302,9 @@ are handled in another way, and should not be added to this list."
   "Major mode for editing Pascal code. \\<pascal-mode-map>
 TAB indents for Pascal code.  Delete converts tabs to spaces as it moves back.
 
-\\[pascal-complete-word] completes the word around current point with respect \
+\\[completion-at-point] completes the word around current point with respect \
 to position in code
-\\[pascal-show-completions] shows all possible completions at this point.
+\\[completion-help-at-point] shows all possible completions at this point.
 
 Other useful functions are:
 
@@ -366,6 +353,7 @@ no args, if that value is non-nil."
   (set (make-local-variable 'comment-start) "{")
   (set (make-local-variable 'comment-start-skip) "(\\*+ *\\|{ *")
   (set (make-local-variable 'comment-end) "}")
+  (add-hook 'completion-at-point-functions 'pascal-completions-at-point nil t)
   ;; Font lock support
   (set (make-local-variable 'font-lock-defaults)
        '(pascal-font-lock-keywords nil t))
@@ -407,8 +395,7 @@ no args, if that value is non-nil."
             (forward-char 1)
             (delete-horizontal-space))
            ((and (looking-at "(\\*\\|\\*[^)]")
-                 (not (save-excursion
-                        (search-forward "*)" (pascal-get-end-of-line) t))))
+                 (not (save-excursion (search-forward "*)" (point-at-eol) t))))
             (setq setstar t))))
     ;; If last line was a star comment line then this one shall be too.
     (if (null setstar)
@@ -427,7 +414,7 @@ no args, if that value is non-nil."
       (electric-pascal-terminate-line)))
 
 (defun electric-pascal-colon ()
-  "Insert `:' and do all indentions except line indent on this line."
+  "Insert `:' and do all indentations except line indent on this line."
   (interactive)
   (insert last-command-event)
   ;; Do nothing if within string.
@@ -440,7 +427,7 @@ no args, if that value is non-nil."
       (pascal-indent-command))))
 
 (defun electric-pascal-equal ()
-  "Insert `=', and do indention if within type declaration."
+  "Insert `=', and do indentation if within type declaration."
   (interactive)
   (insert last-command-event)
   (if (eq (car (pascal-calculate-indent)) 'declaration)
@@ -727,7 +714,7 @@ on the line which ends a function or procedure named NAME."
     (if (and (looking-at "\\<end;")
             (not (save-excursion
                    (end-of-line)
-                   (search-backward "{" (pascal-get-beg-of-line) t))))
+                   (search-backward "{" (point-at-bol) t))))
        (let ((type (car (pascal-calculate-indent))))
          (if (eq type 'declaration)
              ()
@@ -799,6 +786,7 @@ on the line which ends a function or procedure named NAME."
     (if (looking-at "[ \t]+$")
        (skip-chars-forward " \t"))))
 
+(defvar ind)                          ;Used via `eval' in pascal-indent-alist.
 (defun pascal-indent-line ()
   "Indent current line as a Pascal statement."
   (let* ((indent-str (pascal-calculate-indent))
@@ -999,7 +987,7 @@ indent of the current line in parameterlist."
           (stpos (progn (goto-char (scan-lists (point) -1 1)) (point)))
           (stcol (1+ (current-column)))
           (edpos (progn (pascal-declaration-end)
-                        (search-backward ")" (pascal-get-beg-of-line) t)
+                        (search-backward ")" (point-at-bol) t)
                         (point)))
           (usevar (re-search-backward "\\<var\\>" stpos t)))
       (if arg (progn
@@ -1046,7 +1034,7 @@ indent of the current line in parameterlist."
        (setq ind (pascal-get-lineup-indent stpos edpos lineup))
        (goto-char stpos)
        (while (and (<= (point) edpos) (not (eobp)))
-         (if (search-forward lineup (pascal-get-end-of-line) 'move)
+         (if (search-forward lineup (point-at-eol) 'move)
              (forward-char -1))
          (delete-horizontal-space)
          (indent-to ind)
@@ -1073,7 +1061,7 @@ indent of the current line in parameterlist."
       (goto-char b)
       ;; Get rightmost position
       (while (< (point) e)
-       (and (re-search-forward reg (min e (pascal-get-end-of-line 2)) 'move)
+       (and (re-search-forward reg (min e (point-at-eol 2)) 'move)
             (cond ((match-beginning 1)
                    ;; Skip record blocks
                    (pascal-declaration-end))
@@ -1137,7 +1125,7 @@ indent of the current line in parameterlist."
 
       ;; Search through all reachable functions
       (while (pascal-beg-of-defun)
-        (if (re-search-forward pascal-str (pascal-get-end-of-line) t)
+        (if (re-search-forward pascal-str (point-at-eol) t)
             (progn (setq match (buffer-substring (match-beginning 2)
                                                  (match-end 2)))
                    (push match pascal-all)))
@@ -1154,17 +1142,17 @@ indent of the current line in parameterlist."
        match)
     ;; Traverse lines
     (while (< (point) end)
-      (if (re-search-forward "[:=]" (pascal-get-end-of-line) t)
+      (if (re-search-forward "[:=]" (point-at-eol) t)
          ;; Traverse current line
          (while (and (re-search-backward
                       (concat "\\((\\|\\<\\(var\\|type\\|const\\)\\>\\)\\|"
                               pascal-symbol-re)
-                      (pascal-get-beg-of-line) t)
+                      (point-at-bol) t)
                      (not (match-end 1)))
            (setq match (buffer-substring (match-beginning 0) (match-end 0)))
            (if (string-match (concat "\\<" pascal-str) match)
                 (push match pascal-all))))
-      (if (re-search-forward "\\<record\\>" (pascal-get-end-of-line) t)
+      (if (re-search-forward "\\<record\\>" (point-at-eol) t)
          (pascal-declaration-end)
        (forward-line 1)))
 
@@ -1206,7 +1194,7 @@ indent of the current line in parameterlist."
           (if (> start (prog1 (save-excursion (pascal-end-of-defun)
                                               (point))))
               ()                        ; Declarations not reachable
-            (if (search-forward "(" (pascal-get-end-of-line) t)
+            (if (search-forward "(" (point-at-eol) t)
                 ;; Check parameterlist
                 ;; FIXME: pascal-get-completion-decl doesn't understand
                 ;; the var declarations in parameter lists :-(
@@ -1264,8 +1252,7 @@ indent of the current line in parameterlist."
                 (or (eq state 'declaration) (eq state 'paramlist)
                     (and (eq state 'defun)
                          (save-excursion
-                           (re-search-backward ")[ \t]*:"
-                                               (pascal-get-beg-of-line) t))))
+                           (re-search-backward ")[ \t]*:" (point-at-bol) t))))
                 (if (or (eq state 'paramlist) (eq state 'defun))
                     (pascal-beg-of-defun))
                 (nconc
@@ -1301,54 +1288,17 @@ indent of the current line in parameterlist."
 (defvar pascal-last-word-shown nil)
 (defvar pascal-last-completions nil)
 
-(defun pascal-complete-word ()
-  "Complete word at current point.
-\(See also `pascal-toggle-completions', `pascal-type-keywords',
-`pascal-start-keywords' and `pascal-separator-keywords'.)"
-  (interactive)
+(defun pascal-completions-at-point ()
   (let* ((b (save-excursion (skip-chars-backward "a-zA-Z0-9_") (point)))
         (e (save-excursion (skip-chars-forward "a-zA-Z0-9_") (point))))
+    (when (> e b)
+      (list b e #'pascal-completion))))
 
-    ;; Toggle-completions inserts whole labels
-    (if pascal-toggle-completions
-       (let* ((pascal-str (buffer-substring b e))
-               (allcomp (if (and pascal-toggle-completions
-                                 (string= pascal-last-word-shown pascal-str))
-                            pascal-last-completions
-                          (all-completions pascal-str 'pascal-completion))))
-         ;; Update entry number in list
-         (setq pascal-last-completions allcomp
-               pascal-last-word-numb
-               (if (>= pascal-last-word-numb (1- (length allcomp)))
-                   0
-                 (1+ pascal-last-word-numb)))
-         (setq pascal-last-word-shown (elt allcomp pascal-last-word-numb))
-         ;; Display next match or same string if no match was found
-         (if allcomp
-              (progn
-                (goto-char e)
-                (insert-before-markers pascal-last-word-shown)
-                (delete-region b e))
-           (message "(No match)")))
-      ;; The other form of completion does not necessarily do that.
-      (completion-in-region b e 'pascal-completion))))
-
-(defun pascal-show-completions ()
-  "Show all possible completions at current point."
-  (interactive)
-  (let* ((b (save-excursion (skip-chars-backward "a-zA-Z0-9_") (point)))
-        (e (save-excursion (skip-chars-forward "a-zA-Z0-9_") (point)))
-        (pascal-str (buffer-substring b e))
-        (allcomp (if (and pascal-toggle-completions
-                          (string= pascal-last-word-shown pascal-str))
-                     pascal-last-completions
-                   (all-completions pascal-str 'pascal-completion))))
-    ;; Show possible completions in a temporary buffer.
-    (with-output-to-temp-buffer "*Completions*"
-      (display-completion-list allcomp pascal-str))
-    ;; Wait for a keypress. Then delete *Completion*  window
-    (momentary-string-display "" (point))
-    (delete-window (get-buffer-window (get-buffer "*Completions*")))))
+(define-obsolete-function-alias 'pascal-complete-word
+  'completion-at-point "24.1")
+
+(define-obsolete-function-alias 'pascal-show-completions
+  'completion-help-at-point "24.1")
 
 
 (defun pascal-get-default-symbol ()
@@ -1554,5 +1504,4 @@ Pascal Outline mode provides some additional commands.
 
 (provide 'pascal)
 
-;; arch-tag: 04535136-fd93-40b4-a505-c9bebdc051f5
 ;;; pascal.el ends here