The compilation process is very mute in emacs and that's why I decided to make it faster and more convenient, and I only got the problem that this program (strings of code) is not self-updating, that is, compile-command (after first launching), stay the same and will not change without intervention, and I decided to add on defin, but my ignorance in lisp stopped me, the result was unsuccessful.
Question: How to make my function work correctly so that with every new ALT-M click compile-command is new.
What I tried to do:
(defun x-recompile (compile-command)
(setq compile-command '(concat "/usr/local/Cellar/gcc/8.3.0/bin/gcc-8 -O2 -Wall -o "
(if (file-name-sans-extension buffer-file-name)
(shell-quote-argument
(file-name-sans-extension buffer-file-name)))
" "
(if buffer-file-name
(shell-quote-argument (buffer-file-name))))))
(define-key global-map "\eM" 'compile)
(define-key global-map "\em" 'x-recompile)
Initial version:
(setq compile-command '(concat "/usr/local/Cellar/gcc/8.3.0/bin/gcc-8 -O2 -Wall -o "
(if (file-name-sans-extension buffer-file-name)
(shell-quote-argument
(file-name-sans-extension buffer-file-name)))
" "
(if buffer-file-name
(shell-quote-argument (buffer-file-name)))))
(define-key global-map "\eM" 'compile)
(define-key global-map "\em" 'recompile)
I think the easiest way to accomplish this is to use a mode hook:
(add-hook 'c-mode-hook
(lambda ()
(set (make-local-variable 'compile-command)
(concat "/usr/local/Cellar/gcc/8.3.0/bin/gcc-8 -O2 -Wall -o "
(if (file-name-sans-extension buffer-file-name)
(shell-quote-argument
(file-name-sans-extension buffer-file-name)))
" "
(if buffer-file-name
(shell-quote-argument (buffer-file-name)))))))
This will set compile-command when you visit the file and then compile and recompile will work appropriately.
Add the above code to .emacs
Evaluate the form
Re-visit the file (kill the buffer and open the file again)
C-h v compile-command RET to see the value
M-x compile RET or whatever you bind compile to
Related
My emacs takes a few seconds to start up every time, which is slower than I'd expect. During that time, it says "contacting host: " marmalade-repo and melpa.
Is my current config a reasonable one? Is there a way I can speed it up, while still being able to install packages when I need them?
;; init.el --- Emacs configuration
;; INSTALL PACKAGES
;; --------------------------------------
(require 'package)
(add-to-list 'package-archives '("melpa" . "http://melpa.org/packages/") t)
(add-to-list 'package-archives '("marmalade" . "https://marmalade-repo.org/packages/"))
(package-initialize)
(when (not package-archive-contents)
(package-refresh-contents))
(defvar myPackages
'(better-defaults
paredit
idle-highlight-mode
ido-ubiquitous
find-file-in-project
smex
scpaste
ein
elpy
flycheck
material-theme
py-autopep8))
(package-refresh-contents)
(mapc #'(lambda (package)
(unless (package-installed-p package)
(package-install package)))
myPackages)
;; BASIC CUSTOMIZATION
;; --------------------------------------
(setq inhibit-startup-message t) ;; hide the startup message
(load-theme 'material t) ;; load material theme
(global-linum-mode t) ;; enable line numbers globally
;; PYTHON CONFIGURATION
;; --------------------------------------
(elpy-enable)
(elpy-use-ipython)
;; use flycheck not flymake with elpy
(when (require 'flycheck nil t)
(setq elpy-modules (delq 'elpy-module-flymake elpy-modules))
(add-hook 'elpy-mode-hook 'flycheck-mode))
;; enable autopep8 formatting on save
(require 'py-autopep8)
(add-hook 'elpy-mode-hook 'py-autopep8-enable-on-save)
;; enable electric pair minor mode
(defun electric-pair ()
"If at end of line, insert character pair without surrounding spaces.
Otherwise, just insert the typed character."
(interactive)
(if (eolp) (let (parens-require-spaces) (insert-pair)) (self-insert-command 1)))
(add-hook 'python-mode-hook
(lambda ()
(define-key python-mode-map "\"" 'electric-pair)
(define-key python-mode-map "\'" 'electric-pair)
(define-key python-mode-map "(" 'electric-pair)
(define-key python-mode-map "[" 'electric-pair)
(define-key python-mode-map "{" 'electric-pair)))
;; Send line from code buffer
;; http://stackoverflow.com/questions/27777133/change-the-emacs-send-code-to-interpreter-c-c-c-r-command-in-ipython-mode/30774439#30774439
(defun my-python-line ()
(interactive)
(save-excursion
(setq the_script_buffer (format (buffer-name)))
(end-of-line)
(kill-region (point) (progn (back-to-indentation) (point)))
(if (get-buffer "*Python*")
(message "")
(run-python "ipython" nil nil))
;; (setq the_py_buffer (format "*Python[%s]*" (buffer-file-name)))
(setq the_py_buffer "*Python*")
(switch-to-buffer-other-window the_py_buffer)
(goto-char (buffer-end 1))
(yank)
(comint-send-input)
(switch-to-buffer-other-window the_script_buffer)
(yank))
(end-of-line)
(next-line)
)
(add-hook 'python-mode-hook
(lambda ()
(define-key python-mode-map "\C-cn" 'my-python-line)))
Recently, loading packages from MELPA has been really slow, I'm not so sure why. What you can do to get around this (or at least make sure it only happens once) is to run Emacs in daemon mode, and then connect to it whenever you want to edit a file. It means that the cost of loading is only incurred once. It's as simple as running emacs --daemon at startup. Then you connect via emacsclient.
How to compile a single file c++ program, print the output to compile buffer, run the c++ executable if compilation success and append the output of the c++ executable to compile output.
The below elisp code can compile the c++ program with the same name as buffer name. Now how to run and append the output of the exectutable to the compile buffer
(defun cpp-single-file-compile ()
(interactive)
(save-buffer)
(compile
(concat "g++ -g " (buffer-file-name) " -o " (file-name-sans-extension (buffer-file-name)))))
(add-hook 'c++-mode-hook (lambda () (local-set-key "\C-c\C-c" 'cpp-single-file-compile)))
The solution is very simple. Just execute after compilation
(defun cpp-single-file-compile ()
(interactive)
(save-buffer)
(compile
(concat "g++ -g " (buffer-file-name) " -o " (file-name-sans-extension (buffer-file-name))
" && " (file-name-sans-extension (buffer-file-name)) ".exe")))
(add-hook 'c++-mode-hook (lambda () (local-set-key "\C-c\C-c" 'cpp-single-file-compile)))
Additionally, to your code I have a hack-local-variables in the C-c C-c command:
(defun compileCompileCommand ()
"Compile current file."
(interactive)
(hack-local-variables)
(unless file-local-variables-alist
(setq compile-command (concat "g++ -g " (buffer-file-name) " -o " (file-name-sans-extension (buffer-file-name))
" && " (file-name-sans-extension (buffer-file-name)) ".exe")))
(compile compile-command))
(add-hook 'c-mode-common-hook '(lambda ()
(local-set-key (kbd "C-c C-c") 'compileCompileCommand)))
Then I add local variables to my source files. Example:
#include <iostream>
int main() {
std::cout << "Test";
return 0;
}
/*
Local Variables:
compile-command: "g++ -g test.cc && ./a.exe"
End:
*/
Therewith, you can decide on a per-file basis what you want to do. Changes of the compile-command are regarded at the next compilation because of the hack-local-variables thing.
The output is:
-*- mode: compilation; default-directory: "/c/temp/" -*-
Compilation started at Fri Nov 15 15:22:34
g++ -g test.cc && ./a.exe
Test
Compilation finished at Fri Nov 15 15:22:34
EDIT: Sometimes it is necessary to link some additional library or specify some additional include path. Therefore, I could not accept the answer of Talespin_Kit as a complete solution. I think a combination as it is given in the above edited version is better. If local variables are given then the original compile-command is used else an automatically generated compile-command is used. Note, one could also use assq to check whether compile-command is a member of the local variables.
When I try to export a org-mode file to odf I end up with a corrupt output file. I narrowed the problem down to that the odt-file (which in fact is a zip file) contained a file named content_flymake.xml. If I remove that file, I can open the it without a problem.
Now I'm stucked. I don't know what to do next.
A grep of flymake in my config files:
nine#nine-laptop:~/.emacs.d/configfile$ grep -A 5 -B 5 -R "flymake" *
blocks/python.el-
blocks/python.el-(add-to-list 'load-path "~/.emacs.d/vendor")
blocks/python.el-
blocks/python.el-; Make Flymake work with python
blocks/python.el-(add-to-list 'load-path "~/.emacs.d/plugins")
blocks/python.el:(add-hook 'find-file-hook 'flymake-find-file-hook)
blocks/python.el-
blocks/python.el:(when (load "flymake" t)
blocks/python.el: (defun flymake-pyflakes-init ()
blocks/python.el: (let* ((temp-file (flymake-init-create-temp-buffer-copy
blocks/python.el: 'flymake-create-temp-inplace))
blocks/python.el- (local-file (file-relative-name
blocks/python.el- temp-file
blocks/python.el- (file-name-directory buffer-file-name))))
blocks/python.el- (list "pycheckers" (list local-file))))
blocks/python.el: (add-to-list 'flymake-allowed-file-name-masks
blocks/python.el: '("\\.py\\'" flymake-pyflakes-init)))
blocks/python.el:(load-library "flymake-cursor")
blocks/python.el:(global-set-key [f10] 'flymake-goto-prev-error)
blocks/python.el:(global-set-key [f11] 'flymake-goto-next-error)
blocks/python.el-
blocks/python.el-(require 'ipython)
blocks/python.el-
blocks/python.el-;(define-key py-mode-map (kbd "M-TAB") 'anything-ipython-complete)
blocks/python.el-;(define-key py-shell-map (kbd "M-TAB") 'anything-ipython-complete)
--
emacs- ;; If you edit it by hand, you could mess it up, so be careful.
emacs- ;; Your init file should contain only one such instance.
emacs- ;; If there is more than one, they won't work right.
emacs- )
emacs-
emacs:; Workaround for broken flymake configuration (Might be fixed in future versions)
emacs:(defun flymake-xml-init ()
emacs: (list "xmlstarlet" (list "val" "-e" (flymake-init-create-temp-buffer-copy 'flymake-create-temp-inplace))))
emacs-
emacs-;;;;;;;;;;;;;;;;;;
emacs-; Mutt mail-mode ;
emacs-;;;;;;;;;;;;;;;;;;
emacs-(add-to-list 'auto-mode-alist '(".*mutt.*" . message-mode))
And my org-mode configuration
nine#nine-laptop:~/.emacs.d/configfile$ cat blocks/orgmode.el
;; Org mode
(add-to-list 'load-path "~/.emacs.d/plugins/org-mode/lisp/")
;; odt-support
(require 'ox-odt)
;(require 'org-mode)
(add-to-list 'auto-mode-alist '("\\.org\\'" . org-mode))
(add-hook 'org-mode-hook 'turn-on-font-lock) ; not needed when global-font-lock-mode is on
(setq org-agenda-files (list "~/Dokument/org/arbete.org"
"~/Dokument/org/calendar.org"))
(setq org-startup-indented t)
;; Default ODF style
;(setq org-export-odt-styles-file "~/.emacs.d/org-mode-odtconv/predikan-style.xml")
;(setq org-default-notes-file (concat org-directory "/anteckningar.org"))
(global-set-key "\C-cl" 'org-store-link)
(global-set-key "\C-cc" 'org-capture)
(global-set-key "\C-ca" 'org-agenda)
(global-set-key "\C-cb" 'org-iswitchb)
;; Automatic Org mode pull and push
;(add-hook 'after-init-hook 'org-mobile-pull)
;(add-hook 'kill-emacs-hook 'org-mobile-push)
One possibility is to disable the flymake-find-file-hook while running org-export-as-odt. The following lines may work:
(defadvice org-export-as-odt (around remove-flymake-hook first act)
(let ((find-file-hook find-file-hook))
(remove-hook 'find-file-hook 'flymake-find-file-hook)
ad-do-it))
You can also try to customize flymake-allowed-file-name-masks and remove the .xml binding there. But this means that no xml file would run under flymake by default anymore.
When writing simple, one file, C++ code, I usually call g++ directly. By default, Flymake seems to assume the presence of a Makefile with a check-syntax target. How do I configure Flymake to simply call g++ directly, e.g:
g++ -c a.cpp
If the answer could be modified to include compiler flags, that would be even better
Many thanks
You can call g++ directly with following flymake configuration.
(require 'flymake)
(defun flymake-cc-init ()
(let* ((temp-file (flymake-init-create-temp-buffer-copy
'flymake-create-temp-inplace))
(local-file (file-relative-name
temp-file
(file-name-directory buffer-file-name))))
(list "g++" (list "-Wall" "-Wextra" "-fsyntax-only" local-file))))
(push '("\\.cpp$" flymake-cc-init) flymake-allowed-file-name-masks)
(add-hook 'c++-mode-hook 'flymake-mode)
I got following screenshot when I call flymake-allowed-file-name-masks
Following is comment version.
;; Import flymake
(require 'flymake)
;; Define function
(defun flymake-cc-init ()
(let* (;; Create temp file which is copy of current file
(temp-file (flymake-init-create-temp-buffer-copy
'flymake-create-temp-inplace))
;; Get relative path of temp file from current directory
(local-file (file-relative-name
temp-file
(file-name-directory buffer-file-name))))
;; Construct compile command which is defined list.
;; First element is program name, "g++" in this case.
;; Second element is list of options.
;; So this means "g++ -Wall -Wextra -fsyntax-only tempfile-path"
(list "g++" (list "-Wall" "-Wextra" "-fsyntax-only" local-file))))
;; Enable above flymake setting for C++ files(suffix is '.cpp')
(push '("\\.cpp$" flymake-cc-init) flymake-allowed-file-name-masks)
;; Enable flymake-mode for C++ files.
(add-hook 'c++-mode-hook 'flymake-mode)
I am looking for equivalent of following vi command
:! nl %
this runs nl command on currently open file
What is emacs way to detect name of open file ?
M-X shell-commnad nl
I am not able find determine value of current open/buffer and substitute.
Thx/Mahesh
EDIT: Misread your question as wanting to apply that change to the file you're working on. If you just want to run a shell command against a buffer, you can use shell-command-on-region, which is usually bound to M-|.
If you're just trying to get to a particular line number, M-x goto-line works. I bind that to C-x C-l by putting (define-key global-map "\C-x\C-l" 'goto-line) in my ~/.emacs.
Try this (in your ~/.emacs file):
;;; Run a shell command on all text between the mark and the point and
;;; replace with the output.
(defun shell-command-in-region (start end command &optional flag interactive)
"Execute shell-command-on-region and replace the region with the output
of the shell command."
(interactive (list (region-beginning) (region-end)
(read-from-minibuffer "Shell command in region: "
nil nil nil 'shell-command-history)
current-prefix-arg
(prefix-numeric-value current-prefix-arg)))
(shell-command-on-region (point) (mark) command t)
)
(define-key esc-map "#" 'shell-command-in-region)
Invoke it by selecting a region you want to operate on and then doing M-#.
If you always want the buffer's file name to be inserted for the shell command, you can use this advice:
(defadvice read-shell-command (before read-shell-command-with-filename activate)
"force the initial contents to contain the buffer's filename"
(if (and (null (ad-get-arg 1))
buffer-file-name)
(ad-set-arg 1 buffer-file-name)))
Once you've added the above code, M-x shell-command will always start with the buffer's file name, so you can use it in the command.
I use this:
(defun my-shell-command-on-current-file (command &optional output-buffer error-buffer)
"Run a shell command on the current file (or marked dired files).
In the shell command, the file(s) will be substituted wherever a '%' is."
(interactive (list (read-from-minibuffer "Shell command: "
nil nil nil 'shell-command-history)
current-prefix-arg
shell-command-default-error-buffer))
(cond ((buffer-file-name)
(setq command (replace-regexp-in-string "%" (buffer-file-name) command nil t)))
((and (equal major-mode 'dired-mode) (save-excursion (dired-move-to-filename)))
(setq command (replace-regexp-in-string "%" (mapconcat 'identity (dired-get-marked-files) " ") command nil t))))
(shell-command command output-buffer error-buffer))
(global-set-key (kbd "M-!") 'my-shell-command-on-current-file)
Then you can do M-! nl %