;; enter insert newline ( defun vi-open-line-above () " Insert a newline above the current line and put point at beginning. " ( interactive ) ( unless ( bolp ) ( beginning-of-line )) ( newline ) ( forward-line -1 ) ) ( defun vi-open-line-below () " Insert a newline below the current line and put point at beginning. " ( interactive ) ( unless ( eolp ) ( end-of-line )) ( newline ) ) ;; simulate return in insert mode ( define-key evil-normal-state-map ( kbd "<S-return>" ) 'generic-line-break-split ) ( define-key evil-normal-state-map ( kbd "<C-return>" ) 'generic-line-break-above ) ( defun generic-line-break () " Add a newline, without splitting the current line. " ( interactive ) ( cond (( or ( string= major-mode "c-mode" ) ( string= major-mode "c++-mode" )) ( unless ( eolp ) ( end-of-line )) ( c-context-line-break ) ) ;; fallback ( t ( call-interactively 'vi-open-line-below )) )) ( defun generic-line-break-above () " Add a newline above the current line, without splitting the current line. " ( interactive ) ( cond (( or ( string= major-mode "c-mode" ) ( string= major-mode "c++-mode" )) ;; note, this isn't perfect - if you perform it on the last line of a comment. ( unless ( eolp ) ( end-of-line )) ( c-context-line-break ) ;; move line up and goto end ( transpose-lines 1 ) ( forward-line -2 ) ( unless ( eolp ) ( end-of-line )) ) ;; fallback ( t ( call-interactively 'vi-open-line-above )) ) ) ( defun generic-line-break-split () " Add a newline, splitting the current line. " ( interactive ) ( cond (( or ( string= major-mode "c-mode" ) ( string= major-mode "c++-mode" )) ( c-context-line-break ) ) ;; fallback ( t ( call-interactively 'indent-new-comment-line )) ) ) ;; (define-key evil-normal-state-map (kbd "RET") 'vi-open-line-below) ( define-key evil-normal-state-map ( kbd "RET" ) 'generic-line-break ) ( defun generic-next () " Follow link/goto declaration at point. " ( interactive ) ( cond (( string= major-mode "c-mode" ) ;; (call-interactively 'rtags-find-symbol-at-point)) ( call-interactively 'evil-jump-forward )) (( string= major-mode "c++-mode" ) ;; (call-interactively 'rtags-find-symbol-at-point)) ( call-interactively 'evil-jump-forward )) (( string= major-mode "rust-mode" ) ;; (call-interactively 'racer-find-definition)) ( call-interactively 'evil-jump-forward )) (( string= major-mode "diff-mode" ) ( call-interactively 'diff-goto-source-and-close )) ;; fallback ( t ( call-interactively 'evil-jump-forward )) ) ) ( defun generic-prev () " Go back/out of the current location. " ( interactive ) ( cond (( string= major-mode "c-mode" ) ;; (call-interactively 'rtags-location-stack-back)) ( call-interactively 'evil-jump-backward )) (( string= major-mode "c++-mode" ) ;; (call-interactively 'rtags-location-stack-back)) ( call-interactively 'evil-jump-backward )) (( string= major-mode "rust-mode" ) ;; (call-interactively 'pop-tag-mark)) ( call-interactively 'evil-jump-backward )) (( string= major-mode "diff-mode" ) ( call-interactively 'kill-buffer-and-window )) ;; fallback ( t ( call-interactively 'evil-jump-backward )) ) ) ;; access as a different kind of 'lookup' ( defun generic-lookup () " Follow link/goto line. " ( interactive ) ( cond (( string= major-mode "c-mode" ) ( call-interactively 'rtags-find-symbol-at-point )) (( string= major-mode "c++-mode" ) ( call-interactively 'rtags-find-symbol-at-point )) (( string= major-mode "rust-mode" ) ( call-interactively 'racer-find-definition )) (( string= major-mode "diff-mode" ) ( call-interactively 'diff-goto-source-and-close )) ;; fallback ( t ( call-interactively 'evil-jump-forward )) ) ) ;; access as a different kind of 'usage' ( defun generic-usage () " Go back/into references (find usage). " ( interactive ) ( cond (( string= major-mode "c-mode" ) ( call-interactively 'rtags-find-references-at-point )) (( string= major-mode "c++-mode" ) ( call-interactively 'rtags-find-references-at-point )) ) ) ;; Override pop-up-windows ( defun vc-root-diff-fullscreen () " Open a diff of the repository in the current frame. " ( interactive ) ( let ( ( pop-up-windows nil ) ) ( call-interactively 'vc-root-diff ) ) ) ( defun diff-goto-source-and-close () " Go to the source and close the current diff buffer. " ( interactive ) ( let ( ( buf ( current-buffer )) ( pop-up-windows nil ) ) ( diff-goto-source ) ( kill-buffer buf ) ) ) ( define-key evil-normal-state-map ( kbd "M-d" ) 'vc-root-diff-fullscreen ) ;; go to next ( define-key evil-normal-state-map ( kbd "M-l" ) 'generic-next ) ;; go back ( define-key evil-normal-state-map ( kbd "M-h" ) 'generic-prev ) ;; lookup declaration ( define-key evil-normal-state-map ( kbd "M-RET" ) 'generic-lookup ) ;; lookup usage ( define-key evil-normal-state-map ( kbd "C-M-h" ) 'generic-usage ) ( add-hook 'diff-mode-hook ( lambda () ;; go to definition (rust only) ;; (define-key diff-mode-shared-map (kbd "M-l") 'diff-goto-source-and-close) ( define-key diff-mode-shared-map ( kbd "M-l" ) 'diff-goto-source-and-close ) ( define-key diff-mode-shared-map ( kbd "C-M-l" ) 'diff-goto-source ) ;; go back ( define-key diff-mode-shared-map ( kbd "M-h" ) 'kill-buffer-and-window ) ) ) ( defun c-doc-next () " Follow link/goto line " ( interactive ) ( if ( search-forward-regexp "\\(\\\\}\\|\\\\{\\)" nil t 1 ) ( pcase ( preceding-char ) ( ?{ ;; Section Start ( search-backward "/*" nil t 1 ) ;; find start of comment ( move-beginning-of-line nil ) ;; line start ( recenter 6 ) ;; place view ( search-forward "*/" nil t 1 ) ;; comment end ( next-line ) ;; step out of the comment ) ( ?} ;; Section End ( search-forward "*/" nil t 1 ) ;; comment end ( move-beginning-of-line nil ) ;; un-indent ( recenter ) ;; place view (likely its already been moved down) ( next-line ) ;; step out of the comment ) ) ;; fallback ( end-of-buffer ) ) ) ( defun c-doc-prev () " Follow link/goto line " ( interactive ) ( if ( search-backward-regexp "\\(\\\\}\\|\\\\{\\)" nil t 1 ) ( pcase ( char-after ( + 1 ( point ))) ( ?{ ;; Section Start ( search-backward "/*" nil t 1 ) ;; find start of comment ( move-beginning-of-line nil ) ;; line start ( recenter 6 ) ;; place view ( previous-line ) ;; step out of the comment ) ( ?} ;; Section End ( search-backward "/*" nil t 1 ) ;; comment end ( move-beginning-of-line nil ) ;; line start ( previous-line ) ;; step out of the comment ( recenter ) ;; place view (likely its already been moved down) ) ) ;; fallback ( beginning-of-buffer ) ) ) ( defun generic-doc-next () " Follow link/goto line " ( interactive ) ( cond (( string= major-mode "c-mode" ) ( call-interactively 'c-doc-next )) (( string= major-mode "c++-mode" ) ( call-interactively 'c-doc-next )) (( string= major-mode "diff-mode" ) ( call-interactively 'diff-hunk-next )) ;; TODO, other modes ) ) ( defun generic-doc-prev () " Go back/out of " ( interactive ) ( cond (( string= major-mode "c-mode" ) ( call-interactively 'c-doc-prev )) (( string= major-mode "c++-mode" ) ( call-interactively 'c-doc-prev )) (( string= major-mode "diff-mode" ) ( call-interactively 'diff-hunk-prev )) ;; TODO, other modes ) ) ;; Handy in-document next/prev (based on doxy groups for eg) ( global-set-key ( kbd "C-}" ) 'generic-doc-next ) ( global-set-key ( kbd "C-{" ) 'generic-doc-prev ) ;; alternate access (page up/down) ( global-set-key ( kbd "<C-next>" ) 'generic-doc-next ) ( global-set-key ( kbd "<C-prior>" ) 'generic-doc-prev ) ;; handy, Same as S-{} ;; (global-set-key (kbd "<S-next>") 'evil-forward-paragraph) ;; (global-set-key (kbd "<S-prior>") 'evil-backward-paragraph) ;; Re-center is nicer ( global-set-key ( kbd "<S-next>" ) ( lambda () ( interactive ) ( evil-forward-paragraph ) ( recenter ))) ( global-set-key ( kbd "<S-prior>" ) ( lambda () ( interactive ) ( evil-backward-paragraph ) ( recenter ))) ;; https://emacs.stackexchange.com/questions/31454 ( defun paste-and-indent-after () ( interactive ) ( with-undo-collapse ( evil-paste-after 1 ) ( evil-indent ( evil-get-marker ?\[ ) ( evil-get-marker ?\] )))) ( defun paste-and-indent-before () ( interactive ) ( with-undo-collapse ( evil-paste-before 1 ) ( evil-indent ( evil-get-marker ?\[ ) ( evil-get-marker ?\] )))) ( evil-leader/set-key "p" 'paste-and-indent-after ) ( evil-leader/set-key "P" 'paste-and-indent-before )