No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

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