Emacs folding mode error - emacs

I want to be able to use the emacs folding mode provided by folding.el from http://www.emacswiki.org/emacs/FoldingMode
I put the following in my .emacs file:
(setq load-path (cons (concat (getenv "HOME") "/.emacs.d") load-path))
(load "folding")
(folding-mode-add-find-file-hook)
(folding-add-to-marks-list 'latex-mode "%{" "%}" nil t)
Then, when I select a region and run
M-x folding-fold-region
I get the error
Wrong type argument: char-or-string-p, nil

There are two problems :
you don't have to re-declare marks for latex-mode as this is already done in folder.el line 4411. Thus you should remove the line (folding-add-to-marks-list 'latex-mode "%{" "%}" nil t)
You get the error Wrong type argument: char-or-string-p, nil when the folder-mode is not enabled. Adding the line (folding-mode-add-find-file-hook) is not enough to open a file in folder-mode by default. To open in folder-mode, you should also place the folded-file local variable in the first line of the file you want to open, for example, in lisp :
;; -*- folded-file: t; -*-
With this local variable, and the (folding-mode-add-find-file-hook) command in your .emacs the folder-mode is enabled and you don't have problem anymore when calling folding-fold-region on a region.
Do a C-h f folding-mode-add-find-file-hook RET to have the explanation of this mechanism.

Related

emacs ERC commands conditional only if file exists

I have the following in my emacs init file:
(if (file-readable-p "~/.ercpass") (load "~/.ercpass"))
(setq erc-nickserv-passwords
`((freenode (("jacob" . ,freenode-nickone-pass)))))
Only if ~/.ercpass (its contents being (setq freenode-nickone-pass "mypassword")) is readable do I want the file to be loaded and the ERC password to used. If the file exists, everything works. But if it doesn't exist, the second line throws an error:
Symbol's value as variable is void: freenode-nickone-pass
How can I make the second line also conditional on whether ~/.ercpass is readable?
(if (file-readable-p "~/.ercpass")
(progn
(load "~/.ercpass")
(setq erc-nickserv-passwords
`((freenode (("jacob" . ,freenode-nickone-pass)))))))

How to program .emacs file to end up in a specific buffer?

I have this code in my .emacs file:
(load "ielm" nil t)
(switch-to-buffer "*ielm*")
(inferior-emacs-lisp-mode)
(set-buffer-modified-p nil)
In emacs 21 and earlier, I ended up with *ielm* as the current buffer, but starting with emacs 22 I end up with *GNU Emacs* as the current buffer. What changed in emacs 22 to cause the new behavior, and what can I do to automatically end up in the *ielm* buffer?
The Emacs manual, node Entering Emacs tells you:
You can also force Emacs to display a file or directory at startup by
setting the variable initial-buffer-choice to a string naming that
file or directory. The value of initial-buffer-choice may also be a
function (of no arguments) that should return a buffer which is then
displayed. If initial-buffer-choice is non-nil, then if you specify
any files on the command line, Emacs still visits them, but does not
display them initially.
You can find this node in the manual by using C-h r (open the manual) followed by i startup TAB (search the index for "startup"), and choose startup screen. (Other index choices will also take you there.)
Thanks, #Drew.
Adding the following to my .emacs file gives me the behavior I desire.
(if (= (length command-line-args) 1)
(setq initial-buffer-choice
(lambda () (get-buffer "*ielm*"))))

Configuring emacs for latex - void-variable LaTeX-mode-hook

Following this pdf document I added the following to my ~/.emacs file:
(load "auctex.el" nil t t)
(setq TeX-auto-save t)
(setq TeX-parse-self t)
(setq TeX-PDF-mode t) ;; .pdf statt .dvi per default:
;;Zeilenumbruch
;;(add-hook ’LaTeX-mode-hook ’turn-on-auto-fill)
;;Syntax Higlight
(add-hook ’LaTeX-mode-hook ’turn-on-font-lock)
;; Mathe Modus
(add-hook ’LaTeX-mode-hook ’LaTeX-math-mode)
;; Reftex einflechten und laden
(setq reftex-plug-into-AUCTeX t)
(add-hook ’LaTeX-mode-hook ’turn-on-reftex)
;; Satzende ". " statt ". ". " fuer M-k: loeschen bis Satzende usw.
;;(setq sentence-end "[.?!][]\"’)}]*\\($\\| \\| \\)[
;;]*") ;; Da ist ein "Newline in der Zeile!"
;;(setq sentence-end-double-space nil)
;;direkte Rechtschreib Korrektur:
;;(add-hook ’LaTeX-mode-hook ’flyspell-mode)
;; Nur benutzen falls Auctex > 11.81 mit preview-latex:
(load "preview-latex.el" nil t t)
;; aspell ist besser als ispell.
;; Zeile kommentieren, falls nicht installiert:
(setq-default ispell-program-name "aspell")
;; Deutsche Rechtschreibung falls \usepackage{ngerman}
;; oder german benutzt wird
(add-hook ’TeX-language-de-hook
(function (lambda () (ispell-change-dictionary "german8"))))
Unfortunately emacs doesn't start now, instead it gives the error
Warning (initialization): An error occurred while loading `/home/../.emacs':
Symbol's value as variable is void: ’LaTeX-mode-hook
To ensure normal operation, you should investigate and remove the
cause of the error in your initialization file. Start Emacs with
the `--debug-init' option to view a complete error backtrace.
When starting with --debug-init it gives the following information
Debugger entered--Lisp error: (void-variable ’LaTeX-mode-hook)
(add-hook ’LaTeX-mode-hook ’turn-on-font-lock)
eval-buffer(#<buffer *load*> nil "/home/../.emacs" nil t) ; Reading at buffer position 812
load-with-code-conversion("/home/../.emacs" "/home/../.emacs" t t)
load("~/.emacs" t t)
...
I tried using latex-mode-hook instead. I searched for a solution, but I seem to be the only one having exactly this problem.
I'm using Ubuntu 12.04 with the latest Emacs and Auctex. If required I'll post version information, but I rather think that something has to be added into the configuration or any package has to be installed instead.
How can I get emacs work with that configuration?
Are you using the wrong single quote character? it seems to be some kind of a backward quote instead of a plain single quote. Try
'LaTeX-mode-hook
instead of
’LaTeX-mode-hook
(and likewise for all other occurrences of that character).
As Thomas also said, the back quote is not the character you want to use there, it should be the single straight quote. But, in general, if you get "symbol's value as variable is void" error, it means the same as NPE (null pointer exception) in other languages. The way to check what went wrong is like so:
Move point to the variable that gives the problem and C-h v (or M-x describe-variable [name of the variable without quote]). You can use TAB to complete the variable name as you type to see if you by chance didn't mistype it.
Once you see the buffer that describes the variable - you know you've fixed the error.
Now, if you have LaTeX mode set through auctex.el, then latex-mode-hook must exist. However, you need to make sure that auctex.el actually loads and requires latex-mode. The way it does so isn't an idiomatic way for Emacs to do it, most of the time you add the source files for the mode to the load-path variable and then (require 'mode-name) or load the mode conditionally once Emacs opens the type of the file associated with it (makes startup time for Emacs shorter) through autoload as described here: Emacs: Best-practice for lazy loading modes in .emacs? .
However, whenever you see a variable called [something]-mode-hook it means that this is a list of functions you want to call when [something] mode loads up. If the [something] mode at all exists, there's a 99.99% chance that variable exists too (can't be void). So, if it is void - you need to make sure that the mode it belongs to at all loads.

Opening emacs without the scratch buffer when opening a file

I've changed a setting in Emacs. Now, whenever I try to open a file from the command line, it opens a *scratch* buffer on top of the file. Is there a way to get rid of this? Or a way to reset my emacs startup settings?
M-x customize-group
initialization
Then, on Initial Buffer Choice, you can select among:
Startup Screen
Directory
File
scratch buffer
Finally, click on save for future sessions.
You can also toggle it on/off.
See if this can help.
(The other thing you probably want to do is to open just a single buffer at start-up. I can't remember by heart how this is done. I'll post an update if I find it out).
You can reload your .emacs file with M-x load-file ~/.emacs. There are also other ways to do it, check out the question How can I load changes to my .emacs without rebooting Emacs?.
If you think you have a problem with your .emacs file, try opening using the -q option:
emacs -q somefile
If that works as expected, you probably have an error in your .emacs, and a good way to debug your .emacs is to use the option --debug-init:
emacs --debug-init
Which will tell Emacs to provide a stack trace of any error it encounters while loading your .emacs, something like:
Debugger entered--Lisp error: (wrong-type-argument symbolp (car n))
(setq (car n) 3)
(let ((n ...)) (setq (car n) 3))
eval-buffer(#<buffer *load*<2>> nil "/home/tjackson/.emacs.tjackson.el" nil t) ; Reading at buffer position 161460
load-with-code-conversion("/home/tjackson/.emacs.tjackson.el" "/home/tjackson/.emacs.tjackson.el" nil nil)
load("/home/tjackson/.emacs.tjackson.el")
(let ((debug-on-error t)) (load user-init-file))
(if init-file-debug (let (...) (load user-init-file)) (error (format "Problems while loading the file %s: %s" user-init-file ...)))
(condition-case err (load user-init-file) (error (if init-file-debug ... ...)))
(if (file-exists-p user-init-file) (condition-case err (load user-init-file) (error ...)))
eval-buffer(#<buffer *load*> nil "/home/tjackson/.emacs" nil t) ; Reading at buffer position 12150
load-with-code-conversion("/home/tjackson/.emacs" "/home/tjackson/.emacs" t t)
load("~/.emacs" t t)
#[nil "....."]
command-line()
normal-top-level()
And that generally can point you to what might be wrong. In my case above, I'm using setq improperly, and it looks like it's inside a let statement, which is inside the file /home/tjackson/.emacs.tjackson.el. A quick search in that file leads me to the error and I can fix it.
If you want to reset emacs settings, you could rename your current .emacs file to something else to have a backup - then relaunch Emacs.
My ~/.emacs.d folder was owned by root with restrictive r/w/x privileges, so I was unable to access as any non-root user.
Make sure your current user has read privileges for the emacs config files/directories.
This fixed it for me:
sudo chown -R user:group ~/.emacs.d/
The -R works recursively in the directory.
I removed my ~/.emacs.d directory and that fixed the situation for me.

Emacs custom command line argument

From the documentation I can see I can access command line arguments (command-line-args).
I'd like to add my own arguments but Emacs complains at start up that it doesn't recognize them.
E.g.
emacs -my_argument
I get:
command-line-1: Unknown option `-my_argument'
What's a proper way to define my custom arguments and provide information to my Emacs session?
Is there a way to pop an argument from a command line?
Add something like this to your ~/.emacs, ~/.emacs.el, or ~/.emacs.d/init.el file:
(defun my-argument-fn (switch)
(message "i was passed -my_argument"))
(add-to-list 'command-switch-alist '("-my_argument" . my-argument-fn))
Then you can execute emacs -my_argument and it should print i was passed -my_argument to the minibuffer. You can find more information in the GNU elisp reference.
As stated in another post you can add your custom switches to command-switch-alist and emacs will call the handler function for any matching switch passed in on the command line. However, this operation is done after your .emacs file has been evaluated. This is fine for most cases but you may wish for a command line argument to alter the execution path or behaviour of your .emacs evaluation; I often do this to enable/disable configuration chunks (mainly for debugging).
To achieve this you can read command-line-args and check for your switch manually and then delete it from the list, this will stop emacs complaining about an unknown argument.
(setq my-switch-found (member "-myswitch" command-line-args))
(setq command-line-args (delete "-myswitch" command-line-args))
Which can alter your .emacs evaluation like so:
(unless my-switch-found
(message "Didn't find inhibit switch, loading some config.")
...)
And you could build this into a single step:
;; This was written in SO text-box, not been tested.
(defun found-custom-arg (switch)
(let ((found-switch (member switch command-line-args)))
(setq command-line-args (delete switch command-line-args))
found-switch))
(unless (found-custom-arg "-myswitch")
(message "Loading config...")
...)
For those who are interested, here is a code snip to show how to process custom arguments in Emacs lisp. In this case, I am processing an argument --suffix / -S to variable _suffix.
I pulled the idea from a BSD-Lite Script Emacs script.
(setq _suffix nil)
;; Process cli args
(while command-line-args-left
(setq k (car command-line-args-left))
(setq command-line-args-left (cdr command-line-args-left))
(setq command-line-args (delete k command-line-args))
(cond
(or (string-equal k "--cs-suffix")
(string-equal k "-S"))
(setq _suffix (intern (car command-line-args-left)))
(setq command-line-args-left (cdr command-line-args-left))
(setq command-line-args (delete _suffix command-line-args))
)))
This will roll through command-line-args-left and remove them all from command-line-args which will prevent Emacs from complaining.