;;; ravi-init-navigation.el --- navigation utilities -*- lexical-binding: t; -*- ;; Copyright (C) 2013 ;; Author: ;; Keywords: ;; 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 . ;;; Commentary: ;; Buffer navigation utilities ;;; Code: (use-package region-bindings-mode :bind (:map region-bindings-mode-map ("b" . backward-word) ("f" . forward-word) ;; HJKL cluster to replace arrow keys ("h" . backward-char) ("l" . forward-char) ("k" . previous-line) ("j" . next-line) ;; Most of the time, we mark regions to copy ("w" . kill-ring-save) ;; Expand yasnippet on a region ("y" . yas-insert-snippet) ) :demand t :config (region-bindings-mode-enable) (defvar ravi/disable-region-bindings-mode nil "Is region bindings mode disabled?") (defun ravi/is-region-bindings-mode-disabled () ravi/disable-region-bindings-mode) (add-to-list 'region-bindings-mode-disable-predicates 'ravi/is-region-bindings-mode-disabled) (defun ravi/without-region-bindings-mode (orig-func &rest args) (let* ((ravi/disable-region-bindings-mode t) (res (apply orig-func args))) res))) (use-package avy :bind (("M-H" . avy-goto-word-1) ("M-h" . avy-goto-char-timer) ("M-g g" . avy-goto-line) ("M-g M-g" . avy-goto-line)) :config ;; Many of these from: https://karthinks.com/software/avy-can-do-anything/ ;; Fix dispatch help (defun avy-show-dispatch-help () (let* ((len (length "avy-action-")) (fw (frame-width)) (raw-strings (mapcar (lambda (x) (format "%2s: %-19s" (propertize (char-to-string (car x)) 'face 'aw-key-face) (substring (symbol-name (cdr x)) len))) avy-dispatch-alist)) (max-len (1+ (apply #'max (mapcar #'length raw-strings)))) (strings-len (length raw-strings)) (per-row (floor fw max-len)) display-strings) (cl-loop for string in raw-strings for N from 1 to strings-len do (push (concat string " ") display-strings) (when (= (mod N per-row) 0) (push "\n" display-strings))) (message "%s" (apply #'concat (nreverse display-strings))))) ;; Kill text (defun avy-action-kill-whole-line (pt) (save-excursion (goto-char pt) (kill-whole-line)) (select-window (cdr (ring-ref avy-ring 0))) t) (setf (alist-get ?k avy-dispatch-alist) 'avy-action-kill-stay (alist-get ?k avy-dispatch-alist) 'avy-action-kill-whole-line) ;; Copy text (defun avy-action-copy-whole-line (pt) (save-excursion (goto-char pt) (cl-destructuring-bind (start . end) (bounds-of-thing-at-point 'line) (copy-region-as-kill start end))) (select-window (cdr (ring-ref avy-ring 0))) t) (setf (alist-get ?w avy-dispatch-alist) 'avy-action-copy (alist-get ?W avy-dispatch-alist) 'avy-action-copy-whole-line) ;; Yank text (defun avy-action-yank-whole-line (pt) (avy-action-copy-whole-line pt) (save-excursion (yank)) t) (setf (alist-get ?y avy-dispatch-alist) 'avy-action-yank (alist-get ?Y avy-dispatch-alist) 'avy-action-yank-whole-line) ;; Transpose/Move text (defun avy-action-teleport-whole-line (pt) (avy-action-kill-whole-line pt) (save-excursion (yank)) t) (setf (alist-get ?t avy-dispatch-alist) 'avy-action-teleport (alist-get ?T avy-dispatch-alist) 'avy-action-teleport-whole-line) ;; Mark text (defun avy-action-mark-to-char (pt) (activate-mark) (goto-char pt)) (setf (alist-get ?\s avy-dispatch-alist) 'avy-action-mark-to-char) ;; Any custom actions should not be preempted (let ((key-list '(?a ?s ?d ?f ?g ?h ?j ?k ?l ?r ?u ?t ?y ?w ?o ?e ?i))) (setq avy-keys (seq-filter (lambda (k) (not (alist-get k avy-dispatch-alist))) key-list))) (setq avy-style 'at-full) (setq avy-background t)) (use-package jump-char :bind (("M-m" . jump-char-forward))) (use-package imenu-anywhere :bind ("s-." . imenu-anywhere)) (use-package expand-region :bind ("C-=" . 'er/expand-region)) (use-package multiple-cursors :bind (("C->" . mc/mark-next-like-this) ("C-<" . mc/mark-previous-like-this) ("C-c C-<" . mc/mark-all-like-this)) :commands (mc/mark-more-like-this-extended mc/edit-lines) :init (setq mc/list-file (ravi/emacs-file "past/mc-lists.el")) (bind-key (if (xterm-kitty-in-use) "C-C C-C" "C-S-c C-S-c") #'mc/edit-lines) (with-eval-after-load "region-bindings-mode" (bind-key "a" 'mc/mark-all-like-this region-bindings-mode-map) (bind-key "p" 'mc/mark-previous-like-this region-bindings-mode-map) (bind-key "n" 'mc/mark-next-like-this region-bindings-mode-map) (bind-key "m" 'mc/mark-more-like-this-extended region-bindings-mode-map))) (use-package fold-this :commands fold-this :init (with-eval-after-load "region-bindings-mode" (bind-key "t" 'fold-this region-bindings-mode-map))) (use-package indirect :commands indirect-region :init (with-eval-after-load "region-bindings-mode" (bind-key "I" 'indirect-region region-bindings-mode-map)) :ensure nil) (use-package visual-regexp :bind (("C-c r" . vr/replace) ("C-c q" . vr/query-replace) ("C-c m" . vr/mc-mark))) (bind-key "" 'ff-find-other-file) (setq compilation-scroll-output 'first-error) (use-package smartparens :commands (smartparens-mode show-smartparens-mode) :demand t :config (show-smartparens-global-mode 1) (require 'smartparens-config) (add-hook 'emacs-lisp-mode-hook 'smartparens-mode) (add-hook 'emacs-lisp-mode-hook 'sp-use-smartparens-bindings) (use-package minor-mode-hack :ensure t) (with-eval-after-load "region-bindings-mode" (raise-minor-mode-map-alist 'region-bindings-mode))) (use-package corral :bind (("M-9" . corral-parentheses-backward) ("M-0" . corral-parentheses-forward) ("M-[" . corral-brackets-backward) ("M-]" . corral-brackets-forward) ("M-{" . corral-braces-backward) ("M-}" . corral-braces-forward) ("M-\"" . corral-double-quotes-backward)) :config (setq corral-preserve-point t)) ;; Marking and moving lines (defun ravi/pull-up-a-line() "Pull up the next line" (interactive) (join-line -1)) (bind-key "M-j" 'ravi/pull-up-a-line) (use-package move-lines :bind (("" . move-lines-down) ("" . move-lines-up)) :ensure nil) ;; From Kyle Sherman's comment at ;; http://emacs-fu.blogspot.com/2010/01/duplicating-lines-and-commenting-them.html ;; \todo Make this work someday with regions too. (defun ravi/kyle-sherman-duplicate-line (&optional comment line) "Duplicate the line containing the point. \nIf COMMENT is non-nil, also comment out the original line. If LINE is non-nil, duplicate that line instead." (interactive "P") (let ((col (current-column))) (save-excursion (when line (goto-line line)) (let ((line (buffer-substring (point-at-bol) (point-at-eol)))) (when comment (comment-region (point-at-bol) (point-at-eol))) (goto-char (point-at-eol)) (if (eobp) (newline) (forward-line 1)) (open-line 1) (insert line))) (forward-line 1) (move-to-column col))) ;; C-: duplicates line, C-u C-: comments first line (bind-key "C-:" 'ravi/kyle-sherman-duplicate-line) (use-package mwim :bind (("C-a" . mwim-beginning) ("C-e" . mwim-end))) (use-package ialign :commands ialign) ;; Use current line for region-based commands if no region selected (use-package whole-line-or-region :hook ((prog-mode-hook . turn-on-whole-line-or-region-mode)) :config (defun turn-on-whole-line-or-region-mode() (interactive) (whole-line-or-region-local-mode 1)) :diminish whole-line-or-region-local-mode) ;; This must be after enabling whole-line-or-region (use-package easy-kill :commands (easy-kill) :bind (([remap kill-ring-save] . easy-kill) ([remap mark-sexp] . easy-mark)) :init (with-eval-after-load "whole-line-or-region" (bind-key [remap kill-ring-save] 'easy-kill whole-line-or-region-local-mode-map)) :config ;; Copied easy-kill-activate-keymap from easy-kill.el to add on-exit and on-enter (defun ravi/easy-kill-activate-keymap-with-hooks (&optional on-enter-with-map on-exit-with-map) (let ((map (easy-kill-map))) (when on-enter-with-map (funcall on-enter-with-map map)) (set-transient-map map (lambda () ;; Prevent any error from activating the keymap forever. (condition-case err (or (and (not (easy-kill-exit-p this-command)) (or (eq this-command (lookup-key map (this-single-command-keys))) (let ((cmd (key-binding (this-single-command-keys) nil t))) (command-remapping cmd nil (list map))))) (ignore (easy-kill-destroy-candidate) (unless (or (easy-kill-get mark) (easy-kill-exit-p this-command)) (easy-kill-save-candidate)))) (error (message "%s:%s" this-command (error-message-string err)) nil))) (when on-exit-with-map (lambda () (funcall on-exit-with-map map)))))) (defun ravi/concat-easy-kill-hints (hints max-size) (let ((current-line-size 0) (current-hint-size) (result "")) (dolist (hint hints) (setq current-hint-size (1+ (length hint))) (when (> (+ current-line-size current-hint-size) max-size) (setq result (concat result "\n")) (setq current-line-size 0)) (setq result (concat result " " hint)) (setq current-line-size (+ current-line-size current-hint-size))) result)) (defun ravi/easy-kill-make-hint-message () (let* ((hint-maker (lambda (key-desc) (concat (propertize (car key-desc) 'face 'font-lock-keyword-face) ":" (cdr key-desc)))) (basic (mapcar hint-maker '(("+=" . "expand") ("-" . "shrink") ("SPC" . "cycle") ("@" . "append") ("0" . "original") ("1-9" . "more") ("?" . "help")))) (remapped-basic (delq nil (mapcar (lambda (c) (when-let ((binding (where-is-internal (car c) nil t))) (funcall hint-maker (cons (propertize (key-description binding) 'face 'font-lock-keyword-face) (symbol-name (car c)))))) (cdr (alist-get 'remap easy-kill-base-map))))) (configurable (mapcar (lambda (item) (funcall hint-maker (cons (propertize (char-to-string (car item)) 'face 'font-lock-keyword-face) (symbol-name (cadr item))))) easy-kill-alist))) (mapconcat (lambda (hints) (ravi/concat-easy-kill-hints hints (window-width (minibuffer-window)))) (list basic remapped-basic configurable) "\n"))) (defvar ravi/easy-kill-hint-delay 2.0 "Delay before easy-kill hints are shown") (defun ravi/easy-kill-activate-keymap () (let* ((help-timer)) (ravi/easy-kill-activate-keymap-with-hooks ;; Set up a help message display on a timer before the transient keymap (lambda (map) (setq help-timer (run-with-timer ravi/easy-kill-hint-delay nil (lambda () (let ((message-log-max nil)) (message (ravi/easy-kill-make-hint-message))))))) ;; Cancel the timer on exitng the transient keymap (lambda (map) (when help-timer (cancel-timer help-timer))) ))) (advice-add 'easy-kill-activate-keymap :override #'ravi/easy-kill-activate-keymap) (require 'easy-kill-extras) ) (use-package easy-kill-extras :init (setq easy-kill-ace-jump-enable-p nil) ; we use avy instead of ace-jump (with-eval-after-load 'syntax-subword (bind-key [remap mark-word] 'easy-mark-word syntax-subword-mode-map)) :bind (:map easy-kill-base-map ("o" . easy-kill-er-expand) ("i" . easy-kill-er-unexpand) ("M-@" . easy-mark-word)) :config (require 'extra-things) (dolist (cmd '((?^ backward-line-edge "") (?$ forward-line-edge "") (?a buffer "") (?f string-to-char-forward "") (?F string-up-to-char-forward "") (?t string-to-char-backward "") (?T string-up-to-char-backward "") ;; The rest come from extra-things (?W WORD " ") (?\' squoted-string "") (?\" dquoted-string "") (?\` bquoted-string "") (?q quoted-string "") (?Q quoted-string-universal "") (?\) parentheses-pair-content "\n") (?\( parentheses-pair "\n") (?\] brackets-pair-content "\n") (?\[ brackets-pair "\n") (?} curlies-pair-content "\n") (?{ curlies-pair "\n") (?> angles-pair-content "\n") (?< angles-pair "\n"))) (add-to-list 'easy-kill-alist cmd)) ) ;; grep and friends (use-package wgrep :defer t) (if (executable-find "ag") ;; Prefer the silver-searcher if available (use-package ag :bind (("H-g" . ag) ("H-G" . ag-files) ("M-s g" . grep)) :config (progn (setq ag-highlight-search t) (setq ag-reuse-buffers t) (use-package wgrep-ag))) ;; Use grep if ag is not available on the system (use-package grep :bind (("H-g" . grep)))) (let ((ad-redefinition-action 'accept)) (use-package color-moccur :commands (isearch-moccur isearch-all) :bind ("M-s O" . moccur) :init (progn (bind-key "M-o" 'isearch-moccur isearch-mode-map) (bind-key "M-O" 'isearch-moccur-all isearch-mode-map)) :config (use-package moccur-edit :ensure nil))) ;; Delete via isearch (defun zap-to-isearch (rbeg rend) "Kill the region between the mark and the closest portion of the isearch match string. The behaviour is meant to be analogous to zap-to-char; let's call it zap-to-isearch. The deleted region does not include the isearch word. This is meant to be bound only in isearch mode. The point of this function is that oftentimes you want to delete some portion of text, one end of which happens to be an active isearch word. The observation to make is that if you use isearch a lot to move the cursor around (as you should, it is much more efficient than using the arrows), it happens a lot that you could just delete the active region between the mark and the point, not include the isearch word." (interactive "r") (when (not mark-active) (error "Mark is not active")) (let* ((isearch-bounds (list isearch-other-end (point))) (ismin (apply 'min isearch-bounds)) (ismax (apply 'max isearch-bounds)) ) (if (< (mark) ismin) (kill-region (mark) ismin) (if (> (mark) ismax) (kill-region ismax (mark)) (error "Internal error in isearch kill function."))) (isearch-exit) )) (bind-key "M-z" 'zap-to-isearch isearch-mode-map) ;; Exit isearch at the beginning (defun isearch-exit-other-end (rbeg rend) "Exit isearch, but at the other end of the search string. This is useful when followed by an immediate kill." (interactive "r") (isearch-exit) (goto-char isearch-other-end)) (bind-key "" 'isearch-exit-other-end isearch-mode-map) ;; Search the web (use-package webjump :bind ("C-x g" . webjump) :config (progn ;; Suggest word at point if possible (defun webjump-read-string (prompt) (let* ((prompt (concat prompt (if (word-at-point) (concat " (default: " (word-at-point) ")")) ": ")) (input (read-string prompt))) (if (webjump-null-or-blank-string-p input) (word-at-point) input))) ;; C++ reference (add-to-list 'webjump-sites '("cpp" . [simple-query "en.cppreference.com" "http://en.cppreference.com/mwiki/index.php?title=Special:Search&search=" ""])) ) ) (use-package anzu :demand t :bind (("C-%" . anzu-query-replace) ("C-M-%" . anzu-query-replace-regexp)) :config (progn (global-anzu-mode)) :diminish anzu-mode :ensure anzu) (bind-key (kbd "y") 'info-display-manual help-map) (use-package beginend :config (progn (beginend-global-mode) (dolist (m beginend-modes) (diminish (cdr m))))) (require 'midnight) (provide 'ravi-init-navigation) ;;; ravi-init-navigation.el ends here