You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

2746 line
95 KiB

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