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

2727 lines
94 KiB

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