bug-global
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] Enhancement: Switch gtags-rootdir easily and interactively


From: Kazuo YAGI
Subject: Re: [PATCH] Enhancement: Switch gtags-rootdir easily and interactively
Date: Sun, 06 Jul 2008 04:59:05 +0900
User-agent: Thunderbird 2.0.0.12 (X11/20080227)

Hi YAMAGUCHI-san and all,

Thank you for your reply and I'm really glad to get it from you.

I agree your suggestion that it's better to create the independent minor mode
for this switching directory facility.

And I've created the first version. The attached image shows how to work
`switch-dir-select-mode' function. I with you would like new interface.

The usage is as follows, please try to use it and comment if any.
Your reply encourage me to my happy hacking :-)

* Usage *
0. Edit the line 72-77 in switch-dir.el.
1. Move the switch-dir.el to your load-path.
2. $ echo -e "(require 'switch-dir)\n(switch-dir-mode t)" >> $HOME/.emacs
3. M-x switch-dir-select after emacs invoked.

Regards,
- Kazuo YAGI

Shigio YAMAGUCHI Wrote:
> But it seems not to be the one which specially relates
> to gtags-mode.
> 
> It does the followings:
> 1. make user choose a directory path in a path list.
> 2. change directory to the path.
> 
> This facility is convenient in a lot of modes beside gtags-mode.
> You might make this program an independent minor mode?
;;; switch-dir.el --- switch current directory easily from directory list.

;; Copyright (C) 2008 Free Software Foundation, Inc.

;; Author:     Kazuo YAGI <address@hidden>
;; Maintainer: Kazuo YAGI <address@hidden>
;; Created:    2008-07-06
;; Version:    0.9
;; Keywords:   cd, directory, gtags, tools

;; This file is part of GNU Emacs.

;; GNU Emacs 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.

;; GNU Emacs 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 GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.

;;; Commentary:

;; Provides a local minor mode (toggled by M-x switch-dir-mode) to change
;; a directory from directory list.
;;
;; You could make the directory list for using varialbe `switch-dir-alist'
;; to show and select these easily and interactively.
;;
;; This was impremented to satisfy a request for GNU GLOBAL to change
;; a directory under which the tag file(GTAGS) is located.

;; `switch-dir-mode' is implemented as a minor mode so that it can work with any
;; other major modes. `switch-dir-select-mode' is implemented as a major mode.
;;
;; Please copy this file into emacs lisp library directory or place it in
;; a directory (for example "~/switch-dir.el") and write $HOME/.emacs like this.
;;
;;      (setq load-path (cons "~/switch-dir.el" load-path))
;;
;; If you hope to use `switch-dir-mode' with GNU GLOBAL `gtags-mode', please add
;; `gtags-mode-hook' to your $HOME/.emacs like below.
;;
;;      (setq gtags-mode-hook '(lambda () (switch-dir-mode t)))
;;
;; Happy Hacking!

;;; Code:

(require 'hl-line)

;; switch-dir-mode initialization
(defvar switch-dir-mode nil
  "Non-nil if switch-dir-mode is enabled.")
(make-variable-buffer-local 'switch-dir-mode)
(defvar switch-dir-mode-map (make-sparse-keymap)
  "Keymap used in switch-dir-mode")
(define-key switch-dir-mode-map "\M-~" 'switch-dir-select-mode)
(defvar switch-dir-alist nil
        "Root directory alist of source trees.")
;
; If you hope to creat a directory list to show and select, please add
; the following code to your $HOME/.emacs:
; 
(setq switch-dir-alist
      '(("emacs 22.1" . "/home/kyagi/cr/emacs-22.1")
                                ("emacs 22.2" . "/home/kyagi/cr/emacs-22.2")
                                ("rpm 4.4"    . "/home/kyagi/cr/rpm-4.4")
                                ("global 5.7.1" . "/home/kyagi/cr/global-5.7.1")
                                ("synergy 1.3.1" . 
"/home/kyagi/cr/synergy-1.3.1/")
        ))
(defvar switch-dir-alist-buffer-name "*switch-dir-alist*"
  "Buffer name for showing and hiding switch-dir-alist.")
(defvar switch-rootdir nil
        "The directory you want to go.")
(defvar switch-currentdir default-directory
        "Current directory.")

;; switch-dir-select-mode initialization
(defvar switch-dir-select-mode nil
  "Non-nil if switch-dir-select-mode is enabled.")
(make-variable-buffer-local 'switch-dir-select-mode)
(defvar switch-dir-select-mode-map (make-sparse-keymap)
  "Keymap used in switch-dir-select-mode")
(define-key switch-dir-select-mode-map [return] 'switch-change-dir)
(define-key switch-dir-select-mode-map "\^?"    'scroll-down)
(define-key switch-dir-select-mode-map " "      'scroll-up)
(define-key switch-dir-select-mode-map "\C-b"   'scroll-down)
(define-key switch-dir-select-mode-map "\C-f"   'scroll-up)
(define-key switch-dir-select-mode-map "k"      'previous-line)
(define-key switch-dir-select-mode-map "j"      'next-line)
(define-key switch-dir-select-mode-map "p"      'previous-line)
(define-key switch-dir-select-mode-map "n"      'next-line)
(defalias 'switch-dir-select 'switch-dir-select-mode)

;; switch-dir-mode defintion
(define-minor-mode switch-dir-mode
  "`switch-dir-mode' is to let you change a directory easily and interactively.
With ARG, turn `switch-dir-mode' on if ARG is positive, off otherwise.

You could make the directory list for using varialbe `switch-dir-alist' to show
and select these easily and interactively.

`switch-dir-mode' automatically load `hl-line-mode' to show and select a 
directory list."
        :global     nil
  :group      'programming
        :init-value nil
        :lighter    " SwDir"
        :keymap     switch-dir-mode-map
        :require    'hl-line
  :version    "0.9")

;; switch-dir-select-mode defintion
(defun switch-dir-select-mode ()
  "Switch the switch-dir and change the directory to it."
  (interactive)
  (switch-split-window-for-alist))

;; Utility functions
(defun switch-split-window-for-alist ()
  "Split the current window and show the switch-dir-alist."
  (split-window-vertically)
  (other-window 1)
  (switch-to-buffer switch-dir-alist-buffer-name)
  (kill-all-local-variables)
        (setq truncate-lines t
        major-mode 'switch-dir-select-mode
        mode-name "Switch-Dir-Select")
        (use-local-map switch-dir-select-mode-map)
        (hl-line-mode t)
  (let ((n 0) (dir-alist) (m 1))
    (dolist (dir-conscell switch-dir-alist)
      (insert (format "%2d %-20s %-60s\n" n (car dir-conscell) (cdr 
dir-conscell)))
      (setq n (1+ n)))
                (toggle-read-only t)
                (setq n 0)
                ; If finding current direcoty from the list, move the cursor to 
this line. 
                (dolist (dir-conscell switch-dir-alist)
                        (if (string=
                                         (expand-file-name (directory-file-name 
default-directory))
                                         (expand-file-name (cdr dir-conscell)))
                                        (setq m (1+ n))
                                (setq n (1+ n))))
                (goto-line m))
        (message "Please select the directory which you want to go, and press 
RETURN.")
        (sit-for 3))

(defun switch-change-dir ()
        (interactive)
        (let ((dir) (n))
                (setq n (1- (line-number-at-pos)))
                (setq dir (cdr (nth n switch-dir-alist)))
                (switch-delete-window-for-alist)
                (cd dir)
                (message (format "Chnaging the directory to %s ..." dir))))
                                
(defun switch-delete-window-for-alist ()
  "Delete the window of the switch-dir-alist."
        (hl-line-mode -1)
  (kill-buffer switch-dir-alist-buffer-name)
  (other-window 1)
  (delete-other-windows))

(provide 'switch-dir)

; cf. which-function-mode
;(sting= (expand-file-name "~/cr/") (expand-file-name "~/cr"))
;(force-mode-line-update))
;(setq source-directory "~/cr/emacs-22.1/src")
;M-x apropos line -> what-line -> line-number-at-pos
;(setq mode-line-format (list mode-line-format 'switch-dir))

PNG image


reply via email to

[Prev in Thread] Current Thread [Next in Thread]