You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

2192 line
75 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. ;; show current 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://github.com/10sr/emacs-lisp/raw/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. (add-hook 'js2-mode-hook
  868. (lambda ()
  869. (define-key js2-mode-map (kbd "C-m") (lambda ()
  870. (interactive)
  871. (js2-enter-key)
  872. (indent-for-tab-command)))
  873. (add-hook (kill-local-variable 'before-save-hook)
  874. 'js2-before-save)
  875. ;; (add-hook 'before-save-hook
  876. ;; 'my-indent-buffer
  877. ;; nil
  878. ;; t)
  879. )))
  880. (when (require 'uniquify nil t)
  881. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  882. (setq uniquify-ignore-buffers-re "*[^*]+*")
  883. (setq uniquify-min-dir-content 1))
  884. (add-hook 'view-mode-hook
  885. (lambda()
  886. (define-key view-mode-map "j"
  887. (lambda() (interactive) (scroll-up 1)))
  888. (define-key view-mode-map "k"
  889. (lambda() (interactive) (scroll-down 1)))
  890. (define-key view-mode-map "v" 'toggle-read-only)
  891. (define-key view-mode-map "q" 'bury-buffer)
  892. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  893. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  894. ;; (define-key view-mode-map
  895. ;; "n" 'nonincremental-repeat-search-forward)
  896. ;; (define-key view-mode-map
  897. ;; "N" 'nonincremental-repeat-search-backward)
  898. (define-key view-mode-map "/" 'isearch-forward-regexp)
  899. (define-key view-mode-map "?" 'isearch-backward-regexp)
  900. (define-key view-mode-map "n" 'isearch-repeat-forward)
  901. (define-key view-mode-map "N" 'isearch-repeat-backward)
  902. (define-key view-mode-map (kbd "C-m") 'my-view-mode-search-word)
  903. ))
  904. (global-set-key "\M-r" 'view-mode)
  905. (setq view-read-only t)
  906. (defun my-view-mode-search-word (word)
  907. "Search for word current directory and subdirectories.
  908. If called intearctively, find word at point."
  909. (interactive (list (thing-at-point 'symbol)))
  910. (if word
  911. (if (and (require 'gtags nil t)
  912. (gtags-get-rootpath))
  913. (gtags-goto-tag word "s")
  914. (my-rgrep word))
  915. (message "No word at point.")
  916. nil))
  917. (add-hook 'Man-mode-hook
  918. (lambda ()
  919. (view-mode 1)
  920. (setq truncate-lines nil)))
  921. (setq Man-notify-method (if window-system
  922. 'newframe
  923. 'aggressive))
  924. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  925. ;; python
  926. (when (lazy-load-eval 'python '(python-mode))
  927. (setq python-python-command (or (executable-find "python3")
  928. (executable-find "python")))
  929. (defun my-python-run-as-command ()
  930. ""
  931. (interactive)
  932. (shell-command (concat python-python-command " " buffer-file-name)))
  933. (defun my-python-display-python-buffer ()
  934. ""
  935. (interactive)
  936. (set-window-text-height (display-buffer python-buffer
  937. t)
  938. 7))
  939. (add-hook 'python-mode-hook
  940. (lambda ()
  941. (define-key python-mode-map
  942. (kbd "C-c C-e") 'my-python-run-as-command)
  943. (define-key python-mode-map
  944. (kbd "C-c C-b") 'my-python-display-python-buffer)
  945. (define-key python-mode-map (kbd "C-m") 'newline-and-indent)))
  946. (add-hook 'inferior-python-mode-hook
  947. (lambda ()
  948. (my-python-display-python-buffer)
  949. (define-key inferior-python-mode-map
  950. (kbd "<up>") 'comint-previous-input)
  951. (define-key inferior-python-mode-map
  952. (kbd "<down>") 'comint-next-input))))
  953. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  954. ;; GNU GLOBAL(gtags)
  955. ;; http://uguisu.skr.jp/Windows/gtags.html
  956. ;; http://eigyr.dip.jp/gtags.html
  957. ;; http://cha.la.coocan.jp/doc/gnu_global.html
  958. (let ((d "/opt/local/share/gtags/"))
  959. (and (file-directory-p d)
  960. (add-to-list 'load-path
  961. d)))
  962. (when (lazy-load-eval 'gtags '(gtags-mode))
  963. (add-hook 'gtags-mode-hook
  964. (lambda ()
  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") 'gtags-find-tag-from-here)
  971. (define-key gtags-mode-map (kbd "C-x t t") 'gtags-find-tag)
  972. (define-key gtags-mode-map (kbd "C-x t r") 'gtags-find-rtag)
  973. (define-key gtags-mode-map (kbd "C-x t s") 'gtags-find-symbol)
  974. (define-key gtags-mode-map (kbd "C-x t p") 'gtags-find-pattern)
  975. (define-key gtags-mdoe-map (kbd "C-x t f") 'gtags-find-file)
  976. (define-key gtags-mode-map (kbd "C-x t b") 'gtags-pop-stack) ;back
  977. )))
  978. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  979. ;; term mode
  980. ;; (setq multi-term-program shell-file-name)
  981. (and (fetch-library "http://www.emacswiki.org/emacs/download/multi-term.el"
  982. t)
  983. (lazy-load-eval 'multi-term)
  984. (progn
  985. (setq multi-term-switch-after-close nil)
  986. (setq multi-term-dedicated-select-after-open-p t)
  987. (setq multi-term-dedicated-window-height 20)))
  988. (when (lazy-load-eval 'term '(term ansi-term))
  989. (defun my-term-quit-or-send-raw ()
  990. ""
  991. (interactive)
  992. (if (get-buffer-process (current-buffer))
  993. (call-interactively 'term-send-raw)
  994. (kill-buffer)))
  995. ;; http://d.hatena.ne.jp/goinger/20100416/1271399150
  996. ;; (setq term-ansi-default-program shell-file-name)
  997. (add-hook 'term-setup-hook
  998. (lambda ()
  999. (setq term-display-table (make-display-table))))
  1000. (add-hook 'term-mode-hook
  1001. (lambda ()
  1002. (unless (memq (current-buffer)
  1003. (and (featurep 'multi-term)
  1004. ;; current buffer is not multi-term buffer
  1005. (multi-term-list)))
  1006. ;; (define-key term-raw-map "\C-q" 'move-beginning-of-line)
  1007. ;; (define-key term-raw-map "\C-r" 'term-send-raw)
  1008. ;; (define-key term-raw-map "\C-s" 'term-send-raw)
  1009. ;; (define-key term-raw-map "\C-f" 'forward-char)
  1010. ;; (define-key term-raw-map "\C-b" 'backward-char)
  1011. ;; (define-key term-raw-map "\C-t" 'set-mark-command)
  1012. (define-key term-raw-map
  1013. "\C-x" (lookup-key (current-global-map) "\C-x"))
  1014. (define-key term-raw-map
  1015. "\C-z" (lookup-key (current-global-map) "\C-z"))
  1016. )
  1017. ;; (define-key term-raw-map "\C-xl" 'term-line-mode)
  1018. ;; (define-key term-mode-map "\C-xc" 'term-char-mode)
  1019. (define-key term-raw-map (kbd "<up>")
  1020. (lambda () (interactive) (scroll-down 1)))
  1021. (define-key term-raw-map (kbd "<down>")
  1022. (lambda () (interactive) (scroll-up 1)))
  1023. (define-key term-raw-map (kbd "<right>") 'scroll-up)
  1024. (define-key term-raw-map (kbd "<left>") 'scroll-down)
  1025. (define-key term-raw-map (kbd "C-p") 'term-send-raw)
  1026. (define-key term-raw-map (kbd "C-n") 'term-send-raw)
  1027. (define-key term-raw-map "q" 'my-term-quit-or-send-raw)
  1028. ;; (define-key term-raw-map (kbd "ESC") 'term-send-raw)
  1029. (define-key term-raw-map [delete] 'term-send-raw)
  1030. (define-key term-raw-map (kbd "DEL") 'term-send-backspace)
  1031. (define-key term-raw-map "\C-y" 'term-paste)
  1032. (define-key term-raw-map
  1033. "\C-c" 'term-send-raw) ;; 'term-interrupt-subjob)
  1034. '(define-key term-mode-map (kbd "C-x C-q") 'term-pager-toggle)
  1035. ;; (dolist (key '("<up>" "<down>" "<right>" "<left>"))
  1036. ;; (define-key term-raw-map (read-kbd-macro key) 'term-send-raw))
  1037. ;; (define-key term-raw-map "\C-d" 'delete-char)
  1038. (set (make-local-variable 'scroll-margin) 0)
  1039. ;; (set (make-local-variable 'cua-enable-cua-keys) nil)
  1040. ;; (cua-mode 0)
  1041. ;; (and cua-mode
  1042. ;; (local-unset-key (kbd "C-c")))
  1043. ;; (define-key cua--prefix-override-keymap
  1044. ;;"\C-c" 'term-interrupt-subjob)
  1045. (set (make-local-variable 'hl-line-range-function)
  1046. (lambda ()
  1047. '(0 . 0)))
  1048. ))
  1049. ;; (add-hook 'term-exec-hook 'forward-char)
  1050. )
  1051. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1052. ;; buffer switching
  1053. (when (lazy-load-eval 'bs '(bs-show)
  1054. ;; (add-to-list 'bs-configurations
  1055. ;; '("processes" nil get-buffer-process ".*" nil nil))
  1056. (add-to-list 'bs-configurations
  1057. '("this-frame" nil (lambda (buf)
  1058. (memq buf (my-frame-buffer-get)))
  1059. ".*" nil nil))
  1060. (add-to-list 'bs-configurations
  1061. '("files-and-terminals" nil nil nil
  1062. (lambda (buf)
  1063. (and (bs-visits-non-file buf)
  1064. (save-excursion
  1065. (set-buffer buf)
  1066. (not (memq major-mode
  1067. '(term-mode
  1068. eshell-mode))))))))
  1069. ;; (setq bs-configurations (list
  1070. ;; '("processes" nil get-buffer-process ".*" nil nil)
  1071. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil
  1072. ;; bs-visits-non-file bs-sort-buffer-interns-are-last)))
  1073. )
  1074. ;; (global-set-key "\C-x\C-b" 'bs-show)
  1075. (defalias 'list-buffers 'bs-show)
  1076. (setq bs-default-configuration "files-and-terminals")
  1077. (setq bs-default-sort-name "by nothing")
  1078. (add-hook 'bs-mode-hook
  1079. (lambda ()
  1080. ;; (setq bs-default-configuration "files")
  1081. ;; (and bs--show-all
  1082. ;; (call-interactively 'bs-toggle-show-all))
  1083. (set (make-local-variable 'scroll-margin) 0))))
  1084. (iswitchb-mode 1)
  1085. (defun iswitchb-buffer-display-other-window ()
  1086. ""
  1087. (interactive)
  1088. (let ((iswitchb-default-method 'display))
  1089. (call-interactively 'iswitchb-buffer)))
  1090. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1091. ;; sdic
  1092. (when (lazy-load-eval 'sdic '(sdic-describe-word-at-point))
  1093. ;; (define-key my-prefix-map "\C-w" 'sdic-describe-word)
  1094. (define-key my-prefix-map "\C-t" 'sdic-describe-word-at-point-echo)
  1095. (defun sdic-describe-word-at-point-echo ()
  1096. ""
  1097. (interactive)
  1098. (save-window-excursion
  1099. (sdic-describe-word-at-point))
  1100. (save-excursion
  1101. (set-buffer sdic-buffer-name)
  1102. (message (buffer-substring (point-min)
  1103. (progn (goto-char (point-min))
  1104. (or (and (re-search-forward "^\\w"
  1105. nil
  1106. t
  1107. 4)
  1108. (progn (previous-line) t)
  1109. (point-at-eol))
  1110. (point-max)))))))
  1111. (setq sdic-eiwa-dictionary-list '((sdicf-client "/usr/share/dict/gene.sdic")))
  1112. (setq sdic-waei-dictionary-list
  1113. '((sdicf-client "/usr/share/dict/jedict.sdic" (add-keys-to-headword t))))
  1114. (setq sdic-disable-select-window t)
  1115. (setq sdic-window-height 7))
  1116. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1117. ;; vc
  1118. ;; (require 'vc)
  1119. (setq vc-handled-backends '())
  1120. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1121. ;; gauche-mode
  1122. ;; http://d.hatena.ne.jp/kobapan/20090305/1236261804
  1123. ;; http://www.katch.ne.jp/~leque/software/repos/gauche-mode/gauche-mode.el
  1124. (when (and (fetch-library
  1125. "http://www.katch.ne.jp/~leque/software/repos/gauche-mode/gauche-mode.el"
  1126. t)
  1127. (lazy-load-eval 'gauche-mode '(gauche-mode run-scheme)))
  1128. (let ((s (executable-find "gosh")))
  1129. (setq scheme-program-name s
  1130. gauche-program-name s))
  1131. (defun run-gauche-other-window ()
  1132. "Run gauche on other window"
  1133. (interactive)
  1134. (switch-to-buffer-other-window
  1135. (get-buffer-create "*scheme*"))
  1136. (run-gauche))
  1137. (defun run-gauche ()
  1138. "run gauche"
  1139. (run-scheme gauche-program-name)
  1140. )
  1141. (defun scheme-send-buffer ()
  1142. ""
  1143. (interactive)
  1144. (scheme-send-region (point-min) (point-max))
  1145. (my-scheme-display-scheme-buffer)
  1146. )
  1147. (defun my-scheme-display-scheme-buffer ()
  1148. ""
  1149. (interactive)
  1150. (set-window-text-height (display-buffer scheme-buffer
  1151. t)
  1152. 7))
  1153. (add-hook 'scheme-mode-hook
  1154. (lambda ()
  1155. nil))
  1156. (add-hook 'inferior-scheme-mode-hook
  1157. (lambda ()
  1158. ;; (my-scheme-display-scheme-buffer)
  1159. ))
  1160. (setq auto-mode-alist
  1161. (cons '("\.gosh\\'" . gauche-mode) auto-mode-alist))
  1162. (setq auto-mode-alist
  1163. (cons '("\.gaucherc\\'" . gauche-mode) auto-mode-alist))
  1164. (add-hook 'gauche-mode-hook
  1165. (lambda ()
  1166. (define-key gauche-mode-map
  1167. (kbd "C-c C-z") 'run-gauche-other-window)
  1168. (define-key scheme-mode-map
  1169. (kbd "C-c C-c") 'scheme-send-buffer)
  1170. (define-key scheme-mode-map
  1171. (kbd "C-c C-b") 'my-scheme-display-scheme-buffer))))
  1172. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1173. ;; recentf-mode
  1174. (setq recentf-save-file (expand-file-name "~/.emacs.d/recentf")
  1175. recentf-max-menu-items 20
  1176. recentf-max-saved-items 30
  1177. recentf-show-file-shortcuts-flag nil)
  1178. (when (require 'recentf nil t)
  1179. (add-to-list 'recentf-exclude (regexp-quote recentf-save-file))
  1180. (define-key ctl-x-map (kbd "C-r") 'recentf-open-files)
  1181. (add-hook 'find-file-hook
  1182. 'recentf-save-list
  1183. t) ; save to file immediately after adding file to recentf list
  1184. (add-hook 'kill-emacs-hook
  1185. 'recentf-load-list)
  1186. (run-with-idle-timer 5 t 'recentf-save-list)
  1187. ;; (add-hook 'find-file-hook
  1188. ;; (lambda ()
  1189. ;; (recentf-add-file default-directory)))
  1190. (and (fetch-library
  1191. "https://raw.github.com/10sr/emacs-lisp/master/recentf-show.el"
  1192. t)
  1193. (lazy-load-eval 'recentf-show)
  1194. (define-key ctl-x-map (kbd "C-r") 'recentf-show)
  1195. (add-hook 'recentf-show-before-listing-hook
  1196. 'recentf-load-list))
  1197. (recentf-mode 1)
  1198. (add-hook 'recentf-dialog-mode-hook
  1199. (lambda ()
  1200. ;; (recentf-save-list)
  1201. ;; (define-key recentf-dialog-mode-map (kbd "C-x C-f")
  1202. ;; 'my-recentf-cd-and-find-file)
  1203. (define-key recentf-dialog-mode-map (kbd "<up>") 'previous-line)
  1204. (define-key recentf-dialog-mode-map (kbd "<down>") 'next-line)
  1205. (define-key recentf-dialog-mode-map "p" 'previous-line)
  1206. (define-key recentf-dialog-mode-map "n" 'next-line)
  1207. (cd "~/"))))
  1208. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1209. ;; dired
  1210. (when (lazy-load-eval 'dired nil)
  1211. (defun my-dired-echo-file-head (arg)
  1212. ""
  1213. (interactive "P")
  1214. (let ((f (dired-get-filename)))
  1215. (message "%s"
  1216. (with-temp-buffer
  1217. (insert-file-contents f)
  1218. (buffer-substring-no-properties
  1219. (point-min)
  1220. (progn (goto-line (if arg
  1221. (prefix-numeric-value arg)
  1222. 10))
  1223. (point-at-eol)))))))
  1224. (defun my-dired-diff ()
  1225. ""
  1226. (interactive)
  1227. (let ((files (dired-get-marked-files nil nil nil t)))
  1228. (if (eq (car files)
  1229. t)
  1230. (diff (cadr files) (dired-get-filename))
  1231. (message "One files must be marked!"))))
  1232. (defun my-pop-to-buffer-erase-noselect (buffer-or-name)
  1233. "pop up buffer using `display-buffer' and return that buffer."
  1234. (let ((bf (get-buffer-create buffer-or-name)))
  1235. (with-current-buffer bf
  1236. (cd ".")
  1237. (erase-buffer))
  1238. (display-buffer bf)
  1239. bf))
  1240. (defun my-replace-nasi-none ()
  1241. ""
  1242. (save-excursion
  1243. (let ((buffer-read-only nil))
  1244. (goto-char (point-min))
  1245. (while (search-forward "なし" nil t)
  1246. (replace-match "none")))))
  1247. (defun dired-get-file-info ()
  1248. "dired get file info"
  1249. (interactive)
  1250. (let ((f (shell-quote-argument (dired-get-filename t))))
  1251. (if (file-directory-p f)
  1252. (progn
  1253. (message "Calculating disk usage...")
  1254. (shell-command (concat "du -hsD "
  1255. f)))
  1256. (shell-command (concat "file "
  1257. f)))))
  1258. (defun my-dired-scroll-up ()
  1259. ""
  1260. (interactive)
  1261. (my-dired-previous-line (- (window-height) 1)))
  1262. (defun my-dired-scroll-down ()
  1263. ""
  1264. (interactive)
  1265. (my-dired-next-line (- (window-height) 1)))
  1266. ;; (defun my-dired-forward-line (arg)
  1267. ;; ""
  1268. ;; (interactive "p"))
  1269. (defun my-dired-previous-line (arg)
  1270. ""
  1271. (interactive "p")
  1272. (if (> arg 0)
  1273. (progn
  1274. (if (eq (line-number-at-pos)
  1275. 1)
  1276. (goto-char (point-max))
  1277. (forward-line -1))
  1278. (my-dired-previous-line (if (or (dired-get-filename nil t)
  1279. (dired-get-subdir))
  1280. (- arg 1)
  1281. arg)))
  1282. (dired-move-to-filename)))
  1283. (defun my-dired-next-line (arg)
  1284. ""
  1285. (interactive "p")
  1286. (if (> arg 0)
  1287. (progn
  1288. (if (eq (point)
  1289. (point-max))
  1290. (goto-char (point-min))
  1291. (forward-line 1))
  1292. (my-dired-next-line (if (or (dired-get-filename nil t)
  1293. (dired-get-subdir))
  1294. (- arg 1)
  1295. arg)))
  1296. (dired-move-to-filename)))
  1297. (defun my-dired-print-current-dir-and-file ()
  1298. (message "%s %s"
  1299. default-directory
  1300. (buffer-substring-no-properties (point-at-bol)
  1301. (point-at-eol))))
  1302. (defun dired-do-execute-as-command ()
  1303. ""
  1304. (interactive)
  1305. (let ((file (dired-get-filename t)))
  1306. (if (file-executable-p file)
  1307. (start-process file nil file)
  1308. (when (y-or-n-p
  1309. "this file cant be executed. mark as executable and go? : ")
  1310. (set-file-modes file
  1311. (file-modes-symbolic-to-number "u+x" (file-modes file)))
  1312. (start-process file nil file)))))
  1313. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  1314. (defun my-dired-x-open ()
  1315. ""
  1316. (interactive)
  1317. (my-x-open (dired-get-filename t t)))
  1318. (if (eq window-system 'mac)
  1319. (setq dired-listing-switches "-lhF")
  1320. (setq dired-listing-switches "-lhF --time-style=long-iso")
  1321. )
  1322. (setq dired-listing-switches "-lhF")
  1323. (put 'dired-find-alternate-file 'disabled nil)
  1324. ;; when using dired-find-alternate-file
  1325. ;; reuse current dired buffer for the file to open
  1326. (setq dired-ls-F-marks-symlinks t)
  1327. (require 'ls-lisp)
  1328. (setq ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  1329. (setq ls-lisp-dirs-first t)
  1330. (setq ls-lisp-use-localized-time-format t)
  1331. (setq ls-lisp-format-time-list
  1332. '("%Y-%m-%d %H:%M"
  1333. "%Y-%m-%d "))
  1334. (setq dired-dwim-target t)
  1335. ;; (add-hook 'dired-after-readin-hook
  1336. ;; 'my-replace-nasi-none)
  1337. ;; (add-hook 'after-init-hook
  1338. ;; (lambda ()
  1339. ;; (dired ".")))
  1340. (add-hook 'dired-mode-hook
  1341. (lambda ()
  1342. (define-key dired-mode-map "o" 'my-dired-x-open)
  1343. (define-key dired-mode-map "i" 'dired-get-file-info)
  1344. (define-key dired-mode-map "f" 'find-file)
  1345. (define-key dired-mode-map "!" 'shell-command)
  1346. (define-key dired-mode-map "&" 'async-shell-command)
  1347. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  1348. (define-key dired-mode-map "=" 'my-dired-diff)
  1349. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  1350. (define-key dired-mode-map "b" 'gtkbm)
  1351. (define-key dired-mode-map "h" 'my-dired-echo-file-head)
  1352. (define-key dired-mode-map "@" (lambda ()
  1353. (interactive) (my-x-open ".")))
  1354. (define-key dired-mode-map (kbd "TAB") 'other-window)
  1355. ;; (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  1356. (define-key dired-mode-map "/" 'dired-isearch-filenames)
  1357. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  1358. (define-key dired-mode-map (kbd "C-h") 'dired-up-directory)
  1359. (substitute-key-definition 'dired-next-line
  1360. 'my-dired-next-line dired-mode-map)
  1361. (substitute-key-definition 'dired-previous-line
  1362. 'my-dired-previous-line dired-mode-map)
  1363. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  1364. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  1365. (define-key dired-mode-map (kbd "ESC p") 'my-dired-scroll-up)
  1366. (define-key dired-mode-map (kbd "ESC n") 'my-dired-scroll-down)
  1367. (let ((file "._Icon\015"))
  1368. (when nil (file-readable-p file)
  1369. (delete-file file)))))
  1370. (and (fetch-library "https://raw.github.com/10sr/emacs-lisp/master/pack.el"
  1371. t)
  1372. (lazy-load-eval 'pack '(dired-do-pack-or-unpack pack))
  1373. (add-hook 'dired-mode-hook
  1374. (lambda ()
  1375. (define-key dired-mode-map "P" 'dired-do-pack-or-unpack))))
  1376. (and (fetch-library
  1377. "https://raw.github.com/10sr/emacs-lisp/master/dired-list-all-mode.el"
  1378. t)
  1379. (lazy-load-eval 'dired-list-all-mode)
  1380. (setq dired-listing-switches "-lhF")
  1381. (add-hook 'dired-mode-hook
  1382. (lambda ()
  1383. (define-key dired-mode-map "a" 'dired-list-all-mode)
  1384. )))
  1385. ) ; when dired locate
  1386. ;; http://blog.livedoor.jp/tek_nishi/archives/4693204.html
  1387. (defun my-dired-toggle-mark()
  1388. (let ((cur (cond ((eq (following-char) dired-marker-char) ?\040)
  1389. (t dired-marker-char))))
  1390. (delete-char 1)
  1391. (insert cur)))
  1392. (defun my-dired-mark (arg)
  1393. "toggle mark the current (or next ARG) files.
  1394. If on a subdir headerline, mark all its files except `.' and `..'.
  1395. Use \\[dired-unmark-all-files] to remove all marks
  1396. and \\[dired-unmark] on a subdir to remove the marks in
  1397. this subdir."
  1398. (interactive "P")
  1399. (if (dired-get-subdir)
  1400. (save-excursion (dired-mark-subdir-files))
  1401. (let ((inhibit-read-only t))
  1402. (dired-repeat-over-lines
  1403. (prefix-numeric-value arg)
  1404. 'my-dired-toggle-mark))))
  1405. (defun my-dired-mark-backward (arg)
  1406. "In Dired, move up lines and toggle mark there.
  1407. Optional prefix ARG says how many lines to unflag; default is one line."
  1408. (interactive "p")
  1409. (my-dired-mark (- arg)))
  1410. (add-hook 'dired-mode-hook
  1411. (lambda ()
  1412. (local-set-key (kbd "SPC") 'my-dired-mark)
  1413. (local-set-key (kbd "S-SPC") 'my-dired-mark-backward))
  1414. )
  1415. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1416. ;; eshell
  1417. (lazy-load-eval 'eshell nil
  1418. (defun my-eshell-backward-delete-char ()
  1419. (interactive)
  1420. (when (< (save-excursion
  1421. (eshell-bol)
  1422. (point))
  1423. (point))
  1424. (backward-delete-char 1)))
  1425. (defun my-file-owner-p (file)
  1426. "t if FILE is owned by me."
  1427. (eq (user-uid) (nth 2 (file-attributes file))))
  1428. "http://www.bookshelf.jp/pukiwiki/pukiwiki.php\
  1429. ?Eshell%A4%F2%BB%C8%A4%A4%A4%B3%A4%CA%A4%B9"
  1430. ;; ;; written by Stefan Reichoer <reichoer@web.de>
  1431. ;; (defun eshell/less (&rest args)
  1432. ;; "Invoke `view-file' on the file.
  1433. ;; \"less +42 foo\" also goes to line 42 in the buffer."
  1434. ;; (if args
  1435. ;; (while args
  1436. ;; (if (string-match "\\`\\+\\([0-9]+\\)\\'" (car args))
  1437. ;; (let* ((line (string-to-number (match-string 1 (pop args))))
  1438. ;; (file (pop args)))
  1439. ;; (view-file file)
  1440. ;; (goto-line line))
  1441. ;; (view-file (pop args))))))
  1442. (defun eshell/o (&optional file)
  1443. (my-x-open (or file ".")))
  1444. ;; (defun eshell/vi (&rest args)
  1445. ;; "Invoke `find-file' on the file.
  1446. ;; \"vi +42 foo\" also goes to line 42 in the buffer."
  1447. ;; (while args
  1448. ;; (if (string-match "\\`\\+\\([0-9]+\\)\\'" (car args))
  1449. ;; (let* ((line (string-to-number (match-string 1 (pop args))))
  1450. ;; (file (pop args)))
  1451. ;; (find-file file)
  1452. ;; (goto-line line))
  1453. ;; (find-file (pop args)))))
  1454. (defun eshell/clear ()
  1455. "Clear the current buffer, leaving one prompt at the top."
  1456. (let ((inhibit-read-only t))
  1457. (erase-buffer)))
  1458. (defun eshell/d (&optional dirname switches)
  1459. "if first arg is omitted open current directory."
  1460. (dired (or dirname ".") switches))
  1461. (defun eshell/v ()
  1462. (view-mode 1))
  1463. (defun eshell/git (&rest args)
  1464. ""
  1465. (if (member (car args)
  1466. '("di" "diff" "log" "show"))
  1467. (apply 'eshell-exec-visual "git" args)
  1468. (shell-command (mapconcat 'shell-quote-argument
  1469. `("git" ,@args)
  1470. " ")
  1471. t)
  1472. ;; (eshell-external-command "git" args)
  1473. ))
  1474. (defalias 'eshell/: 'ignore)
  1475. (defalias 'eshell/type 'eshell/which)
  1476. ;; (defalias 'eshell/vim 'eshell/vi)
  1477. (defalias 'eshell/ff 'find-file)
  1478. (defalias 'eshell/q 'eshell/exit)
  1479. (defun eshell-goto-prompt ()
  1480. ""
  1481. (interactive)
  1482. (goto-char (point-max)))
  1483. (defun eshell-cd-default-directory (&optional eshell-buffer-or-name)
  1484. "open eshell and change wd
  1485. if arg given, use that eshell buffer, otherwise make new eshell buffer."
  1486. (interactive)
  1487. (let ((dir (expand-file-name default-directory)))
  1488. (switch-to-buffer (or eshell-buffer-or-name
  1489. (eshell t)))
  1490. (unless (equal dir (expand-file-name default-directory))
  1491. ;; (cd dir)
  1492. ;; (eshell-interactive-print (concat "cd " dir "\n"))
  1493. ;; (eshell-emit-prompt)
  1494. (goto-char (point-max))
  1495. (eshell-kill-input)
  1496. (insert "cd " dir)
  1497. (eshell-send-input))))
  1498. (setq eshell-directory-name "~/.emacs.d/eshell/")
  1499. (setq eshell-term-name "eterm-color")
  1500. (setq eshell-scroll-to-bottom-on-input t)
  1501. (setq eshell-cmpl-ignore-case t)
  1502. (setq eshell-cmpl-cycle-completions nil)
  1503. (setq eshell-highlight-prompt nil)
  1504. (setq eshell-ls-initial-args '("-hCFG"
  1505. "--color=auto"
  1506. "--time-style=long-iso")) ; "-hF")
  1507. (setq eshell-prompt-function
  1508. (lambda ()
  1509. (with-temp-buffer
  1510. (let (p1 p2 p3 p4)
  1511. (insert " [")
  1512. (setq p1 (point))
  1513. (insert (abbreviate-file-name default-directory))
  1514. (setq p2 (point))
  1515. (insert "]"
  1516. "\n")
  1517. (setq p3 (point))
  1518. (insert user-login-name
  1519. "@"
  1520. (or (getenv "HOSTNAME")
  1521. (substring (shell-command-to-string
  1522. (or (executable-find "hostname")
  1523. "echo ''"))
  1524. 0
  1525. -1)))
  1526. (setq p4 (point))
  1527. (insert " "
  1528. (format-time-string "%a, %d %b %Y %T %z")
  1529. " eshell\n"
  1530. "last:"
  1531. (number-to-string eshell-last-command-status)
  1532. (if (= (user-uid)
  1533. 0)
  1534. " # "
  1535. " $ "))
  1536. (add-text-properties p1
  1537. p2
  1538. '(face ((foreground-color . "yellow"))))
  1539. (add-text-properties p3
  1540. p4
  1541. '(face ((foreground-color . "cyan"))))
  1542. (buffer-substring (point-min)
  1543. (point-max))))))
  1544. (add-hook 'eshell-mode-hook
  1545. (lambda ()
  1546. ;; (define-key eshell-mode-map (kbd "C-x C-x") (lambda ()
  1547. ;; (interactive)
  1548. ;; (switch-to-buffer (other-buffer))))
  1549. (define-key eshell-mode-map (kbd "C-u") (lambda ()
  1550. (interactive)
  1551. (eshell-goto-prompt)
  1552. (eshell-kill-input)))
  1553. (define-key eshell-mode-map (kbd "C-g") (lambda ()
  1554. (interactive)
  1555. (eshell-goto-prompt)
  1556. (my-keyboard-quit)))
  1557. (define-key eshell-mode-map
  1558. (kbd "DEL") 'my-eshell-backward-delete-char)
  1559. (define-key eshell-mode-map
  1560. (kbd "C-p") 'eshell-previous-matching-input-from-input)
  1561. (define-key eshell-mode-map
  1562. (kbd "C-n") 'eshell-next-matching-input-from-input)
  1563. (apply 'eshell/addpath exec-path)
  1564. (set (make-local-variable 'scroll-margin) 0)
  1565. ;; (eshell/export "GIT_PAGER=")
  1566. ;; (eshell/export "GIT_EDITOR=")
  1567. (eshell/export "LC_MESSAGES=C")
  1568. (switch-to-buffer (current-buffer)) ; move buffer top of list
  1569. (set (make-local-variable 'hl-line-range-function)
  1570. (lambda ()
  1571. '(0 . 0)))
  1572. (add-to-list 'eshell-virtual-targets
  1573. '("/dev/less"
  1574. (lambda (str)
  1575. (if str
  1576. (with-current-buffer nil)))
  1577. nil))
  1578. ))
  1579. (add-hook 'eshell-mode-hook
  1580. (lambda ()
  1581. (add-to-list 'eshell-visual-commands "vim")
  1582. ;; (add-to-list 'eshell-visual-commands "git")
  1583. (add-to-list 'eshell-output-filter-functions
  1584. 'eshell-truncate-buffer)
  1585. (mapcar (lambda (alias)
  1586. (add-to-list 'eshell-command-aliases-list
  1587. alias))
  1588. '(
  1589. ; ("ll" "ls -l $*")
  1590. ; ("la" "ls -a $*")
  1591. ; ("lla" "ls -al $*")
  1592. ("aptin" "apt-get install $*")
  1593. ("eless"
  1594. (concat "cat >>> (with-current-buffer "
  1595. "(get-buffer-create \"*eshell output\") "
  1596. "(erase-buffer) "
  1597. "(setq buffer-read-only nil) "
  1598. "(current-buffer)) "
  1599. "(view-buffer (get-buffer \"*eshell output*\"))")
  1600. ("g" "git $*")
  1601. ))
  1602. )))
  1603. ) ; eval after load eshell
  1604. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1605. ;; frame buffer
  1606. ;; todo: work well when opening files already opened on another window
  1607. (add-hook 'after-make-frame-functions
  1608. (lambda (f)
  1609. (set-window-buffer (frame-selected-window f)
  1610. "*Messages*")))
  1611. (defun make-frame-command-with-name (name)
  1612. "Make frame with name specified."
  1613. (interactive "sName for new frame: ")
  1614. (set-frame-parameter (make-frame-command)
  1615. 'name
  1616. name))
  1617. (defvar my-frame-buffer-plist nil)
  1618. (defun my-frame-buffer-add (&optional buf frame)
  1619. ""
  1620. (setq my-frame-buffer-plist
  1621. (plist-put my-frame-buffer-plist
  1622. (or frame
  1623. (selected-frame))
  1624. (let ((lst (my-frame-buffer-get frame)))
  1625. (if lst
  1626. (add-to-list 'lst
  1627. (or buf
  1628. (current-buffer)))
  1629. (list (or buf
  1630. (current-buffer))))))))
  1631. (defun my-frame-buffer-remove (&optional buf frame)
  1632. ""
  1633. (setq my-frame-buffer-plist
  1634. (plist-put my-frame-buffer-plist
  1635. (or frame
  1636. (selected-frame))
  1637. (delq (or buf
  1638. (current-buffer))
  1639. (my-frame-buffer-get frame)))))
  1640. (defun my-frame-buffer-get (&optional frame)
  1641. ""
  1642. (plist-get my-frame-buffer-plist
  1643. (or frame
  1644. (selected-frame))))
  1645. (defun my-frame-buffer-kill-all-buffer (&optional frame)
  1646. ""
  1647. (mapcar 'kill-buffer
  1648. (my-frame-buffer-get frame)))
  1649. (add-hook 'find-file-hook
  1650. 'my-frame-buffer-add)
  1651. ;; (add-hook 'term-mode-hook
  1652. ;; 'my-frame-buffer-add)
  1653. (add-hook 'eshell-mode-hook
  1654. 'my-frame-buffer-add)
  1655. (add-hook 'Man-mode-hook
  1656. 'my-frame-buffer-add)
  1657. (add-hook 'kill-buffer-hook
  1658. 'my-frame-buffer-remove)
  1659. (add-hook 'delete-frame-functions
  1660. 'my-frame-buffer-kill-all-buffer)
  1661. (defvar my-desktop-terminal "roxterm")
  1662. (defun my-execute-terminal ()
  1663. ""
  1664. (interactive)
  1665. (if (and (or (eq system-type 'windows-nt)
  1666. window-system)
  1667. my-desktop-terminal
  1668. )
  1669. (let ((process-environment (cons "TERM=xterm" process-environment)))
  1670. (start-process "terminal"
  1671. nil
  1672. my-desktop-terminal))
  1673. (my-term)))
  1674. (defvar my-term nil "my terminal buffer")
  1675. (defun my-term ()
  1676. "open terminal buffer and return that buffer."
  1677. (interactive)
  1678. (if (and my-term
  1679. (buffer-name my-term))
  1680. (pop-to-buffer my-term)
  1681. (setq my-term
  1682. (save-window-excursion
  1683. (funcall my-term-function)))
  1684. (and my-term
  1685. (my-term))))
  1686. (defvar my-term-function nil
  1687. "Function to create terminal buffer.")
  1688. (setq my-term-function
  1689. (lambda ()
  1690. (if (eq system-type 'windows-nt)
  1691. (eshell)
  1692. (if (require 'multi-term nil t)
  1693. (multi-term)
  1694. (ansi-term shell-file-name)))))
  1695. ;; (setq my-term-function 'eshell)
  1696. (defun my-delete-frame-or-kill-emacs ()
  1697. "delete frame when opening multiple frame, kill emacs when only one."
  1698. (interactive)
  1699. (if (eq 1
  1700. (length (frame-list)))
  1701. (save-buffers-kill-emacs)
  1702. (delete-frame)))
  1703. (define-key my-prefix-map (kbd "C-s") 'my-execute-terminal)
  1704. (define-key my-prefix-map (kbd "C-f") 'make-frame-command-with-name)
  1705. (global-set-key (kbd "C-x C-c") 'my-delete-frame-or-kill-emacs)
  1706. (define-key my-prefix-map (kbd "C-x C-c") 'save-buffers-kill-emacs)
  1707. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1708. ;; x open
  1709. (defvar my-filer nil)
  1710. (setq my-filer (or (executable-find "pcmanfm")
  1711. (executable-find "nautilus")))
  1712. (defun my-x-open (file)
  1713. "open file."
  1714. (interactive "FOpen File: ")
  1715. (setq file (expand-file-name file))
  1716. (message "Opening %s..." file)
  1717. (cond ((eq system-type 'windows-nt)
  1718. (call-process "cmd.exe" nil 0 nil
  1719. "/c" "start" "" (convert-standard-filename file)))
  1720. ((eq system-type 'darwin)
  1721. (call-process "open" nil 0 nil file))
  1722. ((getenv "DISPLAY")
  1723. (call-process (or my-filer "xdg-open") nil 0 nil file))
  1724. (t
  1725. (find-file file))
  1726. )
  1727. ;; (recentf-add-file file)
  1728. (message "Opening %s...done" file))
  1729. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1730. ;; misc funcs
  1731. (defun memo (&optional dir)
  1732. "Open memo.txt"
  1733. (interactive)
  1734. (pop-to-buffer (find-file-noselect (concat (if dir
  1735. (file-name-as-directory dir)
  1736. "")
  1737. "memo.txt"))))
  1738. (defvar my-rgrep-gitgrep
  1739. "git --no-pager -c color.grep=false grep -nH -e "
  1740. "grep command for git grep.")
  1741. (defvar my-rgrep-ag
  1742. "ag --nocolor --nogroup --nopager "
  1743. "grep command for ag")
  1744. (defvar my-rgrep-ack
  1745. "ack --nocolor --nogroup --nopager "
  1746. "grep command for ack")
  1747. (defvar my-rgrep-grep
  1748. (concat "find . "
  1749. "-path '*/.git' -prune -o "
  1750. "-path '*/.svn' -prune -o "
  1751. "-type f -print0 | "
  1752. "xargs -0 -e grep -nH -e ")
  1753. "grep command for grep")
  1754. (defun my-rgrep-grep-command ()
  1755. "Return recursive grep command for current directory."
  1756. (if (eq 0
  1757. (shell-command "git rev-parse --git-dir"))
  1758. my-rgrep-gitgrep
  1759. (if (executable-find "ag")
  1760. my-rgrep-ag
  1761. (if (executable-find "ack")
  1762. my-rgrep-ack
  1763. my-rgrep-grep))))
  1764. (defun my-rgrep (command-args)
  1765. "My recursive grep."
  1766. (interactive (list (read-shell-command "grep command: "
  1767. (my-rgrep-grep-command)
  1768. 'grep-find-history)))
  1769. (compilation-start command-args
  1770. 'grep-mode))
  1771. (defun my-rgrep-ack (command-args)
  1772. "My recursive grep."
  1773. (interactive (list (read-shell-command "grep command: "
  1774. my-rgrep-ack
  1775. 'grep-find-history)))
  1776. (my-rgrep command-args))
  1777. (defun my-rgrep-ag (command-args)
  1778. "My recursive grep."
  1779. (interactive (list (read-shell-command "grep command: "
  1780. my-rgrep-ag
  1781. 'grep-find-history)))
  1782. (my-rgrep command-args))
  1783. (defun my-rgrep-grep (command-args)
  1784. "My recursive grep."
  1785. (interactive (list (read-shell-command "grep command: "
  1786. my-rgrep-grep
  1787. 'grep-find-history)))
  1788. (my-rgrep command-args))
  1789. (define-key ctl-x-map "s" 'my-rgrep)
  1790. ;; (defun make ()
  1791. ;; "Run \"make -k\" in current directory."
  1792. ;; (interactive)
  1793. ;; (compile "make -k"))
  1794. (defalias 'make 'compile)
  1795. (defvar sed-in-place-history nil
  1796. "History of `sed-in-place'")
  1797. (defvar sed-in-place-command "sed --in-place=.bak -e")
  1798. (defun sed-in-place (command)
  1799. "sed in place"
  1800. (interactive (list (read-shell-command "sed in place: "
  1801. (concat sed-in-place-command " ")
  1802. 'sed-in-place-history)))
  1803. (shell-command command
  1804. "*sed in place*"))
  1805. (defun dired-do-sed-in-place (&optional arg)
  1806. "sed in place dired"
  1807. (interactive "P")
  1808. (require 'dired-aux)
  1809. (let* ((files (dired-get-marked-files t arg))
  1810. (expr (dired-mark-read-string "Run sed-in-place for %s: "
  1811. nil
  1812. 'sed-in-place
  1813. arg
  1814. files)))
  1815. (if (equal expr
  1816. "")
  1817. (error "No expression specified")
  1818. (shell-command (concat sed-in-place-command
  1819. " '"
  1820. expr
  1821. "' "
  1822. (mapconcat 'shell-quote-argument
  1823. files
  1824. " "))
  1825. "*sed in place*"))))
  1826. (defun dir-show (&optional dir)
  1827. (interactive)
  1828. (let ((bf (get-buffer-create "*dir show*"))
  1829. (list-directory-brief-switches "-C"))
  1830. (with-current-buffer bf
  1831. (list-directory (or nil
  1832. default-directory)
  1833. nil))
  1834. ))
  1835. (defun my-convmv-sjis2utf8-test ()
  1836. "Run `convmv -r -f sjis -t utf8 *'.
  1837. this is test, does not rename files."
  1838. (interactive)
  1839. (shell-command "convmv -r -f sjis -t utf8 *"))
  1840. (defun my-convmv-sjis2utf8-notest ()
  1841. "Run `convmv -r -f sjis -t utf8 * --notest'."
  1842. (interactive)
  1843. (shell-command "convmv -r -f sjis -t utf8 * --notest"))
  1844. (defun kill-ring-save-buffer-file-name ()
  1845. "Get current filename."
  1846. (interactive)
  1847. (let ((file buffer-file-name))
  1848. (if file
  1849. (progn (kill-new file)
  1850. (message file))
  1851. (message "not visiting file."))))
  1852. (defvar kill-ring-buffer-name "*kill-ring*"
  1853. "Buffer name for `kill-ring-buffer'.")
  1854. (defun open-kill-ring-buffer ()
  1855. "Open kill- ring buffer."
  1856. (interactive)
  1857. (pop-to-buffer
  1858. (with-current-buffer (get-buffer-create kill-ring-buffer-name)
  1859. (erase-buffer)
  1860. (yank)
  1861. (text-mode)
  1862. (current-local-map)
  1863. (goto-char (point-min)))))
  1864. ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1865. ;; ;; savage emacs
  1866. ;; ;; when enabled emacs fails to complete
  1867. ;; ;; http://e-arrows.sakura.ne.jp/2010/05/emacs-should-be-more-savage.html
  1868. ;; (defadvice message (before message-for-stupid (arg &rest arg2) activate)
  1869. ;; (setq arg
  1870. ;; (concat arg
  1871. ;; (if (eq nil
  1872. ;; (string-match "\\. *$"
  1873. ;; arg))
  1874. ;; ".")
  1875. ;; " Stupid!")))
  1876. (defvar my-system-info
  1877. nil
  1878. "System info in the form of \"[user@host] \".")
  1879. (setq my-system-info
  1880. (concat "["
  1881. user-login-name
  1882. "@"
  1883. (car (split-string system-name
  1884. "\\."))
  1885. "] "))
  1886. (defadvice read-from-minibuffer (before info-in-prompt activate)
  1887. (ad-set-arg 0
  1888. (concat my-system-info
  1889. (ad-get-arg 0))))
  1890. (defadvice read-string (before info-in-prompt activate)
  1891. (ad-set-arg 0
  1892. (concat my-system-info
  1893. (ad-get-arg 0))))
  1894. (when (< emacs-major-version 24)
  1895. (defadvice completing-read (before info-in-prompt activate)
  1896. (ad-set-arg 0
  1897. (concat my-system-info
  1898. (ad-get-arg 0)))))
  1899. (defun my-real-function-subr-p (function)
  1900. "Return t if FUNCTION is a built-in function even if it is advised."
  1901. (let* ((advised (and (symbolp function)
  1902. (featurep 'advice)
  1903. (ad-get-advice-info function)))
  1904. (real-function
  1905. (or (and advised (let ((origname (cdr (assq 'origname advised))))
  1906. (and (fboundp origname)
  1907. origname)))
  1908. function))
  1909. (def (if (symbolp real-function)
  1910. (symbol-function real-function)
  1911. function)))
  1912. (subrp def)))
  1913. ;; (my-real-function-subr-p 'my-real-function-subr-p)
  1914. ;;; emacs.el ends here