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

2717 lines
94 KiB

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