(modify-syntax-entry ?\n ">" table)
(setq octave-mode-syntax-table table)))
+(defvar octave-auto-indent nil
+ "*Non-nil means indent line after a semicolon or space in Octave mode.")
+
(defvar octave-auto-newline nil
- "*Non-nil means automatically newline after a semicolon in Octave mode.")
+ "*Non-nil means insert newline after a semicolon in Octave mode.")
(defvar octave-blink-matching-block t
"*Control the blinking of matching Octave block keywords.
'(("for" . ("end" "endfor"))
("function" . ("end" "endfunction"))
("if" . ("else" "elseif" "end" "endif"))
- ("switch" . ("case" "end" "endswitch" "otherwise"))
+ ("switch" . ("case" "otherwise" "end" "endswitch"))
("try" . ("catch" "end" "end_try_catch"))
("unwind_protect" . ("unwind_protect_cleanup" "end"
"end_unwind_protect"))
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.
(error nil))
(< pos (point)))))))
+(defun octave-maybe-insert-continuation-string ()
+ (if (or (octave-in-comment-p)
+ (save-excursion
+ (beginning-of-line)
+ (looking-at octave-continuation-regexp)))
+ nil
+ (delete-horizontal-space)
+ (insert (concat " " octave-continuation-string))))
+
;;; Comments
(defun octave-comment-region (beg end &optional arg)
"Comment or uncomment each line in the region as Octave code.
((and (looking-at octave-block-end-regexp)
(octave-not-in-string-or-comment-p))
(setq icol (- icol (octave-block-end-offset))))
- ((looking-at "\\s<\\s<\\s<\\S<")
+ ((or (looking-at "\\s<\\s<\\s<\\S<")
+ (octave-before-magic-comment-p))
(setq icol (list 0 icol)))
((looking-at "\\s<\\S<")
(setq icol (list comment-column icol)))))
(* octave-block-offset
(if (string-match (match-string 0) "switch") 2 1))))
+(defun octave-before-magic-comment-p ()
+ (save-excursion
+ (beginning-of-line)
+ (and (bobp) (looking-at "\\s-*#!"))))
+
(defun octave-comment-indent ()
- (if (looking-at "\\s<\\s<\\s<")
+ (if (or (looking-at "\\s<\\s<\\s<")
+ (octave-before-magic-comment-p))
0
(if (looking-at "\\s<\\s<")
(calculate-octave-indent)
\f
;;; Filling
(defun octave-auto-fill ()
- "Perform auto-fill in Octave mode."
- (if (> (current-column) (current-fill-column))
- (if (octave-in-comment-p)
- (do-auto-fill)
- (if (> (current-column) (current-fill-column))
- (let ((fill-column (- (current-fill-column)
- (length octave-continuation-string))))
- (do-auto-fill)
- (save-excursion
- (forward-line -1)
- (end-of-line)
- (insert (concat " " octave-continuation-string)))
- (indent-according-to-mode))))))
+ "Perform auto-fill in Octave mode.
+Returns nil if no feasible place to break the line could be found, and t
+otherwise."
+ (let (fc give-up)
+ (if (or (null (setq fc (current-fill-column)))
+ (save-excursion
+ (beginning-of-line)
+ (and auto-fill-inhibit-regexp
+ (looking-at auto-fill-inhibit-regexp))))
+ nil ; Can't do anything
+ (if (and (not (octave-in-comment-p))
+ (> (current-column) fc))
+ (setq fc (- fc (+ (length octave-continuation-string) 1))))
+ (while (and (not give-up) (> (current-column) fc))
+ (let* ((opoint (point))
+ (fpoint
+ (save-excursion
+ (move-to-column (+ fc 1))
+ (skip-chars-backward "^ \t\n")
+ ;; If we're at the beginning of the line, break after
+ ;; the first word
+ (if (bolp)
+ (re-search-forward "[ \t]" opoint t))
+ ;; If we're in a comment line, don't break after the
+ ;; comment chars
+ (if (save-excursion
+ (skip-syntax-backward " <")
+ (bolp))
+ (re-search-forward "[ \t]" (octave-point 'eol)
+ 'move))
+ ;; If we're not in a comment line and just ahead the
+ ;; continuation string, don't break here.
+ (if (and (not (octave-in-comment-p))
+ (looking-at
+ (concat "\\s-*"
+ (regexp-quote
+ octave-continuation-string)
+ "\\s-*$")))
+ (end-of-line))
+ (skip-chars-backward " \t")
+ (point))))
+ (if (save-excursion
+ (goto-char fpoint)
+ (not (or (bolp) (eolp))))
+ (let ((prev-column (current-column)))
+ (if (save-excursion
+ (skip-chars-backward " \t")
+ (= (point) fpoint))
+ (progn
+ (octave-maybe-insert-continuation-string)
+ (indent-new-comment-line t))
+ (save-excursion
+ (goto-char fpoint)
+ (octave-maybe-insert-continuation-string)
+ (indent-new-comment-line t)))
+ (if (>= (current-column) prev-column)
+ (setq give-up t)))
+ (setq give-up t))))
+ (not give-up))))
(defun octave-fill-paragraph (&optional arg)
"Fill paragraph of Octave code, handling Octave comments."
(delete-region (match-beginning 0) (match-end 0))
(fixup-whitespace)
(move-to-column cfc))))
+ ;; We might also try to combine continued code lines> Perhaps
+ ;; some other time ...
(skip-chars-forward "^ \t\n")
(delete-horizontal-space)
(if (or (< (current-column) cfc)
(and (= (current-column) cfc) (eolp)))
(forward-line 1)
(if (not (eolp)) (insert " "))
- (octave-auto-fill))))
+ (or (octave-auto-fill)
+ (forward-line 1)))))
t)))
\f
(defun octave-electric-semi ()
"Insert a semicolon in Octave mode.
-Always reindent the line. Insert a newline if `octave-auto-newline' is
-non-nil."
+Maybe expand abbrevs and blink matching block open keywords.
+Reindent the line of `octave-auto-indent' is non-nil.
+Insert a newline if `octave-auto-newline' is non-nil."
(interactive)
(if (not (octave-not-in-string-or-comment-p))
(insert ";")
(if abbrev-mode (expand-abbrev))
(if octave-blink-matching-block
(octave-blink-matching-block-open))
- (indent-according-to-mode)
+ (if octave-auto-indent
+ (indent-according-to-mode))
(insert ";")
(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."
+Maybe expand abbrevs and blink matching block open keywords.
+Reindent the line of `octave-auto-indent' is non-nil."
(interactive)
(setq last-command-char ? )
(if (not (octave-not-in-string-or-comment-p))
(if abbrev-mode (expand-abbrev))
(if octave-blink-matching-block
(octave-blink-matching-block-open))
- (if (save-excursion
- (skip-syntax-backward " ")
- (not (bolp)))
+ (if (and octave-auto-indent
+ (save-excursion
+ (skip-syntax-backward " ")
+ (not (bolp))))
(indent-according-to-mode))
(self-insert-command 1)))
octave-maintainer-address
(concat "Emacs version " emacs-version)
(list
+ 'octave-auto-indent
'octave-auto-newline
'octave-blink-matching-block
'octave-block-offset