Skip to content

Commit c1e7ffe

Browse files
committed
Implement going to the beginning of a defun
This implements support for `(beginning-of-defun)`. As a side note, the match is done inside a single line string because I tried implementing a regexp to make Emacs to restrain to a single line, and it turned out to be very hard. I didn't succeed at that for like ½-hour I spent, and constraining the search artifically turned out not just much easier, but also significantly simplified the regexp.
1 parent d187b3d commit c1e7ffe

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

purescript-mode.el

Lines changed: 31 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,36 @@ 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 (bolp)))
481+
(forward-line -1)) ; can't get indentation on an empty line
482+
(let ((indent-level (current-indentation)))
483+
(print (line-number-at-pos))
484+
(while
485+
(not
486+
(or (ignore (forward-line -1)) ; do-while implementation
487+
(bobp)
488+
(and (< (current-indentation) indent-level)
489+
(string-match-p
490+
(rx (*? anything)
491+
(or bol space word-boundary) "="
492+
(or eol space word-boundary))
493+
;; Emacs doesn't allow to limit search just to the curent line
494+
;; barring the regex eol, but eol overly complicates matching.
495+
(purescript-current-line-string))))))))
496+
497+
(defun purescript-beginning-of-defun (&optional repeat)
498+
"Move point to the beginning of the current PureScript function."
499+
(purescript-beginning-of-defun-single)
500+
(dotimes (_ (if repeat
501+
(- repeat 1) ; one time function was already called
502+
0))
503+
(purescript-beginning-of-defun-single)))
473504

474505
(provide 'purescript-mode)
475506

0 commit comments

Comments
 (0)