You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

emacs.el 64 KiB

12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818
  1. ;; load another file
  2. ;; (load-file "~/filepath")
  3. (unless (file-directory-p (expand-file-name user-emacs-directory))
  4. (make-directory (expand-file-name user-emacs-directory)))
  5. (let ((d (expand-file-name (concat user-emacs-directory
  6. "lisp"))))
  7. (unless (file-directory-p d)
  8. (make-directory d))
  9. (add-to-list 'load-path d))
  10. (require 'cl nil t)
  11. (progn ; hook run when directory changed
  12. (defvar buffer-file-changed-function nil "Hook run when buffer file changed.
  13. Each function is called with two args, the filename before changing and after changing.")
  14. (declare-function run-buffer-file-change-function "emacs.el")
  15. (add-hook 'post-command-hook
  16. 'run-buffer-file-changed-function)
  17. (lexical-let (previous-file)
  18. (defun run-buffer-file-changed-function ()
  19. ""
  20. (unless (and previous-file
  21. (equal previous-file
  22. (expand-file-name (or buffer-file-name
  23. default-directory))))
  24. (let ((pfile previous-file)
  25. (cfile (expand-file-name (or buffer-file-name
  26. default-directory))))
  27. (setq previous-file cfile)
  28. (run-hook-with-args 'directory-changed-function pfile cfile)))))
  29. ;; (add-hook 'directory-changed-function
  30. ;; (lambda (pdir cdir)
  31. ;; (message "dir changed %s to %s !" pdir cdir)))
  32. )
  33. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  34. ;; download library from web
  35. (require 'url)
  36. (defun dllib-if-unfound (lib url &optional bite-compile-p force-download-p)
  37. "if LIB does not exist, download it from URL and locate it to \"~/emacs.d/lisp/LIB.el\".
  38. return nil if LIB unfound and downloading failed, otherwise the path of LIB."
  39. (let* ((dir (expand-file-name (concat user-emacs-directory "lisp/")))
  40. (lpath (concat dir lib ".el"))
  41. (locate-p (locate-library lib)))
  42. (if (or force-download-p (not locate-p))
  43. (progn (condition-case nil
  44. (progn (message "downloading %s..." url)
  45. (url-copy-file url
  46. lpath
  47. t)
  48. (when bite-compile-p
  49. (byte-compile-file lpath)))
  50. (error (and (file-readable-p lpath)
  51. (delete-file lpath))
  52. (message "downloading %s...something wrong happened!" url)
  53. nil))
  54. (locate-library lib))
  55. locate-p)))
  56. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  57. ;; start and quit
  58. (setq inhibit-startup-message t)
  59. (setq frame-title-format (list '(:eval (format-time-string (or display-time-format "")))
  60. " | %b "
  61. '(:eval (number-to-string (length (buffer-list-not-start-with-space))))
  62. " buffers ["
  63. invocation-name
  64. " "
  65. emacs-version
  66. " "
  67. (symbol-name system-type)
  68. "] "
  69. '(:eval (symbol-name last-command))))
  70. (defun set-terminal-title (&rest args)
  71. ""
  72. (interactive "sString to set as title: ")
  73. (let ((tty (frame-parameter nil
  74. 'tty-type)))
  75. (when (and tty
  76. (eq t (compare-strings "xterm" 0 5
  77. tty 0 5)))
  78. (send-string-to-terminal (apply 'concat
  79. "\033]0;"
  80. `(,@args "\007"))))))
  81. (defun my-set-terminal-title ()
  82. ""
  83. (set-terminal-title "["
  84. invocation-name
  85. " "
  86. emacs-version
  87. " "
  88. (symbol-name system-type)
  89. "] "
  90. (abbreviate-file-name (or buffer-file-name default-directory))))
  91. (add-hook 'directory-changed-function
  92. (lambda (p c)
  93. (my-set-terminal-title)))
  94. (add-hook 'suspend-resume-hook
  95. 'my-set-terminal-title)
  96. (defun buffer-list-not-start-with-space ()
  97. (let ((bl (buffer-list))
  98. b nbl)
  99. (while bl
  100. (setq b (pop bl))
  101. (unless (string-equal " "
  102. (substring (buffer-name b)
  103. 0
  104. 1))
  105. (add-to-list 'nbl b)))
  106. nbl))
  107. (setq confirm-kill-emacs 'y-or-n-p)
  108. (setq gc-cons-threshold (* 1024 1024 4))
  109. (when window-system
  110. (add-to-list 'default-frame-alist '(cursor-type . box))
  111. (add-to-list 'default-frame-alist '(background-color . "white"))
  112. (add-to-list 'default-frame-alist '(foreground-color . "gray10"))
  113. ;; (add-to-list 'default-frame-alist '(alpha . (80 100 100 100))) ; does not work?
  114. )
  115. ;; (add-to-list 'default-frame-alist '(cursor-type . box))
  116. (if window-system (menu-bar-mode 1) (menu-bar-mode 0))
  117. (tool-bar-mode 0)
  118. (set-scroll-bar-mode nil)
  119. (add-hook 'kill-emacs-hook ; load when exitting to examine if file is written properly
  120. (lambda ()
  121. (when (file-readable-p "~/.emacs")
  122. (load-file "~/.emacs"))
  123. ))
  124. (add-hook 'after-init-hook
  125. (lambda ()
  126. (message (emacs-init-time))
  127. (switch-to-buffer "*Messages*")
  128. ))
  129. (cd ".") ; when using windows use / instead of \ in `default-directory'
  130. ;; locale
  131. (set-language-environment "Japanese")
  132. (set-default-coding-systems 'utf-8-unix)
  133. (prefer-coding-system 'utf-8-unix)
  134. (setq system-time-locale "C")
  135. ;; my prefix map
  136. (define-prefix-command 'my-prefix-map)
  137. '(add-hook 'after-init-hook
  138. (lambda ()
  139. (define-key ctl-x-map (kbd "C-x") 'my-prefix-map)
  140. ))
  141. (define-key ctl-x-map (kbd "C-x") 'my-prefix-map)
  142. (define-key my-prefix-map (kbd "C-q") 'quoted-insert)
  143. (define-key my-prefix-map (kbd "C-z") 'suspend-frame)
  144. ;; (comint-show-maximum-output)
  145. ;; kill scratch
  146. (add-hook 'after-init-hook
  147. (lambda ()
  148. (kill-buffer "*scratch*")))
  149. ;; modifier keys
  150. (setq mac-option-modifier 'control)
  151. (setq w32-apps-modifier 'meta)
  152. ;; display
  153. (setq redisplay-dont-pause t)
  154. (setq visible-bell t)
  155. (setq ring-bell-function 'ignore)
  156. (mouse-avoidance-mode 'banish)
  157. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  158. ;; global keys
  159. (and (dllib-if-unfound "drill-instructor"
  160. "https://raw.github.com/k1LoW/emacs-drill-instructor/master/drill-instructor.el"
  161. t)
  162. (require 'drill-instructor nil t)
  163. (setq drill-instructor-global t)
  164. (let ((map drill-instructor-key-map))
  165. (define-key map (kbd "RET") nil)
  166. (define-key map (kbd "DEL") nil)))
  167. '(mapc (lambda (key)
  168. (global-set-key (read-kbd-macro key) 'ignore))
  169. '("<up>" "<down>" "<right>" "<left>"))
  170. (define-key my-prefix-map (kbd "C-o") 'occur)
  171. ;; (define-key my-prefix-map (kbd "C-h") help-map)
  172. (global-set-key (kbd "C-\\") help-map)
  173. (define-key ctl-x-map (kbd "DEL") help-map)
  174. (define-key ctl-x-map (kbd "C-h") help-map)
  175. (define-key help-map "a" 'apropos)
  176. ;; compose window
  177. (global-set-key [?\C--] 'other-window)
  178. (global-set-key [?\C-0] 'delete-window)
  179. (global-set-key [?\C-1] 'delete-other-windows)
  180. (global-set-key [?\C-2] 'split-window-vertically)
  181. (global-set-key [?\C-3] 'split-window-horizontally)
  182. ;; disable annoying keys
  183. (global-set-key [prior] 'ignore)
  184. (global-set-key (kbd "<next>") 'ignore)
  185. (global-set-key [menu] 'ignore)
  186. (global-set-key [down-mouse-1] 'ignore)
  187. (global-set-key [down-mouse-2] 'ignore)
  188. (global-set-key [down-mouse-3] 'ignore)
  189. (global-set-key [mouse-1] 'ignore)
  190. (global-set-key [mouse-2] 'ignore)
  191. (global-set-key [mouse-3] 'ignore)
  192. (global-set-key (kbd "<eisu-toggle>") 'ignore)
  193. (global-set-key (kbd "C-<eisu-toggle>") 'ignore)
  194. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  195. ;; mode-line
  196. (setq eol-mnemonic-dos "crlf")
  197. (setq eol-mnemonic-mac "cr")
  198. (setq eol-mnemonic-unix "lf")
  199. (which-function-mode 0)
  200. (line-number-mode 0)
  201. (column-number-mode 0)
  202. (size-indication-mode 0)
  203. (setq mode-line-position
  204. '(:eval (format "L%%l/%d,C%%c"
  205. (count-lines (point-max)
  206. (point-min)))))
  207. ;; http://www.geocities.jp/simizu_daisuke/bunkei-meadow.html#frame-title
  208. ;; display date
  209. (add-hook 'after-init-hook
  210. (lambda ()
  211. (when display-time-mode
  212. (display-time-update))
  213. ))
  214. (setq display-time-interval 29)
  215. (setq display-time-day-and-date t)
  216. (setq display-time-format "%a, %d %b %Y %T")
  217. (if window-system
  218. (display-time-mode 0)
  219. (display-time-mode 1))
  220. ;; ;; current directory
  221. ;; (let ((ls (member 'mode-line-buffer-identification
  222. ;; mode-line-format)))
  223. ;; (setcdr ls
  224. ;; (cons '(:eval (concat " ("
  225. ;; (abbreviate-file-name default-directory)
  226. ;; ")"))
  227. ;; (cdr ls))))
  228. ;; ;; display last modified time
  229. ;; (let ((ls (member 'mode-line-buffer-identification
  230. ;; mode-line-format)))
  231. ;; (setcdr ls
  232. ;; (cons '(:eval (concat " "
  233. ;; my-buffer-file-last-modified-time))
  234. ;; (cdr ls))))
  235. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  236. ;; minibuffer
  237. (setq insert-default-directory t)
  238. (setq completion-ignore-case t
  239. read-file-name-completion-ignore-case t
  240. read-buffer-completion-ignore-case t)
  241. (setq resize-mini-window t)
  242. (temp-buffer-resize-mode 1)
  243. (savehist-mode 1)
  244. (fset 'yes-or-no-p 'y-or-n-p)
  245. (define-key read-expression-map (kbd "TAB") 'lisp-complete-symbol) ; complete symbol when `eval'
  246. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  247. ;; letters, font-lock mode and fonts
  248. ;; (set-face-background 'vertical-border (face-foreground 'mode-line))
  249. (when (eq system-type 'Darwin)
  250. (mac-set-input-method-parameter 'japanese 'cursor-color "red")
  251. (mac-set-input-method-parameter 'roman 'cursor-color "black"))
  252. (when (and (boundp 'input-method-activate-hook) ;ちょっと正しいかわかんない
  253. (boundp 'input-method-inactivate-hook))
  254. (add-hook 'input-method-activate-hook
  255. (lambda () (set-cursor-color "red")))
  256. (add-hook 'input-method-inactivate-hook
  257. (lambda () (set-cursor-color "black"))))
  258. (show-paren-mode 1)
  259. (setq show-paren-style 'mixed)
  260. (transient-mark-mode 1)
  261. (global-font-lock-mode 1)
  262. (setq font-lock-global-modes
  263. '(not
  264. help-mode
  265. eshell-mode
  266. term-mode
  267. Man-mode))
  268. (standard-display-ascii ?\n "$\n")
  269. (defvar my-eol-face
  270. '(("\n" . (0 font-lock-comment-face t nil)))
  271. )
  272. (defvar my-tab-face
  273. '(("\t" . '(0 highlight t nil))))
  274. (defvar my-jspace-face
  275. '(("\u3000" . '(0 highlight t nil))))
  276. (add-hook 'font-lock-mode-hook
  277. (lambda ()
  278. (font-lock-add-keywords nil my-eol-face)
  279. (font-lock-add-keywords nil my-jspace-face)
  280. ))
  281. ;; highlight current line
  282. ;; http://wiki.riywo.com/index.php?Meadow
  283. (defface hlline-face
  284. '((((type x w32)
  285. (class color)
  286. (background dark))
  287. (:background "midnightblue")) ; :foreground "white")) ;; ハイライトの文字色は変えない方がいいかも
  288. (((type x w32)
  289. (class color)
  290. (background light))
  291. (:background "gainsboro"))
  292. (t
  293. (:underline "black")))
  294. "*Face used by hl-line.")
  295. ;; (defface hlline-ul-face
  296. ;; '((t (:underline "yellow")))
  297. ;; "underline yellow")
  298. (setq hl-line-face 'hlline-face) ;; (setq hl-line-face nil)
  299. (global-hl-line-mode 1) ;; (hl-line-mode 1)
  300. (setq hl-line-global-modes
  301. '(not
  302. term-mode))
  303. (set-face-foreground 'font-lock-regexp-grouping-backslash "#666")
  304. (set-face-foreground 'font-lock-regexp-grouping-construct "#f60")
  305. ;; fonts
  306. (defun my-set-ascii-and-jp-font-with-size (list)
  307. ""
  308. (if (> emacs-major-version 22)
  309. (progn ; 23 or later
  310. (set-face-attribute 'default nil
  311. :family (nth 0 list)
  312. :height (nth 1 list))
  313. (set-fontset-font "fontset-default"
  314. 'japanese-jisx0208
  315. (font-spec :family (nth 2 list) :size (nth 3 list)))
  316. (set-fontset-font "fontset-default"
  317. 'katakana-jisx0201
  318. (font-spec :family (nth 2 list) :size (nth 3 list))))
  319. ; font spec is available in emacs23 and later, cannot used in emacs22
  320. (progn ; 22
  321. (set-face-attribute 'default nil
  322. :family (nth 0 list)
  323. :height (nth 1 list))
  324. (set-fontset-font "fontset-default"
  325. 'japanese-jisx0208
  326. (cons (nth 2 list) "jisx0208.*"))
  327. (set-fontset-font "fontset-default"
  328. 'katakana-jisx0201
  329. (cons (nth 2 list) "jisx0201.*"))
  330. )))
  331. ;; (my-set-ascii-and-jp-font-with-size '("dejavu sans mono" 90 "takaogothic" 13))
  332. ;; (my-set-ascii-and-jp-font-with-size '("dejavu sans mono" 100 "takaogothic" 14))
  333. ;; (my-set-ascii-and-jp-font-with-size '("dejavu sans mono" 100 "ms gothic" 14))
  334. ;; (my-set-ascii-and-jp-font-with-size '("monaco" 75 "takaogothic" 11))
  335. ;; (my-set-ascii-and-jp-font-with-size '("monaco" 90 "takaogothic" 13))
  336. ;; (my-set-ascii-and-jp-font-with-size '("ProggyCleanTTSZ" 120 "takaogothic" 11))
  337. ;; あ a
  338. (and (dllib-if-unfound "set-modeline-color"
  339. "https://raw.github.com/10sr/emacs-lisp/master/set-modeline-color.el"
  340. t)
  341. (progn
  342. (require 'set-modeline-color nil t)))
  343. (let ((fg (face-foreground 'default))
  344. (bg (face-background 'default)))
  345. (set-face-background 'mode-line-inactive (if (face-inverse-video-p 'mode-line) fg bg))
  346. (set-face-foreground 'mode-line-inactive (if (face-inverse-video-p 'mode-line) bg fg)))
  347. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  348. ;; file handling
  349. (setq revert-without-query '(".+"))
  350. ;; save cursor position
  351. (setq save-place-file (concat user-emacs-directory
  352. "places"))
  353. (when (require 'saveplace nil t)
  354. (setq-default save-place t))
  355. ;; http://www.bookshelf.jp/soft/meadow_24.html#SEC260
  356. (setq make-backup-files t)
  357. ;; (make-directory (expand-file-name "~/.emacsbackup"))
  358. (setq backup-directory-alist
  359. (cons (cons "\\.*$" (expand-file-name "~/.emacs.d/backup"))
  360. backup-directory-alist))
  361. (setq version-control 'never)
  362. (setq delete-old-versions t)
  363. (setq auto-save-list-file-prefix (expand-file-name "~/.emacs.d/autosave/"))
  364. (setq delete-auto-save-files t)
  365. (add-to-list 'completion-ignored-extensions ".bak")
  366. ;; (setq delete-by-moving-to-trash t
  367. ;; trash-directory "~/.emacs.d/trash")
  368. (add-hook 'after-save-hook
  369. 'executable-make-buffer-file-executable-if-script-p)
  370. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  371. ;; editting
  372. (setq require-final-newline t)
  373. (setq kill-whole-line t)
  374. (setq scroll-conservatively 35
  375. scroll-margin 2
  376. scroll-step 0)
  377. (setq default-major-mode 'text-mode)
  378. (setq next-line-add-newlines nil)
  379. (setq kill-read-only-ok t)
  380. (setq truncate-partial-width-windows nil) ; when splitted horizontally
  381. ;; (setq-default line-spacing 0.2)
  382. (setq-default indicate-empty-lines t) ; when using x indicate empty line
  383. (setq-default tab-width 4)
  384. (setq-default indent-tabs-mode nil)
  385. (setq-default indent-line-function nil)
  386. ;(pc-selection-mode 1) ; this make some already defined keybind back to default
  387. (delete-selection-mode 1)
  388. (cua-mode 0)
  389. (setq line-move-visual nil)
  390. ;; key bindings
  391. ;; moving around
  392. ;; (global-set-key (kbd "M-j") 'next-line)
  393. ;; (global-set-key (kbd "M-k") 'previous-line)
  394. ;; (global-set-key (kbd "M-h") 'backward-char)
  395. ;; (global-set-key (kbd "M-l") 'forward-char)
  396. ;;(keyboard-translate ?\M-j ?\C-j)
  397. ;; (global-set-key (kbd "M-p") 'backward-paragraph)
  398. (define-key esc-map "p" 'backward-paragraph)
  399. ;; (global-set-key (kbd "M-n") 'forward-paragraph)
  400. (define-key esc-map "n" 'forward-paragraph)
  401. (global-set-key (kbd "C-<up>") (lambda () (interactive)(scroll-down 1)))
  402. (global-set-key (kbd "C-<down>") (lambda () (interactive)(scroll-up 1)))
  403. (global-set-key (kbd "C-<left>") 'scroll-down)
  404. (global-set-key (kbd "C-<right>") 'scroll-up)
  405. (global-set-key (kbd "<select>") 'ignore) ; 'previous-line-mark)
  406. (define-key ctl-x-map (kbd "ESC x") 'execute-extended-command)
  407. (define-key ctl-x-map (kbd "ESC :") 'eval-expression)
  408. ;; C-h and DEL
  409. (global-set-key (kbd "C-h") (kbd "DEL"))
  410. (global-set-key (kbd "C-m") 'reindent-then-newline-and-indent)
  411. (global-set-key (kbd "C-o")
  412. ;; (lambda ()
  413. ;; (interactive)
  414. ;; (move-end-of-line nil)
  415. ;; (newline-and-indent))
  416. (kbd "C-e C-m")
  417. )
  418. (global-set-key (kbd "C-k") 'kill-whole-line)
  419. (global-set-key (kbd "M-k") 'my-copy-whole-line)
  420. ;; (global-set-key "\C-z" 'undo) ; undo is M-u
  421. (global-set-key (kbd "M-u") 'undo)
  422. (global-set-key (kbd "C-r") 'query-replace-regexp)
  423. (global-set-key (kbd "C-s") 'isearch-forward-regexp)
  424. (global-set-key (kbd "M-i") (kbd "ESC TAB"))
  425. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  426. ;; gmail
  427. (setq mail-interactive t
  428. send-mail-function 'smtpmail-send-it
  429. ;; message-send-mail-function 'smtpmail-send-it
  430. smtpmail-smtp-server "smtp.gmail.com"
  431. smtpmail-smtp-service 587
  432. smtpmail-starttls-credentials '(("smtp.gmail.com" 587 "8.slashes@gmail.com" nil))
  433. smtpmail-auth-credentials '(("smtp.gmail.com" 587 "8.slashes@gmail.com" nil))
  434. user-mail-address "8.slashes@gmail.com")
  435. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  436. ;; buffer killing
  437. (defun my-query-kill-this-buffer ()
  438. ""
  439. (interactive)
  440. (if (y-or-n-p (concat "kill this buffer? :"))
  441. (kill-buffer (current-buffer))))
  442. (substitute-key-definition 'kill-buffer 'my-query-kill-this-buffer global-map)
  443. ;;(global-set-key "\C-xk" 'my-query-kill-this-buffer)
  444. '(setq package-archives '(("ELPA" . "http://tromey.com/elpa/")
  445. ("gnu" . "http://elpa.gnu.org/packages/")
  446. ("marmalade" . "http://marmalade-repo.org/packages/")))
  447. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  448. ;; some modes and hooks
  449. (require 'simple nil t)
  450. (and window-system
  451. (dllib-if-unfound "save-window-size" "https://raw.github.com/10sr/emacs-lisp/master/save-window-size.el" t)
  452. (require 'save-window-size nil t))
  453. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  454. ;; share clipboard with x
  455. ;; this page describes this in details, but only these sexps are needed
  456. ;; http://garin.jp/doc/Linux/xwindow_clipboard
  457. (when (and window-system
  458. (not (eq window-system 'mac))
  459. )
  460. (setq x-select-enable-clipboard t ; these settings seems to be useless when using emacs in terminal
  461. x-select-enable-primary nil)
  462. ;; (global-set-key "\C-y" 'x-clipboard-yank)
  463. )
  464. (and (not x-select-enable-clipboard)
  465. (getenv "DISPLAY")
  466. (executable-find "xclip")
  467. (dllib-if-unfound "xclip" "http://www.emacswiki.org/emacs/download/xclip.el" t)
  468. (require 'xclip nil t)
  469. (turn-on-xclip))
  470. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  471. ;; mode
  472. (add-hook 'verilog-mode-hook
  473. (lambda ()
  474. (define-key verilog-mode-map ";" 'self-insert-command)))
  475. (setq diff-switches "-u")
  476. (add-hook 'diff-mode-hook
  477. (lambda ()
  478. (view-mode 1)
  479. (set-face-foreground 'diff-file-header-face nil)
  480. (set-face-foreground 'diff-header-face nil)
  481. (set-face-foreground 'diff-index-face "blue")
  482. (set-face-foreground 'diff-hunk-header-face "cyan")
  483. (set-face-foreground 'diff-context-face nil)
  484. (set-face-foreground 'diff-removed-face "red")
  485. (set-face-foreground 'diff-added-face "green")
  486. (set-face-foreground 'diff-changed-face "magenda")
  487. ))
  488. ;; (ffap-bindings)
  489. (add-hook 'sh-mode-hook
  490. (lambda ()
  491. (define-key sh-mode-map (kbd "C-x C-e") 'my-execute-shell-command-current-line)))
  492. (defun my-execute-shell-command-current-line ()
  493. ""
  494. (interactive)
  495. (shell-command (buffer-substring-no-properties (point-at-bol)
  496. (point))))
  497. (add-to-list 'auto-mode-alist
  498. '("\\(xinitrc\\|xprograms\\)\\'" . sh-mode))
  499. (setq python-python-command (or (executable-find "python3")
  500. (executable-find "python")))
  501. (defun my-python-run-as-command ()
  502. ""
  503. (interactive)
  504. (shell-command (concat python-python-command " " buffer-file-name)))
  505. (defun my-python-display-python-buffer ()
  506. ""
  507. (interactive)
  508. (set-window-text-height (display-buffer python-buffer
  509. t)
  510. 7))
  511. (add-hook 'python-mode-hook
  512. (lambda ()
  513. (define-key python-mode-map (kbd "C-c C-e") 'my-python-run-as-command)
  514. (define-key python-mode-map (kbd "C-c C-b") 'my-python-display-python-buffer)
  515. (define-key python-mode-map (kbd "C-m") 'newline-and-indent)))
  516. (add-hook 'inferior-python-mode-hook
  517. (lambda ()
  518. (my-python-display-python-buffer)
  519. (define-key inferior-python-mode-map (kbd "<up>") 'comint-previous-input)
  520. (define-key inferior-python-mode-map (kbd "<down>") 'comint-next-input)))
  521. (add-hook 'text-mode-hook
  522. (lambda ()
  523. (define-key text-mode-map (kbd "C-m") 'newline)))
  524. (add-to-list 'Info-default-directory-list (expand-file-name "~/.info/emacs-ja"))
  525. (setq bookmark-default-file "~/.emacs.d/bmk")
  526. (add-hook 'apropos-mode-hook
  527. (lambda ()
  528. (define-key apropos-mode-map "n" 'next-line)
  529. (define-key apropos-mode-map "p" 'previous-line)
  530. ))
  531. (define-key minibuffer-local-map (kbd "C-u") (lambda () (interactive) (delete-region (point-at-bol) (point))))
  532. (add-hook 'isearch-mode-hook
  533. (lambda ()
  534. ;; (define-key isearch-mode-map (kbd "C-j") 'isearch-other-control-char)
  535. ;; (define-key isearch-mode-map (kbd "C-k") 'isearch-other-control-char)
  536. ;; (define-key isearch-mode-map (kbd "C-h") 'isearch-other-control-char)
  537. (define-key isearch-mode-map (kbd "C-h") 'isearch-delete-char)
  538. (define-key isearch-mode-map (kbd "M-r") 'isearch-query-replace-regexp)))
  539. (add-hook 'outline-mode-hook
  540. (lambda ()
  541. (if (string-match "\\.md\\'" buffer-file-name)
  542. (set (make-local-variable 'outline-regexp) "#+ "))))
  543. (add-to-list 'auto-mode-alist (cons "\\.ol\\'" 'outline-mode))
  544. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'outline-mode))
  545. (setq markdown-command (or (executable-find "markdown")
  546. (executable-find "markdown.pl")))
  547. (when (dllib-if-unfound "markdown-mode"
  548. "http://jblevins.org/projects/markdown-mode/markdown-mode.el"
  549. t)
  550. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'markdown-mode))
  551. (autoload 'markdown-mode "markdown-mode" "Major mode for editing Markdown files." nil)
  552. (add-hook 'markdown-mode-hook
  553. (lambda ()
  554. (outline-minor-mode 1)
  555. (set (make-local-variable 'comment-start) ";"))))
  556. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  557. ;; c-mode
  558. ;; (setq c-default-style "bsd")
  559. ;; BackSpace キーを「賢く」し,インデント幅は2桁,タブはスペースに展開
  560. (add-hook 'c-mode-common-hook
  561. (lambda ()
  562. (setq c-basic-offset 2
  563. indent-tabs-mode nil)
  564. ;; (set-face-foreground 'font-lock-keyword-face "blue")
  565. (c-toggle-hungry-state 1)
  566. ))
  567. (when (dllib-if-unfound "js2-mode"
  568. "https://raw.github.com/mooz/js2-mode/master/js2-mode.el"
  569. t)
  570. (autoload 'js2-mode "js2-mode" nil t)
  571. (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  572. (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode)))
  573. ;; (add-hook 'js2-mode-hook
  574. ;; (lambda ()
  575. ;; (add-hook 'before-save-hook
  576. ;; 'my-indent-buffer
  577. ;; nil
  578. ;; t)))
  579. (add-hook 'js2-mode-hook
  580. (lambda ()
  581. (define-key js2-mode-map (kbd "C-m") (lambda ()
  582. (interactive)
  583. (js2-enter-key)
  584. (indent-for-tab-command)))
  585. (add-hook (kill-local-variable 'before-save-hook)
  586. 'js2-before-save)))
  587. (and nil
  588. (require 'zone nil t)
  589. (not (eq system-type 'windows-nt))
  590. ;; (zone-when-idle 180)
  591. (run-with-idle-timer 180 t (lambda ()
  592. (unless (memq major-mode
  593. '(term-mode))
  594. (zone)))))
  595. (when (require 'uniquify nil t)
  596. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  597. (setq uniquify-ignore-buffers-re "*[^*]+*")
  598. (setq uniquify-min-dir-content 1))
  599. (add-hook 'view-mode-hook
  600. (lambda()
  601. (define-key view-mode-map "j" (lambda() (interactive) (scroll-up 1)))
  602. (define-key view-mode-map "k" (lambda() (interactive) (scroll-down 1)))
  603. (define-key view-mode-map "v" 'toggle-read-only)
  604. (define-key view-mode-map "q" 'bury-buffer)
  605. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  606. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  607. ;; (define-key view-mode-map "n" 'nonincremental-repeat-search-forward)
  608. ;; (define-key view-mode-map "N" 'nonincremental-repeat-search-backward)
  609. (define-key view-mode-map "/" 'isearch-forward-regexp)
  610. (define-key view-mode-map "?" 'isearch-backward-regexp)
  611. (define-key view-mode-map "n" 'isearch-repeat-forward)
  612. (define-key view-mode-map "N" 'isearch-repeat-backward)
  613. ))
  614. (global-set-key "\M-r" 'view-mode)
  615. (setq view-read-only t)
  616. (add-hook 'Man-mode-hook
  617. (lambda ()
  618. (view-mode 1)
  619. (setq truncate-lines nil)))
  620. (setq Man-notify-method (if window-system
  621. 'newframe
  622. 'pushy))
  623. (require 'session nil t)
  624. (and (dllib-if-unfound "gtkbm"
  625. "https://raw.github.com/10sr/emacs-lisp/master/gtkbm.el"
  626. t)
  627. (require 'gtkbm nil t)
  628. (global-set-key (kbd "C-x C-d") 'gtkbm))
  629. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  630. ;; term mode
  631. ;; (setq multi-term-program shell-file-name)
  632. (and (dllib-if-unfound "multi-term"
  633. "http://www.emacswiki.org/emacs/download/multi-term.el"
  634. t)
  635. (require 'multi-term nil t)
  636. (setq multi-term-switch-after-close nil))
  637. (defun my-term-quit-or-send-raw ()
  638. ""
  639. (interactive)
  640. (if (get-buffer-process (current-buffer))
  641. (call-interactively 'term-send-raw)
  642. (kill-buffer)))
  643. ;; http://d.hatena.ne.jp/goinger/20100416/1271399150
  644. ;; (setq term-ansi-default-program shell-file-name)
  645. (add-hook 'term-setup-hook
  646. (lambda ()
  647. (setq term-display-table (make-display-table))))
  648. (add-hook 'term-mode-hook
  649. (lambda ()
  650. (unless (memq (current-buffer) (and (featurep 'multi-term) ; current buffer is not multi-term buffer
  651. (multi-term-list)))
  652. ;; (define-key term-raw-map "\C-q" 'move-beginning-of-line)
  653. ;; (define-key term-raw-map "\C-r" 'term-send-raw)
  654. ;; (define-key term-raw-map "\C-s" 'term-send-raw)
  655. ;; (define-key term-raw-map "\C-f" 'forward-char)
  656. ;; (define-key term-raw-map "\C-b" 'backward-char)
  657. ;; (define-key term-raw-map "\C-t" 'set-mark-command)
  658. (define-key term-raw-map "\C-x" (lookup-key (current-global-map) "\C-x"))
  659. (define-key term-raw-map "\C-z" (lookup-key (current-global-map) "\C-z")))
  660. (define-key term-raw-map (kbd "C-p") 'term-send-raw)
  661. (define-key term-raw-map (kbd "C-n") 'term-send-raw)
  662. (define-key term-raw-map "q" 'my-term-quit-or-send-raw)
  663. ;; (define-key term-raw-map (kbd "ESC") 'term-send-raw)
  664. (define-key term-raw-map [delete] 'term-send-raw)
  665. (define-key term-raw-map (kbd "DEL") 'term-send-backspace)
  666. (define-key term-raw-map "\C-y" 'term-paste)
  667. (define-key term-raw-map "\C-c" 'term-send-raw) ;; 'term-interrupt-subjob)
  668. '(define-key term-mode-map (kbd "C-x C-q") 'term-pager-toggle)
  669. ;; (dolist (key '("<up>" "<down>" "<right>" "<left>"))
  670. ;; (define-key term-raw-map (read-kbd-macro key) 'term-send-raw))
  671. ;; (define-key term-raw-map "\C-d" 'delete-char)
  672. (set (make-local-variable 'scroll-margin) 0)
  673. ;; (set (make-local-variable 'cua-enable-cua-keys) nil)
  674. ;; (cua-mode 0)
  675. ;; (and cua-mode
  676. ;; (local-unset-key (kbd "C-c")))
  677. ;; (define-key cua--prefix-override-keymap "\C-c" 'term-interrupt-subjob)
  678. (set (make-local-variable 'hl-line-range-function)
  679. (lambda ()
  680. '(0 . 0)))
  681. ))
  682. ;; (add-hook 'term-exec-hook 'forward-char)
  683. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  684. ;; buffer switching
  685. (when (require 'bs nil t)
  686. ;; (global-set-key "\C-x\C-b" 'bs-show)
  687. (defalias 'list-buffers 'bs-show))
  688. ;; (add-to-list 'bs-configurations '("processes" nil get-buffer-process ".*" nil nil))
  689. (add-to-list 'bs-configurations '("same-dir" nil buffer-same-dir-p ".*" nil nil))
  690. (add-to-list 'bs-configurations '("this-frame" nil (lambda (buf) (memq buf (my-frame-buffer-get))) ".*" nil nil))
  691. ;; (setq bs-configurations (list '("processes" nil get-buffer-process ".*" nil nil)
  692. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil bs-visits-non-file bs-sort-buffer-interns-are-last)))
  693. (setq bs-default-configuration "this-frame")
  694. (setq bs-default-sort-name "by name")
  695. (add-hook 'bs-mode-hook
  696. (lambda ()
  697. (setq bs-default-configuration "this-frame")
  698. ;; (and bs--show-all
  699. ;; (call-interactively 'bs-toggle-show-all))
  700. (set (make-local-variable 'scroll-margin) 0)
  701. ))
  702. (defun buffer-same-dir-p (bf)
  703. "return t if BF's dir is same as current dir, otherwise nil."
  704. (let ((cdir (expand-file-name default-directory)))
  705. (with-current-buffer bf
  706. (equal (expand-file-name default-directory) cdir))))
  707. (iswitchb-mode 1)
  708. (defun iswitchb-buffer-display-other-window ()
  709. ""
  710. (interactive)
  711. (let ((iswitchb-default-method 'display))
  712. (call-interactively 'iswitchb-buffer)))
  713. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  714. ;; sdic
  715. (defun sdic-describe-word-at-point-echo ()
  716. ""
  717. (interactive)
  718. (save-window-excursion
  719. (sdic-describe-word-at-point))
  720. (save-excursion
  721. (set-buffer sdic-buffer-name)
  722. (message (buffer-substring (point-min)
  723. (progn (goto-char (point-min))
  724. (or (and (re-search-forward "^\\w" nil t 4)
  725. (progn (previous-line) t)
  726. (point-at-eol))
  727. (point-max)))))))
  728. (setq sdic-eiwa-dictionary-list '((sdicf-client "/usr/share/dict/gene.sdic")))
  729. (setq sdic-waei-dictionary-list '((sdicf-client "/usr/share/dict/jedict.sdic" (add-keys-to-headword t))))
  730. (setq sdic-disable-select-window t)
  731. (setq sdic-window-height 7)
  732. (when (require 'sdic nil t)
  733. ;; (define-key my-prefix-map "\C-w" 'sdic-describe-word)
  734. (define-key my-prefix-map "\C-t" 'sdic-describe-word-at-point-echo))
  735. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  736. ;; vc
  737. ;; (require 'vc)
  738. (setq vc-handled-backends '())
  739. (and (executable-find "git")
  740. (add-to-list 'vc-handled-backends 'GIT))
  741. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  742. ;; gauche-mode
  743. (let ((s (executable-find "gosh")))
  744. (setq scheme-program-name s
  745. gauche-program-name s))
  746. (defun run-gauche-other-window ()
  747. "Run gauche on other window"
  748. (interactive)
  749. (switch-to-buffer-other-window
  750. (get-buffer-create "*scheme*"))
  751. (run-gauche))
  752. (defun run-gauche ()
  753. "run gauche"
  754. (run-scheme gauche-program-name)
  755. )
  756. (defun scheme-send-buffer ()
  757. ""
  758. (interactive)
  759. (scheme-send-region (point-min) (point-max))
  760. (my-scheme-display-scheme-buffer)
  761. )
  762. (defun my-scheme-display-scheme-buffer ()
  763. ""
  764. (interactive)
  765. (set-window-text-height (display-buffer scheme-buffer
  766. t)
  767. 7))
  768. (add-hook 'scheme-mode-hook
  769. (lambda ()
  770. nil))
  771. (add-hook 'inferior-scheme-mode-hook
  772. (lambda ()
  773. ;; (my-scheme-display-scheme-buffer)
  774. ))
  775. ;; http://d.hatena.ne.jp/kobapan/20090305/1236261804
  776. ;; http://www.katch.ne.jp/~leque/software/repos/gauche-mode/gauche-mode.el
  777. (when (dllib-if-unfound "gauche-mode"
  778. "http://www.katch.ne.jp/~leque/software/repos/gauche-mode/gauche-mode.el"
  779. t)
  780. (setq auto-mode-alist
  781. (cons '("\.gosh\\'" . gauche-mode) auto-mode-alist))
  782. (setq auto-mode-alist
  783. (cons '("\.gaucherc\\'" . gauche-mode) auto-mode-alist))
  784. (autoload 'gauche-mode "gauche-mode" "Major mode for Scheme." t)
  785. (autoload 'run-scheme "gauche-mode" "Run an inferior Scheme process." t)
  786. (add-hook 'gauche-mode-hook
  787. (lambda ()
  788. (define-key gauche-mode-map (kbd "C-c C-z") 'run-gauche-other-window)
  789. (define-key scheme-mode-map (kbd "C-c C-c") 'scheme-send-buffer)
  790. (define-key scheme-mode-map (kbd "C-c C-b") 'my-scheme-display-scheme-buffer)
  791. )))
  792. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  793. ;; recentf-mode
  794. (setq recentf-save-file (expand-file-name "~/.emacs.d/recentf")
  795. recentf-max-menu-items 20
  796. recentf-max-saved-items 30
  797. recentf-show-file-shortcuts-flag nil)
  798. (when (require 'recentf nil t)
  799. ;; (global-set-key "\C-x\C-r" 'recentf-open-files)
  800. (define-key ctl-x-map (kbd "C-r") 'recentf-open-files)
  801. ;; (add-hook 'find-file-hook
  802. ;; (lambda ()
  803. ;; (recentf-add-file default-directory)))
  804. (recentf-mode 1)
  805. ;; (add-to-list 'recentf-filename-handlers 'abbreviate-file-name)
  806. (add-to-list 'recentf-exclude (regexp-quote recentf-save-file))
  807. (and (dllib-if-unfound "recentf-show"
  808. "https://raw.github.com/10sr/emacs-lisp/master/recentf-show.el"
  809. t)
  810. (require 'recentf-show nil t)
  811. (define-key ctl-x-map (kbd "C-r") 'recentf-show)))
  812. (add-hook 'recentf-dialog-mode-hook
  813. (lambda ()
  814. (recentf-save-list)
  815. ;; (define-key recentf-dialog-mode-map (kbd "C-x C-f") 'my-recentf-cd-and-find-file)
  816. (define-key recentf-dialog-mode-map (kbd "<up>") 'previous-line)
  817. (define-key recentf-dialog-mode-map (kbd "<down>") 'next-line)
  818. (define-key recentf-dialog-mode-map "p" 'previous-line)
  819. (define-key recentf-dialog-mode-map "n" 'next-line)
  820. (cd "~/")))
  821. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  822. ;; dired
  823. (require 'dired)
  824. (defun my-dired-echo-file-head (arg)
  825. ""
  826. (interactive "P")
  827. (let ((f (dired-get-filename)))
  828. (message "%s"
  829. (with-temp-buffer
  830. (insert-file-contents f)
  831. (buffer-substring-no-properties (point-min)
  832. (progn (goto-line (if arg
  833. (prefix-numeric-value arg)
  834. 10))
  835. (point-at-eol)))))))
  836. (defun my-dired-diff ()
  837. ""
  838. (interactive)
  839. (let ((files (dired-get-marked-files nil nil nil t)))
  840. (if (eq (car files)
  841. t)
  842. (diff (cadr files) (dired-get-filename))
  843. (message "One files must be marked!"))))
  844. (defun my-pop-to-buffer-erase-noselect (buffer-or-name)
  845. "pop up buffer using `display-buffer' and return that buffer."
  846. (let ((bf (get-buffer-create buffer-or-name)))
  847. (with-current-buffer bf
  848. (cd ".")
  849. (erase-buffer))
  850. (display-buffer bf)
  851. bf))
  852. (defun my-replace-nasi-none ()
  853. ""
  854. (save-excursion
  855. (let ((buffer-read-only nil))
  856. (goto-char (point-min))
  857. (while (search-forward "なし" nil t)
  858. (replace-match "none")))))
  859. (defun dired-get-file-info ()
  860. "dired get disk usage"
  861. (interactive)
  862. (let ((f (dired-get-filename)))
  863. (if (file-directory-p f)
  864. (progn
  865. (message "calculating du...")
  866. (shell-command (concat "du -hsD "
  867. f)))
  868. (shell-command (concat "file "
  869. f)))))
  870. (defun my-dired-scroll-up ()
  871. ""
  872. (interactive)
  873. (my-dired-previous-line (- (window-height) 1)))
  874. (defun my-dired-scroll-down ()
  875. ""
  876. (interactive)
  877. (my-dired-next-line (- (window-height) 1)))
  878. (defun my-dired-previous-line (arg)
  879. ""
  880. (interactive "p")
  881. (when (> arg 0)
  882. ;; (ignore 'my-dired-print-current-dir-and-file)
  883. (dired-previous-line 1)
  884. (when (eq (line-number-at-pos)
  885. 2)
  886. (goto-line (- (line-number-at-pos (point-max))
  887. 1))
  888. (dired-move-to-filename))
  889. (my-dired-previous-line (- arg 1))
  890. ))
  891. (defun my-dired-next-line (arg)
  892. ""
  893. (interactive "p")
  894. (when (> arg 0)
  895. ;; (ignore 'my-dired-print-current-dir-and-file)
  896. (dired-next-line 1)
  897. (when (eq (point)
  898. (point-max))
  899. (goto-line 3)
  900. (dired-move-to-filename))
  901. (my-dired-next-line (- arg 1))
  902. ))
  903. (defun my-dired-print-current-dir-and-file ()
  904. (message "%s %s"
  905. default-directory
  906. (buffer-substring-no-properties (point-at-bol)
  907. (point-at-eol))))
  908. (defun dired-do-execute-as-command ()
  909. ""
  910. (interactive)
  911. (let ((file (dired-get-filename t)))
  912. (if (file-executable-p file)
  913. (start-process file nil file)
  914. (when (y-or-n-p "this file cant be executed. mark as executable and go? : ")
  915. (set-file-modes file (file-modes-symbolic-to-number "u+x" (file-modes file)))
  916. (start-process file nil file)))))
  917. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  918. (defun my-dired-x-open ()
  919. ""
  920. (interactive)
  921. (my-x-open (dired-get-filename t t)))
  922. (if (eq window-system 'mac)
  923. (setq dired-listing-switches "-lhFG")
  924. (setq dired-listing-switches "-lhFG --time-style=long-iso")
  925. )
  926. (setq dired-listing-switches "-lhFG")
  927. (put 'dired-find-alternate-file 'disabled nil) ; when using dired-find-alternate-file reuse current dired buffer for the file to open
  928. (setq dired-ls-F-marks-symlinks t)
  929. (require 'ls-lisp)
  930. (setq ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  931. (setq ls-lisp-dirs-first t)
  932. (setq ls-lisp-use-localized-time-format t)
  933. (setq ls-lisp-format-time-list
  934. '("%Y-%m-%d %H:%M"
  935. "%Y-%m-%d "))
  936. (setq dired-dwim-target t)
  937. ;; (add-hook 'dired-after-readin-hook
  938. ;; 'my-replace-nasi-none)
  939. ;; (add-hook 'after-init-hook
  940. ;; (lambda ()
  941. ;; (dired ".")))
  942. (add-hook 'dired-mode-hook
  943. (lambda ()
  944. (define-key dired-mode-map "o" 'my-dired-x-open)
  945. (define-key dired-mode-map "i" 'dired-get-file-info)
  946. (define-key dired-mode-map "f" 'find-file)
  947. (define-key dired-mode-map "!" 'shell-command)
  948. (define-key dired-mode-map "&" 'async-shell-command)
  949. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  950. (define-key dired-mode-map "=" 'my-dired-diff)
  951. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  952. (define-key dired-mode-map "b" 'gtkbm)
  953. (define-key dired-mode-map "h" 'my-dired-echo-file-head)
  954. (define-key dired-mode-map "@" (lambda () (interactive) (my-x-open ".")))
  955. (define-key dired-mode-map (kbd "TAB") 'other-window)
  956. ;; (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  957. (define-key dired-mode-map "/" 'dired-isearch-filenames)
  958. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  959. (define-key dired-mode-map (kbd "C-h") 'dired-up-directory)
  960. (substitute-key-definition 'dired-next-line 'my-dired-next-line dired-mode-map)
  961. (substitute-key-definition 'dired-previous-line 'my-dired-previous-line dired-mode-map)
  962. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  963. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  964. (let ((file "._Icon\015"))
  965. (when (file-readable-p file)
  966. (delete-file file)))))
  967. (and (dllib-if-unfound "pack"
  968. "https://raw.github.com/10sr/emacs-lisp/master/pack.el"
  969. t)
  970. (require 'pack nil t)
  971. (add-hook 'dired-mode-hook
  972. (lambda ()
  973. (define-key dired-mode-map "P" 'dired-do-pack-or-unpack))))
  974. (and (dllib-if-unfound "dired-list-all-mode"
  975. "https://raw.github.com/10sr/emacs-lisp/master/dired-list-all-mode.el"
  976. t)
  977. (require 'dired-list-all-mode nil t)
  978. (setq dired-listing-switches "-lhFG")
  979. (add-hook 'dired-mode-hook
  980. (lambda ()
  981. (define-key dired-mode-map "a" 'dired-list-all-mode)
  982. )))
  983. ;; http://blog.livedoor.jp/tek_nishi/archives/4693204.html
  984. (defun my-dired-toggle-mark()
  985. (let ((cur (cond ((eq (following-char) dired-marker-char) ?\040)
  986. (t dired-marker-char))))
  987. (delete-char 1)
  988. (insert cur)))
  989. (defun my-dired-mark (arg)
  990. "toggle mark the current (or next ARG) files.
  991. If on a subdir headerline, mark all its files except `.' and `..'.
  992. Use \\[dired-unmark-all-files] to remove all marks
  993. and \\[dired-unmark] on a subdir to remove the marks in
  994. this subdir."
  995. (interactive "P")
  996. (if (dired-get-subdir)
  997. (save-excursion (dired-mark-subdir-files))
  998. (let ((inhibit-read-only t))
  999. (dired-repeat-over-lines
  1000. (prefix-numeric-value arg)
  1001. 'my-dired-toggle-mark))))
  1002. (defun my-dired-mark-backward (arg)
  1003. "In Dired, move up lines and toggle mark there.
  1004. Optional prefix ARG says how many lines to unflag; default is one line."
  1005. (interactive "p")
  1006. (my-dired-mark (- arg)))
  1007. (add-hook 'dired-mode-hook
  1008. (lambda ()
  1009. (local-set-key (kbd "SPC") 'my-dired-mark)
  1010. (local-set-key (kbd "S-SPC") 'my-dired-mark-backward))
  1011. )
  1012. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1013. ;; eshell
  1014. (defun my-eshell-backward-delete-char ()
  1015. (interactive)
  1016. (when (< (save-excursion
  1017. (eshell-bol)
  1018. (point))
  1019. (point))
  1020. (backward-delete-char 1)))
  1021. (defun my-file-owner-p (file)
  1022. "t if FILE is owned by me."
  1023. (eq (user-uid) (nth 2 (file-attributes file))))
  1024. ;; ;; http://www.bookshelf.jp/pukiwiki/pukiwiki.php?Eshell%A4%F2%BB%C8%A4%A4%A4%B3%A4%CA%A4%B9
  1025. ;; ;; written by Stefan Reichoer <reichoer@web.de>
  1026. ;; (defun eshell/less (&rest args)
  1027. ;; "Invoke `view-file' on the file.
  1028. ;; \"less +42 foo\" also goes to line 42 in the buffer."
  1029. ;; (if args
  1030. ;; (while args
  1031. ;; (if (string-match "\\`\\+\\([0-9]+\\)\\'" (car args))
  1032. ;; (let* ((line (string-to-number (match-string 1 (pop args))))
  1033. ;; (file (pop args)))
  1034. ;; (view-file file)
  1035. ;; (goto-line line))
  1036. ;; (view-file (pop args))))))
  1037. (defun eshell/o (&optional file)
  1038. (my-x-open (or file ".")))
  1039. ;; (defun eshell/vi (&rest args)
  1040. ;; "Invoke `find-file' on the file.
  1041. ;; \"vi +42 foo\" also goes to line 42 in the buffer."
  1042. ;; (while args
  1043. ;; (if (string-match "\\`\\+\\([0-9]+\\)\\'" (car args))
  1044. ;; (let* ((line (string-to-number (match-string 1 (pop args))))
  1045. ;; (file (pop args)))
  1046. ;; (find-file file)
  1047. ;; (goto-line line))
  1048. ;; (find-file (pop args)))))
  1049. (defun eshell/clear ()
  1050. "Clear the current buffer, leaving one prompt at the top."
  1051. (let ((inhibit-read-only t))
  1052. (erase-buffer)))
  1053. (defun eshell/d (&optional dirname switches)
  1054. "if first arg is omitted open current directory."
  1055. (dired (or dirname ".") switches))
  1056. (defun eshell/v ()
  1057. (view-mode 1))
  1058. (defun eshell/git (&rest args)
  1059. ""
  1060. (if (member (car args)
  1061. '("di" "diff" "log" "show"))
  1062. (apply 'eshell-exec-visual "git" args)
  1063. (shell-command (mapconcat 'shell-quote-argument
  1064. `("git" ,@args)
  1065. " ")
  1066. t)
  1067. ;; (eshell-external-command "git" args)
  1068. ))
  1069. (defalias 'eshell/: 'ignore)
  1070. (defalias 'eshell/type 'eshell/which)
  1071. ;; (defalias 'eshell/vim 'eshell/vi)
  1072. (defalias 'eshell/ff 'find-file)
  1073. (defalias 'eshell/q 'eshell/exit)
  1074. (defun eshell-goto-prompt ()
  1075. ""
  1076. (interactive)
  1077. (goto-char (point-max)))
  1078. (defun eshell-cd-default-directory (&optional eshell-buffer-or-name)
  1079. "open eshell and change wd
  1080. if arg given, use that eshell buffer, otherwise make new eshell buffer."
  1081. (interactive)
  1082. (let ((dir (expand-file-name default-directory)))
  1083. (switch-to-buffer (or eshell-buffer-or-name
  1084. (eshell t)))
  1085. (unless (equal dir (expand-file-name default-directory))
  1086. ;; (cd dir)
  1087. ;; (eshell-interactive-print (concat "cd " dir "\n"))
  1088. ;; (eshell-emit-prompt)
  1089. (goto-char (point-max))
  1090. (eshell-kill-input)
  1091. (insert "cd " dir)
  1092. (eshell-send-input))))
  1093. (setq eshell-directory-name "~/.emacs.d/eshell/")
  1094. (setq eshell-term-name "eterm-color")
  1095. (setq eshell-scroll-to-bottom-on-input t)
  1096. (setq eshell-cmpl-ignore-case t)
  1097. (setq eshell-cmpl-cycle-completions nil)
  1098. (setq eshell-highlight-prompt nil)
  1099. (setq eshell-ls-initial-args '("-hCFG"
  1100. "--color=auto"
  1101. "--time-style=long-iso")) ; "-hF")
  1102. (setq eshell-prompt-function
  1103. (lambda ()
  1104. (with-temp-buffer
  1105. (let (p1 p2 p3 p4)
  1106. (insert " [")
  1107. (setq p1 (point))
  1108. (insert (abbreviate-file-name default-directory))
  1109. (setq p2 (point))
  1110. (insert "]"
  1111. "\n")
  1112. (setq p3 (point))
  1113. (insert user-login-name
  1114. "@"
  1115. (or (getenv "HOSTNAME")
  1116. (substring (shell-command-to-string (or (executable-find "hostname")
  1117. "echo ''"))
  1118. 0
  1119. -1)))
  1120. (setq p4 (point))
  1121. (insert " "
  1122. (format-time-string "%a, %d %b %Y %T %z")
  1123. " eshell\n"
  1124. "last:"
  1125. (number-to-string eshell-last-command-status)
  1126. (if (= (user-uid)
  1127. 0)
  1128. " # "
  1129. " $ "))
  1130. (add-text-properties p1
  1131. p2
  1132. '(face ((foreground-color . "yellow"))))
  1133. (add-text-properties p3
  1134. p4
  1135. '(face ((foreground-color . "cyan"))))
  1136. (buffer-substring (point-min)
  1137. (point-max))))))
  1138. (add-hook 'eshell-mode-hook
  1139. (lambda ()
  1140. ;; (define-key eshell-mode-map (kbd "C-x C-x") (lambda ()
  1141. ;; (interactive)
  1142. ;; (switch-to-buffer (other-buffer))))
  1143. (define-key eshell-mode-map (kbd "C-u") (lambda ()
  1144. (interactive)
  1145. (eshell-goto-prompt)
  1146. (eshell-kill-input)))
  1147. (define-key eshell-mode-map (kbd "C-g") (lambda ()
  1148. (interactive)
  1149. (eshell-goto-prompt)
  1150. (my-keyboard-quit)))
  1151. (define-key eshell-mode-map (kbd "DEL") 'my-eshell-backward-delete-char)
  1152. (define-key eshell-mode-map (kbd "C-p") 'eshell-previous-matching-input-from-input)
  1153. (define-key eshell-mode-map (kbd "C-n") 'eshell-next-matching-input-from-input)
  1154. (apply 'eshell/addpath exec-path)
  1155. (set (make-local-variable 'scroll-margin) 0)
  1156. ;; (eshell/export "GIT_PAGER=")
  1157. ;; (eshell/export "GIT_EDITOR=")
  1158. (eshell/export "LC_MESSAGES=C")
  1159. (switch-to-buffer (current-buffer)) ; move buffer top of list
  1160. (set (make-local-variable 'hl-line-range-function)
  1161. (lambda ()
  1162. '(0 . 0)))
  1163. (add-to-list 'eshell-virtual-targets
  1164. '("/dev/less"
  1165. (lambda (str)
  1166. (if str
  1167. (with-current-buffer nil)))
  1168. nil))
  1169. ))
  1170. (add-hook 'eshell-mode-hook
  1171. (lambda ()
  1172. (add-to-list 'eshell-visual-commands "vim")
  1173. ;; (add-to-list 'eshell-visual-commands "git")
  1174. (add-to-list 'eshell-output-filter-functions 'eshell-truncate-buffer)
  1175. (mapcar (lambda (alias)
  1176. (add-to-list 'eshell-command-aliases-list
  1177. alias))
  1178. '(
  1179. ; ("ll" "ls -l $*")
  1180. ; ("la" "ls -a $*")
  1181. ; ("lla" "ls -al $*")
  1182. ("ut" "slogin 03110414@un001.ecc.u-tokyo.ac.jp $*")
  1183. ("aptin" "apt-get install $*")
  1184. ("eless" "cat >>> (with-current-buffer (get-buffer-create \"*eshell output\") (erase-buffer) (setq buffer-read-only nil) (current-buffer)); (view-buffer (get-buffer \"*eshell output*\"))")
  1185. ("g" "git $*")
  1186. ))
  1187. ))
  1188. ;; (eval-after-load "em-alias"
  1189. ;; '(progn ;; (eshell/alias "ll" "ls -l")
  1190. ;; ;; (eshell/alias "la" "ls -a")
  1191. ;; ;; (eshell/alias "lla" "ls -al")
  1192. ;; (eshell/alias "sgcc" (if (eq system-type 'windows-nt)
  1193. ;; "gcc -o win.$1.exe $1"
  1194. ;; "gcc -o ${uname}.$1.out $1"))
  1195. ;; (eshell/alias "slmgcc" (if (eq system-type 'windows-nt)
  1196. ;; "gcc -lm -o win.$1.exe $1"
  1197. ;; "gcc -lm -o ${uname}.$1.out $1"))
  1198. ;; ;; (eshell/alias "ut" "ssh g841105@un001.ecc.u-tokyo.ac.jp")
  1199. ;; (add-to-list 'recentf-exclude (concat eshell-directory-name "alias"))))
  1200. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1201. ;; get last modified date
  1202. (defvar my-buffer-file-last-modified-time nil "")
  1203. (make-variable-buffer-local 'my-buffer-file-last-modified-time)
  1204. (defun my-get-file-last-modified-time (file)
  1205. ""
  1206. (nth 5
  1207. (file-attributes file)))
  1208. (defun my-set-buffer-file-last-modified-time ()
  1209. ""
  1210. (make-local-variable 'my-buffer-file-last-modified-time)
  1211. (setq my-buffer-file-last-modified-time
  1212. (format-time-string "%Y/%m/%d %H:%M" (my-get-file-last-modified-time buffer-file-name))))
  1213. (add-hook 'find-file-hook
  1214. 'my-set-buffer-file-last-modified-time)
  1215. (add-hook 'after-save-hook
  1216. 'my-set-buffer-file-last-modified-time)
  1217. (add-hook 'after-revert-hook
  1218. 'my-set-buffer-file-last-modified-time)
  1219. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1220. ;; frame buffer
  1221. ;; todo: work well when opening the file that was already opened on another window
  1222. (add-hook 'after-make-frame-functions
  1223. (lambda (f)
  1224. (set-window-buffer (frame-selected-window f)
  1225. "*Messages*")))
  1226. (defun make-frame-command-with-name (name)
  1227. "Make frame with name specified."
  1228. (interactive "sName for new frame: ")
  1229. (set-frame-parameter (make-frame-command)
  1230. 'name
  1231. name))
  1232. (defvar my-frame-buffer-plist nil)
  1233. (defun my-frame-buffer-add (&optional buf frame)
  1234. ""
  1235. (setq my-frame-buffer-plist
  1236. (plist-put my-frame-buffer-plist
  1237. (or frame
  1238. (selected-frame))
  1239. (let ((lst (my-frame-buffer-get frame)))
  1240. (if lst
  1241. (add-to-list 'lst
  1242. (or buf
  1243. (current-buffer)))
  1244. (list (or buf
  1245. (current-buffer))))))))
  1246. (defun my-frame-buffer-remove (&optional buf frame)
  1247. ""
  1248. (setq my-frame-buffer-plist
  1249. (plist-put my-frame-buffer-plist
  1250. (or frame
  1251. (selected-frame))
  1252. (delq (or buf
  1253. (current-buffer))
  1254. (my-frame-buffer-get frame)))))
  1255. (defun my-frame-buffer-get (&optional frame)
  1256. ""
  1257. (plist-get my-frame-buffer-plist
  1258. (or frame
  1259. (selected-frame))))
  1260. (defun my-frame-buffer-kill-all-buffer (&optional frame)
  1261. ""
  1262. (mapcar 'kill-buffer
  1263. (my-frame-buffer-get frame)))
  1264. (add-hook 'find-file-hook
  1265. 'my-frame-buffer-add)
  1266. (add-hook 'term-mode-hook
  1267. 'my-frame-buffer-add)
  1268. (add-hook 'eshell-mode-hook
  1269. 'my-frame-buffer-add)
  1270. (add-hook 'Man-mode-hook
  1271. 'my-frame-buffer-add)
  1272. (add-hook 'kill-buffer-hook
  1273. 'my-frame-buffer-remove)
  1274. (add-hook 'delete-frame-functions
  1275. 'my-frame-buffer-kill-all-buffer)
  1276. (defvar my-desktop-terminal "roxterm")
  1277. (defun my-execute-terminal ()
  1278. ""
  1279. (interactive)
  1280. (if (and (or (eq system-type 'windows-nt)
  1281. window-system)
  1282. my-desktop-terminal
  1283. )
  1284. (let ((process-environment (cons "TERM=xterm" process-environment)))
  1285. (start-process "terminal"
  1286. nil
  1287. my-desktop-terminal))
  1288. (my-term)))
  1289. (defun my-term ()
  1290. "open terminal buffer and return that buffer."
  1291. (interactive)
  1292. (if (eq system-type 'windows-nt)
  1293. (eshell t)
  1294. (if (featurep 'multi-term)
  1295. (multi-term)
  1296. (ansi-term "/bin/bash"))))
  1297. (defun my-delete-frame-or-kill-emacs ()
  1298. "delete frame when opening multiple frame, kill emacs when only one."
  1299. (interactive)
  1300. (if (eq 1
  1301. (length (frame-list)))
  1302. (save-buffers-kill-emacs)
  1303. (delete-frame)))
  1304. (define-key my-prefix-map (kbd "C-s") 'my-execute-terminal)
  1305. (define-key my-prefix-map (kbd "C-f") 'make-frame-command-with-name)
  1306. (global-set-key (kbd "C-x C-c") 'my-delete-frame-or-kill-emacs)
  1307. (define-key my-prefix-map (kbd "C-x C-c") 'save-buffers-kill-emacs)
  1308. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1309. ;; auto saving
  1310. (defun my-save-this-buffer (silent-p)
  1311. "save current buffer if can without asking"
  1312. (let ((cm (if (current-message)
  1313. (format "%s\n" (current-message))
  1314. ""))
  1315. (fun (symbol-function (if silent-p
  1316. 'ignore
  1317. 'message))))
  1318. (cond ((active-minibuffer-window) nil)
  1319. ((not buffer-file-name) (funcall fun "%ssaving... this buffer doesn't visit any file." cm) nil)
  1320. ((not (file-exists-p buffer-file-name)) (funcall fun "%ssaving... file not exist. save manually first." com) nil)
  1321. (buffer-read-only (funcall fun "%ssaving... this buffer is read-only." cm) nil)
  1322. ((not (buffer-modified-p)) (funcal fun "%ssaving... not modified yet." cm) nil)
  1323. ((not (file-writable-p buffer-file-name)) (funcall fun "%ssaving... you cannot change this file." cm) nil)
  1324. (t (funcall fun "%ssaving..." cm)
  1325. (save-buffer)
  1326. (funcall fun "%ssaving... done." cm)))))
  1327. ;; (if (and buffer-file-name
  1328. ;; (not buffer-read-only)
  1329. ;; (buffer-modified-p)
  1330. ;; (file-writable-p buffer-file-name))
  1331. ;; (save-buffer))) ; silent one
  1332. (defvar my-auto-save-this-buffer nil "auto save timer object")
  1333. (defun my-auto-save-this-buffer (sec &optional silent-p)
  1334. "auto save current buffer if idle for SEC.
  1335. when SEC is nil, stop auto save if enabled."
  1336. (if sec
  1337. (progn (when my-auto-save-this-buffer
  1338. (cancel-timer my-auto-save-this-buffer)
  1339. (setq my-auto-save-this-buffer nil))
  1340. (setq my-auto-save-this-buffer (run-with-idle-timer sec t 'my-save-this-buffer silent-p)))
  1341. (when my-auto-save-this-buffer
  1342. (cancel-timer my-auto-save-this-buffer)
  1343. (setq my-auto-save-this-buffer nil))))
  1344. (my-auto-save-this-buffer 2 t)
  1345. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1346. ;; misc funcs
  1347. (defalias 'qcalc 'quick-calc)
  1348. (defvar git-command-history nil
  1349. "History list for git command.")
  1350. (defun my-git-branch-name (str)
  1351. ""
  1352. (with-temp-buffer
  1353. (shell-command "git branch --no-color" t)
  1354. (if (search-forward "*" nil t)
  1355. (progn (forward-char 1)
  1356. (format str
  1357. (buffer-substring-no-properties (point)
  1358. (point-at-eol))))
  1359. "")))
  1360. (defun my-git-ps1 (str)
  1361. (with-temp-buffer
  1362. (insert ". /etc/bash_completion.d/git; __git_ps1 "
  1363. (shell-quote-argument str)
  1364. ";")
  1365. (shell-command-on-region (point-min)
  1366. (point-max)
  1367. "bash -s"
  1368. nil
  1369. t)
  1370. (buffer-substring-no-properties (point-min)
  1371. (point-max))))
  1372. (defun my-git-shell-command (cmd)
  1373. ""
  1374. (interactive (list (read-shell-command (format "[%s]%s $ git : "
  1375. (abbreviate-file-name default-directory)
  1376. (my-git-ps1 "[GIT:%s]"))
  1377. nil
  1378. 'git-command-history)))
  1379. (let ((dir default-directory)
  1380. (bf (get-buffer-create "*Git Output*"))
  1381. )
  1382. (delete-windows-on bf t)
  1383. (shell-command (concat "git "
  1384. cmd)
  1385. bf)
  1386. (with-current-buffer bf
  1387. (cd dir)
  1388. (and (require 'ansi-color nil t)
  1389. (ansi-color-apply-on-region (point-min)
  1390. (point-max)))
  1391. (setq buffer-read-only t)
  1392. (view-mode 1)
  1393. )))
  1394. (define-key ctl-x-map "g" 'my-git-shell-command)
  1395. (defun my-kill-buffers ()
  1396. ""
  1397. (interactive)
  1398. (mapcar (lambda (buf)
  1399. (when (buffer-file-name buf)
  1400. (kill-buffer buf)))
  1401. (buffer-list)))
  1402. (defvar my-filer nil)
  1403. (setq my-filer (or (executable-find "pcmanfm")
  1404. (executable-find "nautilus")))
  1405. (defun my-x-open (file)
  1406. "open file."
  1407. (interactive "FOpen File: ")
  1408. (setq file (expand-file-name file))
  1409. (message "Opening %s..." file)
  1410. (cond ((eq system-type 'windows-nt)
  1411. (call-process "cmd.exe" nil 0 nil "/c" "start" "" (convert-standard-filename file)))
  1412. ((eq system-type 'darwin)
  1413. (call-process "open" nil 0 nil file))
  1414. ((getenv "DISPLAY")
  1415. (call-process (or my-filer "xdg-open") nil 0 nil file))
  1416. (t
  1417. (find-file file))
  1418. )
  1419. ;; (recentf-add-file file)
  1420. (message "Opening %s...done" file))
  1421. (defun my-keyboard-quit ()
  1422. ""
  1423. (interactive)
  1424. (run-hooks 'before-keyboard-quit-hook)
  1425. ;; (redisplay t)
  1426. (redraw-display)
  1427. ;; (run-hooks 'window-configuration-change-hook)
  1428. (my-revert-buffer-if-needed)
  1429. ;; (revert-buffer t t)
  1430. (keyboard-quit)
  1431. (insert "insert me")
  1432. (run-hooks 'after-keyboard-quit-hook))
  1433. (substitute-key-definition 'keyboard-quit 'my-keyboard-quit global-map)
  1434. ;; (global-set-key (kbd "C-g") 'my-keyboard-quit)
  1435. (defun my-convmv-sjis2utf8-test ()
  1436. "run `convmv -r -f sjis -t utf8 *'
  1437. this is test, does not rename files"
  1438. (interactive)
  1439. (shell-command "convmv -r -f sjis -t utf8 *"))
  1440. (defun my-convmv-sjis2utf8-notest ()
  1441. "run `convmv -r -f sjis -t utf8 * --notest'"
  1442. (interactive)
  1443. (shell-command "convmv -r -f sjis -t utf8 * --notest"))
  1444. (defun my-copy-whole-line ()
  1445. ""
  1446. (interactive)
  1447. (kill-new (concat (buffer-substring (point-at-bol)
  1448. (point-at-eol))
  1449. "\n")))
  1450. (defun kill-ring-save-buffer-file-name ()
  1451. "get current filename"
  1452. (interactive)
  1453. (let ((file buffer-file-name))
  1454. (if file
  1455. (progn (kill-new file)
  1456. (message file))
  1457. (message "not visiting file."))))
  1458. (defvar my-revert-buffer-if-needed-last-buffer nil)
  1459. (defun my-revert-buffer-if-needed ()
  1460. ""
  1461. (interactive)
  1462. (unless (eq my-revert-buffer-if-needed-last-buffer (current-buffer))
  1463. (setq my-revert-buffer-if-needed-last-buffer (current-buffer))
  1464. (when (or (and (eq major-mode 'dired-mode)
  1465. (dired-directory-changed-p default-directory))
  1466. (not (verify-visited-file-modtime (current-buffer))))
  1467. (revert-buffer t t)
  1468. (message "%s reverted." (buffer-name))
  1469. )))
  1470. (add-hook 'post-command-hook ; 'window-configuration-change-hook
  1471. 'my-revert-buffer-if-needed)
  1472. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1473. ;; forked from http://d.hatena.ne.jp/khiker/20100119/window_resize
  1474. (define-key my-prefix-map (kbd "C-w") 'my-window-organizer)
  1475. (defun my-window-organizer ()
  1476. "Control window size and position."
  1477. (interactive)
  1478. (save-selected-window
  1479. (select-window (window-at 0 0))
  1480. (let ( ;; (window-obj (selected-window))
  1481. ;; (current-width (window-width))
  1482. ;; (current-height (window-height))
  1483. action
  1484. c)
  1485. (catch 'end-flag
  1486. (while t
  1487. (setq action
  1488. (read-key-sequence-vector (format "size[%dx%d] 1: maximize; 2, 3: split; 0: delete; o: select other; j, l: enlarge; h, k: shrink; q: quit."
  1489. (window-width)
  1490. (window-height))))
  1491. (setq c (aref action 0))
  1492. (cond ((= c ?l)
  1493. (unless (eq (window-width) (frame-width))
  1494. (enlarge-window-horizontally 1)))
  1495. ((= c ?h)
  1496. (unless (eq (window-width) (frame-width))
  1497. (shrink-window-horizontally 1)))
  1498. ((= c ?j)
  1499. (enlarge-window 1))
  1500. ((= c ?k)
  1501. (shrink-window 1))
  1502. ((= c ?o)
  1503. (other-window 1))
  1504. ((memq c '(?d ?0))
  1505. (unless (eq (selected-window) (next-window (selected-window) 0 1))
  1506. (delete-window (selected-window))))
  1507. ((= c ?1)
  1508. (delete-other-windows))
  1509. ((= c ?2)
  1510. (split-window-vertically))
  1511. ((= c ?3)
  1512. (split-window-horizontally))
  1513. ((memq c '(?q ?\C-g))
  1514. (message "Quit")
  1515. (throw 'end-flag t))
  1516. (t
  1517. (beep))))))))
  1518. ;; (aref (read-key-sequence-vector "aa") 0)
  1519. ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1520. ;; ;; emacsを殺伐とさせる
  1521. ;; ;; 補完させるとき失敗するからなし
  1522. ;; ;; http://e-arrows.sakura.ne.jp/2010/05/emacs-should-be-more-savage.html
  1523. ;; (defadvice message (before message-for-stupid (arg &rest arg2) activate)
  1524. ;; (setq arg
  1525. ;; (concat arg
  1526. ;; (if (eq nil (string-match "\\. *$" arg)) ".")
  1527. ;; " Stupid!")))
  1528. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1529. ;; japanese input method
  1530. (defun my-load-scim ()
  1531. "use scim-bridge.el as japanese im."
  1532. ;; Load scim-bridge.
  1533. (when (require 'scim-bridge nil t)
  1534. ;; Turn on scim-mode automatically after loading .emacs
  1535. (add-hook 'after-init-hook 'scim-mode-on)
  1536. (setq scim-cursor-color "red")
  1537. (scim-define-preedit-key ?\^h t)
  1538. (scim-define-common-key ?\* nil)
  1539. (scim-define-common-key ?\^/ nil)))
  1540. (defun my-load-anthy ()
  1541. "use anthy.el as japanese im."
  1542. ;; anthy
  1543. (when (require 'anthy nil t)
  1544. (global-set-key (kbd "<muhenkan>") (lambda () (interactive) (anthy-mode-off)))
  1545. (global-set-key (kbd "<henkan>") (lambda () (interactive) (anthy-mode-on)))
  1546. (when (>= emacs-major-version 23)
  1547. (setq anthy-accept-timeout 1))))
  1548. ;; quail
  1549. ;; aproposs input-method for some information
  1550. ;; (setq default-input-method "japanese")
  1551. (defun my-load-mozc-el ()
  1552. ""
  1553. (setq mozc-leim-title "[MZ]")
  1554. (when (require 'mozc nil t)
  1555. (setq defauit-input-method "japanese-mozc")
  1556. ))
  1557. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1558. ;; for windows
  1559. ;; (add-to-list 'exec-path "c:/Program Files/Gauche/bin/")
  1560. (defun start-ckw-bash ()
  1561. ""
  1562. (interactive)
  1563. (start-process "ckw_bash"
  1564. nil
  1565. "C:/Documents and Settings/sr/Application Data/dbx/apps/ckw/ckw.exe")) ; cじゃないといけないらしい
  1566. (defun my-w32-add-export-path (&rest args)
  1567. ""
  1568. (mapcar (lambda (path)
  1569. (add-to-list 'exec-path (expand-file-name path)))
  1570. (reverse args))
  1571. (setenv "PATH"
  1572. (mapconcat 'convert-standard-filename
  1573. exec-path
  1574. ";")))
  1575. (when (eq system-type 'windows-nt)
  1576. ;; (setq scheme-program-name "\"c:/Program Files/Gauche/bin/gosh.exe\" -i")
  1577. ;; (setq python-python-command "c:/Python26/python.exe")
  1578. (define-key my-prefix-map (kbd "C-c") 'start-ckw-bash)
  1579. (my-w32-add-export-path "c:/WINDOWS"
  1580. (expand-file-name "~/bin")
  1581. (expand-file-name "~/dbx/apps/bin"))
  1582. (when window-system
  1583. (setq w32-enable-synthesized-fonts t))
  1584. (setq file-name-coding-system 'sjis))