branch: externals/matlab-mode
commit e0d4b243765f09813fcf310d2480a17f4df3df81
Author: John Ciolfi <john.ciolfi...@gmail.com>
Commit: John Ciolfi <john.ciolfi...@gmail.com>

    matlab-sections: fix case of only one code section in a script file
---
 matlab-sections.el       | 56 ++++++++++++++++++++++++++----------------------
 tests/mstest-sections.el | 41 ++++++++++++++++++++++++++++++-----
 tests/mstest.el          |  4 +++-
 tests/sections_single.m  |  5 +++++
 4 files changed, 74 insertions(+), 32 deletions(-)

diff --git a/matlab-sections.el b/matlab-sections.el
index 4bb7b96d13..359bf1e409 100644
--- a/matlab-sections.el
+++ b/matlab-sections.el
@@ -129,32 +129,35 @@ the command `matlab-sections-minor-mode' to turn 
matlab-sections mode on."
   :type 'boolean
   :group 'matlab-sections)
 
-;; Function to obtain range of current section
+;; Function to obtain range of current code section
 
 (defun matlab-sections-range-function ()
-  "Function to call to return highlight range.
-The function of no args should return a cons section; its car value
-is the beginning position of highlight and its cdr value is the
-end position of highlight in the buffer.
-It should return nil if there's no region to be highlighted."
+  "Return range (START-PT . END-PT) of current MATLAB code section.
+nil is returned if there is no code section."
   (save-match-data
-    (let ((r-start (save-excursion
-                    (progn (end-of-line)
-                           (if (re-search-backward 
matlab-sections-section-break-regexp nil t)
-                               (progn (goto-char (match-beginning 0))
-                                      (point))
-                             (point-min)))))
-         (r-end (save-excursion
-                  (progn (end-of-line)
-                         (if (re-search-forward 
matlab-sections-section-break-regexp nil t)
-                             (progn (goto-char (match-beginning 0))
-                                    (point))
-                           (point-max))))))
-      (progn
-       ;; (message "cp is %s start is %s; end is %s" (point) r-start r-end)
-       (if (and (eq r-start (point-min)) (eq r-end (point-max)))
-           nil
-         `(,r-start . ,r-end))))))
+    (let* (in-section
+           (r-start (save-excursion
+                      (save-restriction
+                        (widen)
+                       (end-of-line)
+                       (if (re-search-backward 
matlab-sections-section-break-regexp nil t)
+                           (progn (setq in-section t)
+                                   (goto-char (match-beginning 0))
+                                  (point))
+                         (point-min)))))
+          (r-end (save-excursion
+                    (save-restriction
+                      (widen)
+                     (end-of-line)
+                     (if (re-search-forward 
matlab-sections-section-break-regexp nil t)
+                         (progn (setq in-section t)
+                                 (goto-char (match-beginning 0))
+                                (point))
+                       (point-max))))))
+      (if in-section
+          `(,r-start . ,r-end)
+       nil)
+      )))
 
 ;; Navigation
 
@@ -359,11 +362,12 @@ You can enable / disable super \"Windows\" key bindings 
by customizing
 
 ;;; Enable/Disable sections mode automatically
 ;;;###autoload
-(defun matlab-sections-auto-enable-on-mfile-type-fcn (mfile-type)
+(defun matlab-sections-auto-enable-on-mfile-type-fcn (mfile-type &optional 
skip-noninteractive)
   "Activate or deactivate sections mode based on MFILE-TYPE.
-This is a noop if `noninteractive' is t."
+This is a noop if SKIP-NONINTERACTIVE is nil and `noninteractive' is t."
   ;; Code sections "%% description" have some cost, thus don't activate in 
batch mode.
-  (unless noninteractive
+  (when (or skip-noninteractive
+            (not noninteractive))
     (let ((is-enabled matlab-sections-minor-mode)
           (enable (eq mfile-type 'script)))
       (if enable
diff --git a/tests/mstest-sections.el b/tests/mstest-sections.el
index 708ea22bd2..243e4940c4 100644
--- a/tests/mstest-sections.el
+++ b/tests/mstest-sections.el
@@ -27,6 +27,9 @@
 
 (defun mstest-sections-header ()
   "Exercise the regexp that identifies the \"%% section\" header."
+
+  (message "TEST: running (mstest-sections-header)")
+
   (let ((header-comments
          '("%% description of header" ;; typical section header
            "%%"   ;; section header without a description
@@ -50,15 +53,15 @@
     (dolist (non-header-comment non-header-comments)
       (when (string-match matlab-sections-section-break-regexp 
non-header-comment)
         (user-error "Matched \"%s\" as a section header comment when it should 
have failed to match"
-                    non-header-comment)))))
+                    non-header-comment))))
+
+  (message "PASSED: (mstest-single-sections-header)"))
 
 (defun mstest-sections ()
-  "Test \"%% section\" support."
+  "Test \"%% code section\" support."
 
   (message "TEST: running (mstest-sections)")
 
-  (mstest-sections-header)
-  
   (save-excursion
     (let ((sections-buf (find-file "sections.m")))
 
@@ -209,7 +212,35 @@ sectionOneB =
 
   (message "PASSED: (mstest-sections)"))
 
+(defun mstest-sections-single ()
+  "Test \"%% code section\" support on a script with one section."
+
+  (message "TEST: running (mstest-sections-single)")
+
+  (save-excursion
+    (let ((sections-single-buf (find-file "sections_single.m")))
+      (matlab-sections-auto-enable-on-mfile-type-fcn (matlab-guess-mfile-type) 
t)
+
+      (font-lock-mode 1)
+      (font-lock-flush (point-min) (point-max))
+      (font-lock-ensure (point-min) (point-max))
+      (font-lock-fontify-region (point-min) (point-max))
+
+      (goto-char (point-min))
+      (let ((test-point-desc "matlab-sections-single test case heading face")
+            (got (face-at-point))
+            (expected 'matlab-sections-highlight-face))
+        (when (not (eq got expected))
+          (user-error "Unexpected result for %s. Got '%s' expected '%s'"
+                      test-point-desc got expected))
+        (message "PASS: %s" test-point-desc))
+
+      (kill-buffer sections-single-buf)))
+
+  (message "PASSED: (mstest-sections-single)"))
+
+
 (provide 'mstest-sections)
 ;;; mstest-sections.el ends here
 
-;; LocalWords:  gmail defun buf
+;; LocalWords:  gmail defun buf dolist
diff --git a/tests/mstest.el b/tests/mstest.el
index 2de2416d8c..ed18c4126e 100644
--- a/tests/mstest.el
+++ b/tests/mstest.el
@@ -60,7 +60,9 @@
 
   (mstest-start)
   (mstest-capture)
+  (mstest-sections-header)
   (mstest-sections)
+  (mstest-sections-single)
   (mstest-completion)
   (mstest-error-parse)
   (mstest-debugger)
@@ -906,4 +908,4 @@ Searches for the text between the last prompt, and the 
previous prompt."
 ;; LocalWords:  stacklist TMP expectedstack ebstack ebstatus dolist bol eol 
expectedbreakpoints
 ;; LocalWords:  fileexp lineexp skipchecktxt fname progn dbln gud MSDB 
fileexplst lineexplst
 ;; LocalWords:  functionp commandp txtend listp tmp sinewave ctxt funcall 
mtest starttime totaltime
-;; LocalWords:  cco ctxte endpt fboundp fn MATLABSHELL
+;; LocalWords:  cco ctxte endpt fboundp fn MATLABSHELL emacsstripremote
diff --git a/tests/sections_single.m b/tests/sections_single.m
new file mode 100644
index 0000000000..1a4451595f
--- /dev/null
+++ b/tests/sections_single.m
@@ -0,0 +1,5 @@
+%% one section
+
+sectionOneA = 1
+sectionOneB = 2
+

Reply via email to