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.
 
 
 
 
 
 

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