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.
 
 
 
 
 
 

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