Skip to content

Instantly share code, notes, and snippets.

@nfedyashev
Created September 5, 2024 14:30
Show Gist options
  • Save nfedyashev/51b994941f738b5341b4d67fefb32c88 to your computer and use it in GitHub Desktop.
Save nfedyashev/51b994941f738b5341b4d67fefb32c88 to your computer and use it in GitHub Desktop.
(defun gptel--strip-mode-suffix (mode-sym)
"Remove the -mode suffix from MODE-NAME.
MODE-NAME is typically a major-mode symbol."
- (let ((mode-name (thread-last
- (symbol-name mode-sym)
- (string-remove-suffix "-mode")
- (string-remove-suffix "-ts"))))
- (if (provided-mode-derived-p
- mode-sym 'prog-mode 'text-mode 'tex-mode)
- mode-name "")))
+ (cond
+ ((eq mode-sym 'enh-ruby-mode) "ruby")
+ ((eq mode-sym 'js2-mode) "js")
+ ((eq mode-sym 'scad-mode) "openscad")
+ (t
+ (let ((mode-name (thread-last
+ (symbol-name mode-sym)
+ (string-remove-suffix "-mode")
+ (string-remove-suffix "-ts"))))
+ (if (provided-mode-derived-p
+ mode-sym 'prog-mode 'text-mode 'tex-mode)
+ mode-name "")))))
+
@nfedyashev
Copy link
Author

@dgutov thank you!

@dgutov
Copy link

dgutov commented Sep 6, 2024

And your question about a better way: unfortunately we don't have an unequivocal major-mode=>language mapping anywhere yet, but there is another source of information: major-mode-remap-alist and (most recently) major-mode-remap-defaults. So one could also check whether the current mode is associated as a value with some major mode as a key which fits the traditional naming scheme (when the mapped one doesn't).

But I would only bother with it after finding a real-world mode that doesn't fit the current detector. That mapping is only used for TeX/LaTeX and ts modes by default.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment