Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

2706 рядки
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. (when (autoload-eval-lazily 'dirtree nil
  802. (defun my-dirtree-current-line-directory-p ()
  803. "Return nil if element on current line is not a directory."
  804. (file-directory-p (widget-get (tree-mode-button-current-line)
  805. :file)))
  806. ;; This fix is actually a little strange. Strictly speaking
  807. ;; judging tree should be done by whether the widget is a tree one.
  808. (defun my-dirtree-next-node (arg)
  809. "Fix the problem that `tree-mode-next-node' moves cursor 2 lines."
  810. (interactive "p")
  811. (if (my-dirtree-current-line-directory-p)
  812. (widget-forward (* arg 2))
  813. (widget-forward arg)))
  814. (defun my-dirtree-previous-node (arg)
  815. "Fix the problem that `tree-mode-previous-node' moves cursor 2 lines."
  816. (interactive "p")
  817. (my-dirtree-next-node (- arg)))
  818. (define-key dirtree-mode-map "n" 'my-dirtree-next-node)
  819. (define-key dirtree-mode-map "p" 'my-dirtree-previous-node))
  820. (define-key ctl-x-map "d" 'dirtree))
  821. (and (fetch-library
  822. "https://raw.github.com/10sr/emacs-lisp/master/remember-major-modes-mode.el"
  823. t)
  824. (require 'remember-major-modes-mode nil t)
  825. (remember-major-modes-mode 1)
  826. )
  827. ;; Detect file type from shebang and set major-mode.
  828. (add-to-list 'interpreter-mode-alist
  829. '("python3" . python-mode))
  830. (add-to-list 'interpreter-mode-alist
  831. '("python2" . python-mode))
  832. ;; http://fukuyama.co/foreign-regexp
  833. '(and (require 'foreign-regexp nil t)
  834. (progn
  835. (setq foreign-regexp/regexp-type 'perl)
  836. '(setq reb-re-syntax 'foreign-regexp)
  837. ))
  838. (require 'session nil t)
  839. (autoload-eval-lazily 'sql '(sql-mode)
  840. (require 'sql-indent nil t))
  841. (and (fetch-library "https://raw.github.com/10sr/emacs-lisp/master/gtkbm.el"
  842. t)
  843. (autoload-eval-lazily 'gtkbm)
  844. (global-set-key (kbd "C-x C-d") 'gtkbm))
  845. (and (fetch-library
  846. "https://raw.github.com/10sr/emacs-lisp/master/git-command.el"
  847. t)
  848. (autoload-eval-lazily 'git-command
  849. nil
  850. ;; for git-command old version
  851. (when (boundp 'git-command-major-mode-alist)
  852. (message "You are using old git-command ! Update it !!!")
  853. (add-to-list 'git-command-major-mode-alist
  854. '("di" . diff-mode))
  855. (add-to-list 'git-command-major-mode-alist
  856. '("graph" . fundamental-mode))
  857. (add-to-list 'git-command-major-mode-alist
  858. '("log" . fundamental-mode)))
  859. ;; for git-command new version
  860. (when (boundp 'git-command-view-command-list)
  861. (add-to-list 'git-command-view-command-list
  862. "graph")
  863. (add-to-list 'git-command-view-command-list
  864. "help"))
  865. (when (boundp 'git-command-aliases-alist)
  866. ;; (message "new version of git-command!")
  867. (add-to-list 'git-command-aliases-alist
  868. '("di" . (lambda (options cmd args new-buffer-p)
  869. (git-command-exec options
  870. "diff"
  871. args
  872. new-buffer-p))))
  873. (add-to-list 'git-command-aliases-alist
  874. '("grep" . (lambda (options cmd args new-buffer-p)
  875. (my-rgrep
  876. (concat
  877. "git "
  878. (git-command-construct-commandline
  879. `(,@options "--no-pager"
  880. "-c" "color.grep=false")
  881. cmd
  882. `("-nHe" ,@args))))))))
  883. (setq git-command-use-emacsclient t)
  884. (or git-command-prompt-file
  885. (setq git-command-prompt-file
  886. (git-command-find-git-ps1
  887. "/usr/share/git-core/contrib/completion/git-prompt.sh"))))
  888. ;; (setq git-command-default-options "-c color.ui=always")
  889. (define-key ctl-x-map "g" 'git-command))
  890. (and (fetch-library
  891. "http://www.emacswiki.org/emacs/download/sl.el"
  892. t)
  893. (autoload-eval-lazily 'sl))
  894. (defalias 'qcalc 'quick-calc)
  895. (require 'simple nil t)
  896. (add-hook 'makefile-mode-hook
  897. (lambda ()
  898. (define-key makefile-mode-map (kbd "C-m") 'newline-and-indent)
  899. ;; this functions is set in write-file-functions, i cannot find any
  900. ;; good way to remove this.
  901. (fset 'makefile-warn-suspicious-lines 'ignore)
  902. ))
  903. (add-hook 'verilog-mode-hook
  904. (lambda ()
  905. (define-key verilog-mode-map ";" 'self-insert-command)))
  906. (setq diff-switches "-u")
  907. (add-hook 'diff-mode-hook
  908. (lambda ()
  909. ;; (when (and (eq major-mode
  910. ;; 'diff-mode)
  911. ;; (not buffer-file-name))
  912. ;; ;; do not pass when major-mode is derived mode of diff-mode
  913. ;; (view-mode 1))
  914. (set-face-attribute 'diff-header nil
  915. :foreground nil
  916. :background nil
  917. :weight 'bold)
  918. (set-face-attribute 'diff-file-header nil
  919. :foreground nil
  920. :background nil
  921. :weight 'bold)
  922. (set-face-foreground 'diff-index-face "blue")
  923. (set-face-attribute 'diff-hunk-header nil
  924. :foreground "cyan"
  925. :weight 'normal)
  926. (set-face-attribute 'diff-context nil
  927. ;; :foreground "white"
  928. :foreground nil
  929. :weight 'normal)
  930. (set-face-foreground 'diff-removed-face "red")
  931. (set-face-foreground 'diff-added-face "green")
  932. (set-face-background 'diff-removed-face nil)
  933. (set-face-background 'diff-added-face nil)
  934. (set-face-attribute 'diff-changed nil
  935. :foreground "magenta"
  936. :weight 'normal)
  937. (set-face-attribute 'diff-refine-change nil
  938. :foreground nil
  939. :background nil
  940. :weight 'bold
  941. :inverse-video t)
  942. ;; Annoying !
  943. ;;(diff-auto-refine-mode)
  944. ))
  945. ;; (ffap-bindings)
  946. (add-hook 'sh-mode-hook
  947. (lambda ()
  948. (define-key sh-mode-map
  949. (kbd "C-x C-e")
  950. 'my-execute-shell-command-current-line)))
  951. (setq sh-here-document-word "__EOC__")
  952. (defun my-execute-shell-command-current-line ()
  953. "Run current line as shell command."
  954. (interactive)
  955. (shell-command (buffer-substring-no-properties (point-at-bol)
  956. (point))))
  957. (setq auto-mode-alist
  958. `(("autostart\\'" . sh-mode)
  959. ("xinitrc\\'" . sh-mode)
  960. ("xprograms\\'" . sh-mode)
  961. ("PKGBUILD\\'" . sh-mode)
  962. ,@auto-mode-alist))
  963. (and (autoload-eval-lazily 'pkgbuild-mode)
  964. (setq auto-mode-alist (append '(("PKGBUILD\\'" . pkgbuild-mode))
  965. auto-mode-alist)))
  966. (add-hook 'yaml-mode-hook
  967. (lambda ()
  968. (define-key yaml-mode-map (kbd "C-m")
  969. 'newline)))
  970. (add-hook 'html-mode-hook
  971. (lambda ()
  972. (define-key html-mode-map (kbd "C-m")
  973. 'reindent-then-newline-and-indent)))
  974. (add-hook 'text-mode-hook
  975. (lambda ()
  976. (define-key text-mode-map (kbd "C-m") 'newline)))
  977. (add-to-list 'Info-default-directory-list
  978. (expand-file-name "~/.info/emacs-ja"))
  979. (add-hook 'apropos-mode-hook
  980. (lambda ()
  981. (define-key apropos-mode-map "n" 'next-line)
  982. (define-key apropos-mode-map "p" 'previous-line)
  983. ))
  984. (add-hook 'isearch-mode-hook
  985. (lambda ()
  986. ;; (define-key isearch-mode-map
  987. ;; (kbd "C-j") 'isearch-other-control-char)
  988. ;; (define-key isearch-mode-map
  989. ;; (kbd "C-k") 'isearch-other-control-char)
  990. ;; (define-key isearch-mode-map
  991. ;; (kbd "C-h") 'isearch-other-control-char)
  992. (define-key isearch-mode-map (kbd "C-h") 'isearch-delete-char)
  993. (define-key isearch-mode-map (kbd "M-r")
  994. 'isearch-query-replace-regexp)))
  995. ;; do not cleanup isearch highlight: use `lazy-highlight-cleanup' to remove
  996. (setq lazy-highlight-cleanup nil)
  997. ;; face for isearch highlighing
  998. (set-face-attribute 'lazy-highlight
  999. nil
  1000. :foreground `unspecified
  1001. :background `unspecified
  1002. :underline t
  1003. ;; :weight `bold
  1004. )
  1005. (add-hook 'outline-mode-hook
  1006. (lambda ()
  1007. (if (string-match "\\.md\\'" buffer-file-name)
  1008. (set (make-local-variable 'outline-regexp) "#+ "))))
  1009. (add-to-list 'auto-mode-alist (cons "\\.ol\\'" 'outline-mode))
  1010. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'outline-mode))
  1011. (when (autoload-eval-lazily 'markdown-mode
  1012. '(markdown-mode gfm-mode))
  1013. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'gfm-mode))
  1014. (setq markdown-command (or (executable-find "markdown")
  1015. (executable-find "markdown.pl")))
  1016. (add-hook 'markdown-mode-hook
  1017. (lambda ()
  1018. (outline-minor-mode 1)
  1019. (flyspell-mode)
  1020. (set (make-local-variable 'comment-start) ";"))))
  1021. ;; c-mode
  1022. ;; http://www.emacswiki.org/emacs/IndentingC
  1023. ;; http://en.wikipedia.org/wiki/Indent_style
  1024. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  1025. ;; http://seesaawiki.jp/whiteflare503/d/Emacs%20%a5%a4%a5%f3%a5%c7%a5%f3%a5%c8
  1026. (when (autoload-eval-lazily 'cc-vars
  1027. nil
  1028. (add-to-list 'c-default-style
  1029. '(c-mode . "k&r"))
  1030. (add-to-list 'c-default-style
  1031. '(c++-mode . "k&r"))
  1032. (add-hook 'c-mode-common-hook
  1033. (lambda ()
  1034. ;; why c-basic-offset in k&r style defaults to 5 ???
  1035. (setq c-basic-offset 4
  1036. indent-tabs-mode nil)
  1037. ;; (set-face-foreground 'font-lock-keyword-face "blue")
  1038. (c-toggle-hungry-state -1)
  1039. ;; (and (require 'gtags nil t)
  1040. ;; (gtags-mode 1))
  1041. ))))
  1042. (when (autoload-eval-lazily 'js2-mode)
  1043. ;; currently do not use js2-mode
  1044. ;; (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  1045. ;; (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode))
  1046. (add-hook 'js2-mode-hook
  1047. (lambda ()
  1048. (define-key js2-mode-map (kbd "C-m") (lambda ()
  1049. (interactive)
  1050. (js2-enter-key)
  1051. (indent-for-tab-command)))
  1052. ;; (add-hook (kill-local-variable 'before-save-hook)
  1053. ;; 'js2-before-save)
  1054. ;; (add-hook 'before-save-hook
  1055. ;; 'my-indent-buffer
  1056. ;; nil
  1057. ;; t)
  1058. )))
  1059. (eval-after-load "js"
  1060. (setq js-indent-level 2))
  1061. (add-to-list 'interpreter-mode-alist
  1062. '("node" . js-mode))
  1063. (when (autoload-eval-lazily 'flymake-jslint
  1064. '(flymake-jslint-load))
  1065. (autoload-eval-lazily 'js nil
  1066. (add-hook 'js-mode-hook
  1067. 'flymake-jslint-load)))
  1068. (require 'js-doc nil t)
  1069. (add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)
  1070. (when (require 'uniquify nil t)
  1071. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  1072. (setq uniquify-ignore-buffers-re "*[^*]+*")
  1073. (setq uniquify-min-dir-content 1))
  1074. (add-hook 'view-mode-hook
  1075. (lambda()
  1076. (define-key view-mode-map "j" 'scroll-up-line)
  1077. (define-key view-mode-map "k" 'scroll-down-line)
  1078. (define-key view-mode-map "v" 'toggle-read-only)
  1079. (define-key view-mode-map "q" 'bury-buffer)
  1080. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  1081. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  1082. ;; (define-key view-mode-map
  1083. ;; "n" 'nonincremental-repeat-search-forward)
  1084. ;; (define-key view-mode-map
  1085. ;; "N" 'nonincremental-repeat-search-backward)
  1086. (define-key view-mode-map "/" 'isearch-forward-regexp)
  1087. (define-key view-mode-map "?" 'isearch-backward-regexp)
  1088. (define-key view-mode-map "n" 'isearch-repeat-forward)
  1089. (define-key view-mode-map "N" 'isearch-repeat-backward)
  1090. (define-key view-mode-map (kbd "C-m") 'my-rgrep-symbol-at-point)
  1091. ))
  1092. (global-set-key "\M-r" 'view-mode)
  1093. ;; (setq view-read-only t)
  1094. ;; (defun my-view-mode-search-word (word)
  1095. ;; "Search for word current directory and subdirectories.
  1096. ;; If called intearctively, find word at point."
  1097. ;; (interactive (list (thing-at-point 'symbol)))
  1098. ;; (if word
  1099. ;; (if (and (require 'gtags nil t)
  1100. ;; (gtags-get-rootpath))
  1101. ;; (gtags-goto-tag word "s")
  1102. ;; (my-rgrep word))
  1103. ;; (message "No word at point.")
  1104. ;; nil))
  1105. (add-hook 'Man-mode-hook
  1106. (lambda ()
  1107. (view-mode 1)
  1108. (setq truncate-lines nil)))
  1109. (setq Man-notify-method (if window-system
  1110. 'newframe
  1111. 'aggressive))
  1112. (setq woman-cache-filename (expand-file-name (concat user-emacs-directory
  1113. "woman_cache.el")))
  1114. (defalias 'man 'woman)
  1115. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1116. ;; python
  1117. (when (autoload-eval-lazily 'python '(python-mode))
  1118. (setq python-python-command (or (executable-find "python3")
  1119. (executable-find "python")))
  1120. ;; (defun my-python-run-as-command ()
  1121. ;; ""
  1122. ;; (interactive)
  1123. ;; (shell-command (concat python-python-command " " buffer-file-name)))
  1124. (defun my-python-display-python-buffer ()
  1125. ""
  1126. (interactive)
  1127. (set-window-text-height (display-buffer python-buffer
  1128. t)
  1129. 7))
  1130. (add-hook 'python-mode-hook
  1131. (lambda ()
  1132. (define-key python-mode-map
  1133. (kbd "C-c C-e") 'my-python-run-as-command)
  1134. (define-key python-mode-map
  1135. (kbd "C-c C-b") 'my-python-display-python-buffer)
  1136. (define-key python-mode-map (kbd "C-m") 'newline-and-indent)))
  1137. (add-hook 'inferior-python-mode-hook
  1138. (lambda ()
  1139. (my-python-display-python-buffer)
  1140. (define-key inferior-python-mode-map
  1141. (kbd "<up>") 'comint-previous-input)
  1142. (define-key inferior-python-mode-map
  1143. (kbd "<down>") 'comint-next-input))))
  1144. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1145. ;; GNU GLOBAL(gtags)
  1146. ;; http://uguisu.skr.jp/Windows/gtags.html
  1147. ;; http://eigyr.dip.jp/gtags.html
  1148. ;; http://cha.la.coocan.jp/doc/gnu_global.html
  1149. (let ((d "/opt/local/share/gtags/"))
  1150. (and (file-directory-p d)
  1151. (add-to-list 'load-path
  1152. d)))
  1153. (when (autoload-eval-lazily 'gtags '(gtags-mode))
  1154. (add-hook 'gtags-mode-hook
  1155. (lambda ()
  1156. (view-mode gtags-mode)
  1157. (setq gtags-select-buffer-single t)
  1158. ;; (local-set-key "\M-t" 'gtags-find-tag)
  1159. ;; (local-set-key "\M-r" 'gtags-find-rtag)
  1160. ;; (local-set-key "\M-s" 'gtags-find-symbol)
  1161. ;; (local-set-key "\C-t" 'gtags-pop-stack)
  1162. (define-key gtags-mode-map (kbd "C-x t h")
  1163. 'gtags-find-tag-from-here)
  1164. (define-key gtags-mode-map (kbd "C-x t t") 'gtags-find-tag)
  1165. (define-key gtags-mode-map (kbd "C-x t r") 'gtags-find-rtag)
  1166. (define-key gtags-mode-map (kbd "C-x t s") 'gtags-find-symbol)
  1167. (define-key gtags-mode-map (kbd "C-x t p") 'gtags-find-pattern)
  1168. (define-key gtags-mode-map (kbd "C-x t f") 'gtags-find-file)
  1169. (define-key gtags-mode-map (kbd "C-x t b") 'gtags-pop-stack) ;back
  1170. ))
  1171. (add-hook 'gtags-select-mode-hook
  1172. (lambda ()
  1173. (define-key gtags-select-mode-map (kbd "C-m") 'gtags-select-tag)
  1174. ))
  1175. )
  1176. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1177. ;; term mode
  1178. ;; (setq multi-term-program shell-file-name)
  1179. (when (autoload-eval-lazily 'multi-term)
  1180. (setq multi-term-switch-after-close nil)
  1181. (setq multi-term-dedicated-select-after-open-p t)
  1182. (setq multi-term-dedicated-window-height 20))
  1183. (when (autoload-eval-lazily 'term '(term ansi-term))
  1184. (defun my-term-quit-or-send-raw ()
  1185. ""
  1186. (interactive)
  1187. (if (get-buffer-process (current-buffer))
  1188. (call-interactively 'term-send-raw)
  1189. (kill-buffer)))
  1190. ;; http://d.hatena.ne.jp/goinger/20100416/1271399150
  1191. ;; (setq term-ansi-default-program shell-file-name)
  1192. (add-hook 'term-setup-hook
  1193. (lambda ()
  1194. (setq term-display-table (make-display-table))))
  1195. (add-hook 'term-mode-hook
  1196. (lambda ()
  1197. (unless (memq (current-buffer)
  1198. (and (featurep 'multi-term)
  1199. ;; current buffer is not multi-term buffer
  1200. multi-term-buffer-list))
  1201. ;; (define-key term-raw-map "\C-q" 'move-beginning-of-line)
  1202. ;; (define-key term-raw-map "\C-r" 'term-send-raw)
  1203. ;; (define-key term-raw-map "\C-s" 'term-send-raw)
  1204. ;; (define-key term-raw-map "\C-f" 'forward-char)
  1205. ;; (define-key term-raw-map "\C-b" 'backward-char)
  1206. ;; (define-key term-raw-map "\C-t" 'set-mark-command)
  1207. (define-key term-raw-map
  1208. "\C-x" (lookup-key (current-global-map) "\C-x"))
  1209. (define-key term-raw-map
  1210. "\C-z" (lookup-key (current-global-map) "\C-z"))
  1211. )
  1212. ;; (define-key term-raw-map "\C-xl" 'term-line-mode)
  1213. ;; (define-key term-mode-map "\C-xc" 'term-char-mode)
  1214. (define-key term-raw-map (kbd "<up>") 'scroll-down-line)
  1215. (define-key term-raw-map (kbd "<down>") 'scroll-up-line)
  1216. (define-key term-raw-map (kbd "<right>") 'scroll-up)
  1217. (define-key term-raw-map (kbd "<left>") 'scroll-down)
  1218. (define-key term-raw-map (kbd "C-p") 'term-send-raw)
  1219. (define-key term-raw-map (kbd "C-n") 'term-send-raw)
  1220. (define-key term-raw-map "q" 'my-term-quit-or-send-raw)
  1221. ;; (define-key term-raw-map (kbd "ESC") 'term-send-raw)
  1222. (define-key term-raw-map [delete] 'term-send-raw)
  1223. (define-key term-raw-map (kbd "DEL") 'term-send-backspace)
  1224. (define-key term-raw-map "\C-y" 'term-paste)
  1225. (define-key term-raw-map
  1226. "\C-c" 'term-send-raw) ;; 'term-interrupt-subjob)
  1227. '(define-key term-mode-map (kbd "C-x C-q") 'term-pager-toggle)
  1228. ;; (dolist (key '("<up>" "<down>" "<right>" "<left>"))
  1229. ;; (define-key term-raw-map (read-kbd-macro key) 'term-send-raw))
  1230. ;; (define-key term-raw-map "\C-d" 'delete-char)
  1231. (set (make-local-variable 'scroll-margin) 0)
  1232. ;; (set (make-local-variable 'cua-enable-cua-keys) nil)
  1233. ;; (cua-mode 0)
  1234. ;; (and cua-mode
  1235. ;; (local-unset-key (kbd "C-c")))
  1236. ;; (define-key cua--prefix-override-keymap
  1237. ;;"\C-c" 'term-interrupt-subjob)
  1238. (set (make-local-variable 'hl-line-range-function)
  1239. (lambda ()
  1240. '(0 . 0)))
  1241. ))
  1242. ;; (add-hook 'term-exec-hook 'forward-char)
  1243. )
  1244. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1245. ;; buffer switching
  1246. (when (autoload-eval-lazily 'bs '(bs-show)
  1247. ;; (add-to-list 'bs-configurations
  1248. ;; '("processes" nil get-buffer-process ".*" nil nil))
  1249. (add-to-list 'bs-configurations
  1250. '("files-and-terminals" nil nil nil
  1251. (lambda (buf)
  1252. (and (bs-visits-non-file buf)
  1253. (save-excursion
  1254. (set-buffer buf)
  1255. (not (memq major-mode
  1256. '(term-mode
  1257. eshell-mode))))))))
  1258. ;; (setq bs-configurations (list
  1259. ;; '("processes" nil get-buffer-process ".*" nil nil)
  1260. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil
  1261. ;; bs-visits-non-file bs-sort-buffer-interns-are-last)))
  1262. )
  1263. ;; (global-set-key "\C-x\C-b" 'bs-show)
  1264. (defalias 'list-buffers 'bs-show)
  1265. (setq bs-default-configuration "files-and-terminals")
  1266. (setq bs-default-sort-name "by nothing")
  1267. (add-hook 'bs-mode-hook
  1268. (lambda ()
  1269. ;; (setq bs-default-configuration "files")
  1270. ;; (and bs--show-all
  1271. ;; (call-interactively 'bs-toggle-show-all))
  1272. (set (make-local-variable 'scroll-margin) 0))))
  1273. ;;(iswitchb-mode 1)
  1274. (icomplete-mode)
  1275. (defun iswitchb-buffer-display-other-window ()
  1276. "Do iswitchb in other window."
  1277. (interactive)
  1278. (let ((iswitchb-default-method 'display))
  1279. (call-interactively 'iswitchb-buffer)))
  1280. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1281. ;; sdic
  1282. (when (autoload-eval-lazily 'sdic '(sdic-describe-word-at-point))
  1283. ;; (define-key my-prefix-map "\C-w" 'sdic-describe-word)
  1284. (define-key my-prefix-map "\C-t" 'sdic-describe-word-at-point-echo)
  1285. (defun sdic-describe-word-at-point-echo ()
  1286. ""
  1287. (interactive)
  1288. (save-window-excursion
  1289. (sdic-describe-word-at-point))
  1290. (save-excursion
  1291. (set-buffer sdic-buffer-name)
  1292. (message (buffer-substring (point-min)
  1293. (progn (goto-char (point-min))
  1294. (or (and (re-search-forward "^\\w"
  1295. nil
  1296. t
  1297. 4)
  1298. (progn (previous-line) t)
  1299. (point-at-eol))
  1300. (point-max)))))))
  1301. (setq sdic-eiwa-dictionary-list '((sdicf-client "/usr/share/dict/gene.sdic")))
  1302. (setq sdic-waei-dictionary-list
  1303. '((sdicf-client "/usr/share/dict/jedict.sdic" (add-keys-to-headword t))))
  1304. (setq sdic-disable-select-window t)
  1305. (setq sdic-window-height 7))
  1306. ;;;;;;;;;;;;;;;;;;;;;;;;
  1307. ;; ilookup
  1308. (when (fetch-library
  1309. "https://raw.github.com/10sr/emacs-lisp/master/ilookup.el"
  1310. t)
  1311. (autoload-eval-lazily 'ilookup
  1312. '(ilookup-open)
  1313. (setq ilookup-dict-alist
  1314. '(
  1315. ("en" . (lambda (word)
  1316. (shell-command-to-string
  1317. (format "sdcv -n -u dictd_www.dict.org_gcide '%s'"
  1318. word))))
  1319. ("ja" . (lambda (word)
  1320. (shell-command-to-string
  1321. (format "sdcv -n -u EJ-GENE95 -u jmdict-en-ja '%s'"
  1322. word))))
  1323. ("jaj" . (lambda (word)
  1324. (shell-command-to-string
  1325. (format "sdcv -n -u jmdict-en-ja '%s'"
  1326. word))))
  1327. ("jag" .
  1328. (lambda (word)
  1329. (with-temp-buffer
  1330. (insert (shell-command-to-string
  1331. (format "sdcv -n -u 'Genius English-Japanese' '%s'"
  1332. word)))
  1333. (html2text)
  1334. (buffer-substring (point-min)
  1335. (point-max)))))
  1336. ("alc" . (lambda (word)
  1337. (shell-command-to-string
  1338. (format "alc '%s' | head -n 20"
  1339. word))))
  1340. ("app" . (lambda (word)
  1341. (shell-command-to-string
  1342. (format "dict_app '%s'"
  1343. word))))
  1344. ;; letters broken
  1345. ("ms" .
  1346. (lambda (word)
  1347. (let ((url (concat
  1348. "http://api.microsofttranslator.com/V2/Ajax.svc/"
  1349. "Translate?appId=%s&text=%s&to=%s"))
  1350. (apikey "3C9778666C5BA4B406FFCBEE64EF478963039C51")
  1351. (target "ja")
  1352. (eword (url-hexify-string word)))
  1353. (with-current-buffer (url-retrieve-synchronously
  1354. (format url
  1355. apikey
  1356. eword
  1357. target))
  1358. (message "")
  1359. (goto-char (point-min))
  1360. (search-forward-regexp "^$"
  1361. nil
  1362. t)
  1363. (url-unhex-string (buffer-substring-no-properties
  1364. (point)
  1365. (point-max)))))))
  1366. ))
  1367. ;; (funcall (cdr (assoc "ms"
  1368. ;; ilookup-alist))
  1369. ;; "dictionary")
  1370. ;; (switch-to-buffer (url-retrieve-synchronously "http://api.microsofttranslator.com/V2/Ajax.svc/Translate?appId=3C9778666C5BA4B406FFCBEE64EF478963039C51&text=dictionary&to=ja"))
  1371. ;; (switch-to-buffer (url-retrieve-synchronously "http://google.com"))
  1372. (setq ilookup-default "ja")
  1373. (when (locate-library "google-translate")
  1374. (add-to-list 'ilookup-dict-alist
  1375. '("gt" .
  1376. (lambda (word)
  1377. (save-excursion
  1378. (google-translate-translate "auto"
  1379. "ja"
  1380. word))
  1381. (with-current-buffer "*Google Translate*"
  1382. (buffer-substring-no-properties (point-min)
  1383. (point-max)))))))
  1384. ))
  1385. (when (autoload-eval-lazily 'google-translate '(google-translate-translate
  1386. google-translate-at-point))
  1387. (setq google-translate-default-source-language "auto")
  1388. (setq google-translate-default-target-language "ja"))
  1389. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1390. ;; vc
  1391. ;; (require 'vc)
  1392. (setq vc-handled-backends '())
  1393. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1394. ;; gauche-mode
  1395. ;; http://d.hatena.ne.jp/kobapan/20090305/1236261804
  1396. ;; http://www.katch.ne.jp/~leque/software/repos/gauche-mode/gauche-mode.el
  1397. (when (and (fetch-library
  1398. "http://www.katch.ne.jp/~leque/software/repos/gauche-mode/gauche-mode.el"
  1399. t)
  1400. (autoload-eval-lazily 'gauche-mode '(gauche-mode run-scheme)))
  1401. (let ((s (executable-find "gosh")))
  1402. (setq scheme-program-name s
  1403. gauche-program-name s))
  1404. (defun run-gauche-other-window ()
  1405. "Run gauche on other window"
  1406. (interactive)
  1407. (switch-to-buffer-other-window
  1408. (get-buffer-create "*scheme*"))
  1409. (run-gauche))
  1410. (defun run-gauche ()
  1411. "run gauche"
  1412. (run-scheme gauche-program-name)
  1413. )
  1414. (defun scheme-send-buffer ()
  1415. ""
  1416. (interactive)
  1417. (scheme-send-region (point-min) (point-max))
  1418. (my-scheme-display-scheme-buffer)
  1419. )
  1420. (defun my-scheme-display-scheme-buffer ()
  1421. ""
  1422. (interactive)
  1423. (set-window-text-height (display-buffer scheme-buffer
  1424. t)
  1425. 7))
  1426. (add-hook 'scheme-mode-hook
  1427. (lambda ()
  1428. nil))
  1429. (add-hook 'inferior-scheme-mode-hook
  1430. (lambda ()
  1431. ;; (my-scheme-display-scheme-buffer)
  1432. ))
  1433. (setq auto-mode-alist
  1434. (cons '("\.gosh\\'" . gauche-mode) auto-mode-alist))
  1435. (setq auto-mode-alist
  1436. (cons '("\.gaucherc\\'" . gauche-mode) auto-mode-alist))
  1437. (add-hook 'gauche-mode-hook
  1438. (lambda ()
  1439. (define-key gauche-mode-map
  1440. (kbd "C-c C-z") 'run-gauche-other-window)
  1441. (define-key scheme-mode-map
  1442. (kbd "C-c C-c") 'scheme-send-buffer)
  1443. (define-key scheme-mode-map
  1444. (kbd "C-c C-b") 'my-scheme-display-scheme-buffer))))
  1445. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1446. ;; recentf-mode
  1447. (setq recentf-save-file (expand-file-name "~/.emacs.d/recentf")
  1448. recentf-max-menu-items 20
  1449. recentf-max-saved-items 30
  1450. recentf-show-file-shortcuts-flag nil)
  1451. (when (require 'recentf nil t)
  1452. (add-to-list 'recentf-exclude
  1453. (regexp-quote recentf-save-file))
  1454. (add-to-list 'recentf-exclude
  1455. (regexp-quote (expand-file-name user-emacs-directory)))
  1456. (define-key ctl-x-map (kbd "C-r") 'recentf-open-files)
  1457. (add-hook 'find-file-hook
  1458. 'recentf-save-list
  1459. t) ; save to file immediately after adding file to recentf list
  1460. (add-hook 'kill-emacs-hook
  1461. 'recentf-load-list)
  1462. ;;(run-with-idle-timer 5 t 'recentf-save-list)
  1463. ;; (add-hook 'find-file-hook
  1464. ;; (lambda ()
  1465. ;; (recentf-add-file default-directory)))
  1466. (and (fetch-library
  1467. "https://raw.github.com/10sr/emacs-lisp/master/recentf-show.el"
  1468. t)
  1469. (autoload-eval-lazily 'recentf-show)
  1470. (define-key ctl-x-map (kbd "C-r") 'recentf-show)
  1471. (add-hook 'recentf-show-before-listing-hook
  1472. 'recentf-load-list))
  1473. (recentf-mode 1)
  1474. (add-hook 'recentf-dialog-mode-hook
  1475. (lambda ()
  1476. ;; (recentf-save-list)
  1477. ;; (define-key recentf-dialog-mode-map (kbd "C-x C-f")
  1478. ;; 'my-recentf-cd-and-find-file)
  1479. (define-key recentf-dialog-mode-map (kbd "<up>") 'previous-line)
  1480. (define-key recentf-dialog-mode-map (kbd "<down>") 'next-line)
  1481. (define-key recentf-dialog-mode-map "p" 'previous-line)
  1482. (define-key recentf-dialog-mode-map "n" 'next-line)
  1483. (cd "~/"))))
  1484. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1485. ;; dired
  1486. (when (autoload-eval-lazily 'dired nil)
  1487. (defun my-dired-echo-file-head (arg)
  1488. ""
  1489. (interactive "P")
  1490. (let ((f (dired-get-filename)))
  1491. (message "%s"
  1492. (with-temp-buffer
  1493. (insert-file-contents f)
  1494. (buffer-substring-no-properties
  1495. (point-min)
  1496. (progn (goto-line (if arg
  1497. (prefix-numeric-value arg)
  1498. 7))
  1499. (point-at-eol)))))))
  1500. (defun my-dired-diff ()
  1501. ""
  1502. (interactive)
  1503. (let ((files (dired-get-marked-files nil nil nil t)))
  1504. (if (eq (car files)
  1505. t)
  1506. (diff (cadr files) (dired-get-filename))
  1507. (message "One files must be marked!"))))
  1508. (defun my-pop-to-buffer-erase-noselect (buffer-or-name)
  1509. "pop up buffer using `display-buffer' and return that buffer."
  1510. (let ((bf (get-buffer-create buffer-or-name)))
  1511. (with-current-buffer bf
  1512. (cd ".")
  1513. (erase-buffer))
  1514. (display-buffer bf)
  1515. bf))
  1516. (defun my-replace-nasi-none ()
  1517. ""
  1518. (save-excursion
  1519. (let ((buffer-read-only nil))
  1520. (goto-char (point-min))
  1521. (while (search-forward "なし" nil t)
  1522. (replace-match "none")))))
  1523. (defun dired-get-file-info ()
  1524. "dired get file info"
  1525. (interactive)
  1526. (let ((f (shell-quote-argument (dired-get-filename t))))
  1527. (if (file-directory-p f)
  1528. (progn
  1529. (message "Calculating disk usage...")
  1530. (shell-command (concat "du -hsD "
  1531. f)))
  1532. (shell-command (concat "file "
  1533. f)))))
  1534. (defun my-dired-scroll-up ()
  1535. ""
  1536. (interactive)
  1537. (my-dired-previous-line (- (window-height) 1)))
  1538. (defun my-dired-scroll-down ()
  1539. ""
  1540. (interactive)
  1541. (my-dired-next-line (- (window-height) 1)))
  1542. ;; (defun my-dired-forward-line (arg)
  1543. ;; ""
  1544. ;; (interactive "p"))
  1545. (defun my-dired-previous-line (arg)
  1546. ""
  1547. (interactive "p")
  1548. (if (> arg 0)
  1549. (progn
  1550. (if (eq (line-number-at-pos)
  1551. 1)
  1552. (goto-char (point-max))
  1553. (forward-line -1))
  1554. (my-dired-previous-line (if (or (dired-get-filename nil t)
  1555. (dired-get-subdir))
  1556. (- arg 1)
  1557. arg)))
  1558. (dired-move-to-filename)))
  1559. (defun my-dired-next-line (arg)
  1560. ""
  1561. (interactive "p")
  1562. (if (> arg 0)
  1563. (progn
  1564. (if (eq (point)
  1565. (point-max))
  1566. (goto-char (point-min))
  1567. (forward-line 1))
  1568. (my-dired-next-line (if (or (dired-get-filename nil t)
  1569. (dired-get-subdir))
  1570. (- arg 1)
  1571. arg)))
  1572. (dired-move-to-filename)))
  1573. (defun my-dired-print-current-dir-and-file ()
  1574. (message "%s %s"
  1575. default-directory
  1576. (buffer-substring-no-properties (point-at-bol)
  1577. (point-at-eol))))
  1578. (defun dired-do-execute-as-command ()
  1579. ""
  1580. (interactive)
  1581. (let ((file (dired-get-filename t)))
  1582. (if (file-executable-p file)
  1583. (start-process file nil file)
  1584. (when (y-or-n-p
  1585. "this file cant be executed. mark as executable and go? : ")
  1586. (set-file-modes file
  1587. (file-modes-symbolic-to-number "u+x" (file-modes file)))
  1588. (start-process file nil file)))))
  1589. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  1590. (defun my-dired-x-open ()
  1591. ""
  1592. (interactive)
  1593. (my-x-open (dired-get-filename t t)))
  1594. (if (eq window-system 'mac)
  1595. (setq dired-listing-switches "-lhF")
  1596. (setq dired-listing-switches "-lhF --time-style=long-iso")
  1597. )
  1598. (setq dired-listing-switches "-lhF")
  1599. (put 'dired-find-alternate-file 'disabled nil)
  1600. ;; when using dired-find-alternate-file
  1601. ;; reuse current dired buffer for the file to open
  1602. (setq dired-ls-F-marks-symlinks t)
  1603. (when (require 'ls-lisp nil t)
  1604. (setq ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  1605. (setq ls-lisp-dirs-first t)
  1606. (setq ls-lisp-use-localized-time-format t)
  1607. (setq ls-lisp-format-time-list
  1608. '("%Y-%m-%d %H:%M"
  1609. "%Y-%m-%d ")))
  1610. (setq dired-dwim-target t)
  1611. ;; (add-hook 'dired-after-readin-hook
  1612. ;; 'my-replace-nasi-none)
  1613. ;; (add-hook 'after-init-hook
  1614. ;; (lambda ()
  1615. ;; (dired ".")))
  1616. (add-hook 'dired-mode-hook
  1617. (lambda ()
  1618. (define-key dired-mode-map "o" 'my-dired-x-open)
  1619. (define-key dired-mode-map "i" 'dired-get-file-info)
  1620. (define-key dired-mode-map "f" 'find-file)
  1621. (define-key dired-mode-map "!" 'shell-command)
  1622. (define-key dired-mode-map "&" 'async-shell-command)
  1623. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  1624. (define-key dired-mode-map "=" 'my-dired-diff)
  1625. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  1626. (define-key dired-mode-map "b" 'gtkbm)
  1627. (define-key dired-mode-map "h" 'my-dired-echo-file-head)
  1628. (define-key dired-mode-map "@" (lambda ()
  1629. (interactive) (my-x-open ".")))
  1630. (define-key dired-mode-map (kbd "TAB") 'other-window)
  1631. ;; (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  1632. (define-key dired-mode-map "/" 'dired-isearch-filenames)
  1633. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  1634. (define-key dired-mode-map (kbd "C-h") 'dired-up-directory)
  1635. (substitute-key-definition 'dired-next-line
  1636. 'my-dired-next-line dired-mode-map)
  1637. (substitute-key-definition 'dired-previous-line
  1638. 'my-dired-previous-line dired-mode-map)
  1639. ;; (define-key dired-mode-map (kbd "C-p") 'my-dired-previous-line)
  1640. ;; (define-key dired-mode-map (kbd "p") 'my-dired-previous-line)
  1641. ;; (define-key dired-mode-map (kbd "C-n") 'my-dired-next-line)
  1642. ;; (define-key dired-mode-map (kbd "n") 'my-dired-next-line)
  1643. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  1644. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  1645. (define-key dired-mode-map (kbd "ESC p") 'my-dired-scroll-up)
  1646. (define-key dired-mode-map (kbd "ESC n") 'my-dired-scroll-down)
  1647. (let ((file "._Icon\015"))
  1648. (when nil (file-readable-p file)
  1649. (delete-file file)))))
  1650. (and (fetch-library "https://raw.github.com/10sr/emacs-lisp/master/pack.el"
  1651. t)
  1652. (autoload-eval-lazily 'pack '(dired-do-pack-or-unpack pack))
  1653. (add-hook 'dired-mode-hook
  1654. (lambda ()
  1655. (define-key dired-mode-map "P" 'dired-do-pack-or-unpack))))
  1656. (and (fetch-library
  1657. "https://raw.github.com/10sr/emacs-lisp/master/dired-list-all-mode.el"
  1658. t)
  1659. (autoload-eval-lazily 'dired-list-all-mode)
  1660. (setq dired-listing-switches "-lhF")
  1661. (add-hook 'dired-mode-hook
  1662. (lambda ()
  1663. (define-key dired-mode-map "a" 'dired-list-all-mode)
  1664. )))
  1665. ) ; when dired locate
  1666. ;; http://blog.livedoor.jp/tek_nishi/archives/4693204.html
  1667. (defun my-dired-toggle-mark()
  1668. (let ((cur (cond ((eq (following-char) dired-marker-char) ?\040)
  1669. (t dired-marker-char))))
  1670. (delete-char 1)
  1671. (insert cur)))
  1672. (defun my-dired-mark (arg)
  1673. "Toggle mark the current (or next ARG) files.
  1674. If on a subdir headerline, mark all its files except `.' and `..'.
  1675. Use \\[dired-unmark-all-files] to remove all marks
  1676. and \\[dired-unmark] on a subdir to remove the marks in
  1677. this subdir."
  1678. (interactive "P")
  1679. (if (dired-get-subdir)
  1680. (save-excursion (dired-mark-subdir-files))
  1681. (let ((inhibit-read-only t))
  1682. (dired-repeat-over-lines
  1683. (prefix-numeric-value arg)
  1684. 'my-dired-toggle-mark))))
  1685. (defun my-dired-mark-backward (arg)
  1686. "In Dired, move up lines and toggle mark there.
  1687. Optional prefix ARG says how many lines to unflag; default is one line."
  1688. (interactive "p")
  1689. (my-dired-mark (- arg)))
  1690. (add-hook 'dired-mode-hook
  1691. (lambda ()
  1692. (local-set-key (kbd "SPC") 'my-dired-mark)
  1693. (local-set-key (kbd "S-SPC") 'my-dired-mark-backward))
  1694. )
  1695. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1696. ;; eshell
  1697. (autoload-eval-lazily 'eshell nil
  1698. (setq eshell-banner-message (format "Welcome to the Emacs shell
  1699. %s
  1700. C-x t to toggling emacs-text-mode
  1701. "
  1702. (shell-command-to-string "uname -a")
  1703. ))
  1704. (defvar eshell-text-mode-map
  1705. (let ((map (make-sparse-keymap)))
  1706. (define-key map (kbd "C-x t") 'eshell-text-mode-toggle)
  1707. map))
  1708. (define-derived-mode eshell-text-mode text-mode
  1709. "Eshell-Text"
  1710. "Text-mode for Eshell."
  1711. nil)
  1712. (defun eshell-text-mode-toggle ()
  1713. "Toggle eshell-text-mode and eshell-mode."
  1714. (interactive)
  1715. (cond ((eq major-mode
  1716. 'eshell-text-mode)
  1717. (goto-char (point-max))
  1718. (message "Eshell text mode disabled")
  1719. (eshell-mode))
  1720. ((eq major-mode
  1721. 'eshell-mode)
  1722. (message "Eshell text mode enabled")
  1723. (eshell-write-history)
  1724. (eshell-text-mode))
  1725. (t
  1726. (message "Not in eshell buffer")
  1727. nil)))
  1728. (defun my-eshell-backward-delete-char ()
  1729. (interactive)
  1730. (when (< (save-excursion
  1731. (eshell-bol)
  1732. (point))
  1733. (point))
  1734. (backward-delete-char 1)))
  1735. (defun my-file-owner-p (file)
  1736. "t if FILE is owned by me."
  1737. (eq (user-uid) (nth 2 (file-attributes file))))
  1738. "http://www.bookshelf.jp/pukiwiki/pukiwiki.php\
  1739. ?Eshell%A4%F2%BB%C8%A4%A4%A4%B3%A4%CA%A4%B9"
  1740. ;; ;; written by Stefan Reichoer <reichoer@web.de>
  1741. ;; (defun eshell/less (&rest args)
  1742. ;; "Invoke `view-file' on the file.
  1743. ;; \"less +42 foo\" also goes to line 42 in the buffer."
  1744. ;; (if args
  1745. ;; (while args
  1746. ;; (if (string-match "\\`\\+\\([0-9]+\\)\\'" (car args))
  1747. ;; (let* ((line (string-to-number (match-string 1 (pop args))))
  1748. ;; (file (pop args)))
  1749. ;; (view-file file)
  1750. ;; (goto-line line))
  1751. ;; (view-file (pop args))))))
  1752. (defun eshell/o (&optional file)
  1753. (my-x-open (or file ".")))
  1754. ;; (defun eshell/vi (&rest args)
  1755. ;; "Invoke `find-file' on the file.
  1756. ;; \"vi +42 foo\" also goes to line 42 in the buffer."
  1757. ;; (while args
  1758. ;; (if (string-match "\\`\\+\\([0-9]+\\)\\'" (car args))
  1759. ;; (let* ((line (string-to-number (match-string 1 (pop args))))
  1760. ;; (file (pop args)))
  1761. ;; (find-file file)
  1762. ;; (goto-line line))
  1763. ;; (find-file (pop args)))))
  1764. (defun eshell/clear ()
  1765. "Clear the current buffer, leaving one prompt at the top."
  1766. (interactive)
  1767. (let ((inhibit-read-only t))
  1768. (erase-buffer)))
  1769. (defun eshell-clear ()
  1770. (interactive)
  1771. (let ((inhibit-read-only t))
  1772. (erase-buffer)
  1773. (insert (funcall eshell-prompt-function))))
  1774. (defun eshell/d (&optional dirname switches)
  1775. "if first arg is omitted open current directory."
  1776. (dired (or dirname ".") switches))
  1777. (defun eshell/v ()
  1778. (view-mode 1))
  1779. ;; (defun eshell/aaa (&rest args)
  1780. ;; (message "%S"
  1781. ;; args))
  1782. (defvar eshell/git-cat-command
  1783. nil
  1784. "List of git commands that cat just return strings as results.")
  1785. (setq eshell/git-cat-command
  1786. '("status" "st" "b" "branch" "ls" "ls-files")
  1787. )
  1788. (defun eshell/git (&rest args)
  1789. (if (member (car args)
  1790. eshell/git-cat-command)
  1791. (shell-command-to-string (mapconcat 'shell-quote-argument
  1792. `("git"
  1793. "-c"
  1794. "color.ui=always"
  1795. ,@args)
  1796. " "))
  1797. ;; (eshell-git-shell-command-to-string args)
  1798. (if (require 'git-command nil t)
  1799. (git-command (mapconcat 'shell-quote-argument
  1800. args
  1801. " "))
  1802. (apply 'eshell-exec-visual "git" args))))
  1803. ;; (defun eshell-git-shell-command-to-string (args)
  1804. ;; "Return string of output of ARGS."
  1805. ;; (let ((sargs (mapconcat 'shell-quote-argument
  1806. ;; args
  1807. ;; " ")))
  1808. ;; (if (require 'ansi-color nil t)
  1809. ;; (identity
  1810. ;; (shell-command-to-string (concat "git "
  1811. ;; "-c color.ui=always "
  1812. ;; sargs)))
  1813. ;; (shell-command-to-string (concat "git "
  1814. ;; sargs)))))
  1815. (defalias 'eshell/g 'eshell/git)
  1816. (defalias 'eshell/: 'ignore)
  1817. (defalias 'eshell/type 'eshell/which)
  1818. ;; (defalias 'eshell/vim 'eshell/vi)
  1819. (defalias 'eshell/ff 'find-file)
  1820. (defalias 'eshell/q 'eshell/exit)
  1821. (defun eshell-goto-prompt ()
  1822. ""
  1823. (interactive)
  1824. (goto-char (point-max)))
  1825. (defun eshell-delete-char-or-logout (n)
  1826. (interactive "p")
  1827. (if (equal (eshell-get-old-input)
  1828. "")
  1829. (progn
  1830. (insert "exit")
  1831. (eshell-send-input))
  1832. (delete-char n)))
  1833. (defun eshell-kill-input ()
  1834. (interactive)
  1835. (delete-region (point)
  1836. (progn (eshell-bol)
  1837. (point))))
  1838. (defalias 'eshell/logout 'eshell/exit)
  1839. (defun eshell-cd-default-directory (&optional eshell-buffer-or-name)
  1840. "open eshell and change wd
  1841. if arg given, use that eshell buffer, otherwise make new eshell buffer."
  1842. (interactive)
  1843. (let ((dir (expand-file-name default-directory)))
  1844. (switch-to-buffer (or eshell-buffer-or-name
  1845. (eshell t)))
  1846. (unless (equal dir (expand-file-name default-directory))
  1847. ;; (cd dir)
  1848. ;; (eshell-interactive-print (concat "cd " dir "\n"))
  1849. ;; (eshell-emit-prompt)
  1850. (goto-char (point-max))
  1851. (eshell-kill-input)
  1852. (insert "cd " dir)
  1853. (eshell-send-input))))
  1854. (defadvice eshell-next-matching-input-from-input
  1855. ;; do not cycle history
  1856. (around eshell-history-do-not-cycle activate)
  1857. (if (= 0
  1858. (or eshell-history-index
  1859. 0))
  1860. (progn
  1861. (delete-region eshell-last-output-end (point))
  1862. (insert-and-inherit eshell-matching-input-from-input-string)
  1863. (setq eshell-history-index nil))
  1864. ad-do-it))
  1865. (setq eshell-directory-name "~/.emacs.d/eshell/")
  1866. (setq eshell-term-name "eterm-color")
  1867. (setq eshell-scroll-to-bottom-on-input t)
  1868. (setq eshell-cmpl-ignore-case t)
  1869. (setq eshell-cmpl-cycle-completions nil)
  1870. (setq eshell-highlight-prompt nil)
  1871. (setq eshell-ls-initial-args '("-hCFG"
  1872. "--color=auto"
  1873. "--time-style=long-iso")) ; "-hF")
  1874. (setq eshell-prompt-function
  1875. 'my-eshell-prompt-function)
  1876. (defun my-eshell-prompt-function ()
  1877. (with-temp-buffer
  1878. (let (p1 p2 p3 p4)
  1879. (insert ":: [")
  1880. (setq p1 (point))
  1881. (insert user-login-name
  1882. "@"
  1883. (car (split-string system-name
  1884. "\\."))
  1885. )
  1886. (setq p2 (point))
  1887. (insert ":")
  1888. (setq p3 (point))
  1889. (insert (abbreviate-file-name default-directory))
  1890. (setq p4 (point))
  1891. (insert "]")
  1892. (insert "\n:: ")
  1893. (unless (eq 0
  1894. eshell-last-command-status)
  1895. (insert (format "[STATUS:%d] "
  1896. eshell-last-command-status)))
  1897. (insert (if (= (user-uid)
  1898. 0)
  1899. "# "
  1900. "$ "))
  1901. (add-text-properties p1
  1902. p2
  1903. '(face underline))
  1904. (add-text-properties p3
  1905. p4
  1906. '(face underline))
  1907. (buffer-substring (point-min)
  1908. (point-max)))))
  1909. (add-hook 'eshell-mode-hook
  1910. (lambda ()
  1911. ;; (define-key eshell-mode-map (kbd "C-x C-x") (lambda ()
  1912. ;; (interactive)
  1913. ;; (switch-to-buffer (other-buffer))))
  1914. ;; (define-key eshell-mode-map (kbd "C-g") (lambda ()
  1915. ;; (interactive)
  1916. ;; (eshell-goto-prompt)
  1917. ;; (keyboard-quit)))
  1918. (define-key eshell-mode-map (kbd "C-x t")
  1919. 'eshell-text-mode-toggle)
  1920. (define-key eshell-mode-map (kbd "C-u")
  1921. 'eshell-kill-input)
  1922. (define-key eshell-mode-map (kbd "C-d")
  1923. 'eshell-delete-char-or-logout)
  1924. ;; (define-key eshell-mode-map (kbd "C-l")
  1925. ;; 'eshell-clear)
  1926. (define-key eshell-mode-map (kbd "DEL")
  1927. 'my-eshell-backward-delete-char)
  1928. (define-key eshell-mode-map (kbd "<up>")
  1929. 'scroll-down-line)
  1930. (define-key eshell-mode-map (kbd "<down>")
  1931. 'scroll-up-line)
  1932. (define-key eshell-mode-map
  1933. (kbd "C-p") 'eshell-previous-matching-input-from-input)
  1934. (define-key eshell-mode-map
  1935. (kbd "C-n") 'eshell-next-matching-input-from-input)
  1936. (apply 'eshell/addpath exec-path)
  1937. (set (make-local-variable 'scroll-margin) 0)
  1938. ;; (eshell/export "GIT_PAGER=")
  1939. ;; (eshell/export "GIT_EDITOR=")
  1940. (eshell/export "LC_MESSAGES=C")
  1941. (switch-to-buffer (current-buffer)) ; move buffer top of list
  1942. (set (make-local-variable 'hl-line-range-function)
  1943. (lambda ()
  1944. '(0 . 0)))
  1945. (add-to-list 'eshell-virtual-targets
  1946. '("/dev/less"
  1947. (lambda (str)
  1948. (if str
  1949. (with-current-buffer nil)))
  1950. nil))
  1951. ))
  1952. (add-hook 'eshell-mode-hook
  1953. (lambda ()
  1954. (add-to-list 'eshell-visual-commands "vim")
  1955. ;; (add-to-list 'eshell-visual-commands "git")
  1956. (add-to-list 'eshell-output-filter-functions
  1957. 'eshell-truncate-buffer)
  1958. (mapcar (lambda (alias)
  1959. (add-to-list 'eshell-command-aliases-list
  1960. alias))
  1961. '(
  1962. ; ("ll" "ls -l $*")
  1963. ; ("la" "ls -a $*")
  1964. ; ("lla" "ls -al $*")
  1965. ("eless"
  1966. (concat "cat >>> (with-current-buffer "
  1967. "(get-buffer-create \"*eshell output\") "
  1968. "(erase-buffer) "
  1969. "(setq buffer-read-only nil) "
  1970. "(current-buffer)) "
  1971. "(view-buffer (get-buffer \"*eshell output*\"))")
  1972. ))
  1973. )))
  1974. ) ; eval after load eshell
  1975. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1976. ;; my-term
  1977. (defvar my-term nil
  1978. "My terminal buffer.")
  1979. (defvar my-term-function nil
  1980. "Function to create terminal buffer.
  1981. This function accept no argument and return newly created buffer of terminal.")
  1982. (defun my-term (&optional arg)
  1983. "Open terminal buffer and return that buffer.
  1984. ARG is ignored."
  1985. (interactive "P")
  1986. (if (and my-term
  1987. (buffer-name my-term))
  1988. (pop-to-buffer my-term)
  1989. (setq my-term
  1990. (save-window-excursion
  1991. (funcall my-term-function)))
  1992. (and my-term
  1993. (my-term))))
  1994. ;; (setq my-term-function
  1995. ;; (lambda ()
  1996. ;; (if (eq system-type 'windows-nt)
  1997. ;; (eshell)
  1998. ;; (if (require 'multi-term nil t)
  1999. ;; (multi-term)
  2000. ;; (ansi-term shell-file-name)))))
  2001. (setq my-term-function 'eshell)
  2002. ;;(define-key my-prefix-map (kbd "C-s") 'my-term)
  2003. (define-key ctl-x-map "i" 'my-term)
  2004. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2005. ;; x open
  2006. (defvar my-filer nil)
  2007. (setq my-filer (or (executable-find "pcmanfm")
  2008. (executable-find "nautilus")))
  2009. (defun my-x-open (file)
  2010. "open FILE."
  2011. (interactive "FOpen File: ")
  2012. (setq file (expand-file-name file))
  2013. (message "Opening %s..." file)
  2014. (cond ((eq system-type 'windows-nt)
  2015. (call-process "cmd.exe" nil 0 nil
  2016. "/c" "start" "" (convert-standard-filename file)))
  2017. ((eq system-type 'darwin)
  2018. (call-process "open" nil 0 nil file))
  2019. ((getenv "DISPLAY")
  2020. (call-process (or my-filer "xdg-open") nil 0 nil file))
  2021. (t
  2022. (find-file file))
  2023. )
  2024. ;; (recentf-add-file file)
  2025. (message "Opening %s...done" file))
  2026. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2027. ;; misc funcs
  2028. (defun my-git-apply-index-from-buffer (&optional buf)
  2029. "Git apply buffer. BUF is buffer to apply. nil to use current buffer."
  2030. (interactive)
  2031. (let ((buf (or buf
  2032. (current-buffer)))
  2033. (file (make-temp-file "git-apply-diff.emacs")))
  2034. (with-current-buffer buf
  2035. (write-region (point-min)
  2036. (point-max)
  2037. file)
  2038. (call-process "git"
  2039. nil
  2040. nil
  2041. nil
  2042. "apply"
  2043. "--cached"
  2044. file))))
  2045. (defvar term-shell-command-history nil
  2046. "History for term-shell-command.")
  2047. (defun my-term-shell-command (command &optional new-buffer-p)
  2048. "Run COMMAND in terminal emulator.
  2049. If NEW-BUFFER-P is given or called with prefix argument, generate new buffer
  2050. and set the buffer the process buffer. Otherwise, existing buffer will be used."
  2051. (interactive (list (read-shell-command "Shell Command: "
  2052. nil
  2053. 'term-shell-command-history)
  2054. current-prefix-arg))
  2055. (let* ((name (car (split-string command
  2056. " ")))
  2057. (buf (if new-buffer-p
  2058. (generate-new-buffer "*term shell command*")
  2059. (get-buffer-create "*term shell command*")))
  2060. (proc (get-buffer-process buf))
  2061. (dir default-directory))
  2062. (and proc
  2063. (delete-process proc))
  2064. (display-buffer buf)
  2065. (with-current-buffer buf
  2066. (cd dir)
  2067. (set (make-local-variable 'term-scroll-to-bottom-on-output)
  2068. t)
  2069. (let ((inhibit-read-only t))
  2070. (goto-char (point-max))
  2071. (insert "\n")
  2072. (insert "Start executing "
  2073. command)
  2074. (add-text-properties (point-at-bol)
  2075. (point-at-eol)
  2076. '(face bold))
  2077. (insert "\n\n"))
  2078. (require 'term)
  2079. (term-mode)
  2080. (term-exec buf
  2081. (concat "term-" name)
  2082. shell-file-name
  2083. nil
  2084. (list shell-command-switch
  2085. command))
  2086. (term-char-mode)
  2087. (if (ignore-errors (get-buffer-process buf))
  2088. (set-process-sentinel (get-buffer-process buf)
  2089. (lambda (proc change)
  2090. (with-current-buffer (process-buffer proc)
  2091. (term-sentinel proc change)
  2092. (goto-char (point-max))
  2093. (fundamental-mode))))
  2094. (fundamental-mode)
  2095. (goto-char (point-max))
  2096. ))))
  2097. (define-key ctl-x-map ":" 'my-term-shell-command)
  2098. (defun memo (&optional dir)
  2099. "Open memo.txt in DIR."
  2100. (interactive)
  2101. (pop-to-buffer (find-file-noselect (concat (if dir
  2102. (file-name-as-directory dir)
  2103. "")
  2104. "memo.txt"))))
  2105. (defvar my-rgrep-alist
  2106. `(
  2107. ;; the silver searcher
  2108. ("ag"
  2109. (executable-find "ag")
  2110. "ag --nocolor --nogroup --nopager ")
  2111. ;; ack
  2112. ("ack"
  2113. (executable-find "ack")
  2114. "ack --nocolor --nogroup --nopager --with-filename ")
  2115. ;; gnu global
  2116. ("global"
  2117. (and (require 'gtags nil t)
  2118. (executable-find "global")
  2119. (gtags-get-rootpath))
  2120. "global --result grep ")
  2121. ;; git grep
  2122. ("gitgrep"
  2123. (eq 0
  2124. (shell-command "git rev-parse --git-dir"))
  2125. "git --no-pager -c color.grep=false grep -nH -e ")
  2126. ;; grep
  2127. ("grep"
  2128. t
  2129. ,(concat "find . "
  2130. "-path '*/.git' -prune -o "
  2131. "-path '*/.svn' -prune -o "
  2132. "-type f -print0 | "
  2133. "xargs -0 grep -nH -e "))
  2134. )
  2135. "Alist of rgrep command.
  2136. Each element is in the form like (NAME SEXP COMMAND), where SEXP returns the
  2137. condition to choose COMMAND when evaluated.")
  2138. (defvar my-rgrep-default nil
  2139. "Default command name for my-rgrep.")
  2140. (defun my-rgrep-grep-command (&optional name alist)
  2141. "Return recursive grep command for current directory or nil.
  2142. If NAME is given, use that without testing.
  2143. Commands are searched from ALIST."
  2144. (if alist
  2145. (if name
  2146. ;; if name is given search that from alist and return the command
  2147. (nth 2 (assoc name
  2148. alist))
  2149. ;; if name is not given try test in 1th elem
  2150. (let ((car (car alist))
  2151. (cdr (cdr alist)))
  2152. (if (eval (nth 1 car))
  2153. ;; if the condition is true return the command
  2154. (nth 2 car)
  2155. ;; try next one
  2156. (and cdr
  2157. (my-rgrep-grep-command name cdr)))))
  2158. ;; if alist is not given set default value
  2159. (my-rgrep-grep-command name my-rgrep-alist)))
  2160. (defun my-rgrep (command-args)
  2161. "My recursive grep. Run COMMAND-ARGS."
  2162. (interactive (let ((cmd (my-rgrep-grep-command my-rgrep-default
  2163. nil)))
  2164. (if cmd
  2165. (list (read-shell-command "grep command: "
  2166. cmd
  2167. 'grep-find-history))
  2168. (error "my-rgrep: Command for rgrep not found")
  2169. )))
  2170. (compilation-start command-args
  2171. 'grep-mode))
  2172. ;; (defun my-rgrep-symbol-at-point (command-args)
  2173. ;; "My recursive grep. Run COMMAND-ARGS."
  2174. ;; (interactive (list (read-shell-command "grep command: "
  2175. ;; (concat (my-rgrep-grep-command)
  2176. ;; " "
  2177. ;; (thing-at-point 'symbol))
  2178. ;; 'grep-find-history)))
  2179. ;; (compilation-start command-args
  2180. ;; 'grep-mode))
  2181. (defmacro define-my-rgrep (name)
  2182. "Define rgrep for NAME."
  2183. `(defun ,(intern (concat "my-rgrep-"
  2184. name)) ()
  2185. ,(format "My recursive grep by %s."
  2186. name)
  2187. (interactive)
  2188. (let ((my-rgrep-default ,name))
  2189. (if (called-interactively-p 'any)
  2190. (call-interactively 'my-rgrep)
  2191. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  2192. )
  2193. (define-my-rgrep "ack")
  2194. (define-my-rgrep "ag")
  2195. (define-my-rgrep "gitgrep")
  2196. (define-my-rgrep "grep")
  2197. (define-my-rgrep "global")
  2198. (define-key ctl-x-map "s" 'my-rgrep)
  2199. ;; (defun make ()
  2200. ;; "Run \"make -k\" in current directory."
  2201. ;; (interactive)
  2202. ;; (compile "make -k"))
  2203. (defalias 'make 'compile)
  2204. (defvar sed-in-place-history nil
  2205. "History of `sed-in-place'.")
  2206. (defvar sed-in-place-command "sed --in-place=.bak -e")
  2207. (defun sed-in-place (command)
  2208. "Issue sed in place COMMAND."
  2209. (interactive (list (read-shell-command "sed in place: "
  2210. (concat sed-in-place-command " ")
  2211. 'sed-in-place-history)))
  2212. (shell-command command
  2213. "*sed in place*"))
  2214. (defun dired-do-sed-in-place (&optional arg)
  2215. "Issue sed in place dired. If ARG is given, use the next ARG files."
  2216. (interactive "p")
  2217. (require 'dired-aux)
  2218. (let* ((files (dired-get-marked-files t arg))
  2219. (expr (dired-mark-read-string "Run sed-in-place for %s: "
  2220. nil
  2221. 'sed-in-place
  2222. arg
  2223. files)))
  2224. (if (equal expr
  2225. "")
  2226. (error "No expression specified")
  2227. (shell-command (concat sed-in-place-command
  2228. " '"
  2229. expr
  2230. "' "
  2231. (mapconcat 'shell-quote-argument
  2232. files
  2233. " "))
  2234. "*sed in place*"))))
  2235. (defun dir-show (&optional dir)
  2236. "Show DIR list."
  2237. (interactive)
  2238. (let ((bf (get-buffer-create "*dir show*"))
  2239. (list-directory-brief-switches "-C"))
  2240. (with-current-buffer bf
  2241. (list-directory (or nil
  2242. default-directory)
  2243. nil))
  2244. ))
  2245. (defun my-convmv-sjis2utf8-test ()
  2246. "Run `convmv -r -f sjis -t utf8 *'.
  2247. this is test, does not rename files."
  2248. (interactive)
  2249. (shell-command "convmv -r -f sjis -t utf8 *"))
  2250. (defun my-convmv-sjis2utf8-notest ()
  2251. "Run `convmv -r -f sjis -t utf8 * --notest'."
  2252. (interactive)
  2253. (shell-command "convmv -r -f sjis -t utf8 * --notest"))
  2254. (defun kill-ring-save-buffer-file-name ()
  2255. "Get current filename."
  2256. (interactive)
  2257. (let ((file buffer-file-name))
  2258. (if file
  2259. (progn (kill-new file)
  2260. (message file))
  2261. (message "not visiting file."))))
  2262. (defvar kill-ring-buffer-name "*kill-ring*"
  2263. "Buffer name for `kill-ring-buffer'.")
  2264. (defun open-kill-ring-buffer ()
  2265. "Open kill- ring buffer."
  2266. (interactive)
  2267. (pop-to-buffer
  2268. (with-current-buffer (get-buffer-create kill-ring-buffer-name)
  2269. (erase-buffer)
  2270. (yank)
  2271. (text-mode)
  2272. (current-local-map)
  2273. (goto-char (point-min))
  2274. (yank)
  2275. (current-buffer))))
  2276. (defun set-terminal-header (string)
  2277. "Set terminal header STRING."
  2278. (let ((savepos "\033[s")
  2279. (restorepos "\033[u")
  2280. (movecursor "\033[0;%dH")
  2281. (inverse "\033[7m")
  2282. (restorecolor "\033[0m")
  2283. (cols (frame-parameter nil 'width))
  2284. (length (length string)))
  2285. ;; (redraw-frame (selected-frame))
  2286. (send-string-to-terminal (concat savepos
  2287. (format movecursor
  2288. (1+ (- cols length)))
  2289. inverse
  2290. string
  2291. restorecolor
  2292. restorepos))
  2293. ))
  2294. (defun my-set-terminal-header ()
  2295. "Set terminal header."
  2296. (set-terminal-header (concat " "
  2297. user-login-name
  2298. "@"
  2299. (car (split-string system-name
  2300. "\\."))
  2301. " "
  2302. (format-time-string "%Y/%m/%d %T %z")
  2303. " ")))
  2304. ;; (run-with-timer
  2305. ;; 0.1
  2306. ;; 1
  2307. ;; 'my-set-terminal-header)
  2308. ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2309. ;; ;; savage emacs
  2310. ;; ;; when enabled emacs fails to complete
  2311. ;; ;; http://e-arrows.sakura.ne.jp/2010/05/emacs-should-be-more-savage.html
  2312. ;; (defadvice message (before message-for-stupid (arg &rest arg2) activate)
  2313. ;; (setq arg
  2314. ;; (concat arg
  2315. ;; (if (eq nil
  2316. ;; (string-match "\\. *$"
  2317. ;; arg))
  2318. ;; ".")
  2319. ;; " Stupid!")))
  2320. (defvar info-in-prompt
  2321. nil
  2322. "System info in the form of \"[user@host] \".")
  2323. (setq info-in-prompt
  2324. (concat "["
  2325. user-login-name
  2326. "@"
  2327. (car (split-string system-name
  2328. "\\."))
  2329. "]"))
  2330. (defun my-real-function-subr-p (function)
  2331. "Return t if FUNCTION is a built-in function even if it is advised."
  2332. (let* ((advised (and (symbolp function)
  2333. (featurep 'advice)
  2334. (ad-get-advice-info function)))
  2335. (real-function
  2336. (or (and advised (let ((origname (cdr (assq 'origname advised))))
  2337. (and (fboundp origname)
  2338. origname)))
  2339. function))
  2340. (def (if (symbolp real-function)
  2341. (symbol-function real-function)
  2342. function)))
  2343. (subrp def)))
  2344. ;; (my-real-function-subr-p 'my-real-function-subr-p)
  2345. ;; (defadvice read-from-minibuffer (before info-in-prompt activate)
  2346. ;; "Show system info when use `read-from-minibuffer'."
  2347. ;; (ad-set-arg 0
  2348. ;; (concat my-system-info
  2349. ;; (ad-get-arg 0))))
  2350. ;; (defadvice read-string (before info-in-prompt activate)
  2351. ;; "Show system info when use `read-string'."
  2352. ;; (ad-set-arg 0
  2353. ;; (concat my-system-info
  2354. ;; (ad-get-arg 0))))
  2355. ;; (when (< emacs-major-version 24)
  2356. ;; (defadvice completing-read (before info-in-prompt activate)
  2357. ;; "Show system info when use `completing-read'."
  2358. ;; (ad-set-arg 0
  2359. ;; (concat my-system-info
  2360. ;; (ad-get-arg 0)))))
  2361. (defmacro info-in-prompt-set (&rest functions)
  2362. "Set info-in-prompt advices for FUNCTIONS."
  2363. `(progn
  2364. ,@(mapcar (lambda (f)
  2365. `(defadvice ,f (before info-in-prompt activate)
  2366. "Show info in prompt."
  2367. (let ((orig (ad-get-arg 0)))
  2368. (unless (string-match-p (regexp-quote info-in-prompt)
  2369. orig)
  2370. (ad-set-arg 0
  2371. (concat info-in-prompt
  2372. " "
  2373. orig))))))
  2374. functions)))
  2375. (info-in-prompt-set read-from-minibuffer
  2376. read-string
  2377. completing-read)
  2378. ;;; emacs.el ends here