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.
 
 
 
 
 
 

2572 lines
88 KiB

  1. ;;; emacs.el --- 10sr emacs initialization
  2. ;;; Commentary:
  3. ;;; Code:
  4. ;; (and (file-readable-p "~/.dotfiles/emacs.el")
  5. ;; (load-file "~/.dotfiles/emacs.el"))
  6. ;; make directories
  7. (unless (file-directory-p (expand-file-name user-emacs-directory))
  8. (make-directory (expand-file-name user-emacs-directory)))
  9. (let ((d (expand-file-name (concat user-emacs-directory
  10. "lisp"))))
  11. (unless (file-directory-p d)
  12. (make-directory d))
  13. (add-to-list 'load-path d))
  14. (eval-when-compile
  15. (require 'cl nil t))
  16. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  17. ;; download library from web
  18. (defun fetch-library (url &optional byte-compile-p force-download-p)
  19. "Download a library from URL and locate it in \"~/emacs.d/lisp/\".
  20. Return nil if library unfound and failed to download,
  21. otherwise the path where the library installed.
  22. If BYTE-COMPILE-P is t byte compile the file after downloading.
  23. If FORCE-DOWNLOAD-P it t ignore exisiting library and always download."
  24. (let* ((dir (expand-file-name (concat user-emacs-directory "lisp/")))
  25. (lib (file-name-sans-extension (file-name-nondirectory url)))
  26. (lpath (concat dir lib ".el"))
  27. (locate-p (locate-library lib)))
  28. (if (or force-download-p (not locate-p))
  29. (if (progn (message "Downloading %s..."
  30. url)
  31. (download-file url
  32. lpath
  33. t))
  34. (progn (message "Downloading %s...done"
  35. url)
  36. (when (and byte-compile-p
  37. (require 'bytecomp nil t))
  38. (and (file-exists-p (byte-compile-dest-file lpath))
  39. (delete-file (byte-compile-dest-file lpath)))
  40. (byte-compile-file lpath)))
  41. (progn (and (file-writable-p lpath)
  42. (delete-file lpath))
  43. (message "Downloading %s...failed"
  44. url))))
  45. (locate-library lib)))
  46. (defun download-file (url path &optional ok-if-already-exists)
  47. "Download file from URL and output to PATH.
  48. IF OK-IF-ALREADY-EXISTS is true force download."
  49. (or
  50. (let ((curl (executable-find "curl")))
  51. (when curl
  52. (if (and (not ok-if-already-exists)
  53. (file-exists-p path))
  54. nil
  55. (and (eq 0
  56. (call-process curl
  57. nil
  58. nil
  59. nil
  60. "--output"
  61. path
  62. url
  63. ))
  64. path))))
  65. (ignore-errors
  66. (require 'url)
  67. (url-copy-file url
  68. path
  69. ok-if-already-exists)
  70. path)))
  71. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  72. ;; package
  73. (defvar my-package-list nil
  74. "Package list just for me.")
  75. (setq my-package-list
  76. '(
  77. markdown-mode
  78. yaml-mode
  79. ;; ack
  80. color-moccur
  81. gtags
  82. flymake-jslint
  83. flymake-python-pyflakes
  84. xclip
  85. gnuplot-mode
  86. erlang
  87. )
  88. )
  89. (when (require 'package nil t)
  90. (add-to-list 'package-archives
  91. '("ELPA" . "http://tromey.com/elpa/"))
  92. (add-to-list 'package-archives
  93. '("melpa" . "http://melpa.milkbox.net/packages/")
  94. t)
  95. (add-to-list 'package-archives
  96. '("marmalade" . "http://marmalade-repo.org/packages/"))
  97. (package-initialize)
  98. ;; (package-refresh-contents)
  99. (defun my-auto-install-package ()
  100. "Install packages semi-automatically."
  101. (interactive)
  102. (mapc (lambda (pkg)
  103. (or (package-installed-p pkg)
  104. (locate-library (symbol-name pkg))
  105. (package-install pkg)))
  106. my-package-list))
  107. )
  108. ;; (lazy-load-eval 'sudoku)
  109. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  110. ;; autoload
  111. (defmacro lazy-load-eval (feature &optional functions &rest body)
  112. "Define autoloading FEATURE that defines FUNCTIONS.
  113. FEATURE is a symbol. FUNCTIONS is a list of symbols. If FUNCTIONS is nil,
  114. the function same as FEATURE is defined as autoloaded function. BODY is passed
  115. to `eval-after-load'.
  116. When this macro is evaluated, this returns the path to library if FEATURE
  117. found, otherwise returns nil."
  118. (let* ((libname (symbol-name (eval feature)))
  119. (libpath (locate-library libname)))
  120. (and libpath
  121. `(progn
  122. ,@(mapcar (lambda (f)
  123. (unless (fboundp f)
  124. `(progn
  125. (message "Autoloaded function `%S' defined (%s)"
  126. (quote ,f)
  127. ,libpath)
  128. (autoload (quote ,f)
  129. ,libname
  130. ,(concat "Autoloaded function defined in \""
  131. libpath
  132. "\".")
  133. t))))
  134. (or (eval functions)
  135. `(,(eval feature))))
  136. (eval-after-load ,feature
  137. (quote (progn
  138. ,@body)))
  139. (locate-library ,libname)))))
  140. (put 'lazy-load-eval 'lisp-indent-function 2)
  141. (when (lazy-load-eval 'tetris nil
  142. (message "Tetris loaded!"))
  143. (message "Tetris found!"))
  144. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  145. ;; my-idle-hook
  146. (defvar my-idle-hook nil
  147. "Hook run when idle for several secs.")
  148. (defvar my-idle-hook-sec 5
  149. "Second to run `my-idle-hook'.")
  150. (run-with-idle-timer my-idle-hook-sec
  151. t
  152. (lambda ()
  153. (run-hooks 'my-idle-hook)))
  154. ;; (add-hook 'my-idle-hook
  155. ;; (lambda ()
  156. ;; (message "idle hook message")))
  157. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  158. ;; start and quit
  159. (setq inhibit-startup-message t)
  160. (setq confirm-kill-emacs 'y-or-n-p)
  161. (setq gc-cons-threshold (* 1024 1024 4))
  162. (when window-system
  163. (add-to-list 'default-frame-alist '(cursor-type . box))
  164. (add-to-list 'default-frame-alist '(background-color . "white"))
  165. (add-to-list 'default-frame-alist '(foreground-color . "gray10"))
  166. ;; (add-to-list 'default-frame-alist '(alpha . (80 100 100 100)))
  167. ;; does not work?
  168. )
  169. ;; (add-to-list 'default-frame-alist '(cursor-type . box))
  170. (if window-system (menu-bar-mode 1) (menu-bar-mode 0))
  171. (and (fboundp 'tool-bar-mode)
  172. (tool-bar-mode 0))
  173. (and (fboundp 'set-scroll-bar-mode)
  174. (set-scroll-bar-mode nil))
  175. (add-hook 'kill-emacs-hook
  176. ;; load init file when terminating emacs to ensure file is not broken
  177. 'reload-init-file)
  178. (add-hook 'after-init-hook
  179. (lambda ()
  180. (message "%s %s" invocation-name emacs-version)
  181. (message "%s was taken to initialize emacs." (emacs-init-time))
  182. (switch-to-buffer "*Messages*")
  183. ))
  184. (cd ".") ; when using windows use / instead of \ in `default-directory'
  185. ;; locale
  186. (set-language-environment "Japanese")
  187. (set-default-coding-systems 'utf-8-unix)
  188. (prefer-coding-system 'utf-8-unix)
  189. (setq system-time-locale "C")
  190. ;; my prefix map
  191. (defvar my-prefix-map nil
  192. "My prefix map.")
  193. (define-prefix-command 'my-prefix-map)
  194. (define-key ctl-x-map (kbd "C-x") 'my-prefix-map)
  195. (define-key my-prefix-map (kbd "C-q") 'quoted-insert)
  196. (define-key my-prefix-map (kbd "C-z") 'suspend-frame)
  197. ;; (comint-show-maximum-output)
  198. ;; kill scratch
  199. (add-hook 'after-init-hook
  200. (lambda ()
  201. (kill-buffer "*scratch*")))
  202. ;; modifier keys
  203. ;; (setq mac-option-modifier 'control)
  204. ;; display
  205. (setq redisplay-dont-pause t)
  206. (setq visible-bell t)
  207. (setq ring-bell-function 'ignore)
  208. (mouse-avoidance-mode 'banish)
  209. (and window-system
  210. (fetch-library
  211. "https://raw.github.com/10sr/emacs-lisp/master/save-window-size.el"
  212. t)
  213. (require 'save-window-size nil t))
  214. (defun reload-init-file ()
  215. "Reload Emacs init file."
  216. (interactive)
  217. (when (file-readable-p user-init-file)
  218. (load-file user-init-file)))
  219. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  220. ;; for windows
  221. (defun start-ckw-bash ()
  222. "Start ckw in windows."
  223. (interactive)
  224. (start-process
  225. "ckw_bash"
  226. nil
  227. "C:/Documents and Settings/sr/Application Data/dbx/apps/ckw/ckw.exe"))
  228. ;; command seems to have to be in c drive
  229. (defun my-w32-add-export-path (&rest args)
  230. "Add pathes ARGS for windows."
  231. (mapc (lambda (path)
  232. (add-to-list 'exec-path (expand-file-name path)))
  233. (reverse args))
  234. (setenv "PATH"
  235. (mapconcat 'convert-standard-filename
  236. exec-path
  237. ";")))
  238. (when (eq system-type 'windows-nt)
  239. ;; (setq scheme-program-name "\"c:/Program Files/Gauche/bin/gosh.exe\" -i")
  240. ;; (setq python-python-command "c:/Python26/python.exe")
  241. ;; (define-key my-prefix-map (kbd "C-c") 'start-ckw-bash)
  242. (my-w32-add-export-path "c:/Windows/system"
  243. "c:/Windows/System32"
  244. "c:/Program Files/Git/bin"
  245. "c:/MinGW/bin"
  246. "c:/MinGW/mingw32/bin"
  247. (expand-file-name "~/.local/bin")
  248. (expand-file-name "~/dbx/apps/bin"))
  249. (when window-system
  250. (setq w32-enable-synthesized-fonts t))
  251. (setq w32-apps-modifier 'meta)
  252. (setq file-name-coding-system 'sjis))
  253. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  254. ;; global keys
  255. (global-set-key (kbd "<up>") 'scroll-down-line)
  256. (global-set-key (kbd "<down>") 'scroll-up-line)
  257. (global-set-key (kbd "<left>") 'scroll-down)
  258. (global-set-key (kbd "<right>") 'scroll-up)
  259. ;; (define-key my-prefix-map (kbd "C-h") help-map)
  260. (global-set-key (kbd "C-\\") help-map)
  261. (define-key ctl-x-map (kbd "DEL") help-map)
  262. (define-key ctl-x-map (kbd "C-h") help-map)
  263. (define-key help-map "a" 'apropos)
  264. ;; disable annoying keys
  265. (global-set-key [prior] 'ignore)
  266. (global-set-key (kbd "<next>") 'ignore)
  267. (global-set-key [menu] 'ignore)
  268. (global-set-key [down-mouse-1] 'ignore)
  269. (global-set-key [down-mouse-2] 'ignore)
  270. (global-set-key [down-mouse-3] 'ignore)
  271. (global-set-key [mouse-1] 'ignore)
  272. (global-set-key [mouse-2] 'ignore)
  273. (global-set-key [mouse-3] 'ignore)
  274. (global-set-key (kbd "<eisu-toggle>") 'ignore)
  275. (global-set-key (kbd "C-<eisu-toggle>") 'ignore)
  276. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  277. ;; title and mode-line
  278. (when (fetch-library
  279. "https://raw.github.com/10sr/emacs-lisp/master/terminal-title.el"
  280. t)
  281. (require 'terminal-title nil t)
  282. ;; if TERM is not screen use default value
  283. (if (getenv "TMUX")
  284. ;; if use tmux locally just basename of current dir
  285. (setq terminal-title-format
  286. '((file-name-nondirectory (directory-file-name
  287. default-directory))))
  288. (when (equal (car (split-string (frame-parameter nil
  289. 'tty-type)
  290. "-"))
  291. "screen")
  292. (if (getenv "SSH_CONNECTION")
  293. ;; TMUX is not set but TERM is screen:
  294. ;; it seems that TMUX is used by locally and ssh to remote host
  295. (setq terminal-title-format
  296. `("em:"
  297. ,user-login-name
  298. "@"
  299. ,(car (split-string system-name
  300. "\\."))
  301. ":"
  302. (file-name-nondirectory (directory-file-name
  303. default-directory))))
  304. ;; this wont happen? (TMUX is not set, TERM is screen, not ssh-ed)
  305. (setq terminal-title-format
  306. '((file-name-nondirectory (directory-file-name
  307. default-directory)))))))
  308. (terminal-title-mode))
  309. (setq eol-mnemonic-dos "\\r\\n")
  310. (setq eol-mnemonic-mac "\\r")
  311. (setq eol-mnemonic-unix "\\n")
  312. (which-function-mode 0)
  313. (line-number-mode 0)
  314. (column-number-mode 0)
  315. (size-indication-mode 0)
  316. (setq mode-line-position
  317. '(:eval (format "L%%l/%d,C%%c"
  318. (count-lines (point-max)
  319. (point-min)))))
  320. ;; http://www.geocities.jp/simizu_daisuke/bunkei-meadow.html#frame-title
  321. ;; display date
  322. (add-hook 'after-init-hook
  323. (lambda ()
  324. (when display-time-mode
  325. (display-time-update))
  326. ))
  327. (when (require 'time nil t)
  328. (setq display-time-interval 29)
  329. (setq display-time-day-and-date t)
  330. (setq display-time-format "%a, %d %b %Y %T")
  331. (if window-system
  332. (display-time-mode 0)
  333. (display-time-mode 1)))
  334. ;; ;; current directory
  335. ;; (let ((ls (member 'mode-line-buffer-identification
  336. ;; mode-line-format)))
  337. ;; (setcdr ls
  338. ;; (cons '(:eval (concat " ("
  339. ;; (abbreviate-file-name default-directory)
  340. ;; ")"))
  341. ;; (cdr ls))))
  342. ;; ;; display last modified time
  343. ;; (let ((ls (member 'mode-line-buffer-identification
  344. ;; mode-line-format)))
  345. ;; (setcdr ls
  346. ;; (cons '(:eval (concat " "
  347. ;; my-buffer-file-last-modified-time))
  348. ;; (cdr ls))))
  349. (defun buffer-list-not-start-with-space ()
  350. "Return a list of buffers that not start with whitespaces."
  351. (let ((bl (buffer-list))
  352. b nbl)
  353. (while bl
  354. (setq b (pop bl))
  355. (unless (string-equal " "
  356. (substring (buffer-name b)
  357. 0
  358. 1))
  359. (add-to-list 'nbl b)))
  360. nbl))
  361. ;; http://www.masteringemacs.org/articles/2012/09/10/hiding-replacing-modeline-strings/
  362. ;; (add-to-list 'minor-mode-alist
  363. ;; '(global-whitespace-mode ""))
  364. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  365. ;; system info
  366. (defun my-message-current-info ()
  367. "Echo current login name, hostname and directory."
  368. (interactive)
  369. (message "%s@%s:%s"
  370. user-login-name
  371. system-name
  372. (abbreviate-file-name default-directory)))
  373. ;; (run-with-idle-timer 3
  374. ;; t
  375. ;; 'my-message-current-info)
  376. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  377. ;; minibuffer
  378. (setq insert-default-directory t)
  379. (setq completion-ignore-case t
  380. read-file-name-completion-ignore-case t
  381. read-buffer-completion-ignore-case t)
  382. (setq resize-mini-windows t)
  383. (temp-buffer-resize-mode 1)
  384. (savehist-mode 1)
  385. (fset 'yes-or-no-p 'y-or-n-p)
  386. ;; complete symbol when `eval'
  387. (define-key read-expression-map (kbd "TAB") 'lisp-complete-symbol)
  388. (define-key minibuffer-local-map (kbd "C-u")
  389. (lambda () (interactive) (delete-region (point-at-bol) (point))))
  390. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  391. ;; letters, font-lock mode and fonts
  392. ;; (set-face-background 'vertical-border (face-foreground 'mode-line))
  393. ;; (set-window-margins (selected-window) 1 1)
  394. (and (or (eq system-type 'Darwin)
  395. (eq system-type 'darwin))
  396. (fboundp 'mac-set-input-method-parameter)
  397. (mac-set-input-method-parameter 'japanese 'cursor-color "red")
  398. (mac-set-input-method-parameter 'roman 'cursor-color "black"))
  399. (when (and (boundp 'input-method-activate-hook) ; i dont know this is correct
  400. (boundp 'input-method-inactivate-hook))
  401. (add-hook 'input-method-activate-hook
  402. (lambda () (set-cursor-color "red")))
  403. (add-hook 'input-method-inactivate-hook
  404. (lambda () (set-cursor-color "black"))))
  405. (when (require 'paren nil t)
  406. (show-paren-mode 1)
  407. (setq show-paren-delay 0.5
  408. show-paren-style 'parenthesis) ; mixed is hard to read
  409. (set-face-background 'show-paren-match
  410. (face-foreground 'default))
  411. (set-face-inverse-video-p 'show-paren-match
  412. t))
  413. (transient-mark-mode 1)
  414. (global-font-lock-mode 1)
  415. (setq font-lock-global-modes
  416. '(not
  417. help-mode
  418. eshell-mode
  419. term-mode
  420. Man-mode))
  421. ;; (standard-display-ascii ?\n "$\n")
  422. (defvar my-eol-face
  423. '(("\n" . (0 font-lock-comment-face t nil)))
  424. )
  425. (defvar my-tab-face
  426. '(("\t" . '(0 highlight t nil))))
  427. (defvar my-jspace-face
  428. '(("\u3000" . '(0 highlight t nil))))
  429. (add-hook 'font-lock-mode-hook
  430. (lambda ()
  431. ;; (font-lock-add-keywords nil my-eol-face)
  432. (font-lock-add-keywords nil my-jspace-face)
  433. ))
  434. (when (require 'whitespace nil t)
  435. (add-to-list 'whitespace-display-mappings ; not work
  436. `(tab-mark ?\t ,(vconcat "^I\t")))
  437. (add-to-list 'whitespace-display-mappings
  438. `(newline-mark ?\n ,(vconcat "$\n")))
  439. (setq whitespace-style '(face
  440. trailing ; trailing blanks
  441. newline ; newlines
  442. newline-mark ; use display table for newline
  443. ;; tab-mark
  444. empty ; empty lines at beg or end of buffer
  445. lines-tail ; lines over 80
  446. ))
  447. ;; (setq whitespace-newline 'font-lock-comment-face)
  448. (global-whitespace-mode t)
  449. (when (eq (display-color-cells)
  450. 256)
  451. (set-face-foreground 'whitespace-newline "brightblack")))
  452. (and nil
  453. (fetch-library
  454. "http://www.emacswiki.org/emacs/download/fill-column-indicator.el"
  455. t)
  456. (require 'fill-column-indicator nil t)
  457. (setq fill-column-indicator))
  458. ;; highlight current line
  459. ;; http://wiki.riywo.com/index.php?Meadow
  460. (defface my-hl-line
  461. '((((min-colors 256)
  462. (background dark))
  463. (:background "color-234"))
  464. (((min-colors 256)
  465. (background light))
  466. (:background "color-234"))
  467. (t
  468. (:underline "black")))
  469. "*Face used by hl-line.")
  470. (setq hl-line-face 'my-hl-line) ;; (setq hl-line-face nil)
  471. (global-hl-line-mode 1) ;; (hl-line-mode 1)
  472. (setq hl-line-global-modes
  473. '(not
  474. term-mode))
  475. (set-face-foreground 'font-lock-regexp-grouping-backslash "#666")
  476. (set-face-foreground 'font-lock-regexp-grouping-construct "#f60")
  477. ;; fonts
  478. (defun my-set-ascii-and-jp-font (list)
  479. "Set font configuration List."
  480. (let ((fspec1 (if (> emacs-major-version 22)
  481. ;; font spec is available in emacs23 and later
  482. (font-spec :family (nth 2 list) :size (nth 3 list))
  483. (cons (nth 2 list) "jisx0208.*")))
  484. (fspec2 (if (> emacs-major-version 22)
  485. (font-spec :family (nth 2 list) :size (nth 3 list))
  486. (cons (nth 2 list) "jisx0201.*"))))
  487. (set-face-attribute 'default nil
  488. :family (nth 0 list)
  489. :height (nth 1 list))
  490. (set-fontset-font "fontset-default"
  491. 'japanese-jisx0208
  492. fspec1)
  493. (set-fontset-font "fontset-default"
  494. 'katakana-jisx0201
  495. fspec2)))
  496. ;; (my-set-ascii-and-jp-font '("dejavu sans mono" 90 "takaogothic" 13))
  497. ;; (my-set-ascii-and-jp-font '("dejavu sans mono" 100 "takaogothic" 14))
  498. ;; (my-set-ascii-and-jp-font '("dejavu sans mono" 100 "ms gothic" 14))
  499. ;; (my-set-ascii-and-jp-font '("monaco" 75 "takaogothic" 11))
  500. ;; (my-set-ascii-and-jp-font '("monaco" 90 "takaogothic" 13))
  501. ;; (my-set-ascii-and-jp-font '("ProggyCleanTTSZ" 120 "takaogothic" 11))
  502. ;; あ a
  503. (and (fetch-library
  504. "https://raw.github.com/10sr/emacs-lisp/master/set-modeline-color.el"
  505. t)
  506. (progn
  507. (require 'set-modeline-color nil t)))
  508. (let ((fg (face-foreground 'default))
  509. (bg (face-background 'default)))
  510. (set-face-background 'mode-line-inactive
  511. (if (face-inverse-video-p 'mode-line) fg bg))
  512. (set-face-foreground 'mode-line-inactive
  513. (if (face-inverse-video-p 'mode-line) bg fg)))
  514. (set-face-underline 'mode-line-inactive
  515. t)
  516. (set-face-underline 'vertical-border
  517. nil)
  518. (and (fetch-library
  519. "https://raw.github.com/tarao/elisp/master/end-mark.el"
  520. t)
  521. (require 'end-mark nil t)
  522. (global-end-mark-mode))
  523. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  524. ;; file handling
  525. (setq revert-without-query '(".+"))
  526. ;; save cursor position
  527. (setq save-place-file (concat user-emacs-directory
  528. "places"))
  529. (when (require 'saveplace nil t)
  530. (setq-default save-place t))
  531. ;; http://www.bookshelf.jp/soft/meadow_24.html#SEC260
  532. (setq make-backup-files t)
  533. ;; (make-directory (expand-file-name "~/.emacsbackup"))
  534. (setq backup-directory-alist
  535. (cons (cons "\\.*$" (expand-file-name "~/.emacs.d/backup"))
  536. backup-directory-alist))
  537. (setq version-control 'never)
  538. (setq delete-old-versions t)
  539. (setq auto-save-list-file-prefix (expand-file-name "~/.emacs.d/auto-save/"))
  540. (setq delete-auto-save-files t)
  541. (add-to-list 'completion-ignored-extensions ".bak")
  542. ;; (setq delete-by-moving-to-trash t
  543. ;; trash-directory "~/.emacs.d/trash")
  544. (add-hook 'after-save-hook
  545. 'executable-make-buffer-file-executable-if-script-p)
  546. (setq bookmark-default-file "~/.emacs.d/bmk")
  547. (add-hook 'recentf-load-hook
  548. (lambda ()
  549. (add-to-list 'recentf-exclude
  550. (regexp-quote bookmark-default-file))))
  551. (and (fetch-library
  552. "https://raw.github.com/10sr/emacs-lisp/master/read-only-only-mode.el"
  553. t)
  554. (lazy-load-eval 'read-only-only-mode))
  555. (and (fetch-library
  556. "https://raw.github.com/10sr/emacs-lisp/master/smart-revert.el"
  557. t)
  558. (require 'smart-revert nil t)
  559. (smart-revert-on))
  560. ;; autosave
  561. (and (fetch-library
  562. "https://raw.github.com/10sr/emacs-lisp/master/autosave.el"
  563. t)
  564. (require 'autosave nil t)
  565. (autosave-set 2))
  566. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  567. ;; editting
  568. (defun my-copy-whole-line ()
  569. "Copy whole line."
  570. (interactive)
  571. (kill-new (concat (buffer-substring (point-at-bol)
  572. (point-at-eol))
  573. "\n")))
  574. (setq require-final-newline t)
  575. (setq kill-whole-line t)
  576. (setq scroll-conservatively 35
  577. scroll-margin 2
  578. scroll-step 0)
  579. (setq-default major-mode 'text-mode)
  580. (setq next-line-add-newlines nil)
  581. (setq kill-read-only-ok t)
  582. (setq truncate-partial-width-windows nil) ; when splitted horizontally
  583. ;; (setq-default line-spacing 0.2)
  584. (setq-default indicate-empty-lines t) ; when using x indicate empty line
  585. (setq-default tab-width 4)
  586. (setq-default indent-tabs-mode nil)
  587. (setq-default indent-line-function nil)
  588. ;; (pc-selection-mode 1) ; make some already defined keybind back to default
  589. (delete-selection-mode 1)
  590. (cua-mode 0)
  591. (setq line-move-visual nil)
  592. ;; key bindings
  593. ;; moving around
  594. ;; (global-set-key (kbd "M-j") 'next-line)
  595. ;; (global-set-key (kbd "M-k") 'previous-line)
  596. ;; (global-set-key (kbd "M-h") 'backward-char)
  597. ;; (global-set-key (kbd "M-l") 'forward-char)
  598. ;;(keyboard-translate ?\M-j ?\C-j)
  599. ;; (global-set-key (kbd "M-p") 'backward-paragraph)
  600. (define-key esc-map "p" 'backward-paragraph)
  601. ;; (global-set-key (kbd "M-n") 'forward-paragraph)
  602. (define-key esc-map "n" 'forward-paragraph)
  603. (global-set-key (kbd "C-<up>") 'scroll-down-line)
  604. (global-set-key (kbd "C-<down>") 'scroll-up-line)
  605. (global-set-key (kbd "C-<left>") 'scroll-down)
  606. (global-set-key (kbd "C-<right>") 'scroll-up)
  607. (global-set-key (kbd "<select>") 'ignore) ; 'previous-line-mark)
  608. (define-key ctl-x-map (kbd "ESC x") 'execute-extended-command)
  609. (define-key ctl-x-map (kbd "ESC :") 'eval-expression)
  610. ;; C-h and DEL
  611. (global-set-key (kbd "C-h") (kbd "DEL"))
  612. (global-set-key (kbd "C-m") 'reindent-then-newline-and-indent)
  613. (global-set-key (kbd "C-o") (kbd "C-e C-m"))
  614. (define-key esc-map "k" 'my-copy-whole-line)
  615. ;; (global-set-key "\C-z" 'undo) ; undo is M-u
  616. (define-key esc-map "u" 'undo)
  617. (define-key esc-map "i" (kbd "ESC TAB"))
  618. ;; (global-set-key (kbd "C-r") 'query-replace-regexp)
  619. (global-set-key (kbd "C-s") 'isearch-forward-regexp)
  620. (global-set-key (kbd "C-r") 'isearch-backward-regexp)
  621. (define-key my-prefix-map (kbd "C-o") 'occur)
  622. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  623. ;; japanese input method
  624. (defun my-load-scim ()
  625. "Use scim-bridge.el as japanese im."
  626. ;; Load scim-bridge.
  627. (when (require 'scim-bridge nil t)
  628. ;; Turn on scim-mode automatically after loading .emacs
  629. (add-hook 'after-init-hook 'scim-mode-on)
  630. (setq scim-cursor-color "red")
  631. (scim-define-preedit-key ?\^h t)
  632. (scim-define-common-key ?\* nil)
  633. (scim-define-common-key ?\^/ nil)))
  634. (defun my-load-anthy ()
  635. "Use anthy.el as japanese im."
  636. ;; anthy
  637. (when (require 'anthy nil t)
  638. (global-set-key
  639. (kbd "<muhenkan>") (lambda () (interactive) (anthy-mode-off)))
  640. (global-set-key (kbd "<henkan>") (lambda () (interactive) (anthy-mode-on)))
  641. (when (>= emacs-major-version 23)
  642. (setq anthy-accept-timeout 1))))
  643. ;; quail
  644. ;; aproposs input-method for some information
  645. ;; (setq default-input-method "japanese")
  646. (defun my-load-mozc-el ()
  647. "Use mozc.el as japanese im."
  648. (setq mozc-leim-title "[MZ]")
  649. (when (require 'mozc nil t)
  650. (setq defauit-input-method "japanese-mozc")
  651. ))
  652. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  653. ;; gmail
  654. (setq mail-interactive t
  655. send-mail-function 'smtpmail-send-it
  656. ;; message-send-mail-function 'smtpmail-send-it
  657. smtpmail-smtp-server "smtp.gmail.com"
  658. smtpmail-smtp-service 587
  659. smtpmail-starttls-credentials '(("smtp.gmail.com" 587
  660. "8.slashes@gmail.com" nil))
  661. smtpmail-auth-credentials '(("smtp.gmail.com" 587
  662. "8.slashes@gmail.com" nil))
  663. user-mail-address "8.slashes@gmail.com")
  664. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  665. ;; buffer killing
  666. ;; (defun my-delete-window-killing-buffer () nil)
  667. (defun my-query-kill-current-buffer ()
  668. "Interactively kill current buffer."
  669. (interactive)
  670. (if (y-or-n-p (concat "kill current buffer? :"))
  671. (kill-buffer (current-buffer))))
  672. (substitute-key-definition 'kill-buffer
  673. 'my-query-kill-current-buffer
  674. global-map)
  675. ;;(global-set-key "\C-xk" 'my-query-kill-current-buffer)
  676. (defun my-kill-buffers ()
  677. "Kill buffers that visit files."
  678. (interactive)
  679. (mapcar (lambda (buf)
  680. (when (buffer-file-name buf)
  681. (kill-buffer buf)))
  682. (buffer-list)))
  683. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  684. ;; share clipboard with x
  685. ;; this page describes this in details, but only these sexps seem to be needed
  686. ;; http://garin.jp/doc/Linux/xwindow_clipboard
  687. (and (not window-system)
  688. (not (eq window-system 'mac))
  689. (getenv "DISPLAY")
  690. (not (equal (getenv "DISPLAY") ""))
  691. (executable-find "xclip")
  692. ;; (< emacs-major-version 24)
  693. (fetch-library "http://www.emacswiki.org/emacs/download/xclip.el" t)
  694. (require 'xclip nil t)
  695. (turn-on-xclip))
  696. (and (eq system-type 'darwin)
  697. (fetch-library
  698. "https://raw.github.com/10sr/emacs-lisp/master/pasteboard.el"
  699. t)
  700. (require 'pasteboard nil t)
  701. (turn-on-pasteboard)
  702. (getenv "TMUX")
  703. (pasteboard-enable-rtun))
  704. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  705. ;; https://github.com/lunaryorn/flycheck
  706. (when (require 'flycheck nil t)
  707. (add-hook 'after-init-hook 'global-flycheck-mode))
  708. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  709. ;; window
  710. (and (fetch-library
  711. "https://raw.github.com/10sr/emacs-lisp/master/window-organizer.el"
  712. t)
  713. (lazy-load-eval 'window-organizer)
  714. (define-key ctl-x-map (kbd "w") 'window-organizer))
  715. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  716. ;; some modes and hooks
  717. (and (fetch-library
  718. "https://raw.github.com/10sr/emacs-lisp/master/remember-major-modes-mode.el"
  719. t)
  720. (require 'remember-major-modes-mode nil t)
  721. (remember-major-modes-mode 1)
  722. )
  723. ;; Detect file type from shebang and set major-mode.
  724. (add-to-list 'interpreter-mode-alist
  725. '("python3" . python-mode))
  726. (add-to-list 'interpreter-mode-alist
  727. '("python2" . python-mode))
  728. ;; http://fukuyama.co/foreign-regexp
  729. '(and (fetch-library
  730. "https://raw.github.com/k-talo/foreign-regexp.el/master/foreign-regexp.el"
  731. t)
  732. (require 'foreign-regexp nil t)
  733. (progn
  734. (setq foreign-regexp/regexp-type 'perl)
  735. '(setq reb-re-syntax 'foreign-regexp)
  736. ))
  737. (require 'session nil t)
  738. (lazy-load-eval 'sql '(sql-mode)
  739. (require 'sql-indent nil t))
  740. (and (fetch-library "https://raw.github.com/10sr/emacs-lisp/master/gtkbm.el"
  741. t)
  742. (lazy-load-eval 'gtkbm)
  743. (global-set-key (kbd "C-x C-d") 'gtkbm))
  744. (and (fetch-library
  745. "https://raw.github.com/10sr/emacs-lisp/master/git-command.el"
  746. t)
  747. (lazy-load-eval 'git-command
  748. nil
  749. (add-to-list 'git-command-major-mode-alist
  750. '("di" . diff-mode))
  751. (add-to-list 'git-command-major-mode-alist
  752. '("graph" . fundamental-mode))
  753. (add-to-list 'git-command-major-mode-alist
  754. '("log" . fundamental-mode)))
  755. ;; (setq git-command-default-options "-c color.ui=always")
  756. (define-key ctl-x-map "g" 'git-command))
  757. (and (fetch-library
  758. "http://www.emacswiki.org/emacs/download/sl.el"
  759. t)
  760. (lazy-load-eval 'sl))
  761. (defalias 'qcalc 'quick-calc)
  762. (require 'simple nil t)
  763. (add-hook 'makefile-mode-hook
  764. (lambda ()
  765. (define-key makefile-mode-map (kbd "C-m") 'newline-and-indent)
  766. ;; this functions is set in write-file-functions, i cannot find any
  767. ;; good way to remove this.
  768. (fset 'makefile-warn-suspicious-lines 'ignore)
  769. ))
  770. (add-hook 'verilog-mode-hook
  771. (lambda ()
  772. (define-key verilog-mode-map ";" 'self-insert-command)))
  773. (setq diff-switches "-u")
  774. (add-hook 'diff-mode-hook
  775. (lambda ()
  776. (when (eq major-mode
  777. 'diff-mode)
  778. ;; do not pass when major-mode is derived mode of diff-mode
  779. (view-mode 1))
  780. (set-face-attribute 'diff-header nil
  781. :foreground nil
  782. :background nil
  783. :weight 'bold)
  784. (set-face-attribute 'diff-file-header nil
  785. :foreground nil
  786. :background nil
  787. :weight 'bold)
  788. (set-face-foreground 'diff-index-face "blue")
  789. (set-face-attribute 'diff-hunk-header nil
  790. :foreground "cyan"
  791. :weight 'normal)
  792. (set-face-attribute 'diff-context nil
  793. ;; :foreground "white"
  794. :foreground nil
  795. :weight 'normal)
  796. (set-face-foreground 'diff-removed-face "red")
  797. (set-face-foreground 'diff-added-face "green")
  798. (set-face-background 'diff-removed-face nil)
  799. (set-face-background 'diff-added-face nil)
  800. (set-face-attribute 'diff-changed nil
  801. :foreground "magenta"
  802. :weight 'normal)
  803. ))
  804. ;; (ffap-bindings)
  805. (add-hook 'sh-mode-hook
  806. (lambda ()
  807. (define-key sh-mode-map
  808. (kbd "C-x C-e")
  809. 'my-execute-shell-command-current-line)))
  810. (setq sh-here-document-word "__EOC__")
  811. (defun my-execute-shell-command-current-line ()
  812. "Run current line as shell command."
  813. (interactive)
  814. (shell-command (buffer-substring-no-properties (point-at-bol)
  815. (point))))
  816. (setq auto-mode-alist
  817. `(("autostart\\'" . sh-mode)
  818. ("xinitrc\\'" . sh-mode)
  819. ("xprograms\\'" . sh-mode)
  820. ("PKGBUILD\\'" . sh-mode)
  821. ,@auto-mode-alist))
  822. (and (lazy-load-eval 'pkgbuild-mode)
  823. (setq auto-mode-alist (append '(("PKGBUILD\\'" . pkgbuild-mode))
  824. auto-mode-alist)))
  825. (add-hook 'html-mode-hook
  826. (lambda ()
  827. (define-key html-mode-map (kbd "C-m")
  828. 'reindent-then-newline-and-indent)))
  829. (add-hook 'text-mode-hook
  830. (lambda ()
  831. (define-key text-mode-map (kbd "C-m") 'newline)))
  832. (add-to-list 'Info-default-directory-list
  833. (expand-file-name "~/.info/emacs-ja"))
  834. (add-hook 'apropos-mode-hook
  835. (lambda ()
  836. (define-key apropos-mode-map "n" 'next-line)
  837. (define-key apropos-mode-map "p" 'previous-line)
  838. ))
  839. (add-hook 'isearch-mode-hook
  840. (lambda ()
  841. ;; (define-key isearch-mode-map
  842. ;; (kbd "C-j") 'isearch-other-control-char)
  843. ;; (define-key isearch-mode-map
  844. ;; (kbd "C-k") 'isearch-other-control-char)
  845. ;; (define-key isearch-mode-map
  846. ;; (kbd "C-h") 'isearch-other-control-char)
  847. (define-key isearch-mode-map (kbd "C-h") 'isearch-delete-char)
  848. (define-key isearch-mode-map (kbd "M-r")
  849. 'isearch-query-replace-regexp)))
  850. ;; do not cleanup isearch highlight: use `lazy-highlight-cleanup' to remove
  851. (setq lazy-highlight-cleanup nil)
  852. ;; face for isearch highlighing
  853. (set-face-attribute 'lazy-highlight
  854. nil
  855. :foreground `unspecified
  856. :background `unspecified
  857. :underline t
  858. ;; :weight `bold
  859. )
  860. (add-hook 'outline-mode-hook
  861. (lambda ()
  862. (if (string-match "\\.md\\'" buffer-file-name)
  863. (set (make-local-variable 'outline-regexp) "#+ "))))
  864. (add-to-list 'auto-mode-alist (cons "\\.ol\\'" 'outline-mode))
  865. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'outline-mode))
  866. (when (fetch-library
  867. "http://jblevins.org/projects/markdown-mode/markdown-mode.el"
  868. t)
  869. (lazy-load-eval 'markdown-mode)
  870. (setq markdown-command (or (executable-find "markdown")
  871. (executable-find "markdown.pl")))
  872. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'markdown-mode))
  873. (add-hook 'markdown-mode-hook
  874. (lambda ()
  875. (outline-minor-mode 1)
  876. (flyspell-mode)
  877. (set (make-local-variable 'comment-start) ";"))))
  878. ;; c-mode
  879. ;; http://www.emacswiki.org/emacs/IndentingC
  880. ;; http://en.wikipedia.org/wiki/Indent_style
  881. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  882. (when (lazy-load-eval 'cc-vars
  883. nil
  884. (add-to-list 'c-default-style
  885. '(c-mode . "k&r"))
  886. (add-to-list 'c-default-style
  887. '(c++-mode . "k&r"))
  888. (add-hook 'c-mode-common-hook
  889. (lambda ()
  890. ;; why c-basic-offset in k&r style defaults to 5 ???
  891. (setq c-basic-offset 4
  892. indent-tabs-mode nil)
  893. ;; (set-face-foreground 'font-lock-keyword-face "blue")
  894. (c-toggle-hungry-state -1)
  895. ;; (and (require 'gtags nil t)
  896. ;; (gtags-mode 1))
  897. ))))
  898. (when (fetch-library
  899. "https://raw.github.com/mooz/js2-mode/master/js2-mode.el"
  900. t)
  901. (lazy-load-eval 'js2-mode)
  902. ;; (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  903. ;; (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode))
  904. (add-hook 'js2-mode-hook
  905. (lambda ()
  906. (define-key js2-mode-map (kbd "C-m") (lambda ()
  907. (interactive)
  908. (js2-enter-key)
  909. (indent-for-tab-command)))
  910. ;; (add-hook (kill-local-variable 'before-save-hook)
  911. ;; 'js2-before-save)
  912. ;; (add-hook 'before-save-hook
  913. ;; 'my-indent-buffer
  914. ;; nil
  915. ;; t)
  916. )))
  917. (when (lazy-load-eval 'flymake-jslint
  918. '(flymake-jslint-load))
  919. (lazy-load-eval 'js nil
  920. (add-hook 'js-mode-hook
  921. 'flymake-jslint-load)))
  922. (require 'js-doc nil t)
  923. (when (require 'uniquify nil t)
  924. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  925. (setq uniquify-ignore-buffers-re "*[^*]+*")
  926. (setq uniquify-min-dir-content 1))
  927. (add-hook 'view-mode-hook
  928. (lambda()
  929. (define-key view-mode-map "j" 'scroll-up-line)
  930. (define-key view-mode-map "k" 'scroll-down-line)
  931. (define-key view-mode-map "v" 'toggle-read-only)
  932. (define-key view-mode-map "q" 'bury-buffer)
  933. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  934. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  935. ;; (define-key view-mode-map
  936. ;; "n" 'nonincremental-repeat-search-forward)
  937. ;; (define-key view-mode-map
  938. ;; "N" 'nonincremental-repeat-search-backward)
  939. (define-key view-mode-map "/" 'isearch-forward-regexp)
  940. (define-key view-mode-map "?" 'isearch-backward-regexp)
  941. (define-key view-mode-map "n" 'isearch-repeat-forward)
  942. (define-key view-mode-map "N" 'isearch-repeat-backward)
  943. (define-key view-mode-map (kbd "C-m") 'my-rgrep-symbol-at-point)
  944. ))
  945. (global-set-key "\M-r" 'view-mode)
  946. ;; (setq view-read-only t)
  947. ;; (defun my-view-mode-search-word (word)
  948. ;; "Search for word current directory and subdirectories.
  949. ;; If called intearctively, find word at point."
  950. ;; (interactive (list (thing-at-point 'symbol)))
  951. ;; (if word
  952. ;; (if (and (require 'gtags nil t)
  953. ;; (gtags-get-rootpath))
  954. ;; (gtags-goto-tag word "s")
  955. ;; (my-rgrep word))
  956. ;; (message "No word at point.")
  957. ;; nil))
  958. (add-hook 'Man-mode-hook
  959. (lambda ()
  960. (view-mode 1)
  961. (setq truncate-lines nil)))
  962. (setq Man-notify-method (if window-system
  963. 'newframe
  964. 'aggressive))
  965. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  966. ;; python
  967. (when (lazy-load-eval 'python '(python-mode))
  968. (setq python-python-command (or (executable-find "python3")
  969. (executable-find "python")))
  970. ;; (defun my-python-run-as-command ()
  971. ;; ""
  972. ;; (interactive)
  973. ;; (shell-command (concat python-python-command " " buffer-file-name)))
  974. (defun my-python-display-python-buffer ()
  975. ""
  976. (interactive)
  977. (set-window-text-height (display-buffer python-buffer
  978. t)
  979. 7))
  980. (add-hook 'python-mode-hook
  981. (lambda ()
  982. (define-key python-mode-map
  983. (kbd "C-c C-e") 'my-python-run-as-command)
  984. (define-key python-mode-map
  985. (kbd "C-c C-b") 'my-python-display-python-buffer)
  986. (define-key python-mode-map (kbd "C-m") 'newline-and-indent)))
  987. (add-hook 'inferior-python-mode-hook
  988. (lambda ()
  989. (my-python-display-python-buffer)
  990. (define-key inferior-python-mode-map
  991. (kbd "<up>") 'comint-previous-input)
  992. (define-key inferior-python-mode-map
  993. (kbd "<down>") 'comint-next-input))))
  994. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  995. ;; GNU GLOBAL(gtags)
  996. ;; http://uguisu.skr.jp/Windows/gtags.html
  997. ;; http://eigyr.dip.jp/gtags.html
  998. ;; http://cha.la.coocan.jp/doc/gnu_global.html
  999. (let ((d "/opt/local/share/gtags/"))
  1000. (and (file-directory-p d)
  1001. (add-to-list 'load-path
  1002. d)))
  1003. (when (lazy-load-eval 'gtags '(gtags-mode))
  1004. (add-hook 'gtags-mode-hook
  1005. (lambda ()
  1006. (view-mode gtags-mode)
  1007. (setq gtags-select-buffer-single t)
  1008. ;; (local-set-key "\M-t" 'gtags-find-tag)
  1009. ;; (local-set-key "\M-r" 'gtags-find-rtag)
  1010. ;; (local-set-key "\M-s" 'gtags-find-symbol)
  1011. ;; (local-set-key "\C-t" 'gtags-pop-stack)
  1012. (define-key gtags-mode-map (kbd "C-x t h")
  1013. 'gtags-find-tag-from-here)
  1014. (define-key gtags-mode-map (kbd "C-x t t") 'gtags-find-tag)
  1015. (define-key gtags-mode-map (kbd "C-x t r") 'gtags-find-rtag)
  1016. (define-key gtags-mode-map (kbd "C-x t s") 'gtags-find-symbol)
  1017. (define-key gtags-mode-map (kbd "C-x t p") 'gtags-find-pattern)
  1018. (define-key gtags-mode-map (kbd "C-x t f") 'gtags-find-file)
  1019. (define-key gtags-mode-map (kbd "C-x t b") 'gtags-pop-stack) ;back
  1020. ))
  1021. (add-hook 'gtags-select-mode-hook
  1022. (lambda ()
  1023. (define-key gtags-select-mode-map (kbd "C-m") 'gtags-select-tag)
  1024. ))
  1025. )
  1026. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1027. ;; term mode
  1028. ;; (setq multi-term-program shell-file-name)
  1029. (and (fetch-library "http://www.emacswiki.org/emacs/download/multi-term.el"
  1030. t)
  1031. (lazy-load-eval 'multi-term)
  1032. (progn
  1033. (setq multi-term-switch-after-close nil)
  1034. (setq multi-term-dedicated-select-after-open-p t)
  1035. (setq multi-term-dedicated-window-height 20)))
  1036. (when (lazy-load-eval 'term '(term ansi-term))
  1037. (defun my-term-quit-or-send-raw ()
  1038. ""
  1039. (interactive)
  1040. (if (get-buffer-process (current-buffer))
  1041. (call-interactively 'term-send-raw)
  1042. (kill-buffer)))
  1043. ;; http://d.hatena.ne.jp/goinger/20100416/1271399150
  1044. ;; (setq term-ansi-default-program shell-file-name)
  1045. (add-hook 'term-setup-hook
  1046. (lambda ()
  1047. (setq term-display-table (make-display-table))))
  1048. (add-hook 'term-mode-hook
  1049. (lambda ()
  1050. (unless (memq (current-buffer)
  1051. (and (featurep 'multi-term)
  1052. ;; current buffer is not multi-term buffer
  1053. (multi-term-list)))
  1054. ;; (define-key term-raw-map "\C-q" 'move-beginning-of-line)
  1055. ;; (define-key term-raw-map "\C-r" 'term-send-raw)
  1056. ;; (define-key term-raw-map "\C-s" 'term-send-raw)
  1057. ;; (define-key term-raw-map "\C-f" 'forward-char)
  1058. ;; (define-key term-raw-map "\C-b" 'backward-char)
  1059. ;; (define-key term-raw-map "\C-t" 'set-mark-command)
  1060. (define-key term-raw-map
  1061. "\C-x" (lookup-key (current-global-map) "\C-x"))
  1062. (define-key term-raw-map
  1063. "\C-z" (lookup-key (current-global-map) "\C-z"))
  1064. )
  1065. ;; (define-key term-raw-map "\C-xl" 'term-line-mode)
  1066. ;; (define-key term-mode-map "\C-xc" 'term-char-mode)
  1067. (define-key term-raw-map (kbd "<up>") 'scroll-down-line)
  1068. (define-key term-raw-map (kbd "<down>") 'scroll-up-line)
  1069. (define-key term-raw-map (kbd "<right>") 'scroll-up)
  1070. (define-key term-raw-map (kbd "<left>") 'scroll-down)
  1071. (define-key term-raw-map (kbd "C-p") 'term-send-raw)
  1072. (define-key term-raw-map (kbd "C-n") 'term-send-raw)
  1073. (define-key term-raw-map "q" 'my-term-quit-or-send-raw)
  1074. ;; (define-key term-raw-map (kbd "ESC") 'term-send-raw)
  1075. (define-key term-raw-map [delete] 'term-send-raw)
  1076. (define-key term-raw-map (kbd "DEL") 'term-send-backspace)
  1077. (define-key term-raw-map "\C-y" 'term-paste)
  1078. (define-key term-raw-map
  1079. "\C-c" 'term-send-raw) ;; 'term-interrupt-subjob)
  1080. '(define-key term-mode-map (kbd "C-x C-q") 'term-pager-toggle)
  1081. ;; (dolist (key '("<up>" "<down>" "<right>" "<left>"))
  1082. ;; (define-key term-raw-map (read-kbd-macro key) 'term-send-raw))
  1083. ;; (define-key term-raw-map "\C-d" 'delete-char)
  1084. (set (make-local-variable 'scroll-margin) 0)
  1085. ;; (set (make-local-variable 'cua-enable-cua-keys) nil)
  1086. ;; (cua-mode 0)
  1087. ;; (and cua-mode
  1088. ;; (local-unset-key (kbd "C-c")))
  1089. ;; (define-key cua--prefix-override-keymap
  1090. ;;"\C-c" 'term-interrupt-subjob)
  1091. (set (make-local-variable 'hl-line-range-function)
  1092. (lambda ()
  1093. '(0 . 0)))
  1094. ))
  1095. ;; (add-hook 'term-exec-hook 'forward-char)
  1096. )
  1097. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1098. ;; buffer switching
  1099. (when (lazy-load-eval 'bs '(bs-show)
  1100. ;; (add-to-list 'bs-configurations
  1101. ;; '("processes" nil get-buffer-process ".*" nil nil))
  1102. (add-to-list 'bs-configurations
  1103. '("this-frame" nil (lambda (buf)
  1104. (memq buf (my-frame-buffer-get)))
  1105. ".*" nil nil))
  1106. (add-to-list 'bs-configurations
  1107. '("files-and-terminals" nil nil nil
  1108. (lambda (buf)
  1109. (and (bs-visits-non-file buf)
  1110. (save-excursion
  1111. (set-buffer buf)
  1112. (not (memq major-mode
  1113. '(term-mode
  1114. eshell-mode))))))))
  1115. ;; (setq bs-configurations (list
  1116. ;; '("processes" nil get-buffer-process ".*" nil nil)
  1117. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil
  1118. ;; bs-visits-non-file bs-sort-buffer-interns-are-last)))
  1119. )
  1120. ;; (global-set-key "\C-x\C-b" 'bs-show)
  1121. (defalias 'list-buffers 'bs-show)
  1122. (setq bs-default-configuration "files-and-terminals")
  1123. (setq bs-default-sort-name "by nothing")
  1124. (add-hook 'bs-mode-hook
  1125. (lambda ()
  1126. ;; (setq bs-default-configuration "files")
  1127. ;; (and bs--show-all
  1128. ;; (call-interactively 'bs-toggle-show-all))
  1129. (set (make-local-variable 'scroll-margin) 0))))
  1130. (iswitchb-mode 1)
  1131. (defun iswitchb-buffer-display-other-window ()
  1132. "Do iswitchb in other window."
  1133. (interactive)
  1134. (let ((iswitchb-default-method 'display))
  1135. (call-interactively 'iswitchb-buffer)))
  1136. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1137. ;; sdic
  1138. (when (lazy-load-eval 'sdic '(sdic-describe-word-at-point))
  1139. ;; (define-key my-prefix-map "\C-w" 'sdic-describe-word)
  1140. (define-key my-prefix-map "\C-t" 'sdic-describe-word-at-point-echo)
  1141. (defun sdic-describe-word-at-point-echo ()
  1142. ""
  1143. (interactive)
  1144. (save-window-excursion
  1145. (sdic-describe-word-at-point))
  1146. (save-excursion
  1147. (set-buffer sdic-buffer-name)
  1148. (message (buffer-substring (point-min)
  1149. (progn (goto-char (point-min))
  1150. (or (and (re-search-forward "^\\w"
  1151. nil
  1152. t
  1153. 4)
  1154. (progn (previous-line) t)
  1155. (point-at-eol))
  1156. (point-max)))))))
  1157. (setq sdic-eiwa-dictionary-list '((sdicf-client "/usr/share/dict/gene.sdic")))
  1158. (setq sdic-waei-dictionary-list
  1159. '((sdicf-client "/usr/share/dict/jedict.sdic" (add-keys-to-headword t))))
  1160. (setq sdic-disable-select-window t)
  1161. (setq sdic-window-height 7))
  1162. ;;;;;;;;;;;;;;;;;;;;;;;;
  1163. ;; ilookup
  1164. (when (fetch-library
  1165. "https://raw.github.com/10sr/emacs-lisp/master/ilookup.el"
  1166. t)
  1167. (lazy-load-eval 'ilookup
  1168. '(ilookup-open)
  1169. (setq ilookup-dict-alist
  1170. '(
  1171. ("en" . (lambda (word)
  1172. (shell-command-to-string
  1173. (format "sdcv -n -u dictd_www.dict.org_gcide '%s'"
  1174. word))))
  1175. ("ja" . (lambda (word)
  1176. (shell-command-to-string
  1177. (format "sdcv -n -u EJ-GENE95 -u jmdict-en-ja '%s'"
  1178. word))))
  1179. ("jaj" . (lambda (word)
  1180. (shell-command-to-string
  1181. (format "sdcv -n -u jmdict-en-ja '%s'"
  1182. word))))
  1183. ("jag" .
  1184. (lambda (word)
  1185. (with-temp-buffer
  1186. (insert (shell-command-to-string
  1187. (format "sdcv -n -u 'Genius English-Japanese' '%s'"
  1188. word)))
  1189. (html2text)
  1190. (buffer-substring (point-min)
  1191. (point-max)))))
  1192. ("alc" . (lambda (word)
  1193. (shell-command-to-string
  1194. (format "alc '%s' | head -n 20"
  1195. word))))
  1196. ("app" . (lambda (word)
  1197. (shell-command-to-string
  1198. (format "dict_app '%s'"
  1199. word))))
  1200. ;; letters broken
  1201. ("ms" .
  1202. (lambda (word)
  1203. (let ((url (concat
  1204. "http://api.microsofttranslator.com/V2/Ajax.svc/"
  1205. "Translate?appId=%s&text=%s&to=%s"))
  1206. (apikey "3C9778666C5BA4B406FFCBEE64EF478963039C51")
  1207. (target "ja")
  1208. (eword (url-hexify-string word)))
  1209. (with-current-buffer (url-retrieve-synchronously
  1210. (format url
  1211. apikey
  1212. eword
  1213. target))
  1214. (message "")
  1215. (goto-char (point-min))
  1216. (search-forward-regexp "^$"
  1217. nil
  1218. t)
  1219. (url-unhex-string (buffer-substring-no-properties
  1220. (point)
  1221. (point-max)))))))
  1222. ))
  1223. ;; (funcall (cdr (assoc "ms"
  1224. ;; ilookup-alist))
  1225. ;; "dictionary")
  1226. ;; (switch-to-buffer (url-retrieve-synchronously "http://api.microsofttranslator.com/V2/Ajax.svc/Translate?appId=3C9778666C5BA4B406FFCBEE64EF478963039C51&text=dictionary&to=ja"))
  1227. ;; (switch-to-buffer (url-retrieve-synchronously "http://google.com"))
  1228. (setq ilookup-default "ja")
  1229. (when (locate-library "google-translate")
  1230. (add-to-list 'ilookup-dict-alist
  1231. '("gt" .
  1232. (lambda (word)
  1233. (save-excursion
  1234. (google-translate-translate "auto"
  1235. "ja"
  1236. word))
  1237. (with-current-buffer "*Google Translate*"
  1238. (buffer-substring-no-properties (point-min)
  1239. (point-max)))))))
  1240. ))
  1241. (when (lazy-load-eval 'google-translate '(google-translate-translate
  1242. google-translate-at-point))
  1243. (setq google-translate-default-source-language "auto")
  1244. (setq google-translate-default-target-language "ja"))
  1245. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1246. ;; vc
  1247. ;; (require 'vc)
  1248. (setq vc-handled-backends '())
  1249. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1250. ;; gauche-mode
  1251. ;; http://d.hatena.ne.jp/kobapan/20090305/1236261804
  1252. ;; http://www.katch.ne.jp/~leque/software/repos/gauche-mode/gauche-mode.el
  1253. (when (and (fetch-library
  1254. "http://www.katch.ne.jp/~leque/software/repos/gauche-mode/gauche-mode.el"
  1255. t)
  1256. (lazy-load-eval 'gauche-mode '(gauche-mode run-scheme)))
  1257. (let ((s (executable-find "gosh")))
  1258. (setq scheme-program-name s
  1259. gauche-program-name s))
  1260. (defun run-gauche-other-window ()
  1261. "Run gauche on other window"
  1262. (interactive)
  1263. (switch-to-buffer-other-window
  1264. (get-buffer-create "*scheme*"))
  1265. (run-gauche))
  1266. (defun run-gauche ()
  1267. "run gauche"
  1268. (run-scheme gauche-program-name)
  1269. )
  1270. (defun scheme-send-buffer ()
  1271. ""
  1272. (interactive)
  1273. (scheme-send-region (point-min) (point-max))
  1274. (my-scheme-display-scheme-buffer)
  1275. )
  1276. (defun my-scheme-display-scheme-buffer ()
  1277. ""
  1278. (interactive)
  1279. (set-window-text-height (display-buffer scheme-buffer
  1280. t)
  1281. 7))
  1282. (add-hook 'scheme-mode-hook
  1283. (lambda ()
  1284. nil))
  1285. (add-hook 'inferior-scheme-mode-hook
  1286. (lambda ()
  1287. ;; (my-scheme-display-scheme-buffer)
  1288. ))
  1289. (setq auto-mode-alist
  1290. (cons '("\.gosh\\'" . gauche-mode) auto-mode-alist))
  1291. (setq auto-mode-alist
  1292. (cons '("\.gaucherc\\'" . gauche-mode) auto-mode-alist))
  1293. (add-hook 'gauche-mode-hook
  1294. (lambda ()
  1295. (define-key gauche-mode-map
  1296. (kbd "C-c C-z") 'run-gauche-other-window)
  1297. (define-key scheme-mode-map
  1298. (kbd "C-c C-c") 'scheme-send-buffer)
  1299. (define-key scheme-mode-map
  1300. (kbd "C-c C-b") 'my-scheme-display-scheme-buffer))))
  1301. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1302. ;; recentf-mode
  1303. (setq recentf-save-file (expand-file-name "~/.emacs.d/recentf")
  1304. recentf-max-menu-items 20
  1305. recentf-max-saved-items 30
  1306. recentf-show-file-shortcuts-flag nil)
  1307. (when (require 'recentf nil t)
  1308. (add-to-list 'recentf-exclude
  1309. (regexp-quote recentf-save-file))
  1310. (add-to-list 'recentf-exclude
  1311. (regexp-quote (expand-file-name user-emacs-directory)))
  1312. (define-key ctl-x-map (kbd "C-r") 'recentf-open-files)
  1313. (add-hook 'find-file-hook
  1314. 'recentf-save-list
  1315. t) ; save to file immediately after adding file to recentf list
  1316. (add-hook 'kill-emacs-hook
  1317. 'recentf-load-list)
  1318. ;;(run-with-idle-timer 5 t 'recentf-save-list)
  1319. ;; (add-hook 'find-file-hook
  1320. ;; (lambda ()
  1321. ;; (recentf-add-file default-directory)))
  1322. (and (fetch-library
  1323. "https://raw.github.com/10sr/emacs-lisp/master/recentf-show.el"
  1324. t)
  1325. (lazy-load-eval 'recentf-show)
  1326. (define-key ctl-x-map (kbd "C-r") 'recentf-show)
  1327. (add-hook 'recentf-show-before-listing-hook
  1328. 'recentf-load-list))
  1329. (recentf-mode 1)
  1330. (add-hook 'recentf-dialog-mode-hook
  1331. (lambda ()
  1332. ;; (recentf-save-list)
  1333. ;; (define-key recentf-dialog-mode-map (kbd "C-x C-f")
  1334. ;; 'my-recentf-cd-and-find-file)
  1335. (define-key recentf-dialog-mode-map (kbd "<up>") 'previous-line)
  1336. (define-key recentf-dialog-mode-map (kbd "<down>") 'next-line)
  1337. (define-key recentf-dialog-mode-map "p" 'previous-line)
  1338. (define-key recentf-dialog-mode-map "n" 'next-line)
  1339. (cd "~/"))))
  1340. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1341. ;; dired
  1342. (when (lazy-load-eval 'dired nil)
  1343. (defun my-dired-echo-file-head (arg)
  1344. ""
  1345. (interactive "P")
  1346. (let ((f (dired-get-filename)))
  1347. (message "%s"
  1348. (with-temp-buffer
  1349. (insert-file-contents f)
  1350. (buffer-substring-no-properties
  1351. (point-min)
  1352. (progn (goto-line (if arg
  1353. (prefix-numeric-value arg)
  1354. 10))
  1355. (point-at-eol)))))))
  1356. (defun my-dired-diff ()
  1357. ""
  1358. (interactive)
  1359. (let ((files (dired-get-marked-files nil nil nil t)))
  1360. (if (eq (car files)
  1361. t)
  1362. (diff (cadr files) (dired-get-filename))
  1363. (message "One files must be marked!"))))
  1364. (defun my-pop-to-buffer-erase-noselect (buffer-or-name)
  1365. "pop up buffer using `display-buffer' and return that buffer."
  1366. (let ((bf (get-buffer-create buffer-or-name)))
  1367. (with-current-buffer bf
  1368. (cd ".")
  1369. (erase-buffer))
  1370. (display-buffer bf)
  1371. bf))
  1372. (defun my-replace-nasi-none ()
  1373. ""
  1374. (save-excursion
  1375. (let ((buffer-read-only nil))
  1376. (goto-char (point-min))
  1377. (while (search-forward "なし" nil t)
  1378. (replace-match "none")))))
  1379. (defun dired-get-file-info ()
  1380. "dired get file info"
  1381. (interactive)
  1382. (let ((f (shell-quote-argument (dired-get-filename t))))
  1383. (if (file-directory-p f)
  1384. (progn
  1385. (message "Calculating disk usage...")
  1386. (shell-command (concat "du -hsD "
  1387. f)))
  1388. (shell-command (concat "file "
  1389. f)))))
  1390. (defun my-dired-scroll-up ()
  1391. ""
  1392. (interactive)
  1393. (my-dired-previous-line (- (window-height) 1)))
  1394. (defun my-dired-scroll-down ()
  1395. ""
  1396. (interactive)
  1397. (my-dired-next-line (- (window-height) 1)))
  1398. ;; (defun my-dired-forward-line (arg)
  1399. ;; ""
  1400. ;; (interactive "p"))
  1401. (defun my-dired-previous-line (arg)
  1402. ""
  1403. (interactive "p")
  1404. (if (> arg 0)
  1405. (progn
  1406. (if (eq (line-number-at-pos)
  1407. 1)
  1408. (goto-char (point-max))
  1409. (forward-line -1))
  1410. (my-dired-previous-line (if (or (dired-get-filename nil t)
  1411. (dired-get-subdir))
  1412. (- arg 1)
  1413. arg)))
  1414. (dired-move-to-filename)))
  1415. (defun my-dired-next-line (arg)
  1416. ""
  1417. (interactive "p")
  1418. (if (> arg 0)
  1419. (progn
  1420. (if (eq (point)
  1421. (point-max))
  1422. (goto-char (point-min))
  1423. (forward-line 1))
  1424. (my-dired-next-line (if (or (dired-get-filename nil t)
  1425. (dired-get-subdir))
  1426. (- arg 1)
  1427. arg)))
  1428. (dired-move-to-filename)))
  1429. (defun my-dired-print-current-dir-and-file ()
  1430. (message "%s %s"
  1431. default-directory
  1432. (buffer-substring-no-properties (point-at-bol)
  1433. (point-at-eol))))
  1434. (defun dired-do-execute-as-command ()
  1435. ""
  1436. (interactive)
  1437. (let ((file (dired-get-filename t)))
  1438. (if (file-executable-p file)
  1439. (start-process file nil file)
  1440. (when (y-or-n-p
  1441. "this file cant be executed. mark as executable and go? : ")
  1442. (set-file-modes file
  1443. (file-modes-symbolic-to-number "u+x" (file-modes file)))
  1444. (start-process file nil file)))))
  1445. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  1446. (defun my-dired-x-open ()
  1447. ""
  1448. (interactive)
  1449. (my-x-open (dired-get-filename t t)))
  1450. (if (eq window-system 'mac)
  1451. (setq dired-listing-switches "-lhF")
  1452. (setq dired-listing-switches "-lhF --time-style=long-iso")
  1453. )
  1454. (setq dired-listing-switches "-lhF")
  1455. (put 'dired-find-alternate-file 'disabled nil)
  1456. ;; when using dired-find-alternate-file
  1457. ;; reuse current dired buffer for the file to open
  1458. (setq dired-ls-F-marks-symlinks t)
  1459. (when (require 'ls-lisp nil t)
  1460. (setq ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  1461. (setq ls-lisp-dirs-first t)
  1462. (setq ls-lisp-use-localized-time-format t)
  1463. (setq ls-lisp-format-time-list
  1464. '("%Y-%m-%d %H:%M"
  1465. "%Y-%m-%d ")))
  1466. (setq dired-dwim-target t)
  1467. ;; (add-hook 'dired-after-readin-hook
  1468. ;; 'my-replace-nasi-none)
  1469. ;; (add-hook 'after-init-hook
  1470. ;; (lambda ()
  1471. ;; (dired ".")))
  1472. (add-hook 'dired-mode-hook
  1473. (lambda ()
  1474. (define-key dired-mode-map "o" 'my-dired-x-open)
  1475. (define-key dired-mode-map "i" 'dired-get-file-info)
  1476. (define-key dired-mode-map "f" 'find-file)
  1477. (define-key dired-mode-map "!" 'shell-command)
  1478. (define-key dired-mode-map "&" 'async-shell-command)
  1479. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  1480. (define-key dired-mode-map "=" 'my-dired-diff)
  1481. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  1482. (define-key dired-mode-map "b" 'gtkbm)
  1483. (define-key dired-mode-map "h" 'my-dired-echo-file-head)
  1484. (define-key dired-mode-map "@" (lambda ()
  1485. (interactive) (my-x-open ".")))
  1486. (define-key dired-mode-map (kbd "TAB") 'other-window)
  1487. ;; (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  1488. (define-key dired-mode-map "/" 'dired-isearch-filenames)
  1489. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  1490. (define-key dired-mode-map (kbd "C-h") 'dired-up-directory)
  1491. (substitute-key-definition 'dired-next-line
  1492. 'my-dired-next-line dired-mode-map)
  1493. (substitute-key-definition 'dired-previous-line
  1494. 'my-dired-previous-line dired-mode-map)
  1495. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  1496. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  1497. (define-key dired-mode-map (kbd "ESC p") 'my-dired-scroll-up)
  1498. (define-key dired-mode-map (kbd "ESC n") 'my-dired-scroll-down)
  1499. (let ((file "._Icon\015"))
  1500. (when nil (file-readable-p file)
  1501. (delete-file file)))))
  1502. (and (fetch-library "https://raw.github.com/10sr/emacs-lisp/master/pack.el"
  1503. t)
  1504. (lazy-load-eval 'pack '(dired-do-pack-or-unpack pack))
  1505. (add-hook 'dired-mode-hook
  1506. (lambda ()
  1507. (define-key dired-mode-map "P" 'dired-do-pack-or-unpack))))
  1508. (and (fetch-library
  1509. "https://raw.github.com/10sr/emacs-lisp/master/dired-list-all-mode.el"
  1510. t)
  1511. (lazy-load-eval 'dired-list-all-mode)
  1512. (setq dired-listing-switches "-lhF")
  1513. (add-hook 'dired-mode-hook
  1514. (lambda ()
  1515. (define-key dired-mode-map "a" 'dired-list-all-mode)
  1516. )))
  1517. ) ; when dired locate
  1518. ;; http://blog.livedoor.jp/tek_nishi/archives/4693204.html
  1519. (defun my-dired-toggle-mark()
  1520. (let ((cur (cond ((eq (following-char) dired-marker-char) ?\040)
  1521. (t dired-marker-char))))
  1522. (delete-char 1)
  1523. (insert cur)))
  1524. (defun my-dired-mark (arg)
  1525. "Toggle mark the current (or next ARG) files.
  1526. If on a subdir headerline, mark all its files except `.' and `..'.
  1527. Use \\[dired-unmark-all-files] to remove all marks
  1528. and \\[dired-unmark] on a subdir to remove the marks in
  1529. this subdir."
  1530. (interactive "P")
  1531. (if (dired-get-subdir)
  1532. (save-excursion (dired-mark-subdir-files))
  1533. (let ((inhibit-read-only t))
  1534. (dired-repeat-over-lines
  1535. (prefix-numeric-value arg)
  1536. 'my-dired-toggle-mark))))
  1537. (defun my-dired-mark-backward (arg)
  1538. "In Dired, move up lines and toggle mark there.
  1539. Optional prefix ARG says how many lines to unflag; default is one line."
  1540. (interactive "p")
  1541. (my-dired-mark (- arg)))
  1542. (add-hook 'dired-mode-hook
  1543. (lambda ()
  1544. (local-set-key (kbd "SPC") 'my-dired-mark)
  1545. (local-set-key (kbd "S-SPC") 'my-dired-mark-backward))
  1546. )
  1547. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1548. ;; eshell
  1549. (lazy-load-eval 'eshell nil
  1550. (defvar eshell-text-mode-map
  1551. (let ((map (make-sparse-keymap)))
  1552. (define-key map (kbd "C-x t") 'eshell-text-mode-toggle)
  1553. map))
  1554. (define-derived-mode eshell-text-mode text-mode
  1555. "Eshell-Text"
  1556. "Text-mode for Eshell."
  1557. nil)
  1558. (defun eshell-text-mode-toggle ()
  1559. "Toggle eshell-text-mode and eshell-mode."
  1560. (interactive)
  1561. (cond ((eq major-mode
  1562. 'eshell-text-mode)
  1563. (goto-char (point-max))
  1564. (eshell-mode))
  1565. ((eq major-mode
  1566. 'eshell-mode)
  1567. (eshell-text-mode))
  1568. (t
  1569. (message "Not in eshell buffer")
  1570. nil)))
  1571. (defun my-eshell-backward-delete-char ()
  1572. (interactive)
  1573. (when (< (save-excursion
  1574. (eshell-bol)
  1575. (point))
  1576. (point))
  1577. (backward-delete-char 1)))
  1578. (defun my-file-owner-p (file)
  1579. "t if FILE is owned by me."
  1580. (eq (user-uid) (nth 2 (file-attributes file))))
  1581. "http://www.bookshelf.jp/pukiwiki/pukiwiki.php\
  1582. ?Eshell%A4%F2%BB%C8%A4%A4%A4%B3%A4%CA%A4%B9"
  1583. ;; ;; written by Stefan Reichoer <reichoer@web.de>
  1584. ;; (defun eshell/less (&rest args)
  1585. ;; "Invoke `view-file' on the file.
  1586. ;; \"less +42 foo\" also goes to line 42 in the buffer."
  1587. ;; (if args
  1588. ;; (while args
  1589. ;; (if (string-match "\\`\\+\\([0-9]+\\)\\'" (car args))
  1590. ;; (let* ((line (string-to-number (match-string 1 (pop args))))
  1591. ;; (file (pop args)))
  1592. ;; (view-file file)
  1593. ;; (goto-line line))
  1594. ;; (view-file (pop args))))))
  1595. (defun eshell/o (&optional file)
  1596. (my-x-open (or file ".")))
  1597. ;; (defun eshell/vi (&rest args)
  1598. ;; "Invoke `find-file' on the file.
  1599. ;; \"vi +42 foo\" also goes to line 42 in the buffer."
  1600. ;; (while args
  1601. ;; (if (string-match "\\`\\+\\([0-9]+\\)\\'" (car args))
  1602. ;; (let* ((line (string-to-number (match-string 1 (pop args))))
  1603. ;; (file (pop args)))
  1604. ;; (find-file file)
  1605. ;; (goto-line line))
  1606. ;; (find-file (pop args)))))
  1607. (defun eshell/clear ()
  1608. "Clear the current buffer, leaving one prompt at the top."
  1609. (interactive)
  1610. (let ((inhibit-read-only t))
  1611. (erase-buffer)))
  1612. (defun eshell-clear ()
  1613. (interactive)
  1614. (let ((inhibit-read-only t))
  1615. (erase-buffer)
  1616. (insert (funcall eshell-prompt-function))))
  1617. (defun eshell/d (&optional dirname switches)
  1618. "if first arg is omitted open current directory."
  1619. (dired (or dirname ".") switches))
  1620. (defun eshell/v ()
  1621. (view-mode 1))
  1622. ;; (defun eshell/aaa (&rest args)
  1623. ;; (message "%S"
  1624. ;; args))
  1625. (defvar eshell/git-cat-command
  1626. nil
  1627. "List of git commands that cat just return strings as results.")
  1628. (setq eshell/git-cat-command
  1629. '("status" "st" "b" "branch" "ls" "ls-files")
  1630. )
  1631. (defun eshell/git (&rest args)
  1632. (if (member (car args)
  1633. eshell/git-cat-command)
  1634. (shell-command-to-string (mapconcat 'shell-quote-argument
  1635. `("git"
  1636. "-c"
  1637. "color.ui=always"
  1638. ,@args)
  1639. " "))
  1640. ;; (eshell-git-shell-command-to-string args)
  1641. (if (require 'git-command nil t)
  1642. (git-command (mapconcat 'shell-quote-argument
  1643. args
  1644. " "))
  1645. (apply 'eshell-exec-visual "git" args))))
  1646. ;; (defun eshell-git-shell-command-to-string (args)
  1647. ;; "Return string of output of ARGS."
  1648. ;; (let ((sargs (mapconcat 'shell-quote-argument
  1649. ;; args
  1650. ;; " ")))
  1651. ;; (if (require 'ansi-color nil t)
  1652. ;; (identity
  1653. ;; (shell-command-to-string (concat "git "
  1654. ;; "-c color.ui=always "
  1655. ;; sargs)))
  1656. ;; (shell-command-to-string (concat "git "
  1657. ;; sargs)))))
  1658. (defalias 'eshell/g 'eshell/git)
  1659. (defalias 'eshell/: 'ignore)
  1660. (defalias 'eshell/type 'eshell/which)
  1661. ;; (defalias 'eshell/vim 'eshell/vi)
  1662. (defalias 'eshell/ff 'find-file)
  1663. (defalias 'eshell/q 'eshell/exit)
  1664. (defun eshell-goto-prompt ()
  1665. ""
  1666. (interactive)
  1667. (goto-char (point-max)))
  1668. (defun eshell-delete-char-or-logout (n)
  1669. (interactive "p")
  1670. (if (equal (eshell-get-old-input)
  1671. "")
  1672. (progn
  1673. (insert "exit")
  1674. (eshell-send-input))
  1675. (delete-char n)))
  1676. (defun eshell-kill-input ()
  1677. (interactive)
  1678. (delete-region (point)
  1679. (progn (eshell-bol)
  1680. (point))))
  1681. (defalias 'eshell/logout 'eshell/exit)
  1682. (defun eshell-cd-default-directory (&optional eshell-buffer-or-name)
  1683. "open eshell and change wd
  1684. if arg given, use that eshell buffer, otherwise make new eshell buffer."
  1685. (interactive)
  1686. (let ((dir (expand-file-name default-directory)))
  1687. (switch-to-buffer (or eshell-buffer-or-name
  1688. (eshell t)))
  1689. (unless (equal dir (expand-file-name default-directory))
  1690. ;; (cd dir)
  1691. ;; (eshell-interactive-print (concat "cd " dir "\n"))
  1692. ;; (eshell-emit-prompt)
  1693. (goto-char (point-max))
  1694. (eshell-kill-input)
  1695. (insert "cd " dir)
  1696. (eshell-send-input))))
  1697. (defadvice eshell-next-matching-input-from-input
  1698. ;; do not cycle history
  1699. (around eshell-history-do-not-cycle activate)
  1700. (if (= 0
  1701. (or eshell-history-index
  1702. 0))
  1703. (progn
  1704. (delete-region eshell-last-output-end (point))
  1705. (insert-and-inherit eshell-matching-input-from-input-string)
  1706. (setq eshell-history-index nil))
  1707. ad-do-it))
  1708. (setq eshell-directory-name "~/.emacs.d/eshell/")
  1709. (setq eshell-term-name "eterm-color")
  1710. (setq eshell-scroll-to-bottom-on-input t)
  1711. (setq eshell-cmpl-ignore-case t)
  1712. (setq eshell-cmpl-cycle-completions nil)
  1713. (setq eshell-highlight-prompt nil)
  1714. (setq eshell-ls-initial-args '("-hCFG"
  1715. "--color=auto"
  1716. "--time-style=long-iso")) ; "-hF")
  1717. (setq eshell-prompt-function
  1718. 'my-eshell-prompt-function)
  1719. (defun my-eshell-prompt-function ()
  1720. (with-temp-buffer
  1721. (let (p1 p2 p3 p4)
  1722. (insert ":: [")
  1723. (setq p1 (point))
  1724. (insert user-login-name
  1725. "@"
  1726. (car (split-string system-name
  1727. "\\."))
  1728. )
  1729. (setq p2 (point))
  1730. (insert ":")
  1731. (setq p3 (point))
  1732. (insert (abbreviate-file-name default-directory))
  1733. (setq p4 (point))
  1734. (insert "]")
  1735. (insert "\n:: ")
  1736. (unless (eq 0
  1737. eshell-last-command-status)
  1738. (insert (format "[STATUS:%d] "
  1739. eshell-last-command-status)))
  1740. (insert (if (= (user-uid)
  1741. 0)
  1742. "# "
  1743. "$ "))
  1744. (add-text-properties p1
  1745. p2
  1746. '(face underline))
  1747. (add-text-properties p3
  1748. p4
  1749. '(face underline))
  1750. (buffer-substring (point-min)
  1751. (point-max)))))
  1752. (add-hook 'eshell-mode-hook
  1753. (lambda ()
  1754. ;; (define-key eshell-mode-map (kbd "C-x C-x") (lambda ()
  1755. ;; (interactive)
  1756. ;; (switch-to-buffer (other-buffer))))
  1757. ;; (define-key eshell-mode-map (kbd "C-g") (lambda ()
  1758. ;; (interactive)
  1759. ;; (eshell-goto-prompt)
  1760. ;; (keyboard-quit)))
  1761. (define-key eshell-mode-map (kbd "C-x t")
  1762. 'eshell-text-mode-toggle)
  1763. (define-key eshell-mode-map (kbd "C-u")
  1764. 'eshell-kill-input)
  1765. (define-key eshell-mode-map (kbd "C-d")
  1766. 'eshell-delete-char-or-logout)
  1767. ;; (define-key eshell-mode-map (kbd "C-l")
  1768. ;; 'eshell-clear)
  1769. (define-key eshell-mode-map (kbd "DEL")
  1770. 'my-eshell-backward-delete-char)
  1771. (define-key eshell-mode-map
  1772. (kbd "C-p") 'eshell-previous-matching-input-from-input)
  1773. (define-key eshell-mode-map
  1774. (kbd "C-n") 'eshell-next-matching-input-from-input)
  1775. (apply 'eshell/addpath exec-path)
  1776. (set (make-local-variable 'scroll-margin) 0)
  1777. ;; (eshell/export "GIT_PAGER=")
  1778. ;; (eshell/export "GIT_EDITOR=")
  1779. (eshell/export "LC_MESSAGES=C")
  1780. (switch-to-buffer (current-buffer)) ; move buffer top of list
  1781. (set (make-local-variable 'hl-line-range-function)
  1782. (lambda ()
  1783. '(0 . 0)))
  1784. (add-to-list 'eshell-virtual-targets
  1785. '("/dev/less"
  1786. (lambda (str)
  1787. (if str
  1788. (with-current-buffer nil)))
  1789. nil))
  1790. ))
  1791. (add-hook 'eshell-mode-hook
  1792. (lambda ()
  1793. (add-to-list 'eshell-visual-commands "vim")
  1794. ;; (add-to-list 'eshell-visual-commands "git")
  1795. (add-to-list 'eshell-output-filter-functions
  1796. 'eshell-truncate-buffer)
  1797. (mapcar (lambda (alias)
  1798. (add-to-list 'eshell-command-aliases-list
  1799. alias))
  1800. '(
  1801. ; ("ll" "ls -l $*")
  1802. ; ("la" "ls -a $*")
  1803. ; ("lla" "ls -al $*")
  1804. ("eless"
  1805. (concat "cat >>> (with-current-buffer "
  1806. "(get-buffer-create \"*eshell output\") "
  1807. "(erase-buffer) "
  1808. "(setq buffer-read-only nil) "
  1809. "(current-buffer)) "
  1810. "(view-buffer (get-buffer \"*eshell output*\"))")
  1811. ))
  1812. )))
  1813. ) ; eval after load eshell
  1814. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1815. ;; frame buffer
  1816. ;; todo: work well when opening files already opened on another window
  1817. (add-hook 'after-make-frame-functions
  1818. (lambda (f)
  1819. (set-window-buffer (frame-selected-window f)
  1820. "*Messages*")))
  1821. (defun make-frame-command-with-name (name)
  1822. "Make frame with NAME specified."
  1823. (interactive "sName for new frame: ")
  1824. (set-frame-parameter (make-frame-command)
  1825. 'name
  1826. name))
  1827. (defvar my-frame-buffer-plist nil)
  1828. (defun my-frame-buffer-add (&optional buf frame)
  1829. "Add BUF to buffer list for FRAME."
  1830. (setq my-frame-buffer-plist
  1831. (plist-put my-frame-buffer-plist
  1832. (or frame
  1833. (selected-frame))
  1834. (let ((lst (my-frame-buffer-get frame)))
  1835. (if lst
  1836. (add-to-list 'lst
  1837. (or buf
  1838. (current-buffer)))
  1839. (list (or buf
  1840. (current-buffer))))))))
  1841. (defun my-frame-buffer-remove (&optional buf frame)
  1842. "Remove BUF from bufferlist for FRAME."
  1843. (setq my-frame-buffer-plist
  1844. (plist-put my-frame-buffer-plist
  1845. (or frame
  1846. (selected-frame))
  1847. (delq (or buf
  1848. (current-buffer))
  1849. (my-frame-buffer-get frame)))))
  1850. (defun my-frame-buffer-get (&optional frame)
  1851. "Get buffer list for FRAME."
  1852. (plist-get my-frame-buffer-plist
  1853. (or frame
  1854. (selected-frame))))
  1855. (defun my-frame-buffer-kill-all-buffer (&optional frame)
  1856. "Kill all buffer of FRAME."
  1857. (mapcar 'kill-buffer
  1858. (my-frame-buffer-get frame)))
  1859. (add-hook 'find-file-hook
  1860. 'my-frame-buffer-add)
  1861. ;; (add-hook 'term-mode-hook
  1862. ;; 'my-frame-buffer-add)
  1863. (add-hook 'eshell-mode-hook
  1864. 'my-frame-buffer-add)
  1865. (add-hook 'Man-mode-hook
  1866. 'my-frame-buffer-add)
  1867. (add-hook 'kill-buffer-hook
  1868. 'my-frame-buffer-remove)
  1869. (add-hook 'delete-frame-functions
  1870. 'my-frame-buffer-kill-all-buffer)
  1871. (defvar my-desktop-terminal "roxterm")
  1872. (defun my-execute-terminal ()
  1873. "Invole terminal program."
  1874. (interactive)
  1875. (if (and (or (eq system-type 'windows-nt)
  1876. window-system)
  1877. my-desktop-terminal
  1878. )
  1879. (let ((process-environment (cons "TERM=xterm" process-environment)))
  1880. (start-process "terminal"
  1881. nil
  1882. my-desktop-terminal))
  1883. (my-term)))
  1884. (defun my-delete-frame-or-kill-emacs ()
  1885. "Delete frame when opening multiple frame, kill Emacs when only one."
  1886. (interactive)
  1887. (if (eq 1
  1888. (length (frame-list)))
  1889. (save-buffers-kill-emacs)
  1890. (delete-frame)))
  1891. ;; (define-key my-prefix-map (kbd "C-s") 'my-execute-terminal)
  1892. (define-key my-prefix-map (kbd "C-f") 'make-frame-command-with-name)
  1893. (global-set-key (kbd "C-x C-c") 'my-delete-frame-or-kill-emacs)
  1894. (define-key my-prefix-map (kbd "C-x C-c") 'save-buffers-kill-emacs)
  1895. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1896. ;; my-term
  1897. (defvar my-term nil
  1898. "My terminal buffer.")
  1899. (defvar my-term-function nil
  1900. "Function to create terminal buffer.
  1901. This function accept no argument and return newly created buffer of terminal.")
  1902. (defun my-term (&optional arg)
  1903. "Open terminal buffer and return that buffer.
  1904. ARG is ignored."
  1905. (interactive "P")
  1906. (if (and my-term
  1907. (buffer-name my-term))
  1908. (pop-to-buffer my-term)
  1909. (setq my-term
  1910. (save-window-excursion
  1911. (funcall my-term-function)))
  1912. (and my-term
  1913. (my-term))))
  1914. ;; (setq my-term-function
  1915. ;; (lambda ()
  1916. ;; (if (eq system-type 'windows-nt)
  1917. ;; (eshell)
  1918. ;; (if (require 'multi-term nil t)
  1919. ;; (multi-term)
  1920. ;; (ansi-term shell-file-name)))))
  1921. (setq my-term-function 'eshell)
  1922. (define-key my-prefix-map (kbd "C-s") 'my-term)
  1923. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1924. ;; x open
  1925. (defvar my-filer nil)
  1926. (setq my-filer (or (executable-find "pcmanfm")
  1927. (executable-find "nautilus")))
  1928. (defun my-x-open (file)
  1929. "open FILE."
  1930. (interactive "FOpen File: ")
  1931. (setq file (expand-file-name file))
  1932. (message "Opening %s..." file)
  1933. (cond ((eq system-type 'windows-nt)
  1934. (call-process "cmd.exe" nil 0 nil
  1935. "/c" "start" "" (convert-standard-filename file)))
  1936. ((eq system-type 'darwin)
  1937. (call-process "open" nil 0 nil file))
  1938. ((getenv "DISPLAY")
  1939. (call-process (or my-filer "xdg-open") nil 0 nil file))
  1940. (t
  1941. (find-file file))
  1942. )
  1943. ;; (recentf-add-file file)
  1944. (message "Opening %s...done" file))
  1945. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1946. ;; misc funcs
  1947. (defun memo (&optional dir)
  1948. "Open memo.txt in DIR."
  1949. (interactive)
  1950. (pop-to-buffer (find-file-noselect (concat (if dir
  1951. (file-name-as-directory dir)
  1952. "")
  1953. "memo.txt"))))
  1954. (defvar my-rgrep-alist
  1955. `(
  1956. ;; the silver searcher
  1957. ("ag"
  1958. (executable-find "ag")
  1959. "ag --nocolor --nogroup --nopager ")
  1960. ;; ack
  1961. ("ack"
  1962. (executable-find "ack")
  1963. "ack --nocolor --nogroup --nopager ")
  1964. ;; gnu global
  1965. ("global"
  1966. (and (require 'gtags nil t)
  1967. (executable-find "global")
  1968. (gtags-get-rootpath))
  1969. "global --result grep ")
  1970. ;; git grep
  1971. ("gitgrep"
  1972. (eq 0
  1973. (shell-command "git rev-parse --git-dir"))
  1974. "git --no-pager -c color.grep=false grep -nH -e ")
  1975. ;; grep
  1976. ("grep"
  1977. t
  1978. ,(concat "find . "
  1979. "-path '*/.git' -prune -o "
  1980. "-path '*/.svn' -prune -o "
  1981. "-type f -print0 | "
  1982. "xargs -0 grep -nH -e "))
  1983. )
  1984. "Alist of rgrep command.
  1985. Each element is in the form like (NAME SEXP COMMAND), where SEXP returns the
  1986. condition to choose COMMAND when evaluated.")
  1987. (defvar my-rgrep-default nil
  1988. "Default command name for my-rgrep.")
  1989. (defun my-rgrep-grep-command (&optional name alist)
  1990. "Return recursive grep command for current directory or nil.
  1991. If NAME is given, use that without testing.
  1992. Commands are searched from ALIST."
  1993. (if alist
  1994. (if name
  1995. ;; if name is given search that from alist and return the command
  1996. (nth 2 (assoc name
  1997. alist))
  1998. ;; if name is not given try test in 1th elem
  1999. (let ((car (car alist))
  2000. (cdr (cdr alist)))
  2001. (if (eval (nth 1 car))
  2002. ;; if the condition is true return the command
  2003. (nth 2 car)
  2004. ;; try next one
  2005. (and cdr
  2006. (my-rgrep-grep-command name cdr)))))
  2007. ;; if alist is not given set default value
  2008. (my-rgrep-grep-command name my-rgrep-alist)))
  2009. (my-rgrep-grep-command "ag" nil)
  2010. (defun my-rgrep (command-args)
  2011. "My recursive grep. Run COMMAND-ARGS."
  2012. (interactive (let ((cmd (my-rgrep-grep-command my-rgrep-default
  2013. nil)))
  2014. (if cmd
  2015. (list (read-shell-command "grep command: "
  2016. cmd
  2017. 'grep-find-history))
  2018. (error "my-rgrep: Command for rgrep not found")
  2019. )))
  2020. (compilation-start command-args
  2021. 'grep-mode))
  2022. ;; (defun my-rgrep-symbol-at-point (command-args)
  2023. ;; "My recursive grep. Run COMMAND-ARGS."
  2024. ;; (interactive (list (read-shell-command "grep command: "
  2025. ;; (concat (my-rgrep-grep-command)
  2026. ;; " "
  2027. ;; (thing-at-point 'symbol))
  2028. ;; 'grep-find-history)))
  2029. ;; (compilation-start command-args
  2030. ;; 'grep-mode))
  2031. (defun my-rgrep-ack ()
  2032. "My recursive grep by ack."
  2033. (interactive)
  2034. (let ((my-rgrep-default "ack"))
  2035. (if (called-interactively-p 'any)
  2036. (call-interactively 'my-rgrep)
  2037. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  2038. (defun my-rgrep-ag ()
  2039. "My recursive grep by ack."
  2040. (interactive)
  2041. (let ((my-rgrep-default "ag"))
  2042. (if (called-interactively-p 'any)
  2043. (call-interactively 'my-rgrep)
  2044. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  2045. (defun my-rgrep-gitgrep ()
  2046. "My recursive grep by ack."
  2047. (interactive)
  2048. (let ((my-rgrep-default "gitgrep"))
  2049. (if (called-interactively-p 'any)
  2050. (call-interactively 'my-rgrep)
  2051. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  2052. (defun my-rgrep-grep ()
  2053. "My recursive grep by ack."
  2054. (interactive)
  2055. (let ((my-rgrep-default "grep"))
  2056. (if (called-interactively-p 'any)
  2057. (call-interactively 'my-rgrep)
  2058. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  2059. (defun my-rgrep-global ()
  2060. "My recursive grep by ack."
  2061. (interactive)
  2062. (let ((my-rgrep-default "global"))
  2063. (if (called-interactively-p 'any)
  2064. (call-interactively 'my-rgrep)
  2065. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  2066. (define-key ctl-x-map "s" 'my-rgrep)
  2067. ;; (defun make ()
  2068. ;; "Run \"make -k\" in current directory."
  2069. ;; (interactive)
  2070. ;; (compile "make -k"))
  2071. (defalias 'make 'compile)
  2072. (defvar sed-in-place-history nil
  2073. "History of `sed-in-place'.")
  2074. (defvar sed-in-place-command "sed --in-place=.bak -e")
  2075. (defun sed-in-place (command)
  2076. "Issue sed in place COMMAND."
  2077. (interactive (list (read-shell-command "sed in place: "
  2078. (concat sed-in-place-command " ")
  2079. 'sed-in-place-history)))
  2080. (shell-command command
  2081. "*sed in place*"))
  2082. (defun dired-do-sed-in-place (&optional arg)
  2083. "Issue sed in place dired. If ARG is given, use the next ARG files."
  2084. (interactive "p")
  2085. (require 'dired-aux)
  2086. (let* ((files (dired-get-marked-files t arg))
  2087. (expr (dired-mark-read-string "Run sed-in-place for %s: "
  2088. nil
  2089. 'sed-in-place
  2090. arg
  2091. files)))
  2092. (if (equal expr
  2093. "")
  2094. (error "No expression specified")
  2095. (shell-command (concat sed-in-place-command
  2096. " '"
  2097. expr
  2098. "' "
  2099. (mapconcat 'shell-quote-argument
  2100. files
  2101. " "))
  2102. "*sed in place*"))))
  2103. (defun dir-show (&optional dir)
  2104. "Show DIR list."
  2105. (interactive)
  2106. (let ((bf (get-buffer-create "*dir show*"))
  2107. (list-directory-brief-switches "-C"))
  2108. (with-current-buffer bf
  2109. (list-directory (or nil
  2110. default-directory)
  2111. nil))
  2112. ))
  2113. (defun my-convmv-sjis2utf8-test ()
  2114. "Run `convmv -r -f sjis -t utf8 *'.
  2115. this is test, does not rename files."
  2116. (interactive)
  2117. (shell-command "convmv -r -f sjis -t utf8 *"))
  2118. (defun my-convmv-sjis2utf8-notest ()
  2119. "Run `convmv -r -f sjis -t utf8 * --notest'."
  2120. (interactive)
  2121. (shell-command "convmv -r -f sjis -t utf8 * --notest"))
  2122. (defun kill-ring-save-buffer-file-name ()
  2123. "Get current filename."
  2124. (interactive)
  2125. (let ((file buffer-file-name))
  2126. (if file
  2127. (progn (kill-new file)
  2128. (message file))
  2129. (message "not visiting file."))))
  2130. (defvar kill-ring-buffer-name "*kill-ring*"
  2131. "Buffer name for `kill-ring-buffer'.")
  2132. (defun open-kill-ring-buffer ()
  2133. "Open kill- ring buffer."
  2134. (interactive)
  2135. (pop-to-buffer
  2136. (with-current-buffer (get-buffer-create kill-ring-buffer-name)
  2137. (erase-buffer)
  2138. (yank)
  2139. (text-mode)
  2140. (current-local-map)
  2141. (goto-char (point-min))
  2142. (yank)
  2143. (current-buffer))))
  2144. (defun set-terminal-header (string)
  2145. "Set terminal header STRING."
  2146. (let ((savepos "\033[s")
  2147. (restorepos "\033[u")
  2148. (movecursor "\033[0;%dH")
  2149. (inverse "\033[7m")
  2150. (restorecolor "\033[0m")
  2151. (cols (frame-parameter nil 'width))
  2152. (length (length string)))
  2153. ;; (redraw-frame (selected-frame))
  2154. (send-string-to-terminal (concat savepos
  2155. (format movecursor
  2156. (1+ (- cols length)))
  2157. inverse
  2158. string
  2159. restorecolor
  2160. restorepos))
  2161. ))
  2162. (defun my-set-terminal-header ()
  2163. "Set terminal header."
  2164. (set-terminal-header (concat " "
  2165. user-login-name
  2166. "@"
  2167. (car (split-string system-name
  2168. "\\."))
  2169. " "
  2170. (format-time-string "%Y/%m/%d %T %z")
  2171. " ")))
  2172. ;; (run-with-timer
  2173. ;; 0.1
  2174. ;; 1
  2175. ;; 'my-set-terminal-header)
  2176. ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2177. ;; ;; savage emacs
  2178. ;; ;; when enabled emacs fails to complete
  2179. ;; ;; http://e-arrows.sakura.ne.jp/2010/05/emacs-should-be-more-savage.html
  2180. ;; (defadvice message (before message-for-stupid (arg &rest arg2) activate)
  2181. ;; (setq arg
  2182. ;; (concat arg
  2183. ;; (if (eq nil
  2184. ;; (string-match "\\. *$"
  2185. ;; arg))
  2186. ;; ".")
  2187. ;; " Stupid!")))
  2188. (defvar info-in-prompt
  2189. nil
  2190. "System info in the form of \"[user@host] \".")
  2191. (setq info-in-prompt
  2192. (concat "["
  2193. user-login-name
  2194. "@"
  2195. (car (split-string system-name
  2196. "\\."))
  2197. "]"))
  2198. (defun my-real-function-subr-p (function)
  2199. "Return t if FUNCTION is a built-in function even if it is advised."
  2200. (let* ((advised (and (symbolp function)
  2201. (featurep 'advice)
  2202. (ad-get-advice-info function)))
  2203. (real-function
  2204. (or (and advised (let ((origname (cdr (assq 'origname advised))))
  2205. (and (fboundp origname)
  2206. origname)))
  2207. function))
  2208. (def (if (symbolp real-function)
  2209. (symbol-function real-function)
  2210. function)))
  2211. (subrp def)))
  2212. ;; (my-real-function-subr-p 'my-real-function-subr-p)
  2213. ;; (defadvice read-from-minibuffer (before info-in-prompt activate)
  2214. ;; "Show system info when use `read-from-minibuffer'."
  2215. ;; (ad-set-arg 0
  2216. ;; (concat my-system-info
  2217. ;; (ad-get-arg 0))))
  2218. ;; (defadvice read-string (before info-in-prompt activate)
  2219. ;; "Show system info when use `read-string'."
  2220. ;; (ad-set-arg 0
  2221. ;; (concat my-system-info
  2222. ;; (ad-get-arg 0))))
  2223. ;; (when (< emacs-major-version 24)
  2224. ;; (defadvice completing-read (before info-in-prompt activate)
  2225. ;; "Show system info when use `completing-read'."
  2226. ;; (ad-set-arg 0
  2227. ;; (concat my-system-info
  2228. ;; (ad-get-arg 0)))))
  2229. (defmacro info-in-prompt-set (&rest functions)
  2230. "Set info-in-prompt advices for FUNCTIONS."
  2231. `(progn
  2232. ,@(mapcar (lambda (f)
  2233. `(defadvice ,f (before info-in-prompt activate)
  2234. "Show info in prompt."
  2235. (let ((orig (ad-get-arg 0)))
  2236. (unless (string-match-p (regexp-quote info-in-prompt)
  2237. orig)
  2238. (ad-set-arg 0
  2239. (concat info-in-prompt
  2240. " "
  2241. orig))))))
  2242. functions)))
  2243. (info-in-prompt-set read-from-minibuffer
  2244. read-string
  2245. completing-read)
  2246. ;;; emacs.el ends here