Skip to content

Commit

Permalink
Fix error during indentation of first non-empty line
Browse files Browse the repository at this point in the history
Fixes #13

The package was throwing an error (or hanging, rather) when starting a
new file with a blank line at the very begining. To reproduce:

- Create a new hamlet file and open it
- Hit "o" (newline-and-indent)
- Emacs hangs until C-g is pressed

The error is caused by trying to count the indentation level on a line
that doesn't have any characters on it. There's a two part fix here,
mostly for posterity:

1. Prematurely check if we're on the first non-empty line and use
'noindent.
2. When calcuating indentation of previous lines, make sure that a
character exists on the line before calculating the number of empty
spaces.

There still seems to be a couple of corner cases around auto-indenting
and just various setups, but this should fix the immediate issue.
  • Loading branch information
CodyReichert committed Jul 4, 2018
1 parent f95444a commit c442eee
Showing 1 changed file with 28 additions and 15 deletions.
43 changes: 28 additions & 15 deletions shakespeare-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,13 @@
(defun shakespeare-hamlet-mode--count-indent ()
"It just counts indent of current line."
(let ((count 0))
(save-excursion
(beginning-of-line)
(while (string-match (rx blank) (char-to-string (char-after)))
(setq count (+ count 1))
(forward-char))
count)))
(unless (eq 'nil (char-after))
(save-excursion
(beginning-of-line)
(while (string-match (rx blank) (char-to-string (char-after)))
(setq count (+ count 1))
(forward-char))))
count))

(defun shakespeare-hamlet-mode--set-indent (indent-count)
"Set indent of current line to INDENT-COUNT."
Expand All @@ -107,6 +108,16 @@
t
nil))

(defun shakespeare-mode--first-non-empty-line-p ()
"Return t if this is the first non-empty-line."
(let ((emp t))
(save-excursion
(while (not (= 1 (line-number-at-pos)))
(forward-line -1)
(when (not (shakespeare-hamlet-mode--blank-line-p))
(setq emp nil))))
emp))

(defun shakespeare-hamlet-mode--count-indent-of-previous-line ()
"Count indent of previous non-blank line."
(save-excursion
Expand All @@ -127,16 +138,18 @@

(defun shakespeare-hamlet-mode-indent-line ()
"Cycle the indent like hyai-mode.
If current line's indent is deeper than previous line's, set current line's indent to zero.
Else, indent current line deeper."
If current line's indent is deeper than previous line's, set current
line's indent to zero. Else, indent current line deeper."
(interactive)
(let* ((indent-of-previous-line (shakespeare-hamlet-mode--count-indent-of-previous-line))
(maximum-indent (+ sgml-basic-offset indent-of-previous-line))
(indent-of-current-line (shakespeare-hamlet-mode--count-indent)))

(if (>= indent-of-current-line maximum-indent)
(shakespeare-hamlet-mode--set-indent 0)
(shakespeare-hamlet-mode--indent-deeper))))
(if (shakespeare-mode--first-non-empty-line-p)
'noindent
(let* ((indent-of-previous-line (shakespeare-hamlet-mode--count-indent-of-previous-line))
(maximum-indent (+ sgml-basic-offset indent-of-previous-line))
(indent-of-current-line (shakespeare-hamlet-mode--count-indent)))

(if (>= indent-of-current-line maximum-indent)
(shakespeare-hamlet-mode--set-indent 0)
(shakespeare-hamlet-mode--indent-deeper)))))

(defun shakespeare-hamlet-mode-indent-backward ()
"Similar to `shakespeare-hamlet-mode-indent-line', but cycle inversely."
Expand Down

0 comments on commit c442eee

Please sign in to comment.