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.
 
 
 
 
 
 

2746 lines
94 KiB

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