[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/nhexl-mode 6c31e4e 15/23: * nhexl-mode.el: Make C-u use
From: |
Stefan Monnier |
Subject: |
[elpa] externals/nhexl-mode 6c31e4e 15/23: * nhexl-mode.el: Make C-u use hexadecimal |
Date: |
Sat, 28 Nov 2020 18:35:44 -0500 (EST) |
branch: externals/nhexl-mode
commit 6c31e4e8475548f9cfd3440c45374a8f25b04e77
Author: Stefan Monnier <monnier@iro.umontreal.ca>
Commit: Stefan Monnier <monnier@iro.umontreal.ca>
* nhexl-mode.el: Make C-u use hexadecimal
(nhexl-universal-argument-map): New var.
(nhexl--universal-argument-description)
(nhexl--universal-argument-mode): New advice functions.
(nhexl-digit-argument): New command.
---
nhexl-mode.el | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 71 insertions(+), 1 deletion(-)
diff --git a/nhexl-mode.el b/nhexl-mode.el
index 4635251..b7221bf 100644
--- a/nhexl-mode.el
+++ b/nhexl-mode.el
@@ -4,7 +4,7 @@
;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
;; Keywords: data
-;; Version: 0.9
+;; Version: 1.0
;; Package-Requires: ((emacs "24.4") (cl-lib "0.5"))
;; This program is free software; you can redistribute it and/or modify
@@ -40,6 +40,9 @@
;; - `nhexl-overwrite-only-mode': a minor mode to try and avoid moving text.
;; In this minor mode, not only self-inserting keys overwrite existing
;; text, but commands like `yank' and `kill-region' as well.
+;;
+;; - it overrides C-u to use hexadecimal, so you can do C-u a 4 C-f
+;; to advance by #xa4 characters.
;; Even though the hex addresses displayed by this mode aren't actually
;; part of the buffer's text (contrary to hexl-mode, for example), you can
@@ -836,5 +839,72 @@ existing text, if needed with
`nhexl-overwrite-clear-byte'."
(unless (eql pow2bytes nhexl-line-width)
(setq-local nhexl-line-width pow2bytes))))))
+;;;;; The main prefix command.
+
+(defvar nhexl-universal-argument-map
+ (let ((map (make-sparse-keymap)))
+ (set-keymap-parent map universal-argument-map)
+ (define-key map [?\C-u] 'universal-argument-more)
+ (define-key map [remap digit-argument] 'nhexl-digit-argument)
+ (dolist (k '("a" "b" "c" "d" "e" "f"))
+ (define-key map k 'nhexl-digit-argument))
+ map)
+ "Keymap used while processing nhexl-mode's \\[universal-argument].")
+
+;; FIXME: Using advice is ugly!
+
+;; Instead of an advice, we'd prefer to replace universal-argument--description
+;; on prefix-command-echo-keystrokes-functions, but there's no mechanism to
+;; do that.
+(advice-add 'universal-argument--description :around
+ #'nhexl--universal-argument-description)
+(defun nhexl--universal-argument-description (orig-fun &rest args)
+ (cond
+ ((not nhexl-mode) (apply orig-fun args))
+ ((null prefix-arg) nil)
+ (t
+ (concat "C-u"
+ (pcase prefix-arg
+ (`(-) " -")
+ (`(,(and (pred integerp) n))
+ (let ((str ""))
+ (while (and (> n 4) (= (mod n 4) 0))
+ (setq str (concat str " C-u"))
+ (setq n (/ n 4)))
+ (if (= n 4) str (format " %s" prefix-arg))))
+ ((pred integerp) (format " #x%X" prefix-arg))
+ (_ (format " %s" prefix-arg)))))))
+
+(advice-add 'universal-argument--mode :around
+ #'nhexl--universal-argument-mode)
+(defun nhexl--universal-argument-mode (orig-fun &rest args)
+ (if (not nhexl-mode)
+ (apply orig-fun args)
+ (let ((universal-argument-map nhexl-universal-argument-map))
+ (apply orig-fun args))))
+
+(defun nhexl-digit-argument (arg)
+ "Part of the hexadecimal numeric argument for the next command.
+\\[universal-argument] following digits or minus sign ends the argument."
+ (interactive "P")
+ (prefix-command-preserve-state)
+ (let* ((keys (this-command-keys))
+ (key (aref keys (1- (length keys))))
+ (char (if (integerp key) (logand key #x7f)))
+ (digit (cond
+ ((<= ?a char ?f) (+ 10 (- char ?a)))
+ ((<= ?A char ?F) (+ 10 (- char ?A)))
+ ((<= ?0 char ?9) (- char ?0)))))
+ (setq prefix-arg (cond ((integerp arg)
+ (+ (* arg 16)
+ (if (< arg 0) (- digit) digit)))
+ ((eq arg '-)
+ ;; Treat -0 as just -, so that -01 will work.
+ (if (zerop digit) '- (- digit)))
+ (t
+ digit))))
+ (universal-argument--mode))
+
+
(provide 'nhexl-mode)
;;; nhexl-mode.el ends here
- [elpa] externals/nhexl-mode 16b716f 03/23: nhexl-mode.el: Fix last change., (continued)
- [elpa] externals/nhexl-mode 16b716f 03/23: nhexl-mode.el: Fix last change., Stefan Monnier, 2020/11/28
- [elpa] externals/nhexl-mode e2450fd 09/23: * nhexl-mode.el: Hide undisplayable chars by default, Stefan Monnier, 2020/11/28
- [elpa] externals/nhexl-mode 1082cc6 12/23: * nhexl-mode/nhexl-mode.el: Fix minor issues, Stefan Monnier, 2020/11/28
- [elpa] externals/nhexl-mode 8310ad1 13/23: * nhexl-mode/nhexl-mode.el: Improve multi-window behavior, Stefan Monnier, 2020/11/28
- [elpa] externals/nhexl-mode 1cb2581 14/23: * nhexl-mode/nhexl-mode.el: Get rid of the spurious top empty line, Stefan Monnier, 2020/11/28
- [elpa] externals/nhexl-mode 42110aa 16/23: * nhexl-mode.el: Improve handling of mouse events, Stefan Monnier, 2020/11/28
- [elpa] externals/nhexl-mode fa39048 17/23: * nhexl-mode.el: Add isearch and highlight to hex area, Stefan Monnier, 2020/11/28
- [elpa] externals/nhexl-mode 9549cf5 21/23: * packages/nhexl-mode/nhexl-mode.el (nhexl-separate-line): New user config, Stefan Monnier, 2020/11/28
- [elpa] externals/nhexl-mode ec80692 22/23: * packages/nhexl-mode/nhexl-mode.el (nhexl-mode-map): Remap EOL/BOL, Stefan Monnier, 2020/11/28
- [elpa] externals/nhexl-mode 903f1b6 23/23: * .gitignore: New file, Stefan Monnier, 2020/11/28
- [elpa] externals/nhexl-mode 6c31e4e 15/23: * nhexl-mode.el: Make C-u use hexadecimal,
Stefan Monnier <=
- [elpa] externals/nhexl-mode d8fa839 18/23: * nhexl-mode/nhexl-mode.el: Fix performance bug#33708, Stefan Monnier, 2020/11/28
- [elpa] externals/nhexl-mode c0dac99 20/23: * packages/nhexl-mode/nhexl-mode.el (nhexl-group-size): New user config, Stefan Monnier, 2020/11/28
- [elpa] externals/nhexl-mode 630f003 19/23: * nhexl-mode.el (nhexl-nibble-self-insert): Obey overwrite-mode, Stefan Monnier, 2020/11/28