[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] master 91704f9 141/215: Automatically attach to a command buffer
From: |
Rocky Bernstein |
Subject: |
[elpa] master 91704f9 141/215: Automatically attach to a command buffer when enabling short-key-mode |
Date: |
Sat, 30 Jul 2016 14:49:00 +0000 (UTC) |
branch: master
commit 91704f93d344a24d0bdcd4a987905e35563e3488
Author: Clément Pit--Claudel <address@hidden>
Commit: Clément Pit--Claudel <address@hidden>
Automatically attach to a command buffer when enabling short-key-mode
When enabling short-key-mode in an orphan source buffer (i.e. one that
isn't attached to a command buffer), prompt the user for a command
buffer to attach to instead of complaining (and complain only if there
is no available command buffer).
Additionally make shortkey-mode-setup more robust by restoring the
original local map properly disabling the toolbar after exiting.
Closes #36; thanks for the suggestion!
---
realgud/common/buffer/source.el | 28 ++++++++++++-
realgud/common/shortkey.el | 86 ++++++++++++++++-----------------------
2 files changed, 61 insertions(+), 53 deletions(-)
diff --git a/realgud/common/buffer/source.el b/realgud/common/buffer/source.el
index e99a026..e052149 100644
--- a/realgud/common/buffer/source.el
+++ b/realgud/common/buffer/source.el
@@ -1,11 +1,11 @@
;;; Copyright (C) 2010, 2012-2015 Rocky Bernstein <address@hidden>
;;; source-code buffer code
(eval-when-compile
- (require 'cl-lib)
(defvar realgud-srcbuf-info) ;; is buffer local
(defvar realgud-cmdbuf-info) ;; in the cmdbuf, this is buffer local
)
+(require 'cl-lib)
(require 'load-relative)
(require-relative-list '("../helper" "../key") "realgud-")
@@ -38,6 +38,7 @@ to be debugged."
;; variable. Short-key-mode may change the read-only
;; state, so we need restore this value when leaving
;; short-key mode
+ prev-local-map ;; Local map before enabling short-key-mode
loc-hist ;; ring of locations seen
@@ -54,6 +55,7 @@ to be debugged."
(realgud-struct-field-setter "realgud-srcbuf-info" "cmdproc")
(realgud-struct-field-setter "realgud-srcbuf-info" "short-key?")
(realgud-struct-field-setter "realgud-srcbuf-info" "was-read-only?")
+(realgud-struct-field-setter "realgud-srcbuf-info" "prev-local-map")
(defun realgud-srcbuf-info-set? ()
"Return true if `realgud-srcbuf-info' is set."
@@ -68,6 +70,30 @@ to be debugged."
(not (buffer-killed? (realgud-sget 'srcbuf-info 'cmdproc)))
)))
+(defun realgud--read-cmd-buf (prompt)
+ "Read a command buffer, prompting with PROMPT."
+ (let* ((cmd-bufs (cl-remove-if-not #'realgud-cmdbuf? (buffer-list)))
+ (cmd-buf-names (mapcar #'buffer-name cmd-bufs)))
+ (when cmd-buf-names
+ ;; Use completing-read instead of read-buffer: annoyingly, ido's
+ ;; read-buffer ignores predicates.
+ (get-buffer (completing-read prompt cmd-buf-names nil t
+ nil nil (car cmd-buf-names))))))
+
+(defun realgud--ensure-attached (&optional src-buf)
+ "Try to attach SRC-BUF to a command buffer.
+If SRC-BUF is already attached, do nothing. Otherwise, prompt
+the user for a command buffer to associate SRC-BUF to. Returns
+non-nil if association was successful. SRC-BUF defaults to
+current buffer."
+ (setq src-buf (or src-buf (current-buffer)))
+ (unless (realgud-srcbuf? src-buf)
+ (let ((cmd-buf (realgud--read-cmd-buf "Command buffer to associate to: ")))
+ (if cmd-buf
+ (realgud-srcbuf-init src-buf cmd-buf)
+ (message "No debugger process found to attach %s to" (buffer-name)))))
+ (realgud-srcbuf? src-buf))
+
(defun realgud-srcbuf-debugger-name (&optional src-buf)
"Return the debugger name recorded in the debugger command-process buffer."
(with-current-buffer-safe (or src-buf (current-buffer))
diff --git a/realgud/common/shortkey.el b/realgud/common/shortkey.el
index 6c03a4f..8eb9392 100644
--- a/realgud/common/shortkey.el
+++ b/realgud/common/shortkey.el
@@ -22,6 +22,7 @@
(declare-function realgud-populate-src-buffer-map-plain 'realgud-key)
(declare-function realgud-srcbuf-info-short-key?=, 'realgud-source)
(declare-function realgud-srcbuf-info-was-read-only?= 'realgud-source)
+(declare-function realgud-srcbuf-info-prev-local-map= 'realgud-source)
(declare-function realgud-srcbuf? 'realgud-buffer-source)
;; (defvar realgud::tool-bar-map) ;; fully defined in track-mode.el
@@ -102,58 +103,39 @@ The buffer is read-only when the minor mode is active.
))
(defun realgud-short-key-mode-setup (mode-on?)
- "Called when entering or leaving `realgud-short-key-mode'. Variable
-MODE-ON? a boolean which specifies if we are going into or out of this mode."
- (if (realgud-srcbuf?)
- (let* ((cmdbuf (realgud-get-cmdbuf))
- (shortkey-keymap (realgud-get-short-key-mode-map cmdbuf))
- )
-
- ;; If there's a shortkey keymap that is custom
- ;; for this debugger mode, use it.
- (when shortkey-keymap
- (cond
- (mode-on?
- (set (make-local-variable 'tool-bar-map) realgud:tool-bar-map)
- (use-local-map shortkey-keymap))
- ('t
- (kill-local-variable 'realgud:tool-bar-map)
- (use-local-map nil))
- ))
-
- ;; Ensure action only is performed when the state actually is toggled.
- ;; or when not read-only
- (when (or (not buffer-read-only)
- (not (eq (realgud-sget 'srcbuf-info 'short-key?) mode-on?)))
- ;; Save the current state, so we can determine when the
- ;; state is toggled in the future.
- (when (not (eq (realgud-sget 'srcbuf-info 'short-key?) mode-on?))
- (realgud-srcbuf-info-short-key?= mode-on?)
- (setq realgud-short-key-mode mode-on?)
- (if mode-on?
- ;; mode is being turned on.
- (progn
- (realgud-srcbuf-info-was-read-only?= buffer-read-only)
-
- ;; If there's a shortkey keymap that is custom
- ;; for this debugger mode, use it.
- (if shortkey-keymap (use-local-map shortkey-keymap))
-
- (local-set-key [m-insert] 'realgud-short-key-mode)
- (when realgud-srcbuf-lock (setq buffer-read-only t))
- (run-mode-hooks 'realgud-short-key-mode-hook))
- ;; mode is being turned off: restore read-only state.
- (setq buffer-read-only
- (realgud-sget 'srcbuf-info 'was-read-only?))))
- ;; (with-current-buffer-safe cmdbuf
- ;; (realgud-cmdbuf-info-src-shortkey?= mode-on?)
- ;; (realgud-cmdbuf-info-in-srcbuf?= mode-on?)
- ;; )
- ))
- (progn
- (setq realgud-short-key-mode nil)
- (error "buffer %s does not seem to be attached to a debugger"
- (buffer-name)))))
+ "Set up or tear down `realgud-short-key-mode'.
+MODE-ON? is a boolean indicating whether the mode should be
+turned on or off."
+ (setq realgud-short-key-mode mode-on?)
+ ;; When enabling, try to find a command buffer to attach to.
+ (when (and realgud-short-key-mode (not (realgud--ensure-attached)))
+ (setq realgud-short-key-mode nil))
+ ;; Now apply mode change
+ (cond
+ ;; Mode was just enabled
+ (realgud-short-key-mode
+ ;; Record info to restore it when disabling
+ (unless (equal (realgud-sget 'srcbuf-info 'short-key?)
realgud-short-key-mode)
+ (realgud-srcbuf-info-prev-local-map= (current-local-map))
+ (realgud-srcbuf-info-was-read-only?= buffer-read-only))
+ ;; Apply local map
+ (let ((keymap (realgud-get-short-key-mode-map (realgud-get-cmdbuf))))
+ (when keymap (use-local-map keymap)))
+ ;; Finish setting up
+ (set (make-local-variable 'tool-bar-map) realgud:tool-bar-map)
+ (local-set-key [m-insert] #'realgud-short-key-mode)
+ (setq buffer-read-only realgud-srcbuf-lock)
+ (run-mode-hooks 'realgud-short-key-mode-hook))
+ ;; Mode was just disabled
+ (t
+ (kill-local-variable 'tool-bar-map)
+ (when (realgud-srcbuf-info-set?)
+ ;; Restore previous state
+ (use-local-map (realgud-sget 'srcbuf-info 'prev-local-map))
+ (setq buffer-read-only (realgud-sget 'srcbuf-info 'was-read-only?)))))
+ ;; Record state
+ (when (realgud-srcbuf-info-set?)
+ (realgud-srcbuf-info-short-key?= realgud-short-key-mode)))
(defun realgud-short-key-mode-off ()
"Turn off `realgud-short-key-mode' in all buffers."
- [elpa] master d437c3d 113/215: Merge pull request #81 from rocky/ipdb-complete, (continued)
- [elpa] master d437c3d 113/215: Merge pull request #81 from rocky/ipdb-complete, Rocky Bernstein, 2016/07/30
- [elpa] master 5155528 105/215: Bump version, Rocky Bernstein, 2016/07/30
- [elpa] master 76cb0d4 108/215: Fix wrong setq call, Rocky Bernstein, 2016/07/30
- [elpa] master 1716c8a 109/215: Towards fixing up tab completion in ipdb., Rocky Bernstein, 2016/07/30
- [elpa] master e04eeb2 117/215: Improve eval: run on region if active, and fall back to prompting, Rocky Bernstein, 2016/07/30
- [elpa] master fd00428 118/215: Merge pull request #86 from cpitclaudel/83-improve-e-binding, Rocky Bernstein, 2016/07/30
- [elpa] master b275bd2 119/215: Improve behavior of delete, enable, and disable, Rocky Bernstein, 2016/07/30
- [elpa] master ca33520 120/215: Add a prefix arg to "continue" command, Rocky Bernstein, 2016/07/30
- [elpa] master 379528b 106/215: Disable ipdb until melpa is updated, Rocky Bernstein, 2016/07/30
- [elpa] master e049c12 124/215: Merge pull request #89 from cpitclaudel/87-improve-delete-command, Rocky Bernstein, 2016/07/30
- [elpa] master 91704f9 141/215: Automatically attach to a command buffer when enabling short-key-mode,
Rocky Bernstein <=
- [elpa] master 40e73bc 123/215: Merge pull request #92 from cpitclaudel/82-add-safe-mode, Rocky Bernstein, 2016/07/30
- [elpa] master f3426ed 072/215: Merge branch 'master' of github.com:rocky/emacs-dbgr, Rocky Bernstein, 2016/07/30
- [elpa] master 4e85910 088/215: Add pytest error message pattern in python debuggers, Rocky Bernstein, 2016/07/30
- [elpa] master 01617c5 100/215: ipdb: add regex test, Rocky Bernstein, 2016/07/30
- [elpa] master 5395757 091/215: More cleanup from last two commits., Rocky Bernstein, 2016/07/30
- [elpa] master 1e2fe55 098/215: Add rspec loc pattern.., Rocky Bernstein, 2016/07/30
- [elpa] master ea5dcba 094/215: ipdb: add new debugger based on pdb, Rocky Bernstein, 2016/07/30
- [elpa] master d1c1176 146/215: Simplify the implementation of realgud:cmdbuf-associate, Rocky Bernstein, 2016/07/30
- [elpa] master 95f076e 150/215: Improve IPDB completion, Rocky Bernstein, 2016/07/30
- [elpa] master d45788c 152/215: Merge pull request #106 from rocky/75-ipdb-completion, Rocky Bernstein, 2016/07/30