How to correctly use setq in elisp? - variables

In my emacs config I try to configure the project root for jedi using the following code:
(setq jedi:server-args '("--sys-path" (projectile-project-root)))
Which throws a:
deferred error : (wrong-type-argument stringp (projectile-project-root))
(I have (setq debug-on-error t) but the backtrace shows nothing)
If I hardcode the path like this all works as expected:
(setq jedi:server-args '("--sys-path" "/some/path"))
To give this line some context, here's the surrounding code:
(add-hook 'python-mode-hook 'jedi:setup)
(setq debug-on-error t)
(defun jedi-config:setup-server-args ()
(message (format "Configuring current project dir: %s"
(projectile-project-root)))
(setq jedi:server-args '("--sys-path" (projectile-project-root))))
(add-hook 'python-mode-hook
'jedi-config:setup-server-args)
How can I set the server-args for jedi using the projectile variable?

I finally figured it out - rather simple really:
(setq jedi:server-args (list "--sys-path" (projectile-project-root)))

Related

emacs sql mode could not save history

below is my settings:
(defun my-sql-save-history-hook ()
(let ((lval 'sql-input-ring-file-name)
(rval 'sql-product))
(if (symbol-value rval)
(let ((filename
(concat "~/.emacs.d/sql/"
(symbol-name (symbol-value rval))
"-history.sql")))
(set (make-local-variable lval) filename)
(set-default 'sql-input-ring-file-name filename))
(error
(format "SQL history will not be saved because %s is nil"
(symbol-name rval))))))
and i describe the variable values:
sql-input-ring-file-name's value is
"~/.emacs.d/sql/mysql-history.sql"
Original value was nil
Local in buffer *SQL*; global value is the same.
and i have directory ~/.emacs.d/sql but do not have any file
my emacs version is 24.4.1
I had trouble getting the answers above to work, but this works for me. The main change is lval and kill-buffer-hook
(defun my-sql-save-history-hook ()
(let ((lval 'comint-input-ring-file-name)
(rval 'sql-product))
(if (symbol-value rval)
(let ((filename
(concat "~/.emacs.d/sql/"
(symbol-name (symbol-value rval))
"-history.sql")))
(set (make-local-variable lval) filename))
(error
(format "SQL history will not be saved because %s is nil"
(symbol-name rval))))))
(add-hook 'sql-interactive-mode-hook 'my-sql-save-history-hook)
(add-hook 'kill-buffer-hook 'comint-write-input-ring)

Spacemacs set tab width

I just migrated from VIM to Spacemacs and would like to change the tab width from default (\t?) to only 2 spaces. I found commands like
(setq-default indent-tabs-mode nil)
and
(setq tab-width 4) ; or any other preferred value
(defvaralias 'c-basic-offset 'tab-width)
(defvaralias 'cperl-indent-level 'tab-width)
My problem is that I don't know if they are correct, where in the .spacemacs file I should insert them, and what they even mean.
I found this article:
http://blog.binchen.org/posts/easy-indentation-setup-in-emacs-for-web-development.html
I added this part of the code into my .spacemacs file outside of any function (but before (defun dotspacemacs/user-init () ... )):
(defun my-setup-indent (n)
;; java/c/c++
(setq c-basic-offset n)
;; web development
(setq coffee-tab-width n) ; coffeescript
(setq javascript-indent-level n) ; javascript-mode
(setq js-indent-level n) ; js-mode
(setq js2-basic-offset n) ; js2-mode, in latest js2-mode, it's alias of js-indent-level
(setq web-mode-markup-indent-offset n) ; web-mode, html tag in html file
(setq web-mode-css-indent-offset n) ; web-mode, css in html file
(setq web-mode-code-indent-offset n) ; web-mode, js code in html file
(setq css-indent-offset n) ; css-mode
)
and added the line
(my-setup-indent 2) ; indent 2 spaces width
into the (defun dotspacemacs/user-init () ... ) like this:
(defun dotspacemacs/user-init ()
"Initialization function for user code.
It is called immediately after `dotspacemacs/init', before layer configuration
executes.
This function is mostly useful for variables that need to be set
before packages are loaded. If you are unsure, you should try in setting them in
`dotspacemacs/user-config' first."
(my-setup-indent 2) ; indent 2 spaces width
)
You can also just customize the the standard-indent variable, setting it to 2, by calling the command customize-variable within spacemacs. This will save the customization into your .spacemacs file.
Edit:
to run 'customize-variable' use the hotkey M-x (alt-x on most systems) then type customize-variable in to the prompt.
you can use the search to search for 'standard-indent'

Common Lisp unbound variable / defmacro

I need to make the function defmacro for my meta-circular interpreter that can read this syntax:
pseudoscheme> (defmacro (minus x y) (list ‘- y x))
MINUS
pseudoscheme> (expand-macro '(minus 3 2))
(- 3 2)
When I use this:
(defmacro my-defmacro ((name &rest args) &body body)
(let ((form (gensym))(env (gensym)))
(progn
`(setf (macro-function ',name)
(lambda (,form ,env))
(destructuring-bind ,args (rest, form) ,#body))
name
)
)
)
and then:
(my-defmacro (min a b)(list '- a b))
I get this error:
Error: The variable MIN is unbound.
I can't understand why.
-----EDITED-----
If I use this:
(defmacro my-defmacro ((name &rest args) &body body)
(let ((form (gensym))(env (gensym)))
`(progn (setf (macro-function ',name)
(lambda (,form ,env))
(destructuring-bind ,args (rest, form) ,#body))
',name)
)
)
and then:
(my-defmacro (min a b)(list '- a b))
I get this error:
Error: Odd number of args to SETF: ((MACRO-FUNCTION (QUOTE PLUS)) (LAMBDA (#:G786 #:G787)) (DESTRUCTURING-BIND (A B) (REST #:G786) (LIST # A B)))
Your my-defmacro works for you host CL system, but I get the feeling you want macro capabilities in your interpreter and this won't do it. (except if ythe interpreter environment is the global host implementations environment, but that would make a lot of challenges)
I don't know how you do compound procedures in your evaluator but when my evaluator gets a (lambda (x) (+ x x)) it is turned into (compound-procedure <env> (x) (+ x x)). My macros turn into almost the same except the first element is compound-syntax.
Every evaluated operator has a tag which tells it what it is (one of primitive-syntax, primitive-procedure, compound-syntax, compound-procedure) and I only need a general way of dealing with those 4.
The real difference between a compound procedure and compound syntax is that the arguments gets evaluated for a procedure and in a compound syntax the result gets evaluated.
So. Have you implemented so that ((lambda (x) (+ x x)) 5) works? Well, then you'll almost implemented macros as well. This is of course not true for a compiler, since this approach would expand the code every time it's run instead of expanding once when the closure gets created. (Optimizations is no way to go on the first version anyway)
For your 'edited code' you have a misplaced paren:
(defmacro my-defmacro ((name &rest args) &body body)
(let ((form (gensym))(env (gensym)))
`(progn (setf (macro-function ',name)
(lambda (,form ,env)) ;; <== HERE
(destructuring-bind ,args (rest, form) ,#body))
',name)
)
)
which leads to setf having three subforms. Rewrite it like this (while using standard Lisp formatting):
(defmacro my-defmacro ((name &rest args) &body body)
(let ((form (gensym))
(env (gensym)))
`(progn (setf (macro-function ',name)
(lambda (,form ,env)
(destructuring-bind ,args (rest, form)
,#body)))
',name)))

Switching buffers between header/implementation in emacs in obj-c (obj-c++) mode

How do I make emacs switch between buffers with header/implementation files in obj-c (c++) mode (that is, between foo.[mM]{1,2} and foo.[hH]{1,2})? Is it possible to assign standard Xcode keyboard shortcut (ctrl+cmd) for running such a macro?
Emacs has built-in support for jumping from header file to implementation file using ff-find-other-file:
(add-hook 'c-mode-common-hook
(lambda()
(local-set-key (kbd "C-c m d") 'ff-find-other-file)))
CC mode includes C, C++, Java, Objective C, etc.
Have a look at this link. He describes this function:
(defun objc-in-header-file ()
(let* ((filename (buffer-file-name))
(extension (car (last (split-string filename "\\.")))))
(string= "h" extension)))
(defun objc-jump-to-extension (extension)
(let* ((filename (buffer-file-name))
(file-components (append (butlast (split-string filename
"\\."))
(list extension))))
(find-file (mapconcat 'identity file-components "."))))
;;; Assumes that Header and Source file are in same directory
(defun objc-jump-between-header-source ()
(interactive)
(if (objc-in-header-file)
(objc-jump-to-extension "m")
(objc-jump-to-extension "h")))
(defun objc-mode-customizations ()
(define-key objc-mode-map (kbd "C-c t") 'objc-jump-between-header-source))
(add-hook 'objc-mode-hook 'objc-mode-customizations)
It's possible to extend the eassist-switch-h-cpp function from CEDET's contrib to work with ObjC files, in addition to C/C++ code. The change is simple, add following code to your init file:
(eval-after-load "eassist"
'(progn
(setq eassist-header-switches '(("h" . ("cpp" "cc" "c" "m"))
("hpp" . ("cpp" "cc"))
("cpp" . ("h" "hpp"))
("c" . ("h"))
("m" . ("h"))
("C" . ("H"))
("H" . ("C" "CPP" "CC"))
("cc" . ("h" "hpp"))))))
and you can call the eassist-swith-h-cpp function - it will handle switching between header and implementation files. And you may bound this function to selected key (C-c t in given hook):
(defun my/c-mode-cedet-hook ()
(local-set-key "\C-ct" 'eassist-switch-h-cpp))
(add-hook 'c-mode-common-hook 'my/c-mode-cedet-hook)

Xemacs with Windows Style Key Bindings

Is Xemacs available with Windows Style Key Bindings ?
Emacs has these Windows key bindings
The keybindings of Emacs predate
modern GUIs, and the keys that were
chosen by later GUIs for cut and copy
were given important functions as
extended keymaps in Emacs. CUA mode
attempts to let both bindings co-exist
by defining C-x and C-c as kill-region
and copy-region-as-kill when the
region is active, and letting them
have their normal Emacs bindings when
the region is not active. Many people
find this to be an acceptable
compromise. CUA mode also defines a
number of other keys (C-v, Shift
selection), and can be turned on from
the Options menu
If you could put up with emacs instead of Xemacs there is EmacsW32
it's plain emacs modified to integrate better with windows. It has a lot of features including a choice between emacs/win keybindings.
From webpage:
EmacsW32 is a collection of Emacs
lisp modules and MS Windows programs
you can use from Emacs. It can make
the keyboard and other things in Emacs
function more like they do usually in
MS Windows programs.
EmacsW32 is not Emacs for MS Windows.
Instead it is an add-on to Emacs for
MS Windows. You can however download
Emacs+EmacsW32 in one installer.
I'm not sure which Windows key bindings you are looking for, probably cut.copy,paste? That is called CUA and no it does not come with those key bindings by default, but they can be easily added.
Here's a link to a site that has a CUA-mode for xemacs. You should be able to install XEmacs, then add the CUA-mode, effectively creating what you are wanting.
http://sites.google.com/site/olegalexandrov/xemacs
Alternatively, you can add them yourself with a few lines of key assignments added to your init.el file. Try them first in a buffer with C-x C-e to run them and make sure they work.
I do not use the kill-ring and wanted to mark blocks a different way, so I wrote some functions in a file skm-mark-blocks.el which I will try to insert or attach here. At the end of the file you can see the global-set-key lines and use them as a template for making the Windows keys work the way they do in Windows.
-snip--------------------------------------
; skm-blocks.el
; Byrel and Steve Mitchell
; Nov 12, 2009
; mark blocks by:
; same key for marking first and second block end markers
; once both ends marked, keys to copy, cut, move, & delete block
;
; Goal:
; to do all block commands: mark, copy, move, del, etc. with the left hand
; while the right hand used for positioning in buffer with cursor keys
;
; first written mimicing Vedit+ method (for right hand)::
; F9 to mark first end,
; F9 to mark second end,
; then Cntl-F9 (copy to cursor) or Alt-F9 (move to cursor)
; Vedit+ uses the delete key while block highlighted to delete
block. Won't work here
; so we define a key with the same prefix (super) for deleting highlighted block
;
; possible improvements:
; add vars to choose how point (or cursor position or block markers) is moved when block
; is copied, etc.
; that is, do these things move with the block, stay where they were was,
; or move to end of new block, etc.
; add function to unmark all block ends?
; currently marking a "third" end unmarks the 2 previously
selected block ends
; and counts the third end as marking a new first end of block
; find a name for this kind of block marking other than my intials.
; add functions to do columnar block marking (rectangles), with new key combinations.
; add vars to config how columnar block marking should work, insert, overwrite, etc.
;
(defvar block-marker-highlight-mode 1
"block-marker-highlight-mode can have 3 values:
0 = highlighing is removed following a block copy or block move
1 = w/ a copy, orig block remains highlighted
w/ a move, block is highlighted at new position
2 = w/ copy or move, block is highlighted at new position" )
(defvar block-marker-end-position-mode t
"block-marker-end-position-mode has 2 values:
t = after a block copy/move, cursor is positioned at end of
inserted block
nil = after a block copy/move, cursor is positioned at beginning of inserted block
note: t is similar to the way Xemacs works by default")
(defvar block-mark1 (point-marker)) ;var to hold 1st end of our block
(defvar block-mark2 (point-marker)) ;var to hold 2nd end of our block
(defvar block-ends-marked 0) ;0 if no ends marked,
;1 or 2 for number of ends marked
(defvar block-copiedp nil) ;t if block copied
;-------- mark-block --------------------------
(defun mark-block ()
"Marks either end of block with skm type blocks."
(interactive)
(if ( or (eq block-ends-marked 0 ) (eq block-ends-marked 2))
;are we marking the first end of a block?
(progn
(setq block-mark1 (point-marker))
(setq block-ends-marked 1)
(clear-highlighting )
(set-mark-command nil)) ;starts highlighting
( if (eq block-ends-marked 1) ; if there is 1 block marker already, we are marking the second end.
(progn (setq block-mark2 (point-marker))
(setq block-ends-marked 2)
(highlight-region ) ))) )
;-------- copy-block-to-point -----------------
(defun copy-block-to-point ()
"Copies skm-marked block to cur. cursor pos. "
(interactive)
(let ((start-pos (point)))
(if ( < block-ends-marked 2)
(message "Both ends not marked: %d end(s) marked." block-ends-marked ) ;error if there aren't 2 ends marked
(save-excursion
(set-buffer (marker-buffer block-mark1))
(copy-to-register ?c block-mark1 block-mark2))
(insert-register ?c t)
(setq block-copiedp t)
(let ((end-pos (point)))
(if (eq block-marker-highlight-mode 0) ;0 = clear all highlighting
(clear-highlighting-at-point ( marker-buffer block-mark1) block-mark1)
(if (eq block-marker-highlight-mode 2 ) ;2 = highlight at new position
(progn
(clear-highlighting-at-point ( marker-buffer block-mark1) block-mark1)
(goto-char start-pos)
(push-mark)
(goto-char end-pos)
(highlight-region))))))
(if (not block-marker-end-position-mode) ;determine where to leave cursor
(goto-char start-pos)) ))
;-------- move-block-to-point -----------------
(defun move-block-to-point ()
"Moves skm-marked block to current cursor pos. "
(interactive)
(if ( < block-ends-marked 2)
(message "Both ends not marked: %d end(s) marked." block-ends-marked )
(save-excursion
(set-buffer (marker-buffer block-mark1))
(copy-to-register ?c block-mark1 block-mark2 t))
(let ((start-pos (point)) end-pos )
(insert-register ?c t)
(setq end-pos (point))
(setq block-copiedp t)
(clear-highlighting-at-point ( marker-buffer block-mark1) block-mark1)
(if (eq block-marker-highlight-mode 0) ;0 = clear all highlighting
nil
(goto-char start-pos)
(push-mark)
(goto-char end-pos)
(highlight-region))
(if (not block-marker-end-position-mode) ;determine where to leave cursor
(goto-char start-pos)) )))
;-------- cut-block ---------------------------
(defun cut-block ()
"Cuts skm-marked block from file."
(interactive)
(if ( < block-ends-marked 2)
(message "Both ends not marked: %d end(s) marked." block-ends-marked )
(copy-to-register ?c block-mark1 block-mark2 t)
(setq block-copiedp t)))
;------- highlight a block persistantly -----------
(defun highlight-region ()
(interactive)
(let (new-extent) (setq new-extent (make-extent (mark t)
(point)))
(set-extent-property new-extent 'face 'zmacs-region)
(set-extent-property new-extent 'wordstar-block t)))
;------- clear the highlighted blocks in a buffer -----------
(defun clear-highlighting-whole-buffer (&optional buffer)
(interactive)
(let ((highlighted-list (extent-list buffer nil nil nil 'face 'zmacs-region)))
(while highlighted-list
(delete-extent (car highlighted-list))
(setq highlighted-list (cdr highlighted-list)))))
(defun clear-highlighting-at-point (&optional buffer position)
(interactive)
(if (not position)
(setq position (point)))
(while (extent-at position buffer 'wordstar-block nil 'at )
(delete-extent (extent-at position buffer 'wordstar-block nil 'at )) ))
;------------key assignments ------------------
;--- key assignments to mimic VEdit+ method
;--- to verify we have algorithm correct
;--- comment out once they are not needed
(global-set-key [ f9 ] 'mark-block)
(global-set-key [ (control f9) ] 'copy-block-to-point)
(global-set-key [ (meta f9) ] 'move-block-to-point)
(global-set-key [ (control meta f9) ] 'cut-block) ;not in Vedit, but for testing
(global-set-key [ (control shift f9) ] 'clear-highlighting) ;not in Vedit, but for testing
;------- key assignments for left hand use
;--- using only super key (left windows-logo key) shifting
;Doesn't work with xemacs in Windows since Windows preempts the super key
; have to experiment to find something to work for windows'
xemacs...
(global-set-key [ (super space) ] 'mark-block)
(global-set-key [ (super v) ] 'copy-block-to-point) ;mnemonic: V for insert, drive a V (wedge) in
(global-set-key [ (super m) ] 'move-block-to-point) ;mnemonic: M for move
(global-set-key [ (super c) ] 'cut-block) ;mnemonic: C for Cut
(global-set-key [ (super n) ] 'clear-highlighting-at-point)
;mnemonic: think N for No highlighting
--snip-----------------
Hope this helps you see how easy it is to do.
After seeing how the code went into this message, it is obvious that I need some practice putting code into this editor (grin).