From a6fd6508c9f853df9f0a61079f2268cd88e3d5f7 Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Mon, 16 Aug 2021 22:48:29 -0500 Subject: Break out functionality into other files --- init.el | 178 ++++++++++++------------------------------------ lisp/acdw-browse-url.el | 122 +++++++++++++++++++++++++++++++++ lisp/acdw-consult.el | 37 ++++++++++ lisp/acdw-lisp.el | 25 +++++++ lisp/acdw.el | 79 +-------------------- 5 files changed, 232 insertions(+), 209 deletions(-) create mode 100644 lisp/acdw-browse-url.el create mode 100644 lisp/acdw-consult.el create mode 100644 lisp/acdw-lisp.el diff --git a/init.el b/init.el index 550cb34..968627d 100644 --- a/init.el +++ b/init.el @@ -51,20 +51,8 @@ ;;;; Compatibility with older versions (require 'acdw-compat) - -;;;; Utility functions and variables -;; see also: `acdw' and friends. Functions here aren't big enough, or they're -;; too tightly bound to stuff here, to be placed in `acdw'. - -(defvar lispy-modes '(emacs-lisp-mode - eval-expression-minibuffer - ielm-mode - lisp-mode - lisp-interaction-mode - scheme-mode - slime-repl-mode - sly-mrepl-mode) - "List of modes that are lisp-like enough to hook packages into.") +;;;; Lisp +(require 'acdw-lisp) ;;; Basics @@ -81,63 +69,26 @@ (global-auto-revert-mode +1)) (setup browse-url + (require 'acdw-browse-url) + (setq-default browse-url-secondary-browser-function (if (executable-find "firefox") ; prefer Firefox - 'browse-url-firefox - 'browse-url-default-browser) + #'browse-url-firefox + #'browse-url-default-browser) browse-url-new-window-flag nil ; for eww browse-url-firefox-arguments '("--new-tab") ; for firefox browse-url-firefox-new-window-is-tab t) - (defvar browse-url-mpv-arguments nil - "Arguments to pass to mpv in `browse-url-mpv'.") - - (defun browse-url-mpv (url &optional new-window) - "Play `URL' in mpv." - (interactive (browse-url-interactive-arg "Video URL: ")) - (ignore new-window) ;; mpv always opens a new window - (let* ((url (browse-url-encode-url url)) - (process-environment (browse-url-process-environment))) - (message "Playing %s in mpv..." url) - (apply #'start-process - (concat "mpv " url) nil - "mpv" - (append - browse-url-mpv-arguments - (list url))))) - - (defvar browse-url-feh-arguments '("--auto-zoom" - "--geometry" "800x600") - "Arguments to pass to feh in `browse-url-feh'.") - - (defun browse-url-feh (url &optional new-window) - "Open `URL' in feh." - (interactive (browse-url-interactive-arg "Video URL: ")) - (ignore new-window) ;; mpv always opens a new window - (let* ((url (browse-url-encode-url url)) - (process-environment (browse-url-process-environment))) - (message "Opening %s in feh..." url) - (apply #'start-process - (concat "feh " url) nil - "feh" - (append - browse-url-feh-arguments - (list url))))) - - ;; `browse-url-browser-function' as an alist is deprecated in Emacs 28 for - ;; `browse-url-handlers'. - (set-default (if (version<= emacs-version "28") - 'browse-url-browser-function - 'browse-url-handlers) - `(("\\.jpe?g\\'" . ,(if (executable-find "feh") - 'browse-url-feh - 'eww-browse-url)) - ("youtube\\.com\\|youtu\\.be" . ,(if (executable-find "mpv") - 'browse-url-mpv - 'eww-browse-url)) - ("google\\.com" . browse-url-default-browser) - ("\\(twitter\\.com\\|t\\.co\\)" . acdw/eww-browse-twitter-url) - ("." . eww-browse-url))) + (acdw/browse-url-set-handlers + `(("\\.jpe?g\\'" . ,(if (executable-find "feh") + #'browse-url-feh + #'eww-browse-url)) + ("youtube\\.com\\|youtu\\.be" . ,(if (executable-find "mpv") + #'browse-url-mpv + #'eww-browse-url)) + ("google\\.com" . browse-url-default-browser) + ("\\(twitter\\.com\\|t\\.co\\)" . acdw/eww-browse-twitter-url) + ("." . eww-browse-url))) ;; Buttonize gemini:// links. (acdw/add-button-url-regexp-protocol "gemini")) @@ -270,24 +221,14 @@ '("Setup" "\\(^\\s-*(setup +(?\\)\\(\\_<.+\\_>\\)" 2)))) - (defun acdw/eval-region-or-buffer () - (interactive) - (if (region-active-p) - (let ((begin (region-beginning)) - (end (region-end))) - (with-message (format "Evaluating %S -> %S" begin end) - (eval-region begin end))) - (with-message "Evaluating buffer" - (eval-buffer)))) - ;; Emulate slime's eval binds (:with-map emacs-lisp-mode-map (:bind "C-c C-c" eval-defun "C-c C-k" acdw/eval-region-or-buffer "C-c C-z" ielm)) - (add-hook 'emacs-lisp-mode-hook 'turn-on-eldoc-mode) - (add-hook 'ielm-mode-hook 'turn-on-eldoc-mode) + (add-hook 'emacs-lisp-mode-hook #'turn-on-eldoc-mode) + (add-hook 'ielm-mode-hook #'turn-on-eldoc-mode) (setup (:straight macrostep) (define-key emacs-lisp-mode-map (kbd "C-c e") #'macrostep-expand)) @@ -488,15 +429,9 @@ like a dumbass." (unless (file-exists-p ispell-personal-dictionary) (write-region "" nil ispell-personal-dictionary nil 0)) - (defun flyspell-start () - "Start `flyspell-mode' or `flyspell-prog-mode', depending on current mode." - (cond ((derived-mode-p 'text-mode) - (flyspell-mode)) - ((derived-mode-p 'prog-mode) - (flyspell-prog-mode)))) - (when (executable-find ispell-program-name) - (add-hook 'change-major-mode-hook #'flyspell-start)) + (add-hook 'text-mode-hook #'flyspell-mode) + (add-hook 'prog-mode-hook #'flyspell-prog-mode)) (:when-loaded (setup (:straight flyspell-correct) @@ -556,9 +491,9 @@ like a dumbass." (mode . eww-mode)))))) (:global "C-x C-b" ibuffer) - (defun ibuffer-filter-to-default () - (ibuffer-switch-to-saved-filter-groups "default")) - (:hook ibuffer-filter-to-default) + (add-hook 'ibuffer-mode + (defun ibuffer@filter-to-default () + (ibuffer-switch-to-saved-filter-groups "default"))) (:also-load ibuf-ext) (:option ibuffer-show-empty-filter-groups nil @@ -693,7 +628,7 @@ like a dumbass." regexp-search-ring search-ring mark-ring)) - (:option (append savehist-additional-variables) var)) + (add-to-list 'savehist-additional-variables var)) (savehist-mode +1)) @@ -779,9 +714,6 @@ like a dumbass." (:hook turn-on-auto-fill acdw/setup-fringes)) -(setup (:straight typo) - (:hook-into text-mode)) - (setup uniquify (:option uniquify-buffer-name-style 'forward uniquify-separator path-separator @@ -840,14 +772,17 @@ like a dumbass." ;; This should stay as /minimal/ as possible. Anything that can go somewhere ;; else /should/ go there. (setup emacs - (:option disabled-command-function nil - kill-read-only-ok t - load-prefer-newer t - native-comp-async-report-warnings-errors nil - echo-keystrokes 0.01 - attempt-stack-overflow-recovery nil - attempt-orderly-shutdown-on-fatal-signal nil - find-function-C-source-directory (acdw/find-emacs-source)) + (:option + attempt-orderly-shutdown-on-fatal-signal nil + attempt-stack-overflow-recovery nil + disabled-command-function nil + echo-keystrokes 0.01 + find-function-C-source-directory (acdw/find-emacs-source) + kill-read-only-ok t + load-prefer-newer t + native-comp-async-report-warnings-errors nil + set-mark-command-repeat-pop t + ) (:global "M-=" count-words "C-w" kill-region-or-backward-word @@ -906,30 +841,7 @@ like a dumbass." (setup (:straight (consult :host github :repo "minad/consult")) - - ;; "Sensible" functions - (defun consult-sensible-grep (&optional arg) - "Perform `consult-git-grep' if in a git project, otherwise `consult-ripgrep' -if ripgrep is installed, otherwise `consult-grep'." - (interactive "P") - (cond ((executable-find "rg") - (call-interactively #'consult-ripgrep)) - ((string-equal (vc-backend buffer-file-name) "Git") - (call-interactively #'consult-git-grep)) - (t (call-interactively #'consult-grep)))) - - (defun consult-sensible-find (&optional arg) - "Peform `consult-locate' if locate is installed, otehrwise `consult-find'." - (interactive "P") - (cond ((executable-find "locate") (call-interactively #'consult-locate)) - (t (call-interactively #'consult-find)))) - - ;; Orderless Regexp Compiler! -- from Consult Wiki - (defun consult--orderless-regexp-compiler (input type) - (setq input (orderless-pattern-compiler input)) - (cons - (mapcar (lambda (r) (consult--convert-regexp r type)) input) - (lambda (str) (orderless--highlight input str)))) + (require 'acdw-consult) (setq consult--regexp-compiler #'consult--orderless-regexp-compiler) @@ -960,8 +872,8 @@ if ripgrep is installed, otherwise `consult-grep'." "M-g i" consult-imenu "M-g I" consult-project-imenu ;; M-s bindings (`search-map') - "M-s g" consult-sensible-grep - "M-s f" consult-sensible-find + "M-s g" acdw-consult/sensible-grep + "M-s f" acdw-consult/sensible-find "M-s l" consult-line "M-s m" consult-multi-occur "M-s k" consult-keep-lines @@ -991,12 +903,7 @@ if ripgrep is installed, otherwise `consult-grep'." ;; Competion-at-point (complete-region) (:global "M-/" completion-at-point) - (:option completion-in-region-function - (lambda (&rest args) - (apply (if vertico-mode - #'consult-completion-in-region - #'completion--in-region) - args)) + (:option completion-in-region-function #'acdw-consult/complete-in-region completion-cycle-threshold 3 tab-always-indent 'complete) @@ -1154,7 +1061,9 @@ With any prefix argument successive invocations." (interactive "P") (cond - (arg + ((or arg + (and set-mark-command-repeat-pop + (eq last-command 'pop-to-mark-command))) (setq this-command 'set-mark-command) (set-mark-command arg)) ((eq last-command 'acdw/set-mark-or-expand-region) @@ -1493,6 +1402,9 @@ successive invocations." (add-to-list 'auto-mode-alist spec)) (add-hook 'ssh-config-mode-hook #'turn-on-font-lock)) +(setup (:straight typo) + (:hook-into text-mode)) + (setup (:straight undo-fu) (:global "C-/" undo-fu-only-undo "C-?" undo-fu-only-redo)) diff --git a/lisp/acdw-browse-url.el b/lisp/acdw-browse-url.el new file mode 100644 index 0000000..93f470c --- /dev/null +++ b/lisp/acdw-browse-url.el @@ -0,0 +1,122 @@ +;;; acdw-browse-url.el -*- lexical-binding: t; coding: utf-8-unix -*- +;; +;; Add-ons to `browse-url'. + +(defvar browse-url-mpv-arguments nil + "Arguments to pass to mpv in `browse-url-mpv'.") + +(defun browse-url-mpv (url &optional new-window) + "Play `URL' in mpv." + (interactive (browse-url-interactive-arg "Video URL: ")) + (ignore new-window) ;; mpv always opens a new window + (let* ((url (browse-url-encode-url url)) + (process-environment (browse-url-process-environment))) + (message "Playing %s in mpv..." url) + (apply #'start-process + (concat "mpv " url) nil + "mpv" + (append + browse-url-mpv-arguments + (list url))))) + +(defvar browse-url-feh-arguments '("--auto-zoom" + "--geometry" "800x600") + "Arguments to pass to feh in `browse-url-feh'.") + +(defun browse-url-feh (url &optional new-window) + "Open `URL' in feh." + (interactive (browse-url-interactive-arg "Video URL: ")) + (ignore new-window) ;; mpv always opens a new window + (let* ((url (browse-url-encode-url url)) + (process-environment (browse-url-process-environment))) + (message "Opening %s in feh..." url) + (apply #'start-process + (concat "feh " url) nil + "feh" + (append + browse-url-feh-arguments + (list url))))) + +(defun acdw/browse-url-set-handlers (handlers) + "Set handlers for `browse-url'. +If Emacs' version is 28 or higher, set `browse-url-handlers'. +Else, set `browse-url-browser-function'; it's deprecated in 28+." + (set-default (if (version< emacs-version "28") + #'browse-url-browser-function + #'browse-url-handlers) + handlers)) + +;;; URL regexp +;; really, I just want to add gemini:// protocol, but I'm going to do some +;; reverse-engineering here. +(defvar acdw/button-protocols '("http" + "https" + "shttp" + "shttps" + "ftp" + "file" + "gopher" + "nntp" + "news" + "telnet" + "wais" + "mailto" + "info") + "The list of protocols to splice into `browse-url-button-regexp'.") + +(defun acdw/build-button-url-regexp () + "Build `browse-url-button-regexp' from `acdw/button-protocols'. +I used `xr' (not included in Emacs) to get the RX form of the +default, so I can easily splice the list into it. THIS IS +BRITTLE AF!!!" + (rx-to-string ; thanks wgreenhouse! + `(seq word-boundary + (group + (group + (or "www." + (seq + (group (or ,@acdw/button-protocols)) + ":"))) + (opt + (group "//" + (one-or-more + (any "0-9a-z" "._-")) + ":" + (zero-or-more + (any "0-9")))) + (or + (seq + (one-or-more + (any "0-9a-z" "!#$%&*+,./:;=?@\\_~-" word)) + "(" + (one-or-more + (any "0-9a-z" "!#$%&*+,./:;=?@\\_~-" word)) + (zero-or-more + (any "0-9a-z" "#$%&*+/=@\\_~-" word)) + ")" + (opt + (one-or-more + (any "0-9a-z" "!#$%&*+,./:;=?@\\_~-" word)) + (any "0-9a-z" "#$%&*+/=@\\_~-" word))) + (seq + (one-or-more + (any "0-9a-z" "!#$%&*+,./:;=?@\\_~-" word)) + (any "0-9a-z" "#$%&*+/=@\\_~-" word))))))) + +(defun acdw/add-button-url-regexp-protocol (proto) + "Add PROTO to `browse-url-button-regexp' +First, add PROTO to `acdw/button-protocols'. +Then, build `browse-url-button-regexp' with the new protocol." + (add-to-list 'acdw/button-protocols proto) + (setq-default browse-url-button-regexp (acdw/build-button-url-regexp))) + +;;; Browse-URL tweaks + +;; convert twitter.com to nitter +(defun acdw/eww-browse-twitter-url (url &rest args) + "Browse a Twitter.com URL using Nitter." + (let* ((nitter "nitter.snopyta.org") + (url (replace-regexp-in-string "twitter\\.com" nitter url))) + (eww-browse-url url args))) + +(provide 'acdw-browse-url) diff --git a/lisp/acdw-consult.el b/lisp/acdw-consult.el new file mode 100644 index 0000000..e6995f5 --- /dev/null +++ b/lisp/acdw-consult.el @@ -0,0 +1,37 @@ +;;; acdw-consult.el -*- lexical-binding: t; coding: utf-8-unix -*- + +;; Customization for consult. + +(require 'consult) + +;; "Sensible" functions +(defun acdw-consult/sensible-grep (&optional arg) + "Perform `consult-git-grep' if in a git project, otherwise `consult-ripgrep' +if ripgrep is installed, otherwise `consult-grep'." + (interactive "P") + (cond ((executable-find "rg") + (call-interactively #'consult-ripgrep)) + ((string-equal (vc-backend buffer-file-name) "Git") + (call-interactively #'consult-git-grep)) + (t (call-interactively #'consult-grep)))) + +(defun acdw-consult/sensible-find (&optional arg) + "Peform `consult-locate' if locate is installed, otehrwise `consult-find'." + (interactive "P") + (cond ((executable-find "locate") (call-interactively #'consult-locate)) + (t (call-interactively #'consult-find)))) + +;; Orderless Regexp Compiler! -- from Consult Wiki +(defun consult--orderless-regexp-compiler (input type) + (setq input (orderless-pattern-compiler input)) + (cons + (mapcar (lambda (r) (consult--convert-regexp r type)) input) + (lambda (str) (orderless--highlight input str)))) + +(defun acdw-consult/complete-in-region (&rest args) + (apply (if vertico-mode + #'consult-completion-in-region + #'completion--in-region) + args)) + +(provide 'acdw-consult) diff --git a/lisp/acdw-lisp.el b/lisp/acdw-lisp.el new file mode 100644 index 0000000..8f551c9 --- /dev/null +++ b/lisp/acdw-lisp.el @@ -0,0 +1,25 @@ +;;; acdw-lisp.el -*- lexical-binding: t; coding: utf-8-unix -*- +;; +;; Extras for Lisp modes. + +(defvar lispy-modes '(emacs-lisp-mode + eval-expression-minibuffer + ielm-mode + lisp-mode + lisp-interaction-mode + scheme-mode + slime-repl-mode + sly-mrepl-mode) + "List of modes that are lisp-like enough to hook packages into.") + +(defun acdw/eval-region-or-buffer () + (interactive) + (if (region-active-p) + (let ((begin (region-beginning)) + (end (region-end))) + (with-message (format "Evaluating %S -> %S" begin end) + (eval-region begin end))) + (with-message "Evaluating buffer" + (eval-buffer)))) + +(provide 'acdw-lisp) diff --git a/lisp/acdw.el b/lisp/acdw.el index 2aa6c1f..8cbe16d 100644 --- a/lisp/acdw.el +++ b/lisp/acdw.el @@ -318,7 +318,9 @@ With a prefix argument, run git pull on the repo first." (insert s) (let ((sentence-end-double-space nil)) (unfill-region (point-min) (point-max))) - (copy-region-as-kill (point-min) (point-max))))) + (copy-region-as-kill (point-min) (point-max)) + (when (called-interactively-p 'interactive) + (indicate-copied-region))))) (defun acdw/dir (&optional file make-directory) "Place Emacs files in one place. @@ -442,81 +444,6 @@ It's called 'require-private' for historical reasons." (load (expand-file-name "private.el" user-emacs-directory) :noerror :nomessage)) - -;;; URL regexp -;; really, I just want to add gemini:// protocol, but I'm going to do some -;; reverse-engineering here. - -(defvar acdw/button-protocols '("http" - "https" - "shttp" - "shttps" - "ftp" - "file" - "gopher" - "nntp" - "news" - "telnet" - "wais" - "mailto" - "info") - "The list of protocols to splice into `browse-url-button-regexp'.") - -(defun acdw/build-button-url-regexp () - "Build `browse-url-button-regexp' from `acdw/button-protocols'. -I used `xr' (not included in Emacs) to get the RX form of the -default, so I can easily splice the list into it. THIS IS -BRITTLE AF!!!" - (rx-to-string ; thanks wgreenhouse! - `(seq word-boundary - (group - (group - (or "www." - (seq - (group (or ,@acdw/button-protocols)) - ":"))) - (opt - (group "//" - (one-or-more - (any "0-9a-z" "._-")) - ":" - (zero-or-more - (any "0-9")))) - (or - (seq - (one-or-more - (any "0-9a-z" "!#$%&*+,./:;=?@\\_~-" word)) - "(" - (one-or-more - (any "0-9a-z" "!#$%&*+,./:;=?@\\_~-" word)) - (zero-or-more - (any "0-9a-z" "#$%&*+/=@\\_~-" word)) - ")" - (opt - (one-or-more - (any "0-9a-z" "!#$%&*+,./:;=?@\\_~-" word)) - (any "0-9a-z" "#$%&*+/=@\\_~-" word))) - (seq - (one-or-more - (any "0-9a-z" "!#$%&*+,./:;=?@\\_~-" word)) - (any "0-9a-z" "#$%&*+/=@\\_~-" word))))))) - -(defun acdw/add-button-url-regexp-protocol (proto) - "Add PROTO to `browse-url-button-regexp' -First, add PROTO to `acdw/button-protocols'. -Then, build `browse-url-button-regexp' with the new protocol." - (add-to-list 'acdw/button-protocols proto) - (setq-default browse-url-button-regexp (acdw/build-button-url-regexp))) - -;;; Browse-URL tweaks - -;; convert twitter.com to nitter -(defun acdw/eww-browse-twitter-url (url &rest args) - "Browse a Twitter.com URL using Nitter." - (let* ((nitter "nitter.snopyta.org") - (url (replace-regexp-in-string "twitter\\.com" nitter url))) - (eww-browse-url url args))) - ;;; Recentf renaming with dired ;; from ... somewhere. 'rjs', apparently? -- cgit 1.4.1-21-gabe81