unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* [PATCH] lisp/files.el and lisp/buf-menu.el
@ 2009-07-16 20:47 Thomas Lord
  2009-07-17  3:08 ` Stefan Monnier
  0 siblings, 1 reply; 6+ messages in thread
From: Thomas Lord @ 2009-07-16 20:47 UTC (permalink / raw)
  To: emacs-devel

As earlier explained, I have the case of creating
a buffer with no visited file, yet I would like 
to use (normal-mode) to set the mode AS IF the 
name of the visited file was a particular string.

It was also pointed out to me that I should make
sure "list-buffers-noselect" puts something helpful
where a file name would usually go.

I have accordingly patched lisp/files.el and lisp/buf-menu.el.

First, I'll just walk through the simple patches.
Then I'll include the patches at the end without 
the narrative.

To lisp/files.el:

$ diff -c files.el.~1.1052.~ files.el
*** files.el.~1.1052.~	2009-06-22 00:02:08.000000000 -0700
--- files.el	2009-07-16 13:21:21.000000000 -0700
***************
*** 2428,2433 ****
--- 2428,2437 ----
    "Upper limit on `magic-mode-alist' regexp matches.
  Also applies to `magic-fallback-mode-alist'.")
  
+ (defvar buffer-automode-file-name ()
+   "If not nil, use this rather than `buffer-file-name' for automode.")
+ (make-variable-buffer-local 'buffer-automode-file-name)
+ 
  (defun set-auto-mode (&optional keep-mode-if-same)
    "Select major mode appropriate for current buffer.



If that variable is dynamically bound, it overrides
BUFFER-FILE-NAME in SET-AUTO-MODE.

A typical use is:

    (let ((automode-file-name my-name-for-mode-purposes))
      (normal-mode))


And here is how that works in SET-AUTO-MODE:

  
***************
*** 2515,2523 ****
  	  (set-auto-mode-0 done keep-mode-if-same)))
      ;; Next compare the filename against the entries in
auto-mode-alist.
      (unless done
!       (if buffer-file-name
! 	  (let ((name buffer-file-name)
! 		(remote-id (file-remote-p buffer-file-name)))
  	    ;; Remove remote file name identification.
  	    (when (and (stringp remote-id)
  		       (string-match (regexp-quote remote-id) name))
--- 2519,2527 ----
  	  (set-auto-mode-0 done keep-mode-if-same)))
      ;; Next compare the filename against the entries in
auto-mode-alist.
      (unless done
!       (if (or buffer-automode-file-name buffer-file-name)
! 	  (let ((name (or buffer-automode-file-name buffer-file-name))
! 		(remote-id (and (not buffer-automode-file-name) (file-remote-p
buffer-file-name))))
  	    ;; Remove remote file name identification.
  	    (when (and (stringp remote-id)
  		       (string-match (regexp-quote remote-id) name))




Next, I would like to customize the string that
appears in the "file column" of a buffer menu:


$ diff -c buff-menu.el.~1.125.~ buff-menu.el
*** buff-menu.el.~1.125.~	2009-01-15 08:46:09.000000000 -0800
--- buff-menu.el	2009-07-16 13:29:51.000000000 -0700
***************
*** 755,760 ****
--- 755,764 ----
  	      'mouse-face 'highlight
  	      'keymap Buffer-menu-sort-button-map))
  
+ (defvar list-buffers-description ()
+   "If not nil, use in place of a file name in a buffer menu.")
+ (make-variable-buffer-local 'list-buffers-description)
+ 
  (defun list-buffers-noselect (&optional files-only buffer-list)
    "Create and return a buffer with a list of names of existing
buffers.
  The buffer is named `*Buffer List*'.



That new variable works EXACTLY LIKE the existing
variable LIST-BUFFERS-DIRECTORY except that the new
variable has a more general name, has a doc string,
and takes precedence over LIST-BUFFERS-DIRECTORY


It works trivially (in LIST-BUFFERS-NOSELECT):


***************
*** 842,848 ****
  		  ;; No visited file.  Check local value of
  		  ;; list-buffers-directory and, for Info buffers,
  		  ;; Info-current-file.
! 		  (cond ((and (boundp 'list-buffers-directory)
  			      list-buffers-directory)
  			 (setq file list-buffers-directory))
  			((eq major-mode 'Info-mode)
--- 846,855 ----
  		  ;; No visited file.  Check local value of
  		  ;; list-buffers-directory and, for Info buffers,
  		  ;; Info-current-file.
! 		  (cond ((and (boundp 'list-buffers-description)
!                               list-buffers-description)
!                          (setq file list-buffers-description))
!                         ((and (boundp 'list-buffers-directory)
  			      list-buffers-directory)
  			 (setq file list-buffers-directory))
  			((eq major-mode 'Info-mode)




-t

The patches without interruption:


$ diff -c files.el.~1.1052.~ files.el
*** files.el.~1.1052.~	2009-06-22 00:02:08.000000000 -0700
--- files.el	2009-07-16 13:21:21.000000000 -0700
***************
*** 2428,2433 ****
--- 2428,2437 ----
    "Upper limit on `magic-mode-alist' regexp matches.
  Also applies to `magic-fallback-mode-alist'.")
  
+ (defvar buffer-automode-file-name ()
+   "If not nil, use this rather than `buffer-file-name' for automode.")
+ (make-variable-buffer-local 'buffer-automode-file-name)
+ 
  (defun set-auto-mode (&optional keep-mode-if-same)
    "Select major mode appropriate for current buffer.
  
***************
*** 2515,2523 ****
  	  (set-auto-mode-0 done keep-mode-if-same)))
      ;; Next compare the filename against the entries in
auto-mode-alist.
      (unless done
!       (if buffer-file-name
! 	  (let ((name buffer-file-name)
! 		(remote-id (file-remote-p buffer-file-name)))
  	    ;; Remove remote file name identification.
  	    (when (and (stringp remote-id)
  		       (string-match (regexp-quote remote-id) name))
--- 2519,2527 ----
  	  (set-auto-mode-0 done keep-mode-if-same)))
      ;; Next compare the filename against the entries in
auto-mode-alist.
      (unless done
!       (if (or buffer-automode-file-name buffer-file-name)
! 	  (let ((name (or buffer-automode-file-name buffer-file-name))
! 		(remote-id (and (not buffer-automode-file-name) (file-remote-p
buffer-file-name))))
  	    ;; Remove remote file name identification.
  	    (when (and (stringp remote-id)
  		       (string-match (regexp-quote remote-id) name))





$ diff -c buff-menu.el.~1.125.~ buff-menu.el
*** buff-menu.el.~1.125.~	2009-01-15 08:46:09.000000000 -0800
--- buff-menu.el	2009-07-16 13:29:51.000000000 -0700
***************
*** 755,760 ****
--- 755,764 ----
  	      'mouse-face 'highlight
  	      'keymap Buffer-menu-sort-button-map))
  
+ (defvar list-buffers-description ()
+   "If not nil, use in place of a file name in a buffer menu.")
+ (make-variable-buffer-local 'list-buffers-description)
+ 
  (defun list-buffers-noselect (&optional files-only buffer-list)
    "Create and return a buffer with a list of names of existing
buffers.
  The buffer is named `*Buffer List*'.
***************
*** 842,848 ****
  		  ;; No visited file.  Check local value of
  		  ;; list-buffers-directory and, for Info buffers,
  		  ;; Info-current-file.
! 		  (cond ((and (boundp 'list-buffers-directory)
  			      list-buffers-directory)
  			 (setq file list-buffers-directory))
  			((eq major-mode 'Info-mode)
--- 846,855 ----
  		  ;; No visited file.  Check local value of
  		  ;; list-buffers-directory and, for Info buffers,
  		  ;; Info-current-file.
! 		  (cond ((and (boundp 'list-buffers-description)
!                               list-buffers-description)
!                          (setq file list-buffers-description))
!                         ((and (boundp 'list-buffers-directory)
  			      list-buffers-directory)
  			 (setq file list-buffers-directory))
  			((eq major-mode 'Info-mode)






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

* Re: [PATCH] lisp/files.el and lisp/buf-menu.el
  2009-07-16 20:47 [PATCH] lisp/files.el and lisp/buf-menu.el Thomas Lord
@ 2009-07-17  3:08 ` Stefan Monnier
  2009-07-17  4:56   ` Thomas Lord
  0 siblings, 1 reply; 6+ messages in thread
From: Stefan Monnier @ 2009-07-17  3:08 UTC (permalink / raw)
  To: Thomas Lord; +Cc: emacs-devel

> As earlier explained, I have the case of creating
> a buffer with no visited file, yet I would like
> to use (normal-mode) to set the mode AS IF the
> name of the visited file was a particular string.

You'll probbaly also want to reuse the coding-system auto-detection, and
maybe more.

> It was also pointed out to me that I should make
> sure "list-buffers-noselect" puts something helpful
> where a file name would usually go.

What's the difference between list-buffers-description and list-buffers-directory?

> I have accordingly patched lisp/files.el and lisp/buf-menu.el.

I'm not completely convinced that buffer-automode-file-name is
a good idea.  Why not just let-bind buffer-file-name instead?
Also, if let-binding buffer-file-name is not good enough, maybe I'd
rather see a refactoring of the code.


        Stefan




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

* Re: [PATCH] lisp/files.el and lisp/buf-menu.el
  2009-07-17  3:08 ` Stefan Monnier
@ 2009-07-17  4:56   ` Thomas Lord
  2009-07-17 15:46     ` Stefan Monnier
  0 siblings, 1 reply; 6+ messages in thread
From: Thomas Lord @ 2009-07-17  4:56 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: emacs-devel

Stefan,

I did initially solve this problem with 
(let ((buffer-file-name yaddayadda)) (normal-mode))

I wasn't happy with that because I assume
that the binding of buffer-file-name should
be one of: nil, a local file, a remote file.
I don't know what code distributed with
Emacs or what third party Emacs code would
break under other conditions.  That is 
why my patch adds buffer-automode-file-name.

LIST-BUFFERS-DESCRIPTION differs from ...-DIRECTORY
by being better named (so people won't 
assume it necessarily refers to a "directory")
and with a doc string.   I don't know
to what extent third party code assumes that
LIST-BUFFERS-DIRECTORY is, well, a directory 
and so I didn't want to overload it. 

I'm not clear on what you suggest I do about
coding systems.

-t




On Thu, 2009-07-16 at 23:08 -0400, Stefan Monnier wrote:
> > As earlier explained, I have the case of creating
> > a buffer with no visited file, yet I would like
> > to use (normal-mode) to set the mode AS IF the
> > name of the visited file was a particular string.
> 
> You'll probbaly also want to reuse the coding-system auto-detection, and
> maybe more.
> 
> > It was also pointed out to me that I should make
> > sure "list-buffers-noselect" puts something helpful
> > where a file name would usually go.
> 
> What's the difference between list-buffers-description and list-buffers-directory?
> 
> > I have accordingly patched lisp/files.el and lisp/buf-menu.el.
> 
> I'm not completely convinced that buffer-automode-file-name is
> a good idea.  Why not just let-bind buffer-file-name instead?
> Also, if let-binding buffer-file-name is not good enough, maybe I'd
> rather see a refactoring of the code.
> 
> 
>         Stefan





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

* Re: [PATCH] lisp/files.el and lisp/buf-menu.el
  2009-07-17  4:56   ` Thomas Lord
@ 2009-07-17 15:46     ` Stefan Monnier
  2009-07-17 17:56       ` Thomas Lord
  0 siblings, 1 reply; 6+ messages in thread
From: Stefan Monnier @ 2009-07-17 15:46 UTC (permalink / raw)
  To: Thomas Lord; +Cc: emacs-devel

> I did initially solve this problem with 
> (let ((buffer-file-name yaddayadda)) (normal-mode))

> I wasn't happy with that because I assume
> that the binding of buffer-file-name should
> be one of: nil, a local file, a remote file.
> I don't know what code distributed with
> Emacs or what third party Emacs code would
> break under other conditions.  That is 
> why my patch adds buffer-automode-file-name.

As shown in the code I sent a few days ago, PCL-CVS does use such
a let-binding (and uses after-find-file rather than normal-mode), so
I wouldn't worry too much about it.

> LIST-BUFFERS-DESCRIPTION differs from ...-DIRECTORY
> by being better named (so people won't 
> assume it necessarily refers to a "directory")
> and with a doc string.   I don't know
> to what extent third party code assumes that
> LIST-BUFFERS-DIRECTORY is, well, a directory 
> and so I didn't want to overload it. 

So the better thing to do would be to suggest renaming
list-buffers-directory to list-buffers-description and adding
a docstring.

> I'm not clear on what you suggest I do about
> coding systems.

I'm just pointing out you may want to reuse more of the code used to
open files, so it also obeys things like the -*- coding -*- tags as well
as file-coding-system-alist.


        Stefan




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

* Re: [PATCH] lisp/files.el and lisp/buf-menu.el
  2009-07-17 15:46     ` Stefan Monnier
@ 2009-07-17 17:56       ` Thomas Lord
  2009-07-17 22:22         ` Stefan Monnier
  0 siblings, 1 reply; 6+ messages in thread
From: Thomas Lord @ 2009-07-17 17:56 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: emacs-devel

Stefan,

Thanks.

For buff-menu.el how would you feel about a patch
that:

Declares LIST-FILES-DIRECTORY obsolete.

Declares LIST-FILES-DIRECTORY to be an alias for
LIST-FILES-DESCRIPTION.

Otherwise renames LIST-FILES-DIRECTORY in lisp/*.el lisp/*/*.el

The other one, files.el and the case of BUFFER-FILE-MODE
vs. SET-AUTO-MODE: could we discuss that a little
further?

I get that PCL-CVS gets away with a let-binding
for BUFFER-FILE-MODE but that strikes me as a kludge
and a kludge with the potential to break things.

I think that third party code should be free to
assume that if BUFFER-FILE-MODE is not nil 
then it is, as the name implies, the name of
a (potentially remote) file.  I'm thinking 
about, for example, what someone might put 
in a mode hook in their ~/.emacs

At the same time, I'm apparently not the first
person that wants a mechanism to tweak what 
SET-AUTO-MODE does by providing an override string
to use against the auto-mode alist.  So,
I'm still kind of fond of the files.el patch
although, ultimately, of course, I defer to the
maintainers.

Finally,

> > I'm not clear on what you suggest I do about
> > coding systems.

> I'm just pointing out you may want to reuse more of the code used to
> open files, so it also obeys things like the -*- coding -*- tags as well
> as file-coding-system-alist.

I'll look into it.

-t






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

* Re: [PATCH] lisp/files.el and lisp/buf-menu.el
  2009-07-17 17:56       ` Thomas Lord
@ 2009-07-17 22:22         ` Stefan Monnier
  0 siblings, 0 replies; 6+ messages in thread
From: Stefan Monnier @ 2009-07-17 22:22 UTC (permalink / raw)
  To: Thomas Lord; +Cc: emacs-devel

> For buff-menu.el how would you feel about a patch that:

> Declares LIST-FILES-DIRECTORY obsolete.

> Declares LIST-FILES-DIRECTORY to be an alias for
> LIST-FILES-DESCRIPTION.

> Otherwise renames LIST-FILES-DIRECTORY in lisp/*.el lisp/*/*.el

It might be OK.  I haven't thought about it enough recently, but I know
that list-files-directory is a poor name and the lack of doc has bugged
me when I used it in PCL-CVS, so I come from a favorable background.

I'm not yet sure whether list-files-description is really a name I like
(I'm not sure whether its name should bind it so closely to "list"ing).

> I get that PCL-CVS gets away with a let-binding
> for BUFFER-FILE-MODE but that strikes me as a kludge
> and a kludge with the potential to break things.

Emacs is made of those.

> I think that third party code should be free to assume that if
> BUFFER-FILE-MODE is not nil then it is, as the name implies, the name
> of a (potentially remote) file.

It's already not true for members of archives opened via tar-mode.el
and arc-mode.el.

> At the same time, I'm apparently not the first person that wants
> a mechanism to tweak what SET-AUTO-MODE does by providing an override
> string to use against the auto-mode alist.

I don't understand what you mean.


        Stefan




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

end of thread, other threads:[~2009-07-17 22:22 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-07-16 20:47 [PATCH] lisp/files.el and lisp/buf-menu.el Thomas Lord
2009-07-17  3:08 ` Stefan Monnier
2009-07-17  4:56   ` Thomas Lord
2009-07-17 15:46     ` Stefan Monnier
2009-07-17 17:56       ` Thomas Lord
2009-07-17 22:22         ` Stefan Monnier

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).