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.
 
 
 
 
 
 

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