]> git.eshelyaron.com Git - emacs.git/commitdiff
* lisp/progmodes/octave.el: Register on auto-mode-alist
authorStefan Monnier <monnier@iro.umontreal.ca>
Tue, 16 Oct 2018 01:24:14 +0000 (21:24 -0400)
committerStefan Monnier <monnier@iro.umontreal.ca>
Tue, 16 Oct 2018 01:24:14 +0000 (21:24 -0400)
(octave-maybe-mode): New function.

etc/NEWS
lisp/progmodes/octave.el

index 946a823173a193846754784d594c5fdb43477532..b46dcae9c23dbb2d0b97fb2ee41b23045892f6d6 100644 (file)
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -287,6 +287,10 @@ in (info "(emacs) Directory Variables")
 \f
 * Changes in Specialized Modes and Packages in Emacs 27.1
 
+** Octave mode
+The mode is automatically enabled in files that start with the
+'function' keyword.
+
 ** project.el
 *** New commands project-search and project-query-replace
 
index 984bb73c73ed54ad3bd9cff3c401d5d6156c9aa9..13510eef805b0347da076b548c94087d2417151f 100644 (file)
@@ -170,8 +170,8 @@ parenthetical grouping.")
     (modify-syntax-entry ?. "."   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.
+    ;; The "b" flag only applies to the second letter of the comstart and
+    ;; the first letter of the comend, i.e. a "4b" below would be ineffective.
     ;; If we try to put `b' on the single-line comments, we get a similar
     ;; problem where the % and # chars appear as first chars of the 2-char
     ;; comend, so the multi-line ender is also turned into style-b.
@@ -533,6 +533,27 @@ Non-nil means always go to the next Octave code line after sending."
 
 (defvar electric-layout-rules)
 
+;; FIXME: cc-mode.el also adds an entry for .m files, mapping them to
+;; objc-mode.  We here rely on the fact that loaddefs.el is filled in
+;; alphabetical order, so cc-mode.el comes before octave-mode.el, which lets
+;; our entry come first!
+;;;###autoload (add-to-list 'auto-mode-alist '("\\.m\\'" . octave-maybe-mode))
+
+;;;###autoload
+(defun octave-maybe-mode ()
+  "Select `octave-mode' if the current buffer seems to hold Octave code."
+  (if (save-excursion
+        (with-syntax-table octave-mode-syntax-table
+          (goto-char (point-min))
+          (forward-comment (point-max))
+          ;; FIXME: What about Octave files which don't start with "function"?
+          (looking-at "function")))
+      (octave-mode)
+    (let ((x (rassq 'octave-maybe-mode auto-mode-alist)))
+      (when x
+        (let ((auto-mode-alist (remove x auto-mode-alist)))
+          (set-auto-mode))))))
+
 ;;;###autoload
 (define-derived-mode octave-mode prog-mode "Octave"
   "Major mode for editing Octave code.