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

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