Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

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