How to configure cleverly org-archive-location in org-mode - emacs

BACKGROUND: In org-mode, the variable org-archive-location is set to "%s_archive::" by default, so that a file "toto.org" archives into a file "toto.org_archive". I would like it to archive to "toto.ref" instead. I am using org-mode version 7.4 (out of the git server).
I would have thought it to be as simple as
(setq org-archive-location
`(replace-regexp-in-string ".org" ".ref" %s)
)
But I was pointed out that this was not proper in LISP (plus, it did not work). My final solution is as follow, you should be able to adapt to most clever configurations of org-archive-location:
(setq org-archive-location "%s::* ARCHIVES")
(defadvice org-extract-archive-file (after org-to-ref activate)
(setq ad-return-value
(replace-regexp-in-string "\\.org" ".ref" ad-return-value)
)
)
Note that:
1) I voluntarily did not add a $ at the end of ".org" so that it would properly alter "test.org.gpg" into "test.ref.gpg".
2) It seems that one should use the regular expression "\.org" (rather than, say, ".org") (longer explanation below in the answers).

You can't define a variable in Emacs such that its value is obtained by running code; variables have simple, static values.
You can achieve the effect you described by advising the function org-extract-archive-file, which is the one that generates an archive location from org-archive-location:
(defadvice org-extract-archive-file (after org-to-ref activate)
(setq ad-return-value
(replace-regexp-in-string "\\.org" ".ref" ad-return-value)))
This works for me now, but of course the internals of org-mode are subject to change and this solution may not work forever.

You should not quote an expression that you want to evaluate. Note also that in a regular expression, . matches any character.

Here is an example of how to set the file, the location (e.g., main heading) in the file, and whether or not to include additional archive information:
(let* (
(org-archive-location "~/todo.org::* TASKS")
(org-archive-save-context-info nil))
...)

You can try this: #+ARCHIVE: %s.ref:: at the beginning of your org file.
Read more about it here.
Also, other interesting option is to set inside your headtree the following, for instance:
* Main Header of tree
:PROPERTIES:
:ARCHIVE: toto.ref:: * Main Header of tree in archive file
:END:
** sub tree of main header and so on
The latter I took from this video.

Related

Secret structure in org-mode?

I'm wondering I if there's any functionality in org-mode that can make me able to operate with secret structure, that is: structure that I can see when I'm editing but that is treated as if it wasn't there when exporting. It's mainly importing when I export to ascii.
Example:
I would like this in the .org file:
* Normal heading
** Secret heading 1
Some text 1
** Secret heading 2
Some text 2
** Secret heading 3
Some text 3
To be exported to this:
Normal heading
--------------
Some text 1
Some text 2
Some text 3
What makes the headings secret can be anything like a tag, a property or something else but the secret headings should be foldable.
Edit:
Found this solution (from here) (I'm using org-mode 7.9.3 f. It doesn't work. Headlines with the :ignoreheading: tag are still displayed:
;; backend aware export preprocess hook
(defun sa-org-export-preprocess-hook ()
"My backend aware export preprocess hook."
(save-excursion
(when (eq org-export-current-backend 'latex)
;; ignoreheading tag for bibliographies and appendices
(let* ((tag "ignoreheading"))
(org-map-entries (lambda ()
(delete-region (point-at-bol) (point-at-eol)))
(concat ":" tag ":"))))))
(add-hook 'org-export-preprocess-hook 'sa-org-export-preprocess-hook)
You can use the EXCLUDE_TAGS property and tag certain sections, then export with org-export-exclude-tags. E.g:
#+EXCLUDE_TAGS: noexport
* Public Section
* Secret Section :noexport:
Documentation here.
What you want is addressed here -- and here's the answer (repeated):
Add the following to your .emacs file:
(require 'ox-extra)
(ox-extras-activate '(ignore-headlines))
Use the ignore tag on headlines you'd like to have ignored (while not ignoring their content)
I upgraded to org-mode 8.2.5h and with that this works:
(defun sa-ignore-headline (contents backend info)
"Ignore headlines with tag `ignoreheading'."
(when (and (org-export-derived-backend-p backend 'latex 'html 'ascii)
(string-match "\\`.*ignoreheading.*\n"
(downcase contents)))
(replace-match "" nil nil contents)))
(add-to-list 'org-export-filter-headline-functions 'sa-ignore-headline)
But only if you don't have the options: #+OPTIONS: tags:nil. Guess it's sort of obvious that tags shouldn't be filtered away before a filtering that relies on a certain tag is invoked - but that bugged me for quite some time.
Note: when exporting to ascii the headline underlining will remain without the headline, so you need this setting too:
(setq org-ascii-underline (quote ((ascii) (latin1) (utf-8))))
... to remove headlines all together.
In the linked question about the ignoreheading tag I posted a working, simpler org-export-before-parsing-hook solution for Org 8.2.10 in Emacs 24.1.1.
It is based on the documentation of the org-map-entries function which also states that it wraps it in save-recursion automatically. It is simpler than using concat because the second argument to org-map-entries is an agenda-style match string.
While trying to solve the same problem I found this thread describing how to extend ox-extra.el using a notignore tag. This method does not export any headings unless explicitly tagged notignore. Content of the heading are exported normally.
For most of my documents the notignore approach is more useful than the 'ignore' approach because the majority of headings are 'secret structure' not intended for export.
Presently I have notignore-headlines activated in my init.el file. Can anyone suggest a way to activate this on a per document basis.

How to configure Emacs to save backup for files under temp directory?

I use emacsclient to edit temp files in /tmp a lot and would like to create backup copies of my files automatically like we do with other files. I'm sure there is a way to do it - but how? :)
(I searched the Emacs manual, emacswiki and SO but couldn't find anything useful)
Look at the normal-backup-enable-predicate function, which is the default value for the backup-enable-predicate variable.
As the sole purpose of the default function is to inhibit backups for files in various temporary directories, you may just want to set a replacement which returns t unconditionally.
(setq backup-enable-predicate (lambda (name) t))
The usage in files.el suggests to me that you could also just set this variable to nil. That's not stated in the documentation, so it might not be reliable, but the variable isn't referenced by any other library in Emacs, so it's probably fine (but I'd still recommend using the lambda, because it's more obvious what that's doing).
See also C-hig (elisp) Making Backups RET
n.b. I'm not actually familiar with small-temporary-file-directory (see the docstring for that variable), but the temporary-file-directory value would typically be /tmp/, so those two cases are usually the same.
If you did want to retain the default behaviour for some temporary directories but not others, you should define a modified copy of the original function: (defun my-backup-enable-predicate ...) and then (setq backup-enable-predicate 'my-backup-enable-predicate)
Stick this in yer .emacs file:
;; create an invisible backup directory so our directories
;; look a bit cleaner
;; thanks to #emacs in irc.freenode.org, Ryan Barrett of snarfed.org
;; and freethegnu.wordpress.com
(defun make-backup-file-name (filename)
(defvar backups-dir "/tmp/")
(make-directory backups-dir t)
(expand-file-name
(concat backups-dir (file-name-nondirectory filename) "~")
(file-name-directory filename)))

org-mode export to Latex — suppress generation of labels

I'm using org-mode to write a report which I then export to LaTeX.
I have a few different .org files (one per chapter), which I export as "headless" LaTeX and then combine in a master .tex file.
This works nicely, except that the generated .tex files contain labels with conflicting numbers. So both a.tex and b.tex contain \label{sec-1}, for example.
As long as I never actually use these references then it's not much of a problem I think, although the warnings do annoy me. Is there any way to turn off the generation of these labels? It should be simple but I cannot find anything about this in the documentation.
I have written a bit of Lisp which will remove said labels after the export to LaTeX, which looks like this:
(defun remove-orgmode-latex-labels ()
"Remove labels generated by org-mode"
(interactive)
(let ((case-fold-search nil))
(goto-char 1)
(replace-regexp "\\\\label{sec-[0-9][^}]*}" "")
)
)
(add-hook 'org-export-latex-final-hook 'remove-orgmode-latex-labels)
This seems to do the job without removing my own custom labels.
Why not writing your full report as one big Org file?
Anyway, if your prefer having multiple smaller files, I would advice "including" them in one Org master file, as this:
* Chapter 1
#+INCLUDE: "chapter1.org"
* Chapter 2
#+INCLUDE: "chapter2.org"
That way, Org sees only one file (then, I guess that your problem simply disappears), while you edit them as you wish.
As of now (18. 05. 2021) correct solution is this:
(defun my-latex-filter-removeOrgAutoLabels (text backend info)
"Org-mode automatically generates labels for headings despite explicit use of `#+LABEL`. This filter forcibly removes all automatically generated org-labels in headings."
(when (org-export-derived-backend-p backend 'latex)
(replace-regexp-in-string "\\\\label{sec:org[a-f0-9]+}\n" "" text)))
(add-to-list 'org-export-filter-headline-functions
'my-latex-filter-removeOrgAutoLabels)
Only slight modification of previous answer.
This is what worked for me with recent (2020) Org-Mode:
(defun rm-org-latex-labels (text backend _info)
"Remove labels auto-generated by `org-mode' export to LaTeX."
(when (eq backend 'latex)
(replace-regexp-in-string "\\\\label{sec:org[a-f0-9]+}\n" "" text)))
(add-to-list #'org-export-filter-headline-functions
#'rm-org-latex-labels)

How to get Dired to ignore files with specific extensions

I put the following in my .emacs file:
(require 'dired-x)
(add-hook 'dired-load-hook '(lambda () (require 'dired-x)))
(setq dired-omit-files-p t)
(setq dired-omit-files
(concat dired-omit-files "\\|^\\..+$\\|-t\\.tex$\\|-t\\.pdf$"))
But C-x d still shows me .pdf and .tex files. Did I get the syntax wrong in that last line?
Bonus question: Is there a way to get Dired to hide hidden directories, like .git folders?
A simple and very general solution which doesn't rely on any extras is to do C-u s to change the ls flags and immediately refresh (that is, C-u s takes care of refreshing also, so there is very little typing involved). Usually you will want to remove -a to hide dotfiles. But you can do everything you're already able to do in the shell console, which is far more than what a simple toggle mode could offer (at the cost of some extra keypressings). And there is a history of previous flags available, so "toggling" is pretty fast too.
Your regexp will match *-t.tex files, not *.tex ones.
With recent version of Emacs, it should be sufficient to add the following section to ~/.emacs to filter what you want:
(require 'dired-x)
(setq-default dired-omit-files-p t) ; this is buffer-local variable
(setq dired-omit-files
(concat dired-omit-files "\\|^\\..+$\\|\\.pdf$\\|\\.tex$"))
Update: by default, dired-omit-files regexp filters out special directories . and ... If you don't want this behavior, you can just override defaults (instead of inheriting them with concat):
(setq dired-omit-files "^\\.[^.]\\|\\.pdf$\\|\\.tex$")
The regexp ^\\.[^.] will match any string of length 2+ starting with a dot where second character is any character except the dot itself. It's not perfect (will not match filenames like "..foo"), but should be ok most of the time.

How can I check if a file exists using Emacs Lisp?

I would like emacs to mark files that are generated as read-only when they're opened. The part of the puzzle that I'm missing is how to check if a file "exists". I currently have the following:
;;
;; get file extension
;;
(defun get-ext (file-name)
(car (cdr (split-string file-name "\\."))))
;;
;; get the base name of the file
;;
(defun base-name (file-name)
(car (split-string file-name "\\.")))
;;
;; if an 'lzz' file exists for this header, mark it as read only
;;
(defun mark-read-only ()
(if (string= (get-ext (cur-file)) "h")
(if ( ??file-exists??? (concat (base-name (cur-file)) ".lzz") )
(toggle-read-only))))
What can I use for "???file-exists???"?
Once I find this, I'll add "mark-read-only" to the appropriate hook (which I think is the find-file-hook).
BACKGROUND
We use lzz as a code generator to simplify our C/C++ development process. Briefly, lzz takes a single input file (which looks very like C/C++) and generates header and source files as appropriate.
By default, lzz includes #line directives so that the debugger points to the original source and not the generated source, however, to reduce compilation dependencies we normally disable these directives in header files. The result is that when debugging templates or inline functions, the debugger normally points to the generated header file and not the original source file.
This is not a big deal, however, recently I've found that when debugging I'll make a quick modification to the displayed file and then I'll rebuild. Of course this normally means that the change I made disappears because the file I edited is generated and so the changes are "blown away" during the library rebuild.
SOLUTION
Thanks to everyone for their help and comments. A special thanks to cobbal for pointing out the correct function to use.
Here's the resulting code (with updates based on the other comments here too):
(defun cur-file ()
"Return the filename (without directory) of the current buffer"
(file-name-nondirectory (buffer-file-name (current-buffer)))
)
(defun mark-generated-as-read-only ()
"Mark generated source files as read only.
Mark generated files (lzz or gz) read only to avoid accidental updates."
(if
(or (string= (file-name-extension (cur-file)) "h")
(string= (file-name-extension (cur-file)) "cpp"))
(cond
(
(file-exists-p (concat (file-name-sans-extension (cur-file)) ".lzz"))
(toggle-read-only))
(
(file-exists-p (concat (file-name-sans-extension (cur-file)) ".gz") )
(toggle-read-only))
)
)
)
try file-exists-p
"Return t if file filename exists (whether or not you can read it.)".
Note that it's not spesific to files and works for directories too.
Depending on what you need, you might want file-readable-p instead of file-exists-p.
Apropos will only get you so far. Icicles provides apropos completion and progressive completion which let you find help easily for command, function, variable, etc. names that match subparts in an arbitrary order (is it file-exists-p or exists-file-p?).
Use f.el, modern library for file and directory manipulation. You can use f-exists?, f-file?, f-directory? and many other predicates. The library is better than standard functions, because it's every file related function you'll ever need under one namespace.