branch: elpa/logview
commit 5b517de7ace17d7b64549264d883af1a64205703
Author: Paul Pogonyshev <pogonys...@gmail.com>
Commit: Paul Pogonyshev <pogonys...@gmail.com>

    Improve log editing possibilities and simplify submode option customization 
a bit.
    
    If submode guessing fails, buffer is not switched to read-only mode
    anymore.  When buffer is read-write, (nearly) all Logview commands are
    disabled, so you get `self-insert-command' bindings back.  There is
    now C-c C-s binding to customize all submode-related variables at once.
---
 README.md  | 22 ++++++++++++++++------
 logview.el | 59 +++++++++++++++++++++++++++++++++++++++++++----------------
 2 files changed, 59 insertions(+), 22 deletions(-)

diff --git a/README.md b/README.md
index ba046fe84f..f749143a04 100644
--- a/README.md
+++ b/README.md
@@ -24,16 +24,21 @@ submodes it has.
 
 If it succeeds in guessing, you will see major mode specifed as
 ‘Logview/...’ in the modeline, where the second part is the submode
-name.  In case it fails, you will see it complain in the echo area and
-the buffer will not be highlighted.
+name.
+
+In case it fails, you will see it complain in the echo area.  The
+buffer will also not be highlighted or switched to read-only mode, so
+you will be able to edit it.
 
 #### What to do if Logview mode fails to guess format
 
-Currently your only option is to customize the mode.  You will want to
-add some entries to either ‘Additional Level Mappings’, ‘Additional
-Submodes’, ‘Additional Timestamp Formats’, or maybe to all three.
+Currently your only option is to customize the mode.  `C-c C-s` will
+show you only those options that are relevant to submode guessing.
+You will need to customize at least one of those, or maybe all three.
+All the variables are well-documented in customization interface.
 
-All these variables are well-documented in customization interface.
+If you think your log format is standard enough, you can open an issue
+and request format addition to the list of mode built-ins.
 
 
 ### Commands
@@ -42,6 +47,11 @@ Nearly all commands have some use for prefix argument.  It 
can be
 usually just guessed, but you can always check individual command
 documentation within Emacs.
 
+When buffer is switched to read-write mode, Logview automatically
+deactivates all its commands so as to not interfere with editing.
+Once you switch the buffer back to read-only mode, commands will be
+active again.
+
 #### Movement
 
 * All standard Emacs commands
diff --git a/logview.el b/logview.el
index 9436a404ab..0bf53b2224 100644
--- a/logview.el
+++ b/logview.el
@@ -453,6 +453,7 @@ that the line is not the first in the buffer."
 
 (defvar logview-mode-map
   (let ((map (make-sparse-keymap)))
+    (suppress-keymap map)
     (dolist (binding '(;; Movement commands.
                        ("TAB" logview-go-to-message-beginning)
                        ("n"   logview-next-entry)
@@ -499,25 +500,28 @@ that the line is not the first in the buffer."
                        ;; Option changing commands.
                        ("o v" logview-toggle-copy-visible-text-only)
                        ("o e" logview-toggle-show-ellipses)
+                       ("o S" logview-customize-submode-options)
+                       ;; For compatibility with the inactive keymap.
+                       ("C-c C-s" logview-customize-submode-options)
                        ;; Miscellaneous commands.
                        ("?"   logview-mode-help)
                        ("q"   bury-buffer)
-                       ;; Simplified universal argument command rebindings.
-                       ("u"   universal-argument)
-                       ("-"   negative-argument)
-                       ("0"   digit-argument)
-                       ("1"   digit-argument)
-                       ("2"   digit-argument)
-                       ("3"   digit-argument)
-                       ("4"   digit-argument)
-                       ("5"   digit-argument)
-                       ("6"   digit-argument)
-                       ("7"   digit-argument)
-                       ("8"   digit-argument)
-                       ("9"   digit-argument)))
+                       ;; Simplified universal argument command
+                       ;; rebindings.  Digits and minus are set up by
+                       ;; 'suppress-keymap' already.
+                       ("u"   universal-argument)))
       (define-key map (kbd (car binding)) (cadr binding)))
     map))
 
+(defvar logview-mode-inactive-map
+  (let ((map (make-sparse-keymap)))
+    (define-key map (kbd "C-c C-s") 'logview-customize-submode-options)
+    map)
+  "Keymap used by `logview-mode' when the mode is inactive.
+Mode is inactive when the buffer is not read-only (to not
+interfere with editing) or if submode wasn't guessed
+successfully.")
+
 
 ;;;###autoload
 (define-derived-mode logview-mode nil "Logview"
@@ -526,10 +530,18 @@ that the line is not the first in the buffer."
   (logview--guess-submode)
   (logview--split-region-into-entries (point-min) (point-max) 'report-progress)
   (add-hook 'after-change-functions 'logview--split-region-into-entries t t)
-  (read-only-mode 1)
+  (when logview--entry-regexp
+    (read-only-mode 1))
+  (add-hook 'read-only-mode-hook 'logview--update-keymap nil t)
+  (logview--update-keymap)
   (set (make-local-variable 'filter-buffer-substring-function) 
'logview--buffer-substring-filter)
   (add-hook 'change-major-mode-hook 'logview--exiting-mode nil t))
 
+(defun logview--update-keymap ()
+  (use-local-map (if (and buffer-read-only logview--entry-regexp)
+                     logview-mode-map
+                   logview-mode-inactive-map)))
+
 (defun logview--exiting-mode ()
   ;; Remove custom invisibility property values, as otherwise other
   ;; modes will show empty buffers.  Also remove face property, as we
@@ -550,7 +562,7 @@ that the line is not the first in the buffer."
                                                (logview--initialize-submode 
name definition first-line)
                                              (error (warn 
(error-message-string error)))))
                                          logview-additional-submodes 
logview-std-submodes)
-          (message "Logview mode was unable to determine log format; please 
consult documentation"))))))
+          (message "Cannot determine log format; press C-c C-s to customize 
relevant options"))))))
 
 
 
@@ -997,6 +1009,20 @@ argument is positive, disable it otherwise."
                                   "Hidden log entries are completely 
invisible")
   (logview--update-invisibility-spec))
 
+(defun logview-customize-submode-options ()
+  "Customize all options that affect submode selection.
+These are:
+* `logview-additional-submodes'
+* `logview-additional-level-mappings'
+* `logview-additional-timestamp-formats'"
+  (interactive)
+  ;; Existing entry point only customizes single option, we need three
+  ;; at once (but this hardly warrants a separate group).
+  (custom-buffer-create-other-window '((logview-additional-submodes          
custom-variable)
+                                       (logview-additional-level-mappings    
custom-variable)
+                                       (logview-additional-timestamp-formats 
custom-variable))
+                                     "*Customize Logview Submodes*"))
+
 (defun logview--toggle-option-locally (variable arg &optional show-message 
message-if-true message-if-false)
   (set (make-local-variable variable)
        (if (eq arg 'toggle)
@@ -1090,6 +1116,7 @@ argument is positive, disable it otherwise."
                                           (intern (format "logview-%s-entry" 
(symbol-name final-level)))
                                           (intern (format "logview-level-%s" 
(symbol-name final-level)))))
                         logview--submode-level-data))))
+            (logview--update-keymap)
             (throw 'success nil))
         (when (not (memq 'timestamp features))
           ;; Else we will maybe retry with different timestamp formats.
@@ -1098,7 +1125,7 @@ argument is positive, disable it otherwise."
 
 (defun logview--assert (&rest assertions)
   (unless logview--entry-regexp
-    (error "Logview mode was unable to determine log format; please consult 
documentation"))
+    (error "Couldn't determine log format; press C-c C-s to customize relevant 
options"))
   (dolist (assertion assertions)
     (unless (memq assertion logview--submode-features)
       (error (cdr (assq assertion '((level  . "Log lacks entry levels")

Reply via email to