Full Code of knu/elscreen for AI

master cc58337faf5b cached
16 files
166.0 KB
40.6k tokens
1 requests
Download .txt
Repository: knu/elscreen
Branch: master
Commit: cc58337faf5b
Files: 16
Total size: 166.0 KB

Directory structure:
gitextract_x2mkpamo/

├── COPYING
├── GF-QuickStart
├── GF-README
├── Readme.md
├── elscreen-buffer-list.el
├── elscreen-color-theme.el
├── elscreen-dired.el
├── elscreen-dnd.el
├── elscreen-gf.el
├── elscreen-goby.el
├── elscreen-howm.el
├── elscreen-server.el
├── elscreen-speedbar.el
├── elscreen-w3m.el
├── elscreen-wl.el
└── elscreen.el

================================================
FILE CONTENTS
================================================

================================================
FILE: COPYING
================================================
                    GNU GENERAL PUBLIC LICENSE
                       Version 2, June 1991

 Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 Everyone is permitted to copy and distribute verbatim copies
 of this license document, but changing it is not allowed.

                            Preamble

  The licenses for most software are designed to take away your
freedom to share and change it.  By contrast, the GNU General Public
License is intended to guarantee your freedom to share and change free
software--to make sure the software is free for all its users.  This
General Public License applies to most of the Free Software
Foundation's software and to any other program whose authors commit to
using it.  (Some other Free Software Foundation software is covered by
the GNU Lesser General Public License instead.)  You can apply it to
your programs, too.

  When we speak of free software, we are referring to freedom, not
price.  Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
this service if you wish), that you receive source code or can get it
if you want it, that you can change the software or use pieces of it
in new free programs; and that you know you can do these things.

  To protect your rights, we need to make restrictions that forbid
anyone to deny you these rights or to ask you to surrender the rights.
These restrictions translate to certain responsibilities for you if you
distribute copies of the software, or if you modify it.

  For example, if you distribute copies of such a program, whether
gratis or for a fee, you must give the recipients all the rights that
you have.  You must make sure that they, too, receive or can get the
source code.  And you must show them these terms so they know their
rights.

  We protect your rights with two steps: (1) copyright the software, and
(2) offer you this license which gives you legal permission to copy,
distribute and/or modify the software.

  Also, for each author's protection and ours, we want to make certain
that everyone understands that there is no warranty for this free
software.  If the software is modified by someone else and passed on, we
want its recipients to know that what they have is not the original, so
that any problems introduced by others will not reflect on the original
authors' reputations.

  Finally, any free program is threatened constantly by software
patents.  We wish to avoid the danger that redistributors of a free
program will individually obtain patent licenses, in effect making the
program proprietary.  To prevent this, we have made it clear that any
patent must be licensed for everyone's free use or not licensed at all.

  The precise terms and conditions for copying, distribution and
modification follow.

                    GNU GENERAL PUBLIC LICENSE
   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

  0. This License applies to any program or other work which contains
a notice placed by the copyright holder saying it may be distributed
under the terms of this General Public License.  The "Program", below,
refers to any such program or work, and a "work based on the Program"
means either the Program or any derivative work under copyright law:
that is to say, a work containing the Program or a portion of it,
either verbatim or with modifications and/or translated into another
language.  (Hereinafter, translation is included without limitation in
the term "modification".)  Each licensee is addressed as "you".

Activities other than copying, distribution and modification are not
covered by this License; they are outside its scope.  The act of
running the Program is not restricted, and the output from the Program
is covered only if its contents constitute a work based on the
Program (independent of having been made by running the Program).
Whether that is true depends on what the Program does.

  1. You may copy and distribute verbatim copies of the Program's
source code as you receive it, in any medium, provided that you
conspicuously and appropriately publish on each copy an appropriate
copyright notice and disclaimer of warranty; keep intact all the
notices that refer to this License and to the absence of any warranty;
and give any other recipients of the Program a copy of this License
along with the Program.

You may charge a fee for the physical act of transferring a copy, and
you may at your option offer warranty protection in exchange for a fee.

  2. You may modify your copy or copies of the Program or any portion
of it, thus forming a work based on the Program, and copy and
distribute such modifications or work under the terms of Section 1
above, provided that you also meet all of these conditions:

    a) You must cause the modified files to carry prominent notices
    stating that you changed the files and the date of any change.

    b) You must cause any work that you distribute or publish, that in
    whole or in part contains or is derived from the Program or any
    part thereof, to be licensed as a whole at no charge to all third
    parties under the terms of this License.

    c) If the modified program normally reads commands interactively
    when run, you must cause it, when started running for such
    interactive use in the most ordinary way, to print or display an
    announcement including an appropriate copyright notice and a
    notice that there is no warranty (or else, saying that you provide
    a warranty) and that users may redistribute the program under
    these conditions, and telling the user how to view a copy of this
    License.  (Exception: if the Program itself is interactive but
    does not normally print such an announcement, your work based on
    the Program is not required to print an announcement.)

These requirements apply to the modified work as a whole.  If
identifiable sections of that work are not derived from the Program,
and can be reasonably considered independent and separate works in
themselves, then this License, and its terms, do not apply to those
sections when you distribute them as separate works.  But when you
distribute the same sections as part of a whole which is a work based
on the Program, the distribution of the whole must be on the terms of
this License, whose permissions for other licensees extend to the
entire whole, and thus to each and every part regardless of who wrote it.

Thus, it is not the intent of this section to claim rights or contest
your rights to work written entirely by you; rather, the intent is to
exercise the right to control the distribution of derivative or
collective works based on the Program.

In addition, mere aggregation of another work not based on the Program
with the Program (or with a work based on the Program) on a volume of
a storage or distribution medium does not bring the other work under
the scope of this License.

  3. You may copy and distribute the Program (or a work based on it,
under Section 2) in object code or executable form under the terms of
Sections 1 and 2 above provided that you also do one of the following:

    a) Accompany it with the complete corresponding machine-readable
    source code, which must be distributed under the terms of Sections
    1 and 2 above on a medium customarily used for software interchange; or,

    b) Accompany it with a written offer, valid for at least three
    years, to give any third party, for a charge no more than your
    cost of physically performing source distribution, a complete
    machine-readable copy of the corresponding source code, to be
    distributed under the terms of Sections 1 and 2 above on a medium
    customarily used for software interchange; or,

    c) Accompany it with the information you received as to the offer
    to distribute corresponding source code.  (This alternative is
    allowed only for noncommercial distribution and only if you
    received the program in object code or executable form with such
    an offer, in accord with Subsection b above.)

The source code for a work means the preferred form of the work for
making modifications to it.  For an executable work, complete source
code means all the source code for all modules it contains, plus any
associated interface definition files, plus the scripts used to
control compilation and installation of the executable.  However, as a
special exception, the source code distributed need not include
anything that is normally distributed (in either source or binary
form) with the major components (compiler, kernel, and so on) of the
operating system on which the executable runs, unless that component
itself accompanies the executable.

If distribution of executable or object code is made by offering
access to copy from a designated place, then offering equivalent
access to copy the source code from the same place counts as
distribution of the source code, even though third parties are not
compelled to copy the source along with the object code.

  4. You may not copy, modify, sublicense, or distribute the Program
except as expressly provided under this License.  Any attempt
otherwise to copy, modify, sublicense or distribute the Program is
void, and will automatically terminate your rights under this License.
However, parties who have received copies, or rights, from you under
this License will not have their licenses terminated so long as such
parties remain in full compliance.

  5. You are not required to accept this License, since you have not
signed it.  However, nothing else grants you permission to modify or
distribute the Program or its derivative works.  These actions are
prohibited by law if you do not accept this License.  Therefore, by
modifying or distributing the Program (or any work based on the
Program), you indicate your acceptance of this License to do so, and
all its terms and conditions for copying, distributing or modifying
the Program or works based on it.

  6. Each time you redistribute the Program (or any work based on the
Program), the recipient automatically receives a license from the
original licensor to copy, distribute or modify the Program subject to
these terms and conditions.  You may not impose any further
restrictions on the recipients' exercise of the rights granted herein.
You are not responsible for enforcing compliance by third parties to
this License.

  7. If, as a consequence of a court judgment or allegation of patent
infringement or for any other reason (not limited to patent issues),
conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License.  If you cannot
distribute so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you
may not distribute the Program at all.  For example, if a patent
license would not permit royalty-free redistribution of the Program by
all those who receive copies directly or indirectly through you, then
the only way you could satisfy both it and this License would be to
refrain entirely from distribution of the Program.

If any portion of this section is held invalid or unenforceable under
any particular circumstance, the balance of the section is intended to
apply and the section as a whole is intended to apply in other
circumstances.

It is not the purpose of this section to induce you to infringe any
patents or other property right claims or to contest validity of any
such claims; this section has the sole purpose of protecting the
integrity of the free software distribution system, which is
implemented by public license practices.  Many people have made
generous contributions to the wide range of software distributed
through that system in reliance on consistent application of that
system; it is up to the author/donor to decide if he or she is willing
to distribute software through any other system and a licensee cannot
impose that choice.

This section is intended to make thoroughly clear what is believed to
be a consequence of the rest of this License.

  8. If the distribution and/or use of the Program is restricted in
certain countries either by patents or by copyrighted interfaces, the
original copyright holder who places the Program under this License
may add an explicit geographical distribution limitation excluding
those countries, so that distribution is permitted only in or among
countries not thus excluded.  In such case, this License incorporates
the limitation as if written in the body of this License.

  9. The Free Software Foundation may publish revised and/or new versions
of the General Public License from time to time.  Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.

Each version is given a distinguishing version number.  If the Program
specifies a version number of this License which applies to it and "any
later version", you have the option of following the terms and conditions
either of that version or of any later version published by the Free
Software Foundation.  If the Program does not specify a version number of
this License, you may choose any version ever published by the Free Software
Foundation.

  10. If you wish to incorporate parts of the Program into other free
programs whose distribution conditions are different, write to the author
to ask for permission.  For software which is copyrighted by the Free
Software Foundation, write to the Free Software Foundation; we sometimes
make exceptions for this.  Our decision will be guided by the two goals
of preserving the free status of all derivatives of our free software and
of promoting the sharing and reuse of software generally.

                            NO WARRANTY

  11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
REPAIR OR CORRECTION.

  12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.

                     END OF TERMS AND CONDITIONS

            How to Apply These Terms to Your New Programs

  If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.

  To do so, attach the following notices to the program.  It is safest
to attach them to the start of each source file to most effectively
convey the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.

    <one line to give the program's name and a brief idea of what it does.>
    Copyright (C) <year>  <name of author>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

Also add information on how to contact you by electronic and paper mail.

If the program is interactive, make it output a short notice like this
when it starts in an interactive mode:

    Gnomovision version 69, Copyright (C) year name of author
    Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
    This is free software, and you are welcome to redistribute it
    under certain conditions; type `show c' for details.

The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License.  Of course, the commands you use may
be called something other than `show w' and `show c'; they could even be
mouse-clicks or menu items--whatever suits your program.

You should also get your employer (if you work as a programmer) or your
school, if any, to sign a "copyright disclaimer" for the program, if
necessary.  Here is a sample; alter the names:

  Yoyodyne, Inc., hereby disclaims all copyright interest in the program
  `Gnomovision' (which makes passes at compilers) written by James Hacker.

  <signature of Ty Coon>, 1 April 1989
  Ty Coon, President of Vice

This General Public License does not permit incorporating your program into
proprietary programs.  If your program is a subroutine library, you may
consider it more useful to permit linking proprietary applications with the
library.  If this is what you want to do, use the GNU Lesser General
Public License instead of this License.


================================================
FILE: GF-QuickStart
================================================
ElScreen-GF 1.5.3 Quick Start Guide

 1) Install ElScreen, ElScreen-GF.
 2) Make sure that the grep on your system is GNU-compatible.
 3) Visit a source code you want to read.
 4) Put the cursor on the keyword you want to search.
 5) Type `C-z C-g G', and `RET'.
 6) Select line you want to jump to.
 7) Type `O'.

ElScreen-GF also supports GNU ID Utils, cscope and GNU GLOBAL as an
external program to search keywords.  For more detail, please refer
README file in this tarball.


================================================
FILE: GF-README
================================================
ElScreen-GF 1.5.3 README file

This is the distribution of ElScreen-GF 1.5.3, released November 2007.
It is an Emacs interface to GNU grep (or its compatibles), GNU ID
Utils, cscope, and GNU GLOBAL.  ElScreen-GF makes it easier to search
keyword or token in source codes, list the results, and jump to them.
This will help you to read or hack source codes, especially in a big
source tree.

It works with ElScreen 1.4.0 or later, on GNU Emacs 21 or later and
XEmacs 21.4 or later.  Enjoy!


External Programs
-----------------
ElScreen-GF supports the followings as external program:

  - GNU grep (http://www.gnu.org/software/grep/grep.html)

    grep is famous utility to print lines matching the specified
    pattern.  GNU grep has several extended features to traditional
    grep.  Nowadays major state-of-the-art OS may have GNU grep or its
    compatible one.

  - GNU ID Utils (http://www.gnu.org/software/idutils/idutils.html)

    This is language-independent (but currently supports C, C++,
    Assembly, text.  Perl also seems to be supported in the CVS HEAD)
    identifier database tool, which stores tokens, literal numbers, or
    words of human-readable text.

  - cscope (http://cscope.sourceforge.net/)

    cscope is an interactive tool that allows you to browse through C
    source files for specified elements of code.  This provides you
    the curses-based screen-oriented interface, but ElScreen-GF uses
    only line-oriented interface.  In addition, ElScreen-GF uses `sort'
    to sort its output.

  - GNU GLOBAL (http://www.gnu.org/software/global/)

    GNU GLOBAL is an yet another source code tag system.  You can
    locate a symbol in the source files.  In addition, ElScreen-GF
    uses `sort' to sort its output.


Files
-----
This package should contain the following files:

    elscreen-gf.el  ElScreen-GF 1.5.3 main file.
    README          Introduction to ElScreen-GF 1.5.3 (this file)
    QuickStart      Quick Start Guide


Installation
------------
Make sure that you have installed ElScreen.  Put elscreen-gf.el in
this directory to your load-path, and put following line just next to
ElScreen entry:

 (load "elscreen-gf" "ElScreen-GF" t)

As mentioned above, you also have to install one of supported external
programs.  Please refer their own manuals for more detail.


Usage
-----
The following sequences may valid on ElScreen:

    C-z             ElScreen prefix key
    C-z C-g         ElScreen-GF prefix key
    C-z C-g G       Run grep
    C-z C-g g       Run gid (GNU ID Utils)
    C-z C-g m       Run mkid (GNU ID Utils)
    C-z C-g c       Run cscope
    C-z C-g l       Run global (GNU GLOBAL)
    C-z C-g t       Run gtags (GNU GLOBAL)
    C-z C-g u       Go back to the point where the previous search
                    was invoked.
    C-z C-g v       Show ElScreen-GF version.

Although a token around the cursor is shown as default value for grep,
gid, cscope and global, you can specify other keyword.  Regexp is also
accepted.  Once you run grep, gid, cscope or global, ElScreen-GF
buffer is created and becomes active.  In this buffer, you may use
following keys:

    n               Move cursor to the next entry, vertically down.
    p               Move cursor to the previous entry, vertically up.
    SPC             Scroll entries upward full screen.
    DEL             Scroll entries downward full screen.
    <               Move cursor to the first entry.
    >               Move cursor to the last entry.
    N               Move cursor to the first entry of the next source
                    file.
    P               Move cursor to the first entry of the previous
                    source file.
    t               Toggle truncated lines.
    o               Jump to the file and line that the selected entry
                    specifies.
    O               Likewise, except the buffer is set to read-only.
    C-g             Delete running grep, gid, cscope or global process.
    q               Exit from ElScreen-GF mode.  This deletes
                    ElScreen-GF buffer and its window (or screen when
                    there is only one window).
    v               Show ElScreen-GF version.


Setup
-----
You can set the following variables to configure ElScreen-GF.  These
can be set in .emacs (or .emacs.d/init.el) file directly or "Options"
in your menu bar.

    elscreen-gf-grep-program-name
    elscreen-gf-idutils-gid-program-name
    elscreen-gf-idutils-mkid-program-name
    elscreen-gf-cscope-program-name
    elscreen-gf-global-program-name
    elscreen-gf-global-gtags-program-name

        Name of external programs.

    elscreen-gf-mode-truncate-lines

        If non nil, each line of result in the ElScreen-GF mode is
        shown in truncated manner by default.  The default value is
        `t'.

    elscreen-gf-invoke-point-history-length

        Maximum number of invoke-point stored by ElScreen-GF.
        invoke-point is the location where gf searches were invoked.

    elscreen-gf-mode-selected-entry-face

        Face used for the selected entry in ElScreen-GF mode.

    elscreen-gf-mode-file-name-face

        Face used for file name in ElScreen-GF mode.

    elscreen-gf-mode-line-number-face

        Face used for line number in ElScreen-GF mode.

    elscreen-gf-mode-pattern-face

        Face used to emphasize the specified keyword in ElScreen-GF
        mode.

    elscreen-gf-emphasis-after-jump-face

        Face used to emphasize the selected line after jump.


Automatic Database Update
-------------------------
You can update `ID' GNU ID Utils database or `GTAGS' GNU GLOBAL
database after saveing buffer with using

  elscreen-gf-idutils-mkid-setup-after-save-hook
  elscreen-gf-global-gtags-setup-after-save-hook

for GNU ID Utils and GNU GLOBAL respectively.  For instance, you can
add following lines to update `ID' database when saving C source code:

  (add-hook 'c-mode-hook 'elscreen-gf-idutils-mkid-setup-after-save-hook)


Where Can I Get ElScreen-GF?
----------------------------
ElScreen-GF (and ElScreen) is available from the following anonymous
ftp site.

       ftp://ftp.morishima.net/pub/morishima.net/naoto/ElScreen/


Bug Reports
-----------
ElScreen-GF is maintained by Naoto Morishima.  Please mail bug
reports and any comments to:

       naoto@morishima.net


================================================
FILE: Readme.md
================================================
ElScreen
==========


This is a fork of
[ElScreen](http://www.morishima.net/~naoto/elscreen-en/?lang=en)
updated for Emacs 24 and `package.el`.  ElScreen is an Emacs utility
with which you can have multiple screens (window-configuration) on
your GNU Emacs as well as GNU screen on terminal.

If you use emacs-lisp applications which have many windows (like
Gnus, irchat, Wanderlust, Mew...), ElScreen makes it easy to
switch to a different screen, with its configuration unchanged.
You can also create and kill screen, jump to them, rename the
screen, and so on.

This version of ElScreen does not require any external dependencies.


Installation
------------


The preferred way to install ElScreen is through
[MELPA](https://melpa.org/) and `package.el`.  If you have Emacs 24,
you should already have `package.el`.  To enable MELPA, add something
like the following in your `.emacs.d` file:

    (add-to-list 'package-archives
         '("melpa" . "https://melpa.org/packages/") t)

Once you have installed ElScreen, you can activate it like so:

    (elscreen-start)


Usage
-----
You may use following sequences on ElScreen:

`C-z c`<br />
`C-z C-c`<br />
Create a new screen and switch to it.

`C-z C`<br />
Create a new screen with the window-configuration of
the current screen.
           
`C-z k`<br />
`C-z C-k`<br />
Kill current screen.

`C-z M-k`<br />
Kill current screen and buffers.

`C-z K`<br />
Kill other screens.

`C-z n`<br />
`C-z C-n`<br />
Switch to the "next" screen in a cyclic order.

`C-z p`<br />
`C-z C-p`<br />
Switch to the "previous" screen in a cyclic order.

`C-z a`<br />
`C-z C-a`<br />
Toggle to the screen selected previously.

`C-z '`<br />
Prompt for a screen number to switch to.

`C-z "`<br />
Present a list of all screens for selection.

`C-z 0..9`<br />
Jump to the screen number 0-9.

`C-z C-s`<br />
Swap current screen with previous one.
  
`C-z w`<br />
`C-z C-w`<br />
Show a list of screen.
  
`C-z A`<br />
Allow the user to enter a name for the current screen.

`C-z m`<br />
`C-z C-m`<br />
Repeat the last message displayed in the mini-buffer.
  
`C-z t`<br />
`C-z C-t`<br />
    
`C-z b`<br />
Switch to the screen in which specified buffer is
splayed.
  
`C-z C-f`<br />
Create new screen and open file.
  
`C-z C-r`<br />
Create new screen and open file but don't allow changes.
  
`C-z d`<br />
Create new screen and run dired.

`C-z M-x`<br />
Read function name, then call it with new screen.

`C-z i`<br />
Show/hide the screen number in the mode line.

`C-z T`<br />
Show/hide the tab on the top of each frame.

`C-z v`<br />
Display ElScreen version.

`C-z ?`<br />
Show key bindings of ElScreen and Add-On softwares.
  


Setup
-----
You can set the following variables to configure ElScreen.  These
can be set in `.emacs` file directly or "Options" in your menu bar.

    elscreen-prefix-key

ElScreen prefix-key.  The default value is `\C-z`.

    elscreen-buffer-to-nickname-alist

The pairs of buffer-name and corresponding screen nickname or function
that returns nickname, which are listed by
`elscreen-display-screen-name-list` only when major-mode cannot
determine its screen nickname.  The default value is:

        '(("^dired-mode$" .
           (lambda ()
             (format "Dired(%s)" dired-directory)))
          ("^Info-mode$" .
           (lambda ()
             (format "Info(%s)" (file-name-nondirectory Info-current-file))))
          ("^mew-draft-mode$" .
           (lambda ()
             (format "Mew(%s)" (buffer-name (current-buffer)))))
          ("^mew-" . "Mew")
          ("^irchat-" . "IRChat")
          ("^liece-" . "Liece")
          ("^lookup-" . "Lookup"))

    elscreen-mode-to-nickname-alist

The pairs of major-mode and corresponding screen nickname or function
that returns nickname, which are listed by
`elscreen-display-screen-name-list`.  The default value is:

        '(("[Ss]hell" . "shell")
          ("compilation" . "compile")
          ("-telnet" . "telnet")
          ("dict" . "OnlineDict")
          ("*WL:Message*" . "Wanderlust"))

    elscreen-startup-command-line-processing

If non `nil`, ElScreen processes command line arguments of Emacsen when
starting up, and opens files with new screens if needed.  The default
value is `t`.

    elscreen-display-screen-number

If non `nil`, show the number of the current screen in mode line.  The
default value is `t`.

    elscreen-display-tab

Specify how the tabs at the top of frame should be displayed.  `t` means
to display tabs whose width should be calculated automatically.  A
value of integer means to display tabs with fixed width of this value.
`nil` means don't display tabs.  The default value is `t`.

    elscreen-tab-display-control

If non `nil`, display the tab (labeled with `[<->]`) to switch to
next/previous screen or create new screen at the most left side of the
tab line.  The default value is `t`.

    elscreen-tab-display-kill-screen

Location of the icon (`[X]`) to kill corresponding screen on each tab.
Possible values are `'left`, `'right` and `nil` (to hide icons).  The
default value is `'left`.


Bugs
----
Under multiple-frame environment, screen numbers displayed on mode
line of each frame is changed at the same time.  On GNU Emacs 21, tabs
also has this restriction.


Acknowledgment
--------------
Many people contributed to ElScreen by reporting problems or suggesting
various improvements.  Here is a list of these people.

  * Tohru Sugayama
  * Yoshinobu Takenaga
  * Masatoshi Takamura
  * Jin Kashimura
  * Takahiko Sakai
  * Norio Suzuki
  * Yoshitatsu Takeshita
  * Yoichi Nakayama
  * sen_ml@eccosys.com
  * Dan Debertin
  * Yoshinori Koseki
  * Hideyuki Shirai
  * Masahiro Ishiyama
  * Alexy Khrabrov


================================================
FILE: elscreen-buffer-list.el
================================================
;;;

;; This code is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published
;; by the Free Software Foundation; either version 3, or (at your
;; option) any later version.
;;
;; This code is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with Emacs. If not, see http://www.gnu.org/licenses.

(provide 'elscreen-buffer-list)
(require 'elscreen)

;; From escreen.el
(defun buffer-dead-p (buffer)
  (not (static-if (fboundp 'buffer-live-p)
           (buffer-live-p buffer)
         ;; Emacs 18 doesn't have buffer-live-p.
         ;; Killed buffers have no names.
         (buffer-name buffer))))

;; From escreen.el
(defun reorder-buffer-list (new-list)
  "Set buffers in NEW-LIST to be the most recently used, in order."
  (when new-list
    (let (firstbuf buf)
      (while new-list
        (setq buf (car new-list))
        (when (stringp buf)
          (setq buf (get-buffer buf)))
        (unless (buffer-dead-p buf)
          (bury-buffer buf)
          (unless firstbuf
            (setq firstbuf buf)))
        (setq new-list (cdr new-list)))
      (setq new-list (buffer-list))
      (while (not (eq (car new-list) firstbuf))
        (bury-buffer (car new-list))
        (setq new-list (cdr new-list))))))


(defun elscreen-get-buffer-list (screen)
  "Return buffer-list of SCREEN."
  (let ((screen-property (elscreen-get-screen-property screen)))
    (get-alist 'buffer-list screen-property)))

(defun elscreen-set-buffer-list (screen buflist)
  "Set buffer-list of SCREEN to BUFLIST."
  (let ((screen-property (elscreen-get-screen-property screen)))
    (set-alist 'screen-property 'buffer-list buflist)
    (elscreen-set-screen-property screen screen-property)))

(defun elscreen-save-buffer-list (&optional screen)
  "Save the buffer-list order for SCREEN, or current screen"
  (elscreen-set-buffer-list (or screen
                                (elscreen-get-current-screen))
                            (buffer-list)))

(defun elscreen-load-buffer-list (&optional screen)
  "Set emacs' buffer-list order to that of SCREEN, or current screen"
  (reorder-buffer-list (elscreen-get-buffer-list
                        (or screen
                            (elscreen-get-current-screen)))))

(defadvice elscreen-goto-internal (around manage-buffer-list activate)
  "Manage screen-specific buffer lists."
  (when (and elscreen-buffer-list-enabled
             (elscreen-screen-live-p (elscreen-get-previous-screen)))
    (elscreen-save-buffer-list (elscreen-get-previous-screen)))
  ad-do-it
  (when elscreen-buffer-list-enabled
    (elscreen-load-buffer-list (elscreen-get-current-screen))))

(defcustom elscreen-buffer-list-enabled nil
  "Whether to save and load screen-local buffer lists."
  :type 'boolean
  :group 'elscreen)

(defun toggle-elscreen-buffer-list (&optional arg)
  (interactive "P")
  (setq elscreen-buffer-list-enabled
        (cond ((null arg) (not elscreen-buffer-list-enabled))
              (t (> arg 0))))
  (message "Screen-specific buffer lists %s"
	   (if elscreen-buffer-list-enabled "enabled" "disabled")))

(fset 'enable-elscreen-buffer-list
      (apply-partially 'toggle-elscreen-buffer-list 1))
(fset 'disable-elscreen-buffer-list
      (apply-partially 'toggle-elscreen-buffer-list 0))


================================================
FILE: elscreen-color-theme.el
================================================
;; -*- Mode: Emacs-Lisp -*-
;;
;; elscreen-color-theme.el
;;
(defconst elscreen-color-theme-version "0.0.0 (November 19, 2007)")
;;
;; Author:   Naoto Morishima <naoto@morishima.net>
;; Created:  November 19, 2007

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING.  If not, write to
;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

(provide 'elscreen-color-theme)
(require 'elscreen)

(defcustom elscreen-color-theme-override-theme nil
  "Non-nil to override theme's faces"
  :type 'boolean
  :group 'color-theme)

(defcustom elscreen-color-theme-tab-background-face-function
  'elscreen-color-theme-tab-background-face-default-function
  "Function to generate a face for background of the tabs of ElScreen."
  :type 'function
  :group 'color-theme)

(defcustom elscreen-color-theme-tab-control-face-function
  'elscreen-color-theme-tab-control-face-default-function
  "Function to generate a face for the control tab of ElScreen."
  :type 'function
  :group 'color-theme)

(defcustom elscreen-color-theme-tab-current-screen-face-function
  'elscreen-color-theme-tab-current-screen-face-default-function
  "Function to generate a face for the current tab of ElScreen."
  :type 'function
  :group 'color-theme)

(defcustom elscreen-color-theme-tab-other-screen-face-function
  'elscreen-color-theme-tab-other-screen-face-default-function
  "Function to generate a face for inactive tabs of ElScreen."
  :type 'function
  :group 'color-theme)

(defsubst elscreen-color-theme-generate-color (color weight)
  (let* ((max-value (car (color-values "white")))
         (dividing-value (round (/ max-value 2)))
         (unit-value (round (/ dividing-value 16))))
    (apply 'format "#%02x%02x%02x"
           (mapcar
            (lambda (value)
              (let* ((sign (if (< dividing-value value) -1 1))
                     (adjustment (* sign unit-value weight)))
                (+ value adjustment)))
            (color-values color)))))

(defun elscreen-color-theme-tab-background-face-default-function (theme)
  (let* ((params (color-theme-frame-params theme))
         (background (cdr (assoc 'background-color params)))
         (faces
          (when background
            `(:background
              ,(elscreen-color-theme-generate-color background 8)))))
    (when faces `((t ,faces)))))

(defun elscreen-color-theme-tab-control-face-default-function (theme)
  (let* ((params (color-theme-frame-params theme))
         (foreground (cdr (assoc 'foreground-color params)))
         (background (cdr (assoc 'background-color params)))
         (faces (nconc
                 (when foreground `(:foreground ,foreground))
                 (when background `(:background ,background)))))
    (when faces `((t ,faces)))))

(defalias 'elscreen-color-theme-tab-current-screen-face-default-function
  'elscreen-color-theme-tab-control-face-default-function)

(defun elscreen-color-theme-tab-other-screen-face-default-function (theme)
  (let* ((params (color-theme-frame-params theme))
         (foreground (cdr (assoc 'foreground-color params)))
         (background (cdr (assoc 'background-color params)))
         (faces (nconc
                 (when foreground
                   `(:foreground
                     ,(elscreen-color-theme-generate-color foreground 12)))
                 (when background
                   `(:background
                     ,(elscreen-color-theme-generate-color background 4))))))
    (when faces `((t ,faces)))))

(defadvice color-theme-install (around elscreen-color-theme-install activate)
  (let* ((theme-faces (color-theme-faces (color-theme-canonic theme)))
         (elscreen-faces
          (delete nil
                  (mapcar
                   (lambda (face-name)
                     (unless (and (not elscreen-color-theme-override-theme)
                                  (assoc face-name theme-faces))
                       (let* ((face-fn
                               (symbol-value
                                (intern
                                 (concat (replace-regexp-in-string
                                          "^elscreen"
                                          "elscreen-color-theme"
                                          (symbol-name face-name))
                                         "-function"))))
                              (faces (funcall face-fn theme)))
                         (when faces (list face-name faces)))))
                   '(elscreen-tab-background-face
                     elscreen-tab-control-face
                     elscreen-tab-current-screen-face
                     elscreen-tab-other-screen-face)))))
    ad-do-it
    (when elscreen-faces
      (color-theme-install-faces elscreen-faces))))


================================================
FILE: elscreen-dired.el
================================================
;; -*- Mode: Emacs-Lisp -*-
;;
;; elscreen-dired.el 
;;
(defconst elscreen-dired-version "0.1.0 (November 6, 2005)")
;;
;; Author:   Naoto Morishima <naoto@morishima.net>
;; Created:  August 9, 2004
;; Revised:  November 6, 2005

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING.  If not, write to
;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

(provide 'elscreen-dired)
(require 'elscreen)


;;; Code:

(defadvice dired-find-file-other-window (around elscreen-dired-find-file-other-window activate)
  (let ((window-configuration (current-window-configuration))
	(buffer nil))
    ad-do-it
    (unless (eq major-mode 'dired-mode)
      (setq buffer (current-buffer))
      (set-window-configuration window-configuration)
      (elscreen-find-and-goto-by-buffer buffer t))))


================================================
FILE: elscreen-dnd.el
================================================
;; -*- Mode: Emacs-Lisp -*-
;;
;; elscreen-dnd.el
;;
(defconst elscreen-dnd-version "0.0.0 (December 15, 2005)")
;;
;; Author:   Hideyuki Shirai <shirai@meadowy.org>
;;           Naoto Morishima <naoto@morishima.net>
;; Created:  December 15, 2005

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING.  If not, write to
;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

(provide 'elscreen-dnd)
(require 'elscreen)

(defcustom elscreen-dnd-open-file-new-screen t
  "If non-nil, always create new screen to open dropped files."
  :type 'boolean
  :group 'dnd)


;; Code:

(defmacro elscreen-dnd-drag-n-drop (ad-do-it)
  (` (progn
       (elscreen-notify-screen-modification-suppress
	(, ad-do-it))
       (elscreen-notify-screen-modification 'force))))

(defadvice dnd-handle-one-url (around elscreen-dnd-handle-open-url activate)
  (if (not elscreen-dnd-open-file-new-screen)
      ad-do-it
    (let ((dnd-open-file-other-window nil)
	  file-buffer)
      (save-window-excursion
	ad-do-it
	(setq file-buffer (current-buffer)))
      (if (elscreen-screen-modified-p 'dnd-handle-one-url)
	  (elscreen-find-and-goto-by-buffer file-buffer 'create)
	(elscreen-find-screen-by-buffer file-buffer 'create)))))

(mapc
 (lambda (drag-n-drop-function)
   (eval (` (defadvice (, drag-n-drop-function) (around elscreen-dnd-drag-n-drop activate)
	      (elscreen-dnd-drag-n-drop ad-do-it)))))
 (list 'x-dnd-handle-drag-n-drop-event 'mac-drag-n-drop 'w32-drag-n-drop))


================================================
FILE: elscreen-gf.el
================================================
;; -*- Mode: Emacs-Lisp -*-
;;
;; elscreen-gf.el
;;
(defconst elscreen-gf-version "1.5.3 (November 07, 2007)")
;;
;; Author:   Naoto Morishima <naoto@morishima.net>
;; Based on: grep-family.el
;;              by Youki Kadobayashi <youki-k@is.aist-nara.ac.jp>
;; Created:  June 23, 1996
;; Revised:  November 07, 2007

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING.  If not, write to
;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

(provide 'elscreen-gf)
(require 'elscreen)
(require 'ring)

;;; User Customizable Variables:

(defgroup elscreen-gf nil
  "ElScreen-GF -- Grep Family on ElScreen"
  :tag "ElScreen-GF"
  :group 'tools)

(defcustom elscreen-gf-grep-program-name "grep"
  "Program invoked by \\[elscreen-gf-grep] command."
  :type 'string
  :group 'elscreen-gf)

(defcustom elscreen-gf-idutils-gid-program-name "gid"
  "Program invoked by \\[elscreen-gf-idutils-gid] command."
  :type 'string
  :group 'elscreen-gf)

(defcustom elscreen-gf-idutils-mkid-program-name "mkid"
  "Program invoked by \\[elscreen-gf-idutils-mkid] command."
  :type 'string
  :group 'elscreen-gf)

(defcustom elscreen-gf-cscope-program-name "cscope"
  "Program invoked by \\[elscreen-gf-cscope] command."
  :type 'string
  :group 'elscreen-gf)

(defcustom elscreen-gf-global-program-name "global"
  "Program invoked by \\[elscreen-gf-global] command."
  :type 'string
  :group 'elscreen-gf)

(defcustom elscreen-gf-global-gtags-program-name "gtags"
  "Program invoked by \\[elscreen-gf-global-gtags] command."
  :type 'string
  :group 'elscreen-gf)

(defcustom elscreen-gf-mode-truncate-lines t
  "If non-nil, truncate each line in ElScreen-GF mode."
  :tag "Truncate lines in ElScreen-GF mode."
  :type 'boolean
  :group 'elscreen-gf)

(defvar elscreen-gf-invoke-point-history)

(defcustom elscreen-gf-invoke-point-history-length 8
  "Length of histories for the locations where gf searchs were invoked."
  :tag "Length of histries for invoke-point."
  :type '(integer :size 4)
  :set (lambda (symbol value)
         (when (and (numberp value)
                    (>= value 4)
                    (<= value 64))
           (custom-set-default symbol value)
           (let ((old-history (when (boundp 'elscreen-gf-invoke-point-history)
                                elscreen-gf-invoke-point-history)))
             (setq elscreen-gf-invoke-point-history (make-ring value))
             (when (and old-history (ring-p old-history))
               (while (and (not (ring-empty-p old-history))
                           (< (ring-length elscreen-gf-invoke-point-history)
                              value))
                 (ring-insert-at-beginning elscreen-gf-invoke-point-history
                                           (ring-remove old-history 0)))))))
  :group 'elscreen-gf)

(defface elscreen-gf-mode-selected-entry-face
  '((t (:underline t)))
  "Face used for the selected entry in ElScreen-GF mode."
  :group 'elscreen-gf)

(defface elscreen-gf-mode-file-name-face
  '((((class color) (background dark))
     (:foreground "khaki" :bold t))
    (((class color) (background light))
     (:foreground "DarkSlateBlue" :bold t))
    (t (:bold t)))
  "Face used for file name in ElScreen-GF mode."
  :group 'elscreen-gf)

(defface elscreen-gf-mode-line-number-face
  '((((class color) (background dark))
     (:foreground "gray" :bold t))
    (((class color) (background light))
     (:foreground "gray50" :bold t))
    (t (:bold t)))
  "Face used for line number in ElScreen-GF mode."
  :group 'elscreen-gf)

(defface elscreen-gf-mode-pattern-face
  '((((class color) (background dark))
     (:foreground "tomato" :bold t))
    (((class color) (background light))
     (:foreground "tomato" :bold t))
    (t (:bold t)))
  "Face used to emphasize the specified keyword in ElScreen-GF mode."
  :group 'elscreen-gf)

(defface elscreen-gf-emphasis-after-jump-face
  '((((class color) (background light))
     (:background "LightSteelBlue1"))
    (((class color) (background dark))
     (:background "SteelBlue"))
    (t (:bold t)))
  "Face used to emphasize the selected line after jump."
  :group 'elscreen-gf)

;;; Key bindings:

(defvar elscreen-gf-map (make-sparse-keymap)
  "Keymap for elscreen-gf.")
(define-key elscreen-gf-map "G" 'elscreen-gf-grep)
(define-key elscreen-gf-map "m" 'elscreen-gf-idutils-mkid)
(define-key elscreen-gf-map "g" 'elscreen-gf-idutils-gid)
(define-key elscreen-gf-map "c" 'elscreen-gf-cscope)
(define-key elscreen-gf-map "t" 'elscreen-gf-global-gtags)
(define-key elscreen-gf-map "l" 'elscreen-gf-global)
(define-key elscreen-gf-map "u" 'elscreen-gf-go-back-to-latest-invoke-point)
(define-key elscreen-gf-map "v" 'elscreen-gf-display-version)

(define-key elscreen-map "\C-g" elscreen-gf-map)

(defvar elscreen-gf-mode-map (make-sparse-keymap)
  "keymap used in elscreen-gf mode.")
(define-key elscreen-gf-mode-map "n"    'elscreen-gf-mode-next-line)
(define-key elscreen-gf-mode-map "p"    'elscreen-gf-mode-previous-line)
(define-key elscreen-gf-mode-map " "    'elscreen-gf-mode-scroll-up)
(define-key elscreen-gf-mode-map "\177" 'elscreen-gf-mode-scroll-down)
(define-key elscreen-gf-mode-map "<"    'elscreen-gf-mode-beginning-of-buffer)
(define-key elscreen-gf-mode-map ">"    'elscreen-gf-mode-end-of-buffer)
(define-key elscreen-gf-mode-map "N"    'elscreen-gf-mode-next-file)
(define-key elscreen-gf-mode-map "P"    'elscreen-gf-mode-previous-file)
(define-key elscreen-gf-mode-map "t"    'elscreen-gf-mode-truncate-lines-toggle)
(define-key elscreen-gf-mode-map "o"    'elscreen-gf-mode-jump-to-entry)
(define-key elscreen-gf-mode-map "O"    'elscreen-gf-mode-jump-to-entry-read-only)
(define-key elscreen-gf-mode-map "\C-g" 'elscreen-gf-mode-search-quit)
(define-key elscreen-gf-mode-map "q"    'elscreen-gf-mode-exit)
(define-key elscreen-gf-mode-map "v"    'elscreen-gf-display-version)


;;; Internal Functions and Variables:

(defvar elscreen-gf-invoke-point-history
  (make-ring elscreen-gf-invoke-point-history-length))

(defsubst elscreen-gf-overlay-create (start end face)
  (let ((overlay (make-overlay start end)))
    (overlay-put overlay 'face face)
    (overlay-put overlay 'evaporate t)
    overlay))

(defsubst elscreen-gf-move-overlay-create (overlay-symbol start end face)
  (let ((overlay (condition-case nil
                     (symbol-value overlay-symbol)
                   (error nil))))
    (if (overlayp overlay)
        (move-overlay overlay start end (current-buffer))
      (set overlay-symbol (elscreen-gf-overlay-create start end face))
      (setq overlay (symbol-value overlay-symbol)))
    overlay))

(defun elscreen-gf-define-major-mode-token (major-mode token-chars)
  (let ((thing-symbol (intern (format "%s-thing" (symbol-name major-mode)))))
    (put major-mode 'elscreen-gf-major-mode-thing thing-symbol)
    (put thing-symbol 'token-chars token-chars)
    (put thing-symbol 'end-op
         `(lambda ()
            (re-search-forward (concat "\\=[" ,token-chars "]*") nil t)))
    (put thing-symbol 'beginning-op
         `(lambda ()
            (if (re-search-backward (concat "[^" ,token-chars "]") nil t)
                (forward-char)
              (goto-char (point-min)))))))

(elscreen-gf-define-major-mode-token 'c-mode "a-zA-Z0-9_")
(elscreen-gf-define-major-mode-token 'perl-mode "a-zA-Z0-9_")
(elscreen-gf-define-major-mode-token 'emacs-lisp-mode "-a-zA-Z0-9/+:<>")

(defsubst elscreen-gf-major-mode-token-chars (major-mode)
  (get (get major-mode 'elscreen-gf-major-mode-thing) 'token-chars))

(defsubst elscreen-gf-db-directory (file-name &optional directory)
  (let ((directory (expand-file-name
                    (or directory
                        (and (buffer-file-name)
                             (file-name-directory (buffer-file-name)))
                        default-directory)))
        (previous-directory nil))
    (catch 'found
      (while (and directory
                  (not (and previous-directory
                            (string= directory previous-directory))))
        (if (file-exists-p (concat directory file-name))
            (throw 'found directory))
        (setq previous-directory directory)
        (setq directory (file-name-directory
                         (directory-file-name directory)))))))


;;; ElScreen-GF mode

(defconst elscreen-gf-mode-to-nickname-alist
  '(("^elscreen-gf-mode$" . (lambda ()
                              (format "ElScreen-GF%s"
                                      (if (and (elscreen-gf-process-exclusive-p
                                                elscreen-gf-idutils-mkid-process
                                                'noerror)
                                               (elscreen-gf-process-exclusive-p
                                                elscreen-gf-global-gtags-process
                                                'noerror))
                                          "" " (!)")))))
  "*Alist composed of the pair of mode-name and corresponding screen-name.")
(elscreen-set-mode-to-nickname-alist 'elscreen-gf-mode-to-nickname-alist)

(defun elscreen-gf-goto-screen-create (target-directory)
  (let ((buffer (get-buffer-create "ElScreen-GF")))
    (elscreen-find-and-goto-by-buffer buffer 'create)
    (switch-to-buffer buffer)
    (elscreen-gf-mode target-directory)
    (let ((buffer-read-only nil))
      (erase-buffer))))

(defvar elscreen-gf-pattern)
(defvar elscreen-gf-invoke-point)
(defvar elscreen-gf-filter-odd-string)
(defvar elscreen-gf-selected-entry-overlay)
(defun elscreen-gf-mode (target-directory)
  "Major mode for jumping to the entries.

Key bindings:
\\{elscreen-gf-mode-map}"
  (setq major-mode 'elscreen-gf-mode)
  (setq mode-name "ElScreen-GF")
  (use-local-map elscreen-gf-mode-map)
  (setq buffer-read-only t)
  (setq case-fold-search nil)
  (setq truncate-lines elscreen-gf-mode-truncate-lines)
  (set (make-local-variable 'elscreen-gf-pattern) nil)
  (set (make-local-variable 'elscreen-gf-invoke-point) nil)
  (set (make-local-variable 'elscreen-gf-filter-odd-string) nil)
  (set (make-local-variable 'elscreen-gf-selected-entry-overlay) nil)
  (setq default-directory target-directory)

  (auto-fill-mode nil))

(defun elscreen-gf-mode-selected-entry-overlay ()
  (elscreen-gf-move-overlay-create
   'elscreen-gf-selected-entry-overlay
   (point-at-bol) (point-at-eol)
   'elscreen-gf-mode-selected-entry-face))

(defun elscreen-gf-mode-next-line ()
  "Move the current entry vertically down."
  (interactive)
  (let ((current-line (line-number-at-pos)))
    (cond
     ((< current-line 4)
      (goto-char (point-min))
      (forward-line 3))
     ((< current-line (line-number-at-pos (point-max)))
      (forward-line 1)))
    (elscreen-gf-mode-selected-entry-overlay)))

(defun elscreen-gf-mode-previous-line ()
  "Move the current entry vertically up."
  (interactive)
  (let ((current-line (line-number-at-pos)))
    (cond
     ((< 4 current-line)
      (forward-line -1))
     (t
      (goto-char (point-min))
      (forward-line 3)))
    (elscreen-gf-mode-selected-entry-overlay)))

(defun elscreen-gf-mode-scroll-up ()
  "Scroll entries upward full screen."
  (interactive)
  (scroll-up)
  (elscreen-gf-mode-selected-entry-overlay))

(defun elscreen-gf-mode-scroll-down ()
  "Scroll entries downward full screen."
  (interactive)
  (scroll-down)
  (elscreen-gf-mode-selected-entry-overlay))

(defun elscreen-gf-mode-beginning-of-buffer ()
  "Move the current entry to the beginning of the entries."
  (interactive)
  (goto-char (point-min))
  (forward-line 3)
  (elscreen-gf-mode-selected-entry-overlay))

(defun elscreen-gf-mode-end-of-buffer ()
  "Move the current entry to the end of the entries."
  (interactive)
  (goto-char (point-max))
  (forward-line -1)
  (elscreen-gf-mode-selected-entry-overlay))

(defun elscreen-gf-mode-next-file ()
  (interactive)
  (cond
   ((save-excursion
      (beginning-of-line)
      (looking-at "^\\([^:\n]+\\):\\([0-9]+\\):"))
    (let ((file-name (match-string 1)))
      (goto-char (point-max))
      (re-search-backward (concat "^" file-name ":"))
      (forward-line)))
   (t
    (re-search-forward "^\\([^:\n]+\\):\\([0-9]+\\):" nil t)
    (beginning-of-line)))
  (elscreen-gf-mode-selected-entry-overlay))

(defun elscreen-gf-mode-previous-file ()
  (interactive)
  (cond
   ((save-excursion
      (beginning-of-line)
      (looking-at "^\\([^:\n]+\\):\\([0-9]+\\):"))
    (let ((file-name (match-string 1)))
      (goto-char (point-min))
      (re-search-forward (concat "^" file-name ":"))
      (forward-line -1)
      (beginning-of-line)))
   (t
    (re-search-backward "^\\([^:\n]+\\):\\([0-9]+\\):" nil t)))
  (when (looking-at "^\\([^:\n]+\\):\\([0-9]+\\):")
    (let ((file-name (match-string 1)))
      (goto-char (point-min))
      (re-search-forward (concat "^" file-name ":"))
      (beginning-of-line)))
  (elscreen-gf-mode-selected-entry-overlay))

(defun elscreen-gf-mode-truncate-lines-toggle ()
  "Toggle truncated lines."
  (interactive)
  (let ((window-start (window-start))
        (line-number-in-window 0))
    (beginning-of-line)
    (save-excursion
      (while (> (point) window-start)
        (setq line-number-in-window (1+ line-number-in-window))
        (vertical-motion -1)))
    (setq truncate-lines (not truncate-lines))
    (recenter line-number-in-window)))

(defvar elscreen-gf-emphasis-after-jump-overlay nil)
(defun elscreen-gf-mode-jump-to-entry ()
  "Jump to the current entry."
  (interactive)
  (when (save-excursion
          (beginning-of-line)
          (looking-at "^\\([^:\n]+\\):\\([0-9]+\\):"))
    (when (not (eq (marker-buffer (cdr elscreen-gf-invoke-point))
                   (current-buffer)))
      (ring-insert elscreen-gf-invoke-point-history elscreen-gf-invoke-point))
    (let ((file-name (match-string 1))
          (line (string-to-number (match-string 2)))
          (pattern elscreen-gf-pattern)
          token-chars nontoken-chars)
      (isearch-update-ring pattern)
      (elscreen-find-file file-name)
      (setq token-chars
            (or (elscreen-gf-major-mode-token-chars major-mode)
                (elscreen-gf-major-mode-token-chars 'elscreen-gf-mode)
                "a-zA-Z0-9"))
      (setq nontoken-chars (format "[^%s]" token-chars))
      (goto-char (point-min))
      (forward-line (1- line))
      (let ((case-fold-search nil))
        (goto-char (or (and (re-search-forward
                             (format "\\(^\\|%s\\)\\(%s\\)\\(%s\\|$\\)"
                                     nontoken-chars pattern nontoken-chars)
                             (point-at-eol) t)
                            (match-beginning 2))
                       (and (search-forward pattern (point-at-eol) t)
                            (match-beginning 0))
                       (point-at-bol))))
      (elscreen-gf-move-overlay-create 'elscreen-gf-emphasis-after-jump-overlay
                                       (point-at-bol) (point-at-eol)
                                       'elscreen-gf-emphasis-after-jump-face))))

(defun elscreen-gf-mode-jump-to-entry-read-only ()
  (interactive)
  (elscreen-gf-mode-jump-to-entry)
  (read-only-mode 'toggle))

(defvar elscreen-gf-search-process nil)

(defun elscreen-gf-mode-search-quit (&optional force)
  (interactive)
  (when (and (not (elscreen-gf-process-exclusive-p
                   elscreen-gf-search-process 'noerror))
             (or force
                 (yes-or-no-p
                  (format "Really quit %s? "
                          (process-name elscreen-gf-search-process)))))
    (delete-process elscreen-gf-search-process)))

(defun elscreen-gf-mode-exit ()
  (interactive)
  (let* ((current-screen (elscreen-get-current-screen))
         (buffer (get-buffer "ElScreen-GF"))
         (screen (when (buffer-live-p buffer)
                   (elscreen-find-screen-by-buffer buffer))))
    (when screen
      (elscreen-goto-internal screen)
      (mapc
       (lambda (window)
         (if (one-window-p)
             (elscreen-kill screen)
           (delete-window window)))
       (get-buffer-window-list buffer)))
    (when buffer
      (kill-buffer buffer))
    (when (elscreen-screen-live-p screen)
      (elscreen-set-window-configuration
       screen (elscreen-current-window-configuration)))
    (when (elscreen-screen-live-p current-screen)
      (elscreen-goto-internal current-screen))))

;;; Fundamental functions shared among GNU grep/GNI ID Utils/cscope/GNU global

(defun elscreen-gf-process-exclusive-p (process &optional noerror)
  (let ((exclusive-p (not (and (processp process)
                               (eq (process-status process) 'run)))))
    (when (not (or exclusive-p noerror))
      (message "Sorry, %s is running now. Try again later."
               (process-name process)))
    exclusive-p))

(defsubst elscreen-gf-search-token-at-point ()
  (let* ((thing (or (get major-mode 'elscreen-gf-major-mode-thing) 'word))
         (thing-at-point (or (thing-at-point thing) "")))
    (set-text-properties 0 (length thing-at-point) nil thing-at-point)
    thing-at-point))

(defvar elscreen-gf-search-pattern-history nil)
(defsubst elscreen-gf-search-read-pattern (prompt &optional pattern-default)
  (let* ((pattern-default (or pattern-default
                              (elscreen-gf-search-token-at-point)
                              "")))
    (read-string prompt (cons pattern-default 0)
                 'elscreen-gf-search-pattern-history)))

(defun elscreen-gf-search-regexp-dot-to-token (regexp token)
  (let ((index 0)
        (token-length (length token)))
    (save-match-data
      (while (setq index (string-match "\\." regexp index))
        (if (and (/= index 0)
                 (string= (substring regexp (1- index) index) "\\"))
            (setq index (1+ index))
          (setq regexp (concat (substring regexp 0 index)
                               token
                               (substring regexp (1+ index))))
          (setq index (+ index token-length))))))
  regexp)

(defun elscreen-gf-read-selection (title prompt option-defs)
  (let ((candidate-buffer (generate-new-buffer " *ElScreen-GF-Read-Selection*"))
        candidate-window-height
        (minibuffer-map (copy-keymap minibuffer-local-map))
        window frame-last-window mini-hist)
    ;; prepare candidate buffer
    (with-current-buffer candidate-buffer
      (setq buffer-read-only t)
      (let ((buffer-read-only nil))
        (erase-buffer)
        (insert title "\n"
                (mapconcat
                 (lambda (option-def)
                   (apply 'format "  %s) %s\n" option-def))
                 option-defs nil))
        (goto-char (point-min))
        (save-excursion
          (while (not (eobp))
            (when (looking-at "^  \\([0-9a-z]\\)) .*$")
              (put-text-property
               (match-beginning 1) (match-end 1) 'face 'bold))
            (forward-line 1)))
        (setq candidate-window-height (line-number-at-pos (point-max)))
        (set-buffer-modified-p nil)))
    ;; prepare candidate window
    (save-window-excursion
      (setq frame-last-window
            (previous-window (frame-first-window)))
      (while (minibuffer-window-active-p frame-last-window)
        (setq frame-last-window (previous-window frame-last-window)))
      (while (and (not (one-window-p))
                  (or (< (window-width frame-last-window)
                         (frame-width))
                      (< (window-height frame-last-window)
                         (+ candidate-window-height window-min-height))))
        (setq window frame-last-window)
        (setq frame-last-window (previous-window window))
        (delete-window window))
      (select-window (split-window frame-last-window))
      (shrink-window (- (window-height) candidate-window-height))
      (switch-to-buffer candidate-buffer)
      ;; make keymap for minibuffer
      (suppress-keymap minibuffer-map t)
      (define-key minibuffer-map "\C-m" 'undefined)
      (define-key minibuffer-map "\C-g" 'abort-recursive-edit)
      (mapc
       (lambda (option-def)
         (define-key minibuffer-map (car option-def) 'self-insert-and-exit))
       option-defs)
      ;; read key from minibuffer
      (unwind-protect
          (assoc
           (read-from-minibuffer prompt nil minibuffer-map nil 'mini-hist)
           option-defs)
        (kill-buffer candidate-buffer)))))

(defsubst elscreen-gf-run-search-command (command-name pattern command directory line-parser)
  (put 'elscreen-gf-mode 'elscreen-gf-major-mode-thing
       (or (get major-mode 'elscreen-gf-major-mode-thing) 'word))
  (let ((invoke-point (cons (elscreen-get-current-screen) (point-marker))))
    (elscreen-gf-goto-screen-create directory)
    (setq elscreen-gf-pattern pattern)
    (setq elscreen-gf-invoke-point invoke-point))
  (let ((buffer-read-only nil))
    (insert "DIR: " (abbreviate-file-name directory) "\n"
            "CMD: " command " (reformatted by gf)\n\n"))
  (message "Running %s..." command-name)
  (setq elscreen-gf-search-process
        (start-process command-name (current-buffer)
                       "sh" "-c" command "2> /dev/null"))
  (process-put elscreen-gf-search-process
               'elscreen-gf-search-line-parser line-parser)
  (set-process-filter elscreen-gf-search-process 'elscreen-gf-search-filter)
  (set-process-sentinel elscreen-gf-search-process 'elscreen-gf-search-sentinel))

(defun elscreen-gf-search-filter (process string)
  (set-buffer (process-buffer process))
  (save-excursion
    (let* ((string (concat elscreen-gf-filter-odd-string string))
           (token-chars (or (elscreen-gf-major-mode-token-chars major-mode)
                            "a-zA-Z0-9"))
           (nontoken-chars (format "[^%s]" token-chars))
           (line-parser (process-get process 'elscreen-gf-search-line-parser))
           (buffer-read-only nil))
      (goto-char (point-max))
      (save-excursion (insert string))
      (while (and (not (eobp)) (looking-at ".*\n"))
        (beginning-of-line)
        (let* ((line (buffer-substring (point-at-bol) (point-at-eol)))
               (line-elms (funcall line-parser line))
               (file-name (nth 0 line-elms))
               (line-number (nth 1 line-elms))
               (source (nth 2 line-elms))
               start end)
          (when line-elms
            (delete-region (point-at-bol) (point-at-eol))
            (elscreen-gf-overlay-create
             (point) (progn (insert (file-relative-name file-name)) (point))
             'elscreen-gf-mode-file-name-face)
            (insert ":")
            (elscreen-gf-overlay-create
             (point) (progn (insert line-number) (point))
             'elscreen-gf-mode-line-number-face)
            (insert ":")
            (save-excursion (insert (nth 0 source)))
            (forward-char (nth 1 source))
            (setq start (point) end (+ start (nth 2 source)))
            (while (or (and (eq (point) start)
                            (save-excursion
                              (re-search-forward
                               (format "\\(%s\\)\\(%s\\|$\\)"
                                       elscreen-gf-pattern nontoken-chars)
                               end t))
                            (eq (match-beginning 1) start))
                       (re-search-forward
                        (format "%s\\(%s\\)\\(%s\\|$\\)"
                                nontoken-chars elscreen-gf-pattern
                                nontoken-chars)
                        end t))
              (elscreen-gf-overlay-create (match-beginning 1) (match-end 1)
                                          'elscreen-gf-mode-pattern-face)
              (goto-char (match-end 1)))))
        (forward-line))
      (setq elscreen-gf-filter-odd-string
            (buffer-substring (point-at-bol) (point-at-eol)))
      (delete-region (point-at-bol) (point-at-eol)))))

(defun elscreen-gf-search-sentinel (process event)
  (let ((status (string-match "finished" event)))
    (message "Running %s... done" (process-name elscreen-gf-search-process))
    (setq elscreen-gf-search-process nil)
    (set-buffer (process-buffer process))
    (elscreen-gf-mode-selected-entry-overlay)))

;;; GNU grep

(defun elscreen-gf-grep (&optional pattern file-name-re)
  "Run grep, with user-specified args, and collect output
in the ElScreen-GF buffer."
  (interactive)
  (cond
   ((not (executable-find elscreen-gf-grep-program-name))
    (error "grep not found."))
   ((elscreen-gf-process-exclusive-p elscreen-gf-search-process)
    (let* ((pattern (or pattern
                        (elscreen-gf-search-read-pattern "Run grep (pattern): ")))
           (buffer-file-name (or (buffer-file-name) ""))
           (file-name-re-default (concat
                                  (file-name-directory buffer-file-name)
                                  "*"
                                  (when (file-name-extension buffer-file-name)
                                    ".")
                                  (file-name-extension buffer-file-name)))
           (file-name-re (or file-name-re
                             (read-string
                              "Run grep (files): "
                              (cons file-name-re-default 0) nil "*")))
           (token-chars (or (elscreen-gf-major-mode-token-chars major-mode)
                            "a-zA-Z0-9"))
           (token-chars-re (format "[%s]" token-chars))
           (nontoken-chars-re (format "[^%s]" token-chars))
           (pattern-tokenize (elscreen-gf-search-regexp-dot-to-token
                              pattern token-chars-re))
           (command (format "%s -IHn %s %s"
                            elscreen-gf-grep-program-name
                            (format "'\\(^\\|%s\\)\\(%s\\)\\(%s\\|$\\)'"
                                    nontoken-chars-re
                                    pattern-tokenize
                                    nontoken-chars-re)
                            file-name-re)))
      (elscreen-gf-run-search-command
       "grep" pattern-tokenize command default-directory
       'elscreen-gf-grep-line-parser)))))

(defun elscreen-gf-grep-line-parser (line)
  (when (string-match "^\\([^:\n]+\\):\\([0-9]+\\):\\(.*\\)$" line)
    (let ((file-name (match-string 1 line))
          (line-number (match-string 2 line))
          (source (match-string 3 line)))
      (list file-name line-number (list source 0 (length source))))))

;;; GNU ID Utils

(defsubst elscreen-gf-idutils-id-directory (&optional directory)
  (elscreen-gf-db-directory "ID" directory))

(defvar elscreen-gf-idutils-mkid-process nil)

(defun elscreen-gf-idutils-mkid (&optional directory)
  "Run mkid, with user-specified args."
  (interactive)
  (cond
   ((not (executable-find elscreen-gf-idutils-mkid-program-name))
    (error "mkid not found."))
   ((elscreen-gf-process-exclusive-p elscreen-gf-idutils-mkid-process)
    (let ((default-directory
            (or (and directory (file-directory-p directory) directory)
                (read-directory-name "Run mkid (target): "
                                     (or (elscreen-gf-idutils-id-directory)
                                         default-directory) nil t)))
          (command (format "%s" elscreen-gf-idutils-mkid-program-name)))
      (message "Running mkid...")
      (setq elscreen-gf-idutils-mkid-process
            (start-process "mkid" nil "sh" "-c" command))
      (set-process-sentinel elscreen-gf-idutils-mkid-process
                            'elscreen-gf-idutils-mkid-sentinel)
      (elscreen-notify-screen-modification 'force-immediately)))))

(defun elscreen-gf-idutils-mkid-sentinel (process event)
  (let ((status (string-match "finished" event)))
    (message "Running mkid... %s" (if status "done" "error"))
    (setq elscreen-gf-idutils-mkid-process nil)
    (elscreen-notify-screen-modification 'force-immediately)))

(defun elscreen-gf-idutils-mkid-after-save ()
  (when (and (buffer-file-name)
             (elscreen-gf-process-exclusive-p
              elscreen-gf-idutils-mkid-process 'noerror))
    (let ((id-directory (elscreen-gf-idutils-id-directory)))
      (when id-directory
        (elscreen-gf-idutils-mkid id-directory)))))

(defun elscreen-gf-idutils-mkid-setup-after-save-hook ()
  (make-local-variable 'after-save-hook)
  (add-hook 'after-save-hook 'elscreen-gf-idutils-mkid-after-save))

(defun elscreen-gf-idutils-gid (&optional pattern)
  "Run gid, with user-specified args, and collect output
in the ElScreen-GF buffer."
  (interactive)
  (cond
   ((not (executable-find elscreen-gf-idutils-gid-program-name))
    (when (yes-or-no-p "gid not found; execute grep instead? ")
      (elscreen-gf-grep)))
   ((not (elscreen-gf-idutils-id-directory))
    (funcall
     (nth 2 (elscreen-gf-read-selection
             "Cannot locate `ID'; what instead?"
             "Select action: "
             '(("m" "Execute mkid to generate `ID'" elscreen-gf-idutils-mkid)
               ("g" "Execute grep" elscreen-gf-grep))))))
   ((elscreen-gf-process-exclusive-p elscreen-gf-search-process)
    (let* ((pattern (or pattern
                        (elscreen-gf-search-read-pattern "Run gid (pattern): ")))
           (token-chars (or (elscreen-gf-major-mode-token-chars major-mode)
                            "a-zA-Z0-9"))
           (token-chars-re (format "[%s]" token-chars))
           (pattern-tokenize (elscreen-gf-search-regexp-dot-to-token
                              pattern token-chars-re))
           (directory (elscreen-gf-idutils-id-directory))
           (command (format "%s --regexp '^(%s)$'"
                            elscreen-gf-idutils-gid-program-name pattern)))
      ;; XXX: gid does not raise error even if given regexp starts with '*',
      ;; so we should check it here.
      (when (string-match "^\\*" pattern)
        (error "Invalid regexp: %s" pattern))
      (elscreen-gf-run-search-command
       "gid" pattern-tokenize command directory
       'elscreen-gf-grep-line-parser)))))

;;; cscope

(defsubst elscreen-gf-cscope-cross-ref-directory (&optional directory)
  (elscreen-gf-db-directory "cscope.out" directory))

(defun elscreen-gf-cscope (&optional pattern directory)
  "Run cscope, with user-specified args, and collect output
in the ElScreen-GF buffer."
  (interactive)
  (cond
   ((not (executable-find elscreen-gf-cscope-program-name))
    (when (yes-or-no-p "cscope not found; execute grep instead? ")
      (elscreen-gf-grep)))
   ((elscreen-gf-process-exclusive-p elscreen-gf-search-process)
    (let* ((pattern (or pattern
                        (elscreen-gf-search-read-pattern
                         "Run cscope (pattern): ")))
           (directory (or (and directory (file-directory-p directory) directory)
                          (elscreen-gf-cscope-cross-ref-directory)
                          (read-directory-name "Run cscope (target): "
                                               default-directory nil t)))
           (token-chars (or (elscreen-gf-major-mode-token-chars major-mode)
                            "a-zA-Z0-9"))
           (token-chars-re (format "[%s]" token-chars))
           (pattern-tokenize (elscreen-gf-search-regexp-dot-to-token
                              pattern token-chars-re))
           (query-type-def (elscreen-gf-read-selection
                            "Available query type for cscope: "
                            "Select query type: "
                            '(("0" "Find this C symbol" 0)
                              ("1" "Find this function definition" 1)
                              ("2" "Find functions calling this function" 3))))
           (query-type (nth 2 query-type-def))
           (command (format "%s -LR -%d '%s' | sort -k 1,1 -k 3,3n"
                            elscreen-gf-cscope-program-name query-type pattern))
           (line-parser (intern
                         (format "elscreen-gf-cscope-line-parser-query-type-%s"
                                 (car query-type-def)))))
      (elscreen-gf-run-search-command
       "cscope" pattern-tokenize command directory line-parser)))))

(defsubst elscreen-gf-cscope-line-parser-string-match (line)
  (string-match "^\\([^ ]+\\) \\([^ ]+\\) \\([0-9]+\\) \\(.*\\)$" line))

(defun elscreen-gf-cscope-line-parser-common (line)
  (when (elscreen-gf-cscope-line-parser-string-match line)
    (let ((file-name (match-string 1 line))
          (line-number (match-string 3 line))
          (function-name (match-string 2 line))
          (source (match-string 4 line)))
      (list file-name line-number
            (list (format "[%s] %s" function-name source)
                  (+ (length function-name) 3) (length source))))))

(defalias 'elscreen-gf-cscope-line-parser-query-type-0
  'elscreen-gf-cscope-line-parser-common)

(defun elscreen-gf-cscope-line-parser-query-type-1 (line)
  (when (elscreen-gf-cscope-line-parser-string-match line)
    (let ((file-name (match-string 1 line))
          (line-number (match-string 3 line))
          (source (match-string 4 line)))
      (list file-name line-number (list source 0 (length source))))))

(defalias 'elscreen-gf-cscope-line-parser-query-type-2
  'elscreen-gf-cscope-line-parser-common)

;;; GNU global

(defsubst elscreen-gf-global-tags-directory (&optional directory)
  (elscreen-gf-db-directory "GTAGS" directory))

(defvar elscreen-gf-global-gtags-process nil)

(defun elscreen-gf-global-gtags (&optional directory)
  "Run gtags, with user-specified args."
  (interactive)
  (cond
   ((not (executable-find elscreen-gf-global-gtags-program-name))
    (error "gtags not found."))
   ((elscreen-gf-process-exclusive-p elscreen-gf-global-gtags-process)
    (let ((default-directory
            (or (and directory (file-directory-p directory) directory)
                (read-directory-name "Run gtags (target): "
                                     (or (elscreen-gf-global-tags-directory)
                                         default-directory) nil t)))
          (command (format "%s" elscreen-gf-global-gtags-program-name)))
      (message "Running gtags...")
      (setq elscreen-gf-global-gtags-process
            (start-process "gtags" nil "sh" "-c" command))
      (set-process-sentinel elscreen-gf-global-gtags-process
                            'elscreen-gf-global-gtags-sentinel)
      (elscreen-notify-screen-modification 'force-immediately)))))

(defun elscreen-gf-global-gtags-sentinel (process event)
  (let ((status (string-match "finished" event)))
    (message "Running gtags... %s" (if status "done" "error"))
    (setq elscreen-gf-global-gtags-process nil)
    (elscreen-notify-screen-modification 'force-immediately)))

(defun elscreen-gf-global-gtags-after-save ()
  (when (and (buffer-file-name)
             (elscreen-gf-process-exclusive-p
              elscreen-gf-global-gtags-process 'noerror))
    (let ((tags-directory (elscreen-gf-global-tags-directory)))
      (when tags-directory
        (elscreen-gf-global-gtags tags-directory)))))

(defun elscreen-gf-global-gtags-setup-after-save-hook ()
  (make-local-variable 'after-save-hook)
  (add-hook 'after-save-hook 'elscreen-gf-global-gtags-after-save))

(defvar elscreen-gf-global-5.0-or-later-p)
(defsubst elscreen-gf-global-5.0-or-later-p ()
  (unless (boundp 'elscreen-gf-global-5.0-or-later-p)
    (let (global-version)
      (with-temp-buffer
        (call-process elscreen-gf-global-program-name nil t nil "--version")
        (setq global-version (string-to-number
                              (buffer-substring (point-min) (point-max)))))
      (setq elscreen-gf-global-5.0-or-later-p (and global-version
                                                   (<= 5.0 global-version)))))
  elscreen-gf-global-5.0-or-later-p)

(defun elscreen-gf-global (&optional pattern directory)
  "Run global, with user-specified args, and collect output
in the ElScreen-GF buffer."
  (interactive)
  (cond
   ((not (executable-find elscreen-gf-global-program-name))
    (when (yes-or-no-p "global not found; execute grep instead? ")
      (elscreen-gf-grep)))
   ((not (elscreen-gf-global-tags-directory))
    (funcall
     (nth 2 (elscreen-gf-read-selection
             "Cannot locate `GTAGS'; what instead?"
             "Select action: "
             '(("t" "Execute gtags to generate `GTAGS'" elscreen-gf-global-gtags)
               ("g" "Execute grep" elscreen-gf-grep))))))
   ((elscreen-gf-process-exclusive-p elscreen-gf-search-process)
    (let* ((pattern (or pattern
                        (elscreen-gf-search-read-pattern
                         "Run global (pattern): ")))
           (directory (or (and directory (file-directory-p directory) directory)
                          (elscreen-gf-global-tags-directory)
                          (read-directory-name "Run global (target): "
                                               default-directory nil t)))
           (token-chars (or (elscreen-gf-major-mode-token-chars major-mode)
                            "a-zA-Z0-9"))
           (token-chars-re (format "[%s]" token-chars))
           (pattern-tokenize (elscreen-gf-search-regexp-dot-to-token
                              pattern token-chars-re))
           (query-type (nth 2 (elscreen-gf-read-selection
                               "Available query type for GNU GLOBAL: "
                               "Select query type: "
                               '(("0" "Find this object definition" "")
                                 ("1" "Find this object references" " -r")))))
           (command (format
                     (cond
                      ((elscreen-gf-global-5.0-or-later-p)
                       "%s%s --result=grep -e '%s' | sort -t : -k 1,1 -k 2,2n")
                      (t
                       "%s -x%s -e '%s' | sort -k 3,3 -k 2,2n"))
                     elscreen-gf-global-program-name query-type pattern))
           (line-parser (if (elscreen-gf-global-5.0-or-later-p)
                            'elscreen-gf-grep-line-parser
                          'elscreen-gf-global-line-parser)))
      (elscreen-gf-run-search-command
       "global" pattern-tokenize command directory line-parser)))))

(defun elscreen-gf-global-line-parser (line)
  (when (string-match "^[^ ]+ +\\([0-9]+\\) +\\([^ ]+\\) \\(.*\\)$" line)
    (let ((file-name (match-string 2 line))
          (line-number (match-string 1 line))
          (source (match-string 3 line)))
    (list file-name line-number (list source 0 (length source))))))

;;; Going back to the latest invoke-point.

(defun elscreen-gf-go-back-to-latest-invoke-point ()
  (interactive)
  (while (not (or (ring-empty-p elscreen-gf-invoke-point-history)
                  (let ((invoke-point
                         (ring-ref elscreen-gf-invoke-point-history 0)))
                    (buffer-live-p (marker-buffer (cdr invoke-point))))))
    (ring-remove elscreen-gf-invoke-point-history 0))
  (if (ring-empty-p elscreen-gf-invoke-point-history)
      (message "No more invoke-points to go back to.")
    (let* ((invoke-point (ring-remove elscreen-gf-invoke-point-history 0))
           (invoke-point-screen (car invoke-point))
           (invoke-point-marker (cdr invoke-point))
           (invoke-point-buffer (marker-buffer invoke-point-marker))
           (invoke-point-position (marker-position invoke-point-marker))
           (invoke-point-buffer-screen-list
            (elscreen-find-screens
             `(lambda (screen)
                (elscreen-goto-internal screen)
                (not (null (get-buffer-window ,invoke-point-buffer))))))
           (get-marker-window
            (lambda (marker)
              (catch 'found
                (save-window-excursion
                  (mapc
                   (lambda (window)
                     (select-window window)
                     (when (eq (point) (marker-position marker))
                       (throw 'found window)))
                   (get-buffer-window-list (marker-buffer marker))))
                nil))))
      (cond
       ((null invoke-point-buffer-screen-list)
        ;; No screens showing invoke-point-buffer, so create new screen
        ;; and show it.
        (elscreen-create)
        (switch-to-buffer invoke-point-buffer)
        (goto-char invoke-point-position))
       ((memq invoke-point-screen invoke-point-buffer-screen-list)
        ;; invoke-point-screen shows invoke-point-buffer, so go to the
        ;; screen and select the window showing the buffer.  If there are
        ;; two or more window showing the buffer, we prefer the window
        ;; whose buffer's point is invoke-point-position.  Anyway,
        ;; we are moving point to it.
        (elscreen-goto invoke-point-screen)
        (select-window (or (funcall get-marker-window invoke-point-marker)
                           (get-buffer-window invoke-point-buffer)))
        (goto-char invoke-point-position))
       ((catch 'found
          (mapc
           (lambda (screen)
             (let (window)
               (when (save-window-excursion
                       (elscreen-goto-internal screen)
                       (setq window (funcall get-marker-window
                                             invoke-point-marker)))
                 ;; There is (at least one) screen showing buffer whose
                 ;; point is invoke-point-position, so show it.
                 (elscreen-goto screen)
                 (select-window window)
                 (throw 'found t))))
           invoke-point-buffer-screen-list)))
       (t
        ;; invoke-point-screen does not show invoke-point-buffer, and
        ;; the others does not show invoke-point-buffer whose point is
        ;; invoke-point-position; so we're showing one of screens showing
        ;; invoke-point-buffer and moving point to invoke-point-position.
        (elscreen-goto (car invoke-point-buffer-screen-list))
        (select-window (get-buffer-window invoke-point-buffer))
        (goto-char invoke-point-position))))))

;;; Help

(defvar elscreen-gf-help "ElScreen-GF keys:
  \\[elscreen-gf-grep]    Run grep
  \\[elscreen-gf-idutils-mkid]    Run mkid (GNU ID Utils)
  \\[elscreen-gf-idutils-gid]    Run gid (GNU ID Utils)
  \\[elscreen-gf-cscope]    Run cscope
  \\[elscreen-gf-global-gtags]    Run gtags (GNU GLOBAL)
  \\[elscreen-gf-global]    Run global (GNU GLOBAL)
  \\[elscreen-gf-go-back-to-latest-invoke-point]    Go back to the latest invoke-point.
  \\[elscreen-gf-display-version]    Display ElScreen-GF version")
(elscreen-set-help 'elscreen-gf-help)

(defun elscreen-gf-display-version ()
  "Display ElScreen-GF version."
  (interactive)
  (elscreen-message (concat "ElScreen-GF version " elscreen-gf-version)))


================================================
FILE: elscreen-goby.el
================================================
;; -*- Mode: Emacs-Lisp -*-
;;
;; elscreen-goby.el 
;;
(defconst elscreen-goby-version "0.0.0 (July 14, 2005)")
;;
;; Author:   Naoto Morishima <naoto@dl.naist.jp>
;;              Nara Institute of Science and Technology, Japan
;; Created:  July 14, 2005

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING.  If not, write to
;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

(provide 'elscreen-goby)
(require 'elscreen)


;;; Code:

(defadvice goby-view-mode (before elscreen-goby-view-mode activate)
  (set (make-local-variable 'elscreen-display-tab) nil))

(defadvice goby-view-quit (before elscreen-goby-view-quit activate)
  (kill-local-variable 'elscreen-display-tab))


================================================
FILE: elscreen-howm.el
================================================
;; -*- Mode: Emacs-Lisp -*-
;;
;; elscreen-howm.el
;;
(defconst elscreen-howm-version "0.1.3 (September 14, 2008)")
;;
;; Author:   Naoto Morishima <naoto@morishima.net>
;; Created:  November 06, 2005
;; Revised:  September 14, 2008

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING.  If not, write to
;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

(provide 'elscreen-howm)
(require 'elscreen)

;;; User Customizable Variables:

(defcustom elscreen-howm-mode-to-nickname-alist
  '(("^howm-\\(menu-mode$\\|view-\\)" . "howm(menu)"))
  "*Alist composed of the pair of mode-name and corresponding screen-name."
  :type '(alist :key-type string :value-type (choice string function))
  :tag "Howm major-mode to nickname alist"
  :set (lambda (symbol value)
         (custom-set-default symbol value)
         (elscreen-rebuild-mode-to-nickname-alist))
  :group 'howm)
(elscreen-set-mode-to-nickname-alist 'elscreen-howm-mode-to-nickname-alist)

(defcustom elscreen-howm-buffer-to-nickname-alist
  '(("\\.howm$" . elscreen-howm-mode-buffer-to-nickname))
  "*Alist composed of the pair of regular expression of
buffer-name and corresponding screen-name."
  :type '(alist :key-type string :value-type (choice string function))
  :tag "Howm buffer to nickname alist"
  :set (lambda (symbol value)
         (custom-set-default symbol value)
         (elscreen-rebuild-buffer-to-nickname-alist))
  :group 'howm)
(elscreen-set-buffer-to-nickname-alist 'elscreen-howm-buffer-to-nickname-alist)


;;; Code:

;; delete other windows when howm-menu is invoked
(defadvice howm-menu (around elscreen-howm-menu activate)
  (delete-other-windows)
  ad-do-it)

;; create new page with new screen
(defadvice howm-create-file-with-title (around elscreen-howm-create-file-with-title activate)
  (save-current-buffer
    (elscreen-create))
  ad-do-it)

;; open existing page with new screen
(defadvice howm-view-open-item (around elscreen-howm-view-open-item activate)
  (let ((window-configuration (elscreen-current-window-configuration))
        howm-item-buffer)
    ad-do-it
    (setq howm-item-buffer (current-buffer))
    (elscreen-apply-window-configuration window-configuration)
    (elscreen-find-and-goto-by-buffer howm-item-buffer 'create)))

(eval-after-load "howm-view"
  '(unless (fboundp 'howm-view-open-item)
     (elscreen-message "This version of elscreen-howm does not work well with your howm.  Upgrade howm to version 1.3.3 or later, or downgrade elscreen-howm to version 0.0.1.")))

;; screen nickname
(defun elscreen-howm-mode-buffer-to-nickname ()
  (format "howm(%s%s%s)"
          elscreen-howm-title
          (if (zerop (length elscreen-howm-title)) "" ":")
          (substring (buffer-name (current-buffer)) 0 -5)))

(defun elscreen-howm-mode-update-title ()
  (let ((title (howm-title-at-current-point)))
    (when (not (string-equal elscreen-howm-title title))
      (setq elscreen-howm-title title)
      (elscreen-notify-screen-modification 'force-immediately))))

(defun elscreen-howm-mode-initialize ()
  (set (make-local-variable 'elscreen-howm-title)
       (howm-title-at-current-point))
  (add-hook 'post-command-hook 'elscreen-howm-mode-update-title t t))

(add-hook 'howm-mode-on-hook 'elscreen-howm-mode-initialize)

;; kill screen when exiting from howm-mode
(defun howm-save-and-kill-buffer/screen ()
  (interactive)
  (let* ((file-name (buffer-file-name)))
    (if (save-excursion
          (goto-char (point-min))
          (re-search-forward "[^ \t\r\n]" nil t))
        (howm-save-buffer)
      (set-buffer-modified-p nil)
      (when (file-exists-p file-name)
        (delete-file file-name)
        (message "(Deleted %s)" (file-name-nondirectory file-name))))
    (kill-buffer nil)
    (unless (elscreen-one-screen-p)
      (elscreen-kill))))

(eval-after-load "howm-mode"
  '(progn
     (define-key howm-mode-map
       "\C-c\C-c" 'howm-save-and-kill-buffer/screen)))


================================================
FILE: elscreen-server.el
================================================
;;; elscreen-server.el --- server support for elscreen
;;
(defconst elscreen-server-version "0.2.0 (November 23, 2007)")
;;
;; Author:   Hideyuki Shirai <shirai@meadowy.org>
;;           Naoto Morishima <naoto@morishima.net>
;; Created:  October 11, 2007
;; Revised:  November 23, 2007

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING.  If not, write to
;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

;;; Commentary:

;;; Code:

(require 'elscreen)
(require 'dframe)

(defcustom elscreen-server-dont-use-dedicated-frame t
  "*Non-nil to use dframe-attached-frame if frame is dedicated."
  :type 'boolean
  :group 'server)

(defun elscreen-server-visit-files-new-screen (buffer-list)
  "Create a screen for each buffer in BUFFER-LIST."
  (let* ((selected-frame (selected-frame))
         (dframe-attached-frame (dframe-attached-frame selected-frame)))
    (when (and elscreen-server-dont-use-dedicated-frame
               (framep dframe-attached-frame))
      (select-frame dframe-attached-frame))
    (let ((screen (car (mapcar
                        (lambda (buffer)
                          (elscreen-find-screen-by-buffer buffer 'create))
                        buffer-list))))
      (and screen
           (elscreen-goto screen)))
    (elscreen-notify-screen-modification 'force-immediately)
    (select-frame selected-frame)))

(eval-after-load "server"
  ;; For server.el distributed with GNU Emacs
  '(progn
     (defadvice server-visit-files (after elscreen-server-visit-files activate)
       (elscreen-server-visit-files-new-screen ad-return-value))))

(provide 'elscreen-server)
;;; elscreen-server.el ends here


================================================
FILE: elscreen-speedbar.el
================================================
;; -*- Mode: Emacs-Lisp -*-
;;
;; elscreen-speedbar.el
;;
(defconst elscreen-speedbar-version "0.0.0 (November 18, 2007)")
;;
;; Author:   Naoto Morishima <naoto@morishima.net>
;; Created:  November 18, 2007

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING.  If not, write to
;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

(provide 'elscreen-speedbar)
(require 'elscreen)

(defcustom elscreen-speedbar-find-file-in-screen t
  "Non-nil to use ElScreen to open file when the selected file is being
opened in the attached frame."
  :type 'boolean
  :group 'speedbar)

(defadvice speedbar-frame-mode (after elscreen-speedbar-frame-mode activate)
  (with-current-buffer speedbar-buffer
    (set (make-local-variable 'elscreen-display-tab) nil)))

(defadvice speedbar-find-file-in-frame (around elscreen-speedbar-find-file-in-frame activate)
  (let ((buffer (find-file-noselect file)))
    (if (or (get-buffer-window buffer 0)
            dframe-power-click
            (numberp speedbar-select-frame-method)
            (not elscreen-speedbar-find-file-in-screen))
        (let ((dframe-power-click (and (not elscreen-speedbar-find-file-in-screen)
                                       dframe-power-click)))
          ad-do-it)
      (dframe-select-attached-frame speedbar-frame)
      (elscreen-find-and-goto-by-buffer buffer 'create))
    (elscreen-notify-screen-modification 'force-immediately)))


================================================
FILE: elscreen-w3m.el
================================================
;; -*- Mode: Emacs-Lisp -*-
;;
;; elscreen-w3m.el
;;
(defconst elscreen-w3m-version "0.2.2 (July 26, 2006)")
;;
;; Author:   Naoto Morishima <naoto@morishima.net>
;; Created:  August 6, 2004
;; Revised:  July 26, 2006

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING.  If not, write to
;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

(provide 'elscreen-w3m)
(require 'elscreen)

;; Code:

(defconst elscreen-w3m-mode-to-nickname-alist
  '(("^w3m-mode" .
     (lambda (buf)
       (with-current-buffer buf
         (let ((graphic (and window-system
                             w3m-show-graphic-icons-in-header-line)))
           (concat
            (when (and graphic w3m-use-favicon w3m-favicon-image)
              (concat
               (propertize
                " "
                'display w3m-favicon-image)
               (propertize
                " "
                'display '(space :width 0.5))))
            (w3m-current-title)))))))
  "*Alist composed of the pair of mode-name and corresponding screen-name.")
(elscreen-set-mode-to-nickname-alist 'elscreen-w3m-mode-to-nickname-alist)

(defun elscreen-w3m-mailto-url-popup-function (buffer)
  (elscreen-find-and-goto-by-buffer buffer 'create))

(defadvice w3m-copy-buffer (around elscreen-w3m-copy-buffer activate)
  (let ((current-buffer (current-buffer)))
    (if (< (elscreen-get-number-of-screens) 10)
        (elscreen-create)
      (split-window)
      (other-window 1))
    (unless (ad-get-arg 0)
      (ad-set-arg 0 current-buffer))
    ad-do-it))

(defadvice w3m-favicon-retrieve (around elscreen-w3m-favicon-retrieve activate)
  ad-do-it
  (run-at-time 1 nil 'elscreen-notify-screen-modification 'force-immediately))

(defun elscreen-w3m-initialize ()
  (setq w3m-pop-up-frames nil)
  (setq w3m-pop-up-windows nil)
  (setq w3m-use-tab nil)
  (setq w3m-use-tab-menubar nil)
  (setq w3m-use-header-line nil)
  (setq w3m-mailto-url-popup-function-alist
        '((cmail-mail-mode . elscreen-w3m-mailto-url-popup-function)
          (mail-mode . elscreen-w3m-mailto-url-popup-function)
          (message-mode . elscreen-w3m-mailto-url-popup-function)
          (mew-draft-mode . elscreen-w3m-mailto-url-popup-function)
          (mh-letter-mode . elscreen-w3m-mailto-url-popup-function)
          (wl-draft-mode . elscreen-w3m-mailto-url-popup-function)))
  (elscreen-screen-modified-hook-setup
   (w3m-fontify-after-hook 'force-immediately)))

(add-hook 'w3m-load-hook 'elscreen-w3m-initialize)


================================================
FILE: elscreen-wl.el
================================================
;; -*- Mode: Emacs-Lisp -*-
;;
;; elscreen-wl.el
;;
(defconst elscreen-wl-version "0.8.0 (November 03, 2007)")
;;
;; Author:   Naoto Morishima <naoto@morishima.net>
;; Created:  March 24, 2001
;; Revised:  November 03, 2007

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING.  If not, write to
;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

(provide 'elscreen-wl)
(require 'elscreen)
(require 'wl)


;;; User Customizable Variables:

(defcustom elscreen-wl-draft-use-elscreen t
  "Non-nil to use ElScreen when Wanderlust prepares draft buffer.
This option will override the option `wl-draft-use-frame'."
  :type 'boolean
  :group 'wl-draft)

(defcustom elscreen-wl-display-biff-on-tab t
  "Non-nil to use ElScreen's tab to display biff icon instead of
mode-line."
  :type 'boolean
  :set (lambda (symbol value)
         (custom-set-default symbol value)
         (let ((buf (get-buffer wl-folder-buffer-name)))
           (when buf
             (with-current-buffer buf
               (wl-mode-line-buffer-identification))))
         (elscreen-notify-screen-modification 'force-immediately))
  :group 'wl-highlight)

(defcustom elscreen-wl-mode-to-nickname-alist
  '(("^wl-folder-mode$" . (lambda (buf)
                            (elscreen-wl-nickname-with-biff-status "Folder")))
    ("^wl-summary-mode$" . (lambda (buf)
                             (elscreen-wl-nickname-with-biff-status
                              (wl-summary-buffer-folder-name))))
    ("^wl-draft-mode$" . (lambda (buf)
                           (let ((buffer-name (buffer-name))
                                 (subject (std11-field-body "Subject")))
                             (format "WL(%s%s)"
                                     buffer-name
                                     (if (zerop (length subject))
                                         ""
                                       (concat "[" subject "]"))))))
    ("^wl-" . "Wanderlust"))
  "*Alist composed of the pair of mode-name and corresponding screen-name."
  :type '(alist :key-type string :value-type (choice string function))
  :tag "Wanderlust major-mode to screen nickname alist"
  :set (lambda (symbol value)
         (custom-set-default symbol value)
         (elscreen-rebuild-mode-to-nickname-alist))
  :group 'wl)
(elscreen-set-mode-to-nickname-alist 'elscreen-wl-mode-to-nickname-alist)


;;; Code:

(defmacro elscreen-wl-draft-create-buffer (ad-do-it)
  `(let ((wl-draft-use-frame wl-draft-use-frame)
         (wl-draft-reply-buffer-style wl-draft-reply-buffer-style))
     (when elscreen-wl-draft-use-elscreen
       (setq wl-draft-use-frame nil)
       (setq wl-draft-reply-buffer-style 'full)
       (elscreen-create))
     ,ad-do-it
     (when elscreen-wl-draft-use-elscreen
       (make-local-variable 'kill-buffer-hook)
       (add-hook 'kill-buffer-hook
                 (lambda ()
                   (when `(buffer-live-p ,(current-buffer))
                     (elscreen-kill)))))))

(defadvice wl-draft-create-buffer (around
                                   elscreen-ad-wl-draft-create-buffer
                                   activate)
  (elscreen-wl-draft-create-buffer ad-do-it))

(defadvice wl-draft-reedit (around
                            elscreen-ad-wl-draft-reedit
                            activate)
  (elscreen-wl-draft-create-buffer ad-do-it))

(defadvice wl-jump-to-draft-buffer (around
                                    elscreen-ad-wl-jump-to-draft-screen
                                    activate)
  (let ((original-buffer (current-buffer))
        draft-buffer)
    ad-do-it
    (when elscreen-wl-draft-use-elscreen
      (setq draft-buffer (current-buffer))
      (if (not (eq original-buffer draft-buffer))
          (progn
            (switch-to-buffer original-buffer)
            (elscreen-find-and-goto-by-buffer draft-buffer))))))

(defmacro elscreen-wl-compensate-summary-window (ad-do-it)
  `(let ((wl-draft-use-frame (or wl-draft-use-frame
                                 elscreen-wl-draft-use-elscreen)))
     ,ad-do-it))

(defadvice wl-draft-forward (around elscreen-ad-wl-draft-forward activate)
  (elscreen-wl-compensate-summary-window ad-do-it))

(defadvice wl-draft-reply (around elscreen-ad-wl-draft-forward activate)
  (elscreen-wl-compensate-summary-window ad-do-it))

;; Biff on ElScreen's tab

(defsubst elscreen-wl-nickname-with-biff-status (name)
  (concat
   (when (and wl-biff-check-folder-list elscreen-wl-display-biff-on-tab)
     (concat
      (if wl-modeline-biff-status
          wl-modeline-biff-state-on
        wl-modeline-biff-state-off)
      (propertize " " 'display '(space :width 0.5))))
   (format "WL(%s)" name)))

(add-hook 'wl-biff-notify-hook
          (lambda ()
            (when elscreen-wl-display-biff-on-tab
              (setq wl-modeline-biff-status t)
              (elscreen-notify-screen-modification 'force-immediately))))

(add-hook 'wl-biff-unnotify-hook
          (lambda ()
            (when elscreen-wl-display-biff-on-tab
              (setq wl-modeline-biff-status nil)
              (elscreen-notify-screen-modification 'force-immediately))))

(defadvice wl-mode-line-buffer-identification (around elscreen-wl-mode-line-buffer-identification activate)
  (let ((wl-biff-check-folder-list wl-biff-check-folder-list))
    (when elscreen-wl-display-biff-on-tab
      (setq wl-biff-check-folder-list nil))
    ad-do-it))


================================================
FILE: elscreen.el
================================================
;;; elscreen.el --- Emacs window session manager

;; Author: Naoto Morishima <naoto@morishima.net>
;; Based on: screens.el
;;              by Heikki T. Suopanki <suopanki@stekt1.oulu.fi>
;; Created:  June 22, 1996
;; Revised:  April 11, 2012 by Emanuel Evans
;; Maintainer: Akinori MUSHA <knu@iDaemons.org>
;; Homepage: https://github.com/knu/elscreen
;; Version:  20180321
;; Package-Requires: ((emacs "24"))
;; Keywords: window, convenience

;; This file is NOT part of GNU Emacs.

;;; License:

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING.  If not, write to
;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139,
;; USA.

;;; Commentary:

;; This is a fork of ElScreen designed to be installed with
;; package.el.  It does not require APEL (it also probably does not
;; work with XEmacs).  In order to start using ElScreen, add
;; (elscreen-start) to your emacs configuration.  For help on how to
;; use elscreen, try \C-z ?.

;;; Code:

;;; User Customizable Variables:

(require 'dired)

(declare-function iswitchb-read-buffer "iswitchb")

(defconst elscreen-version "20180321")

(defgroup elscreen nil
  "ElScreen -- Screen Manager for Emacs"
  :tag "ElScreen"
  :group 'environment)

(defcustom elscreen-prefix-key "\C-z"
  "*Prefix key for ElScreen commands."
  :tag "Prefix Key of ElScreen"
  :type '(string :size 10)
  :set (lambda (symbol value)
         (when (boundp 'elscreen-map)
           (elscreen-set-prefix-key value))
         (custom-set-default symbol value))
  :group 'elscreen)

(defcustom elscreen-default-buffer-name "*scratch*"
  "*Name of a buffer in new screen."
  :tag "Name of Default Buffer"
  :type '(string :size 24)
  :group 'elscreen)

(defcustom elscreen-default-buffer-initial-major-mode initial-major-mode
  "*Major mode command symbol to use for the default buffer."
  :tag "Major Mode for Default Buffer"
  :type 'function
  :group 'elscreen)

(defcustom elscreen-default-buffer-initial-message initial-scratch-message
  "*Initial message displayed in default buffer.
If this is nil, no message will be displayed."
  :tag "Message to Display in the Default Buffer"
  :type '(choice (text :tag "Message")
                 (const :tag "none" nil))
  :group 'elscreen)

(defcustom elscreen-mode-to-nickname-alist
  '(("^dired-mode$" .
     (lambda ()
       (format "Dired(%s)" dired-directory)))
    ("^Info-mode$" .
     (lambda ()
       (format "Info(%s)" (file-name-nondirectory Info-current-file))))
    ("^mew-draft-mode$" .
     (lambda ()
       (format "Mew(%s)" (buffer-name (current-buffer)))))
    ("^mew-" . "Mew")
    ("^irchat-" . "IRChat")
    ("^liece-" . "Liece")
    ("^lookup-" . "Lookup"))
  "*Alist composed of the pair of name of major-mode and corresponding screen-name."
  :tag "Alist to Derive Screen Names from Major Modes"
  :type '(alist :key-type string :value-type (choice string function))
  :set (lambda (symbol value)
         (custom-set-default symbol value)
         (when (fboundp 'elscreen-rebuild-mode-to-nickname-alist)
           (elscreen-rebuild-mode-to-nickname-alist)))
  :group 'elscreen)

(defcustom elscreen-buffer-to-nickname-alist
  '(("[Ss]hell" . "shell")
    ("compilation" . "compile")
    ("-telnet" . "telnet")
    ("dict" . "OnlineDict")
    ("*WL:Message*" . "Wanderlust"))
  "*Alist composed of the pair of regular expression of
buffer-name and corresponding screen-name."
  :tag "Alist to Derive Screen Names from Major Modes"
  :type '(alist :key-type string :value-type (choice string function))
  :set (lambda (symbol value)
         (custom-set-default symbol value)
         (when (fboundp 'elscreen-rebuild-buffer-to-nickname-alist)
           (elscreen-rebuild-buffer-to-nickname-alist)))
  :group 'elscreen)

(defcustom elscreen-display-screen-number t
  "*Non-nil to display the number of current screen in the mode line."
  :tag "Show/Hide Screen Number on the mode-line"
  :type 'boolean
  :group 'elscreen)

(defcustom elscreen-display-tab t
  "*Specify how the tabs at the top of frame should be displayed.
t means to display tabs whose width should be calculated automatically.
A value of integer means to display tabs with fixed width of this value.
nil means don't display tabs."
  :tag "Specify how the tabs at the top of frame should be displayed"
  :type '(choice (const :tag "Show (automatic width tab)" t)
                 (integer :tag "Show (fixed width tab)" :size 4 :value 16)
                 (const :tag "Hide" nil))
  :set (lambda (symbol value)
         (when (or (booleanp value)
                   (and (numberp value)
                        (> value 0)))
           (custom-set-default symbol value)
           (when (fboundp 'elscreen-tab-update)
             (elscreen-tab-update t))))
  :group 'elscreen)

(make-obsolete-variable 'elscreen-tab-display-create-screen
                        'elscreen-tab-display-control "2012-04-11")
(defcustom elscreen-tab-display-control t
  "*Non-nil to display control tab at the most left side."
  :tag "Show/Hide the Control Tab"
  :type 'boolean
  :set (lambda (symbol value)
         (custom-set-default symbol value)
         (when (fboundp 'elscreen-tab-update)
           (elscreen-tab-update t)))
  :group 'elscreen)

(defcustom elscreen-tab-display-kill-screen 'left
  "*Location of the icons to kill a screen on each tab.  Possible values are 'left, 'right, or nil (to hide them)."
  :tag "Location of Buttons to Kill Screen on Each Tab"
  :type '(choice (const :tag "Left" left)
                 (const :tag "Right" right)
                 (const :tag "None" nil))
  :set (lambda (symbol value)
         (custom-set-default symbol value)
         (when (fboundp 'elscreen-tab-update)
           (elscreen-tab-update t)))
  :group 'elscreen)

(defface elscreen-tab-background-face
  '((((type x w32 mac ns) (class color))
     :background "Gray50")
    (((class color))
     (:background "black")))
  "Face to fontify background of tab line."
  :group 'elscreen)

(defface elscreen-tab-control-face
  '((((type x w32 mac ns) (class color))
     (:background "white" :foreground "black" :underline "Gray50"))
    (((class color))
     (:background "white" :foreground "black" :underline t))
    (t (:underline t)))
  "Face for control tab."
  :group 'elscreen)

(defface elscreen-tab-current-screen-face
  '((((class color))
     (:background "white" :foreground "black"))
    (t (:underline t)))
  "Face for current screen tab."
  :group 'elscreen)

(defface elscreen-tab-other-screen-face
  '((((type x w32 mac ns) (class color))
     :background "Gray85" :foreground "Gray50")
    (((class color))
     (:background "blue" :foreground "black" :underline t)))
  "Face for tabs other than current screen one."
  :group 'elscreen)


;;; Key & Menu bindings:

(defvar elscreen-map (make-sparse-keymap)
  "Keymap for ElScreen.")
(define-key elscreen-map "\C-c" 'elscreen-create)
(define-key elscreen-map "c"    'elscreen-create)
(define-key elscreen-map "C"    'elscreen-clone)
(define-key elscreen-map "\C-k" 'elscreen-kill)
(define-key elscreen-map "k"    'elscreen-kill)
(define-key elscreen-map "\M-k" 'elscreen-kill-screen-and-buffers)
(define-key elscreen-map "K"    'elscreen-kill-others)
(define-key elscreen-map "\C-p" 'elscreen-previous)
(define-key elscreen-map "p"    'elscreen-previous)
(define-key elscreen-map "\C-n" 'elscreen-next)
(define-key elscreen-map "n"    'elscreen-next)
(define-key elscreen-map "\C-a" 'elscreen-toggle)
(define-key elscreen-map "a"    'elscreen-toggle)
(define-key elscreen-map "'"    'elscreen-goto)
(define-key elscreen-map "\""   'elscreen-select-and-goto)
(define-key elscreen-map "0"    'elscreen-jump-0)
(define-key elscreen-map "1"    'elscreen-jump)
(define-key elscreen-map "2"    'elscreen-jump)
(define-key elscreen-map "3"    'elscreen-jump)
(define-key elscreen-map "4"    'elscreen-jump)
(define-key elscreen-map "5"    'elscreen-jump)
(define-key elscreen-map "6"    'elscreen-jump)
(define-key elscreen-map "7"    'elscreen-jump)
(define-key elscreen-map "8"    'elscreen-jump)
(define-key elscreen-map "9"    'elscreen-jump-9)
(define-key elscreen-map "\C-s" 'elscreen-swap)
(define-key elscreen-map "\C-w" 'elscreen-display-screen-name-list)
(define-key elscreen-map "w"    'elscreen-display-screen-name-list)
(define-key elscreen-map "\C-m" 'elscreen-display-last-message)
(define-key elscreen-map "m"    'elscreen-display-last-message)
(define-key elscreen-map "\C-t" 'elscreen-display-time)
(define-key elscreen-map "t"    'elscreen-display-time)
(define-key elscreen-map "A"    'elscreen-screen-nickname)
(define-key elscreen-map "b"    'elscreen-find-and-goto-by-buffer)
(define-key elscreen-map "\C-f" 'elscreen-find-file)
(define-key elscreen-map "\C-r" 'elscreen-find-file-read-only)
(define-key elscreen-map "d"    'elscreen-dired)
(define-key elscreen-map "\M-x" 'elscreen-execute-extended-command)
(define-key elscreen-map "i"    'elscreen-toggle-display-screen-number)
(define-key elscreen-map "T"    'elscreen-toggle-display-tab)
(define-key elscreen-map "?"    'elscreen-help)
(define-key elscreen-map "v"    'elscreen-display-version)
(define-key elscreen-map "j"    'elscreen-link)
(define-key elscreen-map "s"    'elscreen-split)

(defun elscreen-set-prefix-key (prefix-key)
  (when (not (eq elscreen-prefix-key prefix-key))
    (when elscreen-prefix-key
      (global-set-key elscreen-prefix-key
                      (get 'elscreen-prefix-key
                           'global-map-original-definition))
      (define-key minibuffer-local-map elscreen-prefix-key
        (get 'elscreen-prefix-key 'minibuffer-local-map-original-definition)))
    (put 'elscreen-prefix-key 'global-map-original-definition
         (lookup-key global-map prefix-key))
    (put 'elscreen-prefix-key 'minibuffer-local-map-original-definition
         (lookup-key minibuffer-local-map prefix-key)))
  (global-set-key prefix-key elscreen-map)
  (define-key minibuffer-local-map prefix-key 'undefined)
  (setq elscreen-prefix-key prefix-key))

(defvar elscreen-help "ElScreen keys:
  \\[elscreen-create]    Create a new screen and switch to it
  \\[elscreen-clone]    Create a new screen with the window-configuration of current screen
  \\[elscreen-kill]    Kill current screen
  \\[elscreen-kill-screen-and-buffers]  Kill current screen and buffers
  \\[elscreen-kill-others]    Kill other screens
  \\[elscreen-next]    Switch to the \"next\" screen in a cyclic order
  \\[elscreen-previous]    Switch to the \"previous\" screen in a cyclic order
  \\[elscreen-toggle]    Toggle to the screen selected previously
  \\[elscreen-select-and-goto]    Jump to the specified screen
  \\[elscreen-jump-0]
    :      Jump to the screen #
  \\[elscreen-jump-9]
  \\[elscreen-swap]  Swap current screen with previous one
  \\[elscreen-display-screen-name-list]    Show list of screens
  \\[elscreen-screen-nickname]    Name current screen
  \\[elscreen-display-last-message]    Show last message
  \\[elscreen-display-time]    Show time
  \\[elscreen-find-and-goto-by-buffer]    Switch to the screen in which specified buffer is displayed
  \\[elscreen-find-file]  Create new screen and open file
  \\[elscreen-find-file-read-only]  Create new screen and open file but don't allow changes
  \\[elscreen-dired]    Create new screen and run dired
  \\[elscreen-execute-extended-command]  Read function name, then call it with new screen
  \\[elscreen-toggle-display-screen-number]    Show/hide the screen number in the mode line
  \\[elscreen-toggle-display-tab]    Show/hide the tab at the top of screen
  \\[elscreen-display-version]    Show ElScreen version
  \\[elscreen-help]    Show this help"
  "Help shown by elscreen-help-mode")

;;; alist utility functions (taken from APEL):

(defun elscreen--put-alist (key value alist)
  "Set cdr of an element (KEY . ...) in ALIST to VALUE and return ALIST.
If there is no such element, create a new pair (KEY . VALUE) and
return a new alist whose car is the new pair and cdr is ALIST."
  (let ((elm (assoc key alist)))
    (if elm
        (progn
          (setcdr elm value)
          alist)
      (cons (cons key value) alist))))

(defun elscreen--set-alist (symbol key value)
  "Set cdr of an element (KEY . ...) in the alist bound to SYMBOL to VALUE."
  (or (boundp symbol)
      (set symbol nil))
  (set symbol (elscreen--put-alist key value (symbol-value symbol))))

(defun elscreen--del-alist (key alist)
  "Delete an element whose car equals KEY from ALIST.
Return the modified ALIST."
  (let ((pair (assoc key alist)))
    (if pair
        (delq pair alist)
      alist)))

(defun elscreen-window-history-supported-p ()
  (and (fboundp 'window-prev-buffers)
       (fboundp 'window-next-buffers)
       (fboundp 'set-window-prev-buffers)
       (fboundp 'set-window-next-buffers)))

(defun elscreen-get-all-window-history-alist ()
  (when (elscreen-window-history-supported-p)
    (mapcar (lambda (window)
              (let ((prevs (window-prev-buffers window))
                    (nexts (window-next-buffers window)))
                (cons window (cons prevs nexts))))
            (window-list))))

(defun elscreen-restore-all-window-history-alist (history-alist)
  (when (elscreen-window-history-supported-p)
    (mapc (lambda (entry)
            (let* ((window (car entry))
                   (histories (cdr entry))
                   (prevs (car histories))
                   (nexts (cdr histories)))
              (when (window-valid-p window)
                (set-window-prev-buffers window prevs)
                (set-window-next-buffers window nexts))))
          history-alist)))

(defun elscreen--remove-alist (symbol key)
  "Delete an element whose car equals KEY from the alist bound to SYMBOL."
  (and (boundp symbol)
       (set symbol (elscreen--del-alist key (symbol-value symbol)))))

;;; Internal Functions:

(defvar elscreen-frame-confs nil
  "Alist that contains the information about screen configurations.")

(defun elscreen-current-window-configuration ()
  (list (current-window-configuration) (point-marker)))

(defun elscreen-default-window-configuration ()
  (let ((default-buffer (get-buffer elscreen-default-buffer-name)))
    (save-window-excursion
      (set-window-dedicated-p (selected-window) nil)
      (delete-other-windows)
      (if default-buffer
          (switch-to-buffer default-buffer)
        (switch-to-buffer (get-buffer-create elscreen-default-buffer-name))
        (funcall elscreen-default-buffer-initial-major-mode)
        (insert elscreen-default-buffer-initial-message)
        (set-buffer-modified-p nil))
      (elscreen-current-window-configuration))))

(defun elscreen-apply-window-configuration (elscreen-window-configuration)
  (let ((window-configuration (car elscreen-window-configuration))
        (marker (cadr elscreen-window-configuration)))
    (set-window-configuration window-configuration)
    (when (marker-buffer marker)
      (goto-char marker))))

(defsubst elscreen-copy-tree (tree)
  (if (fboundp 'copy-tree)
      (copy-tree tree)
    (elscreen-copy-tree-1 tree)))

(defun elscreen-copy-tree-1 (tree)
  (let (clone)
    (while (consp tree)
      (setq clone (cons (or (and (consp (car tree))
                                 (elscreen-copy-tree-1 (car tree)))
                            (car tree))
                        clone))
      (setq tree (cdr tree)))
    (nconc (nreverse clone) tree)))

(defmacro elscreen-save-screen-excursion (&rest body)
  "Execute BODY, preserving ElScreen meta data.
Return the value of the last form in BODY."
  `(let ((original-buffer-list (buffer-list))
         (original-buffer-live-p nil)
         (original-elscreen-window-configuration
          (elscreen-current-window-configuration))
         (original-frame-confs (elscreen-copy-tree elscreen-frame-confs))
         (original-window-histories (elscreen-get-all-window-history-alist)))
     (unwind-protect
         (save-window-excursion ,@body)
       (setq elscreen-frame-confs original-frame-confs)
       (elscreen-apply-window-configuration
        original-elscreen-window-configuration)
       (mapc
        (lambda (buffer)
          (when (buffer-live-p buffer)
            (bury-buffer buffer)
            (setq original-buffer-live-p t)))
        original-buffer-list)
       (when original-buffer-live-p
         (while (not (memq (car (buffer-list)) original-buffer-list))
           (bury-buffer (car (buffer-list)))))
       (elscreen-restore-all-window-history-alist original-window-histories))))

(defsubst elscreen-get-frame-confs (frame)
  (assoc-default frame elscreen-frame-confs))

(defun elscreen-make-frame-confs (frame &optional keep-window-configuration)
  (when (null (elscreen-get-frame-confs frame))
    (let ((selected-frame (selected-frame))
          elscreen-window-configuration)
      (save-current-buffer
        (select-frame frame)
        (setq elscreen-window-configuration
              (if keep-window-configuration
                  (elscreen-current-window-configuration)
                (elscreen-default-window-configuration)))
        (elscreen--set-alist 'elscreen-frame-confs frame
                   (list
                    (cons 'screen-property
                          (list
                           (cons 0 (list
                                    (cons 'window-configuration
                                          elscreen-window-configuration)))))
                    (cons 'screen-history (list 0))
                    (cons 'modified-inquirer nil)
                    (cons 'screen-to-name-alist-cache nil)))
        (elscreen-apply-window-configuration elscreen-window-configuration)
        (elscreen-notify-screen-modification 'force-immediately)
        (select-frame selected-frame)))))

(defun elscreen-delete-frame-confs (frame)
  (elscreen--remove-alist 'elscreen-frame-confs frame))

(add-hook 'after-make-frame-functions 'elscreen-make-frame-confs)
(add-hook 'delete-frame-functions 'elscreen-delete-frame-confs)

(defsubst elscreen-get-conf-list (type)
  (assoc-default type (elscreen-get-frame-confs (selected-frame))))

(defsubst elscreen-set-conf-list (type conf-list)
  (let ((frame-conf (elscreen-get-frame-confs (selected-frame))))
    (elscreen--set-alist 'frame-conf type conf-list)))

(defun elscreen-get-screen-property (screen)
  (let ((screen-property-list (elscreen-get-conf-list 'screen-property)))
    (assoc-default screen screen-property-list)))

(defun elscreen-set-screen-property (screen property)
  (let ((screen-property-list (elscreen-get-conf-list 'screen-property)))
    (elscreen--set-alist 'screen-property-list screen property)
    (elscreen-set-conf-list 'screen-property screen-property-list)))

(defun elscreen-delete-screen-property (screen)
  (let ((screen-property-list (elscreen-get-conf-list 'screen-property)))
    (elscreen--remove-alist 'screen-property-list screen)
    (elscreen-set-conf-list 'screen-property screen-property-list)))

(defun elscreen-get-number-of-screens ()
  "Return total number of screens."
  (length (elscreen-get-conf-list 'screen-property)))

(defun elscreen-one-screen-p ()
  "Return t if there is only one screen."
  (= (elscreen-get-number-of-screens) 1))

(defun elscreen-get-screen-list ()
  "Return a list of screen numbers."
  (mapcar 'car (elscreen-get-conf-list 'screen-property)))

(defun elscreen-screen-live-p (screen)
  "Return t when SCREEN is alive."
  (not (null (elscreen-get-screen-property screen))))

(defun elscreen-get-window-configuration (screen)
  "Return pair of window-configuration and marker of SCREEN
from `elscreen-frame-confs', a cons cell."
  (let ((screen-property (elscreen-get-screen-property screen)))
    (assoc-default 'window-configuration screen-property)))

(defun elscreen-set-window-configuration (screen winconf)
  "Set pair of window-configuration and marker of SCREEN to WINCONF."
  (let ((screen-property (elscreen-get-screen-property screen)))
    (elscreen--set-alist 'screen-property 'window-configuration winconf)
    (elscreen-set-screen-property screen screen-property)))

(defun elscreen-get-screen-nickname (screen)
  "Return nickname of SCREEN from `elscreen-frame-confs', a string."
  (let ((screen-property (elscreen-get-screen-property screen)))
    (assoc-default 'nickname screen-property)))

(defun elscreen-set-screen-nickname (screen nickname)
  "Set nickname of SCREEN to NICKNAME."
  (let ((screen-property (elscreen-get-screen-property screen)))
    (elscreen--set-alist 'screen-property 'nickname nickname)
    (elscreen-set-screen-property screen screen-property)))

(defun elscreen-delete-screen-nickname (screen)
  "Remove nickname of SCREEN from `elscreen-frame-confs'."
  (let ((screen-property (elscreen-get-screen-property screen)))
    (elscreen--remove-alist 'screen-property 'nickname)
    (elscreen-set-screen-property screen screen-property)))

(defun elscreen-append-screen-to-history (screen)
  (let ((screen-history (elscreen-get-conf-list 'screen-history)))
    (setcdr (last screen-history) (list screen))))

(defun elscreen-delete-screen-from-history (screen)
  (let ((screen-history (elscreen-get-conf-list 'screen-history)))
    (setq screen-history (delq screen screen-history))
    (elscreen-set-conf-list 'screen-history screen-history)))

(defun elscreen-set-current-screen (screen)
  (let ((screen-history (elscreen-get-conf-list 'screen-history)))
    (setq screen-history (cons screen (delq screen screen-history)))
    (elscreen-set-conf-list 'screen-history screen-history)))

(defun elscreen-get-current-screen ()
  (car (elscreen-get-conf-list 'screen-history)))

(defun elscreen-get-previous-screen ()
  (cadr (elscreen-get-conf-list 'screen-history)))

(defun elscreen-status-label (screen &optional default)
  (let ((default (or default " "))
        (current-screen (elscreen-get-current-screen))
        (previous-screen (elscreen-get-previous-screen)))
    (cond
     ((eq screen current-screen) "+")
     ((eq screen previous-screen) "-")
     (t default))))

(defvar elscreen-notify-screen-modification-suppress-flag nil)
(defmacro elscreen-notify-screen-modification-suppress (&rest body)
  `(let ((elscreen-notify-screen-modification-suppress-flag t))
     ,@body))

(defvar elscreen-screen-update-hook nil)
(defun elscreen-run-screen-update-hook ()
  (when elscreen-frame-confs
    (elscreen-notify-screen-modification-suppress
     (run-hooks 'elscreen-screen-update-hook)))
  (remove-hook 'post-command-hook 'elscreen-run-screen-update-hook))

(defun elscreen-screen-modified-p (inquirer)
  (let* ((inquirer-list (elscreen-get-conf-list 'modified-inquirer))
         (modified (null (memq inquirer inquirer-list))))
    (add-to-list 'inquirer-list inquirer)
    (elscreen-set-conf-list 'modified-inquirer inquirer-list)
    modified))

(defun elscreen-set-screen-modified ()
  (elscreen-set-conf-list 'modified-inquirer nil)
  (add-hook 'post-command-hook 'elscreen-run-screen-update-hook))

(defvar elscreen-screen-modified-hook-pwc nil)
(defun elscreen-notify-screen-modification (&optional mode)
  (when (and (not (window-minibuffer-p))
             (not elscreen-notify-screen-modification-suppress-flag)
             (or (eq mode 'force)
                 (eq mode 'force-immediately)
                 (null elscreen-screen-modified-hook-pwc)
                 (not (fboundp 'compare-window-configurations))
                 (not (compare-window-configurations
                       (current-window-configuration)
                       elscreen-screen-modified-hook-pwc))))
    (setq elscreen-screen-modified-hook-pwc
          (current-window-configuration))
    (elscreen-set-screen-modified)
    (when (eq mode 'force-immediately)
      (elscreen-run-screen-update-hook))))

(defmacro elscreen-screen-modified-hook-setup (&rest hooks-and-functions)
  (cons
   'progn
   (mapcar
    (lambda (hook-or-function)
      (let ((mode ''normal))
        (when (listp hook-or-function)
          (setq mode (nth 1 hook-or-function))
          (setq hook-or-function (nth 0 hook-or-function)))
        (cond
         ((string-match "-\\(hooks?\\|functions\\)$"
                        (symbol-name hook-or-function))
          `(add-hook (quote ,hook-or-function)
                     (lambda (&rest ignore)
                       (elscreen-notify-screen-modification ,mode))))
         (t ;; Assume hook-or-function is function
          `(defadvice ,hook-or-function (around
                                         elscreen-screen-modified-advice
                                         activate)
             ad-do-it
             (elscreen-notify-screen-modification ,mode))))))
    hooks-and-functions)))

(elscreen-screen-modified-hook-setup
 (recenter 'force) (change-major-mode-hook 'force)
 other-window
 window-configuration-change-hook window-size-change-functions
 (handle-switch-frame 'force) ;; GNU Emacs 21
 (delete-frame 'force)
 (Info-find-node-2 'force))

(defun elscreen-get-screen-to-name-alist-cache ()
  (elscreen-get-conf-list 'screen-to-name-alist-cache))

(defun elscreen-set-screen-to-name-alist-cache (alist)
  (elscreen-set-conf-list 'screen-to-name-alist-cache alist))

(defvar elscreen-mode-to-nickname-alist-symbol-list nil)
(defvar elscreen-mode-to-nickname-alist-internal nil)
(defun elscreen-rebuild-mode-to-nickname-alist ()
  (setq elscreen-mode-to-nickname-alist-internal
        (apply 'append
               (mapcar 'symbol-value
                       elscreen-mode-to-nickname-alist-symbol-list)))
  (elscreen-notify-screen-modification 'force-immediately))
(defun elscreen-set-mode-to-nickname-alist (mode-to-nickname-alist-symbol)
  (add-to-list 'elscreen-mode-to-nickname-alist-symbol-list
               mode-to-nickname-alist-symbol)
  (elscreen-rebuild-mode-to-nickname-alist))
(elscreen-set-mode-to-nickname-alist 'elscreen-mode-to-nickname-alist)

(defvar elscreen-buffer-to-nickname-alist-symbol-list nil)
(defvar elscreen-buffer-to-nickname-alist-internal nil)
(defun elscreen-rebuild-buffer-to-nickname-alist ()
  (setq elscreen-buffer-to-nickname-alist-internal
        (apply 'append
               (mapcar 'symbol-value
                       elscreen-buffer-to-nickname-alist-symbol-list)))
  (elscreen-notify-screen-modification 'force-immediately))
(defun elscreen-set-buffer-to-nickname-alist (buffer-to-nickname-alist-symbol)
  (add-to-list 'elscreen-buffer-to-nickname-alist-symbol-list
               buffer-to-nickname-alist-symbol)
  (elscreen-rebuild-buffer-to-nickname-alist))
(elscreen-set-buffer-to-nickname-alist 'elscreen-buffer-to-nickname-alist)

(defsubst elscreen-get-alist-to-nickname (alist op mode-or-buffer-name)
  (catch 'found
    (progn
      (mapc
       (lambda (map)
         (let ((nickname nil)
               (condition-data (car map))
               (string-or-function (cdr map)))
           (when (funcall op condition-data mode-or-buffer-name)
             (cond
              ((functionp string-or-function)
               (setq nickname
                     (condition-case nil
                         (funcall string-or-function)
                       (wrong-number-of-arguments
                        (funcall string-or-function (current-buffer))))))
              (t
               (setq nickname string-or-function)))
             (throw 'found (cons 'nickname nickname)))))
       alist)
      nil)))

(defun elscreen-get-screen-to-name-alist (&optional truncate-length padding)
  (when (elscreen-screen-modified-p 'elscreen-get-screen-to-name-alist)
    (elscreen-notify-screen-modification-suppress
     (elscreen-set-window-configuration (elscreen-get-current-screen)
                                        (elscreen-current-window-configuration))
     (let* ((screen-list (sort (elscreen-get-screen-list) '<))
            screen-name screen-to-name-alist nickname-type-map)
       (elscreen-save-screen-excursion
        (mapc
         (lambda (screen)
           ;; If nickname exists, use it.
           (setq screen-name (elscreen-get-screen-nickname screen))
           ;; Nickname does not exist, so examine major-mode and buffer-name.
           (when (null screen-name)
             (elscreen-goto-internal screen)

             (setq nickname-type-map
                   (mapcar
                    (lambda (window)
                      (with-current-buffer (window-buffer window)
                        (or (elscreen-get-alist-to-nickname
                             elscreen-mode-to-nickname-alist-internal
                             'string-match (symbol-name major-mode))
                            (elscreen-get-alist-to-nickname
                             elscreen-buffer-to-nickname-alist-internal
                             'string-match (buffer-name))
                            (cons 'buffer-name (buffer-name)))))
                    (window-list)))

             (let (nickname-list)
               (while (> (length nickname-type-map) 0)
                 (let ((type (caar nickname-type-map))
                       (name (cdar nickname-type-map)))
                   (when name
                     (setq nickname-list (cons name nickname-list)))
                   (setq nickname-type-map
                         (if (eq type 'nickname)
                             (delete (car nickname-type-map) nickname-type-map)
                           (cdr nickname-type-map)))))
               (setq screen-name
                     (mapconcat 'identity (reverse nickname-list) ":"))))

           (elscreen--set-alist 'screen-to-name-alist screen screen-name))
         screen-list))

       (elscreen-set-screen-to-name-alist-cache screen-to-name-alist))))

  ;; Arguments of truncate-length and padding are deprecated.
  (if truncate-length
      (let ((screen-to-name-alist
             (copy-alist (elscreen-get-screen-to-name-alist-cache))))
        (elscreen-message "Arguments for `elscreen-get-screen-to-name-alist' are deprecated.  Use elscreen-truncate-screen-name for each screen-name.")
        (mapc
         (lambda (screen-to-name)
           (setcdr screen-to-name
                   (elscreen-truncate-screen-name (cdr screen-to-name)
                                                  truncate-length padding)))
         screen-to-name-alist)
        screen-to-name-alist)
    (elscreen-get-screen-to-name-alist-cache)))

(defun elscreen-truncate-screen-name (screen-name truncate-length &optional padding)
  (let ((truncate-length (max truncate-length 4)))
    (cond
     ((> (string-width screen-name) truncate-length)
      (concat (truncate-string-to-width screen-name (- truncate-length 3)
                                        nil ?.)
              "..."))
     (padding
      (truncate-string-to-width screen-name truncate-length nil ?\ ))
     (t
      screen-name))))

(defun elscreen-goto-internal (screen)
  "Set the configuration of windows, buffers and markers previousuly
stored as SCREEN."
  (let ((elscreen-window-configuration
         (elscreen-get-window-configuration screen)))
    (elscreen-apply-window-configuration elscreen-window-configuration)))

(defvar elscreen-create-hook nil)
(defun elscreen-create-internal (&optional noerror)
  "Create a new screen.
If NOERROR is not nil, no message is displayed in mini buffer
when error is occurred."
  (cond
   ((>= (elscreen-get-number-of-screens) 10)
    (unless noerror
      (elscreen-message "No more screens."))
    nil)
   (t
    (let ((screen-list (sort (elscreen-get-screen-list) '<))
          (screen 0))
      (elscreen-set-window-configuration
       (elscreen-get-current-screen)
       (elscreen-current-window-configuration))
      (while (eq (nth screen screen-list) screen)
        (setq screen (+ screen 1)))
      (elscreen-set-window-configuration
       screen (elscreen-default-window-configuration))
      (elscreen-append-screen-to-history screen)
      (elscreen-notify-screen-modification 'force)
      (run-hooks 'elscreen-create-hook)
      screen))))

(defun elscreen-kill-internal (screen)
  (elscreen-delete-screen-property screen)
  (elscreen-delete-screen-from-history screen)
  screen)

(defun elscreen-find-screens (condition)
  (let ((screen-list (sort (elscreen-get-screen-list) '<))
        result)
    (save-current-buffer
      (elscreen-set-window-configuration
       (elscreen-get-current-screen)
       (elscreen-current-window-configuration))
      (elscreen-notify-screen-modification-suppress
       (elscreen-save-screen-excursion
        (mapc
         (lambda (screen)
           (when (funcall condition screen)
             (setq result (cons screen result))))
         screen-list))
       result))))

(defun elscreen-find-screen (condition)
  (catch 'elscreen-find-screen
    (elscreen-find-screens `(lambda (screen)
                              (when (funcall ,condition screen)
                                (throw 'elscreen-find-screen screen))))))

(defun elscreen-find-screen-by-buffer (buffer &optional create)
  (let* ((buffer (if (bufferp buffer) buffer (get-buffer buffer)))
         (screen (when buffer
                   (elscreen-find-screen
                    `(lambda (screen)
                       (elscreen-goto-internal screen)
                       (not (null (get-buffer-window ,buffer))))))))
    (when (and buffer (null screen) create)
      (cond
       ((setq screen (elscreen-create-internal 'noerror))
        (save-window-excursion
          (elscreen-goto-internal screen)
          (switch-to-buffer buffer t)
          (elscreen-set-window-configuration
           screen (elscreen-current-window-configuration))))
       (t
        (setq screen (elscreen-get-current-screen))
        (elscreen-goto-internal screen)
        (save-selected-window
          (select-window (split-window))
          (switch-to-buffer buffer t)))))
    screen))

(defun elscreen-find-screen-by-major-mode (major-mode-or-re)
  (let ((major-mode-re (cond
                        ((stringp major-mode-or-re)
                         major-mode-or-re)
                        ((symbolp major-mode-or-re)
                         (format "^%s$" (regexp-quote
                                         (symbol-name major-mode-or-re))))
                        (t nil))))
    (when major-mode-re
      (elscreen-find-screen
       (lambda (screen)
         (elscreen-goto-internal screen)
         (save-selected-window
           (catch 'found
             (mapc
              (lambda (window)
                (select-window window)
                (when (string-match major-mode-re (symbol-name major-mode))
                  (throw 'found t)))
              (window-list))
             nil)))))))

(defvar elscreen-last-message "Welcome to ElScreen!"
  "Last shown message.")
(defun elscreen-message (message &optional sec)
  "Display MESSAGE in mini-buffer for SEC seconds.
Default value for SEC is 3."
  (when message
    (setq elscreen-last-message message)
    (message "%s" message)
    (sit-for (or sec 3)))
  (message nil))


;;; User Interfaces:

(defun elscreen-create ()
  "Create a new screen and switch to it."
  (interactive)
  (let ((screen (elscreen-create-internal)))
    (if screen
        (elscreen-goto screen))))

(defun elscreen-clone (&optional screen)
  "Create a new screen with the window-configuration of SCREEN.
If SCREEN is ommitted, current-screen is used."
  (interactive)
  (let ((screen (or screen (elscreen-get-current-screen)))
        clone elscreen-window-configuration)
    (cond
     ((not (elscreen-screen-live-p screen))
      (elscreen-message "There is no such screen, cannot clone"))
    ((setq clone (elscreen-create-internal))
     (save-window-excursion
       (elscreen-goto-internal screen)
       (setq elscreen-window-configuration
             (elscreen-current-window-configuration)))
     (elscreen-set-window-configuration clone elscreen-window-configuration)
     (elscreen-goto clone)))))

(defvar elscreen-kill-hook nil)
(defun elscreen-kill (&optional screen)
  "Kill SCREEN.  If optional argument SCREEN is
ommitted, current-screen is killed."
  (interactive "P")
  (let ((screen (or (and (numberp screen) screen)
                    (elscreen-get-current-screen))))
    (cond
     ((not (elscreen-screen-live-p screen))
      (elscreen-message "There is no such screen, cannot kill")
      nil)
     ((elscreen-one-screen-p)
      (elscreen-message "There is only one screen, cannot kill")
      nil)
     (t
      (elscreen-kill-internal screen)
      (elscreen-goto-internal (elscreen-get-current-screen))
      (elscreen-notify-screen-modification 'force)
      (run-hooks 'elscreen-kill-hook)
      screen))))

(defun elscreen-kill-screen-and-buffers (&optional screen)
  "Kill buffers on SCREEN and SCREEN itself.  If optional
argument SCREEN is omitted, current screen is killed."
  (interactive)
  (let* ((screen (or screen (elscreen-get-current-screen)))
         (elscreen-window-configuration
          (elscreen-get-window-configuration screen)))
    (when (elscreen-kill screen)
      (save-window-excursion
        (elscreen-apply-window-configuration elscreen-window-configuration)
        (mapc
         (lambda (buffer)
           (when (buffer-live-p buffer)
             (kill-buffer buffer)))
         (mapcar 'window-buffer (window-list))))
      screen)))

(defun elscreen-kill-others (&optional screen)
  "Kill screens other than SCREEN.  If optional argument SCREEN
is ommitted, current screen will survive."
  (interactive)
  (let* ((screen (or screen (elscreen-get-current-screen)))
         (screen-list (when (elscreen-screen-live-p screen)
                        (delq screen (sort (elscreen-get-screen-list) '<))))
         screen-list-string)
    (cond
     ((not (elscreen-screen-live-p screen)) ;; XXX
      (when (called-interactively-p 'any)
        (elscreen-message "There is no such screen")))
     ((null screen-list)
      (when (called-interactively-p 'any)
        (elscreen-message "There is only one screen, cannot kill")))
     ((or
       (not (called-interactively-p 'any))
       (yes-or-no-p (format "Really kill screens other than %d? " screen)))
      (setq screen-list-string (mapconcat
                                (lambda (screen)
                                  (elscreen-kill-internal screen)
                                  (number-to-string screen))
                                screen-list ","))
      (elscreen-goto-internal screen)
      (elscreen-notify-screen-modification 'force-immediately)
      (when (called-interactively-p 'any)
        (elscreen-message (format "screen %s killed" screen-list-string)))))
    screen-list))

(defvar elscreen-goto-hook nil)
(defun elscreen-goto (screen)
  "Switch to screen SCREEN."
  (interactive "NGoto screen number: ")
  (cond
   ((eq (elscreen-get-current-screen) screen))
   ((elscreen-screen-live-p screen)
    (elscreen-set-window-configuration
     (elscreen-get-current-screen)
     (elscreen-current-window-configuration))
    (elscreen-set-current-screen screen)
    (elscreen-goto-internal screen)
    (elscreen-notify-screen-modification 'force)
    (run-hooks 'elscreen-goto-hook)
    screen)
   (t
    (elscreen-message (format "No screen %d" screen))
    nil)))

(defun elscreen-next ()
  "Switch to the next screen."
  (interactive)
  (cond
   ((elscreen-one-screen-p)
    (elscreen-message
     (format "You cannot escape from screen %d!"
             (elscreen-get-current-screen))))
   (t
    (let* ((screen-list (sort (elscreen-get-screen-list) '<))
           (next-screen
            (or (nth 1 (memq (elscreen-get-current-screen) screen-list))
                (car screen-list))))
      (elscreen-goto next-screen)))))

(defun elscreen-previous ()
  "Switch to the previous screen."
  (interactive)
  (cond
   ((elscreen-one-screen-p)
    (elscreen-message
     (format "You cannot escape from screen %d!"
             (elscreen-get-current-screen))))
   (t
    (let* ((screen-list (sort (elscreen-get-screen-list) '>))
           (previous-screen
            (or (nth 1 (memq (elscreen-get-current-screen) screen-list))
                (car screen-list))))
      (elscreen-goto previous-screen)))))

(defun elscreen-toggle ()
  "Toggle to the screen selected previously."
  (interactive)
  (cond
   ((elscreen-one-screen-p)
    (elscreen-message
     (format "You cannot escape from screen %d!"
             (elscreen-get-current-screen))))
   (t
    (elscreen-goto (elscreen-get-previous-screen)))))

(defun elscreen-jump ()
  "Switch to specified screen."
  (interactive)
  (let ((next-screen (string-to-number (string last-command-event))))
    (if (and (<= 0 next-screen) (<= next-screen 9))
        (elscreen-goto next-screen))))
(defalias 'elscreen-jump-0 'elscreen-jump)
(defalias 'elscreen-jump-9 'elscreen-jump)

(defun elscreen-swap ()
  "Interchange screens selected currently and previously."
  (interactive)
  (cond
   ((elscreen-one-screen-p)
    (elscreen-message "There is only one screen, cannot swap"))
   (t
    (let* ((current-screen (elscreen-get-current-screen))
           (previous-screen (elscreen-get-previous-screen))
           (current-screen-property
            (elscreen-get-screen-property current-screen))
           (previous-screen-property
            (elscreen-get-screen-property previous-screen)))
      (elscreen-set-screen-property current-screen previous-screen-property)
      (elscreen-set-screen-property previous-screen current-screen-property)
      (elscreen-goto-internal (elscreen-get-current-screen))))))

(defun elscreen-screen-nickname (nickname)
  "Set nickname for current screen to NICKNAME."
  (interactive "sSet window title to: ")
  (cond
   ((zerop (length nickname))
    (elscreen-delete-screen-nickname (elscreen-get-current-screen)))
   (t
    (elscreen-set-screen-nickname (elscreen-get-current-screen) nickname)))
  (elscreen-notify-screen-modification 'force))

(defun elscreen-display-screen-name-list ()
  "Display the list of screens in mini-buffer."
  (interactive)
  (let ((screen-list (sort (elscreen-get-screen-list) '<))
        (screen-to-name-alist (elscreen-get-screen-to-name-alist)))
    (elscreen-message
     (mapconcat
      (lambda (screen)
        (let ((screen-name (assoc-default screen screen-to-name-alist)))
          (format "%d%s %s" screen
                  (elscreen-status-label screen "")
                  screen-name)))
      screen-list "  "))))


;;; Help

(defvar elscreen-help-symbol-list nil)
(defun elscreen-set-help (help-symbol)
  (add-to-list 'elscreen-help-symbol-list help-symbol t))
(elscreen-set-help 'elscreen-help)

(defun elscreen-help ()
  "Show key bindings of ElScreen and Add-On softwares."
  (interactive)
  (with-output-to-temp-buffer "*ElScreen Help*"
    (princ (substitute-command-keys
            (mapconcat 'symbol-value
                       elscreen-help-symbol-list "\n\n")))
    (help-print-return-message)))


;;; Utility Functions

(defun elscreen-display-version ()
  "Display ElScreen version."
  (interactive)
  (elscreen-message (concat "ElScreen version " elscreen-version)))

(defun elscreen-toggle-display-screen-number ()
  "Toggle the screen number in the mode line."
  (interactive)
  (setq elscreen-display-screen-number (null elscreen-display-screen-number))
  (elscreen-notify-screen-modification 'force))

(defun elscreen-toggle-display-tab ()
  "Toggle the tab on the top of screen."
  (interactive)
  (setq elscreen-display-tab (null elscreen-display-tab))
  (elscreen-notify-screen-modification 'force))

(defun elscreen-display-last-message ()
  "Repeat the last message displayed in the mini-buffer."
  (interactive)
  (elscreen-message elscreen-last-message 5))

(defun elscreen-display-time ()
  "Show system information."
  (interactive)
  (elscreen-message
   (format "%s %s / %s"
           (current-time-string)
           (nth 1 (current-time-zone))
           (mapconcat
            (lambda (load)
              (format "%.2f" (/ load 100.0)))
            (load-average) " "))))

(defun elscreen-select-and-goto ()
  (interactive)
  (let* ((screen-list (sort (elscreen-get-screen-list) '<))
         (screen-to-name-alist (elscreen-get-screen-to-name-alist))
         (command-list '(("c" . elscreen-create)
                         ("n" . elscreen-next)
                         ("p" . elscreen-previous)
                         ("t" .  elscreen-toggle)))
         (truncate-length (- (frame-width) 6))
         (candidate-window-height (max (+ (elscreen-get-number-of-screens) 4)
                                       window-min-height))
         (candidate-buffer (get-buffer-create
                            (format " *ElScreen-select:%s*"
                                    (prin1-to-string (selected-frame)))))
         (candidate (concat
                     "Current screen list: \n\n"
                     (mapconcat
                      (lambda (screen)
                        (format "  %d%s %s\n" screen
                                (elscreen-status-label screen)
                                (elscreen-truncate-screen-name
                                 (assoc-default screen screen-to-name-alist)
                                 truncate-length)))
                      screen-list nil)))
         (prompt "Select screen or (c)reate, (n)ext, (p)revious, (t)oggle: ")
         (minibuffer-map (copy-keymap minibuffer-local-map))
         window frame-last-window command-or-target-screen mini-hist)
    ;; prepare window to show candidates
    (save-window-excursion
      (setq frame-last-window (previous-window (frame-first-window)))
      (while (minibuffer-window-active-p frame-last-window)
        (setq frame-last-window (previous-window frame-last-window)))
      (while (and (not (one-window-p))
                  (or (< (window-width frame-last-window)
                         (frame-width))
                      (< (window-height frame-last-window)
                         (+ candidate-window-height window-min-height))))
        (setq window frame-last-window)
        (setq frame-last-window (previous-window window))
        (delete-window window))
      (select-window (split-window frame-last-window))
      (shrink-window (- (window-height) candidate-window-height))
      ;; now switch to the buffer for candidates and fill it
      (switch-to-buffer candidate-buffer)
      (let ((buffer-read-only nil))
        (erase-buffer)
        (insert candidate)
        (goto-char (point-min))
        (save-excursion
          (while (not (eobp))
            (when (looking-at "^  \\([0-9]\\)\\(.\\) \\(.*\\)$")
              (put-text-property
               (match-beginning 1) (match-end 1) 'face 'bold)
              (cond
               ((string= (match-string 2) "+")
                (put-text-property
                 (match-beginning 3) (match-end 3) 'face 'bold))))
            (forward-line 1)))
        (setq buffer-read-only t)
        (set-buffer-modified-p nil))
      ;; make keymap for minibuffer
      (suppress-keymap minibuffer-map t)
      (define-key minibuffer-map "\C-g" 'abort-recursive-edit)
      (define-key minibuffer-map "\C-m" 'exit-recursive-edit)
      (define-key minibuffer-map "q" 'exit-recursive-edit)
      (define-key minibuffer-map " " 'exit-recursive-edit)
      (mapc
       (lambda (command)
         (define-key minibuffer-map (car command) 'self-insert-and-exit))
       command-list)
      (mapc
       (lambda (screen)
         (define-key minibuffer-map (number-to-string screen)
           'self-insert-and-exit))
       screen-list)
      ;; read key from minibuffer
      (unwind-protect
          (setq command-or-target-screen
                (read-from-minibuffer prompt nil minibuffer-map
                                      nil 'mini-hist))
        (kill-buffer candidate-buffer)))
    (cond
     ((string= command-or-target-screen ""))
     ((assoc-default command-or-target-screen command-list)
      (funcall (assoc-default command-or-target-screen command-list)))
     (t
      (elscreen-goto (string-to-number command-or-target-screen))))))

(defun elscreen-find-and-goto-by-buffer (&optional buffer create noselect)
  "Go to the screen that has the window with buffer BUFFER,
creating one if none already exists."
  (interactive)
  (let* ((prompt "Go to the screen with specified buffer: ")
         (create (or create (called-interactively-p 'any)))
         (buffer-name (or (and (bufferp buffer) (buffer-name buffer))
                          (and (stringp buffer) buffer)
                          (and (featurep 'iswitchb)
                               (iswitchb-read-buffer prompt))
                          (read-buffer prompt)))
         (target-screen (elscreen-find-screen-by-buffer
                         (get-buffer-create buffer-name) create)))
    (when target-screen
      (elscreen-goto target-screen)
      (unless noselect
        (select-window (get-buffer-window buffer-name))))
    target-screen))

(defun elscreen-find-file (filename)
  "Edit file FILENAME.
Switch to a screen visiting file FILENAME,
creating one if none already exists."
  (interactive "FFind file in new screen: ")
  (elscreen-find-and-goto-by-buffer (find-file-noselect filename) 'create))

(defun elscreen-find-file-read-only (filename)
  "Edit file FILENAME with new screen but don't allow changes.
Like \\[elscreen-find-file] but marks buffer as read-only.
Use \\[toggle-read-only] to permit editing."
  (interactive "FFind file read-only in new screen: ")
  (elscreen-find-file filename)
  (setq buffer-read-only t))

(defun elscreen-dired (dirname &optional switches)
  (interactive (progn
                 (or (featurep 'dired) (require 'dired))
                 (dired-read-dir-and-switches "in new screen ")))
  (elscreen-find-and-goto-by-buffer (dired-noselect dirname switches) 'create))

(defun elscreen-execute-extended-command (prefix-arg)
  (interactive "P")
  (let ((prefix-arg prefix-arg)
        (prefix-key (key-description elscreen-prefix-key))
        target-screen)
    (setq this-command (intern (completing-read
                                ;; Note: this has the hard-wired
                                ;;  "C-u" and "M-x" string bug in common
                                ;;  with all Emacs's.
                                ;; (i.e. it prints C-u and M-x regardless of
                                ;; whether some other keys were actually bound
                                ;; to `execute-extended-command' and
                                ;; `universal-argument'.
                                (cond ((eq prefix-arg '-)
                                       (format "- %s M-x " prefix-key))
                                      ((equal prefix-arg '(4))
                                       (format "C-u %s M-x " prefix-key))
                                      ((integerp prefix-arg)
                                       (format "%d %s M-x "
                                               prefix-arg prefix-key))
                                      ((and (consp prefix-arg)
                                            (integerp (car prefix-arg)))
                                       (format "%d %s M-x "
                                               (car prefix-arg) prefix-key))
                                      (t
                                       (format "%s M-x " prefix-key)))
                                obarray 'commandp t nil
                                'extended-command-history)))
    (if (setq target-screen (elscreen-create-internal 'noerror))
        (elscreen-goto target-screen)
      (select-window (split-window)))
    (command-execute this-command t)))


;;; Mode Line & Menu & Tab

;; GNU Emacs
(defvar elscreen-mode-line-string "[0]")
(defun elscreen-mode-line-update ()
  (when (elscreen-screen-modified-p 'elscreen-mode-line-update)
    (setq elscreen-mode-line-string
          (format "[%d]" (elscreen-get-current-screen)))
    (force-mode-line-update)))

(let ((point (memq 'mode-line-position mode-line-format))
      (elscreen-mode-line-elm '(elscreen-display-screen-number
                                (" " elscreen-mode-line-string))))
  (when (and (null (member elscreen-mode-line-elm mode-line-format)) point)
    (setcdr point (cons elscreen-mode-line-elm (cdr point)))))

(add-hook 'elscreen-screen-update-hook 'elscreen-mode-line-update)

  ;; Menu

(define-key-after (lookup-key global-map [menu-bar]) [elscreen]
  (cons "ElScreen" (make-sparse-keymap "ElScreen")) 'buffer)

(defvar elscreen-menu-bar-command-entries
  (list (list 'elscreen-command-separator
              'menu-item
              "--")
        (list 'elscreen-create
              'menu-item
              "Create Screen"
              'elscreen-create
              :help "Create a new screen and switch to it")
        (list 'elscreen-clone
              'menu-item
              "Clone Screen"
              'elscreen-clone
              :help "Create a new screen with the window-configuration of current screen")
        (list 'elscreen-kill
              'menu-item
              "Kill Screen"
              'elscreen-kill
              :help "Kill current screen")
        (list 'elscreen-kill-screen-and-buffers
              'menu-item
              "Kill Screen and Buffers"
              'elscreen-kill-screen-and-buffers
              :help "Kill current screen and buffers")
        (list 'elscreen-kill-others
              'menu-item
              "Kill Other Screens"
              'elscreen-kill-others
              :help "Kill other screens")
        (list 'elscreen-next
              'menu-item
              "Next Screen"
              'elscreen-next
              :help "Switch to the \"next\" screen in a cyclic order")
        (list 'elscreen-previous
              'menu-item
              "Previous Screen"
              'elscreen-previous
              :help "Switch to the \"previous\" screen in a cyclic order")
        (list 'elscreen-toggle
              'menu-item
              "Toggle Screen"
              'elscreen-toggle
              :help "Toggle to the screen selected previously")
        (list 'elscreen-command-separator
              'menu-item
              "--")
        (list 'elscreen-toggle-display-screen-number
              'menu-item
              "Display Screen Number"
              'elscreen-toggle-display-screen-number
              :help "Display screen number on the mode line"
              :button '(:toggle . elscreen-display-screen-number))
        (list 'elscreen-toggle-display-tab
              'menu-item
              "Display Tab"
              'elscreen-toggle-display-tab
              :help "Display tab on the top of screen"
              :button '(:toggle . elscreen-display-tab))))

(defun elscreen-menu-bar-update (&optional force)
  (when (and (lookup-key (current-global-map) [menu-bar elscreen])
             (or force
                 (elscreen-screen-modified-p 'elscreen-menu-bar-update)))
    (let ((screen-list (sort (elscreen-get-screen-list) '<))
          (screen-to-name-alist (elscreen-get-screen-to-name-alist))
          (elscreen-menu nil))
      (setq elscreen-menu
            (mapcar
             (lambda (screen)
               (list (string-to-char (number-to-string screen))
                     'menu-item
                     (format "%d%s %s" screen
                             (elscreen-status-label screen)
                             (elscreen-truncate-screen-name
                              (assoc-default screen screen-to-name-alist) 25))
                     'elscreen-jump
                     :keys (format "%s %d"
                                   (key-description elscreen-prefix-key)
                                   screen)))
             screen-list))
      (setq elscreen-menu
            (nconc elscreen-menu elscreen-menu-bar-command-entries))
      (setq elscreen-menu
            (cons 'keymap (cons "Select Screen" elscreen-menu)))
      (define-key (current-global-map) [menu-bar elscreen]
        (cons (copy-sequence "ElScreen") elscreen-menu)))))

(add-hook 'elscreen-screen-update-hook 'elscreen-menu-bar-update)

  ;; Tab

(defvar elscreen-tab-format nil)
(make-variable-buffer-local 'elscreen-tab-format)

(defsubst elscreen-tab-create-keymap (&rest definitions)
  (let ((keymap (make-sparse-keymap))
        (key-function-pairs
         (eval-when-compile
           (mapcar
            (lambda (key)
              (cons key 'ignore))
            (list 'mouse-1 'mouse-2 'mouse-3
                  'down-mouse-1 'down-mouse-2 'down-mouse-3
                  'drag-mouse-1 'drag-mouse-2 'drag-mouse-3)))))
    (while definitions
      (elscreen--set-alist 'key-function-pairs (car definitions) (cadr definitions))
      (setq definitions (cddr definitions)))
    (mapc
     (lambda (key-function-pair)
       (let ((key (car key-function-pair))
             (function (cdr key-function-pair)))
         (define-key keymap (vector 'header-line key) function)))
     key-function-pairs)
    keymap))

(defsubst elscreen-tab-width ()
  (if (numberp elscreen-display-tab)
      elscreen-display-tab
    (let* ((number-of-screens (elscreen-get-number-of-screens))
           (available-width
            (- (frame-width) (if elscreen-tab-display-control 4 0)))
           (tab-width
            (round (- (/ available-width number-of-screens)
                      (if elscreen-tab-display-kill-screen 5.5 1.5)))))
      (max (min tab-width 16) 1))))

(defun elscreen-tab-escape-% (string)
  (if (string-match "%" string)
      (let ((retval "")
            start (end 0) substring)
        (while (setq start end)
          (setq end (next-property-change start string))
          (setq substring (replace-regexp-in-string
                           "%" "%%" (substring string start end)))
          (set-text-properties 0 (length substring)
                               (text-properties-at start string) substring)
          (setq retval (concat retval substring)))
        retval)
    string))

(defun elscreen-tab-update (&optional force)
  (when (and (not (window-minibuffer-p))
             (or (elscreen-screen-modified-p 'elscreen-tab-update) force))
    (walk-windows
     (lambda (window)
       (with-current-buffer (window-buffer window)
         (when (and (boundp 'elscreen-tab-format)
                    (equal header-line-format elscreen-tab-format)
                    (or (not (eq (window-buffer window)
                                 (window-buffer (frame-first-window))))
                        (not elscreen-display-tab)))
           (kill-local-variable 'elscreen-tab-format)
           (setq header-line-format nil))))
     'other 'other)

    (when elscreen-display-tab
      (let ((screen-list (sort (elscreen-get-screen-list) '<))
            (screen-to-name-alist (elscreen-get-screen-to-name-alist))
            (current-screen (elscreen-get-current-screen))
            (half-space (propertize
                           " "
                           'display '(space :width 0.5)))
            (tab-separator (propertize
                            " "
                            'face 'elscreen-tab-background-face
                            'display '(space :width 0.5)))
            (control-tab (propertize
                          "<->"
                          'face 'elscreen-tab-control-face
                          'local-map (elscreen-tab-create-keymap
                                      'mouse-1 'elscreen-previous
                                      'mouse-2 'elscreen-create
                                      'mouse-3 'elscreen-next)
                          'help-echo "mouse-1: previous screen, mouse-2: create new screen, mouse-3: next screen")))
        (with-current-buffer (window-buffer (frame-first-window))
          (kill-local-variable 'elscreen-tab-format)
          (when elscreen-tab-display-control
            (setq elscreen-tab-format
                  (nconc
                   elscreen-tab-format
                   (list
                    control-tab
                    tab-separator))))

          (mapc
           (lambda (screen)
             (let ((kill-screen
                    (propertize
                     "[X]"
                     'local-map (elscreen-tab-create-keymap
                                 'mouse-1 `(lambda (e)
                                             (interactive "e")
                                             (elscreen-kill ,screen))
                                 'M-mouse-1 `(lambda (e)
                                               (interactive "e")
                                               (elscreen-kill-screen-and-buffers ,screen)))
                     'help-echo (format "mouse-1: kill screen %d, M-mouse-1: kill screen %d and buffers on it" screen screen))))
               (setq elscreen-tab-format
                     (nconc
                      elscreen-tab-format
                      (list
                       (propertize
                        (concat
                         (when (or (eq elscreen-tab-display-kill-screen 'left)
                                   (eq elscreen-tab-display-kill-screen t))
                           kill-screen)
                         half-space
                         (propertize
                          (format "%d%s%s%s"
                                  screen
                                  (elscreen-status-label screen)
                                  half-space
                                  (elscreen-tab-escape-%
                                   (elscreen-truncate-screen-name
                                    (assoc-default screen screen-to-name-alist)
                                    (elscreen-tab-width) t)))
                          'help-echo (assoc-default screen screen-to-name-alist)
                          'local-map (elscreen-tab-create-keymap
                                      'mouse-1 `(lambda (e)
                                                  (interactive "e")
                                                  (elscreen-goto ,screen))))
                         (when (eq elscreen-tab-display-kill-screen 'right)
                           (concat half-space kill-screen)))
                        'face (if (eq current-screen screen)
                                  'elscreen-tab-current-screen-face
                                'elscreen-tab-other-screen-face))
                       tab-separator)))))
           screen-list)

          (setq elscreen-tab-format
                (nconc
                 elscreen-tab-format
                 (list
                  (propertize
                   (make-string (window-width) ?\ )
                   'face 'elscreen-tab-background-face
                   'local-map (elscreen-tab-create-keymap)))))

          (setq header-line-format elscreen-tab-format))))))

(add-hook 'elscreen-screen-update-hook 'elscreen-tab-update)

;;; Unsupported Functions...

(defun elscreen-link ()
  (interactive)
  (cond
   ((null (one-window-p))
    (elscreen-message "current screen must not have two or more window!"))
   ((or (null (elscreen-get-previous-screen))
        (elscreen-one-screen-p))
    (elscreen-message "must specify previous screen!"))
   ((and (elscreen-goto (elscreen-get-previous-screen))
         (null (one-window-p)))
    (elscreen-goto (elscreen-get-previous-screen))
    (elscreen-message "previous screen must not have two or more window!"))
   (t
    (let ((elscreen-link-buffer (current-buffer)))
      (elscreen-kill)
      (switch-to-buffer-other-window elscreen-link-buffer)))))

(defun elscreen-split ()
  (interactive)
  (if (and (null (one-window-p))
           (< (elscreen-get-number-of-screens) 10))
      (let ((elscreen-split-buffer (current-buffer)))
        (delete-window)
        (elscreen-create)
        (switch-to-buffer elscreen-split-buffer)
        (elscreen-goto (elscreen-get-previous-screen)))
    (elscreen-message "cannot split screen!")))

;;; Start ElScreen!

;;;###autoload
(defun elscreen-start ()
  (interactive)
  (mapc
   (lambda (frame)
     (elscreen-make-frame-confs frame 'keep))
   (frame-list))
  (let ((prefix-key elscreen-prefix-key)
        (elscreen-prefix-key nil))
    (elscreen-set-prefix-key prefix-key)))

(provide 'elscreen)

;;; elscreen.el ends here
Download .txt
gitextract_x2mkpamo/

├── COPYING
├── GF-QuickStart
├── GF-README
├── Readme.md
├── elscreen-buffer-list.el
├── elscreen-color-theme.el
├── elscreen-dired.el
├── elscreen-dnd.el
├── elscreen-gf.el
├── elscreen-goby.el
├── elscreen-howm.el
├── elscreen-server.el
├── elscreen-speedbar.el
├── elscreen-w3m.el
├── elscreen-wl.el
└── elscreen.el
Condensed preview — 16 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (176K chars).
[
  {
    "path": "COPYING",
    "chars": 18092,
    "preview": "                    GNU GENERAL PUBLIC LICENSE\n                       Version 2, June 1991\n\n Copyright (C) 1989, 1991 Fr"
  },
  {
    "path": "GF-QuickStart",
    "chars": 480,
    "preview": "ElScreen-GF 1.5.3 Quick Start Guide\n\n 1) Install ElScreen, ElScreen-GF.\n 2) Make sure that the grep on your system is GN"
  },
  {
    "path": "GF-README",
    "chars": 6336,
    "preview": "ElScreen-GF 1.5.3 README file\n\nThis is the distribution of ElScreen-GF 1.5.3, released November 2007.\nIt is an Emacs int"
  },
  {
    "path": "Readme.md",
    "chars": 5726,
    "preview": "ElScreen\n==========\n\n\nThis is a fork of\n[ElScreen](http://www.morishima.net/~naoto/elscreen-en/?lang=en)\nupdated for Ema"
  },
  {
    "path": "elscreen-buffer-list.el",
    "chars": 3560,
    "preview": ";;;\n\n;; This code is free software; you can redistribute it and/or modify\n;; it under the terms of the GNU General Publi"
  },
  {
    "path": "elscreen-color-theme.el",
    "chars": 5295,
    "preview": ";; -*- Mode: Emacs-Lisp -*-\n;;\n;; elscreen-color-theme.el\n;;\n(defconst elscreen-color-theme-version \"0.0.0 (November 19,"
  },
  {
    "path": "elscreen-dired.el",
    "chars": 1359,
    "preview": ";; -*- Mode: Emacs-Lisp -*-\n;;\n;; elscreen-dired.el \n;;\n(defconst elscreen-dired-version \"0.1.0 (November 6, 2005)\")\n;;\n"
  },
  {
    "path": "elscreen-dnd.el",
    "chars": 2032,
    "preview": ";; -*- Mode: Emacs-Lisp -*-\n;;\n;; elscreen-dnd.el\n;;\n(defconst elscreen-dnd-version \"0.0.0 (December 15, 2005)\")\n;;\n;; A"
  },
  {
    "path": "elscreen-gf.el",
    "chars": 43265,
    "preview": ";; -*- Mode: Emacs-Lisp -*-\n;;\n;; elscreen-gf.el\n;;\n(defconst elscreen-gf-version \"1.5.3 (November 07, 2007)\")\n;;\n;; Aut"
  },
  {
    "path": "elscreen-goby.el",
    "chars": 1255,
    "preview": ";; -*- Mode: Emacs-Lisp -*-\n;;\n;; elscreen-goby.el \n;;\n(defconst elscreen-goby-version \"0.0.0 (July 14, 2005)\")\n;;\n;; Au"
  },
  {
    "path": "elscreen-howm.el",
    "chars": 4481,
    "preview": ";; -*- Mode: Emacs-Lisp -*-\n;;\n;; elscreen-howm.el\n;;\n(defconst elscreen-howm-version \"0.1.3 (September 14, 2008)\")\n;;\n;"
  },
  {
    "path": "elscreen-server.el",
    "chars": 2213,
    "preview": ";;; elscreen-server.el --- server support for elscreen\n;;\n(defconst elscreen-server-version \"0.2.0 (November 23, 2007)\")"
  },
  {
    "path": "elscreen-speedbar.el",
    "chars": 1982,
    "preview": ";; -*- Mode: Emacs-Lisp -*-\n;;\n;; elscreen-speedbar.el\n;;\n(defconst elscreen-speedbar-version \"0.0.0 (November 18, 2007)"
  },
  {
    "path": "elscreen-w3m.el",
    "chars": 3054,
    "preview": ";; -*- Mode: Emacs-Lisp -*-\n;;\n;; elscreen-w3m.el\n;;\n(defconst elscreen-w3m-version \"0.2.2 (July 26, 2006)\")\n;;\n;; Autho"
  },
  {
    "path": "elscreen-wl.el",
    "chars": 5965,
    "preview": ";; -*- Mode: Emacs-Lisp -*-\n;;\n;; elscreen-wl.el\n;;\n(defconst elscreen-wl-version \"0.8.0 (November 03, 2007)\")\n;;\n;; Aut"
  },
  {
    "path": "elscreen.el",
    "chars": 64888,
    "preview": ";;; elscreen.el --- Emacs window session manager\n\n;; Author: Naoto Morishima <naoto@morishima.net>\n;; Based on: screens."
  }
]

About this extraction

This page contains the full source code of the knu/elscreen GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 16 files (166.0 KB), approximately 40.6k tokens. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.

Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.

Copied to clipboard!