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.
 
 
 
 
 
 

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