Skip to content

Commit b2184fb

Browse files
authored
Merge pull request #26 from Hi-Angel/implement-defun-beginning
Implement going to the beginning of a defun
2 parents 432be8f + b6d68e3 commit b2184fb

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

purescript-mode.el

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ see documentation for that variable for more details."
340340
(set (make-local-variable 'dabbrev-case-distinction) nil)
341341
(set (make-local-variable 'dabbrev-case-replace) nil)
342342
(set (make-local-variable 'dabbrev-abbrev-char-regexp) "\\sw\\|[.]")
343+
(setq-local beginning-of-defun-function 'purescript-beginning-of-defun)
343344
(setq prettify-symbols-alist purescript-font-lock-prettify-symbols-alist)
344345
(when (bound-and-true-p purescript-font-lock-symbols)
345346
(warn "`purescript-font-lock-symbols' is obsolete: please enable `prettify-symbols-mode' locally or globally instead."))
@@ -470,6 +471,35 @@ Brings up the documentation for purescript-mode-hook."
470471
(format " [ %s .. ]" (purescript-string-take (purescript-trim (cadr lines)) 10))
471472
""))))))
472473

474+
(defun purescript-current-line-string ()
475+
"Returns current line as a string."
476+
(buffer-substring-no-properties (line-beginning-position) (line-end-position)))
477+
478+
(defun purescript-beginning-of-defun-single ()
479+
(while (and (looking-at-p (rx (* space) eol))
480+
(not (bobp)))
481+
(forward-line -1)) ; can't get indentation on an empty line
482+
(let ((indent-level (current-indentation)))
483+
(while
484+
(not
485+
(or (ignore (forward-line -1)) ; do-while implementation
486+
(bobp)
487+
(and (< (current-indentation) indent-level)
488+
(string-match-p
489+
(rx (*? anything)
490+
(or bol space word-boundary) "="
491+
(or eol space word-boundary))
492+
;; Emacs doesn't allow to limit search just to the curent line
493+
;; barring the regex eol, but eol overly complicates matching.
494+
(purescript-current-line-string))))))))
495+
496+
(defun purescript-beginning-of-defun (&optional repeat)
497+
"Move point to the beginning of the current PureScript function."
498+
(purescript-beginning-of-defun-single)
499+
(dotimes (_ (if repeat
500+
(- repeat 1) ; the function was already called once
501+
0))
502+
(purescript-beginning-of-defun-single)))
473503

474504
(provide 'purescript-mode)
475505

0 commit comments

Comments
 (0)