summaryrefslogtreecommitdiffstats
path: root/lisp/ravi-init-maps.el
blob: 7b9b4196448eb9a9d04cacb84422e34bb86e72d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
;;; ravi-init-maps.el --- extra keys                 -*- lexical-binding: t; -*-

;; Copyright (C) 2015  Ravi R Kiran

;; Author:  <ravi@nero.lan>
;; 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 <http://www.gnu.org/licenses/>.

;;; Commentary:

;; Extra keybindings

(use-package hydra
  :config
  (use-package pretty-hydra)
  :ensure t)
(use-package hydra-posframe
  :load-path (lambda () (ravi/emacs-file "site-lisp/hydra-posframe"))
  :after hydra
  :if window-system
  :config
  (hydra-posframe-mode)
  (setq hydra-posframe-poshandler #'posframe-poshandler-frame-bottom-center)
  :diminish hydra-posframe-mode)

(use-package operate-on-number
  :bind (("M-g M-d" . hydra-operate-on-number/body))
  :config
  (progn
    ;; Monkey-patch to fix:
    ;;   https://github.com/knu/operate-on-number.el/issues/5
    ;;   https://github.com/knu/operate-on-number.el/issues/4
    (defun oon--replace-number (parsed number)
      "Replace a number specified by PARSED with NUMBER."
      (let* ((base (elt parsed 0))
             (beg  (elt parsed 1))
             (spos (elt parsed 2))
             (nbeg (elt parsed 3))
             (nend (elt parsed 4))
             (str (buffer-substring-no-properties nbeg nend))
             (abs (if (numberp number) (abs number) number))
             (sign (if (or (not (numberp number)) (< 0 number)) ?+ ?-)))
        (if (and (null spos)
                 (= sign ?-))
            (error "cannot replace with a negative number!"))
        (goto-char nbeg)
        (delete-region nbeg nend)
        (insert (oon--format-number abs base str))
        (if spos
            (save-excursion
              (goto-char spos)
              (if (or (when (looking-at "[+-]")
                        (delete-char 1) t)
                      (= sign ?-))
                  (insert-char sign))))))

    (defhydra hydra-operate-on-number ()
      "
    Arithmetic operations: + - * /
    Remainder: \\
    Exponent: \\^
    Arithmetic shift: < >
    Base conversion: b o x X #
    Format: %%%%
    "
      ("+" apply-operation-to-number-at-point)
      ("-" apply-operation-to-number-at-point)
      ("*" apply-operation-to-number-at-point)
      ("/" apply-operation-to-number-at-point)
      ("\\" apply-operation-to-number-at-point)
      ("^" apply-operation-to-number-at-point)
      ("<" apply-operation-to-number-at-point)
      (">" apply-operation-to-number-at-point)
      ("b" apply-operation-to-number-at-point :exit t)
      ("o" apply-operation-to-number-at-point :exit t)
      ("x" apply-operation-to-number-at-point :exit t)
      ("X" apply-operation-to-number-at-point :exit t)
      ("#" apply-operation-to-number-at-point :exit t)
      ("%" apply-operation-to-number-at-point :exit t))))

(provide 'ravi-init-maps)
;;; ravi-init-maps.el ends here