* [PATCH] Support for switching to hexl-mode from image mode
@ 2016-01-23 15:29 Alexander Kuleshov
2016-01-26 17:57 ` John Wiegley
0 siblings, 1 reply; 2+ messages in thread
From: Alexander Kuleshov @ 2016-01-23 15:29 UTC (permalink / raw
To: emacs-devel; +Cc: Alexander Kuleshov
We can easily switch to text mode from the image mode by the
pressing of C-c C-c. But sometimes, it is more useful to open
an image in hex format. This patch provides new keybinding
for the image mode - C-c C-x which works like C-c C-c, but
executes switch to the hexl-mode. Like switching to text mode,
switching to hex mode supports switching back to the image-mode.
The patch contains following changes:
* lisp/image-mode.el: adapted comments at the top of file;
* lisp/image-mode.el: added new menu item to the Image menu:
'Show as hex';
* lisp/image-mode.el: added description of the new keybinding
to the image-mode help;
* lisp/image-mode.el: all help messages in minibuffer adapted
to new keybinding;
* lisp/image-mode.el: image-toggle-hex-display function added
which checks current mode and switch into text or image mode
depends on it;
* lisp/image-mode.el: add image-mode-to-text helper for
image-toggle-as-hex and image-toggle-as-text functions. It
is built on top of image-toggle-as-text function and used
for auto-mode-alist related preparation before switching
into text or hexl mode.
Patch tested on top of emacs-25 branch on fedora 23 with all
Linux related configured options.
---
lisp/image-mode.el | 96 ++++++++++++++++++++++++++++++++++++++++--------------
1 file changed, 71 insertions(+), 25 deletions(-)
diff --git a/lisp/image-mode.el b/lisp/image-mode.el
index e549b49..5ee00bb 100644
--- a/lisp/image-mode.el
+++ b/lisp/image-mode.el
@@ -24,8 +24,8 @@
;;; Commentary:
;; Defines a major mode for visiting image files
-;; that allows conversion between viewing the text of the file
-;; and viewing the file as an image. Viewing the image
+;; that allows conversion between viewing the text of the file,
+;; hex of the file and viewing the file as an image. Viewing the image
;; works by putting a `display' text-property on the
;; image data, with the image-data still present underneath; if the
;; resulting buffer file is saved to another name it will correctly save
@@ -374,6 +374,7 @@ image-mode-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map special-mode-map)
(define-key map "\C-c\C-c" 'image-toggle-display)
+ (define-key map "\C-c\C-x" 'image-toggle-hex-display)
(define-key map (kbd "SPC") 'image-scroll-up)
(define-key map (kbd "S-SPC") 'image-scroll-down)
(define-key map (kbd "DEL") 'image-scroll-down)
@@ -406,6 +407,8 @@ image-mode-map
'("Image"
["Show as Text" image-toggle-display :active t
:help "Show image as text"]
+ ["Show as Hex" image-toggle-hex-display :active t
+ :help "Show image as hex"]
"--"
["Fit to Window Height" image-transform-fit-to-height
:visible (eq image-type 'imagemagick)
@@ -480,6 +483,7 @@ image-mode-map
(defvar image-minor-mode-map
(let ((map (make-sparse-keymap)))
(define-key map "\C-c\C-c" 'image-toggle-display)
+ (define-key map "\C-c\C-x" 'image-toggle-hex-display)
map)
"Mode keymap for `image-minor-mode'.")
@@ -490,8 +494,8 @@ bookmark-make-record-function
;;;###autoload
(defun image-mode ()
"Major mode for image files.
-You can use \\<image-mode-map>\\[image-toggle-display]
-to toggle between display as an image and display as text.
+You can use \\<image-mode-map>\\[image-toggle-display] or \\<image-mode-map>\\[image-toggle-hex-display]
+to toggle between display as an image and display as text or hex.
Key bindings:
\\{image-mode-map}"
@@ -530,7 +534,7 @@ image-mode
(run-mode-hooks 'image-mode-hook)
(let ((image (image-get-display-property))
(msg1 (substitute-command-keys
- "Type \\[image-toggle-display] to view the image as "))
+ "Type \\[image-toggle-display] or \\[image-toggle-hex-display] to view the image as "))
animated)
(cond
((null image)
@@ -559,7 +563,7 @@ image-mode
;;; (substitute-command-keys
;;; "\\[image-toggle-animation] to animate."))))
(t
- (message "%s" (concat msg1 "text."))))))
+ (message "%s" (concat msg1 "text or hex."))))))
(error
(image-mode-as-text)
@@ -585,19 +589,10 @@ image-minor-mode
(add-hook 'change-major-mode-hook (lambda () (image-minor-mode -1)) nil t)))
;;;###autoload
-(defun image-mode-as-text ()
+(defun image-mode-to-text ()
"Set a non-image mode as major mode in combination with image minor mode.
-A non-image major mode found from `auto-mode-alist' or Fundamental mode
-displays an image file as text. `image-minor-mode' provides the key
-\\<image-mode-map>\\[image-toggle-display] to switch back to `image-mode'
-to display an image file as the actual image.
-
-You can use `image-mode-as-text' in `auto-mode-alist' when you want
-to display an image file as text initially.
-
-See commands `image-mode' and `image-minor-mode' for more information
-on these modes."
- (interactive)
+A non-mage major mode found from `auto-mode-alist' or fundamental mode
+displays an image file as hex."
;; image-mode-as-text = normal-mode + image-minor-mode
(let ((previous-image-type image-type)) ; preserve `image-type'
(if image-mode-previous-major-mode
@@ -625,12 +620,49 @@ image-mode-as-text
;; Enable image minor mode with `C-c C-c'.
(image-minor-mode 1)
;; Show the image file as text.
- (image-toggle-display-text)
- (message "%s" (concat
- (substitute-command-keys
- "Type \\[image-toggle-display] to view the image as ")
- (if (image-get-display-property)
- "text" "an image") "."))))
+ (image-toggle-display-text)))
+
+(defun image-mode-as-hex ()
+ "Set a non-image mode as major mode in combination with image minor mode.
+A non-mage major mode found from `auto-mode-alist' or fundamental mode
+displays an image file as hex. `image-minor-mode' provides the key
+\\<image-mode-map>\\[image-toggle-hex-display] to switch back to `image-mode'
+to display an image file as the actual image.
+
+You can use `image-mode-as-text' in `auto-mode-alist' when you wanto
+to display an image file as text initially.
+
+See commands `image-mode' and `image-minor-mode' for more information
+on these modes."
+ (interactive)
+ (image-mode-to-text)
+ ;; Turn on hexl-mode
+ (hexl-mode)
+ (message "%s" (concat
+ (substitute-command-keys
+ "Type \\[image-toggle-hex-display] or \\[image-toggle-display] to view the image as ")
+ (if (image-get-display-property)
+ "hex" "an image or text") ".")))
+
+(defun image-mode-as-text ()
+ "Set a non-image mode as major mode in combination with image minor mode.
+A non-image major mode found from `auto-mode-alist' or Fundamental mode
+displays an image file as text. `image-minor-mode' provides the key
+\\<image-mode-map>\\[image-toggle-display] to switch back to `image-mode'
+to display an image file as the actual image.
+
+You can use `image-mode-as-text' in `auto-mode-alist' when you want
+to display an image file as text initially.
+
+See commands `image-mode' and `image-minor-mode' for more information
+on these modes."
+ (interactive)
+ (image-mode-to-text)
+ (message "%s" (concat
+ (substitute-command-keys
+ "Type \\[image-toggle-display] or \\[image-toggle-hex-display] to view the image as ")
+ (if (image-get-display-property)
+ "text" "an image or hex") ".")))
(define-obsolete-function-alias 'image-mode-maybe 'image-mode "23.2")
@@ -725,6 +757,18 @@ image-toggle-display-image
(if (called-interactively-p 'any)
(message "Repeat this command to go back to displaying the file as text"))))
+(defun image-toggle-hex-display ()
+ "Toggle between image and text display.
+If the current buffer is displaying an image file as an image,
+call `image-mode-as-hex' to switch to text. Otherwise, display
+the image by calling `image-mode'."
+ (interactive)
+ (if (image-get-display-property)
+ (image-mode-as-hex)
+ (if (eq major-mode 'fundamental-mode)
+ (image-mode-as-hex)
+ (image-mode))))
+
(defun image-toggle-display ()
"Toggle between image and text display.
If the current buffer is displaying an image file as an image,
@@ -733,7 +777,9 @@ image-toggle-display
(interactive)
(if (image-get-display-property)
(image-mode-as-text)
- (image-mode)))
+ (if (eq major-mode 'hexl-mode)
+ (image-mode-as-text)
+ (image-mode))))
(defun image-kill-buffer ()
"Kill the current buffer."
--
2.7.0.25.gfc10eb5
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] Support for switching to hexl-mode from image mode
2016-01-23 15:29 [PATCH] Support for switching to hexl-mode from image mode Alexander Kuleshov
@ 2016-01-26 17:57 ` John Wiegley
0 siblings, 0 replies; 2+ messages in thread
From: John Wiegley @ 2016-01-26 17:57 UTC (permalink / raw
To: Alexander Kuleshov; +Cc: emacs-devel
>>>>> Alexander Kuleshov <kuleshovmail@gmail.com> writes:
> We can easily switch to text mode from the image mode by the pressing of C-c
> C-c. But sometimes, it is more useful to open an image in hex format. This
> patch provides new keybinding for the image mode - C-c C-x which works like
> C-c C-c, but executes switch to the hexl-mode. Like switching to text mode,
> switching to hex mode supports switching back to the image-mode.
This sounds useful to me.
--
John Wiegley GPG fingerprint = 4710 CF98 AF9B 327B B80F
http://newartisans.com 60E1 46C4 BD1A 7AC1 4BA2
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2016-01-26 17:57 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-01-23 15:29 [PATCH] Support for switching to hexl-mode from image mode Alexander Kuleshov
2016-01-26 17:57 ` John Wiegley
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.