Skip to content

Instantly share code, notes, and snippets.

@viraltux
Last active September 8, 2017 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save viraltux/9e33f47cf6e067c0e68f4478db9c31d3 to your computer and use it in GitHub Desktop.
Save viraltux/9e33f47cf6e067c0e68f4478db9c31d3 to your computer and use it in GitHub Desktop.

Emacs

More interesting settings in http://mescal.imag.fr/membres/arnaud.legrand/misc/init.php and https://github.com/dz/dotfiles/tree/master/emacsd/configs Common problems with Emacs can be solve by removing the .elc (compiled files) and recompiling them with byte-recompile-directory. For just one file we can use byte-compile-file.

Check for more seetings in: http://www.i3s.unice.fr/~malapert/org/tips/emacs_orgmode.html

.emacs main content

a soft linked copy of the .emacs file should be place in the home directory ~ with cp -as ~/Dropbox/.emacs ~

The .emacs file will trigger the load and conversion of the init.org file and should contain the following lines in the begginning. Further lines will be added automatically by other packages.

(package-initialize)

(setq user-emacs-directory (expand-file-name "~/Dropbox/.emacs.d/"))

(require 'org)
(org-babel-load-file (expand-file-name (concat user-emacs-directory "init.org")))

Repositories

Instructions at https://stable.melpa.org/#/getting-started

(require 'package) ;; You might already have this line
(let* ((no-ssl (and (memq system-type '(windows-nt ms-dos))
                    (not (gnutls-available-p))))
       (url (concat (if no-ssl "http""https") "://melpa.org/packages/")))
  (add-to-list 'package-archives (cons "melpa"url) t))
(when (< emacs-major-version 24)
  ;; For important compatibility libraries like cl-lib
  (add-to-list 'package-archives '("gnu". "http://elpa.gnu.org/packages/")))
(package-initialize) ;; You might already have this line

Visuals & Sound

;; Text size mimics the gnome-terminal shortcuts
(global-set-key (kbd "C-+") 'text-scale-increase)
(global-set-key (kbd "C--") 'text-scale-decrease)

;; Set sound warnings off
(setq visible-bell 1)

(setq inhibit-splash-screen t)

(beacon-mode 1)
(setq beacon-push-mark 35)
(setq beacon-color "#00005f")

;; graphical mode vs terminal mode settings
(unless (display-graphic-p)
        (menu-bar-mode -1)
)

(when (display-graphic-p) 
      (setq frame-title-format
            '("Emacs - " (buffer-file-name "%f"
	    (dired-directory dired-directory "%b"))))

      (set-background-color "#00005f") ;; darkblue
      (set-background-color "black")
      (set-foreground-color "white")

      ;; Set transparency of emacs when graphical mode
      ;; Use each particular terminal settings otherwise
      (set-frame-parameter (selected-frame) 'alpha '(85 . 50))
      (add-to-list 'default-frame-alist '(alpha . (85 . 50)))
      (defun transparency (value)
             "Sets the transparency of the frame window. 0=transparent/100=opaque"
	     (interactive "nTransparency Value 0 - 100 opaque:")
	     (set-frame-parameter (selected-frame) 'alpha value))
)

Text Input

spanish-prefix-quote

(quail-define-package
 "spanish-prefix-quote" "Spanish" "ES>" t
 "Spanish (EspaΓ±ol) input method with prefix modifiers. 
  
  This is a modification of the default spanish-prefix input method
  where the ~ has no role and is replaced by ' instead.
  
  Also, the guillemets or 'comillas latinas' have as a prefix
  the < and > symbols.

  This modification makes it easier to type in Spanish when using
  English based keyboards.

      effect    | prefix | examples
 ---------------+--------+----------
      tilde     |   \\='    | \\='a -> Γ‘
    exlamaciΓ³n  |   \\='    | \\='! -> Β‘
  interrogaciΓ³n |   \\='    | \\='? -> ΒΏ
     diΓ©resis   |   \"    | \"u -> ΓΌ
       eΓ±e      |   \\='    | \\='n -> Γ±
    guillemets  | < or > | >> -> Β»  << -> Β«   
" nil t nil nil nil nil nil nil nil nil t)

(quail-define-rules
 ("'A" ?Á)
 ("'E" ?Γ‰)
 ("'I" ?Í)
 ("'O" ?Γ“)
 ("'U" ?Ú)
 ("'a" ?Γ‘)
 ("'e" ?Γ©)
 ("'i" ?Γ­)
 ("'o" ?Γ³)
 ("'u" ?ΓΊ)
 ("' " ?')
 ("\"U" ?Ü)
 ("\"u" ?ΓΌ)
 ("\" " ?\")
 ("'N" ?Γ‘)
 ("'n" ?Γ±)
 (">>" ?\Β»)
 ("> " ?>)
 ("<<" ?\Β«)
 ("< " ?<)
 ("'!" ?Β‘)
 ("'?" ?ΒΏ)
 )

Other

;;  Marking Mispellings
;;  (global-font-lock-mode t)
;;  (custom-set-faces
;;    '(flyspell-incorrect ((t (:inverse-video t)))))
;;  ;;  (set-face-attribute 'flyspell-incorrect (t (:inverse-video t)))

Settings

;; Input Mode to allow Spanish ways in an english layout
;; WARNING: When pasting code like lisp (or anything else)
;;          containing things like 'a will be pasted as Γ‘.
(add-hook 'text-mode-hook
	  (lambda () (set-input-method "spanish-prefix-quote")))

Layouts

(defun book-layout()
   (interactive)
   (clone-indirect-buffer-other-window nil t)
   (enlarge-window-horizontally 50)
   (global-visual-line-mode t)
)

(global-set-key (kbd "C-c f b") 'book-layout)

;; Wrapping lines
;; (global-visual-line-mode t)

;; To force wrap text lines at a set width 
;; (add-hook 'text-mode-hook
;;           '(lambda() (turn-on-auto-fill) (set-fill-column 80)))

Other

;; Load initiailzation file 
(global-set-key (kbd "C-c i") 
(lambda() (interactive)(org-babel-load-file "~/.emacs.d/init.org")))


;; Store all backup *~ and autosave #*# files in the tmp dir
(setq backup-directory-alist
      `((".*" . ,temporary-file-directory)))
(setq auto-save-file-name-transforms
      `((".*" ,temporary-file-directory t)))

Org Mode

Main

;; (setq org-icalendar-timezone "Europe/Prague")
;; C-h v org-icalendar-use-UTC-date-time for details
;; Using UTC format is globally is advice since many calendar apps can
;; update its value easily. It works in Google & Yahoo web calendars.
;; warning: Google calendar updates daily and does not allow for manual refresh.


;; http://orgmode.org/manual/Installation.html

;; http://orgmode.org/guide/Activation.html#Activation
;; The following lines are always needed.  Choose your own keys.


(setq org-directory "~/Dropbox/workspace/org")
(when system-type '(windows-nt ms-dos) (setq org-directory "~/workspace/org"))


(add-hook 'org-mode-hook (global-visual-line-mode t))

(global-set-key "\C-cl" 'org-store-link)
(global-set-key "\C-ca" 'org-agenda)
(global-set-key "\C-cc" 'org-capture)
(global-set-key "\C-cb" 'org-iswitchb)

(setq org-icalendar-date-time-format ":%Y%m%dT%H%M%SZ")
(setq org-agenda-default-appointment-duration 30)
(setq org-icalendar-alarm-time 120)

;; Check http://orgmode.org/manual/Template-expansion.html#Template-expansion

;; Global captures
(setq org-capture-templates
      '(
	("t" "Todo" entry (file+headline "agenda.org" "Tasks")
	 "* TODO %?\n  %i\n  %a")
	("c" "Calendar" entry (file+headline "agenda.org" "Calendar")
	 "* %?\n  %T\n %i\n %A")
        ("r" "Random" entry (file "random.org")
	 "* %?\n  %U\n %i\n  %a")
))

;; Personal Captures
(unless system-type '(windows-nt ms-dos) 
(setq org-capture-templates 
  (append org-capture-templates '(
         ("j" "Journal" entry (file+datetree "journal.org.gpg") 
         "* %?\nEntered on %U\n  %i\n")
	 ("p" "Personal" entry (file "personal.org")
	 "* %?\n  %U\n %i\n  %a")
	  ("s" "Science" entry (file "science.org")
	 "* %?\n  %U\n %i\n  %a")
	  ("y" "Philosophy" entry (file "philosophy.org")
	 "* %?\n  %U\n %i\n")
	  ("n" "Novel" entry (file+headline "literature.org" "Novel")
	 "* %?\n  %U\n %i\n")
	  ("p" "Poem" entry (file+headline "literature.org" "Poetry")
	 "* %?\n  %U\n %i\n")
))))

;; Work Captures
(when system-type '(windows-nt ms-dos) 
(setq org-capture-templates 
  (append org-capture-templates '(
         ("p" "project" entry (file+datetree "project.org") 
	 "* %?\n  %U\n %i\n  %a")
	 ("n" "NPSS" entry (file "npss.org")
	 "* %?\n  %U\n %i\n  %a")
))))

;; Check more settings described in Blog
;; https://blog.aaronbieber.com/2016/01/30/dig-into-org-mode.html
(setq org-blank-before-new-entry (quote ((heading) (plain-list-item))))
(setq org-enforce-todo-dependencies t)
(setq org-log-done (quote time))

;; (defun fran-update-agenda ()
;;   (interactive)
;;   (shell-command "cd ~/Dropbox/workspace/org/sync; mv ../agenda.ics .; git commit -a -m 'update'; git push"))
;; (global-set-key (kbd "C-c f") 'fran-update-agenda)  

Programming Languages

;; for windows the Path env variable must have the folder with python.exe
;; for windows the org-babel-R-command must be set
;; http://orgmode.org/worg/org-contrib/babel/languages/ob-doc-R.html

;; Set just for windows
;;(setq org-babel-R-command "C:/Progra~1/R/R-3.4.1/bin/x64/R --slave --no-save")

(org-babel-do-load-languages
 'org-babel-load-languages
 '(
   (python . t)
   (C . t)
   (R . t)
   (org . t)
   ))
(setq org-src-preserve-indentation t)

R

To use this type <r and then TAB. This creates an R block for textual output.

(add-to-list 'org-structure-template-alist
        '("r" "#+begin_src R :results output :session *R* :exports both\n\n#+end_src" "<src lang=\"R\">\n\n</src>"))

To use this type <R and then TAB. This creates an R block for graphics that are stored in the tmp.

(add-to-list 'org-structure-template-alist
        '("R" "#+begin_src R :results output graphics :file (org-babel-temp-file \"figure\" \".png\") :exports both :width 800 :height 600 :session *R* \n\n#+end_src" "<src lang=\"R\">\n\n</src>"))

To use this type <RR and then TAB. This creates an R block for graphics that are stored in the directory of the current file.

(add-to-list 'org-structure-template-alist
        '("RR" "#+begin_src R :results output graphics :file  (org-babel-temp-file (concat (file-name-directory (or load-file-name buffer-file-name)) \"figure-\") \".png\") :exports both :width 800 :height 600 :session *R* \n\n#+end_src" "<src lang=\"R\">\n\n</src>"))

python

(add-to-list 'org-structure-template-alist
        '("p" "#+begin_src python :results output :exports both\n\n#+end_src" "<src lang=\"python\">\n\n</src>"))


(add-to-list 'org-structure-template-alist
        '("P" "#+begin_src python :results output :session *python* :exports both\n\n#+end_src" "<src lang=\"python\">\n\n</src>"))

Helm

(require 'helm)
(require 'helm-config)
(define-key helm-map (kbd "<tab>") 'helm-execute-persistent-action);; to use <tab> with its default behaviour
(global-set-key (kbd "M-i") 'helm-swoop) ;; C-x C-e to evaluate	  
(global-set-key (kbd "C-x b") 'helm-buffers-list)
(global-set-key (kbd "C-x r b") 'helm-bookmarks) ;; set bookmark C-x r m
(global-set-key (kbd "M-x") 'helm-M-x)
(global-set-key (kbd "M-y") 'helm-show-kill-ring) 
(global-set-key (kbd "C-x C-f") 'helm-find-files) 

(with-eval-after-load 'helm
  (define-key helm-map (kbd "C-c p") 'ignore)
  (define-key helm-map (kbd "<tab>") 'helm-execute-persistent-action) ; rebind tab to run persistent action
  (define-key helm-map (kbd "C-i") 'helm-execute-persistent-action) ; make TAB works in terminal
  (define-key helm-map (kbd "C-z")  'helm-select-action))

Org2Blog

;; (require 'auth-source) 
;;  (add-to-list 'auth-sources "~/.authinfo.gpg")
  (setq org2blog/wp-blog-alist
      `(("wordpress"
         :url "https://aleadeum.wordpress.com/xmlrpc.php"
	 :username "viraltux@gmail.com"
         ;; :username ,(car (auth-source-user-and-password "aleadeum"))
	 ;; :password ,(cadr (auth-source-user-and-password "aleadeum"))
         :default-title "Title"
         :default-categories ("Uncategorized")
         :tags-as-categories nil)))

Abbreviations

;; -*- coding: utf-8; lexical-binding: t; -*-
;; sample use of abbrev
(set-default 'abbrev-mode t)
(setq save-abbrevs nil)

global

(clear-abbrev-table global-abbrev-table)
(define-abbrev-table 'global-abbrev-table
  '(

    ;; mathematics
    ("+-" " ")
    ;; net abbrev
    ("afaik" "as far as i know" )
    ("atm" "at the moment" )
    ("ty" "thank you" )
    ("btw" "by the way" )
    
    ;; english word abbrev
    ("bc" "because" )
    ("bg" "background" )
    ("math" "mathematics" )

    ;; computing
    ("cs" "computer science" )

    ;; tech company
    ("macos" "Mac OS" )
    ("msw" "Microsoft Windows" )

    ;; programing
    ("jvm" "Java Virtual Machine" )
    ("gui" "graphical user interface" )
    ("os" "operating system" )

    ;; programing
    ("utf8" "-*- coding: utf-8 -*-" )

    ;; regex
    ("rgx-az09" "/^([A-Za-z0-9]+)$/" )
    ("rgx-ddmmyyy" "/^(0?[1-9]|[12][0-9]|3[01])([ \/\-])(0?[1-9]|1[012])\2([0-9][0-9][0-9][0-9])(([ -])([0-1]?[0-9]|2[0-3]):[0-5]?[0-9]:[0-5]?[0-9])?$/" )
    ("rgx-24h" "/^([01]?[0-9]|2[0-3]):[0-5][0-9]$/")
    ("rgx-isodate" "/^(?![+-]?\d{4,5}-?(?:\d{2}|W\d{2})T)(?:|(\d{4}|[+-]\d{5})-?(?:|(0\d|1[0-2])(?:|-?([0-2]\d|3[0-1]))|([0-2]\d{2}|3[0-5]\d|36[0-6])|W([0-4]\d|5[0-3])(?:|-?([1-7])))(?:(?!\d)|T(?=\d)))(?:|([01]\d|2[0-4])(?:|:?([0-5]\d)(?:|:?([0-5]\d)(?:|\.(\d{3})))(?:|[zZ]|([+-](?:[01]\d|2[0-4]))(?:|:?([0-5]\d)))))$/")
    ("rgx-email" "/^.+@.+$/")
    ("rgx-url" "/^((https?|ftp|file):\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/")
    ("rgx-int" "/^-?\d+$/")
    ("rgx-phone" "/^\+?(\d.*){3,}$/")
    

    ;; unicode
    ("hearts" "β™₯πŸ’•πŸ’“πŸ’”πŸ’–πŸ’—πŸ’˜πŸ’πŸ’žπŸ’ŸπŸ’™πŸ’šπŸ’›πŸ’œ" )

    ;; url
    ("urlemacs" "http://ergoemacs.org/" )

    ;;
    ))

fundamental-mode (ess-r-mode)

define abbrev for specific major mode the first part of the name should be the value of the variable major-mode of that mode e.g. for go-mode, name should be go-mode-abbrev-table To find out about a mode a good way its to

M-x add-mode-abbrev after selecting what we want to abbreviate and check with M-x list-abbrevs what mode was used to store the abbreviation.

(define-abbrev-table 'fundamental-mode-abbrev-table
  '(

("drawnorm"
"mean=0; sd=1
lb=-sd; ub=sd
x <- seq(-4,4,length=100)*sd + mean
hx <- dnorm(x,mean,sd)
plot(x, hx, type='n', xlab='Values', ylab='',
  main='Normal Distribution', axes=FALSE)
i <- x >= lb & x <= ub
lines(x, hx)
polygon(c(lb,x[i],ub), c(0,hx[i],0), col='red')
area <- pnorm(ub, mean, sd) - pnorm(lb, mean, sd)
result <- paste('P(',lb,'< Values <',ub,') =',
   signif(area, digits=3))
mtext(result,3)
axis(1, at=seq(40, 160, 20), pos=0)")

))

Company Mode

(require 'company)
(add-hook 'after-init-hook 'global-company-mode)

(define-key company-active-map (kbd "C-n") 'company-select-next-or-abort)
(define-key company-active-map (kbd "C-p") 'company-select-previous-or-abort)

(custom-set-faces
 '(company-preview
   ((t (:foreground "darkgray" :underline t))))
 '(company-preview-common
   ((t (:inherit company-preview))))
 '(company-tooltip
   ((t (:background "lightgray" :foreground "black"))))
 '(company-tooltip-selection
   ((t (:background "steelblue" :foreground "white"))))
 '(company-tooltip-common
   ((((type x)) (:inherit company-tooltip :weight bold))
    (t (:inherit company-tooltip))))
 '(company-tooltip-common-selection
   ((((type x)) (:inherit company-tooltip-selection :weight bold))
    (t (:inherit company-tooltip-selection)))))

Yasnippet

We need to add your own yasnippets for ESS/R for any yasnippet expansion to work. By default there are none. When typing rnorm(<TAB> the result we see has nothing to do with yasnippet, this is behavior that ESS provides to make your R-coding-life easier.

We need to create your own snippets for R. You need to do this if the folder ess-mode in the snippets directory or wherever your yasnippets are located (we’ll have to create the ess-mode directory).

See more in https://joaotavora.github.io/yasnippet/snippet-development.html

(add-hook 'ess-mode-hook 'r-autoyas-ess-activate)
(yas-global-mode 1)

Hacks

Windows 7

Emacs

Version GNU Emacs 25.2.1 (x86_64-w64-mingw32)

(when system-type '(windows-nt ms-dos)
(when emacs-version "25.2.1"

;; Warning (python): Your python-shell-interpreter doesn’t seem... Removing aparently
;; useless warning using workaround https://github.com/syl20bnr/spacemacs/issues/8797
(setq python-shell-completion-native-enable nil)

))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment