Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

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