Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

2700 linhas
92 KiB

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