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

2059 lignes
70 KiB

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