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.
 
 
 
 
 
 

2693 lines
93 KiB

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