Skip to content

Commit

Permalink
Add hook for module language to racket-hash-lang-mode
Browse files Browse the repository at this point in the history
This is roughly in the same spirit as PR #661, but using the
module-language key supported by new lang's info function.

Also this defines a hook as the means for users to customize. A
default hook function sets comment-start for a few popular
langs (although ultimately comment-start should become a new
"official" lang info key).
  • Loading branch information
greghendershott committed Sep 19, 2023
1 parent 233cd45 commit 92f2d23
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 6 deletions.
37 changes: 35 additions & 2 deletions racket-hash-lang.el
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ until the back end has handled the update commands and also
re-tokenization has progressed sufficiently.")

(defvar-local racket--hash-lang-changed-vars nil
"Where we save spec to restore original values.")
"Original values when minor mode was enabled, to restore when disabled.")

(defvar racket-hash-lang-mode-map
(racket--easy-keymap-define
Expand All @@ -51,6 +51,9 @@ re-tokenization has progressed sufficiently.")

(defvar-local racket--hash-lang-submit-predicate-p nil)

(defvar-local racket-hash-lang-module-language nil
"The symbol for the module language, if any, else nil.")

;;;###autoload
(define-minor-mode racket-hash-lang-mode
"Use color-lexer, indent, and navigation supplied by a #lang.
Expand All @@ -70,6 +73,9 @@ For `racket-repl-mode' buffers, be aware that only input portions
of the buffer use coloring/indent/navigation from the hash-lang.
Output portions are treated as whitespace.
Runs the hook variable `racket-hash-lang-module-language-hook'
when the module language changes.
\\{racket-hash-lang-mode-map}
"
:lighter racket-hash-lang-mode-lighter
Expand Down Expand Up @@ -225,13 +231,15 @@ not. Intended as a convenience so users needn't set a
We do /not/ get notified when a new lang uses exactly the same
attributes as the old one. For example changing from #lang racket
to #lang racket/base will /not/ notify us, because none of the
lang's attributes that care about have changed."
lang's attributes that we care about have changed."
;;;(message "racket--hash-lang-on-new-lang %s" plist)
(with-silent-modifications
(save-restriction
(widen)
(racket--hash-lang-remove-text-properties (point-min) (point-max))
(font-lock-flush (point-min) (point-max))
(setq racket-hash-lang-module-language (plist-get plist 'module-language))
(run-hooks 'racket-hash-lang-module-language-hook)
;; If the lang uses racket-grouping-position, i.e. it uses
;; s-expressions, then use racket-mode-syntax-table. That way
;; other Emacs features and packages are more likely to work.
Expand Down Expand Up @@ -539,6 +547,31 @@ not a complete expression, in which case `newline-and-indent'."
(racket-repl-submit prefix)
(newline-and-indent)))

(defun racket-hash-lang-module-language-hook-default ()
"A default value for hook variable `racket-hash-lang-module-language-hook'.
Sets the variable `comment-start'. This is probably just a
work-around until a lang info key for this is defined and
implemented by various langs?"
(setq comment-start
(cl-case racket-hash-lang-module-language
((racket
racket/base)
";;")
((rhombus)
"//")
((scribble/base/lang
scribble/manual/lang)
"@;"))))

(defvar racket-hash-lang-module-language-hook
(list #'racket-hash-lang-module-language-hook-default)
"Hook run when the module language changes.
Your hook function may consult the variable
`racket-hash-lang-module-language' and do additional
customization based on the specific language.")

(provide 'racket-hash-lang)

;; racket-hash-lang.el ends here
4 changes: 3 additions & 1 deletion racket/hash-lang-bridge.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,16 @@
(list
'hash-lang id
'lang
'module-language (lang-info-module-language li)
'racket-grouping (lang-info-grouping-position-is-racket? li)
'range-indenter (and (lang-info-range-indenter li) #t)
'submit-predicate (and (lang-info-submit-predicate li) #t))))
(define/override (on-changed-tokens gen beg end)
(when (< beg end)
(async-channel-put hash-lang-notify-channel
(list 'hash-lang id
'update gen (add1 beg) (add1 end))))))))
'update
gen (add1 beg) (add1 end))))))))

(define (hash-lang . args)
(unless (class? hash-lang-class-or-error-message)
Expand Down
7 changes: 5 additions & 2 deletions racket/hash-lang.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -658,12 +658,14 @@
(define/public (get-regions)
'((0 end)))))

(define default-module-language #f)
(define default-lexer (waive-option module-lexer*))
(define default-paren-matches '((\( \)) (\[ \]) (\{ \})))
(define default-quote-matches '(#\" #\|))

(define default-lang-info
(lang-info default-lexer
(lang-info default-module-language
default-lexer
default-paren-matches
default-quote-matches
racket-grouping-position
Expand All @@ -676,7 +678,8 @@
(read-language in (λ _ #f)))
(λ (_key default) default)))
(define-values (_line _col end-pos) (port-next-location in))
(values (lang-info (info 'color-lexer default-lexer)
(values (lang-info (info 'module-language default-module-language)
(info 'color-lexer default-lexer)
(info 'drracket:paren-matches default-paren-matches)
(info 'drracket:quote-matches default-quote-matches)
(info 'drracket:grouping-position racket-grouping-position)
Expand Down
3 changes: 2 additions & 1 deletion racket/lang-info.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
;; require it normally and not need to do more dynamic-requires.

(struct lang-info
(lexer
(module-language
lexer
paren-matches
quote-matches
grouping-position
Expand Down

0 comments on commit 92f2d23

Please sign in to comment.