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.
 
 
 
 
 
 

2034 lines
71 KiB

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