Re: Question about File > Open and the "Untitled" buffer

From: mitchell <mforal.n....at.gmail.com>
Date: Fri, 9 Apr 2010 18:35:34 -0700 (PDT)

Rob,

On Apr 9, 2:43 pm, Robert Gieseke <ro....at.web.de> wrote:
> On Fri, Apr 9, 2010 at 5:32 PM, mitchell <mforal.n....at.gmail.com> wrote:
> > Hi,
>
> > On Apr 9, 9:25 am, Robert <ro....at.web.de> wrote:
> >> On 9 Apr., 13:57, morchel <morc....at.hotmail.com> wrote:
>
> >> >http://groups.google.com/group/textadept/browse_thread/thread/8ee7922...
>
> >> A while ago I tried to find a solution for this by closing the
> >> Untitled buffer in sessions.lua. I wasn't happy with my solution (as
> >> there were problems with the wrong view (not the buffer) being
> >> focussed when split views where saved.) Russels request made me think
> >> about trying it within textadept.io, which should be better.
>
> >> This is around line 172 in core/file_io.lua
> >> ...
> >> textadept.events.handle('file_opened', utf8_filename)
> >> if (#textadept.buffers == 2 ) and not textadept.buffers[1].filename
> >> and not textadept.buffers[1].dirty then
> >>   view:goto_buffer(1, true)
> >>   textadept.buffers[1]:close()
> >> end
> >> for index, file in ipairs(recent_files) do
> >> ...
> >> (Sorry no diff tool installed here...)
> >> By checking for the number of open buffers this will not close an
> >> Untitled buffer if it was created by you later.
> >> Could those who are interested maybe try this out, possibly come up
> >> with a more elegant solution and check for other side effects :-). It
> >> should solve both the problem with the open file dialog as well as the
> >> session loading.
>
> > This is cute. I like it. The only problem is what if later you have an
> > error or message buffer as your only buffer? Those special kinds of
> > buffers don't have filenames. You'd have to check their buffer.type
> > (or buffer._type; I don't remember). Otherwise I think its a good
> > solution. textadept.io is definately the way to go.
>
> More cases covered version attached. It will now even restore your ta
> state if you left it with an error and a message buffer :-) I hopeI
> haven't forgotten anything.
>
> >> Is there a way to close a buffer in the background, to avoid actually
> >> switching to it? If I skip the line view:goto... I get an error.
>
> > No, the indexed buffer must be focused. Otherwise calling
> > buffer:new_line() for example causes undesired behavior from
> > Scintilla.
>
> Opening a file when there is only one 'Untitled' buffer feels a little
> bit slower due to the switching back and forth, but I think it's fast
> enough.

Thanks for the patch. I made it a bit more elegant. It's in hg (r556).
Hopefully it preserves the desired effect. I haven't noticed any
abnormalities in my brief tests.

diff -r 0ec8992e6023 -r 67dc0df692fb core/events.lua
--- a/core/events.lua Thu Apr 08 20:05:08 2010 -0400
+++ b/core/events.lua Fri Apr 09 17:26:33 2010 -0400
@@ -398,6 +398,15 @@
     handle('update_ui')
   end)

+textadept.events.add_handler('buffer_new',
+ function() -- close initial 'Untitled' buffer
+ local b = textadept.buffers[1]
+ if #textadept.buffers == 2 and not (b.filename or b._type or
b.dirty) then
+ view:goto_buffer(1, true)
+ buffer:close()
+ end
+ end)
+
 add_handler('quit',
   function() -- prompts for confirmation if any buffers are dirty
     local any = false

Mitchell

>
> Robert
>
>  scintilla_buffer.patch
> 1KViewDownload
Received on Fri 09 Apr 2010 - 21:35:34 EDT

This archive was generated by hypermail 2.2.0 : Thu 08 Mar 2012 - 11:42:56 EST