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 68 KiB

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