[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/webpaste 44df573 244/298: Merge branch 'kaushalmodi-add-la
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/webpaste 44df573 244/298: Merge branch 'kaushalmodi-add-lang-detection-from-shebang' |
Date: |
Thu, 9 Dec 2021 19:00:22 -0500 (EST) |
branch: elpa/webpaste
commit 44df573eca344e7f4b86970717ca64d8d4f4e702
Merge: 69a8eed 2c399a0
Author: Elis Hirwing <elis@hirwing.se>
Commit: Elis Hirwing <elis@hirwing.se>
Merge branch 'kaushalmodi-add-lang-detection-from-shebang'
---
tests/unit/test-webpaste-get-language.el | 45 ++++++++++++++++++++++++++++++++
webpaste.el | 41 ++++++++++++++++++++++++++---
2 files changed, 82 insertions(+), 4 deletions(-)
diff --git a/tests/unit/test-webpaste-get-language.el
b/tests/unit/test-webpaste-get-language.el
new file mode 100644
index 0000000..17d9e4a
--- /dev/null
+++ b/tests/unit/test-webpaste-get-language.el
@@ -0,0 +1,45 @@
+;;; test-webpaste-get-language.el --- Tests for webpaste
+;;; Commentary:
+;;; Code:
+
+(load "tests/load-undercover.el")
+(require 'webpaste)
+
+(describe
+ "Get language for buffer"
+
+ ;; Setup the current buffer with a bash script
+ (before-each
+ (insert "#!/usr/bin/env bash")
+ (newline)
+ (insert "echo 'Hello'"))
+
+ ;; Kill the contents of the current buffers bash script
+ (after-each
+ (kill-region (point-min) (point-max)))
+
+ (it
+ "can get the language by shebang"
+
+ ;; Setup. Let the mode be fundamental-mode.
+ (let ((major-mode 'fundamental-mode))
+ ;; Do test. And expect the matched mode to be bash-mode, since the buffer
+ ;; contains a bash shebang.
+ (expect (webpaste--get-shebang-lang-mode)
+ :to-equal
+ 'bash-mode)))
+
+ (it
+ "will use shebangs before major-mode"
+
+ ;; Setup.
+ (let ((major-mode 'fundamental-mode)
+ (webpaste--provider-lang-alists '(("https://example.com/" .
+ ((fundamental-mode . "text")
+ (bash-mode . "bash"))))))
+
+ (expect (webpaste--get-buffer-language "https://example.com/")
+ :to-equal
+ "bash"))))
+
+;;; test-webpaste-get-language.el ends here
diff --git a/webpaste.el b/webpaste.el
index ce12f2e..3665a38 100644
--- a/webpaste.el
+++ b/webpaste.el
@@ -93,7 +93,13 @@ This uses `browse-url-generic' to open URLs."
:lang-uri-separator "/"
:lang-overrides ((emacs-lisp-mode . "elisp")
(nix-mode . "nix")
- (conf-toml-mode . "toml"))
+ (conf-toml-mode . "toml")
+ (bash-mode . "bash")
+ (sh-mode . "sh")
+ (csh-mode . "csh")
+ (tcsh-mode . "tcsh")
+ (python3-mode . "python3")
+ (nim-mode . "nim"))
:success-lambda webpaste--providers-success-location-header)
("ix.io"
@@ -399,6 +405,32 @@ Optional params:
webpaste-provider-priority)
+(cl-defun webpaste--get-shebang-lang-mode ()
+ "Return language of the buffer set using a shebang as a mode symbol.
+
+Return nil if no shebang found.
+
+Example: For \"#!/usr/bin/env bash\", 'bash-mode is returned.
+ For \"#!/bin/python\", 'python-mode is returned."
+
+ (let* ((end-of-first-line (save-excursion
+ (save-restriction
+ (widen)
+ (goto-char (point-min))
+ (end-of-line 1)
+ (point))))
+ (first-line (save-restriction
+ (widen)
+ (buffer-substring-no-properties
+ (point-min) end-of-first-line))))
+ (when (string-match "\\`#!\\(?1:\\(?:[^ ]+/\\)\\(?2:[^ /]+\\)\\)\\(?:
+\\(?3:[^ ]+\\)\\)*" first-line)
+ (let ((lang (if (string= "/usr/bin/env" (match-string-no-properties 1
first-line))
+ (match-string-no-properties 3 first-line)
+ (match-string-no-properties 2 first-line))))
+ (when lang
+ (intern (format "%s-mode" lang)))))))
+
+
(cl-defun webpaste--get-buffer-language (provider)
"Return language of the buffer that should be sent to the PROVIDER.
@@ -406,9 +438,10 @@ This also depends on which provider it is since different
providers might have
different opinions of how the input for their fields should look like."
(unless webpaste-paste-raw-text
- (let ((provider-lang-alist (cdr (assoc provider
webpaste--provider-lang-alists))))
- (let ((language-name (cdr (assoc major-mode provider-lang-alist))))
- language-name))))
+ (let* ((provider-lang-alist (cdr (assoc provider
webpaste--provider-lang-alists)))
+ (detected-mode (or (webpaste--get-shebang-lang-mode) major-mode))
+ (language-name (cdr (assoc detected-mode provider-lang-alist))))
+ language-name)))
(cl-defun webpaste--return-url (returned-url)
- [nongnu] elpa/webpaste a5d0ba1 237/298: Add nix-mode mapping for nix language files for ptpb.pw and ix.io, (continued)
- [nongnu] elpa/webpaste a5d0ba1 237/298: Add nix-mode mapping for nix language files for ptpb.pw and ix.io, ELPA Syncer, 2021/12/09
- [nongnu] elpa/webpaste 5c742bf 238/298: Remove sprunge.us as provider, ELPA Syncer, 2021/12/09
- [nongnu] elpa/webpaste ba3ce40 221/298: Merge branch 'raxod502-feat/fix-dups', ELPA Syncer, 2021/12/09
- [nongnu] elpa/webpaste d7047a9 222/298: Update copyright year, ELPA Syncer, 2021/12/09
- [nongnu] elpa/webpaste dc99105 260/298: Merge branch 'switch-to-nix-emacs-ci', ELPA Syncer, 2021/12/09
- [nongnu] elpa/webpaste 3a1eaca 183/298: Added integration tests for gist.github.com, ELPA Syncer, 2021/12/09
- [nongnu] elpa/webpaste 8ef26cf 198/298: Switched function and variable names that are supposed to be internal, ELPA Syncer, 2021/12/09
- [nongnu] elpa/webpaste cfd41c7 207/298: Add Emacs-24.3 again as well as Emacs-25.3 as new version is out, ELPA Syncer, 2021/12/09
- [nongnu] elpa/webpaste ba05903 248/298: travis: Run the integration tests in emacs 26.1, ELPA Syncer, 2021/12/09
- [nongnu] elpa/webpaste 9ca4105 267/298: Extend default language list, ELPA Syncer, 2021/12/09
- [nongnu] elpa/webpaste 44df573 244/298: Merge branch 'kaushalmodi-add-lang-detection-from-shebang',
ELPA Syncer <=
- [nongnu] elpa/webpaste 0f79722 270/298: Add wrapper around webpaste for buffer and region, ELPA Syncer, 2021/12/09
- [nongnu] elpa/webpaste 20da215 271/298: Add unit tests for pasting buffer OR region depending on context, ELPA Syncer, 2021/12/09
- [nongnu] elpa/webpaste e892ef2 277/298: webpaste: Add custom to limit amount of retries, ELPA Syncer, 2021/12/09
- [nongnu] elpa/webpaste bd2bf6a 281/298: github-actions: Set up github actions and undercover for unit tests, ELPA Syncer, 2021/12/09
- [nongnu] elpa/webpaste 54e2ed4 178/298: Added integration test for ix.io, ELPA Syncer, 2021/12/09
- [nongnu] elpa/webpaste 26a8035 197/298: Switched to simpler provider definitions where we don't store lambdas in the alist, ELPA Syncer, 2021/12/09
- [nongnu] elpa/webpaste e2ba2f5 282/298: github-actions: Set up github actions for integration tests, ELPA Syncer, 2021/12/09
- [nongnu] elpa/webpaste 67a1a8c 295/298: Copyright header shouldn't be a range of years, ELPA Syncer, 2021/12/09
- [nongnu] elpa/webpaste b063ddd 292/298: README: Update number of providers, ELPA Syncer, 2021/12/09
- [nongnu] elpa/webpaste 0c814bc 179/298: Added integration test for sprunge.us, ELPA Syncer, 2021/12/09