Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

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