Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

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