all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Leo <sdl.web@gmail.com>
To: 8463@debbugs.gnu.org
Cc: Daniel Colascione <dan.colascione@gmail.com>
Subject: bug#8463: 24.0.50; [PATCH] Direct Edit in *Occur* Buffer
Date: Sun, 10 Apr 2011 16:14:53 +0800	[thread overview]
Message-ID: <m1lizi5xqa.fsf@th041153.ip.tsinghua.edu.cn> (raw)

[-- Attachment #1: Type: text/plain, Size: 373 bytes --]

I have extended the occur feature in Emacs to allow direct editing in
the *Occur* buffer by propagating the changes to the original buffers.

With the attached preliminary patch, one can press `C-x C-q' or `C-c
C-c' to enter occur-edit-mode and start editing. Pressing `C-x C-q' or
`C-c C-c' again finishes the edit.

Comments are highly welcomed. Thanks in advance.

Leo


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: occur.diff --]
[-- Type: text/x-diff, Size: 4576 bytes --]

=== modified file 'lisp/replace.el'
--- lisp/replace.el	2011-04-08 03:05:58 +0000
+++ lisp/replace.el	2011-04-10 08:02:15 +0000
@@ -761,7 +761,8 @@
   (let ((map (make-sparse-keymap)))
     ;; We use this alternative name, so we can use \\[occur-mode-mouse-goto].
     (define-key map [mouse-2] 'occur-mode-mouse-goto)
-    (define-key map "\C-c\C-c" 'occur-mode-goto-occurrence)
+    (define-key map "\C-c\C-c" 'occur-edit-mode)
+    (define-key map "\C-x\C-q" 'occur-edit-mode)
     (define-key map "\C-m" 'occur-mode-goto-occurrence)
     (define-key map "o" 'occur-mode-goto-occurrence-other-window)
     (define-key map "\C-o" 'occur-mode-display-occurrence)
@@ -815,6 +816,18 @@
     map)
   "Keymap for `occur-mode'.")
 
+(defvar occur-edit-mode-map
+  (let ((map (make-sparse-keymap)))
+    (define-key map [mouse-2] 'occur-mode-mouse-goto)
+    (define-key map "\C-c\C-c" 'occur-edit-mode-finish)
+    (define-key map "\C-x\C-q" 'occur-edit-mode-finish)
+    (define-key map "\C-m" 'occur-mode-goto-occurrence)
+    (define-key map "\C-o" 'occur-mode-display-occurrence)
+    (define-key map "\M-n" 'occur-next)
+    (define-key map "\M-p" 'occur-prev)
+    (define-key map "\C-c\C-f" 'next-error-follow-minor-mode)
+    map))
+
 (defvar occur-revert-arguments nil
   "Arguments to pass to `occur-1' to revert an Occur mode buffer.
 See `occur-revert-function'.")
@@ -849,6 +862,59 @@
   (add-hook 'change-major-mode-hook 'font-lock-defontify nil t)
   (setq next-error-function 'occur-next-error))
 
+(defun occur-edit-mode ()
+  (interactive)
+  (setq buffer-read-only nil)
+  (use-local-map occur-edit-mode-map)
+  (setq major-mode 'occur-edit-mode
+	mode-name "Occur-Edit")
+  (add-hook 'after-change-functions 'occur-after-change-function nil t)
+  (force-mode-line-update))
+
+(defvar occur-edit-modified-buffers nil)
+(make-variable-buffer-local 'occur-edit-modified-buffers)
+
+(defun occur-edit-mode-finish ()
+  (interactive)
+  (mapc (lambda (buf)
+	  (and (buffer-file-name buf)
+	       (with-current-buffer buf
+		 (save-buffer))))
+	occur-edit-modified-buffers)
+  (setq buffer-read-only t)
+  (kill-local-variable 'occur-edit-modified-buffers)
+  (remove-hook 'after-change-functions 'occur-after-change-function t)
+  (set-buffer-modified-p nil)
+  (use-local-map occur-mode-map)
+  (setq major-mode 'occur-mode
+	mode-name "Occur"))
+
+(defun occur-after-change-function (beg end length)
+  (let* ((m (get-text-property (point) 'occur-target))
+	 (buf (marker-buffer m))
+	 (col (current-column)))
+    (when (and (markerp m) (buffer-live-p buf))
+      (let ((line (- (line-number-at-pos)
+		     (line-number-at-pos (window-start))))
+	    (readonly (with-current-buffer buf buffer-read-only))
+	    (win (or (get-buffer-window buf)
+		     (display-buffer buf t)))
+	    (text (save-excursion
+		    (forward-line 0)
+		    (search-forward ":" nil t)
+		    (setq col (- col (current-column)))
+		    (buffer-substring-no-properties (point) (line-end-position)))))
+	(and (not readonly)
+	     (add-to-list 'occur-edit-modified-buffers buf))
+	(with-selected-window win
+	  (goto-char m)
+	  (recenter line)
+	  (if readonly
+	      (message "Buffer `%s' is read only." buf)
+	    (delete-region (line-beginning-position) (line-end-position))
+	    (insert text))
+	  (move-to-column col))))))
+
 (defun occur-revert-function (ignore1 ignore2)
   "Handle `revert-buffer' for Occur mode buffers."
   (apply 'occur-1 (append occur-revert-arguments (list (buffer-name)))))
@@ -1273,6 +1339,7 @@
 				      `(font-lock-face prefix-face))
 				    `(occur-prefix t mouse-face (highlight)
 						   occur-target ,marker follow-link t
+						   read-only t
 						   help-echo "mouse-2: go to this occurrence"))))
 			   (match-str
 			    ;; We don't put `mouse-face' on the newline,
@@ -1333,13 +1400,15 @@
 		(goto-char headerpt)
 		(let ((beg (point))
 		      end)
-		  (insert (format "%d match%s%s in buffer: %s\n"
-				  matches (if (= matches 1) "" "es")
-				  ;; Don't display regexp for multi-buffer.
-				  (if (> (length buffers) 1)
-				      "" (format " for \"%s\""
-						 (query-replace-descr regexp)))
-				  (buffer-name buf)))
+		  (insert (propertize
+			   (format "%d match%s%s in buffer: %s\n"
+				   matches (if (= matches 1) "" "es")
+				   ;; Don't display regexp for multi-buffer.
+				   (if (> (length buffers) 1)
+				       "" (format " for \"%s\""
+						  (query-replace-descr regexp)))
+				   (buffer-name buf))
+			   'read-only t))
 		  (setq end (point))
 		  (add-text-properties beg end
 				       (append


             reply	other threads:[~2011-04-10  8:14 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-04-10  8:14 Leo [this message]
2011-04-15  1:11 ` bug#8463: 24.0.50; [PATCH] Direct Edit in *Occur* Buffer Stefan Monnier
2011-05-28 23:05 ` Chong Yidong
2011-05-29  4:04   ` Leo
2011-05-30 14:04 ` Andrew W. Nosenko
2011-05-30 23:10   ` Richard Stallman
2011-06-01 23:03   ` Glenn Morris
2011-06-03  2:36     ` Leo
2011-06-03 15:38       ` Stefan Monnier
2011-06-04 21:34         ` Chong Yidong
2011-06-05  9:30           ` Štěpán Němec
2011-06-06 15:34           ` Stefan Monnier
2011-06-09  4:47     ` Leo
2011-06-09  5:14       ` Glenn Morris
2011-06-09  9:44         ` Leo
2011-06-09  9:54           ` Eli Zaretskii
2011-06-09  9:58             ` Leo
2011-06-09  9:42     ` Leo
2011-06-09 18:09       ` Glenn Morris
2011-06-10 13:59         ` Stefan Monnier
2011-06-10 16:14           ` Ted Zlatanov
2011-06-11  9:58           ` Andrew W. Nosenko
2011-06-11 18:00             ` Juri Linkov
2011-06-12 23:10               ` Andrew W. Nosenko
2011-06-18 19:35             ` Chong Yidong
2011-06-18 20:36               ` Andrew W. Nosenko
2011-09-09 11:39 ` Juri Linkov
2011-09-14 19:04   ` Juri Linkov
2011-09-17 21:28   ` Chong Yidong
2011-09-18 19:33     ` Juri Linkov
2011-09-19 18:52       ` Chong Yidong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m1lizi5xqa.fsf@th041153.ip.tsinghua.edu.cn \
    --to=sdl.web@gmail.com \
    --cc=8463@debbugs.gnu.org \
    --cc=dan.colascione@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.