25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

2745 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. ;; (setq git-command-default-options "-c color.ui=always")
  960. (define-key ctl-x-map "g" 'git-command))
  961. (and (fetch-library
  962. "http://www.emacswiki.org/emacs/download/sl.el"
  963. t)
  964. (autoload-eval-lazily 'sl))
  965. (defalias 'qcalc 'quick-calc)
  966. (safe-require-or-eval 'simple)
  967. (add-hook 'makefile-mode-hook
  968. (lambda ()
  969. (local-set-key (kbd "C-m") 'newline-and-indent)
  970. ;; this functions is set in write-file-functions, i cannot find any
  971. ;; good way to remove this.
  972. (fset 'makefile-warn-suspicious-lines 'ignore)
  973. ))
  974. (add-hook 'verilog-mode-hook
  975. (lambda ()
  976. (local-set-key ";" 'self-insert-command)))
  977. (setq diff-switches "-u")
  978. (add-hook 'diff-mode-hook
  979. (lambda ()
  980. ;; (when (and (eq major-mode
  981. ;; 'diff-mode)
  982. ;; (not buffer-file-name))
  983. ;; ;; do not pass when major-mode is derived mode of diff-mode
  984. ;; (view-mode 1))
  985. (set-face-attribute 'diff-header nil
  986. :foreground nil
  987. :background nil
  988. :weight 'bold)
  989. (set-face-attribute 'diff-file-header nil
  990. :foreground nil
  991. :background nil
  992. :weight 'bold)
  993. (set-face-foreground 'diff-index-face "blue")
  994. (set-face-attribute 'diff-hunk-header nil
  995. :foreground "cyan"
  996. :weight 'normal)
  997. (set-face-attribute 'diff-context nil
  998. ;; :foreground "white"
  999. :foreground nil
  1000. :weight 'normal)
  1001. (set-face-foreground 'diff-removed-face "red")
  1002. (set-face-foreground 'diff-added-face "green")
  1003. (set-face-background 'diff-removed-face nil)
  1004. (set-face-background 'diff-added-face nil)
  1005. (set-face-attribute 'diff-changed nil
  1006. :foreground "magenta"
  1007. :weight 'normal)
  1008. (set-face-attribute 'diff-refine-change nil
  1009. :foreground nil
  1010. :background nil
  1011. :weight 'bold
  1012. :inverse-video t)
  1013. ;; Annoying !
  1014. ;;(diff-auto-refine-mode)
  1015. ))
  1016. ;; (ffap-bindings)
  1017. (add-hook 'sh-mode-hook
  1018. (lambda ()
  1019. (local-set-key
  1020. (kbd "C-x C-e")
  1021. 'my-execute-shell-command-current-line)))
  1022. (defvar-set sh-here-document-word "__EOC__")
  1023. (defun my-execute-shell-command-current-line ()
  1024. "Run current line as shell command."
  1025. (interactive)
  1026. (shell-command (buffer-substring-no-properties (point-at-bol)
  1027. (point))))
  1028. (setq auto-mode-alist
  1029. `(("autostart\\'" . sh-mode)
  1030. ("xinitrc\\'" . sh-mode)
  1031. ("xprograms\\'" . sh-mode)
  1032. ("PKGBUILD\\'" . sh-mode)
  1033. ,@auto-mode-alist))
  1034. (and (autoload-eval-lazily 'pkgbuild-mode)
  1035. (setq auto-mode-alist (append '(("PKGBUILD\\'" . pkgbuild-mode))
  1036. auto-mode-alist)))
  1037. (and (autoload-eval-lazily 'groovy-mode)
  1038. (add-to-list 'auto-mode-alist
  1039. '("build.gradle\\'" . groovy-mode)))
  1040. (add-hook 'yaml-mode-hook
  1041. (lambda ()
  1042. (local-set-key(kbd "C-m") 'newline)))
  1043. (add-hook 'html-mode-hook
  1044. (lambda ()
  1045. (local-set-key(kbd "C-m") 'reindent-then-newline-and-indent)))
  1046. (add-hook 'text-mode-hook
  1047. (lambda ()
  1048. (local-set-key (kbd "C-m") 'newline)))
  1049. (add-to-list 'Info-default-directory-list
  1050. (expand-file-name "~/.info/emacs-ja"))
  1051. (add-hook 'apropos-mode-hook
  1052. (lambda ()
  1053. (local-set-key "n" 'next-line)
  1054. (local-set-key "p" 'previous-line)
  1055. ))
  1056. (add-hook 'isearch-mode-hook
  1057. (lambda ()
  1058. ;; (define-key isearch-mode-map
  1059. ;; (kbd "C-j") 'isearch-other-control-char)
  1060. ;; (define-key isearch-mode-map
  1061. ;; (kbd "C-k") 'isearch-other-control-char)
  1062. ;; (define-key isearch-mode-map
  1063. ;; (kbd "C-h") 'isearch-other-control-char)
  1064. (define-key isearch-mode-map (kbd "C-h") 'isearch-delete-char)
  1065. (define-key isearch-mode-map (kbd "M-r")
  1066. 'isearch-query-replace-regexp)))
  1067. ;; do not cleanup isearch highlight: use `lazy-highlight-cleanup' to remove
  1068. (setq lazy-highlight-cleanup nil)
  1069. ;; face for isearch highlighing
  1070. (set-face-attribute 'lazy-highlight
  1071. nil
  1072. :foreground `unspecified
  1073. :background `unspecified
  1074. :underline t
  1075. ;; :weight `bold
  1076. )
  1077. (add-hook 'outline-mode-hook
  1078. (lambda ()
  1079. (if (string-match "\\.md\\'" buffer-file-name)
  1080. (set (make-local-variable 'outline-regexp) "#+ "))))
  1081. (add-to-list 'auto-mode-alist (cons "\\.ol\\'" 'outline-mode))
  1082. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'outline-mode))
  1083. (when (autoload-eval-lazily 'markdown-mode
  1084. '(markdown-mode gfm-mode))
  1085. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'gfm-mode))
  1086. (setq markdown-command (or (executable-find "markdown")
  1087. (executable-find "markdown.pl")))
  1088. (add-hook 'markdown-mode-hook
  1089. (lambda ()
  1090. (outline-minor-mode 1)
  1091. (flyspell-mode)
  1092. (set (make-local-variable 'comment-start) ";")))
  1093. (add-hook 'gfm-mode-hook
  1094. (lambda ()
  1095. (define-key gfm-mode-map (kbd "C-m") 'electric-indent-just-newline)
  1096. ;;(electric-indent-mode 0)
  1097. )))
  1098. ;; c-mode
  1099. ;; http://www.emacswiki.org/emacs/IndentingC
  1100. ;; http://en.wikipedia.org/wiki/Indent_style
  1101. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  1102. ;; http://seesaawiki.jp/whiteflare503/d/Emacs%20%a5%a4%a5%f3%a5%c7%a5%f3%a5%c8
  1103. (when (autoload-eval-lazily 'cc-vars
  1104. nil
  1105. (defvar c-default-style)
  1106. (add-to-list 'c-default-style
  1107. '(c-mode . "k&r"))
  1108. (add-to-list 'c-default-style
  1109. '(c++-mode . "k&r"))
  1110. (add-hook 'c-mode-common-hook
  1111. (lambda ()
  1112. ;; why c-basic-offset in k&r style defaults to 5 ???
  1113. (defvar-set c-basic-offset 4)
  1114. (defvar-set indent-tabs-mode nil)
  1115. ;; (set-face-foreground 'font-lock-keyword-face "blue")
  1116. (c-toggle-hungry-state -1)
  1117. ;; (and (require 'gtags nil t)
  1118. ;; (gtags-mode 1))
  1119. ))))
  1120. (when (autoload-eval-lazily 'php-mode)
  1121. (add-hook 'php-mode-hook
  1122. (lambda ()
  1123. (setq c-basic-offset 2))))
  1124. (when (autoload-eval-lazily 'js2-mode)
  1125. ;; currently do not use js2-mode
  1126. ;; (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  1127. ;; (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode))
  1128. (add-hook 'js2-mode-hook
  1129. (lambda ()
  1130. (define-key js2-mode-map (kbd "C-m") (lambda ()
  1131. (interactive)
  1132. (js2-enter-key)
  1133. (indent-for-tab-command)))
  1134. ;; (add-hook (kill-local-variable 'before-save-hook)
  1135. ;; 'js2-before-save)
  1136. ;; (add-hook 'before-save-hook
  1137. ;; 'my-indent-buffer
  1138. ;; nil
  1139. ;; t)
  1140. )))
  1141. (eval-after-load "js"
  1142. (defvar-set js-indent-level 2))
  1143. (add-to-list 'interpreter-mode-alist
  1144. '("node" . js-mode))
  1145. (when (autoload-eval-lazily 'flymake-jslint
  1146. '(flymake-jslint-load))
  1147. (autoload-eval-lazily 'js nil
  1148. (add-hook 'js-mode-hook
  1149. 'flymake-jslint-load)))
  1150. (safe-require-or-eval 'js-doc)
  1151. (add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)
  1152. (when (safe-require-or-eval 'uniquify)
  1153. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  1154. (setq uniquify-ignore-buffers-re "*[^*]+*")
  1155. (setq uniquify-min-dir-content 1))
  1156. (add-hook 'view-mode-hook
  1157. (lambda()
  1158. (defvar view-mode-map)
  1159. (define-key view-mode-map "j" 'scroll-up-line)
  1160. (define-key view-mode-map "k" 'scroll-down-line)
  1161. (define-key view-mode-map "v" 'toggle-read-only)
  1162. (define-key view-mode-map "q" 'bury-buffer)
  1163. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  1164. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  1165. ;; (define-key view-mode-map
  1166. ;; "n" 'nonincremental-repeat-search-forward)
  1167. ;; (define-key view-mode-map
  1168. ;; "N" 'nonincremental-repeat-search-backward)
  1169. (define-key view-mode-map "/" 'isearch-forward-regexp)
  1170. (define-key view-mode-map "?" 'isearch-backward-regexp)
  1171. (define-key view-mode-map "n" 'isearch-repeat-forward)
  1172. (define-key view-mode-map "N" 'isearch-repeat-backward)
  1173. (define-key view-mode-map (kbd "C-m") 'my-rgrep-symbol-at-point)
  1174. ))
  1175. (global-set-key "\M-r" 'view-mode)
  1176. ;; (setq view-read-only t)
  1177. ;; (defun my-view-mode-search-word (word)
  1178. ;; "Search for word current directory and subdirectories.
  1179. ;; If called intearctively, find word at point."
  1180. ;; (interactive (list (thing-at-point 'symbol)))
  1181. ;; (if word
  1182. ;; (if (and (require 'gtags nil t)
  1183. ;; (gtags-get-rootpath))
  1184. ;; (gtags-goto-tag word "s")
  1185. ;; (my-rgrep word))
  1186. ;; (message "No word at point.")
  1187. ;; nil))
  1188. (add-hook 'Man-mode-hook
  1189. (lambda ()
  1190. (view-mode 1)
  1191. (setq truncate-lines nil)))
  1192. (defvar-set Man-notify-method (if window-system
  1193. 'newframe
  1194. 'aggressive))
  1195. (defvar-set woman-cache-filename (expand-file-name (concat user-emacs-directory
  1196. "woman_cache.el")))
  1197. (defalias 'man 'woman)
  1198. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1199. ;; python
  1200. (when (autoload-eval-lazily 'python '(python-mode))
  1201. (defvar-set python-python-command (or (executable-find "python3")
  1202. (executable-find "python")))
  1203. ;; (defun my-python-run-as-command ()
  1204. ;; ""
  1205. ;; (interactive)
  1206. ;; (shell-command (concat python-python-command " " buffer-file-name)))
  1207. (defun my-python-display-python-buffer ()
  1208. ""
  1209. (interactive)
  1210. (defvar python-buffer)
  1211. (set-window-text-height (display-buffer python-buffer
  1212. t)
  1213. 7))
  1214. (add-hook 'python-mode-hook
  1215. (lambda ()
  1216. (local-set-key (kbd "C-c C-e") 'my-python-run-as-command)
  1217. (local-set-key (kbd "C-c C-b") 'my-python-display-python-buffer)
  1218. (local-set-key (kbd "C-m") 'newline-and-indent)))
  1219. (add-hook 'inferior-python-mode-hook
  1220. (lambda ()
  1221. (my-python-display-python-buffer)
  1222. (local-set-key (kbd "<up>") 'comint-previous-input)
  1223. (local-set-key (kbd "<down>") 'comint-next-input))))
  1224. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1225. ;; GNU GLOBAL(gtags)
  1226. ;; http://uguisu.skr.jp/Windows/gtags.html
  1227. ;; http://eigyr.dip.jp/gtags.html
  1228. ;; http://cha.la.coocan.jp/doc/gnu_global.html
  1229. (let ((d "/opt/local/share/gtags/"))
  1230. (and (file-directory-p d)
  1231. (add-to-list 'load-path
  1232. d)))
  1233. (when (autoload-eval-lazily 'gtags '(gtags-mode))
  1234. (add-hook 'gtags-mode-hook
  1235. (lambda ()
  1236. (view-mode gtags-mode)
  1237. (setq gtags-select-buffer-single t)
  1238. ;; (local-set-key "\M-t" 'gtags-find-tag)
  1239. ;; (local-set-key "\M-r" 'gtags-find-rtag)
  1240. ;; (local-set-key "\M-s" 'gtags-find-symbol)
  1241. ;; (local-set-key "\C-t" 'gtags-pop-stack)
  1242. (define-key gtags-mode-map (kbd "C-x t h")
  1243. 'gtags-find-tag-from-here)
  1244. (define-key gtags-mode-map (kbd "C-x t t") 'gtags-find-tag)
  1245. (define-key gtags-mode-map (kbd "C-x t r") 'gtags-find-rtag)
  1246. (define-key gtags-mode-map (kbd "C-x t s") 'gtags-find-symbol)
  1247. (define-key gtags-mode-map (kbd "C-x t p") 'gtags-find-pattern)
  1248. (define-key gtags-mode-map (kbd "C-x t f") 'gtags-find-file)
  1249. (define-key gtags-mode-map (kbd "C-x t b") 'gtags-pop-stack) ;back
  1250. ))
  1251. (add-hook 'gtags-select-mode-hook
  1252. (lambda ()
  1253. (define-key gtags-select-mode-map (kbd "C-m") 'gtags-select-tag)
  1254. ))
  1255. )
  1256. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1257. ;; term mode
  1258. ;; (setq multi-term-program shell-file-name)
  1259. (when (autoload-eval-lazily 'multi-term)
  1260. (setq multi-term-switch-after-close nil)
  1261. (setq multi-term-dedicated-select-after-open-p t)
  1262. (setq multi-term-dedicated-window-height 20))
  1263. (when (autoload-eval-lazily 'term '(term ansi-term))
  1264. (defun my-term-quit-or-send-raw ()
  1265. ""
  1266. (interactive)
  1267. (if (get-buffer-process (current-buffer))
  1268. (call-interactively 'term-send-raw)
  1269. (kill-buffer)))
  1270. ;; http://d.hatena.ne.jp/goinger/20100416/1271399150
  1271. ;; (setq term-ansi-default-program shell-file-name)
  1272. (add-hook 'term-setup-hook
  1273. (lambda ()
  1274. (defvar-set term-display-table (make-display-table))))
  1275. (add-hook 'term-mode-hook
  1276. (lambda ()
  1277. (defvar term-raw-map)
  1278. (unless (memq (current-buffer)
  1279. (and (featurep 'multi-term)
  1280. (defvar multi-term-buffer-list)
  1281. ;; current buffer is not multi-term buffer
  1282. multi-term-buffer-list))
  1283. ;; (define-key term-raw-map "\C-q" 'move-beginning-of-line)
  1284. ;; (define-key term-raw-map "\C-r" 'term-send-raw)
  1285. ;; (define-key term-raw-map "\C-s" 'term-send-raw)
  1286. ;; (define-key term-raw-map "\C-f" 'forward-char)
  1287. ;; (define-key term-raw-map "\C-b" 'backward-char)
  1288. ;; (define-key term-raw-map "\C-t" 'set-mark-command)
  1289. (define-key term-raw-map
  1290. "\C-x" (lookup-key (current-global-map) "\C-x"))
  1291. (define-key term-raw-map
  1292. "\C-z" (lookup-key (current-global-map) "\C-z"))
  1293. )
  1294. ;; (define-key term-raw-map "\C-xl" 'term-line-mode)
  1295. ;; (define-key term-mode-map "\C-xc" 'term-char-mode)
  1296. (define-key term-raw-map (kbd "<up>") 'scroll-down-line)
  1297. (define-key term-raw-map (kbd "<down>") 'scroll-up-line)
  1298. (define-key term-raw-map (kbd "<right>") 'scroll-up)
  1299. (define-key term-raw-map (kbd "<left>") 'scroll-down)
  1300. (define-key term-raw-map (kbd "C-p") 'term-send-raw)
  1301. (define-key term-raw-map (kbd "C-n") 'term-send-raw)
  1302. (define-key term-raw-map "q" 'my-term-quit-or-send-raw)
  1303. ;; (define-key term-raw-map (kbd "ESC") 'term-send-raw)
  1304. (define-key term-raw-map [delete] 'term-send-raw)
  1305. (define-key term-raw-map (kbd "DEL") 'term-send-backspace)
  1306. (define-key term-raw-map "\C-y" 'term-paste)
  1307. (define-key term-raw-map
  1308. "\C-c" 'term-send-raw) ;; 'term-interrupt-subjob)
  1309. '(define-key term-mode-map (kbd "C-x C-q") 'term-pager-toggle)
  1310. ;; (dolist (key '("<up>" "<down>" "<right>" "<left>"))
  1311. ;; (define-key term-raw-map (read-kbd-macro key) 'term-send-raw))
  1312. ;; (define-key term-raw-map "\C-d" 'delete-char)
  1313. (set (make-local-variable 'scroll-margin) 0)
  1314. ;; (set (make-local-variable 'cua-enable-cua-keys) nil)
  1315. ;; (cua-mode 0)
  1316. ;; (and cua-mode
  1317. ;; (local-unset-key (kbd "C-c")))
  1318. ;; (define-key cua--prefix-override-keymap
  1319. ;;"\C-c" 'term-interrupt-subjob)
  1320. (set (make-local-variable (defvar hl-line-range-function))
  1321. (lambda ()
  1322. '(0 . 0)))
  1323. ))
  1324. ;; (add-hook 'term-exec-hook 'forward-char)
  1325. )
  1326. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1327. ;; buffer switching
  1328. (defvar bs-configurations)
  1329. (when (autoload-eval-lazily 'bs '(bs-show)
  1330. ;; (add-to-list 'bs-configurations
  1331. ;; '("processes" nil get-buffer-process ".*" nil nil))
  1332. (add-to-list 'bs-configurations
  1333. '("files-and-terminals" nil nil nil
  1334. (lambda (buf)
  1335. (and (bs-visits-non-file buf)
  1336. (save-excursion
  1337. (set-buffer buf)
  1338. (not (memq major-mode
  1339. '(term-mode
  1340. eshell-mode))))))))
  1341. ;; (setq bs-configurations (list
  1342. ;; '("processes" nil get-buffer-process ".*" nil nil)
  1343. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil
  1344. ;; bs-visits-non-file bs-sort-buffer-interns-are-last)))
  1345. )
  1346. ;; (global-set-key "\C-x\C-b" 'bs-show)
  1347. (defalias 'list-buffers 'bs-show)
  1348. (defvar-set bs-default-configuration "files-and-terminals")
  1349. (defvar-set bs-default-sort-name "by nothing")
  1350. (add-hook 'bs-mode-hook
  1351. (lambda ()
  1352. ;; (setq bs-default-configuration "files")
  1353. ;; (and bs--show-all
  1354. ;; (call-interactively 'bs-toggle-show-all))
  1355. (set (make-local-variable 'scroll-margin) 0))))
  1356. ;;(iswitchb-mode 1)
  1357. (icomplete-mode)
  1358. (defun iswitchb-buffer-display-other-window ()
  1359. "Do iswitchb in other window."
  1360. (interactive)
  1361. (let ((iswitchb-default-method 'display))
  1362. (call-interactively 'iswitchb-buffer)))
  1363. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1364. ;; sdic
  1365. (when (autoload-eval-lazily 'sdic '(sdic-describe-word-at-point))
  1366. ;; (define-key my-prefix-map "\C-w" 'sdic-describe-word)
  1367. (define-key my-prefix-map "\C-t" 'sdic-describe-word-at-point-echo)
  1368. (defun sdic-describe-word-at-point-echo ()
  1369. ""
  1370. (interactive)
  1371. (save-window-excursion
  1372. (sdic-describe-word-at-point))
  1373. (save-excursion
  1374. (set-buffer sdic-buffer-name)
  1375. (message (buffer-substring (point-min)
  1376. (progn (goto-char (point-min))
  1377. (or (and (re-search-forward "^\\w"
  1378. nil
  1379. t
  1380. 4)
  1381. (progn (previous-line) t)
  1382. (point-at-eol))
  1383. (point-max)))))))
  1384. (setq sdic-eiwa-dictionary-list '((sdicf-client "/usr/share/dict/gene.sdic")))
  1385. (setq sdic-waei-dictionary-list
  1386. '((sdicf-client "/usr/share/dict/jedict.sdic" (add-keys-to-headword t))))
  1387. (setq sdic-disable-select-window t)
  1388. (setq sdic-window-height 7))
  1389. ;;;;;;;;;;;;;;;;;;;;;;;;
  1390. ;; ilookup
  1391. (when (fetch-library
  1392. "https://raw.github.com/10sr/emacs-lisp/master/ilookup.el"
  1393. t)
  1394. (autoload-eval-lazily 'ilookup
  1395. '(ilookup-open)
  1396. (setq ilookup-dict-alist
  1397. '(
  1398. ("sdcv" . (lambda (word)
  1399. (shell-command-to-string
  1400. (format "sdcv -n '%s'"
  1401. word))))
  1402. ("en" . (lambda (word)
  1403. (shell-command-to-string
  1404. (format "sdcv -n -u dictd_www.dict.org_gcide '%s'"
  1405. word))))
  1406. ("ja" . (lambda (word)
  1407. (shell-command-to-string
  1408. (format "sdcv -n -u EJ-GENE95 -u jmdict-en-ja '%s'"
  1409. word))))
  1410. ("jaj" . (lambda (word)
  1411. (shell-command-to-string
  1412. (format "sdcv -n -u jmdict-en-ja '%s'"
  1413. word))))
  1414. ("jag" .
  1415. (lambda (word)
  1416. (with-temp-buffer
  1417. (insert (shell-command-to-string
  1418. (format "sdcv -n -u 'Genius English-Japanese' '%s'"
  1419. word)))
  1420. (html2text)
  1421. (buffer-substring (point-min)
  1422. (point-max)))))
  1423. ("alc" . (lambda (word)
  1424. (shell-command-to-string
  1425. (format "alc '%s' | head -n 20"
  1426. word))))
  1427. ("app" . (lambda (word)
  1428. (shell-command-to-string
  1429. (format "dict_app '%s'"
  1430. word))))
  1431. ;; letters broken
  1432. ("ms" .
  1433. (lambda (word)
  1434. (let ((url (concat
  1435. "http://api.microsofttranslator.com/V2/Ajax.svc/"
  1436. "Translate?appId=%s&text=%s&to=%s"))
  1437. (apikey "3C9778666C5BA4B406FFCBEE64EF478963039C51")
  1438. (target "ja")
  1439. (eword (url-hexify-string word)))
  1440. (with-current-buffer (url-retrieve-synchronously
  1441. (format url
  1442. apikey
  1443. eword
  1444. target))
  1445. (message "")
  1446. (goto-char (point-min))
  1447. (search-forward-regexp "^$"
  1448. nil
  1449. t)
  1450. (url-unhex-string (buffer-substring-no-properties
  1451. (point)
  1452. (point-max)))))))
  1453. ))
  1454. ;; (funcall (cdr (assoc "ms"
  1455. ;; ilookup-alist))
  1456. ;; "dictionary")
  1457. ;; (switch-to-buffer (url-retrieve-synchronously "http://api.microsofttranslator.com/V2/Ajax.svc/Translate?appId=3C9778666C5BA4B406FFCBEE64EF478963039C51&text=dictionary&to=ja"))
  1458. ;; (switch-to-buffer (url-retrieve-synchronously "http://google.com"))
  1459. (setq ilookup-default "ja")
  1460. (when (locate-library "google-translate")
  1461. (add-to-list 'ilookup-dict-alist
  1462. '("gt" .
  1463. (lambda (word)
  1464. (save-excursion
  1465. (google-translate-translate "auto"
  1466. "ja"
  1467. word))
  1468. (with-current-buffer "*Google Translate*"
  1469. (buffer-substring-no-properties (point-min)
  1470. (point-max)))))))
  1471. ))
  1472. (when (autoload-eval-lazily 'google-translate '(google-translate-translate
  1473. google-translate-at-point))
  1474. (setq google-translate-default-source-language "auto")
  1475. (setq google-translate-default-target-language "ja"))
  1476. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1477. ;; vc
  1478. (require 'vc)
  1479. (setq vc-handled-backends '())
  1480. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1481. ;; gauche-mode
  1482. ;; http://d.hatena.ne.jp/kobapan/20090305/1236261804
  1483. ;; http://www.katch.ne.jp/~leque/software/repos/gauche-mode/gauche-mode.el
  1484. (when (and (fetch-library
  1485. "http://www.katch.ne.jp/~leque/software/repos/gauche-mode/gauche-mode.el"
  1486. t)
  1487. (autoload-eval-lazily 'gauche-mode '(gauche-mode run-scheme)))
  1488. (let ((s (executable-find "gosh")))
  1489. (setq scheme-program-name s
  1490. gauche-program-name s))
  1491. (defun run-gauche-other-window ()
  1492. "Run gauche on other window"
  1493. (interactive)
  1494. (switch-to-buffer-other-window
  1495. (get-buffer-create "*scheme*"))
  1496. (run-gauche))
  1497. (defun run-gauche ()
  1498. "run gauche"
  1499. (interactive)
  1500. (run-scheme gauche-program-name)
  1501. )
  1502. (defun scheme-send-buffer ()
  1503. ""
  1504. (interactive)
  1505. (scheme-send-region (point-min) (point-max))
  1506. (my-scheme-display-scheme-buffer)
  1507. )
  1508. (defun my-scheme-display-scheme-buffer ()
  1509. ""
  1510. (interactive)
  1511. (set-window-text-height (display-buffer scheme-buffer
  1512. t)
  1513. 7))
  1514. (add-hook 'scheme-mode-hook
  1515. (lambda ()
  1516. nil))
  1517. (add-hook 'inferior-scheme-mode-hook
  1518. (lambda ()
  1519. ;; (my-scheme-display-scheme-buffer)
  1520. ))
  1521. (setq auto-mode-alist
  1522. (cons '("\.gosh\\'" . gauche-mode) auto-mode-alist))
  1523. (setq auto-mode-alist
  1524. (cons '("\.gaucherc\\'" . gauche-mode) auto-mode-alist))
  1525. (add-hook 'gauche-mode-hook
  1526. (lambda ()
  1527. (define-key gauche-mode-map
  1528. (kbd "C-c C-z") 'run-gauche-other-window)
  1529. (define-key scheme-mode-map
  1530. (kbd "C-c C-c") 'scheme-send-buffer)
  1531. (define-key scheme-mode-map
  1532. (kbd "C-c C-b") 'my-scheme-display-scheme-buffer))))
  1533. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1534. ;; recentf-mode
  1535. (setq recentf-save-file (expand-file-name (concat user-emacs-directory
  1536. "recentf"))
  1537. recentf-max-menu-items 20
  1538. recentf-max-saved-items 30
  1539. recentf-show-file-shortcuts-flag nil)
  1540. (when (safe-require-or-eval 'recentf)
  1541. (add-to-list 'recentf-exclude
  1542. (regexp-quote recentf-save-file))
  1543. (add-to-list 'recentf-exclude
  1544. (regexp-quote (expand-file-name user-emacs-directory)))
  1545. (define-key ctl-x-map (kbd "C-r") 'recentf-open-files)
  1546. (remove-hook 'find-file-hook
  1547. 'recentf-track-opened-file)
  1548. (defun my-recentf-load-track-save-list ()
  1549. "Load current recentf list from file, track current visiting file, then save
  1550. the list."
  1551. (recentf-load-list)
  1552. (recentf-track-opened-file)
  1553. (recentf-save-list))
  1554. (add-hook 'find-file-hook
  1555. 'my-recentf-load-track-save-list)
  1556. (add-hook 'kill-emacs-hook
  1557. 'recentf-load-list)
  1558. ;;(run-with-idle-timer 5 t 'recentf-save-list)
  1559. ;; (add-hook 'find-file-hook
  1560. ;; (lambda ()
  1561. ;; (recentf-add-file default-directory)))
  1562. (and (autoload-eval-lazily 'recentf-show)
  1563. (define-key ctl-x-map (kbd "C-r") 'recentf-show)
  1564. (add-hook 'recentf-show-before-listing-hook
  1565. 'recentf-load-list))
  1566. (recentf-mode 1)
  1567. (add-hook 'recentf-dialog-mode-hook
  1568. (lambda ()
  1569. ;; (recentf-save-list)
  1570. ;; (define-key recentf-dialog-mode-map (kbd "C-x C-f")
  1571. ;; 'my-recentf-cd-and-find-file)
  1572. (define-key recentf-dialog-mode-map (kbd "<up>") 'previous-line)
  1573. (define-key recentf-dialog-mode-map (kbd "<down>") 'next-line)
  1574. (define-key recentf-dialog-mode-map "p" 'previous-line)
  1575. (define-key recentf-dialog-mode-map "n" 'next-line)
  1576. (cd "~/"))))
  1577. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1578. ;; dired
  1579. (when (autoload-eval-lazily 'dired nil)
  1580. (defun my-dired-echo-file-head (arg)
  1581. ""
  1582. (interactive "P")
  1583. (let ((f (dired-get-filename)))
  1584. (message "%s"
  1585. (with-temp-buffer
  1586. (insert-file-contents f)
  1587. (buffer-substring-no-properties
  1588. (point-min)
  1589. (progn (goto-line (if arg
  1590. (prefix-numeric-value arg)
  1591. 7))
  1592. (point-at-eol)))))))
  1593. (defun my-dired-diff ()
  1594. ""
  1595. (interactive)
  1596. (let ((files (dired-get-marked-files nil nil nil t)))
  1597. (if (eq (car files)
  1598. t)
  1599. (diff (cadr files) (dired-get-filename))
  1600. (message "One files must be marked!"))))
  1601. (defun my-pop-to-buffer-erase-noselect (buffer-or-name)
  1602. "pop up buffer using `display-buffer' and return that buffer."
  1603. (let ((bf (get-buffer-create buffer-or-name)))
  1604. (with-current-buffer bf
  1605. (cd ".")
  1606. (erase-buffer))
  1607. (display-buffer bf)
  1608. bf))
  1609. (defun my-replace-nasi-none ()
  1610. ""
  1611. (save-excursion
  1612. (let ((buffer-read-only nil))
  1613. (goto-char (point-min))
  1614. (while (search-forward "なし" nil t)
  1615. (replace-match "none")))))
  1616. (defun dired-get-file-info ()
  1617. "dired get file info"
  1618. (interactive)
  1619. (let ((f (shell-quote-argument (dired-get-filename t))))
  1620. (if (file-directory-p f)
  1621. (progn
  1622. (message "Calculating disk usage...")
  1623. (shell-command (concat "du -hsD "
  1624. f)))
  1625. (shell-command (concat "file "
  1626. f)))))
  1627. (defun my-dired-scroll-up ()
  1628. ""
  1629. (interactive)
  1630. (my-dired-previous-line (- (window-height) 1)))
  1631. (defun my-dired-scroll-down ()
  1632. ""
  1633. (interactive)
  1634. (my-dired-next-line (- (window-height) 1)))
  1635. ;; (defun my-dired-forward-line (arg)
  1636. ;; ""
  1637. ;; (interactive "p"))
  1638. (defun my-dired-previous-line (arg)
  1639. ""
  1640. (interactive "p")
  1641. (if (> arg 0)
  1642. (progn
  1643. (if (eq (line-number-at-pos)
  1644. 1)
  1645. (goto-char (point-max))
  1646. (forward-line -1))
  1647. (my-dired-previous-line (if (or (dired-get-filename nil t)
  1648. (dired-get-subdir))
  1649. (- arg 1)
  1650. arg)))
  1651. (dired-move-to-filename)))
  1652. (defun my-dired-next-line (arg)
  1653. ""
  1654. (interactive "p")
  1655. (if (> arg 0)
  1656. (progn
  1657. (if (eq (point)
  1658. (point-max))
  1659. (goto-char (point-min))
  1660. (forward-line 1))
  1661. (my-dired-next-line (if (or (dired-get-filename nil t)
  1662. (dired-get-subdir))
  1663. (- arg 1)
  1664. arg)))
  1665. (dired-move-to-filename)))
  1666. (defun my-dired-print-current-dir-and-file ()
  1667. (message "%s %s"
  1668. default-directory
  1669. (buffer-substring-no-properties (point-at-bol)
  1670. (point-at-eol))))
  1671. (defun dired-do-execute-as-command ()
  1672. ""
  1673. (interactive)
  1674. (let ((file (dired-get-filename t)))
  1675. (if (file-executable-p file)
  1676. (start-process file nil file)
  1677. (when (y-or-n-p
  1678. "This file cant be executed. Mark as executable and go? ")
  1679. (set-file-modes file
  1680. (file-modes-symbolic-to-number "u+x" (file-modes file)))
  1681. (start-process file nil file)))))
  1682. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  1683. (defun my-dired-x-open ()
  1684. ""
  1685. (interactive)
  1686. (my-x-open (dired-get-filename t t)))
  1687. (if (eq window-system 'mac)
  1688. (setq dired-listing-switches "-lhF")
  1689. (setq dired-listing-switches "-lhF --time-style=long-iso")
  1690. )
  1691. (setq dired-listing-switches "-lhF")
  1692. (put 'dired-find-alternate-file 'disabled nil)
  1693. ;; when using dired-find-alternate-file
  1694. ;; reuse current dired buffer for the file to open
  1695. (defvar-set dired-ls-F-marks-symlinks t)
  1696. (when (safe-require-or-eval 'ls-lisp)
  1697. (setq ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  1698. (setq ls-lisp-dirs-first t)
  1699. (setq ls-lisp-use-localized-time-format t)
  1700. (setq ls-lisp-format-time-list
  1701. '("%Y-%m-%d %H:%M"
  1702. "%Y-%m-%d ")))
  1703. (defvar-set dired-dwim-target t)
  1704. (defvar-set dired-isearch-filenames t)
  1705. (defvar-set dired-hide-details-hide-symlink-targets nil)
  1706. (defvar-set dired-hide-details-hide-information-lines nil)
  1707. ;; (add-hook 'dired-after-readin-hook
  1708. ;; 'my-replace-nasi-none)
  1709. ;; (add-hook 'after-init-hook
  1710. ;; (lambda ()
  1711. ;; (dired ".")))
  1712. (add-hook 'dired-mode-hook
  1713. (lambda ()
  1714. (local-set-key "o" 'my-dired-x-open)
  1715. (local-set-key "i" 'dired-get-file-info)
  1716. (local-set-key "f" 'find-file)
  1717. (local-set-key "!" 'shell-command)
  1718. (local-set-key "&" 'async-shell-command)
  1719. (local-set-key "X" 'dired-do-async-shell-command)
  1720. (local-set-key "=" 'my-dired-diff)
  1721. (local-set-key "B" 'gtkbm-add-current-dir)
  1722. (local-set-key "b" 'gtkbm)
  1723. (local-set-key "h" 'my-dired-echo-file-head)
  1724. (local-set-key "@" (lambda ()
  1725. (interactive) (my-x-open ".")))
  1726. (local-set-key (kbd "TAB") 'other-window)
  1727. ;; (local-set-key "P" 'my-dired-do-pack-or-unpack)
  1728. (local-set-key "/" 'dired-isearch-filenames)
  1729. (local-set-key (kbd "DEL") 'dired-up-directory)
  1730. (local-set-key (kbd "C-h") 'dired-up-directory)
  1731. (substitute-key-definition 'dired-next-line
  1732. 'my-dired-next-line
  1733. (current-local-map))
  1734. (substitute-key-definition 'dired-previous-line
  1735. 'my-dired-previous-line
  1736. (current-local-map))
  1737. ;; (local-set-key (kbd "C-p") 'my-dired-previous-line)
  1738. ;; (local-set-key (kbd "p") 'my-dired-previous-line)
  1739. ;; (local-set-key (kbd "C-n") 'my-dired-next-line)
  1740. ;; (local-set-key (kbd "n") 'my-dired-next-line)
  1741. (local-set-key (kbd "<left>") 'my-dired-scroll-up)
  1742. (local-set-key (kbd "<right>") 'my-dired-scroll-down)
  1743. (local-set-key (kbd "ESC p") 'my-dired-scroll-up)
  1744. (local-set-key (kbd "ESC n") 'my-dired-scroll-down)
  1745. (when (fboundp 'dired-hide-details-mode)
  1746. (dired-hide-details-mode t)
  1747. (local-set-key "l" 'dired-hide-details-mode))
  1748. (let ((file "._Icon\015"))
  1749. (when nil (file-readable-p file)
  1750. (delete-file file)))))
  1751. (and (autoload-eval-lazily 'pack '(dired-do-pack-or-unpack pack))
  1752. (add-hook 'dired-mode-hook
  1753. (lambda ()
  1754. (local-set-key "P" 'dired-do-pack-or-unpack))))
  1755. (and (fetch-library
  1756. "https://raw.github.com/10sr/emacs-lisp/master/dired-list-all-mode.el"
  1757. t)
  1758. (autoload-eval-lazily 'dired-list-all-mode)
  1759. (setq dired-listing-switches "-lhF")
  1760. (add-hook 'dired-mode-hook
  1761. (lambda ()
  1762. (local-set-key "a" 'dired-list-all-mode)
  1763. )))
  1764. ) ; when dired locate
  1765. ;; http://blog.livedoor.jp/tek_nishi/archives/4693204.html
  1766. (defvar dired-marker-char)
  1767. (defun my-dired-toggle-mark()
  1768. (let ((cur (cond ((eq (following-char) dired-marker-char) ?\040)
  1769. (t dired-marker-char))))
  1770. (delete-char 1)
  1771. (insert cur)))
  1772. (defun my-dired-mark (arg)
  1773. "Toggle mark the current (or next ARG) files.
  1774. If on a subdir headerline, mark all its files except `.' and `..'.
  1775. Use \\[dired-unmark-all-files] to remove all marks
  1776. and \\[dired-unmark] on a subdir to remove the marks in
  1777. this subdir."
  1778. (interactive "P")
  1779. (if (dired-get-subdir)
  1780. (save-excursion (dired-mark-subdir-files))
  1781. (let ((inhibit-read-only t))
  1782. (dired-repeat-over-lines
  1783. (prefix-numeric-value arg)
  1784. 'my-dired-toggle-mark))))
  1785. (defun my-dired-mark-backward (arg)
  1786. "In Dired, move up lines and toggle mark there.
  1787. Optional prefix ARG says how many lines to unflag; default is one line."
  1788. (interactive "p")
  1789. (my-dired-mark (- arg)))
  1790. (add-hook 'dired-mode-hook
  1791. (lambda ()
  1792. (local-set-key (kbd "SPC") 'my-dired-mark)
  1793. (local-set-key (kbd "S-SPC") 'my-dired-mark-backward))
  1794. )
  1795. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1796. ;; eshell
  1797. (autoload-eval-lazily 'eshell nil
  1798. (defvar-set eshell-banner-message (format "Welcome to the Emacs shell
  1799. %s
  1800. C-x t to toggling emacs-text-mode
  1801. "
  1802. (shell-command-to-string "uname -a")
  1803. ))
  1804. (defvar eshell-text-mode-map
  1805. (let ((map (make-sparse-keymap)))
  1806. (define-key map (kbd "C-x t") 'eshell-text-mode-toggle)
  1807. map))
  1808. (define-derived-mode eshell-text-mode text-mode
  1809. "Eshell-Text"
  1810. "Text-mode for Eshell."
  1811. nil)
  1812. (defun eshell-text-mode-toggle ()
  1813. "Toggle eshell-text-mode and eshell-mode."
  1814. (interactive)
  1815. (cond ((eq major-mode
  1816. 'eshell-text-mode)
  1817. (goto-char (point-max))
  1818. (message "Eshell text mode disabled")
  1819. (eshell-mode))
  1820. ((eq major-mode
  1821. 'eshell-mode)
  1822. (message "Eshell text mode enabled")
  1823. (eshell-write-history)
  1824. (eshell-text-mode))
  1825. (t
  1826. (message "Not in eshell buffer")
  1827. nil)))
  1828. (defun my-eshell-backward-delete-char ()
  1829. (interactive)
  1830. (when (< (save-excursion
  1831. (eshell-bol)
  1832. (point))
  1833. (point))
  1834. (backward-delete-char 1)))
  1835. (defun my-file-owner-p (file)
  1836. "t if FILE is owned by me."
  1837. (eq (user-uid) (nth 2 (file-attributes file))))
  1838. "http://www.bookshelf.jp/pukiwiki/pukiwiki.php\
  1839. ?Eshell%A4%F2%BB%C8%A4%A4%A4%B3%A4%CA%A4%B9"
  1840. ;; ;; written by Stefan Reichoer <reichoer@web.de>
  1841. ;; (defun eshell/less (&rest args)
  1842. ;; "Invoke `view-file' on the file.
  1843. ;; \"less +42 foo\" also goes to line 42 in the buffer."
  1844. ;; (if args
  1845. ;; (while args
  1846. ;; (if (string-match "\\`\\+\\([0-9]+\\)\\'" (car args))
  1847. ;; (let* ((line (string-to-number (match-string 1 (pop args))))
  1848. ;; (file (pop args)))
  1849. ;; (view-file file)
  1850. ;; (goto-line line))
  1851. ;; (view-file (pop args))))))
  1852. (defun eshell/o (&optional file)
  1853. (my-x-open (or file ".")))
  1854. ;; (defun eshell/vi (&rest args)
  1855. ;; "Invoke `find-file' on the file.
  1856. ;; \"vi +42 foo\" also goes to line 42 in the buffer."
  1857. ;; (while args
  1858. ;; (if (string-match "\\`\\+\\([0-9]+\\)\\'" (car args))
  1859. ;; (let* ((line (string-to-number (match-string 1 (pop args))))
  1860. ;; (file (pop args)))
  1861. ;; (find-file file)
  1862. ;; (goto-line line))
  1863. ;; (find-file (pop args)))))
  1864. (defun eshell/clear ()
  1865. "Clear the current buffer, leaving one prompt at the top."
  1866. (interactive)
  1867. (let ((inhibit-read-only t))
  1868. (erase-buffer)))
  1869. (defun eshell-clear ()
  1870. (interactive)
  1871. (let ((inhibit-read-only t))
  1872. (erase-buffer)
  1873. (insert (funcall eshell-prompt-function))))
  1874. (defun eshell/d (&optional dirname switches)
  1875. "if first arg is omitted open current directory."
  1876. (dired (or dirname ".") switches))
  1877. (defun eshell/v ()
  1878. (view-mode 1))
  1879. ;; (defun eshell/aaa (&rest args)
  1880. ;; (message "%S"
  1881. ;; args))
  1882. (defalias 'eshell/: 'ignore)
  1883. (defalias 'eshell/type 'eshell/which)
  1884. ;; (defalias 'eshell/vim 'eshell/vi)
  1885. (defalias 'eshell/ff 'find-file)
  1886. (defalias 'eshell/q 'eshell/exit)
  1887. (defun eshell-goto-prompt ()
  1888. ""
  1889. (interactive)
  1890. (goto-char (point-max)))
  1891. (defun eshell-delete-char-or-logout (n)
  1892. (interactive "p")
  1893. (if (equal (eshell-get-old-input)
  1894. "")
  1895. (progn
  1896. (insert "exit")
  1897. (eshell-send-input))
  1898. (delete-char n)))
  1899. (defun eshell-kill-input ()
  1900. (interactive)
  1901. (delete-region (point)
  1902. (progn (eshell-bol)
  1903. (point))))
  1904. (defalias 'eshell/logout 'eshell/exit)
  1905. (defun eshell-cd-default-directory (&optional eshell-buffer-or-name)
  1906. "open eshell and change wd
  1907. if arg given, use that eshell buffer, otherwise make new eshell buffer."
  1908. (interactive)
  1909. (let ((dir (expand-file-name default-directory)))
  1910. (switch-to-buffer (or eshell-buffer-or-name
  1911. (eshell t)))
  1912. (unless (equal dir (expand-file-name default-directory))
  1913. ;; (cd dir)
  1914. ;; (eshell-interactive-print (concat "cd " dir "\n"))
  1915. ;; (eshell-emit-prompt)
  1916. (goto-char (point-max))
  1917. (eshell-kill-input)
  1918. (insert "cd " dir)
  1919. (eshell-send-input))))
  1920. (defadvice eshell-next-matching-input-from-input
  1921. ;; do not cycle history
  1922. (around eshell-history-do-not-cycle activate)
  1923. (if (= 0
  1924. (or eshell-history-index
  1925. 0))
  1926. (progn
  1927. (delete-region eshell-last-output-end (point))
  1928. (insert-and-inherit eshell-matching-input-from-input-string)
  1929. (setq eshell-history-index nil))
  1930. ad-do-it))
  1931. (defvar-set eshell-directory-name (concat user-emacs-directory
  1932. "eshell/"))
  1933. (defvar-set eshell-term-name "eterm-color")
  1934. (defvar-set eshell-scroll-to-bottom-on-input t)
  1935. (defvar-set eshell-cmpl-ignore-case t)
  1936. (defvar-set eshell-cmpl-cycle-completions nil)
  1937. (defvar-set eshell-highlight-prompt nil)
  1938. (if (eq system-type 'darwin)
  1939. (defvar-set eshell-ls-initial-args '("-hCFG")
  1940. (defvar-set eshell-ls-initial-args '("-hCFG"
  1941. "--color=auto"
  1942. "--time-style=long-iso")) ; "-hF")
  1943. ))
  1944. (defvar-set eshell-prompt-function
  1945. 'my-eshell-prompt-function)
  1946. (defvar eshell-last-command-status)
  1947. (defun my-eshell-prompt-function()
  1948. "Prompt function.
  1949. It looks like:
  1950. :: [10sr@darwin:~/][ESHELL]
  1951. :: $
  1952. "
  1953. (concat ":: ["
  1954. (let ((str (concat user-login-name
  1955. "@"
  1956. (car (split-string system-name
  1957. "\\."))
  1958. )))
  1959. (put-text-property 0
  1960. (length str)
  1961. 'face
  1962. 'underline
  1963. str)
  1964. str)
  1965. ":"
  1966. (let ((str (abbreviate-file-name default-directory)))
  1967. (put-text-property 0
  1968. (length str)
  1969. 'face
  1970. 'underline
  1971. str)
  1972. str)
  1973. "][ESHELL]\n:: "
  1974. (if (eq 0
  1975. eshell-last-command-status)
  1976. ""
  1977. (format "[STATUS:%d] "
  1978. eshell-last-command-status))
  1979. (if (= (user-uid)
  1980. 0)
  1981. "# "
  1982. "$ ")
  1983. ))
  1984. (add-hook 'eshell-mode-hook
  1985. (lambda ()
  1986. ;; (define-key eshell-mode-map (kbd "C-x C-x") (lambda ()
  1987. ;; (interactive)
  1988. ;; (switch-to-buffer (other-buffer))))
  1989. ;; (define-key eshell-mode-map (kbd "C-g") (lambda ()
  1990. ;; (interactive)
  1991. ;; (eshell-goto-prompt)
  1992. ;; (keyboard-quit)))
  1993. (local-set-key (kbd "C-x t") 'eshell-text-mode-toggle)
  1994. (local-set-key (kbd "C-u") 'eshell-kill-input)
  1995. (local-set-key (kbd "C-d") 'eshell-delete-char-or-logout)
  1996. ;; (define-key eshell-mode-map (kbd "C-l")
  1997. ;; 'eshell-clear)
  1998. (local-set-key (kbd "DEL") 'my-eshell-backward-delete-char)
  1999. (local-set-key (kbd "<up>") 'scroll-down-line)
  2000. (local-set-key (kbd "<down>") 'scroll-up-line)
  2001. ;; (define-key eshell-mode-map
  2002. ;; (kbd "C-p") 'eshell-previous-matching-input-from-input)
  2003. ;; (define-key eshell-mode-map
  2004. ;; (kbd "C-n") 'eshell-next-matching-input-from-input)
  2005. (apply 'eshell/addpath exec-path)
  2006. (set (make-local-variable 'scroll-margin) 0)
  2007. ;; (eshell/export "GIT_PAGER=")
  2008. ;; (eshell/export "GIT_EDITOR=")
  2009. (eshell/export "LC_MESSAGES=C")
  2010. (switch-to-buffer (current-buffer)) ; move buffer top of list
  2011. (set (make-local-variable (defvar hl-line-range-function))
  2012. (lambda ()
  2013. '(0 . 0)))
  2014. (defvar eshell-virtual-targets)
  2015. (add-to-list 'eshell-virtual-targets
  2016. '("/dev/less"
  2017. (lambda (str)
  2018. (if str
  2019. (with-current-buffer nil)))
  2020. nil))
  2021. ))
  2022. (add-hook 'eshell-mode-hook
  2023. (lambda ()
  2024. (defvar eshell-visual-commands)
  2025. (defvar eshell-output-filter-functions)
  2026. (defvar eshell-command-aliases-list)
  2027. (add-to-list 'eshell-visual-commands "vim")
  2028. ;; (add-to-list 'eshell-visual-commands "git")
  2029. (add-to-list 'eshell-output-filter-functions
  2030. 'eshell-truncate-buffer)
  2031. (mapcar (lambda (alias)
  2032. (add-to-list 'eshell-command-aliases-list
  2033. alias))
  2034. '(
  2035. ;; ("ll" "ls -l $*")
  2036. ;; ("la" "ls -a $*")
  2037. ;; ("lla" "ls -al $*")
  2038. ("git" "git -c color.ui=always $*")
  2039. ("g" "git $*")
  2040. ("eless"
  2041. (concat "cat >>> (with-current-buffer "
  2042. "(get-buffer-create \"*eshell output\") "
  2043. "(erase-buffer) "
  2044. "(setq buffer-read-only nil) "
  2045. "(current-buffer)) "
  2046. "(view-buffer (get-buffer \"*eshell output*\"))"))
  2047. )
  2048. )))
  2049. ) ; eval after load eshell
  2050. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2051. ;; my-term
  2052. (defvar my-term nil
  2053. "My terminal buffer.")
  2054. (defvar my-term-function nil
  2055. "Function to create terminal buffer.
  2056. This function accept no argument and return newly created buffer of terminal.")
  2057. (defun my-term (&optional arg)
  2058. "Open terminal buffer and return that buffer.
  2059. If ARG is given or called with prefix argument, create new buffer."
  2060. (interactive "P")
  2061. (if (and (not arg)
  2062. my-term
  2063. (buffer-name my-term))
  2064. (pop-to-buffer my-term)
  2065. (setq my-term
  2066. (save-window-excursion
  2067. (funcall my-term-function)))
  2068. (and my-term
  2069. (my-term))))
  2070. ;; (setq my-term-function
  2071. ;; (lambda ()
  2072. ;; (if (eq system-type 'windows-nt)
  2073. ;; (eshell)
  2074. ;; (if (require 'multi-term nil t)
  2075. ;; (multi-term)
  2076. ;; (ansi-term shell-file-name)))))
  2077. (setq my-term-function (lambda () (eshell t)))
  2078. ;;(define-key my-prefix-map (kbd "C-s") 'my-term)
  2079. (define-key ctl-x-map "i" 'my-term)
  2080. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2081. ;; x open
  2082. (defvar my-filer nil)
  2083. (setq my-filer (or (executable-find "pcmanfm")
  2084. (executable-find "nautilus")))
  2085. (defun my-x-open (file)
  2086. "Open FILE."
  2087. (interactive "FOpen File: ")
  2088. (setq file (expand-file-name file))
  2089. (message "Opening %s..." file)
  2090. (cond ((eq system-type 'windows-nt)
  2091. (call-process "cmd.exe" nil 0 nil
  2092. "/c" "start" "" (convert-standard-filename file)))
  2093. ((eq system-type 'darwin)
  2094. (call-process "open" nil 0 nil file))
  2095. ((getenv "DISPLAY")
  2096. (call-process (or my-filer "xdg-open") nil 0 nil file))
  2097. (t
  2098. (find-file file))
  2099. )
  2100. ;; (recentf-add-file file)
  2101. (message "Opening %s...done" file))
  2102. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2103. ;; misc funcs
  2104. (defun my-git-apply-index-from-buffer (&optional buf)
  2105. "Git apply buffer. BUF is buffer to apply. nil to use current buffer."
  2106. (interactive)
  2107. (let ((buf (or buf
  2108. (current-buffer)))
  2109. (file (make-temp-file "git-apply-diff.emacs")))
  2110. (with-current-buffer buf
  2111. (write-region (point-min)
  2112. (point-max)
  2113. file)
  2114. (call-process "git"
  2115. nil
  2116. nil
  2117. nil
  2118. "apply"
  2119. "--cached"
  2120. file))))
  2121. (defun memo (&optional dir)
  2122. "Open memo.txt in DIR."
  2123. (interactive)
  2124. (pop-to-buffer (find-file-noselect (concat (if dir
  2125. (file-name-as-directory dir)
  2126. "")
  2127. "memo.txt"))))
  2128. (defvar my-rgrep-alist
  2129. `(
  2130. ;; the silver searcher
  2131. ("ag"
  2132. (executable-find "ag")
  2133. "ag --nocolor --nogroup --nopager --filename ")
  2134. ;; ack
  2135. ("ack"
  2136. (executable-find "ack")
  2137. "ack --nocolor --nogroup --nopager --with-filename ")
  2138. ;; gnu global
  2139. ("global"
  2140. (and (require 'gtags nil t)
  2141. (executable-find "global")
  2142. (gtags-get-rootpath))
  2143. "global --result grep ")
  2144. ;; git grep
  2145. ("gitgrep"
  2146. (eq 0
  2147. (shell-command "git rev-parse --git-dir"))
  2148. "git --no-pager -c color.grep=false grep -nH -e ")
  2149. ;; grep
  2150. ("grep"
  2151. t
  2152. ,(concat "find . "
  2153. "-path '*/.git' -prune -o "
  2154. "-path '*/.svn' -prune -o "
  2155. "-type f -print0 | "
  2156. "xargs -0 grep -nH -e "))
  2157. )
  2158. "Alist of rgrep command.
  2159. Each element is in the form like (NAME SEXP COMMAND), where SEXP returns the
  2160. condition to choose COMMAND when evaluated.")
  2161. (defvar my-rgrep-default nil
  2162. "Default command name for my-rgrep.")
  2163. (defun my-rgrep-grep-command (&optional name alist)
  2164. "Return recursive grep command for current directory or nil.
  2165. If NAME is given, use that without testing.
  2166. Commands are searched from ALIST."
  2167. (if alist
  2168. (if name
  2169. ;; if name is given search that from alist and return the command
  2170. (nth 2 (assoc name
  2171. alist))
  2172. ;; if name is not given try test in 1th elem
  2173. (let ((car (car alist))
  2174. (cdr (cdr alist)))
  2175. (if (eval (nth 1 car))
  2176. ;; if the condition is true return the command
  2177. (nth 2 car)
  2178. ;; try next one
  2179. (and cdr
  2180. (my-rgrep-grep-command name cdr)))))
  2181. ;; if alist is not given set default value
  2182. (my-rgrep-grep-command name my-rgrep-alist)))
  2183. (defun my-rgrep (command-args)
  2184. "My recursive grep. Run COMMAND-ARGS."
  2185. (interactive (let ((cmd (my-rgrep-grep-command my-rgrep-default
  2186. nil)))
  2187. (if cmd
  2188. (list (read-shell-command "grep command: "
  2189. cmd
  2190. 'grep-find-history))
  2191. (error "My-Rgrep: Command for rgrep not found")
  2192. )))
  2193. (compilation-start command-args
  2194. 'grep-mode))
  2195. ;; (defun my-rgrep-symbol-at-point (command-args)
  2196. ;; "My recursive grep. Run COMMAND-ARGS."
  2197. ;; (interactive (list (read-shell-command "grep command: "
  2198. ;; (concat (my-rgrep-grep-command)
  2199. ;; " "
  2200. ;; (thing-at-point 'symbol))
  2201. ;; 'grep-find-history)))
  2202. ;; (compilation-start command-args
  2203. ;; 'grep-mode))
  2204. (defmacro define-my-rgrep (name)
  2205. "Define rgrep for NAME."
  2206. `(defun ,(intern (concat "my-rgrep-"
  2207. name)) ()
  2208. ,(format "My recursive grep by %s."
  2209. name)
  2210. (interactive)
  2211. (let ((my-rgrep-default ,name))
  2212. (if (called-interactively-p 'any)
  2213. (call-interactively 'my-rgrep)
  2214. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  2215. )
  2216. (define-my-rgrep "ack")
  2217. (define-my-rgrep "ag")
  2218. (define-my-rgrep "gitgrep")
  2219. (define-my-rgrep "grep")
  2220. (define-my-rgrep "global")
  2221. (define-key ctl-x-map "s" 'my-rgrep)
  2222. ;; (defun make ()
  2223. ;; "Run \"make -k\" in current directory."
  2224. ;; (interactive)
  2225. ;; (compile "make -k"))
  2226. (defalias 'make 'compile)
  2227. (define-key ctl-x-map "c" 'compile)
  2228. (defvar sed-in-place-history nil
  2229. "History of `sed-in-place'.")
  2230. (defvar sed-in-place-command "sed --in-place=.bak -e")
  2231. (defun sed-in-place (command)
  2232. "Issue sed in place COMMAND."
  2233. (interactive (list (read-shell-command "sed in place: "
  2234. (concat sed-in-place-command " ")
  2235. 'sed-in-place-history)))
  2236. (shell-command command
  2237. "*sed in place*"))
  2238. (defun dired-do-sed-in-place (&optional arg)
  2239. "Issue sed in place dired. If ARG is given, use the next ARG files."
  2240. (interactive "p")
  2241. (require 'dired-aux)
  2242. (let* ((files (dired-get-marked-files t arg))
  2243. (expr (dired-mark-read-string "Run sed-in-place for %s: "
  2244. nil
  2245. 'sed-in-place
  2246. arg
  2247. files)))
  2248. (if (equal expr
  2249. "")
  2250. (error "No expression specified")
  2251. (shell-command (concat sed-in-place-command
  2252. " '"
  2253. expr
  2254. "' "
  2255. (mapconcat 'shell-quote-argument
  2256. files
  2257. " "))
  2258. "*sed in place*"))))
  2259. (defun dir-show (&optional dir)
  2260. "Show DIR list."
  2261. (interactive)
  2262. (let ((bf (get-buffer-create "*dir show*"))
  2263. (list-directory-brief-switches "-C"))
  2264. (with-current-buffer bf
  2265. (list-directory (or nil
  2266. default-directory)
  2267. nil))
  2268. ))
  2269. (defun my-convmv-sjis2utf8-test ()
  2270. "Run `convmv -r -f sjis -t utf8 *'.
  2271. this is test, does not rename files."
  2272. (interactive)
  2273. (shell-command "convmv -r -f sjis -t utf8 *"))
  2274. (defun my-convmv-sjis2utf8-notest ()
  2275. "Run `convmv -r -f sjis -t utf8 * --notest'."
  2276. (interactive)
  2277. (shell-command "convmv -r -f sjis -t utf8 * --notest"))
  2278. (defun kill-ring-save-buffer-file-name ()
  2279. "Get current filename."
  2280. (interactive)
  2281. (let ((file buffer-file-name))
  2282. (if file
  2283. (progn (kill-new file)
  2284. (message file))
  2285. (message "not visiting file."))))
  2286. (defvar kill-ring-buffer-name "*kill-ring*"
  2287. "Buffer name for `kill-ring-buffer'.")
  2288. (defun open-kill-ring-buffer ()
  2289. "Open kill- ring buffer."
  2290. (interactive)
  2291. (pop-to-buffer
  2292. (with-current-buffer (get-buffer-create kill-ring-buffer-name)
  2293. (erase-buffer)
  2294. (yank)
  2295. (text-mode)
  2296. (current-local-map)
  2297. (goto-char (point-min))
  2298. (yank)
  2299. (current-buffer))))
  2300. (defun set-terminal-header (string)
  2301. "Set terminal header STRING."
  2302. (let ((savepos "\033[s")
  2303. (restorepos "\033[u")
  2304. (movecursor "\033[0;%dH")
  2305. (inverse "\033[7m")
  2306. (restorecolor "\033[0m")
  2307. (cols (frame-parameter nil 'width))
  2308. (length (length string)))
  2309. ;; (redraw-frame (selected-frame))
  2310. (send-string-to-terminal (concat savepos
  2311. (format movecursor
  2312. (1+ (- cols length)))
  2313. inverse
  2314. string
  2315. restorecolor
  2316. restorepos))
  2317. ))
  2318. (defun my-set-terminal-header ()
  2319. "Set terminal header."
  2320. (set-terminal-header (concat " "
  2321. user-login-name
  2322. "@"
  2323. (car (split-string system-name
  2324. "\\."))
  2325. " "
  2326. (format-time-string "%Y/%m/%d %T %z")
  2327. " ")))
  2328. ;; (run-with-timer
  2329. ;; 0.1
  2330. ;; 1
  2331. ;; 'my-set-terminal-header)
  2332. ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2333. ;; ;; savage emacs
  2334. ;; ;; when enabled emacs fails to complete
  2335. ;; ;; http://e-arrows.sakura.ne.jp/2010/05/emacs-should-be-more-savage.html
  2336. ;; (defadvice message (before message-for-stupid (arg &rest arg2) activate)
  2337. ;; (setq arg
  2338. ;; (concat arg
  2339. ;; (if (eq nil
  2340. ;; (string-match "\\. *$"
  2341. ;; arg))
  2342. ;; ".")
  2343. ;; " Stupid!")))
  2344. ;; TODO: make these a library
  2345. (defvar info-in-prompt
  2346. nil
  2347. "System info in the form of \"[user@host] \".")
  2348. (setq info-in-prompt
  2349. (concat "["
  2350. user-login-name
  2351. "@"
  2352. (car (split-string system-name
  2353. "\\."))
  2354. "]"))
  2355. (defun my-real-function-subr-p (function)
  2356. "Return t if FUNCTION is a built-in function even if it is advised."
  2357. (let* ((advised (and (symbolp function)
  2358. (featurep 'advice)
  2359. (ad-get-advice-info function)))
  2360. (real-function
  2361. (or (and advised (let ((origname (cdr (assq 'origname advised))))
  2362. (and (fboundp origname)
  2363. origname)))
  2364. function))
  2365. (def (if (symbolp real-function)
  2366. (symbol-function real-function)
  2367. function)))
  2368. (subrp def)))
  2369. ;; (my-real-function-subr-p 'my-real-function-subr-p)
  2370. ;; (defadvice read-from-minibuffer (before info-in-prompt activate)
  2371. ;; "Show system info when use `read-from-minibuffer'."
  2372. ;; (ad-set-arg 0
  2373. ;; (concat my-system-info
  2374. ;; (ad-get-arg 0))))
  2375. ;; (defadvice read-string (before info-in-prompt activate)
  2376. ;; "Show system info when use `read-string'."
  2377. ;; (ad-set-arg 0
  2378. ;; (concat my-system-info
  2379. ;; (ad-get-arg 0))))
  2380. ;; (when (< emacs-major-version 24)
  2381. ;; (defadvice completing-read (before info-in-prompt activate)
  2382. ;; "Show system info when use `completing-read'."
  2383. ;; (ad-set-arg 0
  2384. ;; (concat my-system-info
  2385. ;; (ad-get-arg 0)))))
  2386. (defmacro info-in-prompt-set (&rest functions)
  2387. "Set info-in-prompt advices for FUNCTIONS."
  2388. `(progn
  2389. ,@(mapcar (lambda (f)
  2390. `(defadvice ,f (before info-in-prompt activate)
  2391. "Show info in prompt."
  2392. (let ((orig (ad-get-arg 0)))
  2393. (unless (string-match-p (regexp-quote info-in-prompt)
  2394. orig)
  2395. (ad-set-arg 0
  2396. (concat info-in-prompt
  2397. " "
  2398. orig))))))
  2399. functions)))
  2400. (info-in-prompt-set read-from-minibuffer
  2401. read-string
  2402. completing-read)
  2403. ;;; emacs.el ends here