unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* buffer-substring-filter
@ 2005-03-14 12:55 Chong Yidong
  2005-03-14 23:44 ` buffer-substring-filter Richard Stallman
  0 siblings, 1 reply; 5+ messages in thread
From: Chong Yidong @ 2005-03-14 12:55 UTC (permalink / raw)


This patch implements the kill filter following RMS' suggestions, as a
variant of buffer-substring. Is it OK?

*** emacs/lisp/simple.el~	Mon Mar 14 20:50:53 2005
--- emacs/lisp/simple.el	Mon Mar 14 20:53:37 2005
***************
*** 2215,2220 ****
--- 2215,2251 ----
    (reset-this-command-lengths)
    (restore-overriding-map))
  \f
+ (defvar buffer-substring-filters nil
+   "List of filter functions for `filter-buffer-substring'.
+ Each function must accept a single argument, a string, and return
+ a string.  The buffer substring is passed to the first function
+ in the list, and the return value of each function is passed to
+ the next.  The return value of the last function is used as the
+ return value of `filter-buffer-substring'.
+ 
+ If this variable is nil, no filtering is performed.")
+ 
+ (defun filter-buffer-substring (beg end &optional delete)
+   "Return the buffer substring between BEG and END, after filtering.
+ The buffer substring is passed through each of the filter
+ functions in `buffer-substring-filters', and the value from the
+ last filter function is returned.  If `buffer-substring-filters'
+ is nil, the buffer substring is returned unaltered.
+ 
+ If DELETE is non-nil, the text between BEG and END is deleted
+ from the buffer.
+ 
+ This function should be used instead of `buffer-substring' or
+ `delete-and-extract-region' when you want to allow filtering to
+ take place.  For example, major or minor modes can use
+ `buffer-substring-filters' to extract characters that are special
+ to a buffer, and should not be copied into other buffers."
+   (let ((string (if delete (delete-and-extract-region beg end)
+                   (buffer-substring beg end))))
+     (mapcar (lambda (filter) (setq string (funcall filter string)))
+             buffer-substring-filters)
+     string))
+ 
  ;;;; Window system cut and paste hooks.
  
  (defvar interprogram-cut-function nil
***************
*** 2391,2397 ****
  text.  See `insert-for-yank'."
    (interactive "r")
    (condition-case nil
!       (let ((string (delete-and-extract-region beg end)))
  	(when string			;STRING is nil if BEG = END
  	  ;; Add that string to the kill ring, one way or another.
  	  (if (eq last-command 'kill-region)
--- 2422,2428 ----
  text.  See `insert-for-yank'."
    (interactive "r")
    (condition-case nil
!       (let ((string (filter-buffer-substring beg end t)))
  	(when string			;STRING is nil if BEG = END
  	  ;; Add that string to the kill ring, one way or another.
  	  (if (eq last-command 'kill-region)
***************
*** 2427,2434 ****
  system cut and paste."
    (interactive "r")
    (if (eq last-command 'kill-region)
!       (kill-append (buffer-substring beg end) (< end beg))
!     (kill-new (buffer-substring beg end)))
    (if transient-mark-mode
        (setq deactivate-mark t))
    nil)
--- 2458,2465 ----
  system cut and paste."
    (interactive "r")
    (if (eq last-command 'kill-region)
!       (kill-append (filter-buffer-substring beg end) (< end beg))
!     (kill-new (filter-buffer-substring beg end)))
    (if transient-mark-mode
        (setq deactivate-mark t))
    nil)
*** emacs/lisp/register.el~	Mon Mar 14 20:45:35 2005
--- emacs/lisp/register.el	Mon Mar 14 20:46:25 2005
***************
*** 277,283 ****
  Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
  START and END are buffer positions indicating what to copy."
    (interactive "cCopy to register: \nr\nP")
!   (set-register register (buffer-substring start end))
    (if delete-flag (delete-region start end)))
  
  (defun append-to-register (register start end &optional delete-flag)
--- 277,283 ----
  Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
  START and END are buffer positions indicating what to copy."
    (interactive "cCopy to register: \nr\nP")
!   (set-register register (filter-buffer-substring start end))
    (if delete-flag (delete-region start end)))
  
  (defun append-to-register (register start end &optional delete-flag)
***************
*** 289,295 ****
    (or (stringp (get-register register))
        (error "Register does not contain text"))
    (set-register register (concat (get-register register)
! 			    (buffer-substring start end)))
    (if delete-flag (delete-region start end)))
  
  (defun prepend-to-register (register start end &optional delete-flag)
--- 289,295 ----
    (or (stringp (get-register register))
        (error "Register does not contain text"))
    (set-register register (concat (get-register register)
! 			    (filter-buffer-substring start end)))
    (if delete-flag (delete-region start end)))
  
  (defun prepend-to-register (register start end &optional delete-flag)
***************
*** 300,306 ****
    (interactive "cPrepend to register: \nr\nP")
    (or (stringp (get-register register))
        (error "Register does not contain text"))
!   (set-register register (concat (buffer-substring start end)
  			    (get-register register)))
    (if delete-flag (delete-region start end)))
  
--- 300,306 ----
    (interactive "cPrepend to register: \nr\nP")
    (or (stringp (get-register register))
        (error "Register does not contain text"))
!   (set-register register (concat (filter-buffer-substring start end)
  			    (get-register register)))
    (if delete-flag (delete-region start end)))

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: buffer-substring-filter
  2005-03-14 12:55 buffer-substring-filter Chong Yidong
@ 2005-03-14 23:44 ` Richard Stallman
  2005-03-15  0:29   ` buffer-substring-filter Chong Yidong
  0 siblings, 1 reply; 5+ messages in thread
From: Richard Stallman @ 2005-03-14 23:44 UTC (permalink / raw)
  Cc: emacs-devel

This is a clean implementation of the feature I had in mind.
Using mapcar is a little wasteful, since it makes a list of the results
only to throw them all away.  Better to use dolist.

It would be good to move point to BEG, temporarily,
so that the functions can tell where the text came from
in case they care.

However, the real question is whether this is the right feature
for doing the job.  Have you changed longlines.el to use it?
Does it do what longlines needs?

I think that some other functions would need to use this feature.
One is append-to-buffer.  Also the rectangle functions should use it.
Maybe other places too.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: buffer-substring-filter
  2005-03-14 23:44 ` buffer-substring-filter Richard Stallman
@ 2005-03-15  0:29   ` Chong Yidong
  2005-03-16 16:22     ` buffer-substring-filter Richard Stallman
  0 siblings, 1 reply; 5+ messages in thread
From: Chong Yidong @ 2005-03-15  0:29 UTC (permalink / raw)
  Cc: emacs-devel

> Using mapcar is a little wasteful, since it makes a list of the results
> only to throw them all away.  Better to use dolist.
>
> It would be good to move point to BEG, temporarily,
> so that the functions can tell where the text came from
> in case they care.

Yes, true.

> However, the real question is whether this is the right feature
> for doing the job.  Have you changed longlines.el to use it?
> Does it do what longlines needs?

Yes, I have changed longlines.el to use it, and I have confirmed that it
does what I need.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: buffer-substring-filter
  2005-03-15  0:29   ` buffer-substring-filter Chong Yidong
@ 2005-03-16 16:22     ` Richard Stallman
  2005-03-16 22:49       ` buffer-substring-filter Chong Yidong
  0 siblings, 1 reply; 5+ messages in thread
From: Richard Stallman @ 2005-03-16 16:22 UTC (permalink / raw)
  Cc: emacs-devel

    > Using mapcar is a little wasteful, since it makes a list of the results
    > only to throw them all away.  Better to use dolist.
    >
    > It would be good to move point to BEG, temporarily,
    > so that the functions can tell where the text came from
    > in case they care.

    Yes, true.

    > However, the real question is whether this is the right feature
    > for doing the job.  Have you changed longlines.el to use it?
    > Does it do what longlines needs?

    Yes, I have changed longlines.el to use it, and I have confirmed that it
    does what I need.

In that case, could you make those two little changes, and then
would someone please install it?

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: buffer-substring-filter
  2005-03-16 16:22     ` buffer-substring-filter Richard Stallman
@ 2005-03-16 22:49       ` Chong Yidong
  0 siblings, 0 replies; 5+ messages in thread
From: Chong Yidong @ 2005-03-16 22:49 UTC (permalink / raw)
  Cc: emacs-devel

> In that case, could you make those two little changes, and then
> would someone please install it?

Here is the revised patch.


*** emacs/lisp/simple.el~	Thu Mar 17 06:46:12 2005
--- emacs/lisp/simple.el	Thu Mar 17 06:52:25 2005
***************
*** 2215,2220 ****
--- 2215,2256 ----
    (reset-this-command-lengths)
    (restore-overriding-map))
  \f
+ (defvar buffer-substring-filters nil
+   "List of filter functions for `filter-buffer-substring'.
+ Each function must accept a single argument, a string, and return
+ a string.  The buffer substring is passed to the first function
+ in the list, and the return value of each function is passed to
+ the next.  The return value of the last function is used as the
+ return value of `filter-buffer-substring'.
+
+ If this variable is nil, no filtering is performed.")
+
+ (defun filter-buffer-substring (beg end &optional delete)
+   "Return the buffer substring between BEG and END, after filtering.
+ The buffer substring is passed through each of the filter
+ functions in `buffer-substring-filters', and the value from the
+ last filter function is returned.  If `buffer-substring-filters'
+ is nil, the buffer substring is returned unaltered.
+
+ If DELETE is non-nil, the text between BEG and END is deleted
+ from the buffer.
+
+ Point is temporarily set to BEG before caling
+ `buffer-substring-filters', in case the functions need to know
+ where the text came from.
+
+ This function should be used instead of `buffer-substring' or
+ `delete-and-extract-region' when you want to allow filtering to
+ take place.  For example, major or minor modes can use
+ `buffer-substring-filters' to extract characters that are special
+ to a buffer, and should not be copied into other buffers."
+   (save-excursion
+     (goto-char beg)
+     (let ((string (if delete (delete-and-extract-region beg end)
+                     (buffer-substring beg end))))
+       (dolist (filter buffer-substring-filters string)
+         (setq string (funcall filter string))))))
+
  ;;;; Window system cut and paste hooks.

  (defvar interprogram-cut-function nil
***************
*** 2391,2397 ****
  text.  See `insert-for-yank'."
    (interactive "r")
    (condition-case nil
!       (let ((string (delete-and-extract-region beg end)))
  	(when string			;STRING is nil if BEG = END
  	  ;; Add that string to the kill ring, one way or another.
  	  (if (eq last-command 'kill-region)
--- 2427,2433 ----
  text.  See `insert-for-yank'."
    (interactive "r")
    (condition-case nil
!       (let ((string (filter-buffer-substring beg end t)))
  	(when string			;STRING is nil if BEG = END
  	  ;; Add that string to the kill ring, one way or another.
  	  (if (eq last-command 'kill-region)
***************
*** 2427,2434 ****
  system cut and paste."
    (interactive "r")
    (if (eq last-command 'kill-region)
!       (kill-append (buffer-substring beg end) (< end beg))
!     (kill-new (buffer-substring beg end)))
    (if transient-mark-mode
        (setq deactivate-mark t))
    nil)
--- 2463,2470 ----
  system cut and paste."
    (interactive "r")
    (if (eq last-command 'kill-region)
!       (kill-append (filter-buffer-substring beg end) (< end beg))
!     (kill-new (filter-buffer-substring beg end)))
    (if transient-mark-mode
        (setq deactivate-mark t))
    nil)

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2005-03-16 22:49 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-03-14 12:55 buffer-substring-filter Chong Yidong
2005-03-14 23:44 ` buffer-substring-filter Richard Stallman
2005-03-15  0:29   ` buffer-substring-filter Chong Yidong
2005-03-16 16:22     ` buffer-substring-filter Richard Stallman
2005-03-16 22:49       ` buffer-substring-filter Chong Yidong

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).