]> code.delx.au - gnu-emacs/blobdiff - lisp/progmodes/octave-mod.el
Add coding cookie
[gnu-emacs] / lisp / progmodes / octave-mod.el
index 2402540bfb68a6576c9c4ab2d8b79a92b3ff9bd1..79b3fcee720ffd58a3ee9dae6116ce9a352dc3d2 100644 (file)
@@ -1,11 +1,10 @@
 ;;; octave-mod.el --- editing Octave source files under Emacs
 
-;; Copyright (C) 1997, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
-;; Free Software Foundation, Inc.
+;; Copyright (C) 1997, 2001-2012 Free Software Foundation, Inc.
 
 ;; Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at>
-;; Author: John Eaton <jwe@octave.org>
-;; Maintainer: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at>
+;;        John Eaton <jwe@octave.org>
+;; Maintainer: FSF
 ;; Keywords: languages
 
 ;; This file is part of GNU Emacs.
@@ -151,8 +150,8 @@ All Octave abbrevs start with a grave accent (`)."
   "Builtin variables in Octave.")
 
 (defvar octave-function-header-regexp
-  (concat "^\\s-*\\<\\(function\\)\\>"
-         "\\([^=;\n]*=[ \t]*\\|[ \t]*\\)\\(\\w+\\)\\>")
+  (concat "^\\s-*\\_<\\(function\\)\\_>"
+         "\\([^=;\n]*=[ \t]*\\|[ \t]*\\)\\(\\(?:\\w\\|\\s_\\)+\\)\\_>")
   "Regexp to match an Octave function header.
 The string `function' and its name are given by the first and third
 parenthetical grouping.")
@@ -160,10 +159,10 @@ parenthetical grouping.")
 (defvar octave-font-lock-keywords
   (list
    ;; Fontify all builtin keywords.
-   (cons (concat "\\<\\("
+   (cons (concat "\\_<\\("
                 (regexp-opt (append octave-reserved-words
                                      octave-text-functions))
-                "\\)\\>")
+                "\\)\\_>")
         'font-lock-keyword-face)
    ;; Fontify all builtin operators.
    (cons "\\(&\\||\\|<=\\|>=\\|==\\|<\\|>\\|!=\\|!\\)"
@@ -171,7 +170,7 @@ parenthetical grouping.")
             'font-lock-builtin-face
           'font-lock-preprocessor-face))
    ;; Fontify all builtin variables.
-   (cons (concat "\\<" (regexp-opt octave-variables) "\\>")
+   (cons (concat "\\_<" (regexp-opt octave-variables) "\\_>")
         'font-lock-variable-name-face)
    ;; Fontify all function declarations.
    (list octave-function-header-regexp
@@ -183,7 +182,7 @@ parenthetical grouping.")
   (goto-char start)
   (octave-syntax-propertize-sqs end)
   (funcall (syntax-propertize-rules
-  ;; Try to distinguish the string-quotes from the transpose-quotes.
+            ;; Try to distinguish the string-quotes from the transpose-quotes.
             ("[[({,; ]\\('\\)"
              (1 (prog1 "\"'" (octave-syntax-propertize-sqs end)))))
            (point) end))
@@ -191,15 +190,15 @@ parenthetical grouping.")
 (defun octave-syntax-propertize-sqs (end)
   "Propertize the content/end of single-quote strings."
   (when (eq (nth 3 (syntax-ppss)) ?\')
-        ;; A '..' string.
+    ;; A '..' string.
     (when (re-search-forward
            "\\(?:\\=\\|[^']\\)\\(?:''\\)*\\('\\)\\($\\|[^']\\)" end 'move)
       (goto-char (match-beginning 2))
-            (when (eq (char-before (match-beginning 1)) ?\\)
-              ;; Backslash cannot escape a single quote.
-              (put-text-property (1- (match-beginning 1)) (match-beginning 1)
-                                 'syntax-table (string-to-syntax ".")))
-            (put-text-property (match-beginning 1) (match-end 1)
+      (when (eq (char-before (match-beginning 1)) ?\\)
+        ;; Backslash cannot escape a single quote.
+        (put-text-property (1- (match-beginning 1)) (match-beginning 1)
+                           'syntax-table (string-to-syntax ".")))
+      (put-text-property (match-beginning 1) (match-end 1)
                          'syntax-table (string-to-syntax "\"'")))))
 
 (defcustom inferior-octave-buffer "*Inferior Octave*"
@@ -212,9 +211,6 @@ parenthetical grouping.")
 (defvar octave-mode-map
   (let ((map (make-sparse-keymap)))
     (define-key map "`" 'octave-abbrev-start)
-    (define-key map ";" 'octave-electric-semi)
-    (define-key map " " 'octave-electric-space)
-    (define-key map "\n" 'octave-reindent-then-newline-and-indent)
     (define-key map "\e\n" 'octave-indent-new-comment-line)
     (define-key map "\M-\C-q" 'octave-indent-defun)
     (define-key map "\C-c\C-b" 'octave-submit-bug-report)
@@ -227,7 +223,8 @@ parenthetical grouping.")
     (define-key map "\C-c]" 'smie-close-block)
     (define-key map "\C-c/" 'smie-close-block)
     (define-key map "\C-c\C-f" 'octave-insert-defun)
-    (define-key map "\C-c\C-h" 'octave-help)
+    ;; FIXME: free C-h so it can do the describe-prefix-bindings.
+    (define-key map "\C-c\C-h" 'info-lookup-symbol)
     (define-key map "\C-c\C-il" 'octave-send-line)
     (define-key map "\C-c\C-ib" 'octave-send-block)
     (define-key map "\C-c\C-if" 'octave-send-defun)
@@ -240,6 +237,7 @@ parenthetical grouping.")
     (define-key map "\C-c\C-i\C-f" 'octave-send-defun)
     (define-key map "\C-c\C-i\C-r" 'octave-send-region)
     (define-key map "\C-c\C-i\C-s" 'octave-show-process-buffer)
+    ;; FIXME: free C-h so it can do the describe-prefix-bindings.
     (define-key map "\C-c\C-i\C-h" 'octave-hide-process-buffer)
     (define-key map "\C-c\C-i\C-k" 'octave-kill-process)
     map)
@@ -303,8 +301,8 @@ parenthetical grouping.")
     ;; Was "w" for abbrevs, but now that it's not necessary any more,
     (modify-syntax-entry ?\` "."  table)
     (modify-syntax-entry ?\" "\"" table)
-    (modify-syntax-entry ?. "w"   table)
-    (modify-syntax-entry ?_ "w"   table)
+    (modify-syntax-entry ?. "_"   table)
+    (modify-syntax-entry ?_ "_"   table)
     ;; The "b" flag only applies to the second letter of the comstart
     ;; and the first letter of the comend, i.e. the "4b" below is ineffective.
     ;; If we try to put `b' on the single-line comments, we get a similar
@@ -319,16 +317,6 @@ parenthetical grouping.")
     table)
   "Syntax table in use in `octave-mode' buffers.")
 
-(defcustom octave-auto-indent nil
-  "Non-nil means indent line after a semicolon or space in Octave mode."
-  :type 'boolean
-  :group 'octave)
-
-(defcustom octave-auto-newline nil
-  "Non-nil means automatically newline after a semicolon in Octave mode."
-  :type 'boolean
-  :group 'octave)
-
 (defcustom octave-blink-matching-block t
   "Control the blinking of matching Octave block keywords.
 Non-nil means show matching begin of block when inserting a space,
@@ -536,7 +524,7 @@ Non-nil means always go to the next Octave code line after sending."
        ;; (if (smie-parent-p "switch") 4)
        0))))
 
-(defvar electric-indent-chars)
+(defvar electric-layout-rules)
 
 ;;;###autoload
 (define-derived-mode octave-mode prog-mode "Octave"
@@ -567,14 +555,6 @@ Keybindings
 Variables you can use to customize Octave mode
 ==============================================
 
-`octave-auto-indent'
-  Non-nil means indent current line after a semicolon or space.
-  Default is nil.
-
-`octave-auto-newline'
-  Non-nil means auto-insert a newline and indent after a semicolon.
-  Default is nil.
-
 `octave-blink-matching-block'
   Non-nil means show matching begin of block when inserting a space,
   newline or semicolon after an else or end keyword.  Default is t.
@@ -636,6 +616,9 @@ including a reproducible test case and send the message."
 
   (set (make-local-variable 'electric-indent-chars)
        (cons ?\; electric-indent-chars))
+  ;; IIUC matlab-mode takes the opposite approach: it makes RET insert
+  ;; a ";" at those places where it's correct (i.e. outside of parens).
+  (set (make-local-variable 'electric-layout-rules) '((?\; . after)))
 
   (set (make-local-variable 'comment-start) octave-comment-start)
   (set (make-local-variable 'comment-end) "")
@@ -673,35 +656,21 @@ including a reproducible test case and send the message."
        'octave-beginning-of-defun)
 
   (easy-menu-add octave-mode-menu)
-  (octave-initialize-completions)
-  (run-mode-hooks 'octave-mode-hook))
-
-(defvar info-lookup-mode)
-
-(defun octave-help ()
-  "Get help on Octave symbols from the Octave info files.
-Look up symbol in the function, operator and variable indices of the info files."
-  (let ((info-lookup-mode 'octave-mode))
-    (call-interactively 'info-lookup-symbol)))
+  (octave-initialize-completions))
 \f
 ;;; Miscellaneous useful functions
 
 (defsubst octave-in-comment-p ()
   "Return t if point is inside an Octave comment."
-  (save-excursion
-    ;; FIXME: use syntax-ppss?
-    (nth 4 (parse-partial-sexp (line-beginning-position) (point)))))
+  (nth 4 (syntax-ppss)))
 
 (defsubst octave-in-string-p ()
   "Return t if point is inside an Octave string."
-  (save-excursion
-    ;; FIXME: use syntax-ppss?
-    (nth 3 (parse-partial-sexp (line-beginning-position) (point)))))
+  (nth 3 (syntax-ppss)))
 
 (defsubst octave-not-in-string-or-comment-p ()
   "Return t if point is not inside an Octave string or comment."
-  ;; FIXME: Use syntax-ppss?
-  (let ((pps (parse-partial-sexp (line-beginning-position) (point))))
+  (let ((pps (syntax-ppss)))
     (not (or (nth 3 pps) (nth 4 pps)))))
 
 
@@ -718,7 +687,6 @@ Look up symbol in the function, operator and variable indices of the info files.
       nil
     (delete-horizontal-space)
     (insert (concat " " octave-continuation-string))))
-
 \f
 ;;; Indentation
 
@@ -736,7 +704,7 @@ The new line is properly indented."
     (error "Cannot split a code line inside a string"))
    (t
     (insert (concat " " octave-continuation-string))
-    (octave-reindent-then-newline-and-indent))))
+    (reindent-then-newline-and-indent))))
 
 (defun octave-indent-defun ()
   "Properly indent the Octave function which contains point."
@@ -829,7 +797,7 @@ The block marked is the one that contains point or follows point."
   (unless (or (looking-at "\\s(")
               (save-excursion
                 (let* ((token (funcall smie-forward-token-function))
-                       (level (assoc token smie-op-levels)))
+                       (level (assoc token smie-grammar)))
                   (and level (null (cadr level))))))
     (backward-up-list 1))
   (mark-sexp))
@@ -844,11 +812,11 @@ Returns t unless search stops at the beginning or end of the buffer."
         (found nil)
          (case-fold-search nil))
     (and (not (eobp))
-        (not (and (> arg 0) (looking-at "\\<function\\>")))
+        (not (and (> arg 0) (looking-at "\\_<function\\_>")))
         (skip-syntax-forward "w"))
     (while (and (/= arg 0)
                (setq found
-                     (re-search-backward "\\<function\\>" inc)))
+                     (re-search-backward "\\_<function\\_>" inc)))
       (if (octave-not-in-string-or-comment-p)
          (setq arg (- arg inc))))
     (if found
@@ -919,7 +887,7 @@ otherwise."
            (setq give-up t))))
       (not give-up))))
 
-(defun octave-fill-paragraph (&optional arg)
+(defun octave-fill-paragraph (&optional _arg)
   "Fill paragraph of Octave code, handling Octave comments."
   ;; FIXME: difference with generic fill-paragraph:
   ;; - code lines are only split, never joined.
@@ -1001,61 +969,18 @@ otherwise."
 
 (defun octave-completion-at-point-function ()
   "Find the text to complete and the corresponding table."
-  (let* ((beg (save-excursion (backward-sexp 1) (point)))
+  (let* ((beg (save-excursion (skip-syntax-backward "w_") (point)))
          (end (point)))
     (if (< beg (point))
         ;; Extend region past point, if applicable.
-        (save-excursion (goto-char beg) (forward-sexp 1)
-                        (setq end (max end (point)))))
+        (save-excursion (skip-syntax-forward "w_")
+                        (setq end (point))))
     (list beg end octave-completion-alist)))
 
-(defun octave-complete-symbol ()
-  "Perform completion on Octave symbol preceding point.
-Compare that symbol against Octave's reserved words and builtin
-variables."
-  (interactive)
-  (apply 'completion-in-region (octave-completion-at-point-function)))
+(define-obsolete-function-alias 'octave-complete-symbol
+  'completion-at-point "24.1")
 \f
 ;;; Electric characters && friends
-(defun octave-reindent-then-newline-and-indent ()
-  "Reindent current Octave line, insert newline, and indent the new line.
-If Abbrev mode is on, expand abbrevs first."
-  ;; FIXME: None of this is Octave-specific.
-  (interactive)
-  (reindent-then-newline-and-indent))
-
-(defun octave-electric-semi ()
-  "Insert a semicolon in Octave mode.
-Maybe expand abbrevs and blink matching block open keywords.
-Reindent the line if `octave-auto-indent' is non-nil.
-Insert a newline if `octave-auto-newline' is non-nil."
-  (interactive)
-  (setq last-command-event ?\;)
-  (if (not (octave-not-in-string-or-comment-p))
-      (self-insert-command 1)
-    (if octave-auto-indent
-       (indent-according-to-mode))
-    (self-insert-command 1)
-    (if octave-auto-newline
-       (newline-and-indent))))
-
-(defun octave-electric-space ()
-  "Insert a space in Octave mode.
-Maybe expand abbrevs and blink matching block open keywords.
-Reindent the line if `octave-auto-indent' is non-nil."
-  (interactive)
-  (setq last-command-event ? )
-  (if (and octave-auto-indent
-          (not (octave-not-in-string-or-comment-p)))
-      (progn
-       (indent-according-to-mode)
-       (self-insert-command 1))
-    (if (and octave-auto-indent
-            (save-excursion
-              (skip-syntax-backward " ")
-              (not (bolp))))
-       (indent-according-to-mode))
-    (self-insert-command 1)))
 
 (defun octave-abbrev-start ()
   "Start entering an Octave abbreviation.
@@ -1064,18 +989,13 @@ If Abbrev mode is turned on, typing ` (grave accent) followed by ? or
 executed normally.
 Note that all Octave mode abbrevs start with a grave accent."
   (interactive)
-  (if (not abbrev-mode)
-      (self-insert-command 1)
-    (let (c)
-      (insert last-command-event)
-      (if (if (featurep 'xemacs)
-             (or (eq (event-to-character (setq c (next-event))) ??)
-                 (eq (event-to-character c) help-char))
-           (or (eq (setq c (read-event)) ??)
-               (eq c help-char)))
-         (let ((abbrev-table-name-list '(octave-abbrev-table)))
-           (list-abbrevs))
-       (setq unread-command-events (list c))))))
+  (self-insert-command 1)
+  (when abbrev-mode
+    (set-temporary-overlay-map
+     (let ((map (make-sparse-keymap)))
+       (define-key map [??] 'list-abbrevs)
+       (define-key map (vector help-char) 'list-abbrevs)
+       map))))
 
 (define-skeleton octave-insert-defun
   "Insert an Octave function skeleton.
@@ -1213,8 +1133,6 @@ code line."
     octave-maintainer-address
     (concat "Emacs version " emacs-version)
     (list
-     'octave-auto-indent
-     'octave-auto-newline
      'octave-blink-matching-block
      'octave-block-offset
      'octave-comment-char
@@ -1228,5 +1146,4 @@ code line."
 
 (provide 'octave-mod)
 
-;; arch-tag: 05f1ce09-be87-4c00-803e-4919ffa26c23
 ;;; octave-mod.el ends here