Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

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