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.
 
 
 
 
 
 

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