25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

2331 lines
79 KiB

  1. ;;; emacs.el --- 10sr emacs initialization
  2. ;;; Commentary:
  3. ;;; Code:
  4. ;; (and (file-readable-p "~/.dotfiles/emacs.el")
  5. ;; (load-file "~/.dotfiles/emacs.el"))
  6. ;; make directories
  7. (unless (file-directory-p (expand-file-name user-emacs-directory))
  8. (make-directory (expand-file-name user-emacs-directory)))
  9. (let ((d (expand-file-name (concat user-emacs-directory
  10. "lisp"))))
  11. (unless (file-directory-p d)
  12. (make-directory d))
  13. (add-to-list 'load-path d))
  14. (eval-when-compile
  15. (require 'cl nil t))
  16. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  17. ;; download library from web
  18. (defun fetch-library (url &optional byte-compile-p force-download-p)
  19. "If library does not exist, download it from URL and locate it in
  20. \"~/emacs.d/lisp/\". Return nil if library unfound and failed to download,
  21. otherwise the path where the library installed."
  22. (let* ((dir (expand-file-name (concat user-emacs-directory "lisp/")))
  23. (lib (file-name-sans-extension (file-name-nondirectory url)))
  24. (lpath (concat dir lib ".el"))
  25. (locate-p (locate-library lib)))
  26. (if (or force-download-p (not locate-p))
  27. (if (progn (message "Downloading %s..."
  28. url)
  29. (download-file url
  30. lpath
  31. t))
  32. (progn (message "Downloading %s...done"
  33. url)
  34. (when (and byte-compile-p
  35. (require 'bytecomp nil t))
  36. (and (file-exists-p (byte-compile-dest-file lpath))
  37. (delete-file (byte-compile-dest-file lpath)))
  38. (byte-compile-file lpath)))
  39. (progn (and (file-writable-p lpath)
  40. (delete-file lpath))
  41. (message "Downloading %s...failed"
  42. url))))
  43. (locate-library lib)))
  44. (defun download-file (url path &optional ok-if-already-exists)
  45. "Download file from URL and output to PATH."
  46. (or
  47. (let ((curl (executable-find "curl")))
  48. (when curl
  49. (if (and (not ok-if-already-exists)
  50. (file-exists-p path))
  51. nil
  52. (and (eq 0
  53. (call-process curl
  54. nil
  55. nil
  56. nil
  57. "--output"
  58. path
  59. url
  60. ))
  61. path))))
  62. (ignore-errors
  63. (require 'url)
  64. (url-copy-file url
  65. path
  66. ok-if-already-exists)
  67. path)))
  68. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  69. ;; package
  70. (when (require 'package nil t)
  71. (add-to-list 'package-archives
  72. '("ELPA" . "http://tromey.com/elpa/"))
  73. (add-to-list 'package-archives
  74. '("melpa" . "http://melpa.milkbox.net/packages/")
  75. t)
  76. (add-to-list 'package-archives
  77. '("marmalade" . "http://marmalade-repo.org/packages/"))
  78. (package-initialize)
  79. ;; (package-refresh-contents)
  80. )
  81. (defun my-auto-install-package ()
  82. "Install packages semi-automatically."
  83. (interactive)
  84. (mapc (lambda (pkg)
  85. (or (package-installed-p pkg)
  86. (locate-library (symbol-name pkg))
  87. (package-install pkg)))
  88. '(
  89. markdown-mode
  90. yaml-mode
  91. ;; ack
  92. color-moccur
  93. )))
  94. ;; (lazy-load-eval 'sudoku)
  95. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  96. ;; autoload
  97. (defmacro lazy-load-eval (feature &optional functions &rest body)
  98. "Define autoloading FEATURE that defines FUNCTIONS.
  99. FEATURE is a symbol. FUNCTIONS is a list of symbols. If FUNCTIONS is nil,
  100. the function same as FEATURE is defined as autoloaded function. BODY is passed
  101. to `eval-after-load'.
  102. When this macro is evaluated, this returns the path to library if FEATURE
  103. found, otherwise returns nil."
  104. (let* ((libname (symbol-name (eval feature)))
  105. (libpath (locate-library libname)))
  106. (and libpath
  107. `(progn
  108. ,@(mapcar (lambda (f)
  109. (unless (fboundp f)
  110. `(progn
  111. (message "Autoloaded function `%S' defined (%s)"
  112. (quote ,f)
  113. ,libpath)
  114. (autoload (quote ,f)
  115. ,libname
  116. ,(concat "Autoloaded function defined in \""
  117. libpath
  118. "\".")
  119. t))))
  120. (or (eval functions)
  121. `(,(eval feature))))
  122. (eval-after-load ,feature
  123. (quote (progn
  124. ,@body)))
  125. ,libpath))))
  126. (put 'lazy-load-eval 'lisp-indent-function 2)
  127. (when (lazy-load-eval 'tetris nil
  128. (message "Tetris loaded!"))
  129. (message "Tetris found!"))
  130. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  131. ;; my-idle-hook
  132. (defvar my-idle-hook nil
  133. "Hook run when idle for several secs.")
  134. (defvar my-idle-hook-sec 5
  135. "Second to run `my-idle-hook'.")
  136. (run-with-idle-timer my-idle-hook-sec
  137. t
  138. (lambda ()
  139. (run-hooks 'my-idle-hook)))
  140. ;; (add-hook 'my-idle-hook
  141. ;; (lambda ()
  142. ;; (message "idle hook message")))
  143. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  144. ;; start and quit
  145. (setq inhibit-startup-message t)
  146. (setq confirm-kill-emacs 'y-or-n-p)
  147. (setq gc-cons-threshold (* 1024 1024 4))
  148. (when window-system
  149. (add-to-list 'default-frame-alist '(cursor-type . box))
  150. (add-to-list 'default-frame-alist '(background-color . "white"))
  151. (add-to-list 'default-frame-alist '(foreground-color . "gray10"))
  152. ;; (add-to-list 'default-frame-alist '(alpha . (80 100 100 100)))
  153. ;; does not work?
  154. )
  155. ;; (add-to-list 'default-frame-alist '(cursor-type . box))
  156. (if window-system (menu-bar-mode 1) (menu-bar-mode 0))
  157. (and (fboundp 'tool-bar-mode)
  158. (tool-bar-mode 0))
  159. (and (fboundp 'set-scroll-bar-mode)
  160. (set-scroll-bar-mode nil))
  161. (add-hook 'kill-emacs-hook
  162. ;; load init file when terminating emacs to ensure file is not broken
  163. 'reload-init-file)
  164. (add-hook 'after-init-hook
  165. (lambda ()
  166. (message "%s %s" invocation-name emacs-version)
  167. (message "%s was taken to initialize emacs." (emacs-init-time))
  168. (switch-to-buffer "*Messages*")
  169. ))
  170. (cd ".") ; when using windows use / instead of \ in `default-directory'
  171. ;; locale
  172. (set-language-environment "Japanese")
  173. (set-default-coding-systems 'utf-8-unix)
  174. (prefer-coding-system 'utf-8-unix)
  175. (setq system-time-locale "C")
  176. ;; my prefix map
  177. (defvar my-prefix-map nil
  178. "My prefix map.")
  179. (define-prefix-command 'my-prefix-map)
  180. (define-key ctl-x-map (kbd "C-x") 'my-prefix-map)
  181. (define-key my-prefix-map (kbd "C-q") 'quoted-insert)
  182. (define-key my-prefix-map (kbd "C-z") 'suspend-frame)
  183. ;; (comint-show-maximum-output)
  184. ;; kill scratch
  185. (add-hook 'after-init-hook
  186. (lambda ()
  187. (kill-buffer "*scratch*")))
  188. ;; modifier keys
  189. ;; (setq mac-option-modifier 'control)
  190. ;; display
  191. (setq redisplay-dont-pause t)
  192. (setq visible-bell t)
  193. (setq ring-bell-function 'ignore)
  194. (mouse-avoidance-mode 'banish)
  195. (and window-system
  196. (fetch-library
  197. "https://raw.github.com/10sr/emacs-lisp/master/save-window-size.el"
  198. t)
  199. (require 'save-window-size nil t))
  200. (defun reload-init-file ()
  201. "Reload Emacs init file."
  202. (interactive)
  203. (when (file-readable-p user-init-file)
  204. (load-file user-init-file)))
  205. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  206. ;; for windows
  207. (defun start-ckw-bash ()
  208. "Start ckw in windows."
  209. (interactive)
  210. (start-process
  211. "ckw_bash"
  212. nil
  213. "C:/Documents and Settings/sr/Application Data/dbx/apps/ckw/ckw.exe"))
  214. ;; command seems to have to be in c drive
  215. (defun my-w32-add-export-path (&rest args)
  216. "Add pathes ARGS for windows."
  217. (mapc (lambda (path)
  218. (add-to-list 'exec-path (expand-file-name path)))
  219. (reverse args))
  220. (setenv "PATH"
  221. (mapconcat 'convert-standard-filename
  222. exec-path
  223. ";")))
  224. (when (eq system-type 'windows-nt)
  225. ;; (setq scheme-program-name "\"c:/Program Files/Gauche/bin/gosh.exe\" -i")
  226. ;; (setq python-python-command "c:/Python26/python.exe")
  227. ;; (define-key my-prefix-map (kbd "C-c") 'start-ckw-bash)
  228. (my-w32-add-export-path "c:/Windows/system"
  229. "c:/Windows/System32"
  230. "c:/Program Files/Git/bin"
  231. "c:/MinGW/bin"
  232. "c:/MinGW/mingw32/bin"
  233. (expand-file-name "~/.local/bin")
  234. (expand-file-name "~/dbx/apps/bin"))
  235. (when window-system
  236. (setq w32-enable-synthesized-fonts t))
  237. (setq w32-apps-modifier 'meta)
  238. (setq file-name-coding-system 'sjis))
  239. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  240. ;; global keys
  241. (global-set-key (kbd "<up>") (lambda() (interactive) (scroll-down 1)))
  242. (global-set-key (kbd "<down>") (lambda() (interactive) (scroll-up 1)))
  243. (global-set-key (kbd "<left>") 'scroll-down)
  244. (global-set-key (kbd "<right>") 'scroll-up)
  245. ;; (define-key my-prefix-map (kbd "C-h") help-map)
  246. (global-set-key (kbd "C-\\") help-map)
  247. (define-key ctl-x-map (kbd "DEL") help-map)
  248. (define-key ctl-x-map (kbd "C-h") help-map)
  249. (define-key help-map "a" 'apropos)
  250. ;; disable annoying keys
  251. (global-set-key [prior] 'ignore)
  252. (global-set-key (kbd "<next>") 'ignore)
  253. (global-set-key [menu] 'ignore)
  254. (global-set-key [down-mouse-1] 'ignore)
  255. (global-set-key [down-mouse-2] 'ignore)
  256. (global-set-key [down-mouse-3] 'ignore)
  257. (global-set-key [mouse-1] 'ignore)
  258. (global-set-key [mouse-2] 'ignore)
  259. (global-set-key [mouse-3] 'ignore)
  260. (global-set-key (kbd "<eisu-toggle>") 'ignore)
  261. (global-set-key (kbd "C-<eisu-toggle>") 'ignore)
  262. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  263. ;; title and mode-line
  264. (when (fetch-library
  265. "https://raw.github.com/10sr/emacs-lisp/master/terminal-title.el"
  266. t)
  267. (require 'terminal-title nil t)
  268. ;; if TERM is not screen use default value
  269. (if (getenv "TMUX")
  270. ;; if use tmux locally just basename of current dir
  271. (setq terminal-title-format
  272. '((file-name-nondirectory (directory-file-name
  273. default-directory))))
  274. (when (equal (car (split-string (frame-parameter nil
  275. 'tty-type)
  276. "-"))
  277. "screen")
  278. (if (getenv "SSH_CONNECTION")
  279. ;; TMUX is not set but TERM is screen:
  280. ;; it seems that TMUX is used by locally and ssh to remote host
  281. (setq terminal-title-format
  282. `("em:"
  283. ,user-login-name
  284. "@"
  285. ,(car (split-string system-name
  286. "\\."))
  287. ":"
  288. (file-name-nondirectory (directory-file-name
  289. default-directory))))
  290. ;; this wont happen? (TMUX is not set, TERM is screen, not ssh-ed)
  291. (setq terminal-title-format
  292. '((file-name-nondirectory (directory-file-name
  293. default-directory)))))))
  294. (terminal-title-mode))
  295. (setq eol-mnemonic-dos "\\r\\n")
  296. (setq eol-mnemonic-mac "\\r")
  297. (setq eol-mnemonic-unix "\\n")
  298. (which-function-mode 0)
  299. (line-number-mode 0)
  300. (column-number-mode 0)
  301. (size-indication-mode 0)
  302. (setq mode-line-position
  303. '(:eval (format "L%%l/%d,C%%c"
  304. (count-lines (point-max)
  305. (point-min)))))
  306. ;; http://www.geocities.jp/simizu_daisuke/bunkei-meadow.html#frame-title
  307. ;; display date
  308. (add-hook 'after-init-hook
  309. (lambda ()
  310. (when display-time-mode
  311. (display-time-update))
  312. ))
  313. (when (require 'time nil t)
  314. (setq display-time-interval 29)
  315. (setq display-time-day-and-date t)
  316. (setq display-time-format "%a, %d %b %Y %T")
  317. (if window-system
  318. (display-time-mode 0)
  319. (display-time-mode 1)))
  320. ;; ;; current directory
  321. ;; (let ((ls (member 'mode-line-buffer-identification
  322. ;; mode-line-format)))
  323. ;; (setcdr ls
  324. ;; (cons '(:eval (concat " ("
  325. ;; (abbreviate-file-name default-directory)
  326. ;; ")"))
  327. ;; (cdr ls))))
  328. ;; ;; display last modified time
  329. ;; (let ((ls (member 'mode-line-buffer-identification
  330. ;; mode-line-format)))
  331. ;; (setcdr ls
  332. ;; (cons '(:eval (concat " "
  333. ;; my-buffer-file-last-modified-time))
  334. ;; (cdr ls))))
  335. (defun buffer-list-not-start-with-space ()
  336. "Return a list of buffers that not start with whitespaces."
  337. (let ((bl (buffer-list))
  338. b nbl)
  339. (while bl
  340. (setq b (pop bl))
  341. (unless (string-equal " "
  342. (substring (buffer-name b)
  343. 0
  344. 1))
  345. (add-to-list 'nbl b)))
  346. nbl))
  347. ;; http://www.masteringemacs.org/articles/2012/09/10/hiding-replacing-modeline-strings/
  348. ;; (add-to-list 'minor-mode-alist
  349. ;; '(global-whitespace-mode ""))
  350. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  351. ;; system info
  352. (defun my-message-current-info ()
  353. "Echo current login name, hostname and directory."
  354. (interactive)
  355. (message "%s@%s:%s"
  356. user-login-name
  357. system-name
  358. (abbreviate-file-name default-directory)))
  359. ;; (run-with-idle-timer 3
  360. ;; t
  361. ;; 'my-message-current-info)
  362. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  363. ;; minibuffer
  364. (setq insert-default-directory t)
  365. (setq completion-ignore-case t
  366. read-file-name-completion-ignore-case t
  367. read-buffer-completion-ignore-case t)
  368. (setq resize-mini-windows t)
  369. (temp-buffer-resize-mode 1)
  370. (savehist-mode 1)
  371. (fset 'yes-or-no-p 'y-or-n-p)
  372. ;; complete symbol when `eval'
  373. (define-key read-expression-map (kbd "TAB") 'lisp-complete-symbol)
  374. (define-key minibuffer-local-map (kbd "C-u")
  375. (lambda () (interactive) (delete-region (point-at-bol) (point))))
  376. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  377. ;; letters, font-lock mode and fonts
  378. ;; (set-face-background 'vertical-border (face-foreground 'mode-line))
  379. ;; (set-window-margins (selected-window) 1 1)
  380. (and (or (eq system-type 'Darwin)
  381. (eq system-type 'darwin))
  382. (fboundp 'mac-set-input-method-parameter)
  383. (mac-set-input-method-parameter 'japanese 'cursor-color "red")
  384. (mac-set-input-method-parameter 'roman 'cursor-color "black"))
  385. (when (and (boundp 'input-method-activate-hook) ; i dont know this is correct
  386. (boundp 'input-method-inactivate-hook))
  387. (add-hook 'input-method-activate-hook
  388. (lambda () (set-cursor-color "red")))
  389. (add-hook 'input-method-inactivate-hook
  390. (lambda () (set-cursor-color "black"))))
  391. (when (require 'paren nil t)
  392. (show-paren-mode 1)
  393. (setq show-paren-delay 0.5
  394. show-paren-style 'parenthesis) ; mixed is hard to read
  395. (set-face-background 'show-paren-match
  396. (face-foreground 'default))
  397. (set-face-inverse-video-p 'show-paren-match
  398. t))
  399. (transient-mark-mode 1)
  400. (global-font-lock-mode 1)
  401. (setq font-lock-global-modes
  402. '(not
  403. help-mode
  404. eshell-mode
  405. term-mode
  406. Man-mode))
  407. ;; (standard-display-ascii ?\n "$\n")
  408. (defvar my-eol-face
  409. '(("\n" . (0 font-lock-comment-face t nil)))
  410. )
  411. (defvar my-tab-face
  412. '(("\t" . '(0 highlight t nil))))
  413. (defvar my-jspace-face
  414. '(("\u3000" . '(0 highlight t nil))))
  415. (add-hook 'font-lock-mode-hook
  416. (lambda ()
  417. ;; (font-lock-add-keywords nil my-eol-face)
  418. (font-lock-add-keywords nil my-jspace-face)
  419. ))
  420. (when (require 'whitespace nil t)
  421. (add-to-list 'whitespace-display-mappings ; not work
  422. `(tab-mark ?\t ,(vconcat "^I\t")))
  423. (add-to-list 'whitespace-display-mappings
  424. `(newline-mark ?\n ,(vconcat "$\n")))
  425. (setq whitespace-style '(face
  426. trailing ; trailing blanks
  427. newline ; newlines
  428. newline-mark ; use display table for newline
  429. ;; tab-mark
  430. empty ; empty lines at beg or end of buffer
  431. lines-tail ; lines over 80
  432. ))
  433. ;; (setq whitespace-newline 'font-lock-comment-face)
  434. (global-whitespace-mode t)
  435. (when (eq (display-color-cells)
  436. 256)
  437. (set-face-foreground 'whitespace-newline "brightblack")))
  438. (and nil
  439. (fetch-library
  440. "http://www.emacswiki.org/emacs/download/fill-column-indicator.el"
  441. t)
  442. (require 'fill-column-indicator nil t)
  443. (setq fill-column-indicator))
  444. ;; highlight current line
  445. ;; http://wiki.riywo.com/index.php?Meadow
  446. (defface my-hl-line
  447. '((((min-colors 256)
  448. (background dark))
  449. (:background "color-234"))
  450. (((min-colors 256)
  451. (background light))
  452. (:background "color-234"))
  453. (t
  454. (:underline "black")))
  455. "*Face used by hl-line.")
  456. (setq hl-line-face 'my-hl-line) ;; (setq hl-line-face nil)
  457. (global-hl-line-mode 1) ;; (hl-line-mode 1)
  458. (setq hl-line-global-modes
  459. '(not
  460. term-mode))
  461. (set-face-foreground 'font-lock-regexp-grouping-backslash "#666")
  462. (set-face-foreground 'font-lock-regexp-grouping-construct "#f60")
  463. ;; fonts
  464. (defun my-set-ascii-and-jp-font (list)
  465. "Set font configuration List."
  466. (let ((fspec1 (if (> emacs-major-version 22)
  467. ;; font spec is available in emacs23 and later
  468. (font-spec :family (nth 2 list) :size (nth 3 list))
  469. (cons (nth 2 list) "jisx0208.*")))
  470. (fspec2 (if (> emacs-major-version 22)
  471. (font-spec :family (nth 2 list) :size (nth 3 list))
  472. (cons (nth 2 list) "jisx0201.*"))))
  473. (set-face-attribute 'default nil
  474. :family (nth 0 list)
  475. :height (nth 1 list))
  476. (set-fontset-font "fontset-default"
  477. 'japanese-jisx0208
  478. fspec1)
  479. (set-fontset-font "fontset-default"
  480. 'katakana-jisx0201
  481. fspec2)))
  482. ;; (my-set-ascii-and-jp-font '("dejavu sans mono" 90 "takaogothic" 13))
  483. ;; (my-set-ascii-and-jp-font '("dejavu sans mono" 100 "takaogothic" 14))
  484. ;; (my-set-ascii-and-jp-font '("dejavu sans mono" 100 "ms gothic" 14))
  485. ;; (my-set-ascii-and-jp-font '("monaco" 75 "takaogothic" 11))
  486. ;; (my-set-ascii-and-jp-font '("monaco" 90 "takaogothic" 13))
  487. ;; (my-set-ascii-and-jp-font '("ProggyCleanTTSZ" 120 "takaogothic" 11))
  488. ;; あ a
  489. (and (fetch-library
  490. "https://raw.github.com/10sr/emacs-lisp/master/set-modeline-color.el"
  491. t)
  492. (progn
  493. (require 'set-modeline-color nil t)))
  494. (let ((fg (face-foreground 'default))
  495. (bg (face-background 'default)))
  496. (set-face-background 'mode-line-inactive
  497. (if (face-inverse-video-p 'mode-line) fg bg))
  498. (set-face-foreground 'mode-line-inactive
  499. (if (face-inverse-video-p 'mode-line) bg fg)))
  500. (set-face-underline 'mode-line-inactive
  501. t)
  502. (set-face-underline 'vertical-border
  503. nil)
  504. (and (fetch-library
  505. "https://raw.github.com/tarao/elisp/master/end-mark.el"
  506. t)
  507. (require 'end-mark nil t)
  508. (global-end-mark-mode))
  509. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  510. ;; file handling
  511. (setq revert-without-query '(".+"))
  512. ;; save cursor position
  513. (setq save-place-file (concat user-emacs-directory
  514. "places"))
  515. (when (require 'saveplace nil t)
  516. (setq-default save-place t))
  517. ;; http://www.bookshelf.jp/soft/meadow_24.html#SEC260
  518. (setq make-backup-files t)
  519. ;; (make-directory (expand-file-name "~/.emacsbackup"))
  520. (setq backup-directory-alist
  521. (cons (cons "\\.*$" (expand-file-name "~/.emacs.d/backup"))
  522. backup-directory-alist))
  523. (setq version-control 'never)
  524. (setq delete-old-versions t)
  525. (setq auto-save-list-file-prefix (expand-file-name "~/.emacs.d/auto-save/"))
  526. (setq delete-auto-save-files t)
  527. (add-to-list 'completion-ignored-extensions ".bak")
  528. ;; (setq delete-by-moving-to-trash t
  529. ;; trash-directory "~/.emacs.d/trash")
  530. (add-hook 'after-save-hook
  531. 'executable-make-buffer-file-executable-if-script-p)
  532. (setq bookmark-default-file "~/.emacs.d/bmk")
  533. (add-hook 'recentf-load-hook
  534. (lambda ()
  535. (add-to-list 'recentf-exclude
  536. (regexp-quote bookmark-default-file))))
  537. (and (fetch-library
  538. "https://raw.github.com/10sr/emacs-lisp/master/read-only-only-mode.el"
  539. t)
  540. (lazy-load-eval 'read-only-only-mode))
  541. (and (fetch-library
  542. "https://raw.github.com/10sr/emacs-lisp/master/smart-revert.el"
  543. t)
  544. (require 'smart-revert nil t)
  545. (smart-revert-on))
  546. ;; autosave
  547. (and (fetch-library
  548. "https://raw.github.com/10sr/emacs-lisp/master/autosave.el"
  549. t)
  550. (require 'autosave nil t)
  551. (autosave-set 2))
  552. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  553. ;; editting
  554. (defun my-copy-whole-line ()
  555. "Copy whole line."
  556. (interactive)
  557. (kill-new (concat (buffer-substring (point-at-bol)
  558. (point-at-eol))
  559. "\n")))
  560. (setq require-final-newline t)
  561. (setq kill-whole-line t)
  562. (setq scroll-conservatively 35
  563. scroll-margin 2
  564. scroll-step 0)
  565. (setq-default major-mode 'text-mode)
  566. (setq next-line-add-newlines nil)
  567. (setq kill-read-only-ok t)
  568. (setq truncate-partial-width-windows nil) ; when splitted horizontally
  569. ;; (setq-default line-spacing 0.2)
  570. (setq-default indicate-empty-lines t) ; when using x indicate empty line
  571. (setq-default tab-width 4)
  572. (setq-default indent-tabs-mode nil)
  573. (setq-default indent-line-function nil)
  574. ;; (pc-selection-mode 1) ; make some already defined keybind back to default
  575. (delete-selection-mode 1)
  576. (cua-mode 0)
  577. (setq line-move-visual nil)
  578. ;; key bindings
  579. ;; moving around
  580. ;; (global-set-key (kbd "M-j") 'next-line)
  581. ;; (global-set-key (kbd "M-k") 'previous-line)
  582. ;; (global-set-key (kbd "M-h") 'backward-char)
  583. ;; (global-set-key (kbd "M-l") 'forward-char)
  584. ;;(keyboard-translate ?\M-j ?\C-j)
  585. ;; (global-set-key (kbd "M-p") 'backward-paragraph)
  586. (define-key esc-map "p" 'backward-paragraph)
  587. ;; (global-set-key (kbd "M-n") 'forward-paragraph)
  588. (define-key esc-map "n" 'forward-paragraph)
  589. (global-set-key (kbd "C-<up>") (lambda () (interactive)(scroll-down 1)))
  590. (global-set-key (kbd "C-<down>") (lambda () (interactive)(scroll-up 1)))
  591. (global-set-key (kbd "C-<left>") 'scroll-down)
  592. (global-set-key (kbd "C-<right>") 'scroll-up)
  593. (global-set-key (kbd "<select>") 'ignore) ; 'previous-line-mark)
  594. (define-key ctl-x-map (kbd "ESC x") 'execute-extended-command)
  595. (define-key ctl-x-map (kbd "ESC :") 'eval-expression)
  596. ;; C-h and DEL
  597. (global-set-key (kbd "C-h") (kbd "DEL"))
  598. (global-set-key (kbd "C-m") 'reindent-then-newline-and-indent)
  599. (global-set-key (kbd "C-o") (kbd "C-e C-m"))
  600. (define-key esc-map "k" 'my-copy-whole-line)
  601. ;; (global-set-key "\C-z" 'undo) ; undo is M-u
  602. (define-key esc-map "u" 'undo)
  603. (define-key esc-map "i" (kbd "ESC TAB"))
  604. ;; (global-set-key (kbd "C-r") 'query-replace-regexp)
  605. (global-set-key (kbd "C-s") 'isearch-forward-regexp)
  606. (global-set-key (kbd "C-r") 'isearch-backward-regexp)
  607. (define-key my-prefix-map (kbd "C-o") 'occur)
  608. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  609. ;; japanese input method
  610. (defun my-load-scim ()
  611. "Use scim-bridge.el as japanese im."
  612. ;; Load scim-bridge.
  613. (when (require 'scim-bridge nil t)
  614. ;; Turn on scim-mode automatically after loading .emacs
  615. (add-hook 'after-init-hook 'scim-mode-on)
  616. (setq scim-cursor-color "red")
  617. (scim-define-preedit-key ?\^h t)
  618. (scim-define-common-key ?\* nil)
  619. (scim-define-common-key ?\^/ nil)))
  620. (defun my-load-anthy ()
  621. "Use anthy.el as japanese im."
  622. ;; anthy
  623. (when (require 'anthy nil t)
  624. (global-set-key
  625. (kbd "<muhenkan>") (lambda () (interactive) (anthy-mode-off)))
  626. (global-set-key (kbd "<henkan>") (lambda () (interactive) (anthy-mode-on)))
  627. (when (>= emacs-major-version 23)
  628. (setq anthy-accept-timeout 1))))
  629. ;; quail
  630. ;; aproposs input-method for some information
  631. ;; (setq default-input-method "japanese")
  632. (defun my-load-mozc-el ()
  633. "Use mozc.el as japanese im."
  634. (setq mozc-leim-title "[MZ]")
  635. (when (require 'mozc nil t)
  636. (setq defauit-input-method "japanese-mozc")
  637. ))
  638. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  639. ;; gmail
  640. (setq mail-interactive t
  641. send-mail-function 'smtpmail-send-it
  642. ;; message-send-mail-function 'smtpmail-send-it
  643. smtpmail-smtp-server "smtp.gmail.com"
  644. smtpmail-smtp-service 587
  645. smtpmail-starttls-credentials '(("smtp.gmail.com" 587
  646. "8.slashes@gmail.com" nil))
  647. smtpmail-auth-credentials '(("smtp.gmail.com" 587
  648. "8.slashes@gmail.com" nil))
  649. user-mail-address "8.slashes@gmail.com")
  650. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  651. ;; buffer killing
  652. ;; (defun my-delete-window-killing-buffer () nil)
  653. (defun my-query-kill-current-buffer ()
  654. "Interactively kill current buffer."
  655. (interactive)
  656. (if (y-or-n-p (concat "kill current buffer? :"))
  657. (kill-buffer (current-buffer))))
  658. (substitute-key-definition 'kill-buffer
  659. 'my-query-kill-current-buffer
  660. global-map)
  661. ;;(global-set-key "\C-xk" 'my-query-kill-current-buffer)
  662. (defun my-kill-buffers ()
  663. "Kill buffers that visit files."
  664. (interactive)
  665. (mapcar (lambda (buf)
  666. (when (buffer-file-name buf)
  667. (kill-buffer buf)))
  668. (buffer-list)))
  669. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  670. ;; share clipboard with x
  671. ;; this page describes this in details, but only these sexps seem to be needed
  672. ;; http://garin.jp/doc/Linux/xwindow_clipboard
  673. (and (not window-system)
  674. (not (eq window-system 'mac))
  675. (getenv "DISPLAY")
  676. (not (equal (getenv "DISPLAY") ""))
  677. (executable-find "xclip")
  678. ;; (< emacs-major-version 24)
  679. (fetch-library "http://www.emacswiki.org/emacs/download/xclip.el" t)
  680. (require 'xclip nil t)
  681. (turn-on-xclip))
  682. (and (eq system-type 'darwin)
  683. (fetch-library
  684. "https://raw.github.com/10sr/emacs-lisp/master/pasteboard.el"
  685. t)
  686. (require 'pasteboard nil t)
  687. (turn-on-pasteboard)
  688. (getenv "TMUX")
  689. (pasteboard-enable-rtun))
  690. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  691. ;; https://github.com/lunaryorn/flycheck
  692. (when (require 'flycheck nil t)
  693. (add-hook 'after-init-hook 'global-flycheck-mode))
  694. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  695. ;; window
  696. (and (fetch-library
  697. "https://raw.github.com/10sr/emacs-lisp/master/window-organizer.el"
  698. t)
  699. (lazy-load-eval 'window-organizer)
  700. (define-key ctl-x-map (kbd "w") 'window-organizer))
  701. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  702. ;; some modes and hooks
  703. (and (fetch-library
  704. "https://raw.github.com/10sr/emacs-lisp/master/remember-major-modes-mode.el"
  705. t)
  706. (require 'remember-major-modes-mode nil t)
  707. (remember-major-modes-mode 1)
  708. )
  709. ;; Detect file type from shebang and set major-mode.
  710. (add-to-list 'interpreter-mode-alist
  711. '("python3" . python-mode))
  712. (add-to-list 'interpreter-mode-alist
  713. '("python2" . python-mode))
  714. ;; http://fukuyama.co/foreign-regexp
  715. '(and (fetch-library
  716. "https://raw.github.com/k-talo/foreign-regexp.el/master/foreign-regexp.el"
  717. t)
  718. (require 'foreign-regexp nil t)
  719. (progn
  720. (setq foreign-regexp/regexp-type 'perl)
  721. '(setq reb-re-syntax 'foreign-regexp)
  722. ))
  723. (require 'session nil t)
  724. (lazy-load-eval 'sql '(sql-mode)
  725. (require 'sql-indent nil t))
  726. (and (fetch-library "https://raw.github.com/10sr/emacs-lisp/master/gtkbm.el"
  727. t)
  728. (lazy-load-eval 'gtkbm)
  729. (global-set-key (kbd "C-x C-d") 'gtkbm))
  730. (and (fetch-library
  731. "https://raw.github.com/10sr/emacs-lisp/master/git-command.el"
  732. t)
  733. (lazy-load-eval 'git-command
  734. nil
  735. (add-to-list 'git-command-major-mode-alist
  736. '("di" . diff-mode))
  737. (add-to-list 'git-command-major-mode-alist
  738. '("graph" . fundamental-mode))
  739. (add-to-list 'git-command-major-mode-alist
  740. '("log" . fundamental-mode)))
  741. ;; (setq git-command-default-options "-c color.ui=always")
  742. (define-key ctl-x-map "g" 'git-command))
  743. (and (fetch-library
  744. "http://www.emacswiki.org/emacs/download/sl.el"
  745. t)
  746. (lazy-load-eval 'sl))
  747. (defalias 'qcalc 'quick-calc)
  748. (require 'simple nil t)
  749. (add-hook 'makefile-mode-hook
  750. (lambda ()
  751. (define-key makefile-mode-map (kbd "C-m") 'newline-and-indent)
  752. ;; this functions is set in write-file-functions, i cannot find any
  753. ;; good way to remove this.
  754. (fset 'makefile-warn-suspicious-lines 'ignore)
  755. ))
  756. (add-hook 'verilog-mode-hook
  757. (lambda ()
  758. (define-key verilog-mode-map ";" 'self-insert-command)))
  759. (setq diff-switches "-u")
  760. (add-hook 'diff-mode-hook
  761. (lambda ()
  762. (view-mode 1)
  763. (set-face-attribute 'diff-header nil
  764. :foreground nil
  765. :background nil
  766. :weight 'bold)
  767. (set-face-attribute 'diff-file-header nil
  768. :foreground nil
  769. :background nil
  770. :weight 'bold)
  771. (set-face-foreground 'diff-index-face "blue")
  772. (set-face-attribute 'diff-hunk-header nil
  773. :foreground "cyan"
  774. :weight 'normal)
  775. (set-face-attribute 'diff-context nil
  776. ;; :foreground "white"
  777. :foreground nil
  778. :weight 'normal)
  779. (set-face-foreground 'diff-removed-face "red")
  780. (set-face-foreground 'diff-added-face "green")
  781. (set-face-background 'diff-removed-face nil)
  782. (set-face-background 'diff-added-face nil)
  783. (set-face-attribute 'diff-changed nil
  784. :foreground "magenta"
  785. :weight 'normal)
  786. ))
  787. ;; (ffap-bindings)
  788. (add-hook 'sh-mode-hook
  789. (lambda ()
  790. (define-key sh-mode-map
  791. (kbd "C-x C-e")
  792. 'my-execute-shell-command-current-line)))
  793. (setq sh-here-document-word "__EOC__")
  794. (defun my-execute-shell-command-current-line ()
  795. "Run current line as shell command"
  796. (interactive)
  797. (shell-command (buffer-substring-no-properties (point-at-bol)
  798. (point))))
  799. (setq auto-mode-alist
  800. `(("autostart\\'" . sh-mode)
  801. ("xinitrc\\'" . sh-mode)
  802. ("xprograms\\'" . sh-mode)
  803. ("PKGBUILD\\'" . sh-mode)
  804. ,@auto-mode-alist))
  805. (and (lazy-load-eval 'pkgbuild-mode)
  806. (setq auto-mode-alist (append '(("PKGBUILD\\'" . pkgbuild-mode))
  807. auto-mode-alist)))
  808. (add-hook 'html-mode-hook
  809. (lambda ()
  810. (define-key html-mode-map (kbd "C-m")
  811. 'reindent-then-newline-and-indent)))
  812. (add-hook 'text-mode-hook
  813. (lambda ()
  814. (define-key text-mode-map (kbd "C-m") 'newline)))
  815. (add-to-list 'Info-default-directory-list
  816. (expand-file-name "~/.info/emacs-ja"))
  817. (add-hook 'apropos-mode-hook
  818. (lambda ()
  819. (define-key apropos-mode-map "n" 'next-line)
  820. (define-key apropos-mode-map "p" 'previous-line)
  821. ))
  822. (add-hook 'isearch-mode-hook
  823. (lambda ()
  824. ;; (define-key isearch-mode-map
  825. ;; (kbd "C-j") 'isearch-other-control-char)
  826. ;; (define-key isearch-mode-map
  827. ;; (kbd "C-k") 'isearch-other-control-char)
  828. ;; (define-key isearch-mode-map
  829. ;; (kbd "C-h") 'isearch-other-control-char)
  830. (define-key isearch-mode-map (kbd "C-h") 'isearch-delete-char)
  831. (define-key isearch-mode-map (kbd "M-r")
  832. 'isearch-query-replace-regexp)))
  833. (add-hook 'outline-mode-hook
  834. (lambda ()
  835. (if (string-match "\\.md\\'" buffer-file-name)
  836. (set (make-local-variable 'outline-regexp) "#+ "))))
  837. (add-to-list 'auto-mode-alist (cons "\\.ol\\'" 'outline-mode))
  838. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'outline-mode))
  839. (when (fetch-library
  840. "http://jblevins.org/projects/markdown-mode/markdown-mode.el"
  841. t)
  842. (lazy-load-eval 'markdown-mode)
  843. (setq markdown-command (or (executable-find "markdown")
  844. (executable-find "markdown.pl")))
  845. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'markdown-mode))
  846. (add-hook 'markdown-mode-hook
  847. (lambda ()
  848. (outline-minor-mode 1)
  849. (set (make-local-variable 'comment-start) ";"))))
  850. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  851. ;; c-mode
  852. ;; (setq c-default-style "bsd")
  853. (add-hook 'c-mode-common-hook
  854. (lambda ()
  855. (setq c-basic-offset 4
  856. indent-tabs-mode nil)
  857. ;; (set-face-foreground 'font-lock-keyword-face "blue")
  858. (c-toggle-hungry-state -1)
  859. ;; (and (require 'gtags nil t)
  860. ;; (gtags-mode 1))
  861. ))
  862. (when (fetch-library
  863. "https://raw.github.com/mooz/js2-mode/master/js2-mode.el"
  864. t)
  865. (lazy-load-eval 'js2-mode)
  866. ;; (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  867. ;; (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode))
  868. (add-hook 'js2-mode-hook
  869. (lambda ()
  870. (define-key js2-mode-map (kbd "C-m") (lambda ()
  871. (interactive)
  872. (js2-enter-key)
  873. (indent-for-tab-command)))
  874. ;; (add-hook (kill-local-variable 'before-save-hook)
  875. ;; 'js2-before-save)
  876. ;; (add-hook 'before-save-hook
  877. ;; 'my-indent-buffer
  878. ;; nil
  879. ;; t)
  880. )))
  881. (require 'js-doc nil t)
  882. (when (require 'uniquify nil t)
  883. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  884. (setq uniquify-ignore-buffers-re "*[^*]+*")
  885. (setq uniquify-min-dir-content 1))
  886. (add-hook 'view-mode-hook
  887. (lambda()
  888. (define-key view-mode-map "j"
  889. (lambda() (interactive) (scroll-up 1)))
  890. (define-key view-mode-map "k"
  891. (lambda() (interactive) (scroll-down 1)))
  892. (define-key view-mode-map "v" 'toggle-read-only)
  893. (define-key view-mode-map "q" 'bury-buffer)
  894. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  895. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  896. ;; (define-key view-mode-map
  897. ;; "n" 'nonincremental-repeat-search-forward)
  898. ;; (define-key view-mode-map
  899. ;; "N" 'nonincremental-repeat-search-backward)
  900. (define-key view-mode-map "/" 'isearch-forward-regexp)
  901. (define-key view-mode-map "?" 'isearch-backward-regexp)
  902. (define-key view-mode-map "n" 'isearch-repeat-forward)
  903. (define-key view-mode-map "N" 'isearch-repeat-backward)
  904. (define-key view-mode-map (kbd "C-m") 'my-rgrep-symbol-at-point)
  905. ))
  906. (global-set-key "\M-r" 'view-mode)
  907. (setq view-read-only t)
  908. ;; (defun my-view-mode-search-word (word)
  909. ;; "Search for word current directory and subdirectories.
  910. ;; If called intearctively, find word at point."
  911. ;; (interactive (list (thing-at-point 'symbol)))
  912. ;; (if word
  913. ;; (if (and (require 'gtags nil t)
  914. ;; (gtags-get-rootpath))
  915. ;; (gtags-goto-tag word "s")
  916. ;; (my-rgrep word))
  917. ;; (message "No word at point.")
  918. ;; nil))
  919. (add-hook 'Man-mode-hook
  920. (lambda ()
  921. (view-mode 1)
  922. (setq truncate-lines nil)))
  923. (setq Man-notify-method (if window-system
  924. 'newframe
  925. 'aggressive))
  926. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  927. ;; python
  928. (when (lazy-load-eval 'python '(python-mode))
  929. (setq python-python-command (or (executable-find "python3")
  930. (executable-find "python")))
  931. ;; (defun my-python-run-as-command ()
  932. ;; ""
  933. ;; (interactive)
  934. ;; (shell-command (concat python-python-command " " buffer-file-name)))
  935. (defun my-python-display-python-buffer ()
  936. ""
  937. (interactive)
  938. (set-window-text-height (display-buffer python-buffer
  939. t)
  940. 7))
  941. (add-hook 'python-mode-hook
  942. (lambda ()
  943. (define-key python-mode-map
  944. (kbd "C-c C-e") 'my-python-run-as-command)
  945. (define-key python-mode-map
  946. (kbd "C-c C-b") 'my-python-display-python-buffer)
  947. (define-key python-mode-map (kbd "C-m") 'newline-and-indent)))
  948. (add-hook 'inferior-python-mode-hook
  949. (lambda ()
  950. (my-python-display-python-buffer)
  951. (define-key inferior-python-mode-map
  952. (kbd "<up>") 'comint-previous-input)
  953. (define-key inferior-python-mode-map
  954. (kbd "<down>") 'comint-next-input))))
  955. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  956. ;; GNU GLOBAL(gtags)
  957. ;; http://uguisu.skr.jp/Windows/gtags.html
  958. ;; http://eigyr.dip.jp/gtags.html
  959. ;; http://cha.la.coocan.jp/doc/gnu_global.html
  960. (let ((d "/opt/local/share/gtags/"))
  961. (and (file-directory-p d)
  962. (add-to-list 'load-path
  963. d)))
  964. (when (lazy-load-eval 'gtags '(gtags-mode))
  965. (add-hook 'gtags-mode-hook
  966. (lambda ()
  967. (view-mode gtags-mode)
  968. (setq gtags-select-buffer-single t)
  969. ;; (local-set-key "\M-t" 'gtags-find-tag)
  970. ;; (local-set-key "\M-r" 'gtags-find-rtag)
  971. ;; (local-set-key "\M-s" 'gtags-find-symbol)
  972. ;; (local-set-key "\C-t" 'gtags-pop-stack)
  973. (define-key gtags-mode-map (kbd "C-x t h")
  974. 'gtags-find-tag-from-here)
  975. (define-key gtags-mode-map (kbd "C-x t t") 'gtags-find-tag)
  976. (define-key gtags-mode-map (kbd "C-x t r") 'gtags-find-rtag)
  977. (define-key gtags-mode-map (kbd "C-x t s") 'gtags-find-symbol)
  978. (define-key gtags-mode-map (kbd "C-x t p") 'gtags-find-pattern)
  979. (define-key gtags-mode-map (kbd "C-x t f") 'gtags-find-file)
  980. (define-key gtags-mode-map (kbd "C-x t b") 'gtags-pop-stack) ;back
  981. ))
  982. (add-hook 'gtags-select-mode-hook
  983. (lambda ()
  984. (define-key gtags-select-mode-map (kbd "C-m") 'gtags-select-tag)
  985. ))
  986. )
  987. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  988. ;; term mode
  989. ;; (setq multi-term-program shell-file-name)
  990. (and (fetch-library "http://www.emacswiki.org/emacs/download/multi-term.el"
  991. t)
  992. (lazy-load-eval 'multi-term)
  993. (progn
  994. (setq multi-term-switch-after-close nil)
  995. (setq multi-term-dedicated-select-after-open-p t)
  996. (setq multi-term-dedicated-window-height 20)))
  997. (when (lazy-load-eval 'term '(term ansi-term))
  998. (defun my-term-quit-or-send-raw ()
  999. ""
  1000. (interactive)
  1001. (if (get-buffer-process (current-buffer))
  1002. (call-interactively 'term-send-raw)
  1003. (kill-buffer)))
  1004. ;; http://d.hatena.ne.jp/goinger/20100416/1271399150
  1005. ;; (setq term-ansi-default-program shell-file-name)
  1006. (add-hook 'term-setup-hook
  1007. (lambda ()
  1008. (setq term-display-table (make-display-table))))
  1009. (add-hook 'term-mode-hook
  1010. (lambda ()
  1011. (unless (memq (current-buffer)
  1012. (and (featurep 'multi-term)
  1013. ;; current buffer is not multi-term buffer
  1014. (multi-term-list)))
  1015. ;; (define-key term-raw-map "\C-q" 'move-beginning-of-line)
  1016. ;; (define-key term-raw-map "\C-r" 'term-send-raw)
  1017. ;; (define-key term-raw-map "\C-s" 'term-send-raw)
  1018. ;; (define-key term-raw-map "\C-f" 'forward-char)
  1019. ;; (define-key term-raw-map "\C-b" 'backward-char)
  1020. ;; (define-key term-raw-map "\C-t" 'set-mark-command)
  1021. (define-key term-raw-map
  1022. "\C-x" (lookup-key (current-global-map) "\C-x"))
  1023. (define-key term-raw-map
  1024. "\C-z" (lookup-key (current-global-map) "\C-z"))
  1025. )
  1026. ;; (define-key term-raw-map "\C-xl" 'term-line-mode)
  1027. ;; (define-key term-mode-map "\C-xc" 'term-char-mode)
  1028. (define-key term-raw-map (kbd "<up>")
  1029. (lambda () (interactive) (scroll-down 1)))
  1030. (define-key term-raw-map (kbd "<down>")
  1031. (lambda () (interactive) (scroll-up 1)))
  1032. (define-key term-raw-map (kbd "<right>") 'scroll-up)
  1033. (define-key term-raw-map (kbd "<left>") 'scroll-down)
  1034. (define-key term-raw-map (kbd "C-p") 'term-send-raw)
  1035. (define-key term-raw-map (kbd "C-n") 'term-send-raw)
  1036. (define-key term-raw-map "q" 'my-term-quit-or-send-raw)
  1037. ;; (define-key term-raw-map (kbd "ESC") 'term-send-raw)
  1038. (define-key term-raw-map [delete] 'term-send-raw)
  1039. (define-key term-raw-map (kbd "DEL") 'term-send-backspace)
  1040. (define-key term-raw-map "\C-y" 'term-paste)
  1041. (define-key term-raw-map
  1042. "\C-c" 'term-send-raw) ;; 'term-interrupt-subjob)
  1043. '(define-key term-mode-map (kbd "C-x C-q") 'term-pager-toggle)
  1044. ;; (dolist (key '("<up>" "<down>" "<right>" "<left>"))
  1045. ;; (define-key term-raw-map (read-kbd-macro key) 'term-send-raw))
  1046. ;; (define-key term-raw-map "\C-d" 'delete-char)
  1047. (set (make-local-variable 'scroll-margin) 0)
  1048. ;; (set (make-local-variable 'cua-enable-cua-keys) nil)
  1049. ;; (cua-mode 0)
  1050. ;; (and cua-mode
  1051. ;; (local-unset-key (kbd "C-c")))
  1052. ;; (define-key cua--prefix-override-keymap
  1053. ;;"\C-c" 'term-interrupt-subjob)
  1054. (set (make-local-variable 'hl-line-range-function)
  1055. (lambda ()
  1056. '(0 . 0)))
  1057. ))
  1058. ;; (add-hook 'term-exec-hook 'forward-char)
  1059. )
  1060. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1061. ;; buffer switching
  1062. (when (lazy-load-eval 'bs '(bs-show)
  1063. ;; (add-to-list 'bs-configurations
  1064. ;; '("processes" nil get-buffer-process ".*" nil nil))
  1065. (add-to-list 'bs-configurations
  1066. '("this-frame" nil (lambda (buf)
  1067. (memq buf (my-frame-buffer-get)))
  1068. ".*" nil nil))
  1069. (add-to-list 'bs-configurations
  1070. '("files-and-terminals" nil nil nil
  1071. (lambda (buf)
  1072. (and (bs-visits-non-file buf)
  1073. (save-excursion
  1074. (set-buffer buf)
  1075. (not (memq major-mode
  1076. '(term-mode
  1077. eshell-mode))))))))
  1078. ;; (setq bs-configurations (list
  1079. ;; '("processes" nil get-buffer-process ".*" nil nil)
  1080. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil
  1081. ;; bs-visits-non-file bs-sort-buffer-interns-are-last)))
  1082. )
  1083. ;; (global-set-key "\C-x\C-b" 'bs-show)
  1084. (defalias 'list-buffers 'bs-show)
  1085. (setq bs-default-configuration "files-and-terminals")
  1086. (setq bs-default-sort-name "by nothing")
  1087. (add-hook 'bs-mode-hook
  1088. (lambda ()
  1089. ;; (setq bs-default-configuration "files")
  1090. ;; (and bs--show-all
  1091. ;; (call-interactively 'bs-toggle-show-all))
  1092. (set (make-local-variable 'scroll-margin) 0))))
  1093. (iswitchb-mode 1)
  1094. (defun iswitchb-buffer-display-other-window ()
  1095. "Do iswitchb in other window."
  1096. (interactive)
  1097. (let ((iswitchb-default-method 'display))
  1098. (call-interactively 'iswitchb-buffer)))
  1099. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1100. ;; sdic
  1101. (when (lazy-load-eval 'sdic '(sdic-describe-word-at-point))
  1102. ;; (define-key my-prefix-map "\C-w" 'sdic-describe-word)
  1103. (define-key my-prefix-map "\C-t" 'sdic-describe-word-at-point-echo)
  1104. (defun sdic-describe-word-at-point-echo ()
  1105. ""
  1106. (interactive)
  1107. (save-window-excursion
  1108. (sdic-describe-word-at-point))
  1109. (save-excursion
  1110. (set-buffer sdic-buffer-name)
  1111. (message (buffer-substring (point-min)
  1112. (progn (goto-char (point-min))
  1113. (or (and (re-search-forward "^\\w"
  1114. nil
  1115. t
  1116. 4)
  1117. (progn (previous-line) t)
  1118. (point-at-eol))
  1119. (point-max)))))))
  1120. (setq sdic-eiwa-dictionary-list '((sdicf-client "/usr/share/dict/gene.sdic")))
  1121. (setq sdic-waei-dictionary-list
  1122. '((sdicf-client "/usr/share/dict/jedict.sdic" (add-keys-to-headword t))))
  1123. (setq sdic-disable-select-window t)
  1124. (setq sdic-window-height 7))
  1125. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1126. ;; vc
  1127. ;; (require 'vc)
  1128. (setq vc-handled-backends '())
  1129. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1130. ;; gauche-mode
  1131. ;; http://d.hatena.ne.jp/kobapan/20090305/1236261804
  1132. ;; http://www.katch.ne.jp/~leque/software/repos/gauche-mode/gauche-mode.el
  1133. (when (and (fetch-library
  1134. "http://www.katch.ne.jp/~leque/software/repos/gauche-mode/gauche-mode.el"
  1135. t)
  1136. (lazy-load-eval 'gauche-mode '(gauche-mode run-scheme)))
  1137. (let ((s (executable-find "gosh")))
  1138. (setq scheme-program-name s
  1139. gauche-program-name s))
  1140. (defun run-gauche-other-window ()
  1141. "Run gauche on other window"
  1142. (interactive)
  1143. (switch-to-buffer-other-window
  1144. (get-buffer-create "*scheme*"))
  1145. (run-gauche))
  1146. (defun run-gauche ()
  1147. "run gauche"
  1148. (run-scheme gauche-program-name)
  1149. )
  1150. (defun scheme-send-buffer ()
  1151. ""
  1152. (interactive)
  1153. (scheme-send-region (point-min) (point-max))
  1154. (my-scheme-display-scheme-buffer)
  1155. )
  1156. (defun my-scheme-display-scheme-buffer ()
  1157. ""
  1158. (interactive)
  1159. (set-window-text-height (display-buffer scheme-buffer
  1160. t)
  1161. 7))
  1162. (add-hook 'scheme-mode-hook
  1163. (lambda ()
  1164. nil))
  1165. (add-hook 'inferior-scheme-mode-hook
  1166. (lambda ()
  1167. ;; (my-scheme-display-scheme-buffer)
  1168. ))
  1169. (setq auto-mode-alist
  1170. (cons '("\.gosh\\'" . gauche-mode) auto-mode-alist))
  1171. (setq auto-mode-alist
  1172. (cons '("\.gaucherc\\'" . gauche-mode) auto-mode-alist))
  1173. (add-hook 'gauche-mode-hook
  1174. (lambda ()
  1175. (define-key gauche-mode-map
  1176. (kbd "C-c C-z") 'run-gauche-other-window)
  1177. (define-key scheme-mode-map
  1178. (kbd "C-c C-c") 'scheme-send-buffer)
  1179. (define-key scheme-mode-map
  1180. (kbd "C-c C-b") 'my-scheme-display-scheme-buffer))))
  1181. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1182. ;; recentf-mode
  1183. (setq recentf-save-file (expand-file-name "~/.emacs.d/recentf")
  1184. recentf-max-menu-items 20
  1185. recentf-max-saved-items 30
  1186. recentf-show-file-shortcuts-flag nil)
  1187. (when (require 'recentf nil t)
  1188. (add-to-list 'recentf-exclude (regexp-quote recentf-save-file))
  1189. (define-key ctl-x-map (kbd "C-r") 'recentf-open-files)
  1190. (add-hook 'find-file-hook
  1191. 'recentf-save-list
  1192. t) ; save to file immediately after adding file to recentf list
  1193. (add-hook 'kill-emacs-hook
  1194. 'recentf-load-list)
  1195. ;;(run-with-idle-timer 5 t 'recentf-save-list)
  1196. ;; (add-hook 'find-file-hook
  1197. ;; (lambda ()
  1198. ;; (recentf-add-file default-directory)))
  1199. (and (fetch-library
  1200. "https://raw.github.com/10sr/emacs-lisp/master/recentf-show.el"
  1201. t)
  1202. (lazy-load-eval 'recentf-show)
  1203. (define-key ctl-x-map (kbd "C-r") 'recentf-show)
  1204. (add-hook 'recentf-show-before-listing-hook
  1205. 'recentf-load-list))
  1206. (recentf-mode 1)
  1207. (add-hook 'recentf-dialog-mode-hook
  1208. (lambda ()
  1209. ;; (recentf-save-list)
  1210. ;; (define-key recentf-dialog-mode-map (kbd "C-x C-f")
  1211. ;; 'my-recentf-cd-and-find-file)
  1212. (define-key recentf-dialog-mode-map (kbd "<up>") 'previous-line)
  1213. (define-key recentf-dialog-mode-map (kbd "<down>") 'next-line)
  1214. (define-key recentf-dialog-mode-map "p" 'previous-line)
  1215. (define-key recentf-dialog-mode-map "n" 'next-line)
  1216. (cd "~/"))))
  1217. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1218. ;; dired
  1219. (when (lazy-load-eval 'dired nil)
  1220. (defun my-dired-echo-file-head (arg)
  1221. ""
  1222. (interactive "P")
  1223. (let ((f (dired-get-filename)))
  1224. (message "%s"
  1225. (with-temp-buffer
  1226. (insert-file-contents f)
  1227. (buffer-substring-no-properties
  1228. (point-min)
  1229. (progn (goto-line (if arg
  1230. (prefix-numeric-value arg)
  1231. 10))
  1232. (point-at-eol)))))))
  1233. (defun my-dired-diff ()
  1234. ""
  1235. (interactive)
  1236. (let ((files (dired-get-marked-files nil nil nil t)))
  1237. (if (eq (car files)
  1238. t)
  1239. (diff (cadr files) (dired-get-filename))
  1240. (message "One files must be marked!"))))
  1241. (defun my-pop-to-buffer-erase-noselect (buffer-or-name)
  1242. "pop up buffer using `display-buffer' and return that buffer."
  1243. (let ((bf (get-buffer-create buffer-or-name)))
  1244. (with-current-buffer bf
  1245. (cd ".")
  1246. (erase-buffer))
  1247. (display-buffer bf)
  1248. bf))
  1249. (defun my-replace-nasi-none ()
  1250. ""
  1251. (save-excursion
  1252. (let ((buffer-read-only nil))
  1253. (goto-char (point-min))
  1254. (while (search-forward "なし" nil t)
  1255. (replace-match "none")))))
  1256. (defun dired-get-file-info ()
  1257. "dired get file info"
  1258. (interactive)
  1259. (let ((f (shell-quote-argument (dired-get-filename t))))
  1260. (if (file-directory-p f)
  1261. (progn
  1262. (message "Calculating disk usage...")
  1263. (shell-command (concat "du -hsD "
  1264. f)))
  1265. (shell-command (concat "file "
  1266. f)))))
  1267. (defun my-dired-scroll-up ()
  1268. ""
  1269. (interactive)
  1270. (my-dired-previous-line (- (window-height) 1)))
  1271. (defun my-dired-scroll-down ()
  1272. ""
  1273. (interactive)
  1274. (my-dired-next-line (- (window-height) 1)))
  1275. ;; (defun my-dired-forward-line (arg)
  1276. ;; ""
  1277. ;; (interactive "p"))
  1278. (defun my-dired-previous-line (arg)
  1279. ""
  1280. (interactive "p")
  1281. (if (> arg 0)
  1282. (progn
  1283. (if (eq (line-number-at-pos)
  1284. 1)
  1285. (goto-char (point-max))
  1286. (forward-line -1))
  1287. (my-dired-previous-line (if (or (dired-get-filename nil t)
  1288. (dired-get-subdir))
  1289. (- arg 1)
  1290. arg)))
  1291. (dired-move-to-filename)))
  1292. (defun my-dired-next-line (arg)
  1293. ""
  1294. (interactive "p")
  1295. (if (> arg 0)
  1296. (progn
  1297. (if (eq (point)
  1298. (point-max))
  1299. (goto-char (point-min))
  1300. (forward-line 1))
  1301. (my-dired-next-line (if (or (dired-get-filename nil t)
  1302. (dired-get-subdir))
  1303. (- arg 1)
  1304. arg)))
  1305. (dired-move-to-filename)))
  1306. (defun my-dired-print-current-dir-and-file ()
  1307. (message "%s %s"
  1308. default-directory
  1309. (buffer-substring-no-properties (point-at-bol)
  1310. (point-at-eol))))
  1311. (defun dired-do-execute-as-command ()
  1312. ""
  1313. (interactive)
  1314. (let ((file (dired-get-filename t)))
  1315. (if (file-executable-p file)
  1316. (start-process file nil file)
  1317. (when (y-or-n-p
  1318. "this file cant be executed. mark as executable and go? : ")
  1319. (set-file-modes file
  1320. (file-modes-symbolic-to-number "u+x" (file-modes file)))
  1321. (start-process file nil file)))))
  1322. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  1323. (defun my-dired-x-open ()
  1324. ""
  1325. (interactive)
  1326. (my-x-open (dired-get-filename t t)))
  1327. (if (eq window-system 'mac)
  1328. (setq dired-listing-switches "-lhF")
  1329. (setq dired-listing-switches "-lhF --time-style=long-iso")
  1330. )
  1331. (setq dired-listing-switches "-lhF")
  1332. (put 'dired-find-alternate-file 'disabled nil)
  1333. ;; when using dired-find-alternate-file
  1334. ;; reuse current dired buffer for the file to open
  1335. (setq dired-ls-F-marks-symlinks t)
  1336. (when (require 'ls-lisp nil t)
  1337. (setq ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  1338. (setq ls-lisp-dirs-first t)
  1339. (setq ls-lisp-use-localized-time-format t)
  1340. (setq ls-lisp-format-time-list
  1341. '("%Y-%m-%d %H:%M"
  1342. "%Y-%m-%d ")))
  1343. (setq dired-dwim-target t)
  1344. ;; (add-hook 'dired-after-readin-hook
  1345. ;; 'my-replace-nasi-none)
  1346. ;; (add-hook 'after-init-hook
  1347. ;; (lambda ()
  1348. ;; (dired ".")))
  1349. (add-hook 'dired-mode-hook
  1350. (lambda ()
  1351. (define-key dired-mode-map "o" 'my-dired-x-open)
  1352. (define-key dired-mode-map "i" 'dired-get-file-info)
  1353. (define-key dired-mode-map "f" 'find-file)
  1354. (define-key dired-mode-map "!" 'shell-command)
  1355. (define-key dired-mode-map "&" 'async-shell-command)
  1356. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  1357. (define-key dired-mode-map "=" 'my-dired-diff)
  1358. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  1359. (define-key dired-mode-map "b" 'gtkbm)
  1360. (define-key dired-mode-map "h" 'my-dired-echo-file-head)
  1361. (define-key dired-mode-map "@" (lambda ()
  1362. (interactive) (my-x-open ".")))
  1363. (define-key dired-mode-map (kbd "TAB") 'other-window)
  1364. ;; (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  1365. (define-key dired-mode-map "/" 'dired-isearch-filenames)
  1366. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  1367. (define-key dired-mode-map (kbd "C-h") 'dired-up-directory)
  1368. (substitute-key-definition 'dired-next-line
  1369. 'my-dired-next-line dired-mode-map)
  1370. (substitute-key-definition 'dired-previous-line
  1371. 'my-dired-previous-line dired-mode-map)
  1372. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  1373. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  1374. (define-key dired-mode-map (kbd "ESC p") 'my-dired-scroll-up)
  1375. (define-key dired-mode-map (kbd "ESC n") 'my-dired-scroll-down)
  1376. (let ((file "._Icon\015"))
  1377. (when nil (file-readable-p file)
  1378. (delete-file file)))))
  1379. (and (fetch-library "https://raw.github.com/10sr/emacs-lisp/master/pack.el"
  1380. t)
  1381. (lazy-load-eval 'pack '(dired-do-pack-or-unpack pack))
  1382. (add-hook 'dired-mode-hook
  1383. (lambda ()
  1384. (define-key dired-mode-map "P" 'dired-do-pack-or-unpack))))
  1385. (and (fetch-library
  1386. "https://raw.github.com/10sr/emacs-lisp/master/dired-list-all-mode.el"
  1387. t)
  1388. (lazy-load-eval 'dired-list-all-mode)
  1389. (setq dired-listing-switches "-lhF")
  1390. (add-hook 'dired-mode-hook
  1391. (lambda ()
  1392. (define-key dired-mode-map "a" 'dired-list-all-mode)
  1393. )))
  1394. ) ; when dired locate
  1395. ;; http://blog.livedoor.jp/tek_nishi/archives/4693204.html
  1396. (defun my-dired-toggle-mark()
  1397. (let ((cur (cond ((eq (following-char) dired-marker-char) ?\040)
  1398. (t dired-marker-char))))
  1399. (delete-char 1)
  1400. (insert cur)))
  1401. (defun my-dired-mark (arg)
  1402. "Toggle mark the current (or next ARG) files.
  1403. If on a subdir headerline, mark all its files except `.' and `..'.
  1404. Use \\[dired-unmark-all-files] to remove all marks
  1405. and \\[dired-unmark] on a subdir to remove the marks in
  1406. this subdir."
  1407. (interactive "P")
  1408. (if (dired-get-subdir)
  1409. (save-excursion (dired-mark-subdir-files))
  1410. (let ((inhibit-read-only t))
  1411. (dired-repeat-over-lines
  1412. (prefix-numeric-value arg)
  1413. 'my-dired-toggle-mark))))
  1414. (defun my-dired-mark-backward (arg)
  1415. "In Dired, move up lines and toggle mark there.
  1416. Optional prefix ARG says how many lines to unflag; default is one line."
  1417. (interactive "p")
  1418. (my-dired-mark (- arg)))
  1419. (add-hook 'dired-mode-hook
  1420. (lambda ()
  1421. (local-set-key (kbd "SPC") 'my-dired-mark)
  1422. (local-set-key (kbd "S-SPC") 'my-dired-mark-backward))
  1423. )
  1424. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1425. ;; eshell
  1426. (lazy-load-eval 'eshell nil
  1427. (defun my-eshell-backward-delete-char ()
  1428. (interactive)
  1429. (when (< (save-excursion
  1430. (eshell-bol)
  1431. (point))
  1432. (point))
  1433. (backward-delete-char 1)))
  1434. (defun my-file-owner-p (file)
  1435. "t if FILE is owned by me."
  1436. (eq (user-uid) (nth 2 (file-attributes file))))
  1437. "http://www.bookshelf.jp/pukiwiki/pukiwiki.php\
  1438. ?Eshell%A4%F2%BB%C8%A4%A4%A4%B3%A4%CA%A4%B9"
  1439. ;; ;; written by Stefan Reichoer <reichoer@web.de>
  1440. ;; (defun eshell/less (&rest args)
  1441. ;; "Invoke `view-file' on the file.
  1442. ;; \"less +42 foo\" also goes to line 42 in the buffer."
  1443. ;; (if args
  1444. ;; (while args
  1445. ;; (if (string-match "\\`\\+\\([0-9]+\\)\\'" (car args))
  1446. ;; (let* ((line (string-to-number (match-string 1 (pop args))))
  1447. ;; (file (pop args)))
  1448. ;; (view-file file)
  1449. ;; (goto-line line))
  1450. ;; (view-file (pop args))))))
  1451. (defun eshell/o (&optional file)
  1452. (my-x-open (or file ".")))
  1453. ;; (defun eshell/vi (&rest args)
  1454. ;; "Invoke `find-file' on the file.
  1455. ;; \"vi +42 foo\" also goes to line 42 in the buffer."
  1456. ;; (while args
  1457. ;; (if (string-match "\\`\\+\\([0-9]+\\)\\'" (car args))
  1458. ;; (let* ((line (string-to-number (match-string 1 (pop args))))
  1459. ;; (file (pop args)))
  1460. ;; (find-file file)
  1461. ;; (goto-line line))
  1462. ;; (find-file (pop args)))))
  1463. (defun eshell/clear ()
  1464. "Clear the current buffer, leaving one prompt at the top."
  1465. (interactive)
  1466. (let ((inhibit-read-only t))
  1467. (erase-buffer)))
  1468. (defun eshell-clear ()
  1469. (interactive)
  1470. (let ((inhibit-read-only t))
  1471. (erase-buffer)
  1472. (insert (funcall eshell-prompt-function))))
  1473. (defun eshell/d (&optional dirname switches)
  1474. "if first arg is omitted open current directory."
  1475. (dired (or dirname ".") switches))
  1476. (defun eshell/v ()
  1477. (view-mode 1))
  1478. (defun eshell/aaa (&rest args)
  1479. (message "%S"
  1480. args))
  1481. (defun eshell/git (&rest args)
  1482. (if (require 'git-command nil t)
  1483. (git-command (mapconcat 'shell-quote-argument
  1484. args
  1485. " "))
  1486. (apply 'eshell-git-fallback args)))
  1487. (defun eshell-git-fallback (&rest args)
  1488. ""
  1489. (if (member (car args)
  1490. '("di" "diff" "log" "show" "graph"))
  1491. (apply 'eshell-exec-visual "git" args)
  1492. (shell-command-to-string (mapconcat 'shell-quote-argument
  1493. `("git" ,@args)
  1494. " "))
  1495. ;; (eshell-external-command "git" args)
  1496. ))
  1497. (defalias 'eshell/g 'eshell/git)
  1498. (defalias 'eshell/: 'ignore)
  1499. (defalias 'eshell/type 'eshell/which)
  1500. ;; (defalias 'eshell/vim 'eshell/vi)
  1501. (defalias 'eshell/ff 'find-file)
  1502. (defalias 'eshell/q 'eshell/exit)
  1503. (defun eshell-goto-prompt ()
  1504. ""
  1505. (interactive)
  1506. (goto-char (point-max)))
  1507. (defun eshell-delete-char-or-logout (n)
  1508. (interactive "p")
  1509. (if (equal (eshell-get-old-input)
  1510. "")
  1511. (progn
  1512. (insert "exit")
  1513. (eshell-send-input))
  1514. (delete-char n)))
  1515. (defun eshell-kill-input ()
  1516. (interactive)
  1517. (delete-region (point)
  1518. (progn (eshell-bol)
  1519. (point))))
  1520. (defalias 'eshell/logout 'eshell/exit)
  1521. (defun eshell-cd-default-directory (&optional eshell-buffer-or-name)
  1522. "open eshell and change wd
  1523. if arg given, use that eshell buffer, otherwise make new eshell buffer."
  1524. (interactive)
  1525. (let ((dir (expand-file-name default-directory)))
  1526. (switch-to-buffer (or eshell-buffer-or-name
  1527. (eshell t)))
  1528. (unless (equal dir (expand-file-name default-directory))
  1529. ;; (cd dir)
  1530. ;; (eshell-interactive-print (concat "cd " dir "\n"))
  1531. ;; (eshell-emit-prompt)
  1532. (goto-char (point-max))
  1533. (eshell-kill-input)
  1534. (insert "cd " dir)
  1535. (eshell-send-input))))
  1536. (defadvice eshell-next-matching-input-from-input
  1537. ;; do not cycle history
  1538. (around eshell-history-do-not-cycle activate)
  1539. (if (= 0
  1540. (or eshell-history-index
  1541. 0))
  1542. (progn
  1543. (delete-region eshell-last-output-end (point))
  1544. (insert-and-inherit eshell-matching-input-from-input-string)
  1545. (setq eshell-history-index nil))
  1546. ad-do-it))
  1547. (setq eshell-directory-name "~/.emacs.d/eshell/")
  1548. (setq eshell-term-name "eterm-color")
  1549. (setq eshell-scroll-to-bottom-on-input t)
  1550. (setq eshell-cmpl-ignore-case t)
  1551. (setq eshell-cmpl-cycle-completions nil)
  1552. (setq eshell-highlight-prompt nil)
  1553. (setq eshell-ls-initial-args '("-hCFG"
  1554. "--color=auto"
  1555. "--time-style=long-iso")) ; "-hF")
  1556. (setq eshell-prompt-function
  1557. (lambda ()
  1558. (with-temp-buffer
  1559. (let (p1 p2 p3 p4)
  1560. (insert ":: [")
  1561. (setq p1 (point))
  1562. (insert user-login-name
  1563. "@"
  1564. system-name
  1565. )
  1566. (setq p2 (point))
  1567. (insert ":")
  1568. (setq p3 (point))
  1569. (insert (abbreviate-file-name default-directory))
  1570. (setq p4 (point))
  1571. (insert "]")
  1572. (insert "\n:: ")
  1573. (unless (eq 0
  1574. eshell-last-command-status)
  1575. (insert (format "[STATUS:%d] "
  1576. eshell-last-command-status)))
  1577. (insert (if (= (user-uid)
  1578. 0)
  1579. "# "
  1580. "$ "))
  1581. (add-text-properties p1
  1582. p2
  1583. '(face underline))
  1584. (add-text-properties p3
  1585. p4
  1586. '(face underline))
  1587. (buffer-substring (point-min)
  1588. (point-max))))))
  1589. (add-hook 'eshell-mode-hook
  1590. (lambda ()
  1591. ;; (define-key eshell-mode-map (kbd "C-x C-x") (lambda ()
  1592. ;; (interactive)
  1593. ;; (switch-to-buffer (other-buffer))))
  1594. ;; (define-key eshell-mode-map (kbd "C-g") (lambda ()
  1595. ;; (interactive)
  1596. ;; (eshell-goto-prompt)
  1597. ;; (keyboard-quit)))
  1598. (define-key eshell-mode-map (kbd "C-u")
  1599. 'eshell-kill-input)
  1600. (define-key eshell-mode-map (kbd "C-d")
  1601. 'eshell-delete-char-or-logout)
  1602. ;; (define-key eshell-mode-map (kbd "C-l")
  1603. ;; 'eshell-clear)
  1604. (define-key eshell-mode-map (kbd "DEL")
  1605. 'my-eshell-backward-delete-char)
  1606. (define-key eshell-mode-map
  1607. (kbd "C-p") 'eshell-previous-matching-input-from-input)
  1608. (define-key eshell-mode-map
  1609. (kbd "C-n") 'eshell-next-matching-input-from-input)
  1610. (apply 'eshell/addpath exec-path)
  1611. (set (make-local-variable 'scroll-margin) 0)
  1612. ;; (eshell/export "GIT_PAGER=")
  1613. ;; (eshell/export "GIT_EDITOR=")
  1614. (eshell/export "LC_MESSAGES=C")
  1615. (switch-to-buffer (current-buffer)) ; move buffer top of list
  1616. (set (make-local-variable 'hl-line-range-function)
  1617. (lambda ()
  1618. '(0 . 0)))
  1619. (add-to-list 'eshell-virtual-targets
  1620. '("/dev/less"
  1621. (lambda (str)
  1622. (if str
  1623. (with-current-buffer nil)))
  1624. nil))
  1625. ))
  1626. (add-hook 'eshell-mode-hook
  1627. (lambda ()
  1628. (add-to-list 'eshell-visual-commands "vim")
  1629. ;; (add-to-list 'eshell-visual-commands "git")
  1630. (add-to-list 'eshell-output-filter-functions
  1631. 'eshell-truncate-buffer)
  1632. (mapcar (lambda (alias)
  1633. (add-to-list 'eshell-command-aliases-list
  1634. alias))
  1635. '(
  1636. ; ("ll" "ls -l $*")
  1637. ; ("la" "ls -a $*")
  1638. ; ("lla" "ls -al $*")
  1639. ("eless"
  1640. (concat "cat >>> (with-current-buffer "
  1641. "(get-buffer-create \"*eshell output\") "
  1642. "(erase-buffer) "
  1643. "(setq buffer-read-only nil) "
  1644. "(current-buffer)) "
  1645. "(view-buffer (get-buffer \"*eshell output*\"))")
  1646. ))
  1647. )))
  1648. ) ; eval after load eshell
  1649. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1650. ;; frame buffer
  1651. ;; todo: work well when opening files already opened on another window
  1652. (add-hook 'after-make-frame-functions
  1653. (lambda (f)
  1654. (set-window-buffer (frame-selected-window f)
  1655. "*Messages*")))
  1656. (defun make-frame-command-with-name (name)
  1657. "Make frame with NAME specified."
  1658. (interactive "sName for new frame: ")
  1659. (set-frame-parameter (make-frame-command)
  1660. 'name
  1661. name))
  1662. (defvar my-frame-buffer-plist nil)
  1663. (defun my-frame-buffer-add (&optional buf frame)
  1664. "Add BUF to buffer list for FRAME."
  1665. (setq my-frame-buffer-plist
  1666. (plist-put my-frame-buffer-plist
  1667. (or frame
  1668. (selected-frame))
  1669. (let ((lst (my-frame-buffer-get frame)))
  1670. (if lst
  1671. (add-to-list 'lst
  1672. (or buf
  1673. (current-buffer)))
  1674. (list (or buf
  1675. (current-buffer))))))))
  1676. (defun my-frame-buffer-remove (&optional buf frame)
  1677. "Remove BUF from bufferlist for FRAME."
  1678. (setq my-frame-buffer-plist
  1679. (plist-put my-frame-buffer-plist
  1680. (or frame
  1681. (selected-frame))
  1682. (delq (or buf
  1683. (current-buffer))
  1684. (my-frame-buffer-get frame)))))
  1685. (defun my-frame-buffer-get (&optional frame)
  1686. "Get buffer list for FRAME."
  1687. (plist-get my-frame-buffer-plist
  1688. (or frame
  1689. (selected-frame))))
  1690. (defun my-frame-buffer-kill-all-buffer (&optional frame)
  1691. "Kill all buffer of FRAME."
  1692. (mapcar 'kill-buffer
  1693. (my-frame-buffer-get frame)))
  1694. (add-hook 'find-file-hook
  1695. 'my-frame-buffer-add)
  1696. ;; (add-hook 'term-mode-hook
  1697. ;; 'my-frame-buffer-add)
  1698. (add-hook 'eshell-mode-hook
  1699. 'my-frame-buffer-add)
  1700. (add-hook 'Man-mode-hook
  1701. 'my-frame-buffer-add)
  1702. (add-hook 'kill-buffer-hook
  1703. 'my-frame-buffer-remove)
  1704. (add-hook 'delete-frame-functions
  1705. 'my-frame-buffer-kill-all-buffer)
  1706. (defvar my-desktop-terminal "roxterm")
  1707. (defun my-execute-terminal ()
  1708. "Invole terminal program."
  1709. (interactive)
  1710. (if (and (or (eq system-type 'windows-nt)
  1711. window-system)
  1712. my-desktop-terminal
  1713. )
  1714. (let ((process-environment (cons "TERM=xterm" process-environment)))
  1715. (start-process "terminal"
  1716. nil
  1717. my-desktop-terminal))
  1718. (my-term)))
  1719. (defun my-delete-frame-or-kill-emacs ()
  1720. "Delete frame when opening multiple frame, kill Emacs when only one."
  1721. (interactive)
  1722. (if (eq 1
  1723. (length (frame-list)))
  1724. (save-buffers-kill-emacs)
  1725. (delete-frame)))
  1726. ;; (define-key my-prefix-map (kbd "C-s") 'my-execute-terminal)
  1727. (define-key my-prefix-map (kbd "C-f") 'make-frame-command-with-name)
  1728. (global-set-key (kbd "C-x C-c") 'my-delete-frame-or-kill-emacs)
  1729. (define-key my-prefix-map (kbd "C-x C-c") 'save-buffers-kill-emacs)
  1730. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1731. ;; my-term
  1732. (defvar my-term nil
  1733. "My terminal buffer.")
  1734. (defvar my-term-function nil
  1735. "Function to create terminal buffer.
  1736. This function accept no argument and return newly created buffer of terminal.")
  1737. (defun my-term (&optional arg)
  1738. "Open terminal buffer and return that buffer.
  1739. ARG is ignored."
  1740. (interactive "P")
  1741. (if (and my-term
  1742. (buffer-name my-term))
  1743. (pop-to-buffer my-term)
  1744. (setq my-term
  1745. (save-window-excursion
  1746. (funcall my-term-function)))
  1747. (and my-term
  1748. (my-term))))
  1749. ;; (setq my-term-function
  1750. ;; (lambda ()
  1751. ;; (if (eq system-type 'windows-nt)
  1752. ;; (eshell)
  1753. ;; (if (require 'multi-term nil t)
  1754. ;; (multi-term)
  1755. ;; (ansi-term shell-file-name)))))
  1756. (setq my-term-function 'eshell)
  1757. (define-key my-prefix-map (kbd "C-s") 'my-term)
  1758. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1759. ;; x open
  1760. (defvar my-filer nil)
  1761. (setq my-filer (or (executable-find "pcmanfm")
  1762. (executable-find "nautilus")))
  1763. (defun my-x-open (file)
  1764. "open FILE."
  1765. (interactive "FOpen File: ")
  1766. (setq file (expand-file-name file))
  1767. (message "Opening %s..." file)
  1768. (cond ((eq system-type 'windows-nt)
  1769. (call-process "cmd.exe" nil 0 nil
  1770. "/c" "start" "" (convert-standard-filename file)))
  1771. ((eq system-type 'darwin)
  1772. (call-process "open" nil 0 nil file))
  1773. ((getenv "DISPLAY")
  1774. (call-process (or my-filer "xdg-open") nil 0 nil file))
  1775. (t
  1776. (find-file file))
  1777. )
  1778. ;; (recentf-add-file file)
  1779. (message "Opening %s...done" file))
  1780. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1781. ;; misc funcs
  1782. (defun memo (&optional dir)
  1783. "Open memo.txt in DIR."
  1784. (interactive)
  1785. (pop-to-buffer (find-file-noselect (concat (if dir
  1786. (file-name-as-directory dir)
  1787. "")
  1788. "memo.txt"))))
  1789. (defvar my-rgrep-alist
  1790. `(
  1791. ;; gnu global
  1792. ("global"
  1793. (and (require 'gtags nil t)
  1794. (gtags-get-rootpath))
  1795. "global --result grep ")
  1796. ;; git grep
  1797. ("gitgrep"
  1798. (eq 0
  1799. (shell-command "git rev-parse --git-dir"))
  1800. "git --no-pager -c color.grep=false grep -nH -e ")
  1801. ;; the silver searcher
  1802. ("ag"
  1803. (executable-find "ag")
  1804. "ag --nocolor --nogroup --nopager ")
  1805. ;; ack
  1806. ("ack"
  1807. (executable-find "ack")
  1808. "ack --nocolor --nogroup --nopager ")
  1809. ;; grep
  1810. ("grep"
  1811. t
  1812. ,(concat "find . "
  1813. "-path '*/.git' -prune -o "
  1814. "-path '*/.svn' -prune -o "
  1815. "-type f -print0 | "
  1816. "xargs -0 grep -nH -e "))
  1817. )
  1818. "Alist of rgrep command.
  1819. Each element is in the form like (NAME SEXP COMMAND), where SEXP returns the
  1820. condition to choose COMMAND when evaluated.")
  1821. (defvar my-rgrep-default nil
  1822. "Default command name for my-rgrep.")
  1823. (defun my-rgrep-grep-command (&optional name alist)
  1824. "Return recursive grep command for current directory or nil.
  1825. If NAME is given, use that without testing.
  1826. Commands are searched from ALIST."
  1827. (if alist
  1828. (if name
  1829. ;; if name is given search that from alist and return the command
  1830. (nth 2 (assoc name
  1831. alist))
  1832. ;; if name is not given try test in 1th elem
  1833. (let ((car (car alist))
  1834. (cdr (cdr alist)))
  1835. (if (eval (nth 1 car))
  1836. ;; if the condition is true return the command
  1837. (nth 2 car)
  1838. ;; try next one
  1839. (and cdr
  1840. (my-rgrep-grep-command name cdr)))))
  1841. ;; if alist is not given set default value
  1842. (my-rgrep-grep-command name my-rgrep-alist)))
  1843. (my-rgrep-grep-command "ag" nil)
  1844. (defun my-rgrep (command-args)
  1845. "My recursive grep. Run COMMAND-ARGS."
  1846. (interactive (let ((cmd (my-rgrep-grep-command my-rgrep-default
  1847. nil)))
  1848. (if cmd
  1849. (list (read-shell-command "grep command: "
  1850. cmd
  1851. 'grep-find-history))
  1852. (error "my-rgrep: Command for rgrep not found")
  1853. )))
  1854. (compilation-start command-args
  1855. 'grep-mode))
  1856. ;; (defun my-rgrep-symbol-at-point (command-args)
  1857. ;; "My recursive grep. Run COMMAND-ARGS."
  1858. ;; (interactive (list (read-shell-command "grep command: "
  1859. ;; (concat (my-rgrep-grep-command)
  1860. ;; " "
  1861. ;; (thing-at-point 'symbol))
  1862. ;; 'grep-find-history)))
  1863. ;; (compilation-start command-args
  1864. ;; 'grep-mode))
  1865. (defun my-rgrep-ack ()
  1866. "My recursive grep by ack."
  1867. (interactive)
  1868. (let ((my-rgrep-default "ack"))
  1869. (if (called-interactively-p 'any)
  1870. (call-interactively 'my-rgrep)
  1871. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  1872. (defun my-rgrep-ag ()
  1873. "My recursive grep by ack."
  1874. (interactive)
  1875. (let ((my-rgrep-default "ag"))
  1876. (if (called-interactively-p 'any)
  1877. (call-interactively 'my-rgrep)
  1878. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  1879. (defun my-rgrep-gitgrep ()
  1880. "My recursive grep by ack."
  1881. (interactive)
  1882. (let ((my-rgrep-default "gitgrep"))
  1883. (if (called-interactively-p 'any)
  1884. (call-interactively 'my-rgrep)
  1885. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  1886. (defun my-rgrep-grep ()
  1887. "My recursive grep by ack."
  1888. (interactive)
  1889. (let ((my-rgrep-default "grep"))
  1890. (if (called-interactively-p 'any)
  1891. (call-interactively 'my-rgrep)
  1892. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  1893. (defun my-rgrep-global ()
  1894. "My recursive grep by ack."
  1895. (interactive)
  1896. (let ((my-rgrep-default "global"))
  1897. (if (called-interactively-p 'any)
  1898. (call-interactively 'my-rgrep)
  1899. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  1900. (define-key ctl-x-map "s" 'my-rgrep)
  1901. ;; (defun make ()
  1902. ;; "Run \"make -k\" in current directory."
  1903. ;; (interactive)
  1904. ;; (compile "make -k"))
  1905. (defalias 'make 'compile)
  1906. (defvar sed-in-place-history nil
  1907. "History of `sed-in-place'.")
  1908. (defvar sed-in-place-command "sed --in-place=.bak -e")
  1909. (defun sed-in-place (command)
  1910. "Issue sed in place COMMAND."
  1911. (interactive (list (read-shell-command "sed in place: "
  1912. (concat sed-in-place-command " ")
  1913. 'sed-in-place-history)))
  1914. (shell-command command
  1915. "*sed in place*"))
  1916. (defun dired-do-sed-in-place (&optional arg)
  1917. "Issue sed in place dired. If ARG is given, use the next ARG files."
  1918. (interactive "p")
  1919. (require 'dired-aux)
  1920. (let* ((files (dired-get-marked-files t arg))
  1921. (expr (dired-mark-read-string "Run sed-in-place for %s: "
  1922. nil
  1923. 'sed-in-place
  1924. arg
  1925. files)))
  1926. (if (equal expr
  1927. "")
  1928. (error "No expression specified")
  1929. (shell-command (concat sed-in-place-command
  1930. " '"
  1931. expr
  1932. "' "
  1933. (mapconcat 'shell-quote-argument
  1934. files
  1935. " "))
  1936. "*sed in place*"))))
  1937. (defun dir-show (&optional dir)
  1938. "Show DIR list."
  1939. (interactive)
  1940. (let ((bf (get-buffer-create "*dir show*"))
  1941. (list-directory-brief-switches "-C"))
  1942. (with-current-buffer bf
  1943. (list-directory (or nil
  1944. default-directory)
  1945. nil))
  1946. ))
  1947. (defun my-convmv-sjis2utf8-test ()
  1948. "Run `convmv -r -f sjis -t utf8 *'.
  1949. this is test, does not rename files."
  1950. (interactive)
  1951. (shell-command "convmv -r -f sjis -t utf8 *"))
  1952. (defun my-convmv-sjis2utf8-notest ()
  1953. "Run `convmv -r -f sjis -t utf8 * --notest'."
  1954. (interactive)
  1955. (shell-command "convmv -r -f sjis -t utf8 * --notest"))
  1956. (defun kill-ring-save-buffer-file-name ()
  1957. "Get current filename."
  1958. (interactive)
  1959. (let ((file buffer-file-name))
  1960. (if file
  1961. (progn (kill-new file)
  1962. (message file))
  1963. (message "not visiting file."))))
  1964. (defvar kill-ring-buffer-name "*kill-ring*"
  1965. "Buffer name for `kill-ring-buffer'.")
  1966. (defun open-kill-ring-buffer ()
  1967. "Open kill- ring buffer."
  1968. (interactive)
  1969. (pop-to-buffer
  1970. (with-current-buffer (get-buffer-create kill-ring-buffer-name)
  1971. (erase-buffer)
  1972. (yank)
  1973. (text-mode)
  1974. (current-local-map)
  1975. (goto-char (point-min)))))
  1976. ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1977. ;; ;; savage emacs
  1978. ;; ;; when enabled emacs fails to complete
  1979. ;; ;; http://e-arrows.sakura.ne.jp/2010/05/emacs-should-be-more-savage.html
  1980. ;; (defadvice message (before message-for-stupid (arg &rest arg2) activate)
  1981. ;; (setq arg
  1982. ;; (concat arg
  1983. ;; (if (eq nil
  1984. ;; (string-match "\\. *$"
  1985. ;; arg))
  1986. ;; ".")
  1987. ;; " Stupid!")))
  1988. (defvar my-system-info
  1989. nil
  1990. "System info in the form of \"[user@host] \".")
  1991. (setq my-system-info
  1992. (concat "["
  1993. user-login-name
  1994. "@"
  1995. (car (split-string system-name
  1996. "\\."))
  1997. "] "))
  1998. (defadvice read-from-minibuffer (before info-in-prompt activate)
  1999. "Show system info when use `read-from-minibuffer'."
  2000. (ad-set-arg 0
  2001. (concat my-system-info
  2002. (ad-get-arg 0))))
  2003. (defadvice read-string (before info-in-prompt activate)
  2004. "Show system info when use `read-string'."
  2005. (ad-set-arg 0
  2006. (concat my-system-info
  2007. (ad-get-arg 0))))
  2008. (when (< emacs-major-version 24)
  2009. (defadvice completing-read (before info-in-prompt activate)
  2010. "Show system info when use `completing-read'."
  2011. (ad-set-arg 0
  2012. (concat my-system-info
  2013. (ad-get-arg 0)))))
  2014. (defun my-real-function-subr-p (function)
  2015. "Return t if FUNCTION is a built-in function even if it is advised."
  2016. (let* ((advised (and (symbolp function)
  2017. (featurep 'advice)
  2018. (ad-get-advice-info function)))
  2019. (real-function
  2020. (or (and advised (let ((origname (cdr (assq 'origname advised))))
  2021. (and (fboundp origname)
  2022. origname)))
  2023. function))
  2024. (def (if (symbolp real-function)
  2025. (symbol-function real-function)
  2026. function)))
  2027. (subrp def)))
  2028. ;; (my-real-function-subr-p 'my-real-function-subr-p)
  2029. ;;; emacs.el ends here