Skip to content

Commit

Permalink
Merge pull request #43 from markhepburn/master
Browse files Browse the repository at this point in the history
octave-mode expansions
  • Loading branch information
magnars committed Nov 5, 2012
2 parents 0a5008c + 99e9b89 commit d777ff0
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 0 deletions.
1 change: 1 addition & 0 deletions expand-region.el
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ before calling `er/expand-region' for the first time."
(eval-after-load "js3-mode" '(require 'js-mode-expansions))
(eval-after-load "latex" '(require 'latex-mode-expansions))
(eval-after-load "nxml-mode" '(require 'nxml-mode-expansions))
(eval-after-load "octave-mod" '(require 'octave-expansions))
(eval-after-load "python" '(progn
(when expand-region-guess-python-mode
(expand-region-guess-python-mode))
Expand Down
99 changes: 99 additions & 0 deletions features/octave-mode-expansions.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
Feature: octave-mod expansions
In order to quickly and precisely mark octave units
As an Emacs user
I want to expand to them

Scenario: Mark block from inside
Given I turn on octave-mode
And there is no region selected
When I insert:
"""
exprBefore;
for i=1:n,
something;
end;
exprAfter;
"""
And I go to point "26"
And I press "C-@"
And I press "C-@"
Then the region should be:
"""
for i=1:n,
something;
end
"""


Scenario: Mark block when looking at it
Given I turn on octave-mode
And there is no region selected
When I insert:
"""
exprBefore;
for i=1:n,
something;
end;
exprAfter;
"""
And I go to point "13"
And I press "C-@"
And I press "C-@"
Then the region should be:
"""
for i=1:n,
something;
end
"""


Scenario: Mark block when looking at it inside another block
Given I turn on octave-mode
And there is no region selected
When I insert:
"""
exprBefore;
for i=1:n,
for j=i:k,
something;
end;
end;
exprAfter;
"""
And I go to point "26"
And I press "C-@"
And I press "C-@"
Then the region should be:
"""
for j=i:k,
something;
end
"""


Scenario: Mark block from inside while looking at another
Given I turn on octave-mode
And there is no region selected
When I insert:
"""
exprBefore;
for i=1:n,
for j=i:k,
something;
end;
end;
exprAfter;
"""
And I go to point "26"
And I press "C-@"
And I press "C-@"
And I press "C-@"
Then the region should be:
"""
for i=1:n,
for j=i:k,
something;
end;
end
"""

78 changes: 78 additions & 0 deletions octave-expansions.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
;;; octave-expansions.el --- octave-mode expansions for expand-region

;; Copyright (C) 2012 Mark Hepburn

;; Author: Mark Hepburn
;; Keywords: marking region

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;; Feel free to contribute any other expansions for Octave at
;;
;; https://github.com/magnars/expand-region.el

;;; Code:

(require 'expand-region-core)
(require 'octave-mod)

;;; Octave-mod received a major rewrite between versions 23 and 24 of
;;; Emacs, for example using the new smie package instead of
;;; hand-coding a lot of motion commands. Unfortunately for our
;;; purposes here, in the process the behaviour of `octave-mark-block'
;;; changed slightly. So, in order to behave identically across both
;;; versions we need to check which is which in a few places and
;;; adjust accordingly:
(defconst er/old-octave-mod-p (fboundp 'octave-up-block))

(defalias 'er/up-block
(if er/old-octave-mod-p 'octave-up-block 'up-list))

(defun er/octave-mark-up-block ()
"Mark the containing block, assuming the current block has
already been marked."
(interactive)
(when (use-region-p)
(when (< (point) (mark))
(exchange-point-and-mark))
(er/up-block -1) ; -1 means backwards, ie to the front
(octave-mark-block)))

(defun er/octave-mark-block ()
"Not for general use; this is a work-around for the different
behaviour of `octave-mark-block' between emacs versions 23 and
24."
(interactive)
(forward-word)
(octave-mark-block))

(defun er/add-octave-expansions ()
"Adds octave/matlab-specific expansions for buffers in octave-mode"
(let ((try-expand-list-additions (if er/old-octave-mod-p
'(octave-mark-block
er/octave-mark-up-block
octave-mark-defun)
'(octave-mark-block
er/octave-mark-block
er/octave-mark-up-block
mark-defun))))
(set (make-local-variable 'er/try-expand-list)
(append er/try-expand-list try-expand-list-additions))))

(add-hook 'octave-mode-hook 'er/add-octave-expansions)

(provide 'octave-expansions)

0 comments on commit d777ff0

Please sign in to comment.