I'd like emacs to indent my C sources using pico editing style.
How can this be done?
(add-hook 'c-mode-hook (lambda () (setq-local c-basic-offset 2))
That will give you code like this:
int foo()
{ int x = 5;
int y = bar(x);
return x + y; }
Related
so, I added this to my theme:
(font-lock-add-keywords 'c-mode
'(("\\([0-9]+\\)" . font-lock-warning-face)))
so far so good... it changes the color of what the variable contain. ex:
int a = 5
where the '5' is colored with a predefined color. the problem goes when I have a variable whose name correnspond to "variable_1" it colors the '1' in "variable_1"... is there a way to fix this?
This solution was tested on the following three (3) examples:
int a = 5
funtion(1, 2)
variable_1
(defun my-c-mode-keywords ()
(font-lock-add-keywords nil (list
(list "[^_]\\([0-9]+\\)" '(1 'font-lock-warning-face))) 'prepend))
(add-hook 'c-mode-hook 'my-c-mode-keywords)
I'm working on an assignment that used the unicode subscript k ("\_k"). However, instead of getting the subscripted k, I get this:
![][1]
The can still use subscripts with numbers and some letters, but not with 'k'.
I initially had the issue with emacs 24.3, and then was told to update to 24.4. The issue still persisted.
Has anyone else had this issue and found a solution?
I'm running on a Mac with Emacs v.24.4.1
Thanks in advance
EDIT
Here is my .emacs file:
(load-file (let ((coding-system-for-read 'utf-8))
(shell-command-to-string "agda-mode locate")))
(set-default-font "-apple-DejaVu_Sans-medium-normal-normal-*-*-*-*-*-p-0-iso10646-1")
; Change Control-c Control-, and Control-c Control-. in Agda mode
; so they show the normalized rather than the "simplified" goals
(defun agda2-normalized-goal-and-context ()
(interactive)
(agda2-goal-and-context '(3)))
(defun agda2-normalized-goal-and-context-and-inferred ()
(interactive)
(agda2-goal-and-context-and-inferred '(3)))
(eval-after-load "agda2-mode"
'(progn
(define-key agda2-mode-map (kbd "C-c C-,")
'agda2-normalized-goal-and-context)
(define-key agda2-mode-map (kbd "C-c C-.")
'agda2-normalized-goal-and-context-and-inferred)))
; This defines backslash commands for some extra symbols.
(eval-after-load "quail/latin-ltx"
'(mapc (lambda (pair)
(quail-defrule (car pair) (cadr pair) "TeX"))
'( ("\\bb" "๐น") ("\\bl" "๐") ("\\bs" "๐")
("\\bt" "๐") ("\\bv" "๐") ("\\cv" "โ")
("\\comp" "โ") ("\\m" "โฆ") ("\\om" "ฯ"))))
; This sets the Control-c Control-k shortcut to
; describe the character under your cursor.
(global-set-key "\C-c\C-k" 'describe-char)
(custom-set-variables
'(agda2-include-dirs
(quote ("." "/Users/dylanthiemann/Dropbox/University of Iowa/2nd Senior Year/Spring 2015/PLC/ial")))
)
And the bit of code is from Agda:
module bool-kleene-thms where
open import bool
open import bool-kleene
open import eq
&&โ-idem : โ (b : ๐นโ) โ b &&โ b โก b
&&โ-idem b = {!!}
tt-&&โ : โ (b : ๐นโ) โ tt &&โ b โก b
tt-&&โ b = {!!}
||โ-idem : โ (b : ๐นโ) โ b ||โ b โก b
||โ-idem b = {!!}
||โ-tt : โ (b : ๐นโ) โ b ||โ tt โก tt
||โ-tt b = {!!}
||โ-ff : โ (b : ๐นโ) โ b ||โ ff โก b
||โ-ff b = {!!}
Edit 2
Results of Ctrl-u Ctrl-x = on affected character:
position: 91 of 830 (11%), column: 2
character: โ (displayed as โ) (codepoint 8342, #o20226, #x2096)
preferred charset: unicode (Unicode (ISO10646))
code point in charset: 0x2096
script: symbol
syntax: w which means: word
category: .:Base, L:Left-to-right (strong)
to input: type "\_k" with Agda input method
buffer code: #xE2 #x82 #x96
file code: #xE2 #x82 #x96 (encoded by coding system utf-8-unix)
display: terminal code #xE2 #x82 #x96
I needed to install new fonts... I used this http://dejavu-fonts.org/wiki/Download and then installed using Font Books. The issue was then resolved!
I setup 2 spaces for php-mode (using c-basic-offset) but switch statement remain 4 spaces,
function foo($items) {
foreach($item in $items) {
switch($item) {
case 1:
return 10;
case 2:
return 20;
}
}
}
how to change swich statement to be 2 spaces if c-basic-offset is 2?
You can customize the case-label offset:
(add-hook 'php-mode-hook
(lambda () (c-set-offset 'case-label 0)))
For more information, look at the definition of coding styles in php-mode.el (looking for c-add-style calls).
With help from #tungd I have code that work:
(c-set-offset 'case-label 2)
(c-set-offset 'statement-case-intro 2)
I have to do a very specific task to re-do over and over again and would like to have it put permanently in my .emacs file. But I am not versed enough in emacs-lisp to manage it:
Keystroke-1, say [F8]
remember the current cursors' column position in, say, xtab
Keystroke-2, say [F9] while cursor is in some other line:
find leftmost string // in current line, if none is there, beep and stop
insert as many spaces so the // gets to previously remembered column xtab, or do nothing if cursor is already beyond xtab
search-forward for next // and place the cursor on it
I managed to assign it to a temporary keyboard macro, but have to re-record it for every changing xtab value.
The ultimate goal is that I want to align the comments in different pieces of code easily, from
int main() { // the enty function
int x = 100; // my new variable
for(int i=1; i<2012; ++i) { // loop a lot
x -= i;
}
} // end of all things
to
int main() { // the entry function
int x = 100; // my new variable
for(int i=1; i<2012; ++i) { // loop a lot
x -= i;
}
} // end of all things
Any idea how I can automate this? What do I have to put in my .emacs-file to archive this -- or similar?
As tungd said, align-regexp is good for this sort of thing.
(defun my-align-comments (beginning end)
"Align instances of // within marked region."
(interactive "*r")
(let (indent-tabs-mode align-to-tab-stop)
(align-regexp beginning end "\\(\\s-*\\)//")))
Which is like the interactive call:
M-x align-regexp RET // RET
Or for a more language-agnostic version:
(defun my-align-comments (beginning end)
"Align comments within marked region."
(interactive "*r")
(let (indent-tabs-mode align-to-tab-stop)
(align-regexp beginning end (concat "\\(\\s-*\\)"
(regexp-quote comment-start)))))
Not exactly an answer to your question, but to achieve the desired goal you can just mark the region and use align-regexp.
Here's the code:
(defvar c-current-comment-col 30)
(defun c-set-comment-col ()
(interactive)
(setq c-current-comment-col (current-column)))
(defun c-comment-to-col ()
(interactive)
(beginning-of-line)
(when (re-search-forward "//" (line-end-position) t)
(backward-char 2)
(let ((delta (- c-current-comment-col
(current-column))))
(if (plusp delta)
(insert (make-string delta ? ))
(if (looking-back
(format "\\( \\{%d\\}\\)" (- delta)))
(delete-region
(match-beginning 1)
(match-end 1))
(message
"I'm sorry Dave, I afraid can't do that.")))))
(next-line 1))
(global-set-key [C-f6] 'c-set-comment-col)
(global-set-key [f6] 'c-comment-to-col)
I've added a next-line call to the end. Now you can do
C-f6 f3 f6 M-0 f4 to align until end of buffer.
M-x align is very powerful and will automatically handle the particular example given.
However, it will also align variable declarations, which may be more than you want. In that case, you would have to customize align-region-separate or use the align-regexp answer.
A lot of Emacs functions automatically split the screen. However, they all do so such that the windows are one on top of the other. Is there any way to make them split such that they are side-by-side by default instead?
(setq split-height-threshold nil)
(setq split-width-threshold 0)
GNU Emacs Lisp Reference Manual: Choosing Window Options
Two solutions here, use any one you like:
A: Vertically(left/right) by default:
(setq split-height-threshold nil)
(setq split-width-threshold 0)
B: Automatically split window vertically(left/right) if current window is wide enough
(defun display-new-buffer (buffer force-other-window)
"If BUFFER is visible, select it.
If it's not visible and there's only one window, split the
current window and select BUFFER in the new window. If the
current window (before the split) is more than 100 columns wide,
split horizontally(left/right), else split vertically(up/down).
If the current buffer contains more than one window, select
BUFFER in the least recently used window.
This function returns the window which holds BUFFER.
FORCE-OTHER-WINDOW is ignored."
(or (get-buffer-window buffer)
(if (one-window-p)
(let ((new-win
(if (> (window-width) 100)
(split-window-horizontally)
(split-window-vertically))))
(set-window-buffer new-win buffer)
new-win)
(let ((new-win (get-lru-window)))
(set-window-buffer new-win buffer)
new-win))))
;; use display-buffer-alist instead of display-buffer-function if the following line won't work
(setq display-buffer-function 'display-new-buffer)
Put any one in you .emacs/init.el file.
You can change the "100" to the value you like, depending on you screen.
If you got two windows in one frame, and you want to change the layout from vertical to horizontal or vice verse, here is a solution:
(defun toggle-window-split ()
(interactive)
(if (= (count-windows) 2)
(let* ((this-win-buffer (window-buffer))
(next-win-buffer (window-buffer (next-window)))
(this-win-edges (window-edges (selected-window)))
(next-win-edges (window-edges (next-window)))
(this-win-2nd
(not (and (<= (car this-win-edges)
(car next-win-edges))
(<= (cadr this-win-edges)
(cadr next-win-edges)))))
(splitter
(if (= (car this-win-edges)
(car (window-edges (next-window))))
'split-window-horizontally
'split-window-vertically)))
(delete-other-windows)
(let ((first-win (selected-window)))
(funcall splitter)
(if this-win-2nd (other-window 1))
(set-window-buffer (selected-window) this-win-buffer)
(set-window-buffer (next-window) next-win-buffer)
(select-window first-win)
(if this-win-2nd (other-window 1))))))
;; C-x 4 t 'toggle-window-split
(define-key ctl-x-4-map "t" 'toggle-window-split)
Put it in your .emacs/init.el file, Use C-x 4 t to toggle the layout of your windows.
(setq split-height-threshold 0) (setq split-width-threshold 0)
is what i had to use to get the desired behaviour (no horizontal splitting)
Sometimes we need change between Horizontal and Vertical according current display and our requirement (more lines or more columns).
I recommand the great ToggleWindowSplit, And I bind key to "C-c y"
http://www.emacswiki.org/emacs/ToggleWindowSplit
the simple answer of setting 2 variables to nil and 0 didn't work for me, so I wrote 2 simple functions: one just splits the window into NX vertical buffers and opens files named (for example) file.1 file.2 ... file.NX in each and another one does the same think, except does it in 2D (NY rows by NX columns for opening files f.1 f.2 ... f.[NX*NY]). To install, add this code to .emacs:
(defun grid-files-h (nx wx pfx)
"Using dotimes, split the window into NX side-by-side buffers of width WX and load files starting with prefix PFX and ending in numbers 1 through NX"
(let (ox fn k) ; ox is not used, but fn is used to store the filename, and k to store the index string
(dotimes (x (- nx 1) ox) ; go through buffers, x goes from 0 to nx-2 and ox is not used here
; (print x)
(setq k (number-to-string (+ x 1) ) ) ; k is a string that goes from "1" to "nx-1"
; (print k)
(setq fn (concat pfx k) ) ; fn is filename - concatenate prefix with k
; (print fn)
(find-file fn) ; open the filename in current buffer
(split-window-horizontally wx) ; split window (current buffer gets wx-columns)
(other-window 1) ; switch to the next (right) buffer
)
(setq k (number-to-string nx )) ; last (rightmost) buffer gets the "nx" file
(setq fn (concat pfx k) ) ; fn = "pfx"+"nx"
(find-file fn ) ; open fn
(other-window 1) ; go back to the first buffer
)
)
(defun grid-files-sq (ny wy nx wx pfx)
"Using dotimes, split the window into NX columns of width WX and NY rows of height WY and load files starting with prefix PFX and ending in numbers 1 through NX*NY"
(let (oy ox fn k)
(dotimes (y ny oy) ; go through rows, y goes from 0 to ny-1 and oy is not used here
(split-window-vertically wy) ; create this row
(dotimes (x (- nx 1) ox) ; go through columns, x goes from 0 to nx-2 and ox is not used here
(setq k (number-to-string (+ 1 (+ x (* y nx) ) ) ) ) ; k must convert 2 indecies (x,y) into one linear one (like sub2ind in matlab)
(setq fn (concat pfx k) ) ; filename
(find-file fn ) ; open
(split-window-horizontally wx) ; create this column in this row (this "cell")
(other-window 1) ; go to the next buffer on the right
)
(setq k (number-to-string (+ nx (* y nx) ) ) ) ; rightmost buffer in this row needs a file too
(setq fn (concat pfx k) ) ; filename
(find-file fn ) ; open
(other-window 1) ; go to next row (one buffer down)
)
)
)
and then to use the vertical one, I go to *scratch* (C-x b *scratch* RET,C-x 1), type in (grid-files-h 3 20 "file.") then C-x C-e, or if you want to test out the square qrid one, C-x 1, type in (grid-files-sq 2 15 3 20 "f.") and then C-x C-e and you should see something like
This probably can be done better/more efficiently, but it's a start and it does what I need it to do (display a bunch of sequentially named small files). Feel free to improve or reuse.
I use multiple frames (OSX windows) in emacs regularly for different projects. Here's how I setup a few frames initially split to a left and right window.
(defun make-maximized-split-frame (name)
(let (( f (make-frame (list (cons 'name name))) ))
(maximize-frame f)
(split-window (frame-root-window f) nil t)
))
(make-maximized-split-frame "DocRaptor")
(make-maximized-split-frame "Gauges")
(make-maximized-split-frame "Instrumental")
The direct answer is to press C-c 3.
It's not clear from the question if you want a permanent setting change, but I found this question looking for this answer and didn't find it. (The answer has actually been sitting in a comment for the last 11 years)