Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

2035 linhas
72 KiB

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