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.
 
 
 
 
 
 

2040 lines
72 KiB

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