【35歳までに身につけておきたいEmacs】 .emacsとelispでEmacsを便利にする

@orange_cloverへのお勧めのコンピュータ言語は『Lisp』です。
ということなので、Emacslispでもやるか。
その前にまともにEmacsを使えるようにならないと(^ ^;)


便利に使いこなしてる人のを真似るのが一番だと思うので、
雑誌で紹介されてるものをベースにしてカスタマイズしていくことにする。

参考にしたもの

.emacs

こうなった

;;; .emacs
;; base http://bloghackers.net/~naoya/webdb40/files/dot.emacs

;;; uncomment this line to disable loading of "default.el" at startup
;; (setq inhibit-default-init t)

;;; turn on font-lock mode
(when (fboundp 'global-font-lock-mode)
  (global-font-lock-mode t))

;;; enable visual feedback on selections
(setq transient-mark-mode t)

;;; default to better frame titles
(setq frame-title-format
      (concat  "%b - emacs@" (system-name)))

;;; default to unified diffs
(setq diff-switches "-u")

;;; always end a file with a newline
;(setq require-final-newline 'query)


(put 'upcase-region 'disabled nil)

;;;for gdb 6window mode
;;http://narupon.tdiary.net/20061022.html
(setq gdb-many-windows t)
(setq gdb-use-separate-io-buffer t)


;;; -*- mode: lisp-interaction; syntax: lisp;
;; add load path ~/.emacs.d/lisp
(setq load-path
      (append
       (list
        (expand-file-name "~/.emacs.d/lisp/")
        )
       load-path))


;; for UTF-8
;(set-language-environment "Japanese")
;(set-terminal-coding-system 'utf-8)
;(set-keyboard-coding-system 'utf-8)
;(set-buffer-file-coding-system 'utf-8)
;; http://0xcc.net/blog/archives/000041.html
;(setq default-buffer-file-coding-system 'utf-8)

;;;256 color
(require 'color-theme)
(color-theme-initialize)
;(color-theme-tty-dark)
(color-theme-ld-dark)

;;; C-x C-u is nothing to do  (undo of typo is prevented.)
(global-unset-key "\C-x\C-u")

;;; highlight ()
(show-paren-mode 1)

;;; A line feed code is prevented from entering the buffer end.
(setq next-line-add-newlines nil)

;;; C-x l : goto-line
(define-key ctl-x-map "l" 'goto-line)

(display-time)

;;;current line number on modeline
(column-number-mode 1)


;;; C-h:The character in the left of the cursor is erased.
(define-key global-map "\C-h" 'delete-backward-char)

;;; orginal C-h help-command is allocated in C-x C-h
(define-key global-map "\C-x\C-h" 'help-command)

;;; Dynamic abbreviation expand
(define-key global-map "\M-/" 'dabbrev-expand)
(setq dabbrev-case-fold-search nil) ; Capital letters and small letters are distinguished.

;;; The delimitation of sentences of Japanese and an English mixing is judged.
;; http://www.alles.or.jp/~torutk/oojava/meadow/Meadow210Install.html
(defadvice dabbrev-expand
  (around modify-regexp-for-japanese activate compile)
  "Modify `dabbrev-abbrev-char-regexp' dynamically for Japanese words."
  (if (bobp)
      ad-do-it
    (let ((dabbrev-abbrev-char-regexp
           (let ((c (char-category-set (char-before))))
             (cond
              ((aref c ?a) "[-_A-Za-z0-9]") ; ASCII
              ((aref c ?j) ; Japanese
               (cond
                ((aref c ?K) "\\cK") ; katakana
                ((aref c ?A) "\\cA") ; 2byte alphanumeric
                ((aref c ?H) "\\cH") ; hiragana
                ((aref c ?C) "\\cC") ; kanji
                (t "\\cj")))
              ((aref c ?k) "\\ck") ; hankaku-kana
              ((aref c ?r) "\\cr") ; Japanese roman ?
              (t dabbrev-abbrev-char-regexp)))))
      ad-do-it)))

;;; BS :delete selection
(delete-selection-mode 1)

;;; "The local variables list in .emacs" is prevented.
(add-to-list 'ignored-local-variables 'syntax)


;;;line number NG
;(require 'wb-line-number)
;(wb-line-number-toggle)

;;;line number
;;http://www.pshared.net/diary/20080519.html
(require 'linum)
(global-linum-mode t)
(setq linum-format "%5d | ")

;;;backup file directory
;;http://exlight.net/devel/emacs/backup_file/index.html
(setq make-backup-files t)
(setq *backup-directory* "~/.emacs.d/backup")
(if (and (boundp 'backup-directory)
         (not (fboundp 'make-backup-file-name-original)))
    (progn
      (fset 'make-backup-file-name-original
            (symbol-function 'make-backup-file-name))
      (defun make-backup-file-name (filename)
        (if (and (file-exists-p (expand-file-name backup-directory))
                 (file-directory-p (expand-file-name backup-directory)))
            (concat (expand-file-name backup-directory)
                    "/" (file-name-nondirectory filename))
          (make-backup-file-name-original filename)))))


;;; don't make #filename
;;http://d.hatena.ne.jp/TetsuOne/20080625/1214398899
(setq make-backup-files nil)

;;;auto backup
;;http://0xcc.net/misc/auto-save/
;(require 'auto-save-buffers)
;(run-with-idle-timer 2.0 t 'auto-save-buffers)

;;;C mode
(add-hook 'c-mode-hook
          '(lambda ()
               (c-set-style "cc-mode")
;             (c-set-style "GNU")
;             (c-set-style "k&r")
;             (c-set-style "bsd")
;             (c-set-style "whitesmith")
;             (c-set-style "ellemtel")
;             (c-set-stype "linux)

               (setq c-indent-level 4)
               (setq c-tab-width 4)
               (setq tab-width 4)
               (setq c-basic-offset tab-width)
               (setq indent-tabs-mode nil) ;; force only spaces for indentation
           )t)

;;;fold a block of code
;;hs-minor-mode 
;;http://www.bookshelf.jp/pukiwiki/pukiwiki.php?cmd=read&page=Elisp%2Fhideshow.el
;;http://www10.atwiki.jp/csus-shinya/?cmd=word&word=hs-minor-mode%20c-mode&type=normal&page=Debian%20Etch
(add-hook 'c-mode-common-hook
          '(lambda()
             (hs-minor-mode 1)))
(add-hook 'c-mode-common-hook 'hs-minor-mode)
(setq hs-minor-mode-hook
      '(lambda ()
         (local-set-key "\C-c[" 'hs-hide-block)
         (local-set-key "\C-c]" 'hs-show-block)))



;;;function list
;;http://www.bookshelf.jp/soft/meadow_42.html#SEC637
;;http://www.ne.jp/asahi/love/suna/pub/soft/navi.el/index.html
(load-library "navi")
(global-set-key [f11] 'call-navi)
(global-set-key "\C-x\C-l" 'call-navi)
(defun call-navi ()
  (interactive)
  (navi (buffer-name)))

;;;current function on mode line
;;http://www.bookshelf.jp/texi/emacs-man/21-3/jp/emacs_25.html#SEC288
(which-function-mode 1)

;;memo
;;http://0xcc.net/unimag/1/
(defun memo ()
  (interactive)
    (add-change-log-entry
     nil
     (expand-file-name "~/memo.txt")))
(define-key ctl-x-map "M" 'memo)

;;;coloring TAB ,2byte space,end of line space
;;http://sheepman.sakura.ne.jp/diary/?date=20050131#p02
(defface my-face-r-1 '((t (:background "color-233"))) nil)
(defadvice font-lock-mode (before my-font-lock-mode ())
(defvar my-face-r-1 'my-face-r-1)
    (font-lock-add-keywords
     major-mode
     '(("\t" 0 my-face-r-1 append)
       (" " 0 my-face-r-1 append)
       ("[ \t]+$" 0 my-face-r-1 append)
        ;;("[\r]*\n" 0 my-face-r-1 append)
       )))
(ad-enable-advice 'font-lock-mode 'before 'my-font-lock-mode)
(ad-activate 'font-lock-mode)

;;;install-elisp
;;Software Design 2009/05 
(require 'install-elisp)
(setq install-elisp-repository-directory "~/.emacs.d/lisp")

;;;Setting that distinguishes Japanese
;;Software Design 2009/06
(setq grep-host-defaults-alist nil);;
(setq grep-template "lgrep <C> -n <R> <F> <N>")
(setq grep-find-template "find . <X> -type f <F> -print0 |xargs -0 -e lgrep <C> -n <R> <N>")

;;;grep and edit
;;Software Design 2009/05
(require 'grep-edit)

;;;locate
;;Software Design 2009/05
(require 'locate)
 (defvar locate-db-name "locate.db")
 (setq locate-command "/usr/local/bin/locate")

 ;;common function for building locate command line
 (defun locate-make-command-line (search-string dbpath &rest opts)
   (append
    ;;ignore the case
    (list locate-command "-i")
    (when (and dbpath (file-exists-p dbpath))
      (list "-d" dbpath))
    opts
    (list search-string)))

 ;;locate command line for home
 (defun home-locate-make-command-line (search-string &optional &rest opts)
   (apply 'locate-make-command-line
          search-string
          (expand-file-name locate-db-name "~")
          opts))
 ;;for default locate
 (setq locate-make-command-line 'home-locate-make-command-line)

;;function for search specified directory
(defun find-file-upward (name &optional dir)
  (setq dir (file-name-as-directory (or dir default-directory)))
  (cond
   ((string= dir (directory-file-name dir))
    nil)
   ((file-exists-p name)
    (expand-file-name name dir))
   (t
    (find-file-upward name (expand-file-name ".." dir)))))


;;locate command line for locate db of each projects
(defun plocate-make-command-line (search-string &optional &rest opts)
  (apply 'locate-make-command-line
         search-string
         (find-file-upward locate-db-name)
         opts))

;;locate command for locate db of each projects
(defun plocate (search-string &optional arg)
  (interactive
   (list
    (locate-prompt-for-search-string)
    current-prefix-arg))
  (let ((locate-make-command-line 'plocate-make-command-line))
    (locate search-string nil arg)))



;;ant
;;;Software Design 2009/05
(defvar ant-command-history nil)
(defun ant ()
  (interactive)
  (let ((build-xml (find-file-upward "build.xml"))
        command)
    (unless build-xml
      (error "cannot found build.xml"))
    (set q command (read-string "Ant: "
                                (concat "ant -f " build-xml " -e")
                                'ant-coomand-history))
    (let ((default-directory (file-name-directory build-xml)))
      (compile command))))

;;save session
;;;Web + DB PRESS Vol40 Linux Development chapter3 emacs
(require 'session)
(add-hook 'after-init-hook 'session-initialize)

;;Text Web Browser
;;;Web + DB PRESS Vol40 Linux Development chapter3 emacs
(require 'w3m-load)


;;Highlight strings refered dabbrev-expand
;;;Web + DB PRESS Vol40 Linux Development chapter3 emacs
(require 'dabbrev-highlight)

;;Dynamic Supplement
;;;Web + DB PRESS Vol40 Linux Development chapter3 emacs
(define-key global-map"\C-o" 'dabbrev-expand)

;;GNU Global
;;;Software Design 2009/06
;;;http://dev.ariel-networks.com/articles/emacs/part2
(require 'gtags)
(add-hook 'java-mode-hook (lambda () (gtags-mode 1)))
(add-hook 'c-mode-hook (lambda () (gtags-mode 1)))
(add-hook 'c++-mode-hook (lambda () (gtags-mode 1)))

;;open anything
;;;Software Design 2009/06
;;;http://dev.ariel-networks.com/articles/emacs/part2
(require 'anything)
(require 'anything-config)
(require 'anything-match-plugin)
(require 'anything-gtags)
;;;switch-to-buffer -> anything
(global-set-key (kbd "C-x b") 'anything)

(setq anything-sources
      '(anything-c-source-buffers+
        anything-c-source-file-name-history
        anything-c-source-buffer-not-found
        anything-c-source-imenu
        anything-c-source-gtags-select
        anything-c-source-plocate
        anything-c-source-home-locate
        anything-c-source-kill-ring
        ))
;;auto index for imenu
(setq imenu-auto-rescan t)



(defvar anything-c-source-home-locate
  '((name . "Home Locate")
    (candidates . (lambda ()
                    (apply 'start-process "anything-home-locate-process" nil
                           (home-locate-make-command-line anything-pattern "-r"))))
    (type . file)
    (requires-pattern . 3)
    (delayed)))

(defvar anything-c-source-plocate
  '((name . "Project Locate")
    (candidates
     . (lambda ()
         (let ((default-directory
                 (with-current-buffer anything-current-buffer default-directory)))
           (apply 'start-process "anything-plocate-process" nil
                  (plocate-make-command-line anything-pattern "-r")))))
    (type . file)
    (requires-pattern . 3)
    (delayed)))

;;candidates-file  plug-in
;;;http://d.hatena.ne.jp/rubikitch/20090211/1234349678
(defun anything-compile-source--candidates-file (source)
  (if (assoc-default 'candidates-file source)
      `((init acf-init
              ,@(let ((orig-init (assoc-default 'init source)))
                  (cond ((null orig-init) nil)
                        ((functionp orig-init) (list orig-init))
                        (t orig-init))))
        (candidates-in-buffer)
        ,@source)
    source))
(add-to-list 'anything-compile-source-functions 'anything-compile-source--candidates-file)

(defun acf-init ()
  (destructuring-bind (file &optional updating)
      (anything-mklist (anything-attr 'candidates-file))
    (with-current-buffer (anything-candidate-buffer (find-file-noselect file))
      (when updating
        (buffer-disable-undo)
        (font-lock-mode -1)
        (auto-revert-mode 1)))))

(defvar anything-c-source-home-directory
  '((name . "Home directory")
    ;; /log/home.filelist has described the file name of one a line of the home directory.
    (candidates-file "~/.home.filelist" updating)
    (requires-pattern . 5)
    (candidate-number-limit . 20)
    (type . file)))

(defvar anything-c-source-find-library
  '((name . "Elisp libraries")
    ;; All emacs Lisp file
    (candidates-file "~/.elisp.filelist" updating)
    (requires-pattern . 4)
    (type . file)
    (major-mode emacs-lisp-mode)))

;;easy copy & past
;;;Software Design 2009/08
(require 'browse-kill-ring)
(browse-kill-ring-default-keybindings)

(defun anything-kill-ring ()
  (interactive)
  (anything 'anything-c-source-kill-ring nil nil nil nil "*anything kill ring*"))

(defadvice yank-pop (around anything-kill-ring-maybe activate)
  (if (not (eq last-command 'yank))
      (anything-kill-ring)
    ad-do-it))

;;format
;;Software Design 2009/09
(require 'org)

;;;square copy & past
;;;Software Design 2009/09
;;;ex
;;;C-SPC -> region select -> C-SPC
(require 'sense-region)
(sense-region-on)

;;square edit
;;;Software Design 2009/09
;;;ex foo1 -> foo9
;;;C-u 10
;;;goto first line
;;;M-x cua-set-rectangle-mark
;;;C-n,select 10 line
;;;M-n
;;;Start value: (0)1
;;;Increment: (1)1
;;;Format: (%d) foo%d
(cua-mode t)
;;;It is obstructed that C-c and C-v are replaced.
(setq cua-enable-cua-keys nil)


;;select region and isearch
;;;Software Design 2009/09
(defadvice isearch-mode
  (around isearch-mode-default-string (forward &optional regexp op-funrecursive-edit word-p) activate)
  (if (and transient-mark-mode mark-active (not (eq (mark) (point))))
      (progn
        (isearch-update-ring (buffer-substring-no-properties (mark) (point)))
        (deactivate-mark)
        ad-do-it
        (if (not forward)
            (isearch-repeat-backward)
          (goto-char (mark))
          (isearch-repeat-forward)))
    ad-do-it))

;;thing (like Vim text object)
;;;Software Design 2009/09
(require 'thing-opt)
(define-thing-commands)
(global-set-key (kbd "C-$") 'mark-word*)
(global-set-key (kbd "C-\"") 'mark-string)
(global-set-key (kbd "C-(") 'mark-up-list)

(require 'xcscope)

;;pukiwik-mode
;;;http://d.hatena.ne.jp/higepon/20060919/1158674787
(setq pukiwiki-auto-insert t)
(autoload 'pukiwiki-edit "pukiwiki-mode" nil t)
(autoload 'pukiwiki-index "pukiwiki-mode" nil t)
(autoload 'pukiwiki-edit-url "pukiwiki-mode" nil t)
(setq pukiwiki-browser-function 'browse-url)
(setq pukiwiki-diff-using-ediff t)

(defadvice pukiwiki-pukiwiki-version-check (around pukiwiki-pukiwiki-version-check-dummy)
  "always pukiwiki 1.4.6"
  (setq pukiwiki-1-3-p nil)
  (setq pukiwiki-fetch-index-regexp pukiwiki-fetch-index-regexp-1.4.6))
(ad-deactivate-regexp "pukiwiki-pukiwiki-version-check-dummy")

インストールしたlisp

  • anything-config.el
  • anything-gtags.el
  • anything-gtags.el
  • anything-match-plugin.el
  • anything.el
  • auto-save-buffers.el
  • browse-kill-ring.el
  • color-theme.el
  • dabbrev-expand-multiple.el
  • vdabbrev-highlight.el
  • grep-edit.el
  • gtags.el
  • install-elisp.el
  • iswitchb.el
  • kill-summary.el
  • linum.el
  • minibuf-isearch.el
  • navi.dot.emacs
  • navi.el
  • pukiwiki-mode.el
  • sense-region.el
  • session.el
  • themes/
  • thing-opt.el
  • xcscope.el

WEB+DB PRESS Vol.40
WEB+DB PRESS Vol.40
posted with amazlet at 10.04.30

技術評論社
売り上げランキング: 259728