Emacs Stuph

This commit is contained in:
John Doty 2019-03-21 21:06:08 +00:00
parent 2fb0acc5e0
commit 2f4dd68314
4 changed files with 193 additions and 1 deletions

View file

@ -9,6 +9,13 @@ if status --is-login
if test -d ~/.cargo/bin if test -d ~/.cargo/bin
set PATH $PATH ~/.cargo/bin set PATH $PATH ~/.cargo/bin
end end
if test -d ~/.local/bin
set PATH $PATH ~/.local/bin
end
end
function fish_title
true
end end
# This is here to make emacs and ansi-term work properly; I'm not *quite* # This is here to make emacs and ansi-term work properly; I'm not *quite*

View file

@ -466,6 +466,8 @@
:preface :preface
(defun my-csharp-mode-hook () (defun my-csharp-mode-hook ()
"My C# mode hook." "My C# mode hook."
(require 'prettysharp)
(prettysharp-mode)
(turn-on-font-lock) (turn-on-font-lock)
(omnisharp-mode) (omnisharp-mode)
(c-set-style "ms-csharp")) (c-set-style "ms-csharp"))

View file

@ -20,11 +20,16 @@
(company-dabbrev-code company-gtags company-etags company-keywords) (company-dabbrev-code company-gtags company-etags company-keywords)
company-oddmuse company-dabbrev company-flow))) company-oddmuse company-dabbrev company-flow)))
'(css-indent-offset 2) '(css-indent-offset 2)
'(custom-safe-themes
(quote
("8db4b03b9ae654d4a57804286eb3e332725c84d7cdab38463cb6b97d5762ad26" default)))
'(fast-lock-cache-directories (quote ("~/flc-cache"))) '(fast-lock-cache-directories (quote ("~/flc-cache")))
'(fast-lock-minimum-size nil) '(fast-lock-minimum-size nil)
'(fill-column 77) '(fill-column 77)
'(find-file-run-dired t) '(find-file-run-dired t)
'(flycheck-gcc-language-standard "c++11") '(flycheck-gcc-language-standard "c++11")
'(flycheck-javascript-flow-args nil)
'(flycheck-python-flake8-executable "python3")
'(font-lock-global-modes t) '(font-lock-global-modes t)
'(font-lock-maximum-size nil) '(font-lock-maximum-size nil)
'(font-lock-support-mode (quote jit-lock-mode)) '(font-lock-support-mode (quote jit-lock-mode))
@ -51,7 +56,7 @@
((sequence "TODO" "|" "DONE" "ABANDONED" "DEFERRED")))) ((sequence "TODO" "|" "DONE" "ABANDONED" "DEFERRED"))))
'(package-selected-packages '(package-selected-packages
(quote (quote
(lsp-ui wgrep fsharp-mode company-lsp cquery mustache-mode clang-format projectile dash-functional mocha add-node-modules-path rjsx-mode xref-js2 js2-refactor company omnisharp geiser cider clojure-mode graphviz-dot-mode multi-term xterm-color thrift markdown-mode tuareg merlin ag use-package flycheck dockerfile-mode js2-mode web-mode zencoding-mode tss switch-window python-mode paredit magit lua-mode go-mode go-autocomplete exec-path-from-shell csharp-mode color-theme-solarized color-theme-monokai auto-complete auto-complete-nxml flymake flyspell json-mode popup ruby-mode company-jedi tide ahg elm-mode monky))) (lsp-ui yaml-mode wgrep fsharp-mode company-lsp cquery mustache-mode clang-format projectile dash-functional mocha add-node-modules-path rjsx-mode xref-js2 js2-refactor company omnisharp geiser cider clojure-mode graphviz-dot-mode multi-term xterm-color thrift markdown-mode tuareg merlin ag use-package flycheck dockerfile-mode js2-mode web-mode zencoding-mode tss switch-window python-mode paredit magit lua-mode go-mode go-autocomplete exec-path-from-shell csharp-mode color-theme-solarized color-theme-monokai auto-complete auto-complete-nxml flymake flyspell json-mode popup ruby-mode company-jedi tide ahg elm-mode monky)))
'(reb-re-syntax (quote string)) '(reb-re-syntax (quote string))
'(rmail-mail-new-frame t) '(rmail-mail-new-frame t)
'(safe-local-variable-values '(safe-local-variable-values

178
site-lisp/prettysharp.el Normal file
View file

@ -0,0 +1,178 @@
;;; prettysharp.el --- Support for formatting C# with prettysharp
;;; Commentary:
;; Copyright 2019 John Doty. All rights reserved.
;; This program is free software: you can redistribute it and/or modify it
;; under the terms of the GNU General Public License as published by the Free
;; Software Foundation, either version 3 of the License, or (at your option)
;; any later version.
;; This program is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
;; for more details.
;; You should have received a copy of the GNU General Public License along
;; with this program. If not, see <https://www.gnu.org/licenses/>.
;; Author: John Doty
;; Version: 1.0.0
;; Keywords: convenience wp edit csharp
;; URL: https://github.com/decarabas/prettysharp
;;
;; This file is not part of GNU Emacs.
;;; Code:
(defgroup prettysharp nil
"Minor mode to format C# code on save."
:group 'languages
:prefix "prettysharp"
:link '(url-link "https://github.com/DeCarabas/PrettySharp"))
(defcustom prettysharp-command "prettysharp"
"The 'prettysharp' command."
:type 'string
:group 'prettysharp)
(defcustom prettysharp-show-errors 'echo
"Where to display prettysharp error output.
It can either be displayed in its own buffer, in the echo area,
or not at all."
:type '(choice
(const :tag "Own buffer" buffer)
(const :tag "Echo area" echo)
(const :tag "None" nil))
:group 'prettysharp)
(defconst prettysharp/error-buffer-name " *PrettySharp Errors*"
"The title of the buffer we show errors in.")
(defconst prettysharp/patch-buffer-name " *PrettySharp Patch*"
"The title of the buffer we format the patch in.")
(defun prettysharp/show-errors (outfile)
"Display the errors in OUTFILE.
Errors are displayed according to the value of prettysharp-show-errors."
(when prettysharp-show-errors
(let ((error-buffer (get-buffer-create prettysharp/error-buffer-name)))
(with-current-buffer error-buffer
(setq buffer-read-only nil)
(insert-file-contents outfile nil nil nil 'replace)
(cond
((eq prettysharp-show-errors 'buffer)
(goto-char (point-min))
(insert "prettysharp errors:\n")
(compilation-mode)
(display-buffer error-buffer))
((eq prettysharp-show-errors 'echo)
(message "%s" (buffer-string)))
)))))
(defun prettysharp/clear-errors ()
"Clear any errors from PrettySharp."
(let ((error-buffer (get-buffer prettysharp/error-buffer-name)))
(when error-buffer
(message "Clear errors?")
(with-current-buffer error-buffer
(setq buffer-read-only nil)
(erase-buffer))
(kill-buffer error-buffer))))
(defun prettysharp/make-patch (outfile patch-buffer)
"Diff the contents of the current buffer with OUTFILE, generate an RCS-style patch, and put the results into PATCH-BUFFER."
(call-process-region (point-min) (point-max) "diff" nil
patch-buffer nil "-n" "--strip-trailing-cr"
"-" outfile))
(defun prettysharp/apply-diff (patch-buffer)
"Apply the contents of PATCH-BUFFER as an RCS diff against the current buffer."
(let ((target-buffer (current-buffer))
(line-offset 0))
(save-excursion
(goto-char (point-min))
(with-current-buffer patch-buffer
(goto-char (point-min))
(while (not (eobp))
(cond
((looking-at "^$")
;; blank line is a valid command, meaning do nothing.
(forward-line))
((looking-at "^a\\([0-9]+\\) \\([0-9]+\\)$")
;; add lines.
(forward-line)
(let ((text-start (point))
(insert-at (string-to-number (match-string 1)))
(line-count (string-to-number (match-string 2))))
(forward-line line-count)
(let ((to-insert (buffer-substring text-start (point))))
(with-current-buffer target-buffer
(save-excursion
(goto-char (point-min))
(forward-line (+ insert-at line-offset))
;; It can happen that forward-line moves us to the end of
;; the buffer but not a blank line; in this case we need
;; to insert a newline.
(if (and (eobp) (looking-at "$") (not (looking-at "^")))
(insert "\n"))
(insert to-insert))
(setq line-offset (+ line-offset line-count))))))
((looking-at "^d\\([0-9]+\\) \\([0-9]+\\)$")
;; delete lines.
(forward-line)
(let ((delete-at (string-to-number (match-string 1)))
(line-count (string-to-number (match-string 2))))
(with-current-buffer target-buffer
(save-excursion
(goto-char (point-min))
(forward-line (1- (+ delete-at line-offset)))
(let ((delete-start (point)))
(forward-line line-count)
(delete-region delete-start (point))))
(setq line-offset (- line-offset line-count)))))
(t
(error "Unrecognized RCS command in prettysharp/apply-diff"))
))))))
(defun prettysharp ()
"Format the current buffer according to prettysharp."
(interactive)
(let ((outfile (make-temp-file "prettysharp" nil "cs"))
(patch-buffer (get-buffer-create prettysharp/patch-buffer-name))
(coding-system-for-read 'utf-8)
(coding-system-for-write 'utf-8))
(unwind-protect
(save-restriction
(widen)
(with-current-buffer patch-buffer
(erase-buffer))
(if (zerop (call-process-region (point-min) (point-max) prettysharp-command
nil (list :file outfile) nil))
(progn
(prettysharp/make-patch outfile patch-buffer)
(prettysharp/apply-diff patch-buffer)
(prettysharp/clear-errors))
(prettysharp/show-errors outfile)))
(delete-file outfile))))
;;;###autoload
(define-minor-mode prettysharp-mode
"Minor mode to run prettysharp on file save."
:lighter " pretty#"
(if prettysharp-mode
(add-hook 'before-save-hook 'prettysharp nil 'local)
(remove-hook 'before-save-hook 'prettysharp 'local)))
(provide 'prettysharp)
;;; prettysharp.el ends here