Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

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