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.
 
 
 
 
 
 

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